blob: 13ac69eb392fd70822e4ddbbeafe472858d2b761 [file] [log] [blame]
Simon Glass201417d2022-04-24 23:31:07 -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 <dm.h>
11#include <bootdev.h>
12#include <bootflow.h>
Simon Glassa8f5be12022-04-24 23:31:09 -060013#include <bootmeth.h>
Simon Glass201417d2022-04-24 23:31:07 -060014#include <bootstd.h>
15#include <env.h>
16#include <fs.h>
17#include <log.h>
18#include <malloc.h>
19#include <part.h>
20#include <sort.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
23#include <dm/uclass-internal.h>
24
25enum {
26 /*
27 * Set some sort of limit on the number of partitions a bootdev can
28 * have. Note that for disks this limits the partitions numbers that
29 * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV
30 */
31 MAX_PART_PER_BOOTDEV = 30,
32
33 /* Maximum supported length of the "boot_targets" env string */
34 BOOT_TARGETS_MAX_LEN = 100,
35};
36
37int bootdev_add_bootflow(struct bootflow *bflow)
38{
Simon Glass201417d2022-04-24 23:31:07 -060039 struct bootstd_priv *std;
40 struct bootflow *new;
41 int ret;
42
43 assert(bflow->dev);
44 ret = bootstd_get_priv(&std);
45 if (ret)
46 return ret;
47
48 new = malloc(sizeof(*bflow));
49 if (!new)
50 return log_msg_ret("bflow", -ENOMEM);
51 memcpy(new, bflow, sizeof(*bflow));
52
53 list_add_tail(&new->glob_node, &std->glob_head);
Simon Glasseccb25c2022-07-30 15:52:23 -060054 if (bflow->dev) {
55 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
56
57 list_add_tail(&new->bm_node, &ucp->bootflow_head);
58 }
Simon Glass201417d2022-04-24 23:31:07 -060059
60 return 0;
61}
62
63int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
64{
65 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
66
67 if (list_empty(&ucp->bootflow_head))
68 return -ENOENT;
69
70 *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
71 bm_node);
72
73 return 0;
74}
75
76int bootdev_next_bootflow(struct bootflow **bflowp)
77{
78 struct bootflow *bflow = *bflowp;
79 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
80
81 *bflowp = NULL;
82
83 if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
84 return -ENOENT;
85
86 *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
87
88 return 0;
89}
90
91int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
92 struct udevice **devp)
93{
94 struct udevice *dev;
95 char dev_name[30];
96 char *str;
97 int ret;
98
99 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
100 str = strdup(dev_name);
101 if (!str)
102 return -ENOMEM;
103 ret = device_bind_driver(parent, drv_name, str, &dev);
104 if (ret)
105 return ret;
106 device_set_name_alloced(dev);
107 *devp = dev;
108
109 return 0;
110}
111
112int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
113 struct bootflow_iter *iter, struct bootflow *bflow)
114{
115 struct blk_desc *desc = dev_get_uclass_plat(blk);
116 struct disk_partition info;
117 char partstr[20];
118 char name[60];
119 int ret;
120
121 /* Sanity check */
122 if (iter->part >= MAX_PART_PER_BOOTDEV)
123 return log_msg_ret("max", -ESHUTDOWN);
124
125 bflow->blk = blk;
126 if (iter->part)
127 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
128 else
129 strcpy(partstr, "whole");
130 snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
131 bflow->name = strdup(name);
132 if (!bflow->name)
133 return log_msg_ret("name", -ENOMEM);
134
135 bflow->part = iter->part;
136
Simon Glassa8f5be12022-04-24 23:31:09 -0600137 ret = bootmeth_check(bflow->method, iter);
138 if (ret)
139 return log_msg_ret("check", ret);
140
Simon Glass201417d2022-04-24 23:31:07 -0600141 /*
142 * partition numbers start at 0 so this cannot succeed, but it can tell
143 * us whether there is valid media there
144 */
145 ret = part_get_info(desc, iter->part, &info);
146 if (!iter->part && ret == -ENOENT)
147 ret = 0;
148
149 /*
150 * This error indicates the media is not present. Otherwise we just
151 * blindly scan the next partition. We could be more intelligent here
152 * and check which partition numbers actually exist.
153 */
154 if (ret == -EOPNOTSUPP)
155 ret = -ESHUTDOWN;
156 else
157 bflow->state = BOOTFLOWST_MEDIA;
158 if (ret)
159 return log_msg_ret("part", ret);
160
161 /*
162 * Currently we don't get the number of partitions, so just
163 * assume a large number
164 */
165 iter->max_part = MAX_PART_PER_BOOTDEV;
166
167 if (iter->part) {
168 ret = fs_set_blk_dev_with_part(desc, bflow->part);
169 bflow->state = BOOTFLOWST_PART;
170
171 /* Use an #ifdef due to info.sys_ind */
172#ifdef CONFIG_DOS_PARTITION
173 log_debug("%s: Found partition %x type %x fstype %d\n",
174 blk->name, bflow->part, info.sys_ind,
175 ret ? -1 : fs_get_type());
176#endif
177 if (ret)
178 return log_msg_ret("fs", ret);
179 bflow->state = BOOTFLOWST_FS;
180 }
181
Simon Glassa8f5be12022-04-24 23:31:09 -0600182 ret = bootmeth_read_bootflow(bflow->method, bflow);
183 if (ret)
184 return log_msg_ret("method", ret);
185
Simon Glass201417d2022-04-24 23:31:07 -0600186 return 0;
187}
188
189void bootdev_list(bool probe)
190{
191 struct udevice *dev;
192 int ret;
193 int i;
194
195 printf("Seq Probed Status Uclass Name\n");
196 printf("--- ------ ------ -------- ------------------\n");
197 if (probe)
198 ret = uclass_first_device_err(UCLASS_BOOTDEV, &dev);
199 else
200 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
201 for (i = 0; dev; i++) {
202 printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
203 device_active(dev) ? '+' : ' ',
204 ret ? simple_itoa(ret) : "OK",
205 dev_get_uclass_name(dev_get_parent(dev)), dev->name);
206 if (probe)
207 ret = uclass_next_device_err(&dev);
208 else
209 ret = uclass_find_next_device(&dev);
210 }
211 printf("--- ------ ------ -------- ------------------\n");
212 printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
213}
214
215int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
216{
217 struct udevice *bdev;
218 int ret;
219
220 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
221 &bdev);
222 if (ret) {
223 if (ret != -ENODEV) {
224 log_debug("Cannot access bootdev device\n");
225 return ret;
226 }
227
228 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
229 if (ret) {
230 log_debug("Cannot create bootdev device\n");
231 return ret;
232 }
233 }
234
235 return 0;
236}
237
238int bootdev_setup_sibling_blk(struct udevice *blk, const char *drv_name)
239{
240 struct udevice *parent, *dev;
241 char dev_name[50];
242 int ret;
243
244 snprintf(dev_name, sizeof(dev_name), "%s.%s", blk->name, "bootdev");
245
246 parent = dev_get_parent(blk);
247 ret = device_find_child_by_name(parent, dev_name, &dev);
248 if (ret) {
249 char *str;
250
251 if (ret != -ENODEV) {
252 log_debug("Cannot access bootdev device\n");
253 return ret;
254 }
255 str = strdup(dev_name);
256 if (!str)
257 return -ENOMEM;
258
259 ret = device_bind_driver(parent, drv_name, str, &dev);
260 if (ret) {
261 log_debug("Cannot create bootdev device\n");
262 return ret;
263 }
264 device_set_name_alloced(dev);
265 }
266
267 return 0;
268}
269
270int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
271{
272 struct udevice *parent = dev_get_parent(dev);
273 struct udevice *blk;
274 int ret, len;
275 char *p;
276
277 if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
278 return -EINVAL;
279
280 /* This should always work if bootdev_setup_sibling_blk() was used */
281 p = strstr(dev->name, ".bootdev");
282 if (!p)
283 return log_msg_ret("str", -EINVAL);
284
285 len = p - dev->name;
286 ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
287 if (ret)
288 return log_msg_ret("find", ret);
289 *blkp = blk;
290
291 return 0;
292}
293
294static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
295{
296 struct udevice *parent = dev_get_parent(blk);
297 struct udevice *bootdev;
298 char dev_name[50];
299 int ret;
300
301 if (device_get_uclass_id(blk) != UCLASS_BLK)
302 return -EINVAL;
303
304 /* This should always work if bootdev_setup_sibling_blk() was used */
305 snprintf(dev_name, sizeof(dev_name), "%s.%s", blk->name, "bootdev");
306 ret = device_find_child_by_name(parent, dev_name, &bootdev);
307 if (ret)
308 return log_msg_ret("find", ret);
309 *bootdevp = bootdev;
310
311 return 0;
312}
313
314int bootdev_unbind_dev(struct udevice *parent)
315{
316 struct udevice *dev;
317 int ret;
318
319 ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
320 if (!ret) {
321 ret = device_remove(dev, DM_REMOVE_NORMAL);
322 if (ret)
323 return log_msg_ret("rem", ret);
324 ret = device_unbind(dev);
325 if (ret)
326 return log_msg_ret("unb", ret);
327 }
328
329 return 0;
330}
331
332/**
333 * bootdev_find_by_label() - Convert a label string to a bootdev device
334 *
335 * Looks up a label name to find the associated bootdev. For example, if the
336 * label name is "mmc2", this will find a bootdev for an mmc device whose
337 * sequence number is 2.
338 *
339 * @label: Label string to convert, e.g. "mmc2"
340 * @devp: Returns bootdev device corresponding to that boot label
341 * Return: 0 if OK, -EINVAL if the label name (e.g. "mmc") does not refer to a
342 * uclass, -ENOENT if no bootdev for that media has the sequence number
343 * (e.g. 2)
344 */
345int bootdev_find_by_label(const char *label, struct udevice **devp)
346{
347 struct udevice *media;
348 struct uclass *uc;
349 enum uclass_id id;
350 const char *end;
351 int seq;
352
353 seq = trailing_strtoln_end(label, NULL, &end);
354 id = uclass_get_by_namelen(label, end - label);
355 log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
356 uclass_get_name(id));
357 if (id == UCLASS_INVALID) {
358 log_warning("Unknown uclass '%s' in label\n", label);
359 return -EINVAL;
360 }
361 if (id == UCLASS_USB)
362 id = UCLASS_MASS_STORAGE;
363
364 /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
365 uclass_id_foreach_dev(id, media, uc) {
366 struct udevice *bdev, *blk;
367 int ret;
368
369 /* if there is no seq, match anything */
370 if (seq != -1 && dev_seq(media) != seq) {
371 log_debug("- skip, media seq=%d\n", dev_seq(media));
372 continue;
373 }
374
375 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
376 &bdev);
377 if (ret) {
378 log_debug("- looking via blk, seq=%d, id=%d\n", seq,
379 id);
380 ret = blk_find_device(id, seq, &blk);
381 if (!ret) {
382 log_debug("- get from blk %s\n", blk->name);
383 ret = bootdev_get_from_blk(blk, &bdev);
384 }
385 }
386 if (!ret) {
387 log_debug("- found %s\n", bdev->name);
388 *devp = bdev;
389 return 0;
390 }
391 log_debug("- no device in %s\n", media->name);
392 }
393 log_warning("Unknown seq %d for label '%s'\n", seq, label);
394
395 return -ENOENT;
396}
397
398int bootdev_find_by_any(const char *name, struct udevice **devp)
399{
400 struct udevice *dev;
401 int ret, seq;
402 char *endp;
403
404 seq = simple_strtol(name, &endp, 16);
405
406 /* Select by name, label or number */
407 if (*endp) {
408 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
409 if (ret == -ENODEV) {
410 ret = bootdev_find_by_label(name, &dev);
411 if (ret) {
412 printf("Cannot find bootdev '%s' (err=%d)\n",
413 name, ret);
414 return ret;
415 }
416 ret = device_probe(dev);
417 }
418 if (ret) {
419 printf("Cannot probe bootdev '%s' (err=%d)\n", name,
420 ret);
421 return ret;
422 }
423 } else {
424 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
425 }
426 if (ret) {
427 printf("Cannot find '%s' (err=%d)\n", name, ret);
428 return ret;
429 }
430
431 *devp = dev;
432
433 return 0;
434}
435
436int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
437 struct bootflow *bflow)
438{
439 const struct bootdev_ops *ops = bootdev_get_ops(dev);
440
441 if (!ops->get_bootflow)
442 return -ENOSYS;
443 memset(bflow, '\0', sizeof(*bflow));
444 bflow->dev = dev;
445 bflow->method = iter->method;
446 bflow->state = BOOTFLOWST_BASE;
447
448 return ops->get_bootflow(dev, iter, bflow);
449}
450
451void bootdev_clear_bootflows(struct udevice *dev)
452{
453 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
454
455 while (!list_empty(&ucp->bootflow_head)) {
456 struct bootflow *bflow;
457
458 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
459 bm_node);
Simon Glassa8f5be12022-04-24 23:31:09 -0600460 bootflow_remove(bflow);
Simon Glass201417d2022-04-24 23:31:07 -0600461 }
462}
463
464/**
465 * h_cmp_bootdev() - Compare two bootdevs to find out which should go first
466 *
467 * @v1: struct udevice * of first bootdev device
468 * @v2: struct udevice * of second bootdev device
469 * Return: sort order (<0 if dev1 < dev2, ==0 if equal, >0 if dev1 > dev2)
470 */
471static int h_cmp_bootdev(const void *v1, const void *v2)
472{
473 const struct udevice *dev1 = *(struct udevice **)v1;
474 const struct udevice *dev2 = *(struct udevice **)v2;
475 const struct bootdev_uc_plat *ucp1 = dev_get_uclass_plat(dev1);
476 const struct bootdev_uc_plat *ucp2 = dev_get_uclass_plat(dev2);
477 int diff;
478
479 /* Use priority first */
480 diff = ucp1->prio - ucp2->prio;
481 if (diff)
482 return diff;
483
484 /* Fall back to seq for devices of the same priority */
485 diff = dev_seq(dev1) - dev_seq(dev2);
486
487 return diff;
488}
489
490/**
491 * build_order() - Build the ordered list of bootdevs to use
492 *
493 * This builds an ordered list of devices by one of three methods:
494 * - using the boot_targets environment variable, if non-empty
495 * - using the bootdev-order devicetree property, if present
496 * - sorted by priority and sequence number
497 *
498 * @bootstd: BOOTSTD device to use
499 * @order: Bootdevs listed in default order
500 * @max_count: Number of entries in @order
501 * Return: number of bootdevs found in the ordering, or -E2BIG if the
502 * boot_targets string is too long, or -EXDEV if the ordering produced 0 results
503 */
504static int build_order(struct udevice *bootstd, struct udevice **order,
505 int max_count)
506{
507 const char *overflow_target = NULL;
508 const char *const *labels;
509 struct udevice *dev;
510 const char *targets;
511 int i, ret, count;
512
513 targets = env_get("boot_targets");
514 labels = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
515 bootstd_get_bootdev_order(bootstd) : NULL;
516 if (targets) {
517 char str[BOOT_TARGETS_MAX_LEN];
518 char *target;
519
520 if (strlen(targets) >= BOOT_TARGETS_MAX_LEN)
521 return log_msg_ret("len", -E2BIG);
522
523 /* make a copy of the string, since strok() will change it */
524 strcpy(str, targets);
525 for (i = 0, target = strtok(str, " "); target;
526 target = strtok(NULL, " ")) {
527 ret = bootdev_find_by_label(target, &dev);
528 if (!ret) {
529 if (i == max_count) {
530 overflow_target = target;
531 break;
532 }
533 order[i++] = dev;
534 }
535 }
536 count = i;
537 } else if (labels) {
538 int upto;
539
540 upto = 0;
541 for (i = 0; labels[i]; i++) {
542 ret = bootdev_find_by_label(labels[i], &dev);
543 if (!ret) {
544 if (upto == max_count) {
545 overflow_target = labels[i];
546 break;
547 }
548 order[upto++] = dev;
549 }
550 }
551 count = upto;
552 } else {
553 /* sort them into priority order */
554 count = max_count;
555 qsort(order, count, sizeof(struct udevice *), h_cmp_bootdev);
556 }
557
558 if (overflow_target) {
559 log_warning("Expected at most %d bootdevs, but overflowed with boot_target '%s'\n",
560 max_count, overflow_target);
561 }
562
563 if (!count)
564 return log_msg_ret("targ", -EXDEV);
565
566 return count;
567}
568
569int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp)
570{
571 struct udevice *bootstd, *dev = *devp, **order;
572 int upto, i;
573 int count;
574 int ret;
575
576 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
577 if (ret) {
578 log_err("Missing bootstd device\n");
579 return log_msg_ret("std", ret);
580 }
581
582 /* Handle scanning a single device */
583 if (dev) {
584 iter->flags |= BOOTFLOWF_SINGLE_DEV;
585 return 0;
586 }
587
588 count = uclass_id_count(UCLASS_BOOTDEV);
589 if (!count)
590 return log_msg_ret("count", -ENOENT);
591
592 order = calloc(count, sizeof(struct udevice *));
593 if (!order)
594 return log_msg_ret("order", -ENOMEM);
595
596 /*
597 * Get a list of bootdevs, in seq order (i.e. using aliases). There may
598 * be gaps so try to count up high enough to find them all.
599 */
600 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
601 ret = uclass_find_device_by_seq(UCLASS_BOOTDEV, i, &dev);
602 if (!ret)
603 order[upto++] = dev;
604 }
605 log_debug("Found %d bootdevs\n", count);
606 if (upto != count)
607 log_debug("Expected %d bootdevs, found %d using aliases\n",
608 count, upto);
609
Simon Glassa18686c2022-07-30 15:52:20 -0600610 ret = build_order(bootstd, order, upto);
611 if (ret < 0) {
Simon Glass201417d2022-04-24 23:31:07 -0600612 free(order);
Simon Glassa18686c2022-07-30 15:52:20 -0600613 return log_msg_ret("build", ret);
Simon Glass201417d2022-04-24 23:31:07 -0600614 }
615
Simon Glassa18686c2022-07-30 15:52:20 -0600616 iter->num_devs = ret;
Simon Glass201417d2022-04-24 23:31:07 -0600617 iter->dev_order = order;
Simon Glass201417d2022-04-24 23:31:07 -0600618 iter->cur_dev = 0;
619
620 dev = *order;
621 ret = device_probe(dev);
622 if (ret)
623 return log_msg_ret("probe", ret);
624 *devp = dev;
625
626 return 0;
627}
628
629static int bootdev_post_bind(struct udevice *dev)
630{
631 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
632
633 INIT_LIST_HEAD(&ucp->bootflow_head);
634
635 return 0;
636}
637
638static int bootdev_pre_unbind(struct udevice *dev)
639{
640 bootdev_clear_bootflows(dev);
641
642 return 0;
643}
644
645UCLASS_DRIVER(bootdev) = {
646 .id = UCLASS_BOOTDEV,
647 .name = "bootdev",
648 .flags = DM_UC_FLAG_SEQ_ALIAS,
649 .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
650 .post_bind = bootdev_post_bind,
651 .pre_unbind = bootdev_pre_unbind,
652};