blob: c040d5f92b2d2d47bf5808da023523fd8f572ff5 [file] [log] [blame]
Simon Glassa950d312022-04-24 23:31:08 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#define LOG_CATEGORY UCLASS_BOOTSTD
8
9#include <common.h>
10#include <blk.h>
11#include <bootflow.h>
12#include <bootmeth.h>
13#include <bootstd.h>
14#include <dm.h>
15#include <env_internal.h>
16#include <fs.h>
17#include <malloc.h>
18#include <mapmem.h>
19#include <dm/uclass-internal.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
24{
25 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
26
27 if (!ops->check)
28 return 0;
29
30 return ops->check(dev, iter);
31}
32
33int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
34{
35 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
36
37 if (!ops->read_bootflow)
38 return -ENOSYS;
39
40 return ops->read_bootflow(dev, bflow);
41}
42
43int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
44{
45 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
46
47 if (!ops->boot)
48 return -ENOSYS;
49
50 return ops->boot(dev, bflow);
51}
52
53int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
54 const char *file_path, ulong addr, ulong *sizep)
55{
56 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
57
58 if (!ops->read_file)
59 return -ENOSYS;
60
61 return ops->read_file(dev, bflow, file_path, addr, sizep);
62}
63
64/**
65 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
66 *
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
70 *
71 * @iter: Iterator to update with the order
72 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
73 * on other error
74 */
75int bootmeth_setup_iter_order(struct bootflow_iter *iter)
76{
77 struct bootstd_priv *std;
78 struct udevice **order;
79 int count;
80 int ret;
81
82 ret = bootstd_get_priv(&std);
83 if (ret)
84 return ret;
85
86 /* Create an array large enough */
87 count = std->bootmeth_count ? std->bootmeth_count :
88 uclass_id_count(UCLASS_BOOTMETH);
89 if (!count)
90 return log_msg_ret("count", -ENOENT);
91
92 order = calloc(count, sizeof(struct udevice *));
93 if (!order)
94 return log_msg_ret("order", -ENOMEM);
95
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 *));
100 } else {
101 struct udevice *dev;
102 int i, upto;
103
104 /*
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
107 * all.
108 */
109 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
110 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
111 &dev);
112 if (!ret)
113 order[upto++] = dev;
114 }
115 count = upto;
116 }
117
118 iter->method_order = order;
119 iter->num_methods = count;
120 iter->cur_method = 0;
121
122 return 0;
123}
124
125int bootmeth_set_order(const char *order_str)
126{
127 struct bootstd_priv *std;
128 struct udevice **order;
129 int count, ret, i, len;
130 const char *s, *p;
131
132 ret = bootstd_get_priv(&std);
133 if (ret)
134 return ret;
135
136 if (!order_str) {
137 free(std->bootmeth_order);
138 std->bootmeth_order = NULL;
139 std->bootmeth_count = 0;
140 return 0;
141 }
142
143 /* Create an array large enough */
144 count = uclass_id_count(UCLASS_BOOTMETH);
145 if (!count)
146 return log_msg_ret("count", -ENOENT);
147
148 order = calloc(count + 1, sizeof(struct udevice *));
149 if (!order)
150 return log_msg_ret("order", -ENOMEM);
151
152 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
153 struct udevice *dev;
154
155 p = strchrnul(s, ' ');
156 len = p - s;
157 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
158 &dev);
159 if (ret) {
160 printf("Unknown bootmeth '%.*s'\n", len, s);
161 free(order);
162 return ret;
163 }
164 order[i] = dev;
165 }
166 order[i] = NULL;
167 free(std->bootmeth_order);
168 std->bootmeth_order = order;
169 std->bootmeth_count = i;
170
171 return 0;
172}
173
174/**
175 * setup_fs() - Set up read to read a file
176 *
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
179 * being used
180 *
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
184 */
185static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
186{
187 int ret;
188
189 if (desc) {
190 ret = fs_set_blk_dev_with_part(desc, bflow->part);
191 if (ret)
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);
195 }
196
197 return 0;
198}
199
200int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
201 const char *prefix, const char *fname)
202{
203 char path[200];
204 loff_t size;
205 int ret, ret2;
206
207 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
208 log_debug("trying: %s\n", path);
209
210 free(bflow->fname);
211 bflow->fname = strdup(path);
212 if (!bflow->fname)
213 return log_msg_ret("name", -ENOMEM);
214
215 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
216 fs_set_type(bflow->fs_type);
217
218 ret = fs_size(path, &size);
219 log_debug(" %s - err=%d\n", path, ret);
220
221 /* Sadly FS closes the file after fs_size() so we must redo this */
222 ret2 = setup_fs(bflow, desc);
223 if (ret2)
224 return log_msg_ret("fs", ret2);
225
226 if (ret)
227 return log_msg_ret("size", ret);
228
229 bflow->size = size;
230 bflow->state = BOOTFLOWST_FILE;
231
232 return 0;
233}
234
235int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
236{
237 loff_t bytes_read;
238 ulong addr;
239 char *buf;
240 uint size;
241 int ret;
242
243 size = bflow->size;
244 log_debug(" - script file size %x\n", size);
245 if (size > size_limit)
246 return log_msg_ret("chk", -E2BIG);
247
248 buf = memalign(align, size + 1);
249 if (!buf)
250 return log_msg_ret("buf", -ENOMEM);
251 addr = map_to_sysmem(buf);
252
253 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
254 if (ret) {
255 free(buf);
256 return log_msg_ret("read", ret);
257 }
258 if (size != bytes_read)
259 return log_msg_ret("bread", -EINVAL);
260 buf[size] = '\0';
261 bflow->state = BOOTFLOWST_READY;
262 bflow->buf = buf;
263
264 return 0;
265}
266
267int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
268 const char *file_path, ulong addr, ulong *sizep)
269{
270 struct blk_desc *desc = NULL;
271 loff_t len_read;
272 loff_t size;
273 int ret;
274
275 if (bflow->blk)
276 desc = dev_get_uclass_plat(bflow->blk);
277
278 ret = setup_fs(bflow, desc);
279 if (ret)
280 return log_msg_ret("fs", ret);
281
282 ret = fs_size(file_path, &size);
283 if (ret)
284 return log_msg_ret("size", ret);
285 if (size > *sizep)
286 return log_msg_ret("spc", -ENOSPC);
287
288 ret = setup_fs(bflow, desc);
289 if (ret)
290 return log_msg_ret("fs", ret);
291
292 ret = fs_read(file_path, addr, 0, 0, &len_read);
293 if (ret)
294 return ret;
295 *sizep = len_read;
296
297 return 0;
298}
299
300#ifdef CONFIG_BOOTSTD_FULL
301/**
302 * on_bootmeths() - Update the bootmeth order
303 *
304 * This will check for a valid baudrate and only apply it if valid.
305 */
306static int on_bootmeths(const char *name, const char *value, enum env_op op,
307 int flags)
308{
309 int ret;
310
311 switch (op) {
312 case env_op_create:
313 case env_op_overwrite:
314 ret = bootmeth_set_order(value);
315 if (ret)
316 return 1;
317 return 0;
318 case env_op_delete:
319 bootmeth_set_order(NULL);
320 fallthrough;
321 default:
322 return 0;
323 }
324}
325U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
326#endif /* CONFIG_BOOTSTD_FULL */
327
328UCLASS_DRIVER(bootmeth) = {
329 .id = UCLASS_BOOTMETH,
330 .name = "bootmeth",
331 .flags = DM_UC_FLAG_SEQ_ALIAS,
332 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
333};