diff mbox series

[v2] uboot-sign: keep the SPL BSS padding when reassembling the SPL binary

Message ID 20261020223209.282478-1-ricardo.salveti@oss.qualcomm.com
State New
Headers show
Series [v2] uboot-sign: keep the SPL BSS padding when reassembling the SPL binary | expand

Commit Message

Ricardo Salveti Oct. 20, 2026, 10:32 p.m. UTC
concat_spl_dtb() rebuilds the SPL as a plain concatenation of the nodtb
image and the signed device tree. U-Boot only assembles it that way with
SPL_SEPARATE_BSS; otherwise BSS is part of the image and it pads between
the two, so that clearing BSS cannot overwrite the appended device tree.
Platforms loading the SPL as one contiguous image, such as Qualcomm SoCs,
need that layout: without the padding the device tree ends up inside BSS
and spl_early_init() finds none, before the console is up.

Add SPL_WITH_DTB_BINARY, empty by default, for machines to name the SPL
image U-Boot assembled (u-boot-spl-dtb.bin). When set, the signed device
tree is appended at the offset U-Boot used; otherwise nothing changes.

Tested with oe-selftest fitimage.UBootFitImageTests and by booting a
signed SPL FIT on an IQ-9075 EVK, where the device tree lands at the
same offset as in U-Boot's own u-boot-spl-dtb.bin.

AI-Generated: Uses Claude Code
Signed-off-by: Ricardo Salveti <ricardo.salveti@oss.qualcomm.com>
---
Changes in v2:
- Make SPL_WITH_DTB_BINARY opt-in (empty by default); machines that need
  the padding set it to "u-boot-spl-dtb.bin"
- bbfatal when it is set but the file is missing, or is smaller than the
  nodtb image and the DTB combined, instead of silently falling back
- Keep the existing concatenation untouched when it is not set

 meta/classes-recipe/uboot-sign.bbclass | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/uboot-sign.bbclass b/meta/classes-recipe/uboot-sign.bbclass
index 236d19925e..5d9e48e103 100644
--- a/meta/classes-recipe/uboot-sign.bbclass
+++ b/meta/classes-recipe/uboot-sign.bbclass
@@ -57,6 +57,10 @@  SPL_DTB_SYMLINK ?= "u-boot-spl-${MACHINE}.dtb"
 SPL_NODTB_IMAGE ?= "u-boot-spl-nodtb-${MACHINE}-${PV}-${PR}.bin"
 SPL_NODTB_BINARY ?= "u-boot-spl-nodtb.bin"
 SPL_NODTB_SYMLINK ?= "u-boot-spl-nodtb-${MACHINE}.bin"
+# The SPL image as assembled by U-Boot (e.g. "u-boot-spl-dtb.bin"). When set,
+# the signed SPL DTB is appended at the same offset U-Boot used, keeping the
+# padding it adds behind BSS for SPLs built without SPL_SEPARATE_BSS.
+SPL_WITH_DTB_BINARY ?= ""
 
 # U-Boot fitImage description
 UBOOT_FIT_DESC ?= "U-Boot fitImage for ${DISTRO_NAME}/${PV}/${MACHINE}"
@@ -249,7 +253,26 @@  deploy_dtb() {
 
 concat_spl_dtb() {
 	if [ -e "${SPL_DIR}/${SPL_NODTB_BINARY}" ] && [ -e "${SPL_DIR}/${SPL_DTB_BINARY}" ] ; then
-		cat ${SPL_DIR}/${SPL_NODTB_BINARY} ${SPL_DIR}/${SPL_DTB_SIGNED} > "${SPL_BINARY}"
+		if [ -n "${SPL_WITH_DTB_BINARY}" ] ; then
+			test -e "${SPL_DIR}/${SPL_WITH_DTB_BINARY}" || \
+				bbfatal "${SPL_WITH_DTB_BINARY} not found in ${SPL_DIR}. Set SPL_WITH_DTB_BINARY to the SPL image U-Boot assembled, or to an empty string."
+
+			# U-Boot pads behind BSS before appending the device tree
+			# ($(SPL_BIN)-pad.bin in scripts/Makefile.xpl); keep that offset.
+			nodtb_size=$(stat -Lc %s ${SPL_DIR}/${SPL_NODTB_BINARY})
+			image_size=$(stat -Lc %s ${SPL_DIR}/${SPL_WITH_DTB_BINARY})
+			dtb_size=$(stat -Lc %s ${SPL_DIR}/${SPL_DTB_BINARY})
+			dtb_offset=$(expr ${image_size} - ${dtb_size})
+			if [ ${dtb_offset} -lt ${nodtb_size} ] ; then
+				bbfatal "${SPL_WITH_DTB_BINARY} is smaller than ${SPL_NODTB_BINARY} and ${SPL_DTB_BINARY} combined."
+			fi
+
+			cp ${SPL_DIR}/${SPL_NODTB_BINARY} "${SPL_BINARY}"
+			truncate -s ${dtb_offset} "${SPL_BINARY}"
+			cat ${SPL_DIR}/${SPL_DTB_SIGNED} >> "${SPL_BINARY}"
+		else
+			cat ${SPL_DIR}/${SPL_NODTB_BINARY} ${SPL_DIR}/${SPL_DTB_SIGNED} > "${SPL_BINARY}"
+		fi
 	else
 		bbwarn "Failure while adding public key to spl binary. Verified U-Boot boot won't be available."
 	fi