Linux EFI development
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: linux-efi@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>
Subject: [PATCH] efi/zboot: Expose in-memory payload size in header
Date: Sun, 29 Nov 2026 14:45:56 +0100	[thread overview]
Message-ID: <20261129134555.1713362-2-ardb+git@google.com> (raw)

From: Ard Biesheuvel <ardb@kernel.org>

Record the uncompressed kernel payload's full in-memory size (_end - _text,
including .bss and any trailing uninitialized regions) as a 64-bit
little-endian integer in the two reserved words at byte offset 0x10 of
the EFI zboot header, and expose it to the zboot stub via .rodata so it
allocates the full in-memory footprint and zeroes the trailing
[payload_size, load_size) region after decompression.

This allows loaders and kexec implementations to determine the required
destination memory footprint directly from the outer zboot header before
decompressing the payload stream, and eliminates the build-time
copy_and_pad step that previously zero-padded vmlinux.bin to include
.bss.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/libstub/Makefile.zboot | 28 ++++++++-------------
 drivers/firmware/efi/libstub/zboot-header.S |  6 ++++-
 drivers/firmware/efi/libstub/zboot.c        | 15 ++++++++---
 drivers/firmware/efi/libstub/zboot.lds      |  4 ++-
 4 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 846ca323435a..f5dc07c5a65a 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -4,34 +4,28 @@
 # EFI_ZBOOT_PAYLOAD, EFI_ZBOOT_BFD_TARGET, EFI_ZBOOT_MACH_TYPE and
 # EFI_ZBOOT_FORWARD_CFI
 
-quiet_cmd_copy_and_pad = PAD     $@
-      cmd_copy_and_pad = cp $< $@; \
-			 truncate -s $$(hexdump -s16 -n4 -e '"%u"' $<) $@
-
-# Pad the file to the size of the uncompressed image in memory, including BSS
-$(obj)/vmlinux.bin: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
-	$(call if_changed,copy_and_pad)
-
 comp-type-y				:= gzip
 zboot-method-y				:= gzip
 
 comp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd
 zboot-method-$(CONFIG_KERNEL_ZSTD)	:= zstd22
 
-$(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
+$(obj)/vmlinuz: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
 	+$(call if_changed,$(zboot-method-y))
 
-# subtract the two symbol values to obtain the offset of struct efi_image_info
-get_image_info_offset = $(shell $(NM) -n $(objtree)/vmlinux |\
-			grep -E ' _text$$| __efistub_efistub_image_info$$' |\
-			awk 'NR == 1 { a = toupper($$1) } \
-			     NR == 2 { print "obase=16; ibase=16; " toupper($$1) "-" a }' |\
-			bc)
+# subtract two vmlinux symbol values to obtain a relative offset or size
+vmlinux_sym_diff = $(shell $(NM) -n $(objtree)/vmlinux |\
+		   grep -E ' $(1)$$| $(2)$$' |\
+		   awk 'NR == 1 { a = toupper($$1) } \
+			NR == 2 { print "obase=16; ibase=16; " toupper($$1) "-" a }' |\
+		   bc)
 
 # avoid eager evaluation to prevent references to non-existent build artifacts
 OBJCOPYFLAGS_vmlinuz.o = -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
 			 --rename-section .data=.gzdata,load,alloc,readonly,contents \
-			 --add-symbol efistub_image_info_offset=0x$(get_image_info_offset)
+			 --add-symbol efistub_image_info_offset=0x$(call \
+					vmlinux_sym_diff,_text,__efistub_efistub_image_info) \
+			 --add-symbol zboot_load_size=0x$(call vmlinux_sym_diff,_text,_end)
 
 $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
 	$(call if_changed,objcopy)
@@ -62,4 +56,4 @@ OBJCOPYFLAGS_vmlinuz.efi := -O binary
 $(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE
 	$(call if_changed,objcopy)
 
-targets += zboot-header.o vmlinux.bin vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
+targets += zboot-header.o vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
diff --git a/drivers/firmware/efi/libstub/zboot-header.S b/drivers/firmware/efi/libstub/zboot-header.S
index e8da65faf8b7..ba8af9251bf7 100644
--- a/drivers/firmware/efi/libstub/zboot-header.S
+++ b/drivers/firmware/efi/libstub/zboot-header.S
@@ -18,7 +18,11 @@ __efistub_efi_zboot_header:
 	.ascii		"zimg"					// image type
 	.long		__efistub__gzdata_start - .Ldoshdr	// payload offset
 	.long		__efistub__gzdata_size			// payload size
-	.long		0, 0					// reserved
+#ifdef CONFIG_64BIT
+	.quad		zboot_load_size				// load size
+#else
+	.long		zboot_load_size, 0x0			// load size
+#endif
 	.asciz		COMP_TYPE				// compression type
 	.org		.Ldoshdr + 0x38
 	.long		LINUX_PE_MAGIC
diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
index 4089a29068ca..fc37f843557d 100644
--- a/drivers/firmware/efi/libstub/zboot.c
+++ b/drivers/firmware/efi/libstub/zboot.c
@@ -48,9 +48,10 @@ static efi_status_t efi_zboot_remap_image(const struct efi_image_info *image_inf
 asmlinkage efi_status_t __efiapi
 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 {
+	extern const u64 zboot_load_size;
 	static efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID;
+	unsigned long image_base, payload_size, alloc_size;
 	char *cmdline_ptr __free(efi_pool) = NULL;
-	unsigned long image_base, alloc_size;
 	efi_loaded_image_t *image;
 	efi_status_t status;
 
@@ -69,9 +70,15 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 
 	efi_info("Decompressing Linux Kernel...\n");
 
-	status = efi_zboot_decompress_init(&alloc_size);
+	if (zboot_load_size != (unsigned long)zboot_load_size)
+		return EFI_LOAD_ERROR;
+	alloc_size = zboot_load_size;
+
+	status = efi_zboot_decompress_init(&payload_size);
 	if (status != EFI_SUCCESS)
 		return status;
+	if (payload_size > alloc_size)
+		return EFI_LOAD_ERROR;
 
 	 // If the architecture has a preferred address for the image,
 	 // try that first.
@@ -107,10 +114,12 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	}
 
 	// Decompress the payload into the newly allocated buffer
-	status = efi_zboot_decompress((void *)image_base, alloc_size);
+	status = efi_zboot_decompress((void *)image_base, payload_size);
 	if (status != EFI_SUCCESS)
 		goto error;
 
+	memset((void *)image_base + payload_size, 0, alloc_size - payload_size);
+
 	auto image_info = zboot_get_image_info(image_base);
 	status = efi_zboot_remap_image(image_info, image_base, alloc_size) ?:
 		 efi_stub_common(handle, image, image_info, cmdline_ptr);
diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
index 17d1735472d6..975db6253347 100644
--- a/drivers/firmware/efi/libstub/zboot.lds
+++ b/drivers/firmware/efi/libstub/zboot.lds
@@ -18,7 +18,9 @@ SECTIONS
 		__efistub__gzdata_end = .;
 		*(.rodata* .init.rodata* .srodata*)
 
-		. = ALIGN(4);
+		. = ALIGN(8);
+		__efistub_zboot_load_size = .;
+		QUAD(zboot_load_size);
 		__efistub_efistub_image_info_offset = .;
 		LONG(efistub_image_info_offset);
 
-- 
2.56.1.839.g22a8290db9-goog


                 reply	other threads:[~2026-11-29 13:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129134555.1713362-2-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=ardb@kernel.org \
    --cc=linux-efi@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox