[PATCH 4/9] firmware: Move firmware assembly generation to scripts/gen-fw-s
Sascha Hauer
s.hauer at pengutronix.de
Mon Mar 16 10:21:17 PDT 2026
From: Sascha Hauer <sascha at saschahauer.de>
Extract the inline filechk_fwbin shell code from firmware/Makefile into
a standalone script at scripts/gen-fw-s, following the pattern of
scripts/gen-dtb-s.
The FWSTR and FWNAME_EXISTS variables are no longer needed in the
Makefile as they are now computed within the script.
Some #ifdefs are already known during execution of gen-fw-s. As it is
raw shell now instead of shell encapsulated in a Makefile we can improve
readability by replacing these #ifdefs with shell "if".
Also while at it add some comments what is happening as the missing
firmware handling is not entirely obvious.
Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de
---
firmware/Makefile | 51 +++++---------------------------------------
scripts/gen-fw-s | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+), 46 deletions(-)
diff --git a/firmware/Makefile b/firmware/Makefile
index a3b1887dc6..b247db8da2 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -60,56 +60,15 @@ obj-pbl-y := $(addsuffix .gen.o, $(firmware-y))
pbl-fwext-y := $(addsuffix .extgen.o, $(fw-external-y))
FWNAME = $(patsubst $(obj)/%.extgen.S,%,$(patsubst $(obj)/%.gen.S,%,$@))
-FWSTR = $(subst /,_,$(subst .,_,$(subst -,_,$(FWNAME))))
-FWNAME_EXISTS = $(if $(wildcard $(FIRMWARE_DIR)/$(FWNAME)),1,0)
-
-filechk_fwbin = { \
- SHA=$$(sha256sum $(FIRMWARE_DIR)/$(FWNAME) | \
- sed 's/ .*$$//;s/../0x&, /g;s/, $$//') \
- 2>/dev/null ;\
- \
- echo "/* Generated by $(src)/Makefile */" ;\
- echo "\#include <asm-generic/pointer.h>" ;\
- echo ".section .note.GNU-stack,\"\",%progbits" ;\
- echo " .section $2,\"$3\"" ;\
- echo " .p2align ASM_LGPTR" ;\
- echo ".global _fw_$(FWSTR)_start" ;\
- echo "_fw_$(FWSTR)_start:" ;\
- echo "\#if $(FWNAME_EXISTS)" ;\
- echo " .incbin \"$(FIRMWARE_DIR)/$(FWNAME)\"" ;\
- echo "\#elif defined(__PBL__)" ;\
- echo "ASM_PTR _fwname_$(FWSTR)" ;\
- echo "\#endif" ;\
- echo ".global _fw_$(FWSTR)_end" ;\
- echo "_fw_$(FWSTR)_end:" ;\
- echo "\#ifdef __PBL__" ;\
- echo " .section .missing_fw,\"a\"" ;\
- echo "_fwname_$(FWSTR):" ;\
- printf '.ascii "%s"\n' 'firmware/$(FWNAME)\n' ;\
- echo "\#endif" ;\
- echo " .section .rodata.$(FWSTR).sha" ;\
- echo " .p2align ASM_LGPTR" ;\
- echo ".global _fw_$(FWSTR)_sha_start" ;\
- echo "_fw_$(FWSTR)_sha_start:" ;\
- echo " .byte $${SHA}" ;\
- echo ".global _fw_$(FWSTR)_sha_end" ;\
- echo "_fw_$(FWSTR)_sha_end:" ;\
- echo "\#if $(FWNAME_EXISTS)" ;\
- echo ".if _fw_$(FWSTR)_sha_start + 32 - _fw_$(FWSTR)_sha_end";\
- echo ".error \"sha256sum invalid\"" ;\
- echo ".endif" ;\
- echo "\#endif" ;\
-}
-
-filechk_fwbin_ext = { \
- $(filechk_fwbin) ;\
-}
+
+filechk_fwbin = $(srctree)/scripts/gen-fw-s $(FWNAME) $(FIRMWARE_DIR) .rodata
+filechk_fwbin_ext = $(srctree)/scripts/gen-fw-s $(FWNAME) $(FIRMWARE_DIR) .pblext a
$(obj)/%.gen.S: FORCE
- $(call filechk,fwbin,.rodata.$(FWSTR),)
+ $(call filechk,fwbin)
$(obj)/%.extgen.S: FORCE
- $(call filechk,fwbin_ext,.pblext.$(FWSTR),a)
+ $(call filechk,fwbin_ext)
# This dependency is used if missing firmware should fail the build immediately
fwdep-required-y = $(FIRMWARE_DIR)/%
diff --git a/scripts/gen-fw-s b/scripts/gen-fw-s
new file mode 100755
index 0000000000..653e6f013a
--- /dev/null
+++ b/scripts/gen-fw-s
@@ -0,0 +1,63 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Generate assembly source to embed firmware binary
+#
+# Usage: gen-fw-s <fwname> <fwdir> <secprefix> [secflags]
+
+fwname=$1
+fwdir=$2
+secprefix=$3
+secflags=$4
+
+fwstr=$(echo "$fwname" | tr '/.-' '___')
+fwpath="$fwdir/$fwname"
+
+sha=$(sha256sum "$fwpath" 2>/dev/null | sed 's/ .*$//;s/../0x&, /g;s/, $//')
+
+echo "/* Generated by scripts/gen-fw-s */"
+echo "#include <asm-generic/pointer.h>"
+echo ".section .note.GNU-stack,\"\",%progbits"
+
+# include firmware if exists, otherwise include a reference to
+# the firmware filename in the .missing_fw section
+echo " .section ${secprefix}.${fwstr},\"${secflags}\""
+echo " .p2align ASM_LGPTR"
+echo ".global _fw_${fwstr}_start"
+echo "_fw_${fwstr}_start:"
+if [ -f "$fwpath" ]; then
+ echo ".incbin \"${fwpath}\""
+else
+ echo "#if defined(__PBL__)"
+ echo "ASM_PTR _fwname_${fwstr}"
+ echo "#endif"
+fi
+echo ".global _fw_${fwstr}_end"
+echo "_fw_${fwstr}_end:"
+
+# include sha256, needed for external firmware
+echo " .section .rodata.${fwstr}.sha"
+echo " .p2align ASM_LGPTR"
+echo ".global _fw_${fwstr}_sha_start"
+echo "_fw_${fwstr}_sha_start:"
+echo " .byte ${sha}"
+echo ".global _fw_${fwstr}_sha_end"
+echo "_fw_${fwstr}_sha_end:"
+if [ -f "$fwpath" ]; then
+ echo ".if _fw_${fwstr}_sha_start + 32 - _fw_${fwstr}_sha_end"
+ echo ".error \"sha256sum invalid\""
+ echo ".endif"
+fi
+
+# include a string containing the firmware name. When a non existing
+# firmware is referenced in the PBL then _fwname_${fwstr} is referenced
+# above rather than the firmware itself. The barebox build system checks
+# if the missing_fw section is empty. If it is, then we have all necessary
+# firmware files. If not, then we either fail the compilation
+# (CONFIG_MISSING_FIRMWARE_ERROR=y) or print a missing firmware warning
+# (CONFIG_MISSING_FIRMWARE_ERROR=n).
+echo "#ifdef __PBL__"
+echo " .section .missing_fw,\"a\""
+echo "_fwname_${fwstr}:"
+printf '.ascii "%s"\n' "firmware/${fwname}\\n"
+echo "#endif"
--
2.47.3
More information about the barebox
mailing list