* [PATCH 1/2] scripts/Makefile.lib: Capture decompressed size in ZSTD22 metadata
2026-11-26 22:44 [PATCH 0/2] efi/zboot: Put ZSTD output size where it belongs Ard Biesheuvel
@ 2026-11-26 22:44 ` Ard Biesheuvel
2026-11-26 22:44 ` [PATCH 2/2] efi/zboot: Use ZSTD frame header for uncompressed payload size Ard Biesheuvel
1 sibling, 0 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2026-11-26 22:44 UTC (permalink / raw)
To: linux-efi; +Cc: Ard Biesheuvel, Nathan Chancellor, Nicolas Schier, linux-kbuild
Tweak the [unused] ZSTD22 compression rule so that the combined size of
the uncompressed input is recorded in the ZSTD metadata. This allows
ZSTD standard tooling to query it.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
scripts/Makefile.lib | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 19e96b30a122f..5b58577b158af 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -344,13 +344,17 @@ quiet_cmd_gzip = GZIP $@
# Bzip2 and LZMA do not include size in file... so we have to fake that;
# append the size as a 32-bit littleendian number as gzip does.
-size_append = printf $(shell \
+total_file_size = $(shell \
dec_size=0; \
for F in $(real-prereqs); do \
fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \
dec_size=$$(expr $$dec_size + $$fsize); \
done; \
-printf "%08x\n" $$dec_size | \
+printf "%d\n" $$dec_size \
+)
+
+size_append = printf $(shell \
+printf "%08x\n" $(total_file_size) | \
sed 's/\(..\)/\1 /g' | { \
read ch0 ch1 ch2 ch3; \
for ch in $$ch3 $$ch2 $$ch1 $$ch0; do \
@@ -479,7 +483,7 @@ quiet_cmd_zstd = ZSTD $@
cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
quiet_cmd_zstd22 = ZSTD22 $@
- cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
+ cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra --stream-size=$(total_file_size) > $@
quiet_cmd_zstd22_with_size = ZSTD22 $@
cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] efi/zboot: Use ZSTD frame header for uncompressed payload size
2026-11-26 22:44 [PATCH 0/2] efi/zboot: Put ZSTD output size where it belongs Ard Biesheuvel
2026-11-26 22:44 ` [PATCH 1/2] scripts/Makefile.lib: Capture decompressed size in ZSTD22 metadata Ard Biesheuvel
@ 2026-11-26 22:44 ` Ard Biesheuvel
1 sibling, 0 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2026-11-26 22:44 UTC (permalink / raw)
To: linux-efi; +Cc: Ard Biesheuvel, Nathan Chancellor, Nicolas Schier, linux-kbuild
Pass --stream-size to zstd in cmd_zstd22 so the uncompressed content
size is recorded in the ZSTD frame header even when compressing from a
pipe, switch EFI zboot from zstd22_with_size to zstd22, and use
zstd_get_frame_header() in the ZSTD decompressor to obtain the
uncompressed allocation size.
This avoids corrupting the embedded ZSTD stream with a 4-byte trailing
size word from the perspective of standard zstd tooling, removes the
ZBOOT_SIZE_LEN adjustment from the zboot header, and makes reading the
trailing 32-bit uncompressed size specific to the GZIP decompressor
(where ISIZE is part of the format).
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
drivers/firmware/efi/libstub/Makefile.zboot | 8 +-------
drivers/firmware/efi/libstub/zboot-decompress-gzip.c | 5 +++--
drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 17 ++++++++++++++---
| 2 +-
drivers/firmware/efi/libstub/zboot.lds | 1 -
5 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 52baacf3e9d94..1cd3a6ed7f223 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -12,16 +12,11 @@ quiet_cmd_copy_and_pad = PAD $@
$(obj)/vmlinux.bin: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
$(call if_changed,copy_and_pad)
-# in GZIP, the appended le32 carrying the uncompressed size is part of the
-# format, but in other cases, we just append it at the end for convenience,
-# causing the original tools to complain when checking image integrity.
comp-type-y := gzip
zboot-method-y := gzip
-zboot-size-len-y := 0
comp-type-$(CONFIG_KERNEL_ZSTD) := zstd
-zboot-method-$(CONFIG_KERNEL_ZSTD) := zstd22_with_size
-zboot-size-len-$(CONFIG_KERNEL_ZSTD) := 4
+zboot-method-$(CONFIG_KERNEL_ZSTD) := zstd22
$(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
+$(call if_changed,$(zboot-method-y))
@@ -37,7 +32,6 @@ aflags-zboot-header-$(EFI_ZBOOT_FORWARD_CFI) := \
AFLAGS_zboot-header.o += -DMACHINE_TYPE=IMAGE_FILE_MACHINE_$(EFI_ZBOOT_MACH_TYPE) \
-DZBOOT_EFI_PATH="\"$(abspath $(obj)/vmlinuz.efi.elf)\"" \
- -DZBOOT_SIZE_LEN=$(zboot-size-len-y) \
-DCOMP_TYPE="\"$(comp-type-y)\"" \
$(aflags-zboot-header-y)
diff --git a/drivers/firmware/efi/libstub/zboot-decompress-gzip.c b/drivers/firmware/efi/libstub/zboot-decompress-gzip.c
index e97a7e9d3c980..f508816ac26dc 100644
--- a/drivers/firmware/efi/libstub/zboot-decompress-gzip.c
+++ b/drivers/firmware/efi/libstub/zboot-decompress-gzip.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/efi.h>
+#include <linux/unaligned.h>
#include <linux/zlib.h>
#include <asm/efi.h>
@@ -12,7 +13,6 @@
#include "inflate.c"
extern unsigned char _gzdata_start[], _gzdata_end[];
-extern u32 __aligned(1) payload_size;
static struct z_stream_s stream;
@@ -38,7 +38,8 @@ efi_status_t efi_zboot_decompress_init(unsigned long *alloc_size)
goto out;
}
- *alloc_size = payload_size;
+ /* the last 4 bytes of a GZIP stream carry the uncompressed size */
+ *alloc_size = get_unaligned_le32(_gzdata_end - 4);
return EFI_SUCCESS;
out:
efi_free(zlib_inflate_workspacesize(), (unsigned long)stream.workspace);
diff --git a/drivers/firmware/efi/libstub/zboot-decompress-zstd.c b/drivers/firmware/efi/libstub/zboot-decompress-zstd.c
index bde9d94dd2e3a..8a55a09641370 100644
--- a/drivers/firmware/efi/libstub/zboot-decompress-zstd.c
+++ b/drivers/firmware/efi/libstub/zboot-decompress-zstd.c
@@ -9,21 +9,32 @@
#include "efistub.h"
extern unsigned char _gzdata_start[], _gzdata_end[];
-extern u32 __aligned(1) payload_size;
static size_t wksp_size;
static void *wksp;
efi_status_t efi_zboot_decompress_init(unsigned long *alloc_size)
{
+ zstd_frame_header header;
efi_status_t status;
+ size_t ret;
+
+ ret = zstd_get_frame_header(&header, _gzdata_start,
+ _gzdata_end - _gzdata_start);
+ if (ret != 0 ||
+ header.frameContentSize == 0 ||
+ header.frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN ||
+ header.frameContentSize != (unsigned long)header.frameContentSize) {
+ efi_err("ZSTD-compressed data has an invalid frame header\n");
+ return EFI_LOAD_ERROR;
+ }
wksp_size = zstd_dctx_workspace_bound();
status = efi_allocate_pages(wksp_size, (unsigned long *)&wksp, ULONG_MAX);
if (status != EFI_SUCCESS)
return status;
- *alloc_size = payload_size;
+ *alloc_size = header.frameContentSize;
return EFI_SUCCESS;
}
@@ -34,7 +45,7 @@ efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen)
int retval;
ret = zstd_decompress_dctx(dctx, out, outlen, _gzdata_start,
- _gzdata_end - _gzdata_start - 4);
+ _gzdata_end - _gzdata_start);
efi_free(wksp_size, (unsigned long)wksp);
retval = zstd_get_error_code(ret);
--git a/drivers/firmware/efi/libstub/zboot-header.S b/drivers/firmware/efi/libstub/zboot-header.S
index b6431edd0fc96..e8da65faf8b7b 100644
--- a/drivers/firmware/efi/libstub/zboot-header.S
+++ b/drivers/firmware/efi/libstub/zboot-header.S
@@ -17,7 +17,7 @@ __efistub_efi_zboot_header:
.long IMAGE_DOS_SIGNATURE
.ascii "zimg" // image type
.long __efistub__gzdata_start - .Ldoshdr // payload offset
- .long __efistub__gzdata_size - ZBOOT_SIZE_LEN // payload size
+ .long __efistub__gzdata_size // payload size
.long 0, 0 // reserved
.asciz COMP_TYPE // compression type
.org .Ldoshdr + 0x38
diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
index a785c01237497..d7633f5d61c3b 100644
--- a/drivers/firmware/efi/libstub/zboot.lds
+++ b/drivers/firmware/efi/libstub/zboot.lds
@@ -17,7 +17,6 @@ SECTIONS
.rodata : ALIGN(8) {
__efistub__gzdata_start = .;
*(.gzdata)
- __efistub_payload_size = . - 4;
__efistub__gzdata_end = .;
*(.rodata* .init.rodata* .srodata*)
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread