Implement openwrt_read_bootflow() to discover OpenWrt FIT images on
raw block device partitions.

For each partition bootstd offers, read the first block and check for
a valid FDT header via fdt_check_header(). If the header is valid the
partition likely contains a FIT image and the bootflow is marked
BOOTFLOWST_READY.

Full FIT validation, configuration resolution, and sub-image loading
are all deferred to boot() which uses image_loader — keeping the scan
path fast and avoiding large reads for embedded-data FIT images that
can be many megabytes.

Signed-off-by: Daniel Golle <[email protected]>
---
 boot/bootmeth_openwrt.c | 43 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/boot/bootmeth_openwrt.c b/boot/bootmeth_openwrt.c
index 02bf543031b..08c3f98957a 100644
--- a/boot/bootmeth_openwrt.c
+++ b/boot/bootmeth_openwrt.c
@@ -16,6 +16,10 @@
 #include <image-loader.h>
 #include <malloc.h>
 #include <mapmem.h>
+#include <memalign.h>
+#include <part.h>
+#include <linux/libfdt.h>
+#include <linux/sizes.h>
 
 static int openwrt_check(struct udevice *dev, struct bootflow_iter *iter)
 {
@@ -27,7 +31,44 @@ static int openwrt_check(struct udevice *dev, struct 
bootflow_iter *iter)
 
 static int openwrt_read_bootflow(struct udevice *dev, struct bootflow *bflow)
 {
-       return log_msg_ret("nyi", -ENOENT);
+       struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
+       const char *part_name = NULL;
+       struct disk_partition info;
+       void *buf;
+       int ret;
+
+       /* Get partition geometry */
+       ret = part_get_info(desc, bflow->part, &info);
+       if (ret)
+               return log_msg_ret("part", ret);
+
+       part_name = (const char *)info.name;
+
+       /* Read first block to probe for an FDT/FIT header */
+       buf = memalign(SZ_1K, desc->blksz);
+       if (!buf)
+               return log_msg_ret("mem", -ENOMEM);
+
+       ret = blk_read(bflow->blk, info.start, 1, buf);
+       if (ret != 1) {
+               free(buf);
+               return log_msg_ret("rd", -EIO);
+       }
+
+       /* Must start with a valid FDT header */
+       if (fdt_check_header(buf)) {
+               free(buf);
+               return -ENOENT;
+       }
+
+       free(buf);
+
+       /* Show the GPT partition label as Filename */
+       bflow->fname = strdup(part_name);
+
+       bflow->state = BOOTFLOWST_READY;
+
+       return 0;
 }
 
 static int openwrt_boot(struct udevice *dev, struct bootflow *bflow)
-- 
2.53.0

Reply via email to