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 4/9] firmware: Move firmware assembly generation to scripts/gen-fw-s
Date: Mon, 16 Mar 2026 18:21:17 +0100	[thread overview]
Message-ID: <20260316-compressed-firmware-v1-4-d9712142881e@pengutronix.de> (raw)
In-Reply-To: <20260316-compressed-firmware-v1-0-d9712142881e@pengutronix.de>

From: Sascha Hauer <sascha@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@anthropic.com>
Signed-off-by: Sascha Hauer <s.hauer@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




  parent reply	other threads:[~2026-03-16 17:21 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 ` Sascha Hauer [this message]
2026-03-16 17:21 ` [PATCH 5/9] firmware: Use struct fwobj for get_builtin_firmware APIs Sascha Hauer
2026-03-16 17:21 ` [PATCH 6/9] firmware: Add compressed firmware symbols for PBL Sascha Hauer
2026-03-19 14:19   ` [PATCH] fixup! " 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-4-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