1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
14 #include <env_internal.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
20 /* error codes used to signal running out of things */
22 BF_NO_MORE_PARTS = -ESHUTDOWN,
23 BF_NO_MORE_DEVICES = -ENODEV,
27 * bootflow_state - name for each state
29 * See enum bootflow_state_t for what each of these means
31 static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
40 const char *bootflow_state_get_name(enum bootflow_state_t state)
42 /* This doesn't need to be a useful name, since it will never occur */
43 if (state < 0 || state >= BOOTFLOWST_COUNT)
46 return bootflow_state[state];
49 int bootflow_first_glob(struct bootflow **bflowp)
51 struct bootstd_priv *std;
54 ret = bootstd_get_priv(&std);
58 if (list_empty(&std->glob_head))
61 *bflowp = list_first_entry(&std->glob_head, struct bootflow,
67 int bootflow_next_glob(struct bootflow **bflowp)
69 struct bootstd_priv *std;
70 struct bootflow *bflow = *bflowp;
73 ret = bootstd_get_priv(&std);
79 if (list_is_last(&bflow->glob_node, &std->glob_head))
82 *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
87 void bootflow_iter_init(struct bootflow_iter *iter, int flags)
89 memset(iter, '\0', sizeof(*iter));
90 iter->first_glob_method = -1;
93 /* remember the first bootdevs we see */
94 iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
97 void bootflow_iter_uninit(struct bootflow_iter *iter)
99 free(iter->method_order);
102 int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
103 const struct udevice *bmeth)
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)
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 *));
120 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
122 * This sets iter->dev, records the device in the dev_used[] list and shows a
123 * message if required
125 * @iter: Iterator to update
126 * @dev: Bootdev to use, or NULL if there are no more
128 static void bootflow_iter_set_dev(struct bootflow_iter *iter,
129 struct udevice *dev, int method_flags)
131 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
133 log_debug("iter: Setting dev to %s, flags %x\n",
134 dev ? dev->name : "(none)", method_flags);
136 iter->method_flags = method_flags;
138 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;
143 if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_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",
152 printf("No more bootdevs\n");
158 * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
160 * Move through the following bootdevs until we find another in this media
163 * @devp: On entry, the device to check, on exit the new device, or NULL if
166 static void scan_next_in_uclass(struct udevice **devp)
168 struct udevice *dev = *devp;
169 enum uclass_id cur_id = device_get_uclass_id(dev->parent);
172 uclass_find_next_device(&dev);
173 } while (dev && cur_id != device_get_uclass_id(dev->parent));
179 * iter_incr() - Move to the next item (method, part, bootdev)
181 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
183 static int iter_incr(struct bootflow_iter *iter)
190 log_debug("entry: err=%d\n", iter->err);
191 global = iter->doing_global;
193 if (iter->err == BF_NO_MORE_DEVICES)
194 return BF_NO_MORE_DEVICES;
196 if (iter->err != BF_NO_MORE_PARTS) {
197 /* Get the next boothmethod */
198 if (++iter->cur_method < iter->num_methods) {
199 iter->method = iter->method_order[iter->cur_method];
204 * If we have finished scanning the global bootmeths, start the
205 * normal bootdev scan
207 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
208 iter->num_methods = iter->first_glob_method;
209 iter->doing_global = false;
212 * Don't move to the next dev as we haven't tried this
219 if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION)
220 return BF_NO_MORE_DEVICES;
222 /* No more bootmeths; start at the first one, and... */
223 iter->cur_method = 0;
224 iter->method = iter->method_order[iter->cur_method];
226 if (iter->err != BF_NO_MORE_PARTS) {
227 /* ...select next partition */
228 if (++iter->part <= iter->max_part)
232 /* No more partitions; start at the first one and... */
236 * Note: as far as we know, there is no partition table on the next
237 * bootdev, so set max_part to 0 until we discover otherwise. See
238 * bootdev_find_in_blk() for where this is set.
242 /* ...select next bootdev */
243 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
250 log_debug("inc_dev=%d\n", inc_dev);
252 ret = bootdev_setup_iter(iter, NULL, &dev,
254 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
255 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
256 scan_next_in_uclass(&dev);
258 log_debug("finished uclass %s\n",
259 dev_get_uclass_name(dev));
262 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
263 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
264 log_debug("next in single\n");
268 * Move to the next bootdev child of this media
269 * device. This ensures that we cover all the
270 * available SCSI IDs and LUNs.
272 device_find_next_child(&dev);
273 log_debug("- next %s\n",
274 dev ? dev->name : "(none)");
275 } while (dev && device_get_uclass_id(dev) !=
278 log_debug("finished uclass %s\n",
279 dev_get_uclass_name(dev));
283 log_debug("labels %p\n", iter->labels);
286 * when the label is "mmc" we want to scan all
287 * mmc bootdevs, not just the first. See
288 * bootdev_find_by_label() where this flag is
291 if (iter->method_flags &
292 BOOTFLOW_METHF_SINGLE_UCLASS) {
293 scan_next_in_uclass(&dev);
294 log_debug("looking for next device %s: %s\n",
296 dev ? dev->name : "<none>");
301 log_debug("looking at next label\n");
302 ret = bootdev_next_label(iter, &dev,
306 ret = bootdev_next_prio(iter, &dev);
310 log_debug("ret=%d, dev=%p %s\n", ret, dev,
311 dev ? dev->name : "none");
313 bootflow_iter_set_dev(iter, NULL, 0);
316 * Probe the bootdev. This does not probe any attached
317 * block device, since they are siblings
319 ret = device_probe(dev);
320 log_debug("probe %s %d\n", dev->name, ret);
321 if (!log_msg_ret("probe", ret))
322 bootflow_iter_set_dev(iter, dev, method_flags);
326 /* if there are no more bootdevs, give up */
328 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
334 * bootflow_check() - Check if a bootflow can be obtained
336 * @iter: Provides part, bootmeth to use
337 * @bflow: Bootflow to update on success
338 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
339 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
341 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
346 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
347 bootflow_iter_set_dev(iter, NULL, 0);
348 ret = bootmeth_get_bootflow(iter->method, bflow);
350 return log_msg_ret("glob", ret);
356 ret = bootdev_get_bootflow(dev, iter, bflow);
358 /* If we got a valid bootflow, return it */
360 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
361 dev->name, iter->part, iter->method->name);
365 /* Unless there is nothing more to try, move to the next device */
366 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
367 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
368 dev->name, iter->part, iter->method->name, ret);
370 * For 'all' we return all bootflows, even
373 if (iter->flags & BOOTFLOWIF_ALL)
374 return log_msg_ret("all", ret);
377 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)
470 if (!(bflow->flags & BOOTFLOWF_STATIC_BUF))
472 free(bflow->os_name);
473 free(bflow->fdt_fname);
474 free(bflow->bootmeth_priv);
477 void bootflow_remove(struct bootflow *bflow)
480 list_del(&bflow->bm_node);
481 list_del(&bflow->glob_node);
483 bootflow_free(bflow);
487 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
488 int bootflow_read_all(struct bootflow *bflow)
492 if (bflow->state != BOOTFLOWST_READY)
493 return log_msg_ret("rd", -EPROTO);
495 ret = bootmeth_read_all(bflow->method, bflow);
497 return log_msg_ret("rd2", ret);
501 #endif /* BOOTSTD_FULL */
503 int bootflow_boot(struct bootflow *bflow)
507 if (bflow->state != BOOTFLOWST_READY)
508 return log_msg_ret("load", -EPROTO);
510 ret = bootmeth_boot(bflow->method, bflow);
512 return log_msg_ret("boot", ret);
515 * internal error, should not get here since we should have booted
516 * something or returned an error
519 return log_msg_ret("end", -EFAULT);
522 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
526 printf("** Booting bootflow '%s' with %s\n", bflow->name,
527 bflow->method->name);
528 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
529 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
530 printf("Using prior-stage device tree\n");
531 ret = bootflow_boot(bflow);
532 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
533 printf("Boot failed (err=%d)\n", ret);
539 printf("Bootflow not loaded (state '%s')\n",
540 bootflow_state_get_name(bflow->state));
543 printf("Boot method '%s' not supported\n", bflow->method->name);
546 /* Disable this bootflow for this iteration */
550 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
552 printf("Boot method '%s' failed and will not be retried\n",
553 bflow->method->name);
559 printf("Boot failed (err=%d)\n", ret);
566 int bootflow_iter_check_blk(const struct bootflow_iter *iter)
568 const struct udevice *media = dev_get_parent(iter->dev);
569 enum uclass_id id = device_get_uclass_id(media);
571 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
572 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
578 int bootflow_iter_check_sf(const struct bootflow_iter *iter)
580 const struct udevice *media = dev_get_parent(iter->dev);
581 enum uclass_id id = device_get_uclass_id(media);
583 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
584 if (id == UCLASS_SPI_FLASH)
590 int bootflow_iter_check_net(const struct bootflow_iter *iter)
592 const struct udevice *media = dev_get_parent(iter->dev);
593 enum uclass_id id = device_get_uclass_id(media);
595 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
596 if (id == UCLASS_ETH)
602 int bootflow_iter_check_system(const struct bootflow_iter *iter)
604 const struct udevice *media = dev_get_parent(iter->dev);
605 enum uclass_id id = device_get_uclass_id(media);
607 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
608 if (id == UCLASS_BOOTSTD)
615 * bootflow_cmdline_set() - Set the command line for a bootflow
617 * @value: New command-line string
618 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
620 int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
622 char *cmdline = NULL;
625 cmdline = strdup(value);
630 free(bflow->cmdline);
631 bflow->cmdline = cmdline;
636 #ifdef CONFIG_BOOTSTD_FULL
638 * on_bootargs() - Update the cmdline of a bootflow
640 static int on_bootargs(const char *name, const char *value, enum env_op op,
643 struct bootstd_priv *std;
644 struct bootflow *bflow;
647 ret = bootstd_get_priv(&std);
650 bflow = std->cur_bootflow;
656 case env_op_overwrite:
657 ret = bootflow_cmdline_set(bflow, value);
658 if (ret && ret != ENOENT)
662 bootflow_cmdline_set(bflow, NULL);
668 U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
672 * copy_in() - Copy a string into a cmdline buffer
674 * @buf: Buffer to copy into
675 * @end: End of buffer (pointer to char after the end)
676 * @arg: String to copy from
677 * @len: Number of chars to copy from @arg (note that this is not usually the
678 * sane as strlen(arg) since the string may contain following arguments)
679 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
681 * Returns: Number of chars written to @buf
683 static int copy_in(char *buf, char *end, const char *arg, int len,
688 /* copy the arg name */
691 memcpy(to, arg, len);
694 if (new_val == BOOTFLOWCL_EMPTY) {
697 bool need_quote = strchr(new_val, ' ');
698 len = strlen(new_val);
700 /* need space for value, equals sign and maybe two quotes */
701 if (to + 1 + (need_quote ? 2 : 0) + len >= end)
706 memcpy(to, new_val, len);
715 int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
716 const char *set_arg, const char *new_val, int *posp)
718 bool found_arg = false;
725 from = cmdline ?: ∅
727 /* check if the value has quotes inside */
728 if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
731 set_arg_len = strlen(set_arg);
732 for (to = buf, end = buf + maxlen; *from;) {
733 const char *val, *arg_end, *val_end, *p;
743 /* find the end of this arg */
748 for (p = from;; p++) {
756 if (*p == '=' && !arg_end) {
759 } else if (*p == '"') {
761 } else if (!*p || *p == ' ') {
769 * At this point val_end points to the end of the value, or the
770 * last char after the arg name, if there is no label.
771 * arg_end is the char after the arg name
772 * val points to the value, or NULL if there is none
773 * char after the value.
779 * arg_end val val_end
781 log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
782 (long)(arg_end - from), (long)(val - from),
783 (long)(val_end - from));
791 /* if this is the target arg, update it */
792 if (arg_end - from == set_arg_len &&
793 !strncmp(from, set_arg, set_arg_len)) {
795 bool has_quote = val_end[-1] == '"';
798 * exclude any start/end quotes from
803 *posp = val - cmdline + has_quote;
804 return val_end - val - 2 * has_quote;
808 /* delete this arg */
809 from = val_end + (*val_end == ' ');
810 log_debug("delete from: %s\n", from);
812 to--; /* drop the space we added */
816 ret = copy_in(to, end, from, arg_end - from, new_val);
821 /* if not the target arg, copy it unchanged */
825 len = val_end - from;
828 memcpy(to, from, len);
834 /* If we didn't find the arg, add it */
836 /* trying to delete something that is not there */
837 if (!new_val || !buf)
842 /* add a space to separate it from the previous arg */
843 if (to != buf && to[-1] != ' ')
845 ret = copy_in(to, end, set_arg, set_arg_len, new_val);
846 log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
852 /* delete any trailing space */
853 if (to > buf && to[-1] == ' ')
863 int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
864 const char *new_val, bool set_env)
870 ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
875 ret = bootflow_cmdline_set(bflow, buf);
881 free(bflow->cmdline);
882 bflow->cmdline = cmd;
885 ret = env_set("bootargs", bflow->cmdline);
893 int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
897 ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);
902 int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
908 ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
911 *val = bflow->cmdline + pos;
916 int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
918 struct serial_device_info info;
922 ret = serial_getinfo(gd->cur_serial_dev, &info);
927 if (!strcmp("earlycon", arg)) {
928 snprintf(buf, sizeof(buf),
929 "uart8250,mmio32,%#lx,%dn8", info.addr,
931 } else if (!strcmp("console", arg)) {
932 snprintf(buf, sizeof(buf),
933 "ttyS0,%dn8", info.baudrate);
937 printf("Unknown param '%s\n", arg);
941 ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);