]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/efi/libefi/efipart.c
Import DTS files for riscv from Linux 5.4
[FreeBSD/FreeBSD.git] / stand / efi / libefi / efipart.c
1 /*-
2  * Copyright (c) 2010 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/disk.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/queue.h>
34 #include <stddef.h>
35 #include <stdarg.h>
36
37 #include <bootstrap.h>
38
39 #include <efi.h>
40 #include <efilib.h>
41 #include <efiprot.h>
42 #include <efichar.h>
43 #include <disk.h>
44
45 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
46
47 typedef bool (*pd_test_cb_t)(pdinfo_t *, pdinfo_t *);
48 static int efipart_initfd(void);
49 static int efipart_initcd(void);
50 static int efipart_inithd(void);
51 static void efipart_cdinfo_add(pdinfo_t *);
52
53 static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *);
54 static int efipart_realstrategy(void *, int, daddr_t, size_t, char *, size_t *);
55
56 static int efipart_open(struct open_file *, ...);
57 static int efipart_close(struct open_file *);
58 static int efipart_ioctl(struct open_file *, u_long, void *);
59
60 static int efipart_printfd(int);
61 static int efipart_printcd(int);
62 static int efipart_printhd(int);
63
64 /* EISA PNP ID's for floppy controllers */
65 #define PNP0604 0x604
66 #define PNP0700 0x700
67 #define PNP0701 0x701
68
69 /* Bounce buffer max size */
70 #define BIO_BUFFER_SIZE 0x4000
71
72 struct devsw efipart_fddev = {
73         .dv_name = "fd",
74         .dv_type = DEVT_FD,
75         .dv_init = efipart_initfd,
76         .dv_strategy = efipart_strategy,
77         .dv_open = efipart_open,
78         .dv_close = efipart_close,
79         .dv_ioctl = efipart_ioctl,
80         .dv_print = efipart_printfd,
81         .dv_cleanup = NULL
82 };
83
84 struct devsw efipart_cddev = {
85         .dv_name = "cd",
86         .dv_type = DEVT_CD,
87         .dv_init = efipart_initcd,
88         .dv_strategy = efipart_strategy,
89         .dv_open = efipart_open,
90         .dv_close = efipart_close,
91         .dv_ioctl = efipart_ioctl,
92         .dv_print = efipart_printcd,
93         .dv_cleanup = NULL
94 };
95
96 struct devsw efipart_hddev = {
97         .dv_name = "disk",
98         .dv_type = DEVT_DISK,
99         .dv_init = efipart_inithd,
100         .dv_strategy = efipart_strategy,
101         .dv_open = efipart_open,
102         .dv_close = efipart_close,
103         .dv_ioctl = efipart_ioctl,
104         .dv_print = efipart_printhd,
105         .dv_cleanup = NULL
106 };
107
108 static pdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo);
109 static pdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo);
110 static pdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
111
112 /*
113  * efipart_inithandles() is used to build up the pdinfo list from
114  * block device handles. Then each devsw init callback is used to
115  * pick items from pdinfo and move to proper device list.
116  * In ideal world, we should end up with empty pdinfo once all
117  * devsw initializers are called.
118  */
119 static pdinfo_list_t pdinfo = STAILQ_HEAD_INITIALIZER(pdinfo);
120
121 pdinfo_list_t *
122 efiblk_get_pdinfo_list(struct devsw *dev)
123 {
124         if (dev->dv_type == DEVT_DISK)
125                 return (&hdinfo);
126         if (dev->dv_type == DEVT_CD)
127                 return (&cdinfo);
128         if (dev->dv_type == DEVT_FD)
129                 return (&fdinfo);
130         return (NULL);
131 }
132
133 /* XXX this gets called way way too often, investigate */
134 pdinfo_t *
135 efiblk_get_pdinfo(struct devdesc *dev)
136 {
137         pdinfo_list_t *pdi;
138         pdinfo_t *pd = NULL;
139
140         pdi = efiblk_get_pdinfo_list(dev->d_dev);
141         if (pdi == NULL)
142                 return (pd);
143
144         STAILQ_FOREACH(pd, pdi, pd_link) {
145                 if (pd->pd_unit == dev->d_unit)
146                         return (pd);
147         }
148         return (pd);
149 }
150
151 pdinfo_t *
152 efiblk_get_pdinfo_by_device_path(EFI_DEVICE_PATH *path)
153 {
154         EFI_HANDLE h;
155         EFI_STATUS status;
156         EFI_DEVICE_PATH *devp = path;
157
158         status = BS->LocateDevicePath(&blkio_guid, &devp, &h);
159         if (EFI_ERROR(status))
160                 return (NULL);
161         return (efiblk_get_pdinfo_by_handle(h));
162 }
163
164 static bool
165 same_handle(pdinfo_t *pd, EFI_HANDLE h)
166 {
167
168         return (pd->pd_handle == h || pd->pd_alias == h);
169 }
170
171 pdinfo_t *
172 efiblk_get_pdinfo_by_handle(EFI_HANDLE h)
173 {
174         pdinfo_t *dp, *pp;
175
176         /*
177          * Check hard disks, then cd, then floppy
178          */
179         STAILQ_FOREACH(dp, &hdinfo, pd_link) {
180                 if (same_handle(dp, h))
181                         return (dp);
182                 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
183                         if (same_handle(pp, h))
184                                 return (pp);
185                 }
186         }
187         STAILQ_FOREACH(dp, &cdinfo, pd_link) {
188                 if (same_handle(dp, h))
189                         return (dp);
190                 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
191                         if (same_handle(pp, h))
192                                 return (pp);
193                 }
194         }
195         STAILQ_FOREACH(dp, &fdinfo, pd_link) {
196                 if (same_handle(dp, h))
197                         return (dp);
198         }
199         return (NULL);
200 }
201
202 static int
203 efiblk_pdinfo_count(pdinfo_list_t *pdi)
204 {
205         pdinfo_t *pd;
206         int i = 0;
207
208         STAILQ_FOREACH(pd, pdi, pd_link) {
209                 i++;
210         }
211         return (i);
212 }
213
214 static pdinfo_t *
215 efipart_find_parent(pdinfo_list_t *pdi, EFI_DEVICE_PATH *devpath)
216 {
217         pdinfo_t *pd;
218         EFI_DEVICE_PATH *parent;
219
220         /* We want to find direct parent */
221         parent = efi_devpath_trim(devpath);
222         /* We should not get out of memory here but be careful. */
223         if (parent == NULL)
224                 return (NULL);
225
226         STAILQ_FOREACH(pd, pdi, pd_link) {
227                 /* We must have exact match. */
228                 if (efi_devpath_match(pd->pd_devpath, parent))
229                         break;
230         }
231         free(parent);
232         return (pd);
233 }
234
235 /*
236  * Return true when we should ignore this device.
237  */
238 static bool
239 efipart_ignore_device(EFI_HANDLE h, EFI_BLOCK_IO *blkio,
240     EFI_DEVICE_PATH *devpath)
241 {
242         EFI_DEVICE_PATH *node, *parent;
243
244         /*
245          * We assume the block size 512 or greater power of 2.
246          * Also skip devices with block size > 64k (16 is max
247          * ashift supported by zfs).
248          * iPXE is known to insert stub BLOCK IO device with
249          * BlockSize 1.
250          */
251         if (blkio->Media->BlockSize < 512 ||
252             blkio->Media->BlockSize > (1 << 16) ||
253             !powerof2(blkio->Media->BlockSize)) {
254                 efi_close_devpath(h);
255                 return (true);
256         }
257
258         /* Allowed values are 0, 1 and power of 2. */
259         if (blkio->Media->IoAlign > 1 &&
260             !powerof2(blkio->Media->IoAlign)) {
261                 efi_close_devpath(h);
262                 return (true);
263         }
264
265         /*
266          * With device tree setup:
267          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)
268          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x1)
269          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x2)
270          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x3)
271          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x3)/CDROM..
272          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x3)/CDROM..
273          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x4)
274          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x5)
275          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x6)
276          * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x7)
277          *
278          * In above exmple only Unit(0x3) has media, all other nodes are
279          * missing media and should not be used.
280          *
281          * No media does not always mean there is no device, but in above
282          * case, we can not really assume there is any device.
283          * Therefore, if this node is USB, or this node is Unit (LUN) and
284          * direct parent is USB and we have no media, we will ignore this
285          * device.
286          *
287          * Variation of the same situation, but with SCSI devices:
288          * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x1)
289          * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x2)
290          * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x3)
291          * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x3)/CD..
292          * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x3)/CD..
293          * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x4)
294          *
295          * Here above the SCSI luns 1,2 and 4 have no media.
296          */
297
298         /* Do not ignore device with media. */
299         if (blkio->Media->MediaPresent)
300                 return (false);
301
302         node = efi_devpath_last_node(devpath);
303         if (node == NULL)
304                 return (false);
305
306         /* USB without media present */
307         if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
308             DevicePathSubType(node) == MSG_USB_DP) {
309                 efi_close_devpath(h);
310                 return (true);
311         }
312
313         parent = efi_devpath_trim(devpath);
314         if (parent != NULL) {
315                 bool parent_is_usb = false;
316
317                 node = efi_devpath_last_node(parent);
318                 if (node == NULL) {
319                         free(parent);
320                         return (false);
321                 }
322                 if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
323                     DevicePathSubType(node) == MSG_USB_DP)
324                         parent_is_usb = true;
325                 free(parent);
326
327                 /* no media, parent is USB and devicepath is lun. */
328                 node = efi_devpath_last_node(devpath);
329                 if (node == NULL)
330                         return (false);
331                 if (parent_is_usb &&
332                     DevicePathType(node) == MESSAGING_DEVICE_PATH &&
333                     DevicePathSubType(node) == MSG_DEVICE_LOGICAL_UNIT_DP) {
334                         efi_close_devpath(h);
335                         return (true);
336                 }
337                 /* no media, parent is USB and devicepath is SCSI. */
338                 if (parent_is_usb &&
339                     DevicePathType(node) == MESSAGING_DEVICE_PATH &&
340                     DevicePathSubType(node) == MSG_SCSI_DP) {
341                         efi_close_devpath(h);
342                         return (true);
343                 }
344         }
345         return (false);
346 }
347
348 int
349 efipart_inithandles(void)
350 {
351         unsigned i, nin;
352         UINTN sz;
353         EFI_HANDLE *hin;
354         EFI_DEVICE_PATH *devpath;
355         EFI_BLOCK_IO *blkio;
356         EFI_STATUS status;
357         pdinfo_t *pd;
358
359         if (!STAILQ_EMPTY(&pdinfo))
360                 return (0);
361
362         sz = 0;
363         hin = NULL;
364         status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, hin);
365         if (status == EFI_BUFFER_TOO_SMALL) {
366                 hin = malloc(sz);
367                 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
368                     hin);
369                 if (EFI_ERROR(status))
370                         free(hin);
371         }
372         if (EFI_ERROR(status))
373                 return (efi_status_to_errno(status));
374
375         nin = sz / sizeof(*hin);
376 #ifdef EFIPART_DEBUG
377         printf("%s: Got %d BLOCK IO MEDIA handle(s)\n", __func__, nin);
378 #endif
379
380         for (i = 0; i < nin; i++) {
381                 /*
382                  * Get devpath and open protocol.
383                  * We should not get errors here
384                  */
385                 if ((devpath = efi_lookup_devpath(hin[i])) == NULL)
386                         continue;
387
388                 status = OpenProtocolByHandle(hin[i], &blkio_guid,
389                     (void **)&blkio);
390                 if (EFI_ERROR(status)) {
391                         printf("error %lu\n", EFI_ERROR_CODE(status));
392                         continue;
393                 }
394
395                 if (efipart_ignore_device(hin[i], blkio, devpath))
396                         continue;
397
398                 /* This is bad. */
399                 if ((pd = calloc(1, sizeof(*pd))) == NULL) {
400                         printf("efipart_inithandles: Out of memory.\n");
401                         free(hin);
402                         return (ENOMEM);
403                 }
404                 STAILQ_INIT(&pd->pd_part);
405
406                 pd->pd_handle = hin[i];
407                 pd->pd_devpath = devpath;
408                 pd->pd_blkio = blkio;
409                 STAILQ_INSERT_TAIL(&pdinfo, pd, pd_link);
410         }
411
412         /*
413          * Walk pdinfo and set parents based on device path.
414          */
415         STAILQ_FOREACH(pd, &pdinfo, pd_link) {
416                 pd->pd_parent = efipart_find_parent(&pdinfo, pd->pd_devpath);
417         }
418         free(hin);
419         return (0);
420 }
421
422 /*
423  * Get node identified by pd_test() from plist.
424  */
425 static pdinfo_t *
426 efipart_get_pd(pdinfo_list_t *plist, pd_test_cb_t pd_test, pdinfo_t *data)
427 {
428         pdinfo_t *pd;
429
430         STAILQ_FOREACH(pd, plist, pd_link) {
431                 if (pd_test(pd, data))
432                         break;
433         }
434
435         return (pd);
436 }
437
438 static ACPI_HID_DEVICE_PATH *
439 efipart_floppy(EFI_DEVICE_PATH *node)
440 {
441         ACPI_HID_DEVICE_PATH *acpi;
442
443         if (DevicePathType(node) == ACPI_DEVICE_PATH &&
444             DevicePathSubType(node) == ACPI_DP) {
445                 acpi = (ACPI_HID_DEVICE_PATH *) node;
446                 if (acpi->HID == EISA_PNP_ID(PNP0604) ||
447                     acpi->HID == EISA_PNP_ID(PNP0700) ||
448                     acpi->HID == EISA_PNP_ID(PNP0701)) {
449                         return (acpi);
450                 }
451         }
452         return (NULL);
453 }
454
455 static bool
456 efipart_testfd(pdinfo_t *fd, pdinfo_t *data __unused)
457 {
458         EFI_DEVICE_PATH *node;
459
460         node = efi_devpath_last_node(fd->pd_devpath);
461         if (node == NULL)
462                 return (false);
463
464         if (efipart_floppy(node) != NULL)
465                 return (true);
466
467         return (false);
468 }
469
470 static int
471 efipart_initfd(void)
472 {
473         EFI_DEVICE_PATH *node;
474         ACPI_HID_DEVICE_PATH *acpi;
475         pdinfo_t *parent, *fd;
476
477         while ((fd = efipart_get_pd(&pdinfo, efipart_testfd, NULL)) != NULL) {
478                 if ((node = efi_devpath_last_node(fd->pd_devpath)) == NULL)
479                         continue;
480
481                 if ((acpi = efipart_floppy(node)) == NULL)
482                         continue;
483
484                 STAILQ_REMOVE(&pdinfo, fd, pdinfo, pd_link);
485                 parent = fd->pd_parent;
486                 if (parent != NULL) {
487                         STAILQ_REMOVE(&pdinfo, parent, pdinfo, pd_link);
488                         parent->pd_alias = fd->pd_handle;
489                         parent->pd_unit = acpi->UID;
490                         free(fd);
491                         fd = parent;
492                 } else {
493                         fd->pd_unit = acpi->UID;
494                 }
495                 fd->pd_devsw = &efipart_fddev;
496                 STAILQ_INSERT_TAIL(&fdinfo, fd, pd_link);
497         }
498
499         bcache_add_dev(efiblk_pdinfo_count(&fdinfo));
500         return (0);
501 }
502
503 /*
504  * Add or update entries with new handle data.
505  */
506 static void
507 efipart_cdinfo_add(pdinfo_t *cd)
508 {
509         pdinfo_t *parent, *pd, *last;
510
511         if (cd == NULL)
512                 return;
513
514         parent = cd->pd_parent;
515         /* Make sure we have parent added */
516         efipart_cdinfo_add(parent);
517
518         STAILQ_FOREACH(pd, &pdinfo, pd_link) {
519                 if (efi_devpath_match(pd->pd_devpath, cd->pd_devpath)) {
520                         STAILQ_REMOVE(&pdinfo, cd, pdinfo, pd_link);
521                         break;
522                 }
523         }
524         if (pd == NULL) {
525                 /* This device is already added. */
526                 return;
527         }
528
529         if (parent != NULL) {
530                 last = STAILQ_LAST(&parent->pd_part, pdinfo, pd_link);
531                 if (last != NULL)
532                         cd->pd_unit = last->pd_unit + 1;
533                 else
534                         cd->pd_unit = 0;
535                 cd->pd_devsw = &efipart_cddev;
536                 STAILQ_INSERT_TAIL(&parent->pd_part, cd, pd_link);
537                 return;
538         }
539
540         last = STAILQ_LAST(&cdinfo, pdinfo, pd_link);
541         if (last != NULL)
542                 cd->pd_unit = last->pd_unit + 1;
543         else
544                 cd->pd_unit = 0;
545
546         cd->pd_devsw = &efipart_cddev;
547         STAILQ_INSERT_TAIL(&cdinfo, cd, pd_link);
548 }
549
550 static bool
551 efipart_testcd(pdinfo_t *cd, pdinfo_t *data __unused)
552 {
553         EFI_DEVICE_PATH *node;
554
555         node = efi_devpath_last_node(cd->pd_devpath);
556         if (node == NULL)
557                 return (false);
558
559         if (efipart_floppy(node) != NULL)
560                 return (false);
561
562         if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
563             DevicePathSubType(node) == MEDIA_CDROM_DP) {
564                 return (true);
565         }
566
567         /* cd drive without the media. */
568         if (cd->pd_blkio->Media->RemovableMedia &&
569             !cd->pd_blkio->Media->MediaPresent) {
570                 return (true);
571         }
572
573         return (false);
574 }
575
576 /*
577  * Test if pd is parent for device.
578  */
579 static bool
580 efipart_testchild(pdinfo_t *dev, pdinfo_t *pd)
581 {
582         /* device with no parent. */
583         if (dev->pd_parent == NULL)
584                 return (false);
585
586         if (efi_devpath_match(dev->pd_parent->pd_devpath, pd->pd_devpath)) {
587                 return (true);
588         }
589         return (false);
590 }
591
592 static int
593 efipart_initcd(void)
594 {
595         pdinfo_t *cd;
596
597         while ((cd = efipart_get_pd(&pdinfo, efipart_testcd, NULL)) != NULL)
598                 efipart_cdinfo_add(cd);
599
600         /* Find all children of CD devices we did add above. */
601         STAILQ_FOREACH(cd, &cdinfo, pd_link) {
602                 pdinfo_t *child;
603
604                 for (child = efipart_get_pd(&pdinfo, efipart_testchild, cd);
605                     child != NULL;
606                     child = efipart_get_pd(&pdinfo, efipart_testchild, cd))
607                         efipart_cdinfo_add(child);
608         }
609         bcache_add_dev(efiblk_pdinfo_count(&cdinfo));
610         return (0);
611 }
612
613 static void
614 efipart_hdinfo_add_node(pdinfo_t *hd, EFI_DEVICE_PATH *node)
615 {
616         pdinfo_t *parent, *ptr;
617
618         if (node == NULL)
619                 return;
620
621         parent = hd->pd_parent;
622         /*
623          * If the node is not MEDIA_HARDDRIVE_DP, it is sub-partition.
624          * This can happen with Vendor nodes, and since we do not know
625          * the more about those nodes, we just count them.
626          */
627         if (DevicePathSubType(node) != MEDIA_HARDDRIVE_DP) {
628                 ptr = STAILQ_LAST(&parent->pd_part, pdinfo, pd_link);
629                 if (ptr != NULL)
630                         hd->pd_unit = ptr->pd_unit + 1;
631                 else
632                         hd->pd_unit = 0;
633         } else {
634                 hd->pd_unit = ((HARDDRIVE_DEVICE_PATH *)node)->PartitionNumber;
635         }
636
637         hd->pd_devsw = &efipart_hddev;
638         STAILQ_INSERT_TAIL(&parent->pd_part, hd, pd_link);
639 }
640
641 /*
642  * The MEDIA_FILEPATH_DP has device name.
643  * From U-Boot sources it looks like names are in the form
644  * of typeN:M, where type is interface type, N is disk id
645  * and M is partition id.
646  */
647 static void
648 efipart_hdinfo_add_filepath(pdinfo_t *hd, FILEPATH_DEVICE_PATH *node)
649 {
650         char *pathname, *p;
651         int len;
652         pdinfo_t *last;
653
654         last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
655         if (last != NULL)
656                 hd->pd_unit = last->pd_unit + 1;
657         else
658                 hd->pd_unit = 0;
659
660         /* FILEPATH_DEVICE_PATH has 0 terminated string */
661         len = ucs2len(node->PathName);
662         if ((pathname = malloc(len + 1)) == NULL) {
663                 printf("Failed to add disk, out of memory\n");
664                 free(hd);
665                 return;
666         }
667         cpy16to8(node->PathName, pathname, len + 1);
668         p = strchr(pathname, ':');
669
670         /*
671          * Assume we are receiving handles in order, first disk handle,
672          * then partitions for this disk. If this assumption proves
673          * false, this code would need update.
674          */
675         if (p == NULL) {        /* no colon, add the disk */
676                 hd->pd_devsw = &efipart_hddev;
677                 STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link);
678                 free(pathname);
679                 return;
680         }
681         p++;    /* skip the colon */
682         errno = 0;
683         hd->pd_unit = (int)strtol(p, NULL, 0);
684         if (errno != 0) {
685                 printf("Bad unit number for partition \"%s\"\n", pathname);
686                 free(pathname);
687                 free(hd);
688                 return;
689         }
690
691         /*
692          * We should have disk registered, if not, we are receiving
693          * handles out of order, and this code should be reworked
694          * to create "blank" disk for partition, and to find the
695          * disk based on PathName compares.
696          */
697         if (last == NULL) {
698                 printf("BUG: No disk for partition \"%s\"\n", pathname);
699                 free(pathname);
700                 free(hd);
701                 return;
702         }
703         /* Add the partition. */
704         hd->pd_parent = last;
705         hd->pd_devsw = &efipart_hddev;
706         STAILQ_INSERT_TAIL(&last->pd_part, hd, pd_link);
707         free(pathname);
708 }
709
710 static void
711 efipart_hdinfo_add(pdinfo_t *hd)
712 {
713         pdinfo_t *parent, *pd, *last;
714         EFI_DEVICE_PATH *node;
715
716         if (hd == NULL)
717                 return;
718
719         parent = hd->pd_parent;
720         /* Make sure we have parent added */
721         efipart_hdinfo_add(parent);
722
723         STAILQ_FOREACH(pd, &pdinfo, pd_link) {
724                 if (efi_devpath_match(pd->pd_devpath, hd->pd_devpath)) {
725                         STAILQ_REMOVE(&pdinfo, hd, pdinfo, pd_link);
726                         break;
727                 }
728         }
729         if (pd == NULL) {
730                 /* This device is already added. */
731                 return;
732         }
733
734         if ((node = efi_devpath_last_node(hd->pd_devpath)) == NULL)
735                 return;
736
737         if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
738             DevicePathSubType(node) == MEDIA_FILEPATH_DP) {
739                 efipart_hdinfo_add_filepath(hd,
740                     (FILEPATH_DEVICE_PATH *)node);
741                 return;
742         }
743
744         if (parent != NULL) {
745                 efipart_hdinfo_add_node(hd, node);
746                 return;
747         }
748
749         last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
750         if (last != NULL)
751                 hd->pd_unit = last->pd_unit + 1;
752         else
753                 hd->pd_unit = 0;
754
755         /* Add the disk. */
756         hd->pd_devsw = &efipart_hddev;
757         STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link);
758 }
759
760 static bool
761 efipart_testhd(pdinfo_t *hd, pdinfo_t *data __unused)
762 {
763         if (efipart_testfd(hd, NULL))
764                 return (false);
765
766         if (efipart_testcd(hd, NULL))
767                 return (false);
768
769         /* Anything else must be HD. */
770         return (true);
771 }
772
773 static int
774 efipart_inithd(void)
775 {
776         pdinfo_t *hd;
777
778         while ((hd = efipart_get_pd(&pdinfo, efipart_testhd, NULL)) != NULL)
779                 efipart_hdinfo_add(hd);
780
781         bcache_add_dev(efiblk_pdinfo_count(&hdinfo));
782         return (0);
783 }
784
785 static int
786 efipart_print_common(struct devsw *dev, pdinfo_list_t *pdlist, int verbose)
787 {
788         int ret = 0;
789         EFI_BLOCK_IO *blkio;
790         EFI_STATUS status;
791         EFI_HANDLE h;
792         pdinfo_t *pd;
793         CHAR16 *text;
794         struct disk_devdesc pd_dev;
795         char line[80];
796
797         if (STAILQ_EMPTY(pdlist))
798                 return (0);
799
800         printf("%s devices:", dev->dv_name);
801         if ((ret = pager_output("\n")) != 0)
802                 return (ret);
803
804         STAILQ_FOREACH(pd, pdlist, pd_link) {
805                 h = pd->pd_handle;
806                 if (verbose) {  /* Output the device path. */
807                         text = efi_devpath_name(efi_lookup_devpath(h));
808                         if (text != NULL) {
809                                 printf("  %S", text);
810                                 efi_free_devpath_name(text);
811                                 if ((ret = pager_output("\n")) != 0)
812                                         break;
813                         }
814                 }
815                 snprintf(line, sizeof(line),
816                     "    %s%d", dev->dv_name, pd->pd_unit);
817                 printf("%s:", line);
818                 status = OpenProtocolByHandle(h, &blkio_guid, (void **)&blkio);
819                 if (!EFI_ERROR(status)) {
820                         printf("    %llu",
821                             blkio->Media->LastBlock == 0? 0:
822                             (unsigned long long) (blkio->Media->LastBlock + 1));
823                         if (blkio->Media->LastBlock != 0) {
824                                 printf(" X %u", blkio->Media->BlockSize);
825                         }
826                         printf(" blocks");
827                         if (blkio->Media->MediaPresent) {
828                                 if (blkio->Media->RemovableMedia)
829                                         printf(" (removable)");
830                         } else {
831                                 printf(" (no media)");
832                         }
833                         if ((ret = pager_output("\n")) != 0)
834                                 break;
835                         if (!blkio->Media->MediaPresent)
836                                 continue;
837
838                         pd->pd_blkio = blkio;
839                         pd_dev.dd.d_dev = dev;
840                         pd_dev.dd.d_unit = pd->pd_unit;
841                         pd_dev.d_slice = D_SLICENONE;
842                         pd_dev.d_partition = D_PARTNONE;
843                         ret = disk_open(&pd_dev, blkio->Media->BlockSize *
844                             (blkio->Media->LastBlock + 1),
845                             blkio->Media->BlockSize);
846                         if (ret == 0) {
847                                 ret = disk_print(&pd_dev, line, verbose);
848                                 disk_close(&pd_dev);
849                                 if (ret != 0)
850                                         return (ret);
851                         } else {
852                                 /* Do not fail from disk_open() */
853                                 ret = 0;
854                         }
855                 } else {
856                         if ((ret = pager_output("\n")) != 0)
857                                 break;
858                 }
859         }
860         return (ret);
861 }
862
863 static int
864 efipart_printfd(int verbose)
865 {
866         return (efipart_print_common(&efipart_fddev, &fdinfo, verbose));
867 }
868
869 static int
870 efipart_printcd(int verbose)
871 {
872         return (efipart_print_common(&efipart_cddev, &cdinfo, verbose));
873 }
874
875 static int
876 efipart_printhd(int verbose)
877 {
878         return (efipart_print_common(&efipart_hddev, &hdinfo, verbose));
879 }
880
881 static int
882 efipart_open(struct open_file *f, ...)
883 {
884         va_list args;
885         struct disk_devdesc *dev;
886         pdinfo_t *pd;
887         EFI_BLOCK_IO *blkio;
888         EFI_STATUS status;
889
890         va_start(args, f);
891         dev = va_arg(args, struct disk_devdesc *);
892         va_end(args);
893         if (dev == NULL)
894                 return (EINVAL);
895
896         pd = efiblk_get_pdinfo((struct devdesc *)dev);
897         if (pd == NULL)
898                 return (EIO);
899
900         if (pd->pd_blkio == NULL) {
901                 status = OpenProtocolByHandle(pd->pd_handle, &blkio_guid,
902                     (void **)&pd->pd_blkio);
903                 if (EFI_ERROR(status))
904                         return (efi_status_to_errno(status));
905         }
906
907         blkio = pd->pd_blkio;
908         if (!blkio->Media->MediaPresent)
909                 return (EAGAIN);
910
911         pd->pd_open++;
912         if (pd->pd_bcache == NULL)
913                 pd->pd_bcache = bcache_allocate();
914
915         if (dev->dd.d_dev->dv_type == DEVT_DISK) {
916                 int rc;
917
918                 rc = disk_open(dev,
919                     blkio->Media->BlockSize * (blkio->Media->LastBlock + 1),
920                     blkio->Media->BlockSize);
921                 if (rc != 0) {
922                         pd->pd_open--;
923                         if (pd->pd_open == 0) {
924                                 pd->pd_blkio = NULL;
925                                 bcache_free(pd->pd_bcache);
926                                 pd->pd_bcache = NULL;
927                         }
928                 }
929                 return (rc);
930         }
931         return (0);
932 }
933
934 static int
935 efipart_close(struct open_file *f)
936 {
937         struct disk_devdesc *dev;
938         pdinfo_t *pd;
939
940         dev = (struct disk_devdesc *)(f->f_devdata);
941         if (dev == NULL)
942                 return (EINVAL);
943
944         pd = efiblk_get_pdinfo((struct devdesc *)dev);
945         if (pd == NULL)
946                 return (EINVAL);
947
948         pd->pd_open--;
949         if (pd->pd_open == 0) {
950                 pd->pd_blkio = NULL;
951                 bcache_free(pd->pd_bcache);
952                 pd->pd_bcache = NULL;
953         }
954         if (dev->dd.d_dev->dv_type == DEVT_DISK)
955                 return (disk_close(dev));
956         return (0);
957 }
958
959 static int
960 efipart_ioctl(struct open_file *f, u_long cmd, void *data)
961 {
962         struct disk_devdesc *dev;
963         pdinfo_t *pd;
964         int rc;
965
966         dev = (struct disk_devdesc *)(f->f_devdata);
967         if (dev == NULL)
968                 return (EINVAL);
969
970         pd = efiblk_get_pdinfo((struct devdesc *)dev);
971         if (pd == NULL)
972                 return (EINVAL);
973
974         if (dev->dd.d_dev->dv_type == DEVT_DISK) {
975                 rc = disk_ioctl(dev, cmd, data);
976                 if (rc != ENOTTY)
977                         return (rc);
978         }
979
980         switch (cmd) {
981         case DIOCGSECTORSIZE:
982                 *(u_int *)data = pd->pd_blkio->Media->BlockSize;
983                 break;
984         case DIOCGMEDIASIZE:
985                 *(uint64_t *)data = pd->pd_blkio->Media->BlockSize *
986                     (pd->pd_blkio->Media->LastBlock + 1);
987                 break;
988         default:
989                 return (ENOTTY);
990         }
991
992         return (0);
993 }
994
995 /*
996  * efipart_readwrite()
997  * Internal equivalent of efipart_strategy(), which operates on the
998  * media-native block size. This function expects all I/O requests
999  * to be within the media size and returns an error if such is not
1000  * the case.
1001  */
1002 static int
1003 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
1004     char *buf)
1005 {
1006         EFI_STATUS status;
1007
1008         if (blkio == NULL)
1009                 return (ENXIO);
1010         if (blk < 0 || blk > blkio->Media->LastBlock)
1011                 return (EIO);
1012         if ((blk + nblks - 1) > blkio->Media->LastBlock)
1013                 return (EIO);
1014
1015         switch (rw & F_MASK) {
1016         case F_READ:
1017                 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
1018                     nblks * blkio->Media->BlockSize, buf);
1019                 break;
1020         case F_WRITE:
1021                 if (blkio->Media->ReadOnly)
1022                         return (EROFS);
1023                 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
1024                     nblks * blkio->Media->BlockSize, buf);
1025                 break;
1026         default:
1027                 return (ENOSYS);
1028         }
1029
1030         if (EFI_ERROR(status)) {
1031                 printf("%s: rw=%d, blk=%ju size=%ju status=%lu\n", __func__, rw,
1032                     blk, nblks, EFI_ERROR_CODE(status));
1033         }
1034         return (efi_status_to_errno(status));
1035 }
1036
1037 static int
1038 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size,
1039     char *buf, size_t *rsize)
1040 {
1041         struct bcache_devdata bcd;
1042         struct disk_devdesc *dev;
1043         pdinfo_t *pd;
1044
1045         dev = (struct disk_devdesc *)devdata;
1046         if (dev == NULL)
1047                 return (EINVAL);
1048
1049         pd = efiblk_get_pdinfo((struct devdesc *)dev);
1050         if (pd == NULL)
1051                 return (EINVAL);
1052
1053         if (pd->pd_blkio->Media->RemovableMedia &&
1054             !pd->pd_blkio->Media->MediaPresent)
1055                 return (ENXIO);
1056
1057         bcd.dv_strategy = efipart_realstrategy;
1058         bcd.dv_devdata = devdata;
1059         bcd.dv_cache = pd->pd_bcache;
1060
1061         if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1062                 daddr_t offset;
1063
1064                 offset = dev->d_offset * pd->pd_blkio->Media->BlockSize;
1065                 offset /= 512;
1066                 return (bcache_strategy(&bcd, rw, blk + offset,
1067                     size, buf, rsize));
1068         }
1069         return (bcache_strategy(&bcd, rw, blk, size, buf, rsize));
1070 }
1071
1072 static int
1073 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t size,
1074     char *buf, size_t *rsize)
1075 {
1076         struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
1077         pdinfo_t *pd;
1078         EFI_BLOCK_IO *blkio;
1079         uint64_t off, disk_blocks, d_offset = 0;
1080         char *blkbuf;
1081         size_t blkoff, blksz, bio_size;
1082         unsigned ioalign;
1083         bool need_buf;
1084         int rc;
1085         uint64_t diskend, readstart;
1086
1087         if (dev == NULL || blk < 0)
1088                 return (EINVAL);
1089
1090         pd = efiblk_get_pdinfo((struct devdesc *)dev);
1091         if (pd == NULL)
1092                 return (EINVAL);
1093
1094         blkio = pd->pd_blkio;
1095         if (blkio == NULL)
1096                 return (ENXIO);
1097
1098         if (size == 0 || (size % 512) != 0)
1099                 return (EIO);
1100
1101         off = blk * 512;
1102         /*
1103          * Get disk blocks, this value is either for whole disk or for
1104          * partition.
1105          */
1106         disk_blocks = 0;
1107         if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1108                 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1109                         /* DIOCGMEDIASIZE does return bytes. */
1110                         disk_blocks /= blkio->Media->BlockSize;
1111                 }
1112                 d_offset = dev->d_offset;
1113         }
1114         if (disk_blocks == 0)
1115                 disk_blocks = blkio->Media->LastBlock + 1 - d_offset;
1116
1117         /* make sure we don't read past disk end */
1118         if ((off + size) / blkio->Media->BlockSize > d_offset + disk_blocks) {
1119                 diskend = d_offset + disk_blocks;
1120                 readstart = off / blkio->Media->BlockSize;
1121
1122                 if (diskend <= readstart) {
1123                         if (rsize != NULL)
1124                                 *rsize = 0;
1125
1126                         return (EIO);
1127                 }
1128                 size = diskend - readstart;
1129                 size = size * blkio->Media->BlockSize;
1130         }
1131
1132         need_buf = true;
1133         /* Do we need bounce buffer? */
1134         if ((size % blkio->Media->BlockSize == 0) &&
1135             (off % blkio->Media->BlockSize == 0))
1136                 need_buf = false;
1137
1138         /* Do we have IO alignment requirement? */
1139         ioalign = blkio->Media->IoAlign;
1140         if (ioalign == 0)
1141                 ioalign++;
1142
1143         if (ioalign > 1 && (uintptr_t)buf != roundup2((uintptr_t)buf, ioalign))
1144                 need_buf = true;
1145
1146         if (need_buf) {
1147                 for (bio_size = BIO_BUFFER_SIZE; bio_size > 0;
1148                     bio_size -= blkio->Media->BlockSize) {
1149                         blkbuf = memalign(ioalign, bio_size);
1150                         if (blkbuf != NULL)
1151                                 break;
1152                 }
1153         } else {
1154                 blkbuf = buf;
1155                 bio_size = size;
1156         }
1157
1158         if (blkbuf == NULL)
1159                 return (ENOMEM);
1160
1161         if (rsize != NULL)
1162                 *rsize = size;
1163
1164         rc = 0;
1165         blk = off / blkio->Media->BlockSize;
1166         blkoff = off % blkio->Media->BlockSize;
1167
1168         while (size > 0) {
1169                 size_t x = min(size, bio_size);
1170
1171                 if (x < blkio->Media->BlockSize)
1172                         x = 1;
1173                 else
1174                         x /= blkio->Media->BlockSize;
1175
1176                 switch (rw & F_MASK) {
1177                 case F_READ:
1178                         blksz = blkio->Media->BlockSize * x - blkoff;
1179                         if (size < blksz)
1180                                 blksz = size;
1181
1182                         rc = efipart_readwrite(blkio, rw, blk, x, blkbuf);
1183                         if (rc != 0)
1184                                 goto error;
1185
1186                         if (need_buf)
1187                                 bcopy(blkbuf + blkoff, buf, blksz);
1188                         break;
1189                 case F_WRITE:
1190                         rc = 0;
1191                         if (blkoff != 0) {
1192                                 /*
1193                                  * We got offset to sector, read 1 sector to
1194                                  * blkbuf.
1195                                  */
1196                                 x = 1;
1197                                 blksz = blkio->Media->BlockSize - blkoff;
1198                                 blksz = min(blksz, size);
1199                                 rc = efipart_readwrite(blkio, F_READ, blk, x,
1200                                     blkbuf);
1201                         } else if (size < blkio->Media->BlockSize) {
1202                                 /*
1203                                  * The remaining block is not full
1204                                  * sector. Read 1 sector to blkbuf.
1205                                  */
1206                                 x = 1;
1207                                 blksz = size;
1208                                 rc = efipart_readwrite(blkio, F_READ, blk, x,
1209                                     blkbuf);
1210                         } else {
1211                                 /* We can write full sector(s). */
1212                                 blksz = blkio->Media->BlockSize * x;
1213                         }
1214
1215                         if (rc != 0)
1216                                 goto error;
1217                         /*
1218                          * Put your Data In, Put your Data out,
1219                          * Put your Data In, and shake it all about
1220                          */
1221                         if (need_buf)
1222                                 bcopy(buf, blkbuf + blkoff, blksz);
1223                         rc = efipart_readwrite(blkio, F_WRITE, blk, x, blkbuf);
1224                         if (rc != 0)
1225                                 goto error;
1226                         break;
1227                 default:
1228                         /* DO NOTHING */
1229                         rc = EROFS;
1230                         goto error;
1231                 }
1232
1233                 blkoff = 0;
1234                 buf += blksz;
1235                 size -= blksz;
1236                 blk += x;
1237         }
1238
1239 error:
1240         if (rsize != NULL)
1241                 *rsize -= size;
1242
1243         if (need_buf)
1244                 free(blkbuf);
1245         return (rc);
1246 }