Simon Glass | a8f5be1 | 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> |
Simon Glass | d42243f | 2023-07-12 09:04:35 -0600 | [diff] [blame] | 15 | #include <env_internal.h> |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 16 | #include <malloc.h> |
Simon Glass | 33ebcb4 | 2023-07-12 09:04:42 -0600 | [diff] [blame] | 17 | #include <serial.h> |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/uclass-internal.h> |
| 20 | |
| 21 | /* error codes used to signal running out of things */ |
| 22 | enum { |
| 23 | BF_NO_MORE_PARTS = -ESHUTDOWN, |
| 24 | BF_NO_MORE_DEVICES = -ENODEV, |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * bootflow_state - name for each state |
| 29 | * |
| 30 | * See enum bootflow_state_t for what each of these means |
| 31 | */ |
| 32 | static const char *const bootflow_state[BOOTFLOWST_COUNT] = { |
| 33 | "base", |
| 34 | "media", |
| 35 | "part", |
| 36 | "fs", |
| 37 | "file", |
| 38 | "ready", |
| 39 | }; |
| 40 | |
| 41 | const char *bootflow_state_get_name(enum bootflow_state_t state) |
| 42 | { |
| 43 | /* This doesn't need to be a useful name, since it will never occur */ |
| 44 | if (state < 0 || state >= BOOTFLOWST_COUNT) |
| 45 | return "?"; |
| 46 | |
| 47 | return bootflow_state[state]; |
| 48 | } |
| 49 | |
| 50 | int bootflow_first_glob(struct bootflow **bflowp) |
| 51 | { |
| 52 | struct bootstd_priv *std; |
| 53 | int ret; |
| 54 | |
| 55 | ret = bootstd_get_priv(&std); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | if (list_empty(&std->glob_head)) |
| 60 | return -ENOENT; |
| 61 | |
| 62 | *bflowp = list_first_entry(&std->glob_head, struct bootflow, |
| 63 | glob_node); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int bootflow_next_glob(struct bootflow **bflowp) |
| 69 | { |
| 70 | struct bootstd_priv *std; |
| 71 | struct bootflow *bflow = *bflowp; |
| 72 | int ret; |
| 73 | |
| 74 | ret = bootstd_get_priv(&std); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | |
| 78 | *bflowp = NULL; |
| 79 | |
| 80 | if (list_is_last(&bflow->glob_node, &std->glob_head)) |
| 81 | return -ENOENT; |
| 82 | |
| 83 | *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void bootflow_iter_init(struct bootflow_iter *iter, int flags) |
| 89 | { |
| 90 | memset(iter, '\0', sizeof(*iter)); |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 91 | iter->first_glob_method = -1; |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 92 | iter->flags = flags; |
Simon Glass | a950f28 | 2023-01-17 10:48:17 -0700 | [diff] [blame] | 93 | |
| 94 | /* remember the first bootdevs we see */ |
| 95 | iter->max_devs = BOOTFLOW_MAX_USED_DEVS; |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void bootflow_iter_uninit(struct bootflow_iter *iter) |
| 99 | { |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 100 | free(iter->method_order); |
| 101 | } |
| 102 | |
| 103 | int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter, |
| 104 | const struct udevice *bmeth) |
| 105 | { |
| 106 | /* We only support disabling the current bootmeth */ |
| 107 | if (bmeth != iter->method || iter->cur_method >= iter->num_methods || |
| 108 | iter->method_order[iter->cur_method] != bmeth) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | memmove(&iter->method_order[iter->cur_method], |
| 112 | &iter->method_order[iter->cur_method + 1], |
| 113 | (iter->num_methods - iter->cur_method - 1) * sizeof(void *)); |
| 114 | |
| 115 | iter->num_methods--; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 120 | /** |
| 121 | * bootflow_iter_set_dev() - switch to the next bootdev when iterating |
| 122 | * |
| 123 | * This sets iter->dev, records the device in the dev_used[] list and shows a |
| 124 | * message if required |
| 125 | * |
| 126 | * @iter: Iterator to update |
| 127 | * @dev: Bootdev to use, or NULL if there are no more |
| 128 | */ |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 129 | static void bootflow_iter_set_dev(struct bootflow_iter *iter, |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 130 | struct udevice *dev, int method_flags) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 131 | { |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 132 | struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method); |
| 133 | |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 134 | log_debug("iter: Setting dev to %s, flags %x\n", |
| 135 | dev ? dev->name : "(none)", method_flags); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 136 | iter->dev = dev; |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 137 | iter->method_flags = method_flags; |
| 138 | |
Simon Glass | a950f28 | 2023-01-17 10:48:17 -0700 | [diff] [blame] | 139 | if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
| 140 | /* record the device for later */ |
| 141 | if (dev && iter->num_devs < iter->max_devs) |
| 142 | iter->dev_used[iter->num_devs++] = dev; |
| 143 | |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 144 | if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) == |
| 145 | BOOTFLOWIF_SHOW) { |
Simon Glass | a950f28 | 2023-01-17 10:48:17 -0700 | [diff] [blame] | 146 | if (dev) |
| 147 | printf("Scanning bootdev '%s':\n", dev->name); |
| 148 | else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && |
| 149 | ucp->flags & BOOTMETHF_GLOBAL) |
| 150 | printf("Scanning global bootmeth '%s':\n", |
| 151 | iter->method->name); |
| 152 | else |
| 153 | printf("No more bootdevs\n"); |
| 154 | } |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * iter_incr() - Move to the next item (method, part, bootdev) |
| 160 | * |
| 161 | * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs |
| 162 | */ |
| 163 | static int iter_incr(struct bootflow_iter *iter) |
| 164 | { |
| 165 | struct udevice *dev; |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 166 | bool inc_dev = true; |
| 167 | bool global; |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 168 | int ret; |
| 169 | |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 170 | log_debug("entry: err=%d\n", iter->err); |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 171 | global = iter->doing_global; |
| 172 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 173 | if (iter->err == BF_NO_MORE_DEVICES) |
| 174 | return BF_NO_MORE_DEVICES; |
| 175 | |
| 176 | if (iter->err != BF_NO_MORE_PARTS) { |
| 177 | /* Get the next boothmethod */ |
| 178 | if (++iter->cur_method < iter->num_methods) { |
| 179 | iter->method = iter->method_order[iter->cur_method]; |
| 180 | return 0; |
| 181 | } |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * If we have finished scanning the global bootmeths, start the |
| 185 | * normal bootdev scan |
| 186 | */ |
| 187 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) { |
| 188 | iter->num_methods = iter->first_glob_method; |
| 189 | iter->doing_global = false; |
| 190 | |
| 191 | /* |
| 192 | * Don't move to the next dev as we haven't tried this |
| 193 | * one yet! |
| 194 | */ |
| 195 | inc_dev = false; |
| 196 | } |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* No more bootmeths; start at the first one, and... */ |
| 200 | iter->cur_method = 0; |
| 201 | iter->method = iter->method_order[iter->cur_method]; |
| 202 | |
| 203 | if (iter->err != BF_NO_MORE_PARTS) { |
| 204 | /* ...select next partition */ |
| 205 | if (++iter->part <= iter->max_part) |
| 206 | return 0; |
| 207 | } |
| 208 | |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 209 | /* No more partitions; start at the first one and... */ |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 210 | iter->part = 0; |
| 211 | |
| 212 | /* |
| 213 | * Note: as far as we know, there is no partition table on the next |
| 214 | * bootdev, so set max_part to 0 until we discover otherwise. See |
| 215 | * bootdev_find_in_blk() for where this is set. |
| 216 | */ |
| 217 | iter->max_part = 0; |
| 218 | |
| 219 | /* ...select next bootdev */ |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 220 | if (iter->flags & BOOTFLOWIF_SINGLE_DEV) { |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 221 | ret = -ENOENT; |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 222 | } else { |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 223 | int method_flags; |
| 224 | |
| 225 | ret = 0; |
| 226 | dev = iter->dev; |
| 227 | log_debug("inc_dev=%d\n", inc_dev); |
| 228 | if (!inc_dev) { |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 229 | ret = bootdev_setup_iter(iter, NULL, &dev, |
| 230 | &method_flags); |
| 231 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 232 | (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) { |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 233 | /* Move to the next bootdev in this uclass */ |
| 234 | uclass_find_next_device(&dev); |
| 235 | if (!dev) { |
| 236 | log_debug("finished uclass %s\n", |
| 237 | dev_get_uclass_name(dev)); |
| 238 | ret = -ENODEV; |
| 239 | } |
| 240 | } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 241 | iter->flags & BOOTFLOWIF_SINGLE_MEDIA) { |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 242 | log_debug("next in single\n"); |
| 243 | method_flags = 0; |
| 244 | do { |
| 245 | /* |
| 246 | * Move to the next bootdev child of this media |
| 247 | * device. This ensures that we cover all the |
| 248 | * available SCSI IDs and LUNs. |
| 249 | */ |
| 250 | device_find_next_child(&dev); |
| 251 | log_debug("- next %s\n", |
| 252 | dev ? dev->name : "(none)"); |
| 253 | } while (dev && device_get_uclass_id(dev) != |
| 254 | UCLASS_BOOTDEV); |
| 255 | if (!dev) { |
| 256 | log_debug("finished uclass %s\n", |
| 257 | dev_get_uclass_name(dev)); |
| 258 | ret = -ENODEV; |
| 259 | } |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 260 | } else { |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 261 | log_debug("labels %p\n", iter->labels); |
| 262 | if (iter->labels) { |
Simon Glass | e824d0d | 2023-09-23 14:50:15 -0600 | [diff] [blame] | 263 | /* |
| 264 | * when the label is "mmc" we want to scan all |
| 265 | * mmc bootdevs, not just the first. See |
| 266 | * bootdev_find_by_label() where this flag is |
| 267 | * set up |
| 268 | */ |
| 269 | if (iter->method_flags & BOOTFLOW_METHF_SINGLE_UCLASS) { |
| 270 | uclass_next_device(&dev); |
| 271 | log_debug("looking for next device %s: %s\n", |
| 272 | iter->dev->name, |
| 273 | dev ? dev->name : "<none>"); |
| 274 | } else { |
| 275 | dev = NULL; |
| 276 | } |
| 277 | if (!dev) { |
| 278 | log_debug("looking at next label\n"); |
| 279 | ret = bootdev_next_label(iter, &dev, |
| 280 | &method_flags); |
| 281 | } |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 282 | } else { |
| 283 | ret = bootdev_next_prio(iter, &dev); |
| 284 | method_flags = 0; |
| 285 | } |
| 286 | } |
| 287 | log_debug("ret=%d, dev=%p %s\n", ret, dev, |
| 288 | dev ? dev->name : "none"); |
| 289 | if (ret) { |
| 290 | bootflow_iter_set_dev(iter, NULL, 0); |
| 291 | } else { |
Simon Glass | 965020c | 2023-01-28 15:00:19 -0700 | [diff] [blame] | 292 | /* |
| 293 | * Probe the bootdev. This does not probe any attached |
| 294 | * block device, since they are siblings |
| 295 | */ |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 296 | ret = device_probe(dev); |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 297 | log_debug("probe %s %d\n", dev->name, ret); |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 298 | if (!log_msg_ret("probe", ret)) |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 299 | bootflow_iter_set_dev(iter, dev, method_flags); |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 300 | } |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* if there are no more bootdevs, give up */ |
| 304 | if (ret) |
| 305 | return log_msg_ret("incr", BF_NO_MORE_DEVICES); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * bootflow_check() - Check if a bootflow can be obtained |
| 312 | * |
| 313 | * @iter: Provides part, bootmeth to use |
| 314 | * @bflow: Bootflow to update on success |
| 315 | * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device, |
| 316 | * BF_NO_MORE_PARTS if there are no more partitions on bootdev |
| 317 | */ |
| 318 | static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow) |
| 319 | { |
| 320 | struct udevice *dev; |
| 321 | int ret; |
| 322 | |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 323 | if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) { |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 324 | bootflow_iter_set_dev(iter, NULL, 0); |
Simon Glass | 2b80bc1 | 2022-07-30 15:52:25 -0600 | [diff] [blame] | 325 | ret = bootmeth_get_bootflow(iter->method, bflow); |
| 326 | if (ret) |
| 327 | return log_msg_ret("glob", ret); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 332 | dev = iter->dev; |
| 333 | ret = bootdev_get_bootflow(dev, iter, bflow); |
| 334 | |
| 335 | /* If we got a valid bootflow, return it */ |
| 336 | if (!ret) { |
Simon Glass | 4de979f | 2023-07-12 09:04:32 -0600 | [diff] [blame] | 337 | log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n", |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 338 | dev->name, iter->part, iter->method->name); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* Unless there is nothing more to try, move to the next device */ |
| 343 | else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { |
Simon Glass | 4de979f | 2023-07-12 09:04:32 -0600 | [diff] [blame] | 344 | log_debug("Bootdev '%s' part %d method '%s': Error %d\n", |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 345 | dev->name, iter->part, iter->method->name, ret); |
| 346 | /* |
| 347 | * For 'all' we return all bootflows, even |
| 348 | * those with errors |
| 349 | */ |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 350 | if (iter->flags & BOOTFLOWIF_ALL) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 351 | return log_msg_ret("all", ret); |
| 352 | } |
| 353 | if (ret) |
| 354 | return log_msg_ret("check", ret); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Simon Glass | 4b7cb05 | 2023-01-17 10:48:16 -0700 | [diff] [blame] | 359 | int bootflow_scan_first(struct udevice *dev, const char *label, |
| 360 | struct bootflow_iter *iter, int flags, |
| 361 | struct bootflow *bflow) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 362 | { |
| 363 | int ret; |
| 364 | |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 365 | if (dev || label) |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 366 | flags |= BOOTFLOWIF_SKIP_GLOBAL; |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 367 | bootflow_iter_init(iter, flags); |
| 368 | |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Set up the ordering of bootmeths. This sets iter->doing_global and |
| 371 | * iter->first_glob_method if we are starting with the global bootmeths |
| 372 | */ |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 373 | ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL)); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 374 | if (ret) |
| 375 | return log_msg_ret("obmeth", -ENODEV); |
| 376 | |
| 377 | /* Find the first bootmeth (there must be at least one!) */ |
| 378 | iter->method = iter->method_order[iter->cur_method]; |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 379 | |
| 380 | if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) { |
| 381 | struct udevice *dev = NULL; |
| 382 | int method_flags; |
| 383 | |
Simon Glass | 91943ff | 2023-01-17 10:48:15 -0700 | [diff] [blame] | 384 | ret = bootdev_setup_iter(iter, label, &dev, &method_flags); |
Simon Glass | 47aedc2 | 2023-01-17 10:48:14 -0700 | [diff] [blame] | 385 | if (ret) |
| 386 | return log_msg_ret("obdev", -ENODEV); |
| 387 | |
| 388 | bootflow_iter_set_dev(iter, dev, method_flags); |
| 389 | } |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 390 | |
| 391 | ret = bootflow_check(iter, bflow); |
| 392 | if (ret) { |
Simon Glass | f738c73 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 393 | log_debug("check - ret=%d\n", ret); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 394 | if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 395 | if (iter->flags & BOOTFLOWIF_ALL) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 396 | return log_msg_ret("all", ret); |
| 397 | } |
| 398 | iter->err = ret; |
| 399 | ret = bootflow_scan_next(iter, bflow); |
| 400 | if (ret) |
| 401 | return log_msg_ret("get", ret); |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 407 | int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow) |
| 408 | { |
| 409 | int ret; |
| 410 | |
| 411 | do { |
| 412 | ret = iter_incr(iter); |
Simon Glass | f738c73 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 413 | log_debug("iter_incr: ret=%d\n", ret); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 414 | if (ret == BF_NO_MORE_DEVICES) |
| 415 | return log_msg_ret("done", ret); |
| 416 | |
| 417 | if (!ret) { |
| 418 | ret = bootflow_check(iter, bflow); |
Simon Glass | f738c73 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 419 | log_debug("check - ret=%d\n", ret); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 420 | if (!ret) |
| 421 | return 0; |
| 422 | iter->err = ret; |
| 423 | if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) { |
Simon Glass | 4f806f3 | 2023-02-22 12:17:03 -0700 | [diff] [blame] | 424 | if (iter->flags & BOOTFLOWIF_ALL) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 425 | return log_msg_ret("all", ret); |
| 426 | } |
| 427 | } else { |
Simon Glass | f738c73 | 2023-01-17 10:48:18 -0700 | [diff] [blame] | 428 | log_debug("incr failed, err=%d\n", ret); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 429 | iter->err = ret; |
| 430 | } |
| 431 | |
| 432 | } while (1); |
| 433 | } |
| 434 | |
Simon Glass | b190deb | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 435 | void bootflow_init(struct bootflow *bflow, struct udevice *bootdev, |
| 436 | struct udevice *meth) |
| 437 | { |
| 438 | memset(bflow, '\0', sizeof(*bflow)); |
| 439 | bflow->dev = bootdev; |
| 440 | bflow->method = meth; |
| 441 | bflow->state = BOOTFLOWST_BASE; |
| 442 | } |
| 443 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 444 | void bootflow_free(struct bootflow *bflow) |
| 445 | { |
| 446 | free(bflow->name); |
| 447 | free(bflow->subdir); |
| 448 | free(bflow->fname); |
| 449 | free(bflow->buf); |
Simon Glass | 2175e76 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 450 | free(bflow->os_name); |
Simon Glass | 7638c85 | 2023-01-17 10:47:56 -0700 | [diff] [blame] | 451 | free(bflow->fdt_fname); |
Simon Glass | 76bd684 | 2023-07-30 11:16:56 -0600 | [diff] [blame] | 452 | free(bflow->bootmeth_priv); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void bootflow_remove(struct bootflow *bflow) |
| 456 | { |
Simon Glass | eccb25c | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 457 | if (bflow->dev) |
| 458 | list_del(&bflow->bm_node); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 459 | list_del(&bflow->glob_node); |
| 460 | |
| 461 | bootflow_free(bflow); |
| 462 | free(bflow); |
| 463 | } |
| 464 | |
Simon Glass | c279224 | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 465 | #if CONFIG_IS_ENABLED(BOOTSTD_FULL) |
| 466 | int bootflow_read_all(struct bootflow *bflow) |
| 467 | { |
| 468 | int ret; |
| 469 | |
| 470 | if (bflow->state != BOOTFLOWST_READY) |
| 471 | return log_msg_ret("rd", -EPROTO); |
| 472 | |
| 473 | ret = bootmeth_read_all(bflow->method, bflow); |
| 474 | if (ret) |
| 475 | return log_msg_ret("rd2", ret); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | #endif /* BOOTSTD_FULL */ |
| 480 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 481 | int bootflow_boot(struct bootflow *bflow) |
| 482 | { |
| 483 | int ret; |
| 484 | |
| 485 | if (bflow->state != BOOTFLOWST_READY) |
| 486 | return log_msg_ret("load", -EPROTO); |
| 487 | |
| 488 | ret = bootmeth_boot(bflow->method, bflow); |
| 489 | if (ret) |
| 490 | return log_msg_ret("boot", ret); |
| 491 | |
| 492 | /* |
| 493 | * internal error, should not get here since we should have booted |
| 494 | * something or returned an error |
| 495 | */ |
| 496 | |
| 497 | return log_msg_ret("end", -EFAULT); |
| 498 | } |
| 499 | |
| 500 | int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow) |
| 501 | { |
| 502 | int ret; |
| 503 | |
| 504 | printf("** Booting bootflow '%s' with %s\n", bflow->name, |
| 505 | bflow->method->name); |
Simon Glass | 47dd6b4 | 2023-02-22 12:17:04 -0700 | [diff] [blame] | 506 | if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) && |
| 507 | (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT)) |
| 508 | printf("Using prior-stage device tree\n"); |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 509 | ret = bootflow_boot(bflow); |
| 510 | if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) { |
| 511 | printf("Boot failed (err=%d)\n", ret); |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | switch (ret) { |
| 516 | case -EPROTO: |
| 517 | printf("Bootflow not loaded (state '%s')\n", |
| 518 | bootflow_state_get_name(bflow->state)); |
| 519 | break; |
| 520 | case -ENOSYS: |
| 521 | printf("Boot method '%s' not supported\n", bflow->method->name); |
| 522 | break; |
| 523 | case -ENOTSUPP: |
| 524 | /* Disable this bootflow for this iteration */ |
| 525 | if (iter) { |
| 526 | int ret2; |
| 527 | |
| 528 | ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method); |
| 529 | if (!ret2) { |
| 530 | printf("Boot method '%s' failed and will not be retried\n", |
| 531 | bflow->method->name); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | break; |
| 536 | default: |
| 537 | printf("Boot failed (err=%d)\n", ret); |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | return ret; |
| 542 | } |
| 543 | |
Simon Glass | 865328c | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 544 | int bootflow_iter_check_blk(const struct bootflow_iter *iter) |
Simon Glass | a8f5be1 | 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)); |
Simon Glass | 9c6d57d | 2023-01-28 15:00:25 -0700 | [diff] [blame] | 550 | if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 551 | return 0; |
| 552 | |
| 553 | return -ENOTSUPP; |
| 554 | } |
| 555 | |
Simon Glass | 0c1f4a9 | 2023-01-17 10:48:03 -0700 | [diff] [blame] | 556 | int bootflow_iter_check_sf(const struct bootflow_iter *iter) |
| 557 | { |
| 558 | const struct udevice *media = dev_get_parent(iter->dev); |
| 559 | enum uclass_id id = device_get_uclass_id(media); |
| 560 | |
| 561 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
| 562 | if (id == UCLASS_SPI_FLASH) |
| 563 | return 0; |
| 564 | |
| 565 | return -ENOTSUPP; |
| 566 | } |
| 567 | |
Simon Glass | 865328c | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 568 | int bootflow_iter_check_net(const struct bootflow_iter *iter) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 569 | { |
| 570 | const struct udevice *media = dev_get_parent(iter->dev); |
| 571 | enum uclass_id id = device_get_uclass_id(media); |
| 572 | |
| 573 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
| 574 | if (id == UCLASS_ETH) |
| 575 | return 0; |
| 576 | |
| 577 | return -ENOTSUPP; |
| 578 | } |
| 579 | |
Simon Glass | 865328c | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 580 | int bootflow_iter_check_system(const struct bootflow_iter *iter) |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 581 | { |
| 582 | const struct udevice *media = dev_get_parent(iter->dev); |
| 583 | enum uclass_id id = device_get_uclass_id(media); |
| 584 | |
| 585 | log_debug("uclass %d: %s\n", id, uclass_get_name(id)); |
| 586 | if (id == UCLASS_BOOTSTD) |
| 587 | return 0; |
| 588 | |
| 589 | return -ENOTSUPP; |
| 590 | } |
Simon Glass | d42243f | 2023-07-12 09:04:35 -0600 | [diff] [blame] | 591 | |
| 592 | /** |
| 593 | * bootflow_cmdline_set() - Set the command line for a bootflow |
| 594 | * |
| 595 | * @value: New command-line string |
| 596 | * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory |
| 597 | */ |
| 598 | int bootflow_cmdline_set(struct bootflow *bflow, const char *value) |
| 599 | { |
| 600 | char *cmdline = NULL; |
| 601 | |
| 602 | if (value) { |
| 603 | cmdline = strdup(value); |
| 604 | if (!cmdline) |
| 605 | return -ENOMEM; |
| 606 | } |
| 607 | |
| 608 | free(bflow->cmdline); |
| 609 | bflow->cmdline = cmdline; |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | #ifdef CONFIG_BOOTSTD_FULL |
| 615 | /** |
| 616 | * on_bootargs() - Update the cmdline of a bootflow |
| 617 | */ |
| 618 | static int on_bootargs(const char *name, const char *value, enum env_op op, |
| 619 | int flags) |
| 620 | { |
| 621 | struct bootstd_priv *std; |
| 622 | struct bootflow *bflow; |
| 623 | int ret; |
| 624 | |
| 625 | ret = bootstd_get_priv(&std); |
| 626 | if (ret) |
| 627 | return 0; |
| 628 | bflow = std->cur_bootflow; |
| 629 | if (!bflow) |
| 630 | return 0; |
| 631 | |
| 632 | switch (op) { |
| 633 | case env_op_create: |
| 634 | case env_op_overwrite: |
| 635 | ret = bootflow_cmdline_set(bflow, value); |
| 636 | if (ret && ret != ENOENT) |
| 637 | return 1; |
| 638 | return 0; |
| 639 | case env_op_delete: |
| 640 | bootflow_cmdline_set(bflow, NULL); |
| 641 | fallthrough; |
| 642 | default: |
| 643 | return 0; |
| 644 | } |
| 645 | } |
| 646 | U_BOOT_ENV_CALLBACK(bootargs, on_bootargs); |
| 647 | #endif |
Simon Glass | d07861c | 2023-07-12 09:04:38 -0600 | [diff] [blame] | 648 | |
| 649 | /** |
| 650 | * copy_in() - Copy a string into a cmdline buffer |
| 651 | * |
| 652 | * @buf: Buffer to copy into |
| 653 | * @end: End of buffer (pointer to char after the end) |
| 654 | * @arg: String to copy from |
| 655 | * @len: Number of chars to copy from @arg (note that this is not usually the |
| 656 | * sane as strlen(arg) since the string may contain following arguments) |
| 657 | * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value |
| 658 | * with no '=' sign |
| 659 | * Returns: Number of chars written to @buf |
| 660 | */ |
| 661 | static int copy_in(char *buf, char *end, const char *arg, int len, |
| 662 | const char *new_val) |
| 663 | { |
| 664 | char *to = buf; |
| 665 | |
| 666 | /* copy the arg name */ |
| 667 | if (to + len >= end) |
| 668 | return -E2BIG; |
| 669 | memcpy(to, arg, len); |
| 670 | to += len; |
| 671 | |
| 672 | if (new_val == BOOTFLOWCL_EMPTY) { |
| 673 | /* no value */ |
| 674 | } else { |
| 675 | bool need_quote = strchr(new_val, ' '); |
| 676 | len = strlen(new_val); |
| 677 | |
| 678 | /* need space for value, equals sign and maybe two quotes */ |
| 679 | if (to + 1 + (need_quote ? 2 : 0) + len >= end) |
| 680 | return -E2BIG; |
| 681 | *to++ = '='; |
| 682 | if (need_quote) |
| 683 | *to++ = '"'; |
| 684 | memcpy(to, new_val, len); |
| 685 | to += len; |
| 686 | if (need_quote) |
| 687 | *to++ = '"'; |
| 688 | } |
| 689 | |
| 690 | return to - buf; |
| 691 | } |
| 692 | |
| 693 | int cmdline_set_arg(char *buf, int maxlen, const char *cmdline, |
| 694 | const char *set_arg, const char *new_val, int *posp) |
| 695 | { |
| 696 | bool found_arg = false; |
| 697 | const char *from; |
| 698 | char *to, *end; |
| 699 | int set_arg_len; |
| 700 | char empty = '\0'; |
| 701 | int ret; |
| 702 | |
| 703 | from = cmdline ?: ∅ |
| 704 | |
| 705 | /* check if the value has quotes inside */ |
| 706 | if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"')) |
| 707 | return -EBADF; |
| 708 | |
| 709 | set_arg_len = strlen(set_arg); |
| 710 | for (to = buf, end = buf + maxlen; *from;) { |
| 711 | const char *val, *arg_end, *val_end, *p; |
| 712 | bool in_quote; |
| 713 | |
| 714 | if (to >= end) |
| 715 | return -E2BIG; |
| 716 | while (*from == ' ') |
| 717 | from++; |
| 718 | if (!*from) |
| 719 | break; |
| 720 | |
| 721 | /* find the end of this arg */ |
| 722 | val = NULL; |
| 723 | arg_end = NULL; |
| 724 | val_end = NULL; |
| 725 | in_quote = false; |
| 726 | for (p = from;; p++) { |
| 727 | if (in_quote) { |
| 728 | if (!*p) |
| 729 | return -EINVAL; |
| 730 | if (*p == '"') |
| 731 | in_quote = false; |
| 732 | continue; |
| 733 | } |
| 734 | if (*p == '=') { |
| 735 | arg_end = p; |
| 736 | val = p + 1; |
| 737 | } else if (*p == '"') { |
| 738 | in_quote = true; |
| 739 | } else if (!*p || *p == ' ') { |
| 740 | val_end = p; |
| 741 | if (!arg_end) |
| 742 | arg_end = p; |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | /* |
| 747 | * At this point val_end points to the end of the value, or the |
| 748 | * last char after the arg name, if there is no label. |
| 749 | * arg_end is the char after the arg name |
| 750 | * val points to the value, or NULL if there is none |
| 751 | * char after the value. |
| 752 | * |
| 753 | * fred=1234 |
| 754 | * ^ ^^ ^ |
| 755 | * from || | |
| 756 | * / \ \ |
| 757 | * arg_end val val_end |
| 758 | */ |
| 759 | log_debug("from %s arg_end %ld val %ld val_end %ld\n", from, |
| 760 | (long)(arg_end - from), (long)(val - from), |
| 761 | (long)(val_end - from)); |
| 762 | |
| 763 | if (to != buf) { |
| 764 | if (to >= end) |
| 765 | return -E2BIG; |
| 766 | *to++ = ' '; |
| 767 | } |
| 768 | |
| 769 | /* if this is the target arg, update it */ |
| 770 | if (!strncmp(from, set_arg, arg_end - from)) { |
| 771 | if (!buf) { |
| 772 | bool has_quote = val_end[-1] == '"'; |
| 773 | |
| 774 | /* |
| 775 | * exclude any start/end quotes from |
| 776 | * calculations |
| 777 | */ |
| 778 | if (!val) |
| 779 | val = val_end; |
| 780 | *posp = val - cmdline + has_quote; |
| 781 | return val_end - val - 2 * has_quote; |
| 782 | } |
| 783 | found_arg = true; |
| 784 | if (!new_val) { |
| 785 | /* delete this arg */ |
| 786 | from = val_end + (*val_end == ' '); |
| 787 | log_debug("delete from: %s\n", from); |
| 788 | if (to != buf) |
| 789 | to--; /* drop the space we added */ |
| 790 | continue; |
| 791 | } |
| 792 | |
| 793 | ret = copy_in(to, end, from, arg_end - from, new_val); |
| 794 | if (ret < 0) |
| 795 | return ret; |
| 796 | to += ret; |
| 797 | |
| 798 | /* if not the target arg, copy it unchanged */ |
| 799 | } else if (to) { |
| 800 | int len; |
| 801 | |
| 802 | len = val_end - from; |
| 803 | if (to + len >= end) |
| 804 | return -E2BIG; |
| 805 | memcpy(to, from, len); |
| 806 | to += len; |
| 807 | } |
| 808 | from = val_end; |
| 809 | } |
| 810 | |
| 811 | /* If we didn't find the arg, add it */ |
| 812 | if (!found_arg) { |
| 813 | /* trying to delete something that is not there */ |
| 814 | if (!new_val || !buf) |
| 815 | return -ENOENT; |
| 816 | if (to >= end) |
| 817 | return -E2BIG; |
| 818 | |
| 819 | /* add a space to separate it from the previous arg */ |
| 820 | if (to != buf && to[-1] != ' ') |
| 821 | *to++ = ' '; |
| 822 | ret = copy_in(to, end, set_arg, set_arg_len, new_val); |
| 823 | log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf); |
| 824 | if (ret < 0) |
| 825 | return ret; |
| 826 | to += ret; |
| 827 | } |
| 828 | |
| 829 | /* delete any trailing space */ |
| 830 | if (to > buf && to[-1] == ' ') |
| 831 | to--; |
| 832 | |
| 833 | if (to >= end) |
| 834 | return -E2BIG; |
| 835 | *to++ = '\0'; |
| 836 | |
| 837 | return to - buf; |
| 838 | } |
Simon Glass | 82c0938 | 2023-07-12 09:04:39 -0600 | [diff] [blame] | 839 | |
| 840 | int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg, |
| 841 | const char *new_val, bool set_env) |
| 842 | { |
| 843 | char buf[2048]; |
| 844 | char *cmd = NULL; |
| 845 | int ret; |
| 846 | |
| 847 | ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg, |
| 848 | new_val, NULL); |
| 849 | if (ret < 0) |
| 850 | return ret; |
| 851 | |
| 852 | ret = bootflow_cmdline_set(bflow, buf); |
| 853 | if (*buf) { |
| 854 | cmd = strdup(buf); |
| 855 | if (!cmd) |
| 856 | return -ENOMEM; |
| 857 | } |
| 858 | free(bflow->cmdline); |
| 859 | bflow->cmdline = cmd; |
| 860 | |
| 861 | if (set_env) { |
| 862 | ret = env_set("bootargs", bflow->cmdline); |
| 863 | if (ret) |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | int cmdline_get_arg(const char *cmdline, const char *arg, int *posp) |
| 871 | { |
| 872 | int ret; |
| 873 | |
| 874 | ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp); |
| 875 | |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg, |
| 880 | const char **val) |
| 881 | { |
| 882 | int ret; |
| 883 | int pos; |
| 884 | |
| 885 | ret = cmdline_get_arg(bflow->cmdline, arg, &pos); |
| 886 | if (ret < 0) |
| 887 | return ret; |
| 888 | *val = bflow->cmdline + pos; |
| 889 | |
| 890 | return ret; |
| 891 | } |
Simon Glass | 33ebcb4 | 2023-07-12 09:04:42 -0600 | [diff] [blame] | 892 | |
| 893 | int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg) |
| 894 | { |
| 895 | struct serial_device_info info; |
| 896 | char buf[50]; |
| 897 | int ret; |
| 898 | |
| 899 | ret = serial_getinfo(gd->cur_serial_dev, &info); |
| 900 | if (ret) |
| 901 | return ret; |
| 902 | |
| 903 | *buf = '\0'; |
| 904 | if (!strcmp("earlycon", arg)) { |
| 905 | snprintf(buf, sizeof(buf), |
| 906 | "uart8250,mmio32,%#lx,%dn8", info.addr, |
| 907 | info.baudrate); |
| 908 | } else if (!strcmp("console", arg)) { |
| 909 | snprintf(buf, sizeof(buf), |
| 910 | "ttyS0,%dn8", info.baudrate); |
| 911 | } |
| 912 | |
| 913 | if (!*buf) { |
| 914 | printf("Unknown param '%s\n", arg); |
| 915 | return -ENOENT; |
| 916 | } |
| 917 | |
| 918 | ret = bootflow_cmdline_set_arg(bflow, arg, buf, true); |
| 919 | if (ret) |
| 920 | return ret; |
| 921 | |
| 922 | return 0; |
| 923 | } |