All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] arm64: Expose kernel code size to EFI zboot code
@ 2023-04-26 14:11 ` Ard Biesheuvel
  0 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-efi, mark.rutland, catalin.marinas, will, Ard Biesheuvel

The EFI zboot code needs access to the kernel code size, in order to be
able to determine which part of the image needs to be cleaned to the
PoU, and does not tolerate being mapped with non-executable permissions.

Instead of adding [0] this to the kernel image header, which makes it
ABI, let's use Kbuild rules to inject this quantity into the zboot
payload ELF object.

[0] https://lore.kernel.org/all/20230418134952.1170141-1-ardb@kernel.org/

Ard Biesheuvel (2):
  efi/zboot: arm64: Inject kernel code size symbol into the zboot
    payload
  efi/zboot: arm64: Grab kernel code size from zboot payload

 arch/arm64/boot/Makefile                    |  3 +++
 arch/arm64/kernel/image-vars.h              |  4 ++++
 drivers/firmware/efi/libstub/Makefile.zboot | 16 ++++------------
 drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
 drivers/firmware/efi/libstub/efistub.h      |  3 +--
 drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
 drivers/firmware/efi/libstub/zboot.lds      |  7 +++++++
 7 files changed, 36 insertions(+), 31 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 0/2] arm64: Expose kernel code size to EFI zboot code
@ 2023-04-26 14:11 ` Ard Biesheuvel
  0 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-efi, mark.rutland, catalin.marinas, will, Ard Biesheuvel

The EFI zboot code needs access to the kernel code size, in order to be
able to determine which part of the image needs to be cleaned to the
PoU, and does not tolerate being mapped with non-executable permissions.

Instead of adding [0] this to the kernel image header, which makes it
ABI, let's use Kbuild rules to inject this quantity into the zboot
payload ELF object.

[0] https://lore.kernel.org/all/20230418134952.1170141-1-ardb@kernel.org/

Ard Biesheuvel (2):
  efi/zboot: arm64: Inject kernel code size symbol into the zboot
    payload
  efi/zboot: arm64: Grab kernel code size from zboot payload

 arch/arm64/boot/Makefile                    |  3 +++
 arch/arm64/kernel/image-vars.h              |  4 ++++
 drivers/firmware/efi/libstub/Makefile.zboot | 16 ++++------------
 drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
 drivers/firmware/efi/libstub/efistub.h      |  3 +--
 drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
 drivers/firmware/efi/libstub/zboot.lds      |  7 +++++++
 7 files changed, 36 insertions(+), 31 deletions(-)

-- 
2.39.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/2] efi/zboot: arm64: Inject kernel code size symbol into the zboot payload
  2023-04-26 14:11 ` Ard Biesheuvel
@ 2023-04-26 14:11   ` Ard Biesheuvel
  -1 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-efi, mark.rutland, catalin.marinas, will, Ard Biesheuvel

The EFI zboot code is not built as part of the kernel proper, like the
ordinary EFI stub, but still needs access to symbols that are defined
only internally in the kernel, and are left unexposed deliberately to
avoid creating ABI inadvertently that we're stuck with later.

So capture the kernel code size of the kernel image, and inject it as an
ELF symbol into the object that contains the compressed payload, where
it will be accessible to zboot code that needs it.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/arm64/boot/Makefile                    | 3 +++
 arch/arm64/kernel/image-vars.h              | 4 ++++
 drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 7 +++++++
 4 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/boot/Makefile b/arch/arm64/boot/Makefile
index ae645fda90bca574..1761f5972443fc50 100644
--- a/arch/arm64/boot/Makefile
+++ b/arch/arm64/boot/Makefile
@@ -44,4 +44,7 @@ EFI_ZBOOT_BFD_TARGET	:= elf64-littleaarch64
 EFI_ZBOOT_MACH_TYPE	:= ARM64
 EFI_ZBOOT_FORWARD_CFI	:= $(CONFIG_ARM64_BTI_KERNEL)
 
+EFI_ZBOOT_OBJCOPY_FLAGS	= --add-symbol zboot_code_size=0x$(shell \
+				$(NM) vmlinux|grep _kernel_codesize|cut -d' ' -f1)
+
 include $(srctree)/drivers/firmware/efi/libstub/Makefile.zboot
diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h
index 8309197c0ebd4a8e..35f3c79595137354 100644
--- a/arch/arm64/kernel/image-vars.h
+++ b/arch/arm64/kernel/image-vars.h
@@ -108,4 +108,8 @@ KVM_NVHE_ALIAS(kvm_protected_mode_initialized);
 
 #endif /* CONFIG_KVM */
 
+#ifdef CONFIG_EFI_ZBOOT
+_kernel_codesize = ABSOLUTE(__inittext_end - _text);
+#endif
+
 #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 0a9dcc2b13736519..1b101d9076fd49e5 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -40,7 +40,7 @@ quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
 	$(call if_changed,compwithsize)
 
-OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
+OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
 			  --rename-section .data=.gzdata,load,alloc,readonly,contents
 $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
 	$(call if_changed,objcopy)
diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
index 93d33f68333b2b68..ac8c0ef851581f5d 100644
--- a/drivers/firmware/efi/libstub/zboot.lds
+++ b/drivers/firmware/efi/libstub/zboot.lds
@@ -2,6 +2,8 @@
 
 ENTRY(__efistub_efi_zboot_header);
 
+PROVIDE(zboot_code_size = ABSOLUTE(0));
+
 SECTIONS
 {
 	.head : ALIGN(4096) {
@@ -17,6 +19,11 @@ SECTIONS
 		*(.gzdata)
 		__efistub__gzdata_end = .;
 		*(.rodata* .init.rodata* .srodata*)
+
+		. = ALIGN(4);
+		__efistub_code_size = .;
+		LONG(zboot_code_size);
+
 		_etext = ALIGN(4096);
 		. = _etext;
 	}
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 1/2] efi/zboot: arm64: Inject kernel code size symbol into the zboot payload
@ 2023-04-26 14:11   ` Ard Biesheuvel
  0 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-efi, mark.rutland, catalin.marinas, will, Ard Biesheuvel

The EFI zboot code is not built as part of the kernel proper, like the
ordinary EFI stub, but still needs access to symbols that are defined
only internally in the kernel, and are left unexposed deliberately to
avoid creating ABI inadvertently that we're stuck with later.

So capture the kernel code size of the kernel image, and inject it as an
ELF symbol into the object that contains the compressed payload, where
it will be accessible to zboot code that needs it.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/arm64/boot/Makefile                    | 3 +++
 arch/arm64/kernel/image-vars.h              | 4 ++++
 drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
 drivers/firmware/efi/libstub/zboot.lds      | 7 +++++++
 4 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/boot/Makefile b/arch/arm64/boot/Makefile
index ae645fda90bca574..1761f5972443fc50 100644
--- a/arch/arm64/boot/Makefile
+++ b/arch/arm64/boot/Makefile
@@ -44,4 +44,7 @@ EFI_ZBOOT_BFD_TARGET	:= elf64-littleaarch64
 EFI_ZBOOT_MACH_TYPE	:= ARM64
 EFI_ZBOOT_FORWARD_CFI	:= $(CONFIG_ARM64_BTI_KERNEL)
 
+EFI_ZBOOT_OBJCOPY_FLAGS	= --add-symbol zboot_code_size=0x$(shell \
+				$(NM) vmlinux|grep _kernel_codesize|cut -d' ' -f1)
+
 include $(srctree)/drivers/firmware/efi/libstub/Makefile.zboot
diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h
index 8309197c0ebd4a8e..35f3c79595137354 100644
--- a/arch/arm64/kernel/image-vars.h
+++ b/arch/arm64/kernel/image-vars.h
@@ -108,4 +108,8 @@ KVM_NVHE_ALIAS(kvm_protected_mode_initialized);
 
 #endif /* CONFIG_KVM */
 
+#ifdef CONFIG_EFI_ZBOOT
+_kernel_codesize = ABSOLUTE(__inittext_end - _text);
+#endif
+
 #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 0a9dcc2b13736519..1b101d9076fd49e5 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -40,7 +40,7 @@ quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
 	$(call if_changed,compwithsize)
 
-OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
+OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
 			  --rename-section .data=.gzdata,load,alloc,readonly,contents
 $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
 	$(call if_changed,objcopy)
diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
index 93d33f68333b2b68..ac8c0ef851581f5d 100644
--- a/drivers/firmware/efi/libstub/zboot.lds
+++ b/drivers/firmware/efi/libstub/zboot.lds
@@ -2,6 +2,8 @@
 
 ENTRY(__efistub_efi_zboot_header);
 
+PROVIDE(zboot_code_size = ABSOLUTE(0));
+
 SECTIONS
 {
 	.head : ALIGN(4096) {
@@ -17,6 +19,11 @@ SECTIONS
 		*(.gzdata)
 		__efistub__gzdata_end = .;
 		*(.rodata* .init.rodata* .srodata*)
+
+		. = ALIGN(4);
+		__efistub_code_size = .;
+		LONG(zboot_code_size);
+
 		_etext = ALIGN(4096);
 		. = _etext;
 	}
-- 
2.39.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from zboot payload
  2023-04-26 14:11 ` Ard Biesheuvel
@ 2023-04-26 14:11   ` Ard Biesheuvel
  -1 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-efi, mark.rutland, catalin.marinas, will, Ard Biesheuvel

Instead of relying on a dodgy dd hack to copy the image code size from
the uncompressed image's PE header to the end of the compressed image,
let's grab the code size from the text_offset field of the arm64 image
header after decompression, which is where the arm64 specific EFI zboot
make rules will poke the code size when generating the zboot specific
version of the binary Image payload.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/libstub/Makefile.zboot | 14 +++-----------
 drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
 drivers/firmware/efi/libstub/efistub.h      |  3 +--
 drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
 4 files changed, 21 insertions(+), 30 deletions(-)

diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 1b101d9076fd49e5..89ef820f3b34483a 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -24,21 +24,13 @@ comp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd22
 # causing the original tools to complain when checking image integrity.
 # So disregard it when calculating the payload size in the zimage header.
 zboot-method-y                         := $(comp-type-y)_with_size
-zboot-size-len-y                       := 12
+zboot-size-len-y                       := 4
 
 zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
-zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
-
-# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
-# the compressed image. Note that this presupposes a PE header offset of 64
-# bytes, which is what arm64, RISC-V and LoongArch use.
-quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
-      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
-			   dd status=none if=$< bs=4 count=1 skip=37 ; \
-			   dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
+zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
 
 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
-	$(call if_changed,compwithsize)
+	$(call if_changed,$(zboot-method-y))
 
 OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
 			  --rename-section .data=.gzdata,load,alloc,readonly,contents
diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
index 8aad8c49d43f18e0..446e35eaf3d9434c 100644
--- a/drivers/firmware/efi/libstub/arm64.c
+++ b/drivers/firmware/efi/libstub/arm64.c
@@ -9,6 +9,7 @@
 
 #include <linux/efi.h>
 #include <asm/efi.h>
+#include <asm/image.h>
 #include <asm/memory.h>
 #include <asm/sysreg.h>
 
@@ -88,9 +89,10 @@ efi_status_t check_platform_features(void)
 #define DCTYPE	"cvau"
 #endif
 
+u32 __weak code_size;
+
 void efi_cache_sync_image(unsigned long image_base,
-			  unsigned long alloc_size,
-			  unsigned long code_size)
+			  unsigned long alloc_size)
 {
 	u32 ctr = read_cpuid_effective_cachetype();
 	u64 lsize = 4 << cpuid_feature_extract_unsigned_field(ctr,
@@ -98,16 +100,21 @@ void efi_cache_sync_image(unsigned long image_base,
 
 	/* only perform the cache maintenance if needed for I/D coherency */
 	if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
+		unsigned long base = image_base;
+		unsigned long size = code_size;
+
 		do {
-			asm("dc " DCTYPE ", %0" :: "r"(image_base));
-			image_base += lsize;
-			code_size -= lsize;
-		} while (code_size >= lsize);
+			asm("dc " DCTYPE ", %0" :: "r"(base));
+			base += lsize;
+			size -= lsize;
+		} while (size >= lsize);
 	}
 
 	asm("ic ialluis");
 	dsb(ish);
 	isb();
+
+	efi_remap_image(image_base, alloc_size, code_size);
 }
 
 unsigned long __weak primary_entry_offset(void)
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 148013bcb5f89fdd..67d5a20802e0b7c6 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -1066,8 +1066,7 @@ struct screen_info *__alloc_screen_info(void);
 void free_screen_info(struct screen_info *si);
 
 void efi_cache_sync_image(unsigned long image_base,
-			  unsigned long alloc_size,
-			  unsigned long code_size);
+			  unsigned long alloc_size);
 
 struct efi_smbios_record {
 	u8	type;
diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
index 63ece480090032c1..e5d7fa1f1d8fd160 100644
--- a/drivers/firmware/efi/libstub/zboot.c
+++ b/drivers/firmware/efi/libstub/zboot.c
@@ -50,8 +50,7 @@ static unsigned long alloc_preferred_address(unsigned long alloc_size)
 }
 
 void __weak efi_cache_sync_image(unsigned long image_base,
-				 unsigned long alloc_size,
-				 unsigned long code_size)
+				 unsigned long alloc_size)
 {
 	// Provided by the arch to perform the cache maintenance necessary for
 	// executable code loaded into memory to be safe for execution.
@@ -66,7 +65,7 @@ asmlinkage efi_status_t __efiapi
 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 {
 	unsigned long compressed_size = _gzdata_end - _gzdata_start;
-	unsigned long image_base, alloc_size, code_size;
+	unsigned long image_base, alloc_size;
 	efi_loaded_image_t *image;
 	efi_status_t status;
 	char *cmdline_ptr;
@@ -91,13 +90,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	efi_info("Decompressing Linux Kernel...\n");
 
 	// SizeOfImage from the compressee's PE/COFF header
-	alloc_size = round_up(get_unaligned_le32(_gzdata_end - 12),
+	alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4),
 			      EFI_ALLOC_ALIGN);
 
-	// SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header
-	code_size = get_unaligned_le32(_gzdata_end - 4) +
-		    get_unaligned_le32(_gzdata_end - 8);
-
 	 // If the architecture has a preferred address for the image,
 	 // try that first.
 	image_base = alloc_preferred_address(alloc_size);
@@ -140,9 +135,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 		goto free_image;
 	}
 
-	efi_cache_sync_image(image_base, alloc_size, code_size);
-
-	efi_remap_image(image_base, alloc_size, code_size);
+	efi_cache_sync_image(image_base, alloc_size);
 
 	status = efi_stub_common(handle, image, image_base, cmdline_ptr);
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from zboot payload
@ 2023-04-26 14:11   ` Ard Biesheuvel
  0 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: linux-efi, mark.rutland, catalin.marinas, will, Ard Biesheuvel

Instead of relying on a dodgy dd hack to copy the image code size from
the uncompressed image's PE header to the end of the compressed image,
let's grab the code size from the text_offset field of the arm64 image
header after decompression, which is where the arm64 specific EFI zboot
make rules will poke the code size when generating the zboot specific
version of the binary Image payload.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/libstub/Makefile.zboot | 14 +++-----------
 drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
 drivers/firmware/efi/libstub/efistub.h      |  3 +--
 drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
 4 files changed, 21 insertions(+), 30 deletions(-)

diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 1b101d9076fd49e5..89ef820f3b34483a 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -24,21 +24,13 @@ comp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd22
 # causing the original tools to complain when checking image integrity.
 # So disregard it when calculating the payload size in the zimage header.
 zboot-method-y                         := $(comp-type-y)_with_size
-zboot-size-len-y                       := 12
+zboot-size-len-y                       := 4
 
 zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
-zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
-
-# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
-# the compressed image. Note that this presupposes a PE header offset of 64
-# bytes, which is what arm64, RISC-V and LoongArch use.
-quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
-      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
-			   dd status=none if=$< bs=4 count=1 skip=37 ; \
-			   dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
+zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
 
 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
-	$(call if_changed,compwithsize)
+	$(call if_changed,$(zboot-method-y))
 
 OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
 			  --rename-section .data=.gzdata,load,alloc,readonly,contents
diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
index 8aad8c49d43f18e0..446e35eaf3d9434c 100644
--- a/drivers/firmware/efi/libstub/arm64.c
+++ b/drivers/firmware/efi/libstub/arm64.c
@@ -9,6 +9,7 @@
 
 #include <linux/efi.h>
 #include <asm/efi.h>
+#include <asm/image.h>
 #include <asm/memory.h>
 #include <asm/sysreg.h>
 
@@ -88,9 +89,10 @@ efi_status_t check_platform_features(void)
 #define DCTYPE	"cvau"
 #endif
 
+u32 __weak code_size;
+
 void efi_cache_sync_image(unsigned long image_base,
-			  unsigned long alloc_size,
-			  unsigned long code_size)
+			  unsigned long alloc_size)
 {
 	u32 ctr = read_cpuid_effective_cachetype();
 	u64 lsize = 4 << cpuid_feature_extract_unsigned_field(ctr,
@@ -98,16 +100,21 @@ void efi_cache_sync_image(unsigned long image_base,
 
 	/* only perform the cache maintenance if needed for I/D coherency */
 	if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
+		unsigned long base = image_base;
+		unsigned long size = code_size;
+
 		do {
-			asm("dc " DCTYPE ", %0" :: "r"(image_base));
-			image_base += lsize;
-			code_size -= lsize;
-		} while (code_size >= lsize);
+			asm("dc " DCTYPE ", %0" :: "r"(base));
+			base += lsize;
+			size -= lsize;
+		} while (size >= lsize);
 	}
 
 	asm("ic ialluis");
 	dsb(ish);
 	isb();
+
+	efi_remap_image(image_base, alloc_size, code_size);
 }
 
 unsigned long __weak primary_entry_offset(void)
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 148013bcb5f89fdd..67d5a20802e0b7c6 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -1066,8 +1066,7 @@ struct screen_info *__alloc_screen_info(void);
 void free_screen_info(struct screen_info *si);
 
 void efi_cache_sync_image(unsigned long image_base,
-			  unsigned long alloc_size,
-			  unsigned long code_size);
+			  unsigned long alloc_size);
 
 struct efi_smbios_record {
 	u8	type;
diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
index 63ece480090032c1..e5d7fa1f1d8fd160 100644
--- a/drivers/firmware/efi/libstub/zboot.c
+++ b/drivers/firmware/efi/libstub/zboot.c
@@ -50,8 +50,7 @@ static unsigned long alloc_preferred_address(unsigned long alloc_size)
 }
 
 void __weak efi_cache_sync_image(unsigned long image_base,
-				 unsigned long alloc_size,
-				 unsigned long code_size)
+				 unsigned long alloc_size)
 {
 	// Provided by the arch to perform the cache maintenance necessary for
 	// executable code loaded into memory to be safe for execution.
@@ -66,7 +65,7 @@ asmlinkage efi_status_t __efiapi
 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 {
 	unsigned long compressed_size = _gzdata_end - _gzdata_start;
-	unsigned long image_base, alloc_size, code_size;
+	unsigned long image_base, alloc_size;
 	efi_loaded_image_t *image;
 	efi_status_t status;
 	char *cmdline_ptr;
@@ -91,13 +90,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	efi_info("Decompressing Linux Kernel...\n");
 
 	// SizeOfImage from the compressee's PE/COFF header
-	alloc_size = round_up(get_unaligned_le32(_gzdata_end - 12),
+	alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4),
 			      EFI_ALLOC_ALIGN);
 
-	// SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header
-	code_size = get_unaligned_le32(_gzdata_end - 4) +
-		    get_unaligned_le32(_gzdata_end - 8);
-
 	 // If the architecture has a preferred address for the image,
 	 // try that first.
 	image_base = alloc_preferred_address(alloc_size);
@@ -140,9 +135,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 		goto free_image;
 	}
 
-	efi_cache_sync_image(image_base, alloc_size, code_size);
-
-	efi_remap_image(image_base, alloc_size, code_size);
+	efi_cache_sync_image(image_base, alloc_size);
 
 	status = efi_stub_common(handle, image, image_base, cmdline_ptr);
 
-- 
2.39.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from zboot payload
  2023-04-26 14:11   ` Ard Biesheuvel
@ 2023-04-26 14:20     ` Ard Biesheuvel
  -1 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:20 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: linux-efi, mark.rutland, catalin.marinas, will

On Wed, 26 Apr 2023 at 15:11, Ard Biesheuvel <ardb@kernel.org> wrote:
>
> Instead of relying on a dodgy dd hack to copy the image code size from
> the uncompressed image's PE header to the end of the compressed image,


> let's grab the code size from the text_offset field of the arm64 image
> header after decompression, which is where the arm64 specific EFI zboot
> make rules will poke the code size when generating the zboot specific
> version of the binary Image payload.
>

Apologies, I failed to update the above when I changed the actual
patch. It should read:

"let's grab the code size from the symbol that is injected into the
ELF by the Kbuild rules that generate the compressed payload".


> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
>  drivers/firmware/efi/libstub/Makefile.zboot | 14 +++-----------
>  drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
>  drivers/firmware/efi/libstub/efistub.h      |  3 +--
>  drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
>  4 files changed, 21 insertions(+), 30 deletions(-)
>
> diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> index 1b101d9076fd49e5..89ef820f3b34483a 100644
> --- a/drivers/firmware/efi/libstub/Makefile.zboot
> +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> @@ -24,21 +24,13 @@ comp-type-$(CONFIG_KERNEL_ZSTD)             := zstd22
>  # causing the original tools to complain when checking image integrity.
>  # So disregard it when calculating the payload size in the zimage header.
>  zboot-method-y                         := $(comp-type-y)_with_size
> -zboot-size-len-y                       := 12
> +zboot-size-len-y                       := 4
>
>  zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
> -zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
> -
> -# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
> -# the compressed image. Note that this presupposes a PE header offset of 64
> -# bytes, which is what arm64, RISC-V and LoongArch use.
> -quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
> -      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
> -                          dd status=none if=$< bs=4 count=1 skip=37 ; \
> -                          dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
> +zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
>
>  $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
> -       $(call if_changed,compwithsize)
> +       $(call if_changed,$(zboot-method-y))
>
>  OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
>                           --rename-section .data=.gzdata,load,alloc,readonly,contents
> diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
> index 8aad8c49d43f18e0..446e35eaf3d9434c 100644
> --- a/drivers/firmware/efi/libstub/arm64.c
> +++ b/drivers/firmware/efi/libstub/arm64.c
> @@ -9,6 +9,7 @@
>
>  #include <linux/efi.h>
>  #include <asm/efi.h>
> +#include <asm/image.h>
>  #include <asm/memory.h>
>  #include <asm/sysreg.h>
>
> @@ -88,9 +89,10 @@ efi_status_t check_platform_features(void)
>  #define DCTYPE "cvau"
>  #endif
>
> +u32 __weak code_size;
> +
>  void efi_cache_sync_image(unsigned long image_base,
> -                         unsigned long alloc_size,
> -                         unsigned long code_size)
> +                         unsigned long alloc_size)
>  {
>         u32 ctr = read_cpuid_effective_cachetype();
>         u64 lsize = 4 << cpuid_feature_extract_unsigned_field(ctr,
> @@ -98,16 +100,21 @@ void efi_cache_sync_image(unsigned long image_base,
>
>         /* only perform the cache maintenance if needed for I/D coherency */
>         if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
> +               unsigned long base = image_base;
> +               unsigned long size = code_size;
> +
>                 do {
> -                       asm("dc " DCTYPE ", %0" :: "r"(image_base));
> -                       image_base += lsize;
> -                       code_size -= lsize;
> -               } while (code_size >= lsize);
> +                       asm("dc " DCTYPE ", %0" :: "r"(base));
> +                       base += lsize;
> +                       size -= lsize;
> +               } while (size >= lsize);
>         }
>
>         asm("ic ialluis");
>         dsb(ish);
>         isb();
> +
> +       efi_remap_image(image_base, alloc_size, code_size);
>  }
>
>  unsigned long __weak primary_entry_offset(void)
> diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
> index 148013bcb5f89fdd..67d5a20802e0b7c6 100644
> --- a/drivers/firmware/efi/libstub/efistub.h
> +++ b/drivers/firmware/efi/libstub/efistub.h
> @@ -1066,8 +1066,7 @@ struct screen_info *__alloc_screen_info(void);
>  void free_screen_info(struct screen_info *si);
>
>  void efi_cache_sync_image(unsigned long image_base,
> -                         unsigned long alloc_size,
> -                         unsigned long code_size);
> +                         unsigned long alloc_size);
>
>  struct efi_smbios_record {
>         u8      type;
> diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
> index 63ece480090032c1..e5d7fa1f1d8fd160 100644
> --- a/drivers/firmware/efi/libstub/zboot.c
> +++ b/drivers/firmware/efi/libstub/zboot.c
> @@ -50,8 +50,7 @@ static unsigned long alloc_preferred_address(unsigned long alloc_size)
>  }
>
>  void __weak efi_cache_sync_image(unsigned long image_base,
> -                                unsigned long alloc_size,
> -                                unsigned long code_size)
> +                                unsigned long alloc_size)
>  {
>         // Provided by the arch to perform the cache maintenance necessary for
>         // executable code loaded into memory to be safe for execution.
> @@ -66,7 +65,7 @@ asmlinkage efi_status_t __efiapi
>  efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
>  {
>         unsigned long compressed_size = _gzdata_end - _gzdata_start;
> -       unsigned long image_base, alloc_size, code_size;
> +       unsigned long image_base, alloc_size;
>         efi_loaded_image_t *image;
>         efi_status_t status;
>         char *cmdline_ptr;
> @@ -91,13 +90,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
>         efi_info("Decompressing Linux Kernel...\n");
>
>         // SizeOfImage from the compressee's PE/COFF header
> -       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 12),
> +       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4),
>                               EFI_ALLOC_ALIGN);
>
> -       // SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header
> -       code_size = get_unaligned_le32(_gzdata_end - 4) +
> -                   get_unaligned_le32(_gzdata_end - 8);
> -
>          // If the architecture has a preferred address for the image,
>          // try that first.
>         image_base = alloc_preferred_address(alloc_size);
> @@ -140,9 +135,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
>                 goto free_image;
>         }
>
> -       efi_cache_sync_image(image_base, alloc_size, code_size);
> -
> -       efi_remap_image(image_base, alloc_size, code_size);
> +       efi_cache_sync_image(image_base, alloc_size);
>
>         status = efi_stub_common(handle, image, image_base, cmdline_ptr);
>
> --
> 2.39.2
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from zboot payload
@ 2023-04-26 14:20     ` Ard Biesheuvel
  0 siblings, 0 replies; 12+ messages in thread
From: Ard Biesheuvel @ 2023-04-26 14:20 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: linux-efi, mark.rutland, catalin.marinas, will

On Wed, 26 Apr 2023 at 15:11, Ard Biesheuvel <ardb@kernel.org> wrote:
>
> Instead of relying on a dodgy dd hack to copy the image code size from
> the uncompressed image's PE header to the end of the compressed image,


> let's grab the code size from the text_offset field of the arm64 image
> header after decompression, which is where the arm64 specific EFI zboot
> make rules will poke the code size when generating the zboot specific
> version of the binary Image payload.
>

Apologies, I failed to update the above when I changed the actual
patch. It should read:

"let's grab the code size from the symbol that is injected into the
ELF by the Kbuild rules that generate the compressed payload".


> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
>  drivers/firmware/efi/libstub/Makefile.zboot | 14 +++-----------
>  drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
>  drivers/firmware/efi/libstub/efistub.h      |  3 +--
>  drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
>  4 files changed, 21 insertions(+), 30 deletions(-)
>
> diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> index 1b101d9076fd49e5..89ef820f3b34483a 100644
> --- a/drivers/firmware/efi/libstub/Makefile.zboot
> +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> @@ -24,21 +24,13 @@ comp-type-$(CONFIG_KERNEL_ZSTD)             := zstd22
>  # causing the original tools to complain when checking image integrity.
>  # So disregard it when calculating the payload size in the zimage header.
>  zboot-method-y                         := $(comp-type-y)_with_size
> -zboot-size-len-y                       := 12
> +zboot-size-len-y                       := 4
>
>  zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
> -zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
> -
> -# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
> -# the compressed image. Note that this presupposes a PE header offset of 64
> -# bytes, which is what arm64, RISC-V and LoongArch use.
> -quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
> -      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
> -                          dd status=none if=$< bs=4 count=1 skip=37 ; \
> -                          dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
> +zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
>
>  $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
> -       $(call if_changed,compwithsize)
> +       $(call if_changed,$(zboot-method-y))
>
>  OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
>                           --rename-section .data=.gzdata,load,alloc,readonly,contents
> diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
> index 8aad8c49d43f18e0..446e35eaf3d9434c 100644
> --- a/drivers/firmware/efi/libstub/arm64.c
> +++ b/drivers/firmware/efi/libstub/arm64.c
> @@ -9,6 +9,7 @@
>
>  #include <linux/efi.h>
>  #include <asm/efi.h>
> +#include <asm/image.h>
>  #include <asm/memory.h>
>  #include <asm/sysreg.h>
>
> @@ -88,9 +89,10 @@ efi_status_t check_platform_features(void)
>  #define DCTYPE "cvau"
>  #endif
>
> +u32 __weak code_size;
> +
>  void efi_cache_sync_image(unsigned long image_base,
> -                         unsigned long alloc_size,
> -                         unsigned long code_size)
> +                         unsigned long alloc_size)
>  {
>         u32 ctr = read_cpuid_effective_cachetype();
>         u64 lsize = 4 << cpuid_feature_extract_unsigned_field(ctr,
> @@ -98,16 +100,21 @@ void efi_cache_sync_image(unsigned long image_base,
>
>         /* only perform the cache maintenance if needed for I/D coherency */
>         if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
> +               unsigned long base = image_base;
> +               unsigned long size = code_size;
> +
>                 do {
> -                       asm("dc " DCTYPE ", %0" :: "r"(image_base));
> -                       image_base += lsize;
> -                       code_size -= lsize;
> -               } while (code_size >= lsize);
> +                       asm("dc " DCTYPE ", %0" :: "r"(base));
> +                       base += lsize;
> +                       size -= lsize;
> +               } while (size >= lsize);
>         }
>
>         asm("ic ialluis");
>         dsb(ish);
>         isb();
> +
> +       efi_remap_image(image_base, alloc_size, code_size);
>  }
>
>  unsigned long __weak primary_entry_offset(void)
> diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
> index 148013bcb5f89fdd..67d5a20802e0b7c6 100644
> --- a/drivers/firmware/efi/libstub/efistub.h
> +++ b/drivers/firmware/efi/libstub/efistub.h
> @@ -1066,8 +1066,7 @@ struct screen_info *__alloc_screen_info(void);
>  void free_screen_info(struct screen_info *si);
>
>  void efi_cache_sync_image(unsigned long image_base,
> -                         unsigned long alloc_size,
> -                         unsigned long code_size);
> +                         unsigned long alloc_size);
>
>  struct efi_smbios_record {
>         u8      type;
> diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
> index 63ece480090032c1..e5d7fa1f1d8fd160 100644
> --- a/drivers/firmware/efi/libstub/zboot.c
> +++ b/drivers/firmware/efi/libstub/zboot.c
> @@ -50,8 +50,7 @@ static unsigned long alloc_preferred_address(unsigned long alloc_size)
>  }
>
>  void __weak efi_cache_sync_image(unsigned long image_base,
> -                                unsigned long alloc_size,
> -                                unsigned long code_size)
> +                                unsigned long alloc_size)
>  {
>         // Provided by the arch to perform the cache maintenance necessary for
>         // executable code loaded into memory to be safe for execution.
> @@ -66,7 +65,7 @@ asmlinkage efi_status_t __efiapi
>  efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
>  {
>         unsigned long compressed_size = _gzdata_end - _gzdata_start;
> -       unsigned long image_base, alloc_size, code_size;
> +       unsigned long image_base, alloc_size;
>         efi_loaded_image_t *image;
>         efi_status_t status;
>         char *cmdline_ptr;
> @@ -91,13 +90,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
>         efi_info("Decompressing Linux Kernel...\n");
>
>         // SizeOfImage from the compressee's PE/COFF header
> -       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 12),
> +       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4),
>                               EFI_ALLOC_ALIGN);
>
> -       // SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header
> -       code_size = get_unaligned_le32(_gzdata_end - 4) +
> -                   get_unaligned_le32(_gzdata_end - 8);
> -
>          // If the architecture has a preferred address for the image,
>          // try that first.
>         image_base = alloc_preferred_address(alloc_size);
> @@ -140,9 +135,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
>                 goto free_image;
>         }
>
> -       efi_cache_sync_image(image_base, alloc_size, code_size);
> -
> -       efi_remap_image(image_base, alloc_size, code_size);
> +       efi_cache_sync_image(image_base, alloc_size);
>
>         status = efi_stub_common(handle, image, image_base, cmdline_ptr);
>
> --
> 2.39.2
>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from zboot payload
  2023-04-26 14:20     ` Ard Biesheuvel
@ 2023-04-26 15:57       ` Mark Rutland
  -1 siblings, 0 replies; 12+ messages in thread
From: Mark Rutland @ 2023-04-26 15:57 UTC (permalink / raw)
  To: Ard Biesheuvel; +Cc: linux-arm-kernel, linux-efi, catalin.marinas, will

On Wed, Apr 26, 2023 at 03:20:43PM +0100, Ard Biesheuvel wrote:
> On Wed, 26 Apr 2023 at 15:11, Ard Biesheuvel <ardb@kernel.org> wrote:
> >
> > Instead of relying on a dodgy dd hack to copy the image code size from
> > the uncompressed image's PE header to the end of the compressed image,
> 
> 
> > let's grab the code size from the text_offset field of the arm64 image
> > header after decompression, which is where the arm64 specific EFI zboot
> > make rules will poke the code size when generating the zboot specific
> > version of the binary Image payload.
> >
> 
> Apologies, I failed to update the above when I changed the actual
> patch. It should read:
> 
> "let's grab the code size from the symbol that is injected into the
> ELF by the Kbuild rules that generate the compressed payload".

With the corrected wording:

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> 
> 
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > ---
> >  drivers/firmware/efi/libstub/Makefile.zboot | 14 +++-----------
> >  drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
> >  drivers/firmware/efi/libstub/efistub.h      |  3 +--
> >  drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
> >  4 files changed, 21 insertions(+), 30 deletions(-)
> >
> > diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> > index 1b101d9076fd49e5..89ef820f3b34483a 100644
> > --- a/drivers/firmware/efi/libstub/Makefile.zboot
> > +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> > @@ -24,21 +24,13 @@ comp-type-$(CONFIG_KERNEL_ZSTD)             := zstd22
> >  # causing the original tools to complain when checking image integrity.
> >  # So disregard it when calculating the payload size in the zimage header.
> >  zboot-method-y                         := $(comp-type-y)_with_size
> > -zboot-size-len-y                       := 12
> > +zboot-size-len-y                       := 4
> >
> >  zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
> > -zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
> > -
> > -# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
> > -# the compressed image. Note that this presupposes a PE header offset of 64
> > -# bytes, which is what arm64, RISC-V and LoongArch use.
> > -quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
> > -      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
> > -                          dd status=none if=$< bs=4 count=1 skip=37 ; \
> > -                          dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
> > +zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
> >
> >  $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
> > -       $(call if_changed,compwithsize)
> > +       $(call if_changed,$(zboot-method-y))
> >
> >  OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
> >                           --rename-section .data=.gzdata,load,alloc,readonly,contents
> > diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
> > index 8aad8c49d43f18e0..446e35eaf3d9434c 100644
> > --- a/drivers/firmware/efi/libstub/arm64.c
> > +++ b/drivers/firmware/efi/libstub/arm64.c
> > @@ -9,6 +9,7 @@
> >
> >  #include <linux/efi.h>
> >  #include <asm/efi.h>
> > +#include <asm/image.h>
> >  #include <asm/memory.h>
> >  #include <asm/sysreg.h>
> >
> > @@ -88,9 +89,10 @@ efi_status_t check_platform_features(void)
> >  #define DCTYPE "cvau"
> >  #endif
> >
> > +u32 __weak code_size;
> > +
> >  void efi_cache_sync_image(unsigned long image_base,
> > -                         unsigned long alloc_size,
> > -                         unsigned long code_size)
> > +                         unsigned long alloc_size)
> >  {
> >         u32 ctr = read_cpuid_effective_cachetype();
> >         u64 lsize = 4 << cpuid_feature_extract_unsigned_field(ctr,
> > @@ -98,16 +100,21 @@ void efi_cache_sync_image(unsigned long image_base,
> >
> >         /* only perform the cache maintenance if needed for I/D coherency */
> >         if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
> > +               unsigned long base = image_base;
> > +               unsigned long size = code_size;
> > +
> >                 do {
> > -                       asm("dc " DCTYPE ", %0" :: "r"(image_base));
> > -                       image_base += lsize;
> > -                       code_size -= lsize;
> > -               } while (code_size >= lsize);
> > +                       asm("dc " DCTYPE ", %0" :: "r"(base));
> > +                       base += lsize;
> > +                       size -= lsize;
> > +               } while (size >= lsize);
> >         }
> >
> >         asm("ic ialluis");
> >         dsb(ish);
> >         isb();
> > +
> > +       efi_remap_image(image_base, alloc_size, code_size);
> >  }
> >
> >  unsigned long __weak primary_entry_offset(void)
> > diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
> > index 148013bcb5f89fdd..67d5a20802e0b7c6 100644
> > --- a/drivers/firmware/efi/libstub/efistub.h
> > +++ b/drivers/firmware/efi/libstub/efistub.h
> > @@ -1066,8 +1066,7 @@ struct screen_info *__alloc_screen_info(void);
> >  void free_screen_info(struct screen_info *si);
> >
> >  void efi_cache_sync_image(unsigned long image_base,
> > -                         unsigned long alloc_size,
> > -                         unsigned long code_size);
> > +                         unsigned long alloc_size);
> >
> >  struct efi_smbios_record {
> >         u8      type;
> > diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
> > index 63ece480090032c1..e5d7fa1f1d8fd160 100644
> > --- a/drivers/firmware/efi/libstub/zboot.c
> > +++ b/drivers/firmware/efi/libstub/zboot.c
> > @@ -50,8 +50,7 @@ static unsigned long alloc_preferred_address(unsigned long alloc_size)
> >  }
> >
> >  void __weak efi_cache_sync_image(unsigned long image_base,
> > -                                unsigned long alloc_size,
> > -                                unsigned long code_size)
> > +                                unsigned long alloc_size)
> >  {
> >         // Provided by the arch to perform the cache maintenance necessary for
> >         // executable code loaded into memory to be safe for execution.
> > @@ -66,7 +65,7 @@ asmlinkage efi_status_t __efiapi
> >  efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
> >  {
> >         unsigned long compressed_size = _gzdata_end - _gzdata_start;
> > -       unsigned long image_base, alloc_size, code_size;
> > +       unsigned long image_base, alloc_size;
> >         efi_loaded_image_t *image;
> >         efi_status_t status;
> >         char *cmdline_ptr;
> > @@ -91,13 +90,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
> >         efi_info("Decompressing Linux Kernel...\n");
> >
> >         // SizeOfImage from the compressee's PE/COFF header
> > -       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 12),
> > +       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4),
> >                               EFI_ALLOC_ALIGN);
> >
> > -       // SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header
> > -       code_size = get_unaligned_le32(_gzdata_end - 4) +
> > -                   get_unaligned_le32(_gzdata_end - 8);
> > -
> >          // If the architecture has a preferred address for the image,
> >          // try that first.
> >         image_base = alloc_preferred_address(alloc_size);
> > @@ -140,9 +135,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
> >                 goto free_image;
> >         }
> >
> > -       efi_cache_sync_image(image_base, alloc_size, code_size);
> > -
> > -       efi_remap_image(image_base, alloc_size, code_size);
> > +       efi_cache_sync_image(image_base, alloc_size);
> >
> >         status = efi_stub_common(handle, image, image_base, cmdline_ptr);
> >
> > --
> > 2.39.2
> >

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from zboot payload
@ 2023-04-26 15:57       ` Mark Rutland
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Rutland @ 2023-04-26 15:57 UTC (permalink / raw)
  To: Ard Biesheuvel; +Cc: linux-arm-kernel, linux-efi, catalin.marinas, will

On Wed, Apr 26, 2023 at 03:20:43PM +0100, Ard Biesheuvel wrote:
> On Wed, 26 Apr 2023 at 15:11, Ard Biesheuvel <ardb@kernel.org> wrote:
> >
> > Instead of relying on a dodgy dd hack to copy the image code size from
> > the uncompressed image's PE header to the end of the compressed image,
> 
> 
> > let's grab the code size from the text_offset field of the arm64 image
> > header after decompression, which is where the arm64 specific EFI zboot
> > make rules will poke the code size when generating the zboot specific
> > version of the binary Image payload.
> >
> 
> Apologies, I failed to update the above when I changed the actual
> patch. It should read:
> 
> "let's grab the code size from the symbol that is injected into the
> ELF by the Kbuild rules that generate the compressed payload".

With the corrected wording:

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> 
> 
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > ---
> >  drivers/firmware/efi/libstub/Makefile.zboot | 14 +++-----------
> >  drivers/firmware/efi/libstub/arm64.c        | 19 +++++++++++++------
> >  drivers/firmware/efi/libstub/efistub.h      |  3 +--
> >  drivers/firmware/efi/libstub/zboot.c        | 15 ++++-----------
> >  4 files changed, 21 insertions(+), 30 deletions(-)
> >
> > diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> > index 1b101d9076fd49e5..89ef820f3b34483a 100644
> > --- a/drivers/firmware/efi/libstub/Makefile.zboot
> > +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> > @@ -24,21 +24,13 @@ comp-type-$(CONFIG_KERNEL_ZSTD)             := zstd22
> >  # causing the original tools to complain when checking image integrity.
> >  # So disregard it when calculating the payload size in the zimage header.
> >  zboot-method-y                         := $(comp-type-y)_with_size
> > -zboot-size-len-y                       := 12
> > +zboot-size-len-y                       := 4
> >
> >  zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
> > -zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
> > -
> > -# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
> > -# the compressed image. Note that this presupposes a PE header offset of 64
> > -# bytes, which is what arm64, RISC-V and LoongArch use.
> > -quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
> > -      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
> > -                          dd status=none if=$< bs=4 count=1 skip=37 ; \
> > -                          dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
> > +zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
> >
> >  $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
> > -       $(call if_changed,compwithsize)
> > +       $(call if_changed,$(zboot-method-y))
> >
> >  OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
> >                           --rename-section .data=.gzdata,load,alloc,readonly,contents
> > diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
> > index 8aad8c49d43f18e0..446e35eaf3d9434c 100644
> > --- a/drivers/firmware/efi/libstub/arm64.c
> > +++ b/drivers/firmware/efi/libstub/arm64.c
> > @@ -9,6 +9,7 @@
> >
> >  #include <linux/efi.h>
> >  #include <asm/efi.h>
> > +#include <asm/image.h>
> >  #include <asm/memory.h>
> >  #include <asm/sysreg.h>
> >
> > @@ -88,9 +89,10 @@ efi_status_t check_platform_features(void)
> >  #define DCTYPE "cvau"
> >  #endif
> >
> > +u32 __weak code_size;
> > +
> >  void efi_cache_sync_image(unsigned long image_base,
> > -                         unsigned long alloc_size,
> > -                         unsigned long code_size)
> > +                         unsigned long alloc_size)
> >  {
> >         u32 ctr = read_cpuid_effective_cachetype();
> >         u64 lsize = 4 << cpuid_feature_extract_unsigned_field(ctr,
> > @@ -98,16 +100,21 @@ void efi_cache_sync_image(unsigned long image_base,
> >
> >         /* only perform the cache maintenance if needed for I/D coherency */
> >         if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
> > +               unsigned long base = image_base;
> > +               unsigned long size = code_size;
> > +
> >                 do {
> > -                       asm("dc " DCTYPE ", %0" :: "r"(image_base));
> > -                       image_base += lsize;
> > -                       code_size -= lsize;
> > -               } while (code_size >= lsize);
> > +                       asm("dc " DCTYPE ", %0" :: "r"(base));
> > +                       base += lsize;
> > +                       size -= lsize;
> > +               } while (size >= lsize);
> >         }
> >
> >         asm("ic ialluis");
> >         dsb(ish);
> >         isb();
> > +
> > +       efi_remap_image(image_base, alloc_size, code_size);
> >  }
> >
> >  unsigned long __weak primary_entry_offset(void)
> > diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
> > index 148013bcb5f89fdd..67d5a20802e0b7c6 100644
> > --- a/drivers/firmware/efi/libstub/efistub.h
> > +++ b/drivers/firmware/efi/libstub/efistub.h
> > @@ -1066,8 +1066,7 @@ struct screen_info *__alloc_screen_info(void);
> >  void free_screen_info(struct screen_info *si);
> >
> >  void efi_cache_sync_image(unsigned long image_base,
> > -                         unsigned long alloc_size,
> > -                         unsigned long code_size);
> > +                         unsigned long alloc_size);
> >
> >  struct efi_smbios_record {
> >         u8      type;
> > diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
> > index 63ece480090032c1..e5d7fa1f1d8fd160 100644
> > --- a/drivers/firmware/efi/libstub/zboot.c
> > +++ b/drivers/firmware/efi/libstub/zboot.c
> > @@ -50,8 +50,7 @@ static unsigned long alloc_preferred_address(unsigned long alloc_size)
> >  }
> >
> >  void __weak efi_cache_sync_image(unsigned long image_base,
> > -                                unsigned long alloc_size,
> > -                                unsigned long code_size)
> > +                                unsigned long alloc_size)
> >  {
> >         // Provided by the arch to perform the cache maintenance necessary for
> >         // executable code loaded into memory to be safe for execution.
> > @@ -66,7 +65,7 @@ asmlinkage efi_status_t __efiapi
> >  efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
> >  {
> >         unsigned long compressed_size = _gzdata_end - _gzdata_start;
> > -       unsigned long image_base, alloc_size, code_size;
> > +       unsigned long image_base, alloc_size;
> >         efi_loaded_image_t *image;
> >         efi_status_t status;
> >         char *cmdline_ptr;
> > @@ -91,13 +90,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
> >         efi_info("Decompressing Linux Kernel...\n");
> >
> >         // SizeOfImage from the compressee's PE/COFF header
> > -       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 12),
> > +       alloc_size = round_up(get_unaligned_le32(_gzdata_end - 4),
> >                               EFI_ALLOC_ALIGN);
> >
> > -       // SizeOfHeaders and SizeOfCode from the compressee's PE/COFF header
> > -       code_size = get_unaligned_le32(_gzdata_end - 4) +
> > -                   get_unaligned_le32(_gzdata_end - 8);
> > -
> >          // If the architecture has a preferred address for the image,
> >          // try that first.
> >         image_base = alloc_preferred_address(alloc_size);
> > @@ -140,9 +135,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
> >                 goto free_image;
> >         }
> >
> > -       efi_cache_sync_image(image_base, alloc_size, code_size);
> > -
> > -       efi_remap_image(image_base, alloc_size, code_size);
> > +       efi_cache_sync_image(image_base, alloc_size);
> >
> >         status = efi_stub_common(handle, image, image_base, cmdline_ptr);
> >
> > --
> > 2.39.2
> >

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] efi/zboot: arm64: Inject kernel code size symbol into the zboot payload
  2023-04-26 14:11   ` Ard Biesheuvel
@ 2023-04-26 15:58     ` Mark Rutland
  -1 siblings, 0 replies; 12+ messages in thread
From: Mark Rutland @ 2023-04-26 15:58 UTC (permalink / raw)
  To: Ard Biesheuvel; +Cc: linux-arm-kernel, linux-efi, catalin.marinas, will

On Wed, Apr 26, 2023 at 04:11:02PM +0200, Ard Biesheuvel wrote:
> The EFI zboot code is not built as part of the kernel proper, like the
> ordinary EFI stub, but still needs access to symbols that are defined
> only internally in the kernel, and are left unexposed deliberately to
> avoid creating ABI inadvertently that we're stuck with later.
> 
> So capture the kernel code size of the kernel image, and inject it as an
> ELF symbol into the object that contains the compressed payload, where
> it will be accessible to zboot code that needs it.
> 
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> ---
>  arch/arm64/boot/Makefile                    | 3 +++
>  arch/arm64/kernel/image-vars.h              | 4 ++++
>  drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
>  drivers/firmware/efi/libstub/zboot.lds      | 7 +++++++
>  4 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/boot/Makefile b/arch/arm64/boot/Makefile
> index ae645fda90bca574..1761f5972443fc50 100644
> --- a/arch/arm64/boot/Makefile
> +++ b/arch/arm64/boot/Makefile
> @@ -44,4 +44,7 @@ EFI_ZBOOT_BFD_TARGET	:= elf64-littleaarch64
>  EFI_ZBOOT_MACH_TYPE	:= ARM64
>  EFI_ZBOOT_FORWARD_CFI	:= $(CONFIG_ARM64_BTI_KERNEL)
>  
> +EFI_ZBOOT_OBJCOPY_FLAGS	= --add-symbol zboot_code_size=0x$(shell \
> +				$(NM) vmlinux|grep _kernel_codesize|cut -d' ' -f1)
> +
>  include $(srctree)/drivers/firmware/efi/libstub/Makefile.zboot
> diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h
> index 8309197c0ebd4a8e..35f3c79595137354 100644
> --- a/arch/arm64/kernel/image-vars.h
> +++ b/arch/arm64/kernel/image-vars.h
> @@ -108,4 +108,8 @@ KVM_NVHE_ALIAS(kvm_protected_mode_initialized);
>  
>  #endif /* CONFIG_KVM */
>  
> +#ifdef CONFIG_EFI_ZBOOT
> +_kernel_codesize = ABSOLUTE(__inittext_end - _text);
> +#endif
> +
>  #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
> diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> index 0a9dcc2b13736519..1b101d9076fd49e5 100644
> --- a/drivers/firmware/efi/libstub/Makefile.zboot
> +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> @@ -40,7 +40,7 @@ quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
>  $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
>  	$(call if_changed,compwithsize)
>  
> -OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
> +OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
>  			  --rename-section .data=.gzdata,load,alloc,readonly,contents
>  $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
>  	$(call if_changed,objcopy)
> diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
> index 93d33f68333b2b68..ac8c0ef851581f5d 100644
> --- a/drivers/firmware/efi/libstub/zboot.lds
> +++ b/drivers/firmware/efi/libstub/zboot.lds
> @@ -2,6 +2,8 @@
>  
>  ENTRY(__efistub_efi_zboot_header);
>  
> +PROVIDE(zboot_code_size = ABSOLUTE(0));
> +
>  SECTIONS
>  {
>  	.head : ALIGN(4096) {
> @@ -17,6 +19,11 @@ SECTIONS
>  		*(.gzdata)
>  		__efistub__gzdata_end = .;
>  		*(.rodata* .init.rodata* .srodata*)
> +
> +		. = ALIGN(4);
> +		__efistub_code_size = .;
> +		LONG(zboot_code_size);
> +
>  		_etext = ALIGN(4096);
>  		. = _etext;
>  	}
> -- 
> 2.39.2
> 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/2] efi/zboot: arm64: Inject kernel code size symbol into the zboot payload
@ 2023-04-26 15:58     ` Mark Rutland
  0 siblings, 0 replies; 12+ messages in thread
From: Mark Rutland @ 2023-04-26 15:58 UTC (permalink / raw)
  To: Ard Biesheuvel; +Cc: linux-arm-kernel, linux-efi, catalin.marinas, will

On Wed, Apr 26, 2023 at 04:11:02PM +0200, Ard Biesheuvel wrote:
> The EFI zboot code is not built as part of the kernel proper, like the
> ordinary EFI stub, but still needs access to symbols that are defined
> only internally in the kernel, and are left unexposed deliberately to
> avoid creating ABI inadvertently that we're stuck with later.
> 
> So capture the kernel code size of the kernel image, and inject it as an
> ELF symbol into the object that contains the compressed payload, where
> it will be accessible to zboot code that needs it.
> 
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> ---
>  arch/arm64/boot/Makefile                    | 3 +++
>  arch/arm64/kernel/image-vars.h              | 4 ++++
>  drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
>  drivers/firmware/efi/libstub/zboot.lds      | 7 +++++++
>  4 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/boot/Makefile b/arch/arm64/boot/Makefile
> index ae645fda90bca574..1761f5972443fc50 100644
> --- a/arch/arm64/boot/Makefile
> +++ b/arch/arm64/boot/Makefile
> @@ -44,4 +44,7 @@ EFI_ZBOOT_BFD_TARGET	:= elf64-littleaarch64
>  EFI_ZBOOT_MACH_TYPE	:= ARM64
>  EFI_ZBOOT_FORWARD_CFI	:= $(CONFIG_ARM64_BTI_KERNEL)
>  
> +EFI_ZBOOT_OBJCOPY_FLAGS	= --add-symbol zboot_code_size=0x$(shell \
> +				$(NM) vmlinux|grep _kernel_codesize|cut -d' ' -f1)
> +
>  include $(srctree)/drivers/firmware/efi/libstub/Makefile.zboot
> diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h
> index 8309197c0ebd4a8e..35f3c79595137354 100644
> --- a/arch/arm64/kernel/image-vars.h
> +++ b/arch/arm64/kernel/image-vars.h
> @@ -108,4 +108,8 @@ KVM_NVHE_ALIAS(kvm_protected_mode_initialized);
>  
>  #endif /* CONFIG_KVM */
>  
> +#ifdef CONFIG_EFI_ZBOOT
> +_kernel_codesize = ABSOLUTE(__inittext_end - _text);
> +#endif
> +
>  #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
> diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> index 0a9dcc2b13736519..1b101d9076fd49e5 100644
> --- a/drivers/firmware/efi/libstub/Makefile.zboot
> +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> @@ -40,7 +40,7 @@ quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
>  $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
>  	$(call if_changed,compwithsize)
>  
> -OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
> +OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
>  			  --rename-section .data=.gzdata,load,alloc,readonly,contents
>  $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
>  	$(call if_changed,objcopy)
> diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
> index 93d33f68333b2b68..ac8c0ef851581f5d 100644
> --- a/drivers/firmware/efi/libstub/zboot.lds
> +++ b/drivers/firmware/efi/libstub/zboot.lds
> @@ -2,6 +2,8 @@
>  
>  ENTRY(__efistub_efi_zboot_header);
>  
> +PROVIDE(zboot_code_size = ABSOLUTE(0));
> +
>  SECTIONS
>  {
>  	.head : ALIGN(4096) {
> @@ -17,6 +19,11 @@ SECTIONS
>  		*(.gzdata)
>  		__efistub__gzdata_end = .;
>  		*(.rodata* .init.rodata* .srodata*)
> +
> +		. = ALIGN(4);
> +		__efistub_code_size = .;
> +		LONG(zboot_code_size);
> +
>  		_etext = ALIGN(4096);
>  		. = _etext;
>  	}
> -- 
> 2.39.2
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-04-26 15:59 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-26 14:11 [PATCH 0/2] arm64: Expose kernel code size to EFI zboot code Ard Biesheuvel
2023-04-26 14:11 ` Ard Biesheuvel
2023-04-26 14:11 ` [PATCH 1/2] efi/zboot: arm64: Inject kernel code size symbol into the zboot payload Ard Biesheuvel
2023-04-26 14:11   ` Ard Biesheuvel
2023-04-26 15:58   ` Mark Rutland
2023-04-26 15:58     ` Mark Rutland
2023-04-26 14:11 ` [PATCH 2/2] efi/zboot: arm64: Grab kernel code size from " Ard Biesheuvel
2023-04-26 14:11   ` Ard Biesheuvel
2023-04-26 14:20   ` Ard Biesheuvel
2023-04-26 14:20     ` Ard Biesheuvel
2023-04-26 15:57     ` Mark Rutland
2023-04-26 15:57       ` Mark Rutland

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.