1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
19 * report_bootflow_err() - Report where a bootflow failed
21 * When a bootflow does not make it to the 'loaded' state, something went wrong.
22 * Print a helpful message if there is an error
24 * @bflow: Bootflow to process
25 * @err: Error code (0 if none)
27 static void report_bootflow_err(struct bootflow *bflow, int err)
32 /* Indent out to 'Method' */
35 switch (bflow->state) {
37 printf("No media/partition found");
39 case BOOTFLOWST_MEDIA:
40 printf("No partition found");
43 printf("No filesystem found");
46 printf("File not found");
49 printf("File cannot be loaded");
51 case BOOTFLOWST_READY:
54 case BOOTFLOWST_COUNT:
58 printf(", err=%dE\n", err);
62 * show_bootflow() - Show the status of a bootflow
64 * @seq: Bootflow index
65 * @bflow: Bootflow to show
66 * @errors: True to show the error received, if any
68 static void show_bootflow(int index, struct bootflow *bflow, bool errors)
70 printf("%3x %-11s %-6s %-9.9s %4x %-25.25s %s\n", index,
71 bflow->method->name, bootflow_state_get_name(bflow->state),
72 bflow->dev ? dev_get_uclass_name(dev_get_parent(bflow->dev)) :
73 "(none)", bflow->part, bflow->name, bflow->fname);
75 report_bootflow_err(bflow, bflow->err);
78 static void show_header(void)
80 printf("Seq Method State Uclass Part Name Filename\n");
81 printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
84 static void show_footer(int count, int num_valid)
86 printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
87 printf("(%d bootflow%s, %d valid)\n", count, count != 1 ? "s" : "",
91 static int do_bootflow_scan(struct cmd_tbl *cmdtp, int flag, int argc,
94 struct bootstd_priv *std;
95 struct bootflow_iter iter;
96 struct udevice *dev = NULL;
97 struct bootflow bflow;
98 bool all = false, boot = false, errors = false, no_global = false;
99 bool list = false, no_hunter = false;
101 const char *label = NULL;
106 ret = bootstd_get_priv(&std);
108 return CMD_RET_FAILURE;
110 has_args = argc > 1 && *argv[1] == '-';
111 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL)) {
113 all = strchr(argv[1], 'a');
114 boot = strchr(argv[1], 'b');
115 errors = strchr(argv[1], 'e');
116 no_global = strchr(argv[1], 'G');
117 list = strchr(argv[1], 'l');
118 no_hunter = strchr(argv[1], 'H');
125 dev = std->cur_bootdev;
128 printf("Flags not supported: enable CONFIG_BOOTSTD_FULL\n");
129 return CMD_RET_USAGE;
134 std->cur_bootflow = NULL;
138 flags |= BOOTFLOWIF_SHOW;
140 flags |= BOOTFLOWIF_ALL;
142 flags |= BOOTFLOWIF_SKIP_GLOBAL;
144 flags |= BOOTFLOWIF_HUNT;
147 * If we have a device, just scan for bootflows attached to that device
150 printf("Scanning for bootflows ");
152 printf("in bootdev '%s'\n", dev->name);
154 printf("with label '%s'\n", label);
156 printf("in all bootdevs\n");
160 bootdev_clear_bootflows(dev);
162 bootstd_clear_glob();
164 ret = bootflow_scan_first(dev, label, &iter, flags, &bflow);
165 i < 1000 && ret != -ENODEV;
166 i++, ret = bootflow_scan_next(&iter, &bflow)) {
170 ret = bootdev_add_bootflow(&bflow);
172 printf("Out of memory\n");
173 return CMD_RET_FAILURE;
176 show_bootflow(i, &bflow, errors);
177 if (boot && !bflow.err)
178 bootflow_run_boot(&iter, &bflow);
180 bootflow_iter_uninit(&iter);
182 show_footer(i, num_valid);
184 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL) && !num_valid && !list)
185 printf("No bootflows found; try again with -l\n");
190 #ifdef CONFIG_CMD_BOOTFLOW_FULL
191 static int do_bootflow_list(struct cmd_tbl *cmdtp, int flag, int argc,
194 struct bootstd_priv *std;
196 struct bootflow *bflow;
201 if (argc > 1 && *argv[1] == '-')
202 errors = strchr(argv[1], 'e');
204 ret = bootstd_get_priv(&std);
206 return CMD_RET_FAILURE;
207 dev = std->cur_bootdev;
209 /* If we have a device, just list bootflows attached to that device */
211 printf("Showing bootflows for bootdev '%s'\n", dev->name);
213 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
215 ret = bootdev_next_bootflow(&bflow), i++) {
216 num_valid += bflow->state == BOOTFLOWST_READY;
217 show_bootflow(i, bflow, errors);
220 printf("Showing all bootflows\n");
222 for (ret = bootflow_first_glob(&bflow), i = 0;
224 ret = bootflow_next_glob(&bflow), i++) {
225 num_valid += bflow->state == BOOTFLOWST_READY;
226 show_bootflow(i, bflow, errors);
229 show_footer(i, num_valid);
234 static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc,
237 struct bootstd_priv *std;
238 struct bootflow *bflow, *found;
245 ret = bootstd_get_priv(&std);
247 return CMD_RET_FAILURE;
250 std->cur_bootflow = NULL;
253 dev = std->cur_bootdev;
256 seq = simple_strtol(name, &endp, 16);
260 * If we have a bootdev device, only allow selection of bootflows
261 * attached to that device
264 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
266 ret = bootdev_next_bootflow(&bflow), i++) {
267 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
273 for (ret = bootflow_first_glob(&bflow), i = 0;
275 ret = bootflow_next_glob(&bflow), i++) {
276 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
284 printf("Cannot find bootflow '%s' ", name);
286 printf("in bootdev '%s' ", dev->name);
287 printf("(err=%d)\n", ret);
288 return CMD_RET_FAILURE;
290 std->cur_bootflow = found;
291 if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
292 if (env_set("bootargs", found->cmdline)) {
293 printf("Cannot set bootargs\n");
294 return CMD_RET_FAILURE;
301 static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc,
304 struct bootstd_priv *std;
305 struct bootflow *bflow;
309 if (argc > 1 && *argv[1] == '-')
310 dump = strchr(argv[1], 'd');
312 ret = bootstd_get_priv(&std);
314 return CMD_RET_FAILURE;
316 if (!std->cur_bootflow) {
317 printf("No bootflow selected\n");
318 return CMD_RET_FAILURE;
320 bflow = std->cur_bootflow;
322 printf("Name: %s\n", bflow->name);
323 printf("Device: %s\n", bflow->dev->name);
324 printf("Block dev: %s\n", bflow->blk ? bflow->blk->name : "(none)");
325 printf("Method: %s\n", bflow->method->name);
326 printf("State: %s\n", bootflow_state_get_name(bflow->state));
327 printf("Partition: %d\n", bflow->part);
328 printf("Subdir: %s\n", bflow->subdir ? bflow->subdir : "(none)");
329 printf("Filename: %s\n", bflow->fname);
330 printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf));
331 printf("Size: %x (%d bytes)\n", bflow->size, bflow->size);
332 printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)");
335 puts(bflow->cmdline);
339 if (bflow->x86_setup)
340 printf("X86 setup: %p\n", bflow->x86_setup);
341 printf("Logo: %s\n", bflow->logo ?
342 simple_xtoa((ulong)map_to_sysmem(bflow->logo)) : "(none)");
344 printf("Logo size: %x (%d bytes)\n", bflow->logo_size,
347 printf("FDT: %s\n", bflow->fdt_fname);
348 if (bflow->fdt_fname) {
349 printf("FDT size: %x (%d bytes)\n", bflow->fdt_size,
351 printf("FDT addr: %lx\n", bflow->fdt_addr);
353 printf("Error: %d\n", bflow->err);
354 if (dump && bflow->buf) {
355 /* Set some sort of maximum on the size */
356 int size = min(bflow->size, 10 << 10);
359 printf("Contents:\n\n");
360 for (i = 0; i < size; i++) {
362 if (!(i % 128) && ctrlc()) {
363 printf("...interrupted\n");
372 static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc,
375 struct bootstd_priv *std;
376 struct bootflow *bflow;
379 ret = bootstd_get_priv(&std);
381 return CMD_RET_FAILURE;
384 * Require a current bootflow. Users can use 'bootflow scan -b' to
385 * automatically scan and boot, if needed.
387 if (!std->cur_bootflow) {
388 printf("No bootflow selected\n");
389 return CMD_RET_FAILURE;
391 bflow = std->cur_bootflow;
392 ret = bootflow_run_boot(NULL, bflow);
394 return CMD_RET_FAILURE;
399 static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc,
402 struct bootstd_priv *std;
403 struct bootflow *bflow;
404 bool text_mode = false;
407 if (!IS_ENABLED(CONFIG_EXPO)) {
408 printf("Menu not supported\n");
409 return CMD_RET_FAILURE;
412 if (argc > 1 && *argv[1] == '-')
413 text_mode = strchr(argv[1], 't');
415 ret = bootstd_get_priv(&std);
417 return CMD_RET_FAILURE;
419 ret = bootflow_menu_run(std, text_mode, &bflow);
422 printf("Nothing chosen\n");
424 printf("Menu failed (err=%d)\n", ret);
425 return CMD_RET_FAILURE;
429 printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name);
430 std->cur_bootflow = bflow;
435 static int do_bootflow_cmdline(struct cmd_tbl *cmdtp, int flag, int argc,
438 struct bootstd_priv *std;
439 struct bootflow *bflow;
440 const char *op, *arg, *val = NULL;
444 return CMD_RET_USAGE;
446 ret = bootstd_get_priv(&std);
448 return CMD_RET_FAILURE;
450 bflow = std->cur_bootflow;
452 printf("No bootflow selected\n");
453 return CMD_RET_FAILURE;
460 return CMD_RET_USAGE;
465 case 'c': /* clear */
469 case 'd': /* delete */
470 ret = bootflow_cmdline_set_arg(bflow, arg, val, true);
473 ret = bootflow_cmdline_get_arg(bflow, arg, &val);
475 printf("%.*s\n", ret, val);
478 ret = bootflow_cmdline_auto(bflow, arg);
483 printf("Argument too long\n");
486 printf("Argument not found\n");
489 printf("Mismatched quotes\n");
492 printf("Value must be quoted\n");
496 printf("Unknown error: %dE\n", ret);
499 return CMD_RET_FAILURE;
503 #endif /* CONFIG_CMD_BOOTFLOW_FULL */
505 #ifdef CONFIG_SYS_LONGHELP
506 static char bootflow_help_text[] =
507 #ifdef CONFIG_CMD_BOOTFLOW_FULL
508 "scan [-abeGl] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot, -G no global)\n"
509 "bootflow list [-e] - list scanned bootflows (-e errors)\n"
510 "bootflow select [<num>|<name>] - select a bootflow\n"
511 "bootflow info [-d] - show info on current bootflow (-d dump bootflow)\n"
512 "bootflow boot - boot current bootflow (or first available if none selected)\n"
513 "bootflow menu [-t] - show a menu of available bootflows\n"
514 "bootflow cmdline [set|get|clear|delete|auto] <param> [<value>] - update cmdline";
516 "scan - boot first available bootflow\n";
518 #endif /* CONFIG_SYS_LONGHELP */
520 U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text,
521 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_bootflow_scan),
522 #ifdef CONFIG_CMD_BOOTFLOW_FULL
523 U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list),
524 U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select),
525 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info),
526 U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot),
527 U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu),
528 U_BOOT_SUBCMD_MKENT(cmdline, 4, 1, do_bootflow_cmdline),