Simon Glass | 017656e | 2022-04-24 23:31:07 -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 <dm.h> |
| 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 13 | #include <bootmeth.h> |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 14 | #include <bootstd.h> |
| 15 | #include <env.h> |
| 16 | #include <fs.h> |
| 17 | #include <log.h> |
| 18 | #include <malloc.h> |
| 19 | #include <part.h> |
| 20 | #include <sort.h> |
| 21 | #include <dm/device-internal.h> |
| 22 | #include <dm/lists.h> |
| 23 | #include <dm/uclass-internal.h> |
| 24 | |
| 25 | enum { |
| 26 | /* |
| 27 | * Set some sort of limit on the number of partitions a bootdev can |
| 28 | * have. Note that for disks this limits the partitions numbers that |
| 29 | * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV |
| 30 | */ |
| 31 | MAX_PART_PER_BOOTDEV = 30, |
| 32 | |
| 33 | /* Maximum supported length of the "boot_targets" env string */ |
| 34 | BOOT_TARGETS_MAX_LEN = 100, |
| 35 | }; |
| 36 | |
| 37 | int bootdev_add_bootflow(struct bootflow *bflow) |
| 38 | { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 39 | struct bootstd_priv *std; |
| 40 | struct bootflow *new; |
| 41 | int ret; |
| 42 | |
| 43 | assert(bflow->dev); |
| 44 | ret = bootstd_get_priv(&std); |
| 45 | if (ret) |
| 46 | return ret; |
| 47 | |
| 48 | new = malloc(sizeof(*bflow)); |
| 49 | if (!new) |
| 50 | return log_msg_ret("bflow", -ENOMEM); |
| 51 | memcpy(new, bflow, sizeof(*bflow)); |
| 52 | |
| 53 | list_add_tail(&new->glob_node, &std->glob_head); |
Simon Glass | dee4d64 | 2022-07-30 15:52:23 -0600 | [diff] [blame] | 54 | if (bflow->dev) { |
| 55 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 56 | |
| 57 | list_add_tail(&new->bm_node, &ucp->bootflow_head); |
| 58 | } |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp) |
| 64 | { |
| 65 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 66 | |
| 67 | if (list_empty(&ucp->bootflow_head)) |
| 68 | return -ENOENT; |
| 69 | |
| 70 | *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 71 | bm_node); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int bootdev_next_bootflow(struct bootflow **bflowp) |
| 77 | { |
| 78 | struct bootflow *bflow = *bflowp; |
| 79 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 80 | |
| 81 | *bflowp = NULL; |
| 82 | |
| 83 | if (list_is_last(&bflow->bm_node, &ucp->bootflow_head)) |
| 84 | return -ENOENT; |
| 85 | |
| 86 | *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name, |
| 92 | struct udevice **devp) |
| 93 | { |
| 94 | struct udevice *dev; |
| 95 | char dev_name[30]; |
| 96 | char *str; |
| 97 | int ret; |
| 98 | |
| 99 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 100 | str = strdup(dev_name); |
| 101 | if (!str) |
| 102 | return -ENOMEM; |
| 103 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | device_set_name_alloced(dev); |
| 107 | *devp = dev; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, |
| 113 | struct bootflow_iter *iter, struct bootflow *bflow) |
| 114 | { |
| 115 | struct blk_desc *desc = dev_get_uclass_plat(blk); |
| 116 | struct disk_partition info; |
| 117 | char partstr[20]; |
| 118 | char name[60]; |
| 119 | int ret; |
| 120 | |
| 121 | /* Sanity check */ |
| 122 | if (iter->part >= MAX_PART_PER_BOOTDEV) |
| 123 | return log_msg_ret("max", -ESHUTDOWN); |
| 124 | |
| 125 | bflow->blk = blk; |
| 126 | if (iter->part) |
| 127 | snprintf(partstr, sizeof(partstr), "part_%x", iter->part); |
| 128 | else |
| 129 | strcpy(partstr, "whole"); |
| 130 | snprintf(name, sizeof(name), "%s.%s", dev->name, partstr); |
| 131 | bflow->name = strdup(name); |
| 132 | if (!bflow->name) |
| 133 | return log_msg_ret("name", -ENOMEM); |
| 134 | |
| 135 | bflow->part = iter->part; |
| 136 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 137 | ret = bootmeth_check(bflow->method, iter); |
| 138 | if (ret) |
| 139 | return log_msg_ret("check", ret); |
| 140 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 141 | /* |
| 142 | * partition numbers start at 0 so this cannot succeed, but it can tell |
| 143 | * us whether there is valid media there |
| 144 | */ |
| 145 | ret = part_get_info(desc, iter->part, &info); |
| 146 | if (!iter->part && ret == -ENOENT) |
| 147 | ret = 0; |
| 148 | |
| 149 | /* |
| 150 | * This error indicates the media is not present. Otherwise we just |
| 151 | * blindly scan the next partition. We could be more intelligent here |
| 152 | * and check which partition numbers actually exist. |
| 153 | */ |
| 154 | if (ret == -EOPNOTSUPP) |
| 155 | ret = -ESHUTDOWN; |
| 156 | else |
| 157 | bflow->state = BOOTFLOWST_MEDIA; |
| 158 | if (ret) |
| 159 | return log_msg_ret("part", ret); |
| 160 | |
| 161 | /* |
| 162 | * Currently we don't get the number of partitions, so just |
| 163 | * assume a large number |
| 164 | */ |
| 165 | iter->max_part = MAX_PART_PER_BOOTDEV; |
| 166 | |
| 167 | if (iter->part) { |
| 168 | ret = fs_set_blk_dev_with_part(desc, bflow->part); |
| 169 | bflow->state = BOOTFLOWST_PART; |
| 170 | |
| 171 | /* Use an #ifdef due to info.sys_ind */ |
| 172 | #ifdef CONFIG_DOS_PARTITION |
| 173 | log_debug("%s: Found partition %x type %x fstype %d\n", |
| 174 | blk->name, bflow->part, info.sys_ind, |
| 175 | ret ? -1 : fs_get_type()); |
| 176 | #endif |
| 177 | if (ret) |
| 178 | return log_msg_ret("fs", ret); |
| 179 | bflow->state = BOOTFLOWST_FS; |
| 180 | } |
| 181 | |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 182 | ret = bootmeth_read_bootflow(bflow->method, bflow); |
| 183 | if (ret) |
| 184 | return log_msg_ret("method", ret); |
| 185 | |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | void bootdev_list(bool probe) |
| 190 | { |
| 191 | struct udevice *dev; |
| 192 | int ret; |
| 193 | int i; |
| 194 | |
| 195 | printf("Seq Probed Status Uclass Name\n"); |
| 196 | printf("--- ------ ------ -------- ------------------\n"); |
| 197 | if (probe) |
Michal Suchanek | 675e908 | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 198 | ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 199 | else |
| 200 | ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev); |
| 201 | for (i = 0; dev; i++) { |
| 202 | printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev), |
| 203 | device_active(dev) ? '+' : ' ', |
| 204 | ret ? simple_itoa(ret) : "OK", |
| 205 | dev_get_uclass_name(dev_get_parent(dev)), dev->name); |
| 206 | if (probe) |
Michal Suchanek | 675e908 | 2022-10-12 21:57:53 +0200 | [diff] [blame] | 207 | ret = uclass_next_device_check(&dev); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 208 | else |
| 209 | ret = uclass_find_next_device(&dev); |
| 210 | } |
| 211 | printf("--- ------ ------ -------- ------------------\n"); |
| 212 | printf("(%d bootdev%s)\n", i, i != 1 ? "s" : ""); |
| 213 | } |
| 214 | |
| 215 | int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name) |
| 216 | { |
| 217 | struct udevice *bdev; |
| 218 | int ret; |
| 219 | |
| 220 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, |
| 221 | &bdev); |
| 222 | if (ret) { |
| 223 | if (ret != -ENODEV) { |
| 224 | log_debug("Cannot access bootdev device\n"); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | ret = bootdev_bind(parent, drv_name, "bootdev", &bdev); |
| 229 | if (ret) { |
| 230 | log_debug("Cannot create bootdev device\n"); |
| 231 | return ret; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name) |
| 239 | { |
| 240 | struct udevice *parent, *dev; |
| 241 | char dev_name[50]; |
| 242 | int ret; |
| 243 | |
| 244 | snprintf(dev_name, sizeof(dev_name), "%s.%s", blk->name, "bootdev"); |
| 245 | |
| 246 | parent = dev_get_parent(blk); |
| 247 | ret = device_find_child_by_name(parent, dev_name, &dev); |
| 248 | if (ret) { |
| 249 | char *str; |
| 250 | |
| 251 | if (ret != -ENODEV) { |
| 252 | log_debug("Cannot access bootdev device\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | str = strdup(dev_name); |
| 256 | if (!str) |
| 257 | return -ENOMEM; |
| 258 | |
| 259 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 260 | if (ret) { |
| 261 | log_debug("Cannot create bootdev device\n"); |
| 262 | return ret; |
| 263 | } |
| 264 | device_set_name_alloced(dev); |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp) |
| 271 | { |
| 272 | struct udevice *parent = dev_get_parent(dev); |
| 273 | struct udevice *blk; |
| 274 | int ret, len; |
| 275 | char *p; |
| 276 | |
| 277 | if (device_get_uclass_id(dev) != UCLASS_BOOTDEV) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | /* This should always work if bootdev_setup_sibling_blk() was used */ |
| 281 | p = strstr(dev->name, ".bootdev"); |
| 282 | if (!p) |
| 283 | return log_msg_ret("str", -EINVAL); |
| 284 | |
| 285 | len = p - dev->name; |
| 286 | ret = device_find_child_by_namelen(parent, dev->name, len, &blk); |
| 287 | if (ret) |
| 288 | return log_msg_ret("find", ret); |
| 289 | *blkp = blk; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp) |
| 295 | { |
| 296 | struct udevice *parent = dev_get_parent(blk); |
| 297 | struct udevice *bootdev; |
| 298 | char dev_name[50]; |
| 299 | int ret; |
| 300 | |
| 301 | if (device_get_uclass_id(blk) != UCLASS_BLK) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | /* This should always work if bootdev_setup_sibling_blk() was used */ |
| 305 | snprintf(dev_name, sizeof(dev_name), "%s.%s", blk->name, "bootdev"); |
| 306 | ret = device_find_child_by_name(parent, dev_name, &bootdev); |
| 307 | if (ret) |
| 308 | return log_msg_ret("find", ret); |
| 309 | *bootdevp = bootdev; |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | int bootdev_unbind_dev(struct udevice *parent) |
| 315 | { |
| 316 | struct udevice *dev; |
| 317 | int ret; |
| 318 | |
| 319 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev); |
| 320 | if (!ret) { |
| 321 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 322 | if (ret) |
| 323 | return log_msg_ret("rem", ret); |
| 324 | ret = device_unbind(dev); |
| 325 | if (ret) |
| 326 | return log_msg_ret("unb", ret); |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * bootdev_find_by_label() - Convert a label string to a bootdev device |
| 334 | * |
| 335 | * Looks up a label name to find the associated bootdev. For example, if the |
| 336 | * label name is "mmc2", this will find a bootdev for an mmc device whose |
| 337 | * sequence number is 2. |
| 338 | * |
| 339 | * @label: Label string to convert, e.g. "mmc2" |
| 340 | * @devp: Returns bootdev device corresponding to that boot label |
| 341 | * Return: 0 if OK, -EINVAL if the label name (e.g. "mmc") does not refer to a |
| 342 | * uclass, -ENOENT if no bootdev for that media has the sequence number |
| 343 | * (e.g. 2) |
| 344 | */ |
| 345 | int bootdev_find_by_label(const char *label, struct udevice **devp) |
| 346 | { |
| 347 | struct udevice *media; |
| 348 | struct uclass *uc; |
| 349 | enum uclass_id id; |
| 350 | const char *end; |
| 351 | int seq; |
| 352 | |
| 353 | seq = trailing_strtoln_end(label, NULL, &end); |
| 354 | id = uclass_get_by_namelen(label, end - label); |
| 355 | log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id, |
| 356 | uclass_get_name(id)); |
| 357 | if (id == UCLASS_INVALID) { |
| 358 | log_warning("Unknown uclass '%s' in label\n", label); |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | if (id == UCLASS_USB) |
| 362 | id = UCLASS_MASS_STORAGE; |
| 363 | |
| 364 | /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */ |
| 365 | uclass_id_foreach_dev(id, media, uc) { |
| 366 | struct udevice *bdev, *blk; |
| 367 | int ret; |
| 368 | |
| 369 | /* if there is no seq, match anything */ |
| 370 | if (seq != -1 && dev_seq(media) != seq) { |
| 371 | log_debug("- skip, media seq=%d\n", dev_seq(media)); |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV, |
| 376 | &bdev); |
| 377 | if (ret) { |
| 378 | log_debug("- looking via blk, seq=%d, id=%d\n", seq, |
| 379 | id); |
| 380 | ret = blk_find_device(id, seq, &blk); |
| 381 | if (!ret) { |
| 382 | log_debug("- get from blk %s\n", blk->name); |
| 383 | ret = bootdev_get_from_blk(blk, &bdev); |
| 384 | } |
| 385 | } |
| 386 | if (!ret) { |
| 387 | log_debug("- found %s\n", bdev->name); |
| 388 | *devp = bdev; |
| 389 | return 0; |
| 390 | } |
| 391 | log_debug("- no device in %s\n", media->name); |
| 392 | } |
| 393 | log_warning("Unknown seq %d for label '%s'\n", seq, label); |
| 394 | |
| 395 | return -ENOENT; |
| 396 | } |
| 397 | |
| 398 | int bootdev_find_by_any(const char *name, struct udevice **devp) |
| 399 | { |
| 400 | struct udevice *dev; |
| 401 | int ret, seq; |
| 402 | char *endp; |
| 403 | |
| 404 | seq = simple_strtol(name, &endp, 16); |
| 405 | |
| 406 | /* Select by name, label or number */ |
| 407 | if (*endp) { |
| 408 | ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev); |
| 409 | if (ret == -ENODEV) { |
| 410 | ret = bootdev_find_by_label(name, &dev); |
| 411 | if (ret) { |
| 412 | printf("Cannot find bootdev '%s' (err=%d)\n", |
| 413 | name, ret); |
| 414 | return ret; |
| 415 | } |
| 416 | ret = device_probe(dev); |
| 417 | } |
| 418 | if (ret) { |
| 419 | printf("Cannot probe bootdev '%s' (err=%d)\n", name, |
| 420 | ret); |
| 421 | return ret; |
| 422 | } |
| 423 | } else { |
| 424 | ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev); |
| 425 | } |
| 426 | if (ret) { |
| 427 | printf("Cannot find '%s' (err=%d)\n", name, ret); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | *devp = dev; |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 437 | struct bootflow *bflow) |
| 438 | { |
| 439 | const struct bootdev_ops *ops = bootdev_get_ops(dev); |
| 440 | |
| 441 | if (!ops->get_bootflow) |
| 442 | return -ENOSYS; |
Simon Glass | 00501fc | 2022-10-20 18:22:51 -0600 | [diff] [blame] | 443 | bootflow_init(bflow, dev, iter->method); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 444 | |
| 445 | return ops->get_bootflow(dev, iter, bflow); |
| 446 | } |
| 447 | |
| 448 | void bootdev_clear_bootflows(struct udevice *dev) |
| 449 | { |
| 450 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 451 | |
| 452 | while (!list_empty(&ucp->bootflow_head)) { |
| 453 | struct bootflow *bflow; |
| 454 | |
| 455 | bflow = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 456 | bm_node); |
Simon Glass | 03fcbf9 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 457 | bootflow_remove(bflow); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * h_cmp_bootdev() - Compare two bootdevs to find out which should go first |
| 463 | * |
| 464 | * @v1: struct udevice * of first bootdev device |
| 465 | * @v2: struct udevice * of second bootdev device |
| 466 | * Return: sort order (<0 if dev1 < dev2, ==0 if equal, >0 if dev1 > dev2) |
| 467 | */ |
| 468 | static int h_cmp_bootdev(const void *v1, const void *v2) |
| 469 | { |
| 470 | const struct udevice *dev1 = *(struct udevice **)v1; |
| 471 | const struct udevice *dev2 = *(struct udevice **)v2; |
| 472 | const struct bootdev_uc_plat *ucp1 = dev_get_uclass_plat(dev1); |
| 473 | const struct bootdev_uc_plat *ucp2 = dev_get_uclass_plat(dev2); |
| 474 | int diff; |
| 475 | |
| 476 | /* Use priority first */ |
| 477 | diff = ucp1->prio - ucp2->prio; |
| 478 | if (diff) |
| 479 | return diff; |
| 480 | |
| 481 | /* Fall back to seq for devices of the same priority */ |
| 482 | diff = dev_seq(dev1) - dev_seq(dev2); |
| 483 | |
| 484 | return diff; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * build_order() - Build the ordered list of bootdevs to use |
| 489 | * |
| 490 | * This builds an ordered list of devices by one of three methods: |
| 491 | * - using the boot_targets environment variable, if non-empty |
| 492 | * - using the bootdev-order devicetree property, if present |
| 493 | * - sorted by priority and sequence number |
| 494 | * |
| 495 | * @bootstd: BOOTSTD device to use |
| 496 | * @order: Bootdevs listed in default order |
| 497 | * @max_count: Number of entries in @order |
| 498 | * Return: number of bootdevs found in the ordering, or -E2BIG if the |
| 499 | * boot_targets string is too long, or -EXDEV if the ordering produced 0 results |
| 500 | */ |
| 501 | static int build_order(struct udevice *bootstd, struct udevice **order, |
| 502 | int max_count) |
| 503 | { |
| 504 | const char *overflow_target = NULL; |
| 505 | const char *const *labels; |
| 506 | struct udevice *dev; |
| 507 | const char *targets; |
| 508 | int i, ret, count; |
| 509 | |
| 510 | targets = env_get("boot_targets"); |
| 511 | labels = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
| 512 | bootstd_get_bootdev_order(bootstd) : NULL; |
| 513 | if (targets) { |
| 514 | char str[BOOT_TARGETS_MAX_LEN]; |
| 515 | char *target; |
| 516 | |
| 517 | if (strlen(targets) >= BOOT_TARGETS_MAX_LEN) |
| 518 | return log_msg_ret("len", -E2BIG); |
| 519 | |
| 520 | /* make a copy of the string, since strok() will change it */ |
| 521 | strcpy(str, targets); |
| 522 | for (i = 0, target = strtok(str, " "); target; |
| 523 | target = strtok(NULL, " ")) { |
| 524 | ret = bootdev_find_by_label(target, &dev); |
| 525 | if (!ret) { |
| 526 | if (i == max_count) { |
| 527 | overflow_target = target; |
| 528 | break; |
| 529 | } |
| 530 | order[i++] = dev; |
| 531 | } |
| 532 | } |
| 533 | count = i; |
| 534 | } else if (labels) { |
| 535 | int upto; |
| 536 | |
| 537 | upto = 0; |
| 538 | for (i = 0; labels[i]; i++) { |
| 539 | ret = bootdev_find_by_label(labels[i], &dev); |
| 540 | if (!ret) { |
| 541 | if (upto == max_count) { |
| 542 | overflow_target = labels[i]; |
| 543 | break; |
| 544 | } |
| 545 | order[upto++] = dev; |
| 546 | } |
| 547 | } |
| 548 | count = upto; |
| 549 | } else { |
| 550 | /* sort them into priority order */ |
| 551 | count = max_count; |
| 552 | qsort(order, count, sizeof(struct udevice *), h_cmp_bootdev); |
| 553 | } |
| 554 | |
| 555 | if (overflow_target) { |
| 556 | log_warning("Expected at most %d bootdevs, but overflowed with boot_target '%s'\n", |
| 557 | max_count, overflow_target); |
| 558 | } |
| 559 | |
| 560 | if (!count) |
| 561 | return log_msg_ret("targ", -EXDEV); |
| 562 | |
| 563 | return count; |
| 564 | } |
| 565 | |
| 566 | int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp) |
| 567 | { |
| 568 | struct udevice *bootstd, *dev = *devp, **order; |
| 569 | int upto, i; |
| 570 | int count; |
| 571 | int ret; |
| 572 | |
| 573 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 574 | if (ret) { |
| 575 | log_err("Missing bootstd device\n"); |
| 576 | return log_msg_ret("std", ret); |
| 577 | } |
| 578 | |
| 579 | /* Handle scanning a single device */ |
| 580 | if (dev) { |
| 581 | iter->flags |= BOOTFLOWF_SINGLE_DEV; |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | count = uclass_id_count(UCLASS_BOOTDEV); |
| 586 | if (!count) |
| 587 | return log_msg_ret("count", -ENOENT); |
| 588 | |
| 589 | order = calloc(count, sizeof(struct udevice *)); |
| 590 | if (!order) |
| 591 | return log_msg_ret("order", -ENOMEM); |
| 592 | |
| 593 | /* |
| 594 | * Get a list of bootdevs, in seq order (i.e. using aliases). There may |
| 595 | * be gaps so try to count up high enough to find them all. |
| 596 | */ |
| 597 | for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) { |
| 598 | ret = uclass_find_device_by_seq(UCLASS_BOOTDEV, i, &dev); |
| 599 | if (!ret) |
| 600 | order[upto++] = dev; |
| 601 | } |
| 602 | log_debug("Found %d bootdevs\n", count); |
| 603 | if (upto != count) |
| 604 | log_debug("Expected %d bootdevs, found %d using aliases\n", |
| 605 | count, upto); |
| 606 | |
Simon Glass | f6f88ba | 2022-07-30 15:52:20 -0600 | [diff] [blame] | 607 | ret = build_order(bootstd, order, upto); |
| 608 | if (ret < 0) { |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 609 | free(order); |
Simon Glass | f6f88ba | 2022-07-30 15:52:20 -0600 | [diff] [blame] | 610 | return log_msg_ret("build", ret); |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 611 | } |
| 612 | |
Simon Glass | f6f88ba | 2022-07-30 15:52:20 -0600 | [diff] [blame] | 613 | iter->num_devs = ret; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 614 | iter->dev_order = order; |
Simon Glass | 017656e | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 615 | iter->cur_dev = 0; |
| 616 | |
| 617 | dev = *order; |
| 618 | ret = device_probe(dev); |
| 619 | if (ret) |
| 620 | return log_msg_ret("probe", ret); |
| 621 | *devp = dev; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int bootdev_post_bind(struct udevice *dev) |
| 627 | { |
| 628 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 629 | |
| 630 | INIT_LIST_HEAD(&ucp->bootflow_head); |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int bootdev_pre_unbind(struct udevice *dev) |
| 636 | { |
| 637 | bootdev_clear_bootflows(dev); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | UCLASS_DRIVER(bootdev) = { |
| 643 | .id = UCLASS_BOOTDEV, |
| 644 | .name = "bootdev", |
| 645 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 646 | .per_device_plat_auto = sizeof(struct bootdev_uc_plat), |
| 647 | .post_bind = bootdev_post_bind, |
| 648 | .pre_unbind = bootdev_pre_unbind, |
| 649 | }; |