blob: 1622a90c97f0354963d4ab9141e82e85207710a4 [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 Glass4e4bf942022-07-31 12:28:48 -060012#include <display_options.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060014#include <part.h>
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +030015#include <malloc.h>
Gary Bisson18956cd2022-01-11 18:06:06 +010016#include <memalign.h>
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020017
18enum bcb_cmd {
19 BCB_CMD_LOAD,
20 BCB_CMD_FIELD_SET,
21 BCB_CMD_FIELD_CLEAR,
22 BCB_CMD_FIELD_TEST,
23 BCB_CMD_FIELD_DUMP,
24 BCB_CMD_STORE,
25};
26
27static int bcb_dev = -1;
28static int bcb_part = -1;
Gary Bisson18956cd2022-01-11 18:06:06 +010029static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020030
31static int bcb_cmd_get(char *cmd)
32{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020033 if (!strcmp(cmd, "load"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020034 return BCB_CMD_LOAD;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020035 if (!strcmp(cmd, "set"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020036 return BCB_CMD_FIELD_SET;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020037 if (!strcmp(cmd, "clear"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020038 return BCB_CMD_FIELD_CLEAR;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020039 if (!strcmp(cmd, "test"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020040 return BCB_CMD_FIELD_TEST;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020041 if (!strcmp(cmd, "store"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020042 return BCB_CMD_STORE;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020043 if (!strcmp(cmd, "dump"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020044 return BCB_CMD_FIELD_DUMP;
45 else
46 return -1;
47}
48
49static int bcb_is_misused(int argc, char *const argv[])
50{
51 int cmd = bcb_cmd_get(argv[0]);
52
53 switch (cmd) {
54 case BCB_CMD_LOAD:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020055 case BCB_CMD_FIELD_SET:
56 if (argc != 3)
57 goto err;
58 break;
59 case BCB_CMD_FIELD_TEST:
60 if (argc != 4)
61 goto err;
62 break;
63 case BCB_CMD_FIELD_CLEAR:
64 if (argc != 1 && argc != 2)
65 goto err;
66 break;
67 case BCB_CMD_STORE:
68 if (argc != 1)
69 goto err;
70 break;
71 case BCB_CMD_FIELD_DUMP:
72 if (argc != 2)
73 goto err;
74 break;
75 default:
76 printf("Error: 'bcb %s' not supported\n", argv[0]);
77 return -1;
78 }
79
80 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
81 printf("Error: Please, load BCB first!\n");
82 return -1;
83 }
84
85 return 0;
86err:
87 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
88
89 return -1;
90}
91
Eugeniu Rosca37f18112019-07-19 23:26:14 +020092static int bcb_field_get(char *name, char **fieldp, int *sizep)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020093{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020094 if (!strcmp(name, "command")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020095 *fieldp = bcb.command;
96 *sizep = sizeof(bcb.command);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020097 } else if (!strcmp(name, "status")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020098 *fieldp = bcb.status;
99 *sizep = sizeof(bcb.status);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200100 } else if (!strcmp(name, "recovery")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200101 *fieldp = bcb.recovery;
102 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200103 } else if (!strcmp(name, "stage")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200104 *fieldp = bcb.stage;
105 *sizep = sizeof(bcb.stage);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200106 } else if (!strcmp(name, "reserved")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200107 *fieldp = bcb.reserved;
108 *sizep = sizeof(bcb.reserved);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200109 } else {
110 printf("Error: Unknown bcb field '%s'\n", name);
111 return -1;
112 }
113
114 return 0;
115}
116
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300117static int __bcb_load(int devnum, const char *partp)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200118{
119 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600120 struct disk_partition info;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200121 u64 cnt;
122 char *endp;
123 int part, ret;
124
Simon Glass8149b152022-09-17 09:00:09 -0600125 desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, devnum);
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300126 if (!desc) {
127 ret = -ENODEV;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200128 goto err_read_fail;
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300129 }
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200130
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300131 part = simple_strtoul(partp, &endp, 0);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200132 if (*endp == '\0') {
133 ret = part_get_info(desc, part, &info);
134 if (ret)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200135 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200136 } else {
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300137 part = part_get_info_by_name(desc, partp, &info);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200138 if (part < 0) {
139 ret = part;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200140 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200141 }
142 }
143
144 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
145 if (cnt > info.size)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200146 goto err_too_small;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200147
148 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
149 ret = -EIO;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200150 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200151 }
152
153 bcb_dev = desc->devnum;
154 bcb_part = part;
155 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
156
157 return CMD_RET_SUCCESS;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200158err_read_fail:
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300159 printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200160 goto err;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200161err_too_small:
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300162 printf("Error: mmc %d:%s too small!", devnum, partp);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200163 goto err;
164err:
165 bcb_dev = -1;
166 bcb_part = -1;
167
168 return CMD_RET_FAILURE;
169}
170
Eugeniu Rosca24e85bb2020-10-23 11:52:19 +0300171static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
172 char * const argv[])
173{
174 char *endp;
175 int devnum = simple_strtoul(argv[1], &endp, 0);
176
177 if (*endp != '\0') {
178 printf("Error: Device id '%s' not a number\n", argv[1]);
179 return CMD_RET_FAILURE;
180 }
181
182 return __bcb_load(devnum, argv[2]);
183}
184
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300185static int __bcb_set(char *fieldp, const char *valp)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200186{
187 int size, len;
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300188 char *field, *str, *found, *tmp;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200189
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300190 if (bcb_field_get(fieldp, &field, &size))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200191 return CMD_RET_FAILURE;
192
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300193 len = strlen(valp);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200194 if (len >= size) {
195 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300196 valp, len, size, fieldp);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200197 return CMD_RET_FAILURE;
198 }
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300199 str = strdup(valp);
200 if (!str) {
201 printf("Error: Out of memory while strdup\n");
202 return CMD_RET_FAILURE;
203 }
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200204
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300205 tmp = str;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200206 field[0] = '\0';
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300207 while ((found = strsep(&tmp, ":"))) {
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200208 if (field[0] != '\0')
209 strcat(field, "\n");
210 strcat(field, found);
211 }
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300212 free(str);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200213
214 return CMD_RET_SUCCESS;
215}
216
Eugeniu Rosca02ebe6d2020-10-23 11:52:20 +0300217static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
218 char * const argv[])
219{
220 return __bcb_set(argv[1], argv[2]);
221}
222
Simon Glass09140112020-05-10 11:40:03 -0600223static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
224 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200225{
226 int size;
227 char *field;
228
229 if (argc == 1) {
230 memset(&bcb, 0, sizeof(bcb));
231 return CMD_RET_SUCCESS;
232 }
233
234 if (bcb_field_get(argv[1], &field, &size))
235 return CMD_RET_FAILURE;
236
237 memset(field, 0, size);
238
239 return CMD_RET_SUCCESS;
240}
241
Simon Glass09140112020-05-10 11:40:03 -0600242static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
243 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200244{
245 int size;
246 char *field;
247 char *op = argv[2];
248
249 if (bcb_field_get(argv[1], &field, &size))
250 return CMD_RET_FAILURE;
251
252 if (*op == '=' && *(op + 1) == '\0') {
253 if (!strncmp(argv[3], field, size))
254 return CMD_RET_SUCCESS;
255 else
256 return CMD_RET_FAILURE;
257 } else if (*op == '~' && *(op + 1) == '\0') {
258 if (!strstr(field, argv[3]))
259 return CMD_RET_FAILURE;
260 else
261 return CMD_RET_SUCCESS;
262 } else {
263 printf("Error: Unknown operator '%s'\n", op);
264 }
265
266 return CMD_RET_FAILURE;
267}
268
Simon Glass09140112020-05-10 11:40:03 -0600269static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
270 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200271{
272 int size;
273 char *field;
274
275 if (bcb_field_get(argv[1], &field, &size))
276 return CMD_RET_FAILURE;
277
278 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
279
280 return CMD_RET_SUCCESS;
281}
282
Eugeniu Roscae2ffb3b2020-10-23 11:52:21 +0300283static int __bcb_store(void)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200284{
285 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600286 struct disk_partition info;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200287 u64 cnt;
288 int ret;
289
Simon Glass8149b152022-09-17 09:00:09 -0600290 desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, bcb_dev);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200291 if (!desc) {
292 ret = -ENODEV;
293 goto err;
294 }
295
296 ret = part_get_info(desc, bcb_part, &info);
297 if (ret)
298 goto err;
299
300 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
301
302 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
303 ret = -EIO;
304 goto err;
305 }
306
307 return CMD_RET_SUCCESS;
308err:
309 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
310
311 return CMD_RET_FAILURE;
312}
313
Eugeniu Roscae2ffb3b2020-10-23 11:52:21 +0300314static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
315 char * const argv[])
316{
317 return __bcb_store();
318}
319
Eugeniu Roscabafdf4c2020-10-23 11:52:23 +0300320int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
Eugeniu Roscae74670a2020-10-23 11:52:22 +0300321{
322 int ret;
323
324 ret = __bcb_load(devnum, partp);
325 if (ret != CMD_RET_SUCCESS)
326 return ret;
327
328 ret = __bcb_set("command", reasonp);
329 if (ret != CMD_RET_SUCCESS)
330 return ret;
331
332 ret = __bcb_store();
333 if (ret != CMD_RET_SUCCESS)
334 return ret;
335
336 return 0;
337}
338
Simon Glass09140112020-05-10 11:40:03 -0600339static struct cmd_tbl cmd_bcb_sub[] = {
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200340 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
341 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
342 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
343 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
344 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
345 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
346};
347
Simon Glass09140112020-05-10 11:40:03 -0600348static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200349{
Simon Glass09140112020-05-10 11:40:03 -0600350 struct cmd_tbl *c;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200351
352 if (argc < 2)
353 return CMD_RET_USAGE;
354
355 argc--;
356 argv++;
357
358 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
359 if (!c)
360 return CMD_RET_USAGE;
361
362 if (bcb_is_misused(argc, argv)) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200363 /*
364 * We try to improve the user experience by reporting the
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200365 * root-cause of misusage, so don't return CMD_RET_USAGE,
366 * since the latter prints out the full-blown help text
367 */
368 return CMD_RET_FAILURE;
369 }
370
371 return c->cmd(cmdtp, flag, argc, argv);
372}
373
374U_BOOT_CMD(
375 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
376 "Load/set/clear/test/dump/store Android BCB fields",
377 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
378 "bcb set <field> <val> - set BCB <field> to <val>\n"
379 "bcb clear [<field>] - clear BCB <field> or all fields\n"
380 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
381 "bcb dump <field> - dump BCB <field>\n"
382 "bcb store - store BCB back to mmc\n"
383 "\n"
384 "Legend:\n"
385 "<dev> - MMC device index containing the BCB partition\n"
386 "<part> - MMC partition index or name containing the BCB\n"
387 "<field> - one of {command,status,recovery,stage,reserved}\n"
388 "<op> - the binary operator used in 'bcb test':\n"
389 " '=' returns true if <val> matches the string stored in <field>\n"
390 " '~' returns true if <val> matches a subset of <field>'s string\n"
391 "<val> - string/text provided as input to bcb {set,test}\n"
392 " NOTE: any ':' character in <val> will be replaced by line feed\n"
393 " during 'bcb set' and used as separator by upper layers\n"
394);