blob: 88bbb32c47fbe19b015bcfd56d725f4cd6bd5313 [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
Simon Glassbc06aa02022-07-30 15:52:21 -060074int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
75{
76 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
77
78 if (!ops->read_bootflow)
79 return -ENOSYS;
80 memset(bflow, '\0', sizeof(*bflow));
81 bflow->dev = NULL;
82 bflow->method = dev;
83 bflow->state = BOOTFLOWST_BASE;
84
85 return ops->read_bootflow(dev, bflow);
86}
87
Simon Glassa950d312022-04-24 23:31:08 -060088/**
89 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
90 *
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
94 *
95 * @iter: Iterator to update with the order
96 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
97 * on other error
98 */
99int bootmeth_setup_iter_order(struct bootflow_iter *iter)
100{
101 struct bootstd_priv *std;
102 struct udevice **order;
103 int count;
104 int ret;
105
106 ret = bootstd_get_priv(&std);
107 if (ret)
108 return ret;
109
110 /* Create an array large enough */
111 count = std->bootmeth_count ? std->bootmeth_count :
112 uclass_id_count(UCLASS_BOOTMETH);
113 if (!count)
114 return log_msg_ret("count", -ENOENT);
115
116 order = calloc(count, sizeof(struct udevice *));
117 if (!order)
118 return log_msg_ret("order", -ENOMEM);
119
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 *));
124 } else {
125 struct udevice *dev;
126 int i, upto;
127
128 /*
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
131 * all.
132 */
133 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
134 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
135 &dev);
136 if (!ret)
137 order[upto++] = dev;
138 }
139 count = upto;
140 }
Simon Glass10d16fa2022-07-30 15:52:18 -0600141 if (!count)
142 return log_msg_ret("count2", -ENOENT);
Simon Glassa950d312022-04-24 23:31:08 -0600143
144 iter->method_order = order;
145 iter->num_methods = count;
146 iter->cur_method = 0;
147
148 return 0;
149}
150
151int bootmeth_set_order(const char *order_str)
152{
153 struct bootstd_priv *std;
154 struct udevice **order;
155 int count, ret, i, len;
156 const char *s, *p;
157
158 ret = bootstd_get_priv(&std);
159 if (ret)
160 return ret;
161
162 if (!order_str) {
163 free(std->bootmeth_order);
164 std->bootmeth_order = NULL;
165 std->bootmeth_count = 0;
166 return 0;
167 }
168
169 /* Create an array large enough */
170 count = uclass_id_count(UCLASS_BOOTMETH);
171 if (!count)
172 return log_msg_ret("count", -ENOENT);
173
174 order = calloc(count + 1, sizeof(struct udevice *));
175 if (!order)
176 return log_msg_ret("order", -ENOMEM);
177
178 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
179 struct udevice *dev;
180
181 p = strchrnul(s, ' ');
182 len = p - s;
183 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
184 &dev);
185 if (ret) {
186 printf("Unknown bootmeth '%.*s'\n", len, s);
187 free(order);
188 return ret;
189 }
190 order[i] = dev;
191 }
192 order[i] = NULL;
193 free(std->bootmeth_order);
194 std->bootmeth_order = order;
195 std->bootmeth_count = i;
196
197 return 0;
198}
199
200/**
201 * setup_fs() - Set up read to read a file
202 *
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
205 * being used
206 *
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
210 */
211static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
212{
213 int ret;
214
215 if (desc) {
216 ret = fs_set_blk_dev_with_part(desc, bflow->part);
217 if (ret)
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);
221 }
222
223 return 0;
224}
225
226int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
227 const char *prefix, const char *fname)
228{
229 char path[200];
230 loff_t size;
231 int ret, ret2;
232
233 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
234 log_debug("trying: %s\n", path);
235
236 free(bflow->fname);
237 bflow->fname = strdup(path);
238 if (!bflow->fname)
239 return log_msg_ret("name", -ENOMEM);
240
241 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
242 fs_set_type(bflow->fs_type);
243
244 ret = fs_size(path, &size);
245 log_debug(" %s - err=%d\n", path, ret);
246
247 /* Sadly FS closes the file after fs_size() so we must redo this */
248 ret2 = setup_fs(bflow, desc);
249 if (ret2)
250 return log_msg_ret("fs", ret2);
251
252 if (ret)
253 return log_msg_ret("size", ret);
254
255 bflow->size = size;
256 bflow->state = BOOTFLOWST_FILE;
257
258 return 0;
259}
260
261int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
262{
263 loff_t bytes_read;
264 ulong addr;
265 char *buf;
266 uint size;
267 int ret;
268
269 size = bflow->size;
270 log_debug(" - script file size %x\n", size);
271 if (size > size_limit)
272 return log_msg_ret("chk", -E2BIG);
273
274 buf = memalign(align, size + 1);
275 if (!buf)
276 return log_msg_ret("buf", -ENOMEM);
277 addr = map_to_sysmem(buf);
278
279 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
280 if (ret) {
281 free(buf);
282 return log_msg_ret("read", ret);
283 }
284 if (size != bytes_read)
285 return log_msg_ret("bread", -EINVAL);
286 buf[size] = '\0';
287 bflow->state = BOOTFLOWST_READY;
288 bflow->buf = buf;
289
290 return 0;
291}
292
293int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
294 const char *file_path, ulong addr, ulong *sizep)
295{
296 struct blk_desc *desc = NULL;
297 loff_t len_read;
298 loff_t size;
299 int ret;
300
301 if (bflow->blk)
302 desc = dev_get_uclass_plat(bflow->blk);
303
304 ret = setup_fs(bflow, desc);
305 if (ret)
306 return log_msg_ret("fs", ret);
307
308 ret = fs_size(file_path, &size);
309 if (ret)
310 return log_msg_ret("size", ret);
311 if (size > *sizep)
312 return log_msg_ret("spc", -ENOSPC);
313
314 ret = setup_fs(bflow, desc);
315 if (ret)
316 return log_msg_ret("fs", ret);
317
318 ret = fs_read(file_path, addr, 0, 0, &len_read);
319 if (ret)
320 return ret;
321 *sizep = len_read;
322
323 return 0;
324}
325
326#ifdef CONFIG_BOOTSTD_FULL
327/**
328 * on_bootmeths() - Update the bootmeth order
329 *
330 * This will check for a valid baudrate and only apply it if valid.
331 */
332static int on_bootmeths(const char *name, const char *value, enum env_op op,
333 int flags)
334{
335 int ret;
336
337 switch (op) {
338 case env_op_create:
339 case env_op_overwrite:
340 ret = bootmeth_set_order(value);
341 if (ret)
342 return 1;
343 return 0;
344 case env_op_delete:
345 bootmeth_set_order(NULL);
346 fallthrough;
347 default:
348 return 0;
349 }
350}
351U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
352#endif /* CONFIG_BOOTSTD_FULL */
353
354UCLASS_DRIVER(bootmeth) = {
355 .id = UCLASS_BOOTMETH,
356 .name = "bootmeth",
357 .flags = DM_UC_FLAG_SEQ_ALIAS,
358 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
359};