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_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);
88 int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
90 struct bootstd_priv *std;
91 struct udevice **order;
95 ret = bootstd_get_priv(&std);
99 /* Create an array large enough */
100 count = std->bootmeth_count ? std->bootmeth_count :
101 uclass_id_count(UCLASS_BOOTMETH);
103 return log_msg_ret("count", -ENOENT);
105 order = calloc(count, sizeof(struct udevice *));
107 return log_msg_ret("order", -ENOMEM);
109 /* If we have an ordering, copy it */
110 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
114 * We don't support skipping global bootmeths. Instead, the user
115 * should omit them from the ordering
118 return log_msg_ret("glob", -EPERM);
119 memcpy(order, std->bootmeth_order,
120 count * sizeof(struct bootmeth *));
122 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
123 for (i = 0; i < count; i++) {
124 struct udevice *dev = order[i];
125 struct bootmeth_uc_plat *ucp;
128 ucp = dev_get_uclass_plat(dev);
129 is_global = ucp->flags &
132 iter->first_glob_method = i;
142 * Do two passes, one to find the normal bootmeths and another
143 * to find the global ones, if required, The global ones go at
146 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
148 iter->first_glob_method = upto;
150 * Get a list of bootmethods, in seq order (i.e. using
151 * aliases). There may be gaps so try to count up high
152 * enough to find them all.
154 for (i = 0; upto < count && i < 20 + count * 2; i++) {
155 struct bootmeth_uc_plat *ucp;
158 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
162 ucp = dev_get_uclass_plat(dev);
164 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
165 (ucp->flags & BOOTMETHF_GLOBAL);
166 if (pass ? is_global : !is_global)
173 return log_msg_ret("count2", -ENOENT);
175 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
176 iter->first_glob_method != -1 && iter->first_glob_method != count) {
177 iter->cur_method = iter->first_glob_method;
178 iter->doing_global = true;
180 iter->method_order = order;
181 iter->num_methods = count;
186 int bootmeth_set_order(const char *order_str)
188 struct bootstd_priv *std;
189 struct udevice **order;
190 int count, ret, i, len;
193 ret = bootstd_get_priv(&std);
198 free(std->bootmeth_order);
199 std->bootmeth_order = NULL;
200 std->bootmeth_count = 0;
204 /* Create an array large enough */
205 count = uclass_id_count(UCLASS_BOOTMETH);
207 return log_msg_ret("count", -ENOENT);
209 order = calloc(count + 1, sizeof(struct udevice *));
211 return log_msg_ret("order", -ENOMEM);
213 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
216 p = strchrnul(s, ' ');
218 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
221 printf("Unknown bootmeth '%.*s'\n", len, s);
228 free(std->bootmeth_order);
229 std->bootmeth_order = order;
230 std->bootmeth_count = i;
236 * setup_fs() - Set up read to read a file
238 * We must redo the setup before each filesystem operation. This function
239 * handles that, including setting the filesystem type if a block device is not
242 * @bflow: Information about file to try
243 * @desc: Block descriptor to read from (NULL if not a block device)
244 * Return: 0 if OK, -ve on error
246 static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
251 ret = fs_set_blk_dev_with_part(desc, bflow->part);
253 return log_msg_ret("set", ret);
254 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
255 fs_set_type(bflow->fs_type);
261 int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
262 const char *prefix, const char *fname)
268 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
269 log_debug("trying: %s\n", path);
272 bflow->fname = strdup(path);
274 return log_msg_ret("name", -ENOMEM);
276 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
277 fs_set_type(bflow->fs_type);
279 ret = fs_size(path, &size);
280 log_debug(" %s - err=%d\n", path, ret);
282 /* Sadly FS closes the file after fs_size() so we must redo this */
283 ret2 = setup_fs(bflow, desc);
285 return log_msg_ret("fs", ret2);
288 return log_msg_ret("size", ret);
291 bflow->state = BOOTFLOWST_FILE;
296 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
305 log_debug(" - script file size %x\n", size);
306 if (size > size_limit)
307 return log_msg_ret("chk", -E2BIG);
309 buf = memalign(align, size + 1);
311 return log_msg_ret("buf", -ENOMEM);
312 addr = map_to_sysmem(buf);
314 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
317 return log_msg_ret("read", ret);
319 if (size != bytes_read)
320 return log_msg_ret("bread", -EINVAL);
322 bflow->state = BOOTFLOWST_READY;
328 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
329 const char *file_path, ulong addr, ulong *sizep)
331 struct blk_desc *desc = NULL;
337 desc = dev_get_uclass_plat(bflow->blk);
339 ret = setup_fs(bflow, desc);
341 return log_msg_ret("fs", ret);
343 ret = fs_size(file_path, &size);
345 return log_msg_ret("size", ret);
347 return log_msg_ret("spc", -ENOSPC);
349 ret = setup_fs(bflow, desc);
351 return log_msg_ret("fs", ret);
353 ret = fs_read(file_path, addr, 0, 0, &len_read);
361 #ifdef CONFIG_BOOTSTD_FULL
363 * on_bootmeths() - Update the bootmeth order
365 * This will check for a valid baudrate and only apply it if valid.
367 static int on_bootmeths(const char *name, const char *value, enum env_op op,
374 case env_op_overwrite:
375 ret = bootmeth_set_order(value);
380 bootmeth_set_order(NULL);
386 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
387 #endif /* CONFIG_BOOTSTD_FULL */
389 UCLASS_DRIVER(bootmeth) = {
390 .id = UCLASS_BOOTMETH,
392 .flags = DM_UC_FLAG_SEQ_ALIAS,
393 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),