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_boot(struct udevice *dev, struct bootflow *bflow)
55 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
60 return ops->boot(dev, bflow);
63 int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
64 const char *file_path, ulong addr, ulong *sizep)
66 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
71 return ops->read_file(dev, bflow, file_path, addr, sizep);
74 int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
76 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
78 if (!ops->read_bootflow)
80 memset(bflow, '\0', sizeof(*bflow));
83 bflow->state = BOOTFLOWST_BASE;
85 return ops->read_bootflow(dev, bflow);
89 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
91 * This sets up the ordering information in @iter, based on the selected
92 * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
93 * ordering there, then all bootmethods are added
95 * @iter: Iterator to update with the order
96 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
99 int bootmeth_setup_iter_order(struct bootflow_iter *iter)
101 struct bootstd_priv *std;
102 struct udevice **order;
106 ret = bootstd_get_priv(&std);
110 /* Create an array large enough */
111 count = std->bootmeth_count ? std->bootmeth_count :
112 uclass_id_count(UCLASS_BOOTMETH);
114 return log_msg_ret("count", -ENOENT);
116 order = calloc(count, sizeof(struct udevice *));
118 return log_msg_ret("order", -ENOMEM);
120 /* If we have an ordering, copy it */
121 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
122 memcpy(order, std->bootmeth_order,
123 count * sizeof(struct bootmeth *));
129 * Get a list of bootmethods, in seq order (i.e. using aliases).
130 * There may be gaps so try to count up high enough to find them
133 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
134 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
142 return log_msg_ret("count2", -ENOENT);
144 iter->method_order = order;
145 iter->num_methods = count;
146 iter->cur_method = 0;
151 int bootmeth_set_order(const char *order_str)
153 struct bootstd_priv *std;
154 struct udevice **order;
155 int count, ret, i, len;
158 ret = bootstd_get_priv(&std);
163 free(std->bootmeth_order);
164 std->bootmeth_order = NULL;
165 std->bootmeth_count = 0;
169 /* Create an array large enough */
170 count = uclass_id_count(UCLASS_BOOTMETH);
172 return log_msg_ret("count", -ENOENT);
174 order = calloc(count + 1, sizeof(struct udevice *));
176 return log_msg_ret("order", -ENOMEM);
178 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
181 p = strchrnul(s, ' ');
183 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
186 printf("Unknown bootmeth '%.*s'\n", len, s);
193 free(std->bootmeth_order);
194 std->bootmeth_order = order;
195 std->bootmeth_count = i;
201 * setup_fs() - Set up read to read a file
203 * We must redo the setup before each filesystem operation. This function
204 * handles that, including setting the filesystem type if a block device is not
207 * @bflow: Information about file to try
208 * @desc: Block descriptor to read from (NULL if not a block device)
209 * Return: 0 if OK, -ve on error
211 static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
216 ret = fs_set_blk_dev_with_part(desc, bflow->part);
218 return log_msg_ret("set", ret);
219 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
220 fs_set_type(bflow->fs_type);
226 int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
227 const char *prefix, const char *fname)
233 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
234 log_debug("trying: %s\n", path);
237 bflow->fname = strdup(path);
239 return log_msg_ret("name", -ENOMEM);
241 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
242 fs_set_type(bflow->fs_type);
244 ret = fs_size(path, &size);
245 log_debug(" %s - err=%d\n", path, ret);
247 /* Sadly FS closes the file after fs_size() so we must redo this */
248 ret2 = setup_fs(bflow, desc);
250 return log_msg_ret("fs", ret2);
253 return log_msg_ret("size", ret);
256 bflow->state = BOOTFLOWST_FILE;
261 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
270 log_debug(" - script file size %x\n", size);
271 if (size > size_limit)
272 return log_msg_ret("chk", -E2BIG);
274 buf = memalign(align, size + 1);
276 return log_msg_ret("buf", -ENOMEM);
277 addr = map_to_sysmem(buf);
279 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
282 return log_msg_ret("read", ret);
284 if (size != bytes_read)
285 return log_msg_ret("bread", -EINVAL);
287 bflow->state = BOOTFLOWST_READY;
293 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
294 const char *file_path, ulong addr, ulong *sizep)
296 struct blk_desc *desc = NULL;
302 desc = dev_get_uclass_plat(bflow->blk);
304 ret = setup_fs(bflow, desc);
306 return log_msg_ret("fs", ret);
308 ret = fs_size(file_path, &size);
310 return log_msg_ret("size", ret);
312 return log_msg_ret("spc", -ENOSPC);
314 ret = setup_fs(bflow, desc);
316 return log_msg_ret("fs", ret);
318 ret = fs_read(file_path, addr, 0, 0, &len_read);
326 #ifdef CONFIG_BOOTSTD_FULL
328 * on_bootmeths() - Update the bootmeth order
330 * This will check for a valid baudrate and only apply it if valid.
332 static int on_bootmeths(const char *name, const char *value, enum env_op op,
339 case env_op_overwrite:
340 ret = bootmeth_set_order(value);
345 bootmeth_set_order(NULL);
351 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
352 #endif /* CONFIG_BOOTSTD_FULL */
354 UCLASS_DRIVER(bootmeth) = {
355 .id = UCLASS_BOOTMETH,
357 .flags = DM_UC_FLAG_SEQ_ALIAS,
358 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),