blob: cb84bb03447de0e57214a832ead504098776982f [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
Simon Glass8db2a382024-11-15 16:19:14 -07009#include <alist.h>
Simon Glass4b508b82022-04-24 23:31:08 -060010#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 Glass6d8f95b2023-08-10 19:33:18 -060064#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
65int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow)
66{
67 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
68
69 if (!ops->read_all)
70 return -ENOSYS;
71
72 return ops->read_all(dev, bflow);
73}
74#endif /* BOOTSTD_FULL */
75
Simon Glass4b508b82022-04-24 23:31:08 -060076int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
77{
78 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
79
80 if (!ops->boot)
81 return -ENOSYS;
82
83 return ops->boot(dev, bflow);
84}
85
86int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -070087 const char *file_path, ulong addr,
88 enum bootflow_img_t type, ulong *sizep)
Simon Glass4b508b82022-04-24 23:31:08 -060089{
90 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
91
92 if (!ops->read_file)
93 return -ENOSYS;
94
Simon Glassf39b5592024-11-15 16:19:17 -070095 return ops->read_file(dev, bflow, file_path, addr, type, sizep);
Simon Glass4b508b82022-04-24 23:31:08 -060096}
97
Simon Glass4f8633d2022-07-30 15:52:21 -060098int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
99{
100 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
101
102 if (!ops->read_bootflow)
103 return -ENOSYS;
Simon Glass00501fc2022-10-20 18:22:51 -0600104 bootflow_init(bflow, NULL, dev);
Simon Glass4f8633d2022-07-30 15:52:21 -0600105
106 return ops->read_bootflow(dev, bflow);
107}
108
Simon Glasscc15e142022-07-30 15:52:27 -0600109int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
Simon Glass4b508b82022-04-24 23:31:08 -0600110{
111 struct bootstd_priv *std;
112 struct udevice **order;
113 int count;
114 int ret;
115
116 ret = bootstd_get_priv(&std);
117 if (ret)
118 return ret;
119
120 /* Create an array large enough */
121 count = std->bootmeth_count ? std->bootmeth_count :
122 uclass_id_count(UCLASS_BOOTMETH);
123 if (!count)
124 return log_msg_ret("count", -ENOENT);
125
126 order = calloc(count, sizeof(struct udevice *));
127 if (!order)
128 return log_msg_ret("order", -ENOMEM);
129
130 /* If we have an ordering, copy it */
131 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
Simon Glasscc15e142022-07-30 15:52:27 -0600132 int i;
133
134 /*
135 * We don't support skipping global bootmeths. Instead, the user
136 * should omit them from the ordering
137 */
Sam Protsenkob1c6d812025-01-11 21:42:11 -0600138 if (!include_global) {
139 ret = log_msg_ret("glob", -EPERM);
140 goto err_order;
141 }
Simon Glass4b508b82022-04-24 23:31:08 -0600142 memcpy(order, std->bootmeth_order,
143 count * sizeof(struct bootmeth *));
Simon Glasscc15e142022-07-30 15:52:27 -0600144
145 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
146 for (i = 0; i < count; i++) {
147 struct udevice *dev = order[i];
148 struct bootmeth_uc_plat *ucp;
149 bool is_global;
150
151 ucp = dev_get_uclass_plat(dev);
152 is_global = ucp->flags &
153 BOOTMETHF_GLOBAL;
154 if (is_global) {
155 iter->first_glob_method = i;
156 break;
157 }
158 }
159 }
Simon Glass4b508b82022-04-24 23:31:08 -0600160 } else {
161 struct udevice *dev;
Simon Glasscc15e142022-07-30 15:52:27 -0600162 int i, upto, pass;
Simon Glass4b508b82022-04-24 23:31:08 -0600163
164 /*
Simon Glasscc15e142022-07-30 15:52:27 -0600165 * Do two passes, one to find the normal bootmeths and another
166 * to find the global ones, if required, The global ones go at
167 * the end.
Simon Glass4b508b82022-04-24 23:31:08 -0600168 */
Simon Glasscc15e142022-07-30 15:52:27 -0600169 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
170 if (pass)
171 iter->first_glob_method = upto;
172 /*
173 * Get a list of bootmethods, in seq order (i.e. using
174 * aliases). There may be gaps so try to count up high
175 * enough to find them all.
176 */
177 for (i = 0; upto < count && i < 20 + count * 2; i++) {
178 struct bootmeth_uc_plat *ucp;
179 bool is_global;
180
181 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
182 i, &dev);
183 if (ret)
184 continue;
185 ucp = dev_get_uclass_plat(dev);
186 is_global =
187 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
188 (ucp->flags & BOOTMETHF_GLOBAL);
189 if (pass ? is_global : !is_global)
190 order[upto++] = dev;
191 }
Simon Glass4b508b82022-04-24 23:31:08 -0600192 }
193 count = upto;
194 }
Sam Protsenkob1c6d812025-01-11 21:42:11 -0600195 if (!count) {
196 ret = log_msg_ret("count2", -ENOENT);
197 goto err_order;
198 }
Simon Glass4b508b82022-04-24 23:31:08 -0600199
Simon Glasscc15e142022-07-30 15:52:27 -0600200 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
201 iter->first_glob_method != -1 && iter->first_glob_method != count) {
202 iter->cur_method = iter->first_glob_method;
203 iter->doing_global = true;
204 }
Simon Glass4b508b82022-04-24 23:31:08 -0600205 iter->method_order = order;
206 iter->num_methods = count;
Simon Glass4b508b82022-04-24 23:31:08 -0600207
208 return 0;
Sam Protsenkob1c6d812025-01-11 21:42:11 -0600209
210err_order:
211 free(order);
212 return ret;
Simon Glass4b508b82022-04-24 23:31:08 -0600213}
214
215int bootmeth_set_order(const char *order_str)
216{
217 struct bootstd_priv *std;
218 struct udevice **order;
219 int count, ret, i, len;
220 const char *s, *p;
221
222 ret = bootstd_get_priv(&std);
223 if (ret)
224 return ret;
225
226 if (!order_str) {
227 free(std->bootmeth_order);
228 std->bootmeth_order = NULL;
229 std->bootmeth_count = 0;
230 return 0;
231 }
232
233 /* Create an array large enough */
234 count = uclass_id_count(UCLASS_BOOTMETH);
235 if (!count)
236 return log_msg_ret("count", -ENOENT);
237
238 order = calloc(count + 1, sizeof(struct udevice *));
239 if (!order)
240 return log_msg_ret("order", -ENOMEM);
241
242 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
243 struct udevice *dev;
244
245 p = strchrnul(s, ' ');
246 len = p - s;
247 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
248 &dev);
249 if (ret) {
250 printf("Unknown bootmeth '%.*s'\n", len, s);
251 free(order);
252 return ret;
253 }
254 order[i] = dev;
255 }
256 order[i] = NULL;
257 free(std->bootmeth_order);
258 std->bootmeth_order = order;
259 std->bootmeth_count = i;
260
261 return 0;
262}
263
Martyn Welch93a0d162024-10-09 14:15:40 +0100264int bootmeth_set_property(const char *name, const char *property, const char *value)
265{
266 int ret;
267 int len;
268 struct udevice *dev;
269 const struct bootmeth_ops *ops;
270
271 len = strlen(name);
272
273 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, name, len,
274 &dev);
275 if (ret) {
276 printf("Unknown bootmeth '%s'\n", name);
277 return ret;
278 }
279
280 ops = bootmeth_get_ops(dev);
281 if (!ops->set_property) {
282 printf("set_property not found\n");
283 return -ENODEV;
284 }
285
286 return ops->set_property(dev, property, value);
287}
288
Simon Glassf41c4452023-07-26 21:01:21 -0600289int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc)
Simon Glass4b508b82022-04-24 23:31:08 -0600290{
291 int ret;
292
293 if (desc) {
294 ret = fs_set_blk_dev_with_part(desc, bflow->part);
295 if (ret)
296 return log_msg_ret("set", ret);
297 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
298 fs_set_type(bflow->fs_type);
299 }
300
301 return 0;
302}
303
304int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
305 const char *prefix, const char *fname)
306{
307 char path[200];
308 loff_t size;
309 int ret, ret2;
310
311 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
312 log_debug("trying: %s\n", path);
313
314 free(bflow->fname);
315 bflow->fname = strdup(path);
316 if (!bflow->fname)
317 return log_msg_ret("name", -ENOMEM);
318
319 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
320 fs_set_type(bflow->fs_type);
321
322 ret = fs_size(path, &size);
323 log_debug(" %s - err=%d\n", path, ret);
324
325 /* Sadly FS closes the file after fs_size() so we must redo this */
Simon Glassf41c4452023-07-26 21:01:21 -0600326 ret2 = bootmeth_setup_fs(bflow, desc);
Simon Glass4b508b82022-04-24 23:31:08 -0600327 if (ret2)
328 return log_msg_ret("fs", ret2);
329
330 if (ret)
331 return log_msg_ret("size", ret);
332
333 bflow->size = size;
334 bflow->state = BOOTFLOWST_FILE;
335
336 return 0;
337}
338
Simon Glass8db2a382024-11-15 16:19:14 -0700339int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align,
340 enum bootflow_img_t type)
Simon Glass612b9cc2023-01-06 08:52:34 -0600341{
Simon Glass8db2a382024-11-15 16:19:14 -0700342 struct blk_desc *desc = NULL;
Simon Glass612b9cc2023-01-06 08:52:34 -0600343 void *buf;
344 uint size;
345 int ret;
346
347 size = bflow->size;
348 log_debug(" - script file size %x\n", size);
349 if (size > size_limit)
350 return log_msg_ret("chk", -E2BIG);
351
Simon Glasse6c49fc2023-06-01 10:22:38 -0600352 ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf);
Simon Glass612b9cc2023-01-06 08:52:34 -0600353 if (ret)
354 return log_msg_ret("all", ret);
355
Simon Glass4b508b82022-04-24 23:31:08 -0600356 bflow->state = BOOTFLOWST_READY;
357 bflow->buf = buf;
358
Simon Glass8db2a382024-11-15 16:19:14 -0700359 if (bflow->blk)
360 desc = dev_get_uclass_plat(bflow->blk);
361
362 if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
363 size))
364 return log_msg_ret("bai", -ENOMEM);
365
Simon Glass4b508b82022-04-24 23:31:08 -0600366 return 0;
367}
368
Simon Glass612b9cc2023-01-06 08:52:34 -0600369int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
Simon Glass62632b22024-11-15 16:19:21 -0700370 enum bootflow_img_t type, void **bufp, uint *sizep)
Simon Glass612b9cc2023-01-06 08:52:34 -0600371{
372 struct blk_desc *desc = NULL;
373 char path[200];
374 loff_t size;
375 void *buf;
376 int ret;
377
378 snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
379 log_debug("trying: %s\n", path);
380
381 if (bflow->blk)
382 desc = dev_get_uclass_plat(bflow->blk);
383
Simon Glassf41c4452023-07-26 21:01:21 -0600384 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass612b9cc2023-01-06 08:52:34 -0600385 if (ret)
386 return log_msg_ret("fs", ret);
387
388 ret = fs_size(path, &size);
389 log_debug(" %s - err=%d\n", path, ret);
390
Simon Glassf41c4452023-07-26 21:01:21 -0600391 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass612b9cc2023-01-06 08:52:34 -0600392 if (ret)
393 return log_msg_ret("fs", ret);
394
Simon Glasse6c49fc2023-06-01 10:22:38 -0600395 ret = fs_read_alloc(path, size, 0, &buf);
Simon Glass612b9cc2023-01-06 08:52:34 -0600396 if (ret)
397 return log_msg_ret("all", ret);
398
Simon Glass62632b22024-11-15 16:19:21 -0700399 if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
400 size))
401 return log_msg_ret("boi", -ENOMEM);
402
Simon Glass612b9cc2023-01-06 08:52:34 -0600403 *bufp = buf;
404 *sizep = size;
405
406 return 0;
407}
408
Simon Glass4b508b82022-04-24 23:31:08 -0600409int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -0700410 const char *file_path, ulong addr,
411 enum bootflow_img_t type, ulong *sizep)
Simon Glass4b508b82022-04-24 23:31:08 -0600412{
413 struct blk_desc *desc = NULL;
414 loff_t len_read;
415 loff_t size;
416 int ret;
417
418 if (bflow->blk)
419 desc = dev_get_uclass_plat(bflow->blk);
420
Simon Glassf41c4452023-07-26 21:01:21 -0600421 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass4b508b82022-04-24 23:31:08 -0600422 if (ret)
423 return log_msg_ret("fs", ret);
424
425 ret = fs_size(file_path, &size);
426 if (ret)
427 return log_msg_ret("size", ret);
428 if (size > *sizep)
429 return log_msg_ret("spc", -ENOSPC);
430
Simon Glassf41c4452023-07-26 21:01:21 -0600431 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass4b508b82022-04-24 23:31:08 -0600432 if (ret)
433 return log_msg_ret("fs", ret);
434
435 ret = fs_read(file_path, addr, 0, 0, &len_read);
436 if (ret)
437 return ret;
438 *sizep = len_read;
439
Simon Glassf39b5592024-11-15 16:19:17 -0700440 if (!bootflow_img_add(bflow, bflow->fname, type, addr, size))
441 return log_msg_ret("bci", -ENOMEM);
442
Simon Glass4b508b82022-04-24 23:31:08 -0600443 return 0;
444}
445
446#ifdef CONFIG_BOOTSTD_FULL
447/**
448 * on_bootmeths() - Update the bootmeth order
449 *
Simon Glass7fbbbc82023-07-12 09:04:33 -0600450 * This will check for a valid list of bootmeths and only apply it if valid.
Simon Glass4b508b82022-04-24 23:31:08 -0600451 */
452static int on_bootmeths(const char *name, const char *value, enum env_op op,
453 int flags)
454{
455 int ret;
456
457 switch (op) {
458 case env_op_create:
459 case env_op_overwrite:
460 ret = bootmeth_set_order(value);
461 if (ret)
462 return 1;
463 return 0;
464 case env_op_delete:
465 bootmeth_set_order(NULL);
466 fallthrough;
467 default:
468 return 0;
469 }
470}
471U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
472#endif /* CONFIG_BOOTSTD_FULL */
473
474UCLASS_DRIVER(bootmeth) = {
475 .id = UCLASS_BOOTMETH,
476 .name = "bootmeth",
477 .flags = DM_UC_FLAG_SEQ_ALIAS,
478 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
479};