blob: 3b3e0614dafa01de3149e62aefde2feb43db6f8a [file] [log] [blame]
Simon Glass4b508b82022-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 Glassf6d71a82022-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 Glass4b508b82022-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
Simon Glassa63bda62023-01-17 10:48:01 -070053int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
54 char *buf, int size)
55{
56 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
57
58 if (!ops->set_bootflow)
59 return -ENOSYS;
60
61 return ops->set_bootflow(dev, bflow, buf, size);
62}
63
Simon Glass4b508b82022-04-24 23:31:08 -060064int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
65{
66 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
67
68 if (!ops->boot)
69 return -ENOSYS;
70
71 return ops->boot(dev, bflow);
72}
73
74int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
75 const char *file_path, ulong addr, ulong *sizep)
76{
77 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
78
79 if (!ops->read_file)
80 return -ENOSYS;
81
82 return ops->read_file(dev, bflow, file_path, addr, sizep);
83}
84
Simon Glass4f8633d2022-07-30 15:52:21 -060085int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
86{
87 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
88
89 if (!ops->read_bootflow)
90 return -ENOSYS;
Simon Glass00501fc2022-10-20 18:22:51 -060091 bootflow_init(bflow, NULL, dev);
Simon Glass4f8633d2022-07-30 15:52:21 -060092
93 return ops->read_bootflow(dev, bflow);
94}
95
Simon Glasscc15e142022-07-30 15:52:27 -060096int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
Simon Glass4b508b82022-04-24 23:31:08 -060097{
98 struct bootstd_priv *std;
99 struct udevice **order;
100 int count;
101 int ret;
102
103 ret = bootstd_get_priv(&std);
104 if (ret)
105 return ret;
106
107 /* Create an array large enough */
108 count = std->bootmeth_count ? std->bootmeth_count :
109 uclass_id_count(UCLASS_BOOTMETH);
110 if (!count)
111 return log_msg_ret("count", -ENOENT);
112
113 order = calloc(count, sizeof(struct udevice *));
114 if (!order)
115 return log_msg_ret("order", -ENOMEM);
116
117 /* If we have an ordering, copy it */
118 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
Simon Glasscc15e142022-07-30 15:52:27 -0600119 int i;
120
121 /*
122 * We don't support skipping global bootmeths. Instead, the user
123 * should omit them from the ordering
124 */
125 if (!include_global)
126 return log_msg_ret("glob", -EPERM);
Simon Glass4b508b82022-04-24 23:31:08 -0600127 memcpy(order, std->bootmeth_order,
128 count * sizeof(struct bootmeth *));
Simon Glasscc15e142022-07-30 15:52:27 -0600129
130 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
131 for (i = 0; i < count; i++) {
132 struct udevice *dev = order[i];
133 struct bootmeth_uc_plat *ucp;
134 bool is_global;
135
136 ucp = dev_get_uclass_plat(dev);
137 is_global = ucp->flags &
138 BOOTMETHF_GLOBAL;
139 if (is_global) {
140 iter->first_glob_method = i;
141 break;
142 }
143 }
144 }
Simon Glass4b508b82022-04-24 23:31:08 -0600145 } else {
146 struct udevice *dev;
Simon Glasscc15e142022-07-30 15:52:27 -0600147 int i, upto, pass;
Simon Glass4b508b82022-04-24 23:31:08 -0600148
149 /*
Simon Glasscc15e142022-07-30 15:52:27 -0600150 * Do two passes, one to find the normal bootmeths and another
151 * to find the global ones, if required, The global ones go at
152 * the end.
Simon Glass4b508b82022-04-24 23:31:08 -0600153 */
Simon Glasscc15e142022-07-30 15:52:27 -0600154 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
155 if (pass)
156 iter->first_glob_method = upto;
157 /*
158 * Get a list of bootmethods, in seq order (i.e. using
159 * aliases). There may be gaps so try to count up high
160 * enough to find them all.
161 */
162 for (i = 0; upto < count && i < 20 + count * 2; i++) {
163 struct bootmeth_uc_plat *ucp;
164 bool is_global;
165
166 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
167 i, &dev);
168 if (ret)
169 continue;
170 ucp = dev_get_uclass_plat(dev);
171 is_global =
172 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
173 (ucp->flags & BOOTMETHF_GLOBAL);
174 if (pass ? is_global : !is_global)
175 order[upto++] = dev;
176 }
Simon Glass4b508b82022-04-24 23:31:08 -0600177 }
178 count = upto;
179 }
Simon Glass23e59ec2022-07-30 15:52:18 -0600180 if (!count)
181 return log_msg_ret("count2", -ENOENT);
Simon Glass4b508b82022-04-24 23:31:08 -0600182
Simon Glasscc15e142022-07-30 15:52:27 -0600183 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
184 iter->first_glob_method != -1 && iter->first_glob_method != count) {
185 iter->cur_method = iter->first_glob_method;
186 iter->doing_global = true;
187 }
Simon Glass4b508b82022-04-24 23:31:08 -0600188 iter->method_order = order;
189 iter->num_methods = count;
Simon Glass4b508b82022-04-24 23:31:08 -0600190
191 return 0;
192}
193
194int bootmeth_set_order(const char *order_str)
195{
196 struct bootstd_priv *std;
197 struct udevice **order;
198 int count, ret, i, len;
199 const char *s, *p;
200
201 ret = bootstd_get_priv(&std);
202 if (ret)
203 return ret;
204
205 if (!order_str) {
206 free(std->bootmeth_order);
207 std->bootmeth_order = NULL;
208 std->bootmeth_count = 0;
209 return 0;
210 }
211
212 /* Create an array large enough */
213 count = uclass_id_count(UCLASS_BOOTMETH);
214 if (!count)
215 return log_msg_ret("count", -ENOENT);
216
217 order = calloc(count + 1, sizeof(struct udevice *));
218 if (!order)
219 return log_msg_ret("order", -ENOMEM);
220
221 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
222 struct udevice *dev;
223
224 p = strchrnul(s, ' ');
225 len = p - s;
226 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
227 &dev);
228 if (ret) {
229 printf("Unknown bootmeth '%.*s'\n", len, s);
230 free(order);
231 return ret;
232 }
233 order[i] = dev;
234 }
235 order[i] = NULL;
236 free(std->bootmeth_order);
237 std->bootmeth_order = order;
238 std->bootmeth_count = i;
239
240 return 0;
241}
242
243/**
244 * setup_fs() - Set up read to read a file
245 *
246 * We must redo the setup before each filesystem operation. This function
247 * handles that, including setting the filesystem type if a block device is not
248 * being used
249 *
250 * @bflow: Information about file to try
251 * @desc: Block descriptor to read from (NULL if not a block device)
252 * Return: 0 if OK, -ve on error
253 */
254static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
255{
256 int ret;
257
258 if (desc) {
259 ret = fs_set_blk_dev_with_part(desc, bflow->part);
260 if (ret)
261 return log_msg_ret("set", ret);
262 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
263 fs_set_type(bflow->fs_type);
264 }
265
266 return 0;
267}
268
269int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
270 const char *prefix, const char *fname)
271{
272 char path[200];
273 loff_t size;
274 int ret, ret2;
275
276 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
277 log_debug("trying: %s\n", path);
278
279 free(bflow->fname);
280 bflow->fname = strdup(path);
281 if (!bflow->fname)
282 return log_msg_ret("name", -ENOMEM);
283
284 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
285 fs_set_type(bflow->fs_type);
286
287 ret = fs_size(path, &size);
288 log_debug(" %s - err=%d\n", path, ret);
289
290 /* Sadly FS closes the file after fs_size() so we must redo this */
291 ret2 = setup_fs(bflow, desc);
292 if (ret2)
293 return log_msg_ret("fs", ret2);
294
295 if (ret)
296 return log_msg_ret("size", ret);
297
298 bflow->size = size;
299 bflow->state = BOOTFLOWST_FILE;
300
301 return 0;
302}
303
Simon Glass612b9cc2023-01-06 08:52:34 -0600304static int alloc_file(const char *fname, uint size, void **bufp)
Simon Glass4b508b82022-04-24 23:31:08 -0600305{
306 loff_t bytes_read;
307 ulong addr;
308 char *buf;
Simon Glass4b508b82022-04-24 23:31:08 -0600309 int ret;
310
Simon Glass612b9cc2023-01-06 08:52:34 -0600311 buf = malloc(size + 1);
Simon Glass4b508b82022-04-24 23:31:08 -0600312 if (!buf)
313 return log_msg_ret("buf", -ENOMEM);
314 addr = map_to_sysmem(buf);
315
Simon Glass612b9cc2023-01-06 08:52:34 -0600316 ret = fs_read(fname, addr, 0, size, &bytes_read);
Simon Glass4b508b82022-04-24 23:31:08 -0600317 if (ret) {
318 free(buf);
319 return log_msg_ret("read", ret);
320 }
321 if (size != bytes_read)
Simon Glass9482fdf2023-05-10 16:34:26 -0600322 return log_msg_ret("bread", -EIO);
Simon Glass4b508b82022-04-24 23:31:08 -0600323 buf[size] = '\0';
Simon Glass612b9cc2023-01-06 08:52:34 -0600324
325 *bufp = buf;
326
327 return 0;
328}
329
330int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
331{
332 void *buf;
333 uint size;
334 int ret;
335
336 size = bflow->size;
337 log_debug(" - script file size %x\n", size);
338 if (size > size_limit)
339 return log_msg_ret("chk", -E2BIG);
340
341 ret = alloc_file(bflow->fname, bflow->size, &buf);
342 if (ret)
343 return log_msg_ret("all", ret);
344
Simon Glass4b508b82022-04-24 23:31:08 -0600345 bflow->state = BOOTFLOWST_READY;
346 bflow->buf = buf;
347
348 return 0;
349}
350
Simon Glass612b9cc2023-01-06 08:52:34 -0600351int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
352 void **bufp, uint *sizep)
353{
354 struct blk_desc *desc = NULL;
355 char path[200];
356 loff_t size;
357 void *buf;
358 int ret;
359
360 snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
361 log_debug("trying: %s\n", path);
362
363 if (bflow->blk)
364 desc = dev_get_uclass_plat(bflow->blk);
365
366 ret = setup_fs(bflow, desc);
367 if (ret)
368 return log_msg_ret("fs", ret);
369
370 ret = fs_size(path, &size);
371 log_debug(" %s - err=%d\n", path, ret);
372
373 ret = setup_fs(bflow, desc);
374 if (ret)
375 return log_msg_ret("fs", ret);
376
377 ret = alloc_file(path, size, &buf);
378 if (ret)
379 return log_msg_ret("all", ret);
380
381 *bufp = buf;
382 *sizep = size;
383
384 return 0;
385}
386
Simon Glass4b508b82022-04-24 23:31:08 -0600387int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
388 const char *file_path, ulong addr, ulong *sizep)
389{
390 struct blk_desc *desc = NULL;
391 loff_t len_read;
392 loff_t size;
393 int ret;
394
395 if (bflow->blk)
396 desc = dev_get_uclass_plat(bflow->blk);
397
398 ret = setup_fs(bflow, desc);
399 if (ret)
400 return log_msg_ret("fs", ret);
401
402 ret = fs_size(file_path, &size);
403 if (ret)
404 return log_msg_ret("size", ret);
405 if (size > *sizep)
406 return log_msg_ret("spc", -ENOSPC);
407
408 ret = setup_fs(bflow, desc);
409 if (ret)
410 return log_msg_ret("fs", ret);
411
412 ret = fs_read(file_path, addr, 0, 0, &len_read);
413 if (ret)
414 return ret;
415 *sizep = len_read;
416
417 return 0;
418}
419
420#ifdef CONFIG_BOOTSTD_FULL
421/**
422 * on_bootmeths() - Update the bootmeth order
423 *
424 * This will check for a valid baudrate and only apply it if valid.
425 */
426static int on_bootmeths(const char *name, const char *value, enum env_op op,
427 int flags)
428{
429 int ret;
430
431 switch (op) {
432 case env_op_create:
433 case env_op_overwrite:
434 ret = bootmeth_set_order(value);
435 if (ret)
436 return 1;
437 return 0;
438 case env_op_delete:
439 bootmeth_set_order(NULL);
440 fallthrough;
441 default:
442 return 0;
443 }
444}
445U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
446#endif /* CONFIG_BOOTSTD_FULL */
447
448UCLASS_DRIVER(bootmeth) = {
449 .id = UCLASS_BOOTMETH,
450 .name = "bootmeth",
451 .flags = DM_UC_FLAG_SEQ_ALIAS,
452 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
453};