8960 libefi: import efichar.c for ucs2 support
[unleashed.git] / usr / src / boot / sys / boot / efi / libefi / efipart.c
blobe2ea9bb95a6fc173fdabda0ebe06e670bb9a4421
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 <efichar.h>
42 #include <disk.h>
44 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
46 static int efipart_initfd(void);
47 static int efipart_initcd(void);
48 static int efipart_inithd(void);
50 static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *);
51 static int efipart_realstrategy(void *, int, daddr_t, size_t, char *, size_t *);
53 static int efipart_open(struct open_file *, ...);
54 static int efipart_close(struct open_file *);
55 static int efipart_ioctl(struct open_file *, u_long, void *);
57 static int efipart_printfd(int);
58 static int efipart_printcd(int);
59 static int efipart_printhd(int);
61 /* EISA PNP ID's for floppy controllers */
62 #define PNP0604 0x604
63 #define PNP0700 0x700
64 #define PNP0701 0x701
66 struct devsw efipart_fddev = {
67 .dv_name = "fd",
68 .dv_type = DEVT_FD,
69 .dv_init = efipart_initfd,
70 .dv_strategy = efipart_strategy,
71 .dv_open = efipart_open,
72 .dv_close = efipart_close,
73 .dv_ioctl = efipart_ioctl,
74 .dv_print = efipart_printfd,
75 .dv_cleanup = NULL
78 struct devsw efipart_cddev = {
79 .dv_name = "cd",
80 .dv_type = DEVT_CD,
81 .dv_init = efipart_initcd,
82 .dv_strategy = efipart_strategy,
83 .dv_open = efipart_open,
84 .dv_close = efipart_close,
85 .dv_ioctl = efipart_ioctl,
86 .dv_print = efipart_printcd,
87 .dv_cleanup = NULL
90 struct devsw efipart_hddev = {
91 .dv_name = "disk",
92 .dv_type = DEVT_DISK,
93 .dv_init = efipart_inithd,
94 .dv_strategy = efipart_strategy,
95 .dv_open = efipart_open,
96 .dv_close = efipart_close,
97 .dv_ioctl = efipart_ioctl,
98 .dv_print = efipart_printhd,
99 .dv_cleanup = NULL
102 static pdinfo_list_t fdinfo;
103 static pdinfo_list_t cdinfo;
104 static pdinfo_list_t hdinfo;
106 static EFI_HANDLE *efipart_handles = NULL;
107 static UINTN efipart_nhandles = 0;
109 static pdinfo_t *
110 efiblk_get_pdinfo(pdinfo_list_t *pdi, int unit)
112 pdinfo_t *pd;
114 STAILQ_FOREACH(pd, pdi, pd_link) {
115 if (pd->pd_unit == unit)
116 return (pd);
118 return (NULL);
121 static int
122 efiblk_pdinfo_count(pdinfo_list_t *pdi)
124 pdinfo_t *pd;
125 int i = 0;
127 STAILQ_FOREACH(pd, pdi, pd_link) {
128 i++;
130 return (i);
133 static int
134 efipart_inithandles(void)
136 UINTN sz;
137 EFI_HANDLE *hin;
138 EFI_STATUS status;
140 if (efipart_nhandles != 0) {
141 free(efipart_handles);
142 efipart_handles = NULL;
143 efipart_nhandles = 0;
146 sz = 0;
147 hin = NULL;
148 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, hin);
149 if (status == EFI_BUFFER_TOO_SMALL) {
150 hin = malloc(sz);
151 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
152 hin);
153 if (EFI_ERROR(status))
154 free(hin);
156 if (EFI_ERROR(status))
157 return (efi_status_to_errno(status));
159 efipart_handles = hin;
160 efipart_nhandles = sz;
161 return (0);
164 static ACPI_HID_DEVICE_PATH *
165 efipart_floppy(EFI_DEVICE_PATH *node)
167 ACPI_HID_DEVICE_PATH *acpi;
169 if (DevicePathType(node) == ACPI_DEVICE_PATH &&
170 DevicePathSubType(node) == ACPI_DP) {
171 acpi = (ACPI_HID_DEVICE_PATH *) node;
172 if (acpi->HID == EISA_PNP_ID(PNP0604) ||
173 acpi->HID == EISA_PNP_ID(PNP0700) ||
174 acpi->HID == EISA_PNP_ID(PNP0701)) {
175 return (acpi);
178 return (NULL);
182 * Determine if the provided device path is hdd.
184 * There really is no simple fool proof way to classify the devices.
185 * Since we do build three lists of devices - floppy, cd and hdd, we
186 * will try to see if the device is floppy or cd, and list anything else
187 * as hdd.
189 static bool
190 efipart_hdd(EFI_DEVICE_PATH *dp)
192 unsigned i, nin;
193 EFI_DEVICE_PATH *devpath, *node;
194 EFI_BLOCK_IO *blkio;
195 EFI_STATUS status;
197 if (dp == NULL)
198 return (false);
200 if ((node = efi_devpath_last_node(dp)) == NULL)
201 return (false);
203 if (efipart_floppy(node) != NULL)
204 return (false);
207 * Test every EFI BLOCK IO handle to make sure dp is not device path
208 * for CD/DVD.
210 nin = efipart_nhandles / sizeof (*efipart_handles);
211 for (i = 0; i < nin; i++) {
212 devpath = efi_lookup_devpath(efipart_handles[i]);
213 if (devpath == NULL)
214 return (false);
216 /* Only continue testing when dp is prefix in devpath. */
217 if (!efi_devpath_is_prefix(dp, devpath))
218 continue;
221 * The device path has to have last node describing the
222 * device, or we can not test the type.
224 if ((node = efi_devpath_last_node(devpath)) == NULL)
225 return (false);
227 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
228 DevicePathSubType(node) == MEDIA_CDROM_DP) {
229 return (false);
232 /* Make sure we do have the media. */
233 status = BS->HandleProtocol(efipart_handles[i],
234 &blkio_guid, (void **)&blkio);
235 if (EFI_ERROR(status))
236 return (false);
238 /* USB or SATA cd without the media. */
239 if (blkio->Media->RemovableMedia &&
240 !blkio->Media->MediaPresent) {
241 return (false);
245 * We assume the block size 512 or greater power of 2.
246 * iPXE is known to insert stub BLOCK IO device with
247 * BlockSize 1.
249 if (blkio->Media->BlockSize < 512 ||
250 !powerof2(blkio->Media->BlockSize)) {
251 return (false);
254 return (true);
258 * Add or update entries with new handle data.
260 static int
261 efipart_fdinfo_add(EFI_HANDLE handle, uint32_t uid, EFI_DEVICE_PATH *devpath)
263 pdinfo_t *fd;
265 fd = calloc(1, sizeof(pdinfo_t));
266 if (fd == NULL) {
267 printf("Failed to register floppy %d, out of memory\n", uid);
268 return (ENOMEM);
270 STAILQ_INIT(&fd->pd_part);
272 fd->pd_unit = uid;
273 fd->pd_handle = handle;
274 fd->pd_devpath = devpath;
275 STAILQ_INSERT_TAIL(&fdinfo, fd, pd_link);
276 return (0);
279 static void
280 efipart_updatefd(void)
282 EFI_DEVICE_PATH *devpath, *node;
283 ACPI_HID_DEVICE_PATH *acpi;
284 int i, nin;
286 nin = efipart_nhandles / sizeof (*efipart_handles);
287 for (i = 0; i < nin; i++) {
288 devpath = efi_lookup_devpath(efipart_handles[i]);
289 if (devpath == NULL)
290 continue;
292 if ((node = efi_devpath_last_node(devpath)) == NULL)
293 continue;
294 if ((acpi = efipart_floppy(node)) != NULL) {
295 efipart_fdinfo_add(efipart_handles[i], acpi->UID,
296 devpath);
301 static int
302 efipart_initfd(void)
304 int rv;
306 rv = efipart_inithandles();
307 if (rv != 0)
308 return (rv);
309 STAILQ_INIT(&fdinfo);
311 efipart_updatefd();
313 bcache_add_dev(efiblk_pdinfo_count(&fdinfo));
314 return (0);
318 * Add or update entries with new handle data.
320 static int
321 efipart_cdinfo_add(EFI_HANDLE handle, EFI_HANDLE alias,
322 EFI_DEVICE_PATH *devpath)
324 int unit;
325 pdinfo_t *cd;
326 pdinfo_t *pd;
328 unit = 0;
329 STAILQ_FOREACH(pd, &cdinfo, pd_link) {
330 if (efi_devpath_match(pd->pd_devpath, devpath) == true) {
331 pd->pd_handle = handle;
332 pd->pd_alias = alias;
333 return (0);
335 unit++;
338 cd = calloc(1, sizeof(pdinfo_t));
339 if (cd == NULL) {
340 printf("Failed to add cd %d, out of memory\n", unit);
341 return (ENOMEM);
343 STAILQ_INIT(&cd->pd_part);
345 cd->pd_handle = handle;
346 cd->pd_unit = unit;
347 cd->pd_alias = alias;
348 cd->pd_devpath = devpath;
349 STAILQ_INSERT_TAIL(&cdinfo, cd, pd_link);
350 return (0);
353 static void
354 efipart_updatecd(void)
356 int i, nin;
357 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
358 EFI_HANDLE handle;
359 EFI_BLOCK_IO *blkio;
360 EFI_STATUS status;
362 nin = efipart_nhandles / sizeof (*efipart_handles);
363 for (i = 0; i < nin; i++) {
364 devpath = efi_lookup_devpath(efipart_handles[i]);
365 if (devpath == NULL)
366 continue;
368 if ((node = efi_devpath_last_node(devpath)) == NULL)
369 continue;
371 if (efipart_floppy(node) != NULL)
372 continue;
374 if (efipart_hdd(devpath))
375 continue;
377 status = BS->HandleProtocol(efipart_handles[i],
378 &blkio_guid, (void **)&blkio);
379 if (EFI_ERROR(status))
380 continue;
382 * If we come across a logical partition of subtype CDROM
383 * it doesn't refer to the CD filesystem itself, but rather
384 * to any usable El Torito boot image on it. In this case
385 * we try to find the parent device and add that instead as
386 * that will be the CD filesystem.
388 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
389 DevicePathSubType(node) == MEDIA_CDROM_DP) {
390 devpathcpy = efi_devpath_trim(devpath);
391 if (devpathcpy == NULL)
392 continue;
393 tmpdevpath = devpathcpy;
394 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
395 &handle);
396 free(devpathcpy);
397 if (EFI_ERROR(status))
398 continue;
399 devpath = efi_lookup_devpath(handle);
400 efipart_cdinfo_add(handle, efipart_handles[i],
401 devpath);
402 continue;
405 if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
406 DevicePathSubType(node) == MSG_ATAPI_DP) {
407 efipart_cdinfo_add(efipart_handles[i], NULL,
408 devpath);
409 continue;
412 /* USB or SATA cd without the media. */
413 if (blkio->Media->RemovableMedia &&
414 !blkio->Media->MediaPresent) {
415 efipart_cdinfo_add(efipart_handles[i], NULL,
416 devpath);
421 static int
422 efipart_initcd(void)
424 int rv;
426 rv = efipart_inithandles();
427 if (rv != 0)
428 return (rv);
429 STAILQ_INIT(&cdinfo);
431 efipart_updatecd();
433 bcache_add_dev(efiblk_pdinfo_count(&cdinfo));
434 return (0);
437 static int
438 efipart_hdinfo_add(EFI_HANDLE disk_handle, EFI_HANDLE part_handle)
440 EFI_DEVICE_PATH *disk_devpath, *part_devpath;
441 HARDDRIVE_DEVICE_PATH *node;
442 int unit;
443 pdinfo_t *hd, *pd, *last;
445 disk_devpath = efi_lookup_devpath(disk_handle);
446 if (disk_devpath == NULL)
447 return (ENOENT);
449 if (part_handle != NULL) {
450 part_devpath = efi_lookup_devpath(part_handle);
451 if (part_devpath == NULL)
452 return (ENOENT);
453 node = (HARDDRIVE_DEVICE_PATH *)
454 efi_devpath_last_node(part_devpath);
455 if (node == NULL)
456 return (ENOENT); /* This should not happen. */
457 } else {
458 part_devpath = NULL;
459 node = NULL;
462 pd = calloc(1, sizeof(pdinfo_t));
463 if (pd == NULL) {
464 printf("Failed to add disk, out of memory\n");
465 return (ENOMEM);
467 STAILQ_INIT(&pd->pd_part);
469 STAILQ_FOREACH(hd, &hdinfo, pd_link) {
470 if (efi_devpath_match(hd->pd_devpath, disk_devpath) == true) {
471 if (part_devpath == NULL)
472 return (0);
474 /* Add the partition. */
475 pd->pd_handle = part_handle;
476 pd->pd_unit = node->PartitionNumber;
477 pd->pd_devpath = part_devpath;
478 STAILQ_INSERT_TAIL(&hd->pd_part, pd, pd_link);
479 return (0);
483 last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
484 if (last != NULL)
485 unit = last->pd_unit + 1;
486 else
487 unit = 0;
489 /* Add the disk. */
490 hd = pd;
491 hd->pd_handle = disk_handle;
492 hd->pd_unit = unit;
493 hd->pd_devpath = disk_devpath;
494 STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link);
496 if (part_devpath == NULL)
497 return (0);
499 pd = calloc(1, sizeof(pdinfo_t));
500 if (pd == NULL) {
501 printf("Failed to add partition, out of memory\n");
502 return (ENOMEM);
504 STAILQ_INIT(&pd->pd_part);
506 /* Add the partition. */
507 pd->pd_handle = part_handle;
508 pd->pd_unit = node->PartitionNumber;
509 pd->pd_devpath = part_devpath;
510 STAILQ_INSERT_TAIL(&hd->pd_part, pd, pd_link);
512 return (0);
516 * The MEDIA_FILEPATH_DP has device name.
517 * From U-Boot sources it looks like names are in the form
518 * of typeN:M, where type is interface type, N is disk id
519 * and M is partition id.
521 static int
522 efipart_hdinfo_add_filepath(EFI_HANDLE disk_handle)
524 EFI_DEVICE_PATH *devpath;
525 FILEPATH_DEVICE_PATH *node;
526 char *pathname, *p;
527 int unit, len;
528 pdinfo_t *pd, *last;
530 /* First collect and verify all the data */
531 if ((devpath = efi_lookup_devpath(disk_handle)) == NULL)
532 return (ENOENT);
533 node = (FILEPATH_DEVICE_PATH *)efi_devpath_last_node(devpath);
534 if (node == NULL)
535 return (ENOENT); /* This should not happen. */
537 pd = calloc(1, sizeof(pdinfo_t));
538 if (pd == NULL) {
539 printf("Failed to add disk, out of memory\n");
540 return (ENOMEM);
542 STAILQ_INIT(&pd->pd_part);
543 last = STAILQ_LAST(&hdinfo, pdinfo, pd_link);
544 if (last != NULL)
545 unit = last->pd_unit + 1;
546 else
547 unit = 0;
549 /* FILEPATH_DEVICE_PATH has 0 terminated string */
550 len = ucs2len(node->PathName);
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);