1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2023 Addiva Elektronik
12 #include <dm/device.h>
14 static int blkmap_curr_dev;
18 lbaint_t blknr, blkcnt;
21 typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]);
28 static int do_blkmap_map_linear(struct map_ctx *ctx, int argc,
38 ldevnum = dectoul(argv[2], NULL);
39 lblknr = dectoul(argv[3], NULL);
41 lbd = blk_get_devnum_by_uclass_idname(argv[1], ldevnum);
43 printf("Found no device matching \"%s %d\"\n",
45 return CMD_RET_FAILURE;
48 err = blkmap_map_linear(ctx->dev, ctx->blknr, ctx->blkcnt,
51 printf("Unable to map \"%s %d\" at block 0x" LBAF ": %d\n",
52 argv[1], ldevnum, ctx->blknr, err);
54 return CMD_RET_FAILURE;
57 printf("Block 0x" LBAF "+0x" LBAF " mapped to block 0x" LBAF " of \"%s %d\"\n",
58 ctx->blknr, ctx->blkcnt, lblknr, argv[1], ldevnum);
59 return CMD_RET_SUCCESS;
62 static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
70 addr = hextoul(argv[1], NULL);
72 err = blkmap_map_pmem(ctx->dev, ctx->blknr, ctx->blkcnt, addr);
74 printf("Unable to map %#llx at block 0x" LBAF ": %d\n",
75 (unsigned long long)addr, ctx->blknr, err);
76 return CMD_RET_FAILURE;
79 printf("Block 0x" LBAF "+0x" LBAF " mapped to %#llx\n",
80 ctx->blknr, ctx->blkcnt, (unsigned long long)addr);
81 return CMD_RET_SUCCESS;
84 static struct map_handler map_handlers[] = {
85 { .name = "linear", .fn = do_blkmap_map_linear },
86 { .name = "mem", .fn = do_blkmap_map_mem },
91 static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag,
92 int argc, char *const argv[])
94 struct map_handler *handler;
100 ctx.dev = blkmap_from_label(argv[1]);
102 printf("\"%s\" is not the name of any known blkmap\n", argv[1]);
103 return CMD_RET_FAILURE;
106 ctx.blknr = hextoul(argv[2], NULL);
107 ctx.blkcnt = hextoul(argv[3], NULL);
111 for (handler = map_handlers; handler->name; handler++) {
112 if (!strcmp(handler->name, argv[0]))
113 return handler->fn(&ctx, argc, argv);
116 printf("Unknown map type \"%s\"\n", argv[0]);
117 return CMD_RET_USAGE;
120 static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag,
121 int argc, char *const argv[])
127 return CMD_RET_USAGE;
131 err = blkmap_create(label, NULL);
133 printf("Unable to create \"%s\": %d\n", label, err);
134 return CMD_RET_FAILURE;
137 printf("Created \"%s\"\n", label);
138 return CMD_RET_SUCCESS;
141 static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag,
142 int argc, char *const argv[])
149 return CMD_RET_USAGE;
153 dev = blkmap_from_label(label);
155 printf("\"%s\" is not the name of any known blkmap\n", label);
156 return CMD_RET_FAILURE;
159 err = blkmap_destroy(dev);
161 printf("Unable to destroy \"%s\": %d\n", label, err);
162 return CMD_RET_FAILURE;
165 printf("Destroyed \"%s\"\n", label);
166 return CMD_RET_SUCCESS;
169 static int do_blkmap_get(struct cmd_tbl *cmdtp, int flag,
170 int argc, char *const argv[])
177 return CMD_RET_USAGE;
181 dev = blkmap_from_label(label);
183 printf("\"%s\" is not the name of any known blkmap\n", label);
184 return CMD_RET_FAILURE;
187 if (!strcmp(argv[2], "dev")) {
189 printf("%d\n", dev_seq(dev));
191 err = env_set_hex(argv[3], dev_seq(dev));
193 return CMD_RET_FAILURE;
196 return CMD_RET_USAGE;
199 return CMD_RET_SUCCESS;
202 static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag,
203 int argc, char *const argv[])
205 /* The subcommand parsing pops the original argv[0] ("blkmap")
206 * which blk_common_cmd expects. Push it back again.
211 return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev);
214 U_BOOT_CMD_WITH_SUBCMDS(
215 blkmap, "Composeable virtual block devices",
216 "info - list configured devices\n"
217 "blkmap part - list available partitions on current blkmap device\n"
218 "blkmap dev [<dev>] - show or set current blkmap device\n"
219 "blkmap read <addr> <blk#> <cnt>\n"
220 "blkmap write <addr> <blk#> <cnt>\n"
221 "blkmap get <label> dev [<var>] - store device number in variable\n"
222 "blkmap create <label> - create device\n"
223 "blkmap destroy <label> - destroy device\n"
224 "blkmap map <label> <blk#> <cnt> linear <interface> <dev> <blk#> - device mapping\n"
225 "blkmap map <label> <blk#> <cnt> mem <addr> - memory mapping\n",
226 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_blkmap_common),
227 U_BOOT_SUBCMD_MKENT(part, 2, 1, do_blkmap_common),
228 U_BOOT_SUBCMD_MKENT(dev, 4, 1, do_blkmap_common),
229 U_BOOT_SUBCMD_MKENT(read, 5, 1, do_blkmap_common),
230 U_BOOT_SUBCMD_MKENT(write, 5, 1, do_blkmap_common),
231 U_BOOT_SUBCMD_MKENT(get, 5, 1, do_blkmap_get),
232 U_BOOT_SUBCMD_MKENT(create, 2, 1, do_blkmap_create),
233 U_BOOT_SUBCMD_MKENT(destroy, 2, 1, do_blkmap_destroy),
234 U_BOOT_SUBCMD_MKENT(map, 32, 1, do_blkmap_map));