1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
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 /* allow partition 1 to be missing */
159 if (iter->part == 1) {
164 return log_msg_ret("part", ret);
168 * Currently we don't get the number of partitions, so just
169 * assume a large number
171 iter->max_part = MAX_PART_PER_BOOTDEV;
173 /* If this is the whole disk, check if we have bootable partitions */
175 iter->first_bootable = part_get_bootable(desc);
176 log_debug("checking bootable=%d\n", iter->first_bootable);
178 /* if there are bootable partitions, scan only those */
179 } else if (iter->first_bootable ? !info.bootable : iter->part != 1) {
180 return log_msg_ret("boot", -EINVAL);
182 ret = fs_set_blk_dev_with_part(desc, bflow->part);
183 bflow->state = BOOTFLOWST_PART;
185 return log_msg_ret("fs", ret);
187 /* Use an #ifdef due to info.sys_ind */
188 #ifdef CONFIG_DOS_PARTITION
189 log_debug("%s: Found partition %x type %x fstype %d\n",
190 blk->name, bflow->part, info.sys_ind,
191 ret ? -1 : fs_get_type());
194 bflow->state = BOOTFLOWST_FS;
197 ret = bootmeth_read_bootflow(bflow->method, bflow);
199 return log_msg_ret("method", ret);
204 void bootdev_list(bool probe)
210 printf("Seq Probed Status Uclass Name\n");
211 printf("--- ------ ------ -------- ------------------\n");
213 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
215 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
216 for (i = 0; dev; i++) {
217 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
218 device_active(dev) ? '+' : ' ',
219 ret ? simple_itoa(-ret) : "OK",
220 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
222 ret = uclass_next_device_check(&dev);
224 ret = uclass_find_next_device(&dev);
226 printf("--- ------ ------ -------- ------------------\n");
227 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
230 int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
232 struct udevice *bdev;
235 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
238 if (ret != -ENODEV) {
239 log_debug("Cannot access bootdev device\n");
243 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
245 log_debug("Cannot create bootdev device\n");
253 static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
257 len = strlen(dev->name);
258 slen = strlen(suffix);
259 if (len > slen && !strcmp(suffix, dev->name + len - slen))
265 int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
267 struct udevice *parent, *dev;
271 len = bootdev_get_suffix_start(blk, ".blk");
272 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
275 parent = dev_get_parent(blk);
276 ret = device_find_child_by_name(parent, dev_name, &dev);
280 if (ret != -ENODEV) {
281 log_debug("Cannot access bootdev device\n");
284 str = strdup(dev_name);
288 ret = device_bind_driver(parent, drv_name, str, &dev);
290 log_debug("Cannot create bootdev device\n");
293 device_set_name_alloced(dev);
299 int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
301 struct udevice *parent = dev_get_parent(dev);
305 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
309 * This should always work if bootdev_setup_for_sibling_blk() was used
311 len = bootdev_get_suffix_start(dev, ".bootdev");
312 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
316 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
318 ret = device_find_child_by_name(parent, dev_name, &blk);
320 return log_msg_ret("find", ret);
322 ret = device_probe(blk);
324 return log_msg_ret("act", ret);
330 static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
332 struct udevice *parent = dev_get_parent(blk);
333 struct udevice *bootdev;
337 if (device_get_uclass_id(blk) != UCLASS_BLK)
340 /* This should always work if bootdev_setup_for_sibling_blk() was used */
341 len = bootdev_get_suffix_start(blk, ".blk");
342 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
344 ret = device_find_child_by_name(parent, dev_name, &bootdev);
346 return log_msg_ret("find", ret);
352 int bootdev_unbind_dev(struct udevice *parent)
357 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
359 ret = device_remove(dev, DM_REMOVE_NORMAL);
361 return log_msg_ret("rem", ret);
362 ret = device_unbind(dev);
364 return log_msg_ret("unb", ret);
371 * label_to_uclass() - Convert a label to a uclass and sequence number
373 * @label: Label to look up (e.g. "mmc1" or "mmc0")
374 * @seqp: Returns the sequence number, or -1 if none
375 * @method_flagsp: If non-NULL, returns any flags implied by the label
376 * (enum bootflow_meth_flags_t), 0 if none
377 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
378 * known, other -ve error code on other error
380 static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
382 int seq, len, method_flags;
387 seq = trailing_strtoln_end(label, NULL, &end);
391 id = uclass_get_by_namelen(label, len);
392 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
393 uclass_get_name(id));
394 if (id == UCLASS_INVALID) {
395 /* try some special cases */
396 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
397 !strncmp("spi", label, len)) {
398 id = UCLASS_SPI_FLASH;
399 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
400 !strncmp("pxe", label, len)) {
402 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
403 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
404 !strncmp("dhcp", label, len)) {
406 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
408 return -EPFNOSUPPORT;
411 if (id == UCLASS_USB)
412 id = UCLASS_MASS_STORAGE;
415 *method_flagsp = method_flags;
420 int bootdev_find_by_label(const char *label, struct udevice **devp,
423 int seq, ret, method_flags = 0;
424 struct udevice *media;
428 ret = label_to_uclass(label, &seq, &method_flags);
430 return log_msg_ret("uc", ret);
433 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
434 uclass_id_foreach_dev(id, media, uc) {
435 struct udevice *bdev, *blk;
438 /* if there is no seq, match anything */
439 if (seq != -1 && dev_seq(media) != seq) {
440 log_debug("- skip, media seq=%d\n", dev_seq(media));
444 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
447 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
449 ret = blk_find_device(id, seq, &blk);
451 log_debug("- get from blk %s\n", blk->name);
452 ret = bootdev_get_from_blk(blk, &bdev);
456 log_debug("- found %s\n", bdev->name);
460 * if no sequence number was provided, we must scan all
461 * bootdevs for this media uclass
463 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
464 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
466 *method_flagsp = method_flags;
469 log_debug("- no device in %s\n", media->name);
475 int bootdev_find_by_any(const char *name, struct udevice **devp,
479 int method_flags = 0;
480 int ret = -ENODEV, seq;
483 seq = simple_strtol(name, &endp, 16);
485 /* Select by name, label or number */
487 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
488 if (ret == -ENODEV) {
489 ret = bootdev_find_by_label(name, &dev, &method_flags);
491 printf("Cannot find bootdev '%s' (err=%d)\n",
493 return log_msg_ret("lab", ret);
495 ret = device_probe(dev);
498 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
500 return log_msg_ret("pro", ret);
502 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
503 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
504 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
507 printf("Cannot find '%s' (err=%d)\n", name, ret);
513 *method_flagsp = method_flags;
518 int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
523 ret = bootdev_hunt(label, false);
525 return log_msg_ret("scn", ret);
526 ret = bootdev_find_by_label(label, devp, method_flagsp);
528 return log_msg_ret("fnd", ret);
533 static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
534 struct bootflow *bflow)
539 ret = bootdev_get_sibling_blk(dev, &blk);
540 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
541 ret ? "(none)" : blk->name);
543 * If there is no media, indicate that no more partitions should be
546 if (ret == -EOPNOTSUPP)
549 return log_msg_ret("blk", ret);
551 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
553 return log_msg_ret("find", ret);
558 int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
559 struct bootflow *bflow)
561 const struct bootdev_ops *ops = bootdev_get_ops(dev);
563 log_debug("->get_bootflow %s=%p\n", dev->name, ops->get_bootflow);
564 bootflow_init(bflow, dev, iter->method);
565 if (!ops->get_bootflow)
566 return default_get_bootflow(dev, iter, bflow);
568 return ops->get_bootflow(dev, iter, bflow);
571 void bootdev_clear_bootflows(struct udevice *dev)
573 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
575 while (!list_empty(&ucp->bootflow_head)) {
576 struct bootflow *bflow;
578 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
580 bootflow_remove(bflow);
584 int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
590 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
591 const char *label = iter->labels[iter->cur_label];
594 log_debug("Scanning: %s\n", label);
595 ret = bootdev_hunt_and_find_by_label(label, &dev,
597 if (iter->flags & BOOTFLOWIF_SHOW) {
598 if (ret == -EPFNOSUPPORT) {
599 log_warning("Unknown uclass '%s' in label\n",
601 } else if (ret == -ENOENT) {
603 * looking for, e.g. 'scsi0' should find
604 * something if SCSI is present
606 if (!trailing_strtol(label)) {
607 log_warning("No bootdevs for '%s'\n",
616 return log_msg_ret("fin", -ENODEV);
622 int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
624 struct udevice *dev = *devp;
628 /* find the next device with this priority */
630 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
631 dev ? dev->name : "none");
634 * Don't probe devices here since they may not be of the
638 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
640 uclass_find_next_device(&dev);
643 /* scan for the next device with the correct priority */
645 struct bootdev_uc_plat *plat;
647 plat = dev_get_uclass_plat(dev);
648 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
650 if (plat->prio == iter->cur_prio)
652 uclass_find_next_device(&dev);
655 /* none found for this priority, so move to the next */
657 log_debug("None found at prio %d, moving to %d\n",
658 iter->cur_prio, iter->cur_prio + 1);
659 if (++iter->cur_prio == BOOTDEVP_COUNT)
660 return log_msg_ret("fin", -ENODEV);
662 if (iter->flags & BOOTFLOWIF_HUNT) {
663 /* hunt to find new bootdevs */
664 ret = bootdev_hunt_prio(iter->cur_prio,
667 log_debug("- bootdev_hunt_prio() ret %d\n",
670 return log_msg_ret("hun", ret);
673 ret = device_probe(dev);
675 log_debug("Device '%s' failed to probe\n",
687 int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
688 struct udevice **devp, int *method_flagsp)
690 struct udevice *bootstd, *dev = NULL;
691 bool show = iter->flags & BOOTFLOWIF_SHOW;
695 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
697 log_err("Missing bootstd device\n");
698 return log_msg_ret("std", ret);
701 /* hunt for any pre-scan devices */
702 if (iter->flags & BOOTFLOWIF_HUNT) {
703 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
704 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
706 return log_msg_ret("pre", ret);
709 /* Handle scanning a single device */
710 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
711 if (iter->flags & BOOTFLOWIF_HUNT) {
712 ret = bootdev_hunt(label, show);
714 return log_msg_ret("hun", ret);
716 ret = bootdev_find_by_any(label, &dev, &method_flags);
718 return log_msg_ret("lab", ret);
720 log_debug("method_flags: %x\n", method_flags);
721 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
722 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
723 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
724 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
726 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
727 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
731 /* This either returns a non-empty list or NULL */
732 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
734 return log_msg_ret("ord", -ENOMEM);
735 log_debug("setup labels %p\n", iter->labels);
737 iter->cur_label = -1;
738 ret = bootdev_next_label(iter, &dev, &method_flags);
740 ret = bootdev_next_prio(iter, &dev);
744 return log_msg_ret("fin", -ENOENT);
745 log_debug("Selected bootdev: %s\n", dev->name);
748 ret = device_probe(dev);
750 return log_msg_ret("probe", ret);
752 *method_flagsp = method_flags;
758 static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
760 const char *name = uclass_get_name(info->uclass);
761 struct bootstd_priv *std;
764 ret = bootstd_get_priv(&std);
766 return log_msg_ret("std", ret);
768 if (!(std->hunters_used & BIT(seq))) {
770 printf("Hunting with: %s\n",
771 uclass_get_name(info->uclass));
772 log_debug("Hunting with: %s\n", name);
774 ret = info->hunt(info, show);
775 log_debug(" - hunt result %d\n", ret);
779 std->hunters_used |= BIT(seq);
785 int bootdev_hunt(const char *spec, bool show)
787 struct bootdev_hunter *start;
793 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
794 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
799 trailing_strtoln_end(spec, NULL, &end);
803 for (i = 0; i < n_ent; i++) {
804 struct bootdev_hunter *info = start + i;
805 const char *name = uclass_get_name(info->uclass);
808 log_debug("looking at %.*s for %s\n",
809 (int)max(strlen(name), len), spec, name);
810 if (spec && strncmp(spec, name, max(strlen(name), len))) {
811 if (info->uclass != UCLASS_ETH ||
812 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
815 ret = bootdev_hunt_drv(info, i, show);
823 int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
825 struct bootdev_hunter *start;
829 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
830 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
833 log_debug("Hunting for priority %d\n", prio);
834 for (i = 0; i < n_ent; i++) {
835 struct bootdev_hunter *info = start + i;
838 if (prio != info->prio)
840 ret = bootdev_hunt_drv(info, i, show);
841 log_debug("bootdev_hunt_drv() return %d\n", ret);
842 if (ret && ret != -ENOENT)
845 log_debug("exit %d\n", result);
850 void bootdev_list_hunters(struct bootstd_priv *std)
852 struct bootdev_hunter *orig, *start;
855 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
856 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
859 * workaround for strange bug in clang-12 which sees all the below data
860 * as zeroes. Any access of start seems to fix it, such as
862 * printf("%p", start);
864 * Use memcpy() to force the correct behaviour.
866 memcpy(&start, &orig, sizeof(orig));
867 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
868 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
869 for (i = 0; i < n_ent; i++) {
870 struct bootdev_hunter *info = start + i;
872 printf("%4d %4s %-15s %s\n", info->prio,
873 std->hunters_used & BIT(i) ? "*" : "",
874 uclass_get_name(info->uclass),
875 info->drv ? info->drv->name : "(none)");
878 printf("(total hunters: %d)\n", n_ent);
881 static int bootdev_post_bind(struct udevice *dev)
883 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
885 INIT_LIST_HEAD(&ucp->bootflow_head);
890 static int bootdev_pre_unbind(struct udevice *dev)
892 bootdev_clear_bootflows(dev);
897 UCLASS_DRIVER(bootdev) = {
898 .id = UCLASS_BOOTDEV,
900 .flags = DM_UC_FLAG_SEQ_ALIAS,
901 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
902 .post_bind = bootdev_post_bind,
903 .pre_unbind = bootdev_pre_unbind,