Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <bootdev.h> |
| 11 | #include <bootflow.h> |
| 12 | #include <bootmeth.h> |
| 13 | #include <bootstd.h> |
| 14 | #include <dm.h> |
| 15 | #include <malloc.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | |
| 19 | /* error codes used to signal running out of things */ |
| 20 | enum { |
| 21 | BF_NO_MORE_PARTS = -ESHUTDOWN, |
| 22 | BF_NO_MORE_DEVICES = -ENODEV, |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * bootflow_state - name for each state |
| 27 | * |
| 28 | * See enum bootflow_state_t for what each of these means |
| 29 | */ |
| 30 | static const char *const bootflow_state[BOOTFLOWST_COUNT] = { |
| 31 | "base", |
| 32 | "media", |
| 33 | "part", |
| 34 | "fs", |
| 35 | "file", |
| 36 | "ready", |
| 37 | }; |
| 38 | |
| 39 | const char *bootflow_state_get_name(enum bootflow_state_t state) |
| 40 | { |
| 41 | /* This doesn't need to be a useful name, since it will never occur */ |
| 42 | if (state < 0 || state >= BOOTFLOWST_COUNT) |
| 43 | return "?"; |
| 44 | |
| 45 | return bootflow_state[state]; |
| 46 | } |
| 47 | |
| 48 | int bootflow_first_glob(struct bootflow **bflowp) |
| 49 | { |
| 50 | struct bootstd_priv *std; |
| 51 | int ret; |
| 52 | |
| 53 | ret = bootstd_get_priv(&std); |
| 54 | if (ret) |
| 55 | return ret; |
| 56 | |
| 57 | if (list_empty(&std->glob_head)) |
| 58 | return -ENOENT; |
| 59 | |
| 60 | *bflowp = list_first_entry(&std->glob_head, struct bootflow, |
| 61 | glob_node); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | int bootflow_next_glob(struct bootflow **bflowp) |
| 67 | { |
| 68 | struct bootstd_priv *std; |
| 69 | struct bootflow *bflow = *bflowp; |
| 70 | int ret; |
| 71 | |
| 72 | ret = bootstd_get_priv(&std); |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | |
| 76 | *bflowp = NULL; |
| 77 | |
| 78 | if (list_is_last(&bflow->glob_node, &std->glob_head)) |
| 79 | return -ENOENT; |
| 80 | |
| 81 | *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | void bootflow_iter_init(struct bootflow_iter *iter, int flags) |
| 87 | { |
| 88 | memset(iter, '\0', sizeof(*iter)); |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 89 | iter->first_glob_method = -1; |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 90 | iter->flags = flags; |
Simon Glass | cb2a5cd | 2023-01-17 10:48:17 -0700 | [diff] [blame] | 91 | |
| 92 | /* remember the first bootdevs we see */ |
| 93 | iter->max_devs = BOOTFLOW_MAX_USED_DEVS; |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void bootflow_iter_uninit(struct bootflow_iter *iter) |
| 97 | { |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 98 | free(iter->method_order); |
| 99 | } |
| 100 | |
| 101 | int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter, |
| 102 | const struct udevice *bmeth) |
| 103 | { |
| 104 | /* We only support disabling the current bootmeth */ |
| 105 | if (bmeth != iter->method || iter->cur_method >= iter->num_methods || |
| 106 | iter->method_order[iter->cur_method] != bmeth) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | memmove(&iter->method_order[iter->cur_method], |
| 110 | &iter->method_order[iter->cur_method + 1], |
| 111 | (iter->num_methods - iter->cur_method - 1) * sizeof(void *)); |
| 112 | |
| 113 | iter->num_methods--; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 118 | /** |
| 119 | * bootflow_iter_set_dev() - switch to the next bootdev when iterating |
| 120 | * |
| 121 | * This sets iter->dev, records the device in the dev_used[] list and shows a |
| 122 | * message if required |
| 123 | * |
| 124 | * @iter: Iterator to update |
| 125 | * @dev: Bootdev to use, or NULL if there are no more |
| 126 | */ |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 127 | static void bootflow_iter_set_dev(struct bootflow_iter *iter, |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 128 | struct udevice *dev, int method_flags) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 129 | { |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 130 | struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method); |
| 131 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 132 | log_debug("iter: Setting dev to %s, flags %x\n", |
| 133 | dev ? dev->name : "(none)", method_flags); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 134 | iter->dev = dev; |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 135 | iter->method_flags = method_flags; |
| 136 | |
Simon Glass | cb2a5cd | 2023-01-17 10:48:17 -0700 | [diff] [blame] | 137 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
| 138 | /* record the device for later */ |
| 139 | if (dev && iter->num_devs < iter->max_devs) |
| 140 | iter->dev_used[iter->num_devs++] = dev; |
| 141 | |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 142 | if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) == |
| 143 | BOOTFLOWIF_SHOW) { |
Simon Glass | cb2a5cd | 2023-01-17 10:48:17 -0700 | [diff] [blame] | 144 | if (dev) |
| 145 | printf("Scanning bootdev '%s':\n", dev->name); |
| 146 | else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && |
| 147 | ucp->flags & BOOTMETHF_GLOBAL) |
| 148 | printf("Scanning global bootmeth '%s':\n", |
| 149 | iter->method->name); |
| 150 | else |
| 151 | printf("No more bootdevs\n"); |
| 152 | } |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * iter_incr() - Move to the next item (method, part, bootdev) |
| 158 | * |
| 159 | * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs |
| 160 | */ |
| 161 | static int iter_incr(struct bootflow_iter *iter) |
| 162 | { |
| 163 | struct udevice *dev; |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 164 | bool inc_dev = true; |
| 165 | bool global; |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 166 | int ret; |
| 167 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 168 | log_debug("entry: err=%d\n", iter->err); |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 169 | global = iter->doing_global; |
| 170 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 171 | if (iter->err == BF_NO_MORE_DEVICES) |
| 172 | return BF_NO_MORE_DEVICES; |
| 173 | |
| 174 | if (iter->err != BF_NO_MORE_PARTS) { |
| 175 | /* Get the next boothmethod */ |
| 176 | if (++iter->cur_method < iter->num_methods) { |
| 177 | iter->method = iter->method_order[iter->cur_method]; |
| 178 | return 0; |
| 179 | } |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * If we have finished scanning the global bootmeths, start the |
| 183 | * normal bootdev scan |
| 184 | */ |
| 185 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) { |
| 186 | iter->num_methods = iter->first_glob_method; |
| 187 | iter->doing_global = false; |
| 188 | |
| 189 | /* |
| 190 | * Don't move to the next dev as we haven't tried this |
| 191 | * one yet! |
| 192 | */ |
| 193 | inc_dev = false; |
| 194 | } |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* No more bootmeths; start at the first one, and... */ |
| 198 | iter->cur_method = 0; |
| 199 | iter->method = iter->method_order[iter->cur_method]; |
| 200 | |
| 201 | if (iter->err != BF_NO_MORE_PARTS) { |
| 202 | /* ...select next partition */ |
| 203 | if (++iter->part <= iter->max_part) |
| 204 | return 0; |
| 205 | } |
| 206 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 207 | /* No more partitions; start at the first one and... */ |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 208 | iter->part = 0; |
| 209 | |
| 210 | /* |
| 211 | * Note: as far as we know, there is no partition table on the next |
| 212 | * bootdev, so set max_part to 0 until we discover otherwise. See |
| 213 | * bootdev_find_in_blk() for where this is set. |
| 214 | */ |
| 215 | iter->max_part = 0; |
| 216 | |
| 217 | /* ...select next bootdev */ |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 218 | if (iter->flags & BOOTFLOWIF_SINGLE_DEV) { |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 219 | ret = -ENOENT; |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 220 | } else { |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 221 | int method_flags; |
| 222 | |
| 223 | ret = 0; |
| 224 | dev = iter->dev; |
| 225 | log_debug("inc_dev=%d\n", inc_dev); |
| 226 | if (!inc_dev) { |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 227 | ret = bootdev_setup_iter(iter, NULL, &dev, |
| 228 | &method_flags); |
| 229 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 230 | (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) { |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 231 | /* Move to the next bootdev in this uclass */ |
| 232 | uclass_find_next_device(&dev); |
| 233 | if (!dev) { |
| 234 | log_debug("finished uclass %s\n", |
| 235 | dev_get_uclass_name(dev)); |
| 236 | ret = -ENODEV; |
| 237 | } |
| 238 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 239 | iter->flags & BOOTFLOWIF_SINGLE_MEDIA) { |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 240 | log_debug("next in single\n"); |
| 241 | method_flags = 0; |
| 242 | do { |
| 243 | /* |
| 244 | * Move to the next bootdev child of this media |
| 245 | * device. This ensures that we cover all the |
| 246 | * available SCSI IDs and LUNs. |
| 247 | */ |
| 248 | device_find_next_child(&dev); |
| 249 | log_debug("- next %s\n", |
| 250 | dev ? dev->name : "(none)"); |
| 251 | } while (dev && device_get_uclass_id(dev) != |
| 252 | UCLASS_BOOTDEV); |
| 253 | if (!dev) { |
| 254 | log_debug("finished uclass %s\n", |
| 255 | dev_get_uclass_name(dev)); |
| 256 | ret = -ENODEV; |
| 257 | } |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 258 | } else { |
| 259 | log_debug("labels %p\n", iter->labels); |
| 260 | if (iter->labels) { |
| 261 | ret = bootdev_next_label(iter, &dev, |
| 262 | &method_flags); |
| 263 | } else { |
| 264 | ret = bootdev_next_prio(iter, &dev); |
| 265 | method_flags = 0; |
| 266 | } |
| 267 | } |
| 268 | log_debug("ret=%d, dev=%p %s\n", ret, dev, |
| 269 | dev ? dev->name : "none"); |
| 270 | if (ret) { |
| 271 | bootflow_iter_set_dev(iter, NULL, 0); |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 272 | } else { |
Simon Glass | b73a829 | 2023-01-28 15:00:19 -0700 | [diff] [blame] | 273 | /* |
| 274 | * Probe the bootdev. This does not probe any attached |
| 275 | * block device, since they are siblings |
| 276 | */ |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 277 | ret = device_probe(dev); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 278 | log_debug("probe %s %d\n", dev->name, ret); |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 279 | if (!log_msg_ret("probe", ret)) |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 280 | bootflow_iter_set_dev(iter, dev, method_flags); |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 281 | } |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* if there are no more bootdevs, give up */ |
| 285 | if (ret) |
| 286 | return log_msg_ret("incr", BF_NO_MORE_DEVICES); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * bootflow_check() - Check if a bootflow can be obtained |
| 293 | * |
| 294 | * @iter: Provides part, bootmeth to use |
| 295 | * @bflow: Bootflow to update on success |
| 296 | * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device, |
| 297 | * BF_NO_MORE_PARTS if there are no more partitions on bootdev |
| 298 | */ |
| 299 | static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow) |
| 300 | { |
| 301 | struct udevice *dev; |
| 302 | int ret; |
| 303 | |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 304 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) { |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 305 | bootflow_iter_set_dev(iter, NULL, 0); |
Simon Glass | 73fcf51 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 306 | ret = bootmeth_get_bootflow(iter->method, bflow); |
| 307 | if (ret) |
| 308 | return log_msg_ret("glob", ret); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 313 | dev = iter->dev; |
| 314 | ret = bootdev_get_bootflow(dev, iter, bflow); |
| 315 | |
| 316 | /* If we got a valid bootflow, return it */ |
| 317 | if (!ret) { |
| 318 | log_debug("Bootdevice '%s' part %d method '%s': Found bootflow\n", |
| 319 | dev->name, iter->part, iter->method->name); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* Unless there is nothing more to try, move to the next device */ |
| 324 | else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { |
| 325 | log_debug("Bootdevice '%s' part %d method '%s': Error %d\n", |
| 326 | dev->name, iter->part, iter->method->name, ret); |
| 327 | /* |
| 328 | * For 'all' we return all bootflows, even |
| 329 | * those with errors |
| 330 | */ |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 331 | if (iter->flags & BOOTFLOWIF_ALL) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 332 | return log_msg_ret("all", ret); |
| 333 | } |
| 334 | if (ret) |
| 335 | return log_msg_ret("check", ret); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Simon Glass | 5d3d44f | 2023-01-17 10:48:16 -0700 | [diff] [blame] | 340 | int bootflow_scan_first(struct udevice *dev, const char *label, |
| 341 | struct bootflow_iter *iter, int flags, |
| 342 | struct bootflow *bflow) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 343 | { |
| 344 | int ret; |
| 345 | |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 346 | if (dev || label) |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 347 | flags |= BOOTFLOWIF_SKIP_GLOBAL; |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 348 | bootflow_iter_init(iter, flags); |
| 349 | |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 350 | /* |
| 351 | * Set up the ordering of bootmeths. This sets iter->doing_global and |
| 352 | * iter->first_glob_method if we are starting with the global bootmeths |
| 353 | */ |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 354 | ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL)); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 355 | if (ret) |
| 356 | return log_msg_ret("obmeth", -ENODEV); |
| 357 | |
| 358 | /* Find the first bootmeth (there must be at least one!) */ |
| 359 | iter->method = iter->method_order[iter->cur_method]; |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 360 | |
| 361 | if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) { |
| 362 | struct udevice *dev = NULL; |
| 363 | int method_flags; |
| 364 | |
Simon Glass | ba3d537 | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 365 | ret = bootdev_setup_iter(iter, label, &dev, &method_flags); |
Simon Glass | 484e407 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 366 | if (ret) |
| 367 | return log_msg_ret("obdev", -ENODEV); |
| 368 | |
| 369 | bootflow_iter_set_dev(iter, dev, method_flags); |
| 370 | } |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 371 | |
| 372 | ret = bootflow_check(iter, bflow); |
| 373 | if (ret) { |
Simon Glass | 3306346 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 374 | log_debug("check - ret=%d\n", ret); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 375 | if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 376 | if (iter->flags & BOOTFLOWIF_ALL) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 377 | return log_msg_ret("all", ret); |
| 378 | } |
| 379 | iter->err = ret; |
| 380 | ret = bootflow_scan_next(iter, bflow); |
| 381 | if (ret) |
| 382 | return log_msg_ret("get", ret); |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 388 | int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow) |
| 389 | { |
| 390 | int ret; |
| 391 | |
| 392 | do { |
| 393 | ret = iter_incr(iter); |
Simon Glass | 3306346 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 394 | log_debug("iter_incr: ret=%d\n", ret); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 395 | if (ret == BF_NO_MORE_DEVICES) |
| 396 | return log_msg_ret("done", ret); |
| 397 | |
| 398 | if (!ret) { |
| 399 | ret = bootflow_check(iter, bflow); |
Simon Glass | 3306346 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 400 | log_debug("check - ret=%d\n", ret); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 401 | if (!ret) |
| 402 | return 0; |
| 403 | iter->err = ret; |
| 404 | if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { |
Simon Glass | 99e6818 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 405 | if (iter->flags & BOOTFLOWIF_ALL) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 406 | return log_msg_ret("all", ret); |
| 407 | } |
| 408 | } else { |
Simon Glass | 3306346 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 409 | log_debug("incr failed, err=%d\n", ret); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 410 | iter->err = ret; |
| 411 | } |
| 412 | |
| 413 | } while (1); |
| 414 | } |
| 415 | |
Simon Glass | 00501fc | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 416 | void bootflow_init(struct bootflow *bflow, struct udevice *bootdev, |
| 417 | struct udevice *meth) |
| 418 | { |
| 419 | memset(bflow, '\0', sizeof(*bflow)); |
| 420 | bflow->dev = bootdev; |
| 421 | bflow->method = meth; |
| 422 | bflow->state = BOOTFLOWST_BASE; |
| 423 | } |
| 424 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 425 | void bootflow_free(struct bootflow *bflow) |
| 426 | { |
| 427 | free(bflow->name); |
| 428 | free(bflow->subdir); |
| 429 | free(bflow->fname); |
| 430 | free(bflow->buf); |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 431 | free(bflow->os_name); |
Simon Glass | 7b8c634 | 2023-01-17 10:47:56 -0700 | [diff] [blame] | 432 | free(bflow->fdt_fname); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void bootflow_remove(struct bootflow *bflow) |
| 436 | { |
Simon Glass | dee4d64 | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 437 | if (bflow->dev) |
| 438 | list_del(&bflow->bm_node); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 439 | list_del(&bflow->glob_node); |
| 440 | |
| 441 | bootflow_free(bflow); |
| 442 | free(bflow); |
| 443 | } |
| 444 | |
| 445 | int bootflow_boot(struct bootflow *bflow) |
| 446 | { |
| 447 | int ret; |
| 448 | |
| 449 | if (bflow->state != BOOTFLOWST_READY) |
| 450 | return log_msg_ret("load", -EPROTO); |
| 451 | |
| 452 | ret = bootmeth_boot(bflow->method, bflow); |
| 453 | if (ret) |
| 454 | return log_msg_ret("boot", ret); |
| 455 | |
| 456 | /* |
| 457 | * internal error, should not get here since we should have booted |
| 458 | * something or returned an error |
| 459 | */ |
| 460 | |
| 461 | return log_msg_ret("end", -EFAULT); |
| 462 | } |
| 463 | |
| 464 | int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow) |
| 465 | { |
| 466 | int ret; |
| 467 | |
| 468 | printf("** Booting bootflow '%s' with %s\n", bflow->name, |
| 469 | bflow->method->name); |
Simon Glass | ca84dc8 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 470 | if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) && |
| 471 | (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT)) |
| 472 | printf("Using prior-stage device tree\n"); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 473 | ret = bootflow_boot(bflow); |
| 474 | if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
| 475 | printf("Boot failed (err=%d)\n", ret); |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | switch (ret) { |
| 480 | case -EPROTO: |
| 481 | printf("Bootflow not loaded (state '%s')\n", |
| 482 | bootflow_state_get_name(bflow->state)); |
| 483 | break; |
| 484 | case -ENOSYS: |
| 485 | printf("Boot method '%s' not supported\n", bflow->method->name); |
| 486 | break; |
| 487 | case -ENOTSUPP: |
| 488 | /* Disable this bootflow for this iteration */ |
| 489 | if (iter) { |
| 490 | int ret2; |
| 491 | |
| 492 | ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method); |
| 493 | if (!ret2) { |
| 494 | printf("Boot method '%s' failed and will not be retried\n", |
| 495 | bflow->method->name); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | break; |
| 500 | default: |
| 501 | printf("Boot failed (err=%d)\n", ret); |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | return ret; |
| 506 | } |
| 507 | |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 508 | int bootflow_iter_check_blk(const struct bootflow_iter *iter) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 509 | { |
| 510 | const struct udevice *media = dev_get_parent(iter->dev); |
| 511 | enum uclass_id id = device_get_uclass_id(media); |
| 512 | |
| 513 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
Simon Glass | 5820c2f | 2023-01-28 15:00:25 -0700 | [diff] [blame] | 514 | if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 515 | return 0; |
| 516 | |
| 517 | return -ENOTSUPP; |
| 518 | } |
| 519 | |
Simon Glass | 215be68 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 520 | int bootflow_iter_check_sf(const struct bootflow_iter *iter) |
| 521 | { |
| 522 | const struct udevice *media = dev_get_parent(iter->dev); |
| 523 | enum uclass_id id = device_get_uclass_id(media); |
| 524 | |
| 525 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
| 526 | if (id == UCLASS_SPI_FLASH) |
| 527 | return 0; |
| 528 | |
| 529 | return -ENOTSUPP; |
| 530 | } |
| 531 | |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 532 | int bootflow_iter_check_net(const struct bootflow_iter *iter) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 533 | { |
| 534 | const struct udevice *media = dev_get_parent(iter->dev); |
| 535 | enum uclass_id id = device_get_uclass_id(media); |
| 536 | |
| 537 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
| 538 | if (id == UCLASS_ETH) |
| 539 | return 0; |
| 540 | |
| 541 | return -ENOTSUPP; |
| 542 | } |
| 543 | |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 544 | int bootflow_iter_check_system(const struct bootflow_iter *iter) |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 545 | { |
| 546 | const struct udevice *media = dev_get_parent(iter->dev); |
| 547 | enum uclass_id id = device_get_uclass_id(media); |
| 548 | |
| 549 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
| 550 | if (id == UCLASS_BOOTSTD) |
| 551 | return 0; |
| 552 | |
| 553 | return -ENOTSUPP; |
| 554 | } |