Home | History | Annotate | Line # | Download | only in efiboot
efiblock.c revision 1.18
      1 /* $NetBSD: efiblock.c,v 1.18 2021/10/30 11:18:51 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
      5  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #define FSTYPENAMES
     31 
     32 #include <sys/param.h>
     33 #include <sys/md5.h>
     34 #include <sys/uuid.h>
     35 
     36 #include <fs/cd9660/iso.h>
     37 
     38 #include "efiboot.h"
     39 #include "efiblock.h"
     40 
     41 #define	EFI_BLOCK_READAHEAD	(64 * 1024)
     42 #define	EFI_BLOCK_TIMEOUT	120
     43 #define	EFI_BLOCK_TIMEOUT_CODE	0x810c0000
     44 
     45 /*
     46  * The raidframe support is basic.  Ideally, it should be expanded to
     47  * consider raid volumes a first-class citizen like the x86 efiboot does,
     48  * but for now, we simply assume each RAID is potentially bootable.
     49  */
     50 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
     51 
     52 static EFI_HANDLE *efi_block;
     53 static UINTN efi_nblock;
     54 static struct efi_block_part *efi_block_booted = NULL;
     55 
     56 static bool efi_ra_enable = false;
     57 static UINT8 *efi_ra_buffer = NULL;
     58 static UINT32 efi_ra_media_id;
     59 static UINT64 efi_ra_start = 0;
     60 static UINT64 efi_ra_length = 0;
     61 
     62 static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
     63 
     64 static int
     65 efi_block_parse(const char *fname, struct efi_block_part **pbpart, char **pfile)
     66 {
     67 	struct efi_block_dev *bdev;
     68 	struct efi_block_part *bpart;
     69 	char pathbuf[PATH_MAX], *default_device, *ep = NULL;
     70 	const char *full_path;
     71 	intmax_t dev;
     72 	int part;
     73 
     74 	default_device = get_default_device();
     75 	if (strchr(fname, ':') == NULL) {
     76 		if (strlen(default_device) > 0) {
     77 			snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
     78 			full_path = pathbuf;
     79 			*pfile = __UNCONST(fname);
     80 		} else {
     81 			return EINVAL;
     82 		}
     83 	} else {
     84 		full_path = fname;
     85 		*pfile = strchr(fname, ':') + 1;
     86 	}
     87 
     88 	if (*pfile[0] == '\0') {
     89 		*pfile = __UNCONST("/");
     90 	}
     91 
     92 	if (strncasecmp(full_path, "hd", 2) != 0)
     93 		return EINVAL;
     94 	dev = strtoimax(full_path + 2, &ep, 10);
     95 	if (dev < 0 || dev >= efi_nblock)
     96 		return ENXIO;
     97 	if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
     98 		return EINVAL;
     99 	part = ep[0] - 'a';
    100 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
    101 		if (bdev->index == dev) {
    102 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    103 				if (bpart->index == part) {
    104 					*pbpart = bpart;
    105 					return 0;
    106 				}
    107 			}
    108 		}
    109 	}
    110 
    111 	return ENOENT;
    112 }
    113 
    114 static void
    115 efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
    116 {
    117 	MD5_CTX md5ctx;
    118 
    119 	MD5Init(&md5ctx);
    120 	MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
    121 	MD5Final(bpart->hash, &md5ctx);
    122 }
    123 
    124 static EFI_STATUS
    125 efi_block_do_read_blockio(struct efi_block_dev *bdev, UINT64 off, void *buf,
    126     UINTN bufsize)
    127 {
    128 	UINT8 *blkbuf, *blkbuf_start;
    129 	EFI_STATUS status;
    130 	EFI_LBA lba_start, lba_end;
    131 	UINT64 blkbuf_offset;
    132 	UINT64 blkbuf_size;
    133 
    134 	lba_start = off / bdev->bio->Media->BlockSize;
    135 	lba_end = (off + bufsize + bdev->bio->Media->BlockSize - 1) /
    136 	    bdev->bio->Media->BlockSize;
    137 	blkbuf_offset = off % bdev->bio->Media->BlockSize;
    138 	blkbuf_size = (lba_end - lba_start) * bdev->bio->Media->BlockSize;
    139 	if (bdev->bio->Media->IoAlign > 1) {
    140 		blkbuf_size = (blkbuf_size + bdev->bio->Media->IoAlign - 1) /
    141 		    bdev->bio->Media->IoAlign *
    142 		    bdev->bio->Media->IoAlign;
    143 	}
    144 
    145 	blkbuf = AllocatePool(blkbuf_size);
    146 	if (blkbuf == NULL) {
    147 		return EFI_OUT_OF_RESOURCES;
    148 	}
    149 
    150 	if (bdev->bio->Media->IoAlign > 1) {
    151 		blkbuf_start = (void *)roundup2((intptr_t)blkbuf,
    152 		    bdev->bio->Media->IoAlign);
    153 	} else {
    154 		blkbuf_start = blkbuf;
    155 	}
    156 
    157 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio,
    158 	    bdev->media_id, lba_start, blkbuf_size, blkbuf_start);
    159 	if (EFI_ERROR(status)) {
    160 		goto done;
    161 	}
    162 
    163 	memcpy(buf, blkbuf_start + blkbuf_offset, bufsize);
    164 
    165 done:
    166 	FreePool(blkbuf);
    167 	return status;
    168 }
    169 
    170 static EFI_STATUS
    171 efi_block_do_read_diskio(struct efi_block_dev *bdev, UINT64 off, void *buf,
    172     UINTN bufsize)
    173 {
    174 	return uefi_call_wrapper(bdev->dio->ReadDisk, 5, bdev->dio,
    175 	    bdev->media_id, off, bufsize, buf);
    176 }
    177 
    178 static EFI_STATUS
    179 efi_block_do_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
    180     UINTN bufsize)
    181 {
    182 	/*
    183 	 * Perform read access using EFI_DISK_IO_PROTOCOL if available,
    184 	 * otherwise use EFI_BLOCK_IO_PROTOCOL.
    185 	 */
    186 	if (bdev->dio != NULL) {
    187 		return efi_block_do_read_diskio(bdev, off, buf, bufsize);
    188 	} else {
    189 		return efi_block_do_read_blockio(bdev, off, buf, bufsize);
    190 	}
    191 }
    192 
    193 static EFI_STATUS
    194 efi_block_readahead(struct efi_block_dev *bdev, UINT64 off, void *buf,
    195     UINTN bufsize)
    196 {
    197 	EFI_STATUS status;
    198 	UINT64 mediasize, len;
    199 
    200 	if (efi_ra_buffer == NULL) {
    201 		efi_ra_buffer = AllocatePool(EFI_BLOCK_READAHEAD);
    202 		if (efi_ra_buffer == NULL) {
    203 			return EFI_OUT_OF_RESOURCES;
    204 		}
    205 	}
    206 
    207 	if (bdev->media_id != efi_ra_media_id ||
    208 	    off < efi_ra_start ||
    209 	    off + bufsize > efi_ra_start + efi_ra_length) {
    210 		mediasize = bdev->bio->Media->BlockSize *
    211 		    (bdev->bio->Media->LastBlock + 1);
    212 		len = EFI_BLOCK_READAHEAD;
    213 		if (len > mediasize - off) {
    214 			len = mediasize - off;
    215 		}
    216 		status = efi_block_do_read(bdev, off, efi_ra_buffer, len);
    217 		if (EFI_ERROR(status)) {
    218 			efi_ra_start = efi_ra_length = 0;
    219 			return status;
    220 		}
    221 		efi_ra_start = off;
    222 		efi_ra_length = len;
    223 		efi_ra_media_id = bdev->media_id;
    224 	}
    225 
    226 	memcpy(buf, &efi_ra_buffer[off - efi_ra_start], bufsize);
    227 	return EFI_SUCCESS;
    228 }
    229 
    230 static EFI_STATUS
    231 efi_block_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
    232     UINTN bufsize)
    233 {
    234 	if (efi_ra_enable) {
    235 		return efi_block_readahead(bdev, off, buf, bufsize);
    236 	}
    237 
    238 	return efi_block_do_read(bdev, off, buf, bufsize);
    239 }
    240 
    241 static int
    242 efi_block_find_partitions_cd9660(struct efi_block_dev *bdev)
    243 {
    244 	struct efi_block_part *bpart;
    245 	struct iso_primary_descriptor vd;
    246 	EFI_STATUS status;
    247 	EFI_LBA lba;
    248 
    249 	for (lba = 16;; lba++) {
    250 		status = efi_block_read(bdev,
    251 		    lba * ISO_DEFAULT_BLOCK_SIZE, &vd, sizeof(vd));
    252 		if (EFI_ERROR(status)) {
    253 			goto io_error;
    254 		}
    255 
    256 		if (memcmp(vd.id, ISO_STANDARD_ID, sizeof vd.id) != 0) {
    257 			goto io_error;
    258 		}
    259 		if (isonum_711(vd.type) == ISO_VD_END) {
    260 			goto io_error;
    261 		}
    262 		if (isonum_711(vd.type) == ISO_VD_PRIMARY) {
    263 			break;
    264 		}
    265 	}
    266 
    267 	if (isonum_723(vd.logical_block_size) != ISO_DEFAULT_BLOCK_SIZE) {
    268 		goto io_error;
    269 	}
    270 
    271 	bpart = alloc(sizeof(*bpart));
    272 	bpart->index = 0;
    273 	bpart->bdev = bdev;
    274 	bpart->type = EFI_BLOCK_PART_CD9660;
    275 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    276 
    277 	return 0;
    278 
    279 io_error:
    280 	return EIO;
    281 }
    282 
    283 static int
    284 efi_block_find_partitions_disklabel(struct efi_block_dev *bdev,
    285     struct mbr_sector *mbr, uint32_t start, uint32_t size)
    286 {
    287 	struct efi_block_part *bpart;
    288 	char buf[DEV_BSIZE];
    289 	struct disklabel d;
    290 	struct partition *p;
    291 	EFI_STATUS status;
    292 	int n;
    293 
    294 	status = efi_block_read(bdev,
    295 	    ((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE, buf, sizeof(buf));
    296 	if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL) {
    297 		FreePool(buf);
    298 		return EIO;
    299 	}
    300 
    301 	if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
    302 		return EINVAL;
    303 	if (le16toh(d.d_npartitions) > MAXPARTITIONS)
    304 		return EINVAL;
    305 
    306 	for (n = 0; n < le16toh(d.d_npartitions); n++) {
    307 		p = &d.d_partitions[n];
    308 		switch (p->p_fstype) {
    309 		case FS_BSDFFS:
    310 		case FS_MSDOS:
    311 		case FS_BSDLFS:
    312 			break;
    313 		case FS_RAID:
    314 			p->p_size -= RF_PROTECTED_SECTORS;
    315 			p->p_offset += RF_PROTECTED_SECTORS;
    316 			break;
    317 		default:
    318 			continue;
    319 		}
    320 
    321 		bpart = alloc(sizeof(*bpart));
    322 		bpart->index = n;
    323 		bpart->bdev = bdev;
    324 		bpart->type = EFI_BLOCK_PART_DISKLABEL;
    325 		bpart->disklabel.secsize = d.d_secsize;
    326 		bpart->disklabel.part = *p;
    327 		efi_block_generate_hash_mbr(bpart, mbr);
    328 		TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    329 	}
    330 
    331 	return 0;
    332 }
    333 
    334 static int
    335 efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
    336 {
    337 	struct mbr_sector mbr;
    338 	struct mbr_partition *mbr_part;
    339 	EFI_STATUS status;
    340 	int n;
    341 
    342 	status = efi_block_read(bdev, 0, &mbr, sizeof(mbr));
    343 	if (EFI_ERROR(status))
    344 		return EIO;
    345 
    346 	if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
    347 		return ENOENT;
    348 
    349 	for (n = 0; n < MBR_PART_COUNT; n++) {
    350 		mbr_part = &mbr.mbr_parts[n];
    351 		if (le32toh(mbr_part->mbrp_size) == 0)
    352 			continue;
    353 		if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
    354 			efi_block_find_partitions_disklabel(bdev, &mbr,
    355 			    le32toh(mbr_part->mbrp_start),
    356 			    le32toh(mbr_part->mbrp_size));
    357 			break;
    358 		}
    359 	}
    360 
    361 	return 0;
    362 }
    363 
    364 static const struct {
    365 	struct uuid guid;
    366 	uint8_t fstype;
    367 } gpt_guid_to_str[] = {
    368 	{ GPT_ENT_TYPE_NETBSD_FFS,		FS_BSDFFS },
    369 	{ GPT_ENT_TYPE_NETBSD_LFS,		FS_BSDLFS },
    370 	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME,	FS_RAID },
    371 	{ GPT_ENT_TYPE_NETBSD_CCD,		FS_CCD },
    372 	{ GPT_ENT_TYPE_NETBSD_CGD,		FS_CGD },
    373 	{ GPT_ENT_TYPE_MS_BASIC_DATA,		FS_MSDOS },	/* or NTFS? ambiguous */
    374 	{ GPT_ENT_TYPE_EFI,			FS_MSDOS },
    375 };
    376 
    377 static int
    378 efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev,
    379     struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
    380 {
    381 	struct efi_block_part *bpart;
    382 	uint8_t fstype = FS_UNUSED;
    383 	struct uuid uuid;
    384 	int n;
    385 
    386 	memcpy(&uuid, ent->ent_type, sizeof(uuid));
    387 	for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
    388 		if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid,
    389 		    sizeof(ent->ent_type)) == 0) {
    390 			fstype = gpt_guid_to_str[n].fstype;
    391 			break;
    392 		}
    393 	if (fstype == FS_UNUSED)
    394 		return 0;
    395 
    396 	bpart = alloc(sizeof(*bpart));
    397 	bpart->index = index;
    398 	bpart->bdev = bdev;
    399 	bpart->type = EFI_BLOCK_PART_GPT;
    400 	bpart->gpt.fstype = fstype;
    401 	bpart->gpt.ent = *ent;
    402 	if (fstype == FS_RAID) {
    403 		bpart->gpt.ent.ent_lba_start += RF_PROTECTED_SECTORS;
    404 		bpart->gpt.ent.ent_lba_end -= RF_PROTECTED_SECTORS;
    405 	}
    406 	memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
    407 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    408 
    409 	return 0;
    410 }
    411 
    412 static int
    413 efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
    414 {
    415 	struct gpt_hdr hdr;
    416 	struct gpt_ent ent;
    417 	EFI_STATUS status;
    418 	UINT32 entry;
    419 	void *buf;
    420 	UINTN sz;
    421 
    422 	status = efi_block_read(bdev, GPT_HDR_BLKNO * DEV_BSIZE, &hdr,
    423 	    sizeof(hdr));
    424 	if (EFI_ERROR(status)) {
    425 		return EIO;
    426 	}
    427 
    428 	if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
    429 		return ENOENT;
    430 	if (le32toh(hdr.hdr_entsz) < sizeof(ent))
    431 		return EINVAL;
    432 
    433 	sz = le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries);
    434 	buf = AllocatePool(sz);
    435 	if (buf == NULL)
    436 		return ENOMEM;
    437 
    438 	status = efi_block_read(bdev,
    439 	    le64toh(hdr.hdr_lba_table) * DEV_BSIZE, buf, sz);
    440 	if (EFI_ERROR(status)) {
    441 		FreePool(buf);
    442 		return EIO;
    443 	}
    444 
    445 	for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
    446 		memcpy(&ent, buf + (entry * le32toh(hdr.hdr_entsz)),
    447 			sizeof(ent));
    448 		efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
    449 	}
    450 
    451 	FreePool(buf);
    452 
    453 	return 0;
    454 }
    455 
    456 static int
    457 efi_block_find_partitions(struct efi_block_dev *bdev)
    458 {
    459 	int error;
    460 
    461 	error = efi_block_find_partitions_gpt(bdev);
    462 	if (error)
    463 		error = efi_block_find_partitions_mbr(bdev);
    464 	if (error)
    465 		error = efi_block_find_partitions_cd9660(bdev);
    466 
    467 	return error;
    468 }
    469 
    470 void
    471 efi_block_probe(void)
    472 {
    473 	struct efi_block_dev *bdev;
    474 	struct efi_block_part *bpart;
    475 	EFI_BLOCK_IO *bio;
    476 	EFI_DISK_IO *dio;
    477 	EFI_STATUS status;
    478 	uint16_t devindex = 0;
    479 	int depth = -1;
    480 	int n;
    481 
    482 	status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
    483 	if (EFI_ERROR(status))
    484 		return;
    485 
    486 	if (efi_bootdp) {
    487 		depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
    488 		if (depth == 0)
    489 			depth = 1;
    490 		else if (depth == -1)
    491 			depth = 2;
    492 	}
    493 
    494 	for (n = 0; n < efi_nblock; n++) {
    495 		/* EFI_BLOCK_IO_PROTOCOL is required */
    496 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n],
    497 		    &BlockIoProtocol, (void **)&bio);
    498 		if (EFI_ERROR(status) || !bio->Media->MediaPresent)
    499 			continue;
    500 
    501 		/* Ignore logical partitions (we do our own partition discovery) */
    502 		if (bio->Media->LogicalPartition)
    503 			continue;
    504 
    505 		/* EFI_DISK_IO_PROTOCOL is optional */
    506 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n],
    507 		    &DiskIoProtocol, (void **)&dio);
    508 		if (EFI_ERROR(status)) {
    509 			dio = NULL;
    510 		}
    511 
    512 		bdev = alloc(sizeof(*bdev));
    513 		bdev->index = devindex++;
    514 		bdev->bio = bio;
    515 		bdev->dio = dio;
    516 		bdev->media_id = bio->Media->MediaId;
    517 		bdev->path = DevicePathFromHandle(efi_block[n]);
    518 		TAILQ_INIT(&bdev->partitions);
    519 		TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
    520 
    521 		efi_block_find_partitions(bdev);
    522 
    523 		if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
    524 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    525 				uint8_t fstype = FS_UNUSED;
    526 				switch (bpart->type) {
    527 				case EFI_BLOCK_PART_DISKLABEL:
    528 					fstype = bpart->disklabel.part.p_fstype;
    529 					break;
    530 				case EFI_BLOCK_PART_GPT:
    531 					fstype = bpart->gpt.fstype;
    532 					break;
    533 				case EFI_BLOCK_PART_CD9660:
    534 					fstype = FS_ISO9660;
    535 					break;
    536 				}
    537 				if (fstype == FS_BSDFFS || fstype == FS_ISO9660 || fstype == FS_RAID) {
    538 					char devname[9];
    539 					snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
    540 					set_default_device(devname);
    541 					set_default_fstype(fstype);
    542 					break;
    543 				}
    544 			}
    545 		}
    546 	}
    547 }
    548 
    549 static void
    550 print_guid(const uint8_t *guid)
    551 {
    552 	const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
    553 	int i;
    554 
    555 	for (i = 0; i < 16; i++) {
    556 		printf("%02x", guid[index[i]]);
    557 		if (i == 3 || i == 5 || i == 7 || i == 9)
    558 			printf("-");
    559 	}
    560 }
    561 
    562 void
    563 efi_block_show(void)
    564 {
    565 	struct efi_block_dev *bdev;
    566 	struct efi_block_part *bpart;
    567 	uint64_t size;
    568 	CHAR16 *path;
    569 
    570 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
    571 		printf("hd%u (", bdev->index);
    572 
    573 		/* Size in MB */
    574 		size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
    575 		if (size >= 10000)
    576 			printf("%"PRIu64" GB", size / 1024);
    577 		else
    578 			printf("%"PRIu64" MB", size);
    579 		printf("): ");
    580 
    581 		path = DevicePathToStr(bdev->path);
    582 		Print(L"%s", path);
    583 		FreePool(path);
    584 
    585 		printf("\n");
    586 
    587 		TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    588 			switch (bpart->type) {
    589 			case EFI_BLOCK_PART_DISKLABEL:
    590 				printf("  hd%u%c (", bdev->index, bpart->index + 'a');
    591 
    592 				/* Size in MB */
    593 				size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
    594 				if (size >= 10000)
    595 					printf("%"PRIu64" GB", size / 1024);
    596 				else
    597 					printf("%"PRIu64" MB", size);
    598 				printf("): ");
    599 
    600 				printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
    601 				break;
    602 			case EFI_BLOCK_PART_GPT:
    603 				printf("  hd%u%c ", bdev->index, bpart->index + 'a');
    604 
    605 				if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    606 					printf("\"");
    607 					print_guid(bpart->gpt.ent.ent_guid);
    608 					printf("\"");
    609 				} else {
    610 					Print(L"\"%s\"", bpart->gpt.ent.ent_name);
    611 				}
    612 
    613 				/* Size in MB */
    614 				size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
    615 				size /= (1024 * 1024);
    616 				if (size >= 10000)
    617 					printf(" (%"PRIu64" GB): ", size / 1024);
    618 				else
    619 					printf(" (%"PRIu64" MB): ", size);
    620 
    621 				printf("%s\n", fstypenames[bpart->gpt.fstype]);
    622 				break;
    623 			case EFI_BLOCK_PART_CD9660:
    624 				printf("  hd%u%c %s\n", bdev->index, bpart->index + 'a', fstypenames[FS_ISO9660]);
    625 				break;
    626 			default:
    627 				break;
    628 			}
    629 		}
    630 	}
    631 }
    632 
    633 struct efi_block_part *
    634 efi_block_boot_part(void)
    635 {
    636 	return efi_block_booted;
    637 }
    638 
    639 int
    640 efi_block_open(struct open_file *f, ...)
    641 {
    642 	struct efi_block_part *bpart;
    643 	const char *fname;
    644 	char **file;
    645 	char *path;
    646 	va_list ap;
    647 	int rv, n;
    648 
    649 	va_start(ap, f);
    650 	fname = va_arg(ap, const char *);
    651 	file = va_arg(ap, char **);
    652 	va_end(ap);
    653 
    654 	rv = efi_block_parse(fname, &bpart, &path);
    655 	if (rv != 0)
    656 		return rv;
    657 
    658 	for (n = 0; n < ndevs; n++)
    659 		if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
    660 			f->f_dev = &devsw[n];
    661 			break;
    662 		}
    663 	if (n == ndevs)
    664 		return ENXIO;
    665 
    666 	f->f_devdata = bpart;
    667 
    668 	*file = path;
    669 
    670 	efi_block_booted = bpart;
    671 
    672 	return 0;
    673 }
    674 
    675 int
    676 efi_block_close(struct open_file *f)
    677 {
    678 	return 0;
    679 }
    680 
    681 int
    682 efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    683 {
    684 	struct efi_block_part *bpart = devdata;
    685 	EFI_STATUS status;
    686 	UINT64 off;
    687 
    688 	if (rw != F_READ)
    689 		return EROFS;
    690 
    691 	efi_set_watchdog(EFI_BLOCK_TIMEOUT, EFI_BLOCK_TIMEOUT_CODE);
    692 
    693 	switch (bpart->type) {
    694 	case EFI_BLOCK_PART_DISKLABEL:
    695 		off = (dblk + bpart->disklabel.part.p_offset) * DEV_BSIZE;
    696 		break;
    697 	case EFI_BLOCK_PART_GPT:
    698 		off = (dblk + le64toh(bpart->gpt.ent.ent_lba_start)) * DEV_BSIZE;
    699 		break;
    700 	case EFI_BLOCK_PART_CD9660:
    701 		off = dblk * ISO_DEFAULT_BLOCK_SIZE;
    702 		break;
    703 	default:
    704 		return EINVAL;
    705 	}
    706 
    707 	status = efi_block_read(bpart->bdev, off, buf, size);
    708 	if (EFI_ERROR(status))
    709 		return EIO;
    710 
    711 	*rsize = size;
    712 
    713 	return 0;
    714 }
    715 
    716 void
    717 efi_block_set_readahead(bool onoff)
    718 {
    719 	efi_ra_enable = onoff;
    720 }
    721