blob: 02d0c70d87e24a4e99daaa5691b25c71bae32e1d [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 Glass1ab16922022-07-31 12:28:48 -060012#include <display_options.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <part.h>
Eugeniu Rosca6005e932020-10-23 11:52:23 +030015#include <malloc.h>
Gary Bissonf3f2c7b2022-01-11 18:06:06 +010016#include <memalign.h>
Mattijs Korpershoekcb254102022-10-13 14:25:59 +020017#include <linux/err.h>
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020018
19enum bcb_cmd {
20 BCB_CMD_LOAD,
21 BCB_CMD_FIELD_SET,
22 BCB_CMD_FIELD_CLEAR,
23 BCB_CMD_FIELD_TEST,
24 BCB_CMD_FIELD_DUMP,
25 BCB_CMD_STORE,
26};
27
28static int bcb_dev = -1;
29static int bcb_part = -1;
Gary Bissonf3f2c7b2022-01-11 18:06:06 +010030static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020031
32static int bcb_cmd_get(char *cmd)
33{
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020034 if (!strcmp(cmd, "load"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020035 return BCB_CMD_LOAD;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020036 if (!strcmp(cmd, "set"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020037 return BCB_CMD_FIELD_SET;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020038 if (!strcmp(cmd, "clear"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020039 return BCB_CMD_FIELD_CLEAR;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020040 if (!strcmp(cmd, "test"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020041 return BCB_CMD_FIELD_TEST;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020042 if (!strcmp(cmd, "store"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020043 return BCB_CMD_STORE;
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020044 if (!strcmp(cmd, "dump"))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020045 return BCB_CMD_FIELD_DUMP;
46 else
47 return -1;
48}
49
50static int bcb_is_misused(int argc, char *const argv[])
51{
52 int cmd = bcb_cmd_get(argv[0]);
53
54 switch (cmd) {
55 case BCB_CMD_LOAD:
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020056 case BCB_CMD_FIELD_SET:
57 if (argc != 3)
58 goto err;
59 break;
60 case BCB_CMD_FIELD_TEST:
61 if (argc != 4)
62 goto err;
63 break;
64 case BCB_CMD_FIELD_CLEAR:
65 if (argc != 1 && argc != 2)
66 goto err;
67 break;
68 case BCB_CMD_STORE:
69 if (argc != 1)
70 goto err;
71 break;
72 case BCB_CMD_FIELD_DUMP:
73 if (argc != 2)
74 goto err;
75 break;
76 default:
77 printf("Error: 'bcb %s' not supported\n", argv[0]);
78 return -1;
79 }
80
81 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
82 printf("Error: Please, load BCB first!\n");
83 return -1;
84 }
85
86 return 0;
87err:
88 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
89
90 return -1;
91}
92
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020093static int bcb_field_get(char *name, char **fieldp, int *sizep)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020094{
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020095 if (!strcmp(name, "command")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020096 *fieldp = bcb.command;
97 *sizep = sizeof(bcb.command);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020098 } else if (!strcmp(name, "status")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020099 *fieldp = bcb.status;
100 *sizep = sizeof(bcb.status);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +0200101 } else if (!strcmp(name, "recovery")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200102 *fieldp = bcb.recovery;
103 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +0200104 } else if (!strcmp(name, "stage")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200105 *fieldp = bcb.stage;
106 *sizep = sizeof(bcb.stage);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +0200107 } else if (!strcmp(name, "reserved")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200108 *fieldp = bcb.reserved;
109 *sizep = sizeof(bcb.reserved);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200110 } else {
111 printf("Error: Unknown bcb field '%s'\n", name);
112 return -1;
113 }
114
115 return 0;
116}
117
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300118static int __bcb_load(int devnum, const char *partp)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200119{
120 struct blk_desc *desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600121 struct disk_partition info;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200122 u64 cnt;
123 char *endp;
124 int part, ret;
125
Simon Glassfada3f92022-09-17 09:00:09 -0600126 desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, devnum);
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300127 if (!desc) {
128 ret = -ENODEV;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200129 goto err_read_fail;
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300130 }
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200131
Mattijs Korpershoekcb254102022-10-13 14:25:59 +0200132 /*
133 * always select the USER mmc hwpart in case another
134 * blk operation selected a different hwpart
135 */
136 ret = blk_dselect_hwpart(desc, 0);
137 if (IS_ERR_VALUE(ret)) {
138 ret = -ENODEV;
139 goto err_read_fail;
140 }
141
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300142 part = simple_strtoul(partp, &endp, 0);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200143 if (*endp == '\0') {
144 ret = part_get_info(desc, part, &info);
145 if (ret)
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200146 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200147 } else {
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300148 part = part_get_info_by_name(desc, partp, &info);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200149 if (part < 0) {
150 ret = part;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200151 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200152 }
153 }
154
155 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
156 if (cnt > info.size)
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200157 goto err_too_small;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200158
159 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
160 ret = -EIO;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200161 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200162 }
163
164 bcb_dev = desc->devnum;
165 bcb_part = part;
166 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
167
168 return CMD_RET_SUCCESS;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200169err_read_fail:
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300170 printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200171 goto err;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200172err_too_small:
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300173 printf("Error: mmc %d:%s too small!", devnum, partp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200174 goto err;
175err:
176 bcb_dev = -1;
177 bcb_part = -1;
178
179 return CMD_RET_FAILURE;
180}
181
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300182static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
183 char * const argv[])
184{
185 char *endp;
186 int devnum = simple_strtoul(argv[1], &endp, 0);
187
188 if (*endp != '\0') {
189 printf("Error: Device id '%s' not a number\n", argv[1]);
190 return CMD_RET_FAILURE;
191 }
192
193 return __bcb_load(devnum, argv[2]);
194}
195
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300196static int __bcb_set(char *fieldp, const char *valp)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200197{
198 int size, len;
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300199 char *field, *str, *found, *tmp;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200200
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300201 if (bcb_field_get(fieldp, &field, &size))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200202 return CMD_RET_FAILURE;
203
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300204 len = strlen(valp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200205 if (len >= size) {
206 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300207 valp, len, size, fieldp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200208 return CMD_RET_FAILURE;
209 }
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300210 str = strdup(valp);
211 if (!str) {
212 printf("Error: Out of memory while strdup\n");
213 return CMD_RET_FAILURE;
214 }
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200215
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300216 tmp = str;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200217 field[0] = '\0';
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300218 while ((found = strsep(&tmp, ":"))) {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200219 if (field[0] != '\0')
220 strcat(field, "\n");
221 strcat(field, found);
222 }
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300223 free(str);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200224
225 return CMD_RET_SUCCESS;
226}
227
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300228static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
229 char * const argv[])
230{
231 return __bcb_set(argv[1], argv[2]);
232}
233
Simon Glassed38aef2020-05-10 11:40:03 -0600234static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
235 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200236{
237 int size;
238 char *field;
239
240 if (argc == 1) {
241 memset(&bcb, 0, sizeof(bcb));
242 return CMD_RET_SUCCESS;
243 }
244
245 if (bcb_field_get(argv[1], &field, &size))
246 return CMD_RET_FAILURE;
247
248 memset(field, 0, size);
249
250 return CMD_RET_SUCCESS;
251}
252
Simon Glassed38aef2020-05-10 11:40:03 -0600253static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
254 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200255{
256 int size;
257 char *field;
258 char *op = argv[2];
259
260 if (bcb_field_get(argv[1], &field, &size))
261 return CMD_RET_FAILURE;
262
263 if (*op == '=' && *(op + 1) == '\0') {
264 if (!strncmp(argv[3], field, size))
265 return CMD_RET_SUCCESS;
266 else
267 return CMD_RET_FAILURE;
268 } else if (*op == '~' && *(op + 1) == '\0') {
269 if (!strstr(field, argv[3]))
270 return CMD_RET_FAILURE;
271 else
272 return CMD_RET_SUCCESS;
273 } else {
274 printf("Error: Unknown operator '%s'\n", op);
275 }
276
277 return CMD_RET_FAILURE;
278}
279
Simon Glassed38aef2020-05-10 11:40:03 -0600280static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
281 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200282{
283 int size;
284 char *field;
285
286 if (bcb_field_get(argv[1], &field, &size))
287 return CMD_RET_FAILURE;
288
289 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
290
291 return CMD_RET_SUCCESS;
292}
293
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300294static int __bcb_store(void)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200295{
296 struct blk_desc *desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600297 struct disk_partition info;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200298 u64 cnt;
299 int ret;
300
Simon Glassfada3f92022-09-17 09:00:09 -0600301 desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, bcb_dev);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200302 if (!desc) {
303 ret = -ENODEV;
304 goto err;
305 }
306
307 ret = part_get_info(desc, bcb_part, &info);
308 if (ret)
309 goto err;
310
311 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
312
313 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
314 ret = -EIO;
315 goto err;
316 }
317
318 return CMD_RET_SUCCESS;
319err:
320 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
321
322 return CMD_RET_FAILURE;
323}
324
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300325static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
326 char * const argv[])
327{
328 return __bcb_store();
329}
330
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300331int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300332{
333 int ret;
334
335 ret = __bcb_load(devnum, partp);
336 if (ret != CMD_RET_SUCCESS)
337 return ret;
338
339 ret = __bcb_set("command", reasonp);
340 if (ret != CMD_RET_SUCCESS)
341 return ret;
342
343 ret = __bcb_store();
344 if (ret != CMD_RET_SUCCESS)
345 return ret;
346
347 return 0;
348}
349
Simon Glassed38aef2020-05-10 11:40:03 -0600350static struct cmd_tbl cmd_bcb_sub[] = {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200351 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
352 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
353 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
354 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
355 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
356 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
357};
358
Simon Glassed38aef2020-05-10 11:40:03 -0600359static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200360{
Simon Glassed38aef2020-05-10 11:40:03 -0600361 struct cmd_tbl *c;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200362
363 if (argc < 2)
364 return CMD_RET_USAGE;
365
366 argc--;
367 argv++;
368
369 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
370 if (!c)
371 return CMD_RET_USAGE;
372
373 if (bcb_is_misused(argc, argv)) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200374 /*
375 * We try to improve the user experience by reporting the
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200376 * root-cause of misusage, so don't return CMD_RET_USAGE,
377 * since the latter prints out the full-blown help text
378 */
379 return CMD_RET_FAILURE;
380 }
381
382 return c->cmd(cmdtp, flag, argc, argv);
383}
384
385U_BOOT_CMD(
386 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
387 "Load/set/clear/test/dump/store Android BCB fields",
388 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
389 "bcb set <field> <val> - set BCB <field> to <val>\n"
390 "bcb clear [<field>] - clear BCB <field> or all fields\n"
391 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
392 "bcb dump <field> - dump BCB <field>\n"
393 "bcb store - store BCB back to mmc\n"
394 "\n"
395 "Legend:\n"
396 "<dev> - MMC device index containing the BCB partition\n"
397 "<part> - MMC partition index or name containing the BCB\n"
398 "<field> - one of {command,status,recovery,stage,reserved}\n"
399 "<op> - the binary operator used in 'bcb test':\n"
400 " '=' returns true if <val> matches the string stored in <field>\n"
401 " '~' returns true if <val> matches a subset of <field>'s string\n"
402 "<val> - string/text provided as input to bcb {set,test}\n"
403 " NOTE: any ':' character in <val> will be replaced by line feed\n"
404 " during 'bcb set' and used as separator by upper layers\n"
405);