mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Cc: Sascha Hauer <sascha@saschahauer.de>,
	"Claude Opus 4.6" <noreply@anthropic.com>
Subject: [PATCH 6/9] firmware: Add compressed firmware symbols for PBL
Date: Mon, 16 Mar 2026 18:21:19 +0100	[thread overview]
Message-ID: <20260316-compressed-firmware-v1-6-d9712142881e@pengutronix.de> (raw)
In-Reply-To: <20260316-compressed-firmware-v1-0-d9712142881e@pengutronix.de>

From: Sascha Hauer <sascha@saschahauer.de>

Similar to how device trees get both compressed and uncompressed
symbols (__dtb_<name>_start vs __dtb_z_<name>_start), generate
compressed variants of firmware blobs for PBL use.

When a firmware binary exists, it is compressed using the configured
IMAGE_COMPRESSION algorithm and linked into a separate .fwz.rodata
section with _fw_z_<name>_start/_fw_z_<name>_end symbols. The
uncompressed size is available as _fw_z_<name>_uncompressed_size.

The compressed symbols are only available in PBL context
(guarded by __PBL__). A new get_builtin_firmware_compressed()
macro is provided for C consumers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 firmware/Makefile  | 18 +++++++++++++++---
 include/firmware.h | 12 ++++++++++++
 scripts/gen-fw-s   | 26 +++++++++++++++++++++++++-
 3 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/firmware/Makefile b/firmware/Makefile
index b247db8da2..00412042b7 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -61,8 +61,8 @@ pbl-fwext-y := $(addsuffix .extgen.o, $(fw-external-y))
 
 FWNAME    = $(patsubst $(obj)/%.extgen.S,%,$(patsubst $(obj)/%.gen.S,%,$@))
 
-filechk_fwbin = $(srctree)/scripts/gen-fw-s $(FWNAME) $(FIRMWARE_DIR) .rodata
-filechk_fwbin_ext = $(srctree)/scripts/gen-fw-s $(FWNAME) $(FIRMWARE_DIR) .pblext a
+filechk_fwbin = $(srctree)/scripts/gen-fw-s $(FWNAME) $(FIRMWARE_DIR) .rodata '' $(fwobjdir)
+filechk_fwbin_ext = $(srctree)/scripts/gen-fw-s $(FWNAME) $(FIRMWARE_DIR) .pblext a $(fwobjdir)
 
 $(obj)/%.gen.S: FORCE
 	$(call filechk,fwbin)
@@ -70,6 +70,10 @@ $(obj)/%.gen.S: FORCE
 $(obj)/%.extgen.S: FORCE
 	$(call filechk,fwbin_ext)
 
+# Compress firmware files
+$(fwobjdir)/%.z: $(FIRMWARE_DIR)/% FORCE
+	$(call if_changed,$(suffix_y))
+
 # This dependency is used if missing firmware should fail the build immediately
 fwdep-required-y = $(FIRMWARE_DIR)/%
 # This dependency expands to nothing if the file doesn't exist. This allows
@@ -82,9 +86,15 @@ fwdep-required-y = $(FIRMWARE_DIR)/%
 # them to install all firmware for all platforms if only few are of interest.
 fwdep-required-n = $$(wildcard $(FIRMWARE_DIR)/%)
 
+# Compressed firmware dependency: only if the firmware binary exists.
+# Use $$* (stem) instead of % because % is not substituted inside $$(if ...).
+fwzdep-n = $$(if $$(wildcard $(FIRMWARE_DIR)/$$*),$(fwobjdir)/$$*.z)
+
 .SECONDEXPANSION:
+# .gen.S files depend on compressed firmware for correct size computation
+$(patsubst %.gen.o,$(obj)/%.gen.S, $(obj-pbl-y) $(pbl-y)): $(obj)/%.gen.S: $(fwzdep-n)
 # The .o files depend on the binaries directly if available; the .S files don't.
-$(patsubst %.gen.o,$(obj)/%.gen.pbl.o, $(obj-pbl-y) $(pbl-y)): $(obj)/%.gen.pbl.o: $(fwdep-required-n)
+$(patsubst %.gen.o,$(obj)/%.gen.pbl.o, $(obj-pbl-y) $(pbl-y)): $(obj)/%.gen.pbl.o: $(fwdep-required-n) $(fwzdep-n)
 $(patsubst %.extgen.o,$(obj)/%.extgen.pbl.o, $(pbl-fwext-y)): $(obj)/%.extgen.pbl.o: $(fwdep-required-n)
 # For barebox proper, firmware existance is either checked here
 # or in driver code by checking whether size != 0
@@ -96,6 +106,8 @@ targets := $(patsubst $(obj)/%,%, \
 	                        $(shell find $(obj) -name \*.gen.S 2>/dev/null))
 targets += $(patsubst $(obj)/%,%, \
 	                        $(shell find $(obj) -name \*.extgen.S 2>/dev/null))
+targets += $(patsubst $(obj)/%,%, \
+	                        $(shell find $(obj) -name \*.z 2>/dev/null))
 
 # just to build a built-in.o. Otherwise compilation fails when no
 # firmware is built.
diff --git a/include/firmware.h b/include/firmware.h
index 6609bdbc9e..2f75ded82b 100644
--- a/include/firmware.h
+++ b/include/firmware.h
@@ -92,6 +92,7 @@ static inline void firmware_ext_verify(const void *data_start, size_t data_size,
 
 struct fwobj {
 	size_t size;
+	size_t uncompressed_size;
 	void *data;
 };
 
@@ -120,6 +121,17 @@ struct fwobj {
 #define get_builtin_firmware_ext(name, base, fwobj)			\
 	__get_builtin_firmware(name, (long)base - (long)_text, fwobj)
 
+#define get_builtin_firmware_compressed(name, fwobj)			\
+	do {								\
+		extern char _fw_z_##name##_start[];			\
+		extern char _fw_z_##name##_end[];			\
+		extern unsigned long _fw_z_##name##_uncompressed_size;	\
+		(fwobj)->data = _fw_z_##name##_start;			\
+		(fwobj)->size = _fw_z_##name##_end - _fw_z_##name##_start;\
+		(fwobj)->uncompressed_size =				\
+			(size_t)_fw_z_##name##_uncompressed_size;	\
+	} while (0)
+
 static inline int firmware_next_image_check_sha256(const void *hash, bool verbose)
 {
 	extern char _fw_next_image_bin_sha_start[];
diff --git a/scripts/gen-fw-s b/scripts/gen-fw-s
index 653e6f013a..78c3193479 100755
--- a/scripts/gen-fw-s
+++ b/scripts/gen-fw-s
@@ -3,16 +3,22 @@
 #
 # Generate assembly source to embed firmware binary
 #
-# Usage: gen-fw-s <fwname> <fwdir> <secprefix> [secflags]
+# Usage: gen-fw-s <fwname> <fwdir> <secprefix> [secflags] [fwobjdir]
 
 fwname=$1
 fwdir=$2
 secprefix=$3
 secflags=$4
+fwobjdir=$5
 
 fwstr=$(echo "$fwname" | tr '/.-' '___')
 fwpath="$fwdir/$fwname"
 
+fw_uncompressed=0
+if [ -f "$fwpath" ]; then
+	fw_uncompressed=$(stat -c %s "$fwpath")
+fi
+
 sha=$(sha256sum "$fwpath" 2>/dev/null | sed 's/ .*$//;s/../0x&, /g;s/, $//')
 
 echo "/* Generated by scripts/gen-fw-s */"
@@ -35,6 +41,24 @@ fi
 echo ".global _fw_${fwstr}_end"
 echo "_fw_${fwstr}_end:"
 
+# include compressed firmware if exists, otherwise include a reference to
+# the firmware filename in the .missing_fw section
+echo "#if defined(__PBL__)"
+echo "    .section .fwz.rodata.${fwstr},\"a\""
+echo "    .p2align ASM_LGPTR"
+echo ".global _fw_z_${fwstr}_uncompressed_size"
+echo ".set _fw_z_${fwstr}_uncompressed_size, ${fw_uncompressed}"
+echo ".global _fw_z_${fwstr}_start"
+echo "_fw_z_${fwstr}_start:"
+if [ -f "$fwpath" ]; then
+	echo ".incbin \"${fwobjdir}/${fwname}.z\""
+else
+	echo "ASM_PTR _fwname_${fwstr}"
+fi
+echo ".global _fw_z_${fwstr}_end"
+echo "_fw_z_${fwstr}_end:"
+echo "#endif"
+
 # include sha256, needed for external firmware
 echo "    .section .rodata.${fwstr}.sha"
 echo "    .p2align ASM_LGPTR"

-- 
2.47.3




  parent reply	other threads:[~2026-03-16 17:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-16 17:21 [PATCH 0/9] Firmware: support compressing firmware files Sascha Hauer
2026-03-16 17:21 ` [PATCH 1/9] ARM: socfpga: Drop unnecessary select USE_COMPRESSED_DTB Sascha Hauer
2026-03-16 17:21 ` [PATCH 2/9] ARM: radxa-rock5: Use compressed DTB Sascha Hauer
2026-03-16 17:21 ` [PATCH 3/9] ARM: Rockchip: Simplify retrieval of SoC specific addresses Sascha Hauer
2026-03-16 17:21 ` [PATCH 4/9] firmware: Move firmware assembly generation to scripts/gen-fw-s Sascha Hauer
2026-03-16 17:21 ` [PATCH 5/9] firmware: Use struct fwobj for get_builtin_firmware APIs Sascha Hauer
2026-03-16 17:21 ` Sascha Hauer [this message]
2026-03-19 14:19   ` [PATCH] fixup! firmware: Add compressed firmware symbols for PBL Sascha Hauer
2026-03-20  8:54   ` Sascha Hauer
2026-03-16 17:21 ` [PATCH 7/9] firmware: Add fwobj_uncompress() for decompressing firmware in PBL Sascha Hauer
2026-03-16 17:21 ` [PATCH 8/9] ARM: Rockchip: Use compressed OP-TEE binary Sascha Hauer
2026-03-18  8:10   ` Sascha Hauer
2026-03-16 17:21 ` [PATCH 9/9] ARM: Rockchip: Use compressed TF-A binary Sascha Hauer
2026-03-19  6:53 ` [PATCH 0/9] Firmware: support compressing firmware files Sascha Hauer

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=20260316-compressed-firmware-v1-6-d9712142881e@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=noreply@anthropic.com \
    --cc=sascha@saschahauer.de \
    /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