blob: 974ddee5d2faa05af687dab16f99ab4065a31f08 [file] [log] [blame]
Simon Glass017656e2022-04-24 23:31:07 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#define LOG_CATEGORY UCLASS_BOOTSTD
8
9#include <common.h>
10#include <dm.h>
11#include <bootdev.h>
12#include <bootflow.h>
Simon Glass03fcbf92022-04-24 23:31:09 -060013#include <bootmeth.h>
Simon Glass017656e2022-04-24 23:31:07 -060014#include <bootstd.h>
Simon Glass017656e2022-04-24 23:31:07 -060015#include <fs.h>
16#include <log.h>
17#include <malloc.h>
18#include <part.h>
19#include <sort.h>
20#include <dm/device-internal.h>
21#include <dm/lists.h>
22#include <dm/uclass-internal.h>
23
24enum {
25 /*
26 * Set some sort of limit on the number of partitions a bootdev can
27 * have. Note that for disks this limits the partitions numbers that
28 * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV
29 */
30 MAX_PART_PER_BOOTDEV = 30,
31
32 /* Maximum supported length of the "boot_targets" env string */
33 BOOT_TARGETS_MAX_LEN = 100,
34};
35
36int bootdev_add_bootflow(struct bootflow *bflow)
37{
Simon Glass017656e2022-04-24 23:31:07 -060038 struct bootstd_priv *std;
39 struct bootflow *new;
40 int ret;
41
42 assert(bflow->dev);
43 ret = bootstd_get_priv(&std);
44 if (ret)
45 return ret;
46
47 new = malloc(sizeof(*bflow));
48 if (!new)
49 return log_msg_ret("bflow", -ENOMEM);
50 memcpy(new, bflow, sizeof(*bflow));
51
52 list_add_tail(&new->glob_node, &std->glob_head);
Simon Glassdee4d642022-07-30 15:52:23 -060053 if (bflow->dev) {
54 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
55
56 list_add_tail(&new->bm_node, &ucp->bootflow_head);
57 }
Simon Glass017656e2022-04-24 23:31:07 -060058
59 return 0;
60}
61
62int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
63{
64 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
65
66 if (list_empty(&ucp->bootflow_head))
67 return -ENOENT;
68
69 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
70 bm_node);
71
72 return 0;
73}
74
75int bootdev_next_bootflow(struct bootflow **bflowp)
76{
77 struct bootflow *bflow = *bflowp;
78 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
79
80 *bflowp = NULL;
81
82 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
83 return -ENOENT;
84
85 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
86
87 return 0;
88}
89
90int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
91 struct udevice **devp)
92{
93 struct udevice *dev;
94 char dev_name[30];
95 char *str;
96 int ret;
97
98 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
99 str = strdup(dev_name);
100 if (!str)
101 return -ENOMEM;
102 ret = device_bind_driver(parent, drv_name, str, &dev);
103 if (ret)
104 return ret;
105 device_set_name_alloced(dev);
106 *devp = dev;
107
108 return 0;
109}
110
111int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
112 struct bootflow_iter *iter, struct bootflow *bflow)
113{
Simon Glass0fca7e82023-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 Glass017656e2022-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 Glass03fcbf92022-04-24 23:31:09 -0600138 ret = bootmeth_check(bflow->method, iter);
139 if (ret)
140 return log_msg_ret("check", ret);
141
Simon Glass017656e2022-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 Glass0fca7e82023-08-24 13:55:43 -0600147 log_debug("part_get_info() returned %d\n", ret);
Simon Glass017656e2022-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 Glass0fca7e82023-08-24 13:55:43 -0600160 if (ret && !allow_any_part) {
Simon Glassa8e7d512023-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 Glass017656e2022-04-24 23:31:07 -0600167 return log_msg_ret("part", ret);
Simon Glassa8e7d512023-04-28 13:18:09 -0600168 }
Simon Glass017656e2022-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
Simon Glass64cbc5c2023-01-17 10:47:42 -0700176 /* If this is the whole disk, check if we have bootable partitions */
177 if (!iter->part) {
178 iter->first_bootable = part_get_bootable(desc);
179 log_debug("checking bootable=%d\n", iter->first_bootable);
Simon Glass0fca7e82023-08-24 13:55:43 -0600180 } else if (allow_any_part) {
181 /*
182 * allow any partition to be scanned, by skipping any checks
183 * for filesystems or partition contents on this disk
184 */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700185
186 /* if there are bootable partitions, scan only those */
Simon Glass0fca7e82023-08-24 13:55:43 -0600187 } else if (iter->first_bootable >= 0 &&
188 (iter->first_bootable ? !info.bootable : iter->part != 1)) {
Simon Glass64cbc5c2023-01-17 10:47:42 -0700189 return log_msg_ret("boot", -EINVAL);
190 } else {
Simon Glass017656e2022-04-24 23:31:07 -0600191 ret = fs_set_blk_dev_with_part(desc, bflow->part);
192 bflow->state = BOOTFLOWST_PART;
Simon Glassa40f7822023-04-24 13:49:49 +1200193 if (ret)
194 return log_msg_ret("fs", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600195
Simon Glass017656e2022-04-24 23:31:07 -0600196 log_debug("%s: Found partition %x type %x fstype %d\n",
Simon Glassed0ec662023-08-24 13:55:33 -0600197 blk->name, bflow->part,
198 IS_ENABLED(CONFIG_DOS_PARTITION) ?
199 disk_partition_sys_ind(&info) : 0,
Simon Glass017656e2022-04-24 23:31:07 -0600200 ret ? -1 : fs_get_type());
Simon Glassa40f7822023-04-24 13:49:49 +1200201 bflow->blk = blk;
Simon Glass017656e2022-04-24 23:31:07 -0600202 bflow->state = BOOTFLOWST_FS;
203 }
204
Simon Glass0fca7e82023-08-24 13:55:43 -0600205 log_debug("method %s\n", bflow->method->name);
Simon Glass03fcbf92022-04-24 23:31:09 -0600206 ret = bootmeth_read_bootflow(bflow->method, bflow);
207 if (ret)
208 return log_msg_ret("method", ret);
209
Simon Glass017656e2022-04-24 23:31:07 -0600210 return 0;
211}
212
213void bootdev_list(bool probe)
214{
215 struct udevice *dev;
216 int ret;
217 int i;
218
219 printf("Seq Probed Status Uclass Name\n");
220 printf("--- ------ ------ -------- ------------------\n");
221 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200222 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass017656e2022-04-24 23:31:07 -0600223 else
224 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
225 for (i = 0; dev; i++) {
226 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
227 device_active(dev) ? '+' : ' ',
Heinrich Schuchardt91592d62023-07-30 16:29:25 +0200228 ret ? simple_itoa(-ret) : "OK",
Simon Glass017656e2022-04-24 23:31:07 -0600229 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
230 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200231 ret = uclass_next_device_check(&dev);
Simon Glass017656e2022-04-24 23:31:07 -0600232 else
233 ret = uclass_find_next_device(&dev);
234 }
235 printf("--- ------ ------ -------- ------------------\n");
236 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
237}
238
239int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
240{
241 struct udevice *bdev;
242 int ret;
243
244 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
245 &bdev);
246 if (ret) {
247 if (ret != -ENODEV) {
248 log_debug("Cannot access bootdev device\n");
249 return ret;
250 }
251
252 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
253 if (ret) {
254 log_debug("Cannot create bootdev device\n");
255 return ret;
256 }
257 }
258
259 return 0;
260}
261
Simon Glassb1dc36a2023-01-17 10:47:25 -0700262static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
263{
264 int len, slen;
265
266 len = strlen(dev->name);
267 slen = strlen(suffix);
268 if (len > slen && !strcmp(suffix, dev->name + len - slen))
269 return len - slen;
270
271 return len;
272}
273
Simon Glassb1d581d2023-07-30 11:15:14 -0600274int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
Simon Glass017656e2022-04-24 23:31:07 -0600275{
276 struct udevice *parent, *dev;
277 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700278 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600279
Simon Glassb1dc36a2023-01-17 10:47:25 -0700280 len = bootdev_get_suffix_start(blk, ".blk");
281 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
282 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600283
284 parent = dev_get_parent(blk);
285 ret = device_find_child_by_name(parent, dev_name, &dev);
286 if (ret) {
287 char *str;
288
289 if (ret != -ENODEV) {
290 log_debug("Cannot access bootdev device\n");
291 return ret;
292 }
293 str = strdup(dev_name);
294 if (!str)
295 return -ENOMEM;
296
297 ret = device_bind_driver(parent, drv_name, str, &dev);
298 if (ret) {
299 log_debug("Cannot create bootdev device\n");
300 return ret;
301 }
302 device_set_name_alloced(dev);
303 }
304
305 return 0;
306}
307
308int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
309{
310 struct udevice *parent = dev_get_parent(dev);
311 struct udevice *blk;
312 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600313
314 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
315 return -EINVAL;
316
Simon Glassb1d581d2023-07-30 11:15:14 -0600317 /*
318 * This should always work if bootdev_setup_for_sibling_blk() was used
319 */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700320 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600321 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glassb1dc36a2023-01-17 10:47:25 -0700322 if (ret) {
323 char dev_name[50];
324
325 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
326 dev->name);
327 ret = device_find_child_by_name(parent, dev_name, &blk);
328 if (ret)
329 return log_msg_ret("find", ret);
330 }
Simon Glassb73a8292023-01-28 15:00:19 -0700331 ret = device_probe(blk);
332 if (ret)
333 return log_msg_ret("act", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600334 *blkp = blk;
335
336 return 0;
337}
338
339static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
340{
341 struct udevice *parent = dev_get_parent(blk);
342 struct udevice *bootdev;
343 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700344 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600345
346 if (device_get_uclass_id(blk) != UCLASS_BLK)
347 return -EINVAL;
348
Simon Glassb1d581d2023-07-30 11:15:14 -0600349 /* This should always work if bootdev_setup_for_sibling_blk() was used */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700350 len = bootdev_get_suffix_start(blk, ".blk");
351 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
352 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600353 ret = device_find_child_by_name(parent, dev_name, &bootdev);
354 if (ret)
355 return log_msg_ret("find", ret);
356 *bootdevp = bootdev;
357
358 return 0;
359}
360
361int bootdev_unbind_dev(struct udevice *parent)
362{
363 struct udevice *dev;
364 int ret;
365
366 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
367 if (!ret) {
368 ret = device_remove(dev, DM_REMOVE_NORMAL);
369 if (ret)
370 return log_msg_ret("rem", ret);
371 ret = device_unbind(dev);
372 if (ret)
373 return log_msg_ret("unb", ret);
374 }
375
376 return 0;
377}
378
379/**
Simon Glass73708252023-01-17 10:48:00 -0700380 * label_to_uclass() - Convert a label to a uclass and sequence number
381 *
382 * @label: Label to look up (e.g. "mmc1" or "mmc0")
383 * @seqp: Returns the sequence number, or -1 if none
Simon Glasse22fe922023-01-17 10:48:05 -0700384 * @method_flagsp: If non-NULL, returns any flags implied by the label
385 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass4108c6b2023-04-24 13:49:47 +1200386 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
387 * known, other -ve error code on other error
Simon Glass73708252023-01-17 10:48:00 -0700388 */
Simon Glasse22fe922023-01-17 10:48:05 -0700389static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass73708252023-01-17 10:48:00 -0700390{
Simon Glasse22fe922023-01-17 10:48:05 -0700391 int seq, len, method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700392 enum uclass_id id;
393 const char *end;
Simon Glass73708252023-01-17 10:48:00 -0700394
Simon Glasse22fe922023-01-17 10:48:05 -0700395 method_flags = 0;
Simon Glass73708252023-01-17 10:48:00 -0700396 seq = trailing_strtoln_end(label, NULL, &end);
397 len = end - label;
398 if (!len)
399 return -EINVAL;
400 id = uclass_get_by_namelen(label, len);
401 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
402 uclass_get_name(id));
403 if (id == UCLASS_INVALID) {
Simon Glass215be682023-01-17 10:48:03 -0700404 /* try some special cases */
405 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
406 !strncmp("spi", label, len)) {
407 id = UCLASS_SPI_FLASH;
Simon Glasse22fe922023-01-17 10:48:05 -0700408 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
409 !strncmp("pxe", label, len)) {
410 id = UCLASS_ETH;
411 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
412 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
413 !strncmp("dhcp", label, len)) {
414 id = UCLASS_ETH;
415 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass215be682023-01-17 10:48:03 -0700416 } else {
Simon Glass4108c6b2023-04-24 13:49:47 +1200417 return -EPFNOSUPPORT;
Simon Glass215be682023-01-17 10:48:03 -0700418 }
Simon Glass73708252023-01-17 10:48:00 -0700419 }
420 if (id == UCLASS_USB)
421 id = UCLASS_MASS_STORAGE;
422 *seqp = seq;
Simon Glasse22fe922023-01-17 10:48:05 -0700423 if (method_flagsp)
424 *method_flagsp = method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700425
426 return id;
427}
428
Simon Glasse22fe922023-01-17 10:48:05 -0700429int bootdev_find_by_label(const char *label, struct udevice **devp,
430 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600431{
Simon Glasse22fe922023-01-17 10:48:05 -0700432 int seq, ret, method_flags = 0;
Simon Glass017656e2022-04-24 23:31:07 -0600433 struct udevice *media;
434 struct uclass *uc;
435 enum uclass_id id;
Simon Glass017656e2022-04-24 23:31:07 -0600436
Simon Glasse22fe922023-01-17 10:48:05 -0700437 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass73708252023-01-17 10:48:00 -0700438 if (ret < 0)
439 return log_msg_ret("uc", ret);
440 id = ret;
Simon Glass017656e2022-04-24 23:31:07 -0600441
442 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
443 uclass_id_foreach_dev(id, media, uc) {
444 struct udevice *bdev, *blk;
445 int ret;
446
447 /* if there is no seq, match anything */
448 if (seq != -1 && dev_seq(media) != seq) {
449 log_debug("- skip, media seq=%d\n", dev_seq(media));
450 continue;
451 }
452
453 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
454 &bdev);
455 if (ret) {
456 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
457 id);
458 ret = blk_find_device(id, seq, &blk);
459 if (!ret) {
460 log_debug("- get from blk %s\n", blk->name);
461 ret = bootdev_get_from_blk(blk, &bdev);
462 }
463 }
464 if (!ret) {
465 log_debug("- found %s\n", bdev->name);
466 *devp = bdev;
Simon Glassde567b12023-01-17 10:48:09 -0700467
468 /*
469 * if no sequence number was provided, we must scan all
470 * bootdevs for this media uclass
471 */
472 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
473 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glasse22fe922023-01-17 10:48:05 -0700474 if (method_flagsp)
475 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600476 return 0;
477 }
478 log_debug("- no device in %s\n", media->name);
479 }
Simon Glass017656e2022-04-24 23:31:07 -0600480
481 return -ENOENT;
482}
483
Simon Glasse22fe922023-01-17 10:48:05 -0700484int bootdev_find_by_any(const char *name, struct udevice **devp,
485 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600486{
487 struct udevice *dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700488 int method_flags = 0;
Simon Glassde567b12023-01-17 10:48:09 -0700489 int ret = -ENODEV, seq;
Simon Glass017656e2022-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 Glasse22fe922023-01-17 10:48:05 -0700498 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600499 if (ret) {
500 printf("Cannot find bootdev '%s' (err=%d)\n",
501 name, ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700502 return log_msg_ret("lab", ret);
Simon Glass017656e2022-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 Glasse22fe922023-01-17 10:48:05 -0700509 return log_msg_ret("pro", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600510 }
Simon Glassde567b12023-01-17 10:48:09 -0700511 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass017656e2022-04-24 23:31:07 -0600512 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glassde567b12023-01-17 10:48:09 -0700513 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass017656e2022-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 Glasse22fe922023-01-17 10:48:05 -0700521 if (method_flagsp)
522 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600523
524 return 0;
525}
526
Simon Glassde567b12023-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 Glassd6e39d12023-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 Glassbf2d02a2023-07-30 11:15:16 -0600549 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
550 ret ? "(none)" : blk->name);
Simon Glassd6e39d12023-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 Glass017656e2022-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 Glass0fca7e82023-08-24 13:55:43 -0600572 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
573 ops->get_bootflow);
Simon Glass00501fc2022-10-20 18:22:51 -0600574 bootflow_init(bflow, dev, iter->method);
Simon Glassd6e39d12023-01-17 10:47:26 -0700575 if (!ops->get_bootflow)
576 return default_get_bootflow(dev, iter, bflow);
Simon Glass017656e2022-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 Glass03fcbf92022-04-24 23:31:09 -0600590 bootflow_remove(bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600591 }
592}
593
Simon Glassac06b26a2023-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 Glass4108c6b2023-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 Glassac06b26a2023-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 Glass660a9952023-01-17 10:48:11 -0700632int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
633{
634 struct udevice *dev = *devp;
635 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 Glass99e68182023-02-22 12:17:03 -0700672 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass660a9952023-01-17 10:48:11 -0700673 /* hunt to find new bootdevs */
674 ret = bootdev_hunt_prio(iter->cur_prio,
675 iter->flags &
Simon Glass99e68182023-02-22 12:17:03 -0700676 BOOTFLOWIF_SHOW);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600677 log_debug("- bootdev_hunt_prio() ret %d\n",
678 ret);
Simon Glass660a9952023-01-17 10:48:11 -0700679 if (ret)
680 return log_msg_ret("hun", ret);
681 }
682 } else {
683 ret = device_probe(dev);
684 if (ret) {
685 log_debug("Device '%s' failed to probe\n",
686 dev->name);
687 dev = NULL;
688 }
689 }
690 } while (!dev);
691
692 *devp = dev;
693
694 return 0;
695}
696
Simon Glassba3d5372023-01-17 10:48:15 -0700697int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
698 struct udevice **devp, int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600699{
Simon Glassba3d5372023-01-17 10:48:15 -0700700 struct udevice *bootstd, *dev = NULL;
Simon Glass99e68182023-02-22 12:17:03 -0700701 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass484e4072023-01-17 10:48:14 -0700702 int method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600703 int ret;
704
705 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
706 if (ret) {
707 log_err("Missing bootstd device\n");
708 return log_msg_ret("std", ret);
709 }
710
Simon Glass7e1f6a42023-01-17 10:48:08 -0700711 /* hunt for any pre-scan devices */
Simon Glass99e68182023-02-22 12:17:03 -0700712 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700713 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600714 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700715 if (ret)
716 return log_msg_ret("pre", ret);
717 }
718
Simon Glass017656e2022-04-24 23:31:07 -0600719 /* Handle scanning a single device */
Simon Glassba3d5372023-01-17 10:48:15 -0700720 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass99e68182023-02-22 12:17:03 -0700721 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glassba3d5372023-01-17 10:48:15 -0700722 ret = bootdev_hunt(label, show);
723 if (ret)
724 return log_msg_ret("hun", ret);
725 }
726 ret = bootdev_find_by_any(label, &dev, &method_flags);
727 if (ret)
728 return log_msg_ret("lab", ret);
729
730 log_debug("method_flags: %x\n", method_flags);
731 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass99e68182023-02-22 12:17:03 -0700732 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glassba3d5372023-01-17 10:48:15 -0700733 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass99e68182023-02-22 12:17:03 -0700734 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glassba3d5372023-01-17 10:48:15 -0700735 else
Simon Glass99e68182023-02-22 12:17:03 -0700736 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glassba3d5372023-01-17 10:48:15 -0700737 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass484e4072023-01-17 10:48:14 -0700738 } else {
739 bool ok;
Simon Glass017656e2022-04-24 23:31:07 -0600740
Simon Glass484e4072023-01-17 10:48:14 -0700741 /* This either returns a non-empty list or NULL */
742 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
743 if (!ok)
744 return log_msg_ret("ord", -ENOMEM);
745 log_debug("setup labels %p\n", iter->labels);
746 if (iter->labels) {
747 iter->cur_label = -1;
748 ret = bootdev_next_label(iter, &dev, &method_flags);
749 } else {
750 ret = bootdev_next_prio(iter, &dev);
751 method_flags = 0;
752 }
753 if (!dev)
754 return log_msg_ret("fin", -ENOENT);
755 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass017656e2022-04-24 23:31:07 -0600756 }
757
Simon Glass017656e2022-04-24 23:31:07 -0600758 ret = device_probe(dev);
759 if (ret)
760 return log_msg_ret("probe", ret);
Simon Glass484e4072023-01-17 10:48:14 -0700761 if (method_flagsp)
762 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600763 *devp = dev;
764
765 return 0;
766}
767
Simon Glass1248d2b2023-01-17 10:47:34 -0700768static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
769{
770 const char *name = uclass_get_name(info->uclass);
771 struct bootstd_priv *std;
772 int ret;
773
774 ret = bootstd_get_priv(&std);
775 if (ret)
776 return log_msg_ret("std", ret);
777
778 if (!(std->hunters_used & BIT(seq))) {
779 if (show)
780 printf("Hunting with: %s\n",
781 uclass_get_name(info->uclass));
782 log_debug("Hunting with: %s\n", name);
783 if (info->hunt) {
784 ret = info->hunt(info, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600785 log_debug(" - hunt result %d\n", ret);
Simon Glass1248d2b2023-01-17 10:47:34 -0700786 if (ret)
787 return ret;
788 }
789 std->hunters_used |= BIT(seq);
790 }
791
792 return 0;
793}
794
795int bootdev_hunt(const char *spec, bool show)
796{
797 struct bootdev_hunter *start;
798 const char *end;
799 int n_ent, i;
800 int result;
801 size_t len;
802
803 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
804 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
805 result = 0;
806
807 len = SIZE_MAX;
808 if (spec) {
809 trailing_strtoln_end(spec, NULL, &end);
810 len = end - spec;
811 }
812
813 for (i = 0; i < n_ent; i++) {
814 struct bootdev_hunter *info = start + i;
815 const char *name = uclass_get_name(info->uclass);
816 int ret;
817
818 log_debug("looking at %.*s for %s\n",
819 (int)max(strlen(name), len), spec, name);
Simon Glassac06b26a2023-01-17 10:48:10 -0700820 if (spec && strncmp(spec, name, max(strlen(name), len))) {
821 if (info->uclass != UCLASS_ETH ||
822 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
823 continue;
824 }
Simon Glass1248d2b2023-01-17 10:47:34 -0700825 ret = bootdev_hunt_drv(info, i, show);
826 if (ret)
827 result = ret;
828 }
829
830 return result;
831}
832
Simon Glass62b03cc2023-09-20 07:29:49 -0600833int bootdev_unhunt(enum uclass_id id)
834{
835 struct bootdev_hunter *start;
836 int n_ent, i;
837
838 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
839 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
840 for (i = 0; i < n_ent; i++) {
841 struct bootdev_hunter *info = start + i;
842
843 if (info->uclass == id) {
844 struct bootstd_priv *std;
845 int ret;
846
847 ret = bootstd_get_priv(&std);
848 if (ret)
849 return log_msg_ret("std", ret);
850 if (!(std->hunters_used & BIT(i)))
851 return -EALREADY;
852 std->hunters_used &= ~BIT(i);
853 return 0;
854 }
855 }
856
857 return -ENOENT;
858}
859
Simon Glass3e9f6be2023-01-17 10:48:07 -0700860int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
861{
862 struct bootdev_hunter *start;
863 int n_ent, i;
864 int result;
865
866 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
867 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
868 result = 0;
869
870 log_debug("Hunting for priority %d\n", prio);
871 for (i = 0; i < n_ent; i++) {
872 struct bootdev_hunter *info = start + i;
873 int ret;
874
875 if (prio != info->prio)
876 continue;
877 ret = bootdev_hunt_drv(info, i, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600878 log_debug("bootdev_hunt_drv() return %d\n", ret);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700879 if (ret && ret != -ENOENT)
880 result = ret;
881 }
Simon Glassbf2d02a2023-07-30 11:15:16 -0600882 log_debug("exit %d\n", result);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700883
884 return result;
885}
886
Simon Glass5acb97a2023-01-17 10:47:33 -0700887void bootdev_list_hunters(struct bootstd_priv *std)
888{
889 struct bootdev_hunter *orig, *start;
890 int n_ent, i;
891
892 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
893 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
894
895 /*
896 * workaround for strange bug in clang-12 which sees all the below data
897 * as zeroes. Any access of start seems to fix it, such as
898 *
899 * printf("%p", start);
900 *
901 * Use memcpy() to force the correct behaviour.
902 */
903 memcpy(&start, &orig, sizeof(orig));
904 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
905 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
906 for (i = 0; i < n_ent; i++) {
907 struct bootdev_hunter *info = start + i;
908
909 printf("%4d %4s %-15s %s\n", info->prio,
910 std->hunters_used & BIT(i) ? "*" : "",
911 uclass_get_name(info->uclass),
912 info->drv ? info->drv->name : "(none)");
913 }
914
915 printf("(total hunters: %d)\n", n_ent);
916}
917
Simon Glass017656e2022-04-24 23:31:07 -0600918static int bootdev_post_bind(struct udevice *dev)
919{
920 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
921
922 INIT_LIST_HEAD(&ucp->bootflow_head);
923
924 return 0;
925}
926
927static int bootdev_pre_unbind(struct udevice *dev)
928{
929 bootdev_clear_bootflows(dev);
930
931 return 0;
932}
933
934UCLASS_DRIVER(bootdev) = {
935 .id = UCLASS_BOOTDEV,
936 .name = "bootdev",
937 .flags = DM_UC_FLAG_SEQ_ALIAS,
938 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
939 .post_bind = bootdev_post_bind,
940 .pre_unbind = bootdev_pre_unbind,
941};