Simon Glass | 2d653f6 | 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> |
Simon Glass | cbb607d | 2023-07-30 11:17:00 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 13 | #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 | */ |
| 28 | static 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 Glass | c889434 | 2023-05-10 16:34:26 -0600 | [diff] [blame] | 59 | printf(", err=%dE\n", err); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 60 | } |
| 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 | */ |
| 69 | static 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 Glass | eccb25c | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 73 | bflow->dev ? dev_get_uclass_name(dev_get_parent(bflow->dev)) : |
Simon Glass | c3867e2 | 2023-08-24 13:55:39 -0600 | [diff] [blame] | 74 | "(none)", bflow->part, bflow->name, bflow->fname ?: ""); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 75 | if (errors) |
| 76 | report_bootflow_err(bflow, bflow->err); |
| 77 | } |
| 78 | |
| 79 | static void show_header(void) |
| 80 | { |
| 81 | printf("Seq Method State Uclass Part Name Filename\n"); |
| 82 | printf("--- ----------- ------ -------- ---- ------------------------ ----------------\n"); |
| 83 | } |
| 84 | |
| 85 | static 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 | |
Simon Glass | 3fa53b9 | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 92 | /** |
| 93 | * bootflow_handle_menu() - Handle running the menu and updating cur bootflow |
| 94 | * |
| 95 | * This shows the menu, allows the user to select something and then prints |
| 96 | * what happened |
| 97 | * |
| 98 | * @std: bootstd information |
| 99 | * @text_mode: true to run the menu in text mode |
| 100 | * @bflowp: Returns selected bootflow, on success |
| 101 | * Return: 0 on success (a bootflow was selected), -EAGAIN if nothing was |
| 102 | * chosen, other -ve value on other error |
| 103 | */ |
| 104 | __maybe_unused static int bootflow_handle_menu(struct bootstd_priv *std, |
| 105 | bool text_mode, |
| 106 | struct bootflow **bflowp) |
| 107 | { |
| 108 | struct bootflow *bflow; |
| 109 | int ret; |
| 110 | |
| 111 | ret = bootflow_menu_run(std, text_mode, &bflow); |
| 112 | if (ret) { |
| 113 | if (ret == -EAGAIN) { |
| 114 | printf("Nothing chosen\n"); |
| 115 | std->cur_bootflow = NULL; |
| 116 | } else { |
| 117 | printf("Menu failed (err=%d)\n", ret); |
| 118 | } |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name); |
| 124 | std->cur_bootflow = bflow; |
| 125 | *bflowp = bflow; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 130 | static int do_bootflow_scan(struct cmd_tbl *cmdtp, int flag, int argc, |
| 131 | char *const argv[]) |
| 132 | { |
| 133 | struct bootstd_priv *std; |
| 134 | struct bootflow_iter iter; |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 135 | struct udevice *dev = NULL; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 136 | struct bootflow bflow; |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 137 | bool all = false, boot = false, errors = false, no_global = false; |
Simon Glass | d73420e | 2023-01-17 10:48:06 -0700 | [diff] [blame] | 138 | bool list = false, no_hunter = false; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 139 | int num_valid = 0; |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 140 | const char *label = NULL; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 141 | bool has_args; |
| 142 | int ret, i; |
| 143 | int flags; |
| 144 | |
| 145 | ret = bootstd_get_priv(&std); |
| 146 | if (ret) |
| 147 | return CMD_RET_FAILURE; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 148 | |
| 149 | has_args = argc > 1 && *argv[1] == '-'; |
| 150 | if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL)) { |
| 151 | if (has_args) { |
| 152 | all = strchr(argv[1], 'a'); |
| 153 | boot = strchr(argv[1], 'b'); |
| 154 | errors = strchr(argv[1], 'e'); |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 155 | no_global = strchr(argv[1], 'G'); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 156 | list = strchr(argv[1], 'l'); |
Simon Glass | d73420e | 2023-01-17 10:48:06 -0700 | [diff] [blame] | 157 | no_hunter = strchr(argv[1], 'H'); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 158 | argc--; |
| 159 | argv++; |
| 160 | } |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 161 | if (argc > 1) |
| 162 | label = argv[1]; |
| 163 | if (!label) |
| 164 | dev = std->cur_bootdev; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 165 | } else { |
| 166 | if (has_args) { |
Simon Glass | d8d40bc | 2023-05-06 08:27:09 -0600 | [diff] [blame] | 167 | printf("Flags not supported: enable CONFIG_BOOTSTD_FULL\n"); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 168 | return CMD_RET_USAGE; |
| 169 | } |
| 170 | boot = true; |
| 171 | } |
| 172 | |
| 173 | std->cur_bootflow = NULL; |
| 174 | |
| 175 | flags = 0; |
| 176 | if (list) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 177 | flags |= BOOTFLOWIF_SHOW; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 178 | if (all) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 179 | flags |= BOOTFLOWIF_ALL; |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 180 | if (no_global) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 181 | flags |= BOOTFLOWIF_SKIP_GLOBAL; |
Simon Glass | d73420e | 2023-01-17 10:48:06 -0700 | [diff] [blame] | 182 | if (!no_hunter) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 183 | flags |= BOOTFLOWIF_HUNT; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * If we have a device, just scan for bootflows attached to that device |
| 187 | */ |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 188 | if (list) { |
| 189 | printf("Scanning for bootflows "); |
| 190 | if (dev) |
| 191 | printf("in bootdev '%s'\n", dev->name); |
| 192 | else if (label) |
| 193 | printf("with label '%s'\n", label); |
| 194 | else |
| 195 | printf("in all bootdevs\n"); |
| 196 | show_header(); |
| 197 | } |
| 198 | if (dev) |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 199 | bootdev_clear_bootflows(dev); |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 200 | else |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 201 | bootstd_clear_glob(); |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 202 | for (i = 0, |
Simon Glass | 4b7cb05 | 2023-01-17 10:48:16 -0700 | [diff] [blame] | 203 | ret = bootflow_scan_first(dev, label, &iter, flags, &bflow); |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 204 | i < 1000 && ret != -ENODEV; |
| 205 | i++, ret = bootflow_scan_next(&iter, &bflow)) { |
| 206 | bflow.err = ret; |
| 207 | if (!ret) |
| 208 | num_valid++; |
| 209 | ret = bootdev_add_bootflow(&bflow); |
| 210 | if (ret) { |
| 211 | printf("Out of memory\n"); |
| 212 | return CMD_RET_FAILURE; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 213 | } |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 214 | if (list) |
| 215 | show_bootflow(i, &bflow, errors); |
| 216 | if (boot && !bflow.err) |
| 217 | bootflow_run_boot(&iter, &bflow); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 218 | } |
| 219 | bootflow_iter_uninit(&iter); |
| 220 | if (list) |
| 221 | show_footer(i, num_valid); |
| 222 | |
Simon Glass | f9fb57c | 2023-04-24 13:49:48 +1200 | [diff] [blame] | 223 | if (IS_ENABLED(CONFIG_CMD_BOOTFLOW_FULL) && !num_valid && !list) |
| 224 | printf("No bootflows found; try again with -l\n"); |
| 225 | |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | #ifdef CONFIG_CMD_BOOTFLOW_FULL |
| 230 | static int do_bootflow_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 231 | char *const argv[]) |
| 232 | { |
| 233 | struct bootstd_priv *std; |
| 234 | struct udevice *dev; |
| 235 | struct bootflow *bflow; |
| 236 | int num_valid = 0; |
| 237 | bool errors = false; |
| 238 | int ret, i; |
| 239 | |
| 240 | if (argc > 1 && *argv[1] == '-') |
| 241 | errors = strchr(argv[1], 'e'); |
| 242 | |
| 243 | ret = bootstd_get_priv(&std); |
| 244 | if (ret) |
| 245 | return CMD_RET_FAILURE; |
| 246 | dev = std->cur_bootdev; |
| 247 | |
| 248 | /* If we have a device, just list bootflows attached to that device */ |
| 249 | if (dev) { |
| 250 | printf("Showing bootflows for bootdev '%s'\n", dev->name); |
| 251 | show_header(); |
| 252 | for (ret = bootdev_first_bootflow(dev, &bflow), i = 0; |
| 253 | !ret; |
| 254 | ret = bootdev_next_bootflow(&bflow), i++) { |
| 255 | num_valid += bflow->state == BOOTFLOWST_READY; |
| 256 | show_bootflow(i, bflow, errors); |
| 257 | } |
| 258 | } else { |
| 259 | printf("Showing all bootflows\n"); |
| 260 | show_header(); |
| 261 | for (ret = bootflow_first_glob(&bflow), i = 0; |
| 262 | !ret; |
| 263 | ret = bootflow_next_glob(&bflow), i++) { |
| 264 | num_valid += bflow->state == BOOTFLOWST_READY; |
| 265 | show_bootflow(i, bflow, errors); |
| 266 | } |
| 267 | } |
| 268 | show_footer(i, num_valid); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int do_bootflow_select(struct cmd_tbl *cmdtp, int flag, int argc, |
| 274 | char *const argv[]) |
| 275 | { |
| 276 | struct bootstd_priv *std; |
| 277 | struct bootflow *bflow, *found; |
| 278 | struct udevice *dev; |
| 279 | const char *name; |
| 280 | char *endp; |
| 281 | int seq, i; |
| 282 | int ret; |
| 283 | |
| 284 | ret = bootstd_get_priv(&std); |
| 285 | if (ret) |
| 286 | return CMD_RET_FAILURE; |
| 287 | ; |
| 288 | if (argc < 2) { |
| 289 | std->cur_bootflow = NULL; |
| 290 | return 0; |
| 291 | } |
| 292 | dev = std->cur_bootdev; |
| 293 | |
| 294 | name = argv[1]; |
| 295 | seq = simple_strtol(name, &endp, 16); |
| 296 | found = NULL; |
| 297 | |
| 298 | /* |
| 299 | * If we have a bootdev device, only allow selection of bootflows |
| 300 | * attached to that device |
| 301 | */ |
| 302 | if (dev) { |
| 303 | for (ret = bootdev_first_bootflow(dev, &bflow), i = 0; |
| 304 | !ret; |
| 305 | ret = bootdev_next_bootflow(&bflow), i++) { |
| 306 | if (*endp ? !strcmp(bflow->name, name) : i == seq) { |
| 307 | found = bflow; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | } else { |
| 312 | for (ret = bootflow_first_glob(&bflow), i = 0; |
| 313 | !ret; |
| 314 | ret = bootflow_next_glob(&bflow), i++) { |
| 315 | if (*endp ? !strcmp(bflow->name, name) : i == seq) { |
| 316 | found = bflow; |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (!found) { |
| 323 | printf("Cannot find bootflow '%s' ", name); |
| 324 | if (dev) |
| 325 | printf("in bootdev '%s' ", dev->name); |
| 326 | printf("(err=%d)\n", ret); |
| 327 | return CMD_RET_FAILURE; |
| 328 | } |
| 329 | std->cur_bootflow = found; |
Simon Glass | d42243f | 2023-07-12 09:04:35 -0600 | [diff] [blame] | 330 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
| 331 | if (env_set("bootargs", found->cmdline)) { |
| 332 | printf("Cannot set bootargs\n"); |
| 333 | return CMD_RET_FAILURE; |
| 334 | } |
| 335 | } |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 341 | char *const argv[]) |
| 342 | { |
| 343 | struct bootstd_priv *std; |
| 344 | struct bootflow *bflow; |
Simon Glass | cbb607d | 2023-07-30 11:17:00 -0600 | [diff] [blame] | 345 | bool x86_setup = false; |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 346 | bool dump = false; |
| 347 | int ret; |
| 348 | |
Simon Glass | cbb607d | 2023-07-30 11:17:00 -0600 | [diff] [blame] | 349 | if (argc > 1 && *argv[1] == '-') { |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 350 | dump = strchr(argv[1], 'd'); |
Simon Glass | cbb607d | 2023-07-30 11:17:00 -0600 | [diff] [blame] | 351 | x86_setup = strchr(argv[1], 's'); |
| 352 | } |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 353 | |
| 354 | ret = bootstd_get_priv(&std); |
| 355 | if (ret) |
| 356 | return CMD_RET_FAILURE; |
| 357 | |
| 358 | if (!std->cur_bootflow) { |
| 359 | printf("No bootflow selected\n"); |
| 360 | return CMD_RET_FAILURE; |
| 361 | } |
| 362 | bflow = std->cur_bootflow; |
| 363 | |
Simon Glass | cbb607d | 2023-07-30 11:17:00 -0600 | [diff] [blame] | 364 | if (IS_ENABLED(CONFIG_X86) && x86_setup) { |
| 365 | zimage_dump(bflow->x86_setup, false); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 370 | printf("Name: %s\n", bflow->name); |
| 371 | printf("Device: %s\n", bflow->dev->name); |
| 372 | printf("Block dev: %s\n", bflow->blk ? bflow->blk->name : "(none)"); |
| 373 | printf("Method: %s\n", bflow->method->name); |
| 374 | printf("State: %s\n", bootflow_state_get_name(bflow->state)); |
| 375 | printf("Partition: %d\n", bflow->part); |
| 376 | printf("Subdir: %s\n", bflow->subdir ? bflow->subdir : "(none)"); |
| 377 | printf("Filename: %s\n", bflow->fname); |
| 378 | printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf)); |
| 379 | printf("Size: %x (%d bytes)\n", bflow->size, bflow->size); |
Simon Glass | 2175e76 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 380 | printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)"); |
Simon Glass | f4a9165 | 2023-07-12 09:04:34 -0600 | [diff] [blame] | 381 | printf("Cmdline: "); |
| 382 | if (bflow->cmdline) |
| 383 | puts(bflow->cmdline); |
| 384 | else |
| 385 | puts("(none)"); |
| 386 | putc('\n'); |
Simon Glass | 43b6fa9 | 2023-07-12 09:04:36 -0600 | [diff] [blame] | 387 | if (bflow->x86_setup) |
| 388 | printf("X86 setup: %p\n", bflow->x86_setup); |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 389 | printf("Logo: %s\n", bflow->logo ? |
| 390 | simple_xtoa((ulong)map_to_sysmem(bflow->logo)) : "(none)"); |
| 391 | if (bflow->logo) { |
| 392 | printf("Logo size: %x (%d bytes)\n", bflow->logo_size, |
| 393 | bflow->logo_size); |
| 394 | } |
Simon Glass | 7638c85 | 2023-01-17 10:47:56 -0700 | [diff] [blame] | 395 | printf("FDT: %s\n", bflow->fdt_fname); |
| 396 | if (bflow->fdt_fname) { |
| 397 | printf("FDT size: %x (%d bytes)\n", bflow->fdt_size, |
| 398 | bflow->fdt_size); |
| 399 | printf("FDT addr: %lx\n", bflow->fdt_addr); |
| 400 | } |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 401 | printf("Error: %d\n", bflow->err); |
| 402 | if (dump && bflow->buf) { |
| 403 | /* Set some sort of maximum on the size */ |
| 404 | int size = min(bflow->size, 10 << 10); |
| 405 | int i; |
| 406 | |
| 407 | printf("Contents:\n\n"); |
| 408 | for (i = 0; i < size; i++) { |
| 409 | putc(bflow->buf[i]); |
| 410 | if (!(i % 128) && ctrlc()) { |
| 411 | printf("...interrupted\n"); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Simon Glass | c279224 | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 420 | static int do_bootflow_read(struct cmd_tbl *cmdtp, int flag, int argc, |
| 421 | char *const argv[]) |
| 422 | { |
| 423 | struct bootstd_priv *std; |
| 424 | struct bootflow *bflow; |
| 425 | int ret; |
| 426 | |
| 427 | ret = bootstd_get_priv(&std); |
| 428 | if (ret) |
| 429 | return CMD_RET_FAILURE; |
| 430 | |
| 431 | /* |
| 432 | * Require a current bootflow. Users can use 'bootflow scan -b' to |
| 433 | * automatically scan and boot, if needed. |
| 434 | */ |
| 435 | if (!std->cur_bootflow) { |
| 436 | printf("No bootflow selected\n"); |
| 437 | return CMD_RET_FAILURE; |
| 438 | } |
| 439 | bflow = std->cur_bootflow; |
| 440 | ret = bootflow_read_all(bflow); |
| 441 | if (ret) { |
| 442 | printf("Failed: err=%dE\n", ret); |
| 443 | return CMD_RET_FAILURE; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 449 | static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 450 | char *const argv[]) |
| 451 | { |
| 452 | struct bootstd_priv *std; |
| 453 | struct bootflow *bflow; |
| 454 | int ret; |
| 455 | |
| 456 | ret = bootstd_get_priv(&std); |
| 457 | if (ret) |
| 458 | return CMD_RET_FAILURE; |
| 459 | |
| 460 | /* |
| 461 | * Require a current bootflow. Users can use 'bootflow scan -b' to |
| 462 | * automatically scan and boot, if needed. |
| 463 | */ |
| 464 | if (!std->cur_bootflow) { |
| 465 | printf("No bootflow selected\n"); |
| 466 | return CMD_RET_FAILURE; |
| 467 | } |
| 468 | bflow = std->cur_bootflow; |
| 469 | ret = bootflow_run_boot(NULL, bflow); |
| 470 | if (ret) |
| 471 | return CMD_RET_FAILURE; |
| 472 | |
| 473 | return 0; |
| 474 | } |
Simon Glass | 02d929b | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 475 | |
| 476 | static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc, |
| 477 | char *const argv[]) |
| 478 | { |
| 479 | struct bootstd_priv *std; |
| 480 | struct bootflow *bflow; |
| 481 | bool text_mode = false; |
| 482 | int ret; |
| 483 | |
Tom Rini | e0dda26 | 2023-04-06 10:03:33 -0400 | [diff] [blame] | 484 | if (!IS_ENABLED(CONFIG_EXPO)) { |
| 485 | printf("Menu not supported\n"); |
| 486 | return CMD_RET_FAILURE; |
| 487 | } |
| 488 | |
Simon Glass | 02d929b | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 489 | if (argc > 1 && *argv[1] == '-') |
| 490 | text_mode = strchr(argv[1], 't'); |
| 491 | |
| 492 | ret = bootstd_get_priv(&std); |
| 493 | if (ret) |
| 494 | return CMD_RET_FAILURE; |
| 495 | |
Simon Glass | 3fa53b9 | 2023-10-01 19:14:38 -0600 | [diff] [blame] | 496 | ret = bootflow_handle_menu(std, text_mode, &bflow); |
| 497 | if (ret) |
| 498 | return CMD_RET_FAILURE; |
Simon Glass | 02d929b | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
Simon Glass | 82c0938 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 502 | |
| 503 | static int do_bootflow_cmdline(struct cmd_tbl *cmdtp, int flag, int argc, |
| 504 | char *const argv[]) |
| 505 | { |
| 506 | struct bootstd_priv *std; |
| 507 | struct bootflow *bflow; |
| 508 | const char *op, *arg, *val = NULL; |
| 509 | int ret; |
| 510 | |
| 511 | if (argc < 3) |
| 512 | return CMD_RET_USAGE; |
| 513 | |
| 514 | ret = bootstd_get_priv(&std); |
| 515 | if (ret) |
| 516 | return CMD_RET_FAILURE; |
| 517 | |
| 518 | bflow = std->cur_bootflow; |
| 519 | if (!bflow) { |
| 520 | printf("No bootflow selected\n"); |
| 521 | return CMD_RET_FAILURE; |
| 522 | } |
| 523 | |
| 524 | op = argv[1]; |
| 525 | arg = argv[2]; |
| 526 | if (*op == 's') { |
| 527 | if (argc < 4) |
| 528 | return CMD_RET_USAGE; |
| 529 | val = argv[3]; |
| 530 | } |
| 531 | |
| 532 | switch (*op) { |
| 533 | case 'c': /* clear */ |
| 534 | val = ""; |
| 535 | fallthrough; |
| 536 | case 's': /* set */ |
| 537 | case 'd': /* delete */ |
| 538 | ret = bootflow_cmdline_set_arg(bflow, arg, val, true); |
| 539 | break; |
| 540 | case 'g': /* get */ |
| 541 | ret = bootflow_cmdline_get_arg(bflow, arg, &val); |
| 542 | if (ret >= 0) |
| 543 | printf("%.*s\n", ret, val); |
| 544 | break; |
Simon Glass | 33ebcb4 | 2023-07-12 09:04:42 -0600 | [diff] [blame] | 545 | case 'a': /* auto */ |
| 546 | ret = bootflow_cmdline_auto(bflow, arg); |
| 547 | break; |
Simon Glass | 82c0938 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 548 | } |
| 549 | switch (ret) { |
| 550 | case -E2BIG: |
| 551 | printf("Argument too long\n"); |
| 552 | break; |
| 553 | case -ENOENT: |
| 554 | printf("Argument not found\n"); |
| 555 | break; |
| 556 | case -EINVAL: |
| 557 | printf("Mismatched quotes\n"); |
| 558 | break; |
| 559 | case -EBADF: |
| 560 | printf("Value must be quoted\n"); |
| 561 | break; |
| 562 | default: |
| 563 | if (ret < 0) |
| 564 | printf("Unknown error: %dE\n", ret); |
| 565 | } |
| 566 | if (ret < 0) |
| 567 | return CMD_RET_FAILURE; |
| 568 | |
| 569 | return 0; |
| 570 | } |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 571 | #endif /* CONFIG_CMD_BOOTFLOW_FULL */ |
| 572 | |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 573 | U_BOOT_LONGHELP(bootflow, |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 574 | #ifdef CONFIG_CMD_BOOTFLOW_FULL |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 575 | "scan [-abeGl] [bdev] - scan for valid bootflows (-l list, -a all, -e errors, -b boot, -G no global)\n" |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 576 | "bootflow list [-e] - list scanned bootflows (-e errors)\n" |
| 577 | "bootflow select [<num>|<name>] - select a bootflow\n" |
Simon Glass | cbb607d | 2023-07-30 11:17:00 -0600 | [diff] [blame] | 578 | "bootflow info [-ds] - show info on current bootflow (-d dump bootflow)\n" |
Simon Glass | c279224 | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 579 | "bootflow read - read all current-bootflow files\n" |
| 580 | "bootflow boot - boot current bootflow\n" |
Simon Glass | 82c0938 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 581 | "bootflow menu [-t] - show a menu of available bootflows\n" |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 582 | "bootflow cmdline [set|get|clear|delete|auto] <param> [<value>] - update cmdline" |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 583 | #else |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 584 | "scan - boot first available bootflow\n" |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 585 | #endif |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 586 | ); |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 587 | |
| 588 | U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text, |
| 589 | U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_bootflow_scan), |
| 590 | #ifdef CONFIG_CMD_BOOTFLOW_FULL |
| 591 | U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list), |
| 592 | U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select), |
| 593 | U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info), |
Simon Glass | c279224 | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 594 | U_BOOT_SUBCMD_MKENT(read, 1, 1, do_bootflow_read), |
Simon Glass | 02d929b | 2023-01-06 08:52:40 -0600 | [diff] [blame] | 595 | U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot), |
| 596 | U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu), |
Simon Glass | 82c0938 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 597 | U_BOOT_SUBCMD_MKENT(cmdline, 4, 1, do_bootflow_cmdline), |
Simon Glass | 2d653f6 | 2022-04-24 23:31:11 -0600 | [diff] [blame] | 598 | #endif |
| 599 | ); |