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
16 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
19 /* error codes used to signal running out of things */
21 BF_NO_MORE_PARTS = -ESHUTDOWN,
22 BF_NO_MORE_DEVICES = -ENODEV,
26 * bootflow_state - name for each state
28 * See enum bootflow_state_t for what each of these means
30 static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
39 const char *bootflow_state_get_name(enum bootflow_state_t state)
41 /* This doesn't need to be a useful name, since it will never occur */
42 if (state < 0 || state >= BOOTFLOWST_COUNT)
45 return bootflow_state[state];
48 int bootflow_first_glob(struct bootflow **bflowp)
50 struct bootstd_priv *std;
53 ret = bootstd_get_priv(&std);
57 if (list_empty(&std->glob_head))
60 *bflowp = list_first_entry(&std->glob_head, struct bootflow,
66 int bootflow_next_glob(struct bootflow **bflowp)
68 struct bootstd_priv *std;
69 struct bootflow *bflow = *bflowp;
72 ret = bootstd_get_priv(&std);
78 if (list_is_last(&bflow->glob_node, &std->glob_head))
81 *bflowp = list_entry(bflow->glob_node.next, struct bootflow, glob_node);
86 void bootflow_iter_init(struct bootflow_iter *iter, int flags)
88 memset(iter, '\0', sizeof(*iter));
89 iter->first_glob_method = -1;
93 void bootflow_iter_uninit(struct bootflow_iter *iter)
95 free(iter->dev_order);
96 free(iter->method_order);
99 int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
100 const struct udevice *bmeth)
102 /* We only support disabling the current bootmeth */
103 if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
104 iter->method_order[iter->cur_method] != bmeth)
107 memmove(&iter->method_order[iter->cur_method],
108 &iter->method_order[iter->cur_method + 1],
109 (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
116 static void bootflow_iter_set_dev(struct bootflow_iter *iter,
119 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);
122 if ((iter->flags & (BOOTFLOWF_SHOW | BOOTFLOWF_SINGLE_DEV)) ==
125 printf("Scanning bootdev '%s':\n", dev->name);
126 else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
127 ucp->flags & BOOTMETHF_GLOBAL)
128 printf("Scanning global bootmeth '%s':\n",
131 printf("No more bootdevs\n");
136 * iter_incr() - Move to the next item (method, part, bootdev)
138 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
140 static int iter_incr(struct bootflow_iter *iter)
147 global = iter->doing_global;
149 if (iter->err == BF_NO_MORE_DEVICES)
150 return BF_NO_MORE_DEVICES;
152 if (iter->err != BF_NO_MORE_PARTS) {
153 /* Get the next boothmethod */
154 if (++iter->cur_method < iter->num_methods) {
155 iter->method = iter->method_order[iter->cur_method];
160 * If we have finished scanning the global bootmeths, start the
161 * normal bootdev scan
163 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
164 iter->num_methods = iter->first_glob_method;
165 iter->doing_global = false;
168 * Don't move to the next dev as we haven't tried this
175 /* No more bootmeths; start at the first one, and... */
176 iter->cur_method = 0;
177 iter->method = iter->method_order[iter->cur_method];
179 if (iter->err != BF_NO_MORE_PARTS) {
180 /* ...select next partition */
181 if (++iter->part <= iter->max_part)
185 /* No more partitions; start at the first one and...*/
189 * Note: as far as we know, there is no partition table on the next
190 * bootdev, so set max_part to 0 until we discover otherwise. See
191 * bootdev_find_in_blk() for where this is set.
195 /* ...select next bootdev */
196 if (iter->flags & BOOTFLOWF_SINGLE_DEV) {
201 if (iter->cur_dev == iter->num_devs) {
203 bootflow_iter_set_dev(iter, NULL);
205 dev = iter->dev_order[iter->cur_dev];
206 ret = device_probe(dev);
207 if (!log_msg_ret("probe", ret))
208 bootflow_iter_set_dev(iter, dev);
212 /* if there are no more bootdevs, give up */
214 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
220 * bootflow_check() - Check if a bootflow can be obtained
222 * @iter: Provides part, bootmeth to use
223 * @bflow: Bootflow to update on success
224 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
225 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
227 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
232 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
233 bootflow_iter_set_dev(iter, NULL);
234 ret = bootmeth_get_bootflow(iter->method, bflow);
236 return log_msg_ret("glob", ret);
242 ret = bootdev_get_bootflow(dev, iter, bflow);
244 /* If we got a valid bootflow, return it */
246 log_debug("Bootdevice '%s' part %d method '%s': Found bootflow\n",
247 dev->name, iter->part, iter->method->name);
251 /* Unless there is nothing more to try, move to the next device */
252 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
253 log_debug("Bootdevice '%s' part %d method '%s': Error %d\n",
254 dev->name, iter->part, iter->method->name, ret);
256 * For 'all' we return all bootflows, even
259 if (iter->flags & BOOTFLOWF_ALL)
260 return log_msg_ret("all", ret);
263 return log_msg_ret("check", ret);
268 int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
269 int flags, struct bootflow *bflow)
274 flags |= BOOTFLOWF_SKIP_GLOBAL;
275 bootflow_iter_init(iter, flags);
277 ret = bootdev_setup_iter_order(iter, &dev);
279 return log_msg_ret("obdev", -ENODEV);
281 ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWF_SKIP_GLOBAL));
283 return log_msg_ret("obmeth", -ENODEV);
285 /* Find the first bootmeth (there must be at least one!) */
286 iter->method = iter->method_order[iter->cur_method];
287 if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global)
288 bootflow_iter_set_dev(iter, dev);
290 ret = bootflow_check(iter, bflow);
292 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
293 if (iter->flags & BOOTFLOWF_ALL)
294 return log_msg_ret("all", ret);
297 ret = bootflow_scan_next(iter, bflow);
299 return log_msg_ret("get", ret);
305 int bootflow_scan_first(struct bootflow_iter *iter, int flags,
306 struct bootflow *bflow)
310 ret = bootflow_scan_bootdev(NULL, iter, flags, bflow);
312 return log_msg_ret("start", ret);
317 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
322 ret = iter_incr(iter);
323 if (ret == BF_NO_MORE_DEVICES)
324 return log_msg_ret("done", ret);
327 ret = bootflow_check(iter, bflow);
331 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
332 if (iter->flags & BOOTFLOWF_ALL)
333 return log_msg_ret("all", ret);
342 void bootflow_free(struct bootflow *bflow)
350 void bootflow_remove(struct bootflow *bflow)
353 list_del(&bflow->bm_node);
354 list_del(&bflow->glob_node);
356 bootflow_free(bflow);
360 int bootflow_boot(struct bootflow *bflow)
364 if (bflow->state != BOOTFLOWST_READY)
365 return log_msg_ret("load", -EPROTO);
367 ret = bootmeth_boot(bflow->method, bflow);
369 return log_msg_ret("boot", ret);
372 * internal error, should not get here since we should have booted
373 * something or returned an error
376 return log_msg_ret("end", -EFAULT);
379 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
383 printf("** Booting bootflow '%s' with %s\n", bflow->name,
384 bflow->method->name);
385 ret = bootflow_boot(bflow);
386 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
387 printf("Boot failed (err=%d)\n", ret);
393 printf("Bootflow not loaded (state '%s')\n",
394 bootflow_state_get_name(bflow->state));
397 printf("Boot method '%s' not supported\n", bflow->method->name);
400 /* Disable this bootflow for this iteration */
404 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
406 printf("Boot method '%s' failed and will not be retried\n",
407 bflow->method->name);
413 printf("Boot failed (err=%d)\n", ret);
420 int bootflow_iter_uses_blk_dev(const struct bootflow_iter *iter)
422 const struct udevice *media = dev_get_parent(iter->dev);
423 enum uclass_id id = device_get_uclass_id(media);
425 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
426 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD)
432 int bootflow_iter_uses_network(const struct bootflow_iter *iter)
434 const struct udevice *media = dev_get_parent(iter->dev);
435 enum uclass_id id = device_get_uclass_id(media);
437 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
438 if (id == UCLASS_ETH)
444 int bootflow_iter_uses_system(const struct bootflow_iter *iter)
446 const struct udevice *media = dev_get_parent(iter->dev);
447 enum uclass_id id = device_get_uclass_id(media);
449 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
450 if (id == UCLASS_BOOTSTD)