1 // SPDX-License-Identifier: GPL-2.0+
5 * Command to read/modify/write Android BCB fields
8 #include <android_bootloader_message.h>
22 static int bcb_dev = -1;
23 static int bcb_part = -1;
24 static struct bootloader_message bcb = { { 0 } };
26 static int bcb_cmd_get(char *cmd)
28 if (!strcmp(cmd, "load"))
30 if (!strcmp(cmd, "set"))
31 return BCB_CMD_FIELD_SET;
32 if (!strcmp(cmd, "clear"))
33 return BCB_CMD_FIELD_CLEAR;
34 if (!strcmp(cmd, "test"))
35 return BCB_CMD_FIELD_TEST;
36 if (!strcmp(cmd, "store"))
38 if (!strcmp(cmd, "dump"))
39 return BCB_CMD_FIELD_DUMP;
44 static int bcb_is_misused(int argc, char *const argv[])
46 int cmd = bcb_cmd_get(argv[0]);
50 case BCB_CMD_FIELD_SET:
54 case BCB_CMD_FIELD_TEST:
58 case BCB_CMD_FIELD_CLEAR:
59 if (argc != 1 && argc != 2)
66 case BCB_CMD_FIELD_DUMP:
71 printf("Error: 'bcb %s' not supported\n", argv[0]);
75 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
76 printf("Error: Please, load BCB first!\n");
82 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
87 static int bcb_field_get(char *name, char **fieldp, int *sizep)
89 if (!strcmp(name, "command")) {
90 *fieldp = bcb.command;
91 *sizep = sizeof(bcb.command);
92 } else if (!strcmp(name, "status")) {
94 *sizep = sizeof(bcb.status);
95 } else if (!strcmp(name, "recovery")) {
96 *fieldp = bcb.recovery;
97 *sizep = sizeof(bcb.recovery);
98 } else if (!strcmp(name, "stage")) {
100 *sizep = sizeof(bcb.stage);
101 } else if (!strcmp(name, "reserved")) {
102 *fieldp = bcb.reserved;
103 *sizep = sizeof(bcb.reserved);
105 printf("Error: Unknown bcb field '%s'\n", name);
112 static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
115 struct blk_desc *desc;
116 struct disk_partition info;
121 ret = blk_get_device_by_str("mmc", argv[1], &desc);
125 part = simple_strtoul(argv[2], &endp, 0);
127 ret = part_get_info(desc, part, &info);
131 part = part_get_info_by_name(desc, argv[2], &info);
138 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
142 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
147 bcb_dev = desc->devnum;
149 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
151 return CMD_RET_SUCCESS;
153 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
156 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
162 return CMD_RET_FAILURE;
165 static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
169 char *field, *str, *found;
171 if (bcb_field_get(argv[1], &field, &size))
172 return CMD_RET_FAILURE;
174 len = strlen(argv[2]);
176 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
177 argv[2], len, size, argv[1]);
178 return CMD_RET_FAILURE;
183 while ((found = strsep(&str, ":"))) {
184 if (field[0] != '\0')
186 strcat(field, found);
189 return CMD_RET_SUCCESS;
192 static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
199 memset(&bcb, 0, sizeof(bcb));
200 return CMD_RET_SUCCESS;
203 if (bcb_field_get(argv[1], &field, &size))
204 return CMD_RET_FAILURE;
206 memset(field, 0, size);
208 return CMD_RET_SUCCESS;
211 static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
218 if (bcb_field_get(argv[1], &field, &size))
219 return CMD_RET_FAILURE;
221 if (*op == '=' && *(op + 1) == '\0') {
222 if (!strncmp(argv[3], field, size))
223 return CMD_RET_SUCCESS;
225 return CMD_RET_FAILURE;
226 } else if (*op == '~' && *(op + 1) == '\0') {
227 if (!strstr(field, argv[3]))
228 return CMD_RET_FAILURE;
230 return CMD_RET_SUCCESS;
232 printf("Error: Unknown operator '%s'\n", op);
235 return CMD_RET_FAILURE;
238 static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
244 if (bcb_field_get(argv[1], &field, &size))
245 return CMD_RET_FAILURE;
247 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
249 return CMD_RET_SUCCESS;
252 static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
255 struct blk_desc *desc;
256 struct disk_partition info;
260 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
266 ret = part_get_info(desc, bcb_part, &info);
270 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
272 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
277 return CMD_RET_SUCCESS;
279 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
281 return CMD_RET_FAILURE;
284 static struct cmd_tbl cmd_bcb_sub[] = {
285 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
286 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
287 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
288 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
289 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
290 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
293 static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
298 return CMD_RET_USAGE;
303 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
305 return CMD_RET_USAGE;
307 if (bcb_is_misused(argc, argv)) {
309 * We try to improve the user experience by reporting the
310 * root-cause of misusage, so don't return CMD_RET_USAGE,
311 * since the latter prints out the full-blown help text
313 return CMD_RET_FAILURE;
316 return c->cmd(cmdtp, flag, argc, argv);
320 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
321 "Load/set/clear/test/dump/store Android BCB fields",
322 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
323 "bcb set <field> <val> - set BCB <field> to <val>\n"
324 "bcb clear [<field>] - clear BCB <field> or all fields\n"
325 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
326 "bcb dump <field> - dump BCB <field>\n"
327 "bcb store - store BCB back to mmc\n"
330 "<dev> - MMC device index containing the BCB partition\n"
331 "<part> - MMC partition index or name containing the BCB\n"
332 "<field> - one of {command,status,recovery,stage,reserved}\n"
333 "<op> - the binary operator used in 'bcb test':\n"
334 " '=' returns true if <val> matches the string stored in <field>\n"
335 " '~' returns true if <val> matches a subset of <field>'s string\n"
336 "<val> - string/text provided as input to bcb {set,test}\n"
337 " NOTE: any ':' character in <val> will be replaced by line feed\n"
338 " during 'bcb set' and used as separator by upper layers\n"