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>
21 static int bcb_dev = -1;
22 static int bcb_part = -1;
23 static struct bootloader_message bcb = { { 0 } };
25 static int bcb_cmd_get(char *cmd)
27 if (!strncmp(cmd, "load", sizeof("load")))
29 if (!strncmp(cmd, "set", sizeof("set")))
30 return BCB_CMD_FIELD_SET;
31 if (!strncmp(cmd, "clear", sizeof("clear")))
32 return BCB_CMD_FIELD_CLEAR;
33 if (!strncmp(cmd, "test", sizeof("test")))
34 return BCB_CMD_FIELD_TEST;
35 if (!strncmp(cmd, "store", sizeof("store")))
37 if (!strncmp(cmd, "dump", sizeof("dump")))
38 return BCB_CMD_FIELD_DUMP;
43 static int bcb_is_misused(int argc, char *const argv[])
45 int cmd = bcb_cmd_get(argv[0]);
52 case BCB_CMD_FIELD_SET:
56 case BCB_CMD_FIELD_TEST:
60 case BCB_CMD_FIELD_CLEAR:
61 if (argc != 1 && argc != 2)
68 case BCB_CMD_FIELD_DUMP:
73 printf("Error: 'bcb %s' not supported\n", argv[0]);
77 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
78 printf("Error: Please, load BCB first!\n");
84 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
89 static int bcb_field_get(char *name, char **field, int *size)
91 if (!strncmp(name, "command", sizeof("command"))) {
93 *size = sizeof(bcb.command);
94 } else if (!strncmp(name, "status", sizeof("status"))) {
96 *size = sizeof(bcb.status);
97 } else if (!strncmp(name, "recovery", sizeof("recovery"))) {
98 *field = bcb.recovery;
99 *size = sizeof(bcb.recovery);
100 } else if (!strncmp(name, "stage", sizeof("stage"))) {
102 *size = sizeof(bcb.stage);
103 } else if (!strncmp(name, "reserved", sizeof("reserved"))) {
104 *field = bcb.reserved;
105 *size = sizeof(bcb.reserved);
107 printf("Error: Unknown bcb field '%s'\n", name);
115 do_bcb_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
117 struct blk_desc *desc;
118 disk_partition_t info;
123 ret = blk_get_device_by_str("mmc", argv[1], &desc);
127 part = simple_strtoul(argv[2], &endp, 0);
129 ret = part_get_info(desc, part, &info);
133 part = part_get_info_by_name(desc, argv[2], &info);
140 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
144 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
149 bcb_dev = desc->devnum;
151 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
153 return CMD_RET_SUCCESS;
155 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
158 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
164 return CMD_RET_FAILURE;
167 static int do_bcb_set(cmd_tbl_t *cmdtp, int flag, int argc,
171 char *field, *str, *found;
173 if (bcb_field_get(argv[1], &field, &size))
174 return CMD_RET_FAILURE;
176 len = strlen(argv[2]);
178 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
179 argv[2], len, size, argv[1]);
180 return CMD_RET_FAILURE;
185 while ((found = strsep(&str, ":"))) {
186 if (field[0] != '\0')
188 strcat(field, found);
191 return CMD_RET_SUCCESS;
194 static int do_bcb_clear(cmd_tbl_t *cmdtp, int flag, int argc,
201 memset(&bcb, 0, sizeof(bcb));
202 return CMD_RET_SUCCESS;
205 if (bcb_field_get(argv[1], &field, &size))
206 return CMD_RET_FAILURE;
208 memset(field, 0, size);
210 return CMD_RET_SUCCESS;
213 static int do_bcb_test(cmd_tbl_t *cmdtp, int flag, int argc,
220 if (bcb_field_get(argv[1], &field, &size))
221 return CMD_RET_FAILURE;
223 if (*op == '=' && *(op + 1) == '\0') {
224 if (!strncmp(argv[3], field, size))
225 return CMD_RET_SUCCESS;
227 return CMD_RET_FAILURE;
228 } else if (*op == '~' && *(op + 1) == '\0') {
229 if (!strstr(field, argv[3]))
230 return CMD_RET_FAILURE;
232 return CMD_RET_SUCCESS;
234 printf("Error: Unknown operator '%s'\n", op);
237 return CMD_RET_FAILURE;
240 static int do_bcb_dump(cmd_tbl_t *cmdtp, int flag, int argc,
246 if (bcb_field_get(argv[1], &field, &size))
247 return CMD_RET_FAILURE;
249 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
251 return CMD_RET_SUCCESS;
254 static int do_bcb_store(cmd_tbl_t *cmdtp, int flag, int argc,
257 struct blk_desc *desc;
258 disk_partition_t info;
262 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
268 ret = part_get_info(desc, bcb_part, &info);
272 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
274 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
279 return CMD_RET_SUCCESS;
281 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
283 return CMD_RET_FAILURE;
286 static cmd_tbl_t cmd_bcb_sub[] = {
287 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
288 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
289 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
290 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
291 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
292 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
295 static int do_bcb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
300 return CMD_RET_USAGE;
305 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
307 return CMD_RET_USAGE;
309 if (bcb_is_misused(argc, argv)) {
310 /* We try to improve the user experience by reporting the
311 * root-cause of misusage, so don't return CMD_RET_USAGE,
312 * since the latter prints out the full-blown help text
314 return CMD_RET_FAILURE;
317 return c->cmd(cmdtp, flag, argc, argv);
321 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
322 "Load/set/clear/test/dump/store Android BCB fields",
323 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
324 "bcb set <field> <val> - set BCB <field> to <val>\n"
325 "bcb clear [<field>] - clear BCB <field> or all fields\n"
326 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
327 "bcb dump <field> - dump BCB <field>\n"
328 "bcb store - store BCB back to mmc\n"
331 "<dev> - MMC device index containing the BCB partition\n"
332 "<part> - MMC partition index or name containing the BCB\n"
333 "<field> - one of {command,status,recovery,stage,reserved}\n"
334 "<op> - the binary operator used in 'bcb test':\n"
335 " '=' returns true if <val> matches the string stored in <field>\n"
336 " '~' returns true if <val> matches a subset of <field>'s string\n"
337 "<val> - string/text provided as input to bcb {set,test}\n"
338 " NOTE: any ':' character in <val> will be replaced by line feed\n"
339 " during 'bcb set' and used as separator by upper layers\n"