[PATCH 6/8] cmd: blkmap: Add blkmap command

Tobias Waldekranz tobias at waldekranz.com
Wed Feb 1 19:10:14 CET 2023


Add a frontend for the blkmap subsystem. In addition to the common
block device operations, this allows users to create and destroy
devices, and map in memory and slices of other block devices.

With that we support two primary use-cases:

- Being able to "distro boot" from a RAM disk. I.e., from an image
  where the kernel is stored in /boot of some filesystem supported
  by U-Boot.

- Accessing filesystems not located on exact partition boundaries,
  e.g. when a filesystem image is wrapped in an FIT image and stored
  in a disk partition.

Signed-off-by: Tobias Waldekranz <tobias at waldekranz.com>
---
 MAINTAINERS  |   1 +
 cmd/Kconfig  |  19 ++++++
 cmd/Makefile |   1 +
 cmd/blkmap.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 202 insertions(+)
 create mode 100644 cmd/blkmap.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 28a34231bf..83c0f90a53 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -789,6 +789,7 @@ F:	tools/binman/
 BLKMAP
 M:	Tobias Waldekranz <tobias at waldekranz.com>
 S:	Maintained
+F:	cmd/blkmap.c
 F:	drivers/block/blkmap.c
 F:	include/blkmap.h
 
diff --git a/cmd/Kconfig b/cmd/Kconfig
index dc0446e02e..cd35b8318d 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1953,6 +1953,25 @@ config CMD_BLOCK_CACHE
 	  during development, but also allows the cache to be disabled when
 	  it might hurt performance (e.g. when using the ums command).
 
+config CMD_BLKMAP
+	bool "blkmap - Composable virtual block devices"
+	depends on BLKMAP
+	default y if BLKMAP
+	help
+ 	  Create virtual block devices that are backed by various sources,
+ 	  e.g. RAM, or parts of an existing block device. Though much more
+ 	  rudimentary, it borrows a lot of ideas from Linux's device mapper
+ 	  subsystem.
+
+	  Example use-cases:
+	  - Treat a region of RAM as a block device, i.e. a RAM disk. This let's
+            you extract files from filesystem images stored in RAM (perhaps as a
+            result of a TFTP transfer).
+	  - Create a virtual partition on an existing device. This let's you
+            access filesystems that aren't stored at an exact partition
+            boundary. A common example is a filesystem image embedded in an FIT
+            image.
+
 config CMD_BUTTON
 	bool "button"
 	depends on BUTTON
diff --git a/cmd/Makefile b/cmd/Makefile
index 7b6ff73186..1d51fddec1 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -27,6 +27,7 @@ obj-$(CONFIG_CMD_BCB) += bcb.o
 obj-$(CONFIG_CMD_BDI) += bdinfo.o
 obj-$(CONFIG_CMD_BIND) += bind.o
 obj-$(CONFIG_CMD_BINOP) += binop.o
+obj-$(CONFIG_CMD_BLKMAP) += blkmap.o
 obj-$(CONFIG_CMD_BLOBLIST) += bloblist.o
 obj-$(CONFIG_CMD_BLOCK_CACHE) += blkcache.o
 obj-$(CONFIG_CMD_BMP) += bmp.o
diff --git a/cmd/blkmap.c b/cmd/blkmap.c
new file mode 100644
index 0000000000..f1d4a4bab0
--- /dev/null
+++ b/cmd/blkmap.c
@@ -0,0 +1,181 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2023 Addiva Elektronik
+ * Author: Tobias Waldekranz <tobias at waldekranz.com>
+ */
+
+#include <blk.h>
+#include <blkmap.h>
+#include <common.h>
+#include <command.h>
+#include <malloc.h>
+
+static int blkmap_curr_dev;
+
+struct map_ctx {
+	int devnum;
+	lbaint_t blknr, blkcnt;
+};
+
+typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]);
+
+struct map_handler {
+	const char *name;
+	map_parser_fn fn;
+};
+
+int do_blkmap_map_linear(struct map_ctx *ctx, int argc, char *const argv[])
+{
+	struct blk_desc *lbd;
+	int err, ldevnum;
+	lbaint_t lblknr;
+
+	if (argc < 4)
+		return CMD_RET_USAGE;
+
+	ldevnum = dectoul(argv[2], NULL);
+	lblknr = dectoul(argv[3], NULL);
+
+	lbd = blk_get_devnum_by_uclass_idname(argv[1], ldevnum);
+	if (!lbd) {
+		printf("Found no device matching \"%s %d\"\n",
+		       argv[1], ldevnum);
+		return CMD_RET_FAILURE;
+	}
+
+	err = blkmap_map_linear(ctx->devnum, ctx->blknr, ctx->blkcnt,
+				lbd->uclass_id, ldevnum, lblknr);
+	if (err) {
+		printf("Unable to map \"%s %d\" at block 0x" LBAF ": %d\n",
+		       argv[1], ldevnum, ctx->blknr, err);
+
+		return CMD_RET_FAILURE;
+	}
+
+	printf("Block 0x" LBAF "+0x" LBAF " mapped to block 0x" LBAF " of \"%s %d\"\n",
+	       ctx->blknr, ctx->blkcnt, lblknr, argv[1], ldevnum);
+	return CMD_RET_SUCCESS;
+}
+
+int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
+{
+	phys_addr_t addr;
+	int err;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	addr = hextoul(argv[1], NULL);
+
+	err = blkmap_map_pmem(ctx->devnum, ctx->blknr, ctx->blkcnt, addr);
+	if (err) {
+		printf("Unable to map %#llx at block 0x" LBAF ": %d\n",
+		       (unsigned long long)addr, ctx->blknr, err);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("Block 0x" LBAF "+0x" LBAF " mapped to %#llx\n",
+	       ctx->blknr, ctx->blkcnt, (unsigned long long)addr);
+	return CMD_RET_SUCCESS;
+}
+
+struct map_handler map_handlers[] = {
+	{ .name = "linear", .fn = do_blkmap_map_linear },
+	{ .name = "mem", .fn = do_blkmap_map_mem },
+
+	{ .name = NULL }
+};
+
+static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag,
+			 int argc, char *const argv[])
+{
+	struct map_handler *handler;
+	struct map_ctx ctx;
+
+	if (argc < 5)
+		return CMD_RET_USAGE;
+
+	ctx.devnum = dectoul(argv[1], NULL);
+	ctx.blknr = hextoul(argv[2], NULL);
+	ctx.blkcnt = hextoul(argv[3], NULL);
+	argc -= 4;
+	argv += 4;
+
+	for (handler = map_handlers; handler->name; handler++) {
+		if (!strcmp(handler->name, argv[0]))
+			return handler->fn(&ctx, argc, argv);
+	}
+
+	printf("Unknown map type \"%s\"\n", argv[0]);
+	return CMD_RET_USAGE;
+}
+
+static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag,
+			    int argc, char *const argv[])
+{
+	int devnum = -1;
+
+	if (argc == 2)
+		devnum = dectoul(argv[1], NULL);
+
+	devnum = blkmap_create(devnum);
+	if (devnum < 0) {
+		printf("Unable to create device: %d\n", devnum);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("Created device %d\n", devnum);
+	return CMD_RET_SUCCESS;
+}
+
+static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag,
+			     int argc, char *const argv[])
+{
+	int err, devnum;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+
+	devnum = dectoul(argv[1], NULL);
+
+	err = blkmap_destroy(devnum);
+	if (err) {
+		printf("Unable to destroy device %d: %d\n", devnum, err);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("Destroyed device %d\n", devnum);
+	return CMD_RET_SUCCESS;
+}
+
+static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag,
+			    int argc, char *const argv[])
+{
+	/* The subcommand parsing pops the original argv[0] ("blkmap")
+	 * which blk_common_cmd expects. Push it back again.
+	 */
+	argc++;
+	argv--;
+
+	return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev);
+}
+
+U_BOOT_CMD_WITH_SUBCMDS(
+	blkmap, "Composeable virtual block devices",
+	"info - list configured devices\n"
+	"blkmap part - list available partitions on current blkmap device\n"
+	"blkmap dev [<dev>] - show or set current blkmap device\n"
+	"blkmap read <addr> <blk#> <cnt>\n"
+	"blkmap write <addr> <blk#> <cnt>\n"
+	"blkmap create [<dev>] - create device\n"
+	"blkmap destroy <dev> - destroy device\n"
+	"blkmap map <dev> <blk#> <cnt> linear <interface> <dev> <blk#> - device mapping\n"
+	"blkmap map <dev> <blk#> <cnt> mem <addr> - memory mapping\n",
+	U_BOOT_SUBCMD_MKENT(info, 2, 1, do_blkmap_common),
+	U_BOOT_SUBCMD_MKENT(part, 2, 1, do_blkmap_common),
+	U_BOOT_SUBCMD_MKENT(dev, 4, 1, do_blkmap_common),
+	U_BOOT_SUBCMD_MKENT(read, 5, 1, do_blkmap_common),
+	U_BOOT_SUBCMD_MKENT(write, 5, 1, do_blkmap_common),
+	U_BOOT_SUBCMD_MKENT(create, 2, 1, do_blkmap_create),
+	U_BOOT_SUBCMD_MKENT(destroy, 2, 1, do_blkmap_destroy),
+	U_BOOT_SUBCMD_MKENT(map, 32, 1, do_blkmap_map));
-- 
2.34.1



More information about the U-Boot mailing list