blob: 56dd35b69cf49be6064db266e4336a0924e769ba [file] [log] [blame]
Simon Glass2d653f62022-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>
12#include <bootstd.h>
13#include <command.h>
14#include <console.h>
15#include <dm.h>
16#include <mapmem.h>
17
18/**
19 * report_bootflow_err() - Report where a bootflow failed
20 *
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
23 *
24 * @bflow: Bootflow to process
25 * @err: Error code (0 if none)
26 */
27static void report_bootflow_err(struct bootflow *bflow, int err)
28{
29 if (!err)
30 return;
31
32 /* Indent out to 'Method' */
33 printf(" ** ");
34
35 switch (bflow->state) {
36 case BOOTFLOWST_BASE:
37 printf("No media/partition found");
38 break;
39 case BOOTFLOWST_MEDIA:
40 printf("No partition found");
41 break;
42 case BOOTFLOWST_PART:
43 printf("No filesystem found");
44 break;
45 case BOOTFLOWST_FS:
46 printf("File not found");
47 break;
48 case BOOTFLOWST_FILE:
49 printf("File cannot be loaded");
50 break;
51 case BOOTFLOWST_READY:
52 printf("Ready");
53 break;
54 case BOOTFLOWST_COUNT:
55 break;
56 }
57
58 printf(", err=%d\n", err);
59}
60
61/**
62 * show_bootflow() - Show the status of a bootflow
63 *
64 * @seq: Bootflow index
65 * @bflow: Bootflow to show
66 * @errors: True to show the error received, if any
67 */
68static void show_bootflow(int index, struct bootflow *bflow, bool errors)
69{
70 printf("%3x %-11s %-6s %-9.9s %4x %-25.25s %s\n", index,
71 bflow->method->name, bootflow_state_get_name(bflow->state),
Simon Glasseccb25c2022-07-30 15:52:23 -060072 bflow->dev ? dev_get_uclass_name(dev_get_parent(bflow->dev)) :
73 "(none)", bflow->part, bflow->name, bflow->fname);
Simon Glass2d653f62022-04-24 23:31:11 -060074 if (errors)
75 report_bootflow_err(bflow, bflow->err);
76}
77
78static void show_header(void)
79{
80 printf("Seq Method State Uclass Part Name Filename\n");
81 printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
82}
83
84static void show_footer(int count, int num_valid)
85{
86 printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n");
87 printf("(%d bootflow%s, %d valid)\n", count, count != 1 ? "s" : "",
88 num_valid);
89}
90
91static int do_bootflow_scan(struct cmd_tbl *cmdtp, int flag, int argc,
92 char *const argv[])
93{
94 struct bootstd_priv *std;
95 struct bootflow_iter iter;
96 struct udevice *dev;
97 struct bootflow bflow;
Simon Glass2b80bc12022-07-30 15:52:25 -060098 bool all = false, boot = false, errors = false, no_global = false;
99 bool list = false;
Simon Glass2d653f62022-04-24 23:31:11 -0600100 int num_valid = 0;
101 bool has_args;
102 int ret, i;
103 int flags;
104
105 ret = bootstd_get_priv(&std);
106 if (ret)
107 return CMD_RET_FAILURE;
108 dev = std->cur_bootdev;
109
110 has_args = argc > 1 && *argv[1] == '-';
111 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL)) {
112 if (has_args) {
113 all = strchr(argv[1], 'a');
114 boot = strchr(argv[1], 'b');
115 errors = strchr(argv[1], 'e');
Simon Glass2b80bc12022-07-30 15:52:25 -0600116 no_global = strchr(argv[1], 'G');
Simon Glass2d653f62022-04-24 23:31:11 -0600117 list = strchr(argv[1], 'l');
118 argc--;
119 argv++;
120 }
121 if (argc > 1) {
122 const char *label = argv[1];
123
124 if (bootdev_find_by_any(label, &dev))
125 return CMD_RET_FAILURE;
126 }
127 } else {
128 if (has_args) {
129 printf("Flags not supported: enable CONFIG_BOOTFLOW_FULL\n");
130 return CMD_RET_USAGE;
131 }
132 boot = true;
133 }
134
135 std->cur_bootflow = NULL;
136
137 flags = 0;
138 if (list)
139 flags |= BOOTFLOWF_SHOW;
140 if (all)
141 flags |= BOOTFLOWF_ALL;
Simon Glass2b80bc12022-07-30 15:52:25 -0600142 if (no_global)
143 flags |= BOOTFLOWF_SKIP_GLOBAL;
Simon Glass2d653f62022-04-24 23:31:11 -0600144
145 /*
146 * If we have a device, just scan for bootflows attached to that device
147 */
148 if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL) && dev) {
149 if (list) {
150 printf("Scanning for bootflows in bootdev '%s'\n",
151 dev->name);
152 show_header();
153 }
154 bootdev_clear_bootflows(dev);
155 for (i = 0,
156 ret = bootflow_scan_bootdev(dev, &iter, flags, &bflow);
157 i < 1000 && ret != -ENODEV;
158 i++, ret = bootflow_scan_next(&iter, &bflow)) {
159 bflow.err = ret;
160 if (!ret)
161 num_valid++;
162 ret = bootdev_add_bootflow(&bflow);
163 if (ret) {
164 printf("Out of memory\n");
165 return CMD_RET_FAILURE;
166 }
167 if (list)
168 show_bootflow(i, &bflow, errors);
169 if (boot && !bflow.err)
170 bootflow_run_boot(&iter, &bflow);
171 }
172 } else {
173 if (list) {
174 printf("Scanning for bootflows in all bootdevs\n");
175 show_header();
176 }
177 bootstd_clear_glob();
178
179 for (i = 0,
180 ret = bootflow_scan_first(&iter, flags, &bflow);
181 i < 1000 && ret != -ENODEV;
182 i++, ret = bootflow_scan_next(&iter, &bflow)) {
183 bflow.err = ret;
184 if (!ret)
185 num_valid++;
186 ret = bootdev_add_bootflow(&bflow);
187 if (ret) {
188 printf("Out of memory\n");
189 return CMD_RET_FAILURE;
190 }
191 if (list)
192 show_bootflow(i, &bflow, errors);
193 if (boot && !bflow.err)
194 bootflow_run_boot(&iter, &bflow);
195 }
196 }
197 bootflow_iter_uninit(&iter);
198 if (list)
199 show_footer(i, num_valid);
200
201 return 0;
202}
203
204#ifdef CONFIG_CMD_BOOTFLOW_FULL
205static int do_bootflow_list(struct cmd_tbl *cmdtp, int flag, int argc,
206 char *const argv[])
207{
208 struct bootstd_priv *std;
209 struct udevice *dev;
210 struct bootflow *bflow;
211 int num_valid = 0;
212 bool errors = false;
213 int ret, i;
214
215 if (argc > 1 && *argv[1] == '-')
216 errors = strchr(argv[1], 'e');
217
218 ret = bootstd_get_priv(&std);
219 if (ret)
220 return CMD_RET_FAILURE;
221 dev = std->cur_bootdev;
222
223 /* If we have a device, just list bootflows attached to that device */
224 if (dev) {
225 printf("Showing bootflows for bootdev '%s'\n", dev->name);
226 show_header();
227 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
228 !ret;
229 ret = bootdev_next_bootflow(&bflow), i++) {
230 num_valid += bflow->state == BOOTFLOWST_READY;
231 show_bootflow(i, bflow, errors);
232 }
233 } else {
234 printf("Showing all bootflows\n");
235 show_header();
236 for (ret = bootflow_first_glob(&bflow), i = 0;
237 !ret;
238 ret = bootflow_next_glob(&bflow), i++) {
239 num_valid += bflow->state == BOOTFLOWST_READY;
240 show_bootflow(i, bflow, errors);
241 }
242 }
243 show_footer(i, num_valid);
244
245 return 0;
246}
247
248static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc,
249 char *const argv[])
250{
251 struct bootstd_priv *std;
252 struct bootflow *bflow, *found;
253 struct udevice *dev;
254 const char *name;
255 char *endp;
256 int seq, i;
257 int ret;
258
259 ret = bootstd_get_priv(&std);
260 if (ret)
261 return CMD_RET_FAILURE;
262;
263 if (argc < 2) {
264 std->cur_bootflow = NULL;
265 return 0;
266 }
267 dev = std->cur_bootdev;
268
269 name = argv[1];
270 seq = simple_strtol(name, &endp, 16);
271 found = NULL;
272
273 /*
274 * If we have a bootdev device, only allow selection of bootflows
275 * attached to that device
276 */
277 if (dev) {
278 for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
279 !ret;
280 ret = bootdev_next_bootflow(&bflow), i++) {
281 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
282 found = bflow;
283 break;
284 }
285 }
286 } else {
287 for (ret = bootflow_first_glob(&bflow), i = 0;
288 !ret;
289 ret = bootflow_next_glob(&bflow), i++) {
290 if (*endp ? !strcmp(bflow->name, name) : i == seq) {
291 found = bflow;
292 break;
293 }
294 }
295 }
296
297 if (!found) {
298 printf("Cannot find bootflow '%s' ", name);
299 if (dev)
300 printf("in bootdev '%s' ", dev->name);
301 printf("(err=%d)\n", ret);
302 return CMD_RET_FAILURE;
303 }
304 std->cur_bootflow = found;
305
306 return 0;
307}
308
309static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc,
310 char *const argv[])
311{
312 struct bootstd_priv *std;
313 struct bootflow *bflow;
314 bool dump = false;
315 int ret;
316
317 if (argc > 1 && *argv[1] == '-')
318 dump = strchr(argv[1], 'd');
319
320 ret = bootstd_get_priv(&std);
321 if (ret)
322 return CMD_RET_FAILURE;
323
324 if (!std->cur_bootflow) {
325 printf("No bootflow selected\n");
326 return CMD_RET_FAILURE;
327 }
328 bflow = std->cur_bootflow;
329
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);
Simon Glass2175e762023-01-06 08:52:33 -0600340 printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)");
Simon Glass24d8e1b2023-01-06 08:52:34 -0600341 printf("Logo: %s\n", bflow->logo ?
342 simple_xtoa((ulong)map_to_sysmem(bflow->logo)) : "(none)");
343 if (bflow->logo) {
344 printf("Logo size: %x (%d bytes)\n", bflow->logo_size,
345 bflow->logo_size);
346 }
Simon Glass7638c852023-01-17 10:47:56 -0700347 printf("FDT: %s\n", bflow->fdt_fname);
348 if (bflow->fdt_fname) {
349 printf("FDT size: %x (%d bytes)\n", bflow->fdt_size,
350 bflow->fdt_size);
351 printf("FDT addr: %lx\n", bflow->fdt_addr);
352 }
Simon Glass2d653f62022-04-24 23:31:11 -0600353 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);
357 int i;
358
359 printf("Contents:\n\n");
360 for (i = 0; i < size; i++) {
361 putc(bflow->buf[i]);
362 if (!(i % 128) && ctrlc()) {
363 printf("...interrupted\n");
364 break;
365 }
366 }
367 }
368
369 return 0;
370}
371
372static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc,
373 char *const argv[])
374{
375 struct bootstd_priv *std;
376 struct bootflow *bflow;
377 int ret;
378
379 ret = bootstd_get_priv(&std);
380 if (ret)
381 return CMD_RET_FAILURE;
382
383 /*
384 * Require a current bootflow. Users can use 'bootflow scan -b' to
385 * automatically scan and boot, if needed.
386 */
387 if (!std->cur_bootflow) {
388 printf("No bootflow selected\n");
389 return CMD_RET_FAILURE;
390 }
391 bflow = std->cur_bootflow;
392 ret = bootflow_run_boot(NULL, bflow);
393 if (ret)
394 return CMD_RET_FAILURE;
395
396 return 0;
397}
Simon Glass02d929b2023-01-06 08:52:40 -0600398
399static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc,
400 char *const argv[])
401{
402 struct bootstd_priv *std;
403 struct bootflow *bflow;
404 bool text_mode = false;
405 int ret;
406
407 if (argc > 1 && *argv[1] == '-')
408 text_mode = strchr(argv[1], 't');
409
410 ret = bootstd_get_priv(&std);
411 if (ret)
412 return CMD_RET_FAILURE;
413
414 ret = bootflow_menu_run(std, text_mode, &bflow);
415 if (ret) {
416 if (ret == -EAGAIN)
417 printf("Nothing chosen\n");
418 else
419 printf("Menu failed (err=%d)\n", ret);
420
421 return CMD_RET_FAILURE;
422 }
423
424 printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name);
425 std->cur_bootflow = bflow;
426
427 return 0;
428}
Simon Glass2d653f62022-04-24 23:31:11 -0600429#endif /* CONFIG_CMD_BOOTFLOW_FULL */
430
431#ifdef CONFIG_SYS_LONGHELP
432static char bootflow_help_text[] =
433#ifdef CONFIG_CMD_BOOTFLOW_FULL
Simon Glass2b80bc12022-07-30 15:52:25 -0600434 "scan [-abeGl] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot, -G no global)\n"
Simon Glass2d653f62022-04-24 23:31:11 -0600435 "bootflow list [-e] - list scanned bootflows (-e errors)\n"
436 "bootflow select [<num>|<name>] - select a bootflow\n"
437 "bootflow info [-d] - show info on current bootflow (-d dump bootflow)\n"
Simon Glass02d929b2023-01-06 08:52:40 -0600438 "bootflow boot - boot current bootflow (or first available if none selected)\n"
439 "bootflow menu [-t] - show a menu of available bootflows";
Simon Glass2d653f62022-04-24 23:31:11 -0600440#else
441 "scan - boot first available bootflow\n";
442#endif
443#endif /* CONFIG_SYS_LONGHELP */
444
445U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text,
446 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_bootflow_scan),
447#ifdef CONFIG_CMD_BOOTFLOW_FULL
448 U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list),
449 U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select),
450 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info),
Simon Glass02d929b2023-01-06 08:52:40 -0600451 U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot),
452 U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu),
Simon Glass2d653f62022-04-24 23:31:11 -0600453#endif
454);