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 bootflow_init(bflow, NULL, dev);
82 return ops->read_bootflow(dev, bflow);
85 int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
87 struct bootstd_priv *std;
88 struct udevice **order;
92 ret = bootstd_get_priv(&std);
96 /* Create an array large enough */
97 count = std->bootmeth_count ? std->bootmeth_count :
98 uclass_id_count(UCLASS_BOOTMETH);
100 return log_msg_ret("count", -ENOENT);
102 order = calloc(count, sizeof(struct udevice *));
104 return log_msg_ret("order", -ENOMEM);
106 /* If we have an ordering, copy it */
107 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
111 * We don't support skipping global bootmeths. Instead, the user
112 * should omit them from the ordering
115 return log_msg_ret("glob", -EPERM);
116 memcpy(order, std->bootmeth_order,
117 count * sizeof(struct bootmeth *));
119 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
120 for (i = 0; i < count; i++) {
121 struct udevice *dev = order[i];
122 struct bootmeth_uc_plat *ucp;
125 ucp = dev_get_uclass_plat(dev);
126 is_global = ucp->flags &
129 iter->first_glob_method = i;
139 * Do two passes, one to find the normal bootmeths and another
140 * to find the global ones, if required, The global ones go at
143 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
145 iter->first_glob_method = upto;
147 * Get a list of bootmethods, in seq order (i.e. using
148 * aliases). There may be gaps so try to count up high
149 * enough to find them all.
151 for (i = 0; upto < count && i < 20 + count * 2; i++) {
152 struct bootmeth_uc_plat *ucp;
155 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
159 ucp = dev_get_uclass_plat(dev);
161 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
162 (ucp->flags & BOOTMETHF_GLOBAL);
163 if (pass ? is_global : !is_global)
170 return log_msg_ret("count2", -ENOENT);
172 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
173 iter->first_glob_method != -1 && iter->first_glob_method != count) {
174 iter->cur_method = iter->first_glob_method;
175 iter->doing_global = true;
177 iter->method_order = order;
178 iter->num_methods = count;
183 int bootmeth_set_order(const char *order_str)
185 struct bootstd_priv *std;
186 struct udevice **order;
187 int count, ret, i, len;
190 ret = bootstd_get_priv(&std);
195 free(std->bootmeth_order);
196 std->bootmeth_order = NULL;
197 std->bootmeth_count = 0;
201 /* Create an array large enough */
202 count = uclass_id_count(UCLASS_BOOTMETH);
204 return log_msg_ret("count", -ENOENT);
206 order = calloc(count + 1, sizeof(struct udevice *));
208 return log_msg_ret("order", -ENOMEM);
210 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
213 p = strchrnul(s, ' ');
215 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
218 printf("Unknown bootmeth '%.*s'\n", len, s);
225 free(std->bootmeth_order);
226 std->bootmeth_order = order;
227 std->bootmeth_count = i;
233 * setup_fs() - Set up read to read a file
235 * We must redo the setup before each filesystem operation. This function
236 * handles that, including setting the filesystem type if a block device is not
239 * @bflow: Information about file to try
240 * @desc: Block descriptor to read from (NULL if not a block device)
241 * Return: 0 if OK, -ve on error
243 static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
248 ret = fs_set_blk_dev_with_part(desc, bflow->part);
250 return log_msg_ret("set", ret);
251 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
252 fs_set_type(bflow->fs_type);
258 int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
259 const char *prefix, const char *fname)
265 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
266 log_debug("trying: %s\n", path);
269 bflow->fname = strdup(path);
271 return log_msg_ret("name", -ENOMEM);
273 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
274 fs_set_type(bflow->fs_type);
276 ret = fs_size(path, &size);
277 log_debug(" %s - err=%d\n", path, ret);
279 /* Sadly FS closes the file after fs_size() so we must redo this */
280 ret2 = setup_fs(bflow, desc);
282 return log_msg_ret("fs", ret2);
285 return log_msg_ret("size", ret);
288 bflow->state = BOOTFLOWST_FILE;
293 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
302 log_debug(" - script file size %x\n", size);
303 if (size > size_limit)
304 return log_msg_ret("chk", -E2BIG);
306 buf = memalign(align, size + 1);
308 return log_msg_ret("buf", -ENOMEM);
309 addr = map_to_sysmem(buf);
311 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
314 return log_msg_ret("read", ret);
316 if (size != bytes_read)
317 return log_msg_ret("bread", -EINVAL);
319 bflow->state = BOOTFLOWST_READY;
325 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
326 const char *file_path, ulong addr, ulong *sizep)
328 struct blk_desc *desc = NULL;
334 desc = dev_get_uclass_plat(bflow->blk);
336 ret = setup_fs(bflow, desc);
338 return log_msg_ret("fs", ret);
340 ret = fs_size(file_path, &size);
342 return log_msg_ret("size", ret);
344 return log_msg_ret("spc", -ENOSPC);
346 ret = setup_fs(bflow, desc);
348 return log_msg_ret("fs", ret);
350 ret = fs_read(file_path, addr, 0, 0, &len_read);
358 #ifdef CONFIG_BOOTSTD_FULL
360 * on_bootmeths() - Update the bootmeth order
362 * This will check for a valid baudrate and only apply it if valid.
364 static int on_bootmeths(const char *name, const char *value, enum env_op op,
371 case env_op_overwrite:
372 ret = bootmeth_set_order(value);
377 bootmeth_set_order(NULL);
383 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
384 #endif /* CONFIG_BOOTSTD_FULL */
386 UCLASS_DRIVER(bootmeth) = {
387 .id = UCLASS_BOOTMETH,
389 .flags = DM_UC_FLAG_SEQ_ALIAS,
390 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),