1 From 6173a065cb395d4a9528c4e49810af127db68141 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Wed, 16 Nov 2022 12:49:52 +0000
4 Subject: [PATCH 1/2] block: add uImage.FIT subimage block driver
6 Add a small block driver which exposes filesystem sub-images contained
7 in U-Boot uImage.FIT images as block devices.
9 The uImage.FIT image has to be stored directly on a block device or
10 partition, MTD device or partition, or UBI volume.
12 The driver is intended for systems using the U-Boot bootloader and
13 uses the root device hint left by the bootloader (or the user) in
14 the 'chosen' section of the device-tree.
20 rootdisk = <&mmc0_part3>;
24 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
27 drivers/block/Kconfig | 12 +
28 drivers/block/Makefile | 2 +
29 drivers/block/fitblk.c | 658 ++++++++++++++++++++++++++++++++++++
30 drivers/block/open | 4 +
31 include/uapi/linux/fitblk.h | 10 +
32 6 files changed, 692 insertions(+)
33 create mode 100644 drivers/block/fitblk.c
34 create mode 100644 drivers/block/open
35 create mode 100644 include/uapi/linux/fitblk.h
39 @@ -22006,6 +22006,12 @@ F: Documentation/filesystems/ubifs-authe
40 F: Documentation/filesystems/ubifs.rst
43 +U-BOOT UIMAGE.FIT PARSER
44 +M: Daniel Golle <daniel@makrotopia.org>
45 +L: linux-block@vger.kernel.org
47 +F: drivers/block/fitblk.c
49 UBLK USERSPACE BLOCK DRIVER
50 M: Ming Lei <ming.lei@redhat.com>
51 L: linux-block@vger.kernel.org
52 --- a/drivers/block/Kconfig
53 +++ b/drivers/block/Kconfig
54 @@ -354,6 +354,18 @@ config VIRTIO_BLK
55 This is the virtual block driver for virtio. It can be used with
56 QEMU based VMMs (like KVM or Xen). Say Y or M.
58 +config UIMAGE_FIT_BLK
59 + bool "uImage.FIT block driver"
61 + This driver allows using filesystems contained in uImage.FIT images
62 + by mapping them as block devices.
64 + It can currently not be built as a module due to libfdt symbols not
67 + Say Y if you want to mount filesystems sub-images of a uImage.FIT
68 + stored in a block device partition, mtdblock or ubiblock device.
71 tristate "Rados block device (RBD)"
72 depends on INET && BLOCK
73 --- a/drivers/block/Makefile
74 +++ b/drivers/block/Makefile
75 @@ -39,4 +39,6 @@ obj-$(CONFIG_BLK_DEV_NULL_BLK) += null_b
77 obj-$(CONFIG_BLK_DEV_UBLK) += ublk_drv.o
79 +obj-$(CONFIG_UIMAGE_FIT_BLK) += fitblk.o
81 swim_mod-y := swim.o swim_asm.o
83 +++ b/drivers/block/fitblk.c
85 +// SPDX-License-Identifier: GPL-2.0-only
87 + * uImage.FIT virtual block device driver.
89 + * Copyright (C) 2023 Daniel Golle
90 + * Copyright (C) 2007 Nick Piggin
91 + * Copyright (C) 2007 Novell Inc.
93 + * Initially derived from drivers/block/brd.c which is in parts derived from
94 + * drivers/block/rd.c, and drivers/block/loop.c, copyright of their respective
97 + * uImage.FIT headers extracted from Das U-Boot
98 + * (C) Copyright 2008 Semihalf
99 + * (C) Copyright 2000-2005
100 + * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
103 +#include <linux/init.h>
104 +#include <linux/initrd.h>
105 +#include <linux/module.h>
106 +#include <linux/moduleparam.h>
107 +#include <linux/major.h>
108 +#include <linux/blkdev.h>
109 +#include <linux/blkpg.h>
110 +#include <linux/blk-mq.h>
111 +#include <linux/ctype.h>
112 +#include <linux/hdreg.h>
113 +#include <linux/list.h>
114 +#include <linux/mutex.h>
115 +#include <linux/of.h>
116 +#include <linux/of_device.h>
117 +#include <linux/of_fdt.h>
118 +#include <linux/pagemap.h>
119 +#include <linux/platform_device.h>
120 +#include <linux/property.h>
121 +#include <linux/refcount.h>
122 +#include <linux/task_work.h>
123 +#include <linux/types.h>
124 +#include <linux/libfdt.h>
125 +#include <linux/mtd/mtd.h>
126 +#include <linux/root_dev.h>
127 +#include <uapi/linux/fitblk.h>
129 +#define FIT_DEVICE_PREFIX "fit"
131 +/* maximum number of pages used for the uImage.FIT index structure */
132 +#define FIT_MAX_PAGES 1024
134 +/* minimum free sectors to map as read-write "remainder" volume */
135 +#define MIN_FREE_SECT 16
137 +/* maximum number of mapped loadables */
138 +#define MAX_FIT_LOADABLES 16
140 +/* constants for uImage.FIT structrure traversal */
141 +#define FIT_IMAGES_PATH "/images"
142 +#define FIT_CONFS_PATH "/configurations"
144 +/* hash/signature/key node */
145 +#define FIT_HASH_NODENAME "hash"
146 +#define FIT_ALGO_PROP "algo"
147 +#define FIT_VALUE_PROP "value"
148 +#define FIT_IGNORE_PROP "uboot-ignore"
149 +#define FIT_SIG_NODENAME "signature"
150 +#define FIT_KEY_REQUIRED "required"
151 +#define FIT_KEY_HINT "key-name-hint"
154 +#define FIT_CIPHER_NODENAME "cipher"
155 +#define FIT_ALGO_PROP "algo"
158 +#define FIT_DATA_PROP "data"
159 +#define FIT_DATA_POSITION_PROP "data-position"
160 +#define FIT_DATA_OFFSET_PROP "data-offset"
161 +#define FIT_DATA_SIZE_PROP "data-size"
162 +#define FIT_TIMESTAMP_PROP "timestamp"
163 +#define FIT_DESC_PROP "description"
164 +#define FIT_ARCH_PROP "arch"
165 +#define FIT_TYPE_PROP "type"
166 +#define FIT_OS_PROP "os"
167 +#define FIT_COMP_PROP "compression"
168 +#define FIT_ENTRY_PROP "entry"
169 +#define FIT_LOAD_PROP "load"
171 +/* configuration node */
172 +#define FIT_KERNEL_PROP "kernel"
173 +#define FIT_FILESYSTEM_PROP "filesystem"
174 +#define FIT_RAMDISK_PROP "ramdisk"
175 +#define FIT_FDT_PROP "fdt"
176 +#define FIT_LOADABLE_PROP "loadables"
177 +#define FIT_DEFAULT_PROP "default"
178 +#define FIT_SETUP_PROP "setup"
179 +#define FIT_FPGA_PROP "fpga"
180 +#define FIT_FIRMWARE_PROP "firmware"
181 +#define FIT_STANDALONE_PROP "standalone"
183 +/* fitblk driver data */
184 +static const char *_fitblk_claim_ptr = "I belong to fitblk";
185 +static const char *ubootver;
186 +struct device_node *rootdisk;
187 +static struct platform_device *pdev;
188 +static LIST_HEAD(fitblk_devices);
189 +static DEFINE_MUTEX(devices_mutex);
190 +refcount_t num_devs;
193 + struct platform_device *pdev;
194 + struct block_device *lower_bdev;
195 + sector_t start_sect;
196 + struct gendisk *disk;
197 + struct work_struct remove_work;
198 + struct list_head list;
202 +static int fitblk_open(struct gendisk *disk, fmode_t mode)
204 + struct fitblk *fitblk = disk->private_data;
212 +static void fitblk_release(struct gendisk *disk)
217 +static void fitblk_submit_bio(struct bio *orig_bio)
219 + struct bio *bio = orig_bio;
220 + struct fitblk *fitblk = bio->bi_bdev->bd_disk->private_data;
225 + /* mangle bio and re-submit */
227 + bio->bi_iter.bi_sector += fitblk->start_sect;
228 + bio->bi_bdev = fitblk->lower_bdev;
229 + bio = bio->bi_next;
231 + submit_bio(orig_bio);
234 +static void fitblk_remove(struct fitblk *fitblk)
236 + blk_mark_disk_dead(fitblk->disk);
237 + mutex_lock(&devices_mutex);
238 + fitblk->dead = true;
239 + list_del(&fitblk->list);
240 + mutex_unlock(&devices_mutex);
242 + schedule_work(&fitblk->remove_work);
245 +static int fitblk_ioctl(struct block_device *bdev, fmode_t mode,
246 + unsigned int cmd, unsigned long arg)
248 + struct fitblk *fitblk = bdev->bd_disk->private_data;
250 + if (!capable(CAP_SYS_ADMIN))
257 + case FITBLK_RELEASE:
258 + fitblk_remove(fitblk);
267 +static const struct block_device_operations fitblk_fops = {
268 + .owner = THIS_MODULE,
269 + .ioctl = fitblk_ioctl,
270 + .open = fitblk_open,
271 + .release = fitblk_release,
272 + .submit_bio = fitblk_submit_bio,
275 +static void fitblk_purge(struct work_struct *work)
277 + struct fitblk *fitblk = container_of(work, struct fitblk, remove_work);
279 + //del_gendisk(fitblk->disk); // causes crash, not doing it doesn't matter
280 + refcount_dec(&num_devs);
281 + platform_device_del(fitblk->pdev);
282 + platform_device_put(fitblk->pdev);
284 + if (refcount_dec_if_one(&num_devs)) {
285 + sysfs_remove_link(&pdev->dev.kobj, "lower_dev");
286 + blkdev_put(fitblk->lower_bdev, &_fitblk_claim_ptr);
292 +static int add_fit_subimage_device(struct block_device *lower_bdev,
293 + unsigned int slot, sector_t start_sect,
294 + sector_t nr_sect, bool readonly)
296 + struct fitblk *fitblk;
297 + struct gendisk *disk;
300 + mutex_lock(&devices_mutex);
301 + if (!refcount_inc_not_zero(&num_devs))
304 + fitblk = kzalloc(sizeof(struct fitblk), GFP_KERNEL);
310 + fitblk->lower_bdev = lower_bdev;
311 + fitblk->start_sect = start_sect;
312 + INIT_WORK(&fitblk->remove_work, fitblk_purge);
314 + disk = blk_alloc_disk(NUMA_NO_NODE);
317 + goto out_free_fitblk;
320 + disk->first_minor = 0;
321 + disk->flags = lower_bdev->bd_disk->flags | GENHD_FL_NO_PART;
322 + disk->fops = &fitblk_fops;
323 + disk->private_data = fitblk;
325 + set_disk_ro(disk, 1);
326 + snprintf(disk->disk_name, sizeof(disk->disk_name), FIT_DEVICE_PREFIX "%u", slot);
328 + strcpy(disk->disk_name, FIT_DEVICE_PREFIX "rw");
331 + set_capacity(disk, nr_sect);
333 + disk->queue->queue_flags = lower_bdev->bd_disk->queue->queue_flags;
334 + memcpy(&disk->queue->limits, &lower_bdev->bd_disk->queue->limits,
335 + sizeof(struct queue_limits));
337 + fitblk->disk = disk;
338 + fitblk->pdev = platform_device_alloc(disk->disk_name, PLATFORM_DEVID_NONE);
339 + if (!fitblk->pdev) {
341 + goto out_cleanup_disk;
344 + fitblk->pdev->dev.parent = &pdev->dev;
345 + err = platform_device_add(fitblk->pdev);
349 + err = device_add_disk(&fitblk->pdev->dev, disk, NULL);
354 + ROOT_DEV = disk->part0->bd_dev;
356 + list_add_tail(&fitblk->list, &fitblk_devices);
358 + mutex_unlock(&devices_mutex);
363 + platform_device_del(fitblk->pdev);
365 + platform_device_put(fitblk->pdev);
371 + refcount_dec(&num_devs);
372 + mutex_unlock(&devices_mutex);
376 +static void fitblk_mark_dead(struct block_device *bdev, bool surprise)
378 + struct list_head *n, *tmp;
379 + struct fitblk *fitblk;
381 + mutex_lock(&devices_mutex);
382 + list_for_each_safe(n, tmp, &fitblk_devices) {
383 + fitblk = list_entry(n, struct fitblk, list);
384 + if (fitblk->lower_bdev != bdev)
387 + fitblk->dead = true;
388 + list_del(&fitblk->list);
389 + /* removal needs to be deferred to avoid deadlock */
390 + schedule_work(&fitblk->remove_work);
392 + mutex_unlock(&devices_mutex);
395 +static const struct blk_holder_ops fitblk_hops = {
396 + .mark_dead = fitblk_mark_dead,
399 +static int parse_fit_on_dev(struct device *dev)
401 + struct block_device *bdev;
402 + struct address_space *mapping;
403 + struct folio *folio;
404 + pgoff_t f_index = 0;
405 + size_t bytes_left, bytes_to_copy;
406 + void *pre_fit, *fit, *fit_c;
407 + u64 dsize, dsectors, imgmaxsect = 0;
408 + u32 size, image_pos, image_len;
409 + const __be32 *image_offset_be, *image_len_be, *image_pos_be;
410 + int ret = 0, node, images, config;
411 + const char *image_name, *image_type, *image_description,
412 + *config_default, *config_description, *config_loadables;
413 + u32 image_name_len, image_type_len, image_description_len,
414 + bootconf_len, config_default_len, config_description_len,
415 + config_loadables_len;
416 + sector_t start_sect, nr_sects;
417 + struct device_node *np = NULL;
418 + const char *bootconf_c;
419 + const char *loadable;
420 + char *bootconf = NULL, *bootconf_term;
422 + int loadables_rem_len, loadable_len;
424 + unsigned int slot = 0;
426 + /* Exclusive open the block device to receive holder notifications */
427 + bdev = blkdev_get_by_dev(dev->devt, BLK_OPEN_READ, &_fitblk_claim_ptr, &fitblk_hops);
432 + return PTR_ERR(bdev);
434 + mapping = bdev->bd_inode->i_mapping;
436 + /* map first page */
437 + folio = read_mapping_folio(mapping, f_index++, NULL);
438 + if (IS_ERR(folio)) {
439 + ret = PTR_ERR(folio);
442 + pre_fit = folio_address(folio) + offset_in_folio(folio, 0);
444 + /* uImage.FIT is based on flattened device tree structure */
445 + if (fdt_check_header(pre_fit)) {
451 + size = fdt_totalsize(pre_fit);
453 + if (size > PAGE_SIZE * FIT_MAX_PAGES) {
459 + /* acquire disk size */
460 + dsectors = bdev_nr_sectors(bdev);
461 + dsize = dsectors << SECTOR_SHIFT;
463 + /* abort if FIT structure is larger than disk or partition size */
464 + if (size >= dsize) {
470 + fit = kmalloc(size, GFP_KERNEL);
479 + while (bytes_left > 0) {
480 + bytes_to_copy = min_t(size_t, bytes_left,
481 + folio_size(folio) - offset_in_folio(folio, 0));
482 + memcpy(fit_c, pre_fit, bytes_to_copy);
483 + fit_c += bytes_to_copy;
484 + bytes_left -= bytes_to_copy;
487 + folio = read_mapping_folio(mapping, f_index++, NULL);
488 + if (IS_ERR(folio)) {
489 + ret = PTR_ERR(folio);
492 + pre_fit = folio_address(folio) + offset_in_folio(folio, 0);
497 + /* set boot config node name U-Boot may have added to the device tree */
498 + np = of_find_node_by_path("/chosen");
500 + bootconf_c = of_get_property(np, "u-boot,bootconf", &bootconf_len);
501 + if (bootconf_c && bootconf_len)
502 + bootconf = kmemdup_nul(bootconf_c, bootconf_len, GFP_KERNEL);
506 + bootconf_term = strchr(bootconf, '#');
508 + *bootconf_term = '\0';
511 + /* find configuration path in uImage.FIT */
512 + config = fdt_path_offset(fit, FIT_CONFS_PATH);
514 + pr_err("FIT: Cannot find %s node: %d\n",
515 + FIT_CONFS_PATH, config);
520 + /* get default configuration node name */
522 + fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
524 + /* make sure we got either default or selected boot config node name */
525 + if (!config_default && !bootconf) {
526 + pr_err("FIT: Cannot find default configuration\n");
531 + /* find selected boot config node, fallback on default config node */
532 + node = fdt_subnode_offset(fit, config, bootconf ?: config_default);
534 + pr_err("FIT: Cannot find %s node: %d\n",
535 + bootconf ?: config_default, node);
540 + pr_info("FIT: Detected U-Boot %s\n", ubootver);
542 + /* get selected configuration data */
543 + config_description =
544 + fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
545 + config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP,
546 + &config_loadables_len);
548 + pr_info("FIT: %s configuration: \"%.*s\"%s%.*s%s\n",
549 + bootconf ? "Selected" : "Default",
550 + bootconf ? bootconf_len : config_default_len,
551 + bootconf ?: config_default,
552 + config_description ? " (" : "",
553 + config_description ? config_description_len : 0,
554 + config_description ?: "",
555 + config_description ? ")" : "");
557 + if (!config_loadables || !config_loadables_len) {
558 + pr_err("FIT: No loadables configured in \"%s\"\n",
559 + bootconf ?: config_default);
564 + /* get images path in uImage.FIT */
565 + images = fdt_path_offset(fit, FIT_IMAGES_PATH);
567 + pr_err("FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
572 + /* iterate over images in uImage.FIT */
573 + fdt_for_each_subnode(node, fit, images) {
574 + image_name = fdt_get_name(fit, node, &image_name_len);
575 + image_type = fdt_getprop(fit, node, FIT_TYPE_PROP, &image_type_len);
576 + image_offset_be = fdt_getprop(fit, node, FIT_DATA_OFFSET_PROP, NULL);
577 + image_pos_be = fdt_getprop(fit, node, FIT_DATA_POSITION_PROP, NULL);
578 + image_len_be = fdt_getprop(fit, node, FIT_DATA_SIZE_PROP, NULL);
580 + if (!image_name || !image_type || !image_len_be ||
581 + !image_name_len || !image_type_len)
584 + image_len = be32_to_cpu(*image_len_be);
588 + if (image_offset_be)
589 + image_pos = be32_to_cpu(*image_offset_be) + size;
590 + else if (image_pos_be)
591 + image_pos = be32_to_cpu(*image_pos_be);
595 + image_description = fdt_getprop(fit, node, FIT_DESC_PROP,
596 + &image_description_len);
598 + pr_info("FIT: %16s sub-image 0x%08x..0x%08x \"%.*s\"%s%.*s%s\n",
599 + image_type, image_pos, image_pos + image_len - 1,
600 + image_name_len, image_name, image_description ? " (" : "",
601 + image_description ? image_description_len : 0,
602 + image_description ?: "", image_description ? ") " : "");
604 + /* only 'filesystem' images should be mapped as partitions */
605 + if (strncmp(image_type, FIT_FILESYSTEM_PROP, image_type_len))
608 + /* check if sub-image is part of configured loadables */
610 + loadable = config_loadables;
611 + loadables_rem_len = config_loadables_len;
612 + for (loadcnt = 0; loadables_rem_len > 1 &&
613 + loadcnt < MAX_FIT_LOADABLES; ++loadcnt) {
615 + strnlen(loadable, loadables_rem_len - 1) + 1;
616 + loadables_rem_len -= loadable_len;
617 + if (!strncmp(image_name, loadable, loadable_len)) {
621 + loadable += loadable_len;
626 + if (image_pos % (1 << PAGE_SHIFT)) {
627 + dev_err(dev, "FIT: image %.*s start not aligned to page boundaries, skipping\n",
628 + image_name_len, image_name);
632 + if (image_len % (1 << PAGE_SHIFT)) {
633 + dev_err(dev, "FIT: sub-image %.*s end not aligned to page boundaries, skipping\n",
634 + image_name_len, image_name);
638 + start_sect = image_pos >> SECTOR_SHIFT;
639 + nr_sects = image_len >> SECTOR_SHIFT;
640 + imgmaxsect = max_t(sector_t, imgmaxsect, start_sect + nr_sects);
642 + if (start_sect + nr_sects > dsectors) {
643 + dev_err(dev, "FIT: sub-image %.*s disk access beyond EOD\n",
644 + image_name_len, image_name);
649 + ret = sysfs_create_link_nowarn(&pdev->dev.kobj, bdev_kobj(bdev), "lower_dev");
650 + if (ret && ret != -EEXIST)
656 + add_fit_subimage_device(bdev, slot++, start_sect, nr_sects, true);
659 + if (!found || !slot)
662 + dev_info(dev, "mapped %u uImage.FIT filesystem sub-image%s as /dev/fit%s%u%s\n",
663 + slot, (slot > 1)?"s":"", (slot > 1)?"[0...":"", slot - 1,
664 + (slot > 1)?"]":"");
666 + /* in case uImage.FIT is stored in a partition, map the remaining space */
667 + if (!bdev->bd_read_only && bdev_is_partition(bdev) &&
668 + (imgmaxsect + MIN_FREE_SECT) < dsectors) {
669 + add_fit_subimage_device(bdev, slot++, imgmaxsect,
670 + dsectors - imgmaxsect, false);
671 + dev_info(dev, "mapped remaing space as /dev/fitrw\n");
679 + blkdev_put(bdev, &_fitblk_claim_ptr);
684 +static int fitblk_match_of_node(struct device *dev, const void *np)
688 + ret = device_match_of_node(dev, np);
693 + * To match ubiblock and mtdblock devices by their parent ubi
694 + * or mtd device, also consider block device parent
699 + return device_match_of_node(dev->parent, np);
702 +static int fitblk_probe(struct platform_device *pdev)
704 + struct device *dev;
706 + dev = class_find_device(&block_class, NULL, rootdisk, fitblk_match_of_node);
708 + return -EPROBE_DEFER;
710 + return parse_fit_on_dev(dev);
713 +static struct platform_driver fitblk_driver = {
714 + .probe = fitblk_probe,
717 + .owner = THIS_MODULE,
721 +static int __init fitblk_init(void)
723 + /* detect U-Boot firmware */
724 + ubootver = of_get_property(of_chosen, "u-boot,version", NULL);
728 + /* parse 'rootdisk' property phandle */
729 + rootdisk = of_parse_phandle(of_chosen, "rootdisk", 0);
733 + if (platform_driver_register(&fitblk_driver))
736 + refcount_set(&num_devs, 1);
737 + pdev = platform_device_register_simple("fitblk", -1, NULL, 0);
739 + return PTR_ERR(pdev);
743 +device_initcall(fitblk_init);
745 +++ b/include/uapi/linux/fitblk.h
747 +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
748 +#ifndef _UAPI_LINUX_FITBLK_H
749 +#define _UAPI_LINUX_FITBLK_H
752 + * IOCTL commands --- we will commandeer 0x46 ('F')
754 +#define FITBLK_RELEASE 0x4600
756 +#endif /* _UAPI_LINUX_FITBLK_H */