xref: /onnv-gate/usr/src/lib/libefi/common/rdwr_efi.c (revision 6889:950d5e7bce07)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52285Scg149915  * Common Development and Distribution License (the "License").
62285Scg149915  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226590Syl194034  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <strings.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <uuid/uuid.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/dkio.h>
370Sstevel@tonic-gate #include <sys/vtoc.h>
380Sstevel@tonic-gate #include <sys/mhd.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
410Sstevel@tonic-gate #include <sys/efi_partition.h>
420Sstevel@tonic-gate #include <sys/byteorder.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static struct uuid_to_ptag {
460Sstevel@tonic-gate 	struct uuid	uuid;
470Sstevel@tonic-gate } conversion_array[] = {
480Sstevel@tonic-gate 	{ EFI_UNUSED },
490Sstevel@tonic-gate 	{ EFI_BOOT },
500Sstevel@tonic-gate 	{ EFI_ROOT },
510Sstevel@tonic-gate 	{ EFI_SWAP },
520Sstevel@tonic-gate 	{ EFI_USR },
530Sstevel@tonic-gate 	{ EFI_BACKUP },
540Sstevel@tonic-gate 	{ 0 },			/* STAND is never used */
550Sstevel@tonic-gate 	{ EFI_VAR },
560Sstevel@tonic-gate 	{ EFI_HOME },
570Sstevel@tonic-gate 	{ EFI_ALTSCTR },
580Sstevel@tonic-gate 	{ 0 },			/* CACHE (cachefs) is never used */
590Sstevel@tonic-gate 	{ EFI_RESERVED },
600Sstevel@tonic-gate 	{ EFI_SYSTEM },
610Sstevel@tonic-gate 	{ EFI_LEGACY_MBR },
620Sstevel@tonic-gate 	{ EFI_RESV3 },
630Sstevel@tonic-gate 	{ EFI_RESV4 },
640Sstevel@tonic-gate 	{ EFI_MSFT_RESV },
650Sstevel@tonic-gate 	{ EFI_DELL_BASIC },
660Sstevel@tonic-gate 	{ EFI_DELL_RAID },
670Sstevel@tonic-gate 	{ EFI_DELL_SWAP },
680Sstevel@tonic-gate 	{ EFI_DELL_LVM },
692866Sszhou 	{ EFI_DELL_RESV },
702866Sszhou 	{ EFI_AAPL_HFS },
712866Sszhou 	{ EFI_AAPL_UFS }
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Default vtoc information for non-SVr4 partitions
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate struct dk_map2  default_vtoc_map[NDKMAP] = {
780Sstevel@tonic-gate 	{	V_ROOT,		0	},		/* a - 0 */
790Sstevel@tonic-gate 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
800Sstevel@tonic-gate 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
810Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* d - 3 */
820Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* e - 4 */
830Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* f - 5 */
840Sstevel@tonic-gate 	{	V_USR,		0	},		/* g - 6 */
850Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* h - 7 */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
900Sstevel@tonic-gate 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
910Sstevel@tonic-gate 	{	V_ALTSCTR,	0	},		/* j - 9 */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #else
940Sstevel@tonic-gate #error No VTOC format defined.
950Sstevel@tonic-gate #endif			/* defined(i386) */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* k - 10 */
980Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* l - 11 */
990Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* m - 12 */
1000Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* n - 13 */
1010Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* o - 14 */
1020Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* p - 15 */
1030Sstevel@tonic-gate #endif			/* defined(_SUNOS_VTOC_16) */
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #ifdef DEBUG
1070Sstevel@tonic-gate int efi_debug = 1;
1080Sstevel@tonic-gate #else
1090Sstevel@tonic-gate int efi_debug = 0;
1100Sstevel@tonic-gate #endif
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate extern unsigned int	efi_crc32(const unsigned char *, unsigned int);
1130Sstevel@tonic-gate static int		efi_read(int, struct dk_gpt *);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static int
1160Sstevel@tonic-gate read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	struct dk_minfo		disk_info;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
1210Sstevel@tonic-gate 		return (errno);
1220Sstevel@tonic-gate 	*capacity = disk_info.dki_capacity;
1230Sstevel@tonic-gate 	*lbsize = disk_info.dki_lbsize;
1240Sstevel@tonic-gate 	return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * the number of blocks the EFI label takes up (round up to nearest
1290Sstevel@tonic-gate  * block)
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate #define	NBLOCKS(p, l)	(1 + ((((p) * (int)sizeof (efi_gpe_t))  + \
1320Sstevel@tonic-gate 				((l) - 1)) / (l)))
1330Sstevel@tonic-gate /* number of partitions -- limited by what we can malloc */
1340Sstevel@tonic-gate #define	MAX_PARTS	((4294967295UL - sizeof (struct dk_gpt)) / \
1350Sstevel@tonic-gate 			    sizeof (struct dk_part))
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
1380Sstevel@tonic-gate efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	diskaddr_t	capacity;
1410Sstevel@tonic-gate 	uint_t		lbsize;
1420Sstevel@tonic-gate 	uint_t		nblocks;
1430Sstevel@tonic-gate 	size_t		length;
1440Sstevel@tonic-gate 	struct dk_gpt	*vptr;
1450Sstevel@tonic-gate 	struct uuid	uuid;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (read_disk_info(fd, &capacity, &lbsize) != 0) {
1480Sstevel@tonic-gate 		if (efi_debug)
1490Sstevel@tonic-gate 			(void) fprintf(stderr,
1500Sstevel@tonic-gate 			    "couldn't read disk information\n");
1510Sstevel@tonic-gate 		return (-1);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	nblocks = NBLOCKS(nparts, lbsize);
1550Sstevel@tonic-gate 	if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
1560Sstevel@tonic-gate 		/* 16K plus one block for the GPT */
1570Sstevel@tonic-gate 		nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (nparts > MAX_PARTS) {
1610Sstevel@tonic-gate 		if (efi_debug) {
1620Sstevel@tonic-gate 			(void) fprintf(stderr,
1630Sstevel@tonic-gate 			"the maximum number of partitions supported is %lu\n",
1640Sstevel@tonic-gate 			    MAX_PARTS);
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 		return (-1);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	length = sizeof (struct dk_gpt) +
1700Sstevel@tonic-gate 	    sizeof (struct dk_part) * (nparts - 1);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
1730Sstevel@tonic-gate 		return (-1);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	vptr = *vtoc;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	vptr->efi_version = EFI_VERSION_CURRENT;
1780Sstevel@tonic-gate 	vptr->efi_lbasize = lbsize;
1790Sstevel@tonic-gate 	vptr->efi_nparts = nparts;
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * add one block here for the PMBR; on disks with a 512 byte
1820Sstevel@tonic-gate 	 * block size and 128 or fewer partitions, efi_first_u_lba
1830Sstevel@tonic-gate 	 * should work out to "34"
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	vptr->efi_first_u_lba = nblocks + 1;
1860Sstevel@tonic-gate 	vptr->efi_last_lba = capacity - 1;
1876590Syl194034 	vptr->efi_altern_lba = capacity -1;
1880Sstevel@tonic-gate 	vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
1890Sstevel@tonic-gate 	(void) uuid_generate((uchar_t *)&uuid);
1900Sstevel@tonic-gate 	UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
1910Sstevel@tonic-gate 	return (0);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Read EFI - return partition number upon success.
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate int
1980Sstevel@tonic-gate efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	int			rval;
2010Sstevel@tonic-gate 	uint32_t		nparts;
2020Sstevel@tonic-gate 	int			length;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/* figure out the number of entries that would fit into 16K */
2050Sstevel@tonic-gate 	nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
2060Sstevel@tonic-gate 	length = (int) sizeof (struct dk_gpt) +
2076590Syl194034 	    (int) sizeof (struct dk_part) * (nparts - 1);
2080Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
2090Sstevel@tonic-gate 		return (VT_ERROR);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	(*vtoc)->efi_nparts = nparts;
2120Sstevel@tonic-gate 	rval = efi_read(fd, *vtoc);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
2150Sstevel@tonic-gate 		void *tmp;
2160Sstevel@tonic-gate 		length = (int) sizeof (struct dk_gpt) +
2176590Syl194034 		    (int) sizeof (struct dk_part) *
2186590Syl194034 		    ((*vtoc)->efi_nparts - 1);
2190Sstevel@tonic-gate 		nparts = (*vtoc)->efi_nparts;
2200Sstevel@tonic-gate 		if ((tmp = realloc(*vtoc, length)) == NULL) {
2210Sstevel@tonic-gate 			free (*vtoc);
2220Sstevel@tonic-gate 			*vtoc = NULL;
2230Sstevel@tonic-gate 			return (VT_ERROR);
2240Sstevel@tonic-gate 		} else {
2250Sstevel@tonic-gate 			*vtoc = tmp;
2260Sstevel@tonic-gate 			rval = efi_read(fd, *vtoc);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (rval < 0) {
2310Sstevel@tonic-gate 		if (efi_debug) {
2320Sstevel@tonic-gate 			(void) fprintf(stderr,
2330Sstevel@tonic-gate 			    "read of EFI table failed, rval=%d\n", rval);
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 		free (*vtoc);
2360Sstevel@tonic-gate 		*vtoc = NULL;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	return (rval);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate static int
2430Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
2460Sstevel@tonic-gate 	int error;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
2490Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
2500Sstevel@tonic-gate 	dk_ioc->dki_data = data;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	return (error);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static int
2560Sstevel@tonic-gate check_label(int fd, dk_efi_t *dk_ioc)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	efi_gpt_t		*efi;
2590Sstevel@tonic-gate 	uint_t			crc;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
2620Sstevel@tonic-gate 		switch (errno) {
2630Sstevel@tonic-gate 		case EIO:
2640Sstevel@tonic-gate 			return (VT_EIO);
2650Sstevel@tonic-gate 		default:
2660Sstevel@tonic-gate 			return (VT_ERROR);
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 	efi = dk_ioc->dki_data;
2700Sstevel@tonic-gate 	if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
2710Sstevel@tonic-gate 		if (efi_debug)
2720Sstevel@tonic-gate 			(void) fprintf(stderr,
2730Sstevel@tonic-gate 			    "Bad EFI signature: 0x%llx != 0x%llx\n",
2740Sstevel@tonic-gate 			    (long long)efi->efi_gpt_Signature,
2750Sstevel@tonic-gate 			    (long long)LE_64(EFI_SIGNATURE));
2760Sstevel@tonic-gate 		return (VT_EINVAL);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * check CRC of the header; the size of the header should
2810Sstevel@tonic-gate 	 * never be larger than one block
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 	crc = efi->efi_gpt_HeaderCRC32;
2840Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
2870Sstevel@tonic-gate 	    crc != LE_32(efi_crc32((unsigned char *)efi,
2880Sstevel@tonic-gate 	    LE_32(efi->efi_gpt_HeaderSize)))) {
2890Sstevel@tonic-gate 		if (efi_debug)
2900Sstevel@tonic-gate 			(void) fprintf(stderr,
2916590Syl194034 			    "Bad EFI CRC: 0x%x != 0x%x\n",
2926590Syl194034 			    crc,
2936590Syl194034 			    LE_32(efi_crc32((unsigned char *)efi,
2946590Syl194034 			    sizeof (struct efi_gpt))));
2950Sstevel@tonic-gate 		return (VT_EINVAL);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return (0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static int
3020Sstevel@tonic-gate efi_read(int fd, struct dk_gpt *vtoc)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	int			i, j;
3050Sstevel@tonic-gate 	int			label_len;
3060Sstevel@tonic-gate 	int			rval = 0;
3070Sstevel@tonic-gate 	int			md_flag = 0;
3080Sstevel@tonic-gate 	struct dk_minfo		disk_info;
3090Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
3100Sstevel@tonic-gate 	efi_gpt_t		*efi;
3110Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
3120Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
3130Sstevel@tonic-gate 	uint32_t		user_length;
3143083Scg149915 	boolean_t		legacy_label = B_FALSE;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * get the partition number for this file descriptor.
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
3206590Syl194034 		if (efi_debug) {
3216590Syl194034 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
3226590Syl194034 		}
3230Sstevel@tonic-gate 		switch (errno) {
3240Sstevel@tonic-gate 		case EIO:
3250Sstevel@tonic-gate 			return (VT_EIO);
3260Sstevel@tonic-gate 		case EINVAL:
3270Sstevel@tonic-gate 			return (VT_EINVAL);
3280Sstevel@tonic-gate 		default:
3290Sstevel@tonic-gate 			return (VT_ERROR);
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
3330Sstevel@tonic-gate 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
3340Sstevel@tonic-gate 		md_flag++;
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 	/* get the LBA size */
3370Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
3380Sstevel@tonic-gate 		if (efi_debug) {
3390Sstevel@tonic-gate 			(void) fprintf(stderr,
3400Sstevel@tonic-gate 			    "assuming LBA 512 bytes %d\n",
3410Sstevel@tonic-gate 			    errno);
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	if (disk_info.dki_lbsize == 0) {
3460Sstevel@tonic-gate 		if (efi_debug) {
3470Sstevel@tonic-gate 			(void) fprintf(stderr,
3480Sstevel@tonic-gate 			    "efi_read: assuming LBA 512 bytes\n");
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Read the EFI GPT to figure out how many partitions we need
3540Sstevel@tonic-gate 	 * to deal with.
3550Sstevel@tonic-gate 	 */
3560Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
3570Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
3580Sstevel@tonic-gate 		label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
3590Sstevel@tonic-gate 	} else {
3600Sstevel@tonic-gate 		label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
3616590Syl194034 		    disk_info.dki_lbsize;
3620Sstevel@tonic-gate 		if (label_len % disk_info.dki_lbsize) {
3630Sstevel@tonic-gate 			/* pad to physical sector size */
3640Sstevel@tonic-gate 			label_len += disk_info.dki_lbsize;
3650Sstevel@tonic-gate 			label_len &= ~(disk_info.dki_lbsize - 1);
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
3700Sstevel@tonic-gate 		return (VT_ERROR);
3710Sstevel@tonic-gate 
3722913Syl194034 	dk_ioc.dki_length = disk_info.dki_lbsize;
3730Sstevel@tonic-gate 	user_length = vtoc->efi_nparts;
3740Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
3750Sstevel@tonic-gate 	if (md_flag) {
3762913Syl194034 		dk_ioc.dki_length = label_len;
3770Sstevel@tonic-gate 		if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
3780Sstevel@tonic-gate 			switch (errno) {
3790Sstevel@tonic-gate 			case EIO:
3800Sstevel@tonic-gate 				return (VT_EIO);
3810Sstevel@tonic-gate 			default:
3820Sstevel@tonic-gate 				return (VT_ERROR);
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 	} else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
3863083Scg149915 		/*
3873083Scg149915 		 * No valid label here; try the alternate. Note that here
3883083Scg149915 		 * we just read GPT header and save it into dk_ioc.data,
3893083Scg149915 		 * Later, we will read GUID partition entry array if we
3903083Scg149915 		 * can get valid GPT header.
3913083Scg149915 		 */
3923083Scg149915 
3933083Scg149915 		/*
3943083Scg149915 		 * This is a workaround for legacy systems. In the past, the
3953083Scg149915 		 * last sector of SCSI disk was invisible on x86 platform. At
3963083Scg149915 		 * that time, backup label was saved on the next to the last
3973083Scg149915 		 * sector. It is possible for users to move a disk from previous
3983083Scg149915 		 * solaris system to present system. Here, we attempt to search
3993083Scg149915 		 * legacy backup EFI label first.
4003083Scg149915 		 */
4013083Scg149915 		dk_ioc.dki_lba = disk_info.dki_capacity - 2;
4020Sstevel@tonic-gate 		dk_ioc.dki_length = disk_info.dki_lbsize;
4032285Scg149915 		rval = check_label(fd, &dk_ioc);
4043083Scg149915 		if (rval == VT_EINVAL) {
4052285Scg149915 			/*
4063083Scg149915 			 * we didn't find legacy backup EFI label, try to
4073083Scg149915 			 * search backup EFI label in the last block.
4082285Scg149915 			 */
4093083Scg149915 			dk_ioc.dki_lba = disk_info.dki_capacity - 1;
4102285Scg149915 			dk_ioc.dki_length = disk_info.dki_lbsize;
4112285Scg149915 			rval = check_label(fd, &dk_ioc);
4123083Scg149915 			if (rval == 0) {
4133083Scg149915 				legacy_label = B_TRUE;
4143083Scg149915 				if (efi_debug)
4153083Scg149915 					(void) fprintf(stderr,
4163083Scg149915 					    "efi_read: primary label corrupt; "
4173083Scg149915 					    "using EFI backup label located on"
4183083Scg149915 					    " the last block\n");
4192285Scg149915 			}
4203083Scg149915 		} else {
4213083Scg149915 			if ((efi_debug) && (rval == 0))
4223083Scg149915 				(void) fprintf(stderr, "efi_read: primary label"
4233083Scg149915 				    " corrupt; using legacy EFI backup label "
4243083Scg149915 				    " located on the next to last block\n");
4252285Scg149915 		}
4262285Scg149915 
4272285Scg149915 		if (rval == 0) {
4280Sstevel@tonic-gate 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4290Sstevel@tonic-gate 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
4300Sstevel@tonic-gate 			vtoc->efi_nparts =
4310Sstevel@tonic-gate 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4323083Scg149915 
4330Sstevel@tonic-gate 			/*
4343083Scg149915 			 * Partition tables are between backup GPT header
4353083Scg149915 			 * table and ParitionEntryLBA (the starting LBA of
4363083Scg149915 			 * the GUID partition entries array). Now that we
4373083Scg149915 			 * already got valid GPT header and saved it in
4383083Scg149915 			 * dk_ioc.dki_data, we try to get GUID partition
4393083Scg149915 			 * entry array here.
4400Sstevel@tonic-gate 			 */
4410Sstevel@tonic-gate 			dk_ioc.dki_data++;
4423083Scg149915 			if (legacy_label)
4433083Scg149915 				dk_ioc.dki_length = disk_info.dki_capacity - 1 -
4446590Syl194034 				    dk_ioc.dki_lba;
4453083Scg149915 			else
4463083Scg149915 				dk_ioc.dki_length = disk_info.dki_capacity - 2 -
4476590Syl194034 				    dk_ioc.dki_lba;
4480Sstevel@tonic-gate 			dk_ioc.dki_length *= disk_info.dki_lbsize;
4493083Scg149915 			if (dk_ioc.dki_length >
4503083Scg149915 			    ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
4510Sstevel@tonic-gate 				rval = VT_EINVAL;
4520Sstevel@tonic-gate 			} else {
4533083Scg149915 				/*
4543083Scg149915 				 * read GUID partition entry array
4553083Scg149915 				 */
4560Sstevel@tonic-gate 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
4570Sstevel@tonic-gate 			}
4580Sstevel@tonic-gate 		}
4592913Syl194034 	} else {
4602913Syl194034 		dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4612913Syl194034 		dk_ioc.dki_data++;
4622913Syl194034 		dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
4632913Syl194034 		rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 	if (rval < 0) {
4660Sstevel@tonic-gate 		free(efi);
4670Sstevel@tonic-gate 		return (rval);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
4712913Syl194034 	efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * Assemble this into a "dk_gpt" struct for easier
4750Sstevel@tonic-gate 	 * digestibility by applications.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
4780Sstevel@tonic-gate 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4790Sstevel@tonic-gate 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
4800Sstevel@tonic-gate 	vtoc->efi_lbasize = disk_info.dki_lbsize;
4810Sstevel@tonic-gate 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
4820Sstevel@tonic-gate 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
4830Sstevel@tonic-gate 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
4846590Syl194034 	vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
4850Sstevel@tonic-gate 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * If the array the user passed in is too small, set the length
4890Sstevel@tonic-gate 	 * to what it needs to be and return
4900Sstevel@tonic-gate 	 */
4910Sstevel@tonic-gate 	if (user_length < vtoc->efi_nparts) {
4920Sstevel@tonic-gate 		return (VT_EINVAL);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
4960Sstevel@tonic-gate 
4976590Syl194034 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
4986590Syl194034 		    efi_parts[i].efi_gpe_PartitionTypeGUID);
4990Sstevel@tonic-gate 
5006590Syl194034 		for (j = 0;
5016590Syl194034 		    j < sizeof (conversion_array)
5026590Syl194034 		    / sizeof (struct uuid_to_ptag); j++) {
5030Sstevel@tonic-gate 
5046590Syl194034 			if (bcmp(&vtoc->efi_parts[i].p_guid,
5056590Syl194034 			    &conversion_array[j].uuid,
5066590Syl194034 			    sizeof (struct uuid)) == 0) {
5076590Syl194034 				vtoc->efi_parts[i].p_tag = j;
5086590Syl194034 				break;
5096590Syl194034 			}
5106590Syl194034 		}
5116590Syl194034 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
5126590Syl194034 			continue;
5136590Syl194034 		vtoc->efi_parts[i].p_flag =
5146590Syl194034 		    LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
5156590Syl194034 		vtoc->efi_parts[i].p_start =
5166590Syl194034 		    LE_64(efi_parts[i].efi_gpe_StartingLBA);
5176590Syl194034 		vtoc->efi_parts[i].p_size =
5186590Syl194034 		    LE_64(efi_parts[i].efi_gpe_EndingLBA) -
5190Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_start + 1;
5206590Syl194034 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
5216590Syl194034 			vtoc->efi_parts[i].p_name[j] =
5226590Syl194034 			    (uchar_t)LE_16(
5236590Syl194034 			    efi_parts[i].efi_gpe_PartitionName[j]);
5246590Syl194034 		}
5250Sstevel@tonic-gate 
5266590Syl194034 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
5276590Syl194034 		    efi_parts[i].efi_gpe_UniquePartitionGUID);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 	free(efi);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	return (dki_info.dki_partition);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /* writes a "protective" MBR */
5350Sstevel@tonic-gate static int
5360Sstevel@tonic-gate write_pmbr(int fd, struct dk_gpt *vtoc)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
5390Sstevel@tonic-gate 	struct mboot	mb;
5400Sstevel@tonic-gate 	uchar_t		*cp;
5410Sstevel@tonic-gate 	diskaddr_t	size_in_lba;
5420Sstevel@tonic-gate 
543*6889Sxl222276 	/*
544*6889Sxl222276 	 * Preserve any boot code and disk signature if the first block is
545*6889Sxl222276 	 * already an MBR.
546*6889Sxl222276 	 */
547*6889Sxl222276 	dk_ioc.dki_lba = 0;
548*6889Sxl222276 	dk_ioc.dki_length = sizeof (mb);
549*6889Sxl222276 	/* LINTED -- always longlong aligned */
550*6889Sxl222276 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
551*6889Sxl222276 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1 ||
552*6889Sxl222276 	    mb.signature != LE_16(MBB_MAGIC)) {
553*6889Sxl222276 		bzero(&mb, sizeof (mb));
554*6889Sxl222276 		mb.signature = LE_16(MBB_MAGIC);
555*6889Sxl222276 	}
5560Sstevel@tonic-gate 	bzero(&mb.parts, sizeof (mb.parts));
5570Sstevel@tonic-gate 	cp = (uchar_t *)&mb.parts[0];
5580Sstevel@tonic-gate 	/* bootable or not */
5590Sstevel@tonic-gate 	*cp++ = 0;
5600Sstevel@tonic-gate 	/* beginning CHS; 0xffffff if not representable */
5610Sstevel@tonic-gate 	*cp++ = 0xff;
5620Sstevel@tonic-gate 	*cp++ = 0xff;
5630Sstevel@tonic-gate 	*cp++ = 0xff;
5640Sstevel@tonic-gate 	/* OS type */
5650Sstevel@tonic-gate 	*cp++ = EFI_PMBR;
5660Sstevel@tonic-gate 	/* ending CHS; 0xffffff if not representable */
5670Sstevel@tonic-gate 	*cp++ = 0xff;
5680Sstevel@tonic-gate 	*cp++ = 0xff;
5690Sstevel@tonic-gate 	*cp++ = 0xff;
5700Sstevel@tonic-gate 	/* starting LBA: 1 (little endian format) by EFI definition */
5710Sstevel@tonic-gate 	*cp++ = 0x01;
5720Sstevel@tonic-gate 	*cp++ = 0x00;
5730Sstevel@tonic-gate 	*cp++ = 0x00;
5740Sstevel@tonic-gate 	*cp++ = 0x00;
5750Sstevel@tonic-gate 	/* ending LBA: last block on the disk (little endian format) */
5760Sstevel@tonic-gate 	size_in_lba = vtoc->efi_last_lba;
5770Sstevel@tonic-gate 	if (size_in_lba < 0xffffffff) {
5780Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x000000ff);
5790Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
5800Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
5810Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0xff000000) >> 24;
5820Sstevel@tonic-gate 	} else {
5830Sstevel@tonic-gate 		*cp++ = 0xff;
5840Sstevel@tonic-gate 		*cp++ = 0xff;
5850Sstevel@tonic-gate 		*cp++ = 0xff;
5860Sstevel@tonic-gate 		*cp++ = 0xff;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
5890Sstevel@tonic-gate 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
5900Sstevel@tonic-gate 	dk_ioc.dki_lba = 0;
5910Sstevel@tonic-gate 	dk_ioc.dki_length = sizeof (mb);
5920Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
5930Sstevel@tonic-gate 		switch (errno) {
5940Sstevel@tonic-gate 		case EIO:
5950Sstevel@tonic-gate 			return (VT_EIO);
5960Sstevel@tonic-gate 		case EINVAL:
5970Sstevel@tonic-gate 			return (VT_EINVAL);
5980Sstevel@tonic-gate 		default:
5990Sstevel@tonic-gate 			return (VT_ERROR);
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 	return (0);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /* make sure the user specified something reasonable */
6060Sstevel@tonic-gate static int
6070Sstevel@tonic-gate check_input(struct dk_gpt *vtoc)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	int			resv_part = -1;
6100Sstevel@tonic-gate 	int			i, j;
6110Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	/*
6140Sstevel@tonic-gate 	 * Sanity-check the input (make sure no partitions overlap)
6150Sstevel@tonic-gate 	 */
6160Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
6170Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
6180Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
6190Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
6200Sstevel@tonic-gate 			if (efi_debug) {
6210Sstevel@tonic-gate 				(void) fprintf(stderr,
6220Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size of %llu",
6230Sstevel@tonic-gate 				    i,
6240Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
6250Sstevel@tonic-gate 			}
6260Sstevel@tonic-gate 			return (VT_EINVAL);
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
6292866Sszhou 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
6302866Sszhou 				continue;
6312866Sszhou 			/* we have encountered an unknown uuid */
6322866Sszhou 			vtoc->efi_parts[i].p_tag = 0xff;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
6350Sstevel@tonic-gate 			if (resv_part != -1) {
6360Sstevel@tonic-gate 				if (efi_debug) {
6376590Syl194034 					(void) fprintf(stderr,
6380Sstevel@tonic-gate "found duplicate reserved partition at %d\n",
6396590Syl194034 					    i);
6400Sstevel@tonic-gate 				}
6410Sstevel@tonic-gate 				return (VT_EINVAL);
6420Sstevel@tonic-gate 			}
6430Sstevel@tonic-gate 			resv_part = i;
6440Sstevel@tonic-gate 		}
6450Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
6460Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
6470Sstevel@tonic-gate 			if (efi_debug) {
6480Sstevel@tonic-gate 				(void) fprintf(stderr,
6490Sstevel@tonic-gate 				    "Partition %d starts at %llu.  ",
6500Sstevel@tonic-gate 				    i,
6510Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start);
6520Sstevel@tonic-gate 				(void) fprintf(stderr,
6530Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
6540Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
6550Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
6560Sstevel@tonic-gate 			}
6570Sstevel@tonic-gate 			return (VT_EINVAL);
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
6600Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
6610Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
6620Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
6630Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
6640Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
6650Sstevel@tonic-gate 			if (efi_debug) {
6660Sstevel@tonic-gate 				(void) fprintf(stderr,
6670Sstevel@tonic-gate 				    "Partition %d ends at %llu.  ",
6680Sstevel@tonic-gate 				    i,
6690Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start +
6700Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
6710Sstevel@tonic-gate 				(void) fprintf(stderr,
6720Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
6730Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
6740Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 			return (VT_EINVAL);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
6800Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
6810Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
6820Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
6830Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
6840Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
6850Sstevel@tonic-gate 				endsect = jstart + jsize -1;
6860Sstevel@tonic-gate 				if ((jstart <= istart) &&
6870Sstevel@tonic-gate 				    (istart <= endsect)) {
6880Sstevel@tonic-gate 					if (efi_debug) {
6890Sstevel@tonic-gate 						(void) fprintf(stderr,
6900Sstevel@tonic-gate "Partition %d overlaps partition %d.",
6910Sstevel@tonic-gate 						    i, j);
6926590Syl194034 					}
6936590Syl194034 					return (VT_EINVAL);
6940Sstevel@tonic-gate 				}
6950Sstevel@tonic-gate 			}
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 	/* just a warning for now */
6990Sstevel@tonic-gate 	if ((resv_part == -1) && efi_debug) {
7000Sstevel@tonic-gate 		(void) fprintf(stderr,
7016590Syl194034 		    "no reserved partition found\n");
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 	return (0);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate /*
7076590Syl194034  * add all the unallocated space to the current label
7086590Syl194034  */
7096590Syl194034 int
7106590Syl194034 efi_use_whole_disk(int fd)
7116590Syl194034 {
7126590Syl194034 	struct dk_gpt		*efi_label;
7136590Syl194034 	int			rval;
7146590Syl194034 	int			i;
7156590Syl194034 	uint_t			phy_last_slice = 0;
7166590Syl194034 	diskaddr_t		pl_start = 0;
7176590Syl194034 	diskaddr_t		pl_size;
7186590Syl194034 
7196590Syl194034 	rval = efi_alloc_and_read(fd, &efi_label);
7206590Syl194034 	if (rval < 0) {
7216590Syl194034 		return (rval);
7226590Syl194034 	}
7236590Syl194034 
7246590Syl194034 	/* find the last physically non-zero partition */
7256590Syl194034 	for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
7266590Syl194034 		if (pl_start < efi_label->efi_parts[i].p_start) {
7276590Syl194034 			pl_start = efi_label->efi_parts[i].p_start;
7286590Syl194034 			phy_last_slice = i;
7296590Syl194034 		}
7306590Syl194034 	}
7316590Syl194034 	pl_size = efi_label->efi_parts[phy_last_slice].p_size;
7326590Syl194034 
7336590Syl194034 	/*
7346590Syl194034 	 * If alter_lba is 1, we are using the backup label.
7356590Syl194034 	 * Since we can locate the backup label by disk capacity,
7366590Syl194034 	 * there must be no unallocated space.
7376590Syl194034 	 */
7386590Syl194034 	if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
7396590Syl194034 	    >= efi_label->efi_last_lba)) {
7406590Syl194034 		if (efi_debug) {
7416590Syl194034 			(void) fprintf(stderr,
7426590Syl194034 			    "efi_use_whole_disk: requested space not found\n");
7436590Syl194034 		}
7446590Syl194034 		efi_free(efi_label);
7456590Syl194034 		return (VT_ENOSPC);
7466590Syl194034 	}
7476590Syl194034 
7486590Syl194034 	/*
7496590Syl194034 	 * If there is space between the last physically non-zero partition
7506590Syl194034 	 * and the reserved partition, just add the unallocated space to this
7516590Syl194034 	 * area. Otherwise, the unallocated space is added to the last
7526590Syl194034 	 * physically non-zero partition.
7536590Syl194034 	 */
7546590Syl194034 	if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
7556590Syl194034 	    EFI_MIN_RESV_SIZE) {
7566590Syl194034 		efi_label->efi_parts[phy_last_slice].p_size +=
7576590Syl194034 		    efi_label->efi_last_lba - efi_label->efi_altern_lba;
7586590Syl194034 	}
7596590Syl194034 
7606590Syl194034 	/*
7616590Syl194034 	 * Move the reserved partition. There is currently no data in
7626590Syl194034 	 * here except fabricated devids (which get generated via
7636590Syl194034 	 * efi_write()). So there is no need to copy data.
7646590Syl194034 	 */
7656590Syl194034 	efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
7666590Syl194034 	    efi_label->efi_last_lba - efi_label->efi_altern_lba;
7676590Syl194034 	efi_label->efi_last_u_lba += efi_label->efi_last_lba
7686590Syl194034 	    - efi_label->efi_altern_lba;
7696590Syl194034 
7706590Syl194034 	rval = efi_write(fd, efi_label);
7716590Syl194034 	if (rval < 0) {
7726590Syl194034 		if (efi_debug) {
7736590Syl194034 			(void) fprintf(stderr,
7746590Syl194034 			    "efi_use_whole_disk:fail to write label, rval=%d\n",
7756590Syl194034 			    rval);
7766590Syl194034 		}
7776590Syl194034 		efi_free(efi_label);
7786590Syl194034 		return (rval);
7796590Syl194034 	}
7806590Syl194034 
7816590Syl194034 	efi_free(efi_label);
7826590Syl194034 	return (0);
7836590Syl194034 }
7846590Syl194034 
7856590Syl194034 
7866590Syl194034 /*
7870Sstevel@tonic-gate  * write EFI label and backup label
7880Sstevel@tonic-gate  */
7890Sstevel@tonic-gate int
7900Sstevel@tonic-gate efi_write(int fd, struct dk_gpt *vtoc)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
7930Sstevel@tonic-gate 	efi_gpt_t		*efi;
7940Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
7950Sstevel@tonic-gate 	int			i, j;
7960Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
7970Sstevel@tonic-gate 	int			md_flag = 0;
7983083Scg149915 	int			nblocks;
7993083Scg149915 	diskaddr_t		lba_backup_gpt_hdr;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
8020Sstevel@tonic-gate 		if (efi_debug)
8030Sstevel@tonic-gate 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
8040Sstevel@tonic-gate 		switch (errno) {
8050Sstevel@tonic-gate 		case EIO:
8060Sstevel@tonic-gate 			return (VT_EIO);
8070Sstevel@tonic-gate 		case EINVAL:
8080Sstevel@tonic-gate 			return (VT_EINVAL);
8090Sstevel@tonic-gate 		default:
8100Sstevel@tonic-gate 			return (VT_ERROR);
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/* check if we are dealing wih a metadevice */
8150Sstevel@tonic-gate 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
8160Sstevel@tonic-gate 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
8170Sstevel@tonic-gate 		md_flag = 1;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if (check_input(vtoc)) {
8210Sstevel@tonic-gate 		/*
8220Sstevel@tonic-gate 		 * not valid; if it's a metadevice just pass it down
8230Sstevel@tonic-gate 		 * because SVM will do its own checking
8240Sstevel@tonic-gate 		 */
8250Sstevel@tonic-gate 		if (md_flag == 0) {
8260Sstevel@tonic-gate 			return (VT_EINVAL);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
8310Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
8320Sstevel@tonic-gate 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
8330Sstevel@tonic-gate 	} else {
8340Sstevel@tonic-gate 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
8356590Syl194034 		    vtoc->efi_lbasize) *
8366590Syl194034 		    vtoc->efi_lbasize;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8393083Scg149915 	/*
8403083Scg149915 	 * the number of blocks occupied by GUID partition entry array
8413083Scg149915 	 */
8423083Scg149915 	nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
8433083Scg149915 
8443083Scg149915 	/*
8453083Scg149915 	 * Backup GPT header is located on the block after GUID
8463083Scg149915 	 * partition entry array. Here, we calculate the address
8473083Scg149915 	 * for backup GPT header.
8483083Scg149915 	 */
8493083Scg149915 	lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
8503083Scg149915 
8510Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
8520Sstevel@tonic-gate 		return (VT_ERROR);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/* stuff user's input into EFI struct */
8570Sstevel@tonic-gate 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
8580Sstevel@tonic-gate 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
8590Sstevel@tonic-gate 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
8600Sstevel@tonic-gate 	efi->efi_gpt_Reserved1 = 0;
8610Sstevel@tonic-gate 	efi->efi_gpt_MyLBA = LE_64(1ULL);
8623083Scg149915 	efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
8630Sstevel@tonic-gate 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
8640Sstevel@tonic-gate 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
8650Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
8660Sstevel@tonic-gate 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
8670Sstevel@tonic-gate 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
8680Sstevel@tonic-gate 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
8710Sstevel@tonic-gate 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + sizeof (efi_gpt_t));
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
8746590Syl194034 		for (j = 0;
8756590Syl194034 		    j < sizeof (conversion_array) /
8766590Syl194034 		    sizeof (struct uuid_to_ptag); j++) {
8770Sstevel@tonic-gate 
8786590Syl194034 			if (vtoc->efi_parts[i].p_tag == j) {
8796590Syl194034 				UUID_LE_CONVERT(
8806590Syl194034 				    efi_parts[i].efi_gpe_PartitionTypeGUID,
8816590Syl194034 				    conversion_array[j].uuid);
8826590Syl194034 				break;
8836590Syl194034 			}
8846590Syl194034 		}
8852866Sszhou 
8866590Syl194034 		if (j == sizeof (conversion_array) /
8876590Syl194034 		    sizeof (struct uuid_to_ptag)) {
8886590Syl194034 			/*
8896590Syl194034 			 * If we didn't have a matching uuid match, bail here.
8906590Syl194034 			 * Don't write a label with unknown uuid.
8916590Syl194034 			 */
8926590Syl194034 			if (efi_debug) {
8936590Syl194034 				(void) fprintf(stderr,
8946590Syl194034 				    "Unknown uuid for p_tag %d\n",
8956590Syl194034 				    vtoc->efi_parts[i].p_tag);
8966590Syl194034 			}
8976590Syl194034 			return (VT_EINVAL);
8986590Syl194034 		}
8992866Sszhou 
9006590Syl194034 		efi_parts[i].efi_gpe_StartingLBA =
9016590Syl194034 		    LE_64(vtoc->efi_parts[i].p_start);
9026590Syl194034 		efi_parts[i].efi_gpe_EndingLBA =
9036590Syl194034 		    LE_64(vtoc->efi_parts[i].p_start +
9046590Syl194034 		    vtoc->efi_parts[i].p_size - 1);
9056590Syl194034 		efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
9060Sstevel@tonic-gate 		    LE_16(vtoc->efi_parts[i].p_flag);
9076590Syl194034 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
9086590Syl194034 			efi_parts[i].efi_gpe_PartitionName[j] =
9096590Syl194034 			    LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
9106590Syl194034 		}
9116590Syl194034 		if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
9126590Syl194034 		    uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
9136590Syl194034 			(void) uuid_generate((uchar_t *)
9146590Syl194034 			    &vtoc->efi_parts[i].p_uguid);
9156590Syl194034 		}
9166590Syl194034 		bcopy(&vtoc->efi_parts[i].p_uguid,
9176590Syl194034 		    &efi_parts[i].efi_gpe_UniquePartitionGUID,
9186590Syl194034 		    sizeof (uuid_t));
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryArrayCRC32 =
9210Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi_parts,
9220Sstevel@tonic-gate 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
9230Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
9240Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9270Sstevel@tonic-gate 		free(dk_ioc.dki_data);
9280Sstevel@tonic-gate 		switch (errno) {
9290Sstevel@tonic-gate 		case EIO:
9300Sstevel@tonic-gate 			return (VT_EIO);
9310Sstevel@tonic-gate 		case EINVAL:
9320Sstevel@tonic-gate 			return (VT_EINVAL);
9330Sstevel@tonic-gate 		default:
9340Sstevel@tonic-gate 			return (VT_ERROR);
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 	/* if it's a metadevice we're done */
9380Sstevel@tonic-gate 	if (md_flag) {
9390Sstevel@tonic-gate 		free(dk_ioc.dki_data);
9400Sstevel@tonic-gate 		return (0);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 	/* write backup partition array */
9430Sstevel@tonic-gate 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
9440Sstevel@tonic-gate 	dk_ioc.dki_length -= vtoc->efi_lbasize;
9450Sstevel@tonic-gate 	dk_ioc.dki_data++;
9460Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9470Sstevel@tonic-gate 		/*
9480Sstevel@tonic-gate 		 * we wrote the primary label okay, so don't fail
9490Sstevel@tonic-gate 		 */
9500Sstevel@tonic-gate 		if (efi_debug) {
9510Sstevel@tonic-gate 			(void) fprintf(stderr,
9520Sstevel@tonic-gate 			    "write of backup partitions to block %llu "
9530Sstevel@tonic-gate 			    "failed, errno %d\n",
9540Sstevel@tonic-gate 			    vtoc->efi_last_u_lba + 1,
9550Sstevel@tonic-gate 			    errno);
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * now swap MyLBA and AlternateLBA fields and write backup
9600Sstevel@tonic-gate 	 * partition table header
9610Sstevel@tonic-gate 	 */
9623083Scg149915 	dk_ioc.dki_lba = lba_backup_gpt_hdr;
9630Sstevel@tonic-gate 	dk_ioc.dki_length = vtoc->efi_lbasize;
9640Sstevel@tonic-gate 	dk_ioc.dki_data--;
9650Sstevel@tonic-gate 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
9663083Scg149915 	efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
9670Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
9680Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
9690Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
9700Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
9710Sstevel@tonic-gate 	    sizeof (struct efi_gpt)));
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9740Sstevel@tonic-gate 		if (efi_debug) {
9750Sstevel@tonic-gate 			(void) fprintf(stderr,
9760Sstevel@tonic-gate 			    "write of backup header to block %llu failed, "
9770Sstevel@tonic-gate 			    "errno %d\n",
9783083Scg149915 			    lba_backup_gpt_hdr,
9790Sstevel@tonic-gate 			    errno);
9800Sstevel@tonic-gate 		}
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 	/* write the PMBR */
9830Sstevel@tonic-gate 	(void) write_pmbr(fd, vtoc);
9840Sstevel@tonic-gate 	free(dk_ioc.dki_data);
9850Sstevel@tonic-gate 	return (0);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate void
9890Sstevel@tonic-gate efi_free(struct dk_gpt *ptr)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	free(ptr);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*
9950Sstevel@tonic-gate  * Input: File descriptor
9960Sstevel@tonic-gate  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate int
9990Sstevel@tonic-gate efi_type(int fd)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	struct vtoc vtoc;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
10040Sstevel@tonic-gate 		if (errno == ENOTSUP) {
10050Sstevel@tonic-gate 			return (1);
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 	return (0);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate void
10120Sstevel@tonic-gate efi_err_check(struct dk_gpt *vtoc)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	int			resv_part = -1;
10150Sstevel@tonic-gate 	int			i, j;
10160Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
10170Sstevel@tonic-gate 	int			overlap = 0;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	/*
10200Sstevel@tonic-gate 	 * make sure no partitions overlap
10210Sstevel@tonic-gate 	 */
10220Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
10230Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
10240Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
10250Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
10260Sstevel@tonic-gate 			(void) fprintf(stderr,
10270Sstevel@tonic-gate 			    "partition %d is \"unassigned\" but has a size "
10280Sstevel@tonic-gate 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
10310Sstevel@tonic-gate 			continue;
10320Sstevel@tonic-gate 		}
10330Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
10340Sstevel@tonic-gate 			if (resv_part != -1) {
10350Sstevel@tonic-gate 				(void) fprintf(stderr,
10360Sstevel@tonic-gate 				    "found duplicate reserved partition at "
10370Sstevel@tonic-gate 				    "%d\n", i);
10380Sstevel@tonic-gate 			}
10390Sstevel@tonic-gate 			resv_part = i;
10400Sstevel@tonic-gate 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
10410Sstevel@tonic-gate 				(void) fprintf(stderr,
10420Sstevel@tonic-gate 				    "Warning: reserved partition size must "
10430Sstevel@tonic-gate 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
10460Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
10470Sstevel@tonic-gate 			(void) fprintf(stderr,
10480Sstevel@tonic-gate 			    "Partition %d starts at %llu\n",
10490Sstevel@tonic-gate 			    i,
10500Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start);
10510Sstevel@tonic-gate 			(void) fprintf(stderr,
10520Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
10530Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
10540Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
10570Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
10580Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
10590Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
10600Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
10610Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
10620Sstevel@tonic-gate 			(void) fprintf(stderr,
10630Sstevel@tonic-gate 			    "Partition %d ends at %llu\n",
10640Sstevel@tonic-gate 			    i,
10650Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start +
10660Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_size);
10670Sstevel@tonic-gate 			(void) fprintf(stderr,
10680Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
10690Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
10700Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
10710Sstevel@tonic-gate 		}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
10740Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
10750Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
10760Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
10770Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
10780Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
10790Sstevel@tonic-gate 				endsect = jstart + jsize -1;
10800Sstevel@tonic-gate 				if ((jstart <= istart) &&
10810Sstevel@tonic-gate 				    (istart <= endsect)) {
10820Sstevel@tonic-gate 					if (!overlap) {
10830Sstevel@tonic-gate 					(void) fprintf(stderr,
10840Sstevel@tonic-gate 					    "label error: EFI Labels do not "
10850Sstevel@tonic-gate 					    "support overlapping partitions\n");
10860Sstevel@tonic-gate 					}
10870Sstevel@tonic-gate 					(void) fprintf(stderr,
10880Sstevel@tonic-gate 					    "Partition %d overlaps partition "
10890Sstevel@tonic-gate 					    "%d.\n", i, j);
10900Sstevel@tonic-gate 					overlap = 1;
10910Sstevel@tonic-gate 				}
10920Sstevel@tonic-gate 			}
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	/* make sure there is a reserved partition */
10960Sstevel@tonic-gate 	if (resv_part == -1) {
10970Sstevel@tonic-gate 		(void) fprintf(stderr,
10986590Syl194034 		    "no reserved partition found\n");
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * We need to get information necessary to construct a *new* efi
11040Sstevel@tonic-gate  * label type
11050Sstevel@tonic-gate  */
11060Sstevel@tonic-gate int
11070Sstevel@tonic-gate efi_auto_sense(int fd, struct dk_gpt **vtoc)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	int	i;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	/*
11130Sstevel@tonic-gate 	 * Now build the default partition table
11140Sstevel@tonic-gate 	 */
11150Sstevel@tonic-gate 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
11160Sstevel@tonic-gate 		if (efi_debug) {
11170Sstevel@tonic-gate 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 		return (-1);
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
11230Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
11240Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
11250Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_start = 0;
11260Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_size = 0;
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Make constants first
11300Sstevel@tonic-gate 	 * and variable partitions later
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	/* root partition - s0 128 MB */
11340Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_start = 34;
11350Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_size = 262144;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* partition - s1  128 MB */
11380Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_start = 262178;
11390Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_size = 262144;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	/* partition -s2 is NOT the Backup disk */
11420Sstevel@tonic-gate 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/* partition -s6 /usr partition - HOG */
11450Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_start = 524322;
11460Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
11470Sstevel@tonic-gate 	    - (1024 * 16);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	/* efi reserved partition - s9 16K */
11500Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
11510Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
11520Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
11530Sstevel@tonic-gate 	return (0);
11540Sstevel@tonic-gate }
1155