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>
23 static int bcb_dev = -1;
24 static int bcb_part = -1;
25 static struct bootloader_message bcb = { { 0 } };
27 static int bcb_cmd_get(char *cmd)
29 if (!strcmp(cmd, "load"))
31 if (!strcmp(cmd, "set"))
32 return BCB_CMD_FIELD_SET;
33 if (!strcmp(cmd, "clear"))
34 return BCB_CMD_FIELD_CLEAR;
35 if (!strcmp(cmd, "test"))
36 return BCB_CMD_FIELD_TEST;
37 if (!strcmp(cmd, "store"))
39 if (!strcmp(cmd, "dump"))
40 return BCB_CMD_FIELD_DUMP;
45 static int bcb_is_misused(int argc, char *const argv[])
47 int cmd = bcb_cmd_get(argv[0]);
51 case BCB_CMD_FIELD_SET:
55 case BCB_CMD_FIELD_TEST:
59 case BCB_CMD_FIELD_CLEAR:
60 if (argc != 1 && argc != 2)
67 case BCB_CMD_FIELD_DUMP:
72 printf("Error: 'bcb %s' not supported\n", argv[0]);
76 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
77 printf("Error: Please, load BCB first!\n");
83 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
88 static int bcb_field_get(char *name, char **fieldp, int *sizep)
90 if (!strcmp(name, "command")) {
91 *fieldp = bcb.command;
92 *sizep = sizeof(bcb.command);
93 } else if (!strcmp(name, "status")) {
95 *sizep = sizeof(bcb.status);
96 } else if (!strcmp(name, "recovery")) {
97 *fieldp = bcb.recovery;
98 *sizep = sizeof(bcb.recovery);
99 } else if (!strcmp(name, "stage")) {
101 *sizep = sizeof(bcb.stage);
102 } else if (!strcmp(name, "reserved")) {
103 *fieldp = bcb.reserved;
104 *sizep = sizeof(bcb.reserved);
106 printf("Error: Unknown bcb field '%s'\n", name);
113 static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
116 struct blk_desc *desc;
117 struct disk_partition info;
122 ret = blk_get_device_by_str("mmc", argv[1], &desc);
126 part = simple_strtoul(argv[2], &endp, 0);
128 ret = part_get_info(desc, part, &info);
132 part = part_get_info_by_name(desc, argv[2], &info);
139 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
143 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
148 bcb_dev = desc->devnum;
150 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
152 return CMD_RET_SUCCESS;
154 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
157 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
163 return CMD_RET_FAILURE;
166 static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
170 char *field, *str, *found;
172 if (bcb_field_get(argv[1], &field, &size))
173 return CMD_RET_FAILURE;
175 len = strlen(argv[2]);
177 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
178 argv[2], len, size, argv[1]);
179 return CMD_RET_FAILURE;
184 while ((found = strsep(&str, ":"))) {
185 if (field[0] != '\0')
187 strcat(field, found);
190 return CMD_RET_SUCCESS;
193 static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
200 memset(&bcb, 0, sizeof(bcb));
201 return CMD_RET_SUCCESS;
204 if (bcb_field_get(argv[1], &field, &size))
205 return CMD_RET_FAILURE;
207 memset(field, 0, size);
209 return CMD_RET_SUCCESS;
212 static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
219 if (bcb_field_get(argv[1], &field, &size))
220 return CMD_RET_FAILURE;
222 if (*op == '=' && *(op + 1) == '\0') {
223 if (!strncmp(argv[3], field, size))
224 return CMD_RET_SUCCESS;
226 return CMD_RET_FAILURE;
227 } else if (*op == '~' && *(op + 1) == '\0') {
228 if (!strstr(field, argv[3]))
229 return CMD_RET_FAILURE;
231 return CMD_RET_SUCCESS;
233 printf("Error: Unknown operator '%s'\n", op);
236 return CMD_RET_FAILURE;
239 static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
245 if (bcb_field_get(argv[1], &field, &size))
246 return CMD_RET_FAILURE;
248 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
250 return CMD_RET_SUCCESS;
253 static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
256 struct blk_desc *desc;
257 struct disk_partition info;
261 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
267 ret = part_get_info(desc, bcb_part, &info);
271 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
273 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
278 return CMD_RET_SUCCESS;
280 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
282 return CMD_RET_FAILURE;
285 static struct cmd_tbl cmd_bcb_sub[] = {
286 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
287 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
288 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
289 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
290 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
291 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
294 static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
299 return CMD_RET_USAGE;
304 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
306 return CMD_RET_USAGE;
308 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"