From 67c05a07288ef24b3ac9d013f35a0868d26a34c7 Mon Sep 17 00:00:00 2001
From: Peter Robinson <pbrobinson@gmail.com>
Date: Tue, 18 Jun 2019 11:51:21 +0100
Subject: [PATCH 1/2] distro: load FDT from any partition on boot device

In the EFI_LOADER boot path, we were only checking the FAT partition
containing the EFI payload for dtb files.  But this is somewhat of a
fiction.  In reality there will be one small (V)FAT partition containing
grub (or whatever the payload may be), and a second boot partition
containing kernel/initrd/fdt (typically ext4).  It is this second
partition where we should be looking for a FDT to load.

So instead scan all the partitions of the disk containing the EFI
payload.  This matches where grub looks for kernel/initrd (barring
custom grub.cfg, in which case the user can use grub's 'devicetree'
command to load the correct FDT).

The other option is somehow passing the ${fdtfile} to grub so that it
can load the FDT based on selected kernel version location (which grub
knows) and SoC/board specific ${fdtfile} (which grub does not know).

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Peter Robinson <pbrobinson@gmail.com>
---
 include/config_distro_bootcmd.h | 35 ++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 12 deletions(-)
 
diff --git a/include/config_distro_bootcmd.h b/include/config_distro_bootcmd.h
index 2627c2a6a5..eadd1080b3 100644
--- a/include/config_distro_bootcmd.h
+++ b/include/config_distro_bootcmd.h
@@ -148,26 +148,37 @@
 		"fi\0"                                                    \
 	\
 	"load_efi_dtb="                                                   \
-		"load ${devtype} ${devnum}:${distro_bootpart} "           \
-			"${fdt_addr_r} ${prefix}${efi_fdtfile}\0"         \
+		"load ${devtype} ${devnum}:${dtb_devp} "                  \
+			"${fdt_addr_r} ${prefix}${efi_fdtfile} && "       \
+		"run boot_efi_binary\0"                                   \
 	\
 	"efi_dtb_prefixes=/ /dtb/ /dtb/current/\0"                        \
-	"scan_dev_for_efi="                                               \
+	"scan_dev_for_dtb="                                               \
 		"setenv efi_fdtfile ${fdtfile}; "                         \
 		BOOTENV_EFI_SET_FDTFILE_FALLBACK                          \
-		"for prefix in ${efi_dtb_prefixes}; do "                  \
-			"if test -e ${devtype} "                          \
-					"${devnum}:${distro_bootpart} "   \
-					"${prefix}${efi_fdtfile}; then "  \
-				"run load_efi_dtb; "                      \
-			"fi;"                                             \
-		"done;"                                                   \
-		"run boot_efi_bootmgr;"                                   \
+		"part list ${devtype} ${devnum} dtb_devplist; "           \
+		"env exists dtb_devplist || setenv dtb_devplist "         \
+			"${distro_bootpart}; "                            \
+		"for dtb_devp in ${dtb_devplist}; do "                    \
+			"for prefix in ${efi_dtb_prefixes}; do "          \
+				"if test -e ${devtype} "                  \
+						"${devnum}:${dtb_devp} "  \
+						"${prefix}${efi_fdtfile};"\
+						" then "                  \
+					"echo Found DTB ${devtype} "      \
+						"${devnum}:${dtb_devp} "  \
+						"${prefix}${efi_fdtfile};"\
+					"run load_efi_dtb; "              \
+				"fi;"                                     \
+			"done; "                                          \
+		"done; "                                                  \
+		"run boot_efi_binary\0"                                   \
+	"scan_dev_for_efi="                                               \
 		"if test -e ${devtype} ${devnum}:${distro_bootpart} "     \
 					"efi/boot/"BOOTEFI_NAME"; then "  \
 				"echo Found EFI removable media binary "  \
 					"efi/boot/"BOOTEFI_NAME"; "       \
-				"run boot_efi_binary; "                   \
+				"run scan_dev_for_dtb; "                  \
 				"echo EFI LOAD FAILED: continuing...; "   \
 		"fi; "                                                    \
 		"setenv efi_fdtfile\0"
-- 