generic: v6.6: update fitblk driver to work with Linux 6.6
[openwrt/openwrt.git] / target / linux / generic / pending-6.6 / 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 @@ -22006,6 +22006,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 @@ -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.
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,659 @@
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 gendisk *disk, fmode_t mode)
203 +{
204 + struct fitblk *fitblk = 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)
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, &_fitblk_claim_ptr);
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 void fitblk_mark_dead(struct block_device *bdev, bool surprise)
377 +{
378 + struct list_head *n, *tmp;
379 + struct fitblk *fitblk;
380 +
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)
385 + continue;
386 +
387 + fitblk->dead = true;
388 + list_del(&fitblk->list);
389 + /* removal needs to be deferred to avoid deadlock */
390 + schedule_work(&fitblk->remove_work);
391 + }
392 + mutex_unlock(&devices_mutex);
393 +}
394 +
395 +static const struct blk_holder_ops fitblk_hops = {
396 + .mark_dead = fitblk_mark_dead,
397 +};
398 +
399 +static int parse_fit_on_dev(struct device *dev)
400 +{
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;
421 + bool found;
422 + int loadables_rem_len, loadable_len;
423 + u16 loadcnt;
424 + unsigned int slot = 0;
425 +
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);
428 + if (!bdev)
429 + return -ENODEV;
430 +
431 + if (IS_ERR(bdev))
432 + return PTR_ERR(bdev);
433 +
434 + mapping = bdev->bd_inode->i_mapping;
435 +
436 + /* map first page */
437 + folio = read_mapping_folio(mapping, f_index++, NULL);
438 + if (IS_ERR(folio)) {
439 + ret = PTR_ERR(folio);
440 + goto out_blkdev;
441 + }
442 + pre_fit = folio_address(folio) + offset_in_folio(folio, 0);
443 +
444 + /* uImage.FIT is based on flattened device tree structure */
445 + if (fdt_check_header(pre_fit)) {
446 + ret = -EINVAL;
447 + folio_put(folio);
448 + goto out_blkdev;
449 + }
450 +
451 + size = fdt_totalsize(pre_fit);
452 +
453 + if (size > PAGE_SIZE * FIT_MAX_PAGES) {
454 + ret = -EOPNOTSUPP;
455 + folio_put(folio);
456 + goto out_blkdev;
457 + }
458 +
459 + /* acquire disk size */
460 + dsectors = bdev_nr_sectors(bdev);
461 + dsize = dsectors << SECTOR_SHIFT;
462 +
463 + /* abort if FIT structure is larger than disk or partition size */
464 + if (size >= dsize) {
465 + ret = -EFBIG;
466 + folio_put(folio);
467 + goto out_blkdev;
468 + }
469 +
470 + fit = kmalloc(size, GFP_KERNEL);
471 + if (!fit) {
472 + ret = -ENOMEM;
473 + folio_put(folio);
474 + goto out_blkdev;
475 + }
476 +
477 + bytes_left = size;
478 + fit_c = fit;
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;
485 + if (bytes_left) {
486 + folio_put(folio);
487 + folio = read_mapping_folio(mapping, f_index++, NULL);
488 + if (IS_ERR(folio)) {
489 + ret = PTR_ERR(folio);
490 + goto out_blkdev;
491 + };
492 + pre_fit = folio_address(folio) + offset_in_folio(folio, 0);
493 + }
494 + }
495 + folio_put(folio);
496 +
497 + /* set boot config node name U-Boot may have added to the device tree */
498 + np = of_find_node_by_path("/chosen");
499 + if (np) {
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);
503 + }
504 +
505 + if (bootconf) {
506 + bootconf_term = strchr(bootconf, '#');
507 + if (bootconf_term)
508 + *bootconf_term = '\0';
509 + }
510 +
511 + /* find configuration path in uImage.FIT */
512 + config = fdt_path_offset(fit, FIT_CONFS_PATH);
513 + if (config < 0) {
514 + pr_err("FIT: Cannot find %s node: %d\n",
515 + FIT_CONFS_PATH, config);
516 + ret = -ENOENT;
517 + goto out_bootconf;
518 + }
519 +
520 + /* get default configuration node name */
521 + config_default =
522 + fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
523 +
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");
527 + ret = -ENOENT;
528 + goto out_bootconf;
529 + }
530 +
531 + /* find selected boot config node, fallback on default config node */
532 + node = fdt_subnode_offset(fit, config, bootconf ?: config_default);
533 + if (node < 0) {
534 + pr_err("FIT: Cannot find %s node: %d\n",
535 + bootconf ?: config_default, node);
536 + ret = -ENOENT;
537 + goto out_bootconf;
538 + }
539 +
540 + pr_info("FIT: Detected U-Boot %s\n", ubootver);
541 +
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);
547 +
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 ? ")" : "");
556 +
557 + if (!config_loadables || !config_loadables_len) {
558 + pr_err("FIT: No loadables configured in \"%s\"\n",
559 + bootconf ?: config_default);
560 + ret = -ENOENT;
561 + goto out_bootconf;
562 + }
563 +
564 + /* get images path in uImage.FIT */
565 + images = fdt_path_offset(fit, FIT_IMAGES_PATH);
566 + if (images < 0) {
567 + pr_err("FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
568 + ret = -EINVAL;
569 + goto out_bootconf;
570 + }
571 +
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);
579 +
580 + if (!image_name || !image_type || !image_len_be ||
581 + !image_name_len || !image_type_len)
582 + continue;
583 +
584 + image_len = be32_to_cpu(*image_len_be);
585 + if (!image_len)
586 + continue;
587 +
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);
592 + else
593 + continue;
594 +
595 + image_description = fdt_getprop(fit, node, FIT_DESC_PROP,
596 + &image_description_len);
597 +
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 ? ") " : "");
603 +
604 + /* only 'filesystem' images should be mapped as partitions */
605 + if (strncmp(image_type, FIT_FILESYSTEM_PROP, image_type_len))
606 + continue;
607 +
608 + /* check if sub-image is part of configured loadables */
609 + found = false;
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) {
614 + loadable_len =
615 + strnlen(loadable, loadables_rem_len - 1) + 1;
616 + loadables_rem_len -= loadable_len;
617 + if (!strncmp(image_name, loadable, loadable_len)) {
618 + found = true;
619 + break;
620 + }
621 + loadable += loadable_len;
622 + }
623 + if (!found)
624 + continue;
625 +
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);
629 + continue;
630 + }
631 +
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);
635 + continue;
636 + }
637 +
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);
641 +
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);
645 + continue;
646 + }
647 +
648 + if (!slot) {
649 + ret = sysfs_create_link_nowarn(&pdev->dev.kobj, bdev_kobj(bdev), "lower_dev");
650 + if (ret && ret != -EEXIST)
651 + goto out_bootconf;
652 +
653 + ret = 0;
654 + }
655 +
656 + add_fit_subimage_device(bdev, slot++, start_sect, nr_sects, true);
657 + }
658 +
659 + if (!found || !slot)
660 + goto out_bootconf;
661 +
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)?"]":"");
665 +
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");
672 + }
673 +
674 +out_bootconf:
675 + kfree(bootconf);
676 + kfree(fit);
677 +out_blkdev:
678 + if (!found || ret)
679 + blkdev_put(bdev, &_fitblk_claim_ptr);
680 +
681 + return ret;
682 +}
683 +
684 +static int fitblk_match_of_node(struct device *dev, const void *np)
685 +{
686 + int ret;
687 +
688 + ret = device_match_of_node(dev, np);
689 + if (ret)
690 + return ret;
691 +
692 + /*
693 + * To match ubiblock and mtdblock devices by their parent ubi
694 + * or mtd device, also consider block device parent
695 + */
696 + if (!dev->parent)
697 + return 0;
698 +
699 + return device_match_of_node(dev->parent, np);
700 +}
701 +
702 +static int fitblk_probe(struct platform_device *pdev)
703 +{
704 + struct device *dev;
705 +
706 + dev = class_find_device(&block_class, NULL, rootdisk, fitblk_match_of_node);
707 + if (!dev)
708 + return -EPROBE_DEFER;
709 +
710 + return parse_fit_on_dev(dev);
711 +}
712 +
713 +static struct platform_driver fitblk_driver = {
714 + .probe = fitblk_probe,
715 + .driver = {
716 + .name = "fitblk",
717 + .owner = THIS_MODULE,
718 + },
719 +};
720 +
721 +static int __init fitblk_init(void)
722 +{
723 + /* detect U-Boot firmware */
724 + ubootver = of_get_property(of_chosen, "u-boot,version", NULL);
725 + if (!ubootver)
726 + return 0;
727 +
728 + /* parse 'rootdisk' property phandle */
729 + rootdisk = of_parse_phandle(of_chosen, "rootdisk", 0);
730 + if (!rootdisk)
731 + return 0;
732 +
733 + if (platform_driver_register(&fitblk_driver))
734 + return -ENODEV;
735 +
736 + refcount_set(&num_devs, 1);
737 + pdev = platform_device_register_simple("fitblk", -1, NULL, 0);
738 + if (IS_ERR(pdev))
739 + return PTR_ERR(pdev);
740 +
741 + return 0;
742 +}
743 +device_initcall(fitblk_init);
744 --- /dev/null
745 +++ b/include/uapi/linux/fitblk.h
746 @@ -0,0 +1,10 @@
747 +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
748 +#ifndef _UAPI_LINUX_FITBLK_H
749 +#define _UAPI_LINUX_FITBLK_H
750 +
751 +/*
752 + * IOCTL commands --- we will commandeer 0x46 ('F')
753 + */
754 +#define FITBLK_RELEASE 0x4600
755 +
756 +#endif /* _UAPI_LINUX_FITBLK_H */