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));
92 void bootflow_iter_uninit(struct bootflow_iter *iter)
94 free(iter->dev_order);
95 free(iter->method_order);
98 int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
99 const struct udevice *bmeth)
101 /* We only support disabling the current bootmeth */
102 if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
103 iter->method_order[iter->cur_method] != bmeth)
106 memmove(&iter->method_order[iter->cur_method],
107 &iter->method_order[iter->cur_method + 1],
108 (iter->num_methods - iter->cur_method - 1) * sizeof(void *));
115 static void bootflow_iter_set_dev(struct bootflow_iter *iter,
119 if ((iter->flags & (BOOTFLOWF_SHOW | BOOTFLOWF_SINGLE_DEV)) ==
122 printf("Scanning bootdev '%s':\n", dev->name);
124 printf("No more bootdevs\n");
129 * iter_incr() - Move to the next item (method, part, bootdev)
131 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
133 static int iter_incr(struct bootflow_iter *iter)
138 if (iter->err == BF_NO_MORE_DEVICES)
139 return BF_NO_MORE_DEVICES;
141 if (iter->err != BF_NO_MORE_PARTS) {
142 /* Get the next boothmethod */
143 if (++iter->cur_method < iter->num_methods) {
144 iter->method = iter->method_order[iter->cur_method];
149 /* No more bootmeths; start at the first one, and... */
150 iter->cur_method = 0;
151 iter->method = iter->method_order[iter->cur_method];
153 if (iter->err != BF_NO_MORE_PARTS) {
154 /* ...select next partition */
155 if (++iter->part <= iter->max_part)
159 /* No more partitions; start at the first one and...*/
163 * Note: as far as we know, there is no partition table on the next
164 * bootdev, so set max_part to 0 until we discover otherwise. See
165 * bootdev_find_in_blk() for where this is set.
169 /* ...select next bootdev */
170 if (iter->flags & BOOTFLOWF_SINGLE_DEV) {
172 } else if (++iter->cur_dev == iter->num_devs) {
174 bootflow_iter_set_dev(iter, NULL);
176 dev = iter->dev_order[iter->cur_dev];
177 ret = device_probe(dev);
178 if (!log_msg_ret("probe", ret))
179 bootflow_iter_set_dev(iter, dev);
182 /* if there are no more bootdevs, give up */
184 return log_msg_ret("incr", BF_NO_MORE_DEVICES);
190 * bootflow_check() - Check if a bootflow can be obtained
192 * @iter: Provides part, bootmeth to use
193 * @bflow: Bootflow to update on success
194 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
195 * BF_NO_MORE_PARTS if there are no more partitions on bootdev
197 static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
203 ret = bootdev_get_bootflow(dev, iter, bflow);
205 /* If we got a valid bootflow, return it */
207 log_debug("Bootdevice '%s' part %d method '%s': Found bootflow\n",
208 dev->name, iter->part, iter->method->name);
212 /* Unless there is nothing more to try, move to the next device */
213 else if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
214 log_debug("Bootdevice '%s' part %d method '%s': Error %d\n",
215 dev->name, iter->part, iter->method->name, ret);
217 * For 'all' we return all bootflows, even
220 if (iter->flags & BOOTFLOWF_ALL)
221 return log_msg_ret("all", ret);
224 return log_msg_ret("check", ret);
229 int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
230 int flags, struct bootflow *bflow)
234 bootflow_iter_init(iter, flags);
236 ret = bootdev_setup_iter_order(iter, &dev);
238 return log_msg_ret("obdev", -ENODEV);
239 bootflow_iter_set_dev(iter, dev);
241 ret = bootmeth_setup_iter_order(iter);
243 return log_msg_ret("obmeth", -ENODEV);
245 /* Find the first bootmeth (there must be at least one!) */
246 iter->method = iter->method_order[iter->cur_method];
248 ret = bootflow_check(iter, bflow);
250 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
251 if (iter->flags & BOOTFLOWF_ALL)
252 return log_msg_ret("all", ret);
255 ret = bootflow_scan_next(iter, bflow);
257 return log_msg_ret("get", ret);
263 int bootflow_scan_first(struct bootflow_iter *iter, int flags,
264 struct bootflow *bflow)
268 ret = bootflow_scan_bootdev(NULL, iter, flags, bflow);
270 return log_msg_ret("start", ret);
275 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
280 ret = iter_incr(iter);
281 if (ret == BF_NO_MORE_DEVICES)
282 return log_msg_ret("done", ret);
285 ret = bootflow_check(iter, bflow);
289 if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
290 if (iter->flags & BOOTFLOWF_ALL)
291 return log_msg_ret("all", ret);
300 void bootflow_free(struct bootflow *bflow)
308 void bootflow_remove(struct bootflow *bflow)
310 list_del(&bflow->bm_node);
311 list_del(&bflow->glob_node);
313 bootflow_free(bflow);
317 int bootflow_boot(struct bootflow *bflow)
321 if (bflow->state != BOOTFLOWST_READY)
322 return log_msg_ret("load", -EPROTO);
324 ret = bootmeth_boot(bflow->method, bflow);
326 return log_msg_ret("boot", ret);
329 * internal error, should not get here since we should have booted
330 * something or returned an error
333 return log_msg_ret("end", -EFAULT);
336 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
340 printf("** Booting bootflow '%s' with %s\n", bflow->name,
341 bflow->method->name);
342 ret = bootflow_boot(bflow);
343 if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
344 printf("Boot failed (err=%d)\n", ret);
350 printf("Bootflow not loaded (state '%s')\n",
351 bootflow_state_get_name(bflow->state));
354 printf("Boot method '%s' not supported\n", bflow->method->name);
357 /* Disable this bootflow for this iteration */
361 ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
363 printf("Boot method '%s' failed and will not be retried\n",
364 bflow->method->name);
370 printf("Boot failed (err=%d)\n", ret);
377 int bootflow_iter_uses_blk_dev(const struct bootflow_iter *iter)
379 const struct udevice *media = dev_get_parent(iter->dev);
380 enum uclass_id id = device_get_uclass_id(media);
382 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
383 if (id != UCLASS_ETH && id != UCLASS_BOOTSTD)
389 int bootflow_iter_uses_network(const struct bootflow_iter *iter)
391 const struct udevice *media = dev_get_parent(iter->dev);
392 enum uclass_id id = device_get_uclass_id(media);
394 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
395 if (id == UCLASS_ETH)
401 int bootflow_iter_uses_system(const struct bootflow_iter *iter)
403 const struct udevice *media = dev_get_parent(iter->dev);
404 enum uclass_id id = device_get_uclass_id(media);
406 log_debug("uclass %d: %s\n", id, uclass_get_name(id));
407 if (id == UCLASS_BOOTSTD)