1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
20 #include <dm/device-internal.h>
22 #include <dm/uclass-internal.h>
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
30 MAX_PART_PER_BOOTDEV = 30,
32 /* Maximum supported length of the "boot_targets" env string */
33 BOOT_TARGETS_MAX_LEN = 100,
36 int bootdev_add_bootflow(struct bootflow *bflow)
38 struct bootstd_priv *std;
43 ret = bootstd_get_priv(&std);
47 new = malloc(sizeof(*bflow));
49 return log_msg_ret("bflow", -ENOMEM);
50 memcpy(new, bflow, sizeof(*bflow));
52 list_add_tail(&new->glob_node, &std->glob_head);
54 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
56 list_add_tail(&new->bm_node, &ucp->bootflow_head);
62 int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
64 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
66 if (list_empty(&ucp->bootflow_head))
69 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
75 int bootdev_next_bootflow(struct bootflow **bflowp)
77 struct bootflow *bflow = *bflowp;
78 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
82 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
85 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
90 int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
91 struct udevice **devp)
98 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
99 str = strdup(dev_name);
102 ret = device_bind_driver(parent, drv_name, str, &dev);
105 device_set_name_alloced(dev);
111 int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
112 struct bootflow_iter *iter, struct bootflow *bflow)
114 struct blk_desc *desc = dev_get_uclass_plat(blk);
115 struct disk_partition info;
121 if (iter->part >= MAX_PART_PER_BOOTDEV)
122 return log_msg_ret("max", -ESHUTDOWN);
126 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
128 strcpy(partstr, "whole");
129 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
130 bflow->name = strdup(name);
132 return log_msg_ret("name", -ENOMEM);
134 bflow->part = iter->part;
136 ret = bootmeth_check(bflow->method, iter);
138 return log_msg_ret("check", ret);
141 * partition numbers start at 0 so this cannot succeed, but it can tell
142 * us whether there is valid media there
144 ret = part_get_info(desc, iter->part, &info);
145 if (!iter->part && ret == -ENOENT)
149 * This error indicates the media is not present. Otherwise we just
150 * blindly scan the next partition. We could be more intelligent here
151 * and check which partition numbers actually exist.
153 if (ret == -EOPNOTSUPP)
156 bflow->state = BOOTFLOWST_MEDIA;
158 return log_msg_ret("part", ret);
161 * Currently we don't get the number of partitions, so just
162 * assume a large number
164 iter->max_part = MAX_PART_PER_BOOTDEV;
166 /* If this is the whole disk, check if we have bootable partitions */
168 iter->first_bootable = part_get_bootable(desc);
169 log_debug("checking bootable=%d\n", iter->first_bootable);
171 /* if there are bootable partitions, scan only those */
172 } else if (iter->first_bootable ? !info.bootable : iter->part != 1) {
173 return log_msg_ret("boot", -EINVAL);
175 ret = fs_set_blk_dev_with_part(desc, bflow->part);
176 bflow->state = BOOTFLOWST_PART;
178 /* Use an #ifdef due to info.sys_ind */
179 #ifdef CONFIG_DOS_PARTITION
180 log_debug("%s: Found partition %x type %x fstype %d\n",
181 blk->name, bflow->part, info.sys_ind,
182 ret ? -1 : fs_get_type());
185 return log_msg_ret("fs", ret);
186 bflow->state = BOOTFLOWST_FS;
189 ret = bootmeth_read_bootflow(bflow->method, bflow);
191 return log_msg_ret("method", ret);
196 void bootdev_list(bool probe)
202 printf("Seq Probed Status Uclass Name\n");
203 printf("--- ------ ------ -------- ------------------\n");
205 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
207 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
208 for (i = 0; dev; i++) {
209 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
210 device_active(dev) ? '+' : ' ',
211 ret ? simple_itoa(ret) : "OK",
212 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
214 ret = uclass_next_device_check(&dev);
216 ret = uclass_find_next_device(&dev);
218 printf("--- ------ ------ -------- ------------------\n");
219 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
222 int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
224 struct udevice *bdev;
227 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
230 if (ret != -ENODEV) {
231 log_debug("Cannot access bootdev device\n");
235 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
237 log_debug("Cannot create bootdev device\n");
245 static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
249 len = strlen(dev->name);
250 slen = strlen(suffix);
251 if (len > slen && !strcmp(suffix, dev->name + len - slen))
257 int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name)
259 struct udevice *parent, *dev;
263 len = bootdev_get_suffix_start(blk, ".blk");
264 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
267 parent = dev_get_parent(blk);
268 ret = device_find_child_by_name(parent, dev_name, &dev);
272 if (ret != -ENODEV) {
273 log_debug("Cannot access bootdev device\n");
276 str = strdup(dev_name);
280 ret = device_bind_driver(parent, drv_name, str, &dev);
282 log_debug("Cannot create bootdev device\n");
285 device_set_name_alloced(dev);
291 int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
293 struct udevice *parent = dev_get_parent(dev);
297 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
300 /* This should always work if bootdev_setup_sibling_blk() was used */
301 len = bootdev_get_suffix_start(dev, ".bootdev");
302 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
306 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
308 ret = device_find_child_by_name(parent, dev_name, &blk);
310 return log_msg_ret("find", ret);
317 static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
319 struct udevice *parent = dev_get_parent(blk);
320 struct udevice *bootdev;
324 if (device_get_uclass_id(blk) != UCLASS_BLK)
327 /* This should always work if bootdev_setup_sibling_blk() was used */
328 len = bootdev_get_suffix_start(blk, ".blk");
329 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
331 ret = device_find_child_by_name(parent, dev_name, &bootdev);
333 return log_msg_ret("find", ret);
339 int bootdev_unbind_dev(struct udevice *parent)
344 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
346 ret = device_remove(dev, DM_REMOVE_NORMAL);
348 return log_msg_ret("rem", ret);
349 ret = device_unbind(dev);
351 return log_msg_ret("unb", ret);
358 * label_to_uclass() - Convert a label to a uclass and sequence number
360 * @label: Label to look up (e.g. "mmc1" or "mmc0")
361 * @seqp: Returns the sequence number, or -1 if none
362 * @method_flagsp: If non-NULL, returns any flags implied by the label
363 * (enum bootflow_meth_flags_t), 0 if none
364 * Returns: sequence number on success, else -ve error code
366 static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
368 int seq, len, method_flags;
373 seq = trailing_strtoln_end(label, NULL, &end);
377 id = uclass_get_by_namelen(label, len);
378 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
379 uclass_get_name(id));
380 if (id == UCLASS_INVALID) {
381 /* try some special cases */
382 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
383 !strncmp("spi", label, len)) {
384 id = UCLASS_SPI_FLASH;
385 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
386 !strncmp("pxe", label, len)) {
388 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
389 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
390 !strncmp("dhcp", label, len)) {
392 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
394 log_warning("Unknown uclass '%s' in label\n", label);
398 if (id == UCLASS_USB)
399 id = UCLASS_MASS_STORAGE;
402 *method_flagsp = method_flags;
407 int bootdev_find_by_label(const char *label, struct udevice **devp,
410 int seq, ret, method_flags = 0;
411 struct udevice *media;
415 ret = label_to_uclass(label, &seq, &method_flags);
417 return log_msg_ret("uc", ret);
420 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
421 uclass_id_foreach_dev(id, media, uc) {
422 struct udevice *bdev, *blk;
425 /* if there is no seq, match anything */
426 if (seq != -1 && dev_seq(media) != seq) {
427 log_debug("- skip, media seq=%d\n", dev_seq(media));
431 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
434 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
436 ret = blk_find_device(id, seq, &blk);
438 log_debug("- get from blk %s\n", blk->name);
439 ret = bootdev_get_from_blk(blk, &bdev);
443 log_debug("- found %s\n", bdev->name);
447 * if no sequence number was provided, we must scan all
448 * bootdevs for this media uclass
450 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
451 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
453 *method_flagsp = method_flags;
456 log_debug("- no device in %s\n", media->name);
458 log_warning("Unknown seq %d for label '%s'\n", seq, label);
463 int bootdev_find_by_any(const char *name, struct udevice **devp,
467 int method_flags = 0;
468 int ret = -ENODEV, seq;
471 seq = simple_strtol(name, &endp, 16);
473 /* Select by name, label or number */
475 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
476 if (ret == -ENODEV) {
477 ret = bootdev_find_by_label(name, &dev, &method_flags);
479 printf("Cannot find bootdev '%s' (err=%d)\n",
481 return log_msg_ret("lab", ret);
483 ret = device_probe(dev);
486 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
488 return log_msg_ret("pro", ret);
490 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
491 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
492 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
495 printf("Cannot find '%s' (err=%d)\n", name, ret);
501 *method_flagsp = method_flags;
506 int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
511 ret = bootdev_hunt(label, false);
513 return log_msg_ret("scn", ret);
514 ret = bootdev_find_by_label(label, devp, method_flagsp);
516 return log_msg_ret("fnd", ret);
521 static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
522 struct bootflow *bflow)
527 ret = bootdev_get_sibling_blk(dev, &blk);
529 * If there is no media, indicate that no more partitions should be
532 if (ret == -EOPNOTSUPP)
535 return log_msg_ret("blk", ret);
537 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
539 return log_msg_ret("find", ret);
544 int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
545 struct bootflow *bflow)
547 const struct bootdev_ops *ops = bootdev_get_ops(dev);
549 bootflow_init(bflow, dev, iter->method);
550 if (!ops->get_bootflow)
551 return default_get_bootflow(dev, iter, bflow);
553 return ops->get_bootflow(dev, iter, bflow);
556 void bootdev_clear_bootflows(struct udevice *dev)
558 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
560 while (!list_empty(&ucp->bootflow_head)) {
561 struct bootflow *bflow;
563 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
565 bootflow_remove(bflow);
569 int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
575 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
576 log_debug("Scanning: %s\n", iter->labels[iter->cur_label]);
577 bootdev_hunt_and_find_by_label(iter->labels[iter->cur_label],
578 &dev, method_flagsp);
582 return log_msg_ret("fin", -ENODEV);
588 int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
590 struct udevice *dev = *devp;
594 /* find the next device with this priority */
596 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
597 dev ? dev->name : "none");
600 * Don't probe devices here since they may not be of the
604 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
606 uclass_find_next_device(&dev);
609 /* scan for the next device with the correct priority */
611 struct bootdev_uc_plat *plat;
613 plat = dev_get_uclass_plat(dev);
614 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
616 if (plat->prio == iter->cur_prio)
618 uclass_find_next_device(&dev);
621 /* none found for this priority, so move to the next */
623 log_debug("None found at prio %d, moving to %d\n",
624 iter->cur_prio, iter->cur_prio + 1);
625 if (++iter->cur_prio == BOOTDEVP_COUNT)
626 return log_msg_ret("fin", -ENODEV);
628 if (iter->flags & BOOTFLOWF_HUNT) {
629 /* hunt to find new bootdevs */
630 ret = bootdev_hunt_prio(iter->cur_prio,
633 log_debug("- hunt ret %d\n", ret);
635 return log_msg_ret("hun", ret);
638 ret = device_probe(dev);
640 log_debug("Device '%s' failed to probe\n",
653 * h_cmp_bootdev() - Compare two bootdevs to find out which should go first
655 * @v1: struct udevice * of first bootdev device
656 * @v2: struct udevice * of second bootdev device
657 * Return: sort order (<0 if dev1 < dev2, ==0 if equal, >0 if dev1 > dev2)
659 static int h_cmp_bootdev(const void *v1, const void *v2)
661 const struct udevice *dev1 = *(struct udevice **)v1;
662 const struct udevice *dev2 = *(struct udevice **)v2;
663 const struct bootdev_uc_plat *ucp1 = dev_get_uclass_plat(dev1);
664 const struct bootdev_uc_plat *ucp2 = dev_get_uclass_plat(dev2);
667 /* Use priority first */
668 diff = ucp1->prio - ucp2->prio;
672 /* Fall back to seq for devices of the same priority */
673 diff = dev_seq(dev1) - dev_seq(dev2);
679 * build_order() - Build the ordered list of bootdevs to use
681 * This builds an ordered list of devices by one of three methods:
682 * - using the boot_targets environment variable, if non-empty
683 * - using the bootdev-order devicetree property, if present
684 * - sorted by priority and sequence number
686 * @bootstd: BOOTSTD device to use
687 * @order: Bootdevs listed in default order
688 * @max_count: Number of entries in @order
689 * Return: number of bootdevs found in the ordering, or -E2BIG if the
690 * boot_targets string is too long, or -EXDEV if the ordering produced 0 results
692 static int build_order(struct udevice *bootstd, struct udevice **order,
695 const char *overflow_target = NULL;
696 const char *const *labels;
701 labels = bootstd_get_bootdev_order(bootstd, &ok);
703 return log_msg_ret("ord", -ENOMEM);
708 for (i = 0; labels[i]; i++) {
709 ret = bootdev_find_by_label(labels[i], &dev, NULL);
711 if (upto == max_count) {
712 overflow_target = labels[i];
720 /* sort them into priority order */
722 qsort(order, count, sizeof(struct udevice *), h_cmp_bootdev);
725 if (overflow_target) {
726 log_warning("Expected at most %d bootdevs, but overflowed with boot_target '%s'\n",
727 max_count, overflow_target);
731 return log_msg_ret("targ", -EXDEV);
736 int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp)
738 struct udevice *bootstd, *dev = *devp, **order;
739 bool show = iter->flags & BOOTFLOWF_SHOW;
744 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
746 log_err("Missing bootstd device\n");
747 return log_msg_ret("std", ret);
750 /* hunt for any pre-scan devices */
751 if (iter->flags & BOOTFLOWF_HUNT) {
752 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
754 return log_msg_ret("pre", ret);
757 /* Handle scanning a single device */
759 iter->flags |= BOOTFLOWF_SINGLE_DEV;
763 count = uclass_id_count(UCLASS_BOOTDEV);
765 return log_msg_ret("count", -ENOENT);
767 order = calloc(count, sizeof(struct udevice *));
769 return log_msg_ret("order", -ENOMEM);
771 /* Get the list of bootdevs */
772 uclass_id_foreach_dev(UCLASS_BOOTDEV, dev, uc)
774 log_debug("Found %d bootdevs\n", count);
776 log_debug("Expected %d bootdevs, found %d using aliases\n",
779 ret = build_order(bootstd, order, upto);
782 return log_msg_ret("build", ret);
785 iter->num_devs = ret;
786 iter->dev_order = order;
790 ret = device_probe(dev);
792 return log_msg_ret("probe", ret);
798 static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
800 const char *name = uclass_get_name(info->uclass);
801 struct bootstd_priv *std;
804 ret = bootstd_get_priv(&std);
806 return log_msg_ret("std", ret);
808 if (!(std->hunters_used & BIT(seq))) {
810 printf("Hunting with: %s\n",
811 uclass_get_name(info->uclass));
812 log_debug("Hunting with: %s\n", name);
814 ret = info->hunt(info, show);
818 std->hunters_used |= BIT(seq);
824 int bootdev_hunt(const char *spec, bool show)
826 struct bootdev_hunter *start;
832 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
833 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
838 trailing_strtoln_end(spec, NULL, &end);
842 for (i = 0; i < n_ent; i++) {
843 struct bootdev_hunter *info = start + i;
844 const char *name = uclass_get_name(info->uclass);
847 log_debug("looking at %.*s for %s\n",
848 (int)max(strlen(name), len), spec, name);
849 if (spec && strncmp(spec, name, max(strlen(name), len))) {
850 if (info->uclass != UCLASS_ETH ||
851 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
854 ret = bootdev_hunt_drv(info, i, show);
862 int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
864 struct bootdev_hunter *start;
868 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
869 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
872 log_debug("Hunting for priority %d\n", prio);
873 for (i = 0; i < n_ent; i++) {
874 struct bootdev_hunter *info = start + i;
877 if (prio != info->prio)
879 ret = bootdev_hunt_drv(info, i, show);
880 if (ret && ret != -ENOENT)
887 void bootdev_list_hunters(struct bootstd_priv *std)
889 struct bootdev_hunter *orig, *start;
892 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
893 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
896 * workaround for strange bug in clang-12 which sees all the below data
897 * as zeroes. Any access of start seems to fix it, such as
899 * printf("%p", start);
901 * Use memcpy() to force the correct behaviour.
903 memcpy(&start, &orig, sizeof(orig));
904 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
905 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
906 for (i = 0; i < n_ent; i++) {
907 struct bootdev_hunter *info = start + i;
909 printf("%4d %4s %-15s %s\n", info->prio,
910 std->hunters_used & BIT(i) ? "*" : "",
911 uclass_get_name(info->uclass),
912 info->drv ? info->drv->name : "(none)");
915 printf("(total hunters: %d)\n", n_ent);
918 static int bootdev_post_bind(struct udevice *dev)
920 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
922 INIT_LIST_HEAD(&ucp->bootflow_head);
927 static int bootdev_pre_unbind(struct udevice *dev)
929 bootdev_clear_bootflows(dev);
934 UCLASS_DRIVER(bootdev) = {
935 .id = UCLASS_BOOTDEV,
937 .flags = DM_UC_FLAG_SEQ_ALIAS,
938 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
939 .post_bind = bootdev_post_bind,
940 .pre_unbind = bootdev_pre_unbind,