diff mbox series

[2/2] kernel-fitimage: skip FDT section creation for applicable symlinks

Message ID 20221026132219.3330056-2-a.fatoum@pengutronix.de
State Accepted, archived
Commit 21e240da63239826f3ef50ceef40c9519e9030d8
Headers show
Series [1/2] kernel-fitimage: mangle slashes to underscores as late as possible | expand

Commit Message

Ahmad Fatoum Oct. 26, 2022, 1:22 p.m. UTC
When building a FIT image with device trees, each device tree lands in a
FIT section and is referenced by a FIT configuration node.

FIT images however also allow referencing the same device tree from
multiple configurations. This can be useful to reduce FIT image size
while staying compatible with existing bootloaders. Allow
kernel-fitimage.bbclass users to take advantage of this by mapping
each symlink to a regular device tree included in the FIT to a
configuration that references a common device tree section.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 meta/classes-recipe/kernel-fitimage.bbclass | 33 ++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/kernel-fitimage.bbclass b/meta/classes-recipe/kernel-fitimage.bbclass
index 6307e3c50a3f..7980910aa8c4 100644
--- a/meta/classes-recipe/kernel-fitimage.bbclass
+++ b/meta/classes-recipe/kernel-fitimage.bbclass
@@ -354,6 +354,27 @@  EOF
 	fi
 }
 
+#
+# echoes symlink destination if it points below directory
+#
+# $1 ... file that's a potential symlink
+# $2 ... expected parent directory
+symlink_points_below() {
+	file="$2/$1"
+	dir=$2
+
+	if ! [ -L "$file" ]; then
+		return
+	fi
+
+	realpath="$(realpath --relative-to=$dir $file)"
+	if [ -z "${realpath%%../*}" ]; then
+		return
+	fi
+
+	echo "$realpath"
+}
+
 #
 # Emit the fitImage ITS configuration section
 #
@@ -392,7 +413,13 @@  fitimage_emit_section_config() {
 	setup_line=""
 	default_line=""
 
+	dtb_image_sect=$(symlink_points_below $dtb_image "${EXTERNAL_KERNEL_DEVICETREE}")
+	if [ -z "$dtb_image_sect" ]; then
+		dtb_image_sect=$dtb_image
+	fi
+
 	dtb_image=$(echo $dtb_image | tr '/' '_')
+	dtb_image_sect=$(echo "${dtb_image_sect}" | tr '/' '_')
 
 	# conf node name is selected based on dtb ID if it is present,
 	# otherwise its selected based on kernel ID
@@ -411,7 +438,7 @@  fitimage_emit_section_config() {
 	if [ -n "$dtb_image" ]; then
 		conf_desc="$conf_desc${sep}FDT blob"
 		sep=", "
-		fdt_line="fdt = \"fdt-$dtb_image\";"
+		fdt_line="fdt = \"fdt-$dtb_image_sect\";"
 	fi
 
 	if [ -n "$ramdisk_id" ]; then
@@ -568,6 +595,10 @@  fitimage_assemble() {
 			echo "$DTBS" | tr ' ' '\n' | grep -xq "$DTB" && continue
 
 			DTBS="$DTBS $DTB"
+
+			# Also skip if a symlink. We'll later have each config section point at it
+			[ $(symlink_points_below $DTB "${EXTERNAL_KERNEL_DEVICETREE}") ] && continue
+
 			DTB=$(echo $DTB | tr '/' '_')
 			fitimage_emit_section_dtb $1 $DTB "${EXTERNAL_KERNEL_DEVICETREE}/$DTB"
 		done