| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | From: Daniel Golle <daniel@makrotopia.org> |
| 2 | Subject: ubi: auto-create ubiblock device for rootfs |
| 3 | |
| 4 | Signed-off-by: Daniel Golle <daniel@makrotopia.org> |
| 5 | --- |
| 6 | drivers/mtd/ubi/block.c | 42 ++++++++++++++++++++++++++++++++++++++++++ |
| 7 | 1 file changed, 42 insertions(+) |
| 8 | |
| 9 | --- a/drivers/mtd/ubi/block.c |
| 10 | +++ b/drivers/mtd/ubi/block.c |
| 11 | @@ -652,6 +652,44 @@ static void __init ubiblock_create_from_ |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | +#define UBIFS_NODE_MAGIC 0x06101831 |
| 16 | +static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc) |
| 17 | +{ |
| 18 | + int ret; |
| 19 | + uint32_t magic_of, magic; |
| 20 | + ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4); |
| 21 | + if (ret) |
| 22 | + return 0; |
| 23 | + magic = le32_to_cpu(magic_of); |
| 24 | + return magic == UBIFS_NODE_MAGIC; |
| 25 | +} |
| 26 | + |
| 27 | +static void __init ubiblock_create_auto_rootfs(void) |
| 28 | +{ |
| 29 | + int ubi_num, ret, is_ubifs; |
| 30 | + struct ubi_volume_desc *desc; |
| 31 | + struct ubi_volume_info vi; |
| 32 | + |
| 33 | + for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++) { |
| 34 | + desc = ubi_open_volume_nm(ubi_num, "rootfs", UBI_READONLY); |
| 35 | + if (IS_ERR(desc)) |
| 36 | + continue; |
| 37 | + |
| 38 | + ubi_get_volume_info(desc, &vi); |
| 39 | + is_ubifs = ubi_vol_is_ubifs(desc); |
| 40 | + ubi_close_volume(desc); |
| 41 | + if (is_ubifs) |
| 42 | + break; |
| 43 | + |
| 44 | + ret = ubiblock_create(&vi); |
| 45 | + if (ret) |
| 46 | + pr_err("UBI error: block: can't add '%s' volume, err=%d\n", |
| 47 | + vi.name, ret); |
| 48 | + /* always break if we get here */ |
| 49 | + break; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | static void ubiblock_remove_all(void) |
| 54 | { |
| 55 | struct ubiblock *next; |
| 56 | @@ -684,6 +722,10 @@ int __init ubiblock_init(void) |
| 57 | */ |
| 58 | ubiblock_create_from_param(); |
| 59 | |
| 60 | + /* auto-attach "rootfs" volume if existing and non-ubifs */ |
| 61 | + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV)) |
| 62 | + ubiblock_create_auto_rootfs(); |
| 63 | + |
| 64 | /* |
| 65 | * Block devices are only created upon user requests, so we ignore |
| 66 | * existing volumes. |