diff mbox series

[v2,2/5] block: add partition parser for U-Boot uImage.FIT

Message ID YnjxXASWU5Ps9ZoA@makrotopia.org
State Superseded
Headers show
Series [v2,1/5] block: add new flag to add partitions read-only | expand

Commit Message

Daniel Golle May 9, 2022, 10:47 a.m. UTC
Introduce a new partition parser for U-Boot's Flattened-Image-Tree (FIT) in
order to allow Linux to mount the filesystem part of a uImage.FIT.

uImage.FIT needs to be created with external data and aligned to the
system's memory page size. e.g.
 mkimage -E -B 0x1000 -p 0x1000 ...

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
---
 MAINTAINERS               |   6 +
 block/partitions/Kconfig  |  14 ++
 block/partitions/Makefile |   1 +
 block/partitions/check.h  |   4 +
 block/partitions/core.c   |   3 +
 block/partitions/fit.c    | 352 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 380 insertions(+)
 create mode 100644 block/partitions/fit.c

Comments

kernel test robot May 12, 2022, 1:25 p.m. UTC | #1
Hi Daniel,

I love your patch! Perhaps something to improve:

[auto build test WARNING on axboe-block/for-next]
[also build test WARNING on mtd/mtd/next mtd/mtd/fixes v5.18-rc6 next-20220512]
[cannot apply to rw-ubifs/next rw-ubifs/fixes]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Golle/partition-parser-for-U-Boot-s-uImage-FIT/20220509-185349
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: sh-allyesconfig (https://download.01.org/0day-ci/archive/20220512/202205122136.1KUX4NOx-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 11.3.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # apt-get install sparse
        # sparse version: v0.6.4-dirty
        # https://github.com/intel-lab-lkp/linux/commit/5a69884cfc6091e8d7c5491fbf57dd5e13cd5ee8
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Daniel-Golle/partition-parser-for-U-Boot-s-uImage-FIT/20220509-185349
        git checkout 5a69884cfc6091e8d7c5491fbf57dd5e13cd5ee8
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=sh SHELL=/bin/bash block/partitions/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)
>> block/partitions/fit.c:243:29: sparse: sparse: cast to restricted __be32
>> block/partitions/fit.c:243:29: sparse: sparse: cast to restricted __be32
>> block/partitions/fit.c:243:29: sparse: sparse: cast to restricted __be32
>> block/partitions/fit.c:243:29: sparse: sparse: cast to restricted __be32
>> block/partitions/fit.c:243:29: sparse: sparse: cast to restricted __be32
>> block/partitions/fit.c:243:29: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:248:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:248:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:248:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:248:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:248:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:248:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:250:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:250:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:250:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:250:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:250:37: sparse: sparse: cast to restricted __be32
   block/partitions/fit.c:250:37: sparse: sparse: cast to restricted __be32

vim +243 block/partitions/fit.c

    69	
    70	/**
    71	 * parse_fit_partitions - map uImage.FIT filesystem sub-images into sub-partitions
    72	 * @state: pointer to partition parser state
    73	 * @fit_start_sector: start sector of the FIT structure on disk
    74	 * @sectors: number of sectors of the uImage.FIT partition or 0 if whole device
    75	 * @slot: pointer to the current partition slot number
    76	 * @add_remain: map unused sectors into additional partition
    77	 *
    78	 * To be called by other partition parsers on physical block devices or using
    79	 * wrapper function int fit_partition(struct parsed_partitions *state) for the
    80	 * whole disk, relevant typically for ubiblock or mtdblock devices.
    81	 */
    82	int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
    83				 u64 sectors, int *slot, int max_slot, bool add_remain)
    84	{
    85		struct block_device *bdev = state->disk->part0;
    86		struct address_space *mapping = bdev->bd_inode->i_mapping;
    87		struct page *page;
    88		void *fit, *init_fit;
    89		struct partition_meta_info *info;
    90		char tmp[sizeof(info->volname)];
    91		u64 dsize, dsectors, imgmaxsect = 0;
    92		u32 size, image_pos, image_len;
    93		const u32 *image_offset_be, *image_len_be, *image_pos_be;
    94		int ret = 1, node, images, config;
    95		const char *image_name, *image_type, *image_description,
    96			*config_default, *config_description, *config_loadables;
    97		int image_name_len, image_type_len, image_description_len,
    98			config_default_len, config_description_len,
    99			config_loadables_len;
   100		sector_t start_sect, nr_sects;
   101		size_t label_min;
   102		struct device_node *np = NULL;
   103		const char *bootconf;
   104		const char *loadable;
   105		bool found;
   106		int loadables_rem_len, loadable_len;
   107		u16 loadcnt;
   108	
   109		/* uImage.FIT should be aligned to page boundaries */
   110		if (fit_start_sector % (1 << (PAGE_SHIFT - SECTOR_SHIFT)))
   111			return 0;
   112	
   113		/* map first page */
   114		page = read_mapping_page(
   115			mapping, fit_start_sector >> (PAGE_SHIFT - SECTOR_SHIFT), NULL);
   116	
   117		if (IS_ERR(page))
   118			return -EFAULT;
   119	
   120		if (PageError(page))
   121			return -EFAULT;
   122	
   123		init_fit = page_address(page);
   124	
   125		if (!init_fit) {
   126			put_page(page);
   127			return -EFAULT;
   128		}
   129	
   130		/* uImage.FIT is based on flattened device tree structure */
   131		if (fdt_check_header(init_fit)) {
   132			put_page(page);
   133			return 0;
   134		}
   135	
   136		/* acquire disk or partition size */
   137		dsectors = get_capacity(bdev->bd_disk);
   138		if (sectors)
   139			dsectors = min_t(u64, sectors, dsectors);
   140	
   141		dsize = dsectors << SECTOR_SHIFT;
   142		size = fdt_totalsize(init_fit);
   143	
   144		/* silently skip non-external-data legacy uImage.FIT */
   145		if (size > PAGE_SIZE) {
   146			put_page(page);
   147			return 0;
   148		}
   149	
   150		/* abort if FIT structure is larger than disk or partition size */
   151		if (size >= dsize) {
   152			state->access_beyond_eod = 1;
   153			put_page(page);
   154			return -EFBIG;
   155		}
   156	
   157		/*
   158		 * copy FIT structure for further processing
   159		 * this is necessary for libfdt to work
   160		 */
   161		fit = kmemdup(init_fit, size, GFP_KERNEL);
   162		put_page(page);
   163		if (!fit)
   164			return -ENOMEM;
   165	
   166		/* set boot config node name U-Boot may have added to the device tree */
   167		np = of_find_node_by_path("/chosen");
   168		if (np)
   169			bootconf = of_get_property(np, "u-boot,bootconf", NULL);
   170		else
   171			bootconf = NULL;
   172	
   173		/* find configuration path in uImage.FIT */
   174		config = fdt_path_offset(fit, FIT_CONFS_PATH);
   175		if (config < 0) {
   176			pr_err("FIT: Cannot find %s node: %d\n",
   177			       FIT_CONFS_PATH, config);
   178			ret = -ENOENT;
   179			goto ret_out;
   180		}
   181	
   182		/* get default configuration node name */
   183		config_default =
   184			fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
   185	
   186		/* make sure we got either default or selected boot config node name */
   187		if (!config_default && !bootconf) {
   188			pr_err("FIT: Cannot find default configuration\n");
   189			ret = -ENOENT;
   190			goto ret_out;
   191		}
   192	
   193		/* find selected boot config node, fallback on default config node */
   194		node = fdt_subnode_offset(fit, config, bootconf ?: config_default);
   195		if (node < 0) {
   196			pr_err("FIT: Cannot find %s node: %d\n",
   197			       bootconf ?: config_default, node);
   198			ret = -ENOENT;
   199			goto ret_out;
   200		}
   201	
   202		/* get selected configuration data */
   203		config_description =
   204			fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
   205		config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP,
   206					       &config_loadables_len);
   207	
   208		pr_info("FIT: %s configuration: \"%s\"%s%s%s\n",
   209			bootconf ? "Selected" : "Default", bootconf ?: config_default,
   210			config_description ? " (" : "", config_description ?: "",
   211			config_description ? ")" : "");
   212	
   213		if (!config_loadables || !config_loadables_len) {
   214			pr_err("FIT: No loadables configured in \"%s\"\n",
   215			       bootconf ?: config_default);
   216			ret = -ENOENT;
   217			goto ret_out;
   218		}
   219	
   220		/* get images path in uImage.FIT */
   221		images = fdt_path_offset(fit, FIT_IMAGES_PATH);
   222		if (images < 0) {
   223			pr_err("FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
   224			ret = -EINVAL;
   225			goto ret_out;
   226		}
   227	
   228		/* allocate one slot for mapping remaing space */
   229		if (add_remain)
   230			--max_slot;
   231	
   232		/* iterate over images in uImage.FIT */
   233		fdt_for_each_subnode(node, fit, images) {
   234			image_name = fdt_get_name(fit, node, &image_name_len);
   235			image_type = fdt_getprop(fit, node, FIT_TYPE_PROP, &image_type_len);
   236			image_offset_be = fdt_getprop(fit, node, FIT_DATA_OFFSET_PROP, NULL);
   237			image_pos_be = fdt_getprop(fit, node, FIT_DATA_POSITION_PROP, NULL);
   238			image_len_be = fdt_getprop(fit, node, FIT_DATA_SIZE_PROP, NULL);
   239	
   240			if (!image_name || !image_type || !image_len_be)
   241				continue;
   242	
 > 243			image_len = be32_to_cpu(*image_len_be);
   244			if (!image_len)
   245				continue;
   246	
   247			if (image_offset_be)
   248				image_pos = be32_to_cpu(*image_offset_be) + size;
   249			else if (image_pos_be)
   250				image_pos = be32_to_cpu(*image_pos_be);
   251			else
   252				continue;
   253	
   254			image_description = fdt_getprop(fit, node, FIT_DESC_PROP,
   255							&image_description_len);
   256	
   257			pr_info("FIT: %16s sub-image 0x%08x..0x%08x \"%s\" %s%s%s\n",
   258				image_type, image_pos, image_pos + image_len - 1,
   259				image_name, image_description ? "(" : "",
   260				image_description ?: "", image_description ? ") " : "");
   261	
   262			/* only 'filesystem' images should be mapped as partitions */
   263			if (strcmp(image_type, FIT_FILESYSTEM_PROP))
   264				continue;
   265	
   266			/* check if sub-image is part of configured loadables */
   267			found = false;
   268			loadable = config_loadables;
   269			loadables_rem_len = config_loadables_len;
   270			for (loadcnt = 0; loadables_rem_len > 1 &&
   271					  loadcnt < MAX_FIT_LOADABLES; ++loadcnt) {
   272				loadable_len =
   273					strnlen(loadable, loadables_rem_len - 1) + 1;
   274				loadables_rem_len -= loadable_len;
   275				if (!strncmp(image_name, loadable, loadable_len)) {
   276					found = true;
   277					break;
   278				}
   279				loadable += loadable_len;
   280			}
   281			if (!found)
   282				continue;
   283	
   284			if (image_pos % (1 << PAGE_SHIFT)) {
   285				pr_err("FIT: image %s start not aligned to page boundaries, skipping\n",
   286				       image_name);
   287				continue;
   288			}
   289	
   290			if (image_len % (1 << PAGE_SHIFT)) {
   291				pr_err("FIT: sub-image %s end not aligned to page boundaries, skipping\n",
   292				       image_name);
   293				continue;
   294			}
   295	
   296			start_sect = image_pos >> SECTOR_SHIFT;
   297			nr_sects = image_len >> SECTOR_SHIFT;
   298			imgmaxsect = (imgmaxsect < (start_sect + nr_sects)) ?
   299					     (start_sect + nr_sects) :
   300						   imgmaxsect;
   301	
   302			if (start_sect + nr_sects > dsectors) {
   303				state->access_beyond_eod = 1;
   304				continue;
   305			}
   306	
   307			put_partition(state, *slot, fit_start_sector + start_sect,
   308				      nr_sects);
   309			state->parts[*slot].flags = ADDPART_FLAG_READONLY;
   310			state->parts[*slot].has_info = true;
   311			info = &state->parts[*slot].info;
   312	
   313			label_min = min_t(int, sizeof(info->volname) - 1, image_name_len);
   314			strncpy(info->volname, image_name, label_min);
   315			info->volname[label_min] = '\0';
   316	
   317			snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
   318			strlcat(state->pp_buf, tmp, PAGE_SIZE);
   319	
   320			if (++(*slot) > max_slot)
   321				break;
   322		}
   323	
   324		/* in case uImage.FIT is stored in a partition, map the remaining space */
   325		if (add_remain && (imgmaxsect + MIN_FREE_SECT) < dsectors) {
   326			put_partition(state, *slot, fit_start_sector + imgmaxsect,
   327				      dsectors - imgmaxsect);
   328			state->parts[*slot].flags = 0;
   329			info = &state->parts[*slot].info;
   330			strcpy(info->volname, REMAIN_VOLNAME);
   331			snprintf(tmp, sizeof(tmp), "(%s)", REMAIN_VOLNAME);
   332			strlcat(state->pp_buf, tmp, PAGE_SIZE);
   333			++(*slot);
   334		}
   335	ret_out:
   336		kfree(fit);
   337		return ret;
   338	}
   339
kernel test robot May 12, 2022, 6:22 p.m. UTC | #2
Hi Daniel,

I love your patch! Yet something to improve:

[auto build test ERROR on axboe-block/for-next]
[also build test ERROR on mtd/mtd/next mtd/mtd/fixes v5.18-rc6 next-20220512]
[cannot apply to rw-ubifs/next rw-ubifs/fixes]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Golle/partition-parser-for-U-Boot-s-uImage-FIT/20220509-185349
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: xtensa-buildonly-randconfig-r001-20220512 (https://download.01.org/0day-ci/archive/20220513/202205130213.kRDmMP6B-lkp@intel.com/config)
compiler: xtensa-linux-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/5a69884cfc6091e8d7c5491fbf57dd5e13cd5ee8
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Daniel-Golle/partition-parser-for-U-Boot-s-uImage-FIT/20220509-185349
        git checkout 5a69884cfc6091e8d7c5491fbf57dd5e13cd5ee8
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=xtensa SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x11c): undefined reference to `fdt_check_header'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x262): undefined reference to `fdt_check_header'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x124): undefined reference to `fdt_path_offset'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x320): undefined reference to `fdt_path_offset'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x128): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x337): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x130): undefined reference to `fdt_subnode_offset'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x356): undefined reference to `fdt_subnode_offset'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
   fit.c:(.text+0x138): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x382): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
   fit.c:(.text+0x13c): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x397): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
   fit.c:(.text+0x148): undefined reference to `fdt_path_offset'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x3e3): undefined reference to `fdt_path_offset'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x150): undefined reference to `fdt_first_subnode'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x412): undefined reference to `fdt_first_subnode'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x154): undefined reference to `fdt_get_name'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x423): undefined reference to `fdt_get_name'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
   fit.c:(.text+0x158): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x43a): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
   fit.c:(.text+0x15c): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x44b): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
   fit.c:(.text+0x160): undefined reference to `fdt_getprop'
   xtensa-linux-ld: block/partitions/fit.o:fit.c:(.text+0x45f): more undefined references to `fdt_getprop' follow
   xtensa-linux-ld: block/partitions/fit.o: in function `put_page':
>> fit.c:(.text+0x184): undefined reference to `fdt_next_subnode'
   xtensa-linux-ld: block/partitions/fit.o: in function `parse_fit_partitions':
   fit.c:(.text+0x6ae): undefined reference to `fdt_next_subnode'
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index d4d4aa20fd0847..c9d3775b795ab5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -7687,6 +7687,12 @@  F:	Documentation/firmware_class/
 F:	drivers/base/firmware_loader/
 F:	include/linux/firmware.h
 
+FIT PARTITION TABLE (uImage.FIT)
+M:	Daniel Golle <daniel@makrotopia.org>
+L:	linux-block@vger.kernel.org
+S:	Maintained
+F:	block/partitions/fit.c
+
 FLEXTIMER FTM-QUADDEC DRIVER
 M:	Patrick Havelange <patrick.havelange@essensium.com>
 L:	linux-iio@vger.kernel.org
diff --git a/block/partitions/Kconfig b/block/partitions/Kconfig
index 7aff4eb81c60f4..65d55885321722 100644
--- a/block/partitions/Kconfig
+++ b/block/partitions/Kconfig
@@ -103,6 +103,20 @@  config ATARI_PARTITION
 	  Say Y here if you would like to use hard disks under Linux which
 	  were partitioned under the Atari OS.
 
+config FIT_PARTITION
+	bool "Flattened-Image-Tree (FIT) partition support" if PARTITION_ADVANCED
+	default n
+	help
+	  Say Y here if your system needs to mount the filesystem part of
+	  a Flattened-Image-Tree (FIT) image commonly used with Das U-Boot.
+
+	  uImage.FIT needs to be created with external data and aligned to
+	  the systems memory page size. e.g.
+	    mkimage -E -B 0x1000 -p 0x1000 ...
+
+	  If your system doesn't use U-Boot or you don't need to mount uImage.FIT
+	  filesystem sub-images in Linux, say N.
+
 config IBM_PARTITION
 	bool "IBM disk label and partition support"
 	depends on PARTITION_ADVANCED && S390
diff --git a/block/partitions/Makefile b/block/partitions/Makefile
index a7f05cdb02a844..d319eb1deba97a 100644
--- a/block/partitions/Makefile
+++ b/block/partitions/Makefile
@@ -8,6 +8,7 @@  obj-$(CONFIG_ACORN_PARTITION) += acorn.o
 obj-$(CONFIG_AMIGA_PARTITION) += amiga.o
 obj-$(CONFIG_ATARI_PARTITION) += atari.o
 obj-$(CONFIG_AIX_PARTITION) += aix.o
+obj-$(CONFIG_FIT_PARTITION) += fit.o
 obj-$(CONFIG_CMDLINE_PARTITION) += cmdline.o
 obj-$(CONFIG_MAC_PARTITION) += mac.o
 obj-$(CONFIG_LDM_PARTITION) += ldm.o
diff --git a/block/partitions/check.h b/block/partitions/check.h
index 4ffa2359b1a37e..9abdcbd7ece847 100644
--- a/block/partitions/check.h
+++ b/block/partitions/check.h
@@ -57,6 +57,7 @@  int amiga_partition(struct parsed_partitions *state);
 int atari_partition(struct parsed_partitions *state);
 int cmdline_partition(struct parsed_partitions *state);
 int efi_partition(struct parsed_partitions *state);
+int fit_partition(struct parsed_partitions *state);
 int ibm_partition(struct parsed_partitions *);
 int karma_partition(struct parsed_partitions *state);
 int ldm_partition(struct parsed_partitions *state);
@@ -67,3 +68,6 @@  int sgi_partition(struct parsed_partitions *state);
 int sun_partition(struct parsed_partitions *state);
 int sysv68_partition(struct parsed_partitions *state);
 int ultrix_partition(struct parsed_partitions *state);
+
+int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
+			 u64 sectors, int *slot, int max_slot, bool add_remain);
diff --git a/block/partitions/core.c b/block/partitions/core.c
index 3e70860beb655e..95a6e829448b07 100644
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
@@ -44,6 +44,9 @@  static int (*check_part[])(struct parsed_partitions *) = {
 #ifdef CONFIG_CMDLINE_PARTITION
 	cmdline_partition,
 #endif
+#ifdef CONFIG_FIT_PARTITION
+	fit_partition,
+#endif
 #ifdef CONFIG_EFI_PARTITION
 	efi_partition,		/* this must come before msdos */
 #endif
diff --git a/block/partitions/fit.c b/block/partitions/fit.c
new file mode 100644
index 00000000000000..076c7c7426a420
--- /dev/null
+++ b/block/partitions/fit.c
@@ -0,0 +1,352 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  fs/partitions/fit.c
+ *  Copyright (C) 2021  Daniel Golle
+ *
+ *  headers extracted from U-Boot mkimage sources
+ *  (C) Copyright 2008 Semihalf
+ *  (C) Copyright 2000-2005
+ *  Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ *  based on existing partition parsers
+ *  Copyright (C) 1991-1998  Linus Torvalds
+ *  Re-organised Feb 1998 Russell King
+ */
+
+#include <linux/libfdt.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_fdt.h>
+#include <linux/types.h>
+
+#include "check.h"
+
+#define FIT_IMAGES_PATH		"/images"
+#define FIT_CONFS_PATH		"/configurations"
+
+/* hash/signature/key node */
+#define FIT_HASH_NODENAME	"hash"
+#define FIT_ALGO_PROP		"algo"
+#define FIT_VALUE_PROP		"value"
+#define FIT_IGNORE_PROP		"uboot-ignore"
+#define FIT_SIG_NODENAME	"signature"
+#define FIT_KEY_REQUIRED	"required"
+#define FIT_KEY_HINT		"key-name-hint"
+
+/* cipher node */
+#define FIT_CIPHER_NODENAME	"cipher"
+#define FIT_ALGO_PROP		"algo"
+
+/* image node */
+#define FIT_DATA_PROP		"data"
+#define FIT_DATA_POSITION_PROP	"data-position"
+#define FIT_DATA_OFFSET_PROP	"data-offset"
+#define FIT_DATA_SIZE_PROP	"data-size"
+#define FIT_TIMESTAMP_PROP	"timestamp"
+#define FIT_DESC_PROP		"description"
+#define FIT_ARCH_PROP		"arch"
+#define FIT_TYPE_PROP		"type"
+#define FIT_OS_PROP		"os"
+#define FIT_COMP_PROP		"compression"
+#define FIT_ENTRY_PROP		"entry"
+#define FIT_LOAD_PROP		"load"
+
+/* configuration node */
+#define FIT_KERNEL_PROP		"kernel"
+#define FIT_FILESYSTEM_PROP	"filesystem"
+#define FIT_RAMDISK_PROP	"ramdisk"
+#define FIT_FDT_PROP		"fdt"
+#define FIT_LOADABLE_PROP	"loadables"
+#define FIT_DEFAULT_PROP	"default"
+#define FIT_SETUP_PROP		"setup"
+#define FIT_FPGA_PROP		"fpga"
+#define FIT_FIRMWARE_PROP	"firmware"
+#define FIT_STANDALONE_PROP	"standalone"
+
+#define MIN_FREE_SECT		16
+#define REMAIN_VOLNAME		"rootfs_data"
+#define MAX_FIT_LOADABLES	16
+
+/**
+ * parse_fit_partitions - map uImage.FIT filesystem sub-images into sub-partitions
+ * @state: pointer to partition parser state
+ * @fit_start_sector: start sector of the FIT structure on disk
+ * @sectors: number of sectors of the uImage.FIT partition or 0 if whole device
+ * @slot: pointer to the current partition slot number
+ * @add_remain: map unused sectors into additional partition
+ *
+ * To be called by other partition parsers on physical block devices or using
+ * wrapper function int fit_partition(struct parsed_partitions *state) for the
+ * whole disk, relevant typically for ubiblock or mtdblock devices.
+ */
+int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector,
+			 u64 sectors, int *slot, int max_slot, bool add_remain)
+{
+	struct block_device *bdev = state->disk->part0;
+	struct address_space *mapping = bdev->bd_inode->i_mapping;
+	struct page *page;
+	void *fit, *init_fit;
+	struct partition_meta_info *info;
+	char tmp[sizeof(info->volname)];
+	u64 dsize, dsectors, imgmaxsect = 0;
+	u32 size, image_pos, image_len;
+	const u32 *image_offset_be, *image_len_be, *image_pos_be;
+	int ret = 1, node, images, config;
+	const char *image_name, *image_type, *image_description,
+		*config_default, *config_description, *config_loadables;
+	int image_name_len, image_type_len, image_description_len,
+		config_default_len, 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;
+	bool found;
+	int loadables_rem_len, loadable_len;
+	u16 loadcnt;
+
+	/* uImage.FIT should be aligned to page boundaries */
+	if (fit_start_sector % (1 << (PAGE_SHIFT - SECTOR_SHIFT)))
+		return 0;
+
+	/* map first page */
+	page = read_mapping_page(
+		mapping, fit_start_sector >> (PAGE_SHIFT - SECTOR_SHIFT), NULL);
+
+	if (IS_ERR(page))
+		return -EFAULT;
+
+	if (PageError(page))
+		return -EFAULT;
+
+	init_fit = page_address(page);
+
+	if (!init_fit) {
+		put_page(page);
+		return -EFAULT;
+	}
+
+	/* uImage.FIT is based on flattened device tree structure */
+	if (fdt_check_header(init_fit)) {
+		put_page(page);
+		return 0;
+	}
+
+	/* acquire disk or partition size */
+	dsectors = get_capacity(bdev->bd_disk);
+	if (sectors)
+		dsectors = min_t(u64, sectors, dsectors);
+
+	dsize = dsectors << SECTOR_SHIFT;
+	size = fdt_totalsize(init_fit);
+
+	/* silently skip non-external-data legacy uImage.FIT */
+	if (size > PAGE_SIZE) {
+		put_page(page);
+		return 0;
+	}
+
+	/* abort if FIT structure is larger than disk or partition size */
+	if (size >= dsize) {
+		state->access_beyond_eod = 1;
+		put_page(page);
+		return -EFBIG;
+	}
+
+	/*
+	 * copy FIT structure for further processing
+	 * this is necessary for libfdt to work
+	 */
+	fit = kmemdup(init_fit, size, GFP_KERNEL);
+	put_page(page);
+	if (!fit)
+		return -ENOMEM;
+
+	/* set boot config node name U-Boot may have added to the device tree */
+	np = of_find_node_by_path("/chosen");
+	if (np)
+		bootconf = of_get_property(np, "u-boot,bootconf", NULL);
+	else
+		bootconf = NULL;
+
+	/* find configuration path in uImage.FIT */
+	config = fdt_path_offset(fit, FIT_CONFS_PATH);
+	if (config < 0) {
+		pr_err("FIT: Cannot find %s node: %d\n",
+		       FIT_CONFS_PATH, config);
+		ret = -ENOENT;
+		goto ret_out;
+	}
+
+	/* get default configuration node name */
+	config_default =
+		fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
+
+	/* make sure we got either default or selected boot config node name */
+	if (!config_default && !bootconf) {
+		pr_err("FIT: Cannot find default configuration\n");
+		ret = -ENOENT;
+		goto ret_out;
+	}
+
+	/* find selected boot config node, fallback on default config node */
+	node = fdt_subnode_offset(fit, config, bootconf ?: config_default);
+	if (node < 0) {
+		pr_err("FIT: Cannot find %s node: %d\n",
+		       bootconf ?: config_default, node);
+		ret = -ENOENT;
+		goto ret_out;
+	}
+
+	/* get selected configuration data */
+	config_description =
+		fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
+	config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP,
+				       &config_loadables_len);
+
+	pr_info("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) {
+		pr_err("FIT: No loadables configured in \"%s\"\n",
+		       bootconf ?: config_default);
+		ret = -ENOENT;
+		goto ret_out;
+	}
+
+	/* get images path in uImage.FIT */
+	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
+	if (images < 0) {
+		pr_err("FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
+		ret = -EINVAL;
+		goto ret_out;
+	}
+
+	/* allocate one slot for mapping remaing space */
+	if (add_remain)
+		--max_slot;
+
+	/* iterate over images in uImage.FIT */
+	fdt_for_each_subnode(node, fit, images) {
+		image_name = fdt_get_name(fit, node, &image_name_len);
+		image_type = fdt_getprop(fit, node, FIT_TYPE_PROP, &image_type_len);
+		image_offset_be = fdt_getprop(fit, node, FIT_DATA_OFFSET_PROP, NULL);
+		image_pos_be = fdt_getprop(fit, node, FIT_DATA_POSITION_PROP, NULL);
+		image_len_be = fdt_getprop(fit, node, FIT_DATA_SIZE_PROP, NULL);
+
+		if (!image_name || !image_type || !image_len_be)
+			continue;
+
+		image_len = be32_to_cpu(*image_len_be);
+		if (!image_len)
+			continue;
+
+		if (image_offset_be)
+			image_pos = be32_to_cpu(*image_offset_be) + size;
+		else if (image_pos_be)
+			image_pos = be32_to_cpu(*image_pos_be);
+		else
+			continue;
+
+		image_description = fdt_getprop(fit, node, FIT_DESC_PROP,
+						&image_description_len);
+
+		pr_info("FIT: %16s sub-image 0x%08x..0x%08x \"%s\" %s%s%s\n",
+			image_type, image_pos, image_pos + image_len - 1,
+			image_name, image_description ? "(" : "",
+			image_description ?: "", image_description ? ") " : "");
+
+		/* only 'filesystem' images should be mapped as partitions */
+		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;
+		for (loadcnt = 0; loadables_rem_len > 1 &&
+				  loadcnt < MAX_FIT_LOADABLES; ++loadcnt) {
+			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)) {
+			pr_err("FIT: image %s start not aligned to page boundaries, skipping\n",
+			       image_name);
+			continue;
+		}
+
+		if (image_len % (1 << PAGE_SHIFT)) {
+			pr_err("FIT: sub-image %s end not aligned to page boundaries, skipping\n",
+			       image_name);
+			continue;
+		}
+
+		start_sect = image_pos >> SECTOR_SHIFT;
+		nr_sects = image_len >> SECTOR_SHIFT;
+		imgmaxsect = (imgmaxsect < (start_sect + nr_sects)) ?
+				     (start_sect + nr_sects) :
+					   imgmaxsect;
+
+		if (start_sect + nr_sects > dsectors) {
+			state->access_beyond_eod = 1;
+			continue;
+		}
+
+		put_partition(state, *slot, fit_start_sector + start_sect,
+			      nr_sects);
+		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);
+		strncpy(info->volname, image_name, label_min);
+		info->volname[label_min] = '\0';
+
+		snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
+		strlcat(state->pp_buf, tmp, PAGE_SIZE);
+
+		if (++(*slot) > max_slot)
+			break;
+	}
+
+	/* in case uImage.FIT is stored in a partition, map the remaining space */
+	if (add_remain && (imgmaxsect + MIN_FREE_SECT) < dsectors) {
+		put_partition(state, *slot, fit_start_sector + imgmaxsect,
+			      dsectors - imgmaxsect);
+		state->parts[*slot].flags = 0;
+		info = &state->parts[*slot].info;
+		strcpy(info->volname, REMAIN_VOLNAME);
+		snprintf(tmp, sizeof(tmp), "(%s)", REMAIN_VOLNAME);
+		strlcat(state->pp_buf, tmp, PAGE_SIZE);
+		++(*slot);
+	}
+ret_out:
+	kfree(fit);
+	return ret;
+}
+
+/**
+ * fit_partition - map uImage.FIT filesystem sub-images into partitions
+ * @state: pointer to partition parser state
+ *
+ * Used to parse uImage.FIT structure for images directly stored on
+ * the whole block device (typically ubiblock or mtdblock).
+ */
+int fit_partition(struct parsed_partitions *state)
+{
+	int slot = 1;
+
+	return parse_fit_partitions(state, 0, 0, &slot, MAX_FIT_LOADABLES, false);
+}