1 // 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 <alist.h>
10 #include <blk.h>
11 #include <bootflow.h>
12 #include <bootmeth.h>
13 #include <bootstd.h>
14 #include <dm.h>
15 #include <dm/device-internal.h>
16 #include <env_internal.h>
17 #include <fs.h>
18 #include <malloc.h>
19 #include <mapmem.h>
20 #include <dm/uclass-internal.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
bootmeth_get_state_desc(struct udevice * dev,char * buf,int maxsize)24 int 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
bootmeth_check(struct udevice * dev,struct bootflow_iter * iter)34 int 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
bootmeth_read_bootflow(struct udevice * dev,struct bootflow * bflow)44 int 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
bootmeth_set_bootflow(struct udevice * dev,struct bootflow * bflow,char * buf,int size)54 int 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
65 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
bootmeth_read_all(struct udevice * dev,struct bootflow * bflow)66 int 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
bootmeth_boot(struct udevice * dev,struct bootflow * bflow)77 int 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
bootmeth_read_file(struct udevice * dev,struct bootflow * bflow,const char * file_path,ulong addr,enum bootflow_img_t type,ulong * sizep)87 int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
88 const char *file_path, ulong addr,
89 enum bootflow_img_t type, ulong *sizep)
90 {
91 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
92
93 if (!ops->read_file)
94 return -ENOSYS;
95
96 return ops->read_file(dev, bflow, file_path, addr, type, sizep);
97 }
98
bootmeth_get_bootflow(struct udevice * dev,struct bootflow * bflow)99 int 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;
105 bootflow_init(bflow, NULL, dev);
106
107 return ops->read_bootflow(dev, bflow);
108 }
109
bootmeth_setup_iter_order(struct bootflow_iter * iter,bool include_global)110 int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
111 {
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) {
133 int i;
134
135 /*
136 * We don't support skipping global bootmeths. Instead, the user
137 * should omit them from the ordering
138 */
139 if (!include_global) {
140 ret = log_msg_ret("glob", -EPERM);
141 goto err_order;
142 }
143 memcpy(order, std->bootmeth_order,
144 count * sizeof(struct udevice *));
145
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
152 ret = device_probe(dev);
153 if (ret) {
154 ret = log_msg_ret("probe", ret);
155 goto err_order;
156 }
157
158 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 }
167 } else {
168 struct udevice *dev;
169 int i, upto, pass;
170
171 /*
172 * 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.
175 */
176 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 }
199 }
200 count = upto;
201 }
202 if (!count) {
203 ret = log_msg_ret("count2", -ENOENT);
204 goto err_order;
205 }
206
207 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 }
212 iter->method_order = order;
213 iter->num_methods = count;
214
215 return 0;
216
217 err_order:
218 free(order);
219 return ret;
220 }
221
bootmeth_set_order(const char * order_str)222 int 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
bootmeth_set_property(const char * name,const char * property,const char * value)271 int 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
bootmeth_setup_fs(struct bootflow * bflow,struct blk_desc * desc)296 int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc)
297 {
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
bootmeth_try_file(struct bootflow * bflow,struct blk_desc * desc,const char * prefix,const char * fname)311 int 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 */
333 ret2 = bootmeth_setup_fs(bflow, desc);
334 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
bootmeth_alloc_file(struct bootflow * bflow,uint size_limit,uint align,enum bootflow_img_t type)346 int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align,
347 enum bootflow_img_t type)
348 {
349 struct blk_desc *desc = NULL;
350 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
359 ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf);
360 if (ret)
361 return log_msg_ret("all", ret);
362
363 bflow->state = BOOTFLOWST_READY;
364 bflow->buf = buf;
365
366 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
373 return 0;
374 }
375
bootmeth_alloc_other(struct bootflow * bflow,const char * fname,enum bootflow_img_t type,void ** bufp,uint * sizep)376 int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
377 enum bootflow_img_t type, void **bufp, uint *sizep)
378 {
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
391 ret = bootmeth_setup_fs(bflow, desc);
392 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
398 ret = bootmeth_setup_fs(bflow, desc);
399 if (ret)
400 return log_msg_ret("fs", ret);
401
402 ret = fs_read_alloc(path, size, 0, &buf);
403 if (ret)
404 return log_msg_ret("all", ret);
405
406 if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
407 size))
408 return log_msg_ret("boi", -ENOMEM);
409
410 *bufp = buf;
411 *sizep = size;
412
413 return 0;
414 }
415
bootmeth_common_read_file(struct udevice * dev,struct bootflow * bflow,const char * file_path,ulong addr,enum bootflow_img_t type,ulong * sizep)416 int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
417 const char *file_path, ulong addr,
418 enum bootflow_img_t type, ulong *sizep)
419 {
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
428 ret = bootmeth_setup_fs(bflow, desc);
429 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
438 ret = bootmeth_setup_fs(bflow, desc);
439 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
447 if (!bootflow_img_add(bflow, bflow->fname, type, addr, size))
448 return log_msg_ret("bci", -ENOMEM);
449
450 return 0;
451 }
452
453 #ifdef CONFIG_BOOTSTD_FULL
454 /**
455 * on_bootmeths() - Update the bootmeth order
456 *
457 * This will check for a valid list of bootmeths and only apply it if valid.
458 */
on_bootmeths(const char * name,const char * value,enum env_op op,int flags)459 static 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 }
478 U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
479 #endif /* CONFIG_BOOTSTD_FULL */
480
481 UCLASS_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 };
487