kernel: generic: improve FIT partition parser
authorDaniel Golle <daniel@makrotopia.org>
Wed, 23 Mar 2022 19:29:07 +0000 (19:29 +0000)
committerDaniel Golle <daniel@makrotopia.org>
Thu, 24 Mar 2022 00:46:45 +0000 (00:46 +0000)
 * only map filesystems configured in 'loadables'
 * allow mapping more than one filesystem (e.g. customization/branding
   or localization in addition to rootfs)
 * small cleaning here and there

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
target/linux/generic/files/block/partitions/fit.c
target/linux/generic/hack-5.10/410-block-fit-partition-parser.patch

index 89b5fb3454f0b69cdcfe53958169fb2a6f9c48a9..f6f15f538a5b31736f2ca28f3214e669f0820eec 100644 (file)
@@ -89,12 +89,10 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
        size_t label_min;
        struct device_node *np = NULL;
        const char *bootconf;
-
-       np = of_find_node_by_path("/chosen");
-       if (np)
-               bootconf = of_get_property(np, "bootconf", NULL);
-       else
-               bootconf = NULL;
+       char *loadable;
+       char *select_rootfs = NULL;
+       bool found;
+       int loadables_rem_len, loadable_len;
 
        if (fit_start_sector % (1<<(PAGE_SHIFT - SECTOR_SHIFT)))
                return -ERANGE;
@@ -123,7 +121,6 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                dsectors = (dsectors>sectors)?sectors:dsectors;
 
        dsize = dsectors << SECTOR_SHIFT;
-
        size = fdt_totalsize(init_fit);
 
        /* silently skip non-external-data legacy FIT images */
@@ -143,6 +140,12 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
        if (!fit)
                return -ENOMEM;
 
+       np = of_find_node_by_path("/chosen");
+       if (np)
+               bootconf = of_get_property(np, "bootconf", NULL);
+       else
+               bootconf = NULL;
+
        config = fdt_path_offset(fit, FIT_CONFS_PATH);
        if (config < 0) {
                printk(KERN_ERR "FIT: Cannot find %s node: %d\n", FIT_CONFS_PATH, images);
@@ -172,6 +175,12 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                bootconf?"Selected":"Default", bootconf?:config_default,
                config_description?" (":"", config_description?:"", config_description?")":"");
 
+       if (!config_loadables || !config_loadables_len) {
+               printk(KERN_ERR "FIT: No loadables configured in \"%s\"\n", bootconf?:config_default);
+               ret = -ENOENT;
+               goto ret_out;
+       }
+
        images = fdt_path_offset(fit, FIT_IMAGES_PATH);
        if (images < 0) {
                printk(KERN_ERR "FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
@@ -208,6 +217,22 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                if (strcmp(image_type, FIT_FILESYSTEM_PROP))
                        continue;
 
+               /* check if sub-image is part of configured loadables */
+               found = false;
+               loadable = config_loadables;
+               loadables_rem_len = config_loadables_len;
+               while (loadables_rem_len > 1) {
+                       loadable_len = strnlen(loadable, loadables_rem_len - 1) + 1;
+                       loadables_rem_len -= loadable_len;
+                       if (!strncmp(image_name, loadable, loadable_len)) {
+                               found = true;
+                               break;
+                       }
+                       loadable += loadable_len;
+               }
+               if (!found)
+                       continue;
+
                if (image_pos & ((1 << PAGE_SHIFT)-1)) {
                        printk(KERN_ERR "FIT: image %s start not aligned to page boundaries, skipping\n", image_name);
                        continue;
@@ -228,7 +253,8 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                }
 
                put_partition(state, ++(*slot), fit_start_sector + start_sect, nr_sects);
-               state->parts[*slot].flags = 0;
+               state->parts[*slot].flags = ADDPART_FLAG_READONLY;
+               state->parts[*slot].has_info = true;
                info = &state->parts[*slot].info;
 
                label_min = min_t(int, sizeof(info->volname) - 1, image_name_len);
@@ -238,14 +264,16 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
                strlcat(state->pp_buf, tmp, PAGE_SIZE);
 
-               state->parts[*slot].has_info = true;
-               state->parts[*slot].flags |= ADDPART_FLAG_READONLY;
-               if (config_loadables && !strcmp(image_name, config_loadables)) {
-                       printk(KERN_DEBUG "FIT: selecting configured loadable \"%s\" to be root filesystem\n", image_name);
+               /* Mark first loadable listed to be mounted as rootfs */
+               if (!strcmp(image_name, config_loadables)) {
+                       select_rootfs = image_name;
                        state->parts[*slot].flags |= ADDPART_FLAG_ROOTDEV;
                }
        }
 
+       if (select_rootfs)
+               printk(KERN_DEBUG "FIT: selecting configured loadable \"%s\" to be root filesystem\n", select_rootfs);
+
        if (add_remain && (imgmaxsect + MIN_FREE_SECT) < dsectors) {
                put_partition(state, ++(*slot), fit_start_sector + imgmaxsect, dsectors - imgmaxsect);
                state->parts[*slot].flags = 0;
index bc48296d9c9fb4a5ecd203caa87fcac4d7dac757..781635622782ea4ff935a85a71ad787402592e6a 100644 (file)
        .name           = "mtdblock",
        .major          = MTD_BLOCK_MAJOR,
 +#ifdef CONFIG_FIT_PARTITION
-+      .part_bits      = 1,
++      .part_bits      = 2,
 +#else
        .part_bits      = 0,
 +#endif