]>
Commit | Line | Data |
---|---|---|
bb56da11 TW |
1 | // 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> | |
d678a59d | 9 | #include <common.h> |
bb56da11 TW |
10 | #include <command.h> |
11 | #include <malloc.h> | |
12 | #include <dm/device.h> | |
13 | ||
14 | static int blkmap_curr_dev; | |
15 | ||
16 | struct map_ctx { | |
17 | struct udevice *dev; | |
18 | lbaint_t blknr, blkcnt; | |
19 | }; | |
20 | ||
21 | typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]); | |
22 | ||
23 | struct map_handler { | |
24 | const char *name; | |
25 | map_parser_fn fn; | |
26 | }; | |
27 | ||
42411e06 BM |
28 | static int do_blkmap_map_linear(struct map_ctx *ctx, int argc, |
29 | char *const argv[]) | |
bb56da11 TW |
30 | { |
31 | struct blk_desc *lbd; | |
32 | int err, ldevnum; | |
33 | lbaint_t lblknr; | |
34 | ||
35 | if (argc < 4) | |
36 | return CMD_RET_USAGE; | |
37 | ||
38 | ldevnum = dectoul(argv[2], NULL); | |
39 | lblknr = dectoul(argv[3], NULL); | |
40 | ||
41 | lbd = blk_get_devnum_by_uclass_idname(argv[1], ldevnum); | |
42 | if (!lbd) { | |
43 | printf("Found no device matching \"%s %d\"\n", | |
44 | argv[1], ldevnum); | |
45 | return CMD_RET_FAILURE; | |
46 | } | |
47 | ||
48 | err = blkmap_map_linear(ctx->dev, ctx->blknr, ctx->blkcnt, | |
49 | lbd->bdev, lblknr); | |
50 | if (err) { | |
51 | printf("Unable to map \"%s %d\" at block 0x" LBAF ": %d\n", | |
52 | argv[1], ldevnum, ctx->blknr, err); | |
53 | ||
54 | return CMD_RET_FAILURE; | |
55 | } | |
56 | ||
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; | |
60 | } | |
61 | ||
42411e06 | 62 | static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[]) |
bb56da11 TW |
63 | { |
64 | phys_addr_t addr; | |
65 | int err; | |
66 | ||
67 | if (argc < 2) | |
68 | return CMD_RET_USAGE; | |
69 | ||
70 | addr = hextoul(argv[1], NULL); | |
71 | ||
72 | err = blkmap_map_pmem(ctx->dev, ctx->blknr, ctx->blkcnt, addr); | |
73 | if (err) { | |
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; | |
77 | } | |
78 | ||
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; | |
82 | } | |
83 | ||
42411e06 | 84 | static struct map_handler map_handlers[] = { |
bb56da11 TW |
85 | { .name = "linear", .fn = do_blkmap_map_linear }, |
86 | { .name = "mem", .fn = do_blkmap_map_mem }, | |
87 | ||
88 | { .name = NULL } | |
89 | }; | |
90 | ||
91 | static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag, | |
92 | int argc, char *const argv[]) | |
93 | { | |
94 | struct map_handler *handler; | |
95 | struct map_ctx ctx; | |
96 | ||
97 | if (argc < 5) | |
98 | return CMD_RET_USAGE; | |
99 | ||
100 | ctx.dev = blkmap_from_label(argv[1]); | |
101 | if (!ctx.dev) { | |
102 | printf("\"%s\" is not the name of any known blkmap\n", argv[1]); | |
103 | return CMD_RET_FAILURE; | |
104 | } | |
105 | ||
106 | ctx.blknr = hextoul(argv[2], NULL); | |
107 | ctx.blkcnt = hextoul(argv[3], NULL); | |
108 | argc -= 4; | |
109 | argv += 4; | |
110 | ||
111 | for (handler = map_handlers; handler->name; handler++) { | |
112 | if (!strcmp(handler->name, argv[0])) | |
113 | return handler->fn(&ctx, argc, argv); | |
114 | } | |
115 | ||
116 | printf("Unknown map type \"%s\"\n", argv[0]); | |
117 | return CMD_RET_USAGE; | |
118 | } | |
119 | ||
120 | static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag, | |
121 | int argc, char *const argv[]) | |
122 | { | |
123 | const char *label; | |
124 | int err; | |
125 | ||
126 | if (argc != 2) | |
127 | return CMD_RET_USAGE; | |
128 | ||
129 | label = argv[1]; | |
130 | ||
131 | err = blkmap_create(label, NULL); | |
132 | if (err) { | |
133 | printf("Unable to create \"%s\": %d\n", label, err); | |
134 | return CMD_RET_FAILURE; | |
135 | } | |
136 | ||
137 | printf("Created \"%s\"\n", label); | |
138 | return CMD_RET_SUCCESS; | |
139 | } | |
140 | ||
141 | static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag, | |
142 | int argc, char *const argv[]) | |
143 | { | |
144 | struct udevice *dev; | |
145 | const char *label; | |
146 | int err; | |
147 | ||
148 | if (argc != 2) | |
149 | return CMD_RET_USAGE; | |
150 | ||
151 | label = argv[1]; | |
152 | ||
153 | dev = blkmap_from_label(label); | |
154 | if (!dev) { | |
155 | printf("\"%s\" is not the name of any known blkmap\n", label); | |
156 | return CMD_RET_FAILURE; | |
157 | } | |
158 | ||
159 | err = blkmap_destroy(dev); | |
160 | if (err) { | |
161 | printf("Unable to destroy \"%s\": %d\n", label, err); | |
162 | return CMD_RET_FAILURE; | |
163 | } | |
164 | ||
165 | printf("Destroyed \"%s\"\n", label); | |
166 | return CMD_RET_SUCCESS; | |
167 | } | |
168 | ||
169 | static int do_blkmap_get(struct cmd_tbl *cmdtp, int flag, | |
170 | int argc, char *const argv[]) | |
171 | { | |
172 | struct udevice *dev; | |
173 | const char *label; | |
174 | int err; | |
175 | ||
176 | if (argc < 3) | |
177 | return CMD_RET_USAGE; | |
178 | ||
179 | label = argv[1]; | |
180 | ||
181 | dev = blkmap_from_label(label); | |
182 | if (!dev) { | |
183 | printf("\"%s\" is not the name of any known blkmap\n", label); | |
184 | return CMD_RET_FAILURE; | |
185 | } | |
186 | ||
187 | if (!strcmp(argv[2], "dev")) { | |
188 | if (argc == 3) { | |
189 | printf("%d\n", dev_seq(dev)); | |
190 | } else { | |
191 | err = env_set_hex(argv[3], dev_seq(dev)); | |
192 | if (err) | |
193 | return CMD_RET_FAILURE; | |
194 | } | |
195 | } else { | |
196 | return CMD_RET_USAGE; | |
197 | } | |
198 | ||
199 | return CMD_RET_SUCCESS; | |
200 | } | |
201 | ||
202 | static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag, | |
203 | int argc, char *const argv[]) | |
204 | { | |
205 | /* The subcommand parsing pops the original argv[0] ("blkmap") | |
206 | * which blk_common_cmd expects. Push it back again. | |
207 | */ | |
208 | argc++; | |
209 | argv--; | |
210 | ||
211 | return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev); | |
212 | } | |
213 | ||
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)); |