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_check(struct udevice *dev, struct bootflow_iter *iter)
25 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
30 return ops->check(dev, iter);
33 int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
35 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
37 if (!ops->read_bootflow)
40 return ops->read_bootflow(dev, bflow);
43 int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
45 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
50 return ops->boot(dev, bflow);
53 int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
54 const char *file_path, ulong addr, ulong *sizep)
56 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
61 return ops->read_file(dev, bflow, file_path, addr, sizep);
65 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
67 * This sets up the ordering information in @iter, based on the selected
68 * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
69 * ordering there, then all bootmethods are added
71 * @iter: Iterator to update with the order
72 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
75 int bootmeth_setup_iter_order(struct bootflow_iter *iter)
77 struct bootstd_priv *std;
78 struct udevice **order;
82 ret = bootstd_get_priv(&std);
86 /* Create an array large enough */
87 count = std->bootmeth_count ? std->bootmeth_count :
88 uclass_id_count(UCLASS_BOOTMETH);
90 return log_msg_ret("count", -ENOENT);
92 order = calloc(count, sizeof(struct udevice *));
94 return log_msg_ret("order", -ENOMEM);
96 /* If we have an ordering, copy it */
97 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
98 memcpy(order, std->bootmeth_order,
99 count * sizeof(struct bootmeth *));
105 * Get a list of bootmethods, in seq order (i.e. using aliases).
106 * There may be gaps so try to count up high enough to find them
109 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
110 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
118 iter->method_order = order;
119 iter->num_methods = count;
120 iter->cur_method = 0;
125 int bootmeth_set_order(const char *order_str)
127 struct bootstd_priv *std;
128 struct udevice **order;
129 int count, ret, i, len;
132 ret = bootstd_get_priv(&std);
137 free(std->bootmeth_order);
138 std->bootmeth_order = NULL;
139 std->bootmeth_count = 0;
143 /* Create an array large enough */
144 count = uclass_id_count(UCLASS_BOOTMETH);
146 return log_msg_ret("count", -ENOENT);
148 order = calloc(count + 1, sizeof(struct udevice *));
150 return log_msg_ret("order", -ENOMEM);
152 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
155 p = strchrnul(s, ' ');
157 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
160 printf("Unknown bootmeth '%.*s'\n", len, s);
167 free(std->bootmeth_order);
168 std->bootmeth_order = order;
169 std->bootmeth_count = i;
175 * setup_fs() - Set up read to read a file
177 * We must redo the setup before each filesystem operation. This function
178 * handles that, including setting the filesystem type if a block device is not
181 * @bflow: Information about file to try
182 * @desc: Block descriptor to read from (NULL if not a block device)
183 * Return: 0 if OK, -ve on error
185 static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
190 ret = fs_set_blk_dev_with_part(desc, bflow->part);
192 return log_msg_ret("set", ret);
193 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
194 fs_set_type(bflow->fs_type);
200 int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
201 const char *prefix, const char *fname)
207 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
208 log_debug("trying: %s\n", path);
211 bflow->fname = strdup(path);
213 return log_msg_ret("name", -ENOMEM);
215 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
216 fs_set_type(bflow->fs_type);
218 ret = fs_size(path, &size);
219 log_debug(" %s - err=%d\n", path, ret);
221 /* Sadly FS closes the file after fs_size() so we must redo this */
222 ret2 = setup_fs(bflow, desc);
224 return log_msg_ret("fs", ret2);
227 return log_msg_ret("size", ret);
230 bflow->state = BOOTFLOWST_FILE;
235 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
244 log_debug(" - script file size %x\n", size);
245 if (size > size_limit)
246 return log_msg_ret("chk", -E2BIG);
248 buf = memalign(align, size + 1);
250 return log_msg_ret("buf", -ENOMEM);
251 addr = map_to_sysmem(buf);
253 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
256 return log_msg_ret("read", ret);
258 if (size != bytes_read)
259 return log_msg_ret("bread", -EINVAL);
261 bflow->state = BOOTFLOWST_READY;
267 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
268 const char *file_path, ulong addr, ulong *sizep)
270 struct blk_desc *desc = NULL;
276 desc = dev_get_uclass_plat(bflow->blk);
278 ret = setup_fs(bflow, desc);
280 return log_msg_ret("fs", ret);
282 ret = fs_size(file_path, &size);
284 return log_msg_ret("size", ret);
286 return log_msg_ret("spc", -ENOSPC);
288 ret = setup_fs(bflow, desc);
290 return log_msg_ret("fs", ret);
292 ret = fs_read(file_path, addr, 0, 0, &len_read);
300 #ifdef CONFIG_BOOTSTD_FULL
302 * on_bootmeths() - Update the bootmeth order
304 * This will check for a valid baudrate and only apply it if valid.
306 static int on_bootmeths(const char *name, const char *value, enum env_op op,
313 case env_op_overwrite:
314 ret = bootmeth_set_order(value);
319 bootmeth_set_order(NULL);
325 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
326 #endif /* CONFIG_BOOTSTD_FULL */
328 UCLASS_DRIVER(bootmeth) = {
329 .id = UCLASS_BOOTMETH,
331 .flags = DM_UC_FLAG_SEQ_ALIAS,
332 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),