blob: 92f4d27990df981e66d766dd4c4b4dd7c2f56a03 [file] [log] [blame]
Eugeniu Rosca35c34be2019-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 Rosca54a55482020-10-23 11:52:22 +03009#include <bcb.h>
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020010#include <command.h>
11#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Eugeniu Rosca6005e932020-10-23 11:52:23 +030014#include <malloc.h>
Gary Bissonf3f2c7b2022-01-11 18:06:06 +010015#include <memalign.h>
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020016
17enum bcb_cmd {
18 BCB_CMD_LOAD,
19 BCB_CMD_FIELD_SET,
20 BCB_CMD_FIELD_CLEAR,
21 BCB_CMD_FIELD_TEST,
22 BCB_CMD_FIELD_DUMP,
23 BCB_CMD_STORE,
24};
25
26static int bcb_dev = -1;
27static int bcb_part = -1;
Gary Bissonf3f2c7b2022-01-11 18:06:06 +010028static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020029
30static int bcb_cmd_get(char *cmd)
31{
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020032 if (!strcmp(cmd, "load"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020033 return BCB_CMD_LOAD;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020034 if (!strcmp(cmd, "set"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020035 return BCB_CMD_FIELD_SET;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020036 if (!strcmp(cmd, "clear"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020037 return BCB_CMD_FIELD_CLEAR;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020038 if (!strcmp(cmd, "test"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020039 return BCB_CMD_FIELD_TEST;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020040 if (!strcmp(cmd, "store"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020041 return BCB_CMD_STORE;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020042 if (!strcmp(cmd, "dump"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020043 return BCB_CMD_FIELD_DUMP;
44 else
45 return -1;
46}
47
48static int bcb_is_misused(int argc, char *const argv[])
49{
50 int cmd = bcb_cmd_get(argv[0]);
51
52 switch (cmd) {
53 case BCB_CMD_LOAD:
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020054 case BCB_CMD_FIELD_SET:
55 if (argc != 3)
56 goto err;
57 break;
58 case BCB_CMD_FIELD_TEST:
59 if (argc != 4)
60 goto err;
61 break;
62 case BCB_CMD_FIELD_CLEAR:
63 if (argc != 1 && argc != 2)
64 goto err;
65 break;
66 case BCB_CMD_STORE:
67 if (argc != 1)
68 goto err;
69 break;
70 case BCB_CMD_FIELD_DUMP:
71 if (argc != 2)
72 goto err;
73 break;
74 default:
75 printf("Error: 'bcb %s' not supported\n", argv[0]);
76 return -1;
77 }
78
79 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
80 printf("Error: Please, load BCB first!\n");
81 return -1;
82 }
83
84 return 0;
85err:
86 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
87
88 return -1;
89}
90
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020091static int bcb_field_get(char *name, char **fieldp, int *sizep)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020092{
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020093 if (!strcmp(name, "command")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020094 *fieldp = bcb.command;
95 *sizep = sizeof(bcb.command);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020096 } else if (!strcmp(name, "status")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020097 *fieldp = bcb.status;
98 *sizep = sizeof(bcb.status);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020099 } else if (!strcmp(name, "recovery")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200100 *fieldp = bcb.recovery;
101 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +0200102 } else if (!strcmp(name, "stage")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200103 *fieldp = bcb.stage;
104 *sizep = sizeof(bcb.stage);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +0200105 } else if (!strcmp(name, "reserved")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200106 *fieldp = bcb.reserved;
107 *sizep = sizeof(bcb.reserved);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200108 } else {
109 printf("Error: Unknown bcb field '%s'\n", name);
110 return -1;
111 }
112
113 return 0;
114}
115
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300116static int __bcb_load(int devnum, const char *partp)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200117{
118 struct blk_desc *desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600119 struct disk_partition info;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200120 u64 cnt;
121 char *endp;
122 int part, ret;
123
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300124 desc = blk_get_devnum_by_type(IF_TYPE_MMC, devnum);
125 if (!desc) {
126 ret = -ENODEV;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200127 goto err_read_fail;
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300128 }
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200129
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300130 part = simple_strtoul(partp, &endp, 0);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200131 if (*endp == '\0') {
132 ret = part_get_info(desc, part, &info);
133 if (ret)
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200134 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200135 } else {
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300136 part = part_get_info_by_name(desc, partp, &info);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200137 if (part < 0) {
138 ret = part;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200139 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200140 }
141 }
142
143 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
144 if (cnt > info.size)
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200145 goto err_too_small;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200146
147 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
148 ret = -EIO;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200149 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200150 }
151
152 bcb_dev = desc->devnum;
153 bcb_part = part;
154 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
155
156 return CMD_RET_SUCCESS;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200157err_read_fail:
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300158 printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200159 goto err;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200160err_too_small:
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300161 printf("Error: mmc %d:%s too small!", devnum, partp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200162 goto err;
163err:
164 bcb_dev = -1;
165 bcb_part = -1;
166
167 return CMD_RET_FAILURE;
168}
169
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300170static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
171 char * const argv[])
172{
173 char *endp;
174 int devnum = simple_strtoul(argv[1], &endp, 0);
175
176 if (*endp != '\0') {
177 printf("Error: Device id '%s' not a number\n", argv[1]);
178 return CMD_RET_FAILURE;
179 }
180
181 return __bcb_load(devnum, argv[2]);
182}
183
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300184static int __bcb_set(char *fieldp, const char *valp)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200185{
186 int size, len;
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300187 char *field, *str, *found, *tmp;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200188
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300189 if (bcb_field_get(fieldp, &field, &size))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200190 return CMD_RET_FAILURE;
191
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300192 len = strlen(valp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200193 if (len >= size) {
194 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300195 valp, len, size, fieldp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200196 return CMD_RET_FAILURE;
197 }
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300198 str = strdup(valp);
199 if (!str) {
200 printf("Error: Out of memory while strdup\n");
201 return CMD_RET_FAILURE;
202 }
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200203
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300204 tmp = str;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200205 field[0] = '\0';
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300206 while ((found = strsep(&tmp, ":"))) {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200207 if (field[0] != '\0')
208 strcat(field, "\n");
209 strcat(field, found);
210 }
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300211 free(str);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200212
213 return CMD_RET_SUCCESS;
214}
215
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300216static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
217 char * const argv[])
218{
219 return __bcb_set(argv[1], argv[2]);
220}
221
Simon Glassed38aef2020-05-10 11:40:03 -0600222static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
223 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200224{
225 int size;
226 char *field;
227
228 if (argc == 1) {
229 memset(&bcb, 0, sizeof(bcb));
230 return CMD_RET_SUCCESS;
231 }
232
233 if (bcb_field_get(argv[1], &field, &size))
234 return CMD_RET_FAILURE;
235
236 memset(field, 0, size);
237
238 return CMD_RET_SUCCESS;
239}
240
Simon Glassed38aef2020-05-10 11:40:03 -0600241static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
242 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200243{
244 int size;
245 char *field;
246 char *op = argv[2];
247
248 if (bcb_field_get(argv[1], &field, &size))
249 return CMD_RET_FAILURE;
250
251 if (*op == '=' && *(op + 1) == '\0') {
252 if (!strncmp(argv[3], field, size))
253 return CMD_RET_SUCCESS;
254 else
255 return CMD_RET_FAILURE;
256 } else if (*op == '~' && *(op + 1) == '\0') {
257 if (!strstr(field, argv[3]))
258 return CMD_RET_FAILURE;
259 else
260 return CMD_RET_SUCCESS;
261 } else {
262 printf("Error: Unknown operator '%s'\n", op);
263 }
264
265 return CMD_RET_FAILURE;
266}
267
Simon Glassed38aef2020-05-10 11:40:03 -0600268static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
269 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200270{
271 int size;
272 char *field;
273
274 if (bcb_field_get(argv[1], &field, &size))
275 return CMD_RET_FAILURE;
276
277 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
278
279 return CMD_RET_SUCCESS;
280}
281
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300282static int __bcb_store(void)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200283{
284 struct blk_desc *desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600285 struct disk_partition info;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200286 u64 cnt;
287 int ret;
288
289 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
290 if (!desc) {
291 ret = -ENODEV;
292 goto err;
293 }
294
295 ret = part_get_info(desc, bcb_part, &info);
296 if (ret)
297 goto err;
298
299 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
300
301 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
302 ret = -EIO;
303 goto err;
304 }
305
306 return CMD_RET_SUCCESS;
307err:
308 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
309
310 return CMD_RET_FAILURE;
311}
312
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300313static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
314 char * const argv[])
315{
316 return __bcb_store();
317}
318
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300319int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300320{
321 int ret;
322
323 ret = __bcb_load(devnum, partp);
324 if (ret != CMD_RET_SUCCESS)
325 return ret;
326
327 ret = __bcb_set("command", reasonp);
328 if (ret != CMD_RET_SUCCESS)
329 return ret;
330
331 ret = __bcb_store();
332 if (ret != CMD_RET_SUCCESS)
333 return ret;
334
335 return 0;
336}
337
Simon Glassed38aef2020-05-10 11:40:03 -0600338static struct cmd_tbl cmd_bcb_sub[] = {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200339 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
340 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
341 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
342 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
343 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
344 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
345};
346
Simon Glassed38aef2020-05-10 11:40:03 -0600347static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200348{
Simon Glassed38aef2020-05-10 11:40:03 -0600349 struct cmd_tbl *c;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200350
351 if (argc < 2)
352 return CMD_RET_USAGE;
353
354 argc--;
355 argv++;
356
357 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
358 if (!c)
359 return CMD_RET_USAGE;
360
361 if (bcb_is_misused(argc, argv)) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200362 /*
363 * We try to improve the user experience by reporting the
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200364 * root-cause of misusage, so don't return CMD_RET_USAGE,
365 * since the latter prints out the full-blown help text
366 */
367 return CMD_RET_FAILURE;
368 }
369
370 return c->cmd(cmdtp, flag, argc, argv);
371}
372
373U_BOOT_CMD(
374 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
375 "Load/set/clear/test/dump/store Android BCB fields",
376 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
377 "bcb set <field> <val> - set BCB <field> to <val>\n"
378 "bcb clear [<field>] - clear BCB <field> or all fields\n"
379 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
380 "bcb dump <field> - dump BCB <field>\n"
381 "bcb store - store BCB back to mmc\n"
382 "\n"
383 "Legend:\n"
384 "<dev> - MMC device index containing the BCB partition\n"
385 "<part> - MMC partition index or name containing the BCB\n"
386 "<field> - one of {command,status,recovery,stage,reserved}\n"
387 "<op> - the binary operator used in 'bcb test':\n"
388 " '=' returns true if <val> matches the string stored in <field>\n"
389 " '~' returns true if <val> matches a subset of <field>'s string\n"
390 "<val> - string/text provided as input to bcb {set,test}\n"
391 " NOTE: any ':' character in <val> will be replaced by line feed\n"
392 " during 'bcb set' and used as separator by upper layers\n"
393);