1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Eugeniu Rosca <rosca.eugeniu@gmail.com>
5 * Command to read/modify/write Android BCB fields
8 #include <android_bootloader_message.h>
12 #include <display_options.h>
17 #include <linux/err.h>
28 static int bcb_dev = -1;
29 static int bcb_part = -1;
30 static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
32 static int bcb_cmd_get(char *cmd)
34 if (!strcmp(cmd, "load"))
36 if (!strcmp(cmd, "set"))
37 return BCB_CMD_FIELD_SET;
38 if (!strcmp(cmd, "clear"))
39 return BCB_CMD_FIELD_CLEAR;
40 if (!strcmp(cmd, "test"))
41 return BCB_CMD_FIELD_TEST;
42 if (!strcmp(cmd, "store"))
44 if (!strcmp(cmd, "dump"))
45 return BCB_CMD_FIELD_DUMP;
50 static int bcb_is_misused(int argc, char *const argv[])
52 int cmd = bcb_cmd_get(argv[0]);
56 case BCB_CMD_FIELD_SET:
60 case BCB_CMD_FIELD_TEST:
64 case BCB_CMD_FIELD_CLEAR:
65 if (argc != 1 && argc != 2)
72 case BCB_CMD_FIELD_DUMP:
77 printf("Error: 'bcb %s' not supported\n", argv[0]);
81 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
82 printf("Error: Please, load BCB first!\n");
88 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
93 static int bcb_field_get(char *name, char **fieldp, int *sizep)
95 if (!strcmp(name, "command")) {
96 *fieldp = bcb.command;
97 *sizep = sizeof(bcb.command);
98 } else if (!strcmp(name, "status")) {
100 *sizep = sizeof(bcb.status);
101 } else if (!strcmp(name, "recovery")) {
102 *fieldp = bcb.recovery;
103 *sizep = sizeof(bcb.recovery);
104 } else if (!strcmp(name, "stage")) {
106 *sizep = sizeof(bcb.stage);
107 } else if (!strcmp(name, "reserved")) {
108 *fieldp = bcb.reserved;
109 *sizep = sizeof(bcb.reserved);
111 printf("Error: Unknown bcb field '%s'\n", name);
118 static int __bcb_load(int devnum, const char *partp)
120 struct blk_desc *desc;
121 struct disk_partition info;
126 desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, devnum);
133 * always select the USER mmc hwpart in case another
134 * blk operation selected a different hwpart
136 ret = blk_dselect_hwpart(desc, 0);
137 if (IS_ERR_VALUE(ret)) {
142 part = simple_strtoul(partp, &endp, 0);
144 ret = part_get_info(desc, part, &info);
148 part = part_get_info_by_name(desc, partp, &info);
155 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
159 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
164 bcb_dev = desc->devnum;
166 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
168 return CMD_RET_SUCCESS;
170 printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
173 printf("Error: mmc %d:%s too small!", devnum, partp);
179 return CMD_RET_FAILURE;
182 static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
186 int devnum = simple_strtoul(argv[1], &endp, 0);
189 printf("Error: Device id '%s' not a number\n", argv[1]);
190 return CMD_RET_FAILURE;
193 return __bcb_load(devnum, argv[2]);
196 static int __bcb_set(char *fieldp, const char *valp)
199 char *field, *str, *found, *tmp;
201 if (bcb_field_get(fieldp, &field, &size))
202 return CMD_RET_FAILURE;
206 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
207 valp, len, size, fieldp);
208 return CMD_RET_FAILURE;
212 printf("Error: Out of memory while strdup\n");
213 return CMD_RET_FAILURE;
218 while ((found = strsep(&tmp, ":"))) {
219 if (field[0] != '\0')
221 strcat(field, found);
225 return CMD_RET_SUCCESS;
228 static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
231 return __bcb_set(argv[1], argv[2]);
234 static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
241 memset(&bcb, 0, sizeof(bcb));
242 return CMD_RET_SUCCESS;
245 if (bcb_field_get(argv[1], &field, &size))
246 return CMD_RET_FAILURE;
248 memset(field, 0, size);
250 return CMD_RET_SUCCESS;
253 static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
260 if (bcb_field_get(argv[1], &field, &size))
261 return CMD_RET_FAILURE;
263 if (*op == '=' && *(op + 1) == '\0') {
264 if (!strncmp(argv[3], field, size))
265 return CMD_RET_SUCCESS;
267 return CMD_RET_FAILURE;
268 } else if (*op == '~' && *(op + 1) == '\0') {
269 if (!strstr(field, argv[3]))
270 return CMD_RET_FAILURE;
272 return CMD_RET_SUCCESS;
274 printf("Error: Unknown operator '%s'\n", op);
277 return CMD_RET_FAILURE;
280 static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
286 if (bcb_field_get(argv[1], &field, &size))
287 return CMD_RET_FAILURE;
289 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
291 return CMD_RET_SUCCESS;
294 static int __bcb_store(void)
296 struct blk_desc *desc;
297 struct disk_partition info;
301 desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, bcb_dev);
307 ret = part_get_info(desc, bcb_part, &info);
311 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
313 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
318 return CMD_RET_SUCCESS;
320 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
322 return CMD_RET_FAILURE;
325 static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
328 return __bcb_store();
331 int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
335 ret = __bcb_load(devnum, partp);
336 if (ret != CMD_RET_SUCCESS)
339 ret = __bcb_set("command", reasonp);
340 if (ret != CMD_RET_SUCCESS)
344 if (ret != CMD_RET_SUCCESS)
350 static struct cmd_tbl cmd_bcb_sub[] = {
351 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, "", ""),
359 static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
364 return CMD_RET_USAGE;
369 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
371 return CMD_RET_USAGE;
373 if (bcb_is_misused(argc, argv)) {
375 * We try to improve the user experience by reporting the
376 * root-cause of misusage, so don't return CMD_RET_USAGE,
377 * since the latter prints out the full-blown help text
379 return CMD_RET_FAILURE;
382 return c->cmd(cmdtp, flag, argc, argv);
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"
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"