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
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 int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
66 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
71 return ops->boot(dev, bflow);
74 int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
75 const char *file_path, ulong addr, ulong *sizep)
77 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
82 return ops->read_file(dev, bflow, file_path, addr, sizep);
85 int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
87 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
89 if (!ops->read_bootflow)
91 bootflow_init(bflow, NULL, dev);
93 return ops->read_bootflow(dev, bflow);
96 int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
98 struct bootstd_priv *std;
99 struct udevice **order;
103 ret = bootstd_get_priv(&std);
107 /* Create an array large enough */
108 count = std->bootmeth_count ? std->bootmeth_count :
109 uclass_id_count(UCLASS_BOOTMETH);
111 return log_msg_ret("count", -ENOENT);
113 order = calloc(count, sizeof(struct udevice *));
115 return log_msg_ret("order", -ENOMEM);
117 /* If we have an ordering, copy it */
118 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
122 * We don't support skipping global bootmeths. Instead, the user
123 * should omit them from the ordering
126 return log_msg_ret("glob", -EPERM);
127 memcpy(order, std->bootmeth_order,
128 count * sizeof(struct bootmeth *));
130 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
131 for (i = 0; i < count; i++) {
132 struct udevice *dev = order[i];
133 struct bootmeth_uc_plat *ucp;
136 ucp = dev_get_uclass_plat(dev);
137 is_global = ucp->flags &
140 iter->first_glob_method = i;
150 * Do two passes, one to find the normal bootmeths and another
151 * to find the global ones, if required, The global ones go at
154 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
156 iter->first_glob_method = upto;
158 * Get a list of bootmethods, in seq order (i.e. using
159 * aliases). There may be gaps so try to count up high
160 * enough to find them all.
162 for (i = 0; upto < count && i < 20 + count * 2; i++) {
163 struct bootmeth_uc_plat *ucp;
166 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
170 ucp = dev_get_uclass_plat(dev);
172 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
173 (ucp->flags & BOOTMETHF_GLOBAL);
174 if (pass ? is_global : !is_global)
181 return log_msg_ret("count2", -ENOENT);
183 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
184 iter->first_glob_method != -1 && iter->first_glob_method != count) {
185 iter->cur_method = iter->first_glob_method;
186 iter->doing_global = true;
188 iter->method_order = order;
189 iter->num_methods = count;
194 int bootmeth_set_order(const char *order_str)
196 struct bootstd_priv *std;
197 struct udevice **order;
198 int count, ret, i, len;
201 ret = bootstd_get_priv(&std);
206 free(std->bootmeth_order);
207 std->bootmeth_order = NULL;
208 std->bootmeth_count = 0;
212 /* Create an array large enough */
213 count = uclass_id_count(UCLASS_BOOTMETH);
215 return log_msg_ret("count", -ENOENT);
217 order = calloc(count + 1, sizeof(struct udevice *));
219 return log_msg_ret("order", -ENOMEM);
221 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
224 p = strchrnul(s, ' ');
226 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
229 printf("Unknown bootmeth '%.*s'\n", len, s);
236 free(std->bootmeth_order);
237 std->bootmeth_order = order;
238 std->bootmeth_count = i;
244 * setup_fs() - Set up read to read a file
246 * We must redo the setup before each filesystem operation. This function
247 * handles that, including setting the filesystem type if a block device is not
250 * @bflow: Information about file to try
251 * @desc: Block descriptor to read from (NULL if not a block device)
252 * Return: 0 if OK, -ve on error
254 static int 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 = setup_fs(bflow, desc);
293 return log_msg_ret("fs", ret2);
296 return log_msg_ret("size", ret);
299 bflow->state = BOOTFLOWST_FILE;
304 static int alloc_file(const char *fname, uint size, void **bufp)
311 buf = malloc(size + 1);
313 return log_msg_ret("buf", -ENOMEM);
314 addr = map_to_sysmem(buf);
316 ret = fs_read(fname, addr, 0, size, &bytes_read);
319 return log_msg_ret("read", ret);
321 if (size != bytes_read)
322 return log_msg_ret("bread", -EINVAL);
330 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
337 log_debug(" - script file size %x\n", size);
338 if (size > size_limit)
339 return log_msg_ret("chk", -E2BIG);
341 ret = alloc_file(bflow->fname, bflow->size, &buf);
343 return log_msg_ret("all", ret);
345 bflow->state = BOOTFLOWST_READY;
351 int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
352 void **bufp, uint *sizep)
354 struct blk_desc *desc = NULL;
360 snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
361 log_debug("trying: %s\n", path);
364 desc = dev_get_uclass_plat(bflow->blk);
366 ret = setup_fs(bflow, desc);
368 return log_msg_ret("fs", ret);
370 ret = fs_size(path, &size);
371 log_debug(" %s - err=%d\n", path, ret);
373 ret = setup_fs(bflow, desc);
375 return log_msg_ret("fs", ret);
377 ret = alloc_file(path, size, &buf);
379 return log_msg_ret("all", ret);
387 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
388 const char *file_path, ulong addr, ulong *sizep)
390 struct blk_desc *desc = NULL;
396 desc = dev_get_uclass_plat(bflow->blk);
398 ret = setup_fs(bflow, desc);
400 return log_msg_ret("fs", ret);
402 ret = fs_size(file_path, &size);
404 return log_msg_ret("size", ret);
406 return log_msg_ret("spc", -ENOSPC);
408 ret = setup_fs(bflow, desc);
410 return log_msg_ret("fs", ret);
412 ret = fs_read(file_path, addr, 0, 0, &len_read);
420 #ifdef CONFIG_BOOTSTD_FULL
422 * on_bootmeths() - Update the bootmeth order
424 * This will check for a valid baudrate and only apply it if valid.
426 static int on_bootmeths(const char *name, const char *value, enum env_op op,
433 case env_op_overwrite:
434 ret = bootmeth_set_order(value);
439 bootmeth_set_order(NULL);
445 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
446 #endif /* CONFIG_BOOTSTD_FULL */
448 UCLASS_DRIVER(bootmeth) = {
449 .id = UCLASS_BOOTMETH,
451 .flags = DM_UC_FLAG_SEQ_ALIAS,
452 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),