blob: 05484fd5b1b98bebb431b0a8579ea61c720f526a [file] [log] [blame]
Simon Glassa8f5be12022-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
9#include <common.h>
10#include <bootdev.h>
11#include <bootflow.h>
12#include <bootmeth.h>
13#include <bootstd.h>
14#include <dm.h>
Simon Glassd42243f2023-07-12 09:04:35 -060015#include <env_internal.h>
Simon Glassa8f5be12022-04-24 23:31:09 -060016#include <malloc.h>
Simon Glass33ebcb42023-07-12 09:04:42 -060017#include <serial.h>
Simon Glassa8f5be12022-04-24 23:31:09 -060018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
20
21/* error codes used to signal running out of things */
22enum {
23 BF_NO_MORE_PARTS = -ESHUTDOWN,
24 BF_NO_MORE_DEVICES = -ENODEV,
25};
26
27/**
28 * bootflow_state - name for each state
29 *
30 * See enum bootflow_state_t for what each of these means
31 */
32static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
33 "base",
34 "media",
35 "part",
36 "fs",
37 "file",
38 "ready",
39};
40
41const char *bootflow_state_get_name(enum bootflow_state_t state)
42{
43 /* This doesn't need to be a useful name, since it will never occur */
44 if (state < 0 || state >= BOOTFLOWST_COUNT)
45 return "?";
46
47 return bootflow_state[state];
48}
49
50int bootflow_first_glob(struct bootflow **bflowp)
51{
52 struct bootstd_priv *std;
53 int ret;
54
55 ret = bootstd_get_priv(&std);
56 if (ret)
57 return ret;
58
59 if (list_empty(&std->glob_head))
60 return -ENOENT;
61
62 *bflowp = list_first_entry(&std->glob_head, struct bootflow,
63 glob_node);
64
65 return 0;
66}
67
68int bootflow_next_glob(struct bootflow **bflowp)
69{
70 struct bootstd_priv *std;
71 struct bootflow *bflow = *bflowp;
72 int ret;
73
74 ret = bootstd_get_priv(&std);
75 if (ret)
76 return ret;
77
78 *bflowp = NULL;
79
80 if (list_is_last(&bflow->glob_node, &std->glob_head))
81 return -ENOENT;
82
83 *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
84
85 return 0;
86}
87
88void bootflow_iter_init(struct bootflow_iter *iter, int flags)
89{
90 memset(iter, '\0', sizeof(*iter));
Simon Glass2b80bc12022-07-30 15:52:25 -060091 iter->first_glob_method = -1;
Simon Glassa8f5be12022-04-24 23:31:09 -060092 iter->flags = flags;
Simon Glassa950f282023-01-17 10:48:17 -070093
94 /* remember the first bootdevs we see */
95 iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
Simon Glassa8f5be12022-04-24 23:31:09 -060096}
97
98void bootflow_iter_uninit(struct bootflow_iter *iter)
99{
Simon Glassa8f5be12022-04-24 23:31:09 -0600100 free(iter->method_order);
101}
102
103int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
104 const struct udevice *bmeth)
105{
106 /* We only support disabling the current bootmeth */
107 if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
108 iter->method_order[iter->cur_method] != bmeth)
109 return -EINVAL;
110
111 memmove(&iter->method_order[iter->cur_method],
112 &iter->method_order[iter->cur_method + 1],
113 (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
114
115 iter->num_methods--;
116
117 return 0;
118}
119
Simon Glass47aedc22023-01-17 10:48:14 -0700120/**
121 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
122 *
123 * This sets iter->dev, records the device in the dev_used[] list and shows a
124 * message if required
125 *
126 * @iter: Iterator to update
127 * @dev: Bootdev to use, or NULL if there are no more
128 */
Simon Glassa8f5be12022-04-24 23:31:09 -0600129static void bootflow_iter_set_dev(struct bootflow_iter *iter,
Simon Glass47aedc22023-01-17 10:48:14 -0700130 struct udevice *dev, int method_flags)
Simon Glassa8f5be12022-04-24 23:31:09 -0600131{
Simon Glass2b80bc12022-07-30 15:52:25 -0600132 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
133
Simon Glass47aedc22023-01-17 10:48:14 -0700134 log_debug("iter: Setting dev to %s, flags %x\n",
135 dev ? dev->name : "(none)", method_flags);
Simon Glassa8f5be12022-04-24 23:31:09 -0600136 iter->dev = dev;
Simon Glass47aedc22023-01-17 10:48:14 -0700137 iter->method_flags = method_flags;
138
Simon Glassa950f282023-01-17 10:48:17 -0700139 if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
140 /* record the device for later */
141 if (dev && iter->num_devs < iter->max_devs)
142 iter->dev_used[iter->num_devs++] = dev;
143
Simon Glass4f806f32023-02-22 12:17:03 -0700144 if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) ==
145 BOOTFLOWIF_SHOW) {
Simon Glassa950f282023-01-17 10:48:17 -0700146 if (dev)
147 printf("Scanning bootdev '%s':\n", dev->name);
148 else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
149 ucp->flags & BOOTMETHF_GLOBAL)
150 printf("Scanning global bootmeth '%s':\n",
151 iter->method->name);
152 else
153 printf("No more bootdevs\n");
154 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600155 }
156}
157
158/**
Simon Glass16e19352023-10-23 00:02:12 -0700159 * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
160 *
161 * Move through the following bootdevs until we find another in this media
162 * uclass, or run out
163 *
164 * @devp: On entry, the device to check, on exit the new device, or NULL if
165 * there is none
166 */
167static void scan_next_in_uclass(struct udevice **devp)
168{
169 struct udevice *dev = *devp;
170 enum uclass_id cur_id = device_get_uclass_id(dev->parent);
171
172 do {
173 uclass_find_next_device(&dev);
174 } while (dev && cur_id != device_get_uclass_id(dev->parent));
175
176 *devp = dev;
177}
178
179/**
Simon Glassa8f5be12022-04-24 23:31:09 -0600180 * iter_incr() - Move to the next item (method, part, bootdev)
181 *
182 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
183 */
184static int iter_incr(struct bootflow_iter *iter)
185{
186 struct udevice *dev;
Simon Glass2b80bc12022-07-30 15:52:25 -0600187 bool inc_dev = true;
188 bool global;
Simon Glassa8f5be12022-04-24 23:31:09 -0600189 int ret;
190
Simon Glass47aedc22023-01-17 10:48:14 -0700191 log_debug("entry: err=%d\n", iter->err);
Simon Glass2b80bc12022-07-30 15:52:25 -0600192 global = iter->doing_global;
193
Simon Glassa8f5be12022-04-24 23:31:09 -0600194 if (iter->err == BF_NO_MORE_DEVICES)
195 return BF_NO_MORE_DEVICES;
196
197 if (iter->err != BF_NO_MORE_PARTS) {
198 /* Get the next boothmethod */
199 if (++iter->cur_method < iter->num_methods) {
200 iter->method = iter->method_order[iter->cur_method];
201 return 0;
202 }
Simon Glass2b80bc12022-07-30 15:52:25 -0600203
204 /*
205 * If we have finished scanning the global bootmeths, start the
206 * normal bootdev scan
207 */
208 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
209 iter->num_methods = iter->first_glob_method;
210 iter->doing_global = false;
211
212 /*
213 * Don't move to the next dev as we haven't tried this
214 * one yet!
215 */
216 inc_dev = false;
217 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600218 }
219
220 /* No more bootmeths; start at the first one, and... */
221 iter->cur_method = 0;
222 iter->method = iter->method_order[iter->cur_method];
223
224 if (iter->err != BF_NO_MORE_PARTS) {
225 /* ...select next partition */
226 if (++iter->part <= iter->max_part)
227 return 0;
228 }
229
Simon Glass47aedc22023-01-17 10:48:14 -0700230 /* No more partitions; start at the first one and... */
Simon Glassa8f5be12022-04-24 23:31:09 -0600231 iter->part = 0;
232
233 /*
234 * Note: as far as we know, there is no partition table on the next
235 * bootdev, so set max_part to 0 until we discover otherwise. See
236 * bootdev_find_in_blk() for where this is set.
237 */
238 iter->max_part = 0;
239
240 /* ...select next bootdev */
Simon Glass4f806f32023-02-22 12:17:03 -0700241 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
Simon Glassa8f5be12022-04-24 23:31:09 -0600242 ret = -ENOENT;
Simon Glassa8f5be12022-04-24 23:31:09 -0600243 } else {
Simon Glass47aedc22023-01-17 10:48:14 -0700244 int method_flags;
245
246 ret = 0;
247 dev = iter->dev;
248 log_debug("inc_dev=%d\n", inc_dev);
249 if (!inc_dev) {
Simon Glass91943ff2023-01-17 10:48:15 -0700250 ret = bootdev_setup_iter(iter, NULL, &dev,
251 &method_flags);
252 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass4f806f32023-02-22 12:17:03 -0700253 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
Simon Glass16e19352023-10-23 00:02:12 -0700254 scan_next_in_uclass(&dev);
Simon Glass91943ff2023-01-17 10:48:15 -0700255 if (!dev) {
256 log_debug("finished uclass %s\n",
257 dev_get_uclass_name(dev));
258 ret = -ENODEV;
259 }
260 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass4f806f32023-02-22 12:17:03 -0700261 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
Simon Glass91943ff2023-01-17 10:48:15 -0700262 log_debug("next in single\n");
263 method_flags = 0;
264 do {
265 /*
266 * Move to the next bootdev child of this media
267 * device. This ensures that we cover all the
268 * available SCSI IDs and LUNs.
269 */
270 device_find_next_child(&dev);
271 log_debug("- next %s\n",
272 dev ? dev->name : "(none)");
273 } while (dev && device_get_uclass_id(dev) !=
274 UCLASS_BOOTDEV);
275 if (!dev) {
276 log_debug("finished uclass %s\n",
277 dev_get_uclass_name(dev));
278 ret = -ENODEV;
279 }
Simon Glass2b80bc12022-07-30 15:52:25 -0600280 } else {
Simon Glass47aedc22023-01-17 10:48:14 -0700281 log_debug("labels %p\n", iter->labels);
282 if (iter->labels) {
Simon Glass7a790f02023-10-23 00:02:13 -0700283 /*
284 * when the label is "mmc" we want to scan all
285 * mmc bootdevs, not just the first. See
286 * bootdev_find_by_label() where this flag is
287 * set up
288 */
289 if (iter->method_flags &
290 BOOTFLOW_METHF_SINGLE_UCLASS) {
291 scan_next_in_uclass(&dev);
292 log_debug("looking for next device %s: %s\n",
293 iter->dev->name,
294 dev ? dev->name : "<none>");
295 } else {
296 dev = NULL;
297 }
298 if (!dev) {
299 log_debug("looking at next label\n");
300 ret = bootdev_next_label(iter, &dev,
301 &method_flags);
302 }
Simon Glass47aedc22023-01-17 10:48:14 -0700303 } else {
304 ret = bootdev_next_prio(iter, &dev);
305 method_flags = 0;
306 }
307 }
308 log_debug("ret=%d, dev=%p %s\n", ret, dev,
309 dev ? dev->name : "none");
310 if (ret) {
311 bootflow_iter_set_dev(iter, NULL, 0);
312 } else {
Simon Glass965020c2023-01-28 15:00:19 -0700313 /*
314 * Probe the bootdev. This does not probe any attached
315 * block device, since they are siblings
316 */
Simon Glass2b80bc12022-07-30 15:52:25 -0600317 ret = device_probe(dev);
Simon Glass47aedc22023-01-17 10:48:14 -0700318 log_debug("probe %s %d\n", dev->name, ret);
Simon Glass2b80bc12022-07-30 15:52:25 -0600319 if (!log_msg_ret("probe", ret))
Simon Glass47aedc22023-01-17 10:48:14 -0700320 bootflow_iter_set_dev(iter, dev, method_flags);
Simon Glass2b80bc12022-07-30 15:52:25 -0600321 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600322 }
323
324 /* if there are no more bootdevs, give up */
325 if (ret)
326 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
327
328 return 0;
329}
330
331/**
332 * bootflow_check() - Check if a bootflow can be obtained
333 *
334 * @iter: Provides part, bootmeth to use
335 * @bflow: Bootflow to update on success
336 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
337 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
338 */
339static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
340{
341 struct udevice *dev;
342 int ret;
343
Simon Glass2b80bc12022-07-30 15:52:25 -0600344 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
Simon Glass47aedc22023-01-17 10:48:14 -0700345 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass2b80bc12022-07-30 15:52:25 -0600346 ret = bootmeth_get_bootflow(iter->method, bflow);
347 if (ret)
348 return log_msg_ret("glob", ret);
349
350 return 0;
351 }
352
Simon Glassa8f5be12022-04-24 23:31:09 -0600353 dev = iter->dev;
354 ret = bootdev_get_bootflow(dev, iter, bflow);
355
356 /* If we got a valid bootflow, return it */
357 if (!ret) {
Simon Glass4de979f2023-07-12 09:04:32 -0600358 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
Simon Glassa8f5be12022-04-24 23:31:09 -0600359 dev->name, iter->part, iter->method->name);
360 return 0;
361 }
362
363 /* Unless there is nothing more to try, move to the next device */
Heinrich Schuchardt3f931222024-01-07 09:43:40 +0100364 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass4de979f2023-07-12 09:04:32 -0600365 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
Simon Glassa8f5be12022-04-24 23:31:09 -0600366 dev->name, iter->part, iter->method->name, ret);
367 /*
368 * For 'all' we return all bootflows, even
369 * those with errors
370 */
Simon Glass4f806f32023-02-22 12:17:03 -0700371 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glassa8f5be12022-04-24 23:31:09 -0600372 return log_msg_ret("all", ret);
373 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600374
Heinrich Schuchardt3f931222024-01-07 09:43:40 +0100375 return log_msg_ret("check", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600376}
377
Simon Glass4b7cb052023-01-17 10:48:16 -0700378int bootflow_scan_first(struct udevice *dev, const char *label,
379 struct bootflow_iter *iter, int flags,
380 struct bootflow *bflow)
Simon Glassa8f5be12022-04-24 23:31:09 -0600381{
382 int ret;
383
Simon Glass91943ff2023-01-17 10:48:15 -0700384 if (dev || label)
Simon Glass4f806f32023-02-22 12:17:03 -0700385 flags |= BOOTFLOWIF_SKIP_GLOBAL;
Simon Glassa8f5be12022-04-24 23:31:09 -0600386 bootflow_iter_init(iter, flags);
387
Simon Glass47aedc22023-01-17 10:48:14 -0700388 /*
389 * Set up the ordering of bootmeths. This sets iter->doing_global and
390 * iter->first_glob_method if we are starting with the global bootmeths
391 */
Simon Glass4f806f32023-02-22 12:17:03 -0700392 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
Simon Glassa8f5be12022-04-24 23:31:09 -0600393 if (ret)
394 return log_msg_ret("obmeth", -ENODEV);
395
396 /* Find the first bootmeth (there must be at least one!) */
397 iter->method = iter->method_order[iter->cur_method];
Simon Glass47aedc22023-01-17 10:48:14 -0700398
399 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
400 struct udevice *dev = NULL;
401 int method_flags;
402
Simon Glass91943ff2023-01-17 10:48:15 -0700403 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
Simon Glass47aedc22023-01-17 10:48:14 -0700404 if (ret)
405 return log_msg_ret("obdev", -ENODEV);
406
407 bootflow_iter_set_dev(iter, dev, method_flags);
408 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600409
410 ret = bootflow_check(iter, bflow);
411 if (ret) {
Simon Glassf738c732023-01-17 10:48:18 -0700412 log_debug("check - ret=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600413 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass4f806f32023-02-22 12:17:03 -0700414 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glassa8f5be12022-04-24 23:31:09 -0600415 return log_msg_ret("all", ret);
416 }
417 iter->err = ret;
418 ret = bootflow_scan_next(iter, bflow);
419 if (ret)
420 return log_msg_ret("get", ret);
421 }
422
423 return 0;
424}
425
Simon Glassa8f5be12022-04-24 23:31:09 -0600426int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
427{
428 int ret;
429
430 do {
431 ret = iter_incr(iter);
Simon Glassf738c732023-01-17 10:48:18 -0700432 log_debug("iter_incr: ret=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600433 if (ret == BF_NO_MORE_DEVICES)
434 return log_msg_ret("done", ret);
435
436 if (!ret) {
437 ret = bootflow_check(iter, bflow);
Simon Glassf738c732023-01-17 10:48:18 -0700438 log_debug("check - ret=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600439 if (!ret)
440 return 0;
441 iter->err = ret;
442 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass4f806f32023-02-22 12:17:03 -0700443 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glassa8f5be12022-04-24 23:31:09 -0600444 return log_msg_ret("all", ret);
445 }
446 } else {
Simon Glassf738c732023-01-17 10:48:18 -0700447 log_debug("incr failed, err=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600448 iter->err = ret;
449 }
450
451 } while (1);
452}
453
Simon Glassb190deb2022-10-20 18:22:51 -0600454void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
455 struct udevice *meth)
456{
457 memset(bflow, '\0', sizeof(*bflow));
458 bflow->dev = bootdev;
459 bflow->method = meth;
460 bflow->state = BOOTFLOWST_BASE;
461}
462
Simon Glassa8f5be12022-04-24 23:31:09 -0600463void bootflow_free(struct bootflow *bflow)
464{
465 free(bflow->name);
466 free(bflow->subdir);
467 free(bflow->fname);
Simon Glass741d1e92023-11-15 18:35:23 -0700468 if (!(bflow->flags & BOOTFLOWF_STATIC_BUF))
469 free(bflow->buf);
Simon Glass2175e762023-01-06 08:52:33 -0600470 free(bflow->os_name);
Simon Glass7638c852023-01-17 10:47:56 -0700471 free(bflow->fdt_fname);
Simon Glass76bd6842023-07-30 11:16:56 -0600472 free(bflow->bootmeth_priv);
Simon Glassa8f5be12022-04-24 23:31:09 -0600473}
474
475void bootflow_remove(struct bootflow *bflow)
476{
Simon Glasseccb25c2022-07-30 15:52:23 -0600477 if (bflow->dev)
478 list_del(&bflow->bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600479 list_del(&bflow->glob_node);
480
481 bootflow_free(bflow);
482 free(bflow);
483}
484
Simon Glassc2792242023-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 Glassa8f5be12022-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 Glass47dd6b42023-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 Glassa8f5be12022-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 Glass865328c2023-01-17 10:47:54 -0700564int bootflow_iter_check_blk(const struct bootflow_iter *iter)
Simon Glassa8f5be12022-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 Glass9c6d57d2023-01-28 15:00:25 -0700570 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
Simon Glassa8f5be12022-04-24 23:31:09 -0600571 return 0;
572
573 return -ENOTSUPP;
574}
575
Simon Glass0c1f4a92023-01-17 10:48:03 -0700576int bootflow_iter_check_sf(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_SPI_FLASH)
583 return 0;
584
585 return -ENOTSUPP;
586}
587
Simon Glass865328c2023-01-17 10:47:54 -0700588int bootflow_iter_check_net(const struct bootflow_iter *iter)
Simon Glassa8f5be12022-04-24 23:31:09 -0600589{
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_ETH)
595 return 0;
596
597 return -ENOTSUPP;
598}
599
Simon Glass865328c2023-01-17 10:47:54 -0700600int bootflow_iter_check_system(const struct bootflow_iter *iter)
Simon Glassa8f5be12022-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_BOOTSTD)
607 return 0;
608
609 return -ENOTSUPP;
610}
Simon Glassd42243f2023-07-12 09:04:35 -0600611
612/**
613 * bootflow_cmdline_set() - Set the command line for a bootflow
614 *
615 * @value: New command-line string
616 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
617 */
618int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
619{
620 char *cmdline = NULL;
621
622 if (value) {
623 cmdline = strdup(value);
624 if (!cmdline)
625 return -ENOMEM;
626 }
627
628 free(bflow->cmdline);
629 bflow->cmdline = cmdline;
630
631 return 0;
632}
633
634#ifdef CONFIG_BOOTSTD_FULL
635/**
636 * on_bootargs() - Update the cmdline of a bootflow
637 */
638static int on_bootargs(const char *name, const char *value, enum env_op op,
639 int flags)
640{
641 struct bootstd_priv *std;
642 struct bootflow *bflow;
643 int ret;
644
645 ret = bootstd_get_priv(&std);
646 if (ret)
647 return 0;
648 bflow = std->cur_bootflow;
649 if (!bflow)
650 return 0;
651
652 switch (op) {
653 case env_op_create:
654 case env_op_overwrite:
655 ret = bootflow_cmdline_set(bflow, value);
656 if (ret && ret != ENOENT)
657 return 1;
658 return 0;
659 case env_op_delete:
660 bootflow_cmdline_set(bflow, NULL);
661 fallthrough;
662 default:
663 return 0;
664 }
665}
666U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
667#endif
Simon Glassd07861c2023-07-12 09:04:38 -0600668
669/**
670 * copy_in() - Copy a string into a cmdline buffer
671 *
672 * @buf: Buffer to copy into
673 * @end: End of buffer (pointer to char after the end)
674 * @arg: String to copy from
675 * @len: Number of chars to copy from @arg (note that this is not usually the
676 * sane as strlen(arg) since the string may contain following arguments)
677 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
678 * with no '=' sign
679 * Returns: Number of chars written to @buf
680 */
681static int copy_in(char *buf, char *end, const char *arg, int len,
682 const char *new_val)
683{
684 char *to = buf;
685
686 /* copy the arg name */
687 if (to + len >= end)
688 return -E2BIG;
689 memcpy(to, arg, len);
690 to += len;
691
692 if (new_val == BOOTFLOWCL_EMPTY) {
693 /* no value */
694 } else {
695 bool need_quote = strchr(new_val, ' ');
696 len = strlen(new_val);
697
698 /* need space for value, equals sign and maybe two quotes */
699 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
700 return -E2BIG;
701 *to++ = '=';
702 if (need_quote)
703 *to++ = '"';
704 memcpy(to, new_val, len);
705 to += len;
706 if (need_quote)
707 *to++ = '"';
708 }
709
710 return to - buf;
711}
712
713int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
714 const char *set_arg, const char *new_val, int *posp)
715{
716 bool found_arg = false;
717 const char *from;
718 char *to, *end;
719 int set_arg_len;
720 char empty = '\0';
721 int ret;
722
723 from = cmdline ?: &empty;
724
725 /* check if the value has quotes inside */
726 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
727 return -EBADF;
728
729 set_arg_len = strlen(set_arg);
730 for (to = buf, end = buf + maxlen; *from;) {
731 const char *val, *arg_end, *val_end, *p;
732 bool in_quote;
733
734 if (to >= end)
735 return -E2BIG;
736 while (*from == ' ')
737 from++;
738 if (!*from)
739 break;
740
741 /* find the end of this arg */
742 val = NULL;
743 arg_end = NULL;
744 val_end = NULL;
745 in_quote = false;
746 for (p = from;; p++) {
747 if (in_quote) {
748 if (!*p)
749 return -EINVAL;
750 if (*p == '"')
751 in_quote = false;
752 continue;
753 }
Simon Glass19248dc2023-10-25 07:17:36 +1300754 if (*p == '=' && !arg_end) {
Simon Glassd07861c2023-07-12 09:04:38 -0600755 arg_end = p;
756 val = p + 1;
757 } else if (*p == '"') {
758 in_quote = true;
759 } else if (!*p || *p == ' ') {
760 val_end = p;
761 if (!arg_end)
762 arg_end = p;
763 break;
764 }
765 }
766 /*
767 * At this point val_end points to the end of the value, or the
768 * last char after the arg name, if there is no label.
769 * arg_end is the char after the arg name
770 * val points to the value, or NULL if there is none
771 * char after the value.
772 *
773 * fred=1234
774 * ^ ^^ ^
775 * from || |
776 * / \ \
777 * arg_end val val_end
778 */
779 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
780 (long)(arg_end - from), (long)(val - from),
781 (long)(val_end - from));
782
783 if (to != buf) {
784 if (to >= end)
785 return -E2BIG;
786 *to++ = ' ';
787 }
788
789 /* if this is the target arg, update it */
Simon Glass19248dc2023-10-25 07:17:36 +1300790 if (arg_end - from == set_arg_len &&
791 !strncmp(from, set_arg, set_arg_len)) {
Simon Glassd07861c2023-07-12 09:04:38 -0600792 if (!buf) {
793 bool has_quote = val_end[-1] == '"';
794
795 /*
796 * exclude any start/end quotes from
797 * calculations
798 */
799 if (!val)
800 val = val_end;
801 *posp = val - cmdline + has_quote;
802 return val_end - val - 2 * has_quote;
803 }
804 found_arg = true;
805 if (!new_val) {
806 /* delete this arg */
807 from = val_end + (*val_end == ' ');
808 log_debug("delete from: %s\n", from);
809 if (to != buf)
810 to--; /* drop the space we added */
811 continue;
812 }
813
814 ret = copy_in(to, end, from, arg_end - from, new_val);
815 if (ret < 0)
816 return ret;
817 to += ret;
818
819 /* if not the target arg, copy it unchanged */
820 } else if (to) {
821 int len;
822
823 len = val_end - from;
824 if (to + len >= end)
825 return -E2BIG;
826 memcpy(to, from, len);
827 to += len;
828 }
829 from = val_end;
830 }
831
832 /* If we didn't find the arg, add it */
833 if (!found_arg) {
834 /* trying to delete something that is not there */
835 if (!new_val || !buf)
836 return -ENOENT;
837 if (to >= end)
838 return -E2BIG;
839
840 /* add a space to separate it from the previous arg */
841 if (to != buf && to[-1] != ' ')
842 *to++ = ' ';
843 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
844 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
845 if (ret < 0)
846 return ret;
847 to += ret;
848 }
849
850 /* delete any trailing space */
851 if (to > buf && to[-1] == ' ')
852 to--;
853
854 if (to >= end)
855 return -E2BIG;
856 *to++ = '\0';
857
858 return to - buf;
859}
Simon Glass82c09382023-07-12 09:04:39 -0600860
861int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
862 const char *new_val, bool set_env)
863{
864 char buf[2048];
865 char *cmd = NULL;
866 int ret;
867
868 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
869 new_val, NULL);
870 if (ret < 0)
871 return ret;
872
873 ret = bootflow_cmdline_set(bflow, buf);
874 if (*buf) {
875 cmd = strdup(buf);
876 if (!cmd)
877 return -ENOMEM;
878 }
879 free(bflow->cmdline);
880 bflow->cmdline = cmd;
881
882 if (set_env) {
883 ret = env_set("bootargs", bflow->cmdline);
884 if (ret)
885 return ret;
886 }
887
888 return 0;
889}
890
891int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
892{
893 int ret;
894
895 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
896
897 return ret;
898}
899
900int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
901 const char **val)
902{
903 int ret;
904 int pos;
905
906 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
907 if (ret < 0)
908 return ret;
909 *val = bflow->cmdline + pos;
910
911 return ret;
912}
Simon Glass33ebcb42023-07-12 09:04:42 -0600913
914int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
915{
916 struct serial_device_info info;
917 char buf[50];
918 int ret;
919
920 ret = serial_getinfo(gd->cur_serial_dev, &info);
921 if (ret)
922 return ret;
923
924 *buf = '\0';
925 if (!strcmp("earlycon", arg)) {
926 snprintf(buf, sizeof(buf),
927 "uart8250,mmio32,%#lx,%dn8", info.addr,
928 info.baudrate);
929 } else if (!strcmp("console", arg)) {
930 snprintf(buf, sizeof(buf),
931 "ttyS0,%dn8", info.baudrate);
932 }
933
934 if (!*buf) {
935 printf("Unknown param '%s\n", arg);
936 return -ENOENT;
937 }
938
939 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
940 if (ret)
941 return ret;
942
943 return 0;
944}