blob: 6ef62e1d1896637d1feaf127e0def36c7a341e3d [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/**
159 * iter_incr() - Move to the next item (method, part, bootdev)
160 *
161 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
162 */
163static int iter_incr(struct bootflow_iter *iter)
164{
165 struct udevice *dev;
Simon Glass2b80bc12022-07-30 15:52:25 -0600166 bool inc_dev = true;
167 bool global;
Simon Glassa8f5be12022-04-24 23:31:09 -0600168 int ret;
169
Simon Glass47aedc22023-01-17 10:48:14 -0700170 log_debug("entry: err=%d\n", iter->err);
Simon Glass2b80bc12022-07-30 15:52:25 -0600171 global = iter->doing_global;
172
Simon Glassa8f5be12022-04-24 23:31:09 -0600173 if (iter->err == BF_NO_MORE_DEVICES)
174 return BF_NO_MORE_DEVICES;
175
176 if (iter->err != BF_NO_MORE_PARTS) {
177 /* Get the next boothmethod */
178 if (++iter->cur_method < iter->num_methods) {
179 iter->method = iter->method_order[iter->cur_method];
180 return 0;
181 }
Simon Glass2b80bc12022-07-30 15:52:25 -0600182
183 /*
184 * If we have finished scanning the global bootmeths, start the
185 * normal bootdev scan
186 */
187 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
188 iter->num_methods = iter->first_glob_method;
189 iter->doing_global = false;
190
191 /*
192 * Don't move to the next dev as we haven't tried this
193 * one yet!
194 */
195 inc_dev = false;
196 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600197 }
198
199 /* No more bootmeths; start at the first one, and... */
200 iter->cur_method = 0;
201 iter->method = iter->method_order[iter->cur_method];
202
203 if (iter->err != BF_NO_MORE_PARTS) {
204 /* ...select next partition */
205 if (++iter->part <= iter->max_part)
206 return 0;
207 }
208
Simon Glass47aedc22023-01-17 10:48:14 -0700209 /* No more partitions; start at the first one and... */
Simon Glassa8f5be12022-04-24 23:31:09 -0600210 iter->part = 0;
211
212 /*
213 * Note: as far as we know, there is no partition table on the next
214 * bootdev, so set max_part to 0 until we discover otherwise. See
215 * bootdev_find_in_blk() for where this is set.
216 */
217 iter->max_part = 0;
218
219 /* ...select next bootdev */
Simon Glass4f806f32023-02-22 12:17:03 -0700220 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
Simon Glassa8f5be12022-04-24 23:31:09 -0600221 ret = -ENOENT;
Simon Glassa8f5be12022-04-24 23:31:09 -0600222 } else {
Simon Glass47aedc22023-01-17 10:48:14 -0700223 int method_flags;
224
225 ret = 0;
226 dev = iter->dev;
227 log_debug("inc_dev=%d\n", inc_dev);
228 if (!inc_dev) {
Simon Glass91943ff2023-01-17 10:48:15 -0700229 ret = bootdev_setup_iter(iter, NULL, &dev,
230 &method_flags);
231 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass4f806f32023-02-22 12:17:03 -0700232 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
Simon Glass91943ff2023-01-17 10:48:15 -0700233 /* Move to the next bootdev in this uclass */
234 uclass_find_next_device(&dev);
235 if (!dev) {
236 log_debug("finished uclass %s\n",
237 dev_get_uclass_name(dev));
238 ret = -ENODEV;
239 }
240 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass4f806f32023-02-22 12:17:03 -0700241 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
Simon Glass91943ff2023-01-17 10:48:15 -0700242 log_debug("next in single\n");
243 method_flags = 0;
244 do {
245 /*
246 * Move to the next bootdev child of this media
247 * device. This ensures that we cover all the
248 * available SCSI IDs and LUNs.
249 */
250 device_find_next_child(&dev);
251 log_debug("- next %s\n",
252 dev ? dev->name : "(none)");
253 } while (dev && device_get_uclass_id(dev) !=
254 UCLASS_BOOTDEV);
255 if (!dev) {
256 log_debug("finished uclass %s\n",
257 dev_get_uclass_name(dev));
258 ret = -ENODEV;
259 }
Simon Glass2b80bc12022-07-30 15:52:25 -0600260 } else {
Simon Glass47aedc22023-01-17 10:48:14 -0700261 log_debug("labels %p\n", iter->labels);
262 if (iter->labels) {
263 ret = bootdev_next_label(iter, &dev,
264 &method_flags);
265 } else {
266 ret = bootdev_next_prio(iter, &dev);
267 method_flags = 0;
268 }
269 }
270 log_debug("ret=%d, dev=%p %s\n", ret, dev,
271 dev ? dev->name : "none");
272 if (ret) {
273 bootflow_iter_set_dev(iter, NULL, 0);
274 } else {
Simon Glass965020c2023-01-28 15:00:19 -0700275 /*
276 * Probe the bootdev. This does not probe any attached
277 * block device, since they are siblings
278 */
Simon Glass2b80bc12022-07-30 15:52:25 -0600279 ret = device_probe(dev);
Simon Glass47aedc22023-01-17 10:48:14 -0700280 log_debug("probe %s %d\n", dev->name, ret);
Simon Glass2b80bc12022-07-30 15:52:25 -0600281 if (!log_msg_ret("probe", ret))
Simon Glass47aedc22023-01-17 10:48:14 -0700282 bootflow_iter_set_dev(iter, dev, method_flags);
Simon Glass2b80bc12022-07-30 15:52:25 -0600283 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600284 }
285
286 /* if there are no more bootdevs, give up */
287 if (ret)
288 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
289
290 return 0;
291}
292
293/**
294 * bootflow_check() - Check if a bootflow can be obtained
295 *
296 * @iter: Provides part, bootmeth to use
297 * @bflow: Bootflow to update on success
298 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
299 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
300 */
301static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
302{
303 struct udevice *dev;
304 int ret;
305
Simon Glass2b80bc12022-07-30 15:52:25 -0600306 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
Simon Glass47aedc22023-01-17 10:48:14 -0700307 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass2b80bc12022-07-30 15:52:25 -0600308 ret = bootmeth_get_bootflow(iter->method, bflow);
309 if (ret)
310 return log_msg_ret("glob", ret);
311
312 return 0;
313 }
314
Simon Glassa8f5be12022-04-24 23:31:09 -0600315 dev = iter->dev;
316 ret = bootdev_get_bootflow(dev, iter, bflow);
317
318 /* If we got a valid bootflow, return it */
319 if (!ret) {
Simon Glass4de979f2023-07-12 09:04:32 -0600320 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
Simon Glassa8f5be12022-04-24 23:31:09 -0600321 dev->name, iter->part, iter->method->name);
322 return 0;
323 }
324
325 /* Unless there is nothing more to try, move to the next device */
326 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass4de979f2023-07-12 09:04:32 -0600327 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
Simon Glassa8f5be12022-04-24 23:31:09 -0600328 dev->name, iter->part, iter->method->name, ret);
329 /*
330 * For 'all' we return all bootflows, even
331 * those with errors
332 */
Simon Glass4f806f32023-02-22 12:17:03 -0700333 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glassa8f5be12022-04-24 23:31:09 -0600334 return log_msg_ret("all", ret);
335 }
336 if (ret)
337 return log_msg_ret("check", ret);
338
339 return 0;
340}
341
Simon Glass4b7cb052023-01-17 10:48:16 -0700342int bootflow_scan_first(struct udevice *dev, const char *label,
343 struct bootflow_iter *iter, int flags,
344 struct bootflow *bflow)
Simon Glassa8f5be12022-04-24 23:31:09 -0600345{
346 int ret;
347
Simon Glass91943ff2023-01-17 10:48:15 -0700348 if (dev || label)
Simon Glass4f806f32023-02-22 12:17:03 -0700349 flags |= BOOTFLOWIF_SKIP_GLOBAL;
Simon Glassa8f5be12022-04-24 23:31:09 -0600350 bootflow_iter_init(iter, flags);
351
Simon Glass47aedc22023-01-17 10:48:14 -0700352 /*
353 * Set up the ordering of bootmeths. This sets iter->doing_global and
354 * iter->first_glob_method if we are starting with the global bootmeths
355 */
Simon Glass4f806f32023-02-22 12:17:03 -0700356 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
Simon Glassa8f5be12022-04-24 23:31:09 -0600357 if (ret)
358 return log_msg_ret("obmeth", -ENODEV);
359
360 /* Find the first bootmeth (there must be at least one!) */
361 iter->method = iter->method_order[iter->cur_method];
Simon Glass47aedc22023-01-17 10:48:14 -0700362
363 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
364 struct udevice *dev = NULL;
365 int method_flags;
366
Simon Glass91943ff2023-01-17 10:48:15 -0700367 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
Simon Glass47aedc22023-01-17 10:48:14 -0700368 if (ret)
369 return log_msg_ret("obdev", -ENODEV);
370
371 bootflow_iter_set_dev(iter, dev, method_flags);
372 }
Simon Glassa8f5be12022-04-24 23:31:09 -0600373
374 ret = bootflow_check(iter, bflow);
375 if (ret) {
Simon Glassf738c732023-01-17 10:48:18 -0700376 log_debug("check - ret=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600377 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass4f806f32023-02-22 12:17:03 -0700378 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glassa8f5be12022-04-24 23:31:09 -0600379 return log_msg_ret("all", ret);
380 }
381 iter->err = ret;
382 ret = bootflow_scan_next(iter, bflow);
383 if (ret)
384 return log_msg_ret("get", ret);
385 }
386
387 return 0;
388}
389
Simon Glassa8f5be12022-04-24 23:31:09 -0600390int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
391{
392 int ret;
393
394 do {
395 ret = iter_incr(iter);
Simon Glassf738c732023-01-17 10:48:18 -0700396 log_debug("iter_incr: ret=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600397 if (ret == BF_NO_MORE_DEVICES)
398 return log_msg_ret("done", ret);
399
400 if (!ret) {
401 ret = bootflow_check(iter, bflow);
Simon Glassf738c732023-01-17 10:48:18 -0700402 log_debug("check - ret=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600403 if (!ret)
404 return 0;
405 iter->err = ret;
406 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass4f806f32023-02-22 12:17:03 -0700407 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glassa8f5be12022-04-24 23:31:09 -0600408 return log_msg_ret("all", ret);
409 }
410 } else {
Simon Glassf738c732023-01-17 10:48:18 -0700411 log_debug("incr failed, err=%d\n", ret);
Simon Glassa8f5be12022-04-24 23:31:09 -0600412 iter->err = ret;
413 }
414
415 } while (1);
416}
417
Simon Glassb190deb2022-10-20 18:22:51 -0600418void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
419 struct udevice *meth)
420{
421 memset(bflow, '\0', sizeof(*bflow));
422 bflow->dev = bootdev;
423 bflow->method = meth;
424 bflow->state = BOOTFLOWST_BASE;
425}
426
Simon Glassa8f5be12022-04-24 23:31:09 -0600427void bootflow_free(struct bootflow *bflow)
428{
429 free(bflow->name);
430 free(bflow->subdir);
431 free(bflow->fname);
432 free(bflow->buf);
Simon Glass2175e762023-01-06 08:52:33 -0600433 free(bflow->os_name);
Simon Glass7638c852023-01-17 10:47:56 -0700434 free(bflow->fdt_fname);
Simon Glass76bd6842023-07-30 11:16:56 -0600435 free(bflow->bootmeth_priv);
Simon Glassa8f5be12022-04-24 23:31:09 -0600436}
437
438void bootflow_remove(struct bootflow *bflow)
439{
Simon Glasseccb25c2022-07-30 15:52:23 -0600440 if (bflow->dev)
441 list_del(&bflow->bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600442 list_del(&bflow->glob_node);
443
444 bootflow_free(bflow);
445 free(bflow);
446}
447
Simon Glassc2792242023-08-10 19:33:18 -0600448#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
449int bootflow_read_all(struct bootflow *bflow)
450{
451 int ret;
452
453 if (bflow->state != BOOTFLOWST_READY)
454 return log_msg_ret("rd", -EPROTO);
455
456 ret = bootmeth_read_all(bflow->method, bflow);
457 if (ret)
458 return log_msg_ret("rd2", ret);
459
460 return 0;
461}
462#endif /* BOOTSTD_FULL */
463
Simon Glassa8f5be12022-04-24 23:31:09 -0600464int bootflow_boot(struct bootflow *bflow)
465{
466 int ret;
467
468 if (bflow->state != BOOTFLOWST_READY)
469 return log_msg_ret("load", -EPROTO);
470
471 ret = bootmeth_boot(bflow->method, bflow);
472 if (ret)
473 return log_msg_ret("boot", ret);
474
475 /*
476 * internal error, should not get here since we should have booted
477 * something or returned an error
478 */
479
480 return log_msg_ret("end", -EFAULT);
481}
482
483int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
484{
485 int ret;
486
487 printf("** Booting bootflow '%s' with %s\n", bflow->name,
488 bflow->method->name);
Simon Glass47dd6b42023-02-22 12:17:04 -0700489 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
490 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
491 printf("Using prior-stage device tree\n");
Simon Glassa8f5be12022-04-24 23:31:09 -0600492 ret = bootflow_boot(bflow);
493 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
494 printf("Boot failed (err=%d)\n", ret);
495 return ret;
496 }
497
498 switch (ret) {
499 case -EPROTO:
500 printf("Bootflow not loaded (state '%s')\n",
501 bootflow_state_get_name(bflow->state));
502 break;
503 case -ENOSYS:
504 printf("Boot method '%s' not supported\n", bflow->method->name);
505 break;
506 case -ENOTSUPP:
507 /* Disable this bootflow for this iteration */
508 if (iter) {
509 int ret2;
510
511 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
512 if (!ret2) {
513 printf("Boot method '%s' failed and will not be retried\n",
514 bflow->method->name);
515 }
516 }
517
518 break;
519 default:
520 printf("Boot failed (err=%d)\n", ret);
521 break;
522 }
523
524 return ret;
525}
526
Simon Glass865328c2023-01-17 10:47:54 -0700527int bootflow_iter_check_blk(const struct bootflow_iter *iter)
Simon Glassa8f5be12022-04-24 23:31:09 -0600528{
529 const struct udevice *media = dev_get_parent(iter->dev);
530 enum uclass_id id = device_get_uclass_id(media);
531
532 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
Simon Glass9c6d57d2023-01-28 15:00:25 -0700533 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
Simon Glassa8f5be12022-04-24 23:31:09 -0600534 return 0;
535
536 return -ENOTSUPP;
537}
538
Simon Glass0c1f4a92023-01-17 10:48:03 -0700539int bootflow_iter_check_sf(const struct bootflow_iter *iter)
540{
541 const struct udevice *media = dev_get_parent(iter->dev);
542 enum uclass_id id = device_get_uclass_id(media);
543
544 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
545 if (id == UCLASS_SPI_FLASH)
546 return 0;
547
548 return -ENOTSUPP;
549}
550
Simon Glass865328c2023-01-17 10:47:54 -0700551int bootflow_iter_check_net(const struct bootflow_iter *iter)
Simon Glassa8f5be12022-04-24 23:31:09 -0600552{
553 const struct udevice *media = dev_get_parent(iter->dev);
554 enum uclass_id id = device_get_uclass_id(media);
555
556 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
557 if (id == UCLASS_ETH)
558 return 0;
559
560 return -ENOTSUPP;
561}
562
Simon Glass865328c2023-01-17 10:47:54 -0700563int bootflow_iter_check_system(const struct bootflow_iter *iter)
Simon Glassa8f5be12022-04-24 23:31:09 -0600564{
565 const struct udevice *media = dev_get_parent(iter->dev);
566 enum uclass_id id = device_get_uclass_id(media);
567
568 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
569 if (id == UCLASS_BOOTSTD)
570 return 0;
571
572 return -ENOTSUPP;
573}
Simon Glassd42243f2023-07-12 09:04:35 -0600574
575/**
576 * bootflow_cmdline_set() - Set the command line for a bootflow
577 *
578 * @value: New command-line string
579 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
580 */
581int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
582{
583 char *cmdline = NULL;
584
585 if (value) {
586 cmdline = strdup(value);
587 if (!cmdline)
588 return -ENOMEM;
589 }
590
591 free(bflow->cmdline);
592 bflow->cmdline = cmdline;
593
594 return 0;
595}
596
597#ifdef CONFIG_BOOTSTD_FULL
598/**
599 * on_bootargs() - Update the cmdline of a bootflow
600 */
601static int on_bootargs(const char *name, const char *value, enum env_op op,
602 int flags)
603{
604 struct bootstd_priv *std;
605 struct bootflow *bflow;
606 int ret;
607
608 ret = bootstd_get_priv(&std);
609 if (ret)
610 return 0;
611 bflow = std->cur_bootflow;
612 if (!bflow)
613 return 0;
614
615 switch (op) {
616 case env_op_create:
617 case env_op_overwrite:
618 ret = bootflow_cmdline_set(bflow, value);
619 if (ret && ret != ENOENT)
620 return 1;
621 return 0;
622 case env_op_delete:
623 bootflow_cmdline_set(bflow, NULL);
624 fallthrough;
625 default:
626 return 0;
627 }
628}
629U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
630#endif
Simon Glassd07861c2023-07-12 09:04:38 -0600631
632/**
633 * copy_in() - Copy a string into a cmdline buffer
634 *
635 * @buf: Buffer to copy into
636 * @end: End of buffer (pointer to char after the end)
637 * @arg: String to copy from
638 * @len: Number of chars to copy from @arg (note that this is not usually the
639 * sane as strlen(arg) since the string may contain following arguments)
640 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
641 * with no '=' sign
642 * Returns: Number of chars written to @buf
643 */
644static int copy_in(char *buf, char *end, const char *arg, int len,
645 const char *new_val)
646{
647 char *to = buf;
648
649 /* copy the arg name */
650 if (to + len >= end)
651 return -E2BIG;
652 memcpy(to, arg, len);
653 to += len;
654
655 if (new_val == BOOTFLOWCL_EMPTY) {
656 /* no value */
657 } else {
658 bool need_quote = strchr(new_val, ' ');
659 len = strlen(new_val);
660
661 /* need space for value, equals sign and maybe two quotes */
662 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
663 return -E2BIG;
664 *to++ = '=';
665 if (need_quote)
666 *to++ = '"';
667 memcpy(to, new_val, len);
668 to += len;
669 if (need_quote)
670 *to++ = '"';
671 }
672
673 return to - buf;
674}
675
676int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
677 const char *set_arg, const char *new_val, int *posp)
678{
679 bool found_arg = false;
680 const char *from;
681 char *to, *end;
682 int set_arg_len;
683 char empty = '\0';
684 int ret;
685
686 from = cmdline ?: &empty;
687
688 /* check if the value has quotes inside */
689 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
690 return -EBADF;
691
692 set_arg_len = strlen(set_arg);
693 for (to = buf, end = buf + maxlen; *from;) {
694 const char *val, *arg_end, *val_end, *p;
695 bool in_quote;
696
697 if (to >= end)
698 return -E2BIG;
699 while (*from == ' ')
700 from++;
701 if (!*from)
702 break;
703
704 /* find the end of this arg */
705 val = NULL;
706 arg_end = NULL;
707 val_end = NULL;
708 in_quote = false;
709 for (p = from;; p++) {
710 if (in_quote) {
711 if (!*p)
712 return -EINVAL;
713 if (*p == '"')
714 in_quote = false;
715 continue;
716 }
717 if (*p == '=') {
718 arg_end = p;
719 val = p + 1;
720 } else if (*p == '"') {
721 in_quote = true;
722 } else if (!*p || *p == ' ') {
723 val_end = p;
724 if (!arg_end)
725 arg_end = p;
726 break;
727 }
728 }
729 /*
730 * At this point val_end points to the end of the value, or the
731 * last char after the arg name, if there is no label.
732 * arg_end is the char after the arg name
733 * val points to the value, or NULL if there is none
734 * char after the value.
735 *
736 * fred=1234
737 * ^ ^^ ^
738 * from || |
739 * / \ \
740 * arg_end val val_end
741 */
742 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
743 (long)(arg_end - from), (long)(val - from),
744 (long)(val_end - from));
745
746 if (to != buf) {
747 if (to >= end)
748 return -E2BIG;
749 *to++ = ' ';
750 }
751
752 /* if this is the target arg, update it */
753 if (!strncmp(from, set_arg, arg_end - from)) {
754 if (!buf) {
755 bool has_quote = val_end[-1] == '"';
756
757 /*
758 * exclude any start/end quotes from
759 * calculations
760 */
761 if (!val)
762 val = val_end;
763 *posp = val - cmdline + has_quote;
764 return val_end - val - 2 * has_quote;
765 }
766 found_arg = true;
767 if (!new_val) {
768 /* delete this arg */
769 from = val_end + (*val_end == ' ');
770 log_debug("delete from: %s\n", from);
771 if (to != buf)
772 to--; /* drop the space we added */
773 continue;
774 }
775
776 ret = copy_in(to, end, from, arg_end - from, new_val);
777 if (ret < 0)
778 return ret;
779 to += ret;
780
781 /* if not the target arg, copy it unchanged */
782 } else if (to) {
783 int len;
784
785 len = val_end - from;
786 if (to + len >= end)
787 return -E2BIG;
788 memcpy(to, from, len);
789 to += len;
790 }
791 from = val_end;
792 }
793
794 /* If we didn't find the arg, add it */
795 if (!found_arg) {
796 /* trying to delete something that is not there */
797 if (!new_val || !buf)
798 return -ENOENT;
799 if (to >= end)
800 return -E2BIG;
801
802 /* add a space to separate it from the previous arg */
803 if (to != buf && to[-1] != ' ')
804 *to++ = ' ';
805 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
806 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
807 if (ret < 0)
808 return ret;
809 to += ret;
810 }
811
812 /* delete any trailing space */
813 if (to > buf && to[-1] == ' ')
814 to--;
815
816 if (to >= end)
817 return -E2BIG;
818 *to++ = '\0';
819
820 return to - buf;
821}
Simon Glass82c09382023-07-12 09:04:39 -0600822
823int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
824 const char *new_val, bool set_env)
825{
826 char buf[2048];
827 char *cmd = NULL;
828 int ret;
829
830 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
831 new_val, NULL);
832 if (ret < 0)
833 return ret;
834
835 ret = bootflow_cmdline_set(bflow, buf);
836 if (*buf) {
837 cmd = strdup(buf);
838 if (!cmd)
839 return -ENOMEM;
840 }
841 free(bflow->cmdline);
842 bflow->cmdline = cmd;
843
844 if (set_env) {
845 ret = env_set("bootargs", bflow->cmdline);
846 if (ret)
847 return ret;
848 }
849
850 return 0;
851}
852
853int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
854{
855 int ret;
856
857 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
858
859 return ret;
860}
861
862int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
863 const char **val)
864{
865 int ret;
866 int pos;
867
868 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
869 if (ret < 0)
870 return ret;
871 *val = bflow->cmdline + pos;
872
873 return ret;
874}
Simon Glass33ebcb42023-07-12 09:04:42 -0600875
876int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
877{
878 struct serial_device_info info;
879 char buf[50];
880 int ret;
881
882 ret = serial_getinfo(gd->cur_serial_dev, &info);
883 if (ret)
884 return ret;
885
886 *buf = '\0';
887 if (!strcmp("earlycon", arg)) {
888 snprintf(buf, sizeof(buf),
889 "uart8250,mmio32,%#lx,%dn8", info.addr,
890 info.baudrate);
891 } else if (!strcmp("console", arg)) {
892 snprintf(buf, sizeof(buf),
893 "ttyS0,%dn8", info.baudrate);
894 }
895
896 if (!*buf) {
897 printf("Unknown param '%s\n", arg);
898 return -ENOENT;
899 }
900
901 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
902 if (ret)
903 return ret;
904
905 return 0;
906}