blob: 1e276c0f26bbfc9090a9bc759f26d9e59e1876e1 [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
Simon Glass988caca2022-07-30 15:52:19 -060023int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize)
24{
25 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
26
27 if (!ops->get_state_desc)
28 return -ENOSYS;
29
30 return ops->get_state_desc(dev, buf, maxsize);
31}
32
Simon Glassa950d312022-04-24 23:31:08 -060033int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
34{
35 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
36
37 if (!ops->check)
38 return 0;
39
40 return ops->check(dev, iter);
41}
42
43int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
44{
45 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
46
47 if (!ops->read_bootflow)
48 return -ENOSYS;
49
50 return ops->read_bootflow(dev, bflow);
51}
52
53int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
54{
55 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
56
57 if (!ops->boot)
58 return -ENOSYS;
59
60 return ops->boot(dev, bflow);
61}
62
63int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
64 const char *file_path, ulong addr, ulong *sizep)
65{
66 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
67
68 if (!ops->read_file)
69 return -ENOSYS;
70
71 return ops->read_file(dev, bflow, file_path, addr, sizep);
72}
73
74/**
75 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
76 *
77 * This sets up the ordering information in @iter, based on the selected
78 * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
79 * ordering there, then all bootmethods are added
80 *
81 * @iter: Iterator to update with the order
82 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
83 * on other error
84 */
85int bootmeth_setup_iter_order(struct bootflow_iter *iter)
86{
87 struct bootstd_priv *std;
88 struct udevice **order;
89 int count;
90 int ret;
91
92 ret = bootstd_get_priv(&std);
93 if (ret)
94 return ret;
95
96 /* Create an array large enough */
97 count = std->bootmeth_count ? std->bootmeth_count :
98 uclass_id_count(UCLASS_BOOTMETH);
99 if (!count)
100 return log_msg_ret("count", -ENOENT);
101
102 order = calloc(count, sizeof(struct udevice *));
103 if (!order)
104 return log_msg_ret("order", -ENOMEM);
105
106 /* If we have an ordering, copy it */
107 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
108 memcpy(order, std->bootmeth_order,
109 count * sizeof(struct bootmeth *));
110 } else {
111 struct udevice *dev;
112 int i, upto;
113
114 /*
115 * Get a list of bootmethods, in seq order (i.e. using aliases).
116 * There may be gaps so try to count up high enough to find them
117 * all.
118 */
119 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
120 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
121 &dev);
122 if (!ret)
123 order[upto++] = dev;
124 }
125 count = upto;
126 }
Simon Glass10d16fa2022-07-30 15:52:18 -0600127 if (!count)
128 return log_msg_ret("count2", -ENOENT);
Simon Glassa950d312022-04-24 23:31:08 -0600129
130 iter->method_order = order;
131 iter->num_methods = count;
132 iter->cur_method = 0;
133
134 return 0;
135}
136
137int bootmeth_set_order(const char *order_str)
138{
139 struct bootstd_priv *std;
140 struct udevice **order;
141 int count, ret, i, len;
142 const char *s, *p;
143
144 ret = bootstd_get_priv(&std);
145 if (ret)
146 return ret;
147
148 if (!order_str) {
149 free(std->bootmeth_order);
150 std->bootmeth_order = NULL;
151 std->bootmeth_count = 0;
152 return 0;
153 }
154
155 /* Create an array large enough */
156 count = uclass_id_count(UCLASS_BOOTMETH);
157 if (!count)
158 return log_msg_ret("count", -ENOENT);
159
160 order = calloc(count + 1, sizeof(struct udevice *));
161 if (!order)
162 return log_msg_ret("order", -ENOMEM);
163
164 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
165 struct udevice *dev;
166
167 p = strchrnul(s, ' ');
168 len = p - s;
169 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
170 &dev);
171 if (ret) {
172 printf("Unknown bootmeth '%.*s'\n", len, s);
173 free(order);
174 return ret;
175 }
176 order[i] = dev;
177 }
178 order[i] = NULL;
179 free(std->bootmeth_order);
180 std->bootmeth_order = order;
181 std->bootmeth_count = i;
182
183 return 0;
184}
185
186/**
187 * setup_fs() - Set up read to read a file
188 *
189 * We must redo the setup before each filesystem operation. This function
190 * handles that, including setting the filesystem type if a block device is not
191 * being used
192 *
193 * @bflow: Information about file to try
194 * @desc: Block descriptor to read from (NULL if not a block device)
195 * Return: 0 if OK, -ve on error
196 */
197static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
198{
199 int ret;
200
201 if (desc) {
202 ret = fs_set_blk_dev_with_part(desc, bflow->part);
203 if (ret)
204 return log_msg_ret("set", ret);
205 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
206 fs_set_type(bflow->fs_type);
207 }
208
209 return 0;
210}
211
212int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
213 const char *prefix, const char *fname)
214{
215 char path[200];
216 loff_t size;
217 int ret, ret2;
218
219 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
220 log_debug("trying: %s\n", path);
221
222 free(bflow->fname);
223 bflow->fname = strdup(path);
224 if (!bflow->fname)
225 return log_msg_ret("name", -ENOMEM);
226
227 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
228 fs_set_type(bflow->fs_type);
229
230 ret = fs_size(path, &size);
231 log_debug(" %s - err=%d\n", path, ret);
232
233 /* Sadly FS closes the file after fs_size() so we must redo this */
234 ret2 = setup_fs(bflow, desc);
235 if (ret2)
236 return log_msg_ret("fs", ret2);
237
238 if (ret)
239 return log_msg_ret("size", ret);
240
241 bflow->size = size;
242 bflow->state = BOOTFLOWST_FILE;
243
244 return 0;
245}
246
247int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
248{
249 loff_t bytes_read;
250 ulong addr;
251 char *buf;
252 uint size;
253 int ret;
254
255 size = bflow->size;
256 log_debug(" - script file size %x\n", size);
257 if (size > size_limit)
258 return log_msg_ret("chk", -E2BIG);
259
260 buf = memalign(align, size + 1);
261 if (!buf)
262 return log_msg_ret("buf", -ENOMEM);
263 addr = map_to_sysmem(buf);
264
265 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
266 if (ret) {
267 free(buf);
268 return log_msg_ret("read", ret);
269 }
270 if (size != bytes_read)
271 return log_msg_ret("bread", -EINVAL);
272 buf[size] = '\0';
273 bflow->state = BOOTFLOWST_READY;
274 bflow->buf = buf;
275
276 return 0;
277}
278
279int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
280 const char *file_path, ulong addr, ulong *sizep)
281{
282 struct blk_desc *desc = NULL;
283 loff_t len_read;
284 loff_t size;
285 int ret;
286
287 if (bflow->blk)
288 desc = dev_get_uclass_plat(bflow->blk);
289
290 ret = setup_fs(bflow, desc);
291 if (ret)
292 return log_msg_ret("fs", ret);
293
294 ret = fs_size(file_path, &size);
295 if (ret)
296 return log_msg_ret("size", ret);
297 if (size > *sizep)
298 return log_msg_ret("spc", -ENOSPC);
299
300 ret = setup_fs(bflow, desc);
301 if (ret)
302 return log_msg_ret("fs", ret);
303
304 ret = fs_read(file_path, addr, 0, 0, &len_read);
305 if (ret)
306 return ret;
307 *sizep = len_read;
308
309 return 0;
310}
311
312#ifdef CONFIG_BOOTSTD_FULL
313/**
314 * on_bootmeths() - Update the bootmeth order
315 *
316 * This will check for a valid baudrate and only apply it if valid.
317 */
318static int on_bootmeths(const char *name, const char *value, enum env_op op,
319 int flags)
320{
321 int ret;
322
323 switch (op) {
324 case env_op_create:
325 case env_op_overwrite:
326 ret = bootmeth_set_order(value);
327 if (ret)
328 return 1;
329 return 0;
330 case env_op_delete:
331 bootmeth_set_order(NULL);
332 fallthrough;
333 default:
334 return 0;
335 }
336}
337U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
338#endif /* CONFIG_BOOTSTD_FULL */
339
340UCLASS_DRIVER(bootmeth) = {
341 .id = UCLASS_BOOTMETH,
342 .name = "bootmeth",
343 .flags = DM_UC_FLAG_SEQ_ALIAS,
344 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
345};