Skip to contentSkip to conversation

Read-only block driver on top of UBI volumes

PATCH New Juha Sarlin 2 participants 2 messages Nov 21, 2019 – Dec 28, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Read-only block driver on top of UBI volumes

Add "ubi block" command to create a block device on top of a UBI volume. This block device can be used with some U-Boot commands, eg: fstype, ls and load.

Example use:
=> ubi part fs
=> ubi block root
root dev=0
=> fstype ubi 0
squashfs

ubiblock_read() is based on drivers/mtd/ubi/block.c from Linux 4.19.75.

---

 cmd/ubi.c                    |  36 ++++++++++
 disk/part.c                  |  22 ------
 drivers/block/blk-uclass.c   |   4 +-
 drivers/mtd/ubi/Kconfig      |  15 ++++
 drivers/mtd/ubi/Makefile     |   2 +
 drivers/mtd/ubi/block.c      | 134 +++++++++++++++++++++++++++++++++++
 drivers/mtd/ubi/build.c      |   1 +
 drivers/mtd/ubi/ubi-uclass.c |  74 +++++++++++++++++++
 drivers/mtd/ubi/ubi.h        |  19 +++--
 include/blk.h                |   1 +
 include/dm/uclass-id.h       |   1 +
 include/linux/mtd/ubi.h      |   2 +-
 include/ubi_uboot.h          |   2 -
 13 files changed, 276 insertions(+), 37 deletions(-)
 create mode 100644 drivers/mtd/ubi/block.c
 create mode 100644 drivers/mtd/ubi/ubi-uclass.c
cmd/ubi.c +36 -0
index 22ba5b1a2c..ee5738a9f2 100644
@@ -102,6 +102,34 @@ static int ubi_check(char *name)
 	return 1;
 }
 
+/**
+ * Create BLK device on UBI volume.
+ *
+ * @name: [[ubi]dev]:volume
+ * @return CMD_RET_SUCCESS or CMD_RET_FAILURE
+ */
+static int ubi_block(char *name)
+{
+	const char *device, *volume;
+	int ret, ubi_num = 0;
+
+	volume = strchr(name, ':');
+	if (volume) {
+		volume++;
+		device = strncmp(name, "ubi", 3) ? name : name + 3;
+		ubi_num = simple_strtoul(device, NULL, 0);
+	} else {
+		volume = name;
+	}
+	ret = ubiblock_create(ubi_num, volume);
+	if (ret < 0) {
+		pr_err("Can't create ubi block device: %s\n", errno_str(ret));
+		return CMD_RET_FAILURE;
+	}
+	printf("%s dev=%d\n", name, ret);
+	return CMD_RET_SUCCESS;
+}
+
 static int verify_mkvol_req(const struct ubi_device *ubi,
 			    const struct ubi_mkvol_req *req)
 {
@@ -541,6 +569,13 @@ static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return ubi_info(layout);
 	}
 
+	if (strcmp(argv[1], "block") == 0) {
+		if (argc > 2)
+			return ubi_block(argv[2]);
+		printf("No volume name\n");
+		return CMD_RET_FAILURE;
+	}
+
 	if (strcmp(argv[1], "check") == 0) {
 		if (argc > 2)
 			return ubi_check(argv[2]);
@@ -688,6 +723,7 @@ U_BOOT_CMD(
 		" - Write part of a volume from address\n"
 	"ubi read[vol] address volume [size]"
 		" - Read volume to address with size\n"
+	"ubi block volume - Create block device on UBI volume\n"
 	"ubi remove[vol] volume"
 		" - Remove volume\n"
 	"ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
disk/part.c +0 -22
index 8982ef3bae..f168311e9a 100644
@@ -466,28 +466,6 @@ int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
 	}
 #endif
 
-#ifdef CONFIG_CMD_UBIFS
-	/*
-	 * Special-case ubi, ubi goes through a mtd, rather than through
-	 * a regular block device.
-	 */
-	if (0 == strcmp(ifname, "ubi")) {
-		if (!ubifs_is_mounted()) {
-			printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
-			return -1;
-		}
-
-		*dev_desc = NULL;
-		memset(info, 0, sizeof(*info));
-		strcpy((char *)info->type, BOOT_PART_TYPE);
-		strcpy((char *)info->name, "UBI");
-#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
-		info->uuid[0] = 0;
-#endif
-		return 0;
-	}
-#endif
-
 	/* If no dev_part_str, use bootdevice environment variable */
 	if (!dev_part_str || !strlen(dev_part_str) ||
 	    !strcmp(dev_part_str, "-"))
drivers/block/blk-uclass.c +3 -1
index ca8978f0e1..433ea16db4 100644
@@ -24,6 +24,7 @@ static const char *if_typename_str[IF_TYPE_COUNT] = {
 	[IF_TYPE_NVME]		= "nvme",
 	[IF_TYPE_EFI]		= "efi",
 	[IF_TYPE_VIRTIO]	= "virtio",
+	[IF_TYPE_UBI]		= "ubi",
 };
 
 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
@@ -39,6 +40,7 @@ static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
 	[IF_TYPE_NVME]		= UCLASS_NVME,
 	[IF_TYPE_EFI]		= UCLASS_EFI,
 	[IF_TYPE_VIRTIO]	= UCLASS_VIRTIO,
+	[IF_TYPE_UBI]		= UCLASS_UBI,
 };
 
 static enum if_type if_typename_to_iftype(const char *if_typename)
@@ -112,7 +114,7 @@ struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
 
 		debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
 		      if_type, devnum, dev->name, desc->if_type, desc->devnum);
-		if (desc->devnum != devnum)
+		if (desc->if_type != if_type || desc->devnum != devnum)
 			continue;
 
 		/* Find out the parent device uclass */
drivers/mtd/ubi/Kconfig +15 -0
index a78fd51ba7..008494db1a 100644
@@ -95,6 +95,21 @@ config MTD_UBI_FASTMAP_AUTOCONVERT
 	  Set this parameter to enable fastmap automatically on images
 	  without a fastmap.
 
+config MTD_UBI_BLOCK
+	bool "Read-only block devices on top of UBI volumes"
+	default n
+	help
+	   This option enables read-only UBI block devices support. UBI block
+	   devices will be layered on top of UBI volumes, which means that the
+	   UBI driver will transparently handle things like bad eraseblocks and
+	   bit-flips. You can put any block-oriented file system on top of UBI
+	   volumes in read-only mode (e.g., ext4), but it is probably most
+	   practical for read-only file systems, like squashfs.
+
+	   When selected, this feature will be built in the UBI driver.
+
+	   If in doubt, say "N".
+
 config MTD_UBI_FM_DEBUG
 	int "Enable UBI fastmap debug"
 	depends on MTD_UBI_FASTMAP
drivers/mtd/ubi/Makefile +2 -0
index 30d00fbdfe..92ea1fa0d1 100644
@@ -4,6 +4,8 @@
 # Wolfgang Denk, DENX Software Engineering, wd at denx.de.
 
 obj-y += attach.o build.o vtbl.o vmt.o upd.o kapi.o eba.o io.o wl.o crc32.o
+obj-$(CONFIG_MTD_UBI) += ubi-uclass.o
+obj-$(CONFIG_MTD_UBI_BLOCK) += block.o
 obj-$(CONFIG_MTD_UBI_FASTMAP) += fastmap.o
 obj-y += misc.o
 obj-y += debug.o
drivers/mtd/ubi/block.c new file +134 -0

134 lines not on this page · raw message

drivers/mtd/ubi/build.c +1 -0
index 42c5270c7f..0c15057a8d 100644
@@ -1112,6 +1112,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
 	if (!ubi)
 		return -EINVAL;
 
+	ubi_put(ubi_num);
 	spin_lock(&ubi_devices_lock);
 	put_device(&ubi->dev);
 	ubi->ref_count -= 1;
drivers/mtd/ubi/ubi-uclass.c new file +74 -0

74 lines not on this page · raw message

drivers/mtd/ubi/ubi.h +8 -11
index f44960186b..c0acb67c23 100644
@@ -922,6 +922,10 @@ void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
 		      int pnum, const struct ubi_vid_hdr *vid_hdr);
 
+/* ubi-uclass.c */
+int ubi_get(int ubi_num, struct udevice **devp);
+int ubi_put(int ubi_num);
+
 /* fastmap.c */
 #ifdef CONFIG_MTD_UBI_FASTMAP
 size_t ubi_calc_fm_size(struct ubi_device *ubi);
@@ -933,19 +937,12 @@ static inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; }
 #endif
 
 /* block.c */
-#ifdef CONFIG_MTD_UBI_BLOCK
-int ubiblock_init(void);
-void ubiblock_exit(void);
-int ubiblock_create(struct ubi_volume_info *vi);
-int ubiblock_remove(struct ubi_volume_info *vi);
-#else
 static inline int ubiblock_init(void) { return 0; }
 static inline void ubiblock_exit(void) {}
-static inline int ubiblock_create(struct ubi_volume_info *vi)
-{
-	return -ENOSYS;
-}
-static inline int ubiblock_remove(struct ubi_volume_info *vi)
+#ifdef CONFIG_MTD_UBI_BLOCK
+int ubiblock_create(int ubi_num, const char *volume);
+#else
+static inline int ubiblock_create(int ubi_num, const char *volume)
 {
 	return -ENOSYS;
 }
include/blk.h +1 -0
index d0c033aece..b4c5c35248 100644
@@ -34,6 +34,7 @@ enum if_type {
 	IF_TYPE_NVME,
 	IF_TYPE_EFI,
 	IF_TYPE_VIRTIO,
+	IF_TYPE_UBI,
 
 	IF_TYPE_COUNT,			/* Number of interface types */
 };
include/dm/uclass-id.h +1 -0
index 0c563d898b..b8bc400661 100644
@@ -102,6 +102,7 @@ enum uclass_id {
 	UCLASS_THERMAL,		/* Thermal sensor */
 	UCLASS_TIMER,		/* Timer device */
 	UCLASS_TPM,		/* Trusted Platform Module TIS interface */
+	UCLASS_UBI,		/* Unsorted Block Images */
 	UCLASS_UFS,		/* Universal Flash Storage */
 	UCLASS_USB,		/* USB bus */
 	UCLASS_USB_DEV_GENERIC,	/* USB generic device */
include/linux/mtd/ubi.h +1 -1
index badf6a0c6c..6c7f22a42b 100644
@@ -271,7 +271,7 @@ int ubi_flush(int ubi_num, int vol_id, int lnum);
  * This function is the same as the 'ubi_leb_read()' function, but it does not
  * provide the checking capability.
  */
-static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
+static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, void *buf,
 			   int offset, int len)
 {
 	return ubi_leb_read(desc, lnum, buf, offset, len, 0);
include/ubi_uboot.h +0 -2
index 0770228cd8..7a602c766c 100644
@@ -46,8 +46,6 @@
 #undef CONFIG_MTD_UBI_DEBUG_MSG_IO
 #undef CONFIG_MTD_UBI_DEBUG_MSG_BLD
 
-#undef CONFIG_MTD_UBI_BLOCK
-
 /* ubi_init() disables returning error codes when built into the Linux
  * kernel so that it doesn't hang the Linux kernel boot process.  Since
  * the U-Boot driver code depends on getting valid error codes from this
Signature · 1 line
2.20.1.98.gecbdaf0899
#1 lore raw

PATCH 1/1 Read-only block driver on top of UBI volumes

Hi Juha,

Quoted 17 lines · Juha Sarlin

Add "ubi block" command to create a block device on top of a UBI volume. This block device can be used with some U-Boot commands, eg: fstype, ls and load.

Example use:
=> ubi part fs
=> ubi block root
root dev=0
=> fstype ubi 0
squashfs

ubiblock_read() is based on drivers/mtd/ubi/block.c from Linux 4.19.75.

---

Quoted 16 lines
 cmd/ubi.c                    |  36 ++++++++++
 disk/part.c                  |  22 ------
 drivers/block/blk-uclass.c   |   4 +-
 drivers/mtd/ubi/Kconfig      |  15 ++++
 drivers/mtd/ubi/Makefile     |   2 +
 drivers/mtd/ubi/block.c      | 134 +++++++++++++++++++++++++++++++++++
 drivers/mtd/ubi/build.c      |   1 +
 drivers/mtd/ubi/ubi-uclass.c |  74 +++++++++++++++++++
 drivers/mtd/ubi/ubi.h        |  19 +++--
 include/blk.h                |   1 +
 include/dm/uclass-id.h       |   1 +
 include/linux/mtd/ubi.h      |   2 +-
 include/ubi_uboot.h          |   2 -
 13 files changed, 276 insertions(+), 37 deletions(-)
 create mode 100644 drivers/mtd/ubi/block.c
 create mode 100644 drivers/mtd/ubi/ubi-uclass.c

Thank you for the patch.

Please can you add comments to all the functions (see standard format).

Also I think you should split this patch up a bit, so that your changes to cmd/, disk/ and {drivers,include}/block* are in in separate patches.

Regards,
Simon