blob: d01d603700d9f481478fe603ddd8e142cf53510f [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 */
Caleb Connolly79d87f22023-10-17 11:57:07 +010030 MAX_PART_PER_BOOTDEV = 96,
Simon Glass201417d2022-04-24 23:31:07 -060031
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{
Simon Glass831405f2023-08-24 13:55:43 -0600114 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method);
115 bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART;
Simon Glass201417d2022-04-24 23:31:07 -0600116 struct blk_desc *desc = dev_get_uclass_plat(blk);
117 struct disk_partition info;
118 char partstr[20];
119 char name[60];
120 int ret;
121
122 /* Sanity check */
123 if (iter->part >= MAX_PART_PER_BOOTDEV)
124 return log_msg_ret("max", -ESHUTDOWN);
125
126 bflow->blk = blk;
127 if (iter->part)
128 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
129 else
130 strcpy(partstr, "whole");
131 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
132 bflow->name = strdup(name);
133 if (!bflow->name)
134 return log_msg_ret("name", -ENOMEM);
135
136 bflow->part = iter->part;
137
Simon Glassa8f5be12022-04-24 23:31:09 -0600138 ret = bootmeth_check(bflow->method, iter);
139 if (ret)
140 return log_msg_ret("check", ret);
141
Simon Glass201417d2022-04-24 23:31:07 -0600142 /*
143 * partition numbers start at 0 so this cannot succeed, but it can tell
144 * us whether there is valid media there
145 */
146 ret = part_get_info(desc, iter->part, &info);
Simon Glass831405f2023-08-24 13:55:43 -0600147 log_debug("part_get_info() returned %d\n", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600148 if (!iter->part && ret == -ENOENT)
149 ret = 0;
150
151 /*
152 * This error indicates the media is not present. Otherwise we just
153 * blindly scan the next partition. We could be more intelligent here
154 * and check which partition numbers actually exist.
155 */
156 if (ret == -EOPNOTSUPP)
157 ret = -ESHUTDOWN;
158 else
159 bflow->state = BOOTFLOWST_MEDIA;
Simon Glass831405f2023-08-24 13:55:43 -0600160 if (ret && !allow_any_part) {
Simon Glass76afc842023-04-28 13:18:09 -0600161 /* allow partition 1 to be missing */
162 if (iter->part == 1) {
163 iter->max_part = 3;
164 ret = -ENOENT;
165 }
166
Simon Glass201417d2022-04-24 23:31:07 -0600167 return log_msg_ret("part", ret);
Simon Glass76afc842023-04-28 13:18:09 -0600168 }
Simon Glass201417d2022-04-24 23:31:07 -0600169
170 /*
171 * Currently we don't get the number of partitions, so just
172 * assume a large number
173 */
174 iter->max_part = MAX_PART_PER_BOOTDEV;
175
Caleb Connolly79d87f22023-10-17 11:57:07 +0100176 if (!IS_ENABLED(CONFIG_BOOTSTD_IGNORE_BOOTABLE)) {
177 /* If this is the whole disk, check if we have bootable partitions */
178 if (!iter->part) {
179 iter->first_bootable = part_get_bootable(desc);
180 log_debug("checking bootable=%d\n", iter->first_bootable);
181 } else if (allow_any_part) {
182 /*
183 * allow any partition to be scanned, by skipping any checks
184 * for filesystems or partition contents on this disk
185 */
Simon Glassf0e358f2023-01-17 10:47:42 -0700186
Caleb Connolly79d87f22023-10-17 11:57:07 +0100187 /* if there are bootable partitions, scan only those */
188 } else if (iter->first_bootable >= 0 &&
189 (iter->first_bootable ? !info.bootable : iter->part != 1)) {
190 return log_msg_ret("boot", -EINVAL);
191 }
192 }
193
194 if (iter->part) {
Simon Glass201417d2022-04-24 23:31:07 -0600195 ret = fs_set_blk_dev_with_part(desc, bflow->part);
196 bflow->state = BOOTFLOWST_PART;
Simon Glass1aabe4e2023-04-24 13:49:49 +1200197 if (ret)
198 return log_msg_ret("fs", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600199
Simon Glass201417d2022-04-24 23:31:07 -0600200 log_debug("%s: Found partition %x type %x fstype %d\n",
Simon Glassb2b7e6c2023-08-24 13:55:33 -0600201 blk->name, bflow->part,
202 IS_ENABLED(CONFIG_DOS_PARTITION) ?
203 disk_partition_sys_ind(&info) : 0,
Simon Glass201417d2022-04-24 23:31:07 -0600204 ret ? -1 : fs_get_type());
Simon Glass1aabe4e2023-04-24 13:49:49 +1200205 bflow->blk = blk;
Simon Glass201417d2022-04-24 23:31:07 -0600206 bflow->state = BOOTFLOWST_FS;
207 }
208
Simon Glass831405f2023-08-24 13:55:43 -0600209 log_debug("method %s\n", bflow->method->name);
Simon Glassa8f5be12022-04-24 23:31:09 -0600210 ret = bootmeth_read_bootflow(bflow->method, bflow);
211 if (ret)
212 return log_msg_ret("method", ret);
213
Simon Glass201417d2022-04-24 23:31:07 -0600214 return 0;
215}
216
217void bootdev_list(bool probe)
218{
219 struct udevice *dev;
220 int ret;
221 int i;
222
223 printf("Seq Probed Status Uclass Name\n");
224 printf("--- ------ ------ -------- ------------------\n");
225 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200226 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass201417d2022-04-24 23:31:07 -0600227 else
228 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
229 for (i = 0; dev; i++) {
230 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
231 device_active(dev) ? '+' : ' ',
Heinrich Schuchardtca9d9262023-07-30 16:29:25 +0200232 ret ? simple_itoa(-ret) : "OK",
Simon Glass201417d2022-04-24 23:31:07 -0600233 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
234 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200235 ret = uclass_next_device_check(&dev);
Simon Glass201417d2022-04-24 23:31:07 -0600236 else
237 ret = uclass_find_next_device(&dev);
238 }
239 printf("--- ------ ------ -------- ------------------\n");
240 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
241}
242
243int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
244{
245 struct udevice *bdev;
246 int ret;
247
248 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
249 &bdev);
250 if (ret) {
251 if (ret != -ENODEV) {
252 log_debug("Cannot access bootdev device\n");
253 return ret;
254 }
255
256 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
257 if (ret) {
258 log_debug("Cannot create bootdev device\n");
259 return ret;
260 }
261 }
262
263 return 0;
264}
265
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700266static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
267{
268 int len, slen;
269
270 len = strlen(dev->name);
271 slen = strlen(suffix);
272 if (len > slen && !strcmp(suffix, dev->name + len - slen))
273 return len - slen;
274
275 return len;
276}
277
Simon Glassd7d78572023-07-30 11:15:14 -0600278int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
Simon Glass201417d2022-04-24 23:31:07 -0600279{
280 struct udevice *parent, *dev;
281 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700282 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600283
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700284 len = bootdev_get_suffix_start(blk, ".blk");
285 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
286 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600287
288 parent = dev_get_parent(blk);
289 ret = device_find_child_by_name(parent, dev_name, &dev);
290 if (ret) {
291 char *str;
292
293 if (ret != -ENODEV) {
294 log_debug("Cannot access bootdev device\n");
295 return ret;
296 }
297 str = strdup(dev_name);
298 if (!str)
299 return -ENOMEM;
300
301 ret = device_bind_driver(parent, drv_name, str, &dev);
302 if (ret) {
303 log_debug("Cannot create bootdev device\n");
304 return ret;
305 }
306 device_set_name_alloced(dev);
307 }
308
309 return 0;
310}
311
312int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
313{
314 struct udevice *parent = dev_get_parent(dev);
315 struct udevice *blk;
316 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600317
318 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
319 return -EINVAL;
320
Simon Glassd7d78572023-07-30 11:15:14 -0600321 /*
322 * This should always work if bootdev_setup_for_sibling_blk() was used
323 */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700324 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600325 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700326 if (ret) {
327 char dev_name[50];
328
329 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
330 dev->name);
331 ret = device_find_child_by_name(parent, dev_name, &blk);
332 if (ret)
333 return log_msg_ret("find", ret);
334 }
Simon Glass965020c2023-01-28 15:00:19 -0700335 ret = device_probe(blk);
336 if (ret)
337 return log_msg_ret("act", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600338 *blkp = blk;
339
340 return 0;
341}
342
343static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
344{
345 struct udevice *parent = dev_get_parent(blk);
346 struct udevice *bootdev;
347 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700348 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600349
350 if (device_get_uclass_id(blk) != UCLASS_BLK)
351 return -EINVAL;
352
Simon Glassd7d78572023-07-30 11:15:14 -0600353 /* This should always work if bootdev_setup_for_sibling_blk() was used */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700354 len = bootdev_get_suffix_start(blk, ".blk");
355 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
356 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600357 ret = device_find_child_by_name(parent, dev_name, &bootdev);
358 if (ret)
359 return log_msg_ret("find", ret);
360 *bootdevp = bootdev;
361
362 return 0;
363}
364
365int bootdev_unbind_dev(struct udevice *parent)
366{
367 struct udevice *dev;
368 int ret;
369
370 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
371 if (!ret) {
372 ret = device_remove(dev, DM_REMOVE_NORMAL);
373 if (ret)
374 return log_msg_ret("rem", ret);
375 ret = device_unbind(dev);
376 if (ret)
377 return log_msg_ret("unb", ret);
378 }
379
380 return 0;
381}
382
383/**
Simon Glass74ebfb62023-01-17 10:48:00 -0700384 * label_to_uclass() - Convert a label to a uclass and sequence number
385 *
386 * @label: Label to look up (e.g. "mmc1" or "mmc0")
387 * @seqp: Returns the sequence number, or -1 if none
Simon Glassd9f48572023-01-17 10:48:05 -0700388 * @method_flagsp: If non-NULL, returns any flags implied by the label
389 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass1736b4a2023-04-24 13:49:47 +1200390 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
391 * known, other -ve error code on other error
Simon Glass74ebfb62023-01-17 10:48:00 -0700392 */
Simon Glassd9f48572023-01-17 10:48:05 -0700393static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass74ebfb62023-01-17 10:48:00 -0700394{
Simon Glassd9f48572023-01-17 10:48:05 -0700395 int seq, len, method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700396 enum uclass_id id;
397 const char *end;
Simon Glass74ebfb62023-01-17 10:48:00 -0700398
Simon Glassd9f48572023-01-17 10:48:05 -0700399 method_flags = 0;
Simon Glass74ebfb62023-01-17 10:48:00 -0700400 seq = trailing_strtoln_end(label, NULL, &end);
401 len = end - label;
402 if (!len)
403 return -EINVAL;
404 id = uclass_get_by_namelen(label, len);
405 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
406 uclass_get_name(id));
407 if (id == UCLASS_INVALID) {
Simon Glass0c1f4a92023-01-17 10:48:03 -0700408 /* try some special cases */
409 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
410 !strncmp("spi", label, len)) {
411 id = UCLASS_SPI_FLASH;
Simon Glassd9f48572023-01-17 10:48:05 -0700412 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
413 !strncmp("pxe", label, len)) {
414 id = UCLASS_ETH;
415 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
416 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
417 !strncmp("dhcp", label, len)) {
418 id = UCLASS_ETH;
419 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700420 } else {
Simon Glass1736b4a2023-04-24 13:49:47 +1200421 return -EPFNOSUPPORT;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700422 }
Simon Glass74ebfb62023-01-17 10:48:00 -0700423 }
424 if (id == UCLASS_USB)
425 id = UCLASS_MASS_STORAGE;
426 *seqp = seq;
Simon Glassd9f48572023-01-17 10:48:05 -0700427 if (method_flagsp)
428 *method_flagsp = method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700429
430 return id;
431}
432
Simon Glassd9f48572023-01-17 10:48:05 -0700433int bootdev_find_by_label(const char *label, struct udevice **devp,
434 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600435{
Simon Glassd9f48572023-01-17 10:48:05 -0700436 int seq, ret, method_flags = 0;
Simon Glass201417d2022-04-24 23:31:07 -0600437 struct udevice *media;
438 struct uclass *uc;
439 enum uclass_id id;
Simon Glass201417d2022-04-24 23:31:07 -0600440
Simon Glassd9f48572023-01-17 10:48:05 -0700441 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass74ebfb62023-01-17 10:48:00 -0700442 if (ret < 0)
443 return log_msg_ret("uc", ret);
444 id = ret;
Simon Glass201417d2022-04-24 23:31:07 -0600445
446 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
447 uclass_id_foreach_dev(id, media, uc) {
448 struct udevice *bdev, *blk;
449 int ret;
450
451 /* if there is no seq, match anything */
452 if (seq != -1 && dev_seq(media) != seq) {
453 log_debug("- skip, media seq=%d\n", dev_seq(media));
454 continue;
455 }
456
457 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
458 &bdev);
459 if (ret) {
460 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
461 id);
462 ret = blk_find_device(id, seq, &blk);
463 if (!ret) {
464 log_debug("- get from blk %s\n", blk->name);
465 ret = bootdev_get_from_blk(blk, &bdev);
466 }
467 }
468 if (!ret) {
469 log_debug("- found %s\n", bdev->name);
470 *devp = bdev;
Simon Glass66e3dce2023-01-17 10:48:09 -0700471
472 /*
473 * if no sequence number was provided, we must scan all
474 * bootdevs for this media uclass
475 */
Simon Glasse824d0d2023-09-23 14:50:15 -0600476 if (seq == -1)
Simon Glass66e3dce2023-01-17 10:48:09 -0700477 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glassd9f48572023-01-17 10:48:05 -0700478 if (method_flagsp)
479 *method_flagsp = method_flags;
Simon Glasse824d0d2023-09-23 14:50:15 -0600480 log_debug("method flags %x\n", method_flags);
Simon Glass201417d2022-04-24 23:31:07 -0600481 return 0;
482 }
483 log_debug("- no device in %s\n", media->name);
484 }
Simon Glass201417d2022-04-24 23:31:07 -0600485
486 return -ENOENT;
487}
488
Simon Glassd9f48572023-01-17 10:48:05 -0700489int bootdev_find_by_any(const char *name, struct udevice **devp,
490 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600491{
492 struct udevice *dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700493 int method_flags = 0;
Simon Glass66e3dce2023-01-17 10:48:09 -0700494 int ret = -ENODEV, seq;
Simon Glass201417d2022-04-24 23:31:07 -0600495 char *endp;
496
497 seq = simple_strtol(name, &endp, 16);
498
499 /* Select by name, label or number */
500 if (*endp) {
501 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
502 if (ret == -ENODEV) {
Simon Glassd9f48572023-01-17 10:48:05 -0700503 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass201417d2022-04-24 23:31:07 -0600504 if (ret) {
505 printf("Cannot find bootdev '%s' (err=%d)\n",
506 name, ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700507 return log_msg_ret("lab", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600508 }
509 ret = device_probe(dev);
510 }
511 if (ret) {
512 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
513 ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700514 return log_msg_ret("pro", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600515 }
Simon Glass66e3dce2023-01-17 10:48:09 -0700516 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass201417d2022-04-24 23:31:07 -0600517 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glass66e3dce2023-01-17 10:48:09 -0700518 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass201417d2022-04-24 23:31:07 -0600519 }
520 if (ret) {
521 printf("Cannot find '%s' (err=%d)\n", name, ret);
522 return ret;
523 }
524
525 *devp = dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700526 if (method_flagsp)
527 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600528
529 return 0;
530}
531
Simon Glass66e3dce2023-01-17 10:48:09 -0700532int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
533 int *method_flagsp)
534{
535 int ret;
536
537 ret = bootdev_hunt(label, false);
538 if (ret)
539 return log_msg_ret("scn", ret);
540 ret = bootdev_find_by_label(label, devp, method_flagsp);
541 if (ret)
542 return log_msg_ret("fnd", ret);
543
544 return 0;
545}
546
Simon Glassb85fc8d2023-01-17 10:47:26 -0700547static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
548 struct bootflow *bflow)
549{
550 struct udevice *blk;
551 int ret;
552
553 ret = bootdev_get_sibling_blk(dev, &blk);
Simon Glass3500ae12023-07-30 11:15:16 -0600554 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
555 ret ? "(none)" : blk->name);
Simon Glassb85fc8d2023-01-17 10:47:26 -0700556 /*
557 * If there is no media, indicate that no more partitions should be
558 * checked
559 */
560 if (ret == -EOPNOTSUPP)
561 ret = -ESHUTDOWN;
562 if (ret)
563 return log_msg_ret("blk", ret);
564 assert(blk);
565 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
566 if (ret)
567 return log_msg_ret("find", ret);
568
569 return 0;
570}
571
Simon Glass201417d2022-04-24 23:31:07 -0600572int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
573 struct bootflow *bflow)
574{
575 const struct bootdev_ops *ops = bootdev_get_ops(dev);
576
Simon Glass831405f2023-08-24 13:55:43 -0600577 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
578 ops->get_bootflow);
Simon Glassb190deb2022-10-20 18:22:51 -0600579 bootflow_init(bflow, dev, iter->method);
Simon Glassb85fc8d2023-01-17 10:47:26 -0700580 if (!ops->get_bootflow)
581 return default_get_bootflow(dev, iter, bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600582
583 return ops->get_bootflow(dev, iter, bflow);
584}
585
586void bootdev_clear_bootflows(struct udevice *dev)
587{
588 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
589
590 while (!list_empty(&ucp->bootflow_head)) {
591 struct bootflow *bflow;
592
593 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
594 bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600595 bootflow_remove(bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600596 }
597}
598
Simon Glasse4b69482023-01-17 10:48:10 -0700599int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
600 int *method_flagsp)
601{
602 struct udevice *dev;
603
604 log_debug("next\n");
605 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass1736b4a2023-04-24 13:49:47 +1200606 const char *label = iter->labels[iter->cur_label];
607 int ret;
608
609 log_debug("Scanning: %s\n", label);
610 ret = bootdev_hunt_and_find_by_label(label, &dev,
611 method_flagsp);
612 if (iter->flags & BOOTFLOWIF_SHOW) {
613 if (ret == -EPFNOSUPPORT) {
614 log_warning("Unknown uclass '%s' in label\n",
615 label);
616 } else if (ret == -ENOENT) {
617 /*
618 * looking for, e.g. 'scsi0' should find
619 * something if SCSI is present
620 */
621 if (!trailing_strtol(label)) {
622 log_warning("No bootdevs for '%s'\n",
623 label);
624 }
625 }
626 }
627
Simon Glasse4b69482023-01-17 10:48:10 -0700628 }
629
630 if (!dev)
631 return log_msg_ret("fin", -ENODEV);
632 *devp = dev;
633
634 return 0;
635}
636
Simon Glass43e89a32023-01-17 10:48:11 -0700637int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
638{
639 struct udevice *dev = *devp;
640 bool found;
641 int ret;
642
643 /* find the next device with this priority */
644 *devp = NULL;
645 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
646 dev ? dev->name : "none");
647 do {
648 /*
649 * Don't probe devices here since they may not be of the
650 * required priority
651 */
652 if (!dev)
653 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
654 else
655 uclass_find_next_device(&dev);
656 found = false;
657
658 /* scan for the next device with the correct priority */
659 while (dev) {
660 struct bootdev_uc_plat *plat;
661
662 plat = dev_get_uclass_plat(dev);
663 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
664 iter->cur_prio);
665 if (plat->prio == iter->cur_prio)
666 break;
667 uclass_find_next_device(&dev);
668 }
669
670 /* none found for this priority, so move to the next */
671 if (!dev) {
672 log_debug("None found at prio %d, moving to %d\n",
673 iter->cur_prio, iter->cur_prio + 1);
674 if (++iter->cur_prio == BOOTDEVP_COUNT)
675 return log_msg_ret("fin", -ENODEV);
676
Simon Glass4f806f32023-02-22 12:17:03 -0700677 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass43e89a32023-01-17 10:48:11 -0700678 /* hunt to find new bootdevs */
679 ret = bootdev_hunt_prio(iter->cur_prio,
680 iter->flags &
Simon Glass4f806f32023-02-22 12:17:03 -0700681 BOOTFLOWIF_SHOW);
Simon Glass3500ae12023-07-30 11:15:16 -0600682 log_debug("- bootdev_hunt_prio() ret %d\n",
683 ret);
Simon Glass43e89a32023-01-17 10:48:11 -0700684 if (ret)
685 return log_msg_ret("hun", ret);
686 }
687 } else {
688 ret = device_probe(dev);
689 if (ret) {
690 log_debug("Device '%s' failed to probe\n",
691 dev->name);
692 dev = NULL;
693 }
694 }
695 } while (!dev);
696
697 *devp = dev;
698
699 return 0;
700}
701
Simon Glass91943ff2023-01-17 10:48:15 -0700702int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
703 struct udevice **devp, int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600704{
Simon Glass91943ff2023-01-17 10:48:15 -0700705 struct udevice *bootstd, *dev = NULL;
Simon Glass4f806f32023-02-22 12:17:03 -0700706 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass47aedc22023-01-17 10:48:14 -0700707 int method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600708 int ret;
709
710 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
711 if (ret) {
712 log_err("Missing bootstd device\n");
713 return log_msg_ret("std", ret);
714 }
715
Simon Glasseacc2612023-01-17 10:48:08 -0700716 /* hunt for any pre-scan devices */
Simon Glass4f806f32023-02-22 12:17:03 -0700717 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glasseacc2612023-01-17 10:48:08 -0700718 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
Simon Glass3500ae12023-07-30 11:15:16 -0600719 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
Simon Glasseacc2612023-01-17 10:48:08 -0700720 if (ret)
721 return log_msg_ret("pre", ret);
722 }
723
Simon Glass201417d2022-04-24 23:31:07 -0600724 /* Handle scanning a single device */
Simon Glass91943ff2023-01-17 10:48:15 -0700725 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass4f806f32023-02-22 12:17:03 -0700726 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass91943ff2023-01-17 10:48:15 -0700727 ret = bootdev_hunt(label, show);
728 if (ret)
729 return log_msg_ret("hun", ret);
730 }
731 ret = bootdev_find_by_any(label, &dev, &method_flags);
732 if (ret)
733 return log_msg_ret("lab", ret);
734
735 log_debug("method_flags: %x\n", method_flags);
736 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass4f806f32023-02-22 12:17:03 -0700737 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glass91943ff2023-01-17 10:48:15 -0700738 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass4f806f32023-02-22 12:17:03 -0700739 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glass91943ff2023-01-17 10:48:15 -0700740 else
Simon Glass4f806f32023-02-22 12:17:03 -0700741 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glass91943ff2023-01-17 10:48:15 -0700742 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass47aedc22023-01-17 10:48:14 -0700743 } else {
744 bool ok;
745
746 /* This either returns a non-empty list or NULL */
747 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
748 if (!ok)
749 return log_msg_ret("ord", -ENOMEM);
750 log_debug("setup labels %p\n", iter->labels);
751 if (iter->labels) {
752 iter->cur_label = -1;
753 ret = bootdev_next_label(iter, &dev, &method_flags);
754 } else {
755 ret = bootdev_next_prio(iter, &dev);
756 method_flags = 0;
757 }
758 if (!dev)
759 return log_msg_ret("fin", -ENOENT);
760 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass201417d2022-04-24 23:31:07 -0600761 }
762
Simon Glass201417d2022-04-24 23:31:07 -0600763 ret = device_probe(dev);
764 if (ret)
765 return log_msg_ret("probe", ret);
Simon Glass47aedc22023-01-17 10:48:14 -0700766 if (method_flagsp)
767 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600768 *devp = dev;
769
770 return 0;
771}
772
Simon Glassc7b63d52023-01-17 10:47:34 -0700773static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
774{
775 const char *name = uclass_get_name(info->uclass);
776 struct bootstd_priv *std;
777 int ret;
778
779 ret = bootstd_get_priv(&std);
780 if (ret)
781 return log_msg_ret("std", ret);
782
783 if (!(std->hunters_used & BIT(seq))) {
784 if (show)
785 printf("Hunting with: %s\n",
786 uclass_get_name(info->uclass));
787 log_debug("Hunting with: %s\n", name);
788 if (info->hunt) {
789 ret = info->hunt(info, show);
Simon Glass3500ae12023-07-30 11:15:16 -0600790 log_debug(" - hunt result %d\n", ret);
Simon Glassc7b63d52023-01-17 10:47:34 -0700791 if (ret)
792 return ret;
793 }
794 std->hunters_used |= BIT(seq);
795 }
796
797 return 0;
798}
799
800int bootdev_hunt(const char *spec, bool show)
801{
802 struct bootdev_hunter *start;
803 const char *end;
804 int n_ent, i;
805 int result;
806 size_t len;
807
808 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
809 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
810 result = 0;
811
812 len = SIZE_MAX;
813 if (spec) {
814 trailing_strtoln_end(spec, NULL, &end);
815 len = end - spec;
816 }
817
818 for (i = 0; i < n_ent; i++) {
819 struct bootdev_hunter *info = start + i;
820 const char *name = uclass_get_name(info->uclass);
821 int ret;
822
823 log_debug("looking at %.*s for %s\n",
824 (int)max(strlen(name), len), spec, name);
Simon Glasse4b69482023-01-17 10:48:10 -0700825 if (spec && strncmp(spec, name, max(strlen(name), len))) {
826 if (info->uclass != UCLASS_ETH ||
827 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
828 continue;
829 }
Simon Glassc7b63d52023-01-17 10:47:34 -0700830 ret = bootdev_hunt_drv(info, i, show);
831 if (ret)
832 result = ret;
833 }
834
835 return result;
836}
837
Simon Glass1b1d36e2023-09-20 07:29:49 -0600838int bootdev_unhunt(enum uclass_id id)
839{
840 struct bootdev_hunter *start;
841 int n_ent, i;
842
843 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
844 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
845 for (i = 0; i < n_ent; i++) {
846 struct bootdev_hunter *info = start + i;
847
848 if (info->uclass == id) {
849 struct bootstd_priv *std;
850 int ret;
851
852 ret = bootstd_get_priv(&std);
853 if (ret)
854 return log_msg_ret("std", ret);
855 if (!(std->hunters_used & BIT(i)))
856 return -EALREADY;
857 std->hunters_used &= ~BIT(i);
858 return 0;
859 }
860 }
861
862 return -ENOENT;
863}
864
Simon Glass79a7d4a2023-01-17 10:48:07 -0700865int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
866{
867 struct bootdev_hunter *start;
868 int n_ent, i;
869 int result;
870
871 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
872 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
873 result = 0;
874
875 log_debug("Hunting for priority %d\n", prio);
876 for (i = 0; i < n_ent; i++) {
877 struct bootdev_hunter *info = start + i;
878 int ret;
879
880 if (prio != info->prio)
881 continue;
882 ret = bootdev_hunt_drv(info, i, show);
Simon Glass3500ae12023-07-30 11:15:16 -0600883 log_debug("bootdev_hunt_drv() return %d\n", ret);
Simon Glass79a7d4a2023-01-17 10:48:07 -0700884 if (ret && ret != -ENOENT)
885 result = ret;
886 }
Simon Glass3500ae12023-07-30 11:15:16 -0600887 log_debug("exit %d\n", result);
Simon Glass79a7d4a2023-01-17 10:48:07 -0700888
889 return result;
890}
891
Simon Glassbd90b092023-01-17 10:47:33 -0700892void bootdev_list_hunters(struct bootstd_priv *std)
893{
894 struct bootdev_hunter *orig, *start;
895 int n_ent, i;
896
897 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
898 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
899
900 /*
901 * workaround for strange bug in clang-12 which sees all the below data
902 * as zeroes. Any access of start seems to fix it, such as
903 *
904 * printf("%p", start);
905 *
906 * Use memcpy() to force the correct behaviour.
907 */
908 memcpy(&start, &orig, sizeof(orig));
909 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
910 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
911 for (i = 0; i < n_ent; i++) {
912 struct bootdev_hunter *info = start + i;
913
914 printf("%4d %4s %-15s %s\n", info->prio,
915 std->hunters_used & BIT(i) ? "*" : "",
916 uclass_get_name(info->uclass),
917 info->drv ? info->drv->name : "(none)");
918 }
919
920 printf("(total hunters: %d)\n", n_ent);
921}
922
Simon Glass201417d2022-04-24 23:31:07 -0600923static int bootdev_post_bind(struct udevice *dev)
924{
925 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
926
927 INIT_LIST_HEAD(&ucp->bootflow_head);
928
929 return 0;
930}
931
932static int bootdev_pre_unbind(struct udevice *dev)
933{
934 bootdev_clear_bootflows(dev);
935
936 return 0;
937}
938
939UCLASS_DRIVER(bootdev) = {
940 .id = UCLASS_BOOTDEV,
941 .name = "bootdev",
942 .flags = DM_UC_FLAG_SEQ_ALIAS,
943 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
944 .post_bind = bootdev_post_bind,
945 .pre_unbind = bootdev_pre_unbind,
946};