When parsing a FIT image source (ITS), mkimage does not currently check
whether the image names referenced in the /configurations section (e.g.
"kernel", "fdt", "ramdisk", "loadables") actually exist in the /images
node.

This patch introduces a validation step during FIT import that iterates
over each configuration and verifies that all referenced image names are
defined under /images. If a missing image is detected, an appropriate
error is reported and mkimage exits with FDT_ERR_NOTFOUND.

This ensures that configuration integrity is validated at build time.

Signed-off-by: Aristo Chen <aristo.c...@canonical.com>
---
 tools/fit_image.c | 38 ++++++++++++++++++++++++++++++++++++++
 tools/mkimage.c   |  7 ++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/tools/fit_image.c b/tools/fit_image.c
index 3d2fbad3853..bf9a6fc369a 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -627,6 +627,7 @@ static int fit_import_data(struct image_tool_params 
*params, const char *fname)
        struct stat sbuf;
        int ret;
        int images;
+       int confs;
        int node;
 
        fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
@@ -695,6 +696,43 @@ static int fit_import_data(struct image_tool_params 
*params, const char *fname)
                }
        }
 
+       confs = fdt_path_offset(fdt, FIT_CONFS_PATH);
+       static const char * const props[] = { FIT_KERNEL_PROP,
+                                             FIT_RAMDISK_PROP,
+                                             FIT_FDT_PROP,
+                                             FIT_LOADABLE_PROP,
+                                             FIT_FPGA_PROP,
+                                             FIT_FIRMWARE_PROP,
+                                             FIT_SCRIPT_PROP};
+
+       fdt_for_each_subnode(node, fdt, confs) {
+               const char *conf_name = fdt_get_name(fdt, node, NULL);
+
+               for (int i = 0; i < ARRAY_SIZE(props); i++) {
+                       int count = fdt_stringlist_count(fdt, node, props[i]);
+
+                       if (count < 0)
+                               continue;
+
+                       for (int j = 0; j < count; j++) {
+                               const char *img_name =
+                                       fdt_stringlist_get(fdt, node, props[i], 
j, NULL);
+                               if (!img_name)
+                                       continue;
+
+                               int img = fdt_subnode_offset(fdt, images, 
img_name);
+
+                               if (img < 0) {
+                                       fprintf(stderr,
+                                               "Error: configuration '%s' 
references undefined image '%s' in property '%s'\n",
+                                               conf_name, img_name, props[i]);
+                                       ret = FDT_ERR_NOTFOUND;
+                                       goto err_munmap;
+                               }
+                       }
+               }
+       }
+
        munmap(old_fdt, sbuf.st_size);
 
        /* Close the old fd so we can re-use it. */
diff --git a/tools/mkimage.c b/tools/mkimage.c
index 2954626a283..361711c53b2 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -519,8 +519,13 @@ int main(int argc, char **argv)
                         */
                        retval = tparams->fflag_handle(&params);
 
-               if (retval != EXIT_SUCCESS)
+               if (retval != EXIT_SUCCESS) {
+                       if (retval == FDT_ERR_NOTFOUND) {
+                               // Already printed error, exit cleanly
+                               exit(EXIT_FAILURE);
+                       }
                        usage("Bad parameters for FIT image type");
+               }
        }
 
        if (params.lflag || params.fflag) {
-- 
2.43.0

Reply via email to