1 // 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 <bootdev.h>
10 #include <bootflow.h>
11 #include <bootmeth.h>
12 #include <bootstd.h>
13 #include <dm.h>
14 #include <env_internal.h>
15 #include <malloc.h>
16 #include <serial.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 
20 /* error codes used to signal running out of things */
21 enum {
22 	BF_NO_MORE_PARTS	= -ESHUTDOWN,
23 	BF_NO_MORE_DEVICES	= -ENODEV,
24 };
25 
26 static const char *const bootflow_img[BFI_COUNT - BFI_FIRST] = {
27 	"extlinux_cfg",
28 	"logo",
29 	"efi",
30 	"cmdline",
31 };
32 
33 /**
34  * bootflow_state - name for each state
35  *
36  * See enum bootflow_state_t for what each of these means
37  */
38 static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
39 	"base",
40 	"media",
41 	"part",
42 	"fs",
43 	"file",
44 	"ready",
45 };
46 
bootflow_state_get_name(enum bootflow_state_t state)47 const char *bootflow_state_get_name(enum bootflow_state_t state)
48 {
49 	/* This doesn't need to be a useful name, since it will never occur */
50 	if (state < 0 || state >= BOOTFLOWST_COUNT)
51 		return "?";
52 
53 	return bootflow_state[state];
54 }
55 
bootflow_first_glob(struct bootflow ** bflowp)56 int bootflow_first_glob(struct bootflow **bflowp)
57 {
58 	struct bootstd_priv *std;
59 	int ret;
60 
61 	ret = bootstd_get_priv(&std);
62 	if (ret)
63 		return ret;
64 
65 	if (!std->bootflows.count)
66 		return -ENOENT;
67 
68 	*bflowp = alist_getw(&std->bootflows, 0, struct bootflow);
69 
70 	return 0;
71 }
72 
bootflow_next_glob(struct bootflow ** bflowp)73 int bootflow_next_glob(struct bootflow **bflowp)
74 {
75 	struct bootstd_priv *std;
76 	int ret;
77 
78 	ret = bootstd_get_priv(&std);
79 	if (ret)
80 		return ret;
81 
82 	*bflowp = alist_nextw(&std->bootflows, *bflowp);
83 	if (!*bflowp)
84 		return -ENOENT;
85 
86 	return 0;
87 }
88 
bootflow_iter_init(struct bootflow_iter * iter,int flags)89 void bootflow_iter_init(struct bootflow_iter *iter, int flags)
90 {
91 	memset(iter, '\0', sizeof(*iter));
92 	iter->first_glob_method = -1;
93 	iter->flags = flags;
94 
95 	/* remember the first bootdevs we see */
96 	iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
97 }
98 
bootflow_iter_uninit(struct bootflow_iter * iter)99 void bootflow_iter_uninit(struct bootflow_iter *iter)
100 {
101 	free(iter->method_order);
102 }
103 
bootflow_iter_drop_bootmeth(struct bootflow_iter * iter,const struct udevice * bmeth)104 int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
105 				const struct udevice *bmeth)
106 {
107 	/* We only support disabling the current bootmeth */
108 	if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
109 	    iter->method_order[iter->cur_method] != bmeth)
110 		return -EINVAL;
111 
112 	memmove(&iter->method_order[iter->cur_method],
113 		&iter->method_order[iter->cur_method + 1],
114 		(iter->num_methods - iter->cur_method - 1) * sizeof(void *));
115 
116 	iter->num_methods--;
117 
118 	return 0;
119 }
120 
121 /**
122  * bootflow_iter_set_dev() - switch to the next bootdev when iterating
123  *
124  * This sets iter->dev, records the device in the dev_used[] list and shows a
125  * message if required
126  *
127  * @iter: Iterator to update
128  * @dev: Bootdev to use, or NULL if there are no more
129  */
bootflow_iter_set_dev(struct bootflow_iter * iter,struct udevice * dev,int method_flags)130 static void bootflow_iter_set_dev(struct bootflow_iter *iter,
131 				  struct udevice *dev, int method_flags)
132 {
133 	struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
134 
135 	log_debug("iter: Setting dev to %s, flags %x\n",
136 		  dev ? dev->name : "(none)", method_flags);
137 	iter->dev = dev;
138 	iter->method_flags = method_flags;
139 
140 	if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
141 		/* record the device for later */
142 		if (dev && iter->num_devs < iter->max_devs)
143 			iter->dev_used[iter->num_devs++] = dev;
144 
145 		if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) ==
146 		    BOOTFLOWIF_SHOW) {
147 			if (dev)
148 				printf("Scanning bootdev '%s':\n", dev->name);
149 			else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
150 				ucp->flags & BOOTMETHF_GLOBAL)
151 				printf("Scanning global bootmeth '%s':\n",
152 				iter->method->name);
153 			else
154 				printf("No more bootdevs\n");
155 		}
156 	}
157 }
158 
159 /**
160  * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
161  *
162  * Move through the following bootdevs until we find another in this media
163  * uclass, or run out
164  *
165  * @devp: On entry, the device to check, on exit the new device, or NULL if
166  * there is none
167  */
scan_next_in_uclass(struct udevice ** devp)168 static void scan_next_in_uclass(struct udevice **devp)
169 {
170 	struct udevice *dev = *devp;
171 	enum uclass_id cur_id = device_get_uclass_id(dev->parent);
172 
173 	do {
174 		uclass_find_next_device(&dev);
175 	} while (dev && cur_id != device_get_uclass_id(dev->parent));
176 
177 	*devp = dev;
178 }
179 
180 /**
181  * iter_incr() - Move to the next item (method, part, bootdev)
182  *
183  * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
184  */
iter_incr(struct bootflow_iter * iter)185 static int iter_incr(struct bootflow_iter *iter)
186 {
187 	struct udevice *dev;
188 	bool inc_dev = true;
189 	bool global;
190 	int ret;
191 
192 	log_debug("entry: err=%d\n", iter->err);
193 	global = iter->doing_global;
194 
195 	if (iter->err == BF_NO_MORE_DEVICES)
196 		return BF_NO_MORE_DEVICES;
197 
198 	if (iter->err != BF_NO_MORE_PARTS) {
199 		/* Get the next boothmethod */
200 		if (++iter->cur_method < iter->num_methods) {
201 			iter->method = iter->method_order[iter->cur_method];
202 			return 0;
203 		}
204 
205 		/*
206 		 * If we have finished scanning the global bootmeths, start the
207 		 * normal bootdev scan
208 		 */
209 		if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
210 			iter->num_methods = iter->first_glob_method;
211 			iter->doing_global = false;
212 
213 			/*
214 			 * Don't move to the next dev as we haven't tried this
215 			 * one yet!
216 			 */
217 			inc_dev = false;
218 		}
219 	}
220 
221 	if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION)
222 		return BF_NO_MORE_DEVICES;
223 
224 	/* No more bootmeths; start at the first one, and... */
225 	iter->cur_method = 0;
226 	iter->method = iter->method_order[iter->cur_method];
227 
228 	if (iter->err != BF_NO_MORE_PARTS) {
229 		/* ...select next partition  */
230 		if (++iter->part <= iter->max_part)
231 			return 0;
232 	}
233 
234 	/* No more partitions; start at the first one and... */
235 	iter->part = 0;
236 
237 	/*
238 	 * Note: as far as we know, there is no partition table on the next
239 	 * bootdev, so set max_part to 0 until we discover otherwise. See
240 	 * bootdev_find_in_blk() for where this is set.
241 	 */
242 	iter->max_part = 0;
243 
244 	/* ...select next bootdev */
245 	if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
246 		ret = -ENOENT;
247 	} else {
248 		int method_flags = 0;
249 
250 		ret = 0;
251 		dev = iter->dev;
252 		log_debug("inc_dev=%d\n", inc_dev);
253 		if (!inc_dev) {
254 			ret = bootdev_setup_iter(iter, NULL, &dev,
255 						 &method_flags);
256 		} else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
257 			   (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
258 			scan_next_in_uclass(&dev);
259 			if (!dev) {
260 				log_debug("finished uclass %s\n",
261 					  dev_get_uclass_name(dev));
262 				ret = -ENODEV;
263 			}
264 		} else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
265 			   iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
266 			log_debug("next in single\n");
267 			do {
268 				/*
269 				 * Move to the next bootdev child of this media
270 				 * device. This ensures that we cover all the
271 				 * available SCSI IDs and LUNs.
272 				 */
273 				device_find_next_child(&dev);
274 				log_debug("- next %s\n",
275 					dev ? dev->name : "(none)");
276 			} while (dev && device_get_uclass_id(dev) !=
277 				UCLASS_BOOTDEV);
278 			if (!dev) {
279 				log_debug("finished uclass %s\n",
280 					  dev_get_uclass_name(dev));
281 				ret = -ENODEV;
282 			}
283 		} else {
284 			log_debug("labels %p\n", iter->labels);
285 			if (iter->labels) {
286 				/*
287 				 * when the label is "mmc" we want to scan all
288 				 * mmc bootdevs, not just the first. See
289 				 * bootdev_find_by_label() where this flag is
290 				 * set up
291 				 */
292 				if (iter->method_flags &
293 				    BOOTFLOW_METHF_SINGLE_UCLASS) {
294 					scan_next_in_uclass(&dev);
295 					log_debug("looking for next device %s: %s\n",
296 						  iter->dev->name,
297 						  dev ? dev->name : "<none>");
298 					method_flags = BOOTFLOW_METHF_SINGLE_UCLASS;
299 				} else {
300 					dev = NULL;
301 				}
302 				if (!dev) {
303 					log_debug("looking at next label\n");
304 					ret = bootdev_next_label(iter, &dev,
305 								 &method_flags);
306 				}
307 			} else {
308 				ret = bootdev_next_prio(iter, &dev);
309 			}
310 		}
311 		log_debug("ret=%d, dev=%p %s\n", ret, dev,
312 			  dev ? dev->name : "none");
313 		if (ret) {
314 			bootflow_iter_set_dev(iter, NULL, 0);
315 		} else {
316 			/*
317 			 * Probe the bootdev. This does not probe any attached
318 			 * block device, since they are siblings
319 			 */
320 			ret = device_probe(dev);
321 			log_debug("probe %s %d\n", dev->name, ret);
322 			if (!log_msg_ret("probe", ret))
323 				bootflow_iter_set_dev(iter, dev, method_flags);
324 		}
325 	}
326 
327 	/* if there are no more bootdevs, give up */
328 	if (ret)
329 		return log_msg_ret("incr", BF_NO_MORE_DEVICES);
330 
331 	return 0;
332 }
333 
334 /**
335  * bootflow_check() - Check if a bootflow can be obtained
336  *
337  * @iter: Provides part, bootmeth to use
338  * @bflow: Bootflow to update on success
339  * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
340  *	BF_NO_MORE_PARTS if there are no more partitions on bootdev
341  */
bootflow_check(struct bootflow_iter * iter,struct bootflow * bflow)342 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
343 {
344 	struct udevice *dev;
345 	int ret;
346 
347 	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
348 		bootflow_iter_set_dev(iter, NULL, 0);
349 		ret = bootmeth_get_bootflow(iter->method, bflow);
350 		if (ret)
351 			return log_msg_ret("glob", ret);
352 
353 		return 0;
354 	}
355 
356 	dev = iter->dev;
357 	ret = bootdev_get_bootflow(dev, iter, bflow);
358 
359 	/* If we got a valid bootflow, return it */
360 	if (!ret) {
361 		log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
362 			  dev->name, iter->part, iter->method->name);
363 		return 0;
364 	}
365 
366 	/* Unless there is nothing more to try, move to the next device */
367 	if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
368 		log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
369 			  dev->name, iter->part, iter->method->name, ret);
370 		/*
371 		 * For 'all' we return all bootflows, even
372 		 * those with errors
373 		 */
374 		if (iter->flags & BOOTFLOWIF_ALL)
375 			return log_msg_ret("all", ret);
376 	}
377 
378 	return log_msg_ret("check", ret);
379 }
380 
bootflow_scan_first(struct udevice * dev,const char * label,struct bootflow_iter * iter,int flags,struct bootflow * bflow)381 int bootflow_scan_first(struct udevice *dev, const char *label,
382 			struct bootflow_iter *iter, int flags,
383 			struct bootflow *bflow)
384 {
385 	int ret;
386 
387 	if (dev || label)
388 		flags |= BOOTFLOWIF_SKIP_GLOBAL;
389 	bootflow_iter_init(iter, flags);
390 
391 	/*
392 	 * Set up the ordering of bootmeths. This sets iter->doing_global and
393 	 * iter->first_glob_method if we are starting with the global bootmeths
394 	 */
395 	ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
396 	if (ret)
397 		return log_msg_ret("obmeth", -ENODEV);
398 
399 	/* Find the first bootmeth (there must be at least one!) */
400 	iter->method = iter->method_order[iter->cur_method];
401 
402 	if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
403 		struct udevice *dev = NULL;
404 		int method_flags;
405 
406 		ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
407 		if (ret)
408 			return log_msg_ret("obdev", -ENODEV);
409 
410 		bootflow_iter_set_dev(iter, dev, method_flags);
411 	}
412 
413 	ret = bootflow_check(iter, bflow);
414 	if (ret) {
415 		log_debug("check - ret=%d\n", ret);
416 		if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
417 			if (iter->flags & BOOTFLOWIF_ALL)
418 				return log_msg_ret("all", ret);
419 		}
420 		iter->err = ret;
421 		ret = bootflow_scan_next(iter, bflow);
422 		if (ret)
423 			return log_msg_ret("get", ret);
424 	}
425 
426 	return 0;
427 }
428 
bootflow_scan_next(struct bootflow_iter * iter,struct bootflow * bflow)429 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
430 {
431 	int ret;
432 
433 	do {
434 		ret = iter_incr(iter);
435 		log_debug("iter_incr: ret=%d\n", ret);
436 		if (ret == BF_NO_MORE_DEVICES)
437 			return log_msg_ret("done", ret);
438 
439 		if (!ret) {
440 			ret = bootflow_check(iter, bflow);
441 			log_debug("check - ret=%d\n", ret);
442 			if (!ret)
443 				return 0;
444 			iter->err = ret;
445 			if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
446 				if (iter->flags & BOOTFLOWIF_ALL)
447 					return log_msg_ret("all", ret);
448 			}
449 		} else {
450 			log_debug("incr failed, err=%d\n", ret);
451 			iter->err = ret;
452 		}
453 
454 	} while (1);
455 }
456 
bootflow_init(struct bootflow * bflow,struct udevice * bootdev,struct udevice * meth)457 void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
458 		   struct udevice *meth)
459 {
460 	memset(bflow, '\0', sizeof(*bflow));
461 	bflow->dev = bootdev;
462 	bflow->method = meth;
463 	bflow->state = BOOTFLOWST_BASE;
464 	alist_init_struct(&bflow->images, struct bootflow_img);
465 }
466 
bootflow_free(struct bootflow * bflow)467 void bootflow_free(struct bootflow *bflow)
468 {
469 	struct bootflow_img *img;
470 
471 	free(bflow->name);
472 	free(bflow->subdir);
473 	free(bflow->fname);
474 	if (!(bflow->flags & BOOTFLOWF_STATIC_BUF))
475 		free(bflow->buf);
476 	free(bflow->os_name);
477 	free(bflow->fdt_fname);
478 	free(bflow->bootmeth_priv);
479 
480 	alist_for_each(img, &bflow->images)
481 		free(img->fname);
482 	alist_empty(&bflow->images);
483 }
484 
bootflow_remove(struct bootflow * bflow)485 void bootflow_remove(struct bootflow *bflow)
486 {
487 	bootflow_free(bflow);
488 }
489 
490 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
bootflow_read_all(struct bootflow * bflow)491 int bootflow_read_all(struct bootflow *bflow)
492 {
493 	int ret;
494 
495 	if (bflow->state != BOOTFLOWST_READY)
496 		return log_msg_ret("rd", -EPROTO);
497 
498 	ret = bootmeth_read_all(bflow->method, bflow);
499 	if (ret)
500 		return log_msg_ret("rd2", ret);
501 
502 	return 0;
503 }
504 #endif /* BOOTSTD_FULL */
505 
bootflow_boot(struct bootflow * bflow)506 int bootflow_boot(struct bootflow *bflow)
507 {
508 	int ret;
509 
510 	if (bflow->state != BOOTFLOWST_READY)
511 		return log_msg_ret("load", -EPROTO);
512 
513 	ret = bootmeth_boot(bflow->method, bflow);
514 	if (ret)
515 		return log_msg_ret("boot", ret);
516 
517 	/*
518 	 * internal error, should not get here since we should have booted
519 	 * something or returned an error
520 	 */
521 
522 	return log_msg_ret("end", -EFAULT);
523 }
524 
bootflow_run_boot(struct bootflow_iter * iter,struct bootflow * bflow)525 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
526 {
527 	int ret;
528 
529 	printf("** Booting bootflow '%s' with %s\n", bflow->name,
530 	       bflow->method->name);
531 	if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
532 	    (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
533 		printf("Using prior-stage device tree\n");
534 	ret = bootflow_boot(bflow);
535 	if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
536 		printf("Boot failed (err=%d)\n", ret);
537 		return ret;
538 	}
539 
540 	switch (ret) {
541 	case -EPROTO:
542 		printf("Bootflow not loaded (state '%s')\n",
543 		       bootflow_state_get_name(bflow->state));
544 		break;
545 	case -ENOSYS:
546 		printf("Boot method '%s' not supported\n", bflow->method->name);
547 		break;
548 	case -ENOTSUPP:
549 		/* Disable this bootflow for this iteration */
550 		if (iter) {
551 			int ret2;
552 
553 			ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
554 			if (!ret2) {
555 				printf("Boot method '%s' failed and will not be retried\n",
556 				       bflow->method->name);
557 			}
558 		}
559 
560 		break;
561 	default:
562 		printf("Boot failed (err=%d)\n", ret);
563 		break;
564 	}
565 
566 	return ret;
567 }
568 
bootflow_iter_check_blk(const struct bootflow_iter * iter)569 int bootflow_iter_check_blk(const struct bootflow_iter *iter)
570 {
571 	const struct udevice *media = dev_get_parent(iter->dev);
572 	enum uclass_id id = device_get_uclass_id(media);
573 
574 	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
575 	if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
576 		return 0;
577 
578 	return -ENOTSUPP;
579 }
580 
bootflow_iter_check_mmc(const struct bootflow_iter * iter)581 int bootflow_iter_check_mmc(const struct bootflow_iter *iter)
582 {
583 	const struct udevice *media = dev_get_parent(iter->dev);
584 	enum uclass_id id = device_get_uclass_id(media);
585 
586 	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
587 	if (id == UCLASS_MMC)
588 		return 0;
589 
590 	return -ENOTSUPP;
591 }
592 
bootflow_iter_check_sf(const struct bootflow_iter * iter)593 int bootflow_iter_check_sf(const struct bootflow_iter *iter)
594 {
595 	const struct udevice *media = dev_get_parent(iter->dev);
596 	enum uclass_id id = device_get_uclass_id(media);
597 
598 	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
599 	if (id == UCLASS_SPI_FLASH)
600 		return 0;
601 
602 	return -ENOTSUPP;
603 }
604 
bootflow_iter_check_net(const struct bootflow_iter * iter)605 int bootflow_iter_check_net(const struct bootflow_iter *iter)
606 {
607 	const struct udevice *media = dev_get_parent(iter->dev);
608 	enum uclass_id id = device_get_uclass_id(media);
609 
610 	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
611 	if (id == UCLASS_ETH)
612 		return 0;
613 
614 	return -ENOTSUPP;
615 }
616 
bootflow_iter_check_system(const struct bootflow_iter * iter)617 int bootflow_iter_check_system(const struct bootflow_iter *iter)
618 {
619 	const struct udevice *media = dev_get_parent(iter->dev);
620 	enum uclass_id id = device_get_uclass_id(media);
621 
622 	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
623 	if (id == UCLASS_BOOTSTD)
624 		return 0;
625 
626 	return -ENOTSUPP;
627 }
628 
629 /**
630  * bootflow_cmdline_set() - Set the command line for a bootflow
631  *
632  * @value: New command-line string
633  * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
634  */
bootflow_cmdline_set(struct bootflow * bflow,const char * value)635 int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
636 {
637 	char *cmdline = NULL;
638 
639 	if (value) {
640 		cmdline = strdup(value);
641 		if (!cmdline)
642 			return -ENOMEM;
643 	}
644 
645 	free(bflow->cmdline);
646 	bflow->cmdline = cmdline;
647 
648 	return 0;
649 }
650 
651 #ifdef CONFIG_BOOTSTD_FULL
652 /**
653  * on_bootargs() - Update the cmdline of a bootflow
654  */
on_bootargs(const char * name,const char * value,enum env_op op,int flags)655 static int on_bootargs(const char *name, const char *value, enum env_op op,
656 		       int flags)
657 {
658 	struct bootstd_priv *std;
659 	struct bootflow *bflow;
660 	int ret;
661 
662 	ret = bootstd_get_priv(&std);
663 	if (ret)
664 		return 0;
665 	bflow = std->cur_bootflow;
666 	if (!bflow)
667 		return 0;
668 
669 	switch (op) {
670 	case env_op_create:
671 	case env_op_overwrite:
672 		ret = bootflow_cmdline_set(bflow, value);
673 		if (ret && ret != ENOENT)
674 			return 1;
675 		return 0;
676 	case env_op_delete:
677 		bootflow_cmdline_set(bflow, NULL);
678 		fallthrough;
679 	default:
680 		return 0;
681 	}
682 }
683 U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
684 #endif
685 
686 /**
687  * copy_in() - Copy a string into a cmdline buffer
688  *
689  * @buf: Buffer to copy into
690  * @end: End of buffer (pointer to char after the end)
691  * @arg: String to copy from
692  * @len: Number of chars to copy from @arg (note that this is not usually the
693  * sane as strlen(arg) since the string may contain following arguments)
694  * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
695  * with no '=' sign
696  * Returns: Number of chars written to @buf
697  */
copy_in(char * buf,char * end,const char * arg,int len,const char * new_val)698 static int copy_in(char *buf, char *end, const char *arg, int len,
699 		   const char *new_val)
700 {
701 	char *to = buf;
702 
703 	/* copy the arg name */
704 	if (to + len >= end)
705 		return -E2BIG;
706 	memcpy(to, arg, len);
707 	to += len;
708 
709 	if (new_val == BOOTFLOWCL_EMPTY) {
710 		/* no value */
711 	} else {
712 		bool need_quote = strchr(new_val, ' ');
713 		len = strlen(new_val);
714 
715 		/* need space for value, equals sign and maybe two quotes */
716 		if (to + 1 + (need_quote ? 2 : 0) + len >= end)
717 			return -E2BIG;
718 		*to++ = '=';
719 		if (need_quote)
720 			*to++ = '"';
721 		memcpy(to, new_val, len);
722 		to += len;
723 		if (need_quote)
724 			*to++ = '"';
725 	}
726 
727 	return to - buf;
728 }
729 
cmdline_set_arg(char * buf,int maxlen,const char * cmdline,const char * set_arg,const char * new_val,int * posp)730 int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
731 		    const char *set_arg, const char *new_val, int *posp)
732 {
733 	bool found_arg = false;
734 	const char *from;
735 	char *to, *end;
736 	int set_arg_len;
737 	char empty = '\0';
738 	int ret;
739 
740 	from = cmdline ?: &empty;
741 
742 	/* check if the value has quotes inside */
743 	if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
744 		return -EBADF;
745 
746 	set_arg_len = strlen(set_arg);
747 	for (to = buf, end = buf + maxlen; *from;) {
748 		const char *val, *arg_end, *val_end, *p;
749 		bool in_quote;
750 
751 		if (to >= end)
752 			return -E2BIG;
753 		while (*from == ' ')
754 			from++;
755 		if (!*from)
756 			break;
757 
758 		/* find the end of this arg */
759 		val = NULL;
760 		arg_end = NULL;
761 		val_end = NULL;
762 		in_quote = false;
763 		for (p = from;; p++) {
764 			if (in_quote) {
765 				if (!*p)
766 					return -EINVAL;
767 				if (*p == '"')
768 					in_quote = false;
769 				continue;
770 			}
771 			if (*p == '=' && !arg_end) {
772 				arg_end = p;
773 				val = p + 1;
774 			} else if (*p == '"') {
775 				in_quote = true;
776 			} else if (!*p || *p == ' ') {
777 				val_end = p;
778 				if (!arg_end)
779 					arg_end = p;
780 				break;
781 			}
782 		}
783 		/*
784 		 * At this point val_end points to the end of the value, or the
785 		 * last char after the arg name, if there is no label.
786 		 * arg_end is the char after the arg name
787 		 * val points to the value, or NULL if there is none
788 		 * char after the value.
789 		 *
790 		 *        fred=1234
791 		 *        ^   ^^   ^
792 		 *      from  ||   |
793 		 *           / \    \
794 		 *    arg_end  val   val_end
795 		 */
796 		log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
797 			  (long)(arg_end - from), (long)(val - from),
798 			  (long)(val_end - from));
799 
800 		if (to != buf) {
801 			if (to >= end)
802 				return -E2BIG;
803 			*to++ = ' ';
804 		}
805 
806 		/* if this is the target arg, update it */
807 		if (arg_end - from == set_arg_len &&
808 		    !strncmp(from, set_arg, set_arg_len)) {
809 			if (!buf) {
810 				bool has_quote = val_end[-1] == '"';
811 
812 				/*
813 				 * exclude any start/end quotes from
814 				 * calculations
815 				 */
816 				if (!val)
817 					val = val_end;
818 				*posp = val - cmdline + has_quote;
819 				return val_end - val - 2 * has_quote;
820 			}
821 			found_arg = true;
822 			if (!new_val) {
823 				/* delete this arg */
824 				from = val_end + (*val_end == ' ');
825 				log_debug("delete from: %s\n", from);
826 				if (to != buf)
827 					to--; /* drop the space we added */
828 				continue;
829 			}
830 
831 			ret = copy_in(to, end, from, arg_end - from, new_val);
832 			if (ret < 0)
833 				return ret;
834 			to += ret;
835 
836 		/* if not the target arg, copy it unchanged */
837 		} else if (to) {
838 			int len;
839 
840 			len = val_end - from;
841 			if (to + len >= end)
842 				return -E2BIG;
843 			memcpy(to, from, len);
844 			to += len;
845 		}
846 		from = val_end;
847 	}
848 
849 	/* If we didn't find the arg, add it */
850 	if (!found_arg) {
851 		/* trying to delete something that is not there */
852 		if (!new_val || !buf)
853 			return -ENOENT;
854 		if (to >= end)
855 			return -E2BIG;
856 
857 		/* add a space to separate it from the previous arg */
858 		if (to != buf && to[-1] != ' ')
859 			*to++ = ' ';
860 		ret = copy_in(to, end, set_arg, set_arg_len, new_val);
861 		log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
862 		if (ret < 0)
863 			return ret;
864 		to += ret;
865 	}
866 
867 	/* delete any trailing space */
868 	if (to > buf && to[-1] == ' ')
869 		to--;
870 
871 	if (to >= end)
872 		return -E2BIG;
873 	*to++ = '\0';
874 
875 	return to - buf;
876 }
877 
bootflow_cmdline_set_arg(struct bootflow * bflow,const char * set_arg,const char * new_val,bool set_env)878 int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
879 			     const char *new_val, bool set_env)
880 {
881 	char buf[2048];
882 	char *cmd = NULL;
883 	int ret;
884 
885 	ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
886 			      new_val, NULL);
887 	if (ret < 0)
888 		return ret;
889 
890 	ret = bootflow_cmdline_set(bflow, buf);
891 	if (*buf) {
892 		cmd = strdup(buf);
893 		if (!cmd)
894 			return -ENOMEM;
895 	}
896 	free(bflow->cmdline);
897 	bflow->cmdline = cmd;
898 
899 	if (set_env) {
900 		ret = env_set("bootargs", bflow->cmdline);
901 		if (ret)
902 			return ret;
903 	}
904 
905 	return 0;
906 }
907 
cmdline_get_arg(const char * cmdline,const char * arg,int * posp)908 int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
909 {
910 	int ret;
911 
912 	ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
913 
914 	return ret;
915 }
916 
bootflow_cmdline_get_arg(struct bootflow * bflow,const char * arg,const char ** val)917 int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
918 			     const char **val)
919 {
920 	int ret;
921 	int pos;
922 
923 	ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
924 	if (ret < 0)
925 		return ret;
926 	*val = bflow->cmdline + pos;
927 
928 	return ret;
929 }
930 
bootflow_cmdline_auto(struct bootflow * bflow,const char * arg)931 int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
932 {
933 	struct serial_device_info info;
934 	char buf[50];
935 	int ret;
936 
937 	ret = serial_getinfo(gd->cur_serial_dev, &info);
938 	if (ret)
939 		return ret;
940 
941 	*buf = '\0';
942 	if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
943 		snprintf(buf, sizeof(buf),
944 			 "uart8250,%s,%#lx,%dn8",
945 			 info.addr_space == SERIAL_ADDRESS_SPACE_IO ? "io" :
946 			 "mmio", info.addr, info.baudrate);
947 	} else if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_PL01X) {
948 		snprintf(buf, sizeof(buf),
949 			 "pl011,mmio32,%#lx,%dn8", info.addr,
950 			 info.baudrate);
951 	} else if (!strcmp("console", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
952 		snprintf(buf, sizeof(buf),
953 			 "ttyS0,%dn8", info.baudrate);
954 	}
955 
956 	if (!*buf) {
957 		printf("Unknown param '%s'\n", arg);
958 		return -ENOENT;
959 	}
960 
961 	ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
962 	if (ret)
963 		return ret;
964 
965 	return 0;
966 }
967 
bootflow_img_type_name(enum bootflow_img_t type)968 const char *bootflow_img_type_name(enum bootflow_img_t type)
969 {
970 	const char *name;
971 
972 	if (type >= BFI_FIRST && type < BFI_COUNT)
973 		name = bootflow_img[type - BFI_FIRST];
974 	else
975 		name = genimg_get_type_short_name(type);
976 
977 	return name;
978 }
979 
bootflow_img_add(struct bootflow * bflow,const char * fname,enum bootflow_img_t type,ulong addr,ulong size)980 struct bootflow_img *bootflow_img_add(struct bootflow *bflow, const char *fname,
981 				      enum bootflow_img_t type, ulong addr,
982 				      ulong size)
983 {
984 	struct bootflow_img img, *ptr;
985 
986 	memset(&img, '\0', sizeof(struct bootflow_img));
987 	img.fname = strdup(fname);
988 	if (!img.fname)
989 		return NULL;
990 
991 	img.type = type;
992 	img.addr = addr;
993 	img.size = size;
994 	ptr = alist_add(&bflow->images, img);
995 	if (!ptr)
996 		return NULL;
997 
998 	return ptr;
999 }
1000 
bootflow_get_seq(const struct bootflow * bflow)1001 int bootflow_get_seq(const struct bootflow *bflow)
1002 {
1003 	struct bootstd_priv *std;
1004 	int ret;
1005 
1006 	ret = bootstd_get_priv(&std);
1007 	if (ret)
1008 		return ret;
1009 
1010 	return alist_calc_index(&std->bootflows, bflow);
1011 }
1012