Home | History | Annotate | Line # | Download | only in efiboot
efiblock.c revision 1.17
      1 /* $NetBSD: efiblock.c,v 1.17 2021/06/23 21:42:43 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 (strncasecmp(full_path, "hd", 2) != 0)
     89 		return EINVAL;
     90 	dev = strtoimax(full_path + 2, &ep, 10);
     91 	if (dev < 0 || dev >= efi_nblock)
     92 		return ENXIO;
     93 	if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
     94 		return EINVAL;
     95 	part = ep[0] - 'a';
     96 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
     97 		if (bdev->index == dev) {
     98 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
     99 				if (bpart->index == part) {
    100 					*pbpart = bpart;
    101 					return 0;
    102 				}
    103 			}
    104 		}
    105 	}
    106 
    107 	return ENOENT;
    108 }
    109 
    110 static void
    111 efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
    112 {
    113 	MD5_CTX md5ctx;
    114 
    115 	MD5Init(&md5ctx);
    116 	MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
    117 	MD5Final(bpart->hash, &md5ctx);
    118 }
    119 
    120 static EFI_STATUS
    121 efi_block_do_read_blockio(struct efi_block_dev *bdev, UINT64 off, void *buf,
    122     UINTN bufsize)
    123 {
    124 	UINT8 *blkbuf, *blkbuf_start;
    125 	EFI_STATUS status;
    126 	EFI_LBA lba_start, lba_end;
    127 	UINT64 blkbuf_offset;
    128 	UINT64 blkbuf_size;
    129 
    130 	lba_start = off / bdev->bio->Media->BlockSize;
    131 	lba_end = (off + bufsize + bdev->bio->Media->BlockSize - 1) /
    132 	    bdev->bio->Media->BlockSize;
    133 	blkbuf_offset = off % bdev->bio->Media->BlockSize;
    134 	blkbuf_size = (lba_end - lba_start) * bdev->bio->Media->BlockSize;
    135 	if (bdev->bio->Media->IoAlign > 1) {
    136 		blkbuf_size = (blkbuf_size + bdev->bio->Media->IoAlign - 1) /
    137 		    bdev->bio->Media->IoAlign *
    138 		    bdev->bio->Media->IoAlign;
    139 	}
    140 
    141 	blkbuf = AllocatePool(blkbuf_size);
    142 	if (blkbuf == NULL) {
    143 		return EFI_OUT_OF_RESOURCES;
    144 	}
    145 
    146 	if (bdev->bio->Media->IoAlign > 1) {
    147 		blkbuf_start = (void *)roundup2((intptr_t)blkbuf,
    148 		    bdev->bio->Media->IoAlign);
    149 	} else {
    150 		blkbuf_start = blkbuf;
    151 	}
    152 
    153 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio,
    154 	    bdev->media_id, lba_start, blkbuf_size, blkbuf_start);
    155 	if (EFI_ERROR(status)) {
    156 		goto done;
    157 	}
    158 
    159 	memcpy(buf, blkbuf_start + blkbuf_offset, bufsize);
    160 
    161 done:
    162 	FreePool(blkbuf);
    163 	return status;
    164 }
    165 
    166 static EFI_STATUS
    167 efi_block_do_read_diskio(struct efi_block_dev *bdev, UINT64 off, void *buf,
    168     UINTN bufsize)
    169 {
    170 	return uefi_call_wrapper(bdev->dio->ReadDisk, 5, bdev->dio,
    171 	    bdev->media_id, off, bufsize, buf);
    172 }
    173 
    174 static EFI_STATUS
    175 efi_block_do_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
    176     UINTN bufsize)
    177 {
    178 	/*
    179 	 * Perform read access using EFI_DISK_IO_PROTOCOL if available,
    180 	 * otherwise use EFI_BLOCK_IO_PROTOCOL.
    181 	 */
    182 	if (bdev->dio != NULL) {
    183 		return efi_block_do_read_diskio(bdev, off, buf, bufsize);
    184 	} else {
    185 		return efi_block_do_read_blockio(bdev, off, buf, bufsize);
    186 	}
    187 }
    188 
    189 static EFI_STATUS
    190 efi_block_readahead(struct efi_block_dev *bdev, UINT64 off, void *buf,
    191     UINTN bufsize)
    192 {
    193 	EFI_STATUS status;
    194 	UINT64 mediasize, len;
    195 
    196 	if (efi_ra_buffer == NULL) {
    197 		efi_ra_buffer = AllocatePool(EFI_BLOCK_READAHEAD);
    198 		if (efi_ra_buffer == NULL) {
    199 			return EFI_OUT_OF_RESOURCES;
    200 		}
    201 	}
    202 
    203 	if (bdev->media_id != efi_ra_media_id ||
    204 	    off < efi_ra_start ||
    205 	    off + bufsize > efi_ra_start + efi_ra_length) {
    206 		mediasize = bdev->bio->Media->BlockSize *
    207 		    (bdev->bio->Media->LastBlock + 1);
    208 		len = EFI_BLOCK_READAHEAD;
    209 		if (len > mediasize - off) {
    210 			len = mediasize - off;
    211 		}
    212 		status = efi_block_do_read(bdev, off, efi_ra_buffer, len);
    213 		if (EFI_ERROR(status)) {
    214 			efi_ra_start = efi_ra_length = 0;
    215 			return status;
    216 		}
    217 		efi_ra_start = off;
    218 		efi_ra_length = len;
    219 		efi_ra_media_id = bdev->media_id;
    220 	}
    221 
    222 	memcpy(buf, &efi_ra_buffer[off - efi_ra_start], bufsize);
    223 	return EFI_SUCCESS;
    224 }
    225 
    226 static EFI_STATUS
    227 efi_block_read(struct efi_block_dev *bdev, UINT64 off, void *buf,
    228     UINTN bufsize)
    229 {
    230 	if (efi_ra_enable) {
    231 		return efi_block_readahead(bdev, off, buf, bufsize);
    232 	}
    233 
    234 	return efi_block_do_read(bdev, off, buf, bufsize);
    235 }
    236 
    237 static int
    238 efi_block_find_partitions_cd9660(struct efi_block_dev *bdev)
    239 {
    240 	struct efi_block_part *bpart;
    241 	struct iso_primary_descriptor vd;
    242 	EFI_STATUS status;
    243 	EFI_LBA lba;
    244 
    245 	for (lba = 16;; lba++) {
    246 		status = efi_block_read(bdev,
    247 		    lba * ISO_DEFAULT_BLOCK_SIZE, &vd, sizeof(vd));
    248 		if (EFI_ERROR(status)) {
    249 			goto io_error;
    250 		}
    251 
    252 		if (memcmp(vd.id, ISO_STANDARD_ID, sizeof vd.id) != 0) {
    253 			goto io_error;
    254 		}
    255 		if (isonum_711(vd.type) == ISO_VD_END) {
    256 			goto io_error;
    257 		}
    258 		if (isonum_711(vd.type) == ISO_VD_PRIMARY) {
    259 			break;
    260 		}
    261 	}
    262 
    263 	if (isonum_723(vd.logical_block_size) != ISO_DEFAULT_BLOCK_SIZE) {
    264 		goto io_error;
    265 	}
    266 
    267 	bpart = alloc(sizeof(*bpart));
    268 	bpart->index = 0;
    269 	bpart->bdev = bdev;
    270 	bpart->type = EFI_BLOCK_PART_CD9660;
    271 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    272 
    273 	return 0;
    274 
    275 io_error:
    276 	return EIO;
    277 }
    278 
    279 static int
    280 efi_block_find_partitions_disklabel(struct efi_block_dev *bdev,
    281     struct mbr_sector *mbr, uint32_t start, uint32_t size)
    282 {
    283 	struct efi_block_part *bpart;
    284 	char buf[DEV_BSIZE];
    285 	struct disklabel d;
    286 	struct partition *p;
    287 	EFI_STATUS status;
    288 	int n;
    289 
    290 	status = efi_block_read(bdev,
    291 	    ((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE, buf, sizeof(buf));
    292 	if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL) {
    293 		FreePool(buf);
    294 		return EIO;
    295 	}
    296 
    297 	if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
    298 		return EINVAL;
    299 	if (le16toh(d.d_npartitions) > MAXPARTITIONS)
    300 		return EINVAL;
    301 
    302 	for (n = 0; n < le16toh(d.d_npartitions); n++) {
    303 		p = &d.d_partitions[n];
    304 		switch (p->p_fstype) {
    305 		case FS_BSDFFS:
    306 		case FS_MSDOS:
    307 		case FS_BSDLFS:
    308 			break;
    309 		case FS_RAID:
    310 			p->p_size -= RF_PROTECTED_SECTORS;
    311 			p->p_offset += RF_PROTECTED_SECTORS;
    312 			break;
    313 		default:
    314 			continue;
    315 		}
    316 
    317 		bpart = alloc(sizeof(*bpart));
    318 		bpart->index = n;
    319 		bpart->bdev = bdev;
    320 		bpart->type = EFI_BLOCK_PART_DISKLABEL;
    321 		bpart->disklabel.secsize = d.d_secsize;
    322 		bpart->disklabel.part = *p;
    323 		efi_block_generate_hash_mbr(bpart, mbr);
    324 		TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    325 	}
    326 
    327 	return 0;
    328 }
    329 
    330 static int
    331 efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
    332 {
    333 	struct mbr_sector mbr;
    334 	struct mbr_partition *mbr_part;
    335 	EFI_STATUS status;
    336 	int n;
    337 
    338 	status = efi_block_read(bdev, 0, &mbr, sizeof(mbr));
    339 	if (EFI_ERROR(status))
    340 		return EIO;
    341 
    342 	if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
    343 		return ENOENT;
    344 
    345 	for (n = 0; n < MBR_PART_COUNT; n++) {
    346 		mbr_part = &mbr.mbr_parts[n];
    347 		if (le32toh(mbr_part->mbrp_size) == 0)
    348 			continue;
    349 		if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
    350 			efi_block_find_partitions_disklabel(bdev, &mbr,
    351 			    le32toh(mbr_part->mbrp_start),
    352 			    le32toh(mbr_part->mbrp_size));
    353 			break;
    354 		}
    355 	}
    356 
    357 	return 0;
    358 }
    359 
    360 static const struct {
    361 	struct uuid guid;
    362 	uint8_t fstype;
    363 } gpt_guid_to_str[] = {
    364 	{ GPT_ENT_TYPE_NETBSD_FFS,		FS_BSDFFS },
    365 	{ GPT_ENT_TYPE_NETBSD_LFS,		FS_BSDLFS },
    366 	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME,	FS_RAID },
    367 	{ GPT_ENT_TYPE_NETBSD_CCD,		FS_CCD },
    368 	{ GPT_ENT_TYPE_NETBSD_CGD,		FS_CGD },
    369 	{ GPT_ENT_TYPE_MS_BASIC_DATA,		FS_MSDOS },	/* or NTFS? ambiguous */
    370 	{ GPT_ENT_TYPE_EFI,			FS_MSDOS },
    371 };
    372 
    373 static int
    374 efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev,
    375     struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
    376 {
    377 	struct efi_block_part *bpart;
    378 	uint8_t fstype = FS_UNUSED;
    379 	struct uuid uuid;
    380 	int n;
    381 
    382 	memcpy(&uuid, ent->ent_type, sizeof(uuid));
    383 	for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
    384 		if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid,
    385 		    sizeof(ent->ent_type)) == 0) {
    386 			fstype = gpt_guid_to_str[n].fstype;
    387 			break;
    388 		}
    389 	if (fstype == FS_UNUSED)
    390 		return 0;
    391 
    392 	bpart = alloc(sizeof(*bpart));
    393 	bpart->index = index;
    394 	bpart->bdev = bdev;
    395 	bpart->type = EFI_BLOCK_PART_GPT;
    396 	bpart->gpt.fstype = fstype;
    397 	bpart->gpt.ent = *ent;
    398 	if (fstype == FS_RAID) {
    399 		bpart->gpt.ent.ent_lba_start += RF_PROTECTED_SECTORS;
    400 		bpart->gpt.ent.ent_lba_end -= RF_PROTECTED_SECTORS;
    401 	}
    402 	memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
    403 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    404 
    405 	return 0;
    406 }
    407 
    408 static int
    409 efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
    410 {
    411 	struct gpt_hdr hdr;
    412 	struct gpt_ent ent;
    413 	EFI_STATUS status;
    414 	UINT32 entry;
    415 	void *buf;
    416 	UINTN sz;
    417 
    418 	status = efi_block_read(bdev, GPT_HDR_BLKNO * DEV_BSIZE, &hdr,
    419 	    sizeof(hdr));
    420 	if (EFI_ERROR(status)) {
    421 		return EIO;
    422 	}
    423 
    424 	if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
    425 		return ENOENT;
    426 	if (le32toh(hdr.hdr_entsz) < sizeof(ent))
    427 		return EINVAL;
    428 
    429 	sz = le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries);
    430 	buf = AllocatePool(sz);
    431 	if (buf == NULL)
    432 		return ENOMEM;
    433 
    434 	status = efi_block_read(bdev,
    435 	    le64toh(hdr.hdr_lba_table) * DEV_BSIZE, buf, sz);
    436 	if (EFI_ERROR(status)) {
    437 		FreePool(buf);
    438 		return EIO;
    439 	}
    440 
    441 	for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
    442 		memcpy(&ent, buf + (entry * le32toh(hdr.hdr_entsz)),
    443 			sizeof(ent));
    444 		efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
    445 	}
    446 
    447 	FreePool(buf);
    448 
    449 	return 0;
    450 }
    451 
    452 static int
    453 efi_block_find_partitions(struct efi_block_dev *bdev)
    454 {
    455 	int error;
    456 
    457 	error = efi_block_find_partitions_gpt(bdev);
    458 	if (error)
    459 		error = efi_block_find_partitions_mbr(bdev);
    460 	if (error)
    461 		error = efi_block_find_partitions_cd9660(bdev);
    462 
    463 	return error;
    464 }
    465 
    466 void
    467 efi_block_probe(void)
    468 {
    469 	struct efi_block_dev *bdev;
    470 	struct efi_block_part *bpart;
    471 	EFI_BLOCK_IO *bio;
    472 	EFI_DISK_IO *dio;
    473 	EFI_STATUS status;
    474 	uint16_t devindex = 0;
    475 	int depth = -1;
    476 	int n;
    477 
    478 	status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
    479 	if (EFI_ERROR(status))
    480 		return;
    481 
    482 	if (efi_bootdp) {
    483 		depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
    484 		if (depth == 0)
    485 			depth = 1;
    486 		else if (depth == -1)
    487 			depth = 2;
    488 	}
    489 
    490 	for (n = 0; n < efi_nblock; n++) {
    491 		/* EFI_BLOCK_IO_PROTOCOL is required */
    492 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n],
    493 		    &BlockIoProtocol, (void **)&bio);
    494 		if (EFI_ERROR(status) || !bio->Media->MediaPresent)
    495 			continue;
    496 
    497 		/* Ignore logical partitions (we do our own partition discovery) */
    498 		if (bio->Media->LogicalPartition)
    499 			continue;
    500 
    501 		/* EFI_DISK_IO_PROTOCOL is optional */
    502 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n],
    503 		    &DiskIoProtocol, (void **)&dio);
    504 		if (EFI_ERROR(status)) {
    505 			dio = NULL;
    506 		}
    507 
    508 		bdev = alloc(sizeof(*bdev));
    509 		bdev->index = devindex++;
    510 		bdev->bio = bio;
    511 		bdev->dio = dio;
    512 		bdev->media_id = bio->Media->MediaId;
    513 		bdev->path = DevicePathFromHandle(efi_block[n]);
    514 		TAILQ_INIT(&bdev->partitions);
    515 		TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
    516 
    517 		efi_block_find_partitions(bdev);
    518 
    519 		if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
    520 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    521 				uint8_t fstype = FS_UNUSED;
    522 				switch (bpart->type) {
    523 				case EFI_BLOCK_PART_DISKLABEL:
    524 					fstype = bpart->disklabel.part.p_fstype;
    525 					break;
    526 				case EFI_BLOCK_PART_GPT:
    527 					fstype = bpart->gpt.fstype;
    528 					break;
    529 				case EFI_BLOCK_PART_CD9660:
    530 					fstype = FS_ISO9660;
    531 					break;
    532 				}
    533 				if (fstype == FS_BSDFFS || fstype == FS_ISO9660 || fstype == FS_RAID) {
    534 					char devname[9];
    535 					snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
    536 					set_default_device(devname);
    537 					set_default_fstype(fstype);
    538 					break;
    539 				}
    540 			}
    541 		}
    542 	}
    543 }
    544 
    545 static void
    546 print_guid(const uint8_t *guid)
    547 {
    548 	const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
    549 	int i;
    550 
    551 	for (i = 0; i < 16; i++) {
    552 		printf("%02x", guid[index[i]]);
    553 		if (i == 3 || i == 5 || i == 7 || i == 9)
    554 			printf("-");
    555 	}
    556 }
    557 
    558 void
    559 efi_block_show(void)
    560 {
    561 	struct efi_block_dev *bdev;
    562 	struct efi_block_part *bpart;
    563 	uint64_t size;
    564 	CHAR16 *path;
    565 
    566 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
    567 		printf("hd%u (", bdev->index);
    568 
    569 		/* Size in MB */
    570 		size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
    571 		if (size >= 10000)
    572 			printf("%"PRIu64" GB", size / 1024);
    573 		else
    574 			printf("%"PRIu64" MB", size);
    575 		printf("): ");
    576 
    577 		path = DevicePathToStr(bdev->path);
    578 		Print(L"%s", path);
    579 		FreePool(path);
    580 
    581 		printf("\n");
    582 
    583 		TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    584 			switch (bpart->type) {
    585 			case EFI_BLOCK_PART_DISKLABEL:
    586 				printf("  hd%u%c (", bdev->index, bpart->index + 'a');
    587 
    588 				/* Size in MB */
    589 				size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
    590 				if (size >= 10000)
    591 					printf("%"PRIu64" GB", size / 1024);
    592 				else
    593 					printf("%"PRIu64" MB", size);
    594 				printf("): ");
    595 
    596 				printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
    597 				break;
    598 			case EFI_BLOCK_PART_GPT:
    599 				printf("  hd%u%c ", bdev->index, bpart->index + 'a');
    600 
    601 				if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    602 					printf("\"");
    603 					print_guid(bpart->gpt.ent.ent_guid);
    604 					printf("\"");
    605 				} else {
    606 					Print(L"\"%s\"", bpart->gpt.ent.ent_name);
    607 				}
    608 
    609 				/* Size in MB */
    610 				size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
    611 				size /= (1024 * 1024);
    612 				if (size >= 10000)
    613 					printf(" (%"PRIu64" GB): ", size / 1024);
    614 				else
    615 					printf(" (%"PRIu64" MB): ", size);
    616 
    617 				printf("%s\n", fstypenames[bpart->gpt.fstype]);
    618 				break;
    619 			case EFI_BLOCK_PART_CD9660:
    620 				printf("  hd%u%c %s\n", bdev->index, bpart->index + 'a', fstypenames[FS_ISO9660]);
    621 				break;
    622 			default:
    623 				break;
    624 			}
    625 		}
    626 	}
    627 }
    628 
    629 struct efi_block_part *
    630 efi_block_boot_part(void)
    631 {
    632 	return efi_block_booted;
    633 }
    634 
    635 int
    636 efi_block_open(struct open_file *f, ...)
    637 {
    638 	struct efi_block_part *bpart;
    639 	const char *fname;
    640 	char **file;
    641 	char *path;
    642 	va_list ap;
    643 	int rv, n;
    644 
    645 	va_start(ap, f);
    646 	fname = va_arg(ap, const char *);
    647 	file = va_arg(ap, char **);
    648 	va_end(ap);
    649 
    650 	rv = efi_block_parse(fname, &bpart, &path);
    651 	if (rv != 0)
    652 		return rv;
    653 
    654 	for (n = 0; n < ndevs; n++)
    655 		if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
    656 			f->f_dev = &devsw[n];
    657 			break;
    658 		}
    659 	if (n == ndevs)
    660 		return ENXIO;
    661 
    662 	f->f_devdata = bpart;
    663 
    664 	*file = path;
    665 
    666 	efi_block_booted = bpart;
    667 
    668 	return 0;
    669 }
    670 
    671 int
    672 efi_block_close(struct open_file *f)
    673 {
    674 	return 0;
    675 }
    676 
    677 int
    678 efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    679 {
    680 	struct efi_block_part *bpart = devdata;
    681 	EFI_STATUS status;
    682 	UINT64 off;
    683 
    684 	if (rw != F_READ)
    685 		return EROFS;
    686 
    687 	efi_set_watchdog(EFI_BLOCK_TIMEOUT, EFI_BLOCK_TIMEOUT_CODE);
    688 
    689 	switch (bpart->type) {
    690 	case EFI_BLOCK_PART_DISKLABEL:
    691 		off = (dblk + bpart->disklabel.part.p_offset) * DEV_BSIZE;
    692 		break;
    693 	case EFI_BLOCK_PART_GPT:
    694 		off = (dblk + le64toh(bpart->gpt.ent.ent_lba_start)) * DEV_BSIZE;
    695 		break;
    696 	case EFI_BLOCK_PART_CD9660:
    697 		off = dblk * ISO_DEFAULT_BLOCK_SIZE;
    698 		break;
    699 	default:
    700 		return EINVAL;
    701 	}
    702 
    703 	status = efi_block_read(bpart->bdev, off, buf, size);
    704 	if (EFI_ERROR(status))
    705 		return EIO;
    706 
    707 	*rsize = size;
    708 
    709 	return 0;
    710 }
    711 
    712 void
    713 efi_block_set_readahead(bool onoff)
    714 {
    715 	efi_ra_enable = onoff;
    716 }
    717