2 * Copyright (c) 2010 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/queue.h>
37 #include <bootstrap.h>
45 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
47 static int efipart_initfd(void);
48 static int efipart_initcd(void);
49 static int efipart_inithd(void);
51 static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *);
52 static int efipart_realstrategy(void *, int, daddr_t, size_t, char *, size_t *);
54 static int efipart_open(struct open_file *, ...);
55 static int efipart_close(struct open_file *);
56 static int efipart_ioctl(struct open_file *, u_long, void *);
58 static int efipart_printfd(int);
59 static int efipart_printcd(int);
60 static int efipart_printhd(int);
62 /* EISA PNP ID's for floppy controllers */
67 struct devsw efipart_fddev = {
70 .dv_init = efipart_initfd,
71 .dv_strategy = efipart_strategy,
72 .dv_open = efipart_open,
73 .dv_close = efipart_close,
74 .dv_ioctl = efipart_ioctl,
75 .dv_print = efipart_printfd,
79 struct devsw efipart_cddev = {
82 .dv_init = efipart_initcd,
83 .dv_strategy = efipart_strategy,
84 .dv_open = efipart_open,
85 .dv_close = efipart_close,
86 .dv_ioctl = efipart_ioctl,
87 .dv_print = efipart_printcd,
91 struct devsw efipart_hddev = {
94 .dv_init = efipart_inithd,
95 .dv_strategy = efipart_strategy,
96 .dv_open = efipart_open,
97 .dv_close = efipart_close,
98 .dv_ioctl = efipart_ioctl,
99 .dv_print = efipart_printhd,
103 static pdinfo_list_t fdinfo;
104 static pdinfo_list_t cdinfo;
105 static pdinfo_list_t hdinfo;
107 static EFI_HANDLE *efipart_handles = NULL;
108 static UINTN efipart_nhandles = 0;
111 efiblk_get_pdinfo_list(struct devsw *dev)
113 if (dev->dv_type == DEVT_DISK)
115 if (dev->dv_type == DEVT_CD)
117 if (dev->dv_type == DEVT_FD)
122 /* XXX this gets called way way too often, investigate */
124 efiblk_get_pdinfo(struct devdesc *dev)
129 pdi = efiblk_get_pdinfo_list(dev->d_dev);
133 STAILQ_FOREACH(pd, pdi, pd_link) {
134 if (pd->pd_unit == dev->d_unit)
141 efiblk_get_pdinfo_by_device_path(EFI_DEVICE_PATH *path)
144 EFI_DEVICE_PATH *media, *devpath;
147 media = efi_devpath_to_media_path(path);
150 for (i = 0; i < efipart_nhandles; i++) {
151 h = efipart_handles[i];
152 devpath = efi_lookup_devpath(h);
155 if (!efi_devpath_match_node(media, efi_devpath_to_media_path(devpath)))
157 return (efiblk_get_pdinfo_by_handle(h));
163 same_handle(pdinfo_t *pd, EFI_HANDLE h)
166 return (pd->pd_handle == h || pd->pd_alias == h);
170 efiblk_get_pdinfo_by_handle(EFI_HANDLE h)
175 * Check hard disks, then cd, then floppy
177 STAILQ_FOREACH(dp, &hdinfo, pd_link) {
178 if (same_handle(dp, h))
180 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
181 if (same_handle(pp, h))
185 STAILQ_FOREACH(dp, &cdinfo, pd_link) {
186 if (same_handle(dp, h))
189 STAILQ_FOREACH(dp, &fdinfo, pd_link) {
190 if (same_handle(dp, h))
197 efiblk_pdinfo_count(pdinfo_list_t *pdi)
202 STAILQ_FOREACH(pd, pdi, pd_link) {
209 efipart_inithandles(void)
215 if (efipart_nhandles != 0) {
216 free(efipart_handles);
217 efipart_handles = NULL;
218 efipart_nhandles = 0;
223 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, hin);
224 if (status == EFI_BUFFER_TOO_SMALL) {
226 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
228 if (EFI_ERROR(status))
231 if (EFI_ERROR(status))
232 return (efi_status_to_errno(status));
234 efipart_handles = hin;
235 efipart_nhandles = sz / sizeof(*hin);
237 printf("%s: Got %d BLOCK IO MEDIA handle(s)\n", __func__,
243 static ACPI_HID_DEVICE_PATH *
244 efipart_floppy(EFI_DEVICE_PATH *node)
246 ACPI_HID_DEVICE_PATH *acpi;
248 if (DevicePathType(node) == ACPI_DEVICE_PATH &&
249 DevicePathSubType(node) == ACPI_DP) {
250 acpi = (ACPI_HID_DEVICE_PATH *) node;
251 if (acpi->HID == EISA_PNP_ID(PNP0604) ||
252 acpi->HID == EISA_PNP_ID(PNP0700) ||
253 acpi->HID == EISA_PNP_ID(PNP0701)) {
261 * Determine if the provided device path is hdd.
263 * There really is no simple fool proof way to classify the devices.
264 * Since we do build three lists of devices - floppy, cd and hdd, we
265 * will try to see if the device is floppy or cd, and list anything else
269 efipart_hdd(EFI_DEVICE_PATH *dp)
272 EFI_DEVICE_PATH *devpath, *node;
279 if ((node = efi_devpath_last_node(dp)) == NULL)
282 if (efipart_floppy(node) != NULL)
286 * Test every EFI BLOCK IO handle to make sure dp is not device path
289 for (i = 0; i < efipart_nhandles; i++) {
290 devpath = efi_lookup_devpath(efipart_handles[i]);
294 /* Only continue testing when dp is prefix in devpath. */
295 if (!efi_devpath_is_prefix(dp, devpath))
299 * The device path has to have last node describing the
300 * device, or we can not test the type.
302 if ((node = efi_devpath_last_node(devpath)) == NULL)
305 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
306 DevicePathSubType(node) == MEDIA_CDROM_DP) {
310 /* Make sure we do have the media. */
311 status = BS->HandleProtocol(efipart_handles[i],
312 &blkio_guid, (void **)&blkio);
313 if (EFI_ERROR(status))
316 /* USB or SATA cd without the media. */
317 if (blkio->Media->RemovableMedia &&
318 !blkio->Media->MediaPresent) {
323 * We assume the block size 512 or greater power of 2.
324 * iPXE is known to insert stub BLOCK IO device with
327 if (blkio->Media->BlockSize < 512 ||
328 !powerof2(blkio->Media->BlockSize)) {
336 * Add or update entries with new handle data.
339 efipart_fdinfo_add(EFI_HANDLE handle, uint32_t uid, EFI_DEVICE_PATH *devpath)
343 fd = calloc(1, sizeof(pdinfo_t));
345 printf("Failed to register floppy %d, out of memory\n", uid);
348 STAILQ_INIT(&fd->pd_part);
351 fd->pd_handle = handle;
352 fd->pd_devpath = devpath;
353 fd->pd_parent = NULL;
354 fd->pd_devsw = &efipart_fddev;
355 STAILQ_INSERT_TAIL(&fdinfo, fd, pd_link);
360 efipart_updatefd(void)
362 EFI_DEVICE_PATH *devpath, *node;
363 ACPI_HID_DEVICE_PATH *acpi;
366 for (i = 0; i < efipart_nhandles; i++) {
367 devpath = efi_lookup_devpath(efipart_handles[i]);
371 if ((node = efi_devpath_last_node(devpath)) == NULL)
373 if ((acpi = efipart_floppy(node)) != NULL) {
374 efipart_fdinfo_add(efipart_handles[i], acpi->UID,
384 STAILQ_INIT(&fdinfo);
388 bcache_add_dev(efiblk_pdinfo_count(&fdinfo));
393 * Add or update entries with new handle data.
396 efipart_cdinfo_add(EFI_HANDLE handle, EFI_HANDLE alias,
397 EFI_DEVICE_PATH *devpath)
404 STAILQ_FOREACH(pd, &cdinfo, pd_link) {
405 if (efi_devpath_match(pd->pd_devpath, devpath) == true) {
406 pd->pd_handle = handle;
407 pd->pd_alias = alias;
413 cd = calloc(1, sizeof(pdinfo_t));
415 printf("Failed to add cd %d, out of memory\n", unit);
418 STAILQ_INIT(&cd->pd_part);
420 cd->pd_handle = handle;
422 cd->pd_alias = alias;
423 cd->pd_devpath = devpath;
424 cd->pd_parent = NULL;
425 cd->pd_devsw = &efipart_cddev;
426 STAILQ_INSERT_TAIL(&cdinfo, cd, pd_link);
431 efipart_updatecd(void)
434 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
439 for (i = 0; i < efipart_nhandles; i++) {
440 devpath = efi_lookup_devpath(efipart_handles[i]);
444 if ((node = efi_devpath_last_node(devpath)) == NULL)
447 if (efipart_floppy(node) != NULL)
450 if (efipart_hdd(devpath))
453 status = BS->HandleProtocol(efipart_handles[i],
454 &blkio_guid, (void **)&blkio);
455 if (EFI_ERROR(status))
458 * If we come across a logical partition of subtype CDROM
459 * it doesn't refer to the CD filesystem itself, but rather
460 * to any usable El Torito boot image on it. In this case
461 * we try to find the parent device and add that instead as
462 * that will be the CD filesystem.
464 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
465 DevicePathSubType(node) == MEDIA_CDROM_DP) {
466 devpathcpy = efi_devpath_trim(devpath);
467 if (devpathcpy == NULL)
469 tmpdevpath = devpathcpy;
470 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
473 if (EFI_ERROR(status))
475 devpath = efi_lookup_devpath(handle);
476 efipart_cdinfo_add(handle, efipart_handles[i],
481 if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
482 DevicePathSubType(node) == MSG_ATAPI_DP) {
483 efipart_cdinfo_add(efipart_handles[i], NULL,
488 /* USB or SATA cd without the media. */
489 if (blkio->Media->RemovableMedia &&
490 !blkio->Media->MediaPresent) {
491 efipart_cdinfo_add(efipart_handles[i], NULL,
501 STAILQ_INIT(&cdinfo);
505 bcache_add_dev(efiblk_pdinfo_count(&cdinfo));
510 efipart_hdinfo_add(EFI_HANDLE disk_handle, EFI_HANDLE part_handle)
512 EFI_DEVICE_PATH *disk_devpath, *part_devpath;
513 HARDDRIVE_DEVICE_PATH *node;
515 pdinfo_t *hd, *pd, *last;
517 disk_devpath = efi_lookup_devpath(disk_handle);
518 if (disk_devpath == NULL)
521 if (part_handle != NULL) {
522 part_devpath = efi_lookup_devpath(part_handle);
523 if (part_devpath == NULL)
525 node = (HARDDRIVE_DEVICE_PATH *)
526 efi_devpath_last_node(part_devpath);
528 return (ENOENT); /* This should not happen. */
534 pd = calloc(1, sizeof(pdinfo_t));
536 printf("Failed to add disk, out of memory\n");
539 STAILQ_INIT(&pd->pd_part);
541 STAILQ_FOREACH(hd, &hdinfo, pd_link) {
542 if (efi_devpath_match(hd->pd_devpath, disk_devpath) == true) {
543 if (part_devpath == NULL)
546 /* Add the partition. */
547 pd->pd_handle = part_handle;
548 pd->pd_unit = node->PartitionNumber;
549 pd->pd_devpath = part_devpath;
551 pd->pd_devsw = &efipart_hddev;
552 STAILQ_INSERT_TAIL(&hd->pd_part, pd, pd_link);
557 last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
559 unit = last->pd_unit + 1;
565 hd->pd_handle = disk_handle;
567 hd->pd_devpath = disk_devpath;
568 hd->pd_parent = NULL;
569 hd->pd_devsw = &efipart_hddev;
570 STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link);
572 if (part_devpath == NULL)
575 pd = calloc(1, sizeof(pdinfo_t));
577 printf("Failed to add partition, out of memory\n");
580 STAILQ_INIT(&pd->pd_part);
582 /* Add the partition. */
583 pd->pd_handle = part_handle;
584 pd->pd_unit = node->PartitionNumber;
585 pd->pd_devpath = part_devpath;
587 pd->pd_devsw = &efipart_hddev;
588 STAILQ_INSERT_TAIL(&hd->pd_part, pd, pd_link);
594 * The MEDIA_FILEPATH_DP has device name.
595 * From U-Boot sources it looks like names are in the form
596 * of typeN:M, where type is interface type, N is disk id
597 * and M is partition id.
600 efipart_hdinfo_add_filepath(EFI_HANDLE disk_handle)
602 EFI_DEVICE_PATH *devpath;
603 FILEPATH_DEVICE_PATH *node;
608 /* First collect and verify all the data */
609 if ((devpath = efi_lookup_devpath(disk_handle)) == NULL)
611 node = (FILEPATH_DEVICE_PATH *)efi_devpath_last_node(devpath);
613 return (ENOENT); /* This should not happen. */
615 pd = calloc(1, sizeof(pdinfo_t));
617 printf("Failed to add disk, out of memory\n");
620 STAILQ_INIT(&pd->pd_part);
621 last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
623 unit = last->pd_unit + 1;
627 /* FILEPATH_DEVICE_PATH has 0 terminated string */
628 len = ucs2len(node->PathName);
629 if ((pathname = malloc(len + 1)) == NULL) {
630 printf("Failed to add disk, out of memory\n");
634 cpy16to8(node->PathName, pathname, len + 1);
635 p = strchr(pathname, ':');
638 * Assume we are receiving handles in order, first disk handle,
639 * then partitions for this disk. If this assumption proves
640 * false, this code would need update.
642 if (p == NULL) { /* no colon, add the disk */
643 pd->pd_handle = disk_handle;
645 pd->pd_devpath = devpath;
646 pd->pd_parent = NULL;
647 pd->pd_devsw = &efipart_hddev;
648 STAILQ_INSERT_TAIL(&hdinfo, pd, pd_link);
652 p++; /* skip the colon */
654 unit = (int)strtol(p, NULL, 0);
656 printf("Bad unit number for partition \"%s\"\n", pathname);
663 * We should have disk registered, if not, we are receiving
664 * handles out of order, and this code should be reworked
665 * to create "blank" disk for partition, and to find the
666 * disk based on PathName compares.
669 printf("BUG: No disk for partition \"%s\"\n", pathname);
674 /* Add the partition. */
675 pd->pd_handle = disk_handle;
677 pd->pd_devpath = devpath;
678 pd->pd_parent = last;
679 pd->pd_devsw = &efipart_hddev;
680 STAILQ_INSERT_TAIL(&last->pd_part, pd, pd_link);
686 efipart_updatehd(void)
689 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
694 for (i = 0; i < efipart_nhandles; i++) {
695 devpath = efi_lookup_devpath(efipart_handles[i]);
699 if ((node = efi_devpath_last_node(devpath)) == NULL)
702 if (!efipart_hdd(devpath))
705 status = BS->HandleProtocol(efipart_handles[i],
706 &blkio_guid, (void **)&blkio);
707 if (EFI_ERROR(status))
710 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
711 DevicePathSubType(node) == MEDIA_FILEPATH_DP) {
712 efipart_hdinfo_add_filepath(efipart_handles[i]);
716 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
717 DevicePathSubType(node) == MEDIA_HARDDRIVE_DP) {
718 devpathcpy = efi_devpath_trim(devpath);
719 if (devpathcpy == NULL)
721 tmpdevpath = devpathcpy;
722 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
725 if (EFI_ERROR(status))
728 * We do not support nested partitions.
730 devpathcpy = efi_lookup_devpath(handle);
731 if (devpathcpy == NULL)
733 if ((node = efi_devpath_last_node(devpathcpy)) == NULL)
736 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
737 DevicePathSubType(node) == MEDIA_HARDDRIVE_DP)
740 efipart_hdinfo_add(handle, efipart_handles[i]);
744 efipart_hdinfo_add(efipart_handles[i], NULL);
752 STAILQ_INIT(&hdinfo);
756 bcache_add_dev(efiblk_pdinfo_count(&hdinfo));
761 efipart_print_common(struct devsw *dev, pdinfo_list_t *pdlist, int verbose)
769 struct disk_devdesc pd_dev;
772 if (STAILQ_EMPTY(pdlist))
775 printf("%s devices:", dev->dv_name);
776 if ((ret = pager_output("\n")) != 0)
779 STAILQ_FOREACH(pd, pdlist, pd_link) {
781 if (verbose) { /* Output the device path. */
782 text = efi_devpath_name(efi_lookup_devpath(h));
785 efi_free_devpath_name(text);
786 if ((ret = pager_output("\n")) != 0)
790 snprintf(line, sizeof(line),
791 " %s%d", dev->dv_name, pd->pd_unit);
793 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
794 if (!EFI_ERROR(status)) {
796 blkio->Media->LastBlock == 0? 0:
797 (unsigned long long) (blkio->Media->LastBlock + 1));
798 if (blkio->Media->LastBlock != 0) {
799 printf(" X %u", blkio->Media->BlockSize);
802 if (blkio->Media->MediaPresent) {
803 if (blkio->Media->RemovableMedia)
804 printf(" (removable)");
806 printf(" (no media)");
808 if ((ret = pager_output("\n")) != 0)
810 if (!blkio->Media->MediaPresent)
813 pd->pd_blkio = blkio;
814 pd_dev.dd.d_dev = dev;
815 pd_dev.dd.d_unit = pd->pd_unit;
816 pd_dev.d_slice = D_SLICENONE;
817 pd_dev.d_partition = D_PARTNONE;
818 ret = disk_open(&pd_dev, blkio->Media->BlockSize *
819 (blkio->Media->LastBlock + 1),
820 blkio->Media->BlockSize);
822 ret = disk_print(&pd_dev, line, verbose);
827 /* Do not fail from disk_open() */
831 if ((ret = pager_output("\n")) != 0)
839 efipart_printfd(int verbose)
841 return (efipart_print_common(&efipart_fddev, &fdinfo, verbose));
845 efipart_printcd(int verbose)
847 return (efipart_print_common(&efipart_cddev, &cdinfo, verbose));
851 efipart_printhd(int verbose)
853 return (efipart_print_common(&efipart_hddev, &hdinfo, verbose));
857 efipart_open(struct open_file *f, ...)
860 struct disk_devdesc *dev;
866 dev = va_arg(args, struct disk_devdesc*);
871 pd = efiblk_get_pdinfo((struct devdesc *)dev);
875 if (pd->pd_blkio == NULL) {
876 status = BS->HandleProtocol(pd->pd_handle, &blkio_guid,
877 (void **)&pd->pd_blkio);
878 if (EFI_ERROR(status))
879 return (efi_status_to_errno(status));
882 blkio = pd->pd_blkio;
883 if (!blkio->Media->MediaPresent)
887 if (pd->pd_bcache == NULL)
888 pd->pd_bcache = bcache_allocate();
890 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
894 blkio->Media->BlockSize * (blkio->Media->LastBlock + 1),
895 blkio->Media->BlockSize);
898 if (pd->pd_open == 0) {
900 bcache_free(pd->pd_bcache);
901 pd->pd_bcache = NULL;
910 efipart_close(struct open_file *f)
912 struct disk_devdesc *dev;
915 dev = (struct disk_devdesc *)(f->f_devdata);
919 pd = efiblk_get_pdinfo((struct devdesc *)dev);
924 if (pd->pd_open == 0) {
926 bcache_free(pd->pd_bcache);
927 pd->pd_bcache = NULL;
929 if (dev->dd.d_dev->dv_type == DEVT_DISK)
930 return (disk_close(dev));
935 efipart_ioctl(struct open_file *f, u_long cmd, void *data)
937 struct disk_devdesc *dev;
941 dev = (struct disk_devdesc *)(f->f_devdata);
945 pd = efiblk_get_pdinfo((struct devdesc *)dev);
949 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
950 rc = disk_ioctl(dev, cmd, data);
956 case DIOCGSECTORSIZE:
957 *(u_int *)data = pd->pd_blkio->Media->BlockSize;
960 *(uint64_t *)data = pd->pd_blkio->Media->BlockSize *
961 (pd->pd_blkio->Media->LastBlock + 1);
971 * efipart_readwrite()
972 * Internal equivalent of efipart_strategy(), which operates on the
973 * media-native block size. This function expects all I/O requests
974 * to be within the media size and returns an error if such is not
978 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
985 if (blk < 0 || blk > blkio->Media->LastBlock)
987 if ((blk + nblks - 1) > blkio->Media->LastBlock)
990 switch (rw & F_MASK) {
992 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
993 nblks * blkio->Media->BlockSize, buf);
996 if (blkio->Media->ReadOnly)
998 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
999 nblks * blkio->Media->BlockSize, buf);
1005 if (EFI_ERROR(status)) {
1006 printf("%s: rw=%d, blk=%ju size=%ju status=%lu\n", __func__, rw,
1007 blk, nblks, EFI_ERROR_CODE(status));
1009 return (efi_status_to_errno(status));
1013 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size,
1014 char *buf, size_t *rsize)
1016 struct bcache_devdata bcd;
1017 struct disk_devdesc *dev;
1020 dev = (struct disk_devdesc *)devdata;
1024 pd = efiblk_get_pdinfo((struct devdesc *)dev);
1028 if (pd->pd_blkio->Media->RemovableMedia &&
1029 !pd->pd_blkio->Media->MediaPresent)
1032 bcd.dv_strategy = efipart_realstrategy;
1033 bcd.dv_devdata = devdata;
1034 bcd.dv_cache = pd->pd_bcache;
1036 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1039 offset = dev->d_offset * pd->pd_blkio->Media->BlockSize;
1041 return (bcache_strategy(&bcd, rw, blk + offset,
1044 return (bcache_strategy(&bcd, rw, blk, size, buf, rsize));
1048 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t size,
1049 char *buf, size_t *rsize)
1051 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
1053 EFI_BLOCK_IO *blkio;
1054 uint64_t off, disk_blocks, d_offset = 0;
1056 size_t blkoff, blksz;
1058 size_t diskend, readstart;
1060 if (dev == NULL || blk < 0)
1063 pd = efiblk_get_pdinfo((struct devdesc *)dev);
1067 blkio = pd->pd_blkio;
1071 if (size == 0 || (size % 512) != 0)
1076 * Get disk blocks, this value is either for whole disk or for
1080 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1081 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1082 /* DIOCGMEDIASIZE does return bytes. */
1083 disk_blocks /= blkio->Media->BlockSize;
1085 d_offset = dev->d_offset;
1087 if (disk_blocks == 0)
1088 disk_blocks = blkio->Media->LastBlock + 1 - d_offset;
1090 /* make sure we don't read past disk end */
1091 if ((off + size) / blkio->Media->BlockSize > d_offset + disk_blocks) {
1092 diskend = d_offset + disk_blocks;
1093 readstart = off / blkio->Media->BlockSize;
1095 if (diskend <= readstart) {
1101 size = diskend - readstart;
1102 size = size * blkio->Media->BlockSize;
1108 if ((size % blkio->Media->BlockSize == 0) &&
1109 (off % blkio->Media->BlockSize == 0))
1110 return (efipart_readwrite(blkio, rw,
1111 off / blkio->Media->BlockSize,
1112 size / blkio->Media->BlockSize, buf));
1115 * The block size of the media is not a multiple of I/O.
1117 blkbuf = malloc(blkio->Media->BlockSize);
1122 blk = off / blkio->Media->BlockSize;
1123 blkoff = off % blkio->Media->BlockSize;
1124 blksz = blkio->Media->BlockSize - blkoff;
1126 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
1131 bcopy(blkbuf + blkoff, buf, blksz);
1136 blksz = blkio->Media->BlockSize;