Home | History | Annotate | Line # | Download | only in efiboot
efiblock.c revision 1.9
      1  1.9       tnn /* $NetBSD: efiblock.c,v 1.9 2020/10/18 18:05:48 tnn 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.8  jmcneill 	sz = __MAX(sizeof(*vd), bdev->bio->Media->BlockSize);
    131  1.8  jmcneill 	sz = roundup(sz, bdev->bio->Media->BlockSize);
    132  1.8  jmcneill 	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
    133  1.8  jmcneill 		return ENOMEM;
    134  1.8  jmcneill 
    135  1.8  jmcneill 	for (lba = 16;; lba++) {
    136  1.8  jmcneill 		status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
    137  1.8  jmcneill 		    lba, sz, buf_start);
    138  1.8  jmcneill 		if (EFI_ERROR(status))
    139  1.8  jmcneill 			goto io_error;
    140  1.8  jmcneill 
    141  1.8  jmcneill 		vd = (struct iso_primary_descriptor *)buf_start;
    142  1.8  jmcneill 		if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
    143  1.8  jmcneill 			goto io_error;
    144  1.8  jmcneill 		if (isonum_711(vd->type) == ISO_VD_END)
    145  1.8  jmcneill 			goto io_error;
    146  1.8  jmcneill 		if (isonum_711(vd->type) == ISO_VD_PRIMARY)
    147  1.8  jmcneill 			break;
    148  1.8  jmcneill 	}
    149  1.8  jmcneill 
    150  1.8  jmcneill 	if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
    151  1.8  jmcneill 		goto io_error;
    152  1.8  jmcneill 
    153  1.8  jmcneill 	bpart = alloc(sizeof(*bpart));
    154  1.8  jmcneill 	bpart->index = 0;
    155  1.8  jmcneill 	bpart->bdev = bdev;
    156  1.8  jmcneill 	bpart->type = EFI_BLOCK_PART_CD9660;
    157  1.8  jmcneill 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    158  1.8  jmcneill 
    159  1.8  jmcneill 	FreePool(buf);
    160  1.8  jmcneill 	return 0;
    161  1.8  jmcneill 
    162  1.8  jmcneill io_error:
    163  1.8  jmcneill 	FreePool(buf);
    164  1.8  jmcneill 	return EIO;
    165  1.8  jmcneill }
    166  1.8  jmcneill 
    167  1.8  jmcneill static int
    168  1.2  jmcneill efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, struct mbr_sector *mbr, uint32_t start, uint32_t size)
    169  1.1  jmcneill {
    170  1.1  jmcneill 	struct efi_block_part *bpart;
    171  1.1  jmcneill 	struct disklabel d;
    172  1.1  jmcneill 	struct partition *p;
    173  1.1  jmcneill 	EFI_STATUS status;
    174  1.1  jmcneill 	EFI_LBA lba;
    175  1.6  jmcneill 	void *buf, *buf_start;
    176  1.1  jmcneill 	UINT32 sz;
    177  1.1  jmcneill 	int n;
    178  1.1  jmcneill 
    179  1.1  jmcneill 	sz = __MAX(sizeof(d), bdev->bio->Media->BlockSize);
    180  1.1  jmcneill 	sz = roundup(sz, bdev->bio->Media->BlockSize);
    181  1.6  jmcneill 	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
    182  1.1  jmcneill 		return ENOMEM;
    183  1.1  jmcneill 
    184  1.3  jakllsch 	lba = (((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE) / bdev->bio->Media->BlockSize;
    185  1.6  jmcneill 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
    186  1.6  jmcneill 		lba, sz, buf_start);
    187  1.6  jmcneill 	if (EFI_ERROR(status) || getdisklabel(buf_start, &d) != NULL) {
    188  1.1  jmcneill 		FreePool(buf);
    189  1.1  jmcneill 		return EIO;
    190  1.1  jmcneill 	}
    191  1.1  jmcneill 	FreePool(buf);
    192  1.1  jmcneill 
    193  1.1  jmcneill 	if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
    194  1.1  jmcneill 		return EINVAL;
    195  1.1  jmcneill 	if (le16toh(d.d_npartitions) > MAXPARTITIONS)
    196  1.1  jmcneill 		return EINVAL;
    197  1.1  jmcneill 
    198  1.1  jmcneill 	for (n = 0; n < le16toh(d.d_npartitions); n++) {
    199  1.1  jmcneill 		p = &d.d_partitions[n];
    200  1.1  jmcneill 		switch (p->p_fstype) {
    201  1.1  jmcneill 		case FS_BSDFFS:
    202  1.1  jmcneill 		case FS_MSDOS:
    203  1.1  jmcneill 		case FS_BSDLFS:
    204  1.1  jmcneill 			break;
    205  1.1  jmcneill 		default:
    206  1.1  jmcneill 			continue;
    207  1.1  jmcneill 		}
    208  1.1  jmcneill 
    209  1.1  jmcneill 		bpart = alloc(sizeof(*bpart));
    210  1.1  jmcneill 		bpart->index = n;
    211  1.1  jmcneill 		bpart->bdev = bdev;
    212  1.1  jmcneill 		bpart->type = EFI_BLOCK_PART_DISKLABEL;
    213  1.1  jmcneill 		bpart->disklabel.secsize = le32toh(d.d_secsize);
    214  1.1  jmcneill 		bpart->disklabel.part = *p;
    215  1.2  jmcneill 		efi_block_generate_hash_mbr(bpart, mbr);
    216  1.1  jmcneill 		TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    217  1.1  jmcneill 	}
    218  1.1  jmcneill 
    219  1.1  jmcneill 	return 0;
    220  1.1  jmcneill }
    221  1.1  jmcneill 
    222  1.1  jmcneill static int
    223  1.1  jmcneill efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
    224  1.1  jmcneill {
    225  1.1  jmcneill 	struct mbr_sector mbr;
    226  1.1  jmcneill 	struct mbr_partition *mbr_part;
    227  1.1  jmcneill 	EFI_STATUS status;
    228  1.6  jmcneill 	void *buf, *buf_start;
    229  1.1  jmcneill 	UINT32 sz;
    230  1.1  jmcneill 	int n;
    231  1.1  jmcneill 
    232  1.1  jmcneill 	sz = __MAX(sizeof(mbr), bdev->bio->Media->BlockSize);
    233  1.1  jmcneill 	sz = roundup(sz, bdev->bio->Media->BlockSize);
    234  1.6  jmcneill 	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
    235  1.1  jmcneill 		return ENOMEM;
    236  1.1  jmcneill 
    237  1.6  jmcneill 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
    238  1.6  jmcneill 		0, sz, buf_start);
    239  1.1  jmcneill 	if (EFI_ERROR(status)) {
    240  1.1  jmcneill 		FreePool(buf);
    241  1.1  jmcneill 		return EIO;
    242  1.1  jmcneill 	}
    243  1.6  jmcneill 	memcpy(&mbr, buf_start, sizeof(mbr));
    244  1.1  jmcneill 	FreePool(buf);
    245  1.1  jmcneill 
    246  1.1  jmcneill 	if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
    247  1.1  jmcneill 		return ENOENT;
    248  1.1  jmcneill 
    249  1.1  jmcneill 	for (n = 0; n < MBR_PART_COUNT; n++) {
    250  1.1  jmcneill 		mbr_part = &mbr.mbr_parts[n];
    251  1.1  jmcneill 		if (le32toh(mbr_part->mbrp_size) == 0)
    252  1.1  jmcneill 			continue;
    253  1.1  jmcneill 		if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
    254  1.2  jmcneill 			efi_block_find_partitions_disklabel(bdev, &mbr, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
    255  1.1  jmcneill 			break;
    256  1.1  jmcneill 		}
    257  1.1  jmcneill 	}
    258  1.1  jmcneill 
    259  1.1  jmcneill 	return 0;
    260  1.1  jmcneill }
    261  1.1  jmcneill 
    262  1.4  jmcneill static const struct {
    263  1.4  jmcneill 	struct uuid guid;
    264  1.4  jmcneill 	uint8_t fstype;
    265  1.4  jmcneill } gpt_guid_to_str[] = {
    266  1.4  jmcneill 	{ GPT_ENT_TYPE_NETBSD_FFS,		FS_BSDFFS },
    267  1.4  jmcneill 	{ GPT_ENT_TYPE_NETBSD_LFS,		FS_BSDLFS },
    268  1.4  jmcneill 	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME,	FS_RAID },
    269  1.4  jmcneill 	{ GPT_ENT_TYPE_NETBSD_CCD,		FS_CCD },
    270  1.4  jmcneill 	{ GPT_ENT_TYPE_NETBSD_CGD,		FS_CGD },
    271  1.4  jmcneill 	{ GPT_ENT_TYPE_MS_BASIC_DATA,		FS_MSDOS },	/* or NTFS? ambiguous */
    272  1.9       tnn 	{ GPT_ENT_TYPE_EFI,			FS_MSDOS },
    273  1.4  jmcneill };
    274  1.4  jmcneill 
    275  1.4  jmcneill static int
    276  1.4  jmcneill efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev, struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
    277  1.4  jmcneill {
    278  1.4  jmcneill 	struct efi_block_part *bpart;
    279  1.4  jmcneill 	uint8_t fstype = FS_UNUSED;
    280  1.4  jmcneill 	struct uuid uuid;
    281  1.4  jmcneill 	int n;
    282  1.4  jmcneill 
    283  1.4  jmcneill 	memcpy(&uuid, ent->ent_type, sizeof(uuid));
    284  1.4  jmcneill 	for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
    285  1.4  jmcneill 		if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid, sizeof(ent->ent_type)) == 0) {
    286  1.4  jmcneill 			fstype = gpt_guid_to_str[n].fstype;
    287  1.4  jmcneill 			break;
    288  1.4  jmcneill 		}
    289  1.4  jmcneill 	if (fstype == FS_UNUSED)
    290  1.4  jmcneill 		return 0;
    291  1.4  jmcneill 
    292  1.4  jmcneill 	bpart = alloc(sizeof(*bpart));
    293  1.4  jmcneill 	bpart->index = index;
    294  1.4  jmcneill 	bpart->bdev = bdev;
    295  1.4  jmcneill 	bpart->type = EFI_BLOCK_PART_GPT;
    296  1.4  jmcneill 	bpart->gpt.fstype = fstype;
    297  1.4  jmcneill 	bpart->gpt.ent = *ent;
    298  1.4  jmcneill 	memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
    299  1.4  jmcneill 	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
    300  1.4  jmcneill 
    301  1.4  jmcneill 	return 0;
    302  1.4  jmcneill }
    303  1.4  jmcneill 
    304  1.4  jmcneill static int
    305  1.4  jmcneill efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
    306  1.4  jmcneill {
    307  1.4  jmcneill 	struct gpt_hdr hdr;
    308  1.4  jmcneill 	struct gpt_ent ent;
    309  1.4  jmcneill 	EFI_STATUS status;
    310  1.6  jmcneill 	void *buf, *buf_start;
    311  1.4  jmcneill 	UINT32 sz, entry;
    312  1.4  jmcneill 
    313  1.4  jmcneill 	sz = __MAX(sizeof(hdr), bdev->bio->Media->BlockSize);
    314  1.4  jmcneill 	sz = roundup(sz, bdev->bio->Media->BlockSize);
    315  1.6  jmcneill 	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
    316  1.4  jmcneill 		return ENOMEM;
    317  1.4  jmcneill 
    318  1.6  jmcneill 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
    319  1.6  jmcneill 		GPT_HDR_BLKNO, sz, buf_start);
    320  1.4  jmcneill 	if (EFI_ERROR(status)) {
    321  1.4  jmcneill 		FreePool(buf);
    322  1.4  jmcneill 		return EIO;
    323  1.4  jmcneill 	}
    324  1.6  jmcneill 	memcpy(&hdr, buf_start, sizeof(hdr));
    325  1.4  jmcneill 	FreePool(buf);
    326  1.4  jmcneill 
    327  1.4  jmcneill 	if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
    328  1.4  jmcneill 		return ENOENT;
    329  1.4  jmcneill 	if (le32toh(hdr.hdr_entsz) < sizeof(ent))
    330  1.4  jmcneill 		return EINVAL;
    331  1.4  jmcneill 
    332  1.4  jmcneill 	sz = __MAX(le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries), bdev->bio->Media->BlockSize);
    333  1.4  jmcneill 	sz = roundup(sz, bdev->bio->Media->BlockSize);
    334  1.6  jmcneill 	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
    335  1.4  jmcneill 		return ENOMEM;
    336  1.4  jmcneill 
    337  1.6  jmcneill 	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
    338  1.6  jmcneill 		le64toh(hdr.hdr_lba_table), sz, buf_start);
    339  1.4  jmcneill 	if (EFI_ERROR(status)) {
    340  1.4  jmcneill 		FreePool(buf);
    341  1.4  jmcneill 		return EIO;
    342  1.4  jmcneill 	}
    343  1.4  jmcneill 
    344  1.4  jmcneill 	for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
    345  1.6  jmcneill 		memcpy(&ent, buf_start + (entry * le32toh(hdr.hdr_entsz)),
    346  1.6  jmcneill 			sizeof(ent));
    347  1.4  jmcneill 		efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
    348  1.4  jmcneill 	}
    349  1.4  jmcneill 
    350  1.4  jmcneill 	FreePool(buf);
    351  1.4  jmcneill 
    352  1.4  jmcneill 	return 0;
    353  1.4  jmcneill }
    354  1.4  jmcneill 
    355  1.1  jmcneill static int
    356  1.1  jmcneill efi_block_find_partitions(struct efi_block_dev *bdev)
    357  1.1  jmcneill {
    358  1.4  jmcneill 	int error;
    359  1.4  jmcneill 
    360  1.4  jmcneill 	error = efi_block_find_partitions_gpt(bdev);
    361  1.4  jmcneill 	if (error)
    362  1.4  jmcneill 		error = efi_block_find_partitions_mbr(bdev);
    363  1.8  jmcneill 	if (error)
    364  1.8  jmcneill 		error = efi_block_find_partitions_cd9660(bdev);
    365  1.4  jmcneill 
    366  1.4  jmcneill 	return error;
    367  1.1  jmcneill }
    368  1.1  jmcneill 
    369  1.1  jmcneill void
    370  1.1  jmcneill efi_block_probe(void)
    371  1.1  jmcneill {
    372  1.1  jmcneill 	struct efi_block_dev *bdev;
    373  1.4  jmcneill 	struct efi_block_part *bpart;
    374  1.1  jmcneill 	EFI_BLOCK_IO *bio;
    375  1.1  jmcneill 	EFI_STATUS status;
    376  1.1  jmcneill 	uint16_t devindex = 0;
    377  1.1  jmcneill 	int depth = -1;
    378  1.1  jmcneill 	int n;
    379  1.1  jmcneill 
    380  1.1  jmcneill 	status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
    381  1.1  jmcneill 	if (EFI_ERROR(status))
    382  1.1  jmcneill 		return;
    383  1.1  jmcneill 
    384  1.1  jmcneill 	if (efi_bootdp) {
    385  1.1  jmcneill 		depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
    386  1.1  jmcneill 		if (depth == 0)
    387  1.1  jmcneill 			depth = 1;
    388  1.5  jmcneill 		else if (depth == -1)
    389  1.5  jmcneill 			depth = 2;
    390  1.1  jmcneill 	}
    391  1.1  jmcneill 
    392  1.1  jmcneill 	for (n = 0; n < efi_nblock; n++) {
    393  1.1  jmcneill 		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n], &BlockIoProtocol, (void **)&bio);
    394  1.1  jmcneill 		if (EFI_ERROR(status) || !bio->Media->MediaPresent)
    395  1.1  jmcneill 			continue;
    396  1.1  jmcneill 
    397  1.1  jmcneill 		if (bio->Media->LogicalPartition)
    398  1.1  jmcneill 			continue;
    399  1.1  jmcneill 
    400  1.1  jmcneill 		bdev = alloc(sizeof(*bdev));
    401  1.1  jmcneill 		bdev->index = devindex++;
    402  1.1  jmcneill 		bdev->bio = bio;
    403  1.1  jmcneill 		bdev->media_id = bio->Media->MediaId;
    404  1.1  jmcneill 		bdev->path = DevicePathFromHandle(efi_block[n]);
    405  1.1  jmcneill 		TAILQ_INIT(&bdev->partitions);
    406  1.1  jmcneill 		TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);
    407  1.1  jmcneill 
    408  1.4  jmcneill 		efi_block_find_partitions(bdev);
    409  1.4  jmcneill 
    410  1.1  jmcneill 		if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
    411  1.4  jmcneill 			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    412  1.4  jmcneill 				uint8_t fstype = FS_UNUSED;
    413  1.4  jmcneill 				switch (bpart->type) {
    414  1.4  jmcneill 				case EFI_BLOCK_PART_DISKLABEL:
    415  1.4  jmcneill 					fstype = bpart->disklabel.part.p_fstype;
    416  1.4  jmcneill 					break;
    417  1.4  jmcneill 				case EFI_BLOCK_PART_GPT:
    418  1.4  jmcneill 					fstype = bpart->gpt.fstype;
    419  1.4  jmcneill 					break;
    420  1.8  jmcneill 				case EFI_BLOCK_PART_CD9660:
    421  1.8  jmcneill 					fstype = FS_ISO9660;
    422  1.8  jmcneill 					break;
    423  1.4  jmcneill 				}
    424  1.8  jmcneill 				if (fstype == FS_BSDFFS || fstype == FS_ISO9660) {
    425  1.4  jmcneill 					char devname[9];
    426  1.4  jmcneill 					snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
    427  1.4  jmcneill 					set_default_device(devname);
    428  1.8  jmcneill 					set_default_fstype(fstype);
    429  1.4  jmcneill 					break;
    430  1.4  jmcneill 				}
    431  1.4  jmcneill 			}
    432  1.1  jmcneill 		}
    433  1.4  jmcneill 	}
    434  1.4  jmcneill }
    435  1.1  jmcneill 
    436  1.4  jmcneill static void
    437  1.4  jmcneill print_guid(const uint8_t *guid)
    438  1.4  jmcneill {
    439  1.4  jmcneill 	const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
    440  1.4  jmcneill 	int i;
    441  1.4  jmcneill 
    442  1.4  jmcneill 	for (i = 0; i < 16; i++) {
    443  1.4  jmcneill 		printf("%02x", guid[index[i]]);
    444  1.4  jmcneill 		if (i == 3 || i == 5 || i == 7 || i == 9)
    445  1.4  jmcneill 			printf("-");
    446  1.1  jmcneill 	}
    447  1.1  jmcneill }
    448  1.1  jmcneill 
    449  1.1  jmcneill void
    450  1.1  jmcneill efi_block_show(void)
    451  1.1  jmcneill {
    452  1.1  jmcneill 	struct efi_block_dev *bdev;
    453  1.1  jmcneill 	struct efi_block_part *bpart;
    454  1.1  jmcneill 	uint64_t size;
    455  1.1  jmcneill 	CHAR16 *path;
    456  1.1  jmcneill 
    457  1.1  jmcneill 	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
    458  1.1  jmcneill 		printf("hd%u (", bdev->index);
    459  1.1  jmcneill 
    460  1.1  jmcneill 		/* Size in MB */
    461  1.1  jmcneill 		size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
    462  1.1  jmcneill 		if (size >= 10000)
    463  1.1  jmcneill 			printf("%"PRIu64" GB", size / 1024);
    464  1.1  jmcneill 		else
    465  1.1  jmcneill 			printf("%"PRIu64" MB", size);
    466  1.1  jmcneill 		printf("): ");
    467  1.1  jmcneill 
    468  1.1  jmcneill 		path = DevicePathToStr(bdev->path);
    469  1.1  jmcneill 		Print(L"%s", path);
    470  1.1  jmcneill 		FreePool(path);
    471  1.1  jmcneill 
    472  1.1  jmcneill 		printf("\n");
    473  1.1  jmcneill 
    474  1.1  jmcneill 		TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
    475  1.1  jmcneill 			switch (bpart->type) {
    476  1.1  jmcneill 			case EFI_BLOCK_PART_DISKLABEL:
    477  1.1  jmcneill 				printf("  hd%u%c (", bdev->index, bpart->index + 'a');
    478  1.1  jmcneill 
    479  1.1  jmcneill 				/* Size in MB */
    480  1.1  jmcneill 				size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
    481  1.1  jmcneill 				if (size >= 10000)
    482  1.1  jmcneill 					printf("%"PRIu64" GB", size / 1024);
    483  1.1  jmcneill 				else
    484  1.1  jmcneill 					printf("%"PRIu64" MB", size);
    485  1.1  jmcneill 				printf("): ");
    486  1.1  jmcneill 
    487  1.1  jmcneill 				printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
    488  1.1  jmcneill 				break;
    489  1.4  jmcneill 			case EFI_BLOCK_PART_GPT:
    490  1.4  jmcneill 				printf("  hd%u%c ", bdev->index, bpart->index + 'a');
    491  1.4  jmcneill 
    492  1.4  jmcneill 				if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    493  1.4  jmcneill 					printf("\"");
    494  1.4  jmcneill 					print_guid(bpart->gpt.ent.ent_guid);
    495  1.4  jmcneill 					printf("\"");
    496  1.4  jmcneill 				} else {
    497  1.4  jmcneill 					Print(L"\"%s\"", bpart->gpt.ent.ent_name);
    498  1.4  jmcneill 				}
    499  1.4  jmcneill 
    500  1.4  jmcneill 				/* Size in MB */
    501  1.4  jmcneill 				size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
    502  1.4  jmcneill 				size /= (1024 * 1024);
    503  1.4  jmcneill 				if (size >= 10000)
    504  1.4  jmcneill 					printf(" (%"PRIu64" GB): ", size / 1024);
    505  1.4  jmcneill 				else
    506  1.4  jmcneill 					printf(" (%"PRIu64" MB): ", size);
    507  1.4  jmcneill 
    508  1.4  jmcneill 				printf("%s\n", fstypenames[bpart->gpt.fstype]);
    509  1.4  jmcneill 				break;
    510  1.8  jmcneill 			case EFI_BLOCK_PART_CD9660:
    511  1.8  jmcneill 				printf("  hd%u%c %s\n", bdev->index, bpart->index + 'a', fstypenames[FS_ISO9660]);
    512  1.8  jmcneill 				break;
    513  1.1  jmcneill 			default:
    514  1.1  jmcneill 				break;
    515  1.1  jmcneill 			}
    516  1.1  jmcneill 		}
    517  1.1  jmcneill 	}
    518  1.1  jmcneill }
    519  1.1  jmcneill 
    520  1.2  jmcneill struct efi_block_part *
    521  1.2  jmcneill efi_block_boot_part(void)
    522  1.2  jmcneill {
    523  1.2  jmcneill 	return efi_block_booted;
    524  1.2  jmcneill }
    525  1.2  jmcneill 
    526  1.1  jmcneill int
    527  1.1  jmcneill efi_block_open(struct open_file *f, ...)
    528  1.1  jmcneill {
    529  1.1  jmcneill 	struct efi_block_part *bpart;
    530  1.1  jmcneill 	const char *fname;
    531  1.1  jmcneill 	char **file;
    532  1.1  jmcneill 	char *path;
    533  1.1  jmcneill 	va_list ap;
    534  1.1  jmcneill 	int rv, n;
    535  1.1  jmcneill 
    536  1.1  jmcneill 	va_start(ap, f);
    537  1.1  jmcneill 	fname = va_arg(ap, const char *);
    538  1.1  jmcneill 	file = va_arg(ap, char **);
    539  1.1  jmcneill 	va_end(ap);
    540  1.1  jmcneill 
    541  1.1  jmcneill 	rv = efi_block_parse(fname, &bpart, &path);
    542  1.1  jmcneill 	if (rv != 0)
    543  1.1  jmcneill 		return rv;
    544  1.1  jmcneill 
    545  1.1  jmcneill 	for (n = 0; n < ndevs; n++)
    546  1.1  jmcneill 		if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
    547  1.1  jmcneill 			f->f_dev = &devsw[n];
    548  1.1  jmcneill 			break;
    549  1.1  jmcneill 		}
    550  1.1  jmcneill 	if (n == ndevs)
    551  1.1  jmcneill 		return ENXIO;
    552  1.1  jmcneill 
    553  1.1  jmcneill 	f->f_devdata = bpart;
    554  1.1  jmcneill 
    555  1.1  jmcneill 	*file = path;
    556  1.1  jmcneill 
    557  1.2  jmcneill 	efi_block_booted = bpart;
    558  1.2  jmcneill 
    559  1.1  jmcneill 	return 0;
    560  1.1  jmcneill }
    561  1.1  jmcneill 
    562  1.1  jmcneill int
    563  1.1  jmcneill efi_block_close(struct open_file *f)
    564  1.1  jmcneill {
    565  1.1  jmcneill 	return 0;
    566  1.1  jmcneill }
    567  1.1  jmcneill 
    568  1.1  jmcneill int
    569  1.1  jmcneill efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    570  1.1  jmcneill {
    571  1.1  jmcneill 	struct efi_block_part *bpart = devdata;
    572  1.1  jmcneill 	EFI_STATUS status;
    573  1.6  jmcneill 	void *allocated_buf, *aligned_buf;
    574  1.1  jmcneill 
    575  1.1  jmcneill 	if (rw != F_READ)
    576  1.1  jmcneill 		return EROFS;
    577  1.1  jmcneill 
    578  1.1  jmcneill 	switch (bpart->type) {
    579  1.1  jmcneill 	case EFI_BLOCK_PART_DISKLABEL:
    580  1.1  jmcneill 		if (bpart->bdev->bio->Media->BlockSize != bpart->disklabel.secsize) {
    581  1.1  jmcneill 			printf("%s: unsupported block size %d (expected %d)\n", __func__,
    582  1.1  jmcneill 			    bpart->bdev->bio->Media->BlockSize, bpart->disklabel.secsize);
    583  1.1  jmcneill 			return EIO;
    584  1.1  jmcneill 		}
    585  1.1  jmcneill 		dblk += bpart->disklabel.part.p_offset;
    586  1.1  jmcneill 		break;
    587  1.4  jmcneill 	case EFI_BLOCK_PART_GPT:
    588  1.4  jmcneill 		if (bpart->bdev->bio->Media->BlockSize != DEV_BSIZE) {
    589  1.4  jmcneill 			printf("%s: unsupported block size %d (expected %d)\n", __func__,
    590  1.4  jmcneill 			    bpart->bdev->bio->Media->BlockSize, DEV_BSIZE);
    591  1.4  jmcneill 			return EIO;
    592  1.4  jmcneill 		}
    593  1.4  jmcneill 		dblk += le64toh(bpart->gpt.ent.ent_lba_start);
    594  1.4  jmcneill 		break;
    595  1.8  jmcneill 	case EFI_BLOCK_PART_CD9660:
    596  1.8  jmcneill 		break;
    597  1.1  jmcneill 	default:
    598  1.1  jmcneill 		return EINVAL;
    599  1.1  jmcneill 	}
    600  1.1  jmcneill 
    601  1.6  jmcneill 	if ((bpart->bdev->bio->Media->IoAlign <= 1) ||
    602  1.6  jmcneill 		((intptr_t)buf & (bpart->bdev->bio->Media->IoAlign - 1)) == 0) {
    603  1.6  jmcneill 		allocated_buf = NULL;
    604  1.6  jmcneill 		aligned_buf = buf;
    605  1.6  jmcneill 	} else if ((allocated_buf = efi_block_allocate_device_buffer(bpart->bdev,
    606  1.6  jmcneill 		size, &aligned_buf)) == NULL)
    607  1.6  jmcneill 		return ENOMEM;
    608  1.6  jmcneill 
    609  1.6  jmcneill 	status = uefi_call_wrapper(bpart->bdev->bio->ReadBlocks, 5,
    610  1.6  jmcneill 		bpart->bdev->bio, bpart->bdev->media_id, dblk, size, aligned_buf);
    611  1.6  jmcneill 	if (EFI_ERROR(status)) {
    612  1.6  jmcneill 		if (allocated_buf != NULL)
    613  1.6  jmcneill 			FreePool(allocated_buf);
    614  1.1  jmcneill 		return EIO;
    615  1.6  jmcneill 	}
    616  1.6  jmcneill 	if (allocated_buf != NULL) {
    617  1.6  jmcneill 		memcpy(buf, aligned_buf, size);
    618  1.6  jmcneill 		FreePool(allocated_buf);
    619  1.6  jmcneill 	}
    620  1.1  jmcneill 
    621  1.1  jmcneill 	*rsize = size;
    622  1.1  jmcneill 
    623  1.1  jmcneill 	return 0;
    624  1.1  jmcneill }
    625