blob: 26f26aee38a3866cc05a75cf844be37e3c28f177 [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
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 Glassb35513a2023-07-12 09:04:35 -060015#include <env_internal.h>
Simon Glass03fcbf92022-04-24 23:31:09 -060016#include <malloc.h>
17#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/**
158 * iter_incr() - Move to the next item (method, part, bootdev)
159 *
160 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
161 */
162static int iter_incr(struct bootflow_iter *iter)
163{
164 struct udevice *dev;
Simon Glass73fcf512022-07-30 15:52:25 -0600165 bool inc_dev = true;
166 bool global;
Simon Glass03fcbf92022-04-24 23:31:09 -0600167 int ret;
168
Simon Glass484e4072023-01-17 10:48:14 -0700169 log_debug("entry: err=%d\n", iter->err);
Simon Glass73fcf512022-07-30 15:52:25 -0600170 global = iter->doing_global;
171
Simon Glass03fcbf92022-04-24 23:31:09 -0600172 if (iter->err == BF_NO_MORE_DEVICES)
173 return BF_NO_MORE_DEVICES;
174
175 if (iter->err != BF_NO_MORE_PARTS) {
176 /* Get the next boothmethod */
177 if (++iter->cur_method < iter->num_methods) {
178 iter->method = iter->method_order[iter->cur_method];
179 return 0;
180 }
Simon Glass73fcf512022-07-30 15:52:25 -0600181
182 /*
183 * If we have finished scanning the global bootmeths, start the
184 * normal bootdev scan
185 */
186 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
187 iter->num_methods = iter->first_glob_method;
188 iter->doing_global = false;
189
190 /*
191 * Don't move to the next dev as we haven't tried this
192 * one yet!
193 */
194 inc_dev = false;
195 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600196 }
197
198 /* No more bootmeths; start at the first one, and... */
199 iter->cur_method = 0;
200 iter->method = iter->method_order[iter->cur_method];
201
202 if (iter->err != BF_NO_MORE_PARTS) {
203 /* ...select next partition */
204 if (++iter->part <= iter->max_part)
205 return 0;
206 }
207
Simon Glass484e4072023-01-17 10:48:14 -0700208 /* No more partitions; start at the first one and... */
Simon Glass03fcbf92022-04-24 23:31:09 -0600209 iter->part = 0;
210
211 /*
212 * Note: as far as we know, there is no partition table on the next
213 * bootdev, so set max_part to 0 until we discover otherwise. See
214 * bootdev_find_in_blk() for where this is set.
215 */
216 iter->max_part = 0;
217
218 /* ...select next bootdev */
Simon Glass99e68182023-02-22 12:17:03 -0700219 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
Simon Glass03fcbf92022-04-24 23:31:09 -0600220 ret = -ENOENT;
Simon Glass03fcbf92022-04-24 23:31:09 -0600221 } else {
Simon Glass484e4072023-01-17 10:48:14 -0700222 int method_flags;
223
224 ret = 0;
225 dev = iter->dev;
226 log_debug("inc_dev=%d\n", inc_dev);
227 if (!inc_dev) {
Simon Glassba3d5372023-01-17 10:48:15 -0700228 ret = bootdev_setup_iter(iter, NULL, &dev,
229 &method_flags);
230 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass99e68182023-02-22 12:17:03 -0700231 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
Simon Glassba3d5372023-01-17 10:48:15 -0700232 /* Move to the next bootdev in this uclass */
233 uclass_find_next_device(&dev);
234 if (!dev) {
235 log_debug("finished uclass %s\n",
236 dev_get_uclass_name(dev));
237 ret = -ENODEV;
238 }
239 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
Simon Glass99e68182023-02-22 12:17:03 -0700240 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
Simon Glassba3d5372023-01-17 10:48:15 -0700241 log_debug("next in single\n");
242 method_flags = 0;
243 do {
244 /*
245 * Move to the next bootdev child of this media
246 * device. This ensures that we cover all the
247 * available SCSI IDs and LUNs.
248 */
249 device_find_next_child(&dev);
250 log_debug("- next %s\n",
251 dev ? dev->name : "(none)");
252 } while (dev && device_get_uclass_id(dev) !=
253 UCLASS_BOOTDEV);
254 if (!dev) {
255 log_debug("finished uclass %s\n",
256 dev_get_uclass_name(dev));
257 ret = -ENODEV;
258 }
Simon Glass484e4072023-01-17 10:48:14 -0700259 } else {
260 log_debug("labels %p\n", iter->labels);
261 if (iter->labels) {
262 ret = bootdev_next_label(iter, &dev,
263 &method_flags);
264 } else {
265 ret = bootdev_next_prio(iter, &dev);
266 method_flags = 0;
267 }
268 }
269 log_debug("ret=%d, dev=%p %s\n", ret, dev,
270 dev ? dev->name : "none");
271 if (ret) {
272 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass73fcf512022-07-30 15:52:25 -0600273 } else {
Simon Glassb73a8292023-01-28 15:00:19 -0700274 /*
275 * Probe the bootdev. This does not probe any attached
276 * block device, since they are siblings
277 */
Simon Glass73fcf512022-07-30 15:52:25 -0600278 ret = device_probe(dev);
Simon Glass484e4072023-01-17 10:48:14 -0700279 log_debug("probe %s %d\n", dev->name, ret);
Simon Glass73fcf512022-07-30 15:52:25 -0600280 if (!log_msg_ret("probe", ret))
Simon Glass484e4072023-01-17 10:48:14 -0700281 bootflow_iter_set_dev(iter, dev, method_flags);
Simon Glass73fcf512022-07-30 15:52:25 -0600282 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600283 }
284
285 /* if there are no more bootdevs, give up */
286 if (ret)
287 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
288
289 return 0;
290}
291
292/**
293 * bootflow_check() - Check if a bootflow can be obtained
294 *
295 * @iter: Provides part, bootmeth to use
296 * @bflow: Bootflow to update on success
297 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
298 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
299 */
300static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
301{
302 struct udevice *dev;
303 int ret;
304
Simon Glass73fcf512022-07-30 15:52:25 -0600305 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
Simon Glass484e4072023-01-17 10:48:14 -0700306 bootflow_iter_set_dev(iter, NULL, 0);
Simon Glass73fcf512022-07-30 15:52:25 -0600307 ret = bootmeth_get_bootflow(iter->method, bflow);
308 if (ret)
309 return log_msg_ret("glob", ret);
310
311 return 0;
312 }
313
Simon Glass03fcbf92022-04-24 23:31:09 -0600314 dev = iter->dev;
315 ret = bootdev_get_bootflow(dev, iter, bflow);
316
317 /* If we got a valid bootflow, return it */
318 if (!ret) {
Simon Glass41571582023-07-12 09:04:32 -0600319 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
Simon Glass03fcbf92022-04-24 23:31:09 -0600320 dev->name, iter->part, iter->method->name);
321 return 0;
322 }
323
324 /* Unless there is nothing more to try, move to the next device */
325 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass41571582023-07-12 09:04:32 -0600326 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
Simon Glass03fcbf92022-04-24 23:31:09 -0600327 dev->name, iter->part, iter->method->name, ret);
328 /*
329 * For 'all' we return all bootflows, even
330 * those with errors
331 */
Simon Glass99e68182023-02-22 12:17:03 -0700332 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600333 return log_msg_ret("all", ret);
334 }
335 if (ret)
336 return log_msg_ret("check", ret);
337
338 return 0;
339}
340
Simon Glass5d3d44f2023-01-17 10:48:16 -0700341int bootflow_scan_first(struct udevice *dev, const char *label,
342 struct bootflow_iter *iter, int flags,
343 struct bootflow *bflow)
Simon Glass03fcbf92022-04-24 23:31:09 -0600344{
345 int ret;
346
Simon Glassba3d5372023-01-17 10:48:15 -0700347 if (dev || label)
Simon Glass99e68182023-02-22 12:17:03 -0700348 flags |= BOOTFLOWIF_SKIP_GLOBAL;
Simon Glass03fcbf92022-04-24 23:31:09 -0600349 bootflow_iter_init(iter, flags);
350
Simon Glass484e4072023-01-17 10:48:14 -0700351 /*
352 * Set up the ordering of bootmeths. This sets iter->doing_global and
353 * iter->first_glob_method if we are starting with the global bootmeths
354 */
Simon Glass99e68182023-02-22 12:17:03 -0700355 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
Simon Glass03fcbf92022-04-24 23:31:09 -0600356 if (ret)
357 return log_msg_ret("obmeth", -ENODEV);
358
359 /* Find the first bootmeth (there must be at least one!) */
360 iter->method = iter->method_order[iter->cur_method];
Simon Glass484e4072023-01-17 10:48:14 -0700361
362 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
363 struct udevice *dev = NULL;
364 int method_flags;
365
Simon Glassba3d5372023-01-17 10:48:15 -0700366 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
Simon Glass484e4072023-01-17 10:48:14 -0700367 if (ret)
368 return log_msg_ret("obdev", -ENODEV);
369
370 bootflow_iter_set_dev(iter, dev, method_flags);
371 }
Simon Glass03fcbf92022-04-24 23:31:09 -0600372
373 ret = bootflow_check(iter, bflow);
374 if (ret) {
Simon Glass33063462023-01-17 10:48:18 -0700375 log_debug("check - ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600376 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass99e68182023-02-22 12:17:03 -0700377 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600378 return log_msg_ret("all", ret);
379 }
380 iter->err = ret;
381 ret = bootflow_scan_next(iter, bflow);
382 if (ret)
383 return log_msg_ret("get", ret);
384 }
385
386 return 0;
387}
388
Simon Glass03fcbf92022-04-24 23:31:09 -0600389int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
390{
391 int ret;
392
393 do {
394 ret = iter_incr(iter);
Simon Glass33063462023-01-17 10:48:18 -0700395 log_debug("iter_incr: ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600396 if (ret == BF_NO_MORE_DEVICES)
397 return log_msg_ret("done", ret);
398
399 if (!ret) {
400 ret = bootflow_check(iter, bflow);
Simon Glass33063462023-01-17 10:48:18 -0700401 log_debug("check - ret=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600402 if (!ret)
403 return 0;
404 iter->err = ret;
405 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
Simon Glass99e68182023-02-22 12:17:03 -0700406 if (iter->flags & BOOTFLOWIF_ALL)
Simon Glass03fcbf92022-04-24 23:31:09 -0600407 return log_msg_ret("all", ret);
408 }
409 } else {
Simon Glass33063462023-01-17 10:48:18 -0700410 log_debug("incr failed, err=%d\n", ret);
Simon Glass03fcbf92022-04-24 23:31:09 -0600411 iter->err = ret;
412 }
413
414 } while (1);
415}
416
Simon Glass00501fc2022-10-20 18:22:51 -0600417void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
418 struct udevice *meth)
419{
420 memset(bflow, '\0', sizeof(*bflow));
421 bflow->dev = bootdev;
422 bflow->method = meth;
423 bflow->state = BOOTFLOWST_BASE;
424}
425
Simon Glass03fcbf92022-04-24 23:31:09 -0600426void bootflow_free(struct bootflow *bflow)
427{
428 free(bflow->name);
429 free(bflow->subdir);
430 free(bflow->fname);
431 free(bflow->buf);
Simon Glass72b7b192023-01-06 08:52:33 -0600432 free(bflow->os_name);
Simon Glass7b8c6342023-01-17 10:47:56 -0700433 free(bflow->fdt_fname);
Simon Glass03fcbf92022-04-24 23:31:09 -0600434}
435
436void bootflow_remove(struct bootflow *bflow)
437{
Simon Glassdee4d642022-07-30 15:52:23 -0600438 if (bflow->dev)
439 list_del(&bflow->bm_node);
Simon Glass03fcbf92022-04-24 23:31:09 -0600440 list_del(&bflow->glob_node);
441
442 bootflow_free(bflow);
443 free(bflow);
444}
445
446int bootflow_boot(struct bootflow *bflow)
447{
448 int ret;
449
450 if (bflow->state != BOOTFLOWST_READY)
451 return log_msg_ret("load", -EPROTO);
452
453 ret = bootmeth_boot(bflow->method, bflow);
454 if (ret)
455 return log_msg_ret("boot", ret);
456
457 /*
458 * internal error, should not get here since we should have booted
459 * something or returned an error
460 */
461
462 return log_msg_ret("end", -EFAULT);
463}
464
465int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
466{
467 int ret;
468
469 printf("** Booting bootflow '%s' with %s\n", bflow->name,
470 bflow->method->name);
Simon Glassca84dc82023-02-22 12:17:04 -0700471 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
472 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
473 printf("Using prior-stage device tree\n");
Simon Glass03fcbf92022-04-24 23:31:09 -0600474 ret = bootflow_boot(bflow);
475 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
476 printf("Boot failed (err=%d)\n", ret);
477 return ret;
478 }
479
480 switch (ret) {
481 case -EPROTO:
482 printf("Bootflow not loaded (state '%s')\n",
483 bootflow_state_get_name(bflow->state));
484 break;
485 case -ENOSYS:
486 printf("Boot method '%s' not supported\n", bflow->method->name);
487 break;
488 case -ENOTSUPP:
489 /* Disable this bootflow for this iteration */
490 if (iter) {
491 int ret2;
492
493 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
494 if (!ret2) {
495 printf("Boot method '%s' failed and will not be retried\n",
496 bflow->method->name);
497 }
498 }
499
500 break;
501 default:
502 printf("Boot failed (err=%d)\n", ret);
503 break;
504 }
505
506 return ret;
507}
508
Simon Glass18c50402023-01-17 10:47:54 -0700509int bootflow_iter_check_blk(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600510{
511 const struct udevice *media = dev_get_parent(iter->dev);
512 enum uclass_id id = device_get_uclass_id(media);
513
514 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
Simon Glass5820c2f2023-01-28 15:00:25 -0700515 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
Simon Glass03fcbf92022-04-24 23:31:09 -0600516 return 0;
517
518 return -ENOTSUPP;
519}
520
Simon Glass215be682023-01-17 10:48:03 -0700521int bootflow_iter_check_sf(const struct bootflow_iter *iter)
522{
523 const struct udevice *media = dev_get_parent(iter->dev);
524 enum uclass_id id = device_get_uclass_id(media);
525
526 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
527 if (id == UCLASS_SPI_FLASH)
528 return 0;
529
530 return -ENOTSUPP;
531}
532
Simon Glass18c50402023-01-17 10:47:54 -0700533int bootflow_iter_check_net(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600534{
535 const struct udevice *media = dev_get_parent(iter->dev);
536 enum uclass_id id = device_get_uclass_id(media);
537
538 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
539 if (id == UCLASS_ETH)
540 return 0;
541
542 return -ENOTSUPP;
543}
544
Simon Glass18c50402023-01-17 10:47:54 -0700545int bootflow_iter_check_system(const struct bootflow_iter *iter)
Simon Glass03fcbf92022-04-24 23:31:09 -0600546{
547 const struct udevice *media = dev_get_parent(iter->dev);
548 enum uclass_id id = device_get_uclass_id(media);
549
550 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
551 if (id == UCLASS_BOOTSTD)
552 return 0;
553
554 return -ENOTSUPP;
555}
Simon Glassb35513a2023-07-12 09:04:35 -0600556
557/**
558 * bootflow_cmdline_set() - Set the command line for a bootflow
559 *
560 * @value: New command-line string
561 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
562 */
563int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
564{
565 char *cmdline = NULL;
566
567 if (value) {
568 cmdline = strdup(value);
569 if (!cmdline)
570 return -ENOMEM;
571 }
572
573 free(bflow->cmdline);
574 bflow->cmdline = cmdline;
575
576 return 0;
577}
578
579#ifdef CONFIG_BOOTSTD_FULL
580/**
581 * on_bootargs() - Update the cmdline of a bootflow
582 */
583static int on_bootargs(const char *name, const char *value, enum env_op op,
584 int flags)
585{
586 struct bootstd_priv *std;
587 struct bootflow *bflow;
588 int ret;
589
590 ret = bootstd_get_priv(&std);
591 if (ret)
592 return 0;
593 bflow = std->cur_bootflow;
594 if (!bflow)
595 return 0;
596
597 switch (op) {
598 case env_op_create:
599 case env_op_overwrite:
600 ret = bootflow_cmdline_set(bflow, value);
601 if (ret && ret != ENOENT)
602 return 1;
603 return 0;
604 case env_op_delete:
605 bootflow_cmdline_set(bflow, NULL);
606 fallthrough;
607 default:
608 return 0;
609 }
610}
611U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
612#endif
Simon Glassa08adca2023-07-12 09:04:38 -0600613
614/**
615 * copy_in() - Copy a string into a cmdline buffer
616 *
617 * @buf: Buffer to copy into
618 * @end: End of buffer (pointer to char after the end)
619 * @arg: String to copy from
620 * @len: Number of chars to copy from @arg (note that this is not usually the
621 * sane as strlen(arg) since the string may contain following arguments)
622 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
623 * with no '=' sign
624 * Returns: Number of chars written to @buf
625 */
626static int copy_in(char *buf, char *end, const char *arg, int len,
627 const char *new_val)
628{
629 char *to = buf;
630
631 /* copy the arg name */
632 if (to + len >= end)
633 return -E2BIG;
634 memcpy(to, arg, len);
635 to += len;
636
637 if (new_val == BOOTFLOWCL_EMPTY) {
638 /* no value */
639 } else {
640 bool need_quote = strchr(new_val, ' ');
641 len = strlen(new_val);
642
643 /* need space for value, equals sign and maybe two quotes */
644 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
645 return -E2BIG;
646 *to++ = '=';
647 if (need_quote)
648 *to++ = '"';
649 memcpy(to, new_val, len);
650 to += len;
651 if (need_quote)
652 *to++ = '"';
653 }
654
655 return to - buf;
656}
657
658int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
659 const char *set_arg, const char *new_val, int *posp)
660{
661 bool found_arg = false;
662 const char *from;
663 char *to, *end;
664 int set_arg_len;
665 char empty = '\0';
666 int ret;
667
668 from = cmdline ?: &empty;
669
670 /* check if the value has quotes inside */
671 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
672 return -EBADF;
673
674 set_arg_len = strlen(set_arg);
675 for (to = buf, end = buf + maxlen; *from;) {
676 const char *val, *arg_end, *val_end, *p;
677 bool in_quote;
678
679 if (to >= end)
680 return -E2BIG;
681 while (*from == ' ')
682 from++;
683 if (!*from)
684 break;
685
686 /* find the end of this arg */
687 val = NULL;
688 arg_end = NULL;
689 val_end = NULL;
690 in_quote = false;
691 for (p = from;; p++) {
692 if (in_quote) {
693 if (!*p)
694 return -EINVAL;
695 if (*p == '"')
696 in_quote = false;
697 continue;
698 }
699 if (*p == '=') {
700 arg_end = p;
701 val = p + 1;
702 } else if (*p == '"') {
703 in_quote = true;
704 } else if (!*p || *p == ' ') {
705 val_end = p;
706 if (!arg_end)
707 arg_end = p;
708 break;
709 }
710 }
711 /*
712 * At this point val_end points to the end of the value, or the
713 * last char after the arg name, if there is no label.
714 * arg_end is the char after the arg name
715 * val points to the value, or NULL if there is none
716 * char after the value.
717 *
718 * fred=1234
719 * ^ ^^ ^
720 * from || |
721 * / \ \
722 * arg_end val val_end
723 */
724 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
725 (long)(arg_end - from), (long)(val - from),
726 (long)(val_end - from));
727
728 if (to != buf) {
729 if (to >= end)
730 return -E2BIG;
731 *to++ = ' ';
732 }
733
734 /* if this is the target arg, update it */
735 if (!strncmp(from, set_arg, arg_end - from)) {
736 if (!buf) {
737 bool has_quote = val_end[-1] == '"';
738
739 /*
740 * exclude any start/end quotes from
741 * calculations
742 */
743 if (!val)
744 val = val_end;
745 *posp = val - cmdline + has_quote;
746 return val_end - val - 2 * has_quote;
747 }
748 found_arg = true;
749 if (!new_val) {
750 /* delete this arg */
751 from = val_end + (*val_end == ' ');
752 log_debug("delete from: %s\n", from);
753 if (to != buf)
754 to--; /* drop the space we added */
755 continue;
756 }
757
758 ret = copy_in(to, end, from, arg_end - from, new_val);
759 if (ret < 0)
760 return ret;
761 to += ret;
762
763 /* if not the target arg, copy it unchanged */
764 } else if (to) {
765 int len;
766
767 len = val_end - from;
768 if (to + len >= end)
769 return -E2BIG;
770 memcpy(to, from, len);
771 to += len;
772 }
773 from = val_end;
774 }
775
776 /* If we didn't find the arg, add it */
777 if (!found_arg) {
778 /* trying to delete something that is not there */
779 if (!new_val || !buf)
780 return -ENOENT;
781 if (to >= end)
782 return -E2BIG;
783
784 /* add a space to separate it from the previous arg */
785 if (to != buf && to[-1] != ' ')
786 *to++ = ' ';
787 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
788 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
789 if (ret < 0)
790 return ret;
791 to += ret;
792 }
793
794 /* delete any trailing space */
795 if (to > buf && to[-1] == ' ')
796 to--;
797
798 if (to >= end)
799 return -E2BIG;
800 *to++ = '\0';
801
802 return to - buf;
803}