Add support for overlays from boot partition.

Adds capability to load DT overlays from the boot partition. These files
are indicated in the overlays.txt conffile (also located in the boot
partition). If an overlay fails, fallback to the default device tree.

Note: Debian doesn't currently generate symbols for the device trees so
you can't use the phandles and overlay concept of target. You must use
target-path (for example target=<&i2c2> must be
target-path="/i2c@30a30000").

Change-Id: I000bf9e264fde8febf67bb8a6b2a0f7c8e8f0b6a
diff --git a/debian/boot.txt b/debian/boot.txt
index f1ea6ae..2a2967d 100644
--- a/debian/boot.txt
+++ b/debian/boot.txt
@@ -25,6 +25,28 @@
 cmdline="console=ttymxc0,115200 console=tty0 earlycon=ec_imx6q,0x30860000,115200 root=${root} rootfstype=ext4 rw rootwait init=/sbin/init net.ifnames=0 pci=pcie_bus_perf"
 
 ext2load mmc ${bootdev}:1 ${loadaddr} ${image}
-ext2load mmc ${bootdev}:1 ${fdt_addr} ${fdt_file}
 setenv bootargs ${cmdline} ${extra_bootargs}
+
+#Load base device tree.
+ext2load mmc ${bootdev}:1 ${fdt_addr} ${fdt_file}
+fdt addr ${fdt_addr}
+fdt resize
+
+# Load overlays to arbitrary address beyond fdt location (will apply them later).
+setexpr fdtovaddr ${fdt_addr} + F000
+
+# Parse overlays.txt and apply individual overlays.
+if ext2load mmc ${bootdev}:1 ${fdtovaddr} overlays.txt && env import -t ${fdtovaddr} ${filesize} && test -n ${overlay}; then
+	echo loaded overlay.txt: ${overlay}
+	for ov in ${overlay}; do
+		if ext2load mmc ${bootdev}:1 ${fdtovaddr} ${ov}.dtbo && fdt apply ${fdtovaddr}; then
+			echo overlay of ${ov} successful
+		else
+			echo overlay failed, reverting to original device tree
+			ext2load mmc ${bootdev}:1 ${fdt_addr} ${fdt_file}
+			booti ${loadaddr} - ${fdt_addr}
+		fi
+	done
+fi
+
 booti ${loadaddr} - ${fdt_addr}
diff --git a/debian/changelog b/debian/changelog
index ce6ca6a..d4fc031 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+uboot-imx (0.6) beaker; urgency=medium
+
+  * Add ability to load device tree overlays in boot partition.
+
+ -- AIY Projects <support-aiyprojects@google.com>  Tue, 15 Jan 2019 15:21:16 -0800
+
 uboot-imx (0.5) animal; urgency=medium
 
   * Add fallback selection for device tree
diff --git a/debian/overlays.txt b/debian/overlays.txt
new file mode 100644
index 0000000..dcd7c44
--- /dev/null
+++ b/debian/overlays.txt
@@ -0,0 +1,2 @@
+# List of device tree overlays to load. Format: overlay=<dtbo name, no extenstion> <dtbo2> ...
+overlay=
diff --git a/debian/rules b/debian/rules
index e730cc2..64f1291 100755
--- a/debian/rules
+++ b/debian/rules
@@ -38,4 +38,5 @@
 	install -d debian/uboot-imx/boot
 	install -m 644 -o 0 -g 0 u-boot.imx debian/uboot-imx/boot/u-boot.imx
 	install -m 644 -o 0 -g 0 boot.scr debian/uboot-imx/boot/boot.scr
+	install -m 644 -o 0 -g 0 debian/overlays.txt debian/uboot-imx/boot/overlays.txt
 	dh_install
diff --git a/debian/uboot-imx.conffiles b/debian/uboot-imx.conffiles
new file mode 100644
index 0000000..73c7f8f
--- /dev/null
+++ b/debian/uboot-imx.conffiles
@@ -0,0 +1 @@
+/boot/overlays.txt