8749 loader: efipart_strategy is using wrong offset with >512B sectors
[unleashed.git] / usr / src / boot / sys / boot / efi / libefi / efipart.c
blob2640201698a24ba63d198a64272f9fc184ba5635
1 /*-
2 * Copyright (c) 2010 Marcel Moolenaar
3 * All rights reserved.
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.
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.
27 #include <sys/cdefs.h>
29 #include <sys/disk.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/queue.h>
33 #include <stddef.h>
34 #include <stdarg.h>
36 #include <bootstrap.h>
38 #include <efi.h>
39 #include <efilib.h>
40 #include <efiprot.h>
41 #include <disk.h>
43 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
45 static int efipart_initfd(void);
46 static int efipart_initcd(void);
47 static int efipart_inithd(void);
49 static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *);
50 static int efipart_realstrategy(void *, int, daddr_t, size_t, char *, size_t *);
52 static int efipart_open(struct open_file *, ...);
53 static int efipart_close(struct open_file *);
54 static int efipart_ioctl(struct open_file *, u_long, void *);
56 static int efipart_printfd(int);
57 static int efipart_printcd(int);
58 static int efipart_printhd(int);
60 /* EISA PNP ID's for floppy controllers */
61 #define PNP0604 0x604
62 #define PNP0700 0x700
63 #define PNP0701 0x701
65 struct devsw efipart_fddev = {
66 .dv_name = "fd",
67 .dv_type = DEVT_FD,
68 .dv_init = efipart_initfd,
69 .dv_strategy = efipart_strategy,
70 .dv_open = efipart_open,
71 .dv_close = efipart_close,
72 .dv_ioctl = efipart_ioctl,
73 .dv_print = efipart_printfd,
74 .dv_cleanup = NULL
77 struct devsw efipart_cddev = {
78 .dv_name = "cd",
79 .dv_type = DEVT_CD,
80 .dv_init = efipart_initcd,
81 .dv_strategy = efipart_strategy,
82 .dv_open = efipart_open,
83 .dv_close = efipart_close,
84 .dv_ioctl = efipart_ioctl,
85 .dv_print = efipart_printcd,
86 .dv_cleanup = NULL
89 struct devsw efipart_hddev = {
90 .dv_name = "disk",
91 .dv_type = DEVT_DISK,
92 .dv_init = efipart_inithd,
93 .dv_strategy = efipart_strategy,
94 .dv_open = efipart_open,
95 .dv_close = efipart_close,
96 .dv_ioctl = efipart_ioctl,
97 .dv_print = efipart_printhd,
98 .dv_cleanup = NULL
101 static pdinfo_list_t fdinfo;
102 static pdinfo_list_t cdinfo;
103 static pdinfo_list_t hdinfo;
105 static EFI_HANDLE *efipart_handles = NULL;
106 static UINTN efipart_nhandles = 0;
108 static pdinfo_t *
109 efiblk_get_pdinfo(pdinfo_list_t *pdi, int unit)
111 pdinfo_t *pd;
113 STAILQ_FOREACH(pd, pdi, pd_link) {
114 if (pd->pd_unit == unit)
115 return (pd);
117 return (NULL);
120 static int
121 efiblk_pdinfo_count(pdinfo_list_t *pdi)
123 pdinfo_t *pd;
124 int i = 0;
126 STAILQ_FOREACH(pd, pdi, pd_link) {
127 i++;
129 return (i);
132 static int
133 efipart_inithandles(void)
135 UINTN sz;
136 EFI_HANDLE *hin;
137 EFI_STATUS status;
139 if (efipart_nhandles != 0) {
140 free(efipart_handles);
141 efipart_handles = NULL;
142 efipart_nhandles = 0;
145 sz = 0;
146 hin = NULL;
147 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, hin);
148 if (status == EFI_BUFFER_TOO_SMALL) {
149 hin = malloc(sz);
150 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
151 hin);
152 if (EFI_ERROR(status))
153 free(hin);
155 if (EFI_ERROR(status))
156 return (efi_status_to_errno(status));
158 efipart_handles = hin;
159 efipart_nhandles = sz;
160 return (0);
163 static ACPI_HID_DEVICE_PATH *
164 efipart_floppy(EFI_DEVICE_PATH *node)
166 ACPI_HID_DEVICE_PATH *acpi;
168 if (DevicePathType(node) == ACPI_DEVICE_PATH &&
169 DevicePathSubType(node) == ACPI_DP) {
170 acpi = (ACPI_HID_DEVICE_PATH *) node;
171 if (acpi->HID == EISA_PNP_ID(PNP0604) ||
172 acpi->HID == EISA_PNP_ID(PNP0700) ||
173 acpi->HID == EISA_PNP_ID(PNP0701)) {
174 return (acpi);
177 return (NULL);
181 * Determine if the provided device path is hdd.
183 * There really is no simple fool proof way to classify the devices.
184 * Since we do build three lists of devices - floppy, cd and hdd, we
185 * will try to see if the device is floppy or cd, and list anything else
186 * as hdd.
188 static bool
189 efipart_hdd(EFI_DEVICE_PATH *dp)
191 unsigned i, nin;
192 EFI_DEVICE_PATH *devpath, *node;
193 EFI_BLOCK_IO *blkio;
194 EFI_STATUS status;
196 if (dp == NULL)
197 return (false);
199 if ((node = efi_devpath_last_node(dp)) == NULL)
200 return (false);
202 if (efipart_floppy(node) != NULL)
203 return (false);
206 * Test every EFI BLOCK IO handle to make sure dp is not device path
207 * for CD/DVD.
209 nin = efipart_nhandles / sizeof (*efipart_handles);
210 for (i = 0; i < nin; i++) {
211 devpath = efi_lookup_devpath(efipart_handles[i]);
212 if (devpath == NULL)
213 return (false);
215 /* Only continue testing when dp is prefix in devpath. */
216 if (!efi_devpath_is_prefix(dp, devpath))
217 continue;
220 * The device path has to have last node describing the
221 * device, or we can not test the type.
223 if ((node = efi_devpath_last_node(devpath)) == NULL)
224 return (false);
226 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
227 DevicePathSubType(node) == MEDIA_CDROM_DP) {
228 return (false);
231 /* Make sure we do have the media. */
232 status = BS->HandleProtocol(efipart_handles[i],
233 &blkio_guid, (void **)&blkio);
234 if (EFI_ERROR(status))
235 return (false);
237 /* USB or SATA cd without the media. */
238 if (blkio->Media->RemovableMedia &&
239 !blkio->Media->MediaPresent) {
240 return (false);
244 * We assume the block size 512 or greater power of 2.
245 * iPXE is known to insert stub BLOCK IO device with
246 * BlockSize 1.
248 if (blkio->Media->BlockSize < 512 ||
249 !powerof2(blkio->Media->BlockSize)) {
250 return (false);
253 return (true);
257 * Add or update entries with new handle data.
259 static int
260 efipart_fdinfo_add(EFI_HANDLE handle, uint32_t uid, EFI_DEVICE_PATH *devpath)
262 pdinfo_t *fd;
264 fd = calloc(1, sizeof(pdinfo_t));
265 if (fd == NULL) {
266 printf("Failed to register floppy %d, out of memory\n", uid);
267 return (ENOMEM);
269 STAILQ_INIT(&fd->pd_part);
271 fd->pd_unit = uid;
272 fd->pd_handle = handle;
273 fd->pd_devpath = devpath;
274 STAILQ_INSERT_TAIL(&fdinfo, fd, pd_link);
275 return (0);
278 static void
279 efipart_updatefd(void)
281 EFI_DEVICE_PATH *devpath, *node;
282 ACPI_HID_DEVICE_PATH *acpi;
283 int i, nin;
285 nin = efipart_nhandles / sizeof (*efipart_handles);
286 for (i = 0; i < nin; i++) {
287 devpath = efi_lookup_devpath(efipart_handles[i]);
288 if (devpath == NULL)
289 continue;
291 if ((node = efi_devpath_last_node(devpath)) == NULL)
292 continue;
293 if ((acpi = efipart_floppy(node)) != NULL) {
294 efipart_fdinfo_add(efipart_handles[i], acpi->UID,
295 devpath);
300 static int
301 efipart_initfd(void)
303 int rv;
305 rv = efipart_inithandles();
306 if (rv != 0)
307 return (rv);
308 STAILQ_INIT(&fdinfo);
310 efipart_updatefd();
312 bcache_add_dev(efiblk_pdinfo_count(&fdinfo));
313 return (0);
317 * Add or update entries with new handle data.
319 static int
320 efipart_cdinfo_add(EFI_HANDLE handle, EFI_HANDLE alias,
321 EFI_DEVICE_PATH *devpath)
323 int unit;
324 pdinfo_t *cd;
325 pdinfo_t *pd;
327 unit = 0;
328 STAILQ_FOREACH(pd, &cdinfo, pd_link) {
329 if (efi_devpath_match(pd->pd_devpath, devpath) == true) {
330 pd->pd_handle = handle;
331 pd->pd_alias = alias;
332 return (0);
334 unit++;
337 cd = calloc(1, sizeof(pdinfo_t));
338 if (cd == NULL) {
339 printf("Failed to add cd %d, out of memory\n", unit);
340 return (ENOMEM);
342 STAILQ_INIT(&cd->pd_part);
344 cd->pd_handle = handle;
345 cd->pd_unit = unit;
346 cd->pd_alias = alias;
347 cd->pd_devpath = devpath;
348 STAILQ_INSERT_TAIL(&cdinfo, cd, pd_link);
349 return (0);
352 static void
353 efipart_updatecd(void)
355 int i, nin;
356 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
357 EFI_HANDLE handle;
358 EFI_BLOCK_IO *blkio;
359 EFI_STATUS status;
361 nin = efipart_nhandles / sizeof (*efipart_handles);
362 for (i = 0; i < nin; i++) {
363 devpath = efi_lookup_devpath(efipart_handles[i]);
364 if (devpath == NULL)
365 continue;
367 if ((node = efi_devpath_last_node(devpath)) == NULL)
368 continue;
370 if (efipart_floppy(node) != NULL)
371 continue;
373 if (efipart_hdd(devpath))
374 continue;
376 status = BS->HandleProtocol(efipart_handles[i],
377 &blkio_guid, (void **)&blkio);
378 if (EFI_ERROR(status))
379 continue;
381 * If we come across a logical partition of subtype CDROM
382 * it doesn't refer to the CD filesystem itself, but rather
383 * to any usable El Torito boot image on it. In this case
384 * we try to find the parent device and add that instead as
385 * that will be the CD filesystem.
387 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
388 DevicePathSubType(node) == MEDIA_CDROM_DP) {
389 devpathcpy = efi_devpath_trim(devpath);
390 if (devpathcpy == NULL)
391 continue;
392 tmpdevpath = devpathcpy;
393 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
394 &handle);
395 free(devpathcpy);
396 if (EFI_ERROR(status))
397 continue;
398 devpath = efi_lookup_devpath(handle);
399 efipart_cdinfo_add(handle, efipart_handles[i],
400 devpath);
401 continue;
404 if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
405 DevicePathSubType(node) == MSG_ATAPI_DP) {
406 efipart_cdinfo_add(efipart_handles[i], NULL,
407 devpath);
408 continue;
411 /* USB or SATA cd without the media. */
412 if (blkio->Media->RemovableMedia &&
413 !blkio->Media->MediaPresent) {
414 efipart_cdinfo_add(efipart_handles[i], NULL,
415 devpath);
420 static int
421 efipart_initcd(void)
423 int rv;
425 rv = efipart_inithandles();
426 if (rv != 0)
427 return (rv);
428 STAILQ_INIT(&cdinfo);
430 efipart_updatecd();
432 bcache_add_dev(efiblk_pdinfo_count(&cdinfo));
433 return (0);
436 static int
437 efipart_hdinfo_add(EFI_HANDLE disk_handle, EFI_HANDLE part_handle)
439 EFI_DEVICE_PATH *disk_devpath, *part_devpath;
440 HARDDRIVE_DEVICE_PATH *node;
441 int unit;
442 pdinfo_t *hd, *pd, *last;
444 disk_devpath = efi_lookup_devpath(disk_handle);
445 if (disk_devpath == NULL)
446 return (ENOENT);
448 if (part_handle != NULL) {
449 part_devpath = efi_lookup_devpath(part_handle);
450 if (part_devpath == NULL)
451 return (ENOENT);
452 node = (HARDDRIVE_DEVICE_PATH *)
453 efi_devpath_last_node(part_devpath);
454 if (node == NULL)
455 return (ENOENT); /* This should not happen. */
456 } else {
457 part_devpath = NULL;
458 node = NULL;
461 pd = calloc(1, sizeof(pdinfo_t));
462 if (pd == NULL) {
463 printf("Failed to add disk, out of memory\n");
464 return (ENOMEM);
466 STAILQ_INIT(&pd->pd_part);
468 STAILQ_FOREACH(hd, &hdinfo, pd_link) {
469 if (efi_devpath_match(hd->pd_devpath, disk_devpath) == true) {
470 if (part_devpath == NULL)
471 return (0);
473 /* Add the partition. */
474 pd->pd_handle = part_handle;
475 pd->pd_unit = node->PartitionNumber;
476 pd->pd_devpath = part_devpath;
477 STAILQ_INSERT_TAIL(&hd->pd_part, pd, pd_link);
478 return (0);
482 last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
483 if (last != NULL)
484 unit = last->pd_unit + 1;
485 else
486 unit = 0;
488 /* Add the disk. */
489 hd = pd;
490 hd->pd_handle = disk_handle;
491 hd->pd_unit = unit;
492 hd->pd_devpath = disk_devpath;
493 STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link);
495 if (part_devpath == NULL)
496 return (0);
498 pd = calloc(1, sizeof(pdinfo_t));
499 if (pd == NULL) {
500 printf("Failed to add partition, out of memory\n");
501 return (ENOMEM);
503 STAILQ_INIT(&pd->pd_part);
505 /* Add the partition. */
506 pd->pd_handle = part_handle;
507 pd->pd_unit = node->PartitionNumber;
508 pd->pd_devpath = part_devpath;
509 STAILQ_INSERT_TAIL(&hd->pd_part, pd, pd_link);
511 return (0);
515 * The MEDIA_FILEPATH_DP has device name.
516 * From U-Boot sources it looks like names are in the form
517 * of typeN:M, where type is interface type, N is disk id
518 * and M is partition id.
520 static int
521 efipart_hdinfo_add_filepath(EFI_HANDLE disk_handle)
523 EFI_DEVICE_PATH *devpath;
524 FILEPATH_DEVICE_PATH *node;
525 char *pathname, *p;
526 int unit, len;
527 pdinfo_t *pd, *last;
529 /* First collect and verify all the data */
530 if ((devpath = efi_lookup_devpath(disk_handle)) == NULL)
531 return (ENOENT);
532 node = (FILEPATH_DEVICE_PATH *)efi_devpath_last_node(devpath);
533 if (node == NULL)
534 return (ENOENT); /* This should not happen. */
536 pd = calloc(1, sizeof(pdinfo_t));
537 if (pd == NULL) {
538 printf("Failed to add disk, out of memory\n");
539 return (ENOMEM);
541 STAILQ_INIT(&pd->pd_part);
542 last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
543 if (last != NULL)
544 unit = last->pd_unit + 1;
545 else
546 unit = 0;
548 /* FILEPATH_DEVICE_PATH has 0 terminated string */
549 for (len = 0; node->PathName[len] != 0; len++)
551 if ((pathname = malloc(len + 1)) == NULL) {
552 printf("Failed to add disk, out of memory\n");
553 free(pd);
554 return (ENOMEM);
556 cpy16to8(node->PathName, pathname, len + 1);
557 p = strchr(pathname, ':');
560 * Assume we are receiving handles in order, first disk handle,
561 * then partitions for this disk. If this assumption proves
562 * false, this code would need update.
564 if (p == NULL) { /* no colon, add the disk */
565 pd->pd_handle = disk_handle;
566 pd->pd_unit = unit;
567 pd->pd_devpath = devpath;
568 STAILQ_INSERT_TAIL(&hdinfo, pd, pd_link);
569 free(pathname);
570 return (0);
572 p++; /* skip the colon */
573 errno = 0;
574 unit = (int)strtol(p, NULL, 0);
575 if (errno != 0) {
576 printf("Bad unit number for partition \"%s\"\n", pathname);
577 free(pathname);
578 free(pd);
579 return (EUNIT);
583 * We should have disk registered, if not, we are receiving
584 * handles out of order, and this code should be reworked
585 * to create "blank" disk for partition, and to find the
586 * disk based on PathName compares.
588 if (last == NULL) {
589 printf("BUG: No disk for partition \"%s\"\n", pathname);
590 free(pathname);
591 free(pd);
592 return (EINVAL);
594 /* Add the partition. */
595 pd->pd_handle = disk_handle;
596 pd->pd_unit = unit;
597 pd->pd_devpath = devpath;
598 STAILQ_INSERT_TAIL(&last->pd_part, pd, pd_link);
599 free(pathname);
600 return (0);
603 static void
604 efipart_updatehd(void)
606 int i, nin;
607 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
608 EFI_HANDLE handle;
609 EFI_BLOCK_IO *blkio;
610 EFI_STATUS status;
612 nin = efipart_nhandles / sizeof (*efipart_handles);
613 for (i = 0; i < nin; i++) {
614 devpath = efi_lookup_devpath(efipart_handles[i]);
615 if (devpath == NULL)
616 continue;
618 if ((node = efi_devpath_last_node(devpath)) == NULL)
619 continue;
621 if (!efipart_hdd(devpath))
622 continue;
624 status = BS->HandleProtocol(efipart_handles[i],
625 &blkio_guid, (void **)&blkio);
626 if (EFI_ERROR(status))
627 continue;
629 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
630 DevicePathSubType(node) == MEDIA_FILEPATH_DP) {
631 efipart_hdinfo_add_filepath(efipart_handles[i]);
632 continue;
635 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
636 DevicePathSubType(node) == MEDIA_HARDDRIVE_DP) {
637 devpathcpy = efi_devpath_trim(devpath);
638 if (devpathcpy == NULL)
639 continue;
640 tmpdevpath = devpathcpy;
641 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
642 &handle);
643 free(devpathcpy);
644 if (EFI_ERROR(status))
645 continue;
647 * We do not support nested partitions.
649 devpathcpy = efi_lookup_devpath(handle);
650 if (devpathcpy == NULL)
651 continue;
652 if ((node = efi_devpath_last_node(devpathcpy)) == NULL)
653 continue;
655 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
656 DevicePathSubType(node) == MEDIA_HARDDRIVE_DP)
657 continue;
659 efipart_hdinfo_add(handle, efipart_handles[i]);
660 continue;
663 efipart_hdinfo_add(efipart_handles[i], NULL);
667 static int
668 efipart_inithd(void)
670 int rv;
672 rv = efipart_inithandles();
673 if (rv != 0)
674 return (rv);
675 STAILQ_INIT(&hdinfo);
677 efipart_updatehd();
679 bcache_add_dev(efiblk_pdinfo_count(&hdinfo));
680 return (0);
683 static int
684 efipart_print_common(struct devsw *dev, pdinfo_list_t *pdlist, int verbose)
686 int ret = 0;
687 EFI_BLOCK_IO *blkio;
688 EFI_STATUS status;
689 EFI_HANDLE h;
690 pdinfo_t *pd;
691 CHAR16 *text;
692 struct disk_devdesc pd_dev;
693 char line[80];
695 if (STAILQ_EMPTY(pdlist))
696 return (0);
698 printf("%s devices:", dev->dv_name);
699 if ((ret = pager_output("\n")) != 0)
700 return (ret);
702 STAILQ_FOREACH(pd, pdlist, pd_link) {
703 h = pd->pd_handle;
704 if (verbose) { /* Output the device path. */
705 text = efi_devpath_name(efi_lookup_devpath(h));
706 if (text != NULL) {
707 printf(" %S", text);
708 efi_free_devpath_name(text);
709 if ((ret = pager_output("\n")) != 0)
710 break;
713 snprintf(line, sizeof(line),
714 " %s%d", dev->dv_name, pd->pd_unit);
715 printf("%s:", line);
716 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
717 if (!EFI_ERROR(status)) {
718 printf(" %llu",
719 blkio->Media->LastBlock == 0? 0:
720 (unsigned long long) (blkio->Media->LastBlock + 1));
721 if (blkio->Media->LastBlock != 0) {
722 printf(" X %u", blkio->Media->BlockSize);
724 printf(" blocks");
725 if (blkio->Media->MediaPresent) {
726 if (blkio->Media->RemovableMedia)
727 printf(" (removable)");
728 } else {
729 printf(" (no media)");
731 if ((ret = pager_output("\n")) != 0)
732 break;
733 if (!blkio->Media->MediaPresent)
734 continue;
736 pd->pd_blkio = blkio;
737 pd_dev.d_dev = dev;
738 pd_dev.d_unit = pd->pd_unit;
739 pd_dev.d_slice = -1;
740 pd_dev.d_partition = -1;
741 pd_dev.d_opendata = blkio;
742 ret = disk_open(&pd_dev, blkio->Media->BlockSize *
743 (blkio->Media->LastBlock + 1),
744 blkio->Media->BlockSize);
745 if (ret == 0) {
746 ret = disk_print(&pd_dev, line, verbose);
747 disk_close(&pd_dev);
748 if (ret != 0)
749 return (ret);
750 } else {
751 /* Do not fail from disk_open() */
752 ret = 0;
754 } else {
755 if ((ret = pager_output("\n")) != 0)
756 break;
759 return (ret);
762 static int
763 efipart_printfd(int verbose)
765 return (efipart_print_common(&efipart_fddev, &fdinfo, verbose));
768 static int
769 efipart_printcd(int verbose)
771 return (efipart_print_common(&efipart_cddev, &cdinfo, verbose));
774 static int
775 efipart_printhd(int verbose)
777 return (efipart_print_common(&efipart_hddev, &hdinfo, verbose));
780 pdinfo_list_t *
781 efiblk_get_pdinfo_list(struct devsw *dev)
783 if (dev->dv_type == DEVT_DISK)
784 return (&hdinfo);
785 if (dev->dv_type == DEVT_CD)
786 return (&cdinfo);
787 if (dev->dv_type == DEVT_FD)
788 return (&fdinfo);
789 return (NULL);
792 static int
793 efipart_open(struct open_file *f, ...)
795 va_list args;
796 struct disk_devdesc *dev;
797 pdinfo_list_t *pdi;
798 pdinfo_t *pd;
799 EFI_BLOCK_IO *blkio;
800 EFI_STATUS status;
802 va_start(args, f);
803 dev = va_arg(args, struct disk_devdesc*);
804 va_end(args);
805 if (dev == NULL)
806 return (EINVAL);
808 pdi = efiblk_get_pdinfo_list(dev->d_dev);
809 if (pdi == NULL)
810 return (EINVAL);
812 pd = efiblk_get_pdinfo(pdi, dev->d_unit);
813 if (pd == NULL)
814 return (EIO);
816 if (pd->pd_blkio == NULL) {
817 status = BS->HandleProtocol(pd->pd_handle, &blkio_guid,
818 (void **)&pd->pd_blkio);
819 if (EFI_ERROR(status))
820 return (efi_status_to_errno(status));
823 blkio = pd->pd_blkio;
824 if (!blkio->Media->MediaPresent)
825 return (EAGAIN);
827 pd->pd_open++;
828 if (pd->pd_bcache == NULL)
829 pd->pd_bcache = bcache_allocate();
831 if (dev->d_dev->dv_type == DEVT_DISK) {
832 int rc;
834 rc = disk_open(dev,
835 blkio->Media->BlockSize * (blkio->Media->LastBlock + 1),
836 blkio->Media->BlockSize);
837 if (rc != 0) {
838 pd->pd_open--;
839 if (pd->pd_open == 0) {
840 pd->pd_blkio = NULL;
841 bcache_free(pd->pd_bcache);
842 pd->pd_bcache = NULL;
845 return (rc);
847 return (0);
850 static int
851 efipart_close(struct open_file *f)
853 struct disk_devdesc *dev;
854 pdinfo_list_t *pdi;
855 pdinfo_t *pd;
857 dev = (struct disk_devdesc *)(f->f_devdata);
858 if (dev == NULL)
859 return (EINVAL);
860 pdi = efiblk_get_pdinfo_list(dev->d_dev);
861 if (pdi == NULL)
862 return (EINVAL);
864 pd = efiblk_get_pdinfo(pdi, dev->d_unit);
865 if (pd == NULL)
866 return (EINVAL);
868 pd->pd_open--;
869 if (pd->pd_open == 0) {
870 pd->pd_blkio = NULL;
871 bcache_free(pd->pd_bcache);
872 pd->pd_bcache = NULL;
874 if (dev->d_dev->dv_type == DEVT_DISK)
875 return (disk_close(dev));
876 return (0);
879 static int
880 efipart_ioctl(struct open_file *f, u_long cmd, void *data)
882 struct disk_devdesc *dev;
883 pdinfo_list_t *pdi;
884 pdinfo_t *pd;
885 int rc;
887 dev = (struct disk_devdesc *)(f->f_devdata);
888 if (dev == NULL)
889 return (EINVAL);
890 pdi = efiblk_get_pdinfo_list(dev->d_dev);
891 if (pdi == NULL)
892 return (EINVAL);
894 pd = efiblk_get_pdinfo(pdi, dev->d_unit);
895 if (pd == NULL)
896 return (EINVAL);
898 if (dev->d_dev->dv_type == DEVT_DISK) {
899 rc = disk_ioctl(dev, cmd, data);
900 if (rc != ENOTTY)
901 return (rc);
904 switch (cmd) {
905 case DIOCGSECTORSIZE:
906 *(u_int *)data = pd->pd_blkio->Media->BlockSize;
907 break;
908 case DIOCGMEDIASIZE:
909 *(uint64_t *)data = pd->pd_blkio->Media->BlockSize *
910 (pd->pd_blkio->Media->LastBlock + 1);
911 break;
912 default:
913 return (ENOTTY);
916 return (0);
920 * efipart_readwrite()
921 * Internal equivalent of efipart_strategy(), which operates on the
922 * media-native block size. This function expects all I/O requests
923 * to be within the media size and returns an error if such is not
924 * the case.
926 static int
927 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
928 char *buf)
930 EFI_STATUS status;
932 if (blkio == NULL)
933 return (ENXIO);
934 if (blk < 0 || blk > blkio->Media->LastBlock)
935 return (EIO);
936 if ((blk + nblks - 1) > blkio->Media->LastBlock)
937 return (EIO);
939 switch (rw & F_MASK) {
940 case F_READ:
941 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
942 nblks * blkio->Media->BlockSize, buf);
943 break;
944 case F_WRITE:
945 if (blkio->Media->ReadOnly)
946 return (EROFS);
947 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
948 nblks * blkio->Media->BlockSize, buf);
949 break;
950 default:
951 return (ENOSYS);
954 if (EFI_ERROR(status)) {
955 printf("%s: rw=%d, blk=%ju size=%ju status=%lu\n", __func__, rw,
956 blk, nblks, EFI_ERROR_CODE(status));
958 return (efi_status_to_errno(status));
961 static int
962 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size,
963 char *buf, size_t *rsize)
965 struct bcache_devdata bcd;
966 struct disk_devdesc *dev;
967 pdinfo_list_t *pdi;
968 pdinfo_t *pd;
970 dev = (struct disk_devdesc *)devdata;
971 if (dev == NULL)
972 return (EINVAL);
973 pdi = efiblk_get_pdinfo_list(dev->d_dev);
974 if (pdi == NULL)
975 return (EINVAL);
977 pd = efiblk_get_pdinfo(pdi, dev->d_unit);
978 if (pd == NULL)
979 return (EINVAL);
981 if (pd->pd_blkio->Media->RemovableMedia &&
982 !pd->pd_blkio->Media->MediaPresent)
983 return (ENXIO);
985 bcd.dv_strategy = efipart_realstrategy;
986 bcd.dv_devdata = devdata;
987 bcd.dv_cache = pd->pd_bcache;
989 if (dev->d_dev->dv_type == DEVT_DISK) {
990 daddr_t offset;
992 offset = dev->d_offset * pd->pd_blkio->Media->BlockSize;
993 offset /= 512;
994 return (bcache_strategy(&bcd, rw, blk + offset,
995 size, buf, rsize));
997 return (bcache_strategy(&bcd, rw, blk, size, buf, rsize));
1000 static int
1001 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t size,
1002 char *buf, size_t *rsize)
1004 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
1005 pdinfo_list_t *pdi;
1006 pdinfo_t *pd;
1007 EFI_BLOCK_IO *blkio;
1008 uint64_t off, disk_blocks, d_offset = 0;
1009 char *blkbuf;
1010 size_t blkoff, blksz;
1011 int error;
1012 uint64_t diskend, readstart;
1014 if (dev == NULL || blk < 0)
1015 return (EINVAL);
1017 pdi = efiblk_get_pdinfo_list(dev->d_dev);
1018 if (pdi == NULL)
1019 return (EINVAL);
1021 pd = efiblk_get_pdinfo(pdi, dev->d_unit);
1022 if (pd == NULL)
1023 return (EINVAL);
1025 blkio = pd->pd_blkio;
1026 if (blkio == NULL)
1027 return (ENXIO);
1029 if (size == 0 || (size % 512) != 0)
1030 return (EIO);
1032 off = blk * 512;
1034 * Get disk blocks, this value is either for whole disk or for
1035 * partition.
1037 disk_blocks = 0;
1038 if (dev->d_dev->dv_type == DEVT_DISK) {
1039 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1040 /* DIOCGMEDIASIZE does return bytes. */
1041 disk_blocks /= blkio->Media->BlockSize;
1043 d_offset = dev->d_offset;
1045 if (disk_blocks == 0)
1046 disk_blocks = blkio->Media->LastBlock + 1 - d_offset;
1048 /* make sure we don't read past disk end */
1049 if ((off + size) / blkio->Media->BlockSize > d_offset + disk_blocks) {
1050 diskend = d_offset + disk_blocks;
1051 readstart = off / blkio->Media->BlockSize;
1053 if (diskend <= readstart) {
1054 if (rsize != NULL)
1055 *rsize = 0;
1057 return (EIO);
1059 size = diskend - readstart;
1060 size = size * blkio->Media->BlockSize;
1063 if (rsize != NULL)
1064 *rsize = size;
1066 if ((size % blkio->Media->BlockSize == 0) &&
1067 (off % blkio->Media->BlockSize == 0))
1068 return (efipart_readwrite(blkio, rw,
1069 off / blkio->Media->BlockSize,
1070 size / blkio->Media->BlockSize, buf));
1073 * The buffer size is not a multiple of the media block size.
1075 blkbuf = malloc(blkio->Media->BlockSize);
1076 if (blkbuf == NULL)
1077 return (ENOMEM);
1079 error = 0;
1080 blk = off / blkio->Media->BlockSize;
1081 blkoff = off % blkio->Media->BlockSize;
1082 blksz = blkio->Media->BlockSize - blkoff;
1083 while (size > 0) {
1084 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
1085 if (error)
1086 break;
1087 if (size < blksz)
1088 blksz = size;
1089 bcopy(blkbuf + blkoff, buf, blksz);
1090 buf += blksz;
1091 size -= blksz;
1092 blk++;
1093 blkoff = 0;
1094 blksz = blkio->Media->BlockSize;
1097 free(blkbuf);
1098 return (error);