blob: 2e61c853142b7d4e1993a91722722b030b67609e [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
Simon Glass017656e2022-04-24 23:31:07 -06009#include <dm.h>
10#include <bootdev.h>
11#include <bootflow.h>
Simon Glass03fcbf92022-04-24 23:31:09 -060012#include <bootmeth.h>
Simon Glass017656e2022-04-24 23:31:07 -060013#include <bootstd.h>
Simon Glass017656e2022-04-24 23:31:07 -060014#include <fs.h>
15#include <log.h>
16#include <malloc.h>
17#include <part.h>
18#include <sort.h>
Simon Glassa73f23c2024-12-19 11:28:52 -070019#include <spl.h>
Simon Glass017656e2022-04-24 23:31:07 -060020#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
Simon Glass017656e2022-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 Glassdee4d642022-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 Glass017656e2022-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 Glass0fca7e82023-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 Glass017656e2022-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 Glass03fcbf92022-04-24 23:31:09 -0600137 ret = bootmeth_check(bflow->method, iter);
138 if (ret)
139 return log_msg_ret("check", ret);
140
Simon Glass017656e2022-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 Glass0fca7e82023-08-24 13:55:43 -0600146 log_debug("part_get_info() returned %d\n", ret);
Simon Glass017656e2022-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 Glass0fca7e82023-08-24 13:55:43 -0600159 if (ret && !allow_any_part) {
Simon Glassa8e7d512023-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 Glass017656e2022-04-24 23:31:07 -0600166 return log_msg_ret("part", ret);
Simon Glassa8e7d512023-04-28 13:18:09 -0600167 }
Simon Glass017656e2022-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
Nam Caocb0d3fb2024-02-21 13:41:44 +0100175 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) {
176 /* a particular partition was specified, scan it without checking */
177 } else if (!iter->part) {
178 /* This is the whole disk, check if we have bootable partitions */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700179 iter->first_bootable = part_get_bootable(desc);
180 log_debug("checking bootable=%d\n", iter->first_bootable);
Simon Glass0fca7e82023-08-24 13:55:43 -0600181 } 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 Glass64cbc5c2023-01-17 10:47:42 -0700186
187 /* if there are bootable partitions, scan only those */
Simon Glass0fca7e82023-08-24 13:55:43 -0600188 } else if (iter->first_bootable >= 0 &&
189 (iter->first_bootable ? !info.bootable : iter->part != 1)) {
Simon Glass64cbc5c2023-01-17 10:47:42 -0700190 return log_msg_ret("boot", -EINVAL);
191 } else {
Simon Glass017656e2022-04-24 23:31:07 -0600192 ret = fs_set_blk_dev_with_part(desc, bflow->part);
193 bflow->state = BOOTFLOWST_PART;
Simon Glassa40f7822023-04-24 13:49:49 +1200194 if (ret)
195 return log_msg_ret("fs", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600196
Simon Glass017656e2022-04-24 23:31:07 -0600197 log_debug("%s: Found partition %x type %x fstype %d\n",
Simon Glassed0ec662023-08-24 13:55:33 -0600198 blk->name, bflow->part,
199 IS_ENABLED(CONFIG_DOS_PARTITION) ?
200 disk_partition_sys_ind(&info) : 0,
Simon Glass017656e2022-04-24 23:31:07 -0600201 ret ? -1 : fs_get_type());
Simon Glassa40f7822023-04-24 13:49:49 +1200202 bflow->blk = blk;
Simon Glass017656e2022-04-24 23:31:07 -0600203 bflow->state = BOOTFLOWST_FS;
204 }
205
Simon Glass0fca7e82023-08-24 13:55:43 -0600206 log_debug("method %s\n", bflow->method->name);
Simon Glass03fcbf92022-04-24 23:31:09 -0600207 ret = bootmeth_read_bootflow(bflow->method, bflow);
208 if (ret)
209 return log_msg_ret("method", ret);
210
Simon Glass017656e2022-04-24 23:31:07 -0600211 return 0;
212}
213
214void bootdev_list(bool probe)
215{
216 struct udevice *dev;
217 int ret;
218 int i;
219
220 printf("Seq Probed Status Uclass Name\n");
221 printf("--- ------ ------ -------- ------------------\n");
222 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200223 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass017656e2022-04-24 23:31:07 -0600224 else
225 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
226 for (i = 0; dev; i++) {
227 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
228 device_active(dev) ? '+' : ' ',
Heinrich Schuchardt91592d62023-07-30 16:29:25 +0200229 ret ? simple_itoa(-ret) : "OK",
Simon Glass017656e2022-04-24 23:31:07 -0600230 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
231 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200232 ret = uclass_next_device_check(&dev);
Simon Glass017656e2022-04-24 23:31:07 -0600233 else
234 ret = uclass_find_next_device(&dev);
235 }
236 printf("--- ------ ------ -------- ------------------\n");
237 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
238}
239
240int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
241{
242 struct udevice *bdev;
243 int ret;
244
245 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
246 &bdev);
247 if (ret) {
248 if (ret != -ENODEV) {
249 log_debug("Cannot access bootdev device\n");
250 return ret;
251 }
252
253 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
254 if (ret) {
255 log_debug("Cannot create bootdev device\n");
256 return ret;
257 }
258 }
259
260 return 0;
261}
262
Simon Glassb1dc36a2023-01-17 10:47:25 -0700263static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
264{
265 int len, slen;
266
267 len = strlen(dev->name);
268 slen = strlen(suffix);
269 if (len > slen && !strcmp(suffix, dev->name + len - slen))
270 return len - slen;
271
272 return len;
273}
274
Simon Glassb1d581d2023-07-30 11:15:14 -0600275int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
Simon Glass017656e2022-04-24 23:31:07 -0600276{
277 struct udevice *parent, *dev;
278 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700279 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600280
Simon Glassb1dc36a2023-01-17 10:47:25 -0700281 len = bootdev_get_suffix_start(blk, ".blk");
Simon Glassa73f23c2024-12-19 11:28:52 -0700282 if (xpl_phase() < PHASE_BOARD_R) {
283 strlcpy(dev_name, blk->name, sizeof(dev_name) - 5);
284 strcat(dev_name, ".sib");
285 } else {
286 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
287 "bootdev");
288 }
Simon Glass017656e2022-04-24 23:31:07 -0600289
290 parent = dev_get_parent(blk);
291 ret = device_find_child_by_name(parent, dev_name, &dev);
292 if (ret) {
293 char *str;
294
295 if (ret != -ENODEV) {
296 log_debug("Cannot access bootdev device\n");
297 return ret;
298 }
299 str = strdup(dev_name);
300 if (!str)
301 return -ENOMEM;
302
303 ret = device_bind_driver(parent, drv_name, str, &dev);
304 if (ret) {
305 log_debug("Cannot create bootdev device\n");
306 return ret;
307 }
308 device_set_name_alloced(dev);
309 }
310
311 return 0;
312}
313
314int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
315{
316 struct udevice *parent = dev_get_parent(dev);
317 struct udevice *blk;
318 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600319
320 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
321 return -EINVAL;
322
Simon Glassb1d581d2023-07-30 11:15:14 -0600323 /*
324 * This should always work if bootdev_setup_for_sibling_blk() was used
325 */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700326 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600327 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glassb1dc36a2023-01-17 10:47:25 -0700328 if (ret) {
329 char dev_name[50];
330
331 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
332 dev->name);
333 ret = device_find_child_by_name(parent, dev_name, &blk);
334 if (ret)
335 return log_msg_ret("find", ret);
336 }
Simon Glassb73a8292023-01-28 15:00:19 -0700337 ret = device_probe(blk);
338 if (ret)
339 return log_msg_ret("act", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600340 *blkp = blk;
341
342 return 0;
343}
344
345static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
346{
347 struct udevice *parent = dev_get_parent(blk);
348 struct udevice *bootdev;
349 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700350 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600351
352 if (device_get_uclass_id(blk) != UCLASS_BLK)
353 return -EINVAL;
354
Simon Glassb1d581d2023-07-30 11:15:14 -0600355 /* This should always work if bootdev_setup_for_sibling_blk() was used */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700356 len = bootdev_get_suffix_start(blk, ".blk");
357 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
358 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600359 ret = device_find_child_by_name(parent, dev_name, &bootdev);
360 if (ret)
361 return log_msg_ret("find", ret);
362 *bootdevp = bootdev;
363
364 return 0;
365}
366
367int bootdev_unbind_dev(struct udevice *parent)
368{
369 struct udevice *dev;
370 int ret;
371
372 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
373 if (!ret) {
374 ret = device_remove(dev, DM_REMOVE_NORMAL);
375 if (ret)
376 return log_msg_ret("rem", ret);
377 ret = device_unbind(dev);
378 if (ret)
379 return log_msg_ret("unb", ret);
380 }
381
382 return 0;
383}
384
385/**
Simon Glass73708252023-01-17 10:48:00 -0700386 * label_to_uclass() - Convert a label to a uclass and sequence number
387 *
388 * @label: Label to look up (e.g. "mmc1" or "mmc0")
389 * @seqp: Returns the sequence number, or -1 if none
Simon Glasse22fe922023-01-17 10:48:05 -0700390 * @method_flagsp: If non-NULL, returns any flags implied by the label
391 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass4108c6b2023-04-24 13:49:47 +1200392 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
393 * known, other -ve error code on other error
Simon Glass73708252023-01-17 10:48:00 -0700394 */
Simon Glasse22fe922023-01-17 10:48:05 -0700395static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass73708252023-01-17 10:48:00 -0700396{
Simon Glasse22fe922023-01-17 10:48:05 -0700397 int seq, len, method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700398 enum uclass_id id;
399 const char *end;
Simon Glass73708252023-01-17 10:48:00 -0700400
Simon Glasse22fe922023-01-17 10:48:05 -0700401 method_flags = 0;
Simon Glass73708252023-01-17 10:48:00 -0700402 seq = trailing_strtoln_end(label, NULL, &end);
403 len = end - label;
404 if (!len)
405 return -EINVAL;
406 id = uclass_get_by_namelen(label, len);
407 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
408 uclass_get_name(id));
409 if (id == UCLASS_INVALID) {
Simon Glass215be682023-01-17 10:48:03 -0700410 /* try some special cases */
411 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
412 !strncmp("spi", label, len)) {
413 id = UCLASS_SPI_FLASH;
Simon Glasse22fe922023-01-17 10:48:05 -0700414 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
415 !strncmp("pxe", label, len)) {
416 id = UCLASS_ETH;
417 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
418 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
419 !strncmp("dhcp", label, len)) {
420 id = UCLASS_ETH;
421 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass215be682023-01-17 10:48:03 -0700422 } else {
Simon Glass4108c6b2023-04-24 13:49:47 +1200423 return -EPFNOSUPPORT;
Simon Glass215be682023-01-17 10:48:03 -0700424 }
Simon Glass73708252023-01-17 10:48:00 -0700425 }
426 if (id == UCLASS_USB)
427 id = UCLASS_MASS_STORAGE;
428 *seqp = seq;
Simon Glasse22fe922023-01-17 10:48:05 -0700429 if (method_flagsp)
430 *method_flagsp = method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700431
432 return id;
433}
434
Simon Glasse22fe922023-01-17 10:48:05 -0700435int bootdev_find_by_label(const char *label, struct udevice **devp,
436 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600437{
Simon Glasse22fe922023-01-17 10:48:05 -0700438 int seq, ret, method_flags = 0;
Simon Glass017656e2022-04-24 23:31:07 -0600439 struct udevice *media;
440 struct uclass *uc;
441 enum uclass_id id;
Simon Glass017656e2022-04-24 23:31:07 -0600442
Simon Glass1d809642024-09-01 16:27:28 -0600443 if (!CONFIG_IS_ENABLED(BLK))
444 return -ENOSYS;
445
Simon Glasse22fe922023-01-17 10:48:05 -0700446 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass73708252023-01-17 10:48:00 -0700447 if (ret < 0)
448 return log_msg_ret("uc", ret);
449 id = ret;
Simon Glass017656e2022-04-24 23:31:07 -0600450
451 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
452 uclass_id_foreach_dev(id, media, uc) {
453 struct udevice *bdev, *blk;
454 int ret;
455
456 /* if there is no seq, match anything */
457 if (seq != -1 && dev_seq(media) != seq) {
458 log_debug("- skip, media seq=%d\n", dev_seq(media));
459 continue;
460 }
461
462 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
463 &bdev);
464 if (ret) {
465 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
466 id);
467 ret = blk_find_device(id, seq, &blk);
468 if (!ret) {
469 log_debug("- get from blk %s\n", blk->name);
470 ret = bootdev_get_from_blk(blk, &bdev);
471 }
472 }
473 if (!ret) {
474 log_debug("- found %s\n", bdev->name);
475 *devp = bdev;
Simon Glassde567b12023-01-17 10:48:09 -0700476
477 /*
478 * if no sequence number was provided, we must scan all
479 * bootdevs for this media uclass
480 */
Simon Glass60bcbf02023-09-23 14:50:15 -0600481 if (seq == -1)
Simon Glassde567b12023-01-17 10:48:09 -0700482 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glasse22fe922023-01-17 10:48:05 -0700483 if (method_flagsp)
484 *method_flagsp = method_flags;
Simon Glass60bcbf02023-09-23 14:50:15 -0600485 log_debug("method flags %x\n", method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600486 return 0;
487 }
488 log_debug("- no device in %s\n", media->name);
489 }
Simon Glass017656e2022-04-24 23:31:07 -0600490
491 return -ENOENT;
492}
493
Simon Glasse22fe922023-01-17 10:48:05 -0700494int bootdev_find_by_any(const char *name, struct udevice **devp,
495 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600496{
497 struct udevice *dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700498 int method_flags = 0;
Simon Glassde567b12023-01-17 10:48:09 -0700499 int ret = -ENODEV, seq;
Simon Glass017656e2022-04-24 23:31:07 -0600500 char *endp;
501
502 seq = simple_strtol(name, &endp, 16);
503
504 /* Select by name, label or number */
505 if (*endp) {
506 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
507 if (ret == -ENODEV) {
Simon Glasse22fe922023-01-17 10:48:05 -0700508 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600509 if (ret) {
510 printf("Cannot find bootdev '%s' (err=%d)\n",
511 name, ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700512 return log_msg_ret("lab", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600513 }
514 ret = device_probe(dev);
515 }
516 if (ret) {
517 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
518 ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700519 return log_msg_ret("pro", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600520 }
Simon Glassde567b12023-01-17 10:48:09 -0700521 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass017656e2022-04-24 23:31:07 -0600522 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glassde567b12023-01-17 10:48:09 -0700523 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass017656e2022-04-24 23:31:07 -0600524 }
525 if (ret) {
526 printf("Cannot find '%s' (err=%d)\n", name, ret);
527 return ret;
528 }
529
530 *devp = dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700531 if (method_flagsp)
532 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600533
534 return 0;
535}
536
Simon Glassde567b12023-01-17 10:48:09 -0700537int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
538 int *method_flagsp)
539{
540 int ret;
541
542 ret = bootdev_hunt(label, false);
543 if (ret)
544 return log_msg_ret("scn", ret);
545 ret = bootdev_find_by_label(label, devp, method_flagsp);
546 if (ret)
547 return log_msg_ret("fnd", ret);
548
549 return 0;
550}
551
Simon Glassd6e39d12023-01-17 10:47:26 -0700552static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
553 struct bootflow *bflow)
554{
555 struct udevice *blk;
556 int ret;
557
558 ret = bootdev_get_sibling_blk(dev, &blk);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600559 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
560 ret ? "(none)" : blk->name);
Simon Glassd6e39d12023-01-17 10:47:26 -0700561 /*
562 * If there is no media, indicate that no more partitions should be
563 * checked
564 */
565 if (ret == -EOPNOTSUPP)
566 ret = -ESHUTDOWN;
567 if (ret)
568 return log_msg_ret("blk", ret);
569 assert(blk);
570 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
571 if (ret)
572 return log_msg_ret("find", ret);
573
574 return 0;
575}
576
Simon Glass017656e2022-04-24 23:31:07 -0600577int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
578 struct bootflow *bflow)
579{
580 const struct bootdev_ops *ops = bootdev_get_ops(dev);
581
Simon Glass0fca7e82023-08-24 13:55:43 -0600582 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
583 ops->get_bootflow);
Simon Glass00501fc2022-10-20 18:22:51 -0600584 bootflow_init(bflow, dev, iter->method);
Simon Glassd6e39d12023-01-17 10:47:26 -0700585 if (!ops->get_bootflow)
586 return default_get_bootflow(dev, iter, bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600587
588 return ops->get_bootflow(dev, iter, bflow);
589}
590
591void bootdev_clear_bootflows(struct udevice *dev)
592{
593 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
594
595 while (!list_empty(&ucp->bootflow_head)) {
596 struct bootflow *bflow;
597
598 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
599 bm_node);
Simon Glass03fcbf92022-04-24 23:31:09 -0600600 bootflow_remove(bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600601 }
602}
603
Simon Glassac06b26a2023-01-17 10:48:10 -0700604int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
605 int *method_flagsp)
606{
607 struct udevice *dev;
608
609 log_debug("next\n");
610 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass4108c6b2023-04-24 13:49:47 +1200611 const char *label = iter->labels[iter->cur_label];
612 int ret;
613
614 log_debug("Scanning: %s\n", label);
615 ret = bootdev_hunt_and_find_by_label(label, &dev,
616 method_flagsp);
617 if (iter->flags & BOOTFLOWIF_SHOW) {
618 if (ret == -EPFNOSUPPORT) {
619 log_warning("Unknown uclass '%s' in label\n",
620 label);
621 } else if (ret == -ENOENT) {
622 /*
623 * looking for, e.g. 'scsi0' should find
624 * something if SCSI is present
625 */
626 if (!trailing_strtol(label)) {
627 log_warning("No bootdevs for '%s'\n",
628 label);
629 }
630 }
631 }
632
Simon Glassac06b26a2023-01-17 10:48:10 -0700633 }
634
635 if (!dev)
636 return log_msg_ret("fin", -ENODEV);
637 *devp = dev;
638
639 return 0;
640}
641
Simon Glass660a9952023-01-17 10:48:11 -0700642int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
643{
Simon Glass15f128a2024-08-15 14:30:21 -0600644 struct udevice *dev = *devp;
Simon Glass660a9952023-01-17 10:48:11 -0700645 bool found;
646 int ret;
647
648 /* find the next device with this priority */
649 *devp = NULL;
650 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
651 dev ? dev->name : "none");
Simon Glass5aaa5622024-08-15 14:30:22 -0600652 found = false;
Simon Glass660a9952023-01-17 10:48:11 -0700653 do {
654 /*
655 * Don't probe devices here since they may not be of the
656 * required priority
657 */
658 if (!dev)
659 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
660 else
661 uclass_find_next_device(&dev);
662 found = false;
663
664 /* scan for the next device with the correct priority */
665 while (dev) {
666 struct bootdev_uc_plat *plat;
667
668 plat = dev_get_uclass_plat(dev);
669 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
670 iter->cur_prio);
671 if (plat->prio == iter->cur_prio)
672 break;
673 uclass_find_next_device(&dev);
674 }
675
676 /* none found for this priority, so move to the next */
677 if (!dev) {
678 log_debug("None found at prio %d, moving to %d\n",
679 iter->cur_prio, iter->cur_prio + 1);
680 if (++iter->cur_prio == BOOTDEVP_COUNT)
681 return log_msg_ret("fin", -ENODEV);
682
Simon Glass99e68182023-02-22 12:17:03 -0700683 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass660a9952023-01-17 10:48:11 -0700684 /* hunt to find new bootdevs */
685 ret = bootdev_hunt_prio(iter->cur_prio,
686 iter->flags &
Simon Glass99e68182023-02-22 12:17:03 -0700687 BOOTFLOWIF_SHOW);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600688 log_debug("- bootdev_hunt_prio() ret %d\n",
689 ret);
Simon Glass660a9952023-01-17 10:48:11 -0700690 if (ret)
691 return log_msg_ret("hun", ret);
692 }
693 } else {
694 ret = device_probe(dev);
Simon Glass5aaa5622024-08-15 14:30:22 -0600695 if (ret)
Simon Glass15f128a2024-08-15 14:30:21 -0600696 log_debug("Device '%s' failed to probe\n",
Simon Glass660a9952023-01-17 10:48:11 -0700697 dev->name);
Simon Glass5aaa5622024-08-15 14:30:22 -0600698 else
699 found = true;
Simon Glass660a9952023-01-17 10:48:11 -0700700 }
Simon Glass5aaa5622024-08-15 14:30:22 -0600701 } while (!found);
Simon Glass660a9952023-01-17 10:48:11 -0700702
703 *devp = dev;
704
705 return 0;
706}
707
Simon Glassba3d5372023-01-17 10:48:15 -0700708int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
709 struct udevice **devp, int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600710{
Simon Glassba3d5372023-01-17 10:48:15 -0700711 struct udevice *bootstd, *dev = NULL;
Simon Glass99e68182023-02-22 12:17:03 -0700712 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass484e4072023-01-17 10:48:14 -0700713 int method_flags;
Nam Caocb0d3fb2024-02-21 13:41:44 +0100714 char buf[32];
Simon Glass017656e2022-04-24 23:31:07 -0600715 int ret;
716
Nam Caocb0d3fb2024-02-21 13:41:44 +0100717 if (label) {
718 const char *end = strchr(label, ':');
719
720 if (end) {
721 size_t len = (size_t)(end - label);
722 const char *part = end + 1;
723
724 if (len + 1 > sizeof(buf)) {
725 log_err("label \"%s\" is way too long\n", label);
726 return -EINVAL;
727 }
728
729 memcpy(buf, label, len);
730 buf[len] = '\0';
731 label = buf;
732
733 unsigned long tmp;
734
735 if (strict_strtoul(part, 0, &tmp)) {
736 log_err("Invalid partition number: %s\n", part);
737 return -EINVAL;
738 }
739
740 iter->flags |= BOOTFLOWIF_SINGLE_PARTITION;
741 iter->part = tmp;
742 }
743 }
744
Simon Glass017656e2022-04-24 23:31:07 -0600745 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
746 if (ret) {
747 log_err("Missing bootstd device\n");
748 return log_msg_ret("std", ret);
749 }
750
Simon Glass7e1f6a42023-01-17 10:48:08 -0700751 /* hunt for any pre-scan devices */
Simon Glass99e68182023-02-22 12:17:03 -0700752 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700753 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600754 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700755 if (ret)
756 return log_msg_ret("pre", ret);
757 }
758
Simon Glass017656e2022-04-24 23:31:07 -0600759 /* Handle scanning a single device */
Simon Glassba3d5372023-01-17 10:48:15 -0700760 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass99e68182023-02-22 12:17:03 -0700761 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glassba3d5372023-01-17 10:48:15 -0700762 ret = bootdev_hunt(label, show);
763 if (ret)
764 return log_msg_ret("hun", ret);
765 }
766 ret = bootdev_find_by_any(label, &dev, &method_flags);
767 if (ret)
768 return log_msg_ret("lab", ret);
769
770 log_debug("method_flags: %x\n", method_flags);
771 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass99e68182023-02-22 12:17:03 -0700772 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glassba3d5372023-01-17 10:48:15 -0700773 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass99e68182023-02-22 12:17:03 -0700774 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glassba3d5372023-01-17 10:48:15 -0700775 else
Simon Glass99e68182023-02-22 12:17:03 -0700776 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glassba3d5372023-01-17 10:48:15 -0700777 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass484e4072023-01-17 10:48:14 -0700778 } else {
779 bool ok;
Simon Glass017656e2022-04-24 23:31:07 -0600780
Simon Glass484e4072023-01-17 10:48:14 -0700781 /* This either returns a non-empty list or NULL */
782 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
783 if (!ok)
784 return log_msg_ret("ord", -ENOMEM);
785 log_debug("setup labels %p\n", iter->labels);
786 if (iter->labels) {
787 iter->cur_label = -1;
788 ret = bootdev_next_label(iter, &dev, &method_flags);
789 } else {
790 ret = bootdev_next_prio(iter, &dev);
791 method_flags = 0;
792 }
793 if (!dev)
794 return log_msg_ret("fin", -ENOENT);
795 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass017656e2022-04-24 23:31:07 -0600796 }
797
Simon Glass017656e2022-04-24 23:31:07 -0600798 ret = device_probe(dev);
799 if (ret)
800 return log_msg_ret("probe", ret);
Simon Glass484e4072023-01-17 10:48:14 -0700801 if (method_flagsp)
802 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600803 *devp = dev;
804
805 return 0;
806}
807
Simon Glass1248d2b2023-01-17 10:47:34 -0700808static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
809{
810 const char *name = uclass_get_name(info->uclass);
811 struct bootstd_priv *std;
812 int ret;
813
814 ret = bootstd_get_priv(&std);
815 if (ret)
816 return log_msg_ret("std", ret);
817
818 if (!(std->hunters_used & BIT(seq))) {
819 if (show)
820 printf("Hunting with: %s\n",
821 uclass_get_name(info->uclass));
822 log_debug("Hunting with: %s\n", name);
823 if (info->hunt) {
824 ret = info->hunt(info, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600825 log_debug(" - hunt result %d\n", ret);
Tony Dinh81356b42023-11-02 11:51:15 -0700826 if (ret && ret != -ENOENT)
Simon Glass1248d2b2023-01-17 10:47:34 -0700827 return ret;
828 }
829 std->hunters_used |= BIT(seq);
830 }
831
832 return 0;
833}
834
835int bootdev_hunt(const char *spec, bool show)
836{
837 struct bootdev_hunter *start;
838 const char *end;
839 int n_ent, i;
840 int result;
841 size_t len;
842
843 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
844 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
845 result = 0;
846
847 len = SIZE_MAX;
848 if (spec) {
849 trailing_strtoln_end(spec, NULL, &end);
850 len = end - spec;
851 }
852
853 for (i = 0; i < n_ent; i++) {
854 struct bootdev_hunter *info = start + i;
855 const char *name = uclass_get_name(info->uclass);
856 int ret;
857
858 log_debug("looking at %.*s for %s\n",
859 (int)max(strlen(name), len), spec, name);
Simon Glassac06b26a2023-01-17 10:48:10 -0700860 if (spec && strncmp(spec, name, max(strlen(name), len))) {
861 if (info->uclass != UCLASS_ETH ||
862 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
863 continue;
864 }
Simon Glass1248d2b2023-01-17 10:47:34 -0700865 ret = bootdev_hunt_drv(info, i, show);
866 if (ret)
867 result = ret;
868 }
869
870 return result;
871}
872
Simon Glass62b03cc2023-09-20 07:29:49 -0600873int bootdev_unhunt(enum uclass_id id)
874{
875 struct bootdev_hunter *start;
876 int n_ent, i;
877
878 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
879 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
880 for (i = 0; i < n_ent; i++) {
881 struct bootdev_hunter *info = start + i;
882
883 if (info->uclass == id) {
884 struct bootstd_priv *std;
885 int ret;
886
887 ret = bootstd_get_priv(&std);
888 if (ret)
889 return log_msg_ret("std", ret);
890 if (!(std->hunters_used & BIT(i)))
891 return -EALREADY;
892 std->hunters_used &= ~BIT(i);
893 return 0;
894 }
895 }
896
897 return -ENOENT;
898}
899
Simon Glass3e9f6be2023-01-17 10:48:07 -0700900int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
901{
902 struct bootdev_hunter *start;
903 int n_ent, i;
904 int result;
905
906 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
907 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
908 result = 0;
909
910 log_debug("Hunting for priority %d\n", prio);
911 for (i = 0; i < n_ent; i++) {
912 struct bootdev_hunter *info = start + i;
913 int ret;
914
915 if (prio != info->prio)
916 continue;
917 ret = bootdev_hunt_drv(info, i, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600918 log_debug("bootdev_hunt_drv() return %d\n", ret);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700919 if (ret && ret != -ENOENT)
920 result = ret;
921 }
Simon Glassbf2d02a2023-07-30 11:15:16 -0600922 log_debug("exit %d\n", result);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700923
924 return result;
925}
926
Simon Glass5acb97a2023-01-17 10:47:33 -0700927void bootdev_list_hunters(struct bootstd_priv *std)
928{
929 struct bootdev_hunter *orig, *start;
930 int n_ent, i;
931
932 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
933 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
934
935 /*
936 * workaround for strange bug in clang-12 which sees all the below data
937 * as zeroes. Any access of start seems to fix it, such as
938 *
939 * printf("%p", start);
940 *
941 * Use memcpy() to force the correct behaviour.
942 */
943 memcpy(&start, &orig, sizeof(orig));
944 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
945 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
946 for (i = 0; i < n_ent; i++) {
947 struct bootdev_hunter *info = start + i;
948
949 printf("%4d %4s %-15s %s\n", info->prio,
950 std->hunters_used & BIT(i) ? "*" : "",
951 uclass_get_name(info->uclass),
952 info->drv ? info->drv->name : "(none)");
953 }
954
955 printf("(total hunters: %d)\n", n_ent);
956}
957
Simon Glass017656e2022-04-24 23:31:07 -0600958static int bootdev_post_bind(struct udevice *dev)
959{
960 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
961
962 INIT_LIST_HEAD(&ucp->bootflow_head);
963
964 return 0;
965}
966
967static int bootdev_pre_unbind(struct udevice *dev)
968{
969 bootdev_clear_bootflows(dev);
970
971 return 0;
972}
973
974UCLASS_DRIVER(bootdev) = {
975 .id = UCLASS_BOOTDEV,
976 .name = "bootdev",
977 .flags = DM_UC_FLAG_SEQ_ALIAS,
978 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
979 .post_bind = bootdev_post_bind,
980 .pre_unbind = bootdev_pre_unbind,
981};