1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
7 #define LOG_CATEGORY UCLASS_BOOTSTD
14 #include <env_internal.h>
18 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize)
24 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
26 if (!ops->get_state_desc)
29 return ops->get_state_desc(dev, buf, maxsize);
32 int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
34 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
39 return ops->check(dev, iter);
42 int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
44 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
46 if (!ops->read_bootflow)
49 return ops->read_bootflow(dev, bflow);
52 int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
55 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
57 if (!ops->set_bootflow)
60 return ops->set_bootflow(dev, bflow, buf, size);
63 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
64 int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow)
66 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
71 return ops->read_all(dev, bflow);
73 #endif /* BOOTSTD_FULL */
75 int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
77 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
82 return ops->boot(dev, bflow);
85 int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
86 const char *file_path, ulong addr, ulong *sizep)
88 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
93 return ops->read_file(dev, bflow, file_path, addr, sizep);
96 int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
98 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
100 if (!ops->read_bootflow)
102 bootflow_init(bflow, NULL, dev);
104 return ops->read_bootflow(dev, bflow);
107 int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
109 struct bootstd_priv *std;
110 struct udevice **order;
114 ret = bootstd_get_priv(&std);
118 /* Create an array large enough */
119 count = std->bootmeth_count ? std->bootmeth_count :
120 uclass_id_count(UCLASS_BOOTMETH);
122 return log_msg_ret("count", -ENOENT);
124 order = calloc(count, sizeof(struct udevice *));
126 return log_msg_ret("order", -ENOMEM);
128 /* If we have an ordering, copy it */
129 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
133 * We don't support skipping global bootmeths. Instead, the user
134 * should omit them from the ordering
137 return log_msg_ret("glob", -EPERM);
138 memcpy(order, std->bootmeth_order,
139 count * sizeof(struct bootmeth *));
141 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
142 for (i = 0; i < count; i++) {
143 struct udevice *dev = order[i];
144 struct bootmeth_uc_plat *ucp;
147 ucp = dev_get_uclass_plat(dev);
148 is_global = ucp->flags &
151 iter->first_glob_method = i;
161 * Do two passes, one to find the normal bootmeths and another
162 * to find the global ones, if required, The global ones go at
165 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
167 iter->first_glob_method = upto;
169 * Get a list of bootmethods, in seq order (i.e. using
170 * aliases). There may be gaps so try to count up high
171 * enough to find them all.
173 for (i = 0; upto < count && i < 20 + count * 2; i++) {
174 struct bootmeth_uc_plat *ucp;
177 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
181 ucp = dev_get_uclass_plat(dev);
183 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
184 (ucp->flags & BOOTMETHF_GLOBAL);
185 if (pass ? is_global : !is_global)
192 return log_msg_ret("count2", -ENOENT);
194 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
195 iter->first_glob_method != -1 && iter->first_glob_method != count) {
196 iter->cur_method = iter->first_glob_method;
197 iter->doing_global = true;
199 iter->method_order = order;
200 iter->num_methods = count;
205 int bootmeth_set_order(const char *order_str)
207 struct bootstd_priv *std;
208 struct udevice **order;
209 int count, ret, i, len;
212 ret = bootstd_get_priv(&std);
217 free(std->bootmeth_order);
218 std->bootmeth_order = NULL;
219 std->bootmeth_count = 0;
223 /* Create an array large enough */
224 count = uclass_id_count(UCLASS_BOOTMETH);
226 return log_msg_ret("count", -ENOENT);
228 order = calloc(count + 1, sizeof(struct udevice *));
230 return log_msg_ret("order", -ENOMEM);
232 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
235 p = strchrnul(s, ' ');
237 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
240 printf("Unknown bootmeth '%.*s'\n", len, s);
247 free(std->bootmeth_order);
248 std->bootmeth_order = order;
249 std->bootmeth_count = i;
254 int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc)
259 ret = fs_set_blk_dev_with_part(desc, bflow->part);
261 return log_msg_ret("set", ret);
262 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
263 fs_set_type(bflow->fs_type);
269 int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
270 const char *prefix, const char *fname)
276 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
277 log_debug("trying: %s\n", path);
280 bflow->fname = strdup(path);
282 return log_msg_ret("name", -ENOMEM);
284 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
285 fs_set_type(bflow->fs_type);
287 ret = fs_size(path, &size);
288 log_debug(" %s - err=%d\n", path, ret);
290 /* Sadly FS closes the file after fs_size() so we must redo this */
291 ret2 = bootmeth_setup_fs(bflow, desc);
293 return log_msg_ret("fs", ret2);
296 return log_msg_ret("size", ret);
299 bflow->state = BOOTFLOWST_FILE;
304 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
311 log_debug(" - script file size %x\n", size);
312 if (size > size_limit)
313 return log_msg_ret("chk", -E2BIG);
315 ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf);
317 return log_msg_ret("all", ret);
319 bflow->state = BOOTFLOWST_READY;
325 int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
326 void **bufp, uint *sizep)
328 struct blk_desc *desc = NULL;
334 snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
335 log_debug("trying: %s\n", path);
338 desc = dev_get_uclass_plat(bflow->blk);
340 ret = bootmeth_setup_fs(bflow, desc);
342 return log_msg_ret("fs", ret);
344 ret = fs_size(path, &size);
345 log_debug(" %s - err=%d\n", path, ret);
347 ret = bootmeth_setup_fs(bflow, desc);
349 return log_msg_ret("fs", ret);
351 ret = fs_read_alloc(path, size, 0, &buf);
353 return log_msg_ret("all", ret);
361 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
362 const char *file_path, ulong addr, ulong *sizep)
364 struct blk_desc *desc = NULL;
370 desc = dev_get_uclass_plat(bflow->blk);
372 ret = bootmeth_setup_fs(bflow, desc);
374 return log_msg_ret("fs", ret);
376 ret = fs_size(file_path, &size);
378 return log_msg_ret("size", ret);
380 return log_msg_ret("spc", -ENOSPC);
382 ret = bootmeth_setup_fs(bflow, desc);
384 return log_msg_ret("fs", ret);
386 ret = fs_read(file_path, addr, 0, 0, &len_read);
394 #ifdef CONFIG_BOOTSTD_FULL
396 * on_bootmeths() - Update the bootmeth order
398 * This will check for a valid list of bootmeths and only apply it if valid.
400 static int on_bootmeths(const char *name, const char *value, enum env_op op,
407 case env_op_overwrite:
408 ret = bootmeth_set_order(value);
413 bootmeth_set_order(NULL);
419 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
420 #endif /* CONFIG_BOOTSTD_FULL */
422 UCLASS_DRIVER(bootmeth) = {
423 .id = UCLASS_BOOTMETH,
425 .flags = DM_UC_FLAG_SEQ_ALIAS,
426 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),