blob: 9660ff75676d6607f57fbf645d6ffea2f981dd4d [file] [log] [blame]
Simon Glass201417d2022-04-24 23:31:07 -06001// 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 Glassa8f5be12022-04-24 23:31:09 -060013#include <bootmeth.h>
Simon Glass201417d2022-04-24 23:31:07 -060014#include <bootstd.h>
Simon Glass201417d2022-04-24 23:31:07 -060015#include <fs.h>
16#include <log.h>
17#include <malloc.h>
18#include <part.h>
19#include <sort.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_add_bootflow(struct bootflow *bflow)
37{
Simon Glass201417d2022-04-24 23:31:07 -060038 struct bootstd_priv *std;
39 struct bootflow *new;
40 int ret;
41
42 assert(bflow->dev);
43 ret = bootstd_get_priv(&std);
44 if (ret)
45 return ret;
46
47 new = malloc(sizeof(*bflow));
48 if (!new)
49 return log_msg_ret("bflow", -ENOMEM);
50 memcpy(new, bflow, sizeof(*bflow));
51
52 list_add_tail(&new->glob_node, &std->glob_head);
Simon Glasseccb25c2022-07-30 15:52:23 -060053 if (bflow->dev) {
54 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
55
56 list_add_tail(&new->bm_node, &ucp->bootflow_head);
57 }
Simon Glass201417d2022-04-24 23:31:07 -060058
59 return 0;
60}
61
62int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
63{
64 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
65
66 if (list_empty(&ucp->bootflow_head))
67 return -ENOENT;
68
69 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
70 bm_node);
71
72 return 0;
73}
74
75int bootdev_next_bootflow(struct bootflow **bflowp)
76{
77 struct bootflow *bflow = *bflowp;
78 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
79
80 *bflowp = NULL;
81
82 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
83 return -ENOENT;
84
85 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
86
87 return 0;
88}
89
90int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
91 struct udevice **devp)
92{
93 struct udevice *dev;
94 char dev_name[30];
95 char *str;
96 int ret;
97
98 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
99 str = strdup(dev_name);
100 if (!str)
101 return -ENOMEM;
102 ret = device_bind_driver(parent, drv_name, str, &dev);
103 if (ret)
104 return ret;
105 device_set_name_alloced(dev);
106 *devp = dev;
107
108 return 0;
109}
110
111int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
112 struct bootflow_iter *iter, struct bootflow *bflow)
113{
114 struct blk_desc *desc = dev_get_uclass_plat(blk);
115 struct disk_partition info;
116 char partstr[20];
117 char name[60];
118 int ret;
119
120 /* Sanity check */
121 if (iter->part >= MAX_PART_PER_BOOTDEV)
122 return log_msg_ret("max", -ESHUTDOWN);
123
124 bflow->blk = blk;
125 if (iter->part)
126 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
127 else
128 strcpy(partstr, "whole");
129 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
130 bflow->name = strdup(name);
131 if (!bflow->name)
132 return log_msg_ret("name", -ENOMEM);
133
134 bflow->part = iter->part;
135
Simon Glassa8f5be12022-04-24 23:31:09 -0600136 ret = bootmeth_check(bflow->method, iter);
137 if (ret)
138 return log_msg_ret("check", ret);
139
Simon Glass201417d2022-04-24 23:31:07 -0600140 /*
141 * partition numbers start at 0 so this cannot succeed, but it can tell
142 * us whether there is valid media there
143 */
144 ret = part_get_info(desc, iter->part, &info);
145 if (!iter->part && ret == -ENOENT)
146 ret = 0;
147
148 /*
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.
152 */
153 if (ret == -EOPNOTSUPP)
154 ret = -ESHUTDOWN;
155 else
156 bflow->state = BOOTFLOWST_MEDIA;
Simon Glass76afc842023-04-28 13:18:09 -0600157 if (ret) {
158 /* allow partition 1 to be missing */
159 if (iter->part == 1) {
160 iter->max_part = 3;
161 ret = -ENOENT;
162 }
163
Simon Glass201417d2022-04-24 23:31:07 -0600164 return log_msg_ret("part", ret);
Simon Glass76afc842023-04-28 13:18:09 -0600165 }
Simon Glass201417d2022-04-24 23:31:07 -0600166
167 /*
168 * Currently we don't get the number of partitions, so just
169 * assume a large number
170 */
171 iter->max_part = MAX_PART_PER_BOOTDEV;
172
Simon Glassf0e358f2023-01-17 10:47:42 -0700173 /* If this is the whole disk, check if we have bootable partitions */
174 if (!iter->part) {
175 iter->first_bootable = part_get_bootable(desc);
176 log_debug("checking bootable=%d\n", iter->first_bootable);
177
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);
181 } else {
Simon Glass201417d2022-04-24 23:31:07 -0600182 ret = fs_set_blk_dev_with_part(desc, bflow->part);
183 bflow->state = BOOTFLOWST_PART;
Simon Glass1aabe4e2023-04-24 13:49:49 +1200184 if (ret)
185 return log_msg_ret("fs", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600186
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());
192#endif
Simon Glass1aabe4e2023-04-24 13:49:49 +1200193 bflow->blk = blk;
Simon Glass201417d2022-04-24 23:31:07 -0600194 bflow->state = BOOTFLOWST_FS;
195 }
196
Simon Glassa8f5be12022-04-24 23:31:09 -0600197 ret = bootmeth_read_bootflow(bflow->method, bflow);
198 if (ret)
199 return log_msg_ret("method", ret);
200
Simon Glass201417d2022-04-24 23:31:07 -0600201 return 0;
202}
203
204void bootdev_list(bool probe)
205{
206 struct udevice *dev;
207 int ret;
208 int i;
209
210 printf("Seq Probed Status Uclass Name\n");
211 printf("--- ------ ------ -------- ------------------\n");
212 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200213 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass201417d2022-04-24 23:31:07 -0600214 else
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);
221 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200222 ret = uclass_next_device_check(&dev);
Simon Glass201417d2022-04-24 23:31:07 -0600223 else
224 ret = uclass_find_next_device(&dev);
225 }
226 printf("--- ------ ------ -------- ------------------\n");
227 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
228}
229
230int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
231{
232 struct udevice *bdev;
233 int ret;
234
235 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
236 &bdev);
237 if (ret) {
238 if (ret != -ENODEV) {
239 log_debug("Cannot access bootdev device\n");
240 return ret;
241 }
242
243 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
244 if (ret) {
245 log_debug("Cannot create bootdev device\n");
246 return ret;
247 }
248 }
249
250 return 0;
251}
252
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700253static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
254{
255 int len, slen;
256
257 len = strlen(dev->name);
258 slen = strlen(suffix);
259 if (len > slen && !strcmp(suffix, dev->name + len - slen))
260 return len - slen;
261
262 return len;
263}
264
Simon Glass201417d2022-04-24 23:31:07 -0600265int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name)
266{
267 struct udevice *parent, *dev;
268 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700269 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600270
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700271 len = bootdev_get_suffix_start(blk, ".blk");
272 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
273 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600274
275 parent = dev_get_parent(blk);
276 ret = device_find_child_by_name(parent, dev_name, &dev);
277 if (ret) {
278 char *str;
279
280 if (ret != -ENODEV) {
281 log_debug("Cannot access bootdev device\n");
282 return ret;
283 }
284 str = strdup(dev_name);
285 if (!str)
286 return -ENOMEM;
287
288 ret = device_bind_driver(parent, drv_name, str, &dev);
289 if (ret) {
290 log_debug("Cannot create bootdev device\n");
291 return ret;
292 }
293 device_set_name_alloced(dev);
294 }
295
296 return 0;
297}
298
299int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
300{
301 struct udevice *parent = dev_get_parent(dev);
302 struct udevice *blk;
303 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600304
305 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
306 return -EINVAL;
307
308 /* This should always work if bootdev_setup_sibling_blk() was used */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700309 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600310 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700311 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 }
Simon Glass965020c2023-01-28 15:00:19 -0700320 ret = device_probe(blk);
321 if (ret)
322 return log_msg_ret("act", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600323 *blkp = blk;
324
325 return 0;
326}
327
328static int 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];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700333 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600334
335 if (device_get_uclass_id(blk) != UCLASS_BLK)
336 return -EINVAL;
337
338 /* This should always work if bootdev_setup_sibling_blk() was used */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700339 len = bootdev_get_suffix_start(blk, ".blk");
340 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
341 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600342 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/**
Simon Glass74ebfb62023-01-17 10:48:00 -0700369 * 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
Simon Glassd9f48572023-01-17 10:48:05 -0700373 * @method_flagsp: If non-NULL, returns any flags implied by the label
374 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass1736b4a2023-04-24 13:49:47 +1200375 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
376 * known, other -ve error code on other error
Simon Glass74ebfb62023-01-17 10:48:00 -0700377 */
Simon Glassd9f48572023-01-17 10:48:05 -0700378static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass74ebfb62023-01-17 10:48:00 -0700379{
Simon Glassd9f48572023-01-17 10:48:05 -0700380 int seq, len, method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700381 enum uclass_id id;
382 const char *end;
Simon Glass74ebfb62023-01-17 10:48:00 -0700383
Simon Glassd9f48572023-01-17 10:48:05 -0700384 method_flags = 0;
Simon Glass74ebfb62023-01-17 10:48:00 -0700385 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) {
Simon Glass0c1f4a92023-01-17 10:48:03 -0700393 /* try some special cases */
394 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
395 !strncmp("spi", label, len)) {
396 id = UCLASS_SPI_FLASH;
Simon Glassd9f48572023-01-17 10:48:05 -0700397 } 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;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700405 } else {
Simon Glass1736b4a2023-04-24 13:49:47 +1200406 return -EPFNOSUPPORT;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700407 }
Simon Glass74ebfb62023-01-17 10:48:00 -0700408 }
409 if (id == UCLASS_USB)
410 id = UCLASS_MASS_STORAGE;
411 *seqp = seq;
Simon Glassd9f48572023-01-17 10:48:05 -0700412 if (method_flagsp)
413 *method_flagsp = method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700414
415 return id;
416}
417
Simon Glassd9f48572023-01-17 10:48:05 -0700418int bootdev_find_by_label(const char *label, struct udevice **devp,
419 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600420{
Simon Glassd9f48572023-01-17 10:48:05 -0700421 int seq, ret, method_flags = 0;
Simon Glass201417d2022-04-24 23:31:07 -0600422 struct udevice *media;
423 struct uclass *uc;
424 enum uclass_id id;
Simon Glass201417d2022-04-24 23:31:07 -0600425
Simon Glassd9f48572023-01-17 10:48:05 -0700426 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass74ebfb62023-01-17 10:48:00 -0700427 if (ret < 0)
428 return log_msg_ret("uc", ret);
429 id = ret;
Simon Glass201417d2022-04-24 23:31:07 -0600430
431 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
432 uclass_id_foreach_dev(id, media, uc) {
433 struct udevice *bdev, *blk;
434 int ret;
435
436 /* if there is no seq, match anything */
437 if (seq != -1 && dev_seq(media) != seq) {
438 log_debug("- skip, media seq=%d\n", dev_seq(media));
439 continue;
440 }
441
442 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
443 &bdev);
444 if (ret) {
445 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
446 id);
447 ret = blk_find_device(id, seq, &blk);
448 if (!ret) {
449 log_debug("- get from blk %s\n", blk->name);
450 ret = bootdev_get_from_blk(blk, &bdev);
451 }
452 }
453 if (!ret) {
454 log_debug("- found %s\n", bdev->name);
455 *devp = bdev;
Simon Glass66e3dce2023-01-17 10:48:09 -0700456
457 /*
458 * if no sequence number was provided, we must scan all
459 * bootdevs for this media uclass
460 */
461 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
462 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glassd9f48572023-01-17 10:48:05 -0700463 if (method_flagsp)
464 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600465 return 0;
466 }
467 log_debug("- no device in %s\n", media->name);
468 }
Simon Glass201417d2022-04-24 23:31:07 -0600469
470 return -ENOENT;
471}
472
Simon Glassd9f48572023-01-17 10:48:05 -0700473int bootdev_find_by_any(const char *name, struct udevice **devp,
474 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600475{
476 struct udevice *dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700477 int method_flags = 0;
Simon Glass66e3dce2023-01-17 10:48:09 -0700478 int ret = -ENODEV, seq;
Simon Glass201417d2022-04-24 23:31:07 -0600479 char *endp;
480
481 seq = simple_strtol(name, &endp, 16);
482
483 /* Select by name, label or number */
484 if (*endp) {
485 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
486 if (ret == -ENODEV) {
Simon Glassd9f48572023-01-17 10:48:05 -0700487 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass201417d2022-04-24 23:31:07 -0600488 if (ret) {
489 printf("Cannot find bootdev '%s' (err=%d)\n",
490 name, ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700491 return log_msg_ret("lab", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600492 }
493 ret = device_probe(dev);
494 }
495 if (ret) {
496 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
497 ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700498 return log_msg_ret("pro", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600499 }
Simon Glass66e3dce2023-01-17 10:48:09 -0700500 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass201417d2022-04-24 23:31:07 -0600501 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glass66e3dce2023-01-17 10:48:09 -0700502 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass201417d2022-04-24 23:31:07 -0600503 }
504 if (ret) {
505 printf("Cannot find '%s' (err=%d)\n", name, ret);
506 return ret;
507 }
508
509 *devp = dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700510 if (method_flagsp)
511 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600512
513 return 0;
514}
515
Simon Glass66e3dce2023-01-17 10:48:09 -0700516int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
517 int *method_flagsp)
518{
519 int ret;
520
521 ret = bootdev_hunt(label, false);
522 if (ret)
523 return log_msg_ret("scn", ret);
524 ret = bootdev_find_by_label(label, devp, method_flagsp);
525 if (ret)
526 return log_msg_ret("fnd", ret);
527
528 return 0;
529}
530
Simon Glassb85fc8d2023-01-17 10:47:26 -0700531static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
532 struct bootflow *bflow)
533{
534 struct udevice *blk;
535 int ret;
536
537 ret = bootdev_get_sibling_blk(dev, &blk);
538 /*
539 * If there is no media, indicate that no more partitions should be
540 * checked
541 */
542 if (ret == -EOPNOTSUPP)
543 ret = -ESHUTDOWN;
544 if (ret)
545 return log_msg_ret("blk", ret);
546 assert(blk);
547 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
548 if (ret)
549 return log_msg_ret("find", ret);
550
551 return 0;
552}
553
Simon Glass201417d2022-04-24 23:31:07 -0600554int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
555 struct bootflow *bflow)
556{
557 const struct bootdev_ops *ops = bootdev_get_ops(dev);
558
Simon Glassf738c732023-01-17 10:48:18 -0700559 log_debug("->get_bootflow %s=%p\n", dev->name, ops->get_bootflow);
Simon Glassb190deb2022-10-20 18:22:51 -0600560 bootflow_init(bflow, dev, iter->method);
Simon Glassb85fc8d2023-01-17 10:47:26 -0700561 if (!ops->get_bootflow)
562 return default_get_bootflow(dev, iter, bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600563
564 return ops->get_bootflow(dev, iter, bflow);
565}
566
567void bootdev_clear_bootflows(struct udevice *dev)
568{
569 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
570
571 while (!list_empty(&ucp->bootflow_head)) {
572 struct bootflow *bflow;
573
574 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
575 bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600576 bootflow_remove(bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600577 }
578}
579
Simon Glasse4b69482023-01-17 10:48:10 -0700580int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
581 int *method_flagsp)
582{
583 struct udevice *dev;
584
585 log_debug("next\n");
586 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass1736b4a2023-04-24 13:49:47 +1200587 const char *label = iter->labels[iter->cur_label];
588 int ret;
589
590 log_debug("Scanning: %s\n", label);
591 ret = bootdev_hunt_and_find_by_label(label, &dev,
592 method_flagsp);
593 if (iter->flags & BOOTFLOWIF_SHOW) {
594 if (ret == -EPFNOSUPPORT) {
595 log_warning("Unknown uclass '%s' in label\n",
596 label);
597 } else if (ret == -ENOENT) {
598 /*
599 * looking for, e.g. 'scsi0' should find
600 * something if SCSI is present
601 */
602 if (!trailing_strtol(label)) {
603 log_warning("No bootdevs for '%s'\n",
604 label);
605 }
606 }
607 }
608
Simon Glasse4b69482023-01-17 10:48:10 -0700609 }
610
611 if (!dev)
612 return log_msg_ret("fin", -ENODEV);
613 *devp = dev;
614
615 return 0;
616}
617
Simon Glass43e89a32023-01-17 10:48:11 -0700618int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
619{
620 struct udevice *dev = *devp;
621 bool found;
622 int ret;
623
624 /* find the next device with this priority */
625 *devp = NULL;
626 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
627 dev ? dev->name : "none");
628 do {
629 /*
630 * Don't probe devices here since they may not be of the
631 * required priority
632 */
633 if (!dev)
634 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
635 else
636 uclass_find_next_device(&dev);
637 found = false;
638
639 /* scan for the next device with the correct priority */
640 while (dev) {
641 struct bootdev_uc_plat *plat;
642
643 plat = dev_get_uclass_plat(dev);
644 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
645 iter->cur_prio);
646 if (plat->prio == iter->cur_prio)
647 break;
648 uclass_find_next_device(&dev);
649 }
650
651 /* none found for this priority, so move to the next */
652 if (!dev) {
653 log_debug("None found at prio %d, moving to %d\n",
654 iter->cur_prio, iter->cur_prio + 1);
655 if (++iter->cur_prio == BOOTDEVP_COUNT)
656 return log_msg_ret("fin", -ENODEV);
657
Simon Glass4f806f32023-02-22 12:17:03 -0700658 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass43e89a32023-01-17 10:48:11 -0700659 /* hunt to find new bootdevs */
660 ret = bootdev_hunt_prio(iter->cur_prio,
661 iter->flags &
Simon Glass4f806f32023-02-22 12:17:03 -0700662 BOOTFLOWIF_SHOW);
Simon Glass43e89a32023-01-17 10:48:11 -0700663 log_debug("- hunt ret %d\n", ret);
664 if (ret)
665 return log_msg_ret("hun", ret);
666 }
667 } else {
668 ret = device_probe(dev);
669 if (ret) {
670 log_debug("Device '%s' failed to probe\n",
671 dev->name);
672 dev = NULL;
673 }
674 }
675 } while (!dev);
676
677 *devp = dev;
678
679 return 0;
680}
681
Simon Glass91943ff2023-01-17 10:48:15 -0700682int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
683 struct udevice **devp, int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600684{
Simon Glass91943ff2023-01-17 10:48:15 -0700685 struct udevice *bootstd, *dev = NULL;
Simon Glass4f806f32023-02-22 12:17:03 -0700686 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass47aedc22023-01-17 10:48:14 -0700687 int method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600688 int ret;
689
690 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
691 if (ret) {
692 log_err("Missing bootstd device\n");
693 return log_msg_ret("std", ret);
694 }
695
Simon Glasseacc2612023-01-17 10:48:08 -0700696 /* hunt for any pre-scan devices */
Simon Glass4f806f32023-02-22 12:17:03 -0700697 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glasseacc2612023-01-17 10:48:08 -0700698 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
699 if (ret)
700 return log_msg_ret("pre", ret);
701 }
702
Simon Glass201417d2022-04-24 23:31:07 -0600703 /* Handle scanning a single device */
Simon Glass91943ff2023-01-17 10:48:15 -0700704 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass4f806f32023-02-22 12:17:03 -0700705 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass91943ff2023-01-17 10:48:15 -0700706 ret = bootdev_hunt(label, show);
707 if (ret)
708 return log_msg_ret("hun", ret);
709 }
710 ret = bootdev_find_by_any(label, &dev, &method_flags);
711 if (ret)
712 return log_msg_ret("lab", ret);
713
714 log_debug("method_flags: %x\n", method_flags);
715 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass4f806f32023-02-22 12:17:03 -0700716 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glass91943ff2023-01-17 10:48:15 -0700717 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass4f806f32023-02-22 12:17:03 -0700718 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glass91943ff2023-01-17 10:48:15 -0700719 else
Simon Glass4f806f32023-02-22 12:17:03 -0700720 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glass91943ff2023-01-17 10:48:15 -0700721 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass47aedc22023-01-17 10:48:14 -0700722 } else {
723 bool ok;
724
725 /* This either returns a non-empty list or NULL */
726 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
727 if (!ok)
728 return log_msg_ret("ord", -ENOMEM);
729 log_debug("setup labels %p\n", iter->labels);
730 if (iter->labels) {
731 iter->cur_label = -1;
732 ret = bootdev_next_label(iter, &dev, &method_flags);
733 } else {
734 ret = bootdev_next_prio(iter, &dev);
735 method_flags = 0;
736 }
737 if (!dev)
738 return log_msg_ret("fin", -ENOENT);
739 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass201417d2022-04-24 23:31:07 -0600740 }
741
Simon Glass201417d2022-04-24 23:31:07 -0600742 ret = device_probe(dev);
743 if (ret)
744 return log_msg_ret("probe", ret);
Simon Glass47aedc22023-01-17 10:48:14 -0700745 if (method_flagsp)
746 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600747 *devp = dev;
748
749 return 0;
750}
751
Simon Glassc7b63d52023-01-17 10:47:34 -0700752static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
753{
754 const char *name = uclass_get_name(info->uclass);
755 struct bootstd_priv *std;
756 int ret;
757
758 ret = bootstd_get_priv(&std);
759 if (ret)
760 return log_msg_ret("std", ret);
761
762 if (!(std->hunters_used & BIT(seq))) {
763 if (show)
764 printf("Hunting with: %s\n",
765 uclass_get_name(info->uclass));
766 log_debug("Hunting with: %s\n", name);
767 if (info->hunt) {
768 ret = info->hunt(info, show);
769 if (ret)
770 return ret;
771 }
772 std->hunters_used |= BIT(seq);
773 }
774
775 return 0;
776}
777
778int bootdev_hunt(const char *spec, bool show)
779{
780 struct bootdev_hunter *start;
781 const char *end;
782 int n_ent, i;
783 int result;
784 size_t len;
785
786 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
787 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
788 result = 0;
789
790 len = SIZE_MAX;
791 if (spec) {
792 trailing_strtoln_end(spec, NULL, &end);
793 len = end - spec;
794 }
795
796 for (i = 0; i < n_ent; i++) {
797 struct bootdev_hunter *info = start + i;
798 const char *name = uclass_get_name(info->uclass);
799 int ret;
800
801 log_debug("looking at %.*s for %s\n",
802 (int)max(strlen(name), len), spec, name);
Simon Glasse4b69482023-01-17 10:48:10 -0700803 if (spec && strncmp(spec, name, max(strlen(name), len))) {
804 if (info->uclass != UCLASS_ETH ||
805 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
806 continue;
807 }
Simon Glassc7b63d52023-01-17 10:47:34 -0700808 ret = bootdev_hunt_drv(info, i, show);
809 if (ret)
810 result = ret;
811 }
812
813 return result;
814}
815
Simon Glass79a7d4a2023-01-17 10:48:07 -0700816int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
817{
818 struct bootdev_hunter *start;
819 int n_ent, i;
820 int result;
821
822 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
823 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
824 result = 0;
825
826 log_debug("Hunting for priority %d\n", prio);
827 for (i = 0; i < n_ent; i++) {
828 struct bootdev_hunter *info = start + i;
829 int ret;
830
831 if (prio != info->prio)
832 continue;
833 ret = bootdev_hunt_drv(info, i, show);
834 if (ret && ret != -ENOENT)
835 result = ret;
836 }
837
838 return result;
839}
840
Simon Glassbd90b092023-01-17 10:47:33 -0700841void bootdev_list_hunters(struct bootstd_priv *std)
842{
843 struct bootdev_hunter *orig, *start;
844 int n_ent, i;
845
846 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
847 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
848
849 /*
850 * workaround for strange bug in clang-12 which sees all the below data
851 * as zeroes. Any access of start seems to fix it, such as
852 *
853 * printf("%p", start);
854 *
855 * Use memcpy() to force the correct behaviour.
856 */
857 memcpy(&start, &orig, sizeof(orig));
858 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
859 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
860 for (i = 0; i < n_ent; i++) {
861 struct bootdev_hunter *info = start + i;
862
863 printf("%4d %4s %-15s %s\n", info->prio,
864 std->hunters_used & BIT(i) ? "*" : "",
865 uclass_get_name(info->uclass),
866 info->drv ? info->drv->name : "(none)");
867 }
868
869 printf("(total hunters: %d)\n", n_ent);
870}
871
Simon Glass201417d2022-04-24 23:31:07 -0600872static int bootdev_post_bind(struct udevice *dev)
873{
874 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
875
876 INIT_LIST_HEAD(&ucp->bootflow_head);
877
878 return 0;
879}
880
881static int bootdev_pre_unbind(struct udevice *dev)
882{
883 bootdev_clear_bootflows(dev);
884
885 return 0;
886}
887
888UCLASS_DRIVER(bootdev) = {
889 .id = UCLASS_BOOTDEV,
890 .name = "bootdev",
891 .flags = DM_UC_FLAG_SEQ_ALIAS,
892 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
893 .post_bind = bootdev_post_bind,
894 .pre_unbind = bootdev_pre_unbind,
895};