blob: 804809dc100ccfd1a75ea7608448214c96b30013 [file] [log] [blame]
Simon Glass03fcbf92022-04-24 23:31:09 -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 Glass03fcbf92022-04-24 23:31:09 -06009#include <bootdev.h>
10#include <bootflow.h>
11#include <bootmeth.h>
12#include <bootstd.h>
13#include <dm.h>
Simon Glassb35513a2023-07-12 09:04:35 -060014#include <env_internal.h>
Simon Glass03fcbf92022-04-24 23:31:09 -060015#include <malloc.h>
Simon Glasscd91e992023-07-12 09:04:42 -060016#include <serial.h>
Simon Glass03fcbf92022-04-24 23:31:09 -060017#include <dm/device-internal.h>
18#include <dm/uclass-internal.h>
19
20/* error codes used to signal running out of things */
21enum {
22 BF_NO_MORE_PARTS = -ESHUTDOWN,
23 BF_NO_MORE_DEVICES = -ENODEV,
24};
25
26/**
27 * bootflow_state - name for each state
28 *
29 * See enum bootflow_state_t for what each of these means
30 */
31static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
32 "base",
33 "media",
34 "part",
35 "fs",
36 "file",
37 "ready",
38};
39
40const char *bootflow_state_get_name(enum bootflow_state_t state)
41{
42 /* This doesn't need to be a useful name, since it will never occur */
43 if (state < 0 || state >= BOOTFLOWST_COUNT)
44 return "?";
45
46 return bootflow_state[state];
47}
48
49int bootflow_first_glob(struct bootflow **bflowp)
50{
51 struct bootstd_priv *std;
52 int ret;
53
54 ret = bootstd_get_priv(&std);
55 if (ret)
56 return ret;
57
58 if (list_empty(&std->glob_head))
59 return -ENOENT;
60
61 *bflowp = list_first_entry(&std->glob_head, struct bootflow,
62 glob_node);
63
64 return 0;
65}
66
67int bootflow_next_glob(struct bootflow **bflowp)
68{
69 struct bootstd_priv *std;
70 struct bootflow *bflow = *bflowp;
71 int ret;
72
73 ret = bootstd_get_priv(&std);
74 if (ret)
75 return ret;
76
77 *bflowp = NULL;
78
79 if (list_is_last(&bflow->glob_node, &std->glob_head))
80 return -ENOENT;
81
82 *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
83
84 return 0;
85}
86
87void bootflow_iter_init(struct bootflow_iter *iter, int flags)
88{
89 memset(iter, '\0', sizeof(*iter));
Simon Glass73fcf512022-07-30 15:52:25 -060090 iter->first_glob_method = -1;
Simon Glass03fcbf92022-04-24 23:31:09 -060091 iter->flags = flags;
Simon Glasscb2a5cd2023-01-17 10:48:17 -070092
93 /* remember the first bootdevs we see */
94 iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
Simon Glass03fcbf92022-04-24 23:31:09 -060095}
96
97void bootflow_iter_uninit(struct bootflow_iter *iter)
98{
Simon Glass03fcbf92022-04-24 23:31:09 -060099 free(iter->method_order);
100}
101
102int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
103 const struct udevice *bmeth)
104{
105 /* We only support disabling the current bootmeth */
106 if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
107 iter->method_order[iter->cur_method] != bmeth)
108 return -EINVAL;
109
110 memmove(&iter->method_order[iter->cur_method],
111 &iter->method_order[iter->cur_method + 1],
112 (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
113
114 iter->num_methods--;
115
116 return 0;
117}
118
Simon Glass484e4072023-01-17 10:48:14 -0700119/**
120 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
121 *
122 * This sets iter->dev, records the device in the dev_used[] list and shows a
123 * message if required
124 *
125 * @iter: Iterator to update
126 * @dev: Bootdev to use, or NULL if there are no more
127 */
Simon Glass03fcbf92022-04-24 23:31:09 -0600128static void bootflow_iter_set_dev(struct bootflow_iter *iter,
Simon Glass484e4072023-01-17 10:48:14 -0700129 struct udevice *dev, int method_flags)
Simon Glass03fcbf92022-04-24 23:31:09 -0600130{
Simon Glass73fcf512022-07-30 15:52:25 -0600131 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
132
Simon Glass484e4072023-01-17 10:48:14 -0700133 log_debug("iter: Setting dev to %s, flags %x\n",
134 dev ? dev->name : "(none)", method_flags);
Simon Glass03fcbf92022-04-24 23:31:09 -0600135 iter->dev = dev;
Simon Glass484e4072023-01-17 10:48:14 -0700136 iter->method_flags = method_flags;
137
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700138 if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
139 /* record the device for later */
140 if (dev && iter->num_devs < iter->max_devs)
141 iter->dev_used[iter->num_devs++] = dev;
142
Simon Glass99e68182023-02-22 12:17:03 -0700143 if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) ==
144 BOOTFLOWIF_SHOW) {
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700145 if (dev)
146 printf("Scanning bootdev '%s':\n", dev->name);
147 else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
148 ucp->flags & BOOTMETHF_GLOBAL)
149 printf("Scanning global bootmeth '%s':\n",
150 iter->method->name);
151 else
152 printf("No more bootdevs\n");
153 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600154 }
155}
156
157/**
Simon Glass33795a92023-10-23 00:02:12 -0700158 * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
159 *
160 * Move through the following bootdevs until we find another in this media
161 * uclass, or run out
162 *
163 * @devp: On entry, the device to check, on exit the new device, or NULL if
164 * there is none
165 */
166static void scan_next_in_uclass(struct udevice **devp)
167{
168 struct udevice *dev = *devp;
169 enum uclass_id cur_id = device_get_uclass_id(dev->parent);
170
171 do {
172 uclass_find_next_device(&dev);
173 } while (dev && cur_id != device_get_uclass_id(dev->parent));
174
175 *devp = dev;
176}
177
178/**
Simon Glass03fcbf92022-04-24 23:31:09 -0600179 * iter_incr() - Move to the next item (method, part, bootdev)
180 *
181 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
182 */
183static int iter_incr(struct bootflow_iter *iter)
184{
185 struct udevice *dev;
Simon Glass73fcf512022-07-30 15:52:25 -0600186 bool inc_dev = true;
187 bool global;
Simon Glass03fcbf92022-04-24 23:31:09 -0600188 int ret;
189
Simon Glass484e4072023-01-17 10:48:14 -0700190 log_debug("entry: err=%d\n", iter->err);
Simon Glass73fcf512022-07-30 15:52:25 -0600191 global = iter->doing_global;
192
Simon Glass03fcbf92022-04-24 23:31:09 -0600193 if (iter->err == BF_NO_MORE_DEVICES)
194 return BF_NO_MORE_DEVICES;
195
196 if (iter->err != BF_NO_MORE_PARTS) {
197 /* Get the next boothmethod */
198 if (++iter->cur_method < iter->num_methods) {
199 iter->method = iter->method_order[iter->cur_method];
200 return 0;
201 }
Simon Glass73fcf512022-07-30 15:52:25 -0600202
203 /*
204 * If we have finished scanning the global bootmeths, start the
205 * normal bootdev scan
206 */
207 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
208 iter->num_methods = iter->first_glob_method;
209 iter->doing_global = false;
210
211 /*
212 * Don't move to the next dev as we haven't tried this
213 * one yet!
214 */
215 inc_dev = false;
216 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600217 }
218
Nam Caocb0d3fb2024-02-21 13:41:44 +0100219 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION)
220 return BF_NO_MORE_DEVICES;
221
Simon Glass03fcbf92022-04-24 23:31:09 -0600222 /* No more bootmeths; start at the first one, and... */
223 iter->cur_method = 0;
224 iter->method = iter->method_order[iter->cur_method];
225
226 if (iter->err != BF_NO_MORE_PARTS) {
227 /* ...select next partition */
228 if (++iter->part <= iter->max_part)
229 return 0;
230 }
231
Simon Glass484e4072023-01-17 10:48:14 -0700232 /* No more partitions; start at the first one and... */
Simon Glass03fcbf92022-04-24 23:31:09 -0600233 iter->part = 0;
234
235 /*
236 * Note: as far as we know, there is no partition table on the next
237 * bootdev, so set max_part to 0 until we discover otherwise. See
238 * bootdev_find_in_blk() for where this is set.
239 */
240 iter->max_part = 0;
241
242 /* ...select next bootdev */
Simon Glass99e68182023-02-22 12:17:03 -0700243 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
Simon Glass03fcbf92022-04-24 23:31:09 -0600244 ret = -ENOENT;
Simon Glass03fcbf92022-04-24 23:31:09 -0600245 } else {
Simon Glass484e4072023-01-17 10:48:14 -0700246 int method_flags;
247
248 ret = 0;
249 dev = iter->dev;
250 log_debug("inc_dev=%d\n", inc_dev);
251 if (!inc_dev) {
Simon Glassba3d5372023-01-17 10:48:15 -0700252 ret = bootdev_setup_iter(iter, NULL, &dev,
253 &method_flags);
254 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass99e68182023-02-22 12:17:03 -0700255 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
Simon Glass33795a92023-10-23 00:02:12 -0700256 scan_next_in_uclass(&dev);
Simon Glassba3d5372023-01-17 10:48:15 -0700257 if (!dev) {
258 log_debug("finished uclass %s\n",
259 dev_get_uclass_name(dev));
260 ret = -ENODEV;
261 }
262 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass99e68182023-02-22 12:17:03 -0700263 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
Simon Glassba3d5372023-01-17 10:48:15 -0700264 log_debug("next in single\n");
265 method_flags = 0;
266 do {
267 /*
268 * Move to the next bootdev child of this media
269 * device. This ensures that we cover all the
270 * available SCSI IDs and LUNs.
271 */
272 device_find_next_child(&dev);
273 log_debug("- next %s\n",
274 dev ? dev->name : "(none)");
275 } while (dev && device_get_uclass_id(dev) !=
276 UCLASS_BOOTDEV);
277 if (!dev) {
278 log_debug("finished uclass %s\n",
279 dev_get_uclass_name(dev));
280 ret = -ENODEV;
281 }
Simon Glass484e4072023-01-17 10:48:14 -0700282 } else {
283 log_debug("labels %p\n", iter->labels);
284 if (iter->labels) {
Simon Glassb51f01d2023-10-23 00:02:13 -0700285 /*
286 * when the label is "mmc" we want to scan all
287 * mmc bootdevs, not just the first. See
288 * bootdev_find_by_label() where this flag is
289 * set up
290 */
291 if (iter->method_flags &
292 BOOTFLOW_METHF_SINGLE_UCLASS) {
293 scan_next_in_uclass(&dev);
294 log_debug("looking for next device %s: %s\n",
295 iter->dev->name,
296 dev ? dev->name : "<none>");
297 } else {
298 dev = NULL;
299 }
300 if (!dev) {
301 log_debug("looking at next label\n");
302 ret = bootdev_next_label(iter, &dev,
303 &method_flags);
304 }
Simon Glass484e4072023-01-17 10:48:14 -0700305 } else {
306 ret = bootdev_next_prio(iter, &dev);
307 method_flags = 0;
308 }
309 }
310 log_debug("ret=%d, dev=%p %s\n", ret, dev,
311 dev ? dev->name : "none");
312 if (ret) {
313 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass73fcf512022-07-30 15:52:25 -0600314 } else {
Simon Glassb73a8292023-01-28 15:00:19 -0700315 /*
316 * Probe the bootdev. This does not probe any attached
317 * block device, since they are siblings
318 */
Simon Glass73fcf512022-07-30 15:52:25 -0600319 ret = device_probe(dev);
Simon Glass484e4072023-01-17 10:48:14 -0700320 log_debug("probe %s %d\n", dev->name, ret);
Simon Glass73fcf512022-07-30 15:52:25 -0600321 if (!log_msg_ret("probe", ret))
Simon Glass484e4072023-01-17 10:48:14 -0700322 bootflow_iter_set_dev(iter, dev, method_flags);
Simon Glass73fcf512022-07-30 15:52:25 -0600323 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600324 }
325
326 /* if there are no more bootdevs, give up */
327 if (ret)
328 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
329
330 return 0;
331}
332
333/**
334 * bootflow_check() - Check if a bootflow can be obtained
335 *
336 * @iter: Provides part, bootmeth to use
337 * @bflow: Bootflow to update on success
338 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
339 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
340 */
341static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
342{
343 struct udevice *dev;
344 int ret;
345
Simon Glass73fcf512022-07-30 15:52:25 -0600346 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
Simon Glass484e4072023-01-17 10:48:14 -0700347 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass73fcf512022-07-30 15:52:25 -0600348 ret = bootmeth_get_bootflow(iter->method, bflow);
349 if (ret)
350 return log_msg_ret("glob", ret);
351
352 return 0;
353 }
354
Simon Glass03fcbf92022-04-24 23:31:09 -0600355 dev = iter->dev;
356 ret = bootdev_get_bootflow(dev, iter, bflow);
357
358 /* If we got a valid bootflow, return it */
359 if (!ret) {
Simon Glass41571582023-07-12 09:04:32 -0600360 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
Simon Glass03fcbf92022-04-24 23:31:09 -0600361 dev->name, iter->part, iter->method->name);
362 return 0;
363 }
364
365 /* Unless there is nothing more to try, move to the next device */
Heinrich Schuchardt20fc2312024-01-07 09:43:40 +0100366 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass41571582023-07-12 09:04:32 -0600367 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
Simon Glass03fcbf92022-04-24 23:31:09 -0600368 dev->name, iter->part, iter->method->name, ret);
369 /*
370 * For 'all' we return all bootflows, even
371 * those with errors
372 */
Simon Glass99e68182023-02-22 12:17:03 -0700373 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600374 return log_msg_ret("all", ret);
375 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600376
Heinrich Schuchardt20fc2312024-01-07 09:43:40 +0100377 return log_msg_ret("check", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600378}
379
Simon Glass5d3d44f2023-01-17 10:48:16 -0700380int bootflow_scan_first(struct udevice *dev, const char *label,
381 struct bootflow_iter *iter, int flags,
382 struct bootflow *bflow)
Simon Glass03fcbf92022-04-24 23:31:09 -0600383{
384 int ret;
385
Simon Glassba3d5372023-01-17 10:48:15 -0700386 if (dev || label)
Simon Glass99e68182023-02-22 12:17:03 -0700387 flags |= BOOTFLOWIF_SKIP_GLOBAL;
Simon Glass03fcbf92022-04-24 23:31:09 -0600388 bootflow_iter_init(iter, flags);
389
Simon Glass484e4072023-01-17 10:48:14 -0700390 /*
391 * Set up the ordering of bootmeths. This sets iter->doing_global and
392 * iter->first_glob_method if we are starting with the global bootmeths
393 */
Simon Glass99e68182023-02-22 12:17:03 -0700394 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
Simon Glass03fcbf92022-04-24 23:31:09 -0600395 if (ret)
396 return log_msg_ret("obmeth", -ENODEV);
397
398 /* Find the first bootmeth (there must be at least one!) */
399 iter->method = iter->method_order[iter->cur_method];
Simon Glass484e4072023-01-17 10:48:14 -0700400
401 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
402 struct udevice *dev = NULL;
403 int method_flags;
404
Simon Glassba3d5372023-01-17 10:48:15 -0700405 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
Simon Glass484e4072023-01-17 10:48:14 -0700406 if (ret)
407 return log_msg_ret("obdev", -ENODEV);
408
409 bootflow_iter_set_dev(iter, dev, method_flags);
410 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600411
412 ret = bootflow_check(iter, bflow);
413 if (ret) {
Simon Glass33063462023-01-17 10:48:18 -0700414 log_debug("check - ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600415 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass99e68182023-02-22 12:17:03 -0700416 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600417 return log_msg_ret("all", ret);
418 }
419 iter->err = ret;
420 ret = bootflow_scan_next(iter, bflow);
421 if (ret)
422 return log_msg_ret("get", ret);
423 }
424
425 return 0;
426}
427
Simon Glass03fcbf92022-04-24 23:31:09 -0600428int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
429{
430 int ret;
431
432 do {
433 ret = iter_incr(iter);
Simon Glass33063462023-01-17 10:48:18 -0700434 log_debug("iter_incr: ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600435 if (ret == BF_NO_MORE_DEVICES)
436 return log_msg_ret("done", ret);
437
438 if (!ret) {
439 ret = bootflow_check(iter, bflow);
Simon Glass33063462023-01-17 10:48:18 -0700440 log_debug("check - ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600441 if (!ret)
442 return 0;
443 iter->err = ret;
444 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass99e68182023-02-22 12:17:03 -0700445 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600446 return log_msg_ret("all", ret);
447 }
448 } else {
Simon Glass33063462023-01-17 10:48:18 -0700449 log_debug("incr failed, err=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600450 iter->err = ret;
451 }
452
453 } while (1);
454}
455
Simon Glass00501fc2022-10-20 18:22:51 -0600456void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
457 struct udevice *meth)
458{
459 memset(bflow, '\0', sizeof(*bflow));
460 bflow->dev = bootdev;
461 bflow->method = meth;
462 bflow->state = BOOTFLOWST_BASE;
463}
464
Simon Glass03fcbf92022-04-24 23:31:09 -0600465void bootflow_free(struct bootflow *bflow)
466{
467 free(bflow->name);
468 free(bflow->subdir);
469 free(bflow->fname);
Simon Glass254c9342023-11-15 18:35:23 -0700470 if (!(bflow->flags & BOOTFLOWF_STATIC_BUF))
471 free(bflow->buf);
Simon Glass72b7b192023-01-06 08:52:33 -0600472 free(bflow->os_name);
Simon Glass7b8c6342023-01-17 10:47:56 -0700473 free(bflow->fdt_fname);
Simon Glass7f75f232023-07-30 11:16:56 -0600474 free(bflow->bootmeth_priv);
Simon Glass03fcbf92022-04-24 23:31:09 -0600475}
476
477void bootflow_remove(struct bootflow *bflow)
478{
Simon Glass03fcbf92022-04-24 23:31:09 -0600479 list_del(&bflow->glob_node);
480
481 bootflow_free(bflow);
482 free(bflow);
483}
484
Simon Glass6d8f95b2023-08-10 19:33:18 -0600485#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
486int bootflow_read_all(struct bootflow *bflow)
487{
488 int ret;
489
490 if (bflow->state != BOOTFLOWST_READY)
491 return log_msg_ret("rd", -EPROTO);
492
493 ret = bootmeth_read_all(bflow->method, bflow);
494 if (ret)
495 return log_msg_ret("rd2", ret);
496
497 return 0;
498}
499#endif /* BOOTSTD_FULL */
500
Simon Glass03fcbf92022-04-24 23:31:09 -0600501int bootflow_boot(struct bootflow *bflow)
502{
503 int ret;
504
505 if (bflow->state != BOOTFLOWST_READY)
506 return log_msg_ret("load", -EPROTO);
507
508 ret = bootmeth_boot(bflow->method, bflow);
509 if (ret)
510 return log_msg_ret("boot", ret);
511
512 /*
513 * internal error, should not get here since we should have booted
514 * something or returned an error
515 */
516
517 return log_msg_ret("end", -EFAULT);
518}
519
520int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
521{
522 int ret;
523
524 printf("** Booting bootflow '%s' with %s\n", bflow->name,
525 bflow->method->name);
Simon Glassca84dc82023-02-22 12:17:04 -0700526 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
527 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
528 printf("Using prior-stage device tree\n");
Simon Glass03fcbf92022-04-24 23:31:09 -0600529 ret = bootflow_boot(bflow);
530 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
531 printf("Boot failed (err=%d)\n", ret);
532 return ret;
533 }
534
535 switch (ret) {
536 case -EPROTO:
537 printf("Bootflow not loaded (state '%s')\n",
538 bootflow_state_get_name(bflow->state));
539 break;
540 case -ENOSYS:
541 printf("Boot method '%s' not supported\n", bflow->method->name);
542 break;
543 case -ENOTSUPP:
544 /* Disable this bootflow for this iteration */
545 if (iter) {
546 int ret2;
547
548 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
549 if (!ret2) {
550 printf("Boot method '%s' failed and will not be retried\n",
551 bflow->method->name);
552 }
553 }
554
555 break;
556 default:
557 printf("Boot failed (err=%d)\n", ret);
558 break;
559 }
560
561 return ret;
562}
563
Simon Glass18c50402023-01-17 10:47:54 -0700564int bootflow_iter_check_blk(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600565{
566 const struct udevice *media = dev_get_parent(iter->dev);
567 enum uclass_id id = device_get_uclass_id(media);
568
569 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
Simon Glass5820c2f2023-01-28 15:00:25 -0700570 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
Simon Glass03fcbf92022-04-24 23:31:09 -0600571 return 0;
572
573 return -ENOTSUPP;
574}
575
Mattijs Korpershoekfc420be2024-07-10 10:40:03 +0200576int bootflow_iter_check_mmc(const struct bootflow_iter *iter)
577{
578 const struct udevice *media = dev_get_parent(iter->dev);
579 enum uclass_id id = device_get_uclass_id(media);
580
581 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
582 if (id == UCLASS_MMC)
583 return 0;
584
585 return -ENOTSUPP;
586}
587
Simon Glass215be682023-01-17 10:48:03 -0700588int bootflow_iter_check_sf(const struct bootflow_iter *iter)
589{
590 const struct udevice *media = dev_get_parent(iter->dev);
591 enum uclass_id id = device_get_uclass_id(media);
592
593 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
594 if (id == UCLASS_SPI_FLASH)
595 return 0;
596
597 return -ENOTSUPP;
598}
599
Simon Glass18c50402023-01-17 10:47:54 -0700600int bootflow_iter_check_net(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600601{
602 const struct udevice *media = dev_get_parent(iter->dev);
603 enum uclass_id id = device_get_uclass_id(media);
604
605 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
606 if (id == UCLASS_ETH)
607 return 0;
608
609 return -ENOTSUPP;
610}
611
Simon Glass18c50402023-01-17 10:47:54 -0700612int bootflow_iter_check_system(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600613{
614 const struct udevice *media = dev_get_parent(iter->dev);
615 enum uclass_id id = device_get_uclass_id(media);
616
617 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
618 if (id == UCLASS_BOOTSTD)
619 return 0;
620
621 return -ENOTSUPP;
622}
Simon Glassb35513a2023-07-12 09:04:35 -0600623
624/**
625 * bootflow_cmdline_set() - Set the command line for a bootflow
626 *
627 * @value: New command-line string
628 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
629 */
630int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
631{
632 char *cmdline = NULL;
633
634 if (value) {
635 cmdline = strdup(value);
636 if (!cmdline)
637 return -ENOMEM;
638 }
639
640 free(bflow->cmdline);
641 bflow->cmdline = cmdline;
642
643 return 0;
644}
645
646#ifdef CONFIG_BOOTSTD_FULL
647/**
648 * on_bootargs() - Update the cmdline of a bootflow
649 */
650static int on_bootargs(const char *name, const char *value, enum env_op op,
651 int flags)
652{
653 struct bootstd_priv *std;
654 struct bootflow *bflow;
655 int ret;
656
657 ret = bootstd_get_priv(&std);
658 if (ret)
659 return 0;
660 bflow = std->cur_bootflow;
661 if (!bflow)
662 return 0;
663
664 switch (op) {
665 case env_op_create:
666 case env_op_overwrite:
667 ret = bootflow_cmdline_set(bflow, value);
668 if (ret && ret != ENOENT)
669 return 1;
670 return 0;
671 case env_op_delete:
672 bootflow_cmdline_set(bflow, NULL);
673 fallthrough;
674 default:
675 return 0;
676 }
677}
678U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
679#endif
Simon Glassa08adca2023-07-12 09:04:38 -0600680
681/**
682 * copy_in() - Copy a string into a cmdline buffer
683 *
684 * @buf: Buffer to copy into
685 * @end: End of buffer (pointer to char after the end)
686 * @arg: String to copy from
687 * @len: Number of chars to copy from @arg (note that this is not usually the
688 * sane as strlen(arg) since the string may contain following arguments)
689 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
690 * with no '=' sign
691 * Returns: Number of chars written to @buf
692 */
693static int copy_in(char *buf, char *end, const char *arg, int len,
694 const char *new_val)
695{
696 char *to = buf;
697
698 /* copy the arg name */
699 if (to + len >= end)
700 return -E2BIG;
701 memcpy(to, arg, len);
702 to += len;
703
704 if (new_val == BOOTFLOWCL_EMPTY) {
705 /* no value */
706 } else {
707 bool need_quote = strchr(new_val, ' ');
708 len = strlen(new_val);
709
710 /* need space for value, equals sign and maybe two quotes */
711 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
712 return -E2BIG;
713 *to++ = '=';
714 if (need_quote)
715 *to++ = '"';
716 memcpy(to, new_val, len);
717 to += len;
718 if (need_quote)
719 *to++ = '"';
720 }
721
722 return to - buf;
723}
724
725int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
726 const char *set_arg, const char *new_val, int *posp)
727{
728 bool found_arg = false;
729 const char *from;
730 char *to, *end;
731 int set_arg_len;
732 char empty = '\0';
733 int ret;
734
735 from = cmdline ?: &empty;
736
737 /* check if the value has quotes inside */
738 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
739 return -EBADF;
740
741 set_arg_len = strlen(set_arg);
742 for (to = buf, end = buf + maxlen; *from;) {
743 const char *val, *arg_end, *val_end, *p;
744 bool in_quote;
745
746 if (to >= end)
747 return -E2BIG;
748 while (*from == ' ')
749 from++;
750 if (!*from)
751 break;
752
753 /* find the end of this arg */
754 val = NULL;
755 arg_end = NULL;
756 val_end = NULL;
757 in_quote = false;
758 for (p = from;; p++) {
759 if (in_quote) {
760 if (!*p)
761 return -EINVAL;
762 if (*p == '"')
763 in_quote = false;
764 continue;
765 }
Simon Glassa61057f2023-10-25 07:17:36 +1300766 if (*p == '=' && !arg_end) {
Simon Glassa08adca2023-07-12 09:04:38 -0600767 arg_end = p;
768 val = p + 1;
769 } else if (*p == '"') {
770 in_quote = true;
771 } else if (!*p || *p == ' ') {
772 val_end = p;
773 if (!arg_end)
774 arg_end = p;
775 break;
776 }
777 }
778 /*
779 * At this point val_end points to the end of the value, or the
780 * last char after the arg name, if there is no label.
781 * arg_end is the char after the arg name
782 * val points to the value, or NULL if there is none
783 * char after the value.
784 *
785 * fred=1234
786 * ^ ^^ ^
787 * from || |
788 * / \ \
789 * arg_end val val_end
790 */
791 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
792 (long)(arg_end - from), (long)(val - from),
793 (long)(val_end - from));
794
795 if (to != buf) {
796 if (to >= end)
797 return -E2BIG;
798 *to++ = ' ';
799 }
800
801 /* if this is the target arg, update it */
Simon Glassa61057f2023-10-25 07:17:36 +1300802 if (arg_end - from == set_arg_len &&
803 !strncmp(from, set_arg, set_arg_len)) {
Simon Glassa08adca2023-07-12 09:04:38 -0600804 if (!buf) {
805 bool has_quote = val_end[-1] == '"';
806
807 /*
808 * exclude any start/end quotes from
809 * calculations
810 */
811 if (!val)
812 val = val_end;
813 *posp = val - cmdline + has_quote;
814 return val_end - val - 2 * has_quote;
815 }
816 found_arg = true;
817 if (!new_val) {
818 /* delete this arg */
819 from = val_end + (*val_end == ' ');
820 log_debug("delete from: %s\n", from);
821 if (to != buf)
822 to--; /* drop the space we added */
823 continue;
824 }
825
826 ret = copy_in(to, end, from, arg_end - from, new_val);
827 if (ret < 0)
828 return ret;
829 to += ret;
830
831 /* if not the target arg, copy it unchanged */
832 } else if (to) {
833 int len;
834
835 len = val_end - from;
836 if (to + len >= end)
837 return -E2BIG;
838 memcpy(to, from, len);
839 to += len;
840 }
841 from = val_end;
842 }
843
844 /* If we didn't find the arg, add it */
845 if (!found_arg) {
846 /* trying to delete something that is not there */
847 if (!new_val || !buf)
848 return -ENOENT;
849 if (to >= end)
850 return -E2BIG;
851
852 /* add a space to separate it from the previous arg */
853 if (to != buf && to[-1] != ' ')
854 *to++ = ' ';
855 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
856 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
857 if (ret < 0)
858 return ret;
859 to += ret;
860 }
861
862 /* delete any trailing space */
863 if (to > buf && to[-1] == ' ')
864 to--;
865
866 if (to >= end)
867 return -E2BIG;
868 *to++ = '\0';
869
870 return to - buf;
871}
Simon Glass55a2da32023-07-12 09:04:39 -0600872
873int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
874 const char *new_val, bool set_env)
875{
876 char buf[2048];
877 char *cmd = NULL;
878 int ret;
879
880 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
881 new_val, NULL);
882 if (ret < 0)
883 return ret;
884
885 ret = bootflow_cmdline_set(bflow, buf);
886 if (*buf) {
887 cmd = strdup(buf);
888 if (!cmd)
889 return -ENOMEM;
890 }
891 free(bflow->cmdline);
892 bflow->cmdline = cmd;
893
894 if (set_env) {
895 ret = env_set("bootargs", bflow->cmdline);
896 if (ret)
897 return ret;
898 }
899
900 return 0;
901}
902
903int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
904{
905 int ret;
906
907 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
908
909 return ret;
910}
911
912int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
913 const char **val)
914{
915 int ret;
916 int pos;
917
918 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
919 if (ret < 0)
920 return ret;
921 *val = bflow->cmdline + pos;
922
923 return ret;
924}
Simon Glasscd91e992023-07-12 09:04:42 -0600925
926int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
927{
928 struct serial_device_info info;
929 char buf[50];
930 int ret;
931
932 ret = serial_getinfo(gd->cur_serial_dev, &info);
933 if (ret)
934 return ret;
935
936 *buf = '\0';
Maximilian Bruneb25c4222024-10-23 15:19:48 +0200937 if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
Simon Glasscd91e992023-07-12 09:04:42 -0600938 snprintf(buf, sizeof(buf),
939 "uart8250,mmio32,%#lx,%dn8", info.addr,
940 info.baudrate);
Maximilian Bruneb25c4222024-10-23 15:19:48 +0200941 } else if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_PL01X) {
942 snprintf(buf, sizeof(buf),
943 "pl011,mmio32,%#lx,%dn8", info.addr,
944 info.baudrate);
945 } else if (!strcmp("console", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
Simon Glasscd91e992023-07-12 09:04:42 -0600946 snprintf(buf, sizeof(buf),
947 "ttyS0,%dn8", info.baudrate);
948 }
949
950 if (!*buf) {
951 printf("Unknown param '%s\n", arg);
952 return -ENOENT;
953 }
954
955 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
956 if (ret)
957 return ret;
958
959 return 0;
960}