gpio-nct5104d: fix compilation with kernel 6.6
[openwrt/openwrt.git] / target / linux / generic / pending-6.1 / 510-block-add-uImage.FIT-subimage-block-driver.patch
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
5
6 Add a small block driver which exposes filesystem sub-images contained
7 in U-Boot uImage.FIT images as block devices.
8
9 The uImage.FIT image has to be stored directly on a block device or
10 partition, MTD device or partition, or UBI volume.
11
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.
15
16 Example:
17 /dts-v1/;
18 / {
19 chosen {
20 rootdisk = <&mmc0_part3>;
21 };
22 };
23
24 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
25 ---
26 MAINTAINERS | 6 +
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
36
37 --- a/MAINTAINERS
38 +++ b/MAINTAINERS
39 @@ -21059,6 +21059,12 @@ F: Documentation/filesystems/ubifs-authe
40 F: Documentation/filesystems/ubifs.rst
41 F: fs/ubifs/
42
43 +U-BOOT UIMAGE.FIT PARSER
44 +M: Daniel Golle <daniel@makrotopia.org>
45 +L: linux-block@vger.kernel.org
46 +S: Maintained
47 +F: drivers/block/fitblk.c
48 +
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 @@ -383,6 +383,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.
57
58 +config UIMAGE_FIT_BLK
59 + bool "uImage.FIT block driver"
60 + help
61 + This driver allows using filesystems contained in uImage.FIT images
62 + by mapping them as block devices.
63 +
64 + It can currently not be built as a module due to libfdt symbols not
65 + being exported.
66 +
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.
69 +
70 config BLK_DEV_RBD
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
76
77 obj-$(CONFIG_BLK_DEV_UBLK) += ublk_drv.o
78
79 +obj-$(CONFIG_UIMAGE_FIT_BLK) += fitblk.o
80 +
81 swim_mod-y := swim.o swim_asm.o
82 --- /dev/null
83 +++ b/drivers/block/fitblk.c
84 @@ -0,0 +1,636 @@
85 +// SPDX-License-Identifier: GPL-2.0-only
86 +/*
87 + * uImage.FIT virtual block device driver.
88 + *
89 + * Copyright (C) 2023 Daniel Golle
90 + * Copyright (C) 2007 Nick Piggin
91 + * Copyright (C) 2007 Novell Inc.
92 + *
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
95 + * owners.
96 + *
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.
101 + */
102 +
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>
128 +
129 +#define FIT_DEVICE_PREFIX "fit"
130 +
131 +/* maximum number of pages used for the uImage.FIT index structure */
132 +#define FIT_MAX_PAGES 1024
133 +
134 +/* minimum free sectors to map as read-write "remainder" volume */
135 +#define MIN_FREE_SECT 16
136 +
137 +/* maximum number of mapped loadables */
138 +#define MAX_FIT_LOADABLES 16
139 +
140 +/* constants for uImage.FIT structrure traversal */
141 +#define FIT_IMAGES_PATH "/images"
142 +#define FIT_CONFS_PATH "/configurations"
143 +
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"
152 +
153 +/* cipher node */
154 +#define FIT_CIPHER_NODENAME "cipher"
155 +#define FIT_ALGO_PROP "algo"
156 +
157 +/* image node */
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"
170 +
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"
182 +
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;
191 +
192 +struct fitblk {
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;
199 + bool dead;
200 +};
201 +
202 +static int fitblk_open(struct block_device *bdev, fmode_t mode)
203 +{
204 + struct fitblk *fitblk = bdev->bd_disk->private_data;
205 +
206 + if (fitblk->dead)
207 + return -ENOENT;
208 +
209 + return 0;
210 +}
211 +
212 +static void fitblk_release(struct gendisk *disk, fmode_t mode)
213 +{
214 + return;
215 +}
216 +
217 +static void fitblk_submit_bio(struct bio *orig_bio)
218 +{
219 + struct bio *bio = orig_bio;
220 + struct fitblk *fitblk = bio->bi_bdev->bd_disk->private_data;
221 +
222 + if (fitblk->dead)
223 + return;
224 +
225 + /* mangle bio and re-submit */
226 + while (bio) {
227 + bio->bi_iter.bi_sector += fitblk->start_sect;
228 + bio->bi_bdev = fitblk->lower_bdev;
229 + bio = bio->bi_next;
230 + }
231 + submit_bio(orig_bio);
232 +}
233 +
234 +static void fitblk_remove(struct fitblk *fitblk)
235 +{
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);
241 +
242 + schedule_work(&fitblk->remove_work);
243 +}
244 +
245 +static int fitblk_ioctl(struct block_device *bdev, fmode_t mode,
246 + unsigned int cmd, unsigned long arg)
247 +{
248 + struct fitblk *fitblk = bdev->bd_disk->private_data;
249 +
250 + if (!capable(CAP_SYS_ADMIN))
251 + return -EACCES;
252 +
253 + if (fitblk->dead)
254 + return -ENOENT;
255 +
256 + switch (cmd) {
257 + case FITBLK_RELEASE:
258 + fitblk_remove(fitblk);
259 + break;
260 + default:
261 + return -EINVAL;
262 + }
263 +
264 + return 0;
265 +}
266 +
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,
273 +};
274 +
275 +static void fitblk_purge(struct work_struct *work)
276 +{
277 + struct fitblk *fitblk = container_of(work, struct fitblk, remove_work);
278 +
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);
283 +
284 + if (refcount_dec_if_one(&num_devs)) {
285 + sysfs_remove_link(&pdev->dev.kobj, "lower_dev");
286 + blkdev_put(fitblk->lower_bdev, FMODE_READ | FMODE_EXCL);
287 + }
288 +
289 + kfree(fitblk);
290 +}
291 +
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)
295 +{
296 + struct fitblk *fitblk;
297 + struct gendisk *disk;
298 + int err;
299 +
300 + mutex_lock(&devices_mutex);
301 + if (!refcount_inc_not_zero(&num_devs))
302 + return -EBADF;
303 +
304 + fitblk = kzalloc(sizeof(struct fitblk), GFP_KERNEL);
305 + if (!fitblk) {
306 + err = -ENOMEM;
307 + goto out_unlock;
308 + }
309 +
310 + fitblk->lower_bdev = lower_bdev;
311 + fitblk->start_sect = start_sect;
312 + INIT_WORK(&fitblk->remove_work, fitblk_purge);
313 +
314 + disk = blk_alloc_disk(NUMA_NO_NODE);
315 + if (!disk) {
316 + err = -ENOMEM;
317 + goto out_free_fitblk;
318 + }
319 +
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;
324 + if (readonly) {
325 + set_disk_ro(disk, 1);
326 + snprintf(disk->disk_name, sizeof(disk->disk_name), FIT_DEVICE_PREFIX "%u", slot);
327 + } else {
328 + strcpy(disk->disk_name, FIT_DEVICE_PREFIX "rw");
329 + }
330 +
331 + set_capacity(disk, nr_sect);
332 +
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));
336 +
337 + fitblk->disk = disk;
338 + fitblk->pdev = platform_device_alloc(disk->disk_name, PLATFORM_DEVID_NONE);
339 + if (!fitblk->pdev) {
340 + err = -ENOMEM;
341 + goto out_cleanup_disk;
342 + }
343 +
344 + fitblk->pdev->dev.parent = &pdev->dev;
345 + err = platform_device_add(fitblk->pdev);
346 + if (err)
347 + goto out_put_pdev;
348 +
349 + err = device_add_disk(&fitblk->pdev->dev, disk, NULL);
350 + if (err)
351 + goto out_del_pdev;
352 +
353 + if (!ROOT_DEV)
354 + ROOT_DEV = disk->part0->bd_dev;
355 +
356 + list_add_tail(&fitblk->list, &fitblk_devices);
357 +
358 + mutex_unlock(&devices_mutex);
359 +
360 + return 0;
361 +
362 +out_del_pdev:
363 + platform_device_del(fitblk->pdev);
364 +out_put_pdev:
365 + platform_device_put(fitblk->pdev);
366 +out_cleanup_disk:
367 + put_disk(disk);
368 +out_free_fitblk:
369 + kfree(fitblk);
370 +out_unlock:
371 + refcount_dec(&num_devs);
372 + mutex_unlock(&devices_mutex);
373 + return err;
374 +}
375 +
376 +static int parse_fit_on_dev(struct device *dev)
377 +{
378 + struct block_device *bdev;
379 + struct address_space *mapping;
380 + struct folio *folio;
381 + pgoff_t f_index = 0;
382 + size_t bytes_left, bytes_to_copy;
383 + void *pre_fit, *fit, *fit_c;
384 + u64 dsize, dsectors, imgmaxsect = 0;
385 + u32 size, image_pos, image_len;
386 + const __be32 *image_offset_be, *image_len_be, *image_pos_be;
387 + int ret = 0, node, images, config;
388 + const char *image_name, *image_type, *image_description,
389 + *config_default, *config_description, *config_loadables;
390 + u32 image_name_len, image_type_len, image_description_len,
391 + bootconf_len, config_default_len, config_description_len,
392 + config_loadables_len;
393 + sector_t start_sect, nr_sects;
394 + struct device_node *np = NULL;
395 + const char *bootconf_c;
396 + const char *loadable;
397 + char *bootconf = NULL, *bootconf_term;
398 + bool found;
399 + int loadables_rem_len, loadable_len;
400 + u16 loadcnt;
401 + unsigned int slot = 0;
402 +
403 + /* Exclusive open the block device to receive holder notifications */
404 + bdev = blkdev_get_by_dev(dev->devt, FMODE_READ | FMODE_EXCL, &_fitblk_claim_ptr);
405 + if (!bdev)
406 + return -ENODEV;
407 +
408 + if (IS_ERR(bdev))
409 + return PTR_ERR(bdev);
410 +
411 + mapping = bdev->bd_inode->i_mapping;
412 +
413 + /* map first page */
414 + folio = read_mapping_folio(mapping, f_index++, NULL);
415 + if (IS_ERR(folio)) {
416 + ret = PTR_ERR(folio);
417 + goto out_blkdev;
418 + }
419 + pre_fit = folio_address(folio) + offset_in_folio(folio, 0);
420 +
421 + /* uImage.FIT is based on flattened device tree structure */
422 + if (fdt_check_header(pre_fit)) {
423 + ret = -EINVAL;
424 + folio_put(folio);
425 + goto out_blkdev;
426 + }
427 +
428 + size = fdt_totalsize(pre_fit);
429 +
430 + if (size > PAGE_SIZE * FIT_MAX_PAGES) {
431 + ret = -EOPNOTSUPP;
432 + folio_put(folio);
433 + goto out_blkdev;
434 + }
435 +
436 + /* acquire disk size */
437 + dsectors = bdev_nr_sectors(bdev);
438 + dsize = dsectors << SECTOR_SHIFT;
439 +
440 + /* abort if FIT structure is larger than disk or partition size */
441 + if (size >= dsize) {
442 + ret = -EFBIG;
443 + folio_put(folio);
444 + goto out_blkdev;
445 + }
446 +
447 + fit = kmalloc(size, GFP_KERNEL);
448 + if (!fit) {
449 + ret = -ENOMEM;
450 + folio_put(folio);
451 + goto out_blkdev;
452 + }
453 +
454 + bytes_left = size;
455 + fit_c = fit;
456 + while (bytes_left > 0) {
457 + bytes_to_copy = min_t(size_t, bytes_left,
458 + folio_size(folio) - offset_in_folio(folio, 0));
459 + memcpy(fit_c, pre_fit, bytes_to_copy);
460 + fit_c += bytes_to_copy;
461 + bytes_left -= bytes_to_copy;
462 + if (bytes_left) {
463 + folio_put(folio);
464 + folio = read_mapping_folio(mapping, f_index++, NULL);
465 + if (IS_ERR(folio)) {
466 + ret = PTR_ERR(folio);
467 + goto out_blkdev;
468 + };
469 + pre_fit = folio_address(folio) + offset_in_folio(folio, 0);
470 + }
471 + }
472 + folio_put(folio);
473 +
474 + /* set boot config node name U-Boot may have added to the device tree */
475 + np = of_find_node_by_path("/chosen");
476 + if (np) {
477 + bootconf_c = of_get_property(np, "u-boot,bootconf", &bootconf_len);
478 + if (bootconf_c && bootconf_len)
479 + bootconf = kmemdup_nul(bootconf_c, bootconf_len, GFP_KERNEL);
480 + }
481 +
482 + if (bootconf) {
483 + bootconf_term = strchr(bootconf, '#');
484 + if (bootconf_term)
485 + *bootconf_term = '\0';
486 + }
487 +
488 + /* find configuration path in uImage.FIT */
489 + config = fdt_path_offset(fit, FIT_CONFS_PATH);
490 + if (config < 0) {
491 + pr_err("FIT: Cannot find %s node: %d\n",
492 + FIT_CONFS_PATH, config);
493 + ret = -ENOENT;
494 + goto out_bootconf;
495 + }
496 +
497 + /* get default configuration node name */
498 + config_default =
499 + fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
500 +
501 + /* make sure we got either default or selected boot config node name */
502 + if (!config_default && !bootconf) {
503 + pr_err("FIT: Cannot find default configuration\n");
504 + ret = -ENOENT;
505 + goto out_bootconf;
506 + }
507 +
508 + /* find selected boot config node, fallback on default config node */
509 + node = fdt_subnode_offset(fit, config, bootconf ?: config_default);
510 + if (node < 0) {
511 + pr_err("FIT: Cannot find %s node: %d\n",
512 + bootconf ?: config_default, node);
513 + ret = -ENOENT;
514 + goto out_bootconf;
515 + }
516 +
517 + pr_info("FIT: Detected U-Boot %s\n", ubootver);
518 +
519 + /* get selected configuration data */
520 + config_description =
521 + fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
522 + config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP,
523 + &config_loadables_len);
524 +
525 + pr_info("FIT: %s configuration: \"%.*s\"%s%.*s%s\n",
526 + bootconf ? "Selected" : "Default",
527 + bootconf ? bootconf_len : config_default_len,
528 + bootconf ?: config_default,
529 + config_description ? " (" : "",
530 + config_description ? config_description_len : 0,
531 + config_description ?: "",
532 + config_description ? ")" : "");
533 +
534 + if (!config_loadables || !config_loadables_len) {
535 + pr_err("FIT: No loadables configured in \"%s\"\n",
536 + bootconf ?: config_default);
537 + ret = -ENOENT;
538 + goto out_bootconf;
539 + }
540 +
541 + /* get images path in uImage.FIT */
542 + images = fdt_path_offset(fit, FIT_IMAGES_PATH);
543 + if (images < 0) {
544 + pr_err("FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
545 + ret = -EINVAL;
546 + goto out_bootconf;
547 + }
548 +
549 + /* iterate over images in uImage.FIT */
550 + fdt_for_each_subnode(node, fit, images) {
551 + image_name = fdt_get_name(fit, node, &image_name_len);
552 + image_type = fdt_getprop(fit, node, FIT_TYPE_PROP, &image_type_len);
553 + image_offset_be = fdt_getprop(fit, node, FIT_DATA_OFFSET_PROP, NULL);
554 + image_pos_be = fdt_getprop(fit, node, FIT_DATA_POSITION_PROP, NULL);
555 + image_len_be = fdt_getprop(fit, node, FIT_DATA_SIZE_PROP, NULL);
556 +
557 + if (!image_name || !image_type || !image_len_be ||
558 + !image_name_len || !image_type_len)
559 + continue;
560 +
561 + image_len = be32_to_cpu(*image_len_be);
562 + if (!image_len)
563 + continue;
564 +
565 + if (image_offset_be)
566 + image_pos = be32_to_cpu(*image_offset_be) + size;
567 + else if (image_pos_be)
568 + image_pos = be32_to_cpu(*image_pos_be);
569 + else
570 + continue;
571 +
572 + image_description = fdt_getprop(fit, node, FIT_DESC_PROP,
573 + &image_description_len);
574 +
575 + pr_info("FIT: %16s sub-image 0x%08x..0x%08x \"%.*s\"%s%.*s%s\n",
576 + image_type, image_pos, image_pos + image_len - 1,
577 + image_name_len, image_name, image_description ? " (" : "",
578 + image_description ? image_description_len : 0,
579 + image_description ?: "", image_description ? ") " : "");
580 +
581 + /* only 'filesystem' images should be mapped as partitions */
582 + if (strncmp(image_type, FIT_FILESYSTEM_PROP, image_type_len))
583 + continue;
584 +
585 + /* check if sub-image is part of configured loadables */
586 + found = false;
587 + loadable = config_loadables;
588 + loadables_rem_len = config_loadables_len;
589 + for (loadcnt = 0; loadables_rem_len > 1 &&
590 + loadcnt < MAX_FIT_LOADABLES; ++loadcnt) {
591 + loadable_len =
592 + strnlen(loadable, loadables_rem_len - 1) + 1;
593 + loadables_rem_len -= loadable_len;
594 + if (!strncmp(image_name, loadable, loadable_len)) {
595 + found = true;
596 + break;
597 + }
598 + loadable += loadable_len;
599 + }
600 + if (!found)
601 + continue;
602 +
603 + if (image_pos % (1 << PAGE_SHIFT)) {
604 + dev_err(dev, "FIT: image %.*s start not aligned to page boundaries, skipping\n",
605 + image_name_len, image_name);
606 + continue;
607 + }
608 +
609 + if (image_len % (1 << PAGE_SHIFT)) {
610 + dev_err(dev, "FIT: sub-image %.*s end not aligned to page boundaries, skipping\n",
611 + image_name_len, image_name);
612 + continue;
613 + }
614 +
615 + start_sect = image_pos >> SECTOR_SHIFT;
616 + nr_sects = image_len >> SECTOR_SHIFT;
617 + imgmaxsect = max_t(sector_t, imgmaxsect, start_sect + nr_sects);
618 +
619 + if (start_sect + nr_sects > dsectors) {
620 + dev_err(dev, "FIT: sub-image %.*s disk access beyond EOD\n",
621 + image_name_len, image_name);
622 + continue;
623 + }
624 +
625 + if (!slot) {
626 + ret = sysfs_create_link_nowarn(&pdev->dev.kobj, bdev_kobj(bdev), "lower_dev");
627 + if (ret && ret != -EEXIST)
628 + goto out_bootconf;
629 +
630 + ret = 0;
631 + }
632 +
633 + add_fit_subimage_device(bdev, slot++, start_sect, nr_sects, true);
634 + }
635 +
636 + if (!found || !slot)
637 + goto out_bootconf;
638 +
639 + dev_info(dev, "mapped %u uImage.FIT filesystem sub-image%s as /dev/fit%s%u%s\n",
640 + slot, (slot > 1)?"s":"", (slot > 1)?"[0...":"", slot - 1,
641 + (slot > 1)?"]":"");
642 +
643 + /* in case uImage.FIT is stored in a partition, map the remaining space */
644 + if (!bdev->bd_read_only && bdev_is_partition(bdev) &&
645 + (imgmaxsect + MIN_FREE_SECT) < dsectors) {
646 + add_fit_subimage_device(bdev, slot++, imgmaxsect,
647 + dsectors - imgmaxsect, false);
648 + dev_info(dev, "mapped remaing space as /dev/fitrw\n");
649 + }
650 +
651 +out_bootconf:
652 + kfree(bootconf);
653 + kfree(fit);
654 +out_blkdev:
655 + if (!found || ret)
656 + blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
657 +
658 + return ret;
659 +}
660 +
661 +static int fitblk_match_of_node(struct device *dev, const void *np)
662 +{
663 + int ret;
664 +
665 + ret = device_match_of_node(dev, np);
666 + if (ret)
667 + return ret;
668 +
669 + /*
670 + * To match ubiblock and mtdblock devices by their parent ubi
671 + * or mtd device, also consider block device parent
672 + */
673 + if (!dev->parent)
674 + return 0;
675 +
676 + return device_match_of_node(dev->parent, np);
677 +}
678 +
679 +static int fitblk_probe(struct platform_device *pdev)
680 +{
681 + struct device *dev;
682 +
683 + dev = class_find_device(&block_class, NULL, rootdisk, fitblk_match_of_node);
684 + if (!dev)
685 + return -EPROBE_DEFER;
686 +
687 + return parse_fit_on_dev(dev);
688 +}
689 +
690 +static struct platform_driver fitblk_driver = {
691 + .probe = fitblk_probe,
692 + .driver = {
693 + .name = "fitblk",
694 + .owner = THIS_MODULE,
695 + },
696 +};
697 +
698 +static int __init fitblk_init(void)
699 +{
700 + /* detect U-Boot firmware */
701 + ubootver = of_get_property(of_chosen, "u-boot,version", NULL);
702 + if (!ubootver)
703 + return 0;
704 +
705 + /* parse 'rootdisk' property phandle */
706 + rootdisk = of_parse_phandle(of_chosen, "rootdisk", 0);
707 + if (!rootdisk)
708 + return 0;
709 +
710 + if (platform_driver_register(&fitblk_driver))
711 + return -ENODEV;
712 +
713 + refcount_set(&num_devs, 1);
714 + pdev = platform_device_register_simple("fitblk", -1, NULL, 0);
715 + if (IS_ERR(pdev))
716 + return PTR_ERR(pdev);
717 +
718 + return 0;
719 +}
720 +device_initcall(fitblk_init);
721 --- /dev/null
722 +++ b/include/uapi/linux/fitblk.h
723 @@ -0,0 +1,10 @@
724 +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
725 +#ifndef _UAPI_LINUX_FITBLK_H
726 +#define _UAPI_LINUX_FITBLK_H
727 +
728 +/*
729 + * IOCTL commands --- we will commandeer 0x46 ('F')
730 + */
731 +#define FITBLK_RELEASE 0x4600
732 +
733 +#endif /* _UAPI_LINUX_FITBLK_H */