[PATCH 2/6] base-files: preinit: load device-tree overlays

Daniel Golle daniel at makrotopia.org
Wed Nov 24 17:06:46 PST 2021


Add new boot hooklist 'preinit_late'
'preinit_main' returns once initramfs is detected and skips all later
hooks, hence it cannot be used e.g. for loading device tree overlays
which should work independently of running in initramfs of not, but
needs to happen after rootfs is mounted in the "regular"
squashfs+overlayfs case.

Hence introduce new hooklist 'preinit_late' which is run after
'preinit_main' has completed.

Introduce preinit_late hook to mount configfs and apply device tree
overlays from /lib/firmware/device-tree/overlays/*.dtbo on targets
which got the 'dt-overlay' feature set.
Device tree overlay blobs placed into that folder will be loaded
early on boot before AutoProbing kernel modules.

Signed-off-by: Daniel Golle <daniel at makrotopia.org>
---
 package/base-files/Makefile                   |  6 ++++++
 package/base-files/files/etc/preinit          |  2 ++
 .../files/lib/preinit/90_apply_dt_overlays    | 19 +++++++++++++++++++
 3 files changed, 27 insertions(+)
 create mode 100644 package/base-files/files/lib/preinit/90_apply_dt_overlays

diff --git a/package/base-files/Makefile b/package/base-files/Makefile
index af5c0e6b00..b347eb4143 100644
--- a/package/base-files/Makefile
+++ b/package/base-files/Makefile
@@ -22,6 +22,7 @@ PKG_LICENSE:=GPL-2.0
 # Extend depends from version.mk
 PKG_CONFIG_DEPENDS += \
 	CONFIG_SIGNED_PACKAGES CONFIG_TARGET_INIT_PATH CONFIG_TARGET_PREINIT_DISABLE_FAILSAFE \
+	CONFIG_HAS_DT_OVERLAY_SUPPORT \
 	CONFIG_NAND_SUPPORT \
 	CONFIG_LEGACY_SDCARD_SUPPORT \
 	CONFIG_CLEAN_IPKG \
@@ -189,6 +190,11 @@ else
 	mkdir -p $(1)/var
 	$(LN) /tmp/run $(1)/var/run
 endif
+
+ifneq ($(CONFIG_HAS_DT_OVERLAY_SUPPORT),y)
+	rm $(1)/lib/preinit/90_apply_dt_overlays
+endif
+
 	$(LN) /tmp/resolv.conf /tmp/TZ /tmp/localtime $(1)/etc/
 
 	chmod 0600 $(1)/etc/shadow
diff --git a/package/base-files/files/etc/preinit b/package/base-files/files/etc/preinit
index 829574f5f1..04c11f8833 100755
--- a/package/base-files/files/etc/preinit
+++ b/package/base-files/files/etc/preinit
@@ -15,6 +15,7 @@ boot_hook_init preinit_main
 boot_hook_init failsafe
 boot_hook_init initramfs
 boot_hook_init preinit_mount_root
+boot_hook_init preinit_late
 
 for pi_source_file in /lib/preinit/*; do
 	. $pi_source_file
@@ -27,3 +28,4 @@ pi_jffs2_mount_success=false
 pi_failsafe_net_message=false
 
 boot_run_hook preinit_main
+boot_run_hook preinit_late
diff --git a/package/base-files/files/lib/preinit/90_apply_dt_overlays b/package/base-files/files/lib/preinit/90_apply_dt_overlays
new file mode 100644
index 0000000000..7d17947e1c
--- /dev/null
+++ b/package/base-files/files/lib/preinit/90_apply_dt_overlays
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+do_apply_dt_overlays() {
+	grep -q configfs /proc/filesystems || return 0
+	mount configfs -t configfs /sys/kernel/config
+	local overlays_dir="/sys/kernel/config/device-tree/overlays"
+	[ -d "$overlays_dir" ] || return 0
+	local dto dtoname
+	for dto in /lib/firmware/device-tree/overlays/*.dto ; do
+		[ -r "$dto" ] || continue
+		dtoname="$(basename "$dtbo" .dtbo)"
+		[ -e "$overlays_dir/$dtoname" ] && continue
+		echo " - applying device-tree overlay '$dtoname' - "
+		mkdir "$overlays_dir/$dtoname"
+		echo "$dto" > "$overlays_dir/$dtoname/path"
+	done
+}
+
+boot_hook_add preinit_late do_apply_dt_overlays
-- 
2.34.0




More information about the openwrt-devel mailing list