Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 1 | // 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 | */ |
| 27 | static 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 | */ |
| 68 | static 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 Glass | dee4d64 | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 72 | bflow->dev ? dev_get_uclass_name(dev_get_parent(bflow->dev)) : |
| 73 | "(none)", bflow->part, bflow->name, bflow->fname); |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 74 | if (errors) |
| 75 | report_bootflow_err(bflow, bflow->err); |
| 76 | } |
| 77 | |
| 78 | static void show_header(void) |
| 79 | { |
| 80 | printf("Seq Method State Uclass Part Name Filename\n"); |
| 81 | printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n"); |
| 82 | } |
| 83 | |
| 84 | static 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 | |
| 91 | static 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 Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 98 | bool all = false, boot = false, errors = false, no_global = false; |
| 99 | bool list = false; |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 100 | 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 Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 116 | no_global = strchr(argv[1], 'G'); |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 117 | 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 Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 142 | if (no_global) |
| 143 | flags |= BOOTFLOWF_SKIP_GLOBAL; |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 144 | |
| 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 |
| 205 | static 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 | |
| 248 | static 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 | |
| 309 | static 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 Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 340 | printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)"); |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 341 | 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 Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 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); |
| 351 | int i; |
| 352 | |
| 353 | printf("Contents:\n\n"); |
| 354 | for (i = 0; i < size; i++) { |
| 355 | putc(bflow->buf[i]); |
| 356 | if (!(i % 128) && ctrlc()) { |
| 357 | printf("...interrupted\n"); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 367 | char *const argv[]) |
| 368 | { |
| 369 | struct bootstd_priv *std; |
| 370 | struct bootflow *bflow; |
| 371 | int ret; |
| 372 | |
| 373 | ret = bootstd_get_priv(&std); |
| 374 | if (ret) |
| 375 | return CMD_RET_FAILURE; |
| 376 | |
| 377 | /* |
| 378 | * Require a current bootflow. Users can use 'bootflow scan -b' to |
| 379 | * automatically scan and boot, if needed. |
| 380 | */ |
| 381 | if (!std->cur_bootflow) { |
| 382 | printf("No bootflow selected\n"); |
| 383 | return CMD_RET_FAILURE; |
| 384 | } |
| 385 | bflow = std->cur_bootflow; |
| 386 | ret = bootflow_run_boot(NULL, bflow); |
| 387 | if (ret) |
| 388 | return CMD_RET_FAILURE; |
| 389 | |
| 390 | return 0; |
| 391 | } |
Simon Glass | 0a2f6a3 | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 392 | |
| 393 | static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc, |
| 394 | char *const argv[]) |
| 395 | { |
| 396 | struct bootstd_priv *std; |
| 397 | struct bootflow *bflow; |
| 398 | bool text_mode = false; |
| 399 | int ret; |
| 400 | |
| 401 | if (argc > 1 && *argv[1] == '-') |
| 402 | text_mode = strchr(argv[1], 't'); |
| 403 | |
| 404 | ret = bootstd_get_priv(&std); |
| 405 | if (ret) |
| 406 | return CMD_RET_FAILURE; |
| 407 | |
| 408 | ret = bootflow_menu_run(std, text_mode, &bflow); |
| 409 | if (ret) { |
| 410 | if (ret == -EAGAIN) |
| 411 | printf("Nothing chosen\n"); |
| 412 | else |
| 413 | printf("Menu failed (err=%d)\n", ret); |
| 414 | |
| 415 | return CMD_RET_FAILURE; |
| 416 | } |
| 417 | |
| 418 | printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name); |
| 419 | std->cur_bootflow = bflow; |
| 420 | |
| 421 | return 0; |
| 422 | } |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 423 | #endif /* CONFIG_CMD_BOOTFLOW_FULL */ |
| 424 | |
| 425 | #ifdef CONFIG_SYS_LONGHELP |
| 426 | static char bootflow_help_text[] = |
| 427 | #ifdef CONFIG_CMD_BOOTFLOW_FULL |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 428 | "scan [-abeGl] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot, -G no global)\n" |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 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" |
Simon Glass | 0a2f6a3 | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 432 | "bootflow boot - boot current bootflow (or first available if none selected)\n" |
| 433 | "bootflow menu [-t] - show a menu of available bootflows"; |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 434 | #else |
| 435 | "scan - boot first available bootflow\n"; |
| 436 | #endif |
| 437 | #endif /* CONFIG_SYS_LONGHELP */ |
| 438 | |
| 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), |
Simon Glass | 0a2f6a3 | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 445 | U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot), |
| 446 | U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu), |
Simon Glass | 7617f49 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 447 | #endif |
| 448 | ); |