1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
15 #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 * iter_incr() - Move to the next item (method, part, bootdev)
160 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
162 static int iter_incr(struct bootflow_iter *iter)
169 log_debug("entry: err=%d\n", iter->err);
170 global = iter->doing_global;
172 if (iter->err == BF_NO_MORE_DEVICES)
173 return BF_NO_MORE_DEVICES;
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];
183 * If we have finished scanning the global bootmeths, start the
184 * normal bootdev scan
186 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
187 iter->num_methods = iter->first_glob_method;
188 iter->doing_global = false;
191 * Don't move to the next dev as we haven't tried this
198 /* No more bootmeths; start at the first one, and... */
199 iter->cur_method = 0;
200 iter->method = iter->method_order[iter->cur_method];
202 if (iter->err != BF_NO_MORE_PARTS) {
203 /* ...select next partition */
204 if (++iter->part <= iter->max_part)
208 /* No more partitions; start at the first one and... */
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.
218 /* ...select next bootdev */
219 if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
226 log_debug("inc_dev=%d\n", inc_dev);
228 ret = bootdev_setup_iter(iter, NULL, &dev,
230 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
231 (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
232 /* Move to the next bootdev in this uclass */
233 uclass_find_next_device(&dev);
235 log_debug("finished uclass %s\n",
236 dev_get_uclass_name(dev));
239 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
240 iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
241 log_debug("next in single\n");
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.
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) !=
255 log_debug("finished uclass %s\n",
256 dev_get_uclass_name(dev));
260 log_debug("labels %p\n", iter->labels);
262 ret = bootdev_next_label(iter, &dev,
265 ret = bootdev_next_prio(iter, &dev);
269 log_debug("ret=%d, dev=%p %s\n", ret, dev,
270 dev ? dev->name : "none");
272 bootflow_iter_set_dev(iter, NULL, 0);
275 * Probe the bootdev. This does not probe any attached
276 * block device, since they are siblings
278 ret = device_probe(dev);
279 log_debug("probe %s %d\n", dev->name, ret);
280 if (!log_msg_ret("probe", ret))
281 bootflow_iter_set_dev(iter, dev, method_flags);
285 /* if there are no more bootdevs, give up */
287 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
293 * bootflow_check() - Check if a bootflow can be obtained
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
300 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
305 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
306 bootflow_iter_set_dev(iter, NULL, 0);
307 ret = bootmeth_get_bootflow(iter->method, bflow);
309 return log_msg_ret("glob", ret);
315 ret = bootdev_get_bootflow(dev, iter, bflow);
317 /* If we got a valid bootflow, return it */
319 log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
320 dev->name, iter->part, iter->method->name);
324 /* Unless there is nothing more to try, move to the next device */
325 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
326 log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
327 dev->name, iter->part, iter->method->name, ret);
329 * For 'all' we return all bootflows, even
332 if (iter->flags & BOOTFLOWIF_ALL)
333 return log_msg_ret("all", ret);
336 return log_msg_ret("check", ret);
341 int bootflow_scan_first(struct udevice *dev, const char *label,
342 struct bootflow_iter *iter, int flags,
343 struct bootflow *bflow)
348 flags |= BOOTFLOWIF_SKIP_GLOBAL;
349 bootflow_iter_init(iter, flags);
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
355 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
357 return log_msg_ret("obmeth", -ENODEV);
359 /* Find the first bootmeth (there must be at least one!) */
360 iter->method = iter->method_order[iter->cur_method];
362 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
363 struct udevice *dev = NULL;
366 ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
368 return log_msg_ret("obdev", -ENODEV);
370 bootflow_iter_set_dev(iter, dev, method_flags);
373 ret = bootflow_check(iter, bflow);
375 log_debug("check - ret=%d\n", ret);
376 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
377 if (iter->flags & BOOTFLOWIF_ALL)
378 return log_msg_ret("all", ret);
381 ret = bootflow_scan_next(iter, bflow);
383 return log_msg_ret("get", ret);
389 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
394 ret = iter_incr(iter);
395 log_debug("iter_incr: ret=%d\n", ret);
396 if (ret == BF_NO_MORE_DEVICES)
397 return log_msg_ret("done", ret);
400 ret = bootflow_check(iter, bflow);
401 log_debug("check - ret=%d\n", ret);
405 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
406 if (iter->flags & BOOTFLOWIF_ALL)
407 return log_msg_ret("all", ret);
410 log_debug("incr failed, err=%d\n", ret);
417 void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
418 struct udevice *meth)
420 memset(bflow, '\0', sizeof(*bflow));
421 bflow->dev = bootdev;
422 bflow->method = meth;
423 bflow->state = BOOTFLOWST_BASE;
426 void bootflow_free(struct bootflow *bflow)
432 free(bflow->os_name);
433 free(bflow->fdt_fname);
436 void bootflow_remove(struct bootflow *bflow)
439 list_del(&bflow->bm_node);
440 list_del(&bflow->glob_node);
442 bootflow_free(bflow);
446 int bootflow_boot(struct bootflow *bflow)
450 if (bflow->state != BOOTFLOWST_READY)
451 return log_msg_ret("load", -EPROTO);
453 ret = bootmeth_boot(bflow->method, bflow);
455 return log_msg_ret("boot", ret);
458 * internal error, should not get here since we should have booted
459 * something or returned an error
462 return log_msg_ret("end", -EFAULT);
465 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
469 printf("** Booting bootflow '%s' with %s\n", bflow->name,
470 bflow->method->name);
471 if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
472 (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
473 printf("Using prior-stage device tree\n");
474 ret = bootflow_boot(bflow);
475 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
476 printf("Boot failed (err=%d)\n", ret);
482 printf("Bootflow not loaded (state '%s')\n",
483 bootflow_state_get_name(bflow->state));
486 printf("Boot method '%s' not supported\n", bflow->method->name);
489 /* Disable this bootflow for this iteration */
493 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
495 printf("Boot method '%s' failed and will not be retried\n",
496 bflow->method->name);
502 printf("Boot failed (err=%d)\n", ret);
509 int bootflow_iter_check_blk(const struct bootflow_iter *iter)
511 const struct udevice *media = dev_get_parent(iter->dev);
512 enum uclass_id id = device_get_uclass_id(media);
514 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
515 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
521 int bootflow_iter_check_sf(const struct bootflow_iter *iter)
523 const struct udevice *media = dev_get_parent(iter->dev);
524 enum uclass_id id = device_get_uclass_id(media);
526 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
527 if (id == UCLASS_SPI_FLASH)
533 int bootflow_iter_check_net(const struct bootflow_iter *iter)
535 const struct udevice *media = dev_get_parent(iter->dev);
536 enum uclass_id id = device_get_uclass_id(media);
538 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
539 if (id == UCLASS_ETH)
545 int bootflow_iter_check_system(const struct bootflow_iter *iter)
547 const struct udevice *media = dev_get_parent(iter->dev);
548 enum uclass_id id = device_get_uclass_id(media);
550 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
551 if (id == UCLASS_BOOTSTD)
558 * bootflow_cmdline_set() - Set the command line for a bootflow
560 * @value: New command-line string
561 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
563 int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
565 char *cmdline = NULL;
568 cmdline = strdup(value);
573 free(bflow->cmdline);
574 bflow->cmdline = cmdline;
579 #ifdef CONFIG_BOOTSTD_FULL
581 * on_bootargs() - Update the cmdline of a bootflow
583 static int on_bootargs(const char *name, const char *value, enum env_op op,
586 struct bootstd_priv *std;
587 struct bootflow *bflow;
590 ret = bootstd_get_priv(&std);
593 bflow = std->cur_bootflow;
599 case env_op_overwrite:
600 ret = bootflow_cmdline_set(bflow, value);
601 if (ret && ret != ENOENT)
605 bootflow_cmdline_set(bflow, NULL);
611 U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);