blob: 7ce04fd92cddc98a905a3cbccc984ee6e1c46b94 [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
Simon Glass199f5882024-11-15 16:19:12 -070058 if (!std->bootflows.count)
Simon Glass03fcbf92022-04-24 23:31:09 -060059 return -ENOENT;
60
Simon Glass199f5882024-11-15 16:19:12 -070061 *bflowp = alist_getw(&std->bootflows, 0, struct bootflow);
Simon Glass03fcbf92022-04-24 23:31:09 -060062
63 return 0;
64}
65
66int bootflow_next_glob(struct bootflow **bflowp)
67{
68 struct bootstd_priv *std;
Simon Glass03fcbf92022-04-24 23:31:09 -060069 int ret;
70
71 ret = bootstd_get_priv(&std);
72 if (ret)
73 return ret;
74
Simon Glass199f5882024-11-15 16:19:12 -070075 *bflowp = alist_nextw(&std->bootflows, *bflowp);
76 if (!*bflowp)
Simon Glass03fcbf92022-04-24 23:31:09 -060077 return -ENOENT;
78
Simon Glass03fcbf92022-04-24 23:31:09 -060079 return 0;
80}
81
82void bootflow_iter_init(struct bootflow_iter *iter, int flags)
83{
84 memset(iter, '\0', sizeof(*iter));
Simon Glass73fcf512022-07-30 15:52:25 -060085 iter->first_glob_method = -1;
Simon Glass03fcbf92022-04-24 23:31:09 -060086 iter->flags = flags;
Simon Glasscb2a5cd2023-01-17 10:48:17 -070087
88 /* remember the first bootdevs we see */
89 iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
Simon Glass03fcbf92022-04-24 23:31:09 -060090}
91
92void bootflow_iter_uninit(struct bootflow_iter *iter)
93{
Simon Glass03fcbf92022-04-24 23:31:09 -060094 free(iter->method_order);
95}
96
97int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
98 const struct udevice *bmeth)
99{
100 /* We only support disabling the current bootmeth */
101 if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
102 iter->method_order[iter->cur_method] != bmeth)
103 return -EINVAL;
104
105 memmove(&iter->method_order[iter->cur_method],
106 &iter->method_order[iter->cur_method + 1],
107 (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
108
109 iter->num_methods--;
110
111 return 0;
112}
113
Simon Glass484e4072023-01-17 10:48:14 -0700114/**
115 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
116 *
117 * This sets iter->dev, records the device in the dev_used[] list and shows a
118 * message if required
119 *
120 * @iter: Iterator to update
121 * @dev: Bootdev to use, or NULL if there are no more
122 */
Simon Glass03fcbf92022-04-24 23:31:09 -0600123static void bootflow_iter_set_dev(struct bootflow_iter *iter,
Simon Glass484e4072023-01-17 10:48:14 -0700124 struct udevice *dev, int method_flags)
Simon Glass03fcbf92022-04-24 23:31:09 -0600125{
Simon Glass73fcf512022-07-30 15:52:25 -0600126 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
127
Simon Glass484e4072023-01-17 10:48:14 -0700128 log_debug("iter: Setting dev to %s, flags %x\n",
129 dev ? dev->name : "(none)", method_flags);
Simon Glass03fcbf92022-04-24 23:31:09 -0600130 iter->dev = dev;
Simon Glass484e4072023-01-17 10:48:14 -0700131 iter->method_flags = method_flags;
132
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700133 if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
134 /* record the device for later */
135 if (dev && iter->num_devs < iter->max_devs)
136 iter->dev_used[iter->num_devs++] = dev;
137
Simon Glass99e68182023-02-22 12:17:03 -0700138 if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) ==
139 BOOTFLOWIF_SHOW) {
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700140 if (dev)
141 printf("Scanning bootdev '%s':\n", dev->name);
142 else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
143 ucp->flags & BOOTMETHF_GLOBAL)
144 printf("Scanning global bootmeth '%s':\n",
145 iter->method->name);
146 else
147 printf("No more bootdevs\n");
148 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600149 }
150}
151
152/**
Simon Glass33795a92023-10-23 00:02:12 -0700153 * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
154 *
155 * Move through the following bootdevs until we find another in this media
156 * uclass, or run out
157 *
158 * @devp: On entry, the device to check, on exit the new device, or NULL if
159 * there is none
160 */
161static void scan_next_in_uclass(struct udevice **devp)
162{
163 struct udevice *dev = *devp;
164 enum uclass_id cur_id = device_get_uclass_id(dev->parent);
165
166 do {
167 uclass_find_next_device(&dev);
168 } while (dev && cur_id != device_get_uclass_id(dev->parent));
169
170 *devp = dev;
171}
172
173/**
Simon Glass03fcbf92022-04-24 23:31:09 -0600174 * iter_incr() - Move to the next item (method, part, bootdev)
175 *
176 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
177 */
178static int iter_incr(struct bootflow_iter *iter)
179{
180 struct udevice *dev;
Simon Glass73fcf512022-07-30 15:52:25 -0600181 bool inc_dev = true;
182 bool global;
Simon Glass03fcbf92022-04-24 23:31:09 -0600183 int ret;
184
Simon Glass484e4072023-01-17 10:48:14 -0700185 log_debug("entry: err=%d\n", iter->err);
Simon Glass73fcf512022-07-30 15:52:25 -0600186 global = iter->doing_global;
187
Simon Glass03fcbf92022-04-24 23:31:09 -0600188 if (iter->err == BF_NO_MORE_DEVICES)
189 return BF_NO_MORE_DEVICES;
190
191 if (iter->err != BF_NO_MORE_PARTS) {
192 /* Get the next boothmethod */
193 if (++iter->cur_method < iter->num_methods) {
194 iter->method = iter->method_order[iter->cur_method];
195 return 0;
196 }
Simon Glass73fcf512022-07-30 15:52:25 -0600197
198 /*
199 * If we have finished scanning the global bootmeths, start the
200 * normal bootdev scan
201 */
202 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
203 iter->num_methods = iter->first_glob_method;
204 iter->doing_global = false;
205
206 /*
207 * Don't move to the next dev as we haven't tried this
208 * one yet!
209 */
210 inc_dev = false;
211 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600212 }
213
Nam Caocb0d3fb2024-02-21 13:41:44 +0100214 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION)
215 return BF_NO_MORE_DEVICES;
216
Simon Glass03fcbf92022-04-24 23:31:09 -0600217 /* No more bootmeths; start at the first one, and... */
218 iter->cur_method = 0;
219 iter->method = iter->method_order[iter->cur_method];
220
221 if (iter->err != BF_NO_MORE_PARTS) {
222 /* ...select next partition */
223 if (++iter->part <= iter->max_part)
224 return 0;
225 }
226
Simon Glass484e4072023-01-17 10:48:14 -0700227 /* No more partitions; start at the first one and... */
Simon Glass03fcbf92022-04-24 23:31:09 -0600228 iter->part = 0;
229
230 /*
231 * Note: as far as we know, there is no partition table on the next
232 * bootdev, so set max_part to 0 until we discover otherwise. See
233 * bootdev_find_in_blk() for where this is set.
234 */
235 iter->max_part = 0;
236
237 /* ...select next bootdev */
Simon Glass99e68182023-02-22 12:17:03 -0700238 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
Simon Glass03fcbf92022-04-24 23:31:09 -0600239 ret = -ENOENT;
Simon Glass03fcbf92022-04-24 23:31:09 -0600240 } else {
Simon Glass484e4072023-01-17 10:48:14 -0700241 int method_flags;
242
243 ret = 0;
244 dev = iter->dev;
245 log_debug("inc_dev=%d\n", inc_dev);
246 if (!inc_dev) {
Simon Glassba3d5372023-01-17 10:48:15 -0700247 ret = bootdev_setup_iter(iter, NULL, &dev,
248 &method_flags);
249 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass99e68182023-02-22 12:17:03 -0700250 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
Simon Glass33795a92023-10-23 00:02:12 -0700251 scan_next_in_uclass(&dev);
Simon Glassba3d5372023-01-17 10:48:15 -0700252 if (!dev) {
253 log_debug("finished uclass %s\n",
254 dev_get_uclass_name(dev));
255 ret = -ENODEV;
256 }
257 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass99e68182023-02-22 12:17:03 -0700258 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
Simon Glassba3d5372023-01-17 10:48:15 -0700259 log_debug("next in single\n");
260 method_flags = 0;
261 do {
262 /*
263 * Move to the next bootdev child of this media
264 * device. This ensures that we cover all the
265 * available SCSI IDs and LUNs.
266 */
267 device_find_next_child(&dev);
268 log_debug("- next %s\n",
269 dev ? dev->name : "(none)");
270 } while (dev && device_get_uclass_id(dev) !=
271 UCLASS_BOOTDEV);
272 if (!dev) {
273 log_debug("finished uclass %s\n",
274 dev_get_uclass_name(dev));
275 ret = -ENODEV;
276 }
Simon Glass484e4072023-01-17 10:48:14 -0700277 } else {
278 log_debug("labels %p\n", iter->labels);
279 if (iter->labels) {
Simon Glassb51f01d2023-10-23 00:02:13 -0700280 /*
281 * when the label is "mmc" we want to scan all
282 * mmc bootdevs, not just the first. See
283 * bootdev_find_by_label() where this flag is
284 * set up
285 */
286 if (iter->method_flags &
287 BOOTFLOW_METHF_SINGLE_UCLASS) {
288 scan_next_in_uclass(&dev);
289 log_debug("looking for next device %s: %s\n",
290 iter->dev->name,
291 dev ? dev->name : "<none>");
292 } else {
293 dev = NULL;
294 }
295 if (!dev) {
296 log_debug("looking at next label\n");
297 ret = bootdev_next_label(iter, &dev,
298 &method_flags);
299 }
Simon Glass484e4072023-01-17 10:48:14 -0700300 } else {
301 ret = bootdev_next_prio(iter, &dev);
302 method_flags = 0;
303 }
304 }
305 log_debug("ret=%d, dev=%p %s\n", ret, dev,
306 dev ? dev->name : "none");
307 if (ret) {
308 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass73fcf512022-07-30 15:52:25 -0600309 } else {
Simon Glassb73a8292023-01-28 15:00:19 -0700310 /*
311 * Probe the bootdev. This does not probe any attached
312 * block device, since they are siblings
313 */
Simon Glass73fcf512022-07-30 15:52:25 -0600314 ret = device_probe(dev);
Simon Glass484e4072023-01-17 10:48:14 -0700315 log_debug("probe %s %d\n", dev->name, ret);
Simon Glass73fcf512022-07-30 15:52:25 -0600316 if (!log_msg_ret("probe", ret))
Simon Glass484e4072023-01-17 10:48:14 -0700317 bootflow_iter_set_dev(iter, dev, method_flags);
Simon Glass73fcf512022-07-30 15:52:25 -0600318 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600319 }
320
321 /* if there are no more bootdevs, give up */
322 if (ret)
323 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
324
325 return 0;
326}
327
328/**
329 * bootflow_check() - Check if a bootflow can be obtained
330 *
331 * @iter: Provides part, bootmeth to use
332 * @bflow: Bootflow to update on success
333 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
334 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
335 */
336static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
337{
338 struct udevice *dev;
339 int ret;
340
Simon Glass73fcf512022-07-30 15:52:25 -0600341 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
Simon Glass484e4072023-01-17 10:48:14 -0700342 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass73fcf512022-07-30 15:52:25 -0600343 ret = bootmeth_get_bootflow(iter->method, bflow);
344 if (ret)
345 return log_msg_ret("glob", ret);
346
347 return 0;
348 }
349
Simon Glass03fcbf92022-04-24 23:31:09 -0600350 dev = iter->dev;
351 ret = bootdev_get_bootflow(dev, iter, bflow);
352
353 /* If we got a valid bootflow, return it */
354 if (!ret) {
Simon Glass41571582023-07-12 09:04:32 -0600355 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
Simon Glass03fcbf92022-04-24 23:31:09 -0600356 dev->name, iter->part, iter->method->name);
357 return 0;
358 }
359
360 /* Unless there is nothing more to try, move to the next device */
Heinrich Schuchardt20fc2312024-01-07 09:43:40 +0100361 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass41571582023-07-12 09:04:32 -0600362 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
Simon Glass03fcbf92022-04-24 23:31:09 -0600363 dev->name, iter->part, iter->method->name, ret);
364 /*
365 * For 'all' we return all bootflows, even
366 * those with errors
367 */
Simon Glass99e68182023-02-22 12:17:03 -0700368 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600369 return log_msg_ret("all", ret);
370 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600371
Heinrich Schuchardt20fc2312024-01-07 09:43:40 +0100372 return log_msg_ret("check", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600373}
374
Simon Glass5d3d44f2023-01-17 10:48:16 -0700375int bootflow_scan_first(struct udevice *dev, const char *label,
376 struct bootflow_iter *iter, int flags,
377 struct bootflow *bflow)
Simon Glass03fcbf92022-04-24 23:31:09 -0600378{
379 int ret;
380
Simon Glassba3d5372023-01-17 10:48:15 -0700381 if (dev || label)
Simon Glass99e68182023-02-22 12:17:03 -0700382 flags |= BOOTFLOWIF_SKIP_GLOBAL;
Simon Glass03fcbf92022-04-24 23:31:09 -0600383 bootflow_iter_init(iter, flags);
384
Simon Glass484e4072023-01-17 10:48:14 -0700385 /*
386 * Set up the ordering of bootmeths. This sets iter->doing_global and
387 * iter->first_glob_method if we are starting with the global bootmeths
388 */
Simon Glass99e68182023-02-22 12:17:03 -0700389 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
Simon Glass03fcbf92022-04-24 23:31:09 -0600390 if (ret)
391 return log_msg_ret("obmeth", -ENODEV);
392
393 /* Find the first bootmeth (there must be at least one!) */
394 iter->method = iter->method_order[iter->cur_method];
Simon Glass484e4072023-01-17 10:48:14 -0700395
396 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
397 struct udevice *dev = NULL;
398 int method_flags;
399
Simon Glassba3d5372023-01-17 10:48:15 -0700400 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
Simon Glass484e4072023-01-17 10:48:14 -0700401 if (ret)
402 return log_msg_ret("obdev", -ENODEV);
403
404 bootflow_iter_set_dev(iter, dev, method_flags);
405 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600406
407 ret = bootflow_check(iter, bflow);
408 if (ret) {
Simon Glass33063462023-01-17 10:48:18 -0700409 log_debug("check - ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600410 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass99e68182023-02-22 12:17:03 -0700411 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600412 return log_msg_ret("all", ret);
413 }
414 iter->err = ret;
415 ret = bootflow_scan_next(iter, bflow);
416 if (ret)
417 return log_msg_ret("get", ret);
418 }
419
420 return 0;
421}
422
Simon Glass03fcbf92022-04-24 23:31:09 -0600423int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
424{
425 int ret;
426
427 do {
428 ret = iter_incr(iter);
Simon Glass33063462023-01-17 10:48:18 -0700429 log_debug("iter_incr: ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600430 if (ret == BF_NO_MORE_DEVICES)
431 return log_msg_ret("done", ret);
432
433 if (!ret) {
434 ret = bootflow_check(iter, bflow);
Simon Glass33063462023-01-17 10:48:18 -0700435 log_debug("check - ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600436 if (!ret)
437 return 0;
438 iter->err = ret;
439 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass99e68182023-02-22 12:17:03 -0700440 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600441 return log_msg_ret("all", ret);
442 }
443 } else {
Simon Glass33063462023-01-17 10:48:18 -0700444 log_debug("incr failed, err=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600445 iter->err = ret;
446 }
447
448 } while (1);
449}
450
Simon Glass00501fc2022-10-20 18:22:51 -0600451void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
452 struct udevice *meth)
453{
454 memset(bflow, '\0', sizeof(*bflow));
455 bflow->dev = bootdev;
456 bflow->method = meth;
457 bflow->state = BOOTFLOWST_BASE;
458}
459
Simon Glass03fcbf92022-04-24 23:31:09 -0600460void bootflow_free(struct bootflow *bflow)
461{
462 free(bflow->name);
463 free(bflow->subdir);
464 free(bflow->fname);
Simon Glass254c9342023-11-15 18:35:23 -0700465 if (!(bflow->flags & BOOTFLOWF_STATIC_BUF))
466 free(bflow->buf);
Simon Glass72b7b192023-01-06 08:52:33 -0600467 free(bflow->os_name);
Simon Glass7b8c6342023-01-17 10:47:56 -0700468 free(bflow->fdt_fname);
Simon Glass7f75f232023-07-30 11:16:56 -0600469 free(bflow->bootmeth_priv);
Simon Glass03fcbf92022-04-24 23:31:09 -0600470}
471
472void bootflow_remove(struct bootflow *bflow)
473{
Simon Glass03fcbf92022-04-24 23:31:09 -0600474 bootflow_free(bflow);
Simon Glass03fcbf92022-04-24 23:31:09 -0600475}
476
Simon Glass6d8f95b2023-08-10 19:33:18 -0600477#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
478int bootflow_read_all(struct bootflow *bflow)
479{
480 int ret;
481
482 if (bflow->state != BOOTFLOWST_READY)
483 return log_msg_ret("rd", -EPROTO);
484
485 ret = bootmeth_read_all(bflow->method, bflow);
486 if (ret)
487 return log_msg_ret("rd2", ret);
488
489 return 0;
490}
491#endif /* BOOTSTD_FULL */
492
Simon Glass03fcbf92022-04-24 23:31:09 -0600493int bootflow_boot(struct bootflow *bflow)
494{
495 int ret;
496
497 if (bflow->state != BOOTFLOWST_READY)
498 return log_msg_ret("load", -EPROTO);
499
500 ret = bootmeth_boot(bflow->method, bflow);
501 if (ret)
502 return log_msg_ret("boot", ret);
503
504 /*
505 * internal error, should not get here since we should have booted
506 * something or returned an error
507 */
508
509 return log_msg_ret("end", -EFAULT);
510}
511
512int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
513{
514 int ret;
515
516 printf("** Booting bootflow '%s' with %s\n", bflow->name,
517 bflow->method->name);
Simon Glassca84dc82023-02-22 12:17:04 -0700518 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
519 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
520 printf("Using prior-stage device tree\n");
Simon Glass03fcbf92022-04-24 23:31:09 -0600521 ret = bootflow_boot(bflow);
522 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
523 printf("Boot failed (err=%d)\n", ret);
524 return ret;
525 }
526
527 switch (ret) {
528 case -EPROTO:
529 printf("Bootflow not loaded (state '%s')\n",
530 bootflow_state_get_name(bflow->state));
531 break;
532 case -ENOSYS:
533 printf("Boot method '%s' not supported\n", bflow->method->name);
534 break;
535 case -ENOTSUPP:
536 /* Disable this bootflow for this iteration */
537 if (iter) {
538 int ret2;
539
540 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
541 if (!ret2) {
542 printf("Boot method '%s' failed and will not be retried\n",
543 bflow->method->name);
544 }
545 }
546
547 break;
548 default:
549 printf("Boot failed (err=%d)\n", ret);
550 break;
551 }
552
553 return ret;
554}
555
Simon Glass18c50402023-01-17 10:47:54 -0700556int bootflow_iter_check_blk(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600557{
558 const struct udevice *media = dev_get_parent(iter->dev);
559 enum uclass_id id = device_get_uclass_id(media);
560
561 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
Simon Glass5820c2f2023-01-28 15:00:25 -0700562 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
Simon Glass03fcbf92022-04-24 23:31:09 -0600563 return 0;
564
565 return -ENOTSUPP;
566}
567
Mattijs Korpershoekfc420be2024-07-10 10:40:03 +0200568int bootflow_iter_check_mmc(const struct bootflow_iter *iter)
569{
570 const struct udevice *media = dev_get_parent(iter->dev);
571 enum uclass_id id = device_get_uclass_id(media);
572
573 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
574 if (id == UCLASS_MMC)
575 return 0;
576
577 return -ENOTSUPP;
578}
579
Simon Glass215be682023-01-17 10:48:03 -0700580int bootflow_iter_check_sf(const struct bootflow_iter *iter)
581{
582 const struct udevice *media = dev_get_parent(iter->dev);
583 enum uclass_id id = device_get_uclass_id(media);
584
585 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
586 if (id == UCLASS_SPI_FLASH)
587 return 0;
588
589 return -ENOTSUPP;
590}
591
Simon Glass18c50402023-01-17 10:47:54 -0700592int bootflow_iter_check_net(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600593{
594 const struct udevice *media = dev_get_parent(iter->dev);
595 enum uclass_id id = device_get_uclass_id(media);
596
597 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
598 if (id == UCLASS_ETH)
599 return 0;
600
601 return -ENOTSUPP;
602}
603
Simon Glass18c50402023-01-17 10:47:54 -0700604int bootflow_iter_check_system(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600605{
606 const struct udevice *media = dev_get_parent(iter->dev);
607 enum uclass_id id = device_get_uclass_id(media);
608
609 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
610 if (id == UCLASS_BOOTSTD)
611 return 0;
612
613 return -ENOTSUPP;
614}
Simon Glassb35513a2023-07-12 09:04:35 -0600615
616/**
617 * bootflow_cmdline_set() - Set the command line for a bootflow
618 *
619 * @value: New command-line string
620 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
621 */
622int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
623{
624 char *cmdline = NULL;
625
626 if (value) {
627 cmdline = strdup(value);
628 if (!cmdline)
629 return -ENOMEM;
630 }
631
632 free(bflow->cmdline);
633 bflow->cmdline = cmdline;
634
635 return 0;
636}
637
638#ifdef CONFIG_BOOTSTD_FULL
639/**
640 * on_bootargs() - Update the cmdline of a bootflow
641 */
642static int on_bootargs(const char *name, const char *value, enum env_op op,
643 int flags)
644{
645 struct bootstd_priv *std;
646 struct bootflow *bflow;
647 int ret;
648
649 ret = bootstd_get_priv(&std);
650 if (ret)
651 return 0;
652 bflow = std->cur_bootflow;
653 if (!bflow)
654 return 0;
655
656 switch (op) {
657 case env_op_create:
658 case env_op_overwrite:
659 ret = bootflow_cmdline_set(bflow, value);
660 if (ret && ret != ENOENT)
661 return 1;
662 return 0;
663 case env_op_delete:
664 bootflow_cmdline_set(bflow, NULL);
665 fallthrough;
666 default:
667 return 0;
668 }
669}
670U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
671#endif
Simon Glassa08adca2023-07-12 09:04:38 -0600672
673/**
674 * copy_in() - Copy a string into a cmdline buffer
675 *
676 * @buf: Buffer to copy into
677 * @end: End of buffer (pointer to char after the end)
678 * @arg: String to copy from
679 * @len: Number of chars to copy from @arg (note that this is not usually the
680 * sane as strlen(arg) since the string may contain following arguments)
681 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
682 * with no '=' sign
683 * Returns: Number of chars written to @buf
684 */
685static int copy_in(char *buf, char *end, const char *arg, int len,
686 const char *new_val)
687{
688 char *to = buf;
689
690 /* copy the arg name */
691 if (to + len >= end)
692 return -E2BIG;
693 memcpy(to, arg, len);
694 to += len;
695
696 if (new_val == BOOTFLOWCL_EMPTY) {
697 /* no value */
698 } else {
699 bool need_quote = strchr(new_val, ' ');
700 len = strlen(new_val);
701
702 /* need space for value, equals sign and maybe two quotes */
703 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
704 return -E2BIG;
705 *to++ = '=';
706 if (need_quote)
707 *to++ = '"';
708 memcpy(to, new_val, len);
709 to += len;
710 if (need_quote)
711 *to++ = '"';
712 }
713
714 return to - buf;
715}
716
717int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
718 const char *set_arg, const char *new_val, int *posp)
719{
720 bool found_arg = false;
721 const char *from;
722 char *to, *end;
723 int set_arg_len;
724 char empty = '\0';
725 int ret;
726
727 from = cmdline ?: &empty;
728
729 /* check if the value has quotes inside */
730 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
731 return -EBADF;
732
733 set_arg_len = strlen(set_arg);
734 for (to = buf, end = buf + maxlen; *from;) {
735 const char *val, *arg_end, *val_end, *p;
736 bool in_quote;
737
738 if (to >= end)
739 return -E2BIG;
740 while (*from == ' ')
741 from++;
742 if (!*from)
743 break;
744
745 /* find the end of this arg */
746 val = NULL;
747 arg_end = NULL;
748 val_end = NULL;
749 in_quote = false;
750 for (p = from;; p++) {
751 if (in_quote) {
752 if (!*p)
753 return -EINVAL;
754 if (*p == '"')
755 in_quote = false;
756 continue;
757 }
Simon Glassa61057f2023-10-25 07:17:36 +1300758 if (*p == '=' && !arg_end) {
Simon Glassa08adca2023-07-12 09:04:38 -0600759 arg_end = p;
760 val = p + 1;
761 } else if (*p == '"') {
762 in_quote = true;
763 } else if (!*p || *p == ' ') {
764 val_end = p;
765 if (!arg_end)
766 arg_end = p;
767 break;
768 }
769 }
770 /*
771 * At this point val_end points to the end of the value, or the
772 * last char after the arg name, if there is no label.
773 * arg_end is the char after the arg name
774 * val points to the value, or NULL if there is none
775 * char after the value.
776 *
777 * fred=1234
778 * ^ ^^ ^
779 * from || |
780 * / \ \
781 * arg_end val val_end
782 */
783 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
784 (long)(arg_end - from), (long)(val - from),
785 (long)(val_end - from));
786
787 if (to != buf) {
788 if (to >= end)
789 return -E2BIG;
790 *to++ = ' ';
791 }
792
793 /* if this is the target arg, update it */
Simon Glassa61057f2023-10-25 07:17:36 +1300794 if (arg_end - from == set_arg_len &&
795 !strncmp(from, set_arg, set_arg_len)) {
Simon Glassa08adca2023-07-12 09:04:38 -0600796 if (!buf) {
797 bool has_quote = val_end[-1] == '"';
798
799 /*
800 * exclude any start/end quotes from
801 * calculations
802 */
803 if (!val)
804 val = val_end;
805 *posp = val - cmdline + has_quote;
806 return val_end - val - 2 * has_quote;
807 }
808 found_arg = true;
809 if (!new_val) {
810 /* delete this arg */
811 from = val_end + (*val_end == ' ');
812 log_debug("delete from: %s\n", from);
813 if (to != buf)
814 to--; /* drop the space we added */
815 continue;
816 }
817
818 ret = copy_in(to, end, from, arg_end - from, new_val);
819 if (ret < 0)
820 return ret;
821 to += ret;
822
823 /* if not the target arg, copy it unchanged */
824 } else if (to) {
825 int len;
826
827 len = val_end - from;
828 if (to + len >= end)
829 return -E2BIG;
830 memcpy(to, from, len);
831 to += len;
832 }
833 from = val_end;
834 }
835
836 /* If we didn't find the arg, add it */
837 if (!found_arg) {
838 /* trying to delete something that is not there */
839 if (!new_val || !buf)
840 return -ENOENT;
841 if (to >= end)
842 return -E2BIG;
843
844 /* add a space to separate it from the previous arg */
845 if (to != buf && to[-1] != ' ')
846 *to++ = ' ';
847 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
848 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
849 if (ret < 0)
850 return ret;
851 to += ret;
852 }
853
854 /* delete any trailing space */
855 if (to > buf && to[-1] == ' ')
856 to--;
857
858 if (to >= end)
859 return -E2BIG;
860 *to++ = '\0';
861
862 return to - buf;
863}
Simon Glass55a2da32023-07-12 09:04:39 -0600864
865int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
866 const char *new_val, bool set_env)
867{
868 char buf[2048];
869 char *cmd = NULL;
870 int ret;
871
872 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
873 new_val, NULL);
874 if (ret < 0)
875 return ret;
876
877 ret = bootflow_cmdline_set(bflow, buf);
878 if (*buf) {
879 cmd = strdup(buf);
880 if (!cmd)
881 return -ENOMEM;
882 }
883 free(bflow->cmdline);
884 bflow->cmdline = cmd;
885
886 if (set_env) {
887 ret = env_set("bootargs", bflow->cmdline);
888 if (ret)
889 return ret;
890 }
891
892 return 0;
893}
894
895int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
896{
897 int ret;
898
899 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
900
901 return ret;
902}
903
904int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
905 const char **val)
906{
907 int ret;
908 int pos;
909
910 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
911 if (ret < 0)
912 return ret;
913 *val = bflow->cmdline + pos;
914
915 return ret;
916}
Simon Glasscd91e992023-07-12 09:04:42 -0600917
918int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
919{
920 struct serial_device_info info;
921 char buf[50];
922 int ret;
923
924 ret = serial_getinfo(gd->cur_serial_dev, &info);
925 if (ret)
926 return ret;
927
928 *buf = '\0';
Maximilian Bruneb25c4222024-10-23 15:19:48 +0200929 if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
Simon Glasscd91e992023-07-12 09:04:42 -0600930 snprintf(buf, sizeof(buf),
931 "uart8250,mmio32,%#lx,%dn8", info.addr,
932 info.baudrate);
Maximilian Bruneb25c4222024-10-23 15:19:48 +0200933 } else if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_PL01X) {
934 snprintf(buf, sizeof(buf),
935 "pl011,mmio32,%#lx,%dn8", info.addr,
936 info.baudrate);
937 } else if (!strcmp("console", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
Simon Glasscd91e992023-07-12 09:04:42 -0600938 snprintf(buf, sizeof(buf),
939 "ttyS0,%dn8", info.baudrate);
940 }
941
942 if (!*buf) {
943 printf("Unknown param '%s\n", arg);
944 return -ENOENT;
945 }
946
947 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
948 if (ret)
949 return ret;
950
951 return 0;
952}