blob: e8686159f2b1baed5522f3873ec6fbbb39f0e2e5 [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
42 assert(bflow->dev);
43 ret = bootstd_get_priv(&std);
44 if (ret)
45 return ret;
46
47 new = malloc(sizeof(*bflow));
48 if (!new)
49 return log_msg_ret("bflow", -ENOMEM);
50 memcpy(new, bflow, sizeof(*bflow));
51
52 list_add_tail(&new->glob_node, &std->glob_head);
Simon Glasseccb25c2022-07-30 15:52:23 -060053 if (bflow->dev) {
54 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
55
56 list_add_tail(&new->bm_node, &ucp->bootflow_head);
57 }
Simon Glass201417d2022-04-24 23:31:07 -060058
59 return 0;
60}
61
62int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
63{
64 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
65
66 if (list_empty(&ucp->bootflow_head))
67 return -ENOENT;
68
69 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
70 bm_node);
71
72 return 0;
73}
74
75int bootdev_next_bootflow(struct bootflow **bflowp)
76{
77 struct bootflow *bflow = *bflowp;
78 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
79
80 *bflowp = NULL;
81
82 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
83 return -ENOENT;
84
85 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
86
87 return 0;
88}
89
90int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
91 struct udevice **devp)
92{
93 struct udevice *dev;
94 char dev_name[30];
95 char *str;
96 int ret;
97
98 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
99 str = strdup(dev_name);
100 if (!str)
101 return -ENOMEM;
102 ret = device_bind_driver(parent, drv_name, str, &dev);
103 if (ret)
104 return ret;
105 device_set_name_alloced(dev);
106 *devp = dev;
107
108 return 0;
109}
110
111int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
112 struct bootflow_iter *iter, struct bootflow *bflow)
113{
114 struct blk_desc *desc = dev_get_uclass_plat(blk);
115 struct disk_partition info;
116 char partstr[20];
117 char name[60];
118 int ret;
119
120 /* Sanity check */
121 if (iter->part >= MAX_PART_PER_BOOTDEV)
122 return log_msg_ret("max", -ESHUTDOWN);
123
124 bflow->blk = blk;
125 if (iter->part)
126 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
127 else
128 strcpy(partstr, "whole");
129 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
130 bflow->name = strdup(name);
131 if (!bflow->name)
132 return log_msg_ret("name", -ENOMEM);
133
134 bflow->part = iter->part;
135
Simon Glassa8f5be12022-04-24 23:31:09 -0600136 ret = bootmeth_check(bflow->method, iter);
137 if (ret)
138 return log_msg_ret("check", ret);
139
Simon Glass201417d2022-04-24 23:31:07 -0600140 /*
141 * partition numbers start at 0 so this cannot succeed, but it can tell
142 * us whether there is valid media there
143 */
144 ret = part_get_info(desc, iter->part, &info);
145 if (!iter->part && ret == -ENOENT)
146 ret = 0;
147
148 /*
149 * This error indicates the media is not present. Otherwise we just
150 * blindly scan the next partition. We could be more intelligent here
151 * and check which partition numbers actually exist.
152 */
153 if (ret == -EOPNOTSUPP)
154 ret = -ESHUTDOWN;
155 else
156 bflow->state = BOOTFLOWST_MEDIA;
157 if (ret)
158 return log_msg_ret("part", ret);
159
160 /*
161 * Currently we don't get the number of partitions, so just
162 * assume a large number
163 */
164 iter->max_part = MAX_PART_PER_BOOTDEV;
165
Simon Glassf0e358f2023-01-17 10:47:42 -0700166 /* If this is the whole disk, check if we have bootable partitions */
167 if (!iter->part) {
168 iter->first_bootable = part_get_bootable(desc);
169 log_debug("checking bootable=%d\n", iter->first_bootable);
170
171 /* if there are bootable partitions, scan only those */
172 } else if (iter->first_bootable ? !info.bootable : iter->part != 1) {
173 return log_msg_ret("boot", -EINVAL);
174 } else {
Simon Glass201417d2022-04-24 23:31:07 -0600175 ret = fs_set_blk_dev_with_part(desc, bflow->part);
176 bflow->state = BOOTFLOWST_PART;
177
178 /* Use an #ifdef due to info.sys_ind */
179#ifdef CONFIG_DOS_PARTITION
180 log_debug("%s: Found partition %x type %x fstype %d\n",
181 blk->name, bflow->part, info.sys_ind,
182 ret ? -1 : fs_get_type());
183#endif
184 if (ret)
185 return log_msg_ret("fs", ret);
186 bflow->state = BOOTFLOWST_FS;
187 }
188
Simon Glassa8f5be12022-04-24 23:31:09 -0600189 ret = bootmeth_read_bootflow(bflow->method, bflow);
190 if (ret)
191 return log_msg_ret("method", ret);
192
Simon Glass201417d2022-04-24 23:31:07 -0600193 return 0;
194}
195
196void bootdev_list(bool probe)
197{
198 struct udevice *dev;
199 int ret;
200 int i;
201
202 printf("Seq Probed Status Uclass Name\n");
203 printf("--- ------ ------ -------- ------------------\n");
204 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200205 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass201417d2022-04-24 23:31:07 -0600206 else
207 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
208 for (i = 0; dev; i++) {
209 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
210 device_active(dev) ? '+' : ' ',
211 ret ? simple_itoa(ret) : "OK",
212 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
213 if (probe)
Michal Suchanek28a22cd2022-10-12 21:57:53 +0200214 ret = uclass_next_device_check(&dev);
Simon Glass201417d2022-04-24 23:31:07 -0600215 else
216 ret = uclass_find_next_device(&dev);
217 }
218 printf("--- ------ ------ -------- ------------------\n");
219 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
220}
221
222int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
223{
224 struct udevice *bdev;
225 int ret;
226
227 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
228 &bdev);
229 if (ret) {
230 if (ret != -ENODEV) {
231 log_debug("Cannot access bootdev device\n");
232 return ret;
233 }
234
235 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
236 if (ret) {
237 log_debug("Cannot create bootdev device\n");
238 return ret;
239 }
240 }
241
242 return 0;
243}
244
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700245static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
246{
247 int len, slen;
248
249 len = strlen(dev->name);
250 slen = strlen(suffix);
251 if (len > slen && !strcmp(suffix, dev->name + len - slen))
252 return len - slen;
253
254 return len;
255}
256
Simon Glass201417d2022-04-24 23:31:07 -0600257int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name)
258{
259 struct udevice *parent, *dev;
260 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700261 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600262
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700263 len = bootdev_get_suffix_start(blk, ".blk");
264 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
265 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600266
267 parent = dev_get_parent(blk);
268 ret = device_find_child_by_name(parent, dev_name, &dev);
269 if (ret) {
270 char *str;
271
272 if (ret != -ENODEV) {
273 log_debug("Cannot access bootdev device\n");
274 return ret;
275 }
276 str = strdup(dev_name);
277 if (!str)
278 return -ENOMEM;
279
280 ret = device_bind_driver(parent, drv_name, str, &dev);
281 if (ret) {
282 log_debug("Cannot create bootdev device\n");
283 return ret;
284 }
285 device_set_name_alloced(dev);
286 }
287
288 return 0;
289}
290
291int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
292{
293 struct udevice *parent = dev_get_parent(dev);
294 struct udevice *blk;
295 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600296
297 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
298 return -EINVAL;
299
300 /* This should always work if bootdev_setup_sibling_blk() was used */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700301 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600302 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700303 if (ret) {
304 char dev_name[50];
305
306 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
307 dev->name);
308 ret = device_find_child_by_name(parent, dev_name, &blk);
309 if (ret)
310 return log_msg_ret("find", ret);
311 }
Simon Glass201417d2022-04-24 23:31:07 -0600312 *blkp = blk;
313
314 return 0;
315}
316
317static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
318{
319 struct udevice *parent = dev_get_parent(blk);
320 struct udevice *bootdev;
321 char dev_name[50];
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700322 int ret, len;
Simon Glass201417d2022-04-24 23:31:07 -0600323
324 if (device_get_uclass_id(blk) != UCLASS_BLK)
325 return -EINVAL;
326
327 /* This should always work if bootdev_setup_sibling_blk() was used */
Simon Glass3a2cb96e52023-01-17 10:47:25 -0700328 len = bootdev_get_suffix_start(blk, ".blk");
329 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
330 "bootdev");
Simon Glass201417d2022-04-24 23:31:07 -0600331 ret = device_find_child_by_name(parent, dev_name, &bootdev);
332 if (ret)
333 return log_msg_ret("find", ret);
334 *bootdevp = bootdev;
335
336 return 0;
337}
338
339int bootdev_unbind_dev(struct udevice *parent)
340{
341 struct udevice *dev;
342 int ret;
343
344 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
345 if (!ret) {
346 ret = device_remove(dev, DM_REMOVE_NORMAL);
347 if (ret)
348 return log_msg_ret("rem", ret);
349 ret = device_unbind(dev);
350 if (ret)
351 return log_msg_ret("unb", ret);
352 }
353
354 return 0;
355}
356
357/**
Simon Glass74ebfb62023-01-17 10:48:00 -0700358 * label_to_uclass() - Convert a label to a uclass and sequence number
359 *
360 * @label: Label to look up (e.g. "mmc1" or "mmc0")
361 * @seqp: Returns the sequence number, or -1 if none
Simon Glassd9f48572023-01-17 10:48:05 -0700362 * @method_flagsp: If non-NULL, returns any flags implied by the label
363 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass74ebfb62023-01-17 10:48:00 -0700364 * Returns: sequence number on success, else -ve error code
365 */
Simon Glassd9f48572023-01-17 10:48:05 -0700366static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass74ebfb62023-01-17 10:48:00 -0700367{
Simon Glassd9f48572023-01-17 10:48:05 -0700368 int seq, len, method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700369 enum uclass_id id;
370 const char *end;
Simon Glass74ebfb62023-01-17 10:48:00 -0700371
Simon Glassd9f48572023-01-17 10:48:05 -0700372 method_flags = 0;
Simon Glass74ebfb62023-01-17 10:48:00 -0700373 seq = trailing_strtoln_end(label, NULL, &end);
374 len = end - label;
375 if (!len)
376 return -EINVAL;
377 id = uclass_get_by_namelen(label, len);
378 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
379 uclass_get_name(id));
380 if (id == UCLASS_INVALID) {
Simon Glass0c1f4a92023-01-17 10:48:03 -0700381 /* try some special cases */
382 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
383 !strncmp("spi", label, len)) {
384 id = UCLASS_SPI_FLASH;
Simon Glassd9f48572023-01-17 10:48:05 -0700385 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
386 !strncmp("pxe", label, len)) {
387 id = UCLASS_ETH;
388 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
389 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
390 !strncmp("dhcp", label, len)) {
391 id = UCLASS_ETH;
392 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass0c1f4a92023-01-17 10:48:03 -0700393 } else {
394 log_warning("Unknown uclass '%s' in label\n", label);
395 return -EINVAL;
396 }
Simon Glass74ebfb62023-01-17 10:48:00 -0700397 }
398 if (id == UCLASS_USB)
399 id = UCLASS_MASS_STORAGE;
400 *seqp = seq;
Simon Glassd9f48572023-01-17 10:48:05 -0700401 if (method_flagsp)
402 *method_flagsp = method_flags;
Simon Glass74ebfb62023-01-17 10:48:00 -0700403
404 return id;
405}
406
Simon Glassd9f48572023-01-17 10:48:05 -0700407int bootdev_find_by_label(const char *label, struct udevice **devp,
408 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600409{
Simon Glassd9f48572023-01-17 10:48:05 -0700410 int seq, ret, method_flags = 0;
Simon Glass201417d2022-04-24 23:31:07 -0600411 struct udevice *media;
412 struct uclass *uc;
413 enum uclass_id id;
Simon Glass201417d2022-04-24 23:31:07 -0600414
Simon Glassd9f48572023-01-17 10:48:05 -0700415 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass74ebfb62023-01-17 10:48:00 -0700416 if (ret < 0)
417 return log_msg_ret("uc", ret);
418 id = ret;
Simon Glass201417d2022-04-24 23:31:07 -0600419
420 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
421 uclass_id_foreach_dev(id, media, uc) {
422 struct udevice *bdev, *blk;
423 int ret;
424
425 /* if there is no seq, match anything */
426 if (seq != -1 && dev_seq(media) != seq) {
427 log_debug("- skip, media seq=%d\n", dev_seq(media));
428 continue;
429 }
430
431 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
432 &bdev);
433 if (ret) {
434 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
435 id);
436 ret = blk_find_device(id, seq, &blk);
437 if (!ret) {
438 log_debug("- get from blk %s\n", blk->name);
439 ret = bootdev_get_from_blk(blk, &bdev);
440 }
441 }
442 if (!ret) {
443 log_debug("- found %s\n", bdev->name);
444 *devp = bdev;
Simon Glassd9f48572023-01-17 10:48:05 -0700445 if (method_flagsp)
446 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600447 return 0;
448 }
449 log_debug("- no device in %s\n", media->name);
450 }
451 log_warning("Unknown seq %d for label '%s'\n", seq, label);
452
453 return -ENOENT;
454}
455
Simon Glassd9f48572023-01-17 10:48:05 -0700456int bootdev_find_by_any(const char *name, struct udevice **devp,
457 int *method_flagsp)
Simon Glass201417d2022-04-24 23:31:07 -0600458{
459 struct udevice *dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700460 int method_flags = 0;
Simon Glass201417d2022-04-24 23:31:07 -0600461 int ret, seq;
462 char *endp;
463
464 seq = simple_strtol(name, &endp, 16);
465
466 /* Select by name, label or number */
467 if (*endp) {
468 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
469 if (ret == -ENODEV) {
Simon Glassd9f48572023-01-17 10:48:05 -0700470 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass201417d2022-04-24 23:31:07 -0600471 if (ret) {
472 printf("Cannot find bootdev '%s' (err=%d)\n",
473 name, ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700474 return log_msg_ret("lab", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600475 }
476 ret = device_probe(dev);
477 }
478 if (ret) {
479 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
480 ret);
Simon Glassd9f48572023-01-17 10:48:05 -0700481 return log_msg_ret("pro", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600482 }
483 } else {
484 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
485 }
486 if (ret) {
487 printf("Cannot find '%s' (err=%d)\n", name, ret);
488 return ret;
489 }
490
491 *devp = dev;
Simon Glassd9f48572023-01-17 10:48:05 -0700492 if (method_flagsp)
493 *method_flagsp = method_flags;
Simon Glass201417d2022-04-24 23:31:07 -0600494
495 return 0;
496}
497
Simon Glassb85fc8d2023-01-17 10:47:26 -0700498static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
499 struct bootflow *bflow)
500{
501 struct udevice *blk;
502 int ret;
503
504 ret = bootdev_get_sibling_blk(dev, &blk);
505 /*
506 * If there is no media, indicate that no more partitions should be
507 * checked
508 */
509 if (ret == -EOPNOTSUPP)
510 ret = -ESHUTDOWN;
511 if (ret)
512 return log_msg_ret("blk", ret);
513 assert(blk);
514 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
515 if (ret)
516 return log_msg_ret("find", ret);
517
518 return 0;
519}
520
Simon Glass201417d2022-04-24 23:31:07 -0600521int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
522 struct bootflow *bflow)
523{
524 const struct bootdev_ops *ops = bootdev_get_ops(dev);
525
Simon Glassb190deb2022-10-20 18:22:51 -0600526 bootflow_init(bflow, dev, iter->method);
Simon Glassb85fc8d2023-01-17 10:47:26 -0700527 if (!ops->get_bootflow)
528 return default_get_bootflow(dev, iter, bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600529
530 return ops->get_bootflow(dev, iter, bflow);
531}
532
533void bootdev_clear_bootflows(struct udevice *dev)
534{
535 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
536
537 while (!list_empty(&ucp->bootflow_head)) {
538 struct bootflow *bflow;
539
540 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
541 bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600542 bootflow_remove(bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600543 }
544}
545
546/**
547 * h_cmp_bootdev() - Compare two bootdevs to find out which should go first
548 *
549 * @v1: struct udevice * of first bootdev device
550 * @v2: struct udevice * of second bootdev device
551 * Return: sort order (<0 if dev1 < dev2, ==0 if equal, >0 if dev1 > dev2)
552 */
553static int h_cmp_bootdev(const void *v1, const void *v2)
554{
555 const struct udevice *dev1 = *(struct udevice **)v1;
556 const struct udevice *dev2 = *(struct udevice **)v2;
557 const struct bootdev_uc_plat *ucp1 = dev_get_uclass_plat(dev1);
558 const struct bootdev_uc_plat *ucp2 = dev_get_uclass_plat(dev2);
559 int diff;
560
561 /* Use priority first */
562 diff = ucp1->prio - ucp2->prio;
563 if (diff)
564 return diff;
565
566 /* Fall back to seq for devices of the same priority */
567 diff = dev_seq(dev1) - dev_seq(dev2);
568
569 return diff;
570}
571
572/**
573 * build_order() - Build the ordered list of bootdevs to use
574 *
575 * This builds an ordered list of devices by one of three methods:
576 * - using the boot_targets environment variable, if non-empty
577 * - using the bootdev-order devicetree property, if present
578 * - sorted by priority and sequence number
579 *
580 * @bootstd: BOOTSTD device to use
581 * @order: Bootdevs listed in default order
582 * @max_count: Number of entries in @order
583 * Return: number of bootdevs found in the ordering, or -E2BIG if the
584 * boot_targets string is too long, or -EXDEV if the ordering produced 0 results
585 */
586static int build_order(struct udevice *bootstd, struct udevice **order,
587 int max_count)
588{
589 const char *overflow_target = NULL;
590 const char *const *labels;
591 struct udevice *dev;
Simon Glass201417d2022-04-24 23:31:07 -0600592 int i, ret, count;
Simon Glass6a6638f2023-01-17 10:47:15 -0700593 bool ok;
Simon Glass201417d2022-04-24 23:31:07 -0600594
Simon Glass6a6638f2023-01-17 10:47:15 -0700595 labels = bootstd_get_bootdev_order(bootstd, &ok);
596 if (!ok)
597 return log_msg_ret("ord", -ENOMEM);
598 if (labels) {
Simon Glass201417d2022-04-24 23:31:07 -0600599 int upto;
600
601 upto = 0;
602 for (i = 0; labels[i]; i++) {
Simon Glassd9f48572023-01-17 10:48:05 -0700603 ret = bootdev_find_by_label(labels[i], &dev, NULL);
Simon Glass201417d2022-04-24 23:31:07 -0600604 if (!ret) {
605 if (upto == max_count) {
606 overflow_target = labels[i];
607 break;
608 }
609 order[upto++] = dev;
610 }
611 }
612 count = upto;
613 } else {
614 /* sort them into priority order */
615 count = max_count;
616 qsort(order, count, sizeof(struct udevice *), h_cmp_bootdev);
617 }
618
619 if (overflow_target) {
620 log_warning("Expected at most %d bootdevs, but overflowed with boot_target '%s'\n",
621 max_count, overflow_target);
622 }
623
624 if (!count)
625 return log_msg_ret("targ", -EXDEV);
626
627 return count;
628}
629
630int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp)
631{
632 struct udevice *bootstd, *dev = *devp, **order;
Simon Glasscb698b02023-01-17 10:47:16 -0700633 struct uclass *uc;
634 int count, upto;
Simon Glass201417d2022-04-24 23:31:07 -0600635 int ret;
636
637 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
638 if (ret) {
639 log_err("Missing bootstd device\n");
640 return log_msg_ret("std", ret);
641 }
642
643 /* Handle scanning a single device */
644 if (dev) {
645 iter->flags |= BOOTFLOWF_SINGLE_DEV;
646 return 0;
647 }
648
649 count = uclass_id_count(UCLASS_BOOTDEV);
650 if (!count)
651 return log_msg_ret("count", -ENOENT);
652
653 order = calloc(count, sizeof(struct udevice *));
654 if (!order)
655 return log_msg_ret("order", -ENOMEM);
656
Simon Glasscb698b02023-01-17 10:47:16 -0700657 /* Get the list of bootdevs */
658 uclass_id_foreach_dev(UCLASS_BOOTDEV, dev, uc)
659 order[upto++] = dev;
Simon Glass201417d2022-04-24 23:31:07 -0600660 log_debug("Found %d bootdevs\n", count);
661 if (upto != count)
662 log_debug("Expected %d bootdevs, found %d using aliases\n",
663 count, upto);
664
Simon Glassa18686c2022-07-30 15:52:20 -0600665 ret = build_order(bootstd, order, upto);
666 if (ret < 0) {
Simon Glass201417d2022-04-24 23:31:07 -0600667 free(order);
Simon Glassa18686c2022-07-30 15:52:20 -0600668 return log_msg_ret("build", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600669 }
670
Simon Glassa18686c2022-07-30 15:52:20 -0600671 iter->num_devs = ret;
Simon Glass201417d2022-04-24 23:31:07 -0600672 iter->dev_order = order;
Simon Glass201417d2022-04-24 23:31:07 -0600673 iter->cur_dev = 0;
674
675 dev = *order;
676 ret = device_probe(dev);
677 if (ret)
678 return log_msg_ret("probe", ret);
679 *devp = dev;
680
681 return 0;
682}
683
Simon Glassc7b63d52023-01-17 10:47:34 -0700684static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
685{
686 const char *name = uclass_get_name(info->uclass);
687 struct bootstd_priv *std;
688 int ret;
689
690 ret = bootstd_get_priv(&std);
691 if (ret)
692 return log_msg_ret("std", ret);
693
694 if (!(std->hunters_used & BIT(seq))) {
695 if (show)
696 printf("Hunting with: %s\n",
697 uclass_get_name(info->uclass));
698 log_debug("Hunting with: %s\n", name);
699 if (info->hunt) {
700 ret = info->hunt(info, show);
701 if (ret)
702 return ret;
703 }
704 std->hunters_used |= BIT(seq);
705 }
706
707 return 0;
708}
709
710int bootdev_hunt(const char *spec, bool show)
711{
712 struct bootdev_hunter *start;
713 const char *end;
714 int n_ent, i;
715 int result;
716 size_t len;
717
718 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
719 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
720 result = 0;
721
722 len = SIZE_MAX;
723 if (spec) {
724 trailing_strtoln_end(spec, NULL, &end);
725 len = end - spec;
726 }
727
728 for (i = 0; i < n_ent; i++) {
729 struct bootdev_hunter *info = start + i;
730 const char *name = uclass_get_name(info->uclass);
731 int ret;
732
733 log_debug("looking at %.*s for %s\n",
734 (int)max(strlen(name), len), spec, name);
735 if (spec && strncmp(spec, name, max(strlen(name), len)))
736 continue;
737 ret = bootdev_hunt_drv(info, i, show);
738 if (ret)
739 result = ret;
740 }
741
742 return result;
743}
744
Simon Glass79a7d4a2023-01-17 10:48:07 -0700745int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
746{
747 struct bootdev_hunter *start;
748 int n_ent, i;
749 int result;
750
751 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
752 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
753 result = 0;
754
755 log_debug("Hunting for priority %d\n", prio);
756 for (i = 0; i < n_ent; i++) {
757 struct bootdev_hunter *info = start + i;
758 int ret;
759
760 if (prio != info->prio)
761 continue;
762 ret = bootdev_hunt_drv(info, i, show);
763 if (ret && ret != -ENOENT)
764 result = ret;
765 }
766
767 return result;
768}
769
Simon Glassbd90b092023-01-17 10:47:33 -0700770void bootdev_list_hunters(struct bootstd_priv *std)
771{
772 struct bootdev_hunter *orig, *start;
773 int n_ent, i;
774
775 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
776 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
777
778 /*
779 * workaround for strange bug in clang-12 which sees all the below data
780 * as zeroes. Any access of start seems to fix it, such as
781 *
782 * printf("%p", start);
783 *
784 * Use memcpy() to force the correct behaviour.
785 */
786 memcpy(&start, &orig, sizeof(orig));
787 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
788 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
789 for (i = 0; i < n_ent; i++) {
790 struct bootdev_hunter *info = start + i;
791
792 printf("%4d %4s %-15s %s\n", info->prio,
793 std->hunters_used & BIT(i) ? "*" : "",
794 uclass_get_name(info->uclass),
795 info->drv ? info->drv->name : "(none)");
796 }
797
798 printf("(total hunters: %d)\n", n_ent);
799}
800
Simon Glass201417d2022-04-24 23:31:07 -0600801static int bootdev_post_bind(struct udevice *dev)
802{
803 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
804
805 INIT_LIST_HEAD(&ucp->bootflow_head);
806
807 return 0;
808}
809
810static int bootdev_pre_unbind(struct udevice *dev)
811{
812 bootdev_clear_bootflows(dev);
813
814 return 0;
815}
816
817UCLASS_DRIVER(bootdev) = {
818 .id = UCLASS_BOOTDEV,
819 .name = "bootdev",
820 .flags = DM_UC_FLAG_SEQ_ALIAS,
821 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
822 .post_bind = bootdev_post_bind,
823 .pre_unbind = bootdev_pre_unbind,
824};