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