"Das U-Boot" Source Tree
at master 945 lines 23 kB view raw
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 <dm.h> 10#include <bootdev.h> 11#include <bootflow.h> 12#include <bootmeth.h> 13#include <bootstd.h> 14#include <fs.h> 15#include <log.h> 16#include <malloc.h> 17#include <part.h> 18#include <sort.h> 19#include <spl.h> 20#include <dm/device-internal.h> 21#include <dm/lists.h> 22#include <dm/uclass-internal.h> 23 24enum { 25 /* 26 * Set some sort of limit on the number of partitions a bootdev can 27 * have. Note that for disks this limits the partitions numbers that 28 * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV 29 */ 30 MAX_PART_PER_BOOTDEV = 30, 31 32 /* Maximum supported length of the "boot_targets" env string */ 33 BOOT_TARGETS_MAX_LEN = 100, 34}; 35 36int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp) 37{ 38 struct bootstd_priv *std; 39 struct bootflow *bflow; 40 int ret; 41 42 ret = bootstd_get_priv(&std); 43 if (ret) 44 return log_msg_ret("bff", ret); 45 46 bflow = alist_getw(&std->bootflows, 0, struct bootflow); 47 if (!bflow) 48 return -ENOENT; 49 *bflowp = bflow; 50 51 return 0; 52} 53 54int bootdev_next_bootflow(struct bootflow **bflowp) 55{ 56 struct bootstd_priv *std; 57 struct bootflow *bflow; 58 int ret; 59 60 ret = bootstd_get_priv(&std); 61 if (ret) 62 return log_msg_ret("bff", ret); 63 64 bflow = alist_nextw(&std->bootflows, *bflowp); 65 if (!bflow) 66 return -ENOENT; 67 *bflowp = bflow; 68 69 return 0; 70} 71 72int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name, 73 struct udevice **devp) 74{ 75 struct udevice *dev; 76 char dev_name[30]; 77 char *str; 78 int ret; 79 80 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); 81 str = strdup(dev_name); 82 if (!str) 83 return -ENOMEM; 84 ret = device_bind_driver(parent, drv_name, str, &dev); 85 if (ret) 86 return ret; 87 device_set_name_alloced(dev); 88 *devp = dev; 89 90 return 0; 91} 92 93int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, 94 struct bootflow_iter *iter, struct bootflow *bflow) 95{ 96 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method); 97 bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART; 98 struct blk_desc *desc = dev_get_uclass_plat(blk); 99 struct disk_partition info; 100 char partstr[20]; 101 char name[60]; 102 int ret; 103 104 /* Sanity check */ 105 if (iter->part >= MAX_PART_PER_BOOTDEV) 106 return log_msg_ret("max", -ESHUTDOWN); 107 108 bflow->blk = blk; 109 if (iter->part) 110 snprintf(partstr, sizeof(partstr), "part_%x", iter->part); 111 else 112 strcpy(partstr, "whole"); 113 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr); 114 bflow->name = strdup(name); 115 if (!bflow->name) 116 return log_msg_ret("name", -ENOMEM); 117 118 bflow->part = iter->part; 119 120 ret = bootmeth_check(bflow->method, iter); 121 if (ret) 122 return log_msg_ret("check", ret); 123 124 /* 125 * partition numbers start at 0 so this cannot succeed, but it can tell 126 * us whether there is valid media there 127 */ 128 ret = part_get_info(desc, iter->part, &info); 129 log_debug("part_get_info() returned %d\n", ret); 130 if (!iter->part && ret == -ENOENT) 131 ret = 0; 132 133 /* 134 * This error indicates the media is not present. Otherwise we just 135 * blindly scan the next partition. We could be more intelligent here 136 * and check which partition numbers actually exist. 137 */ 138 if (ret == -EOPNOTSUPP) 139 ret = -ESHUTDOWN; 140 else 141 bflow->state = BOOTFLOWST_MEDIA; 142 if (ret && !allow_any_part) { 143 /* allow partition 1 to be missing */ 144 if (iter->part == 1) { 145 iter->max_part = 3; 146 ret = -ENOENT; 147 } 148 149 return log_msg_ret("part", ret); 150 } 151 152 /* 153 * Currently we don't get the number of partitions, so just 154 * assume a large number 155 */ 156 iter->max_part = MAX_PART_PER_BOOTDEV; 157 158 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) { 159 /* a particular partition was specified, scan it without checking */ 160 } else if (!iter->part) { 161 /* This is the whole disk, check if we have bootable partitions */ 162 iter->first_bootable = part_get_bootable(desc); 163 log_debug("checking bootable=%d\n", iter->first_bootable); 164 } else if (allow_any_part) { 165 /* 166 * allow any partition to be scanned, by skipping any checks 167 * for filesystems or partition contents on this disk 168 */ 169 170 /* if there are bootable partitions, scan only those */ 171 } else if (iter->first_bootable >= 0 && 172 (iter->first_bootable ? !info.bootable : iter->part != 1)) { 173 return log_msg_ret("boot", -EINVAL); 174 } else { 175 ret = fs_set_blk_dev_with_part(desc, bflow->part); 176 bflow->state = BOOTFLOWST_PART; 177 if (ret) 178 return log_msg_ret("fs", ret); 179 180 log_debug("%s: Found partition %x type %x fstype %d\n", 181 blk->name, bflow->part, 182 IS_ENABLED(CONFIG_DOS_PARTITION) ? 183 disk_partition_sys_ind(&info) : 0, 184 ret ? -1 : fs_get_type()); 185 bflow->blk = blk; 186 bflow->state = BOOTFLOWST_FS; 187 } 188 189 log_debug("method %s\n", bflow->method->name); 190 ret = bootmeth_read_bootflow(bflow->method, bflow); 191 if (ret) 192 return log_msg_ret("method", ret); 193 194 return 0; 195} 196 197void bootdev_list(bool probe) 198{ 199 struct udevice *dev; 200 int ret; 201 int i; 202 203 printf("Seq Probed Status Uclass Name\n"); 204 printf("--- ------ ------ -------- ------------------\n"); 205 if (probe) 206 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev); 207 else 208 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev); 209 for (i = 0; dev; i++) { 210 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev), 211 device_active(dev) ? '+' : ' ', 212 ret ? simple_itoa(-ret) : "OK", 213 dev_get_uclass_name(dev_get_parent(dev)), dev->name); 214 if (probe) 215 ret = uclass_next_device_check(&dev); 216 else 217 ret = uclass_find_next_device(&dev); 218 } 219 printf("--- ------ ------ -------- ------------------\n"); 220 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : ""); 221} 222 223int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name) 224{ 225 struct udevice *bdev; 226 int ret; 227 228 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, 229 &bdev); 230 if (ret) { 231 if (ret != -ENODEV) { 232 log_debug("Cannot access bootdev device\n"); 233 return ret; 234 } 235 236 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev); 237 if (ret) { 238 log_debug("Cannot create bootdev device\n"); 239 return ret; 240 } 241 } 242 243 return 0; 244} 245 246static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix) 247{ 248 int len, slen; 249 250 len = strlen(dev->name); 251 slen = strlen(suffix); 252 if (len > slen && !strcmp(suffix, dev->name + len - slen)) 253 return len - slen; 254 255 return len; 256} 257 258int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name) 259{ 260 struct udevice *parent, *dev; 261 char dev_name[50]; 262 int ret, len; 263 264 len = bootdev_get_suffix_start(blk, ".blk"); 265 if (xpl_phase() < PHASE_BOARD_R) { 266 strlcpy(dev_name, blk->name, sizeof(dev_name) - 5); 267 strcat(dev_name, ".sib"); 268 } else { 269 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name, 270 "bootdev"); 271 } 272 273 parent = dev_get_parent(blk); 274 ret = device_find_child_by_name(parent, dev_name, &dev); 275 if (ret) { 276 char *str; 277 278 if (ret != -ENODEV) { 279 log_debug("Cannot access bootdev device\n"); 280 return ret; 281 } 282 str = strdup(dev_name); 283 if (!str) 284 return -ENOMEM; 285 286 ret = device_bind_driver(parent, drv_name, str, &dev); 287 if (ret) { 288 log_debug("Cannot create bootdev device\n"); 289 return ret; 290 } 291 device_set_name_alloced(dev); 292 } 293 294 return 0; 295} 296 297int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp) 298{ 299 struct udevice *parent = dev_get_parent(dev); 300 struct udevice *blk; 301 int ret, len; 302 303 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV) 304 return -EINVAL; 305 306 /* 307 * This should always work if bootdev_setup_for_sibling_blk() was used 308 */ 309 len = bootdev_get_suffix_start(dev, ".bootdev"); 310 ret = device_find_child_by_namelen(parent, dev->name, len, &blk); 311 if (ret) { 312 char dev_name[50]; 313 314 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len, 315 dev->name); 316 ret = device_find_child_by_name(parent, dev_name, &blk); 317 if (ret) 318 return log_msg_ret("find", ret); 319 } 320 ret = device_probe(blk); 321 if (ret) 322 return log_msg_ret("act", ret); 323 *blkp = blk; 324 325 return 0; 326} 327 328int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp) 329{ 330 struct udevice *parent = dev_get_parent(blk); 331 struct udevice *bootdev; 332 char dev_name[50]; 333 int ret, len; 334 335 if (device_get_uclass_id(blk) != UCLASS_BLK) 336 return -EINVAL; 337 338 /* This should always work if bootdev_setup_for_sibling_blk() was used */ 339 len = bootdev_get_suffix_start(blk, ".blk"); 340 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name, 341 "bootdev"); 342 ret = device_find_child_by_name(parent, dev_name, &bootdev); 343 if (ret) 344 return log_msg_ret("find", ret); 345 *bootdevp = bootdev; 346 347 return 0; 348} 349 350int bootdev_unbind_dev(struct udevice *parent) 351{ 352 struct udevice *dev; 353 int ret; 354 355 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev); 356 if (!ret) { 357 ret = device_remove(dev, DM_REMOVE_NORMAL); 358 if (ret) 359 return log_msg_ret("rem", ret); 360 ret = device_unbind(dev); 361 if (ret) 362 return log_msg_ret("unb", ret); 363 } 364 365 return 0; 366} 367 368/** 369 * label_to_uclass() - Convert a label to a uclass and sequence number 370 * 371 * @label: Label to look up (e.g. "mmc1" or "mmc0") 372 * @seqp: Returns the sequence number, or -1 if none 373 * @method_flagsp: If non-NULL, returns any flags implied by the label 374 * (enum bootflow_meth_flags_t), 0 if none 375 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not 376 * known, other -ve error code on other error 377 */ 378static int label_to_uclass(const char *label, int *seqp, int *method_flagsp) 379{ 380 int seq, len, method_flags; 381 enum uclass_id id; 382 const char *end; 383 384 method_flags = 0; 385 seq = trailing_strtoln_end(label, NULL, &end); 386 len = end - label; 387 if (!len) 388 return -EINVAL; 389 id = uclass_get_by_namelen(label, len); 390 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id, 391 uclass_get_name(id)); 392 if (id == UCLASS_INVALID) { 393 /* try some special cases */ 394 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) && 395 !strncmp("spi", label, len)) { 396 id = UCLASS_SPI_FLASH; 397 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) && 398 !strncmp("pxe", label, len)) { 399 id = UCLASS_ETH; 400 method_flags |= BOOTFLOW_METHF_PXE_ONLY; 401 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) && 402 !strncmp("dhcp", label, len)) { 403 id = UCLASS_ETH; 404 method_flags |= BOOTFLOW_METHF_DHCP_ONLY; 405 } else { 406 return -EPFNOSUPPORT; 407 } 408 } 409 if (id == UCLASS_USB) 410 id = UCLASS_MASS_STORAGE; 411 *seqp = seq; 412 if (method_flagsp) 413 *method_flagsp = method_flags; 414 415 return id; 416} 417 418int bootdev_find_by_label(const char *label, struct udevice **devp, 419 int *method_flagsp) 420{ 421 int seq, ret, method_flags = 0; 422 struct udevice *media; 423 struct uclass *uc; 424 enum uclass_id id; 425 426 if (!CONFIG_IS_ENABLED(BLK)) 427 return -ENOSYS; 428 429 ret = label_to_uclass(label, &seq, &method_flags); 430 if (ret < 0) 431 return log_msg_ret("uc", ret); 432 id = ret; 433 434 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */ 435 uclass_id_foreach_dev(id, media, uc) { 436 struct udevice *bdev, *blk; 437 int ret; 438 439 /* if there is no seq, match anything */ 440 if (seq != -1 && dev_seq(media) != seq) { 441 log_debug("- skip, media seq=%d\n", dev_seq(media)); 442 continue; 443 } 444 445 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV, 446 &bdev); 447 if (ret) { 448 log_debug("- looking via blk, seq=%d, id=%d\n", seq, 449 id); 450 ret = blk_find_device(id, seq, &blk); 451 if (!ret) { 452 log_debug("- get from blk %s\n", blk->name); 453 ret = bootdev_get_from_blk(blk, &bdev); 454 } 455 } 456 if (!ret) { 457 log_debug("- found %s\n", bdev->name); 458 *devp = bdev; 459 460 /* 461 * if no sequence number was provided, we must scan all 462 * bootdevs for this media uclass 463 */ 464 if (seq == -1) 465 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS; 466 if (method_flagsp) 467 *method_flagsp = method_flags; 468 log_debug("method flags %x\n", method_flags); 469 return 0; 470 } 471 log_debug("- no device in %s\n", media->name); 472 } 473 474 return -ENOENT; 475} 476 477int bootdev_find_by_any(const char *name, struct udevice **devp, 478 int *method_flagsp) 479{ 480 struct udevice *dev; 481 int method_flags = 0; 482 int ret = -ENODEV, seq; 483 char *endp; 484 485 seq = simple_strtol(name, &endp, 16); 486 487 /* Select by name, label or number */ 488 if (*endp) { 489 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev); 490 if (ret == -ENODEV) { 491 ret = bootdev_find_by_label(name, &dev, &method_flags); 492 if (ret) { 493 printf("Cannot find bootdev '%s' (err=%d)\n", 494 name, ret); 495 return log_msg_ret("lab", ret); 496 } 497 ret = device_probe(dev); 498 } 499 if (ret) { 500 printf("Cannot probe bootdev '%s' (err=%d)\n", name, 501 ret); 502 return log_msg_ret("pro", ret); 503 } 504 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { 505 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev); 506 method_flags |= BOOTFLOW_METHF_SINGLE_DEV; 507 } 508 if (ret) { 509 printf("Cannot find '%s' (err=%d)\n", name, ret); 510 return ret; 511 } 512 513 *devp = dev; 514 if (method_flagsp) 515 *method_flagsp = method_flags; 516 517 return 0; 518} 519 520int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp, 521 int *method_flagsp) 522{ 523 int ret; 524 525 ret = bootdev_hunt(label, false); 526 if (ret) 527 return log_msg_ret("scn", ret); 528 ret = bootdev_find_by_label(label, devp, method_flagsp); 529 if (ret) 530 return log_msg_ret("fnd", ret); 531 532 return 0; 533} 534 535static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, 536 struct bootflow *bflow) 537{ 538 struct udevice *blk; 539 int ret; 540 541 ret = bootdev_get_sibling_blk(dev, &blk); 542 log_debug("sibling_blk ret=%d, blk=%s\n", ret, 543 ret ? "(none)" : blk->name); 544 /* 545 * If there is no media, indicate that no more partitions should be 546 * checked 547 */ 548 if (ret == -EOPNOTSUPP) 549 ret = -ESHUTDOWN; 550 if (ret) 551 return log_msg_ret("blk", ret); 552 assert(blk); 553 ret = bootdev_find_in_blk(dev, blk, iter, bflow); 554 if (ret) 555 return log_msg_ret("find", ret); 556 557 return 0; 558} 559 560int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, 561 struct bootflow *bflow) 562{ 563 const struct bootdev_ops *ops = bootdev_get_ops(dev); 564 565 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part, 566 ops->get_bootflow); 567 bootflow_init(bflow, dev, iter->method); 568 if (!ops->get_bootflow) 569 return default_get_bootflow(dev, iter, bflow); 570 571 return ops->get_bootflow(dev, iter, bflow); 572} 573 574int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp, 575 int *method_flagsp) 576{ 577 struct udevice *dev; 578 579 log_debug("next\n"); 580 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) { 581 const char *label = iter->labels[iter->cur_label]; 582 int ret; 583 584 log_debug("Scanning: %s\n", label); 585 ret = bootdev_hunt_and_find_by_label(label, &dev, 586 method_flagsp); 587 if (iter->flags & BOOTFLOWIF_SHOW) { 588 if (ret == -EPFNOSUPPORT) { 589 log_warning("Unknown uclass '%s' in label\n", 590 label); 591 } else if (ret == -ENOENT) { 592 /* 593 * looking for, e.g. 'scsi0' should find 594 * something if SCSI is present 595 */ 596 if (!trailing_strtol(label)) { 597 log_warning("No bootdevs for '%s'\n", 598 label); 599 } 600 } 601 } 602 603 } 604 605 if (!dev) 606 return log_msg_ret("fin", -ENODEV); 607 *devp = dev; 608 609 return 0; 610} 611 612int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp) 613{ 614 struct udevice *dev = *devp; 615 bool found; 616 int ret; 617 618 /* find the next device with this priority */ 619 *devp = NULL; 620 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev, 621 dev ? dev->name : "none"); 622 found = false; 623 do { 624 /* 625 * Don't probe devices here since they may not be of the 626 * required priority 627 */ 628 if (!dev) 629 uclass_find_first_device(UCLASS_BOOTDEV, &dev); 630 else 631 uclass_find_next_device(&dev); 632 found = false; 633 634 /* scan for the next device with the correct priority */ 635 while (dev) { 636 struct bootdev_uc_plat *plat; 637 638 plat = dev_get_uclass_plat(dev); 639 log_debug("- %s: %d, want %d\n", dev->name, plat->prio, 640 iter->cur_prio); 641 if (plat->prio == iter->cur_prio) 642 break; 643 uclass_find_next_device(&dev); 644 } 645 646 /* none found for this priority, so move to the next */ 647 if (!dev) { 648 log_debug("None found at prio %d, moving to %d\n", 649 iter->cur_prio, iter->cur_prio + 1); 650 if (++iter->cur_prio == BOOTDEVP_COUNT) 651 return log_msg_ret("fin", -ENODEV); 652 653 if (iter->flags & BOOTFLOWIF_HUNT) { 654 /* hunt to find new bootdevs */ 655 ret = bootdev_hunt_prio(iter->cur_prio, 656 iter->flags & 657 BOOTFLOWIF_SHOW); 658 log_debug("- bootdev_hunt_prio() ret %d\n", 659 ret); 660 if (ret) 661 return log_msg_ret("hun", ret); 662 } 663 } else { 664 ret = device_probe(dev); 665 if (ret) 666 log_debug("Device '%s' failed to probe\n", 667 dev->name); 668 else 669 found = true; 670 } 671 } while (!found); 672 673 *devp = dev; 674 675 return 0; 676} 677 678int bootdev_setup_iter(struct bootflow_iter *iter, const char *label, 679 struct udevice **devp, int *method_flagsp) 680{ 681 struct udevice *bootstd, *dev = NULL; 682 bool show = iter->flags & BOOTFLOWIF_SHOW; 683 int method_flags; 684 char buf[32]; 685 int ret; 686 687 if (label) { 688 const char *end = strchr(label, ':'); 689 690 if (end) { 691 size_t len = (size_t)(end - label); 692 const char *part = end + 1; 693 694 if (len + 1 > sizeof(buf)) { 695 log_err("label \"%s\" is way too long\n", label); 696 return -EINVAL; 697 } 698 699 memcpy(buf, label, len); 700 buf[len] = '\0'; 701 label = buf; 702 703 unsigned long tmp; 704 705 if (strict_strtoul(part, 0, &tmp)) { 706 log_err("Invalid partition number: %s\n", part); 707 return -EINVAL; 708 } 709 710 iter->flags |= BOOTFLOWIF_SINGLE_PARTITION; 711 iter->part = tmp; 712 } 713 } 714 715 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); 716 if (ret) { 717 log_err("Missing bootstd device\n"); 718 return log_msg_ret("std", ret); 719 } 720 721 /* hunt for any pre-scan devices */ 722 if (iter->flags & BOOTFLOWIF_HUNT) { 723 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show); 724 log_debug("- bootdev_hunt_prio() ret %d\n", ret); 725 if (ret) 726 return log_msg_ret("pre", ret); 727 } 728 729 /* Handle scanning a single device */ 730 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) { 731 if (iter->flags & BOOTFLOWIF_HUNT) { 732 ret = bootdev_hunt(label, show); 733 if (ret) 734 return log_msg_ret("hun", ret); 735 } 736 ret = bootdev_find_by_any(label, &dev, &method_flags); 737 if (ret) 738 return log_msg_ret("lab", ret); 739 740 log_debug("method_flags: %x\n", method_flags); 741 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS) 742 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS; 743 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV) 744 iter->flags |= BOOTFLOWIF_SINGLE_DEV; 745 else 746 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA; 747 log_debug("Selected label: %s, flags %x\n", label, iter->flags); 748 } else { 749 bool ok; 750 751 /* This either returns a non-empty list or NULL */ 752 iter->labels = bootstd_get_bootdev_order(bootstd, &ok); 753 if (!ok) 754 return log_msg_ret("ord", -ENOMEM); 755 log_debug("setup labels %p\n", iter->labels); 756 if (iter->labels) { 757 iter->cur_label = -1; 758 ret = bootdev_next_label(iter, &dev, &method_flags); 759 } else { 760 ret = bootdev_next_prio(iter, &dev); 761 method_flags = 0; 762 } 763 if (!dev) 764 return log_msg_ret("fin", -ENOENT); 765 log_debug("Selected bootdev: %s\n", dev->name); 766 } 767 768 ret = device_probe(dev); 769 if (ret) 770 return log_msg_ret("probe", ret); 771 if (method_flagsp) 772 *method_flagsp = method_flags; 773 *devp = dev; 774 775 return 0; 776} 777 778static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show) 779{ 780 const char *name = uclass_get_name(info->uclass); 781 struct bootstd_priv *std; 782 int ret; 783 784 ret = bootstd_get_priv(&std); 785 if (ret) 786 return log_msg_ret("std", ret); 787 788 if (!(std->hunters_used & BIT(seq))) { 789 if (show) 790 printf("Hunting with: %s\n", 791 uclass_get_name(info->uclass)); 792 log_debug("Hunting with: %s\n", name); 793 if (info->hunt) { 794 ret = info->hunt(info, show); 795 log_debug(" - hunt result %d\n", ret); 796 if (ret && ret != -ENOENT) 797 return ret; 798 } 799 std->hunters_used |= BIT(seq); 800 } 801 802 return 0; 803} 804 805int bootdev_hunt(const char *spec, bool show) 806{ 807 struct bootdev_hunter *start; 808 const char *end; 809 int n_ent, i; 810 int result; 811 size_t len; 812 813 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); 814 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); 815 result = 0; 816 817 len = SIZE_MAX; 818 if (spec) { 819 trailing_strtoln_end(spec, NULL, &end); 820 len = end - spec; 821 } 822 823 for (i = 0; i < n_ent; i++) { 824 struct bootdev_hunter *info = start + i; 825 const char *name = uclass_get_name(info->uclass); 826 int ret; 827 828 log_debug("looking at %.*s for %s\n", 829 (int)max(strlen(name), len), spec, name); 830 if (spec && strncmp(spec, name, max(strlen(name), len))) { 831 if (info->uclass != UCLASS_ETH || 832 (strcmp("dhcp", spec) && strcmp("pxe", spec))) 833 continue; 834 } 835 ret = bootdev_hunt_drv(info, i, show); 836 if (ret) 837 result = ret; 838 } 839 840 return result; 841} 842 843int bootdev_unhunt(enum uclass_id id) 844{ 845 struct bootdev_hunter *start; 846 int n_ent, i; 847 848 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); 849 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); 850 for (i = 0; i < n_ent; i++) { 851 struct bootdev_hunter *info = start + i; 852 853 if (info->uclass == id) { 854 struct bootstd_priv *std; 855 int ret; 856 857 ret = bootstd_get_priv(&std); 858 if (ret) 859 return log_msg_ret("std", ret); 860 if (!(std->hunters_used & BIT(i))) 861 return -EALREADY; 862 std->hunters_used &= ~BIT(i); 863 return 0; 864 } 865 } 866 867 return -ENOENT; 868} 869 870int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show) 871{ 872 struct bootdev_hunter *start; 873 int n_ent, i; 874 int result; 875 876 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter); 877 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); 878 result = 0; 879 880 log_debug("Hunting for priority %d\n", prio); 881 for (i = 0; i < n_ent; i++) { 882 struct bootdev_hunter *info = start + i; 883 int ret; 884 885 if (prio != info->prio) 886 continue; 887 ret = bootdev_hunt_drv(info, i, show); 888 log_debug("bootdev_hunt_drv() return %d\n", ret); 889 if (ret && ret != -ENOENT) 890 result = ret; 891 } 892 log_debug("exit %d\n", result); 893 894 return result; 895} 896 897void bootdev_list_hunters(struct bootstd_priv *std) 898{ 899 struct bootdev_hunter *orig, *start; 900 int n_ent, i; 901 902 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter); 903 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter); 904 905 /* 906 * workaround for strange bug in clang-12 which sees all the below data 907 * as zeroes. Any access of start seems to fix it, such as 908 * 909 * printf("%p", start); 910 * 911 * Use memcpy() to force the correct behaviour. 912 */ 913 memcpy(&start, &orig, sizeof(orig)); 914 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter"); 915 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------"); 916 for (i = 0; i < n_ent; i++) { 917 struct bootdev_hunter *info = start + i; 918 919 printf("%4d %4s %-15s %s\n", info->prio, 920 std->hunters_used & BIT(i) ? "*" : "", 921 uclass_get_name(info->uclass), 922 info->drv ? info->drv->name : "(none)"); 923 } 924 925 printf("(total hunters: %d)\n", n_ent); 926} 927 928static int bootdev_pre_unbind(struct udevice *dev) 929{ 930 int ret; 931 932 ret = bootstd_clear_bootflows_for_bootdev(dev); 933 if (ret) 934 return log_msg_ret("bun", ret); 935 936 return 0; 937} 938 939UCLASS_DRIVER(bootdev) = { 940 .id = UCLASS_BOOTDEV, 941 .name = "bootdev", 942 .flags = DM_UC_FLAG_SEQ_ALIAS, 943 .per_device_plat_auto = sizeof(struct bootdev_uc_plat), 944 .pre_unbind = bootdev_pre_unbind, 945};