blob: 57b30bcb513af7c8024e8290ff053e2dc09eb8f9 [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>
Sam Protsenkofa78d2a2025-01-11 21:42:12 -060015#include <dm/device-internal.h>
Simon Glass4b508b82022-04-24 23:31:08 -060016#include <env_internal.h>
17#include <fs.h>
18#include <malloc.h>
19#include <mapmem.h>
20#include <dm/uclass-internal.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
Simon Glassf6d71a82022-07-30 15:52:19 -060024int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize)
25{
26 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
27
28 if (!ops->get_state_desc)
29 return -ENOSYS;
30
31 return ops->get_state_desc(dev, buf, maxsize);
32}
33
Simon Glass4b508b82022-04-24 23:31:08 -060034int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
35{
36 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
37
38 if (!ops->check)
39 return 0;
40
41 return ops->check(dev, iter);
42}
43
44int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
45{
46 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
47
48 if (!ops->read_bootflow)
49 return -ENOSYS;
50
51 return ops->read_bootflow(dev, bflow);
52}
53
Simon Glassa63bda62023-01-17 10:48:01 -070054int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
55 char *buf, int size)
56{
57 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
58
59 if (!ops->set_bootflow)
60 return -ENOSYS;
61
62 return ops->set_bootflow(dev, bflow, buf, size);
63}
64
Simon Glass6d8f95b2023-08-10 19:33:18 -060065#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
66int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow)
67{
68 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
69
70 if (!ops->read_all)
71 return -ENOSYS;
72
73 return ops->read_all(dev, bflow);
74}
75#endif /* BOOTSTD_FULL */
76
Simon Glass4b508b82022-04-24 23:31:08 -060077int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
78{
79 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
80
81 if (!ops->boot)
82 return -ENOSYS;
83
84 return ops->boot(dev, bflow);
85}
86
87int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -070088 const char *file_path, ulong addr,
89 enum bootflow_img_t type, ulong *sizep)
Simon Glass4b508b82022-04-24 23:31:08 -060090{
91 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
92
93 if (!ops->read_file)
94 return -ENOSYS;
95
Simon Glassf39b5592024-11-15 16:19:17 -070096 return ops->read_file(dev, bflow, file_path, addr, type, sizep);
Simon Glass4b508b82022-04-24 23:31:08 -060097}
98
Simon Glass4f8633d2022-07-30 15:52:21 -060099int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
100{
101 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
102
103 if (!ops->read_bootflow)
104 return -ENOSYS;
Simon Glass00501fc2022-10-20 18:22:51 -0600105 bootflow_init(bflow, NULL, dev);
Simon Glass4f8633d2022-07-30 15:52:21 -0600106
107 return ops->read_bootflow(dev, bflow);
108}
109
Simon Glasscc15e142022-07-30 15:52:27 -0600110int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
Simon Glass4b508b82022-04-24 23:31:08 -0600111{
112 struct bootstd_priv *std;
113 struct udevice **order;
114 int count;
115 int ret;
116
117 ret = bootstd_get_priv(&std);
118 if (ret)
119 return ret;
120
121 /* Create an array large enough */
122 count = std->bootmeth_count ? std->bootmeth_count :
123 uclass_id_count(UCLASS_BOOTMETH);
124 if (!count)
125 return log_msg_ret("count", -ENOENT);
126
127 order = calloc(count, sizeof(struct udevice *));
128 if (!order)
129 return log_msg_ret("order", -ENOMEM);
130
131 /* If we have an ordering, copy it */
132 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
Simon Glasscc15e142022-07-30 15:52:27 -0600133 int i;
134
135 /*
136 * We don't support skipping global bootmeths. Instead, the user
137 * should omit them from the ordering
138 */
Sam Protsenkob1c6d812025-01-11 21:42:11 -0600139 if (!include_global) {
140 ret = log_msg_ret("glob", -EPERM);
141 goto err_order;
142 }
Simon Glass4b508b82022-04-24 23:31:08 -0600143 memcpy(order, std->bootmeth_order,
144 count * sizeof(struct bootmeth *));
Simon Glasscc15e142022-07-30 15:52:27 -0600145
146 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
147 for (i = 0; i < count; i++) {
148 struct udevice *dev = order[i];
149 struct bootmeth_uc_plat *ucp;
150 bool is_global;
151
Sam Protsenkofa78d2a2025-01-11 21:42:12 -0600152 ret = device_probe(dev);
153 if (ret) {
154 ret = log_msg_ret("probe", ret);
155 goto err_order;
156 }
157
Simon Glasscc15e142022-07-30 15:52:27 -0600158 ucp = dev_get_uclass_plat(dev);
159 is_global = ucp->flags &
160 BOOTMETHF_GLOBAL;
161 if (is_global) {
162 iter->first_glob_method = i;
163 break;
164 }
165 }
166 }
Simon Glass4b508b82022-04-24 23:31:08 -0600167 } else {
168 struct udevice *dev;
Simon Glasscc15e142022-07-30 15:52:27 -0600169 int i, upto, pass;
Simon Glass4b508b82022-04-24 23:31:08 -0600170
171 /*
Simon Glasscc15e142022-07-30 15:52:27 -0600172 * Do two passes, one to find the normal bootmeths and another
173 * to find the global ones, if required, The global ones go at
174 * the end.
Simon Glass4b508b82022-04-24 23:31:08 -0600175 */
Simon Glasscc15e142022-07-30 15:52:27 -0600176 for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
177 if (pass)
178 iter->first_glob_method = upto;
179 /*
180 * Get a list of bootmethods, in seq order (i.e. using
181 * aliases). There may be gaps so try to count up high
182 * enough to find them all.
183 */
184 for (i = 0; upto < count && i < 20 + count * 2; i++) {
185 struct bootmeth_uc_plat *ucp;
186 bool is_global;
187
188 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
189 i, &dev);
190 if (ret)
191 continue;
192 ucp = dev_get_uclass_plat(dev);
193 is_global =
194 IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
195 (ucp->flags & BOOTMETHF_GLOBAL);
196 if (pass ? is_global : !is_global)
197 order[upto++] = dev;
198 }
Simon Glass4b508b82022-04-24 23:31:08 -0600199 }
200 count = upto;
201 }
Sam Protsenkob1c6d812025-01-11 21:42:11 -0600202 if (!count) {
203 ret = log_msg_ret("count2", -ENOENT);
204 goto err_order;
205 }
Simon Glass4b508b82022-04-24 23:31:08 -0600206
Simon Glasscc15e142022-07-30 15:52:27 -0600207 if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
208 iter->first_glob_method != -1 && iter->first_glob_method != count) {
209 iter->cur_method = iter->first_glob_method;
210 iter->doing_global = true;
211 }
Simon Glass4b508b82022-04-24 23:31:08 -0600212 iter->method_order = order;
213 iter->num_methods = count;
Simon Glass4b508b82022-04-24 23:31:08 -0600214
215 return 0;
Sam Protsenkob1c6d812025-01-11 21:42:11 -0600216
217err_order:
218 free(order);
219 return ret;
Simon Glass4b508b82022-04-24 23:31:08 -0600220}
221
222int bootmeth_set_order(const char *order_str)
223{
224 struct bootstd_priv *std;
225 struct udevice **order;
226 int count, ret, i, len;
227 const char *s, *p;
228
229 ret = bootstd_get_priv(&std);
230 if (ret)
231 return ret;
232
233 if (!order_str) {
234 free(std->bootmeth_order);
235 std->bootmeth_order = NULL;
236 std->bootmeth_count = 0;
237 return 0;
238 }
239
240 /* Create an array large enough */
241 count = uclass_id_count(UCLASS_BOOTMETH);
242 if (!count)
243 return log_msg_ret("count", -ENOENT);
244
245 order = calloc(count + 1, sizeof(struct udevice *));
246 if (!order)
247 return log_msg_ret("order", -ENOMEM);
248
249 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
250 struct udevice *dev;
251
252 p = strchrnul(s, ' ');
253 len = p - s;
254 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
255 &dev);
256 if (ret) {
257 printf("Unknown bootmeth '%.*s'\n", len, s);
258 free(order);
259 return ret;
260 }
261 order[i] = dev;
262 }
263 order[i] = NULL;
264 free(std->bootmeth_order);
265 std->bootmeth_order = order;
266 std->bootmeth_count = i;
267
268 return 0;
269}
270
Martyn Welch93a0d162024-10-09 14:15:40 +0100271int bootmeth_set_property(const char *name, const char *property, const char *value)
272{
273 int ret;
274 int len;
275 struct udevice *dev;
276 const struct bootmeth_ops *ops;
277
278 len = strlen(name);
279
280 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, name, len,
281 &dev);
282 if (ret) {
283 printf("Unknown bootmeth '%s'\n", name);
284 return ret;
285 }
286
287 ops = bootmeth_get_ops(dev);
288 if (!ops->set_property) {
289 printf("set_property not found\n");
290 return -ENODEV;
291 }
292
293 return ops->set_property(dev, property, value);
294}
295
Simon Glassf41c4452023-07-26 21:01:21 -0600296int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc)
Simon Glass4b508b82022-04-24 23:31:08 -0600297{
298 int ret;
299
300 if (desc) {
301 ret = fs_set_blk_dev_with_part(desc, bflow->part);
302 if (ret)
303 return log_msg_ret("set", ret);
304 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
305 fs_set_type(bflow->fs_type);
306 }
307
308 return 0;
309}
310
311int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
312 const char *prefix, const char *fname)
313{
314 char path[200];
315 loff_t size;
316 int ret, ret2;
317
318 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
319 log_debug("trying: %s\n", path);
320
321 free(bflow->fname);
322 bflow->fname = strdup(path);
323 if (!bflow->fname)
324 return log_msg_ret("name", -ENOMEM);
325
326 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
327 fs_set_type(bflow->fs_type);
328
329 ret = fs_size(path, &size);
330 log_debug(" %s - err=%d\n", path, ret);
331
332 /* Sadly FS closes the file after fs_size() so we must redo this */
Simon Glassf41c4452023-07-26 21:01:21 -0600333 ret2 = bootmeth_setup_fs(bflow, desc);
Simon Glass4b508b82022-04-24 23:31:08 -0600334 if (ret2)
335 return log_msg_ret("fs", ret2);
336
337 if (ret)
338 return log_msg_ret("size", ret);
339
340 bflow->size = size;
341 bflow->state = BOOTFLOWST_FILE;
342
343 return 0;
344}
345
Simon Glass8db2a382024-11-15 16:19:14 -0700346int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align,
347 enum bootflow_img_t type)
Simon Glass612b9cc2023-01-06 08:52:34 -0600348{
Simon Glass8db2a382024-11-15 16:19:14 -0700349 struct blk_desc *desc = NULL;
Simon Glass612b9cc2023-01-06 08:52:34 -0600350 void *buf;
351 uint size;
352 int ret;
353
354 size = bflow->size;
355 log_debug(" - script file size %x\n", size);
356 if (size > size_limit)
357 return log_msg_ret("chk", -E2BIG);
358
Simon Glasse6c49fc2023-06-01 10:22:38 -0600359 ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf);
Simon Glass612b9cc2023-01-06 08:52:34 -0600360 if (ret)
361 return log_msg_ret("all", ret);
362
Simon Glass4b508b82022-04-24 23:31:08 -0600363 bflow->state = BOOTFLOWST_READY;
364 bflow->buf = buf;
365
Simon Glass8db2a382024-11-15 16:19:14 -0700366 if (bflow->blk)
367 desc = dev_get_uclass_plat(bflow->blk);
368
369 if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
370 size))
371 return log_msg_ret("bai", -ENOMEM);
372
Simon Glass4b508b82022-04-24 23:31:08 -0600373 return 0;
374}
375
Simon Glass612b9cc2023-01-06 08:52:34 -0600376int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
Simon Glass62632b22024-11-15 16:19:21 -0700377 enum bootflow_img_t type, void **bufp, uint *sizep)
Simon Glass612b9cc2023-01-06 08:52:34 -0600378{
379 struct blk_desc *desc = NULL;
380 char path[200];
381 loff_t size;
382 void *buf;
383 int ret;
384
385 snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
386 log_debug("trying: %s\n", path);
387
388 if (bflow->blk)
389 desc = dev_get_uclass_plat(bflow->blk);
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
395 ret = fs_size(path, &size);
396 log_debug(" %s - err=%d\n", path, ret);
397
Simon Glassf41c4452023-07-26 21:01:21 -0600398 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass612b9cc2023-01-06 08:52:34 -0600399 if (ret)
400 return log_msg_ret("fs", ret);
401
Simon Glasse6c49fc2023-06-01 10:22:38 -0600402 ret = fs_read_alloc(path, size, 0, &buf);
Simon Glass612b9cc2023-01-06 08:52:34 -0600403 if (ret)
404 return log_msg_ret("all", ret);
405
Simon Glass62632b22024-11-15 16:19:21 -0700406 if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
407 size))
408 return log_msg_ret("boi", -ENOMEM);
409
Simon Glass612b9cc2023-01-06 08:52:34 -0600410 *bufp = buf;
411 *sizep = size;
412
413 return 0;
414}
415
Simon Glass4b508b82022-04-24 23:31:08 -0600416int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -0700417 const char *file_path, ulong addr,
418 enum bootflow_img_t type, ulong *sizep)
Simon Glass4b508b82022-04-24 23:31:08 -0600419{
420 struct blk_desc *desc = NULL;
421 loff_t len_read;
422 loff_t size;
423 int ret;
424
425 if (bflow->blk)
426 desc = dev_get_uclass_plat(bflow->blk);
427
Simon Glassf41c4452023-07-26 21:01:21 -0600428 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass4b508b82022-04-24 23:31:08 -0600429 if (ret)
430 return log_msg_ret("fs", ret);
431
432 ret = fs_size(file_path, &size);
433 if (ret)
434 return log_msg_ret("size", ret);
435 if (size > *sizep)
436 return log_msg_ret("spc", -ENOSPC);
437
Simon Glassf41c4452023-07-26 21:01:21 -0600438 ret = bootmeth_setup_fs(bflow, desc);
Simon Glass4b508b82022-04-24 23:31:08 -0600439 if (ret)
440 return log_msg_ret("fs", ret);
441
442 ret = fs_read(file_path, addr, 0, 0, &len_read);
443 if (ret)
444 return ret;
445 *sizep = len_read;
446
Simon Glassf39b5592024-11-15 16:19:17 -0700447 if (!bootflow_img_add(bflow, bflow->fname, type, addr, size))
448 return log_msg_ret("bci", -ENOMEM);
449
Simon Glass4b508b82022-04-24 23:31:08 -0600450 return 0;
451}
452
453#ifdef CONFIG_BOOTSTD_FULL
454/**
455 * on_bootmeths() - Update the bootmeth order
456 *
Simon Glass7fbbbc82023-07-12 09:04:33 -0600457 * This will check for a valid list of bootmeths and only apply it if valid.
Simon Glass4b508b82022-04-24 23:31:08 -0600458 */
459static int on_bootmeths(const char *name, const char *value, enum env_op op,
460 int flags)
461{
462 int ret;
463
464 switch (op) {
465 case env_op_create:
466 case env_op_overwrite:
467 ret = bootmeth_set_order(value);
468 if (ret)
469 return 1;
470 return 0;
471 case env_op_delete:
472 bootmeth_set_order(NULL);
473 fallthrough;
474 default:
475 return 0;
476 }
477}
478U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
479#endif /* CONFIG_BOOTSTD_FULL */
480
481UCLASS_DRIVER(bootmeth) = {
482 .id = UCLASS_BOOTMETH,
483 .name = "bootmeth",
484 .flags = DM_UC_FLAG_SEQ_ALIAS,
485 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
486};