Merge tag 'xilinx-for-v2025.01-rc3-v2' of https://source.denx.de/u-boot/custodians...
[u-boot.git] / cmd / blkmap.c
blob164f80f1387d5f86f061fed1f92fe6ffcbfdc9a7
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2023 Addiva Elektronik
4 * Author: Tobias Waldekranz <tobias@waldekranz.com>
5 */
7 #include <blk.h>
8 #include <blkmap.h>
9 #include <command.h>
10 #include <malloc.h>
11 #include <dm/device.h>
13 static int blkmap_curr_dev;
15 struct map_ctx {
16 struct udevice *dev;
17 lbaint_t blknr, blkcnt;
20 typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]);
22 struct map_handler {
23 const char *name;
24 map_parser_fn fn;
27 static int do_blkmap_map_linear(struct map_ctx *ctx, int argc,
28 char *const argv[])
30 struct blk_desc *lbd;
31 int err, ldevnum;
32 lbaint_t lblknr;
34 if (argc < 4)
35 return CMD_RET_USAGE;
37 ldevnum = dectoul(argv[2], NULL);
38 lblknr = dectoul(argv[3], NULL);
40 lbd = blk_get_devnum_by_uclass_idname(argv[1], ldevnum);
41 if (!lbd) {
42 printf("Found no device matching \"%s %d\"\n",
43 argv[1], ldevnum);
44 return CMD_RET_FAILURE;
47 err = blkmap_map_linear(ctx->dev, ctx->blknr, ctx->blkcnt,
48 lbd->bdev, lblknr);
49 if (err) {
50 printf("Unable to map \"%s %d\" at block 0x" LBAF ": %d\n",
51 argv[1], ldevnum, ctx->blknr, err);
53 return CMD_RET_FAILURE;
56 printf("Block 0x" LBAF "+0x" LBAF " mapped to block 0x" LBAF " of \"%s %d\"\n",
57 ctx->blknr, ctx->blkcnt, lblknr, argv[1], ldevnum);
58 return CMD_RET_SUCCESS;
61 static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
63 phys_addr_t addr;
64 int err;
66 if (argc < 2)
67 return CMD_RET_USAGE;
69 addr = hextoul(argv[1], NULL);
71 err = blkmap_map_pmem(ctx->dev, ctx->blknr, ctx->blkcnt, addr);
72 if (err) {
73 printf("Unable to map %#llx at block 0x" LBAF ": %d\n",
74 (unsigned long long)addr, ctx->blknr, err);
75 return CMD_RET_FAILURE;
78 printf("Block 0x" LBAF "+0x" LBAF " mapped to %#llx\n",
79 ctx->blknr, ctx->blkcnt, (unsigned long long)addr);
80 return CMD_RET_SUCCESS;
83 static struct map_handler map_handlers[] = {
84 { .name = "linear", .fn = do_blkmap_map_linear },
85 { .name = "mem", .fn = do_blkmap_map_mem },
87 { .name = NULL }
90 static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag,
91 int argc, char *const argv[])
93 struct map_handler *handler;
94 struct map_ctx ctx;
96 if (argc < 5)
97 return CMD_RET_USAGE;
99 ctx.dev = blkmap_from_label(argv[1]);
100 if (!ctx.dev) {
101 printf("\"%s\" is not the name of any known blkmap\n", argv[1]);
102 return CMD_RET_FAILURE;
105 ctx.blknr = hextoul(argv[2], NULL);
106 ctx.blkcnt = hextoul(argv[3], NULL);
107 argc -= 4;
108 argv += 4;
110 for (handler = map_handlers; handler->name; handler++) {
111 if (!strcmp(handler->name, argv[0]))
112 return handler->fn(&ctx, argc, argv);
115 printf("Unknown map type \"%s\"\n", argv[0]);
116 return CMD_RET_USAGE;
119 static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag,
120 int argc, char *const argv[])
122 const char *label;
123 int err;
125 if (argc != 2)
126 return CMD_RET_USAGE;
128 label = argv[1];
130 err = blkmap_create(label, NULL);
131 if (err) {
132 printf("Unable to create \"%s\": %d\n", label, err);
133 return CMD_RET_FAILURE;
136 printf("Created \"%s\"\n", label);
137 return CMD_RET_SUCCESS;
140 static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag,
141 int argc, char *const argv[])
143 struct udevice *dev;
144 const char *label;
145 int err;
147 if (argc != 2)
148 return CMD_RET_USAGE;
150 label = argv[1];
152 dev = blkmap_from_label(label);
153 if (!dev) {
154 printf("\"%s\" is not the name of any known blkmap\n", label);
155 return CMD_RET_FAILURE;
158 err = blkmap_destroy(dev);
159 if (err) {
160 printf("Unable to destroy \"%s\": %d\n", label, err);
161 return CMD_RET_FAILURE;
164 printf("Destroyed \"%s\"\n", label);
165 return CMD_RET_SUCCESS;
168 static int do_blkmap_get(struct cmd_tbl *cmdtp, int flag,
169 int argc, char *const argv[])
171 struct udevice *dev;
172 const char *label;
173 int err;
175 if (argc < 3)
176 return CMD_RET_USAGE;
178 label = argv[1];
180 dev = blkmap_from_label(label);
181 if (!dev) {
182 printf("\"%s\" is not the name of any known blkmap\n", label);
183 return CMD_RET_FAILURE;
186 if (!strcmp(argv[2], "dev")) {
187 if (argc == 3) {
188 printf("%d\n", dev_seq(dev));
189 } else {
190 err = env_set_hex(argv[3], dev_seq(dev));
191 if (err)
192 return CMD_RET_FAILURE;
194 } else {
195 return CMD_RET_USAGE;
198 return CMD_RET_SUCCESS;
201 static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag,
202 int argc, char *const argv[])
204 /* The subcommand parsing pops the original argv[0] ("blkmap")
205 * which blk_common_cmd expects. Push it back again.
207 argc++;
208 argv--;
210 return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev);
213 U_BOOT_CMD_WITH_SUBCMDS(
214 blkmap, "Composeable virtual block devices",
215 "info - list configured devices\n"
216 "blkmap part - list available partitions on current blkmap device\n"
217 "blkmap dev [<dev>] - show or set current blkmap device\n"
218 "blkmap read <addr> <blk#> <cnt>\n"
219 "blkmap write <addr> <blk#> <cnt>\n"
220 "blkmap get <label> dev [<var>] - store device number in variable\n"
221 "blkmap create <label> - create device\n"
222 "blkmap destroy <label> - destroy device\n"
223 "blkmap map <label> <blk#> <cnt> linear <interface> <dev> <blk#> - device mapping\n"
224 "blkmap map <label> <blk#> <cnt> mem <addr> - memory mapping\n",
225 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_blkmap_common),
226 U_BOOT_SUBCMD_MKENT(part, 2, 1, do_blkmap_common),
227 U_BOOT_SUBCMD_MKENT(dev, 4, 1, do_blkmap_common),
228 U_BOOT_SUBCMD_MKENT(read, 5, 1, do_blkmap_common),
229 U_BOOT_SUBCMD_MKENT(write, 5, 1, do_blkmap_common),
230 U_BOOT_SUBCMD_MKENT(get, 5, 1, do_blkmap_get),
231 U_BOOT_SUBCMD_MKENT(create, 2, 1, do_blkmap_create),
232 U_BOOT_SUBCMD_MKENT(destroy, 2, 1, do_blkmap_destroy),
233 U_BOOT_SUBCMD_MKENT(map, 32, 1, do_blkmap_map));