1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
15 #include <env_internal.h>
19 #include <dm/uclass-internal.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize)
25 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
27 if (!ops->get_state_desc)
30 return ops->get_state_desc(dev, buf, maxsize);
33 int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
35 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
40 return ops->check(dev, iter);
43 int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
45 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
47 if (!ops->read_bootflow)
50 return ops->read_bootflow(dev, bflow);
53 int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
56 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
58 if (!ops->set_bootflow)
61 return ops->set_bootflow(dev, bflow, buf, size);
64 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
65 int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow)
67 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
72 return ops->read_all(dev, bflow);
74 #endif /* BOOTSTD_FULL */
76 int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
78 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
83 return ops->boot(dev, bflow);
86 int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
87 const char *file_path, ulong addr, ulong *sizep)
89 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
94 return ops->read_file(dev, bflow, file_path, addr, sizep);
97 int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
99 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
101 if (!ops->read_bootflow)
103 bootflow_init(bflow, NULL, dev);
105 return ops->read_bootflow(dev, bflow);
108 int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
110 struct bootstd_priv *std;
111 struct udevice **order;
115 ret = bootstd_get_priv(&std);
119 /* Create an array large enough */
120 count = std->bootmeth_count ? std->bootmeth_count :
121 uclass_id_count(UCLASS_BOOTMETH);
123 return log_msg_ret("count", -ENOENT);
125 order = calloc(count, sizeof(struct udevice *));
127 return log_msg_ret("order", -ENOMEM);
129 /* If we have an ordering, copy it */
130 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
134 * We don't support skipping global bootmeths. Instead, the user
135 * should omit them from the ordering
138 return log_msg_ret("glob", -EPERM);
139 memcpy(order, std->bootmeth_order,
140 count * sizeof(struct bootmeth *));
142 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
143 for (i = 0; i < count; i++) {
144 struct udevice *dev = order[i];
145 struct bootmeth_uc_plat *ucp;
148 ucp = dev_get_uclass_plat(dev);
149 is_global = ucp->flags &
152 iter->first_glob_method = i;
162 * Do two passes, one to find the normal bootmeths and another
163 * to find the global ones, if required, The global ones go at
166 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
168 iter->first_glob_method = upto;
170 * Get a list of bootmethods, in seq order (i.e. using
171 * aliases). There may be gaps so try to count up high
172 * enough to find them all.
174 for (i = 0; upto < count && i < 20 + count * 2; i++) {
175 struct bootmeth_uc_plat *ucp;
178 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
182 ucp = dev_get_uclass_plat(dev);
184 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
185 (ucp->flags & BOOTMETHF_GLOBAL);
186 if (pass ? is_global : !is_global)
193 return log_msg_ret("count2", -ENOENT);
195 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
196 iter->first_glob_method != -1 && iter->first_glob_method != count) {
197 iter->cur_method = iter->first_glob_method;
198 iter->doing_global = true;
200 iter->method_order = order;
201 iter->num_methods = count;
206 int bootmeth_set_order(const char *order_str)
208 struct bootstd_priv *std;
209 struct udevice **order;
210 int count, ret, i, len;
213 ret = bootstd_get_priv(&std);
218 free(std->bootmeth_order);
219 std->bootmeth_order = NULL;
220 std->bootmeth_count = 0;
224 /* Create an array large enough */
225 count = uclass_id_count(UCLASS_BOOTMETH);
227 return log_msg_ret("count", -ENOENT);
229 order = calloc(count + 1, sizeof(struct udevice *));
231 return log_msg_ret("order", -ENOMEM);
233 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
236 p = strchrnul(s, ' ');
238 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
241 printf("Unknown bootmeth '%.*s'\n", len, s);
248 free(std->bootmeth_order);
249 std->bootmeth_order = order;
250 std->bootmeth_count = i;
255 int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc)
260 ret = fs_set_blk_dev_with_part(desc, bflow->part);
262 return log_msg_ret("set", ret);
263 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
264 fs_set_type(bflow->fs_type);
270 int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
271 const char *prefix, const char *fname)
277 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
278 log_debug("trying: %s\n", path);
281 bflow->fname = strdup(path);
283 return log_msg_ret("name", -ENOMEM);
285 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
286 fs_set_type(bflow->fs_type);
288 ret = fs_size(path, &size);
289 log_debug(" %s - err=%d\n", path, ret);
291 /* Sadly FS closes the file after fs_size() so we must redo this */
292 ret2 = bootmeth_setup_fs(bflow, desc);
294 return log_msg_ret("fs", ret2);
297 return log_msg_ret("size", ret);
300 bflow->state = BOOTFLOWST_FILE;
305 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
312 log_debug(" - script file size %x\n", size);
313 if (size > size_limit)
314 return log_msg_ret("chk", -E2BIG);
316 ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf);
318 return log_msg_ret("all", ret);
320 bflow->state = BOOTFLOWST_READY;
326 int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
327 void **bufp, uint *sizep)
329 struct blk_desc *desc = NULL;
335 snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
336 log_debug("trying: %s\n", path);
339 desc = dev_get_uclass_plat(bflow->blk);
341 ret = bootmeth_setup_fs(bflow, desc);
343 return log_msg_ret("fs", ret);
345 ret = fs_size(path, &size);
346 log_debug(" %s - err=%d\n", path, ret);
348 ret = bootmeth_setup_fs(bflow, desc);
350 return log_msg_ret("fs", ret);
352 ret = fs_read_alloc(path, size, 0, &buf);
354 return log_msg_ret("all", ret);
362 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
363 const char *file_path, ulong addr, ulong *sizep)
365 struct blk_desc *desc = NULL;
371 desc = dev_get_uclass_plat(bflow->blk);
373 ret = bootmeth_setup_fs(bflow, desc);
375 return log_msg_ret("fs", ret);
377 ret = fs_size(file_path, &size);
379 return log_msg_ret("size", ret);
381 return log_msg_ret("spc", -ENOSPC);
383 ret = bootmeth_setup_fs(bflow, desc);
385 return log_msg_ret("fs", ret);
387 ret = fs_read(file_path, addr, 0, 0, &len_read);
395 #ifdef CONFIG_BOOTSTD_FULL
397 * on_bootmeths() - Update the bootmeth order
399 * This will check for a valid list of bootmeths and only apply it if valid.
401 static int on_bootmeths(const char *name, const char *value, enum env_op op,
408 case env_op_overwrite:
409 ret = bootmeth_set_order(value);
414 bootmeth_set_order(NULL);
420 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
421 #endif /* CONFIG_BOOTSTD_FULL */
423 UCLASS_DRIVER(bootmeth) = {
424 .id = UCLASS_BOOTMETH,
426 .flags = DM_UC_FLAG_SEQ_ALIAS,
427 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),