Simon Glass | 201417d | 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 | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 13 | #include <bootmeth.h> |
Simon Glass | 201417d | 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 | { |
| 39 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 40 | struct bootstd_priv *std; |
| 41 | struct bootflow *new; |
| 42 | int ret; |
| 43 | |
| 44 | assert(bflow->dev); |
| 45 | ret = bootstd_get_priv(&std); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | new = malloc(sizeof(*bflow)); |
| 50 | if (!new) |
| 51 | return log_msg_ret("bflow", -ENOMEM); |
| 52 | memcpy(new, bflow, sizeof(*bflow)); |
| 53 | |
| 54 | list_add_tail(&new->glob_node, &std->glob_head); |
| 55 | list_add_tail(&new->bm_node, &ucp->bootflow_head); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp) |
| 61 | { |
| 62 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 63 | |
| 64 | if (list_empty(&ucp->bootflow_head)) |
| 65 | return -ENOENT; |
| 66 | |
| 67 | *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow, |
| 68 | bm_node); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int bootdev_next_bootflow(struct bootflow **bflowp) |
| 74 | { |
| 75 | struct bootflow *bflow = *bflowp; |
| 76 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev); |
| 77 | |
| 78 | *bflowp = NULL; |
| 79 | |
| 80 | if (list_is_last(&bflow->bm_node, &ucp->bootflow_head)) |
| 81 | return -ENOENT; |
| 82 | |
| 83 | *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name, |
| 89 | struct udevice **devp) |
| 90 | { |
| 91 | struct udevice *dev; |
| 92 | char dev_name[30]; |
| 93 | char *str; |
| 94 | int ret; |
| 95 | |
| 96 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 97 | str = strdup(dev_name); |
| 98 | if (!str) |
| 99 | return -ENOMEM; |
| 100 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | device_set_name_alloced(dev); |
| 104 | *devp = dev; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, |
| 110 | struct bootflow_iter *iter, struct bootflow *bflow) |
| 111 | { |
| 112 | struct blk_desc *desc = dev_get_uclass_plat(blk); |
| 113 | struct disk_partition info; |
| 114 | char partstr[20]; |
| 115 | char name[60]; |
| 116 | int ret; |
| 117 | |
| 118 | /* Sanity check */ |
| 119 | if (iter->part >= MAX_PART_PER_BOOTDEV) |
| 120 | return log_msg_ret("max", -ESHUTDOWN); |
| 121 | |
| 122 | bflow->blk = blk; |
| 123 | if (iter->part) |
| 124 | snprintf(partstr, sizeof(partstr), "part_%x", iter->part); |
| 125 | else |
| 126 | strcpy(partstr, "whole"); |
| 127 | snprintf(name, sizeof(name), "%s.%s", dev->name, partstr); |
| 128 | bflow->name = strdup(name); |
| 129 | if (!bflow->name) |
| 130 | return log_msg_ret("name", -ENOMEM); |
| 131 | |
| 132 | bflow->part = iter->part; |
| 133 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 134 | ret = bootmeth_check(bflow->method, iter); |
| 135 | if (ret) |
| 136 | return log_msg_ret("check", ret); |
| 137 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 138 | /* |
| 139 | * partition numbers start at 0 so this cannot succeed, but it can tell |
| 140 | * us whether there is valid media there |
| 141 | */ |
| 142 | ret = part_get_info(desc, iter->part, &info); |
| 143 | if (!iter->part && ret == -ENOENT) |
| 144 | ret = 0; |
| 145 | |
| 146 | /* |
| 147 | * This error indicates the media is not present. Otherwise we just |
| 148 | * blindly scan the next partition. We could be more intelligent here |
| 149 | * and check which partition numbers actually exist. |
| 150 | */ |
| 151 | if (ret == -EOPNOTSUPP) |
| 152 | ret = -ESHUTDOWN; |
| 153 | else |
| 154 | bflow->state = BOOTFLOWST_MEDIA; |
| 155 | if (ret) |
| 156 | return log_msg_ret("part", ret); |
| 157 | |
| 158 | /* |
| 159 | * Currently we don't get the number of partitions, so just |
| 160 | * assume a large number |
| 161 | */ |
| 162 | iter->max_part = MAX_PART_PER_BOOTDEV; |
| 163 | |
| 164 | if (iter->part) { |
| 165 | ret = fs_set_blk_dev_with_part(desc, bflow->part); |
| 166 | bflow->state = BOOTFLOWST_PART; |
| 167 | |
| 168 | /* Use an #ifdef due to info.sys_ind */ |
| 169 | #ifdef CONFIG_DOS_PARTITION |
| 170 | log_debug("%s: Found partition %x type %x fstype %d\n", |
| 171 | blk->name, bflow->part, info.sys_ind, |
| 172 | ret ? -1 : fs_get_type()); |
| 173 | #endif |
| 174 | if (ret) |
| 175 | return log_msg_ret("fs", ret); |
| 176 | bflow->state = BOOTFLOWST_FS; |
| 177 | } |
| 178 | |
Simon Glass | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 179 | ret = bootmeth_read_bootflow(bflow->method, bflow); |
| 180 | if (ret) |
| 181 | return log_msg_ret("method", ret); |
| 182 | |
Simon Glass | 201417d | 2022-04-24 23:31:07 -0600 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | void bootdev_list(bool probe) |
| 187 | { |
| 188 | struct udevice *dev; |
| 189 | int ret; |
| 190 | int i; |
| 191 | |
| 192 | printf("Seq Probed Status Uclass Name\n"); |
| 193 | printf("--- ------ ------ -------- ------------------\n"); |
| 194 | if (probe) |
| 195 | ret = uclass_first_device_err(UCLASS_BOOTDEV, &dev); |
| 196 | else |
| 197 | ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev); |
| 198 | for (i = 0; dev; i++) { |
| 199 | printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev), |
| 200 | device_active(dev) ? '+' : ' ', |
| 201 | ret ? simple_itoa(ret) : "OK", |
| 202 | dev_get_uclass_name(dev_get_parent(dev)), dev->name); |
| 203 | if (probe) |
| 204 | ret = uclass_next_device_err(&dev); |
| 205 | else |
| 206 | ret = uclass_find_next_device(&dev); |
| 207 | } |
| 208 | printf("--- ------ ------ -------- ------------------\n"); |
| 209 | printf("(%d bootdev%s)\n", i, i != 1 ? "s" : ""); |
| 210 | } |
| 211 | |
| 212 | int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name) |
| 213 | { |
| 214 | struct udevice *bdev; |
| 215 | int ret; |
| 216 | |
| 217 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, |
| 218 | &bdev); |
| 219 | if (ret) { |
| 220 | if (ret != -ENODEV) { |
| 221 | log_debug("Cannot access bootdev device\n"); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | ret = bootdev_bind(parent, drv_name, "bootdev", &bdev); |
| 226 | if (ret) { |
| 227 | log_debug("Cannot create bootdev device\n"); |
| 228 | return ret; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name) |
| 236 | { |
| 237 | struct udevice *parent, *dev; |
| 238 | char dev_name[50]; |
| 239 | int ret; |
| 240 | |
| 241 | snprintf(dev_name, sizeof(dev_name), "%s.%s", blk->name, "bootdev"); |
| 242 | |
| 243 | parent = dev_get_parent(blk); |
| 244 | ret = device_find_child_by_name(parent, dev_name, &dev); |
| 245 | if (ret) { |
| 246 | char *str; |
| 247 | |
| 248 | if (ret != -ENODEV) { |
| 249 | log_debug("Cannot access bootdev device\n"); |
| 250 | return ret; |
| 251 | } |
| 252 | str = strdup(dev_name); |
| 253 | if (!str) |
| 254 | return -ENOMEM; |
| 255 | |
| 256 | ret = device_bind_driver(parent, drv_name, str, &dev); |
| 257 | if (ret) { |
| 258 | log_debug("Cannot create bootdev device\n"); |
| 259 | return ret; |
| 260 | } |
| 261 | device_set_name_alloced(dev); |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp) |
| 268 | { |
| 269 | struct udevice *parent = dev_get_parent(dev); |
| 270 | struct udevice *blk; |
| 271 | int ret, len; |
| 272 | char *p; |
| 273 | |
| 274 | if (device_get_uclass_id(dev) != UCLASS_BOOTDEV) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | /* This should always work if bootdev_setup_sibling_blk() was used */ |
| 278 | p = strstr(dev->name, ".bootdev"); |
| 279 | if (!p) |
| 280 | return log_msg_ret("str", -EINVAL); |
| 281 | |
| 282 | len = p - dev->name; |
| 283 | ret = device_find_child_by_namelen(parent, dev->name, len, &blk); |
| 284 | if (ret) |
| 285 | return log_msg_ret("find", ret); |
| 286 | *blkp = blk; |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp) |
| 292 | { |
| 293 | struct udevice *parent = dev_get_parent(blk); |
| 294 | struct udevice *bootdev; |
| 295 | char dev_name[50]; |
| 296 | int ret; |
| 297 | |
| 298 | if (device_get_uclass_id(blk) != UCLASS_BLK) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | /* This should always work if bootdev_setup_sibling_blk() was used */ |
| 302 | snprintf(dev_name, sizeof(dev_name), "%s.%s", blk->name, "bootdev"); |
| 303 | ret = device_find_child_by_name(parent, dev_name, &bootdev); |
| 304 | if (ret) |
| 305 | return log_msg_ret("find", ret); |
| 306 | *bootdevp = bootdev; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | int bootdev_unbind_dev(struct udevice *parent) |
| 312 | { |
| 313 | struct udevice *dev; |
| 314 | int ret; |
| 315 | |
| 316 | ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev); |
| 317 | if (!ret) { |
| 318 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 319 | if (ret) |
| 320 | return log_msg_ret("rem", ret); |
| 321 | ret = device_unbind(dev); |
| 322 | if (ret) |
| 323 | return log_msg_ret("unb", ret); |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * bootdev_find_by_label() - Convert a label string to a bootdev device |
| 331 | * |
| 332 | * Looks up a label name to find the associated bootdev. For example, if the |
| 333 | * label name is "mmc2", this will find a bootdev for an mmc device whose |
| 334 | * sequence number is 2. |
| 335 | * |
| 336 | * @label: Label string to convert, e.g. "mmc2" |
| 337 | * @devp: Returns bootdev device corresponding to that boot label |
| 338 | * Return: 0 if OK, -EINVAL if the label name (e.g. "mmc") does not refer to a |
| 339 | * uclass, -ENOENT if no bootdev for that media has the sequence number |
| 340 | * (e.g. 2) |
| 341 | */ |
| 342 | int bootdev_find_by_label(const char *label, struct udevice **devp) |
| 343 | { |
| 344 | struct udevice *media; |
| 345 | struct uclass *uc; |
| 346 | enum uclass_id id; |
| 347 | const char *end; |
| 348 | int seq; |
| 349 | |
| 350 | seq = trailing_strtoln_end(label, NULL, &end); |
| 351 | id = uclass_get_by_namelen(label, end - label); |
| 352 | log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id, |
| 353 | uclass_get_name(id)); |
| 354 | if (id == UCLASS_INVALID) { |
| 355 | log_warning("Unknown uclass '%s' in label\n", label); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | if (id == UCLASS_USB) |
| 359 | id = UCLASS_MASS_STORAGE; |
| 360 | |
| 361 | /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */ |
| 362 | uclass_id_foreach_dev(id, media, uc) { |
| 363 | struct udevice *bdev, *blk; |
| 364 | int ret; |
| 365 | |
| 366 | /* if there is no seq, match anything */ |
| 367 | if (seq != -1 && dev_seq(media) != seq) { |
| 368 | log_debug("- skip, media seq=%d\n", dev_seq(media)); |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV, |
| 373 | &bdev); |
| 374 | if (ret) { |
| 375 | log_debug("- looking via blk, seq=%d, id=%d\n", seq, |
| 376 | id); |
| 377 | ret = blk_find_device(id, seq, &blk); |
| 378 | if (!ret) { |
| 379 | log_debug("- get from blk %s\n", blk->name); |
| 380 | ret = bootdev_get_from_blk(blk, &bdev); |
| 381 | } |
| 382 | } |
| 383 | if (!ret) { |
| 384 | log_debug("- found %s\n", bdev->name); |
| 385 | *devp = bdev; |
| 386 | return 0; |
| 387 | } |
| 388 | log_debug("- no device in %s\n", media->name); |
| 389 | } |
| 390 | log_warning("Unknown seq %d for label '%s'\n", seq, label); |
| 391 | |
| 392 | return -ENOENT; |
| 393 | } |
| 394 | |
| 395 | int bootdev_find_by_any(const char *name, struct udevice **devp) |
| 396 | { |
| 397 | struct udevice *dev; |
| 398 | int ret, seq; |
| 399 | char *endp; |
| 400 | |
| 401 | seq = simple_strtol(name, &endp, 16); |
| 402 | |
| 403 | /* Select by name, label or number */ |
| 404 | if (*endp) { |
| 405 | ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev); |
| 406 | if (ret == -ENODEV) { |
| 407 | ret = bootdev_find_by_label(name, &dev); |
| 408 | if (ret) { |
| 409 | printf("Cannot find bootdev '%s' (err=%d)\n", |
| 410 | name, ret); |
| 411 | return ret; |
| 412 | } |
| 413 | ret = device_probe(dev); |
| 414 | } |
| 415 | if (ret) { |
| 416 | printf("Cannot probe bootdev '%s' (err=%d)\n", name, |
| 417 | ret); |
| 418 | return ret; |
| 419 | } |
| 420 | } else { |
| 421 | ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev); |
| 422 | } |
| 423 | if (ret) { |
| 424 | printf("Cannot find '%s' (err=%d)\n", name, ret); |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | *devp = dev; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 434 | struct bootflow *bflow) |
| 435 | { |
| 436 | const struct bootdev_ops *ops = bootdev_get_ops(dev); |
| 437 | |
| 438 | if (!ops->get_bootflow) |
| 439 | return -ENOSYS; |
| 440 | memset(bflow, '\0', sizeof(*bflow)); |
| 441 | bflow->dev = dev; |
| 442 | bflow->method = iter->method; |
| 443 | bflow->state = BOOTFLOWST_BASE; |
| 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 | a8f5be1 | 2022-04-24 23:31:09 -0600 | [diff] [blame] | 457 | bootflow_remove(bflow); |
Simon Glass | 201417d | 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 | |
| 607 | count = build_order(bootstd, order, upto); |
| 608 | if (count < 0) { |
| 609 | free(order); |
| 610 | return log_msg_ret("build", count); |
| 611 | } |
| 612 | |
| 613 | iter->dev_order = order; |
| 614 | iter->num_devs = count; |
| 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 | }; |