1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2019 Eugeniu Rosca <rosca.eugeniu@gmail.com>
4 *
5 * Command to read/modify/write Android BCB fields
6 */
7
8 #include <android_bootloader_message.h>
9 #include <bcb.h>
10 #include <command.h>
11 #include <env.h>
12 #include <android_ab.h>
13 #include <display_options.h>
14 #include <log.h>
15 #include <part.h>
16 #include <malloc.h>
17 #include <memalign.h>
18 #include <vsprintf.h>
19 #include <linux/err.h>
20
21 static const char * const fields[] = {
22 "command",
23 "status",
24 "recovery",
25 "stage"
26 };
27
28 static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
29 static struct disk_partition partition_data;
30
31 static struct blk_desc *block;
32 static struct disk_partition *partition = &partition_data;
33
bcb_not_loaded(void)34 static int bcb_not_loaded(void)
35 {
36 printf("Error: Please, load BCB first!\n");
37 return -1;
38 }
39
bcb_field_get(const char * name,char ** fieldp,int * sizep)40 static int bcb_field_get(const char *name, char **fieldp, int *sizep)
41 {
42 if (!strcmp(name, "command")) {
43 *fieldp = bcb.command;
44 *sizep = sizeof(bcb.command);
45 } else if (!strcmp(name, "status")) {
46 *fieldp = bcb.status;
47 *sizep = sizeof(bcb.status);
48 } else if (!strcmp(name, "recovery")) {
49 *fieldp = bcb.recovery;
50 *sizep = sizeof(bcb.recovery);
51 } else if (!strcmp(name, "stage")) {
52 *fieldp = bcb.stage;
53 *sizep = sizeof(bcb.stage);
54 } else if (!strcmp(name, "reserved")) {
55 *fieldp = bcb.reserved;
56 *sizep = sizeof(bcb.reserved);
57 } else {
58 printf("Error: Unknown bcb field '%s'\n", name);
59 return -1;
60 }
61
62 return 0;
63 }
64
__bcb_reset(void)65 static void __bcb_reset(void)
66 {
67 block = NULL;
68 partition = &partition_data;
69 memset(&partition_data, 0, sizeof(struct disk_partition));
70 memset(&bcb, 0, sizeof(struct bootloader_message));
71 }
72
__bcb_initialize(const char * iface,int devnum,const char * partp)73 static int __bcb_initialize(const char *iface, int devnum, const char *partp)
74 {
75 char *endp;
76 int part, ret;
77
78 block = blk_get_dev(iface, devnum);
79 if (!block) {
80 ret = -ENODEV;
81 goto err_read_fail;
82 }
83
84 /*
85 * always select the first hwpart in case another
86 * blk operation selected a different hwpart
87 */
88 ret = blk_dselect_hwpart(block, 0);
89 if (IS_ERR_VALUE(ret)) {
90 ret = -ENODEV;
91 goto err_read_fail;
92 }
93
94 part = simple_strtoul(partp, &endp, 0);
95 if (*endp == '\0') {
96 ret = part_get_info(block, part, partition);
97 if (ret)
98 goto err_read_fail;
99 } else {
100 part = part_get_info_by_name(block, partp, partition);
101 if (part < 0) {
102 ret = part;
103 goto err_read_fail;
104 }
105 }
106
107 return CMD_RET_SUCCESS;
108
109 err_read_fail:
110 printf("Error: %s %d:%s read failed (%d)\n", iface, devnum,
111 partition->name, ret);
112 __bcb_reset();
113 return CMD_RET_FAILURE;
114 }
115
__bcb_load(void)116 static int __bcb_load(void)
117 {
118 u64 cnt;
119 int ret;
120
121 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), partition->blksz);
122 if (cnt > partition->size)
123 goto err_too_small;
124
125 if (blk_dread(block, partition->start, cnt, &bcb) != cnt) {
126 ret = -EIO;
127 goto err_read_fail;
128 }
129
130 debug("%s: Loaded from %d %d:%s\n", __func__, block->uclass_id,
131 block->devnum, partition->name);
132
133 return CMD_RET_SUCCESS;
134 err_read_fail:
135 printf("Error: %d %d:%s read failed (%d)\n", block->uclass_id,
136 block->devnum, partition->name, ret);
137 goto err;
138 err_too_small:
139 printf("Error: %d %d:%s too small!", block->uclass_id,
140 block->devnum, partition->name);
141 err:
142 __bcb_reset();
143 return CMD_RET_FAILURE;
144 }
145
do_bcb_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])146 static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
147 char * const argv[])
148 {
149 int ret;
150 int devnum;
151 char *endp;
152 char *iface = "mmc";
153
154 if (argc < 3)
155 return CMD_RET_USAGE;
156
157 if (argc == 4) {
158 iface = argv[1];
159 argc--;
160 argv++;
161 }
162
163 devnum = simple_strtoul(argv[1], &endp, 0);
164 if (*endp != '\0') {
165 printf("Error: Device id '%s' not a number\n", argv[1]);
166 return CMD_RET_FAILURE;
167 }
168
169 ret = __bcb_initialize(iface, devnum, argv[2]);
170 if (ret != CMD_RET_SUCCESS)
171 return ret;
172
173 return __bcb_load();
174 }
175
__bcb_set(const char * fieldp,const char * valp)176 static int __bcb_set(const char *fieldp, const char *valp)
177 {
178 int size, len;
179 char *field, *str, *found, *tmp;
180
181 if (bcb_field_get(fieldp, &field, &size))
182 return CMD_RET_FAILURE;
183
184 len = strlen(valp);
185 if (len >= size) {
186 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
187 valp, len, size, fieldp);
188 return CMD_RET_FAILURE;
189 }
190 str = strdup(valp);
191 if (!str) {
192 printf("Error: Out of memory while strdup\n");
193 return CMD_RET_FAILURE;
194 }
195
196 tmp = str;
197 field[0] = '\0';
198 while ((found = strsep(&tmp, ":"))) {
199 if (field[0] != '\0')
200 strcat(field, "\n");
201 strcat(field, found);
202 }
203 free(str);
204
205 return CMD_RET_SUCCESS;
206 }
207
do_bcb_set(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])208 static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
209 char * const argv[])
210 {
211 if (argc < 3)
212 return CMD_RET_USAGE;
213
214 if (!block)
215 return bcb_not_loaded();
216
217 return __bcb_set(argv[1], argv[2]);
218 }
219
do_bcb_clear(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])220 static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
221 char *const argv[])
222 {
223 int size;
224 char *field;
225
226 if (!block)
227 return bcb_not_loaded();
228
229 if (argc == 1) {
230 memset(&bcb, 0, sizeof(bcb));
231 return CMD_RET_SUCCESS;
232 }
233
234 if (bcb_field_get(argv[1], &field, &size))
235 return CMD_RET_FAILURE;
236
237 memset(field, 0, size);
238
239 return CMD_RET_SUCCESS;
240 }
241
do_bcb_test(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])242 static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
243 char *const argv[])
244 {
245 int size;
246 char *field;
247 char *op;
248
249 if (argc < 4)
250 return CMD_RET_USAGE;
251
252 if (!block)
253 return bcb_not_loaded();
254
255 op = argv[2];
256
257 if (bcb_field_get(argv[1], &field, &size))
258 return CMD_RET_FAILURE;
259
260 if (*op == '=' && *(op + 1) == '\0') {
261 if (!strncmp(argv[3], field, size))
262 return CMD_RET_SUCCESS;
263 else
264 return CMD_RET_FAILURE;
265 } else if (*op == '~' && *(op + 1) == '\0') {
266 if (!strstr(field, argv[3]))
267 return CMD_RET_FAILURE;
268 else
269 return CMD_RET_SUCCESS;
270 } else {
271 printf("Error: Unknown operator '%s'\n", op);
272 }
273
274 return CMD_RET_FAILURE;
275 }
276
do_bcb_dump(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])277 static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
278 char *const argv[])
279 {
280 int size;
281 char *field;
282
283 if (argc < 2)
284 return CMD_RET_USAGE;
285
286 if (!block)
287 return bcb_not_loaded();
288
289 if (bcb_field_get(argv[1], &field, &size))
290 return CMD_RET_FAILURE;
291
292 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
293
294 return CMD_RET_SUCCESS;
295 }
296
__bcb_store(void)297 static int __bcb_store(void)
298 {
299 u64 cnt;
300 int ret;
301
302 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), partition->blksz);
303
304 if (blk_dwrite(block, partition->start, cnt, &bcb) != cnt) {
305 ret = -EIO;
306 goto err;
307 }
308
309 return CMD_RET_SUCCESS;
310 err:
311 printf("Error: %d %d:%s write failed (%d)\n", block->uclass_id,
312 block->devnum, partition->name, ret);
313
314 return CMD_RET_FAILURE;
315 }
316
do_bcb_store(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])317 static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
318 char * const argv[])
319 {
320 if (!block)
321 return bcb_not_loaded();
322
323 return __bcb_store();
324 }
325
bcb_find_partition_and_load(const char * iface,int devnum,char * partp)326 int bcb_find_partition_and_load(const char *iface, int devnum, char *partp)
327 {
328 int ret;
329
330 __bcb_reset();
331
332 ret = __bcb_initialize(iface, devnum, partp);
333 if (ret != CMD_RET_SUCCESS)
334 return ret;
335
336 return __bcb_load();
337 }
338
bcb_load(struct blk_desc * block_description,struct disk_partition * disk_partition)339 int bcb_load(struct blk_desc *block_description, struct disk_partition *disk_partition)
340 {
341 __bcb_reset();
342
343 block = block_description;
344 partition = disk_partition;
345
346 return __bcb_load();
347 }
348
bcb_set(enum bcb_field field,const char * value)349 int bcb_set(enum bcb_field field, const char *value)
350 {
351 if (field > BCB_FIELD_STAGE)
352 return CMD_RET_FAILURE;
353 return __bcb_set(fields[field], value);
354 }
355
bcb_get(enum bcb_field field,char * value_out,size_t value_size)356 int bcb_get(enum bcb_field field, char *value_out, size_t value_size)
357 {
358 int size;
359 char *field_value;
360
361 if (field > BCB_FIELD_STAGE)
362 return CMD_RET_FAILURE;
363 if (bcb_field_get(fields[field], &field_value, &size))
364 return CMD_RET_FAILURE;
365
366 strlcpy(value_out, field_value, value_size);
367
368 return CMD_RET_SUCCESS;
369 }
370
bcb_store(void)371 int bcb_store(void)
372 {
373 return __bcb_store();
374 }
375
bcb_reset(void)376 void bcb_reset(void)
377 {
378 __bcb_reset();
379 }
380
do_bcb_ab_select(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])381 __maybe_unused static int do_bcb_ab_select(struct cmd_tbl *cmdtp,
382 int flag, int argc,
383 char * const argv[])
384 {
385 int ret;
386 struct blk_desc *dev_desc;
387 struct disk_partition part_info;
388 char slot[2];
389 bool dec_tries = true;
390
391 if (argc < 4)
392 return CMD_RET_USAGE;
393
394 for (int i = 4; i < argc; i++) {
395 if (!strcmp(argv[i], "--no-dec"))
396 dec_tries = false;
397 else
398 return CMD_RET_USAGE;
399 }
400
401 /* Lookup the "misc" partition from argv[2] and argv[3] */
402 if (part_get_info_by_dev_and_name_or_num(argv[2], argv[3],
403 &dev_desc, &part_info,
404 false) < 0) {
405 return CMD_RET_FAILURE;
406 }
407
408 ret = ab_select_slot(dev_desc, &part_info, dec_tries);
409 if (ret < 0) {
410 printf("Android boot failed, error %d.\n", ret);
411 return CMD_RET_FAILURE;
412 }
413
414 /* Android standard slot names are 'a', 'b', ... */
415 slot[0] = BOOT_SLOT_NAME(ret);
416 slot[1] = '\0';
417 env_set(argv[1], slot);
418 printf("ANDROID: Booting slot: %s\n", slot);
419
420 return CMD_RET_SUCCESS;
421 }
422
do_bcb_ab_dump(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])423 __maybe_unused static int do_bcb_ab_dump(struct cmd_tbl *cmdtp,
424 int flag, int argc,
425 char *const argv[])
426 {
427 int ret;
428 struct blk_desc *dev_desc;
429 struct disk_partition part_info;
430
431 if (argc < 3)
432 return CMD_RET_USAGE;
433
434 if (part_get_info_by_dev_and_name_or_num(argv[1], argv[2],
435 &dev_desc, &part_info,
436 false) < 0) {
437 return CMD_RET_FAILURE;
438 }
439
440 ret = ab_dump_abc(dev_desc, &part_info);
441 if (ret < 0) {
442 printf("Cannot dump ABC data, error %d.\n", ret);
443 return CMD_RET_FAILURE;
444 }
445
446 return CMD_RET_SUCCESS;
447 }
448
449 U_BOOT_LONGHELP(bcb,
450 "load <interface> <dev> <part> - load BCB from <interface> <dev>:<part>\n"
451 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
452 "bcb set <field> <val> - set BCB <field> to <val>\n"
453 "bcb clear [<field>] - clear BCB <field> or all fields\n"
454 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
455 "bcb dump <field> - dump BCB <field>\n"
456 "bcb store - store BCB back to <interface>\n"
457 "\n"
458 #if IS_ENABLED(CONFIG_ANDROID_AB)
459 "bcb ab_select -\n"
460 " Select the slot used to boot from and register the boot attempt.\n"
461 " <slot_var_name> <interface> <dev[:part|#part_name]> [--no-dec]\n"
462 " - Load the slot metadata from the partition 'part' on\n"
463 " device type 'interface' instance 'dev' and store the active\n"
464 " slot in the 'slot_var_name' variable. This also updates the\n"
465 " Android slot metadata with a boot attempt, which can cause\n"
466 " successive calls to this function to return a different result\n"
467 " if the returned slot runs out of boot attempts.\n"
468 " - If 'part_name' is passed, preceded with a # instead of :, the\n"
469 " partition name whose label is 'part_name' will be looked up in\n"
470 " the partition table. This is commonly the \"misc\" partition.\n"
471 " - If '--no-dec' is set, the number of tries remaining will not\n"
472 " decremented for the selected boot slot\n"
473 "\n"
474 "bcb ab_dump -\n"
475 " Dump boot_control information from specific partition.\n"
476 " <interface> <dev[:part|#part_name]>\n"
477 "\n"
478 #endif
479 "Legend:\n"
480 "<interface> - storage device interface (virtio, mmc, etc)\n"
481 "<dev> - storage device index containing the BCB partition\n"
482 "<part> - partition index or name containing the BCB\n"
483 "<field> - one of {command,status,recovery,stage,reserved}\n"
484 "<op> - the binary operator used in 'bcb test':\n"
485 " '=' returns true if <val> matches the string stored in <field>\n"
486 " '~' returns true if <val> matches a subset of <field>'s string\n"
487 "<val> - string/text provided as input to bcb {set,test}\n"
488 " NOTE: any ':' character in <val> will be replaced by line feed\n"
489 " during 'bcb set' and used as separator by upper layers\n"
490 );
491
492 U_BOOT_CMD_WITH_SUBCMDS(bcb,
493 "Load/set/clear/test/dump/store Android BCB fields", bcb_help_text,
494 U_BOOT_SUBCMD_MKENT(load, 4, 1, do_bcb_load),
495 U_BOOT_SUBCMD_MKENT(set, 3, 1, do_bcb_set),
496 U_BOOT_SUBCMD_MKENT(clear, 2, 1, do_bcb_clear),
497 U_BOOT_SUBCMD_MKENT(test, 4, 1, do_bcb_test),
498 U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_bcb_dump),
499 U_BOOT_SUBCMD_MKENT(store, 1, 1, do_bcb_store),
500 #if IS_ENABLED(CONFIG_ANDROID_AB)
501 U_BOOT_SUBCMD_MKENT(ab_select, 5, 1, do_bcb_ab_select),
502 U_BOOT_SUBCMD_MKENT(ab_dump, 3, 1, do_bcb_ab_dump),
503 #endif
504 );
505