blob: c4044d87dc3eef713c8d74ef47ad016f854668f5 [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;
Simon Glassa8e7d512023-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 Glass017656e2022-04-24 23:31:07 -0600164 return log_msg_ret("part", ret);
Simon Glassa8e7d512023-04-28 13:18:09 -0600165 }
Simon Glass017656e2022-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 Glass64cbc5c2023-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 Glass017656e2022-04-24 23:31:07 -0600182 ret = fs_set_blk_dev_with_part(desc, bflow->part);
183 bflow->state = BOOTFLOWST_PART;
Simon Glassa40f7822023-04-24 13:49:49 +1200184 if (ret)
185 return log_msg_ret("fs", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600186
Simon Glass017656e2022-04-24 23:31:07 -0600187 log_debug("%s: Found partition %x type %x fstype %d\n",
Simon Glassed0ec662023-08-24 13:55:33 -0600188 blk->name, bflow->part,
189 IS_ENABLED(CONFIG_DOS_PARTITION) ?
190 disk_partition_sys_ind(&info) : 0,
Simon Glass017656e2022-04-24 23:31:07 -0600191 ret ? -1 : fs_get_type());
Simon Glassa40f7822023-04-24 13:49:49 +1200192 bflow->blk = blk;
Simon Glass017656e2022-04-24 23:31:07 -0600193 bflow->state = BOOTFLOWST_FS;
194 }
195
Simon Glass03fcbf92022-04-24 23:31:09 -0600196 ret = bootmeth_read_bootflow(bflow->method, bflow);
197 if (ret)
198 return log_msg_ret("method", ret);
199
Simon Glass017656e2022-04-24 23:31:07 -0600200 return 0;
201}
202
203void bootdev_list(bool probe)
204{
205 struct udevice *dev;
206 int ret;
207 int i;
208
209 printf("Seq Probed Status Uclass Name\n");
210 printf("--- ------ ------ -------- ------------------\n");
211 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200212 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass017656e2022-04-24 23:31:07 -0600213 else
214 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
215 for (i = 0; dev; i++) {
216 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
217 device_active(dev) ? '+' : ' ',
Heinrich Schuchardt91592d62023-07-30 16:29:25 +0200218 ret ? simple_itoa(-ret) : "OK",
Simon Glass017656e2022-04-24 23:31:07 -0600219 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
220 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200221 ret = uclass_next_device_check(&dev);
Simon Glass017656e2022-04-24 23:31:07 -0600222 else
223 ret = uclass_find_next_device(&dev);
224 }
225 printf("--- ------ ------ -------- ------------------\n");
226 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
227}
228
229int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
230{
231 struct udevice *bdev;
232 int ret;
233
234 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
235 &bdev);
236 if (ret) {
237 if (ret != -ENODEV) {
238 log_debug("Cannot access bootdev device\n");
239 return ret;
240 }
241
242 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
243 if (ret) {
244 log_debug("Cannot create bootdev device\n");
245 return ret;
246 }
247 }
248
249 return 0;
250}
251
Simon Glassb1dc36a2023-01-17 10:47:25 -0700252static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
253{
254 int len, slen;
255
256 len = strlen(dev->name);
257 slen = strlen(suffix);
258 if (len > slen && !strcmp(suffix, dev->name + len - slen))
259 return len - slen;
260
261 return len;
262}
263
Simon Glassb1d581d2023-07-30 11:15:14 -0600264int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
Simon Glass017656e2022-04-24 23:31:07 -0600265{
266 struct udevice *parent, *dev;
267 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700268 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600269
Simon Glassb1dc36a2023-01-17 10:47:25 -0700270 len = bootdev_get_suffix_start(blk, ".blk");
271 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
272 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600273
274 parent = dev_get_parent(blk);
275 ret = device_find_child_by_name(parent, dev_name, &dev);
276 if (ret) {
277 char *str;
278
279 if (ret != -ENODEV) {
280 log_debug("Cannot access bootdev device\n");
281 return ret;
282 }
283 str = strdup(dev_name);
284 if (!str)
285 return -ENOMEM;
286
287 ret = device_bind_driver(parent, drv_name, str, &dev);
288 if (ret) {
289 log_debug("Cannot create bootdev device\n");
290 return ret;
291 }
292 device_set_name_alloced(dev);
293 }
294
295 return 0;
296}
297
298int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
299{
300 struct udevice *parent = dev_get_parent(dev);
301 struct udevice *blk;
302 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600303
304 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
305 return -EINVAL;
306
Simon Glassb1d581d2023-07-30 11:15:14 -0600307 /*
308 * This should always work if bootdev_setup_for_sibling_blk() was used
309 */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700310 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600311 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glassb1dc36a2023-01-17 10:47:25 -0700312 if (ret) {
313 char dev_name[50];
314
315 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
316 dev->name);
317 ret = device_find_child_by_name(parent, dev_name, &blk);
318 if (ret)
319 return log_msg_ret("find", ret);
320 }
Simon Glassb73a8292023-01-28 15:00:19 -0700321 ret = device_probe(blk);
322 if (ret)
323 return log_msg_ret("act", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600324 *blkp = blk;
325
326 return 0;
327}
328
329static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
330{
331 struct udevice *parent = dev_get_parent(blk);
332 struct udevice *bootdev;
333 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700334 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600335
336 if (device_get_uclass_id(blk) != UCLASS_BLK)
337 return -EINVAL;
338
Simon Glassb1d581d2023-07-30 11:15:14 -0600339 /* This should always work if bootdev_setup_for_sibling_blk() was used */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700340 len = bootdev_get_suffix_start(blk, ".blk");
341 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
342 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600343 ret = device_find_child_by_name(parent, dev_name, &bootdev);
344 if (ret)
345 return log_msg_ret("find", ret);
346 *bootdevp = bootdev;
347
348 return 0;
349}
350
351int bootdev_unbind_dev(struct udevice *parent)
352{
353 struct udevice *dev;
354 int ret;
355
356 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
357 if (!ret) {
358 ret = device_remove(dev, DM_REMOVE_NORMAL);
359 if (ret)
360 return log_msg_ret("rem", ret);
361 ret = device_unbind(dev);
362 if (ret)
363 return log_msg_ret("unb", ret);
364 }
365
366 return 0;
367}
368
369/**
Simon Glass73708252023-01-17 10:48:00 -0700370 * label_to_uclass() - Convert a label to a uclass and sequence number
371 *
372 * @label: Label to look up (e.g. "mmc1" or "mmc0")
373 * @seqp: Returns the sequence number, or -1 if none
Simon Glasse22fe922023-01-17 10:48:05 -0700374 * @method_flagsp: If non-NULL, returns any flags implied by the label
375 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass4108c6b2023-04-24 13:49:47 +1200376 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
377 * known, other -ve error code on other error
Simon Glass73708252023-01-17 10:48:00 -0700378 */
Simon Glasse22fe922023-01-17 10:48:05 -0700379static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass73708252023-01-17 10:48:00 -0700380{
Simon Glasse22fe922023-01-17 10:48:05 -0700381 int seq, len, method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700382 enum uclass_id id;
383 const char *end;
Simon Glass73708252023-01-17 10:48:00 -0700384
Simon Glasse22fe922023-01-17 10:48:05 -0700385 method_flags = 0;
Simon Glass73708252023-01-17 10:48:00 -0700386 seq = trailing_strtoln_end(label, NULL, &end);
387 len = end - label;
388 if (!len)
389 return -EINVAL;
390 id = uclass_get_by_namelen(label, len);
391 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
392 uclass_get_name(id));
393 if (id == UCLASS_INVALID) {
Simon Glass215be682023-01-17 10:48:03 -0700394 /* try some special cases */
395 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
396 !strncmp("spi", label, len)) {
397 id = UCLASS_SPI_FLASH;
Simon Glasse22fe922023-01-17 10:48:05 -0700398 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
399 !strncmp("pxe", label, len)) {
400 id = UCLASS_ETH;
401 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
402 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
403 !strncmp("dhcp", label, len)) {
404 id = UCLASS_ETH;
405 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass215be682023-01-17 10:48:03 -0700406 } else {
Simon Glass4108c6b2023-04-24 13:49:47 +1200407 return -EPFNOSUPPORT;
Simon Glass215be682023-01-17 10:48:03 -0700408 }
Simon Glass73708252023-01-17 10:48:00 -0700409 }
410 if (id == UCLASS_USB)
411 id = UCLASS_MASS_STORAGE;
412 *seqp = seq;
Simon Glasse22fe922023-01-17 10:48:05 -0700413 if (method_flagsp)
414 *method_flagsp = method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700415
416 return id;
417}
418
Simon Glasse22fe922023-01-17 10:48:05 -0700419int bootdev_find_by_label(const char *label, struct udevice **devp,
420 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600421{
Simon Glasse22fe922023-01-17 10:48:05 -0700422 int seq, ret, method_flags = 0;
Simon Glass017656e2022-04-24 23:31:07 -0600423 struct udevice *media;
424 struct uclass *uc;
425 enum uclass_id id;
Simon Glass017656e2022-04-24 23:31:07 -0600426
Simon Glasse22fe922023-01-17 10:48:05 -0700427 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass73708252023-01-17 10:48:00 -0700428 if (ret < 0)
429 return log_msg_ret("uc", ret);
430 id = ret;
Simon Glass017656e2022-04-24 23:31:07 -0600431
432 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
433 uclass_id_foreach_dev(id, media, uc) {
434 struct udevice *bdev, *blk;
435 int ret;
436
437 /* if there is no seq, match anything */
438 if (seq != -1 && dev_seq(media) != seq) {
439 log_debug("- skip, media seq=%d\n", dev_seq(media));
440 continue;
441 }
442
443 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
444 &bdev);
445 if (ret) {
446 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
447 id);
448 ret = blk_find_device(id, seq, &blk);
449 if (!ret) {
450 log_debug("- get from blk %s\n", blk->name);
451 ret = bootdev_get_from_blk(blk, &bdev);
452 }
453 }
454 if (!ret) {
455 log_debug("- found %s\n", bdev->name);
456 *devp = bdev;
Simon Glassde567b12023-01-17 10:48:09 -0700457
458 /*
459 * if no sequence number was provided, we must scan all
460 * bootdevs for this media uclass
461 */
462 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
463 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glasse22fe922023-01-17 10:48:05 -0700464 if (method_flagsp)
465 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600466 return 0;
467 }
468 log_debug("- no device in %s\n", media->name);
469 }
Simon Glass017656e2022-04-24 23:31:07 -0600470
471 return -ENOENT;
472}
473
Simon Glasse22fe922023-01-17 10:48:05 -0700474int bootdev_find_by_any(const char *name, struct udevice **devp,
475 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600476{
477 struct udevice *dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700478 int method_flags = 0;
Simon Glassde567b12023-01-17 10:48:09 -0700479 int ret = -ENODEV, seq;
Simon Glass017656e2022-04-24 23:31:07 -0600480 char *endp;
481
482 seq = simple_strtol(name, &endp, 16);
483
484 /* Select by name, label or number */
485 if (*endp) {
486 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
487 if (ret == -ENODEV) {
Simon Glasse22fe922023-01-17 10:48:05 -0700488 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600489 if (ret) {
490 printf("Cannot find bootdev '%s' (err=%d)\n",
491 name, ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700492 return log_msg_ret("lab", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600493 }
494 ret = device_probe(dev);
495 }
496 if (ret) {
497 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
498 ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700499 return log_msg_ret("pro", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600500 }
Simon Glassde567b12023-01-17 10:48:09 -0700501 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass017656e2022-04-24 23:31:07 -0600502 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glassde567b12023-01-17 10:48:09 -0700503 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass017656e2022-04-24 23:31:07 -0600504 }
505 if (ret) {
506 printf("Cannot find '%s' (err=%d)\n", name, ret);
507 return ret;
508 }
509
510 *devp = dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700511 if (method_flagsp)
512 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600513
514 return 0;
515}
516
Simon Glassde567b12023-01-17 10:48:09 -0700517int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
518 int *method_flagsp)
519{
520 int ret;
521
522 ret = bootdev_hunt(label, false);
523 if (ret)
524 return log_msg_ret("scn", ret);
525 ret = bootdev_find_by_label(label, devp, method_flagsp);
526 if (ret)
527 return log_msg_ret("fnd", ret);
528
529 return 0;
530}
531
Simon Glassd6e39d12023-01-17 10:47:26 -0700532static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
533 struct bootflow *bflow)
534{
535 struct udevice *blk;
536 int ret;
537
538 ret = bootdev_get_sibling_blk(dev, &blk);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600539 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
540 ret ? "(none)" : blk->name);
Simon Glassd6e39d12023-01-17 10:47:26 -0700541 /*
542 * If there is no media, indicate that no more partitions should be
543 * checked
544 */
545 if (ret == -EOPNOTSUPP)
546 ret = -ESHUTDOWN;
547 if (ret)
548 return log_msg_ret("blk", ret);
549 assert(blk);
550 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
551 if (ret)
552 return log_msg_ret("find", ret);
553
554 return 0;
555}
556
Simon Glass017656e2022-04-24 23:31:07 -0600557int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
558 struct bootflow *bflow)
559{
560 const struct bootdev_ops *ops = bootdev_get_ops(dev);
561
Simon Glass33063462023-01-17 10:48:18 -0700562 log_debug("->get_bootflow %s=%p\n", dev->name, ops->get_bootflow);
Simon Glass00501fc2022-10-20 18:22:51 -0600563 bootflow_init(bflow, dev, iter->method);
Simon Glassd6e39d12023-01-17 10:47:26 -0700564 if (!ops->get_bootflow)
565 return default_get_bootflow(dev, iter, bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600566
567 return ops->get_bootflow(dev, iter, bflow);
568}
569
570void bootdev_clear_bootflows(struct udevice *dev)
571{
572 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
573
574 while (!list_empty(&ucp->bootflow_head)) {
575 struct bootflow *bflow;
576
577 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
578 bm_node);
Simon Glass03fcbf92022-04-24 23:31:09 -0600579 bootflow_remove(bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600580 }
581}
582
Simon Glassac06b26a2023-01-17 10:48:10 -0700583int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
584 int *method_flagsp)
585{
586 struct udevice *dev;
587
588 log_debug("next\n");
589 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass4108c6b2023-04-24 13:49:47 +1200590 const char *label = iter->labels[iter->cur_label];
591 int ret;
592
593 log_debug("Scanning: %s\n", label);
594 ret = bootdev_hunt_and_find_by_label(label, &dev,
595 method_flagsp);
596 if (iter->flags & BOOTFLOWIF_SHOW) {
597 if (ret == -EPFNOSUPPORT) {
598 log_warning("Unknown uclass '%s' in label\n",
599 label);
600 } else if (ret == -ENOENT) {
601 /*
602 * looking for, e.g. 'scsi0' should find
603 * something if SCSI is present
604 */
605 if (!trailing_strtol(label)) {
606 log_warning("No bootdevs for '%s'\n",
607 label);
608 }
609 }
610 }
611
Simon Glassac06b26a2023-01-17 10:48:10 -0700612 }
613
614 if (!dev)
615 return log_msg_ret("fin", -ENODEV);
616 *devp = dev;
617
618 return 0;
619}
620
Simon Glass660a9952023-01-17 10:48:11 -0700621int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
622{
623 struct udevice *dev = *devp;
624 bool found;
625 int ret;
626
627 /* find the next device with this priority */
628 *devp = NULL;
629 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
630 dev ? dev->name : "none");
631 do {
632 /*
633 * Don't probe devices here since they may not be of the
634 * required priority
635 */
636 if (!dev)
637 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
638 else
639 uclass_find_next_device(&dev);
640 found = false;
641
642 /* scan for the next device with the correct priority */
643 while (dev) {
644 struct bootdev_uc_plat *plat;
645
646 plat = dev_get_uclass_plat(dev);
647 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
648 iter->cur_prio);
649 if (plat->prio == iter->cur_prio)
650 break;
651 uclass_find_next_device(&dev);
652 }
653
654 /* none found for this priority, so move to the next */
655 if (!dev) {
656 log_debug("None found at prio %d, moving to %d\n",
657 iter->cur_prio, iter->cur_prio + 1);
658 if (++iter->cur_prio == BOOTDEVP_COUNT)
659 return log_msg_ret("fin", -ENODEV);
660
Simon Glass99e68182023-02-22 12:17:03 -0700661 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass660a9952023-01-17 10:48:11 -0700662 /* hunt to find new bootdevs */
663 ret = bootdev_hunt_prio(iter->cur_prio,
664 iter->flags &
Simon Glass99e68182023-02-22 12:17:03 -0700665 BOOTFLOWIF_SHOW);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600666 log_debug("- bootdev_hunt_prio() ret %d\n",
667 ret);
Simon Glass660a9952023-01-17 10:48:11 -0700668 if (ret)
669 return log_msg_ret("hun", ret);
670 }
671 } else {
672 ret = device_probe(dev);
673 if (ret) {
674 log_debug("Device '%s' failed to probe\n",
675 dev->name);
676 dev = NULL;
677 }
678 }
679 } while (!dev);
680
681 *devp = dev;
682
683 return 0;
684}
685
Simon Glassba3d5372023-01-17 10:48:15 -0700686int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
687 struct udevice **devp, int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600688{
Simon Glassba3d5372023-01-17 10:48:15 -0700689 struct udevice *bootstd, *dev = NULL;
Simon Glass99e68182023-02-22 12:17:03 -0700690 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass484e4072023-01-17 10:48:14 -0700691 int method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600692 int ret;
693
694 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
695 if (ret) {
696 log_err("Missing bootstd device\n");
697 return log_msg_ret("std", ret);
698 }
699
Simon Glass7e1f6a42023-01-17 10:48:08 -0700700 /* hunt for any pre-scan devices */
Simon Glass99e68182023-02-22 12:17:03 -0700701 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700702 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600703 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700704 if (ret)
705 return log_msg_ret("pre", ret);
706 }
707
Simon Glass017656e2022-04-24 23:31:07 -0600708 /* Handle scanning a single device */
Simon Glassba3d5372023-01-17 10:48:15 -0700709 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass99e68182023-02-22 12:17:03 -0700710 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glassba3d5372023-01-17 10:48:15 -0700711 ret = bootdev_hunt(label, show);
712 if (ret)
713 return log_msg_ret("hun", ret);
714 }
715 ret = bootdev_find_by_any(label, &dev, &method_flags);
716 if (ret)
717 return log_msg_ret("lab", ret);
718
719 log_debug("method_flags: %x\n", method_flags);
720 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass99e68182023-02-22 12:17:03 -0700721 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glassba3d5372023-01-17 10:48:15 -0700722 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass99e68182023-02-22 12:17:03 -0700723 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glassba3d5372023-01-17 10:48:15 -0700724 else
Simon Glass99e68182023-02-22 12:17:03 -0700725 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glassba3d5372023-01-17 10:48:15 -0700726 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass484e4072023-01-17 10:48:14 -0700727 } else {
728 bool ok;
Simon Glass017656e2022-04-24 23:31:07 -0600729
Simon Glass484e4072023-01-17 10:48:14 -0700730 /* This either returns a non-empty list or NULL */
731 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
732 if (!ok)
733 return log_msg_ret("ord", -ENOMEM);
734 log_debug("setup labels %p\n", iter->labels);
735 if (iter->labels) {
736 iter->cur_label = -1;
737 ret = bootdev_next_label(iter, &dev, &method_flags);
738 } else {
739 ret = bootdev_next_prio(iter, &dev);
740 method_flags = 0;
741 }
742 if (!dev)
743 return log_msg_ret("fin", -ENOENT);
744 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass017656e2022-04-24 23:31:07 -0600745 }
746
Simon Glass017656e2022-04-24 23:31:07 -0600747 ret = device_probe(dev);
748 if (ret)
749 return log_msg_ret("probe", ret);
Simon Glass484e4072023-01-17 10:48:14 -0700750 if (method_flagsp)
751 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600752 *devp = dev;
753
754 return 0;
755}
756
Simon Glass1248d2b2023-01-17 10:47:34 -0700757static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
758{
759 const char *name = uclass_get_name(info->uclass);
760 struct bootstd_priv *std;
761 int ret;
762
763 ret = bootstd_get_priv(&std);
764 if (ret)
765 return log_msg_ret("std", ret);
766
767 if (!(std->hunters_used & BIT(seq))) {
768 if (show)
769 printf("Hunting with: %s\n",
770 uclass_get_name(info->uclass));
771 log_debug("Hunting with: %s\n", name);
772 if (info->hunt) {
773 ret = info->hunt(info, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600774 log_debug(" - hunt result %d\n", ret);
Simon Glass1248d2b2023-01-17 10:47:34 -0700775 if (ret)
776 return ret;
777 }
778 std->hunters_used |= BIT(seq);
779 }
780
781 return 0;
782}
783
784int bootdev_hunt(const char *spec, bool show)
785{
786 struct bootdev_hunter *start;
787 const char *end;
788 int n_ent, i;
789 int result;
790 size_t len;
791
792 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
793 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
794 result = 0;
795
796 len = SIZE_MAX;
797 if (spec) {
798 trailing_strtoln_end(spec, NULL, &end);
799 len = end - spec;
800 }
801
802 for (i = 0; i < n_ent; i++) {
803 struct bootdev_hunter *info = start + i;
804 const char *name = uclass_get_name(info->uclass);
805 int ret;
806
807 log_debug("looking at %.*s for %s\n",
808 (int)max(strlen(name), len), spec, name);
Simon Glassac06b26a2023-01-17 10:48:10 -0700809 if (spec && strncmp(spec, name, max(strlen(name), len))) {
810 if (info->uclass != UCLASS_ETH ||
811 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
812 continue;
813 }
Simon Glass1248d2b2023-01-17 10:47:34 -0700814 ret = bootdev_hunt_drv(info, i, show);
815 if (ret)
816 result = ret;
817 }
818
819 return result;
820}
821
Simon Glass3e9f6be2023-01-17 10:48:07 -0700822int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
823{
824 struct bootdev_hunter *start;
825 int n_ent, i;
826 int result;
827
828 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
829 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
830 result = 0;
831
832 log_debug("Hunting for priority %d\n", prio);
833 for (i = 0; i < n_ent; i++) {
834 struct bootdev_hunter *info = start + i;
835 int ret;
836
837 if (prio != info->prio)
838 continue;
839 ret = bootdev_hunt_drv(info, i, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600840 log_debug("bootdev_hunt_drv() return %d\n", ret);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700841 if (ret && ret != -ENOENT)
842 result = ret;
843 }
Simon Glassbf2d02a2023-07-30 11:15:16 -0600844 log_debug("exit %d\n", result);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700845
846 return result;
847}
848
Simon Glass5acb97a2023-01-17 10:47:33 -0700849void bootdev_list_hunters(struct bootstd_priv *std)
850{
851 struct bootdev_hunter *orig, *start;
852 int n_ent, i;
853
854 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
855 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
856
857 /*
858 * workaround for strange bug in clang-12 which sees all the below data
859 * as zeroes. Any access of start seems to fix it, such as
860 *
861 * printf("%p", start);
862 *
863 * Use memcpy() to force the correct behaviour.
864 */
865 memcpy(&start, &orig, sizeof(orig));
866 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
867 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
868 for (i = 0; i < n_ent; i++) {
869 struct bootdev_hunter *info = start + i;
870
871 printf("%4d %4s %-15s %s\n", info->prio,
872 std->hunters_used & BIT(i) ? "*" : "",
873 uclass_get_name(info->uclass),
874 info->drv ? info->drv->name : "(none)");
875 }
876
877 printf("(total hunters: %d)\n", n_ent);
878}
879
Simon Glass017656e2022-04-24 23:31:07 -0600880static int bootdev_post_bind(struct udevice *dev)
881{
882 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
883
884 INIT_LIST_HEAD(&ucp->bootflow_head);
885
886 return 0;
887}
888
889static int bootdev_pre_unbind(struct udevice *dev)
890{
891 bootdev_clear_bootflows(dev);
892
893 return 0;
894}
895
896UCLASS_DRIVER(bootdev) = {
897 .id = UCLASS_BOOTDEV,
898 .name = "bootdev",
899 .flags = DM_UC_FLAG_SEQ_ALIAS,
900 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
901 .post_bind = bootdev_post_bind,
902 .pre_unbind = bootdev_pre_unbind,
903};