blob: 9562832ce4387131c35b03b3b7e6c04f992ae2f7 [file] [log] [blame]
Simon Glass7617f492022-04-24 23:31:11 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * 'bootflow' command
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
10#include <bootdev.h>
11#include <bootflow.h>
Simon Glass5495aaf2023-07-30 11:17:00 -060012#include <bootm.h>
Simon Glass7617f492022-04-24 23:31:11 -060013#include <bootstd.h>
14#include <command.h>
15#include <console.h>
16#include <dm.h>
17#include <mapmem.h>
18
19/**
20 * report_bootflow_err() - Report where a bootflow failed
21 *
22 * When a bootflow does not make it to the 'loaded' state, something went wrong.
23 * Print a helpful message if there is an error
24 *
25 * @bflow: Bootflow to process
26 * @err: Error code (0 if none)
27 */
28static void report_bootflow_err(struct bootflow *bflow, int err)
29{
30 if (!err)
31 return;
32
33 /* Indent out to 'Method' */
34 printf(" ** ");
35
36 switch (bflow->state) {
37 case BOOTFLOWST_BASE:
38 printf("No media/partition found");
39 break;
40 case BOOTFLOWST_MEDIA:
41 printf("No partition found");
42 break;
43 case BOOTFLOWST_PART:
44 printf("No filesystem found");
45 break;
46 case BOOTFLOWST_FS:
47 printf("File not found");
48 break;
49 case BOOTFLOWST_FILE:
50 printf("File cannot be loaded");
51 break;
52 case BOOTFLOWST_READY:
53 printf("Ready");
54 break;
55 case BOOTFLOWST_COUNT:
56 break;
57 }
58
Simon Glass9482fdf2023-05-10 16:34:26 -060059 printf(", err=%dE\n", err);
Simon Glass7617f492022-04-24 23:31:11 -060060}
61
62/**
63 * show_bootflow() - Show the status of a bootflow
64 *
65 * @seq: Bootflow index
66 * @bflow: Bootflow to show
67 * @errors: True to show the error received, if any
68 */
69static void show_bootflow(int index, struct bootflow *bflow, bool errors)
70{
71 printf("%3x %-11s %-6s %-9.9s %4x %-25.25s %s\n", index,
72 bflow->method->name, bootflow_state_get_name(bflow->state),
Simon Glassdee4d642022-07-30 15:52:23 -060073 bflow->dev ? dev_get_uclass_name(dev_get_parent(bflow->dev)) :
74 "(none)", bflow->part, bflow->name, bflow->fname);
Simon Glass7617f492022-04-24 23:31:11 -060075 if (errors)
76 report_bootflow_err(bflow, bflow->err);
77}
78
79static void show_header(void)
80{
81 printf("Seq Method State Uclass Part Name Filename\n");
82 printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
83}
84
85static void show_footer(int count, int num_valid)
86{
87 printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
88 printf("(%d bootflow%s, %d valid)\n", count, count != 1 ? "s" : "",
89 num_valid);
90}
91
92static int do_bootflow_scan(struct cmd_tbl *cmdtp, int flag, int argc,
93 char *const argv[])
94{
95 struct bootstd_priv *std;
96 struct bootflow_iter iter;
Simon Glassba3d5372023-01-17 10:48:15 -070097 struct udevice *dev = NULL;
Simon Glass7617f492022-04-24 23:31:11 -060098 struct bootflow bflow;
Simon Glass73fcf512022-07-30 15:52:25 -060099 bool all = false, boot = false, errors = false, no_global = false;
Simon Glassa21e7752023-01-17 10:48:06 -0700100 bool list = false, no_hunter = false;
Simon Glass7617f492022-04-24 23:31:11 -0600101 int num_valid = 0;
Simon Glassba3d5372023-01-17 10:48:15 -0700102 const char *label = NULL;
Simon Glass7617f492022-04-24 23:31:11 -0600103 bool has_args;
104 int ret, i;
105 int flags;
106
107 ret = bootstd_get_priv(&std);
108 if (ret)
109 return CMD_RET_FAILURE;
Simon Glass7617f492022-04-24 23:31:11 -0600110
111 has_args = argc > 1 && *argv[1] == '-';
112 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL)) {
113 if (has_args) {
114 all = strchr(argv[1], 'a');
115 boot = strchr(argv[1], 'b');
116 errors = strchr(argv[1], 'e');
Simon Glass73fcf512022-07-30 15:52:25 -0600117 no_global = strchr(argv[1], 'G');
Simon Glass7617f492022-04-24 23:31:11 -0600118 list = strchr(argv[1], 'l');
Simon Glassa21e7752023-01-17 10:48:06 -0700119 no_hunter = strchr(argv[1], 'H');
Simon Glass7617f492022-04-24 23:31:11 -0600120 argc--;
121 argv++;
122 }
Simon Glassba3d5372023-01-17 10:48:15 -0700123 if (argc > 1)
124 label = argv[1];
125 if (!label)
126 dev = std->cur_bootdev;
Simon Glass7617f492022-04-24 23:31:11 -0600127 } else {
128 if (has_args) {
Simon Glass2d5a8e12023-05-06 08:27:09 -0600129 printf("Flags not supported: enable CONFIG_BOOTSTD_FULL\n");
Simon Glass7617f492022-04-24 23:31:11 -0600130 return CMD_RET_USAGE;
131 }
132 boot = true;
133 }
134
135 std->cur_bootflow = NULL;
136
137 flags = 0;
138 if (list)
Simon Glass99e68182023-02-22 12:17:03 -0700139 flags |= BOOTFLOWIF_SHOW;
Simon Glass7617f492022-04-24 23:31:11 -0600140 if (all)
Simon Glass99e68182023-02-22 12:17:03 -0700141 flags |= BOOTFLOWIF_ALL;
Simon Glass73fcf512022-07-30 15:52:25 -0600142 if (no_global)
Simon Glass99e68182023-02-22 12:17:03 -0700143 flags |= BOOTFLOWIF_SKIP_GLOBAL;
Simon Glassa21e7752023-01-17 10:48:06 -0700144 if (!no_hunter)
Simon Glass99e68182023-02-22 12:17:03 -0700145 flags |= BOOTFLOWIF_HUNT;
Simon Glass7617f492022-04-24 23:31:11 -0600146
147 /*
148 * If we have a device, just scan for bootflows attached to that device
149 */
Simon Glassba3d5372023-01-17 10:48:15 -0700150 if (list) {
151 printf("Scanning for bootflows ");
152 if (dev)
153 printf("in bootdev '%s'\n", dev->name);
154 else if (label)
155 printf("with label '%s'\n", label);
156 else
157 printf("in all bootdevs\n");
158 show_header();
159 }
160 if (dev)
Simon Glass7617f492022-04-24 23:31:11 -0600161 bootdev_clear_bootflows(dev);
Simon Glassba3d5372023-01-17 10:48:15 -0700162 else
Simon Glass7617f492022-04-24 23:31:11 -0600163 bootstd_clear_glob();
Simon Glassba3d5372023-01-17 10:48:15 -0700164 for (i = 0,
Simon Glass5d3d44f2023-01-17 10:48:16 -0700165 ret = bootflow_scan_first(dev, label, &iter, flags, &bflow);
Simon Glassba3d5372023-01-17 10:48:15 -0700166 i < 1000 && ret != -ENODEV;
167 i++, ret = bootflow_scan_next(&iter, &bflow)) {
168 bflow.err = ret;
169 if (!ret)
170 num_valid++;
171 ret = bootdev_add_bootflow(&bflow);
172 if (ret) {
173 printf("Out of memory\n");
174 return CMD_RET_FAILURE;
Simon Glass7617f492022-04-24 23:31:11 -0600175 }
Simon Glassba3d5372023-01-17 10:48:15 -0700176 if (list)
177 show_bootflow(i, &bflow, errors);
178 if (boot && !bflow.err)
179 bootflow_run_boot(&iter, &bflow);
Simon Glass7617f492022-04-24 23:31:11 -0600180 }
181 bootflow_iter_uninit(&iter);
182 if (list)
183 show_footer(i, num_valid);
184
Simon Glass8f97c672023-04-24 13:49:48 +1200185 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL) && !num_valid && !list)
186 printf("No bootflows found; try again with -l\n");
187
Simon Glass7617f492022-04-24 23:31:11 -0600188 return 0;
189}
190
191#ifdef CONFIG_CMD_BOOTFLOW_FULL
192static int do_bootflow_list(struct cmd_tbl *cmdtp, int flag, int argc,
193 char *const argv[])
194{
195 struct bootstd_priv *std;
196 struct udevice *dev;
197 struct bootflow *bflow;
198 int num_valid = 0;
199 bool errors = false;
200 int ret, i;
201
202 if (argc > 1 && *argv[1] == '-')
203 errors = strchr(argv[1], 'e');
204
205 ret = bootstd_get_priv(&std);
206 if (ret)
207 return CMD_RET_FAILURE;
208 dev = std->cur_bootdev;
209
210 /* If we have a device, just list bootflows attached to that device */
211 if (dev) {
212 printf("Showing bootflows for bootdev '%s'\n", dev->name);
213 show_header();
214 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
215 !ret;
216 ret = bootdev_next_bootflow(&bflow), i++) {
217 num_valid += bflow->state == BOOTFLOWST_READY;
218 show_bootflow(i, bflow, errors);
219 }
220 } else {
221 printf("Showing all bootflows\n");
222 show_header();
223 for (ret = bootflow_first_glob(&bflow), i = 0;
224 !ret;
225 ret = bootflow_next_glob(&bflow), i++) {
226 num_valid += bflow->state == BOOTFLOWST_READY;
227 show_bootflow(i, bflow, errors);
228 }
229 }
230 show_footer(i, num_valid);
231
232 return 0;
233}
234
235static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc,
236 char *const argv[])
237{
238 struct bootstd_priv *std;
239 struct bootflow *bflow, *found;
240 struct udevice *dev;
241 const char *name;
242 char *endp;
243 int seq, i;
244 int ret;
245
246 ret = bootstd_get_priv(&std);
247 if (ret)
248 return CMD_RET_FAILURE;
249;
250 if (argc < 2) {
251 std->cur_bootflow = NULL;
252 return 0;
253 }
254 dev = std->cur_bootdev;
255
256 name = argv[1];
257 seq = simple_strtol(name, &endp, 16);
258 found = NULL;
259
260 /*
261 * If we have a bootdev device, only allow selection of bootflows
262 * attached to that device
263 */
264 if (dev) {
265 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
266 !ret;
267 ret = bootdev_next_bootflow(&bflow), i++) {
268 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
269 found = bflow;
270 break;
271 }
272 }
273 } else {
274 for (ret = bootflow_first_glob(&bflow), i = 0;
275 !ret;
276 ret = bootflow_next_glob(&bflow), i++) {
277 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
278 found = bflow;
279 break;
280 }
281 }
282 }
283
284 if (!found) {
285 printf("Cannot find bootflow '%s' ", name);
286 if (dev)
287 printf("in bootdev '%s' ", dev->name);
288 printf("(err=%d)\n", ret);
289 return CMD_RET_FAILURE;
290 }
291 std->cur_bootflow = found;
Simon Glassb35513a2023-07-12 09:04:35 -0600292 if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
293 if (env_set("bootargs", found->cmdline)) {
294 printf("Cannot set bootargs\n");
295 return CMD_RET_FAILURE;
296 }
297 }
Simon Glass7617f492022-04-24 23:31:11 -0600298
299 return 0;
300}
301
302static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc,
303 char *const argv[])
304{
305 struct bootstd_priv *std;
306 struct bootflow *bflow;
Simon Glass5495aaf2023-07-30 11:17:00 -0600307 bool x86_setup = false;
Simon Glass7617f492022-04-24 23:31:11 -0600308 bool dump = false;
309 int ret;
310
Simon Glass5495aaf2023-07-30 11:17:00 -0600311 if (argc > 1 && *argv[1] == '-') {
Simon Glass7617f492022-04-24 23:31:11 -0600312 dump = strchr(argv[1], 'd');
Simon Glass5495aaf2023-07-30 11:17:00 -0600313 x86_setup = strchr(argv[1], 's');
314 }
Simon Glass7617f492022-04-24 23:31:11 -0600315
316 ret = bootstd_get_priv(&std);
317 if (ret)
318 return CMD_RET_FAILURE;
319
320 if (!std->cur_bootflow) {
321 printf("No bootflow selected\n");
322 return CMD_RET_FAILURE;
323 }
324 bflow = std->cur_bootflow;
325
Simon Glass5495aaf2023-07-30 11:17:00 -0600326 if (IS_ENABLED(CONFIG_X86) && x86_setup) {
327 zimage_dump(bflow->x86_setup, false);
328
329 return 0;
330 }
331
Simon Glass7617f492022-04-24 23:31:11 -0600332 printf("Name: %s\n", bflow->name);
333 printf("Device: %s\n", bflow->dev->name);
334 printf("Block dev: %s\n", bflow->blk ? bflow->blk->name : "(none)");
335 printf("Method: %s\n", bflow->method->name);
336 printf("State: %s\n", bootflow_state_get_name(bflow->state));
337 printf("Partition: %d\n", bflow->part);
338 printf("Subdir: %s\n", bflow->subdir ? bflow->subdir : "(none)");
339 printf("Filename: %s\n", bflow->fname);
340 printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf));
341 printf("Size: %x (%d bytes)\n", bflow->size, bflow->size);
Simon Glass72b7b192023-01-06 08:52:33 -0600342 printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)");
Simon Glass33927522023-07-12 09:04:34 -0600343 printf("Cmdline: ");
344 if (bflow->cmdline)
345 puts(bflow->cmdline);
346 else
347 puts("(none)");
348 putc('\n');
Simon Glass63398b02023-07-12 09:04:36 -0600349 if (bflow->x86_setup)
350 printf("X86 setup: %p\n", bflow->x86_setup);
Simon Glass612b9cc2023-01-06 08:52:34 -0600351 printf("Logo: %s\n", bflow->logo ?
352 simple_xtoa((ulong)map_to_sysmem(bflow->logo)) : "(none)");
353 if (bflow->logo) {
354 printf("Logo size: %x (%d bytes)\n", bflow->logo_size,
355 bflow->logo_size);
356 }
Simon Glass7b8c6342023-01-17 10:47:56 -0700357 printf("FDT: %s\n", bflow->fdt_fname);
358 if (bflow->fdt_fname) {
359 printf("FDT size: %x (%d bytes)\n", bflow->fdt_size,
360 bflow->fdt_size);
361 printf("FDT addr: %lx\n", bflow->fdt_addr);
362 }
Simon Glass7617f492022-04-24 23:31:11 -0600363 printf("Error: %d\n", bflow->err);
364 if (dump && bflow->buf) {
365 /* Set some sort of maximum on the size */
366 int size = min(bflow->size, 10 << 10);
367 int i;
368
369 printf("Contents:\n\n");
370 for (i = 0; i < size; i++) {
371 putc(bflow->buf[i]);
372 if (!(i % 128) && ctrlc()) {
373 printf("...interrupted\n");
374 break;
375 }
376 }
377 }
378
379 return 0;
380}
381
382static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc,
383 char *const argv[])
384{
385 struct bootstd_priv *std;
386 struct bootflow *bflow;
387 int ret;
388
389 ret = bootstd_get_priv(&std);
390 if (ret)
391 return CMD_RET_FAILURE;
392
393 /*
394 * Require a current bootflow. Users can use 'bootflow scan -b' to
395 * automatically scan and boot, if needed.
396 */
397 if (!std->cur_bootflow) {
398 printf("No bootflow selected\n");
399 return CMD_RET_FAILURE;
400 }
401 bflow = std->cur_bootflow;
402 ret = bootflow_run_boot(NULL, bflow);
403 if (ret)
404 return CMD_RET_FAILURE;
405
406 return 0;
407}
Simon Glass0a2f6a32023-01-06 08:52:40 -0600408
409static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc,
410 char *const argv[])
411{
412 struct bootstd_priv *std;
413 struct bootflow *bflow;
414 bool text_mode = false;
415 int ret;
416
Tom Rinif2d51362023-04-06 10:03:33 -0400417 if (!IS_ENABLED(CONFIG_EXPO)) {
418 printf("Menu not supported\n");
419 return CMD_RET_FAILURE;
420 }
421
Simon Glass0a2f6a32023-01-06 08:52:40 -0600422 if (argc > 1 && *argv[1] == '-')
423 text_mode = strchr(argv[1], 't');
424
425 ret = bootstd_get_priv(&std);
426 if (ret)
427 return CMD_RET_FAILURE;
428
Tom Rinif2d51362023-04-06 10:03:33 -0400429 ret = bootflow_menu_run(std, text_mode, &bflow);
430 if (ret) {
431 if (ret == -EAGAIN)
432 printf("Nothing chosen\n");
433 else {
434 printf("Menu failed (err=%d)\n", ret);
435 return CMD_RET_FAILURE;
Simon Glass389a4582023-01-28 15:00:18 -0700436 }
Simon Glass0a2f6a32023-01-06 08:52:40 -0600437 }
438
439 printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name);
440 std->cur_bootflow = bflow;
441
442 return 0;
443}
Simon Glass55a2da32023-07-12 09:04:39 -0600444
445static int do_bootflow_cmdline(struct cmd_tbl *cmdtp, int flag, int argc,
446 char *const argv[])
447{
448 struct bootstd_priv *std;
449 struct bootflow *bflow;
450 const char *op, *arg, *val = NULL;
451 int ret;
452
453 if (argc < 3)
454 return CMD_RET_USAGE;
455
456 ret = bootstd_get_priv(&std);
457 if (ret)
458 return CMD_RET_FAILURE;
459
460 bflow = std->cur_bootflow;
461 if (!bflow) {
462 printf("No bootflow selected\n");
463 return CMD_RET_FAILURE;
464 }
465
466 op = argv[1];
467 arg = argv[2];
468 if (*op == 's') {
469 if (argc < 4)
470 return CMD_RET_USAGE;
471 val = argv[3];
472 }
473
474 switch (*op) {
475 case 'c': /* clear */
476 val = "";
477 fallthrough;
478 case 's': /* set */
479 case 'd': /* delete */
480 ret = bootflow_cmdline_set_arg(bflow, arg, val, true);
481 break;
482 case 'g': /* get */
483 ret = bootflow_cmdline_get_arg(bflow, arg, &val);
484 if (ret >= 0)
485 printf("%.*s\n", ret, val);
486 break;
Simon Glasscd91e992023-07-12 09:04:42 -0600487 case 'a': /* auto */
488 ret = bootflow_cmdline_auto(bflow, arg);
489 break;
Simon Glass55a2da32023-07-12 09:04:39 -0600490 }
491 switch (ret) {
492 case -E2BIG:
493 printf("Argument too long\n");
494 break;
495 case -ENOENT:
496 printf("Argument not found\n");
497 break;
498 case -EINVAL:
499 printf("Mismatched quotes\n");
500 break;
501 case -EBADF:
502 printf("Value must be quoted\n");
503 break;
504 default:
505 if (ret < 0)
506 printf("Unknown error: %dE\n", ret);
507 }
508 if (ret < 0)
509 return CMD_RET_FAILURE;
510
511 return 0;
512}
Simon Glass7617f492022-04-24 23:31:11 -0600513#endif /* CONFIG_CMD_BOOTFLOW_FULL */
514
515#ifdef CONFIG_SYS_LONGHELP
516static char bootflow_help_text[] =
517#ifdef CONFIG_CMD_BOOTFLOW_FULL
Simon Glass73fcf512022-07-30 15:52:25 -0600518 "scan [-abeGl] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot, -G no global)\n"
Simon Glass7617f492022-04-24 23:31:11 -0600519 "bootflow list [-e] - list scanned bootflows (-e errors)\n"
520 "bootflow select [<num>|<name>] - select a bootflow\n"
Simon Glass5495aaf2023-07-30 11:17:00 -0600521 "bootflow info [-ds] - show info on current bootflow (-d dump bootflow)\n"
Simon Glass0a2f6a32023-01-06 08:52:40 -0600522 "bootflow boot - boot current bootflow (or first available if none selected)\n"
Simon Glass55a2da32023-07-12 09:04:39 -0600523 "bootflow menu [-t] - show a menu of available bootflows\n"
Simon Glasscd91e992023-07-12 09:04:42 -0600524 "bootflow cmdline [set|get|clear|delete|auto] <param> [<value>] - update cmdline";
Simon Glass7617f492022-04-24 23:31:11 -0600525#else
526 "scan - boot first available bootflow\n";
527#endif
528#endif /* CONFIG_SYS_LONGHELP */
529
530U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text,
531 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_bootflow_scan),
532#ifdef CONFIG_CMD_BOOTFLOW_FULL
533 U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list),
534 U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select),
535 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info),
Simon Glass0a2f6a32023-01-06 08:52:40 -0600536 U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot),
537 U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu),
Simon Glass55a2da32023-07-12 09:04:39 -0600538 U_BOOT_SUBCMD_MKENT(cmdline, 4, 1, do_bootflow_cmdline),
Simon Glass7617f492022-04-24 23:31:11 -0600539#endif
540);