blob: 6b6f1e9a2f10ca4f2ed6c10e203059f218c8aa43 [file] [log] [blame]
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Eugeniu Rosca <rosca.eugeniu@gmail.com>
4 *
5 * Command to read/modify/write Android BCB fields
6 */
7
8#include <android_bootloader_message.h>
Eugeniu Roscae74670a2020-10-23 11:52:22 +03009#include <bcb.h>
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020010#include <command.h>
11#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <part.h>
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +030014#include <malloc.h>
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020015
16enum bcb_cmd {
17 BCB_CMD_LOAD,
18 BCB_CMD_FIELD_SET,
19 BCB_CMD_FIELD_CLEAR,
20 BCB_CMD_FIELD_TEST,
21 BCB_CMD_FIELD_DUMP,
22 BCB_CMD_STORE,
23};
24
25static int bcb_dev = -1;
26static int bcb_part = -1;
27static struct bootloader_message bcb = { { 0 } };
28
29static int bcb_cmd_get(char *cmd)
30{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020031 if (!strcmp(cmd, "load"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020032 return BCB_CMD_LOAD;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020033 if (!strcmp(cmd, "set"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020034 return BCB_CMD_FIELD_SET;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020035 if (!strcmp(cmd, "clear"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020036 return BCB_CMD_FIELD_CLEAR;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020037 if (!strcmp(cmd, "test"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020038 return BCB_CMD_FIELD_TEST;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020039 if (!strcmp(cmd, "store"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020040 return BCB_CMD_STORE;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020041 if (!strcmp(cmd, "dump"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020042 return BCB_CMD_FIELD_DUMP;
43 else
44 return -1;
45}
46
47static int bcb_is_misused(int argc, char *const argv[])
48{
49 int cmd = bcb_cmd_get(argv[0]);
50
51 switch (cmd) {
52 case BCB_CMD_LOAD:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020053 case BCB_CMD_FIELD_SET:
54 if (argc != 3)
55 goto err;
56 break;
57 case BCB_CMD_FIELD_TEST:
58 if (argc != 4)
59 goto err;
60 break;
61 case BCB_CMD_FIELD_CLEAR:
62 if (argc != 1 && argc != 2)
63 goto err;
64 break;
65 case BCB_CMD_STORE:
66 if (argc != 1)
67 goto err;
68 break;
69 case BCB_CMD_FIELD_DUMP:
70 if (argc != 2)
71 goto err;
72 break;
73 default:
74 printf("Error: 'bcb %s' not supported\n", argv[0]);
75 return -1;
76 }
77
78 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
79 printf("Error: Please, load BCB first!\n");
80 return -1;
81 }
82
83 return 0;
84err:
85 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
86
87 return -1;
88}
89
Eugeniu Rosca37f18112019-07-19 23:26:14 +020090static int bcb_field_get(char *name, char **fieldp, int *sizep)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020091{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020092 if (!strcmp(name, "command")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020093 *fieldp = bcb.command;
94 *sizep = sizeof(bcb.command);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020095 } else if (!strcmp(name, "status")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020096 *fieldp = bcb.status;
97 *sizep = sizeof(bcb.status);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020098 } else if (!strcmp(name, "recovery")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020099 *fieldp = bcb.recovery;
100 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200101 } else if (!strcmp(name, "stage")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200102 *fieldp = bcb.stage;
103 *sizep = sizeof(bcb.stage);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200104 } else if (!strcmp(name, "reserved")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200105 *fieldp = bcb.reserved;
106 *sizep = sizeof(bcb.reserved);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200107 } else {
108 printf("Error: Unknown bcb field '%s'\n", name);
109 return -1;
110 }
111
112 return 0;
113}
114
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300115static int __bcb_load(int devnum, const char *partp)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200116{
117 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600118 struct disk_partition info;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200119 u64 cnt;
120 char *endp;
121 int part, ret;
122
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300123 desc = blk_get_devnum_by_type(IF_TYPE_MMC, devnum);
124 if (!desc) {
125 ret = -ENODEV;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200126 goto err_read_fail;
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300127 }
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200128
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300129 part = simple_strtoul(partp, &endp, 0);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200130 if (*endp == '\0') {
131 ret = part_get_info(desc, part, &info);
132 if (ret)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200133 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200134 } else {
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300135 part = part_get_info_by_name(desc, partp, &info);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200136 if (part < 0) {
137 ret = part;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200138 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200139 }
140 }
141
142 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
143 if (cnt > info.size)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200144 goto err_too_small;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200145
146 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
147 ret = -EIO;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200148 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200149 }
150
151 bcb_dev = desc->devnum;
152 bcb_part = part;
153 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
154
155 return CMD_RET_SUCCESS;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200156err_read_fail:
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300157 printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200158 goto err;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200159err_too_small:
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300160 printf("Error: mmc %d:%s too small!", devnum, partp);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200161 goto err;
162err:
163 bcb_dev = -1;
164 bcb_part = -1;
165
166 return CMD_RET_FAILURE;
167}
168
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300169static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
170 char * const argv[])
171{
172 char *endp;
173 int devnum = simple_strtoul(argv[1], &endp, 0);
174
175 if (*endp != '\0') {
176 printf("Error: Device id '%s' not a number\n", argv[1]);
177 return CMD_RET_FAILURE;
178 }
179
180 return __bcb_load(devnum, argv[2]);
181}
182
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300183static int __bcb_set(char *fieldp, const char *valp)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200184{
185 int size, len;
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300186 char *field, *str, *found, *tmp;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200187
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300188 if (bcb_field_get(fieldp, &field, &size))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200189 return CMD_RET_FAILURE;
190
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300191 len = strlen(valp);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200192 if (len >= size) {
193 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300194 valp, len, size, fieldp);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200195 return CMD_RET_FAILURE;
196 }
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300197 str = strdup(valp);
198 if (!str) {
199 printf("Error: Out of memory while strdup\n");
200 return CMD_RET_FAILURE;
201 }
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200202
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300203 tmp = str;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200204 field[0] = '\0';
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300205 while ((found = strsep(&tmp, ":"))) {
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200206 if (field[0] != '\0')
207 strcat(field, "\n");
208 strcat(field, found);
209 }
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300210 free(str);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200211
212 return CMD_RET_SUCCESS;
213}
214
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300215static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
216 char * const argv[])
217{
218 return __bcb_set(argv[1], argv[2]);
219}
220
Simon Glass09140112020-05-10 11:40:03 -0600221static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
222 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200223{
224 int size;
225 char *field;
226
227 if (argc == 1) {
228 memset(&bcb, 0, sizeof(bcb));
229 return CMD_RET_SUCCESS;
230 }
231
232 if (bcb_field_get(argv[1], &field, &size))
233 return CMD_RET_FAILURE;
234
235 memset(field, 0, size);
236
237 return CMD_RET_SUCCESS;
238}
239
Simon Glass09140112020-05-10 11:40:03 -0600240static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
241 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200242{
243 int size;
244 char *field;
245 char *op = argv[2];
246
247 if (bcb_field_get(argv[1], &field, &size))
248 return CMD_RET_FAILURE;
249
250 if (*op == '=' && *(op + 1) == '\0') {
251 if (!strncmp(argv[3], field, size))
252 return CMD_RET_SUCCESS;
253 else
254 return CMD_RET_FAILURE;
255 } else if (*op == '~' && *(op + 1) == '\0') {
256 if (!strstr(field, argv[3]))
257 return CMD_RET_FAILURE;
258 else
259 return CMD_RET_SUCCESS;
260 } else {
261 printf("Error: Unknown operator '%s'\n", op);
262 }
263
264 return CMD_RET_FAILURE;
265}
266
Simon Glass09140112020-05-10 11:40:03 -0600267static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
268 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200269{
270 int size;
271 char *field;
272
273 if (bcb_field_get(argv[1], &field, &size))
274 return CMD_RET_FAILURE;
275
276 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
277
278 return CMD_RET_SUCCESS;
279}
280
Eugeniu Roscae2ffb3b2020-10-23 11:52:21 +0300281static int __bcb_store(void)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200282{
283 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600284 struct disk_partition info;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200285 u64 cnt;
286 int ret;
287
288 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
289 if (!desc) {
290 ret = -ENODEV;
291 goto err;
292 }
293
294 ret = part_get_info(desc, bcb_part, &info);
295 if (ret)
296 goto err;
297
298 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
299
300 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
301 ret = -EIO;
302 goto err;
303 }
304
305 return CMD_RET_SUCCESS;
306err:
307 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
308
309 return CMD_RET_FAILURE;
310}
311
Eugeniu Roscae2ffb3b2020-10-23 11:52:21 +0300312static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
313 char * const argv[])
314{
315 return __bcb_store();
316}
317
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300318int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
Eugeniu Roscae74670a2020-10-23 11:52:22 +0300319{
320 int ret;
321
322 ret = __bcb_load(devnum, partp);
323 if (ret != CMD_RET_SUCCESS)
324 return ret;
325
326 ret = __bcb_set("command", reasonp);
327 if (ret != CMD_RET_SUCCESS)
328 return ret;
329
330 ret = __bcb_store();
331 if (ret != CMD_RET_SUCCESS)
332 return ret;
333
334 return 0;
335}
336
Simon Glass09140112020-05-10 11:40:03 -0600337static struct cmd_tbl cmd_bcb_sub[] = {
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200338 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
339 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
340 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
341 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
342 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
343 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
344};
345
Simon Glass09140112020-05-10 11:40:03 -0600346static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200347{
Simon Glass09140112020-05-10 11:40:03 -0600348 struct cmd_tbl *c;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200349
350 if (argc < 2)
351 return CMD_RET_USAGE;
352
353 argc--;
354 argv++;
355
356 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
357 if (!c)
358 return CMD_RET_USAGE;
359
360 if (bcb_is_misused(argc, argv)) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200361 /*
362 * We try to improve the user experience by reporting the
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200363 * root-cause of misusage, so don't return CMD_RET_USAGE,
364 * since the latter prints out the full-blown help text
365 */
366 return CMD_RET_FAILURE;
367 }
368
369 return c->cmd(cmdtp, flag, argc, argv);
370}
371
372U_BOOT_CMD(
373 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
374 "Load/set/clear/test/dump/store Android BCB fields",
375 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
376 "bcb set <field> <val> - set BCB <field> to <val>\n"
377 "bcb clear [<field>] - clear BCB <field> or all fields\n"
378 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
379 "bcb dump <field> - dump BCB <field>\n"
380 "bcb store - store BCB back to mmc\n"
381 "\n"
382 "Legend:\n"
383 "<dev> - MMC device index containing the BCB partition\n"
384 "<part> - MMC partition index or name containing the BCB\n"
385 "<field> - one of {command,status,recovery,stage,reserved}\n"
386 "<op> - the binary operator used in 'bcb test':\n"
387 " '=' returns true if <val> matches the string stored in <field>\n"
388 " '~' returns true if <val> matches a subset of <field>'s string\n"
389 "<val> - string/text provided as input to bcb {set,test}\n"
390 " NOTE: any ':' character in <val> will be replaced by line feed\n"
391 " during 'bcb set' and used as separator by upper layers\n"
392);