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