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=%d\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;
97 struct bootflow bflow;
98 bool all = false, boot = false, errors = false, no_global = false;
105 ret = bootstd_get_priv(&std);
107 return CMD_RET_FAILURE;
108 dev = std->cur_bootdev;
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');
122 const char *label = argv[1];
124 if (bootdev_find_by_any(label, &dev))
125 return CMD_RET_FAILURE;
129 printf("Flags not supported: enable CONFIG_BOOTFLOW_FULL\n");
130 return CMD_RET_USAGE;
135 std->cur_bootflow = NULL;
139 flags |= BOOTFLOWF_SHOW;
141 flags |= BOOTFLOWF_ALL;
143 flags |= BOOTFLOWF_SKIP_GLOBAL;
146 * If we have a device, just scan for bootflows attached to that device
148 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL) && dev) {
150 printf("Scanning for bootflows in bootdev '%s'\n",
154 bootdev_clear_bootflows(dev);
156 ret = bootflow_scan_bootdev(dev, &iter, flags, &bflow);
157 i < 1000 && ret != -ENODEV;
158 i++, ret = bootflow_scan_next(&iter, &bflow)) {
162 ret = bootdev_add_bootflow(&bflow);
164 printf("Out of memory\n");
165 return CMD_RET_FAILURE;
168 show_bootflow(i, &bflow, errors);
169 if (boot && !bflow.err)
170 bootflow_run_boot(&iter, &bflow);
174 printf("Scanning for bootflows in all bootdevs\n");
177 bootstd_clear_glob();
180 ret = bootflow_scan_first(&iter, flags, &bflow);
181 i < 1000 && ret != -ENODEV;
182 i++, ret = bootflow_scan_next(&iter, &bflow)) {
186 ret = bootdev_add_bootflow(&bflow);
188 printf("Out of memory\n");
189 return CMD_RET_FAILURE;
192 show_bootflow(i, &bflow, errors);
193 if (boot && !bflow.err)
194 bootflow_run_boot(&iter, &bflow);
197 bootflow_iter_uninit(&iter);
199 show_footer(i, num_valid);
204 #ifdef CONFIG_CMD_BOOTFLOW_FULL
205 static int do_bootflow_list(struct cmd_tbl *cmdtp, int flag, int argc,
208 struct bootstd_priv *std;
210 struct bootflow *bflow;
215 if (argc > 1 && *argv[1] == '-')
216 errors = strchr(argv[1], 'e');
218 ret = bootstd_get_priv(&std);
220 return CMD_RET_FAILURE;
221 dev = std->cur_bootdev;
223 /* If we have a device, just list bootflows attached to that device */
225 printf("Showing bootflows for bootdev '%s'\n", dev->name);
227 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
229 ret = bootdev_next_bootflow(&bflow), i++) {
230 num_valid += bflow->state == BOOTFLOWST_READY;
231 show_bootflow(i, bflow, errors);
234 printf("Showing all bootflows\n");
236 for (ret = bootflow_first_glob(&bflow), i = 0;
238 ret = bootflow_next_glob(&bflow), i++) {
239 num_valid += bflow->state == BOOTFLOWST_READY;
240 show_bootflow(i, bflow, errors);
243 show_footer(i, num_valid);
248 static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc,
251 struct bootstd_priv *std;
252 struct bootflow *bflow, *found;
259 ret = bootstd_get_priv(&std);
261 return CMD_RET_FAILURE;
264 std->cur_bootflow = NULL;
267 dev = std->cur_bootdev;
270 seq = simple_strtol(name, &endp, 16);
274 * If we have a bootdev device, only allow selection of bootflows
275 * attached to that device
278 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
280 ret = bootdev_next_bootflow(&bflow), i++) {
281 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
287 for (ret = bootflow_first_glob(&bflow), i = 0;
289 ret = bootflow_next_glob(&bflow), i++) {
290 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
298 printf("Cannot find bootflow '%s' ", name);
300 printf("in bootdev '%s' ", dev->name);
301 printf("(err=%d)\n", ret);
302 return CMD_RET_FAILURE;
304 std->cur_bootflow = found;
309 static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc,
312 struct bootstd_priv *std;
313 struct bootflow *bflow;
317 if (argc > 1 && *argv[1] == '-')
318 dump = strchr(argv[1], 'd');
320 ret = bootstd_get_priv(&std);
322 return CMD_RET_FAILURE;
324 if (!std->cur_bootflow) {
325 printf("No bootflow selected\n");
326 return CMD_RET_FAILURE;
328 bflow = std->cur_bootflow;
330 printf("Name: %s\n", bflow->name);
331 printf("Device: %s\n", bflow->dev->name);
332 printf("Block dev: %s\n", bflow->blk ? bflow->blk->name : "(none)");
333 printf("Method: %s\n", bflow->method->name);
334 printf("State: %s\n", bootflow_state_get_name(bflow->state));
335 printf("Partition: %d\n", bflow->part);
336 printf("Subdir: %s\n", bflow->subdir ? bflow->subdir : "(none)");
337 printf("Filename: %s\n", bflow->fname);
338 printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf));
339 printf("Size: %x (%d bytes)\n", bflow->size, bflow->size);
340 printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)");
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("Error: %d\n", bflow->err);
348 if (dump && bflow->buf) {
349 /* Set some sort of maximum on the size */
350 int size = min(bflow->size, 10 << 10);
353 printf("Contents:\n\n");
354 for (i = 0; i < size; i++) {
356 if (!(i % 128) && ctrlc()) {
357 printf("...interrupted\n");
366 static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc,
369 struct bootstd_priv *std;
370 struct bootflow *bflow;
373 ret = bootstd_get_priv(&std);
375 return CMD_RET_FAILURE;
378 * Require a current bootflow. Users can use 'bootflow scan -b' to
379 * automatically scan and boot, if needed.
381 if (!std->cur_bootflow) {
382 printf("No bootflow selected\n");
383 return CMD_RET_FAILURE;
385 bflow = std->cur_bootflow;
386 ret = bootflow_run_boot(NULL, bflow);
388 return CMD_RET_FAILURE;
393 static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc,
396 struct bootstd_priv *std;
397 struct bootflow *bflow;
398 bool text_mode = false;
401 if (argc > 1 && *argv[1] == '-')
402 text_mode = strchr(argv[1], 't');
404 ret = bootstd_get_priv(&std);
406 return CMD_RET_FAILURE;
408 ret = bootflow_menu_run(std, text_mode, &bflow);
411 printf("Nothing chosen\n");
413 printf("Menu failed (err=%d)\n", ret);
415 return CMD_RET_FAILURE;
418 printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name);
419 std->cur_bootflow = bflow;
423 #endif /* CONFIG_CMD_BOOTFLOW_FULL */
425 #ifdef CONFIG_SYS_LONGHELP
426 static char bootflow_help_text[] =
427 #ifdef CONFIG_CMD_BOOTFLOW_FULL
428 "scan [-abeGl] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot, -G no global)\n"
429 "bootflow list [-e] - list scanned bootflows (-e errors)\n"
430 "bootflow select [<num>|<name>] - select a bootflow\n"
431 "bootflow info [-d] - show info on current bootflow (-d dump bootflow)\n"
432 "bootflow boot - boot current bootflow (or first available if none selected)\n"
433 "bootflow menu [-t] - show a menu of available bootflows";
435 "scan - boot first available bootflow\n";
437 #endif /* CONFIG_SYS_LONGHELP */
439 U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text,
440 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_bootflow_scan),
441 #ifdef CONFIG_CMD_BOOTFLOW_FULL
442 U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list),
443 U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select),
444 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info),
445 U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot),
446 U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu),