Skip to contentSkip to conversation

arm64,riscv: boot: emit standard LZ4 frame for Image.lz4

PATCH Superseded Ping Li 2 participants 10 messages Nov 24 – Nov 26

Subsystems (3)
lore
Share & tools
lore

Ping Li

author
#0 lore raw

cover arm64,riscv: boot: emit standard LZ4 frame for Image.lz4

The shared cmd_lz4 rule in scripts/Makefile.lib passes -l to the lz4 tool, producing the legacy LZ4 format (magic 0x184C2102). The only in-kernel lz4 decompressor, lib/decompress_unlz4, understands exactly this format, and all of its users - the self-extracting images of arm, mips, parisc, s390 and x86, and the lz4-compressed initramfs - keep working through the *_with_size variants and plain cmd_lz4.

arm64 and riscv also build Image.lz4 with the plain cmd_lz4, but a compressed boot image on these two architectures is not decompressed by the kernel itself - it is handed to the bootloader. U-Boot, which implements this via the 'booti' compressed-kernel path (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn), only accepts the standard LZ4 frame (magic 0x184D2204) and returns -EPROTONOSUPPORT for anything else. U-Boot's own host tooling (binman) likewise emits standard frames.

So the Image.lz4 targets of both architectures currently produce images that no mainstream bootloader can decompress. Fix both by emitting the standard frame through local rules, leaving the shared cmd_lz4 untouched for the in-kernel users.

Bootloaders that decompress the legacy format do exist in the wild (mkbootimg on Android offers a separate lz4_legacy option), but U-Boot and its tooling only ever supported the standard frame.

Testing:
- Verified that the lz4 invocation used by the new rules emits the standard frame magic (04 22 4d 18) and round-trips with the lz4 CLI, while the old rule emits the legacy magic (02 21 4c 18).
- No full kernel build environment was available for either arch here; the rule changes are mechanical. Boot testing on real hardware, especially on riscv, would be appreciated.

Related: an arm64 'make install' fix that recognises all compressed image formats is under review at
https://lore.kernel.org/all/[email protected]/

Ping Li (2):
arm64: boot: emit standard LZ4 frame for Image.lz4
riscv: boot: emit standard LZ4 frame for Image.lz4

 arch/arm64/boot/Makefile | 9 ++++++++-
 arch/riscv/boot/Makefile | 9 ++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 arm64: boot: emit standard LZ4 frame for Image.lz4

The shared cmd_lz4 rule passes -l to the lz4 tool, producing the legacy LZ4 format (magic 0x184C2102). That is the format understood by the kernel's own decompressor, lib/decompress_unlz4, whose users are the self-extracting images of other architectures (all of which compress their payloads with the *_with_size variants) and the lz4-compressed initramfs. An arm64 boot image is not consumed by the kernel itself though: it is decompressed by the bootloader.

U-Boot, which supports booting compressed arm64 kernels with 'booti' (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn), only accepts the standard LZ4 frame (magic 0x184D2204) and returns -EPROTONOSUPPORT for anything else. Its host tooling (binman) likewise emits standard frames. As a result, an arm64 Image.lz4 built today cannot be decompressed by U-Boot at all.

Emit the standard LZ4 frame through a local rule, leaving the shared cmd_lz4 untouched for the in-kernel users.

---
 arch/arm64/boot/Makefile | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/arm64/boot/Makefile +8 -1
index d42d07667822..19b26aa83203 100644
@@ -28,8 +28,15 @@ $(obj)/Image.bz2: $(obj)/Image FORCE
 $(obj)/Image.gz: $(obj)/Image FORCE
 	+$(call if_changed,gzip)
 
+# The shared cmd_lz4 passes -l, producing the legacy LZ4 format that
+# matches the kernel's own decompressor.  Image.lz4 is decompressed by
+# the bootloader though, and U-Boot only supports the standard LZ4 frame
+# format, so use a local rule that omits -l.
+quiet_cmd_lz4_std = LZ4     $@
+      cmd_lz4_std = cat $(real-prereqs) | $(LZ4) -9 - - > $@
+
 $(obj)/Image.lz4: $(obj)/Image FORCE
-	$(call if_changed,lz4)
+	$(call if_changed,lz4_std)
 
 $(obj)/Image.lzma: $(obj)/Image FORCE
 	$(call if_changed,lzma)
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 riscv: boot: emit standard LZ4 frame for Image.lz4

In reply to Ping Li

Like arm64, a compressed riscv boot image is decompressed by the bootloader, not by the kernel itself. The shared cmd_lz4 rule passes -l, producing the legacy LZ4 format (magic 0x184C2102) that only the kernel's own decompressor understands, so U-Boot's 'booti' compressed-kernel path (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn), which only accepts the standard LZ4 frame (magic 0x184D2204), rejects the Image.lz4 built today.

This matters in particular when CONFIG_KERNEL_LZ4 selects Image.lz4 as the default boot image via boot-image-$(CONFIG_KERNEL_LZ4) in arch/riscv/Makefile.

Emit the standard LZ4 frame through a local rule, leaving the shared cmd_lz4 untouched for the in-kernel users (the lz4-compressed initramfs and the self-extracting images of other architectures).

---
 arch/riscv/boot/Makefile | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/riscv/boot/Makefile +8 -1
index b80a077a8e8f..0e7607d7e39f 100644
@@ -33,8 +33,15 @@ $(obj)/loader: $(obj)/loader.o $(obj)/Image $(obj)/loader.lds FORCE
 $(obj)/Image.bz2: $(obj)/Image FORCE
 	$(call if_changed,bzip2)
 
+# The shared cmd_lz4 passes -l, producing the legacy LZ4 format that
+# matches the kernel's own decompressor.  Image.lz4 is decompressed by
+# the bootloader though, and U-Boot only supports the standard LZ4 frame
+# format, so use a local rule that omits -l.
+quiet_cmd_lz4_std = LZ4     $@
+      cmd_lz4_std = cat $(real-prereqs) | $(LZ4) -9 - - > $@
+
 $(obj)/Image.lz4: $(obj)/Image FORCE
-	$(call if_changed,lz4)
+	$(call if_changed,lz4_std)
 
 $(obj)/Image.lzma: $(obj)/Image FORCE
 	$(call if_changed,lzma)
Signature · 1 line
2.34.1
#3 lore raw
In reply to Ping Li
Quoted 7 lines · Ping Li

arm64 and riscv also build Image.lz4 with the plain cmd_lz4, but a compressed boot image on these two architectures is not decompressed by the kernel itself - it is handed to the bootloader. U-Boot, which implements this via the 'booti' compressed-kernel path (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn), only accepts the standard LZ4 frame (magic 0x184D2204) and returns -EPROTONOSUPPORT for anything else.

U-Boot's decoder has a second requirement: independent blocks. It rejects frames with linked blocks, and the new rules rely on independent blocks being the default of the lz4 CLI. lz4 is considering linked blocks by default in a future version. U-Boot can learn to accept them, but bootloaders already deployed won't.

Passing -BI makes the output independent of that default:

	cmd_lz4_std = cat $(real-prereqs) | $(LZ4) -9 -BI - - > $@

With current lz4 versions, the output is unchanged. One caveat: -BI exists since lz4 v1.9.0 (2019). Older versions, still found on some build hosts (lz4 1.8.3 on RHEL 8), reject it, but they produce independent blocks by default anyway. So the rule can pass -BI only when lz4 accepts it: 'lz4 -BI -V' fails on these older versions.

Yann

author
#4 lore raw

Thanks a lot for the review - and for lz4 itself!

That's a great point about independent blocks. v2 passes -BI unconditionally on lz4 >= 1.9.0, with a build-time probe ($(LZ4) -BI -V) so hosts with older lz4 still work, exactly as you suggested. Verified locally that -BI produces byte-identical output on current lz4.

v2 will follow shortly.

Thanks,
Ping

author
#5 lore raw

v2 cover arm64,riscv: boot: emit standard LZ4 frame for Image.lz4

In reply to Ping Li

The shared cmd_lz4 rule in scripts/Makefile.lib passes -l to the lz4 tool, producing the legacy LZ4 format (magic 0x184C2102). The only in-kernel lz4 decompressor, lib/decompress_unlz4, understands exactly this legacy format, and all of its users - the self-extracting images of other architectures (which compress their payloads with the *_with_size variants) and the lz4-compressed initramfs - keep working through the unchanged shared rule.

arm64 and riscv build Image.lz4 with the plain cmd_lz4, but a compressed boot image on these architectures is handed to the bootloader, not decompressed by the kernel itself. U-Boot, whose compressed-kernel path (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn) only accepts the standard LZ4 frame (magic 0x184D2204) and returns -EPROTONOSUPPORT for anything else, cannot decompress the Image.lz4 built today. Bootloaders that decompress legacy lz4 frames do exist in the wild (mkbootimg on Android offers a separate lz4_legacy option), but U-Boot and its tooling only ever supported the standard frame.

So both architectures currently produce images that no mainstream bootloader can decompress. Fix both by emitting the standard frame through local rules, leaving the shared cmd_lz4 untouched for the in-kernel users.

Changes in v2:
- Pass -BI so the blocks stay independent regardless of what the lz4 CLI defaults to in the future; deployed U-Boot decoders reject linked blocks (suggested by Yann Collet).
- -BI was introduced in lz4 v1.9.0 - older lz4 versions reject the flag but already default to independent blocks, so the rule probes for -BI support first.

Ping Li (2):
arm64: boot: emit standard LZ4 frame for Image.lz4
riscv: boot: emit standard LZ4 frame for Image.lz4

 arch/arm64/boot/Makefile | 20 +++++++++++++++++++-
 arch/riscv/boot/Makefile | 20 +++++++++++++++++++-
 2 files changed, 38 insertions(+), 2 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v2 1/2 arm64: boot: emit standard LZ4 frame for Image.lz4

The shared cmd_lz4 rule passes -l to the lz4 tool, producing the legacy LZ4 format (magic 0x184C2102). That is the format understood by the kernel's own decompressor, lib/decompress_unlz4, whose users are the self-extracting images of other architectures (all of which compress their payloads with the *_with_size variants) and the lz4-compressed initramfs. An arm64 boot image is not consumed by the kernel itself though: it is decompressed by the bootloader.

U-Boot, which supports booting compressed arm64 kernels with 'booti' (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn), only accepts the standard LZ4 frame (magic 0x184D2204) and returns -EPROTONOSUPPORT for anything else. Its host tooling (binman) likewise emits standard frames. As a result, an arm64 Image.lz4 built today cannot be decompressed by U-Boot at all.

Emit the standard frame through a local rule, leaving the shared cmd_lz4 untouched for the in-kernel users.

Also pass -BI so the blocks stay independent regardless of what the lz4 CLI defaults to in the future; deployed U-Boot decoders reject linked blocks, and lz4 is considering linked blocks as a future CLI default. -BI was introduced in lz4 v1.9.0 - older lz4 versions reject the flag but already default to independent blocks, so the rule probes for -BI support first. With current lz4 versions the output is unchanged.

---
 arch/arm64/boot/Makefile | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/arm64/boot/Makefile +13 -1
index d42d07667822..eb28967469b4 100644
@@ -28,8 +28,20 @@ $(obj)/Image.bz2: $(obj)/Image FORCE
 $(obj)/Image.gz: $(obj)/Image FORCE
 	+$(call if_changed,gzip)
 
+# U-Boot only accepts standard LZ4 frames with independent blocks;
+# probe -BI first because lz4 < 1.9.0 rejects the flag but already
+# defaults to independent blocks.
+ifeq ($(shell $(LZ4) -BI -V >/dev/null 2>&1 && echo y),y)
+  lz4-bi-flag := -BI
+else
+  lz4-bi-flag :=
+endif
+
+quiet_cmd_lz4_std = LZ4     $@
+      cmd_lz4_std = cat $(real-prereqs) | $(LZ4) $(lz4-bi-flag) -9 - - > $@
+
 $(obj)/Image.lz4: $(obj)/Image FORCE
-	$(call if_changed,lz4)
+	$(call if_changed,lz4_std)
 
 $(obj)/Image.lzma: $(obj)/Image FORCE
 	$(call if_changed,lzma)
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v2 2/2 riscv: boot: emit standard LZ4 frame for Image.lz4

In reply to Ping Li

Like arm64, a compressed riscv boot image is decompressed by the bootloader, not by the kernel itself. The shared cmd_lz4 rule passes -l, producing the legacy LZ4 format that only the kernel's own decompressor understands, so U-Boot (boot/image.c image_decomp -> lib/lz4_wrapper.c ulz4fn), which only accepts the standard LZ4 frame and returns -EPROTONOSUPPORT for anything else, cannot decompress the Image.lz4 built today.

This matters in particular when CONFIG_KERNEL_LZ4 selects Image.lz4 as the default boot image via boot-image-$(CONFIG_KERNEL_LZ4) in arch/riscv/Makefile.

Emit the standard frame through a local rule, leaving the shared cmd_lz4 untouched for the in-kernel users.

Also pass -BI so the blocks stay independent regardless of what the lz4 CLI defaults to in the future; deployed U-Boot decoders reject linked blocks, and lz4 is considering linked blocks as a future CLI default. -BI was introduced in lz4 v1.9.0 - older lz4 versions reject the flag but already default to independent blocks, so the rule probes for -BI support first. With current lz4 versions the output is unchanged.

---
 arch/riscv/boot/Makefile | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
arch/riscv/boot/Makefile +13 -1
index b80a077a8e8f..ab2b1b77e21d 100644
@@ -33,8 +33,20 @@ $(obj)/loader: $(obj)/loader.o $(obj)/Image $(obj)/loader.lds FORCE
 $(obj)/Image.bz2: $(obj)/Image FORCE
 	$(call if_changed,bzip2)
 
+# U-Boot only accepts standard LZ4 frames with independent blocks;
+# probe -BI first because lz4 < 1.9.0 rejects the flag but already
+# defaults to independent blocks.
+ifeq ($(shell $(LZ4) -BI -V >/dev/null 2>&1 && echo y),y)
+  lz4-bi-flag := -BI
+else
+  lz4-bi-flag :=
+endif
+
+quiet_cmd_lz4_std = LZ4     $@
+      cmd_lz4_std = cat $(real-prereqs) | $(LZ4) $(lz4-bi-flag) -9 - - > $@
+
 $(obj)/Image.lz4: $(obj)/Image FORCE
-	$(call if_changed,lz4)
+	$(call if_changed,lz4_std)
 
 $(obj)/Image.lzma: $(obj)/Image FORCE
 	$(call if_changed,lzma)
Signature · 1 line
2.34.1
Reviewed-by on the v2 series
#8 lore raw
In reply to Ping Li
Quoted 7 lines · Ping Li

Changes in v2:
- Pass -BI so the blocks stay independent regardless of what the lz4 CLI defaults to in the future; deployed U-Boot decoders reject linked blocks (suggested by Yann Collet).
- -BI was introduced in lz4 v1.9.0 - older lz4 versions reject the flag but already default to independent blocks, so the rule probes for -BI support first.

Checked the new rule with lz4 1.8.3 (no -BI), 1.9.3 and 1.11.1 (-BI):
independent blocks in all cases, and the output round-trips.

For the series:

author
#9 lore raw
In reply to Ping Li

Thanks a lot for testing across three lz4 versions, Yann - especially 1.11.1, which I hadn't covered locally.

Ping