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>
25 static int bcb_dev = -1;
26 static int bcb_part = -1;
27 static struct bootloader_message bcb = { { 0 } };
29 static int bcb_cmd_get(char *cmd)
31 if (!strcmp(cmd, "load"))
33 if (!strcmp(cmd, "set"))
34 return BCB_CMD_FIELD_SET;
35 if (!strcmp(cmd, "clear"))
36 return BCB_CMD_FIELD_CLEAR;
37 if (!strcmp(cmd, "test"))
38 return BCB_CMD_FIELD_TEST;
39 if (!strcmp(cmd, "store"))
41 if (!strcmp(cmd, "dump"))
42 return BCB_CMD_FIELD_DUMP;
47 static int bcb_is_misused(int argc, char *const argv[])
49 int cmd = bcb_cmd_get(argv[0]);
53 case BCB_CMD_FIELD_SET:
57 case BCB_CMD_FIELD_TEST:
61 case BCB_CMD_FIELD_CLEAR:
62 if (argc != 1 && argc != 2)
69 case BCB_CMD_FIELD_DUMP:
74 printf("Error: 'bcb %s' not supported\n", argv[0]);
78 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
79 printf("Error: Please, load BCB first!\n");
85 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
90 static int bcb_field_get(char *name, char **fieldp, int *sizep)
92 if (!strcmp(name, "command")) {
93 *fieldp = bcb.command;
94 *sizep = sizeof(bcb.command);
95 } else if (!strcmp(name, "status")) {
97 *sizep = sizeof(bcb.status);
98 } else if (!strcmp(name, "recovery")) {
99 *fieldp = bcb.recovery;
100 *sizep = sizeof(bcb.recovery);
101 } else if (!strcmp(name, "stage")) {
103 *sizep = sizeof(bcb.stage);
104 } else if (!strcmp(name, "reserved")) {
105 *fieldp = bcb.reserved;
106 *sizep = sizeof(bcb.reserved);
108 printf("Error: Unknown bcb field '%s'\n", name);
115 static int __bcb_load(int devnum, const char *partp)
117 struct blk_desc *desc;
118 struct disk_partition info;
123 desc = blk_get_devnum_by_type(IF_TYPE_MMC, devnum);
129 part = simple_strtoul(partp, &endp, 0);
131 ret = part_get_info(desc, part, &info);
135 part = part_get_info_by_name(desc, partp, &info);
142 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
146 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
151 bcb_dev = desc->devnum;
153 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
155 return CMD_RET_SUCCESS;
157 printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
160 printf("Error: mmc %d:%s too small!", devnum, partp);
166 return CMD_RET_FAILURE;
169 static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
173 int devnum = simple_strtoul(argv[1], &endp, 0);
176 printf("Error: Device id '%s' not a number\n", argv[1]);
177 return CMD_RET_FAILURE;
180 return __bcb_load(devnum, argv[2]);
183 static int __bcb_set(char *fieldp, const char *valp)
186 char *field, *str, *found, *tmp;
188 if (bcb_field_get(fieldp, &field, &size))
189 return CMD_RET_FAILURE;
193 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
194 valp, len, size, fieldp);
195 return CMD_RET_FAILURE;
199 printf("Error: Out of memory while strdup\n");
200 return CMD_RET_FAILURE;
205 while ((found = strsep(&tmp, ":"))) {
206 if (field[0] != '\0')
208 strcat(field, found);
212 return CMD_RET_SUCCESS;
215 static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
218 return __bcb_set(argv[1], argv[2]);
221 static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
228 memset(&bcb, 0, sizeof(bcb));
229 return CMD_RET_SUCCESS;
232 if (bcb_field_get(argv[1], &field, &size))
233 return CMD_RET_FAILURE;
235 memset(field, 0, size);
237 return CMD_RET_SUCCESS;
240 static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
247 if (bcb_field_get(argv[1], &field, &size))
248 return CMD_RET_FAILURE;
250 if (*op == '=' && *(op + 1) == '\0') {
251 if (!strncmp(argv[3], field, size))
252 return CMD_RET_SUCCESS;
254 return CMD_RET_FAILURE;
255 } else if (*op == '~' && *(op + 1) == '\0') {
256 if (!strstr(field, argv[3]))
257 return CMD_RET_FAILURE;
259 return CMD_RET_SUCCESS;
261 printf("Error: Unknown operator '%s'\n", op);
264 return CMD_RET_FAILURE;
267 static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
273 if (bcb_field_get(argv[1], &field, &size))
274 return CMD_RET_FAILURE;
276 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
278 return CMD_RET_SUCCESS;
281 static int __bcb_store(void)
283 struct blk_desc *desc;
284 struct disk_partition info;
288 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
294 ret = part_get_info(desc, bcb_part, &info);
298 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
300 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
305 return CMD_RET_SUCCESS;
307 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
309 return CMD_RET_FAILURE;
312 static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
315 return __bcb_store();
318 int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
322 ret = __bcb_load(devnum, partp);
323 if (ret != CMD_RET_SUCCESS)
326 ret = __bcb_set("command", reasonp);
327 if (ret != CMD_RET_SUCCESS)
331 if (ret != CMD_RET_SUCCESS)
337 static struct cmd_tbl cmd_bcb_sub[] = {
338 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, "", ""),
346 static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
351 return CMD_RET_USAGE;
356 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
358 return CMD_RET_USAGE;
360 if (bcb_is_misused(argc, argv)) {
362 * We try to improve the user experience by reporting the
363 * root-cause of misusage, so don't return CMD_RET_USAGE,
364 * since the latter prints out the full-blown help text
366 return CMD_RET_FAILURE;
369 return c->cmd(cmdtp, flag, argc, argv);
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"
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"