blob: 0fa6dad8b11bf65f00d87fe25f4647b425170b13 [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
Simon Glass201417d2022-04-24 23:31:07 -060042 ret = bootstd_get_priv(&std);
43 if (ret)
44 return ret;
45
46 new = malloc(sizeof(*bflow));
47 if (!new)
48 return log_msg_ret("bflow", -ENOMEM);
49 memcpy(new, bflow, sizeof(*bflow));
50
51 list_add_tail(&new->glob_node, &std->glob_head);
Simon Glasseccb25c2022-07-30 15:52:23 -060052 if (bflow->dev) {
53 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
54
55 list_add_tail(&new->bm_node, &ucp->bootflow_head);
56 }
Simon Glass201417d2022-04-24 23:31:07 -060057
58 return 0;
59}
60
61int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
62{
63 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
64
65 if (list_empty(&ucp->bootflow_head))
66 return -ENOENT;
67
68 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
69 bm_node);
70
71 return 0;
72}
73
74int bootdev_next_bootflow(struct bootflow **bflowp)
75{
76 struct bootflow *bflow = *bflowp;
77 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
78
79 *bflowp = NULL;
80
81 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
82 return -ENOENT;
83
84 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
85
86 return 0;
87}
88
89int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
90 struct udevice **devp)
91{
92 struct udevice *dev;
93 char dev_name[30];
94 char *str;
95 int ret;
96
97 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
98 str = strdup(dev_name);
99 if (!str)
100 return -ENOMEM;
101 ret = device_bind_driver(parent, drv_name, str, &dev);
102 if (ret)
103 return ret;
104 device_set_name_alloced(dev);
105 *devp = dev;
106
107 return 0;
108}
109
110int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
111 struct bootflow_iter *iter, struct bootflow *bflow)
112{
Simon Glass831405f2023-08-24 13:55:43 -0600113 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method);
114 bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART;
Simon Glass201417d2022-04-24 23:31:07 -0600115 struct blk_desc *desc = dev_get_uclass_plat(blk);
116 struct disk_partition info;
117 char partstr[20];
118 char name[60];
119 int ret;
120
121 /* Sanity check */
122 if (iter->part >= MAX_PART_PER_BOOTDEV)
123 return log_msg_ret("max", -ESHUTDOWN);
124
125 bflow->blk = blk;
126 if (iter->part)
127 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
128 else
129 strcpy(partstr, "whole");
130 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
131 bflow->name = strdup(name);
132 if (!bflow->name)
133 return log_msg_ret("name", -ENOMEM);
134
135 bflow->part = iter->part;
136
Simon Glassa8f5be12022-04-24 23:31:09 -0600137 ret = bootmeth_check(bflow->method, iter);
138 if (ret)
139 return log_msg_ret("check", ret);
140
Simon Glass201417d2022-04-24 23:31:07 -0600141 /*
142 * partition numbers start at 0 so this cannot succeed, but it can tell
143 * us whether there is valid media there
144 */
145 ret = part_get_info(desc, iter->part, &info);
Simon Glass831405f2023-08-24 13:55:43 -0600146 log_debug("part_get_info() returned %d\n", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600147 if (!iter->part && ret == -ENOENT)
148 ret = 0;
149
150 /*
151 * This error indicates the media is not present. Otherwise we just
152 * blindly scan the next partition. We could be more intelligent here
153 * and check which partition numbers actually exist.
154 */
155 if (ret == -EOPNOTSUPP)
156 ret = -ESHUTDOWN;
157 else
158 bflow->state = BOOTFLOWST_MEDIA;
Simon Glass831405f2023-08-24 13:55:43 -0600159 if (ret && !allow_any_part) {
Simon Glass76afc842023-04-28 13:18:09 -0600160 /* allow partition 1 to be missing */
161 if (iter->part == 1) {
162 iter->max_part = 3;
163 ret = -ENOENT;
164 }
165
Simon Glass201417d2022-04-24 23:31:07 -0600166 return log_msg_ret("part", ret);
Simon Glass76afc842023-04-28 13:18:09 -0600167 }
Simon Glass201417d2022-04-24 23:31:07 -0600168
169 /*
170 * Currently we don't get the number of partitions, so just
171 * assume a large number
172 */
173 iter->max_part = MAX_PART_PER_BOOTDEV;
174
Simon Glassf0e358f2023-01-17 10:47:42 -0700175 /* If this is the whole disk, check if we have bootable partitions */
176 if (!iter->part) {
177 iter->first_bootable = part_get_bootable(desc);
178 log_debug("checking bootable=%d\n", iter->first_bootable);
Simon Glass831405f2023-08-24 13:55:43 -0600179 } else if (allow_any_part) {
180 /*
181 * allow any partition to be scanned, by skipping any checks
182 * for filesystems or partition contents on this disk
183 */
Simon Glassf0e358f2023-01-17 10:47:42 -0700184
185 /* if there are bootable partitions, scan only those */
Simon Glass831405f2023-08-24 13:55:43 -0600186 } else if (iter->first_bootable >= 0 &&
187 (iter->first_bootable ? !info.bootable : iter->part != 1)) {
Simon Glassf0e358f2023-01-17 10:47:42 -0700188 return log_msg_ret("boot", -EINVAL);
189 } else {
Simon Glass201417d2022-04-24 23:31:07 -0600190 ret = fs_set_blk_dev_with_part(desc, bflow->part);
191 bflow->state = BOOTFLOWST_PART;
Simon Glass1aabe4e2023-04-24 13:49:49 +1200192 if (ret)
193 return log_msg_ret("fs", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600194
Simon Glass201417d2022-04-24 23:31:07 -0600195 log_debug("%s: Found partition %x type %x fstype %d\n",
Simon Glassb2b7e6c2023-08-24 13:55:33 -0600196 blk->name, bflow->part,
197 IS_ENABLED(CONFIG_DOS_PARTITION) ?
198 disk_partition_sys_ind(&info) : 0,
Simon Glass201417d2022-04-24 23:31:07 -0600199 ret ? -1 : fs_get_type());
Simon Glass1aabe4e2023-04-24 13:49:49 +1200200 bflow->blk = blk;
Simon Glass201417d2022-04-24 23:31:07 -0600201 bflow->state = BOOTFLOWST_FS;
202 }
203
Simon Glass831405f2023-08-24 13:55:43 -0600204 log_debug("method %s\n", bflow->method->name);
Simon Glassa8f5be12022-04-24 23:31:09 -0600205 ret = bootmeth_read_bootflow(bflow->method, bflow);
206 if (ret)
207 return log_msg_ret("method", ret);
208
Simon Glass201417d2022-04-24 23:31:07 -0600209 return 0;
210}
211
212void bootdev_list(bool probe)
213{
214 struct udevice *dev;
215 int ret;
216 int i;
217
218 printf("Seq Probed Status Uclass Name\n");
219 printf("--- ------ ------ -------- ------------------\n");
220 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200221 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass201417d2022-04-24 23:31:07 -0600222 else
223 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
224 for (i = 0; dev; i++) {
225 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
226 device_active(dev) ? '+' : ' ',
Heinrich Schuchardtca9d9262023-07-30 16:29:25 +0200227 ret ? simple_itoa(-ret) : "OK",
Simon Glass201417d2022-04-24 23:31:07 -0600228 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
229 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200230 ret = uclass_next_device_check(&dev);
Simon Glass201417d2022-04-24 23:31:07 -0600231 else
232 ret = uclass_find_next_device(&dev);
233 }
234 printf("--- ------ ------ -------- ------------------\n");
235 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
236}
237
238int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
239{
240 struct udevice *bdev;
241 int ret;
242
243 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
244 &bdev);
245 if (ret) {
246 if (ret != -ENODEV) {
247 log_debug("Cannot access bootdev device\n");
248 return ret;
249 }
250
251 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
252 if (ret) {
253 log_debug("Cannot create bootdev device\n");
254 return ret;
255 }
256 }
257
258 return 0;
259}
260
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700261static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
262{
263 int len, slen;
264
265 len = strlen(dev->name);
266 slen = strlen(suffix);
267 if (len > slen && !strcmp(suffix, dev->name + len - slen))
268 return len - slen;
269
270 return len;
271}
272
Simon Glassd7d78572023-07-30 11:15:14 -0600273int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
Simon Glass201417d2022-04-24 23:31:07 -0600274{
275 struct udevice *parent, *dev;
276 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700277 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600278
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700279 len = bootdev_get_suffix_start(blk, ".blk");
280 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
281 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600282
283 parent = dev_get_parent(blk);
284 ret = device_find_child_by_name(parent, dev_name, &dev);
285 if (ret) {
286 char *str;
287
288 if (ret != -ENODEV) {
289 log_debug("Cannot access bootdev device\n");
290 return ret;
291 }
292 str = strdup(dev_name);
293 if (!str)
294 return -ENOMEM;
295
296 ret = device_bind_driver(parent, drv_name, str, &dev);
297 if (ret) {
298 log_debug("Cannot create bootdev device\n");
299 return ret;
300 }
301 device_set_name_alloced(dev);
302 }
303
304 return 0;
305}
306
307int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
308{
309 struct udevice *parent = dev_get_parent(dev);
310 struct udevice *blk;
311 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600312
313 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
314 return -EINVAL;
315
Simon Glassd7d78572023-07-30 11:15:14 -0600316 /*
317 * This should always work if bootdev_setup_for_sibling_blk() was used
318 */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700319 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600320 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700321 if (ret) {
322 char dev_name[50];
323
324 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
325 dev->name);
326 ret = device_find_child_by_name(parent, dev_name, &blk);
327 if (ret)
328 return log_msg_ret("find", ret);
329 }
Simon Glass965020c2023-01-28 15:00:19 -0700330 ret = device_probe(blk);
331 if (ret)
332 return log_msg_ret("act", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600333 *blkp = blk;
334
335 return 0;
336}
337
338static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
339{
340 struct udevice *parent = dev_get_parent(blk);
341 struct udevice *bootdev;
342 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700343 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600344
345 if (device_get_uclass_id(blk) != UCLASS_BLK)
346 return -EINVAL;
347
Simon Glassd7d78572023-07-30 11:15:14 -0600348 /* This should always work if bootdev_setup_for_sibling_blk() was used */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700349 len = bootdev_get_suffix_start(blk, ".blk");
350 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
351 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600352 ret = device_find_child_by_name(parent, dev_name, &bootdev);
353 if (ret)
354 return log_msg_ret("find", ret);
355 *bootdevp = bootdev;
356
357 return 0;
358}
359
360int bootdev_unbind_dev(struct udevice *parent)
361{
362 struct udevice *dev;
363 int ret;
364
365 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
366 if (!ret) {
367 ret = device_remove(dev, DM_REMOVE_NORMAL);
368 if (ret)
369 return log_msg_ret("rem", ret);
370 ret = device_unbind(dev);
371 if (ret)
372 return log_msg_ret("unb", ret);
373 }
374
375 return 0;
376}
377
378/**
Simon Glass74ebfb62023-01-17 10:48:00 -0700379 * label_to_uclass() - Convert a label to a uclass and sequence number
380 *
381 * @label: Label to look up (e.g. "mmc1" or "mmc0")
382 * @seqp: Returns the sequence number, or -1 if none
Simon Glassd9f48572023-01-17 10:48:05 -0700383 * @method_flagsp: If non-NULL, returns any flags implied by the label
384 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass1736b4a2023-04-24 13:49:47 +1200385 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
386 * known, other -ve error code on other error
Simon Glass74ebfb62023-01-17 10:48:00 -0700387 */
Simon Glassd9f48572023-01-17 10:48:05 -0700388static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass74ebfb62023-01-17 10:48:00 -0700389{
Simon Glassd9f48572023-01-17 10:48:05 -0700390 int seq, len, method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700391 enum uclass_id id;
392 const char *end;
Simon Glass74ebfb62023-01-17 10:48:00 -0700393
Simon Glassd9f48572023-01-17 10:48:05 -0700394 method_flags = 0;
Simon Glass74ebfb62023-01-17 10:48:00 -0700395 seq = trailing_strtoln_end(label, NULL, &end);
396 len = end - label;
397 if (!len)
398 return -EINVAL;
399 id = uclass_get_by_namelen(label, len);
400 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
401 uclass_get_name(id));
402 if (id == UCLASS_INVALID) {
Simon Glass0c1f4a92023-01-17 10:48:03 -0700403 /* try some special cases */
404 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
405 !strncmp("spi", label, len)) {
406 id = UCLASS_SPI_FLASH;
Simon Glassd9f48572023-01-17 10:48:05 -0700407 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
408 !strncmp("pxe", label, len)) {
409 id = UCLASS_ETH;
410 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
411 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
412 !strncmp("dhcp", label, len)) {
413 id = UCLASS_ETH;
414 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700415 } else {
Simon Glass1736b4a2023-04-24 13:49:47 +1200416 return -EPFNOSUPPORT;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700417 }
Simon Glass74ebfb62023-01-17 10:48:00 -0700418 }
419 if (id == UCLASS_USB)
420 id = UCLASS_MASS_STORAGE;
421 *seqp = seq;
Simon Glassd9f48572023-01-17 10:48:05 -0700422 if (method_flagsp)
423 *method_flagsp = method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700424
425 return id;
426}
427
Simon Glassd9f48572023-01-17 10:48:05 -0700428int bootdev_find_by_label(const char *label, struct udevice **devp,
429 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600430{
Simon Glassd9f48572023-01-17 10:48:05 -0700431 int seq, ret, method_flags = 0;
Simon Glass201417d2022-04-24 23:31:07 -0600432 struct udevice *media;
433 struct uclass *uc;
434 enum uclass_id id;
Simon Glass201417d2022-04-24 23:31:07 -0600435
Simon Glassd9f48572023-01-17 10:48:05 -0700436 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass74ebfb62023-01-17 10:48:00 -0700437 if (ret < 0)
438 return log_msg_ret("uc", ret);
439 id = ret;
Simon Glass201417d2022-04-24 23:31:07 -0600440
441 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
442 uclass_id_foreach_dev(id, media, uc) {
443 struct udevice *bdev, *blk;
444 int ret;
445
446 /* if there is no seq, match anything */
447 if (seq != -1 && dev_seq(media) != seq) {
448 log_debug("- skip, media seq=%d\n", dev_seq(media));
449 continue;
450 }
451
452 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
453 &bdev);
454 if (ret) {
455 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
456 id);
457 ret = blk_find_device(id, seq, &blk);
458 if (!ret) {
459 log_debug("- get from blk %s\n", blk->name);
460 ret = bootdev_get_from_blk(blk, &bdev);
461 }
462 }
463 if (!ret) {
464 log_debug("- found %s\n", bdev->name);
465 *devp = bdev;
Simon Glass66e3dce2023-01-17 10:48:09 -0700466
467 /*
468 * if no sequence number was provided, we must scan all
469 * bootdevs for this media uclass
470 */
Simon Glasse824d0d2023-09-23 14:50:15 -0600471 if (seq == -1)
Simon Glass66e3dce2023-01-17 10:48:09 -0700472 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glassd9f48572023-01-17 10:48:05 -0700473 if (method_flagsp)
474 *method_flagsp = method_flags;
Simon Glasse824d0d2023-09-23 14:50:15 -0600475 log_debug("method flags %x\n", method_flags);
Simon Glass201417d2022-04-24 23:31:07 -0600476 return 0;
477 }
478 log_debug("- no device in %s\n", media->name);
479 }
Simon Glass201417d2022-04-24 23:31:07 -0600480
481 return -ENOENT;
482}
483
Simon Glassd9f48572023-01-17 10:48:05 -0700484int bootdev_find_by_any(const char *name, struct udevice **devp,
485 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600486{
487 struct udevice *dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700488 int method_flags = 0;
Simon Glass66e3dce2023-01-17 10:48:09 -0700489 int ret = -ENODEV, seq;
Simon Glass201417d2022-04-24 23:31:07 -0600490 char *endp;
491
492 seq = simple_strtol(name, &endp, 16);
493
494 /* Select by name, label or number */
495 if (*endp) {
496 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
497 if (ret == -ENODEV) {
Simon Glassd9f48572023-01-17 10:48:05 -0700498 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass201417d2022-04-24 23:31:07 -0600499 if (ret) {
500 printf("Cannot find bootdev '%s' (err=%d)\n",
501 name, ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700502 return log_msg_ret("lab", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600503 }
504 ret = device_probe(dev);
505 }
506 if (ret) {
507 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
508 ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700509 return log_msg_ret("pro", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600510 }
Simon Glass66e3dce2023-01-17 10:48:09 -0700511 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass201417d2022-04-24 23:31:07 -0600512 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glass66e3dce2023-01-17 10:48:09 -0700513 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass201417d2022-04-24 23:31:07 -0600514 }
515 if (ret) {
516 printf("Cannot find '%s' (err=%d)\n", name, ret);
517 return ret;
518 }
519
520 *devp = dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700521 if (method_flagsp)
522 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600523
524 return 0;
525}
526
Simon Glass66e3dce2023-01-17 10:48:09 -0700527int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
528 int *method_flagsp)
529{
530 int ret;
531
532 ret = bootdev_hunt(label, false);
533 if (ret)
534 return log_msg_ret("scn", ret);
535 ret = bootdev_find_by_label(label, devp, method_flagsp);
536 if (ret)
537 return log_msg_ret("fnd", ret);
538
539 return 0;
540}
541
Simon Glassb85fc8d2023-01-17 10:47:26 -0700542static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
543 struct bootflow *bflow)
544{
545 struct udevice *blk;
546 int ret;
547
548 ret = bootdev_get_sibling_blk(dev, &blk);
Simon Glass3500ae12023-07-30 11:15:16 -0600549 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
550 ret ? "(none)" : blk->name);
Simon Glassb85fc8d2023-01-17 10:47:26 -0700551 /*
552 * If there is no media, indicate that no more partitions should be
553 * checked
554 */
555 if (ret == -EOPNOTSUPP)
556 ret = -ESHUTDOWN;
557 if (ret)
558 return log_msg_ret("blk", ret);
559 assert(blk);
560 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
561 if (ret)
562 return log_msg_ret("find", ret);
563
564 return 0;
565}
566
Simon Glass201417d2022-04-24 23:31:07 -0600567int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
568 struct bootflow *bflow)
569{
570 const struct bootdev_ops *ops = bootdev_get_ops(dev);
571
Simon Glass831405f2023-08-24 13:55:43 -0600572 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
573 ops->get_bootflow);
Simon Glassb190deb2022-10-20 18:22:51 -0600574 bootflow_init(bflow, dev, iter->method);
Simon Glassb85fc8d2023-01-17 10:47:26 -0700575 if (!ops->get_bootflow)
576 return default_get_bootflow(dev, iter, bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600577
578 return ops->get_bootflow(dev, iter, bflow);
579}
580
581void bootdev_clear_bootflows(struct udevice *dev)
582{
583 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
584
585 while (!list_empty(&ucp->bootflow_head)) {
586 struct bootflow *bflow;
587
588 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
589 bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600590 bootflow_remove(bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600591 }
592}
593
Simon Glasse4b69482023-01-17 10:48:10 -0700594int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
595 int *method_flagsp)
596{
597 struct udevice *dev;
598
599 log_debug("next\n");
600 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass1736b4a2023-04-24 13:49:47 +1200601 const char *label = iter->labels[iter->cur_label];
602 int ret;
603
604 log_debug("Scanning: %s\n", label);
605 ret = bootdev_hunt_and_find_by_label(label, &dev,
606 method_flagsp);
607 if (iter->flags & BOOTFLOWIF_SHOW) {
608 if (ret == -EPFNOSUPPORT) {
609 log_warning("Unknown uclass '%s' in label\n",
610 label);
611 } else if (ret == -ENOENT) {
612 /*
613 * looking for, e.g. 'scsi0' should find
614 * something if SCSI is present
615 */
616 if (!trailing_strtol(label)) {
617 log_warning("No bootdevs for '%s'\n",
618 label);
619 }
620 }
621 }
622
Simon Glasse4b69482023-01-17 10:48:10 -0700623 }
624
625 if (!dev)
626 return log_msg_ret("fin", -ENODEV);
627 *devp = dev;
628
629 return 0;
630}
631
Simon Glass43e89a32023-01-17 10:48:11 -0700632int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
633{
Caleb Connolly9d92c412024-01-04 16:03:35 +0000634 struct udevice *dev = *devp, *last_dev = NULL;
Simon Glass43e89a32023-01-17 10:48:11 -0700635 bool found;
636 int ret;
637
638 /* find the next device with this priority */
639 *devp = NULL;
640 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
641 dev ? dev->name : "none");
642 do {
643 /*
644 * Don't probe devices here since they may not be of the
645 * required priority
646 */
647 if (!dev)
648 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
649 else
650 uclass_find_next_device(&dev);
651 found = false;
652
653 /* scan for the next device with the correct priority */
654 while (dev) {
655 struct bootdev_uc_plat *plat;
656
657 plat = dev_get_uclass_plat(dev);
658 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
659 iter->cur_prio);
660 if (plat->prio == iter->cur_prio)
661 break;
662 uclass_find_next_device(&dev);
663 }
664
665 /* none found for this priority, so move to the next */
666 if (!dev) {
667 log_debug("None found at prio %d, moving to %d\n",
668 iter->cur_prio, iter->cur_prio + 1);
669 if (++iter->cur_prio == BOOTDEVP_COUNT)
670 return log_msg_ret("fin", -ENODEV);
671
Simon Glass4f806f32023-02-22 12:17:03 -0700672 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass43e89a32023-01-17 10:48:11 -0700673 /* hunt to find new bootdevs */
674 ret = bootdev_hunt_prio(iter->cur_prio,
675 iter->flags &
Simon Glass4f806f32023-02-22 12:17:03 -0700676 BOOTFLOWIF_SHOW);
Simon Glass3500ae12023-07-30 11:15:16 -0600677 log_debug("- bootdev_hunt_prio() ret %d\n",
678 ret);
Simon Glass43e89a32023-01-17 10:48:11 -0700679 if (ret)
680 return log_msg_ret("hun", ret);
681 }
682 } else {
683 ret = device_probe(dev);
Caleb Connolly9d92c412024-01-04 16:03:35 +0000684 if (!ret)
685 last_dev = dev;
Simon Glass43e89a32023-01-17 10:48:11 -0700686 if (ret) {
Caleb Connolly9d92c412024-01-04 16:03:35 +0000687 log_warning("Device '%s' failed to probe\n",
Simon Glass43e89a32023-01-17 10:48:11 -0700688 dev->name);
Caleb Connolly9d92c412024-01-04 16:03:35 +0000689 if (last_dev == dev) {
690 /*
691 * We have already tried this device
692 * and it failed to probe. Give up.
693 */
694 return log_msg_ret("probe", ret);
695 }
696 last_dev = dev;
Simon Glass43e89a32023-01-17 10:48:11 -0700697 dev = NULL;
698 }
699 }
700 } while (!dev);
701
702 *devp = dev;
703
704 return 0;
705}
706
Simon Glass91943ff2023-01-17 10:48:15 -0700707int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
708 struct udevice **devp, int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600709{
Simon Glass91943ff2023-01-17 10:48:15 -0700710 struct udevice *bootstd, *dev = NULL;
Simon Glass4f806f32023-02-22 12:17:03 -0700711 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass47aedc22023-01-17 10:48:14 -0700712 int method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600713 int ret;
714
715 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
716 if (ret) {
717 log_err("Missing bootstd device\n");
718 return log_msg_ret("std", ret);
719 }
720
Simon Glasseacc2612023-01-17 10:48:08 -0700721 /* hunt for any pre-scan devices */
Simon Glass4f806f32023-02-22 12:17:03 -0700722 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glasseacc2612023-01-17 10:48:08 -0700723 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
Simon Glass3500ae12023-07-30 11:15:16 -0600724 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
Simon Glasseacc2612023-01-17 10:48:08 -0700725 if (ret)
726 return log_msg_ret("pre", ret);
727 }
728
Simon Glass201417d2022-04-24 23:31:07 -0600729 /* Handle scanning a single device */
Simon Glass91943ff2023-01-17 10:48:15 -0700730 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass4f806f32023-02-22 12:17:03 -0700731 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass91943ff2023-01-17 10:48:15 -0700732 ret = bootdev_hunt(label, show);
733 if (ret)
734 return log_msg_ret("hun", ret);
735 }
736 ret = bootdev_find_by_any(label, &dev, &method_flags);
737 if (ret)
738 return log_msg_ret("lab", ret);
739
740 log_debug("method_flags: %x\n", method_flags);
741 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass4f806f32023-02-22 12:17:03 -0700742 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glass91943ff2023-01-17 10:48:15 -0700743 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass4f806f32023-02-22 12:17:03 -0700744 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glass91943ff2023-01-17 10:48:15 -0700745 else
Simon Glass4f806f32023-02-22 12:17:03 -0700746 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glass91943ff2023-01-17 10:48:15 -0700747 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass47aedc22023-01-17 10:48:14 -0700748 } else {
749 bool ok;
750
751 /* This either returns a non-empty list or NULL */
752 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
753 if (!ok)
754 return log_msg_ret("ord", -ENOMEM);
755 log_debug("setup labels %p\n", iter->labels);
756 if (iter->labels) {
757 iter->cur_label = -1;
758 ret = bootdev_next_label(iter, &dev, &method_flags);
759 } else {
760 ret = bootdev_next_prio(iter, &dev);
761 method_flags = 0;
762 }
763 if (!dev)
764 return log_msg_ret("fin", -ENOENT);
765 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass201417d2022-04-24 23:31:07 -0600766 }
767
Simon Glass201417d2022-04-24 23:31:07 -0600768 ret = device_probe(dev);
769 if (ret)
770 return log_msg_ret("probe", ret);
Simon Glass47aedc22023-01-17 10:48:14 -0700771 if (method_flagsp)
772 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600773 *devp = dev;
774
775 return 0;
776}
777
Simon Glassc7b63d52023-01-17 10:47:34 -0700778static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
779{
780 const char *name = uclass_get_name(info->uclass);
781 struct bootstd_priv *std;
782 int ret;
783
784 ret = bootstd_get_priv(&std);
785 if (ret)
786 return log_msg_ret("std", ret);
787
788 if (!(std->hunters_used & BIT(seq))) {
789 if (show)
790 printf("Hunting with: %s\n",
791 uclass_get_name(info->uclass));
792 log_debug("Hunting with: %s\n", name);
793 if (info->hunt) {
794 ret = info->hunt(info, show);
Simon Glass3500ae12023-07-30 11:15:16 -0600795 log_debug(" - hunt result %d\n", ret);
Tony Dinhee2ce292023-11-02 11:51:15 -0700796 if (ret && ret != -ENOENT)
Simon Glassc7b63d52023-01-17 10:47:34 -0700797 return ret;
798 }
799 std->hunters_used |= BIT(seq);
800 }
801
802 return 0;
803}
804
805int bootdev_hunt(const char *spec, bool show)
806{
807 struct bootdev_hunter *start;
808 const char *end;
809 int n_ent, i;
810 int result;
811 size_t len;
812
813 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
814 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
815 result = 0;
816
817 len = SIZE_MAX;
818 if (spec) {
819 trailing_strtoln_end(spec, NULL, &end);
820 len = end - spec;
821 }
822
823 for (i = 0; i < n_ent; i++) {
824 struct bootdev_hunter *info = start + i;
825 const char *name = uclass_get_name(info->uclass);
826 int ret;
827
828 log_debug("looking at %.*s for %s\n",
829 (int)max(strlen(name), len), spec, name);
Simon Glasse4b69482023-01-17 10:48:10 -0700830 if (spec && strncmp(spec, name, max(strlen(name), len))) {
831 if (info->uclass != UCLASS_ETH ||
832 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
833 continue;
834 }
Simon Glassc7b63d52023-01-17 10:47:34 -0700835 ret = bootdev_hunt_drv(info, i, show);
836 if (ret)
837 result = ret;
838 }
839
840 return result;
841}
842
Simon Glass1b1d36e2023-09-20 07:29:49 -0600843int bootdev_unhunt(enum uclass_id id)
844{
845 struct bootdev_hunter *start;
846 int n_ent, i;
847
848 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
849 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
850 for (i = 0; i < n_ent; i++) {
851 struct bootdev_hunter *info = start + i;
852
853 if (info->uclass == id) {
854 struct bootstd_priv *std;
855 int ret;
856
857 ret = bootstd_get_priv(&std);
858 if (ret)
859 return log_msg_ret("std", ret);
860 if (!(std->hunters_used & BIT(i)))
861 return -EALREADY;
862 std->hunters_used &= ~BIT(i);
863 return 0;
864 }
865 }
866
867 return -ENOENT;
868}
869
Simon Glass79a7d4a2023-01-17 10:48:07 -0700870int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
871{
872 struct bootdev_hunter *start;
873 int n_ent, i;
874 int result;
875
876 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
877 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
878 result = 0;
879
880 log_debug("Hunting for priority %d\n", prio);
881 for (i = 0; i < n_ent; i++) {
882 struct bootdev_hunter *info = start + i;
883 int ret;
884
885 if (prio != info->prio)
886 continue;
887 ret = bootdev_hunt_drv(info, i, show);
Simon Glass3500ae12023-07-30 11:15:16 -0600888 log_debug("bootdev_hunt_drv() return %d\n", ret);
Simon Glass79a7d4a2023-01-17 10:48:07 -0700889 if (ret && ret != -ENOENT)
890 result = ret;
891 }
Simon Glass3500ae12023-07-30 11:15:16 -0600892 log_debug("exit %d\n", result);
Simon Glass79a7d4a2023-01-17 10:48:07 -0700893
894 return result;
895}
896
Simon Glassbd90b092023-01-17 10:47:33 -0700897void bootdev_list_hunters(struct bootstd_priv *std)
898{
899 struct bootdev_hunter *orig, *start;
900 int n_ent, i;
901
902 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
903 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
904
905 /*
906 * workaround for strange bug in clang-12 which sees all the below data
907 * as zeroes. Any access of start seems to fix it, such as
908 *
909 * printf("%p", start);
910 *
911 * Use memcpy() to force the correct behaviour.
912 */
913 memcpy(&start, &orig, sizeof(orig));
914 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
915 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
916 for (i = 0; i < n_ent; i++) {
917 struct bootdev_hunter *info = start + i;
918
919 printf("%4d %4s %-15s %s\n", info->prio,
920 std->hunters_used & BIT(i) ? "*" : "",
921 uclass_get_name(info->uclass),
922 info->drv ? info->drv->name : "(none)");
923 }
924
925 printf("(total hunters: %d)\n", n_ent);
926}
927
Simon Glass201417d2022-04-24 23:31:07 -0600928static int bootdev_post_bind(struct udevice *dev)
929{
930 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
931
932 INIT_LIST_HEAD(&ucp->bootflow_head);
933
934 return 0;
935}
936
937static int bootdev_pre_unbind(struct udevice *dev)
938{
939 bootdev_clear_bootflows(dev);
940
941 return 0;
942}
943
944UCLASS_DRIVER(bootdev) = {
945 .id = UCLASS_BOOTDEV,
946 .name = "bootdev",
947 .flags = DM_UC_FLAG_SEQ_ALIAS,
948 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
949 .post_bind = bootdev_post_bind,
950 .pre_unbind = bootdev_pre_unbind,
951};