kernel: update FIT partition parser to new property name
[openwrt/openwrt.git] / target / linux / generic / files / block / partitions / fit.c
index 27e44a4af4739f06fc39bb067fe4c55555ea7dbd..ce6a2b54115c970d615494f167426c0308fd0f4d 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/of_device.h>
 #include <linux/of_fdt.h>
 #include <linux/libfdt.h>
+#include <linux/version.h>
 
 #include "check.h"
 
 
 int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector, u64 sectors, int *slot, int add_remain)
 {
-       struct address_space *mapping = state->bdev->bd_inode->i_mapping;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)
+       struct block_device *bdev = state->disk->part0;
+#else
+       struct block_device *bdev = state->bdev;
+#endif
+       struct address_space *mapping = bdev->bd_inode->i_mapping;
        struct page *page;
        void *fit, *init_fit;
        struct partition_meta_info *info;
@@ -87,13 +93,22 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                config_description_len, config_loadables_len;
        sector_t start_sect, nr_sects;
        size_t label_min;
+       struct device_node *np = NULL;
+       const char *bootconf;
+       const char *loadable;
+       const char *select_rootfs = NULL;
+       bool found;
+       int loadables_rem_len, loadable_len;
 
        if (fit_start_sector % (1<<(PAGE_SHIFT - SECTOR_SHIFT)))
                return -ERANGE;
 
        page = read_mapping_page(mapping, fit_start_sector >> (PAGE_SHIFT - SECTOR_SHIFT), NULL);
-       if (!page)
-               return -ENOMEM;
+       if (IS_ERR(page))
+               return -EFAULT;
+
+       if (PageError(page))
+               return -EFAULT;
 
        init_fit = page_address(page);
 
@@ -107,12 +122,11 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
                return 0;
        }
 
-       dsectors = get_capacity(state->bdev->bd_disk);
+       dsectors = get_capacity(bdev->bd_disk);
        if (sectors)
                dsectors = (dsectors>sectors)?sectors:dsectors;
 
        dsize = dsectors << SECTOR_SHIFT;
-
        size = fdt_totalsize(init_fit);
 
        /* silently skip non-external-data legacy FIT images */
@@ -132,6 +146,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, "u-boot,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);
@@ -141,15 +161,15 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
 
        config_default = fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
 
-       if (!config_default) {
+       if (!config_default && !bootconf) {
                printk(KERN_ERR "FIT: Cannot find default configuration\n");
                ret = -ENOENT;
                goto ret_out;
        }
 
-       node = fdt_subnode_offset(fit, config, config_default);
+       node = fdt_subnode_offset(fit, config, bootconf?:config_default);
        if (node < 0) {
-               printk(KERN_ERR "FIT: Cannot find %s node: %d\n", config_default, node);
+               printk(KERN_ERR "FIT: Cannot find %s node: %d\n", bootconf?:config_default, node);
                ret = -ENOENT;
                goto ret_out;
        }
@@ -157,9 +177,16 @@ int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
        config_description = fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
        config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP, &config_loadables_len);
 
-       printk(KERN_DEBUG "FIT: Default configuration: \"%s\"%s%s%s\n", config_default,
+       printk(KERN_DEBUG "FIT: %s configuration: \"%s\"%s%s%s\n",
+               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);
@@ -196,6 +223,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;
@@ -216,7 +259,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);
@@ -226,14 +270,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;
-
-               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;