]> Git Repo - J-u-boot.git/blame - boot/bootflow.c
bootstd: Allow scanning a single bootdev label
[J-u-boot.git] / boot / bootflow.c
CommitLineData
a8f5be17
SG
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <[email protected]>
5 */
6
7#define LOG_CATEGORY UCLASS_BOOTSTD
8
9#include <common.h>
10#include <bootdev.h>
11#include <bootflow.h>
12#include <bootmeth.h>
13#include <bootstd.h>
14#include <dm.h>
15#include <malloc.h>
16#include <dm/device-internal.h>
17#include <dm/uclass-internal.h>
18
19/* error codes used to signal running out of things */
20enum {
21 BF_NO_MORE_PARTS = -ESHUTDOWN,
22 BF_NO_MORE_DEVICES = -ENODEV,
23};
24
25/**
26 * bootflow_state - name for each state
27 *
28 * See enum bootflow_state_t for what each of these means
29 */
30static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
31 "base",
32 "media",
33 "part",
34 "fs",
35 "file",
36 "ready",
37};
38
39const char *bootflow_state_get_name(enum bootflow_state_t state)
40{
41 /* This doesn't need to be a useful name, since it will never occur */
42 if (state < 0 || state >= BOOTFLOWST_COUNT)
43 return "?";
44
45 return bootflow_state[state];
46}
47
48int bootflow_first_glob(struct bootflow **bflowp)
49{
50 struct bootstd_priv *std;
51 int ret;
52
53 ret = bootstd_get_priv(&std);
54 if (ret)
55 return ret;
56
57 if (list_empty(&std->glob_head))
58 return -ENOENT;
59
60 *bflowp = list_first_entry(&std->glob_head, struct bootflow,
61 glob_node);
62
63 return 0;
64}
65
66int bootflow_next_glob(struct bootflow **bflowp)
67{
68 struct bootstd_priv *std;
69 struct bootflow *bflow = *bflowp;
70 int ret;
71
72 ret = bootstd_get_priv(&std);
73 if (ret)
74 return ret;
75
76 *bflowp = NULL;
77
78 if (list_is_last(&bflow->glob_node, &std->glob_head))
79 return -ENOENT;
80
81 *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
82
83 return 0;
84}
85
86void bootflow_iter_init(struct bootflow_iter *iter, int flags)
87{
88 memset(iter, '\0', sizeof(*iter));
2b80bc1e 89 iter->first_glob_method = -1;
a8f5be17
SG
90 iter->flags = flags;
91}
92
93void bootflow_iter_uninit(struct bootflow_iter *iter)
94{
a8f5be17
SG
95 free(iter->method_order);
96}
97
98int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
99 const struct udevice *bmeth)
100{
101 /* We only support disabling the current bootmeth */
102 if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
103 iter->method_order[iter->cur_method] != bmeth)
104 return -EINVAL;
105
106 memmove(&iter->method_order[iter->cur_method],
107 &iter->method_order[iter->cur_method + 1],
108 (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
109
110 iter->num_methods--;
111
112 return 0;
113}
114
47aedc29
SG
115/**
116 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
117 *
118 * This sets iter->dev, records the device in the dev_used[] list and shows a
119 * message if required
120 *
121 * @iter: Iterator to update
122 * @dev: Bootdev to use, or NULL if there are no more
123 */
a8f5be17 124static void bootflow_iter_set_dev(struct bootflow_iter *iter,
47aedc29 125 struct udevice *dev, int method_flags)
a8f5be17 126{
2b80bc1e
SG
127 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
128
47aedc29
SG
129 log_debug("iter: Setting dev to %s, flags %x\n",
130 dev ? dev->name : "(none)", method_flags);
a8f5be17 131 iter->dev = dev;
47aedc29
SG
132 iter->method_flags = method_flags;
133
a8f5be17
SG
134 if ((iter->flags & (BOOTFLOWF_SHOW | BOOTFLOWF_SINGLE_DEV)) ==
135 BOOTFLOWF_SHOW) {
136 if (dev)
137 printf("Scanning bootdev '%s':\n", dev->name);
2b80bc1e
SG
138 else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
139 ucp->flags & BOOTMETHF_GLOBAL)
140 printf("Scanning global bootmeth '%s':\n",
141 iter->method->name);
a8f5be17
SG
142 else
143 printf("No more bootdevs\n");
144 }
145}
146
147/**
148 * iter_incr() - Move to the next item (method, part, bootdev)
149 *
150 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
151 */
152static int iter_incr(struct bootflow_iter *iter)
153{
154 struct udevice *dev;
2b80bc1e
SG
155 bool inc_dev = true;
156 bool global;
a8f5be17
SG
157 int ret;
158
47aedc29 159 log_debug("entry: err=%d\n", iter->err);
2b80bc1e
SG
160 global = iter->doing_global;
161
a8f5be17
SG
162 if (iter->err == BF_NO_MORE_DEVICES)
163 return BF_NO_MORE_DEVICES;
164
165 if (iter->err != BF_NO_MORE_PARTS) {
166 /* Get the next boothmethod */
167 if (++iter->cur_method < iter->num_methods) {
168 iter->method = iter->method_order[iter->cur_method];
169 return 0;
170 }
2b80bc1e
SG
171
172 /*
173 * If we have finished scanning the global bootmeths, start the
174 * normal bootdev scan
175 */
176 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
177 iter->num_methods = iter->first_glob_method;
178 iter->doing_global = false;
179
180 /*
181 * Don't move to the next dev as we haven't tried this
182 * one yet!
183 */
184 inc_dev = false;
185 }
a8f5be17
SG
186 }
187
188 /* No more bootmeths; start at the first one, and... */
189 iter->cur_method = 0;
190 iter->method = iter->method_order[iter->cur_method];
191
192 if (iter->err != BF_NO_MORE_PARTS) {
193 /* ...select next partition */
194 if (++iter->part <= iter->max_part)
195 return 0;
196 }
197
47aedc29 198 /* No more partitions; start at the first one and... */
a8f5be17
SG
199 iter->part = 0;
200
201 /*
202 * Note: as far as we know, there is no partition table on the next
203 * bootdev, so set max_part to 0 until we discover otherwise. See
204 * bootdev_find_in_blk() for where this is set.
205 */
206 iter->max_part = 0;
207
208 /* ...select next bootdev */
209 if (iter->flags & BOOTFLOWF_SINGLE_DEV) {
210 ret = -ENOENT;
a8f5be17 211 } else {
47aedc29
SG
212 int method_flags;
213
214 ret = 0;
215 dev = iter->dev;
216 log_debug("inc_dev=%d\n", inc_dev);
217 if (!inc_dev) {
91943ff7
SG
218 ret = bootdev_setup_iter(iter, NULL, &dev,
219 &method_flags);
220 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
221 (iter->flags & BOOTFLOWF_SINGLE_UCLASS)) {
222 /* Move to the next bootdev in this uclass */
223 uclass_find_next_device(&dev);
224 if (!dev) {
225 log_debug("finished uclass %s\n",
226 dev_get_uclass_name(dev));
227 ret = -ENODEV;
228 }
229 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
230 iter->flags & BOOTFLOWF_SINGLE_MEDIA) {
231 log_debug("next in single\n");
232 method_flags = 0;
233 do {
234 /*
235 * Move to the next bootdev child of this media
236 * device. This ensures that we cover all the
237 * available SCSI IDs and LUNs.
238 */
239 device_find_next_child(&dev);
240 log_debug("- next %s\n",
241 dev ? dev->name : "(none)");
242 } while (dev && device_get_uclass_id(dev) !=
243 UCLASS_BOOTDEV);
244 if (!dev) {
245 log_debug("finished uclass %s\n",
246 dev_get_uclass_name(dev));
247 ret = -ENODEV;
248 }
47aedc29
SG
249 } else {
250 log_debug("labels %p\n", iter->labels);
251 if (iter->labels) {
252 ret = bootdev_next_label(iter, &dev,
253 &method_flags);
254 } else {
255 ret = bootdev_next_prio(iter, &dev);
256 method_flags = 0;
257 }
258 }
259 log_debug("ret=%d, dev=%p %s\n", ret, dev,
260 dev ? dev->name : "none");
261 if (ret) {
262 bootflow_iter_set_dev(iter, NULL, 0);
2b80bc1e 263 } else {
2b80bc1e 264 ret = device_probe(dev);
47aedc29 265 log_debug("probe %s %d\n", dev->name, ret);
2b80bc1e 266 if (!log_msg_ret("probe", ret))
47aedc29 267 bootflow_iter_set_dev(iter, dev, method_flags);
2b80bc1e 268 }
a8f5be17
SG
269 }
270
271 /* if there are no more bootdevs, give up */
272 if (ret)
273 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
274
275 return 0;
276}
277
278/**
279 * bootflow_check() - Check if a bootflow can be obtained
280 *
281 * @iter: Provides part, bootmeth to use
282 * @bflow: Bootflow to update on success
283 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
284 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
285 */
286static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
287{
288 struct udevice *dev;
289 int ret;
290
2b80bc1e 291 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
47aedc29 292 bootflow_iter_set_dev(iter, NULL, 0);
2b80bc1e
SG
293 ret = bootmeth_get_bootflow(iter->method, bflow);
294 if (ret)
295 return log_msg_ret("glob", ret);
296
297 return 0;
298 }
299
a8f5be17
SG
300 dev = iter->dev;
301 ret = bootdev_get_bootflow(dev, iter, bflow);
302
303 /* If we got a valid bootflow, return it */
304 if (!ret) {
305 log_debug("Bootdevice '%s' part %d method '%s': Found bootflow\n",
306 dev->name, iter->part, iter->method->name);
307 return 0;
308 }
309
310 /* Unless there is nothing more to try, move to the next device */
311 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
312 log_debug("Bootdevice '%s' part %d method '%s': Error %d\n",
313 dev->name, iter->part, iter->method->name, ret);
314 /*
315 * For 'all' we return all bootflows, even
316 * those with errors
317 */
318 if (iter->flags & BOOTFLOWF_ALL)
319 return log_msg_ret("all", ret);
320 }
321 if (ret)
322 return log_msg_ret("check", ret);
323
324 return 0;
325}
326
91943ff7
SG
327int bootflow_scan_bootdev(struct udevice *dev, const char *label,
328 struct bootflow_iter *iter, int flags,
329 struct bootflow *bflow)
a8f5be17
SG
330{
331 int ret;
332
91943ff7 333 if (dev || label)
2b80bc1e 334 flags |= BOOTFLOWF_SKIP_GLOBAL;
a8f5be17
SG
335 bootflow_iter_init(iter, flags);
336
47aedc29
SG
337 /*
338 * Set up the ordering of bootmeths. This sets iter->doing_global and
339 * iter->first_glob_method if we are starting with the global bootmeths
340 */
c627cfc1 341 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWF_SKIP_GLOBAL));
a8f5be17
SG
342 if (ret)
343 return log_msg_ret("obmeth", -ENODEV);
344
345 /* Find the first bootmeth (there must be at least one!) */
346 iter->method = iter->method_order[iter->cur_method];
47aedc29
SG
347
348 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
349 struct udevice *dev = NULL;
350 int method_flags;
351
91943ff7 352 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
47aedc29
SG
353 if (ret)
354 return log_msg_ret("obdev", -ENODEV);
355
356 bootflow_iter_set_dev(iter, dev, method_flags);
357 }
a8f5be17
SG
358
359 ret = bootflow_check(iter, bflow);
360 if (ret) {
361 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
362 if (iter->flags & BOOTFLOWF_ALL)
363 return log_msg_ret("all", ret);
364 }
365 iter->err = ret;
366 ret = bootflow_scan_next(iter, bflow);
367 if (ret)
368 return log_msg_ret("get", ret);
369 }
370
371 return 0;
372}
373
374int bootflow_scan_first(struct bootflow_iter *iter, int flags,
375 struct bootflow *bflow)
376{
377 int ret;
378
91943ff7 379 ret = bootflow_scan_bootdev(NULL, NULL, iter, flags, bflow);
a8f5be17
SG
380 if (ret)
381 return log_msg_ret("start", ret);
382
383 return 0;
384}
385
386int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
387{
388 int ret;
389
390 do {
391 ret = iter_incr(iter);
392 if (ret == BF_NO_MORE_DEVICES)
393 return log_msg_ret("done", ret);
394
395 if (!ret) {
396 ret = bootflow_check(iter, bflow);
397 if (!ret)
398 return 0;
399 iter->err = ret;
400 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
401 if (iter->flags & BOOTFLOWF_ALL)
402 return log_msg_ret("all", ret);
403 }
404 } else {
405 iter->err = ret;
406 }
407
408 } while (1);
409}
410
b190deb8
SG
411void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
412 struct udevice *meth)
413{
414 memset(bflow, '\0', sizeof(*bflow));
415 bflow->dev = bootdev;
416 bflow->method = meth;
417 bflow->state = BOOTFLOWST_BASE;
418}
419
a8f5be17
SG
420void bootflow_free(struct bootflow *bflow)
421{
422 free(bflow->name);
423 free(bflow->subdir);
424 free(bflow->fname);
425 free(bflow->buf);
2175e76a 426 free(bflow->os_name);
7638c851 427 free(bflow->fdt_fname);
a8f5be17
SG
428}
429
430void bootflow_remove(struct bootflow *bflow)
431{
eccb25cd
SG
432 if (bflow->dev)
433 list_del(&bflow->bm_node);
a8f5be17
SG
434 list_del(&bflow->glob_node);
435
436 bootflow_free(bflow);
437 free(bflow);
438}
439
440int bootflow_boot(struct bootflow *bflow)
441{
442 int ret;
443
444 if (bflow->state != BOOTFLOWST_READY)
445 return log_msg_ret("load", -EPROTO);
446
447 ret = bootmeth_boot(bflow->method, bflow);
448 if (ret)
449 return log_msg_ret("boot", ret);
450
451 /*
452 * internal error, should not get here since we should have booted
453 * something or returned an error
454 */
455
456 return log_msg_ret("end", -EFAULT);
457}
458
459int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
460{
461 int ret;
462
463 printf("** Booting bootflow '%s' with %s\n", bflow->name,
464 bflow->method->name);
465 ret = bootflow_boot(bflow);
466 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
467 printf("Boot failed (err=%d)\n", ret);
468 return ret;
469 }
470
471 switch (ret) {
472 case -EPROTO:
473 printf("Bootflow not loaded (state '%s')\n",
474 bootflow_state_get_name(bflow->state));
475 break;
476 case -ENOSYS:
477 printf("Boot method '%s' not supported\n", bflow->method->name);
478 break;
479 case -ENOTSUPP:
480 /* Disable this bootflow for this iteration */
481 if (iter) {
482 int ret2;
483
484 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
485 if (!ret2) {
486 printf("Boot method '%s' failed and will not be retried\n",
487 bflow->method->name);
488 }
489 }
490
491 break;
492 default:
493 printf("Boot failed (err=%d)\n", ret);
494 break;
495 }
496
497 return ret;
498}
499
865328c3 500int bootflow_iter_check_blk(const struct bootflow_iter *iter)
a8f5be17
SG
501{
502 const struct udevice *media = dev_get_parent(iter->dev);
503 enum uclass_id id = device_get_uclass_id(media);
504
505 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
506 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD)
507 return 0;
508
509 return -ENOTSUPP;
510}
511
0c1f4a9f
SG
512int bootflow_iter_check_sf(const struct bootflow_iter *iter)
513{
514 const struct udevice *media = dev_get_parent(iter->dev);
515 enum uclass_id id = device_get_uclass_id(media);
516
517 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
518 if (id == UCLASS_SPI_FLASH)
519 return 0;
520
521 return -ENOTSUPP;
522}
523
865328c3 524int bootflow_iter_check_net(const struct bootflow_iter *iter)
a8f5be17
SG
525{
526 const struct udevice *media = dev_get_parent(iter->dev);
527 enum uclass_id id = device_get_uclass_id(media);
528
529 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
530 if (id == UCLASS_ETH)
531 return 0;
532
533 return -ENOTSUPP;
534}
535
865328c3 536int bootflow_iter_check_system(const struct bootflow_iter *iter)
a8f5be17
SG
537{
538 const struct udevice *media = dev_get_parent(iter->dev);
539 enum uclass_id id = device_get_uclass_id(media);
540
541 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
542 if (id == UCLASS_BOOTSTD)
543 return 0;
544
545 return -ENOTSUPP;
546}
This page took 0.094467 seconds and 4 git commands to generate.