1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
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 * iter_incr() - Move to the next item (method, part, bootdev)
161 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
163 static int iter_incr(struct bootflow_iter *iter)
170 log_debug("entry: err=%d\n", iter->err);
171 global = iter->doing_global;
173 if (iter->err == BF_NO_MORE_DEVICES)
174 return BF_NO_MORE_DEVICES;
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];
184 * If we have finished scanning the global bootmeths, start the
185 * normal bootdev scan
187 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
188 iter->num_methods = iter->first_glob_method;
189 iter->doing_global = false;
192 * Don't move to the next dev as we haven't tried this
199 /* No more bootmeths; start at the first one, and... */
200 iter->cur_method = 0;
201 iter->method = iter->method_order[iter->cur_method];
203 if (iter->err != BF_NO_MORE_PARTS) {
204 /* ...select next partition */
205 if (++iter->part <= iter->max_part)
209 /* No more partitions; start at the first one and... */
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.
219 /* ...select next bootdev */
220 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
227 log_debug("inc_dev=%d\n", inc_dev);
229 ret = bootdev_setup_iter(iter, NULL, &dev,
231 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
232 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
233 /* Move to the next bootdev in this uclass */
234 uclass_find_next_device(&dev);
236 log_debug("finished uclass %s\n",
237 dev_get_uclass_name(dev));
240 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
241 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
242 log_debug("next in single\n");
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.
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) !=
256 log_debug("finished uclass %s\n",
257 dev_get_uclass_name(dev));
261 log_debug("labels %p\n", iter->labels);
263 ret = bootdev_next_label(iter, &dev,
266 ret = bootdev_next_prio(iter, &dev);
270 log_debug("ret=%d, dev=%p %s\n", ret, dev,
271 dev ? dev->name : "none");
273 bootflow_iter_set_dev(iter, NULL, 0);
276 * Probe the bootdev. This does not probe any attached
277 * block device, since they are siblings
279 ret = device_probe(dev);
280 log_debug("probe %s %d\n", dev->name, ret);
281 if (!log_msg_ret("probe", ret))
282 bootflow_iter_set_dev(iter, dev, method_flags);
286 /* if there are no more bootdevs, give up */
288 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
294 * bootflow_check() - Check if a bootflow can be obtained
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
301 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
306 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
307 bootflow_iter_set_dev(iter, NULL, 0);
308 ret = bootmeth_get_bootflow(iter->method, bflow);
310 return log_msg_ret("glob", ret);
316 ret = bootdev_get_bootflow(dev, iter, bflow);
318 /* If we got a valid bootflow, return it */
320 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
321 dev->name, iter->part, iter->method->name);
325 /* Unless there is nothing more to try, move to the next device */
326 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
327 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
328 dev->name, iter->part, iter->method->name, ret);
330 * For 'all' we return all bootflows, even
333 if (iter->flags & BOOTFLOWIF_ALL)
334 return log_msg_ret("all", ret);
337 return log_msg_ret("check", ret);
342 int bootflow_scan_first(struct udevice *dev, const char *label,
343 struct bootflow_iter *iter, int flags,
344 struct bootflow *bflow)
349 flags |= BOOTFLOWIF_SKIP_GLOBAL;
350 bootflow_iter_init(iter, flags);
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
356 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
358 return log_msg_ret("obmeth", -ENODEV);
360 /* Find the first bootmeth (there must be at least one!) */
361 iter->method = iter->method_order[iter->cur_method];
363 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
364 struct udevice *dev = NULL;
367 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
369 return log_msg_ret("obdev", -ENODEV);
371 bootflow_iter_set_dev(iter, dev, method_flags);
374 ret = bootflow_check(iter, bflow);
376 log_debug("check - ret=%d\n", ret);
377 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
378 if (iter->flags & BOOTFLOWIF_ALL)
379 return log_msg_ret("all", ret);
382 ret = bootflow_scan_next(iter, bflow);
384 return log_msg_ret("get", ret);
390 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
395 ret = iter_incr(iter);
396 log_debug("iter_incr: ret=%d\n", ret);
397 if (ret == BF_NO_MORE_DEVICES)
398 return log_msg_ret("done", ret);
401 ret = bootflow_check(iter, bflow);
402 log_debug("check - ret=%d\n", ret);
406 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
407 if (iter->flags & BOOTFLOWIF_ALL)
408 return log_msg_ret("all", ret);
411 log_debug("incr failed, err=%d\n", ret);
418 void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
419 struct udevice *meth)
421 memset(bflow, '\0', sizeof(*bflow));
422 bflow->dev = bootdev;
423 bflow->method = meth;
424 bflow->state = BOOTFLOWST_BASE;
427 void bootflow_free(struct bootflow *bflow)
433 free(bflow->os_name);
434 free(bflow->fdt_fname);
437 void bootflow_remove(struct bootflow *bflow)
440 list_del(&bflow->bm_node);
441 list_del(&bflow->glob_node);
443 bootflow_free(bflow);
447 int bootflow_boot(struct bootflow *bflow)
451 if (bflow->state != BOOTFLOWST_READY)
452 return log_msg_ret("load", -EPROTO);
454 ret = bootmeth_boot(bflow->method, bflow);
456 return log_msg_ret("boot", ret);
459 * internal error, should not get here since we should have booted
460 * something or returned an error
463 return log_msg_ret("end", -EFAULT);
466 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
470 printf("** Booting bootflow '%s' with %s\n", bflow->name,
471 bflow->method->name);
472 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
473 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
474 printf("Using prior-stage device tree\n");
475 ret = bootflow_boot(bflow);
476 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
477 printf("Boot failed (err=%d)\n", ret);
483 printf("Bootflow not loaded (state '%s')\n",
484 bootflow_state_get_name(bflow->state));
487 printf("Boot method '%s' not supported\n", bflow->method->name);
490 /* Disable this bootflow for this iteration */
494 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
496 printf("Boot method '%s' failed and will not be retried\n",
497 bflow->method->name);
503 printf("Boot failed (err=%d)\n", ret);
510 int bootflow_iter_check_blk(const struct bootflow_iter *iter)
512 const struct udevice *media = dev_get_parent(iter->dev);
513 enum uclass_id id = device_get_uclass_id(media);
515 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
516 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
522 int bootflow_iter_check_sf(const struct bootflow_iter *iter)
524 const struct udevice *media = dev_get_parent(iter->dev);
525 enum uclass_id id = device_get_uclass_id(media);
527 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
528 if (id == UCLASS_SPI_FLASH)
534 int bootflow_iter_check_net(const struct bootflow_iter *iter)
536 const struct udevice *media = dev_get_parent(iter->dev);
537 enum uclass_id id = device_get_uclass_id(media);
539 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
540 if (id == UCLASS_ETH)
546 int bootflow_iter_check_system(const struct bootflow_iter *iter)
548 const struct udevice *media = dev_get_parent(iter->dev);
549 enum uclass_id id = device_get_uclass_id(media);
551 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
552 if (id == UCLASS_BOOTSTD)
559 * bootflow_cmdline_set() - Set the command line for a bootflow
561 * @value: New command-line string
562 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
564 int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
566 char *cmdline = NULL;
569 cmdline = strdup(value);
574 free(bflow->cmdline);
575 bflow->cmdline = cmdline;
580 #ifdef CONFIG_BOOTSTD_FULL
582 * on_bootargs() - Update the cmdline of a bootflow
584 static int on_bootargs(const char *name, const char *value, enum env_op op,
587 struct bootstd_priv *std;
588 struct bootflow *bflow;
591 ret = bootstd_get_priv(&std);
594 bflow = std->cur_bootflow;
600 case env_op_overwrite:
601 ret = bootflow_cmdline_set(bflow, value);
602 if (ret && ret != ENOENT)
606 bootflow_cmdline_set(bflow, NULL);
612 U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
616 * copy_in() - Copy a string into a cmdline buffer
618 * @buf: Buffer to copy into
619 * @end: End of buffer (pointer to char after the end)
620 * @arg: String to copy from
621 * @len: Number of chars to copy from @arg (note that this is not usually the
622 * sane as strlen(arg) since the string may contain following arguments)
623 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
625 * Returns: Number of chars written to @buf
627 static int copy_in(char *buf, char *end, const char *arg, int len,
632 /* copy the arg name */
635 memcpy(to, arg, len);
638 if (new_val == BOOTFLOWCL_EMPTY) {
641 bool need_quote = strchr(new_val, ' ');
642 len = strlen(new_val);
644 /* need space for value, equals sign and maybe two quotes */
645 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
650 memcpy(to, new_val, len);
659 int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
660 const char *set_arg, const char *new_val, int *posp)
662 bool found_arg = false;
669 from = cmdline ?: ∅
671 /* check if the value has quotes inside */
672 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
675 set_arg_len = strlen(set_arg);
676 for (to = buf, end = buf + maxlen; *from;) {
677 const char *val, *arg_end, *val_end, *p;
687 /* find the end of this arg */
692 for (p = from;; p++) {
703 } else if (*p == '"') {
705 } else if (!*p || *p == ' ') {
713 * At this point val_end points to the end of the value, or the
714 * last char after the arg name, if there is no label.
715 * arg_end is the char after the arg name
716 * val points to the value, or NULL if there is none
717 * char after the value.
723 * arg_end val val_end
725 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
726 (long)(arg_end - from), (long)(val - from),
727 (long)(val_end - from));
735 /* if this is the target arg, update it */
736 if (!strncmp(from, set_arg, arg_end - from)) {
738 bool has_quote = val_end[-1] == '"';
741 * exclude any start/end quotes from
746 *posp = val - cmdline + has_quote;
747 return val_end - val - 2 * has_quote;
751 /* delete this arg */
752 from = val_end + (*val_end == ' ');
753 log_debug("delete from: %s\n", from);
755 to--; /* drop the space we added */
759 ret = copy_in(to, end, from, arg_end - from, new_val);
764 /* if not the target arg, copy it unchanged */
768 len = val_end - from;
771 memcpy(to, from, len);
777 /* If we didn't find the arg, add it */
779 /* trying to delete something that is not there */
780 if (!new_val || !buf)
785 /* add a space to separate it from the previous arg */
786 if (to != buf && to[-1] != ' ')
788 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
789 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
795 /* delete any trailing space */
796 if (to > buf && to[-1] == ' ')
806 int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
807 const char *new_val, bool set_env)
813 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
818 ret = bootflow_cmdline_set(bflow, buf);
824 free(bflow->cmdline);
825 bflow->cmdline = cmd;
828 ret = env_set("bootargs", bflow->cmdline);
836 int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
840 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
845 int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
851 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
854 *val = bflow->cmdline + pos;
859 int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
861 struct serial_device_info info;
865 ret = serial_getinfo(gd->cur_serial_dev, &info);
870 if (!strcmp("earlycon", arg)) {
871 snprintf(buf, sizeof(buf),
872 "uart8250,mmio32,%#lx,%dn8", info.addr,
874 } else if (!strcmp("console", arg)) {
875 snprintf(buf, sizeof(buf),
876 "ttyS0,%dn8", info.baudrate);
880 printf("Unknown param '%s\n", arg);
884 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);