blob: 91087981d21326350e0f1841c671436f40cdce23 [file] [log] [blame]
Simon Glass017656e2022-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 Glass03fcbf92022-04-24 23:31:09 -060013#include <bootmeth.h>
Simon Glass017656e2022-04-24 23:31:07 -060014#include <bootstd.h>
Simon Glass017656e2022-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 Glass017656e2022-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 Glassdee4d642022-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 Glass017656e2022-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 Glass03fcbf92022-04-24 23:31:09 -0600136 ret = bootmeth_check(bflow->method, iter);
137 if (ret)
138 return log_msg_ret("check", ret);
139
Simon Glass017656e2022-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;
157 if (ret)
158 return log_msg_ret("part", ret);
159
160 /*
161 * Currently we don't get the number of partitions, so just
162 * assume a large number
163 */
164 iter->max_part = MAX_PART_PER_BOOTDEV;
165
Simon Glass64cbc5c2023-01-17 10:47:42 -0700166 /* If this is the whole disk, check if we have bootable partitions */
167 if (!iter->part) {
168 iter->first_bootable = part_get_bootable(desc);
169 log_debug("checking bootable=%d\n", iter->first_bootable);
170
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);
174 } else {
Simon Glass017656e2022-04-24 23:31:07 -0600175 ret = fs_set_blk_dev_with_part(desc, bflow->part);
176 bflow->state = BOOTFLOWST_PART;
177
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());
183#endif
184 if (ret)
185 return log_msg_ret("fs", ret);
186 bflow->state = BOOTFLOWST_FS;
187 }
188
Simon Glass03fcbf92022-04-24 23:31:09 -0600189 ret = bootmeth_read_bootflow(bflow->method, bflow);
190 if (ret)
191 return log_msg_ret("method", ret);
192
Simon Glass017656e2022-04-24 23:31:07 -0600193 return 0;
194}
195
196void bootdev_list(bool probe)
197{
198 struct udevice *dev;
199 int ret;
200 int i;
201
202 printf("Seq Probed Status Uclass Name\n");
203 printf("--- ------ ------ -------- ------------------\n");
204 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200205 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass017656e2022-04-24 23:31:07 -0600206 else
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);
213 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200214 ret = uclass_next_device_check(&dev);
Simon Glass017656e2022-04-24 23:31:07 -0600215 else
216 ret = uclass_find_next_device(&dev);
217 }
218 printf("--- ------ ------ -------- ------------------\n");
219 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
220}
221
222int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
223{
224 struct udevice *bdev;
225 int ret;
226
227 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
228 &bdev);
229 if (ret) {
230 if (ret != -ENODEV) {
231 log_debug("Cannot access bootdev device\n");
232 return ret;
233 }
234
235 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
236 if (ret) {
237 log_debug("Cannot create bootdev device\n");
238 return ret;
239 }
240 }
241
242 return 0;
243}
244
Simon Glassb1dc36a2023-01-17 10:47:25 -0700245static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
246{
247 int len, slen;
248
249 len = strlen(dev->name);
250 slen = strlen(suffix);
251 if (len > slen && !strcmp(suffix, dev->name + len - slen))
252 return len - slen;
253
254 return len;
255}
256
Simon Glass017656e2022-04-24 23:31:07 -0600257int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name)
258{
259 struct udevice *parent, *dev;
260 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700261 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600262
Simon Glassb1dc36a2023-01-17 10:47:25 -0700263 len = bootdev_get_suffix_start(blk, ".blk");
264 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
265 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600266
267 parent = dev_get_parent(blk);
268 ret = device_find_child_by_name(parent, dev_name, &dev);
269 if (ret) {
270 char *str;
271
272 if (ret != -ENODEV) {
273 log_debug("Cannot access bootdev device\n");
274 return ret;
275 }
276 str = strdup(dev_name);
277 if (!str)
278 return -ENOMEM;
279
280 ret = device_bind_driver(parent, drv_name, str, &dev);
281 if (ret) {
282 log_debug("Cannot create bootdev device\n");
283 return ret;
284 }
285 device_set_name_alloced(dev);
286 }
287
288 return 0;
289}
290
291int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
292{
293 struct udevice *parent = dev_get_parent(dev);
294 struct udevice *blk;
295 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600296
297 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
298 return -EINVAL;
299
300 /* This should always work if bootdev_setup_sibling_blk() was used */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700301 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600302 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glassb1dc36a2023-01-17 10:47:25 -0700303 if (ret) {
304 char dev_name[50];
305
306 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
307 dev->name);
308 ret = device_find_child_by_name(parent, dev_name, &blk);
309 if (ret)
310 return log_msg_ret("find", ret);
311 }
Simon Glassb73a8292023-01-28 15:00:19 -0700312 ret = device_probe(blk);
313 if (ret)
314 return log_msg_ret("act", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600315 *blkp = blk;
316
317 return 0;
318}
319
320static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
321{
322 struct udevice *parent = dev_get_parent(blk);
323 struct udevice *bootdev;
324 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700325 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600326
327 if (device_get_uclass_id(blk) != UCLASS_BLK)
328 return -EINVAL;
329
330 /* This should always work if bootdev_setup_sibling_blk() was used */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700331 len = bootdev_get_suffix_start(blk, ".blk");
332 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
333 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600334 ret = device_find_child_by_name(parent, dev_name, &bootdev);
335 if (ret)
336 return log_msg_ret("find", ret);
337 *bootdevp = bootdev;
338
339 return 0;
340}
341
342int bootdev_unbind_dev(struct udevice *parent)
343{
344 struct udevice *dev;
345 int ret;
346
347 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
348 if (!ret) {
349 ret = device_remove(dev, DM_REMOVE_NORMAL);
350 if (ret)
351 return log_msg_ret("rem", ret);
352 ret = device_unbind(dev);
353 if (ret)
354 return log_msg_ret("unb", ret);
355 }
356
357 return 0;
358}
359
360/**
Simon Glass73708252023-01-17 10:48:00 -0700361 * label_to_uclass() - Convert a label to a uclass and sequence number
362 *
363 * @label: Label to look up (e.g. "mmc1" or "mmc0")
364 * @seqp: Returns the sequence number, or -1 if none
Simon Glasse22fe922023-01-17 10:48:05 -0700365 * @method_flagsp: If non-NULL, returns any flags implied by the label
366 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass4108c6b2023-04-24 13:49:47 +1200367 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
368 * known, other -ve error code on other error
Simon Glass73708252023-01-17 10:48:00 -0700369 */
Simon Glasse22fe922023-01-17 10:48:05 -0700370static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass73708252023-01-17 10:48:00 -0700371{
Simon Glasse22fe922023-01-17 10:48:05 -0700372 int seq, len, method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700373 enum uclass_id id;
374 const char *end;
Simon Glass73708252023-01-17 10:48:00 -0700375
Simon Glasse22fe922023-01-17 10:48:05 -0700376 method_flags = 0;
Simon Glass73708252023-01-17 10:48:00 -0700377 seq = trailing_strtoln_end(label, NULL, &end);
378 len = end - label;
379 if (!len)
380 return -EINVAL;
381 id = uclass_get_by_namelen(label, len);
382 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
383 uclass_get_name(id));
384 if (id == UCLASS_INVALID) {
Simon Glass215be682023-01-17 10:48:03 -0700385 /* try some special cases */
386 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
387 !strncmp("spi", label, len)) {
388 id = UCLASS_SPI_FLASH;
Simon Glasse22fe922023-01-17 10:48:05 -0700389 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
390 !strncmp("pxe", label, len)) {
391 id = UCLASS_ETH;
392 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
393 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
394 !strncmp("dhcp", label, len)) {
395 id = UCLASS_ETH;
396 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass215be682023-01-17 10:48:03 -0700397 } else {
Simon Glass4108c6b2023-04-24 13:49:47 +1200398 return -EPFNOSUPPORT;
Simon Glass215be682023-01-17 10:48:03 -0700399 }
Simon Glass73708252023-01-17 10:48:00 -0700400 }
401 if (id == UCLASS_USB)
402 id = UCLASS_MASS_STORAGE;
403 *seqp = seq;
Simon Glasse22fe922023-01-17 10:48:05 -0700404 if (method_flagsp)
405 *method_flagsp = method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700406
407 return id;
408}
409
Simon Glasse22fe922023-01-17 10:48:05 -0700410int bootdev_find_by_label(const char *label, struct udevice **devp,
411 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600412{
Simon Glasse22fe922023-01-17 10:48:05 -0700413 int seq, ret, method_flags = 0;
Simon Glass017656e2022-04-24 23:31:07 -0600414 struct udevice *media;
415 struct uclass *uc;
416 enum uclass_id id;
Simon Glass017656e2022-04-24 23:31:07 -0600417
Simon Glasse22fe922023-01-17 10:48:05 -0700418 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass73708252023-01-17 10:48:00 -0700419 if (ret < 0)
420 return log_msg_ret("uc", ret);
421 id = ret;
Simon Glass017656e2022-04-24 23:31:07 -0600422
423 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
424 uclass_id_foreach_dev(id, media, uc) {
425 struct udevice *bdev, *blk;
426 int ret;
427
428 /* if there is no seq, match anything */
429 if (seq != -1 && dev_seq(media) != seq) {
430 log_debug("- skip, media seq=%d\n", dev_seq(media));
431 continue;
432 }
433
434 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
435 &bdev);
436 if (ret) {
437 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
438 id);
439 ret = blk_find_device(id, seq, &blk);
440 if (!ret) {
441 log_debug("- get from blk %s\n", blk->name);
442 ret = bootdev_get_from_blk(blk, &bdev);
443 }
444 }
445 if (!ret) {
446 log_debug("- found %s\n", bdev->name);
447 *devp = bdev;
Simon Glassde567b12023-01-17 10:48:09 -0700448
449 /*
450 * if no sequence number was provided, we must scan all
451 * bootdevs for this media uclass
452 */
453 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
454 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glasse22fe922023-01-17 10:48:05 -0700455 if (method_flagsp)
456 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600457 return 0;
458 }
459 log_debug("- no device in %s\n", media->name);
460 }
Simon Glass017656e2022-04-24 23:31:07 -0600461
462 return -ENOENT;
463}
464
Simon Glasse22fe922023-01-17 10:48:05 -0700465int bootdev_find_by_any(const char *name, struct udevice **devp,
466 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600467{
468 struct udevice *dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700469 int method_flags = 0;
Simon Glassde567b12023-01-17 10:48:09 -0700470 int ret = -ENODEV, seq;
Simon Glass017656e2022-04-24 23:31:07 -0600471 char *endp;
472
473 seq = simple_strtol(name, &endp, 16);
474
475 /* Select by name, label or number */
476 if (*endp) {
477 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
478 if (ret == -ENODEV) {
Simon Glasse22fe922023-01-17 10:48:05 -0700479 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600480 if (ret) {
481 printf("Cannot find bootdev '%s' (err=%d)\n",
482 name, ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700483 return log_msg_ret("lab", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600484 }
485 ret = device_probe(dev);
486 }
487 if (ret) {
488 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
489 ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700490 return log_msg_ret("pro", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600491 }
Simon Glassde567b12023-01-17 10:48:09 -0700492 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass017656e2022-04-24 23:31:07 -0600493 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glassde567b12023-01-17 10:48:09 -0700494 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass017656e2022-04-24 23:31:07 -0600495 }
496 if (ret) {
497 printf("Cannot find '%s' (err=%d)\n", name, ret);
498 return ret;
499 }
500
501 *devp = dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700502 if (method_flagsp)
503 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600504
505 return 0;
506}
507
Simon Glassde567b12023-01-17 10:48:09 -0700508int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
509 int *method_flagsp)
510{
511 int ret;
512
513 ret = bootdev_hunt(label, false);
514 if (ret)
515 return log_msg_ret("scn", ret);
516 ret = bootdev_find_by_label(label, devp, method_flagsp);
517 if (ret)
518 return log_msg_ret("fnd", ret);
519
520 return 0;
521}
522
Simon Glassd6e39d12023-01-17 10:47:26 -0700523static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
524 struct bootflow *bflow)
525{
526 struct udevice *blk;
527 int ret;
528
529 ret = bootdev_get_sibling_blk(dev, &blk);
530 /*
531 * If there is no media, indicate that no more partitions should be
532 * checked
533 */
534 if (ret == -EOPNOTSUPP)
535 ret = -ESHUTDOWN;
536 if (ret)
537 return log_msg_ret("blk", ret);
538 assert(blk);
539 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
540 if (ret)
541 return log_msg_ret("find", ret);
542
543 return 0;
544}
545
Simon Glass017656e2022-04-24 23:31:07 -0600546int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
547 struct bootflow *bflow)
548{
549 const struct bootdev_ops *ops = bootdev_get_ops(dev);
550
Simon Glass33063462023-01-17 10:48:18 -0700551 log_debug("->get_bootflow %s=%p\n", dev->name, ops->get_bootflow);
Simon Glass00501fc2022-10-20 18:22:51 -0600552 bootflow_init(bflow, dev, iter->method);
Simon Glassd6e39d12023-01-17 10:47:26 -0700553 if (!ops->get_bootflow)
554 return default_get_bootflow(dev, iter, bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600555
556 return ops->get_bootflow(dev, iter, bflow);
557}
558
559void bootdev_clear_bootflows(struct udevice *dev)
560{
561 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
562
563 while (!list_empty(&ucp->bootflow_head)) {
564 struct bootflow *bflow;
565
566 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
567 bm_node);
Simon Glass03fcbf92022-04-24 23:31:09 -0600568 bootflow_remove(bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600569 }
570}
571
Simon Glassac06b26a2023-01-17 10:48:10 -0700572int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
573 int *method_flagsp)
574{
575 struct udevice *dev;
576
577 log_debug("next\n");
578 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass4108c6b2023-04-24 13:49:47 +1200579 const char *label = iter->labels[iter->cur_label];
580 int ret;
581
582 log_debug("Scanning: %s\n", label);
583 ret = bootdev_hunt_and_find_by_label(label, &dev,
584 method_flagsp);
585 if (iter->flags & BOOTFLOWIF_SHOW) {
586 if (ret == -EPFNOSUPPORT) {
587 log_warning("Unknown uclass '%s' in label\n",
588 label);
589 } else if (ret == -ENOENT) {
590 /*
591 * looking for, e.g. 'scsi0' should find
592 * something if SCSI is present
593 */
594 if (!trailing_strtol(label)) {
595 log_warning("No bootdevs for '%s'\n",
596 label);
597 }
598 }
599 }
600
Simon Glassac06b26a2023-01-17 10:48:10 -0700601 }
602
603 if (!dev)
604 return log_msg_ret("fin", -ENODEV);
605 *devp = dev;
606
607 return 0;
608}
609
Simon Glass660a9952023-01-17 10:48:11 -0700610int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
611{
612 struct udevice *dev = *devp;
613 bool found;
614 int ret;
615
616 /* find the next device with this priority */
617 *devp = NULL;
618 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
619 dev ? dev->name : "none");
620 do {
621 /*
622 * Don't probe devices here since they may not be of the
623 * required priority
624 */
625 if (!dev)
626 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
627 else
628 uclass_find_next_device(&dev);
629 found = false;
630
631 /* scan for the next device with the correct priority */
632 while (dev) {
633 struct bootdev_uc_plat *plat;
634
635 plat = dev_get_uclass_plat(dev);
636 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
637 iter->cur_prio);
638 if (plat->prio == iter->cur_prio)
639 break;
640 uclass_find_next_device(&dev);
641 }
642
643 /* none found for this priority, so move to the next */
644 if (!dev) {
645 log_debug("None found at prio %d, moving to %d\n",
646 iter->cur_prio, iter->cur_prio + 1);
647 if (++iter->cur_prio == BOOTDEVP_COUNT)
648 return log_msg_ret("fin", -ENODEV);
649
Simon Glass99e68182023-02-22 12:17:03 -0700650 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass660a9952023-01-17 10:48:11 -0700651 /* hunt to find new bootdevs */
652 ret = bootdev_hunt_prio(iter->cur_prio,
653 iter->flags &
Simon Glass99e68182023-02-22 12:17:03 -0700654 BOOTFLOWIF_SHOW);
Simon Glass660a9952023-01-17 10:48:11 -0700655 log_debug("- hunt ret %d\n", ret);
656 if (ret)
657 return log_msg_ret("hun", ret);
658 }
659 } else {
660 ret = device_probe(dev);
661 if (ret) {
662 log_debug("Device '%s' failed to probe\n",
663 dev->name);
664 dev = NULL;
665 }
666 }
667 } while (!dev);
668
669 *devp = dev;
670
671 return 0;
672}
673
Simon Glassba3d5372023-01-17 10:48:15 -0700674int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
675 struct udevice **devp, int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600676{
Simon Glassba3d5372023-01-17 10:48:15 -0700677 struct udevice *bootstd, *dev = NULL;
Simon Glass99e68182023-02-22 12:17:03 -0700678 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass484e4072023-01-17 10:48:14 -0700679 int method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600680 int ret;
681
682 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
683 if (ret) {
684 log_err("Missing bootstd device\n");
685 return log_msg_ret("std", ret);
686 }
687
Simon Glass7e1f6a42023-01-17 10:48:08 -0700688 /* hunt for any pre-scan devices */
Simon Glass99e68182023-02-22 12:17:03 -0700689 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700690 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
691 if (ret)
692 return log_msg_ret("pre", ret);
693 }
694
Simon Glass017656e2022-04-24 23:31:07 -0600695 /* Handle scanning a single device */
Simon Glassba3d5372023-01-17 10:48:15 -0700696 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass99e68182023-02-22 12:17:03 -0700697 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glassba3d5372023-01-17 10:48:15 -0700698 ret = bootdev_hunt(label, show);
699 if (ret)
700 return log_msg_ret("hun", ret);
701 }
702 ret = bootdev_find_by_any(label, &dev, &method_flags);
703 if (ret)
704 return log_msg_ret("lab", ret);
705
706 log_debug("method_flags: %x\n", method_flags);
707 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass99e68182023-02-22 12:17:03 -0700708 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glassba3d5372023-01-17 10:48:15 -0700709 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass99e68182023-02-22 12:17:03 -0700710 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glassba3d5372023-01-17 10:48:15 -0700711 else
Simon Glass99e68182023-02-22 12:17:03 -0700712 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glassba3d5372023-01-17 10:48:15 -0700713 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass484e4072023-01-17 10:48:14 -0700714 } else {
715 bool ok;
Simon Glass017656e2022-04-24 23:31:07 -0600716
Simon Glass484e4072023-01-17 10:48:14 -0700717 /* This either returns a non-empty list or NULL */
718 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
719 if (!ok)
720 return log_msg_ret("ord", -ENOMEM);
721 log_debug("setup labels %p\n", iter->labels);
722 if (iter->labels) {
723 iter->cur_label = -1;
724 ret = bootdev_next_label(iter, &dev, &method_flags);
725 } else {
726 ret = bootdev_next_prio(iter, &dev);
727 method_flags = 0;
728 }
729 if (!dev)
730 return log_msg_ret("fin", -ENOENT);
731 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass017656e2022-04-24 23:31:07 -0600732 }
733
Simon Glass017656e2022-04-24 23:31:07 -0600734 ret = device_probe(dev);
735 if (ret)
736 return log_msg_ret("probe", ret);
Simon Glass484e4072023-01-17 10:48:14 -0700737 if (method_flagsp)
738 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600739 *devp = dev;
740
741 return 0;
742}
743
Simon Glass1248d2b2023-01-17 10:47:34 -0700744static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
745{
746 const char *name = uclass_get_name(info->uclass);
747 struct bootstd_priv *std;
748 int ret;
749
750 ret = bootstd_get_priv(&std);
751 if (ret)
752 return log_msg_ret("std", ret);
753
754 if (!(std->hunters_used & BIT(seq))) {
755 if (show)
756 printf("Hunting with: %s\n",
757 uclass_get_name(info->uclass));
758 log_debug("Hunting with: %s\n", name);
759 if (info->hunt) {
760 ret = info->hunt(info, show);
761 if (ret)
762 return ret;
763 }
764 std->hunters_used |= BIT(seq);
765 }
766
767 return 0;
768}
769
770int bootdev_hunt(const char *spec, bool show)
771{
772 struct bootdev_hunter *start;
773 const char *end;
774 int n_ent, i;
775 int result;
776 size_t len;
777
778 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
779 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
780 result = 0;
781
782 len = SIZE_MAX;
783 if (spec) {
784 trailing_strtoln_end(spec, NULL, &end);
785 len = end - spec;
786 }
787
788 for (i = 0; i < n_ent; i++) {
789 struct bootdev_hunter *info = start + i;
790 const char *name = uclass_get_name(info->uclass);
791 int ret;
792
793 log_debug("looking at %.*s for %s\n",
794 (int)max(strlen(name), len), spec, name);
Simon Glassac06b26a2023-01-17 10:48:10 -0700795 if (spec && strncmp(spec, name, max(strlen(name), len))) {
796 if (info->uclass != UCLASS_ETH ||
797 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
798 continue;
799 }
Simon Glass1248d2b2023-01-17 10:47:34 -0700800 ret = bootdev_hunt_drv(info, i, show);
801 if (ret)
802 result = ret;
803 }
804
805 return result;
806}
807
Simon Glass3e9f6be2023-01-17 10:48:07 -0700808int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
809{
810 struct bootdev_hunter *start;
811 int n_ent, i;
812 int result;
813
814 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
815 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
816 result = 0;
817
818 log_debug("Hunting for priority %d\n", prio);
819 for (i = 0; i < n_ent; i++) {
820 struct bootdev_hunter *info = start + i;
821 int ret;
822
823 if (prio != info->prio)
824 continue;
825 ret = bootdev_hunt_drv(info, i, show);
826 if (ret && ret != -ENOENT)
827 result = ret;
828 }
829
830 return result;
831}
832
Simon Glass5acb97a2023-01-17 10:47:33 -0700833void bootdev_list_hunters(struct bootstd_priv *std)
834{
835 struct bootdev_hunter *orig, *start;
836 int n_ent, i;
837
838 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
839 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
840
841 /*
842 * workaround for strange bug in clang-12 which sees all the below data
843 * as zeroes. Any access of start seems to fix it, such as
844 *
845 * printf("%p", start);
846 *
847 * Use memcpy() to force the correct behaviour.
848 */
849 memcpy(&start, &orig, sizeof(orig));
850 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
851 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
852 for (i = 0; i < n_ent; i++) {
853 struct bootdev_hunter *info = start + i;
854
855 printf("%4d %4s %-15s %s\n", info->prio,
856 std->hunters_used & BIT(i) ? "*" : "",
857 uclass_get_name(info->uclass),
858 info->drv ? info->drv->name : "(none)");
859 }
860
861 printf("(total hunters: %d)\n", n_ent);
862}
863
Simon Glass017656e2022-04-24 23:31:07 -0600864static int bootdev_post_bind(struct udevice *dev)
865{
866 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
867
868 INIT_LIST_HEAD(&ucp->bootflow_head);
869
870 return 0;
871}
872
873static int bootdev_pre_unbind(struct udevice *dev)
874{
875 bootdev_clear_bootflows(dev);
876
877 return 0;
878}
879
880UCLASS_DRIVER(bootdev) = {
881 .id = UCLASS_BOOTDEV,
882 .name = "bootdev",
883 .flags = DM_UC_FLAG_SEQ_ALIAS,
884 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
885 .post_bind = bootdev_post_bind,
886 .pre_unbind = bootdev_pre_unbind,
887};