From 2be34e5c09556a6c5304b36c03c5616856fdf73e Mon Sep 17 00:00:00 2001
From: The-going <48602507+The-going@users.noreply.github.com>
Date: Tue, 1 Feb 2022 21:04:08 +0300
Subject: scripts: add overlay compilation support

---
 .gitignore               |  1 +
 scripts/Makefile.dtbinst | 12 +++++++++++-
 scripts/Makefile.lib     | 12 ++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 5937c74d3dc1..3a2bea98567d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -44,6 +44,7 @@
 *.rpm
 *.rsi
 *.s
+*.scr
 *.so
 *.so.dbg
 *.su
diff --git a/scripts/Makefile.dtbinst b/scripts/Makefile.dtbinst
index 9d920419a62c..dc0c8da28494 100644
--- a/scripts/Makefile.dtbinst
+++ b/scripts/Makefile.dtbinst
@@ -23,6 +23,11 @@ $(dst)/%: $(obj)/%
 	$(call cmd,dtb_install)
 
 dtbs := $(patsubst $(obj)/%,%,$(call read-file, $(obj)/dtbs-list))
+vendor := $(sort $(dir $(filter %.dtb, $(dtbs))))
+subdir := $(sort $(dir $(filter %.dtbo, $(dtbs))))
+
+dtbs += $(patsubst $(obj)/%,%,$(if $(subdir), $(shell cd $(srctree)/$(obj) && find $(subdir) -name '*.scr')))
+dtbs += $(patsubst $(obj)/%,%,$(if $(subdir), $(shell cd $(srctree)/$(obj) && find $(subdir) -name 'README.*-overlays')))
 
 ifdef CONFIG_ARCH_WANT_FLAT_DTB_INSTALL
 
@@ -33,7 +38,12 @@ endef
 
 $(foreach d, $(sort $(dir $(dtbs))), $(eval $(call gen_install_rules,$(d))))
 
-dtbs := $(notdir $(dtbs))
+# Armbian uses subdirectories <overlay> for arm
+# and <allwinner/overlay> for arm64.
+# delete the vendor's directory if the target architecture is an arm.
+ifdef CONFIG_ARM
+dtbs := $(patsubst $(vendor)%,%,$(dtbs))
+endif
 
 endif # CONFIG_ARCH_WANT_FLAT_DTB_INSTALL
 
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index cad20f0e66ee..2d8b0b41d5f0 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -75,6 +75,9 @@ always-y += $(hostprogs-always-y) $(hostprogs-always-m)
 userprogs += $(userprogs-always-y) $(userprogs-always-m)
 always-y += $(userprogs-always-y) $(userprogs-always-m)
 
+# Overlay Fixup script targets
+extra-y				+= $(scr-y) $(dtbotxt-y)
+
 # Add subdir path
 
 ifneq ($(obj),.)
@@ -436,6 +439,15 @@ quiet_cmd_lz4_with_size = LZ4     $@
       cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -9 - -; \
                   $(size_append); } > $@
 
+# Fixup script mkimage provided by Armbian
+# ---------------------------------------------------------------------------
+
+quiet_cmd_scr = FIXUP IMAGE $@
+      cmd_scr = mkimage -C none -A $(ARCH) -T script -d $< $@
+
+$(obj)/%.scr: $(src)/%.scr-cmd FORCE
+	$(call if_changed,scr)
+
 # U-Boot mkimage
 # ---------------------------------------------------------------------------
 
-- 
2.35.3

