blob: 164f80f1387d5f86f061fed1f92fe6ffcbfdc9a7 [file] [log] [blame]
Tobias Waldekranz91ec2592023-02-16 16:33:52 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2023 Addiva Elektronik
4 * Author: Tobias Waldekranz <tobias@waldekranz.com>
5 */
6
7#include <blk.h>
8#include <blkmap.h>
Tobias Waldekranz91ec2592023-02-16 16:33:52 +01009#include <command.h>
10#include <malloc.h>
11#include <dm/device.h>
12
13static int blkmap_curr_dev;
14
15struct map_ctx {
16 struct udevice *dev;
17 lbaint_t blknr, blkcnt;
18};
19
20typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]);
21
22struct map_handler {
23 const char *name;
24 map_parser_fn fn;
25};
26
Bin Menga5f93a12023-09-26 16:43:38 +080027static int do_blkmap_map_linear(struct map_ctx *ctx, int argc,
28 char *const argv[])
Tobias Waldekranz91ec2592023-02-16 16:33:52 +010029{
30 struct blk_desc *lbd;
31 int err, ldevnum;
32 lbaint_t lblknr;
33
34 if (argc < 4)
35 return CMD_RET_USAGE;
36
37 ldevnum = dectoul(argv[2], NULL);
38 lblknr = dectoul(argv[3], NULL);
39
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;
45 }
46
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);
52
53 return CMD_RET_FAILURE;
54 }
55
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;
59}
60
Bin Menga5f93a12023-09-26 16:43:38 +080061static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
Tobias Waldekranz91ec2592023-02-16 16:33:52 +010062{
63 phys_addr_t addr;
64 int err;
65
66 if (argc < 2)
67 return CMD_RET_USAGE;
68
69 addr = hextoul(argv[1], NULL);
70
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;
76 }
77
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;
81}
82
Bin Menga5f93a12023-09-26 16:43:38 +080083static struct map_handler map_handlers[] = {
Tobias Waldekranz91ec2592023-02-16 16:33:52 +010084 { .name = "linear", .fn = do_blkmap_map_linear },
85 { .name = "mem", .fn = do_blkmap_map_mem },
86
87 { .name = NULL }
88};
89
90static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag,
91 int argc, char *const argv[])
92{
93 struct map_handler *handler;
94 struct map_ctx ctx;
95
96 if (argc < 5)
97 return CMD_RET_USAGE;
98
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;
103 }
104
105 ctx.blknr = hextoul(argv[2], NULL);
106 ctx.blkcnt = hextoul(argv[3], NULL);
107 argc -= 4;
108 argv += 4;
109
110 for (handler = map_handlers; handler->name; handler++) {
111 if (!strcmp(handler->name, argv[0]))
112 return handler->fn(&ctx, argc, argv);
113 }
114
115 printf("Unknown map type \"%s\"\n", argv[0]);
116 return CMD_RET_USAGE;
117}
118
119static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag,
120 int argc, char *const argv[])
121{
122 const char *label;
123 int err;
124
125 if (argc != 2)
126 return CMD_RET_USAGE;
127
128 label = argv[1];
129
130 err = blkmap_create(label, NULL);
131 if (err) {
132 printf("Unable to create \"%s\": %d\n", label, err);
133 return CMD_RET_FAILURE;
134 }
135
136 printf("Created \"%s\"\n", label);
137 return CMD_RET_SUCCESS;
138}
139
140static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag,
141 int argc, char *const argv[])
142{
143 struct udevice *dev;
144 const char *label;
145 int err;
146
147 if (argc != 2)
148 return CMD_RET_USAGE;
149
150 label = argv[1];
151
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;
156 }
157
158 err = blkmap_destroy(dev);
159 if (err) {
160 printf("Unable to destroy \"%s\": %d\n", label, err);
161 return CMD_RET_FAILURE;
162 }
163
164 printf("Destroyed \"%s\"\n", label);
165 return CMD_RET_SUCCESS;
166}
167
168static int do_blkmap_get(struct cmd_tbl *cmdtp, int flag,
169 int argc, char *const argv[])
170{
171 struct udevice *dev;
172 const char *label;
173 int err;
174
175 if (argc < 3)
176 return CMD_RET_USAGE;
177
178 label = argv[1];
179
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;
184 }
185
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;
193 }
194 } else {
195 return CMD_RET_USAGE;
196 }
197
198 return CMD_RET_SUCCESS;
199}
200
201static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag,
202 int argc, char *const argv[])
203{
204 /* The subcommand parsing pops the original argv[0] ("blkmap")
205 * which blk_common_cmd expects. Push it back again.
206 */
207 argc++;
208 argv--;
209
210 return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev);
211}
212
213U_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));