1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
15 #include <env_internal.h>
18 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
21 /* error codes used to signal running out of things */
23 BF_NO_MORE_PARTS = -ESHUTDOWN,
24 BF_NO_MORE_DEVICES = -ENODEV,
28 * bootflow_state - name for each state
30 * See enum bootflow_state_t for what each of these means
32 static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
41 const char *bootflow_state_get_name(enum bootflow_state_t state)
43 /* This doesn't need to be a useful name, since it will never occur */
44 if (state < 0 || state >= BOOTFLOWST_COUNT)
47 return bootflow_state[state];
50 int bootflow_first_glob(struct bootflow **bflowp)
52 struct bootstd_priv *std;
55 ret = bootstd_get_priv(&std);
59 if (list_empty(&std->glob_head))
62 *bflowp = list_first_entry(&std->glob_head, struct bootflow,
68 int bootflow_next_glob(struct bootflow **bflowp)
70 struct bootstd_priv *std;
71 struct bootflow *bflow = *bflowp;
74 ret = bootstd_get_priv(&std);
80 if (list_is_last(&bflow->glob_node, &std->glob_head))
83 *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
88 void bootflow_iter_init(struct bootflow_iter *iter, int flags)
90 memset(iter, '\0', sizeof(*iter));
91 iter->first_glob_method = -1;
94 /* remember the first bootdevs we see */
95 iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
98 void bootflow_iter_uninit(struct bootflow_iter *iter)
100 free(iter->method_order);
103 int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
104 const struct udevice *bmeth)
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)
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 *));
121 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
123 * This sets iter->dev, records the device in the dev_used[] list and shows a
124 * message if required
126 * @iter: Iterator to update
127 * @dev: Bootdev to use, or NULL if there are no more
129 static void bootflow_iter_set_dev(struct bootflow_iter *iter,
130 struct udevice *dev, int method_flags)
132 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
134 log_debug("iter: Setting dev to %s, flags %x\n",
135 dev ? dev->name : "(none)", method_flags);
137 iter->method_flags = method_flags;
139 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;
144 if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_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",
153 printf("No more bootdevs\n");
159 * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
161 * Move through the following bootdevs until we find another in this media
164 * @devp: On entry, the device to check, on exit the new device, or NULL if
167 static void scan_next_in_uclass(struct udevice **devp)
169 struct udevice *dev = *devp;
170 enum uclass_id cur_id = device_get_uclass_id(dev->parent);
173 uclass_find_next_device(&dev);
174 } while (dev && cur_id != device_get_uclass_id(dev->parent));
180 * iter_incr() - Move to the next item (method, part, bootdev)
182 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
184 static int iter_incr(struct bootflow_iter *iter)
191 log_debug("entry: err=%d\n", iter->err);
192 global = iter->doing_global;
194 if (iter->err == BF_NO_MORE_DEVICES)
195 return BF_NO_MORE_DEVICES;
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];
205 * If we have finished scanning the global bootmeths, start the
206 * normal bootdev scan
208 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
209 iter->num_methods = iter->first_glob_method;
210 iter->doing_global = false;
213 * Don't move to the next dev as we haven't tried this
220 /* No more bootmeths; start at the first one, and... */
221 iter->cur_method = 0;
222 iter->method = iter->method_order[iter->cur_method];
224 if (iter->err != BF_NO_MORE_PARTS) {
225 /* ...select next partition */
226 if (++iter->part <= iter->max_part)
230 /* No more partitions; start at the first one and... */
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.
240 /* ...select next bootdev */
241 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
248 log_debug("inc_dev=%d\n", inc_dev);
250 ret = bootdev_setup_iter(iter, NULL, &dev,
252 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
253 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
254 scan_next_in_uclass(&dev);
256 log_debug("finished uclass %s\n",
257 dev_get_uclass_name(dev));
260 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
261 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
262 log_debug("next in single\n");
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.
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) !=
276 log_debug("finished uclass %s\n",
277 dev_get_uclass_name(dev));
281 log_debug("labels %p\n", iter->labels);
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
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",
294 dev ? dev->name : "<none>");
299 log_debug("looking at next label\n");
300 ret = bootdev_next_label(iter, &dev,
304 ret = bootdev_next_prio(iter, &dev);
308 log_debug("ret=%d, dev=%p %s\n", ret, dev,
309 dev ? dev->name : "none");
311 bootflow_iter_set_dev(iter, NULL, 0);
314 * Probe the bootdev. This does not probe any attached
315 * block device, since they are siblings
317 ret = device_probe(dev);
318 log_debug("probe %s %d\n", dev->name, ret);
319 if (!log_msg_ret("probe", ret))
320 bootflow_iter_set_dev(iter, dev, method_flags);
324 /* if there are no more bootdevs, give up */
326 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
332 * bootflow_check() - Check if a bootflow can be obtained
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
339 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
344 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
345 bootflow_iter_set_dev(iter, NULL, 0);
346 ret = bootmeth_get_bootflow(iter->method, bflow);
348 return log_msg_ret("glob", ret);
354 ret = bootdev_get_bootflow(dev, iter, bflow);
356 /* If we got a valid bootflow, return it */
358 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
359 dev->name, iter->part, iter->method->name);
363 /* Unless there is nothing more to try, move to the next device */
364 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
365 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
366 dev->name, iter->part, iter->method->name, ret);
368 * For 'all' we return all bootflows, even
371 if (iter->flags & BOOTFLOWIF_ALL)
372 return log_msg_ret("all", ret);
375 return log_msg_ret("check", ret);
380 int bootflow_scan_first(struct udevice *dev, const char *label,
381 struct bootflow_iter *iter, int flags,
382 struct bootflow *bflow)
387 flags |= BOOTFLOWIF_SKIP_GLOBAL;
388 bootflow_iter_init(iter, flags);
391 * Set up the ordering of bootmeths. This sets iter->doing_global and
392 * iter->first_glob_method if we are starting with the global bootmeths
394 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
396 return log_msg_ret("obmeth", -ENODEV);
398 /* Find the first bootmeth (there must be at least one!) */
399 iter->method = iter->method_order[iter->cur_method];
401 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
402 struct udevice *dev = NULL;
405 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
407 return log_msg_ret("obdev", -ENODEV);
409 bootflow_iter_set_dev(iter, dev, method_flags);
412 ret = bootflow_check(iter, bflow);
414 log_debug("check - ret=%d\n", ret);
415 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
416 if (iter->flags & BOOTFLOWIF_ALL)
417 return log_msg_ret("all", ret);
420 ret = bootflow_scan_next(iter, bflow);
422 return log_msg_ret("get", ret);
428 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
433 ret = iter_incr(iter);
434 log_debug("iter_incr: ret=%d\n", ret);
435 if (ret == BF_NO_MORE_DEVICES)
436 return log_msg_ret("done", ret);
439 ret = bootflow_check(iter, bflow);
440 log_debug("check - ret=%d\n", ret);
444 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
445 if (iter->flags & BOOTFLOWIF_ALL)
446 return log_msg_ret("all", ret);
449 log_debug("incr failed, err=%d\n", ret);
456 void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
457 struct udevice *meth)
459 memset(bflow, '\0', sizeof(*bflow));
460 bflow->dev = bootdev;
461 bflow->method = meth;
462 bflow->state = BOOTFLOWST_BASE;
465 void bootflow_free(struct bootflow *bflow)
471 free(bflow->os_name);
472 free(bflow->fdt_fname);
473 free(bflow->bootmeth_priv);
476 void bootflow_remove(struct bootflow *bflow)
479 list_del(&bflow->bm_node);
480 list_del(&bflow->glob_node);
482 bootflow_free(bflow);
486 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
487 int bootflow_read_all(struct bootflow *bflow)
491 if (bflow->state != BOOTFLOWST_READY)
492 return log_msg_ret("rd", -EPROTO);
494 ret = bootmeth_read_all(bflow->method, bflow);
496 return log_msg_ret("rd2", ret);
500 #endif /* BOOTSTD_FULL */
502 int bootflow_boot(struct bootflow *bflow)
506 if (bflow->state != BOOTFLOWST_READY)
507 return log_msg_ret("load", -EPROTO);
509 ret = bootmeth_boot(bflow->method, bflow);
511 return log_msg_ret("boot", ret);
514 * internal error, should not get here since we should have booted
515 * something or returned an error
518 return log_msg_ret("end", -EFAULT);
521 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
525 printf("** Booting bootflow '%s' with %s\n", bflow->name,
526 bflow->method->name);
527 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
528 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
529 printf("Using prior-stage device tree\n");
530 ret = bootflow_boot(bflow);
531 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
532 printf("Boot failed (err=%d)\n", ret);
538 printf("Bootflow not loaded (state '%s')\n",
539 bootflow_state_get_name(bflow->state));
542 printf("Boot method '%s' not supported\n", bflow->method->name);
545 /* Disable this bootflow for this iteration */
549 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
551 printf("Boot method '%s' failed and will not be retried\n",
552 bflow->method->name);
558 printf("Boot failed (err=%d)\n", ret);
565 int bootflow_iter_check_blk(const struct bootflow_iter *iter)
567 const struct udevice *media = dev_get_parent(iter->dev);
568 enum uclass_id id = device_get_uclass_id(media);
570 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
571 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
577 int bootflow_iter_check_sf(const struct bootflow_iter *iter)
579 const struct udevice *media = dev_get_parent(iter->dev);
580 enum uclass_id id = device_get_uclass_id(media);
582 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
583 if (id == UCLASS_SPI_FLASH)
589 int bootflow_iter_check_net(const struct bootflow_iter *iter)
591 const struct udevice *media = dev_get_parent(iter->dev);
592 enum uclass_id id = device_get_uclass_id(media);
594 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
595 if (id == UCLASS_ETH)
601 int bootflow_iter_check_system(const struct bootflow_iter *iter)
603 const struct udevice *media = dev_get_parent(iter->dev);
604 enum uclass_id id = device_get_uclass_id(media);
606 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
607 if (id == UCLASS_BOOTSTD)
614 * bootflow_cmdline_set() - Set the command line for a bootflow
616 * @value: New command-line string
617 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
619 int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
621 char *cmdline = NULL;
624 cmdline = strdup(value);
629 free(bflow->cmdline);
630 bflow->cmdline = cmdline;
635 #ifdef CONFIG_BOOTSTD_FULL
637 * on_bootargs() - Update the cmdline of a bootflow
639 static int on_bootargs(const char *name, const char *value, enum env_op op,
642 struct bootstd_priv *std;
643 struct bootflow *bflow;
646 ret = bootstd_get_priv(&std);
649 bflow = std->cur_bootflow;
655 case env_op_overwrite:
656 ret = bootflow_cmdline_set(bflow, value);
657 if (ret && ret != ENOENT)
661 bootflow_cmdline_set(bflow, NULL);
667 U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
671 * copy_in() - Copy a string into a cmdline buffer
673 * @buf: Buffer to copy into
674 * @end: End of buffer (pointer to char after the end)
675 * @arg: String to copy from
676 * @len: Number of chars to copy from @arg (note that this is not usually the
677 * sane as strlen(arg) since the string may contain following arguments)
678 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
680 * Returns: Number of chars written to @buf
682 static int copy_in(char *buf, char *end, const char *arg, int len,
687 /* copy the arg name */
690 memcpy(to, arg, len);
693 if (new_val == BOOTFLOWCL_EMPTY) {
696 bool need_quote = strchr(new_val, ' ');
697 len = strlen(new_val);
699 /* need space for value, equals sign and maybe two quotes */
700 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
705 memcpy(to, new_val, len);
714 int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
715 const char *set_arg, const char *new_val, int *posp)
717 bool found_arg = false;
724 from = cmdline ?: ∅
726 /* check if the value has quotes inside */
727 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
730 set_arg_len = strlen(set_arg);
731 for (to = buf, end = buf + maxlen; *from;) {
732 const char *val, *arg_end, *val_end, *p;
742 /* find the end of this arg */
747 for (p = from;; p++) {
755 if (*p == '=' && !arg_end) {
758 } else if (*p == '"') {
760 } else if (!*p || *p == ' ') {
768 * At this point val_end points to the end of the value, or the
769 * last char after the arg name, if there is no label.
770 * arg_end is the char after the arg name
771 * val points to the value, or NULL if there is none
772 * char after the value.
778 * arg_end val val_end
780 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
781 (long)(arg_end - from), (long)(val - from),
782 (long)(val_end - from));
790 /* if this is the target arg, update it */
791 if (arg_end - from == set_arg_len &&
792 !strncmp(from, set_arg, set_arg_len)) {
794 bool has_quote = val_end[-1] == '"';
797 * exclude any start/end quotes from
802 *posp = val - cmdline + has_quote;
803 return val_end - val - 2 * has_quote;
807 /* delete this arg */
808 from = val_end + (*val_end == ' ');
809 log_debug("delete from: %s\n", from);
811 to--; /* drop the space we added */
815 ret = copy_in(to, end, from, arg_end - from, new_val);
820 /* if not the target arg, copy it unchanged */
824 len = val_end - from;
827 memcpy(to, from, len);
833 /* If we didn't find the arg, add it */
835 /* trying to delete something that is not there */
836 if (!new_val || !buf)
841 /* add a space to separate it from the previous arg */
842 if (to != buf && to[-1] != ' ')
844 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
845 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
851 /* delete any trailing space */
852 if (to > buf && to[-1] == ' ')
862 int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
863 const char *new_val, bool set_env)
869 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
874 ret = bootflow_cmdline_set(bflow, buf);
880 free(bflow->cmdline);
881 bflow->cmdline = cmd;
884 ret = env_set("bootargs", bflow->cmdline);
892 int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
896 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
901 int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
907 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
910 *val = bflow->cmdline + pos;
915 int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
917 struct serial_device_info info;
921 ret = serial_getinfo(gd->cur_serial_dev, &info);
926 if (!strcmp("earlycon", arg)) {
927 snprintf(buf, sizeof(buf),
928 "uart8250,mmio32,%#lx,%dn8", info.addr,
930 } else if (!strcmp("console", arg)) {
931 snprintf(buf, sizeof(buf),
932 "ttyS0,%dn8", info.baudrate);
936 printf("Unknown param '%s\n", arg);
940 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);