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 (!strcmp(cmd, "load"))
29 if (!strcmp(cmd, "set"))
30 return BCB_CMD_FIELD_SET;
31 if (!strcmp(cmd, "clear"))
32 return BCB_CMD_FIELD_CLEAR;
33 if (!strcmp(cmd, "test"))
34 return BCB_CMD_FIELD_TEST;
35 if (!strcmp(cmd, "store"))
37 if (!strcmp(cmd, "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]);
49 case BCB_CMD_FIELD_SET:
53 case BCB_CMD_FIELD_TEST:
57 case BCB_CMD_FIELD_CLEAR:
58 if (argc != 1 && argc != 2)
65 case BCB_CMD_FIELD_DUMP:
70 printf("Error: 'bcb %s' not supported\n", argv[0]);
74 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
75 printf("Error: Please, load BCB first!\n");
81 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
86 static int bcb_field_get(char *name, char **fieldp, int *sizep)
88 if (!strcmp(name, "command")) {
89 *fieldp = bcb.command;
90 *sizep = sizeof(bcb.command);
91 } else if (!strcmp(name, "status")) {
93 *sizep = sizeof(bcb.status);
94 } else if (!strcmp(name, "recovery")) {
95 *fieldp = bcb.recovery;
96 *sizep = sizeof(bcb.recovery);
97 } else if (!strcmp(name, "stage")) {
99 *sizep = sizeof(bcb.stage);
100 } else if (!strcmp(name, "reserved")) {
101 *fieldp = bcb.reserved;
102 *sizep = sizeof(bcb.reserved);
104 printf("Error: Unknown bcb field '%s'\n", name);
111 static int do_bcb_load(cmd_tbl_t *cmdtp, int flag, int argc,
114 struct blk_desc *desc;
115 disk_partition_t info;
120 ret = blk_get_device_by_str("mmc", argv[1], &desc);
124 part = simple_strtoul(argv[2], &endp, 0);
126 ret = part_get_info(desc, part, &info);
130 part = part_get_info_by_name(desc, argv[2], &info);
137 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
141 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
146 bcb_dev = desc->devnum;
148 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
150 return CMD_RET_SUCCESS;
152 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
155 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
161 return CMD_RET_FAILURE;
164 static int do_bcb_set(cmd_tbl_t *cmdtp, int flag, int argc,
168 char *field, *str, *found;
170 if (bcb_field_get(argv[1], &field, &size))
171 return CMD_RET_FAILURE;
173 len = strlen(argv[2]);
175 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
176 argv[2], len, size, argv[1]);
177 return CMD_RET_FAILURE;
182 while ((found = strsep(&str, ":"))) {
183 if (field[0] != '\0')
185 strcat(field, found);
188 return CMD_RET_SUCCESS;
191 static int do_bcb_clear(cmd_tbl_t *cmdtp, int flag, int argc,
198 memset(&bcb, 0, sizeof(bcb));
199 return CMD_RET_SUCCESS;
202 if (bcb_field_get(argv[1], &field, &size))
203 return CMD_RET_FAILURE;
205 memset(field, 0, size);
207 return CMD_RET_SUCCESS;
210 static int do_bcb_test(cmd_tbl_t *cmdtp, int flag, int argc,
217 if (bcb_field_get(argv[1], &field, &size))
218 return CMD_RET_FAILURE;
220 if (*op == '=' && *(op + 1) == '\0') {
221 if (!strncmp(argv[3], field, size))
222 return CMD_RET_SUCCESS;
224 return CMD_RET_FAILURE;
225 } else if (*op == '~' && *(op + 1) == '\0') {
226 if (!strstr(field, argv[3]))
227 return CMD_RET_FAILURE;
229 return CMD_RET_SUCCESS;
231 printf("Error: Unknown operator '%s'\n", op);
234 return CMD_RET_FAILURE;
237 static int do_bcb_dump(cmd_tbl_t *cmdtp, int flag, int argc,
243 if (bcb_field_get(argv[1], &field, &size))
244 return CMD_RET_FAILURE;
246 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
248 return CMD_RET_SUCCESS;
251 static int do_bcb_store(cmd_tbl_t *cmdtp, int flag, int argc,
254 struct blk_desc *desc;
255 disk_partition_t info;
259 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
265 ret = part_get_info(desc, bcb_part, &info);
269 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
271 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
276 return CMD_RET_SUCCESS;
278 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
280 return CMD_RET_FAILURE;
283 static cmd_tbl_t cmd_bcb_sub[] = {
284 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
285 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
286 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
287 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
288 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
289 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
292 static int do_bcb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
297 return CMD_RET_USAGE;
302 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
304 return CMD_RET_USAGE;
306 if (bcb_is_misused(argc, argv)) {
308 * We try to improve the user experience by reporting the
309 * root-cause of misusage, so don't return CMD_RET_USAGE,
310 * since the latter prints out the full-blown help text
312 return CMD_RET_FAILURE;
315 return c->cmd(cmdtp, flag, argc, argv);
319 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
320 "Load/set/clear/test/dump/store Android BCB fields",
321 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
322 "bcb set <field> <val> - set BCB <field> to <val>\n"
323 "bcb clear [<field>] - clear BCB <field> or all fields\n"
324 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
325 "bcb dump <field> - dump BCB <field>\n"
326 "bcb store - store BCB back to mmc\n"
329 "<dev> - MMC device index containing the BCB partition\n"
330 "<part> - MMC partition index or name containing the BCB\n"
331 "<field> - one of {command,status,recovery,stage,reserved}\n"
332 "<op> - the binary operator used in 'bcb test':\n"
333 " '=' returns true if <val> matches the string stored in <field>\n"
334 " '~' returns true if <val> matches a subset of <field>'s string\n"
335 "<val> - string/text provided as input to bcb {set,test}\n"
336 " NOTE: any ':' character in <val> will be replaced by line feed\n"
337 " during 'bcb set' and used as separator by upper layers\n"