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