1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
19 #include <dm/device-internal.h>
21 #include <dm/uclass-internal.h>
25 * Set some sort of limit on the number of partitions a bootdev can
26 * have. Note that for disks this limits the partitions numbers that
27 * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV
29 MAX_PART_PER_BOOTDEV = 30,
31 /* Maximum supported length of the "boot_targets" env string */
32 BOOT_TARGETS_MAX_LEN = 100,
35 int bootdev_add_bootflow(struct bootflow *bflow)
37 struct bootstd_priv *std;
41 ret = bootstd_get_priv(&std);
45 new = malloc(sizeof(*bflow));
47 return log_msg_ret("bflow", -ENOMEM);
48 memcpy(new, bflow, sizeof(*bflow));
50 list_add_tail(&new->glob_node, &std->glob_head);
52 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
54 list_add_tail(&new->bm_node, &ucp->bootflow_head);
60 int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
62 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
64 if (list_empty(&ucp->bootflow_head))
67 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
73 int bootdev_next_bootflow(struct bootflow **bflowp)
75 struct bootflow *bflow = *bflowp;
76 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
80 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
83 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
88 int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
89 struct udevice **devp)
96 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
97 str = strdup(dev_name);
100 ret = device_bind_driver(parent, drv_name, str, &dev);
103 device_set_name_alloced(dev);
109 int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
110 struct bootflow_iter *iter, struct bootflow *bflow)
112 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method);
113 bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART;
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 log_debug("part_get_info() returned %d\n", ret);
146 if (!iter->part && ret == -ENOENT)
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.
154 if (ret == -EOPNOTSUPP)
157 bflow->state = BOOTFLOWST_MEDIA;
158 if (ret && !allow_any_part) {
159 /* allow partition 1 to be missing */
160 if (iter->part == 1) {
165 return log_msg_ret("part", ret);
169 * Currently we don't get the number of partitions, so just
170 * assume a large number
172 iter->max_part = MAX_PART_PER_BOOTDEV;
174 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) {
175 /* a particular partition was specified, scan it without checking */
176 } else if (!iter->part) {
177 /* This is the whole disk, check if we have bootable partitions */
178 iter->first_bootable = part_get_bootable(desc);
179 log_debug("checking bootable=%d\n", iter->first_bootable);
180 } else if (allow_any_part) {
182 * allow any partition to be scanned, by skipping any checks
183 * for filesystems or partition contents on this disk
186 /* if there are bootable partitions, scan only those */
187 } else if (iter->first_bootable >= 0 &&
188 (iter->first_bootable ? !info.bootable : iter->part != 1)) {
189 return log_msg_ret("boot", -EINVAL);
191 ret = fs_set_blk_dev_with_part(desc, bflow->part);
192 bflow->state = BOOTFLOWST_PART;
194 return log_msg_ret("fs", ret);
196 log_debug("%s: Found partition %x type %x fstype %d\n",
197 blk->name, bflow->part,
198 IS_ENABLED(CONFIG_DOS_PARTITION) ?
199 disk_partition_sys_ind(&info) : 0,
200 ret ? -1 : fs_get_type());
202 bflow->state = BOOTFLOWST_FS;
205 log_debug("method %s\n", bflow->method->name);
206 ret = bootmeth_read_bootflow(bflow->method, bflow);
208 return log_msg_ret("method", ret);
213 void bootdev_list(bool probe)
219 printf("Seq Probed Status Uclass Name\n");
220 printf("--- ------ ------ -------- ------------------\n");
222 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
224 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
225 for (i = 0; dev; i++) {
226 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
227 device_active(dev) ? '+' : ' ',
228 ret ? simple_itoa(-ret) : "OK",
229 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
231 ret = uclass_next_device_check(&dev);
233 ret = uclass_find_next_device(&dev);
235 printf("--- ------ ------ -------- ------------------\n");
236 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
239 int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
241 struct udevice *bdev;
244 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
247 if (ret != -ENODEV) {
248 log_debug("Cannot access bootdev device\n");
252 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
254 log_debug("Cannot create bootdev device\n");
262 static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
266 len = strlen(dev->name);
267 slen = strlen(suffix);
268 if (len > slen && !strcmp(suffix, dev->name + len - slen))
274 int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
276 struct udevice *parent, *dev;
280 len = bootdev_get_suffix_start(blk, ".blk");
281 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
284 parent = dev_get_parent(blk);
285 ret = device_find_child_by_name(parent, dev_name, &dev);
289 if (ret != -ENODEV) {
290 log_debug("Cannot access bootdev device\n");
293 str = strdup(dev_name);
297 ret = device_bind_driver(parent, drv_name, str, &dev);
299 log_debug("Cannot create bootdev device\n");
302 device_set_name_alloced(dev);
308 int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
310 struct udevice *parent = dev_get_parent(dev);
314 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
318 * This should always work if bootdev_setup_for_sibling_blk() was used
320 len = bootdev_get_suffix_start(dev, ".bootdev");
321 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
325 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
327 ret = device_find_child_by_name(parent, dev_name, &blk);
329 return log_msg_ret("find", ret);
331 ret = device_probe(blk);
333 return log_msg_ret("act", ret);
339 static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
341 struct udevice *parent = dev_get_parent(blk);
342 struct udevice *bootdev;
346 if (device_get_uclass_id(blk) != UCLASS_BLK)
349 /* This should always work if bootdev_setup_for_sibling_blk() was used */
350 len = bootdev_get_suffix_start(blk, ".blk");
351 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
353 ret = device_find_child_by_name(parent, dev_name, &bootdev);
355 return log_msg_ret("find", ret);
361 int bootdev_unbind_dev(struct udevice *parent)
366 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
368 ret = device_remove(dev, DM_REMOVE_NORMAL);
370 return log_msg_ret("rem", ret);
371 ret = device_unbind(dev);
373 return log_msg_ret("unb", ret);
380 * label_to_uclass() - Convert a label to a uclass and sequence number
382 * @label: Label to look up (e.g. "mmc1" or "mmc0")
383 * @seqp: Returns the sequence number, or -1 if none
384 * @method_flagsp: If non-NULL, returns any flags implied by the label
385 * (enum bootflow_meth_flags_t), 0 if none
386 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
387 * known, other -ve error code on other error
389 static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
391 int seq, len, method_flags;
396 seq = trailing_strtoln_end(label, NULL, &end);
400 id = uclass_get_by_namelen(label, len);
401 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
402 uclass_get_name(id));
403 if (id == UCLASS_INVALID) {
404 /* try some special cases */
405 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
406 !strncmp("spi", label, len)) {
407 id = UCLASS_SPI_FLASH;
408 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
409 !strncmp("pxe", label, len)) {
411 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
412 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
413 !strncmp("dhcp", label, len)) {
415 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
417 return -EPFNOSUPPORT;
420 if (id == UCLASS_USB)
421 id = UCLASS_MASS_STORAGE;
424 *method_flagsp = method_flags;
429 int bootdev_find_by_label(const char *label, struct udevice **devp,
432 int seq, ret, method_flags = 0;
433 struct udevice *media;
437 ret = label_to_uclass(label, &seq, &method_flags);
439 return log_msg_ret("uc", ret);
442 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
443 uclass_id_foreach_dev(id, media, uc) {
444 struct udevice *bdev, *blk;
447 /* if there is no seq, match anything */
448 if (seq != -1 && dev_seq(media) != seq) {
449 log_debug("- skip, media seq=%d\n", dev_seq(media));
453 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
456 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
458 ret = blk_find_device(id, seq, &blk);
460 log_debug("- get from blk %s\n", blk->name);
461 ret = bootdev_get_from_blk(blk, &bdev);
465 log_debug("- found %s\n", bdev->name);
469 * if no sequence number was provided, we must scan all
470 * bootdevs for this media uclass
473 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
475 *method_flagsp = method_flags;
476 log_debug("method flags %x\n", method_flags);
479 log_debug("- no device in %s\n", media->name);
485 int bootdev_find_by_any(const char *name, struct udevice **devp,
489 int method_flags = 0;
490 int ret = -ENODEV, seq;
493 seq = simple_strtol(name, &endp, 16);
495 /* Select by name, label or number */
497 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
498 if (ret == -ENODEV) {
499 ret = bootdev_find_by_label(name, &dev, &method_flags);
501 printf("Cannot find bootdev '%s' (err=%d)\n",
503 return log_msg_ret("lab", ret);
505 ret = device_probe(dev);
508 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
510 return log_msg_ret("pro", ret);
512 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
513 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
514 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
517 printf("Cannot find '%s' (err=%d)\n", name, ret);
523 *method_flagsp = method_flags;
528 int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
533 ret = bootdev_hunt(label, false);
535 return log_msg_ret("scn", ret);
536 ret = bootdev_find_by_label(label, devp, method_flagsp);
538 return log_msg_ret("fnd", ret);
543 static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
544 struct bootflow *bflow)
549 ret = bootdev_get_sibling_blk(dev, &blk);
550 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
551 ret ? "(none)" : blk->name);
553 * If there is no media, indicate that no more partitions should be
556 if (ret == -EOPNOTSUPP)
559 return log_msg_ret("blk", ret);
561 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
563 return log_msg_ret("find", ret);
568 int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
569 struct bootflow *bflow)
571 const struct bootdev_ops *ops = bootdev_get_ops(dev);
573 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
575 bootflow_init(bflow, dev, iter->method);
576 if (!ops->get_bootflow)
577 return default_get_bootflow(dev, iter, bflow);
579 return ops->get_bootflow(dev, iter, bflow);
582 void bootdev_clear_bootflows(struct udevice *dev)
584 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
586 while (!list_empty(&ucp->bootflow_head)) {
587 struct bootflow *bflow;
589 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
591 bootflow_remove(bflow);
595 int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
601 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
602 const char *label = iter->labels[iter->cur_label];
605 log_debug("Scanning: %s\n", label);
606 ret = bootdev_hunt_and_find_by_label(label, &dev,
608 if (iter->flags & BOOTFLOWIF_SHOW) {
609 if (ret == -EPFNOSUPPORT) {
610 log_warning("Unknown uclass '%s' in label\n",
612 } else if (ret == -ENOENT) {
614 * looking for, e.g. 'scsi0' should find
615 * something if SCSI is present
617 if (!trailing_strtol(label)) {
618 log_warning("No bootdevs for '%s'\n",
627 return log_msg_ret("fin", -ENODEV);
633 int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
635 struct udevice *dev = *devp, *last_dev = NULL;
639 /* find the next device with this priority */
641 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
642 dev ? dev->name : "none");
645 * Don't probe devices here since they may not be of the
649 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
651 uclass_find_next_device(&dev);
654 /* scan for the next device with the correct priority */
656 struct bootdev_uc_plat *plat;
658 plat = dev_get_uclass_plat(dev);
659 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
661 if (plat->prio == iter->cur_prio)
663 uclass_find_next_device(&dev);
666 /* none found for this priority, so move to the next */
668 log_debug("None found at prio %d, moving to %d\n",
669 iter->cur_prio, iter->cur_prio + 1);
670 if (++iter->cur_prio == BOOTDEVP_COUNT)
671 return log_msg_ret("fin", -ENODEV);
673 if (iter->flags & BOOTFLOWIF_HUNT) {
674 /* hunt to find new bootdevs */
675 ret = bootdev_hunt_prio(iter->cur_prio,
678 log_debug("- bootdev_hunt_prio() ret %d\n",
681 return log_msg_ret("hun", ret);
684 ret = device_probe(dev);
688 log_warning("Device '%s' failed to probe\n",
690 if (last_dev == dev) {
692 * We have already tried this device
693 * and it failed to probe. Give up.
695 return log_msg_ret("probe", ret);
708 int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
709 struct udevice **devp, int *method_flagsp)
711 struct udevice *bootstd, *dev = NULL;
712 bool show = iter->flags & BOOTFLOWIF_SHOW;
718 const char *end = strchr(label, ':');
721 size_t len = (size_t)(end - label);
722 const char *part = end + 1;
724 if (len + 1 > sizeof(buf)) {
725 log_err("label \"%s\" is way too long\n", label);
729 memcpy(buf, label, len);
735 if (strict_strtoul(part, 0, &tmp)) {
736 log_err("Invalid partition number: %s\n", part);
740 iter->flags |= BOOTFLOWIF_SINGLE_PARTITION;
745 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
747 log_err("Missing bootstd device\n");
748 return log_msg_ret("std", ret);
751 /* hunt for any pre-scan devices */
752 if (iter->flags & BOOTFLOWIF_HUNT) {
753 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
754 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
756 return log_msg_ret("pre", ret);
759 /* Handle scanning a single device */
760 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
761 if (iter->flags & BOOTFLOWIF_HUNT) {
762 ret = bootdev_hunt(label, show);
764 return log_msg_ret("hun", ret);
766 ret = bootdev_find_by_any(label, &dev, &method_flags);
768 return log_msg_ret("lab", ret);
770 log_debug("method_flags: %x\n", method_flags);
771 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
772 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
773 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
774 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
776 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
777 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
781 /* This either returns a non-empty list or NULL */
782 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
784 return log_msg_ret("ord", -ENOMEM);
785 log_debug("setup labels %p\n", iter->labels);
787 iter->cur_label = -1;
788 ret = bootdev_next_label(iter, &dev, &method_flags);
790 ret = bootdev_next_prio(iter, &dev);
794 return log_msg_ret("fin", -ENOENT);
795 log_debug("Selected bootdev: %s\n", dev->name);
798 ret = device_probe(dev);
800 return log_msg_ret("probe", ret);
802 *method_flagsp = method_flags;
808 static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
810 const char *name = uclass_get_name(info->uclass);
811 struct bootstd_priv *std;
814 ret = bootstd_get_priv(&std);
816 return log_msg_ret("std", ret);
818 if (!(std->hunters_used & BIT(seq))) {
820 printf("Hunting with: %s\n",
821 uclass_get_name(info->uclass));
822 log_debug("Hunting with: %s\n", name);
824 ret = info->hunt(info, show);
825 log_debug(" - hunt result %d\n", ret);
826 if (ret && ret != -ENOENT)
829 std->hunters_used |= BIT(seq);
835 int bootdev_hunt(const char *spec, bool show)
837 struct bootdev_hunter *start;
843 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
844 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
849 trailing_strtoln_end(spec, NULL, &end);
853 for (i = 0; i < n_ent; i++) {
854 struct bootdev_hunter *info = start + i;
855 const char *name = uclass_get_name(info->uclass);
858 log_debug("looking at %.*s for %s\n",
859 (int)max(strlen(name), len), spec, name);
860 if (spec && strncmp(spec, name, max(strlen(name), len))) {
861 if (info->uclass != UCLASS_ETH ||
862 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
865 ret = bootdev_hunt_drv(info, i, show);
873 int bootdev_unhunt(enum uclass_id id)
875 struct bootdev_hunter *start;
878 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
879 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
880 for (i = 0; i < n_ent; i++) {
881 struct bootdev_hunter *info = start + i;
883 if (info->uclass == id) {
884 struct bootstd_priv *std;
887 ret = bootstd_get_priv(&std);
889 return log_msg_ret("std", ret);
890 if (!(std->hunters_used & BIT(i)))
892 std->hunters_used &= ~BIT(i);
900 int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
902 struct bootdev_hunter *start;
906 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
907 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
910 log_debug("Hunting for priority %d\n", prio);
911 for (i = 0; i < n_ent; i++) {
912 struct bootdev_hunter *info = start + i;
915 if (prio != info->prio)
917 ret = bootdev_hunt_drv(info, i, show);
918 log_debug("bootdev_hunt_drv() return %d\n", ret);
919 if (ret && ret != -ENOENT)
922 log_debug("exit %d\n", result);
927 void bootdev_list_hunters(struct bootstd_priv *std)
929 struct bootdev_hunter *orig, *start;
932 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
933 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
936 * workaround for strange bug in clang-12 which sees all the below data
937 * as zeroes. Any access of start seems to fix it, such as
939 * printf("%p", start);
941 * Use memcpy() to force the correct behaviour.
943 memcpy(&start, &orig, sizeof(orig));
944 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
945 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
946 for (i = 0; i < n_ent; i++) {
947 struct bootdev_hunter *info = start + i;
949 printf("%4d %4s %-15s %s\n", info->prio,
950 std->hunters_used & BIT(i) ? "*" : "",
951 uclass_get_name(info->uclass),
952 info->drv ? info->drv->name : "(none)");
955 printf("(total hunters: %d)\n", n_ent);
958 static int bootdev_post_bind(struct udevice *dev)
960 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
962 INIT_LIST_HEAD(&ucp->bootflow_head);
967 static int bootdev_pre_unbind(struct udevice *dev)
969 bootdev_clear_bootflows(dev);
974 UCLASS_DRIVER(bootdev) = {
975 .id = UCLASS_BOOTDEV,
977 .flags = DM_UC_FLAG_SEQ_ALIAS,
978 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
979 .post_bind = bootdev_post_bind,
980 .pre_unbind = bootdev_pre_unbind,