]> Git Repo - J-u-boot.git/blame - cmd/bcb.c
command: Remove the cmd_tbl_t typedef
[J-u-boot.git] / cmd / bcb.c
CommitLineData
db7b7a05
ER
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Eugeniu Rosca <[email protected]>
4 *
5 * Command to read/modify/write Android BCB fields
6 */
7
8#include <android_bootloader_message.h>
9#include <command.h>
10#include <common.h>
e6f6f9e6 11#include <part.h>
db7b7a05
ER
12
13enum bcb_cmd {
14 BCB_CMD_LOAD,
15 BCB_CMD_FIELD_SET,
16 BCB_CMD_FIELD_CLEAR,
17 BCB_CMD_FIELD_TEST,
18 BCB_CMD_FIELD_DUMP,
19 BCB_CMD_STORE,
20};
21
22static int bcb_dev = -1;
23static int bcb_part = -1;
24static struct bootloader_message bcb = { { 0 } };
25
26static int bcb_cmd_get(char *cmd)
27{
739168cf 28 if (!strcmp(cmd, "load"))
db7b7a05 29 return BCB_CMD_LOAD;
739168cf 30 if (!strcmp(cmd, "set"))
db7b7a05 31 return BCB_CMD_FIELD_SET;
739168cf 32 if (!strcmp(cmd, "clear"))
db7b7a05 33 return BCB_CMD_FIELD_CLEAR;
739168cf 34 if (!strcmp(cmd, "test"))
db7b7a05 35 return BCB_CMD_FIELD_TEST;
739168cf 36 if (!strcmp(cmd, "store"))
db7b7a05 37 return BCB_CMD_STORE;
739168cf 38 if (!strcmp(cmd, "dump"))
db7b7a05
ER
39 return BCB_CMD_FIELD_DUMP;
40 else
41 return -1;
42}
43
44static int bcb_is_misused(int argc, char *const argv[])
45{
46 int cmd = bcb_cmd_get(argv[0]);
47
48 switch (cmd) {
49 case BCB_CMD_LOAD:
db7b7a05
ER
50 case BCB_CMD_FIELD_SET:
51 if (argc != 3)
52 goto err;
53 break;
54 case BCB_CMD_FIELD_TEST:
55 if (argc != 4)
56 goto err;
57 break;
58 case BCB_CMD_FIELD_CLEAR:
59 if (argc != 1 && argc != 2)
60 goto err;
61 break;
62 case BCB_CMD_STORE:
63 if (argc != 1)
64 goto err;
65 break;
66 case BCB_CMD_FIELD_DUMP:
67 if (argc != 2)
68 goto err;
69 break;
70 default:
71 printf("Error: 'bcb %s' not supported\n", argv[0]);
72 return -1;
73 }
74
75 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
76 printf("Error: Please, load BCB first!\n");
77 return -1;
78 }
79
80 return 0;
81err:
82 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
83
84 return -1;
85}
86
37f1811a 87static int bcb_field_get(char *name, char **fieldp, int *sizep)
db7b7a05 88{
739168cf 89 if (!strcmp(name, "command")) {
37f1811a
ER
90 *fieldp = bcb.command;
91 *sizep = sizeof(bcb.command);
739168cf 92 } else if (!strcmp(name, "status")) {
37f1811a
ER
93 *fieldp = bcb.status;
94 *sizep = sizeof(bcb.status);
739168cf 95 } else if (!strcmp(name, "recovery")) {
37f1811a
ER
96 *fieldp = bcb.recovery;
97 *sizep = sizeof(bcb.recovery);
739168cf 98 } else if (!strcmp(name, "stage")) {
37f1811a
ER
99 *fieldp = bcb.stage;
100 *sizep = sizeof(bcb.stage);
739168cf 101 } else if (!strcmp(name, "reserved")) {
37f1811a
ER
102 *fieldp = bcb.reserved;
103 *sizep = sizeof(bcb.reserved);
db7b7a05
ER
104 } else {
105 printf("Error: Unknown bcb field '%s'\n", name);
106 return -1;
107 }
108
109 return 0;
110}
111
09140113
SG
112static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
113 char *const argv[])
db7b7a05
ER
114{
115 struct blk_desc *desc;
0528979f 116 struct disk_partition info;
db7b7a05
ER
117 u64 cnt;
118 char *endp;
119 int part, ret;
120
121 ret = blk_get_device_by_str("mmc", argv[1], &desc);
122 if (ret < 0)
37f1811a 123 goto err_read_fail;
db7b7a05
ER
124
125 part = simple_strtoul(argv[2], &endp, 0);
126 if (*endp == '\0') {
127 ret = part_get_info(desc, part, &info);
128 if (ret)
37f1811a 129 goto err_read_fail;
db7b7a05
ER
130 } else {
131 part = part_get_info_by_name(desc, argv[2], &info);
132 if (part < 0) {
133 ret = part;
37f1811a 134 goto err_read_fail;
db7b7a05
ER
135 }
136 }
137
138 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
139 if (cnt > info.size)
37f1811a 140 goto err_too_small;
db7b7a05
ER
141
142 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
143 ret = -EIO;
37f1811a 144 goto err_read_fail;
db7b7a05
ER
145 }
146
147 bcb_dev = desc->devnum;
148 bcb_part = part;
149 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
150
151 return CMD_RET_SUCCESS;
37f1811a 152err_read_fail:
db7b7a05
ER
153 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
154 goto err;
37f1811a 155err_too_small:
db7b7a05
ER
156 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
157 goto err;
158err:
159 bcb_dev = -1;
160 bcb_part = -1;
161
162 return CMD_RET_FAILURE;
163}
164
09140113
SG
165static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
166 char *const argv[])
db7b7a05
ER
167{
168 int size, len;
169 char *field, *str, *found;
170
171 if (bcb_field_get(argv[1], &field, &size))
172 return CMD_RET_FAILURE;
173
174 len = strlen(argv[2]);
175 if (len >= size) {
176 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
177 argv[2], len, size, argv[1]);
178 return CMD_RET_FAILURE;
179 }
180 str = argv[2];
181
182 field[0] = '\0';
183 while ((found = strsep(&str, ":"))) {
184 if (field[0] != '\0')
185 strcat(field, "\n");
186 strcat(field, found);
187 }
188
189 return CMD_RET_SUCCESS;
190}
191
09140113
SG
192static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
193 char *const argv[])
db7b7a05
ER
194{
195 int size;
196 char *field;
197
198 if (argc == 1) {
199 memset(&bcb, 0, sizeof(bcb));
200 return CMD_RET_SUCCESS;
201 }
202
203 if (bcb_field_get(argv[1], &field, &size))
204 return CMD_RET_FAILURE;
205
206 memset(field, 0, size);
207
208 return CMD_RET_SUCCESS;
209}
210
09140113
SG
211static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
212 char *const argv[])
db7b7a05
ER
213{
214 int size;
215 char *field;
216 char *op = argv[2];
217
218 if (bcb_field_get(argv[1], &field, &size))
219 return CMD_RET_FAILURE;
220
221 if (*op == '=' && *(op + 1) == '\0') {
222 if (!strncmp(argv[3], field, size))
223 return CMD_RET_SUCCESS;
224 else
225 return CMD_RET_FAILURE;
226 } else if (*op == '~' && *(op + 1) == '\0') {
227 if (!strstr(field, argv[3]))
228 return CMD_RET_FAILURE;
229 else
230 return CMD_RET_SUCCESS;
231 } else {
232 printf("Error: Unknown operator '%s'\n", op);
233 }
234
235 return CMD_RET_FAILURE;
236}
237
09140113
SG
238static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
239 char *const argv[])
db7b7a05
ER
240{
241 int size;
242 char *field;
243
244 if (bcb_field_get(argv[1], &field, &size))
245 return CMD_RET_FAILURE;
246
247 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
248
249 return CMD_RET_SUCCESS;
250}
251
09140113
SG
252static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
253 char *const argv[])
db7b7a05
ER
254{
255 struct blk_desc *desc;
0528979f 256 struct disk_partition info;
db7b7a05
ER
257 u64 cnt;
258 int ret;
259
260 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
261 if (!desc) {
262 ret = -ENODEV;
263 goto err;
264 }
265
266 ret = part_get_info(desc, bcb_part, &info);
267 if (ret)
268 goto err;
269
270 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
271
272 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
273 ret = -EIO;
274 goto err;
275 }
276
277 return CMD_RET_SUCCESS;
278err:
279 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
280
281 return CMD_RET_FAILURE;
282}
283
09140113 284static struct cmd_tbl cmd_bcb_sub[] = {
db7b7a05
ER
285 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
286 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
287 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
288 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
289 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
290 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
291};
292
09140113 293static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
db7b7a05 294{
09140113 295 struct cmd_tbl *c;
db7b7a05
ER
296
297 if (argc < 2)
298 return CMD_RET_USAGE;
299
300 argc--;
301 argv++;
302
303 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
304 if (!c)
305 return CMD_RET_USAGE;
306
307 if (bcb_is_misused(argc, argv)) {
37f1811a
ER
308 /*
309 * We try to improve the user experience by reporting the
db7b7a05
ER
310 * root-cause of misusage, so don't return CMD_RET_USAGE,
311 * since the latter prints out the full-blown help text
312 */
313 return CMD_RET_FAILURE;
314 }
315
316 return c->cmd(cmdtp, flag, argc, argv);
317}
318
319U_BOOT_CMD(
320 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
321 "Load/set/clear/test/dump/store Android BCB fields",
322 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
323 "bcb set <field> <val> - set BCB <field> to <val>\n"
324 "bcb clear [<field>] - clear BCB <field> or all fields\n"
325 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
326 "bcb dump <field> - dump BCB <field>\n"
327 "bcb store - store BCB back to mmc\n"
328 "\n"
329 "Legend:\n"
330 "<dev> - MMC device index containing the BCB partition\n"
331 "<part> - MMC partition index or name containing the BCB\n"
332 "<field> - one of {command,status,recovery,stage,reserved}\n"
333 "<op> - the binary operator used in 'bcb test':\n"
334 " '=' returns true if <val> matches the string stored in <field>\n"
335 " '~' returns true if <val> matches a subset of <field>'s string\n"
336 "<val> - string/text provided as input to bcb {set,test}\n"
337 " NOTE: any ':' character in <val> will be replaced by line feed\n"
338 " during 'bcb set' and used as separator by upper layers\n"
339);
This page took 0.079127 seconds and 4 git commands to generate.