--- zzzz-none-000/linux-4.9.218/drivers/mtd/ubi/block.c 2020-04-02 15:20:41.000000000 +0000 +++ seale-7590ac-750/linux-4.9.218/drivers/mtd/ubi/block.c 2022-11-30 09:46:20.000000000 +0000 @@ -50,6 +50,7 @@ #include #include #include +#include #include "ubi-media.h" #include "ubi.h" @@ -104,6 +105,8 @@ static DEFINE_MUTEX(devices_mutex); static int ubiblock_major; +static bool rootfsname_set = false; + static int __init ubiblock_set_param(const char *val, const struct kernel_param *kp) { @@ -164,6 +167,42 @@ return 0; } +static int __init ubiblock_set_rootfsname_param(const char *val, + const struct kernel_param *kp) +{ + size_t len; + struct ubiblock_param *param; + + if (ubiblock_devs) + return (0); /* if ubi block options already set by ubi.block, + * we can skip this param. */ + + if (!val) + return -EINVAL; + + len = strnlen(val, UBIBLOCK_PARAM_LEN); + if (len == 0) { + pr_warn("UBI: block: empty 'rootfsname=' parameter - ignored\n"); + return 0; + } + + if (len == UBIBLOCK_PARAM_LEN) { + pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n", + val, UBIBLOCK_PARAM_LEN); + return -EINVAL; + } + + param = &ubiblock_param[ubiblock_devs]; + param->ubi_num = 0; + param->vol_id = -1; + strcpy(param->name, val); + ubiblock_devs = 1; + + rootfsname_set = true; + + return (0); +} + static const struct kernel_param_ops ubiblock_param_ops = { .set = ubiblock_set_param, }; @@ -179,6 +218,12 @@ "Using both UBI device number and UBI volume number:\n" "ubi.block=0,0\n"); +/* Fallback approach if command line uses 'rootfsname=' */ +static const struct kernel_param_ops ubiblock_rootfsname_param_ops = { + .set = ubiblock_set_rootfsname_param, +}; +__module_param_call("", rootfsname, &ubiblock_rootfsname_param_ops, NULL, 0, -1, 0); + static struct ubiblock *find_dev_nolock(int ubi_num, int vol_id) { struct ubiblock *dev; @@ -447,6 +492,21 @@ dev_info(disk_to_dev(dev->gd), "created from ubi%d:%d(%s)", dev->ubi_num, dev->vol_id, vi->name); mutex_unlock(&devices_mutex); + + if (!strcmp(vi->name, "rootfs") && + IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) && + ROOT_DEV == 0) { + pr_notice("ubiblock: device ubiblock%d_%d (%s) set to be root filesystem\n", + dev->ubi_num, dev->vol_id, vi->name); + ROOT_DEV = MKDEV(gd->major, gd->first_minor); + } + + if (ROOT_DEV == 0 && rootfsname_set) { + ROOT_DEV = MKDEV(gd->major, gd->first_minor); + pr_notice("ubiblock: device ubiblock%d_%d (%s) set to be root filesystem\n", + dev->ubi_num, dev->vol_id, vi->name); + } + return 0; out_free_queue: @@ -635,6 +695,44 @@ } } +#define UBIFS_NODE_MAGIC 0x06101831 +static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc) +{ + int ret; + uint32_t magic_of, magic; + ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4); + if (ret) + return 0; + magic = le32_to_cpu(magic_of); + return magic == UBIFS_NODE_MAGIC; +} + +static void __init ubiblock_create_auto_rootfs(void) +{ + int ubi_num, ret, is_ubifs; + struct ubi_volume_desc *desc; + struct ubi_volume_info vi; + + for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++) { + desc = ubi_open_volume_nm(ubi_num, "rootfs", UBI_READONLY); + if (IS_ERR(desc)) + continue; + + ubi_get_volume_info(desc, &vi); + is_ubifs = ubi_vol_is_ubifs(desc); + ubi_close_volume(desc); + if (is_ubifs) + break; + + ret = ubiblock_create(&vi); + if (ret) + pr_err("UBI error: block: can't add '%s' volume, err=%d\n", + vi.name, ret); + /* always break if we get here */ + break; + } +} + static void ubiblock_remove_all(void) { struct ubiblock *next; @@ -667,6 +765,10 @@ */ ubiblock_create_from_param(); + /* auto-attach "rootfs" volume if existing and non-ubifs */ + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV)) + ubiblock_create_auto_rootfs(); + /* * Block devices are only created upon user requests, so we ignore * existing volumes.