* [PATCH] efi/zboot: Expose in-memory payload size in header
@ 2026-11-29 13:45 Ard Biesheuvel
0 siblings, 0 replies; only message in thread
From: Ard Biesheuvel @ 2026-11-29 13:45 UTC (permalink / raw)
To: linux-efi; +Cc: Ard Biesheuvel
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 ++++++++-------------
| 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
--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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-29 13:46 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 13:45 [PATCH] efi/zboot: Expose in-memory payload size in header Ard Biesheuvel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox