]> Git Repo - J-u-boot.git/blob - boot/bootdev-uclass.c
Merge patch series "env: mmc: fix use of two separate partitions with proper type...
[J-u-boot.git] / boot / bootdev-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2021 Google LLC
4  * Written by Simon Glass <[email protected]>
5  */
6
7 #define LOG_CATEGORY UCLASS_BOOTSTD
8
9 #include <dm.h>
10 #include <bootdev.h>
11 #include <bootflow.h>
12 #include <bootmeth.h>
13 #include <bootstd.h>
14 #include <fs.h>
15 #include <log.h>
16 #include <malloc.h>
17 #include <part.h>
18 #include <sort.h>
19 #include <dm/device-internal.h>
20 #include <dm/lists.h>
21 #include <dm/uclass-internal.h>
22
23 enum {
24         /*
25          * Set some sort of limit on the number of partitions a bootdev can
26          * have. Note that for disks this limits the partitions numbers that
27          * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV
28          */
29         MAX_PART_PER_BOOTDEV    = 30,
30
31         /* Maximum supported length of the "boot_targets" env string */
32         BOOT_TARGETS_MAX_LEN    = 100,
33 };
34
35 int bootdev_add_bootflow(struct bootflow *bflow)
36 {
37         struct bootstd_priv *std;
38         struct bootflow *new;
39         int ret;
40
41         ret = bootstd_get_priv(&std);
42         if (ret)
43                 return ret;
44
45         new = malloc(sizeof(*bflow));
46         if (!new)
47                 return log_msg_ret("bflow", -ENOMEM);
48         memcpy(new, bflow, sizeof(*bflow));
49
50         list_add_tail(&new->glob_node, &std->glob_head);
51         if (bflow->dev) {
52                 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
53
54                 list_add_tail(&new->bm_node, &ucp->bootflow_head);
55         }
56
57         return 0;
58 }
59
60 int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
61 {
62         struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
63
64         if (list_empty(&ucp->bootflow_head))
65                 return -ENOENT;
66
67         *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
68                                    bm_node);
69
70         return 0;
71 }
72
73 int bootdev_next_bootflow(struct bootflow **bflowp)
74 {
75         struct bootflow *bflow = *bflowp;
76         struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
77
78         *bflowp = NULL;
79
80         if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
81                 return -ENOENT;
82
83         *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
84
85         return 0;
86 }
87
88 int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
89                  struct udevice **devp)
90 {
91         struct udevice *dev;
92         char dev_name[30];
93         char *str;
94         int ret;
95
96         snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
97         str = strdup(dev_name);
98         if (!str)
99                 return -ENOMEM;
100         ret = device_bind_driver(parent, drv_name, str, &dev);
101         if (ret)
102                 return ret;
103         device_set_name_alloced(dev);
104         *devp = dev;
105
106         return 0;
107 }
108
109 int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
110                         struct bootflow_iter *iter, struct bootflow *bflow)
111 {
112         struct bootmeth_uc_plat *plat = dev_get_uclass_plat(bflow->method);
113         bool allow_any_part = plat->flags & BOOTMETHF_ANY_PART;
114         struct blk_desc *desc = dev_get_uclass_plat(blk);
115         struct disk_partition info;
116         char partstr[20];
117         char name[60];
118         int ret;
119
120         /* Sanity check */
121         if (iter->part >= MAX_PART_PER_BOOTDEV)
122                 return log_msg_ret("max", -ESHUTDOWN);
123
124         bflow->blk = blk;
125         if (iter->part)
126                 snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
127         else
128                 strcpy(partstr, "whole");
129         snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
130         bflow->name = strdup(name);
131         if (!bflow->name)
132                 return log_msg_ret("name", -ENOMEM);
133
134         bflow->part = iter->part;
135
136         ret = bootmeth_check(bflow->method, iter);
137         if (ret)
138                 return log_msg_ret("check", ret);
139
140         /*
141          * partition numbers start at 0 so this cannot succeed, but it can tell
142          * us whether there is valid media there
143          */
144         ret = part_get_info(desc, iter->part, &info);
145         log_debug("part_get_info() returned %d\n", ret);
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 && !allow_any_part) {
159                 /* allow partition 1 to be missing */
160                 if (iter->part == 1) {
161                         iter->max_part = 3;
162                         ret = -ENOENT;
163                 }
164
165                 return log_msg_ret("part", ret);
166         }
167
168         /*
169          * Currently we don't get the number of partitions, so just
170          * assume a large number
171          */
172         iter->max_part = MAX_PART_PER_BOOTDEV;
173
174         if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) {
175                 /* a particular partition was specified, scan it without checking */
176         } else if (!iter->part) {
177                 /* This is the whole disk, check if we have bootable partitions */
178                 iter->first_bootable = part_get_bootable(desc);
179                 log_debug("checking bootable=%d\n", iter->first_bootable);
180         } else if (allow_any_part) {
181                 /*
182                  * allow any partition to be scanned, by skipping any checks
183                  * for filesystems or partition contents on this disk
184                  */
185
186         /* if there are bootable partitions, scan only those */
187         } else if (iter->first_bootable >= 0 &&
188                    (iter->first_bootable ? !info.bootable : iter->part != 1)) {
189                 return log_msg_ret("boot", -EINVAL);
190         } else {
191                 ret = fs_set_blk_dev_with_part(desc, bflow->part);
192                 bflow->state = BOOTFLOWST_PART;
193                 if (ret)
194                         return log_msg_ret("fs", ret);
195
196                 log_debug("%s: Found partition %x type %x fstype %d\n",
197                           blk->name, bflow->part,
198                           IS_ENABLED(CONFIG_DOS_PARTITION) ?
199                           disk_partition_sys_ind(&info) : 0,
200                           ret ? -1 : fs_get_type());
201                 bflow->blk = blk;
202                 bflow->state = BOOTFLOWST_FS;
203         }
204
205         log_debug("method %s\n", bflow->method->name);
206         ret = bootmeth_read_bootflow(bflow->method, bflow);
207         if (ret)
208                 return log_msg_ret("method", ret);
209
210         return 0;
211 }
212
213 void bootdev_list(bool probe)
214 {
215         struct udevice *dev;
216         int ret;
217         int i;
218
219         printf("Seq  Probed  Status  Uclass    Name\n");
220         printf("---  ------  ------  --------  ------------------\n");
221         if (probe)
222                 ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
223         else
224                 ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
225         for (i = 0; dev; i++) {
226                 printf("%3x   [ %c ]  %6s  %-9.9s %s\n", dev_seq(dev),
227                        device_active(dev) ? '+' : ' ',
228                        ret ? simple_itoa(-ret) : "OK",
229                        dev_get_uclass_name(dev_get_parent(dev)), dev->name);
230                 if (probe)
231                         ret = uclass_next_device_check(&dev);
232                 else
233                         ret = uclass_find_next_device(&dev);
234         }
235         printf("---  ------  ------  --------  ------------------\n");
236         printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
237 }
238
239 int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
240 {
241         struct udevice *bdev;
242         int ret;
243
244         ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
245                                                 &bdev);
246         if (ret) {
247                 if (ret != -ENODEV) {
248                         log_debug("Cannot access bootdev device\n");
249                         return ret;
250                 }
251
252                 ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
253                 if (ret) {
254                         log_debug("Cannot create bootdev device\n");
255                         return ret;
256                 }
257         }
258
259         return 0;
260 }
261
262 static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
263 {
264         int len, slen;
265
266         len = strlen(dev->name);
267         slen = strlen(suffix);
268         if (len > slen && !strcmp(suffix, dev->name + len - slen))
269                 return len - slen;
270
271         return len;
272 }
273
274 int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
275 {
276         struct udevice *parent, *dev;
277         char dev_name[50];
278         int ret, len;
279
280         len = bootdev_get_suffix_start(blk, ".blk");
281         snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
282                  "bootdev");
283
284         parent = dev_get_parent(blk);
285         ret = device_find_child_by_name(parent, dev_name, &dev);
286         if (ret) {
287                 char *str;
288
289                 if (ret != -ENODEV) {
290                         log_debug("Cannot access bootdev device\n");
291                         return ret;
292                 }
293                 str = strdup(dev_name);
294                 if (!str)
295                         return -ENOMEM;
296
297                 ret = device_bind_driver(parent, drv_name, str, &dev);
298                 if (ret) {
299                         log_debug("Cannot create bootdev device\n");
300                         return ret;
301                 }
302                 device_set_name_alloced(dev);
303         }
304
305         return 0;
306 }
307
308 int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
309 {
310         struct udevice *parent = dev_get_parent(dev);
311         struct udevice *blk;
312         int ret, len;
313
314         if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
315                 return -EINVAL;
316
317         /*
318          * This should always work if bootdev_setup_for_sibling_blk() was used
319          */
320         len = bootdev_get_suffix_start(dev, ".bootdev");
321         ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
322         if (ret) {
323                 char dev_name[50];
324
325                 snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
326                          dev->name);
327                 ret = device_find_child_by_name(parent, dev_name, &blk);
328                 if (ret)
329                         return log_msg_ret("find", ret);
330         }
331         ret = device_probe(blk);
332         if (ret)
333                 return log_msg_ret("act", ret);
334         *blkp = blk;
335
336         return 0;
337 }
338
339 static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
340 {
341         struct udevice *parent = dev_get_parent(blk);
342         struct udevice *bootdev;
343         char dev_name[50];
344         int ret, len;
345
346         if (device_get_uclass_id(blk) != UCLASS_BLK)
347                 return -EINVAL;
348
349         /* This should always work if bootdev_setup_for_sibling_blk() was used */
350         len = bootdev_get_suffix_start(blk, ".blk");
351         snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
352                  "bootdev");
353         ret = device_find_child_by_name(parent, dev_name, &bootdev);
354         if (ret)
355                 return log_msg_ret("find", ret);
356         *bootdevp = bootdev;
357
358         return 0;
359 }
360
361 int bootdev_unbind_dev(struct udevice *parent)
362 {
363         struct udevice *dev;
364         int ret;
365
366         ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
367         if (!ret) {
368                 ret = device_remove(dev, DM_REMOVE_NORMAL);
369                 if (ret)
370                         return log_msg_ret("rem", ret);
371                 ret = device_unbind(dev);
372                 if (ret)
373                         return log_msg_ret("unb", ret);
374         }
375
376         return 0;
377 }
378
379 /**
380  * label_to_uclass() - Convert a label to a uclass and sequence number
381  *
382  * @label: Label to look up (e.g. "mmc1" or "mmc0")
383  * @seqp: Returns the sequence number, or -1 if none
384  * @method_flagsp: If non-NULL, returns any flags implied by the label
385  * (enum bootflow_meth_flags_t), 0 if none
386  * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
387  * known, other -ve error code on other error
388  */
389 static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
390 {
391         int seq, len, method_flags;
392         enum uclass_id id;
393         const char *end;
394
395         method_flags = 0;
396         seq = trailing_strtoln_end(label, NULL, &end);
397         len = end - label;
398         if (!len)
399                 return -EINVAL;
400         id = uclass_get_by_namelen(label, len);
401         log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
402                   uclass_get_name(id));
403         if (id == UCLASS_INVALID) {
404                 /* try some special cases */
405                 if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
406                     !strncmp("spi", label, len)) {
407                         id = UCLASS_SPI_FLASH;
408                 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
409                     !strncmp("pxe", label, len)) {
410                         id = UCLASS_ETH;
411                         method_flags |= BOOTFLOW_METHF_PXE_ONLY;
412                 } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
413                     !strncmp("dhcp", label, len)) {
414                         id = UCLASS_ETH;
415                         method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
416                 } else {
417                         return -EPFNOSUPPORT;
418                 }
419         }
420         if (id == UCLASS_USB)
421                 id = UCLASS_MASS_STORAGE;
422         *seqp = seq;
423         if (method_flagsp)
424                 *method_flagsp = method_flags;
425
426         return id;
427 }
428
429 int bootdev_find_by_label(const char *label, struct udevice **devp,
430                           int *method_flagsp)
431 {
432         int seq, ret, method_flags = 0;
433         struct udevice *media;
434         struct uclass *uc;
435         enum uclass_id id;
436
437         ret = label_to_uclass(label, &seq, &method_flags);
438         if (ret < 0)
439                 return log_msg_ret("uc", ret);
440         id = ret;
441
442         /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
443         uclass_id_foreach_dev(id, media, uc) {
444                 struct udevice *bdev, *blk;
445                 int ret;
446
447                 /* if there is no seq, match anything */
448                 if (seq != -1 && dev_seq(media) != seq) {
449                         log_debug("- skip, media seq=%d\n", dev_seq(media));
450                         continue;
451                 }
452
453                 ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
454                                                         &bdev);
455                 if (ret) {
456                         log_debug("- looking via blk, seq=%d, id=%d\n", seq,
457                                   id);
458                         ret = blk_find_device(id, seq, &blk);
459                         if (!ret) {
460                                 log_debug("- get from blk %s\n", blk->name);
461                                 ret = bootdev_get_from_blk(blk, &bdev);
462                         }
463                 }
464                 if (!ret) {
465                         log_debug("- found %s\n", bdev->name);
466                         *devp = bdev;
467
468                         /*
469                          * if no sequence number was provided, we must scan all
470                          * bootdevs for this media uclass
471                          */
472                         if (seq == -1)
473                                 method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
474                         if (method_flagsp)
475                                 *method_flagsp = method_flags;
476                         log_debug("method flags %x\n", method_flags);
477                         return 0;
478                 }
479                 log_debug("- no device in %s\n", media->name);
480         }
481
482         return -ENOENT;
483 }
484
485 int bootdev_find_by_any(const char *name, struct udevice **devp,
486                         int *method_flagsp)
487 {
488         struct udevice *dev;
489         int method_flags = 0;
490         int ret = -ENODEV, seq;
491         char *endp;
492
493         seq = simple_strtol(name, &endp, 16);
494
495         /* Select by name, label or number */
496         if (*endp) {
497                 ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
498                 if (ret == -ENODEV) {
499                         ret = bootdev_find_by_label(name, &dev, &method_flags);
500                         if (ret) {
501                                 printf("Cannot find bootdev '%s' (err=%d)\n",
502                                        name, ret);
503                                 return log_msg_ret("lab", ret);
504                         }
505                         ret = device_probe(dev);
506                 }
507                 if (ret) {
508                         printf("Cannot probe bootdev '%s' (err=%d)\n", name,
509                                ret);
510                         return log_msg_ret("pro", ret);
511                 }
512         } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
513                 ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
514                 method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
515         }
516         if (ret) {
517                 printf("Cannot find '%s' (err=%d)\n", name, ret);
518                 return ret;
519         }
520
521         *devp = dev;
522         if (method_flagsp)
523                 *method_flagsp = method_flags;
524
525         return 0;
526 }
527
528 int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
529                                    int *method_flagsp)
530 {
531         int ret;
532
533         ret = bootdev_hunt(label, false);
534         if (ret)
535                 return log_msg_ret("scn", ret);
536         ret = bootdev_find_by_label(label, devp, method_flagsp);
537         if (ret)
538                 return log_msg_ret("fnd", ret);
539
540         return 0;
541 }
542
543 static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
544                                 struct bootflow *bflow)
545 {
546         struct udevice *blk;
547         int ret;
548
549         ret = bootdev_get_sibling_blk(dev, &blk);
550         log_debug("sibling_blk ret=%d, blk=%s\n", ret,
551                   ret ? "(none)" : blk->name);
552         /*
553          * If there is no media, indicate that no more partitions should be
554          * checked
555          */
556         if (ret == -EOPNOTSUPP)
557                 ret = -ESHUTDOWN;
558         if (ret)
559                 return log_msg_ret("blk", ret);
560         assert(blk);
561         ret = bootdev_find_in_blk(dev, blk, iter, bflow);
562         if (ret)
563                 return log_msg_ret("find", ret);
564
565         return 0;
566 }
567
568 int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
569                          struct bootflow *bflow)
570 {
571         const struct bootdev_ops *ops = bootdev_get_ops(dev);
572
573         log_debug("->get_bootflow %s,%x=%p\n", dev->name, iter->part,
574                   ops->get_bootflow);
575         bootflow_init(bflow, dev, iter->method);
576         if (!ops->get_bootflow)
577                 return default_get_bootflow(dev, iter, bflow);
578
579         return ops->get_bootflow(dev, iter, bflow);
580 }
581
582 void bootdev_clear_bootflows(struct udevice *dev)
583 {
584         struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
585
586         while (!list_empty(&ucp->bootflow_head)) {
587                 struct bootflow *bflow;
588
589                 bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
590                                          bm_node);
591                 bootflow_remove(bflow);
592         }
593 }
594
595 int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
596                        int *method_flagsp)
597 {
598         struct udevice *dev;
599
600         log_debug("next\n");
601         for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
602                 const char *label = iter->labels[iter->cur_label];
603                 int ret;
604
605                 log_debug("Scanning: %s\n", label);
606                 ret = bootdev_hunt_and_find_by_label(label, &dev,
607                                                      method_flagsp);
608                 if (iter->flags & BOOTFLOWIF_SHOW) {
609                         if (ret == -EPFNOSUPPORT) {
610                                 log_warning("Unknown uclass '%s' in label\n",
611                                             label);
612                         } else if (ret == -ENOENT) {
613                                 /*
614                                  * looking for, e.g. 'scsi0' should find
615                                  * something if SCSI is present
616                                  */
617                                 if (!trailing_strtol(label)) {
618                                         log_warning("No bootdevs for '%s'\n",
619                                                     label);
620                                 }
621                         }
622                 }
623
624         }
625
626         if (!dev)
627                 return log_msg_ret("fin", -ENODEV);
628         *devp = dev;
629
630         return 0;
631 }
632
633 int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
634 {
635         struct udevice *dev = *devp;
636         bool found;
637         int ret;
638
639         /* find the next device with this priority */
640         *devp = NULL;
641         log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
642                   dev ? dev->name : "none");
643         found = false;
644         do {
645                 /*
646                  * Don't probe devices here since they may not be of the
647                  * required priority
648                  */
649                 if (!dev)
650                         uclass_find_first_device(UCLASS_BOOTDEV, &dev);
651                 else
652                         uclass_find_next_device(&dev);
653                 found = false;
654
655                 /* scan for the next device with the correct priority */
656                 while (dev) {
657                         struct bootdev_uc_plat *plat;
658
659                         plat = dev_get_uclass_plat(dev);
660                         log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
661                                   iter->cur_prio);
662                         if (plat->prio == iter->cur_prio)
663                                 break;
664                         uclass_find_next_device(&dev);
665                 }
666
667                 /* none found for this priority, so move to the next */
668                 if (!dev) {
669                         log_debug("None found at prio %d, moving to %d\n",
670                                   iter->cur_prio, iter->cur_prio + 1);
671                         if (++iter->cur_prio == BOOTDEVP_COUNT)
672                                 return log_msg_ret("fin", -ENODEV);
673
674                         if (iter->flags & BOOTFLOWIF_HUNT) {
675                                 /* hunt to find new bootdevs */
676                                 ret = bootdev_hunt_prio(iter->cur_prio,
677                                                         iter->flags &
678                                                         BOOTFLOWIF_SHOW);
679                                 log_debug("- bootdev_hunt_prio() ret %d\n",
680                                           ret);
681                                 if (ret)
682                                         return log_msg_ret("hun", ret);
683                         }
684                 } else {
685                         ret = device_probe(dev);
686                         if (ret)
687                                 log_debug("Device '%s' failed to probe\n",
688                                           dev->name);
689                         else
690                                 found = true;
691                 }
692         } while (!found);
693
694         *devp = dev;
695
696         return 0;
697 }
698
699 int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
700                        struct udevice **devp, int *method_flagsp)
701 {
702         struct udevice *bootstd, *dev = NULL;
703         bool show = iter->flags & BOOTFLOWIF_SHOW;
704         int method_flags;
705         char buf[32];
706         int ret;
707
708         if (label) {
709                 const char *end = strchr(label, ':');
710
711                 if (end) {
712                         size_t len = (size_t)(end - label);
713                         const char *part = end + 1;
714
715                         if (len + 1 > sizeof(buf)) {
716                                 log_err("label \"%s\" is way too long\n", label);
717                                 return -EINVAL;
718                         }
719
720                         memcpy(buf, label, len);
721                         buf[len] = '\0';
722                         label = buf;
723
724                         unsigned long tmp;
725
726                         if (strict_strtoul(part, 0, &tmp)) {
727                                 log_err("Invalid partition number: %s\n", part);
728                                 return -EINVAL;
729                         }
730
731                         iter->flags |= BOOTFLOWIF_SINGLE_PARTITION;
732                         iter->part = tmp;
733                 }
734         }
735
736         ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
737         if (ret) {
738                 log_err("Missing bootstd device\n");
739                 return log_msg_ret("std", ret);
740         }
741
742         /* hunt for any pre-scan devices */
743         if (iter->flags & BOOTFLOWIF_HUNT) {
744                 ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
745                 log_debug("- bootdev_hunt_prio() ret %d\n", ret);
746                 if (ret)
747                         return log_msg_ret("pre", ret);
748         }
749
750         /* Handle scanning a single device */
751         if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
752                 if (iter->flags & BOOTFLOWIF_HUNT) {
753                         ret = bootdev_hunt(label, show);
754                         if (ret)
755                                 return log_msg_ret("hun", ret);
756                 }
757                 ret = bootdev_find_by_any(label, &dev, &method_flags);
758                 if (ret)
759                         return log_msg_ret("lab", ret);
760
761                 log_debug("method_flags: %x\n", method_flags);
762                 if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
763                         iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
764                 else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
765                         iter->flags |= BOOTFLOWIF_SINGLE_DEV;
766                 else
767                         iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
768                 log_debug("Selected label: %s, flags %x\n", label, iter->flags);
769         } else {
770                 bool ok;
771
772                 /* This either returns a non-empty list or NULL */
773                 iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
774                 if (!ok)
775                         return log_msg_ret("ord", -ENOMEM);
776                 log_debug("setup labels %p\n", iter->labels);
777                 if (iter->labels) {
778                         iter->cur_label = -1;
779                         ret = bootdev_next_label(iter, &dev, &method_flags);
780                 } else {
781                         ret = bootdev_next_prio(iter, &dev);
782                         method_flags = 0;
783                 }
784                 if (!dev)
785                         return log_msg_ret("fin", -ENOENT);
786                 log_debug("Selected bootdev: %s\n", dev->name);
787         }
788
789         ret = device_probe(dev);
790         if (ret)
791                 return log_msg_ret("probe", ret);
792         if (method_flagsp)
793                 *method_flagsp = method_flags;
794         *devp = dev;
795
796         return 0;
797 }
798
799 static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
800 {
801         const char *name = uclass_get_name(info->uclass);
802         struct bootstd_priv *std;
803         int ret;
804
805         ret = bootstd_get_priv(&std);
806         if (ret)
807                 return log_msg_ret("std", ret);
808
809         if (!(std->hunters_used & BIT(seq))) {
810                 if (show)
811                         printf("Hunting with: %s\n",
812                                uclass_get_name(info->uclass));
813                 log_debug("Hunting with: %s\n", name);
814                 if (info->hunt) {
815                         ret = info->hunt(info, show);
816                         log_debug("  - hunt result %d\n", ret);
817                         if (ret && ret != -ENOENT)
818                                 return ret;
819                 }
820                 std->hunters_used |= BIT(seq);
821         }
822
823         return 0;
824 }
825
826 int bootdev_hunt(const char *spec, bool show)
827 {
828         struct bootdev_hunter *start;
829         const char *end;
830         int n_ent, i;
831         int result;
832         size_t len;
833
834         start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
835         n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
836         result = 0;
837
838         len = SIZE_MAX;
839         if (spec) {
840                 trailing_strtoln_end(spec, NULL, &end);
841                 len = end - spec;
842         }
843
844         for (i = 0; i < n_ent; i++) {
845                 struct bootdev_hunter *info = start + i;
846                 const char *name = uclass_get_name(info->uclass);
847                 int ret;
848
849                 log_debug("looking at %.*s for %s\n",
850                           (int)max(strlen(name), len), spec, name);
851                 if (spec && strncmp(spec, name, max(strlen(name), len))) {
852                         if (info->uclass != UCLASS_ETH ||
853                             (strcmp("dhcp", spec) && strcmp("pxe", spec)))
854                                 continue;
855                 }
856                 ret = bootdev_hunt_drv(info, i, show);
857                 if (ret)
858                         result = ret;
859         }
860
861         return result;
862 }
863
864 int bootdev_unhunt(enum uclass_id id)
865 {
866         struct bootdev_hunter *start;
867         int n_ent, i;
868
869         start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
870         n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
871         for (i = 0; i < n_ent; i++) {
872                 struct bootdev_hunter *info = start + i;
873
874                 if (info->uclass == id) {
875                         struct bootstd_priv *std;
876                         int ret;
877
878                         ret = bootstd_get_priv(&std);
879                         if (ret)
880                                 return log_msg_ret("std", ret);
881                         if (!(std->hunters_used & BIT(i)))
882                                 return -EALREADY;
883                         std->hunters_used &= ~BIT(i);
884                         return 0;
885                 }
886         }
887
888         return -ENOENT;
889 }
890
891 int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
892 {
893         struct bootdev_hunter *start;
894         int n_ent, i;
895         int result;
896
897         start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
898         n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
899         result = 0;
900
901         log_debug("Hunting for priority %d\n", prio);
902         for (i = 0; i < n_ent; i++) {
903                 struct bootdev_hunter *info = start + i;
904                 int ret;
905
906                 if (prio != info->prio)
907                         continue;
908                 ret = bootdev_hunt_drv(info, i, show);
909                 log_debug("bootdev_hunt_drv() return %d\n", ret);
910                 if (ret && ret != -ENOENT)
911                         result = ret;
912         }
913         log_debug("exit %d\n", result);
914
915         return result;
916 }
917
918 void bootdev_list_hunters(struct bootstd_priv *std)
919 {
920         struct bootdev_hunter *orig, *start;
921         int n_ent, i;
922
923         orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
924         n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
925
926         /*
927          * workaround for strange bug in clang-12 which sees all the below data
928          * as zeroes. Any access of start seems to fix it, such as
929          *
930          *    printf("%p", start);
931          *
932          * Use memcpy() to force the correct behaviour.
933          */
934         memcpy(&start, &orig, sizeof(orig));
935         printf("%4s  %4s  %-15s  %s\n", "Prio", "Used", "Uclass", "Hunter");
936         printf("%4s  %4s  %-15s  %s\n", "----", "----", "---------------", "---------------");
937         for (i = 0; i < n_ent; i++) {
938                 struct bootdev_hunter *info = start + i;
939
940                 printf("%4d  %4s  %-15s  %s\n", info->prio,
941                        std->hunters_used & BIT(i) ? "*" : "",
942                        uclass_get_name(info->uclass),
943                        info->drv ? info->drv->name : "(none)");
944         }
945
946         printf("(total hunters: %d)\n", n_ent);
947 }
948
949 static int bootdev_post_bind(struct udevice *dev)
950 {
951         struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
952
953         INIT_LIST_HEAD(&ucp->bootflow_head);
954
955         return 0;
956 }
957
958 static int bootdev_pre_unbind(struct udevice *dev)
959 {
960         bootdev_clear_bootflows(dev);
961
962         return 0;
963 }
964
965 UCLASS_DRIVER(bootdev) = {
966         .id             = UCLASS_BOOTDEV,
967         .name           = "bootdev",
968         .flags          = DM_UC_FLAG_SEQ_ALIAS,
969         .per_device_plat_auto   = sizeof(struct bootdev_uc_plat),
970         .post_bind      = bootdev_post_bind,
971         .pre_unbind     = bootdev_pre_unbind,
972 };
This page took 0.083755 seconds and 4 git commands to generate.