blob: c39147940b6be207317eaa95489cca26d4f77827 [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
Simon Glass017656e2022-04-24 23:31:07 -060036int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
Simon Glass017656e2022-04-24 23:31:07 -060037{
38 struct bootstd_priv *std;
Simon Glass1d8f8642024-11-15 16:19:11 -070039 struct bootflow *bflow;
Simon Glass017656e2022-04-24 23:31:07 -060040 int ret;
41
42 ret = bootstd_get_priv(&std);
43 if (ret)
Simon Glass199f5882024-11-15 16:19:12 -070044 return log_msg_ret("bff", ret);
Simon Glass017656e2022-04-24 23:31:07 -060045
Simon Glass199f5882024-11-15 16:19:12 -070046 bflow = alist_getw(&std->bootflows, 0, struct bootflow);
Simon Glass1d8f8642024-11-15 16:19:11 -070047 if (!bflow)
Simon Glass017656e2022-04-24 23:31:07 -060048 return -ENOENT;
Simon Glass1d8f8642024-11-15 16:19:11 -070049 *bflowp = bflow;
Simon Glass017656e2022-04-24 23:31:07 -060050
51 return 0;
52}
53
54int bootdev_next_bootflow(struct bootflow **bflowp)
55{
Simon Glass199f5882024-11-15 16:19:12 -070056 struct bootstd_priv *std;
Simon Glass1d8f8642024-11-15 16:19:11 -070057 struct bootflow *bflow;
Simon Glass199f5882024-11-15 16:19:12 -070058 int ret;
Simon Glass017656e2022-04-24 23:31:07 -060059
Simon Glass199f5882024-11-15 16:19:12 -070060 ret = bootstd_get_priv(&std);
61 if (ret)
62 return log_msg_ret("bff", ret);
Simon Glass017656e2022-04-24 23:31:07 -060063
Simon Glass199f5882024-11-15 16:19:12 -070064 bflow = alist_nextw(&std->bootflows, *bflowp);
Simon Glass1d8f8642024-11-15 16:19:11 -070065 if (!bflow)
Simon Glass017656e2022-04-24 23:31:07 -060066 return -ENOENT;
Simon Glass1d8f8642024-11-15 16:19:11 -070067 *bflowp = bflow;
Simon Glass017656e2022-04-24 23:31:07 -060068
69 return 0;
70}
71
72int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
73 struct udevice **devp)
74{
75 struct udevice *dev;
76 char dev_name[30];
77 char *str;
78 int ret;
79
80 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
81 str = strdup(dev_name);
82 if (!str)
83 return -ENOMEM;
84 ret = device_bind_driver(parent, drv_name, str, &dev);
85 if (ret)
86 return ret;
87 device_set_name_alloced(dev);
88 *devp = dev;
89
90 return 0;
91}
92
93int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
94 struct bootflow_iter *iter, struct bootflow *bflow)
95{
Simon Glass0fca7e82023-08-24 13:55:43 -060096 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method);
97 bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART;
Simon Glass017656e2022-04-24 23:31:07 -060098 struct blk_desc *desc = dev_get_uclass_plat(blk);
99 struct disk_partition info;
100 char partstr[20];
101 char name[60];
102 int ret;
103
104 /* Sanity check */
105 if (iter->part >= MAX_PART_PER_BOOTDEV)
106 return log_msg_ret("max", -ESHUTDOWN);
107
108 bflow->blk = blk;
109 if (iter->part)
110 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
111 else
112 strcpy(partstr, "whole");
113 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
114 bflow->name = strdup(name);
115 if (!bflow->name)
116 return log_msg_ret("name", -ENOMEM);
117
118 bflow->part = iter->part;
119
Simon Glass03fcbf92022-04-24 23:31:09 -0600120 ret = bootmeth_check(bflow->method, iter);
121 if (ret)
122 return log_msg_ret("check", ret);
123
Simon Glass017656e2022-04-24 23:31:07 -0600124 /*
125 * partition numbers start at 0 so this cannot succeed, but it can tell
126 * us whether there is valid media there
127 */
128 ret = part_get_info(desc, iter->part, &info);
Simon Glass0fca7e82023-08-24 13:55:43 -0600129 log_debug("part_get_info() returned %d\n", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600130 if (!iter->part && ret == -ENOENT)
131 ret = 0;
132
133 /*
134 * This error indicates the media is not present. Otherwise we just
135 * blindly scan the next partition. We could be more intelligent here
136 * and check which partition numbers actually exist.
137 */
138 if (ret == -EOPNOTSUPP)
139 ret = -ESHUTDOWN;
140 else
141 bflow->state = BOOTFLOWST_MEDIA;
Simon Glass0fca7e82023-08-24 13:55:43 -0600142 if (ret && !allow_any_part) {
Simon Glassa8e7d512023-04-28 13:18:09 -0600143 /* allow partition 1 to be missing */
144 if (iter->part == 1) {
145 iter->max_part = 3;
146 ret = -ENOENT;
147 }
148
Simon Glass017656e2022-04-24 23:31:07 -0600149 return log_msg_ret("part", ret);
Simon Glassa8e7d512023-04-28 13:18:09 -0600150 }
Simon Glass017656e2022-04-24 23:31:07 -0600151
152 /*
153 * Currently we don't get the number of partitions, so just
154 * assume a large number
155 */
156 iter->max_part = MAX_PART_PER_BOOTDEV;
157
Nam Caocb0d3fb2024-02-21 13:41:44 +0100158 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) {
159 /* a particular partition was specified, scan it without checking */
160 } else if (!iter->part) {
161 /* This is the whole disk, check if we have bootable partitions */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700162 iter->first_bootable = part_get_bootable(desc);
163 log_debug("checking bootable=%d\n", iter->first_bootable);
Simon Glass0fca7e82023-08-24 13:55:43 -0600164 } else if (allow_any_part) {
165 /*
166 * allow any partition to be scanned, by skipping any checks
167 * for filesystems or partition contents on this disk
168 */
Simon Glass64cbc5c2023-01-17 10:47:42 -0700169
170 /* if there are bootable partitions, scan only those */
Simon Glass0fca7e82023-08-24 13:55:43 -0600171 } else if (iter->first_bootable >= 0 &&
172 (iter->first_bootable ? !info.bootable : iter->part != 1)) {
Simon Glass64cbc5c2023-01-17 10:47:42 -0700173 return log_msg_ret("boot", -EINVAL);
174 } else {
Simon Glass017656e2022-04-24 23:31:07 -0600175 ret = fs_set_blk_dev_with_part(desc, bflow->part);
176 bflow->state = BOOTFLOWST_PART;
Simon Glassa40f7822023-04-24 13:49:49 +1200177 if (ret)
178 return log_msg_ret("fs", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600179
Simon Glass017656e2022-04-24 23:31:07 -0600180 log_debug("%s: Found partition %x type %x fstype %d\n",
Simon Glassed0ec662023-08-24 13:55:33 -0600181 blk->name, bflow->part,
182 IS_ENABLED(CONFIG_DOS_PARTITION) ?
183 disk_partition_sys_ind(&info) : 0,
Simon Glass017656e2022-04-24 23:31:07 -0600184 ret ? -1 : fs_get_type());
Simon Glassa40f7822023-04-24 13:49:49 +1200185 bflow->blk = blk;
Simon Glass017656e2022-04-24 23:31:07 -0600186 bflow->state = BOOTFLOWST_FS;
187 }
188
Simon Glass0fca7e82023-08-24 13:55:43 -0600189 log_debug("method %s\n", bflow->method->name);
Simon Glass03fcbf92022-04-24 23:31:09 -0600190 ret = bootmeth_read_bootflow(bflow->method, bflow);
191 if (ret)
192 return log_msg_ret("method", ret);
193
Simon Glass017656e2022-04-24 23:31:07 -0600194 return 0;
195}
196
197void bootdev_list(bool probe)
198{
199 struct udevice *dev;
200 int ret;
201 int i;
202
203 printf("Seq Probed Status Uclass Name\n");
204 printf("--- ------ ------ -------- ------------------\n");
205 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200206 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
Simon Glass017656e2022-04-24 23:31:07 -0600207 else
208 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
209 for (i = 0; dev; i++) {
210 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
211 device_active(dev) ? '+' : ' ',
Heinrich Schuchardt91592d62023-07-30 16:29:25 +0200212 ret ? simple_itoa(-ret) : "OK",
Simon Glass017656e2022-04-24 23:31:07 -0600213 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
214 if (probe)
Michal Suchanek675e9082022-10-12 21:57:53 +0200215 ret = uclass_next_device_check(&dev);
Simon Glass017656e2022-04-24 23:31:07 -0600216 else
217 ret = uclass_find_next_device(&dev);
218 }
219 printf("--- ------ ------ -------- ------------------\n");
220 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
221}
222
223int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
224{
225 struct udevice *bdev;
226 int ret;
227
228 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
229 &bdev);
230 if (ret) {
231 if (ret != -ENODEV) {
232 log_debug("Cannot access bootdev device\n");
233 return ret;
234 }
235
236 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
237 if (ret) {
238 log_debug("Cannot create bootdev device\n");
239 return ret;
240 }
241 }
242
243 return 0;
244}
245
Simon Glassb1dc36a2023-01-17 10:47:25 -0700246static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
247{
248 int len, slen;
249
250 len = strlen(dev->name);
251 slen = strlen(suffix);
252 if (len > slen && !strcmp(suffix, dev->name + len - slen))
253 return len - slen;
254
255 return len;
256}
257
Simon Glassb1d581d2023-07-30 11:15:14 -0600258int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
Simon Glass017656e2022-04-24 23:31:07 -0600259{
260 struct udevice *parent, *dev;
261 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700262 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600263
Simon Glassb1dc36a2023-01-17 10:47:25 -0700264 len = bootdev_get_suffix_start(blk, ".blk");
Simon Glassa73f23c2024-12-19 11:28:52 -0700265 if (xpl_phase() < PHASE_BOARD_R) {
266 strlcpy(dev_name, blk->name, sizeof(dev_name) - 5);
267 strcat(dev_name, ".sib");
268 } else {
269 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
270 "bootdev");
271 }
Simon Glass017656e2022-04-24 23:31:07 -0600272
273 parent = dev_get_parent(blk);
274 ret = device_find_child_by_name(parent, dev_name, &dev);
275 if (ret) {
276 char *str;
277
278 if (ret != -ENODEV) {
279 log_debug("Cannot access bootdev device\n");
280 return ret;
281 }
282 str = strdup(dev_name);
283 if (!str)
284 return -ENOMEM;
285
286 ret = device_bind_driver(parent, drv_name, str, &dev);
287 if (ret) {
288 log_debug("Cannot create bootdev device\n");
289 return ret;
290 }
291 device_set_name_alloced(dev);
292 }
293
294 return 0;
295}
296
297int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
298{
299 struct udevice *parent = dev_get_parent(dev);
300 struct udevice *blk;
301 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600302
303 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
304 return -EINVAL;
305
Simon Glassb1d581d2023-07-30 11:15:14 -0600306 /*
307 * This should always work if bootdev_setup_for_sibling_blk() was used
308 */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700309 len = bootdev_get_suffix_start(dev, ".bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600310 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
Simon Glassb1dc36a2023-01-17 10:47:25 -0700311 if (ret) {
312 char dev_name[50];
313
314 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
315 dev->name);
316 ret = device_find_child_by_name(parent, dev_name, &blk);
317 if (ret)
318 return log_msg_ret("find", ret);
319 }
Simon Glassb73a8292023-01-28 15:00:19 -0700320 ret = device_probe(blk);
321 if (ret)
322 return log_msg_ret("act", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600323 *blkp = blk;
324
325 return 0;
326}
327
Simon Glassc8694782024-11-15 16:19:24 -0700328int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
Simon Glass017656e2022-04-24 23:31:07 -0600329{
330 struct udevice *parent = dev_get_parent(blk);
331 struct udevice *bootdev;
332 char dev_name[50];
Simon Glassb1dc36a2023-01-17 10:47:25 -0700333 int ret, len;
Simon Glass017656e2022-04-24 23:31:07 -0600334
335 if (device_get_uclass_id(blk) != UCLASS_BLK)
336 return -EINVAL;
337
Simon Glassb1d581d2023-07-30 11:15:14 -0600338 /* This should always work if bootdev_setup_for_sibling_blk() was used */
Simon Glassb1dc36a2023-01-17 10:47:25 -0700339 len = bootdev_get_suffix_start(blk, ".blk");
340 snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
341 "bootdev");
Simon Glass017656e2022-04-24 23:31:07 -0600342 ret = device_find_child_by_name(parent, dev_name, &bootdev);
343 if (ret)
344 return log_msg_ret("find", ret);
345 *bootdevp = bootdev;
346
347 return 0;
348}
349
350int bootdev_unbind_dev(struct udevice *parent)
351{
352 struct udevice *dev;
353 int ret;
354
355 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
356 if (!ret) {
357 ret = device_remove(dev, DM_REMOVE_NORMAL);
358 if (ret)
359 return log_msg_ret("rem", ret);
360 ret = device_unbind(dev);
361 if (ret)
362 return log_msg_ret("unb", ret);
363 }
364
365 return 0;
366}
367
368/**
Simon Glass73708252023-01-17 10:48:00 -0700369 * label_to_uclass() - Convert a label to a uclass and sequence number
370 *
371 * @label: Label to look up (e.g. "mmc1" or "mmc0")
372 * @seqp: Returns the sequence number, or -1 if none
Simon Glasse22fe922023-01-17 10:48:05 -0700373 * @method_flagsp: If non-NULL, returns any flags implied by the label
374 * (enum bootflow_meth_flags_t), 0 if none
Simon Glass4108c6b2023-04-24 13:49:47 +1200375 * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
376 * known, other -ve error code on other error
Simon Glass73708252023-01-17 10:48:00 -0700377 */
Simon Glasse22fe922023-01-17 10:48:05 -0700378static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
Simon Glass73708252023-01-17 10:48:00 -0700379{
Simon Glasse22fe922023-01-17 10:48:05 -0700380 int seq, len, method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700381 enum uclass_id id;
382 const char *end;
Simon Glass73708252023-01-17 10:48:00 -0700383
Simon Glasse22fe922023-01-17 10:48:05 -0700384 method_flags = 0;
Simon Glass73708252023-01-17 10:48:00 -0700385 seq = trailing_strtoln_end(label, NULL, &end);
386 len = end - label;
387 if (!len)
388 return -EINVAL;
389 id = uclass_get_by_namelen(label, len);
390 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
391 uclass_get_name(id));
392 if (id == UCLASS_INVALID) {
Simon Glass215be682023-01-17 10:48:03 -0700393 /* try some special cases */
394 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
395 !strncmp("spi", label, len)) {
396 id = UCLASS_SPI_FLASH;
Simon Glasse22fe922023-01-17 10:48:05 -0700397 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
398 !strncmp("pxe", label, len)) {
399 id = UCLASS_ETH;
400 method_flags |= BOOTFLOW_METHF_PXE_ONLY;
401 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
402 !strncmp("dhcp", label, len)) {
403 id = UCLASS_ETH;
404 method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
Simon Glass215be682023-01-17 10:48:03 -0700405 } else {
Simon Glass4108c6b2023-04-24 13:49:47 +1200406 return -EPFNOSUPPORT;
Simon Glass215be682023-01-17 10:48:03 -0700407 }
Simon Glass73708252023-01-17 10:48:00 -0700408 }
409 if (id == UCLASS_USB)
410 id = UCLASS_MASS_STORAGE;
411 *seqp = seq;
Simon Glasse22fe922023-01-17 10:48:05 -0700412 if (method_flagsp)
413 *method_flagsp = method_flags;
Simon Glass73708252023-01-17 10:48:00 -0700414
415 return id;
416}
417
Simon Glasse22fe922023-01-17 10:48:05 -0700418int bootdev_find_by_label(const char *label, struct udevice **devp,
419 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600420{
Simon Glasse22fe922023-01-17 10:48:05 -0700421 int seq, ret, method_flags = 0;
Simon Glass017656e2022-04-24 23:31:07 -0600422 struct udevice *media;
423 struct uclass *uc;
424 enum uclass_id id;
Simon Glass017656e2022-04-24 23:31:07 -0600425
Simon Glass1d809642024-09-01 16:27:28 -0600426 if (!CONFIG_IS_ENABLED(BLK))
427 return -ENOSYS;
428
Simon Glasse22fe922023-01-17 10:48:05 -0700429 ret = label_to_uclass(label, &seq, &method_flags);
Simon Glass73708252023-01-17 10:48:00 -0700430 if (ret < 0)
431 return log_msg_ret("uc", ret);
432 id = ret;
Simon Glass017656e2022-04-24 23:31:07 -0600433
434 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
435 uclass_id_foreach_dev(id, media, uc) {
436 struct udevice *bdev, *blk;
437 int ret;
438
439 /* if there is no seq, match anything */
440 if (seq != -1 && dev_seq(media) != seq) {
441 log_debug("- skip, media seq=%d\n", dev_seq(media));
442 continue;
443 }
444
445 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
446 &bdev);
447 if (ret) {
448 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
449 id);
450 ret = blk_find_device(id, seq, &blk);
451 if (!ret) {
452 log_debug("- get from blk %s\n", blk->name);
453 ret = bootdev_get_from_blk(blk, &bdev);
454 }
455 }
456 if (!ret) {
457 log_debug("- found %s\n", bdev->name);
458 *devp = bdev;
Simon Glassde567b12023-01-17 10:48:09 -0700459
460 /*
461 * if no sequence number was provided, we must scan all
462 * bootdevs for this media uclass
463 */
Simon Glass60bcbf02023-09-23 14:50:15 -0600464 if (seq == -1)
Simon Glassde567b12023-01-17 10:48:09 -0700465 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
Simon Glasse22fe922023-01-17 10:48:05 -0700466 if (method_flagsp)
467 *method_flagsp = method_flags;
Simon Glass60bcbf02023-09-23 14:50:15 -0600468 log_debug("method flags %x\n", method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600469 return 0;
470 }
471 log_debug("- no device in %s\n", media->name);
472 }
Simon Glass017656e2022-04-24 23:31:07 -0600473
474 return -ENOENT;
475}
476
Simon Glasse22fe922023-01-17 10:48:05 -0700477int bootdev_find_by_any(const char *name, struct udevice **devp,
478 int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600479{
480 struct udevice *dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700481 int method_flags = 0;
Simon Glassde567b12023-01-17 10:48:09 -0700482 int ret = -ENODEV, seq;
Simon Glass017656e2022-04-24 23:31:07 -0600483 char *endp;
484
485 seq = simple_strtol(name, &endp, 16);
486
487 /* Select by name, label or number */
488 if (*endp) {
489 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
490 if (ret == -ENODEV) {
Simon Glasse22fe922023-01-17 10:48:05 -0700491 ret = bootdev_find_by_label(name, &dev, &method_flags);
Simon Glass017656e2022-04-24 23:31:07 -0600492 if (ret) {
493 printf("Cannot find bootdev '%s' (err=%d)\n",
494 name, ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700495 return log_msg_ret("lab", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600496 }
497 ret = device_probe(dev);
498 }
499 if (ret) {
500 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
501 ret);
Simon Glasse22fe922023-01-17 10:48:05 -0700502 return log_msg_ret("pro", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600503 }
Simon Glassde567b12023-01-17 10:48:09 -0700504 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
Simon Glass017656e2022-04-24 23:31:07 -0600505 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
Simon Glassde567b12023-01-17 10:48:09 -0700506 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
Simon Glass017656e2022-04-24 23:31:07 -0600507 }
508 if (ret) {
509 printf("Cannot find '%s' (err=%d)\n", name, ret);
510 return ret;
511 }
512
513 *devp = dev;
Simon Glasse22fe922023-01-17 10:48:05 -0700514 if (method_flagsp)
515 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600516
517 return 0;
518}
519
Simon Glassde567b12023-01-17 10:48:09 -0700520int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
521 int *method_flagsp)
522{
523 int ret;
524
525 ret = bootdev_hunt(label, false);
526 if (ret)
527 return log_msg_ret("scn", ret);
528 ret = bootdev_find_by_label(label, devp, method_flagsp);
529 if (ret)
530 return log_msg_ret("fnd", ret);
531
532 return 0;
533}
534
Simon Glassd6e39d12023-01-17 10:47:26 -0700535static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
536 struct bootflow *bflow)
537{
538 struct udevice *blk;
539 int ret;
540
541 ret = bootdev_get_sibling_blk(dev, &blk);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600542 log_debug("sibling_blk ret=%d, blk=%s\n", ret,
543 ret ? "(none)" : blk->name);
Simon Glassd6e39d12023-01-17 10:47:26 -0700544 /*
545 * If there is no media, indicate that no more partitions should be
546 * checked
547 */
548 if (ret == -EOPNOTSUPP)
549 ret = -ESHUTDOWN;
550 if (ret)
551 return log_msg_ret("blk", ret);
552 assert(blk);
553 ret = bootdev_find_in_blk(dev, blk, iter, bflow);
554 if (ret)
555 return log_msg_ret("find", ret);
556
557 return 0;
558}
559
Simon Glass017656e2022-04-24 23:31:07 -0600560int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
561 struct bootflow *bflow)
562{
563 const struct bootdev_ops *ops = bootdev_get_ops(dev);
564
Simon Glass0fca7e82023-08-24 13:55:43 -0600565 log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
566 ops->get_bootflow);
Simon Glass00501fc2022-10-20 18:22:51 -0600567 bootflow_init(bflow, dev, iter->method);
Simon Glassd6e39d12023-01-17 10:47:26 -0700568 if (!ops->get_bootflow)
569 return default_get_bootflow(dev, iter, bflow);
Simon Glass017656e2022-04-24 23:31:07 -0600570
571 return ops->get_bootflow(dev, iter, bflow);
572}
573
Simon Glassac06b26a2023-01-17 10:48:10 -0700574int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
575 int *method_flagsp)
576{
577 struct udevice *dev;
578
579 log_debug("next\n");
580 for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
Simon Glass4108c6b2023-04-24 13:49:47 +1200581 const char *label = iter->labels[iter->cur_label];
582 int ret;
583
584 log_debug("Scanning: %s\n", label);
585 ret = bootdev_hunt_and_find_by_label(label, &dev,
586 method_flagsp);
587 if (iter->flags & BOOTFLOWIF_SHOW) {
588 if (ret == -EPFNOSUPPORT) {
589 log_warning("Unknown uclass '%s' in label\n",
590 label);
591 } else if (ret == -ENOENT) {
592 /*
593 * looking for, e.g. 'scsi0' should find
594 * something if SCSI is present
595 */
596 if (!trailing_strtol(label)) {
597 log_warning("No bootdevs for '%s'\n",
598 label);
599 }
600 }
601 }
602
Simon Glassac06b26a2023-01-17 10:48:10 -0700603 }
604
605 if (!dev)
606 return log_msg_ret("fin", -ENODEV);
607 *devp = dev;
608
609 return 0;
610}
611
Simon Glass660a9952023-01-17 10:48:11 -0700612int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
613{
Simon Glass15f128a2024-08-15 14:30:21 -0600614 struct udevice *dev = *devp;
Simon Glass660a9952023-01-17 10:48:11 -0700615 bool found;
616 int ret;
617
618 /* find the next device with this priority */
619 *devp = NULL;
620 log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
621 dev ? dev->name : "none");
Simon Glass5aaa5622024-08-15 14:30:22 -0600622 found = false;
Simon Glass660a9952023-01-17 10:48:11 -0700623 do {
624 /*
625 * Don't probe devices here since they may not be of the
626 * required priority
627 */
628 if (!dev)
629 uclass_find_first_device(UCLASS_BOOTDEV, &dev);
630 else
631 uclass_find_next_device(&dev);
632 found = false;
633
634 /* scan for the next device with the correct priority */
635 while (dev) {
636 struct bootdev_uc_plat *plat;
637
638 plat = dev_get_uclass_plat(dev);
639 log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
640 iter->cur_prio);
641 if (plat->prio == iter->cur_prio)
642 break;
643 uclass_find_next_device(&dev);
644 }
645
646 /* none found for this priority, so move to the next */
647 if (!dev) {
648 log_debug("None found at prio %d, moving to %d\n",
649 iter->cur_prio, iter->cur_prio + 1);
650 if (++iter->cur_prio == BOOTDEVP_COUNT)
651 return log_msg_ret("fin", -ENODEV);
652
Simon Glass99e68182023-02-22 12:17:03 -0700653 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass660a9952023-01-17 10:48:11 -0700654 /* hunt to find new bootdevs */
655 ret = bootdev_hunt_prio(iter->cur_prio,
656 iter->flags &
Simon Glass99e68182023-02-22 12:17:03 -0700657 BOOTFLOWIF_SHOW);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600658 log_debug("- bootdev_hunt_prio() ret %d\n",
659 ret);
Simon Glass660a9952023-01-17 10:48:11 -0700660 if (ret)
661 return log_msg_ret("hun", ret);
662 }
663 } else {
664 ret = device_probe(dev);
Simon Glass5aaa5622024-08-15 14:30:22 -0600665 if (ret)
Simon Glass15f128a2024-08-15 14:30:21 -0600666 log_debug("Device '%s' failed to probe\n",
Simon Glass660a9952023-01-17 10:48:11 -0700667 dev->name);
Simon Glass5aaa5622024-08-15 14:30:22 -0600668 else
669 found = true;
Simon Glass660a9952023-01-17 10:48:11 -0700670 }
Simon Glass5aaa5622024-08-15 14:30:22 -0600671 } while (!found);
Simon Glass660a9952023-01-17 10:48:11 -0700672
673 *devp = dev;
674
675 return 0;
676}
677
Simon Glassba3d5372023-01-17 10:48:15 -0700678int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
679 struct udevice **devp, int *method_flagsp)
Simon Glass017656e2022-04-24 23:31:07 -0600680{
Simon Glassba3d5372023-01-17 10:48:15 -0700681 struct udevice *bootstd, *dev = NULL;
Simon Glass99e68182023-02-22 12:17:03 -0700682 bool show = iter->flags & BOOTFLOWIF_SHOW;
Simon Glass484e4072023-01-17 10:48:14 -0700683 int method_flags;
Nam Caocb0d3fb2024-02-21 13:41:44 +0100684 char buf[32];
Simon Glass017656e2022-04-24 23:31:07 -0600685 int ret;
686
Nam Caocb0d3fb2024-02-21 13:41:44 +0100687 if (label) {
688 const char *end = strchr(label, ':');
689
690 if (end) {
691 size_t len = (size_t)(end - label);
692 const char *part = end + 1;
693
694 if (len + 1 > sizeof(buf)) {
695 log_err("label \"%s\" is way too long\n", label);
696 return -EINVAL;
697 }
698
699 memcpy(buf, label, len);
700 buf[len] = '\0';
701 label = buf;
702
703 unsigned long tmp;
704
705 if (strict_strtoul(part, 0, &tmp)) {
706 log_err("Invalid partition number: %s\n", part);
707 return -EINVAL;
708 }
709
710 iter->flags |= BOOTFLOWIF_SINGLE_PARTITION;
711 iter->part = tmp;
712 }
713 }
714
Simon Glass017656e2022-04-24 23:31:07 -0600715 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
716 if (ret) {
717 log_err("Missing bootstd device\n");
718 return log_msg_ret("std", ret);
719 }
720
Simon Glass7e1f6a42023-01-17 10:48:08 -0700721 /* hunt for any pre-scan devices */
Simon Glass99e68182023-02-22 12:17:03 -0700722 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glass7e1f6a42023-01-17 10:48:08 -0700723 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600724 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
Simon Glass7e1f6a42023-01-17 10:48:08 -0700725 if (ret)
726 return log_msg_ret("pre", ret);
727 }
728
Simon Glass017656e2022-04-24 23:31:07 -0600729 /* Handle scanning a single device */
Simon Glassba3d5372023-01-17 10:48:15 -0700730 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
Simon Glass99e68182023-02-22 12:17:03 -0700731 if (iter->flags & BOOTFLOWIF_HUNT) {
Simon Glassba3d5372023-01-17 10:48:15 -0700732 ret = bootdev_hunt(label, show);
733 if (ret)
734 return log_msg_ret("hun", ret);
735 }
736 ret = bootdev_find_by_any(label, &dev, &method_flags);
737 if (ret)
738 return log_msg_ret("lab", ret);
739
740 log_debug("method_flags: %x\n", method_flags);
741 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
Simon Glass99e68182023-02-22 12:17:03 -0700742 iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
Simon Glassba3d5372023-01-17 10:48:15 -0700743 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
Simon Glass99e68182023-02-22 12:17:03 -0700744 iter->flags |= BOOTFLOWIF_SINGLE_DEV;
Simon Glassba3d5372023-01-17 10:48:15 -0700745 else
Simon Glass99e68182023-02-22 12:17:03 -0700746 iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
Simon Glassba3d5372023-01-17 10:48:15 -0700747 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
Simon Glass484e4072023-01-17 10:48:14 -0700748 } else {
749 bool ok;
Simon Glass017656e2022-04-24 23:31:07 -0600750
Simon Glass484e4072023-01-17 10:48:14 -0700751 /* This either returns a non-empty list or NULL */
752 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
753 if (!ok)
754 return log_msg_ret("ord", -ENOMEM);
755 log_debug("setup labels %p\n", iter->labels);
756 if (iter->labels) {
757 iter->cur_label = -1;
758 ret = bootdev_next_label(iter, &dev, &method_flags);
759 } else {
760 ret = bootdev_next_prio(iter, &dev);
761 method_flags = 0;
762 }
763 if (!dev)
764 return log_msg_ret("fin", -ENOENT);
765 log_debug("Selected bootdev: %s\n", dev->name);
Simon Glass017656e2022-04-24 23:31:07 -0600766 }
767
Simon Glass017656e2022-04-24 23:31:07 -0600768 ret = device_probe(dev);
769 if (ret)
770 return log_msg_ret("probe", ret);
Simon Glass484e4072023-01-17 10:48:14 -0700771 if (method_flagsp)
772 *method_flagsp = method_flags;
Simon Glass017656e2022-04-24 23:31:07 -0600773 *devp = dev;
774
775 return 0;
776}
777
Simon Glass1248d2b2023-01-17 10:47:34 -0700778static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
779{
780 const char *name = uclass_get_name(info->uclass);
781 struct bootstd_priv *std;
782 int ret;
783
784 ret = bootstd_get_priv(&std);
785 if (ret)
786 return log_msg_ret("std", ret);
787
788 if (!(std->hunters_used & BIT(seq))) {
789 if (show)
790 printf("Hunting with: %s\n",
791 uclass_get_name(info->uclass));
792 log_debug("Hunting with: %s\n", name);
793 if (info->hunt) {
794 ret = info->hunt(info, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600795 log_debug(" - hunt result %d\n", ret);
Tony Dinh81356b42023-11-02 11:51:15 -0700796 if (ret && ret != -ENOENT)
Simon Glass1248d2b2023-01-17 10:47:34 -0700797 return ret;
798 }
799 std->hunters_used |= BIT(seq);
800 }
801
802 return 0;
803}
804
805int bootdev_hunt(const char *spec, bool show)
806{
807 struct bootdev_hunter *start;
808 const char *end;
809 int n_ent, i;
810 int result;
811 size_t len;
812
813 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
814 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
815 result = 0;
816
817 len = SIZE_MAX;
818 if (spec) {
819 trailing_strtoln_end(spec, NULL, &end);
820 len = end - spec;
821 }
822
823 for (i = 0; i < n_ent; i++) {
824 struct bootdev_hunter *info = start + i;
825 const char *name = uclass_get_name(info->uclass);
826 int ret;
827
828 log_debug("looking at %.*s for %s\n",
829 (int)max(strlen(name), len), spec, name);
Simon Glassac06b26a2023-01-17 10:48:10 -0700830 if (spec && strncmp(spec, name, max(strlen(name), len))) {
831 if (info->uclass != UCLASS_ETH ||
832 (strcmp("dhcp", spec) && strcmp("pxe", spec)))
833 continue;
834 }
Simon Glass1248d2b2023-01-17 10:47:34 -0700835 ret = bootdev_hunt_drv(info, i, show);
836 if (ret)
837 result = ret;
838 }
839
840 return result;
841}
842
Simon Glass62b03cc2023-09-20 07:29:49 -0600843int bootdev_unhunt(enum uclass_id id)
844{
845 struct bootdev_hunter *start;
846 int n_ent, i;
847
848 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
849 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
850 for (i = 0; i < n_ent; i++) {
851 struct bootdev_hunter *info = start + i;
852
853 if (info->uclass == id) {
854 struct bootstd_priv *std;
855 int ret;
856
857 ret = bootstd_get_priv(&std);
858 if (ret)
859 return log_msg_ret("std", ret);
860 if (!(std->hunters_used & BIT(i)))
861 return -EALREADY;
862 std->hunters_used &= ~BIT(i);
863 return 0;
864 }
865 }
866
867 return -ENOENT;
868}
869
Simon Glass3e9f6be2023-01-17 10:48:07 -0700870int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
871{
872 struct bootdev_hunter *start;
873 int n_ent, i;
874 int result;
875
876 start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
877 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
878 result = 0;
879
880 log_debug("Hunting for priority %d\n", prio);
881 for (i = 0; i < n_ent; i++) {
882 struct bootdev_hunter *info = start + i;
883 int ret;
884
885 if (prio != info->prio)
886 continue;
887 ret = bootdev_hunt_drv(info, i, show);
Simon Glassbf2d02a2023-07-30 11:15:16 -0600888 log_debug("bootdev_hunt_drv() return %d\n", ret);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700889 if (ret && ret != -ENOENT)
890 result = ret;
891 }
Simon Glassbf2d02a2023-07-30 11:15:16 -0600892 log_debug("exit %d\n", result);
Simon Glass3e9f6be2023-01-17 10:48:07 -0700893
894 return result;
895}
896
Simon Glass5acb97a2023-01-17 10:47:33 -0700897void bootdev_list_hunters(struct bootstd_priv *std)
898{
899 struct bootdev_hunter *orig, *start;
900 int n_ent, i;
901
902 orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
903 n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
904
905 /*
906 * workaround for strange bug in clang-12 which sees all the below data
907 * as zeroes. Any access of start seems to fix it, such as
908 *
909 * printf("%p", start);
910 *
911 * Use memcpy() to force the correct behaviour.
912 */
913 memcpy(&start, &orig, sizeof(orig));
914 printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
915 printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
916 for (i = 0; i < n_ent; i++) {
917 struct bootdev_hunter *info = start + i;
918
919 printf("%4d %4s %-15s %s\n", info->prio,
920 std->hunters_used & BIT(i) ? "*" : "",
921 uclass_get_name(info->uclass),
922 info->drv ? info->drv->name : "(none)");
923 }
924
925 printf("(total hunters: %d)\n", n_ent);
926}
927
Simon Glass017656e2022-04-24 23:31:07 -0600928static int bootdev_pre_unbind(struct udevice *dev)
929{
Simon Glass346ab5d2024-11-15 16:19:09 -0700930 int ret;
931
932 ret = bootstd_clear_bootflows_for_bootdev(dev);
933 if (ret)
934 return log_msg_ret("bun", ret);
Simon Glass017656e2022-04-24 23:31:07 -0600935
936 return 0;
937}
938
939UCLASS_DRIVER(bootdev) = {
940 .id = UCLASS_BOOTDEV,
941 .name = "bootdev",
942 .flags = DM_UC_FLAG_SEQ_ALIAS,
943 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
Simon Glass017656e2022-04-24 23:31:07 -0600944 .pre_unbind = bootdev_pre_unbind,
945};