17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54f0dea16Scg  * Common Development and Distribution License (the "License").
64f0dea16Scg  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*af007057Syl  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <uuid/uuid.h>
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
377c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
387c478bd9Sstevel@tonic-gate #include <sys/mhd.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
417c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
427c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static struct uuid_to_ptag {
467c478bd9Sstevel@tonic-gate 	struct uuid	uuid;
477c478bd9Sstevel@tonic-gate } conversion_array[] = {
487c478bd9Sstevel@tonic-gate 	{ EFI_UNUSED },
497c478bd9Sstevel@tonic-gate 	{ EFI_BOOT },
507c478bd9Sstevel@tonic-gate 	{ EFI_ROOT },
517c478bd9Sstevel@tonic-gate 	{ EFI_SWAP },
527c478bd9Sstevel@tonic-gate 	{ EFI_USR },
537c478bd9Sstevel@tonic-gate 	{ EFI_BACKUP },
547c478bd9Sstevel@tonic-gate 	{ 0 },			/* STAND is never used */
557c478bd9Sstevel@tonic-gate 	{ EFI_VAR },
567c478bd9Sstevel@tonic-gate 	{ EFI_HOME },
577c478bd9Sstevel@tonic-gate 	{ EFI_ALTSCTR },
587c478bd9Sstevel@tonic-gate 	{ 0 },			/* CACHE (cachefs) is never used */
597c478bd9Sstevel@tonic-gate 	{ EFI_RESERVED },
607c478bd9Sstevel@tonic-gate 	{ EFI_SYSTEM },
617c478bd9Sstevel@tonic-gate 	{ EFI_LEGACY_MBR },
627c478bd9Sstevel@tonic-gate 	{ EFI_RESV3 },
637c478bd9Sstevel@tonic-gate 	{ EFI_RESV4 },
647c478bd9Sstevel@tonic-gate 	{ EFI_MSFT_RESV },
657c478bd9Sstevel@tonic-gate 	{ EFI_DELL_BASIC },
667c478bd9Sstevel@tonic-gate 	{ EFI_DELL_RAID },
677c478bd9Sstevel@tonic-gate 	{ EFI_DELL_SWAP },
687c478bd9Sstevel@tonic-gate 	{ EFI_DELL_LVM },
69f2be5148Sszhou 	{ EFI_DELL_RESV },
70f2be5148Sszhou 	{ EFI_AAPL_HFS },
71f2be5148Sszhou 	{ EFI_AAPL_UFS }
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Default vtoc information for non-SVr4 partitions
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate struct dk_map2  default_vtoc_map[NDKMAP] = {
787c478bd9Sstevel@tonic-gate 	{	V_ROOT,		0	},		/* a - 0 */
797c478bd9Sstevel@tonic-gate 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
807c478bd9Sstevel@tonic-gate 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
817c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* d - 3 */
827c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* e - 4 */
837c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* f - 5 */
847c478bd9Sstevel@tonic-gate 	{	V_USR,		0	},		/* g - 6 */
857c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* h - 7 */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
907c478bd9Sstevel@tonic-gate 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
917c478bd9Sstevel@tonic-gate 	{	V_ALTSCTR,	0	},		/* j - 9 */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #else
947c478bd9Sstevel@tonic-gate #error No VTOC format defined.
957c478bd9Sstevel@tonic-gate #endif			/* defined(i386) */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* k - 10 */
987c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* l - 11 */
997c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* m - 12 */
1007c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* n - 13 */
1017c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* o - 14 */
1027c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* p - 15 */
1037c478bd9Sstevel@tonic-gate #endif			/* defined(_SUNOS_VTOC_16) */
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #ifdef DEBUG
1077c478bd9Sstevel@tonic-gate int efi_debug = 1;
1087c478bd9Sstevel@tonic-gate #else
1097c478bd9Sstevel@tonic-gate int efi_debug = 0;
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate extern unsigned int	efi_crc32(const unsigned char *, unsigned int);
1137c478bd9Sstevel@tonic-gate static int		efi_read(int, struct dk_gpt *);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static int
1167c478bd9Sstevel@tonic-gate read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	struct dk_minfo		disk_info;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
1217c478bd9Sstevel@tonic-gate 		return (errno);
1227c478bd9Sstevel@tonic-gate 	*capacity = disk_info.dki_capacity;
1237c478bd9Sstevel@tonic-gate 	*lbsize = disk_info.dki_lbsize;
1247c478bd9Sstevel@tonic-gate 	return (0);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * the number of blocks the EFI label takes up (round up to nearest
1297c478bd9Sstevel@tonic-gate  * block)
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate #define	NBLOCKS(p, l)	(1 + ((((p) * (int)sizeof (efi_gpe_t))  + \
1327c478bd9Sstevel@tonic-gate 				((l) - 1)) / (l)))
1337c478bd9Sstevel@tonic-gate /* number of partitions -- limited by what we can malloc */
1347c478bd9Sstevel@tonic-gate #define	MAX_PARTS	((4294967295UL - sizeof (struct dk_gpt)) / \
1357c478bd9Sstevel@tonic-gate 			    sizeof (struct dk_part))
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
1387c478bd9Sstevel@tonic-gate efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	diskaddr_t	capacity;
1417c478bd9Sstevel@tonic-gate 	uint_t		lbsize;
1427c478bd9Sstevel@tonic-gate 	uint_t		nblocks;
1437c478bd9Sstevel@tonic-gate 	size_t		length;
1447c478bd9Sstevel@tonic-gate 	struct dk_gpt	*vptr;
1457c478bd9Sstevel@tonic-gate 	struct uuid	uuid;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (read_disk_info(fd, &capacity, &lbsize) != 0) {
1487c478bd9Sstevel@tonic-gate 		if (efi_debug)
1497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1507c478bd9Sstevel@tonic-gate 			    "couldn't read disk information\n");
1517c478bd9Sstevel@tonic-gate 		return (-1);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	nblocks = NBLOCKS(nparts, lbsize);
1557c478bd9Sstevel@tonic-gate 	if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
1567c478bd9Sstevel@tonic-gate 		/* 16K plus one block for the GPT */
1577c478bd9Sstevel@tonic-gate 		nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (nparts > MAX_PARTS) {
1617c478bd9Sstevel@tonic-gate 		if (efi_debug) {
1627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1637c478bd9Sstevel@tonic-gate 			"the maximum number of partitions supported is %lu\n",
1647c478bd9Sstevel@tonic-gate 			    MAX_PARTS);
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 		return (-1);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	length = sizeof (struct dk_gpt) +
1707c478bd9Sstevel@tonic-gate 	    sizeof (struct dk_part) * (nparts - 1);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
1737c478bd9Sstevel@tonic-gate 		return (-1);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	vptr = *vtoc;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	vptr->efi_version = EFI_VERSION_CURRENT;
1787c478bd9Sstevel@tonic-gate 	vptr->efi_lbasize = lbsize;
1797c478bd9Sstevel@tonic-gate 	vptr->efi_nparts = nparts;
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * add one block here for the PMBR; on disks with a 512 byte
1827c478bd9Sstevel@tonic-gate 	 * block size and 128 or fewer partitions, efi_first_u_lba
1837c478bd9Sstevel@tonic-gate 	 * should work out to "34"
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	vptr->efi_first_u_lba = nblocks + 1;
1867c478bd9Sstevel@tonic-gate 	vptr->efi_last_lba = capacity - 1;
187*af007057Syl 	vptr->efi_altern_lba = capacity -1;
1887c478bd9Sstevel@tonic-gate 	vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
1897c478bd9Sstevel@tonic-gate 	(void) uuid_generate((uchar_t *)&uuid);
1907c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
1917c478bd9Sstevel@tonic-gate 	return (0);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Read EFI - return partition number upon success.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate int
1987c478bd9Sstevel@tonic-gate efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	int			rval;
2017c478bd9Sstevel@tonic-gate 	uint32_t		nparts;
2027c478bd9Sstevel@tonic-gate 	int			length;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* figure out the number of entries that would fit into 16K */
2057c478bd9Sstevel@tonic-gate 	nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
2067c478bd9Sstevel@tonic-gate 	length = (int) sizeof (struct dk_gpt) +
207*af007057Syl 	    (int) sizeof (struct dk_part) * (nparts - 1);
2087c478bd9Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
2097c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_nparts = nparts;
2127c478bd9Sstevel@tonic-gate 	rval = efi_read(fd, *vtoc);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
2157c478bd9Sstevel@tonic-gate 		void *tmp;
2167c478bd9Sstevel@tonic-gate 		length = (int) sizeof (struct dk_gpt) +
217*af007057Syl 		    (int) sizeof (struct dk_part) *
218*af007057Syl 		    ((*vtoc)->efi_nparts - 1);
2197c478bd9Sstevel@tonic-gate 		nparts = (*vtoc)->efi_nparts;
2207c478bd9Sstevel@tonic-gate 		if ((tmp = realloc(*vtoc, length)) == NULL) {
2217c478bd9Sstevel@tonic-gate 			free (*vtoc);
2227c478bd9Sstevel@tonic-gate 			*vtoc = NULL;
2237c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
2247c478bd9Sstevel@tonic-gate 		} else {
2257c478bd9Sstevel@tonic-gate 			*vtoc = tmp;
2267c478bd9Sstevel@tonic-gate 			rval = efi_read(fd, *vtoc);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (rval < 0) {
2317c478bd9Sstevel@tonic-gate 		if (efi_debug) {
2327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2337c478bd9Sstevel@tonic-gate 			    "read of EFI table failed, rval=%d\n", rval);
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		free (*vtoc);
2367c478bd9Sstevel@tonic-gate 		*vtoc = NULL;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (rval);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static int
2437c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
2467c478bd9Sstevel@tonic-gate 	int error;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
2497c478bd9Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
2507c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data = data;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	return (error);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static int
2567c478bd9Sstevel@tonic-gate check_label(int fd, dk_efi_t *dk_ioc)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
2597c478bd9Sstevel@tonic-gate 	uint_t			crc;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
2627c478bd9Sstevel@tonic-gate 		switch (errno) {
2637c478bd9Sstevel@tonic-gate 		case EIO:
2647c478bd9Sstevel@tonic-gate 			return (VT_EIO);
2657c478bd9Sstevel@tonic-gate 		default:
2667c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	efi = dk_ioc->dki_data;
2707c478bd9Sstevel@tonic-gate 	if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
2717c478bd9Sstevel@tonic-gate 		if (efi_debug)
2727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2737c478bd9Sstevel@tonic-gate 			    "Bad EFI signature: 0x%llx != 0x%llx\n",
2747c478bd9Sstevel@tonic-gate 			    (long long)efi->efi_gpt_Signature,
2757c478bd9Sstevel@tonic-gate 			    (long long)LE_64(EFI_SIGNATURE));
2767c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * check CRC of the header; the size of the header should
2817c478bd9Sstevel@tonic-gate 	 * never be larger than one block
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 	crc = efi->efi_gpt_HeaderCRC32;
2847c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
2877c478bd9Sstevel@tonic-gate 	    crc != LE_32(efi_crc32((unsigned char *)efi,
2887c478bd9Sstevel@tonic-gate 	    LE_32(efi->efi_gpt_HeaderSize)))) {
2897c478bd9Sstevel@tonic-gate 		if (efi_debug)
2907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
291*af007057Syl 			    "Bad EFI CRC: 0x%x != 0x%x\n",
292*af007057Syl 			    crc,
293*af007057Syl 			    LE_32(efi_crc32((unsigned char *)efi,
294*af007057Syl 			    sizeof (struct efi_gpt))));
2957c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static int
3027c478bd9Sstevel@tonic-gate efi_read(int fd, struct dk_gpt *vtoc)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	int			i, j;
3057c478bd9Sstevel@tonic-gate 	int			label_len;
3067c478bd9Sstevel@tonic-gate 	int			rval = 0;
3077c478bd9Sstevel@tonic-gate 	int			md_flag = 0;
3087c478bd9Sstevel@tonic-gate 	struct dk_minfo		disk_info;
3097c478bd9Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
3107c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
3117c478bd9Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
3127c478bd9Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
3137c478bd9Sstevel@tonic-gate 	uint32_t		user_length;
31486bbaf93Scg 	boolean_t		legacy_label = B_FALSE;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * get the partition number for this file descriptor.
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
320*af007057Syl 		if (efi_debug) {
321*af007057Syl 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
322*af007057Syl 		}
3237c478bd9Sstevel@tonic-gate 		switch (errno) {
3247c478bd9Sstevel@tonic-gate 		case EIO:
3257c478bd9Sstevel@tonic-gate 			return (VT_EIO);
3267c478bd9Sstevel@tonic-gate 		case EINVAL:
3277c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
3287c478bd9Sstevel@tonic-gate 		default:
3297c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
3337c478bd9Sstevel@tonic-gate 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
3347c478bd9Sstevel@tonic-gate 		md_flag++;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	/* get the LBA size */
3377c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
3387c478bd9Sstevel@tonic-gate 		if (efi_debug) {
3397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3407c478bd9Sstevel@tonic-gate 			    "assuming LBA 512 bytes %d\n",
3417c478bd9Sstevel@tonic-gate 			    errno);
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	if (disk_info.dki_lbsize == 0) {
3467c478bd9Sstevel@tonic-gate 		if (efi_debug) {
3477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3487c478bd9Sstevel@tonic-gate 			    "efi_read: assuming LBA 512 bytes\n");
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Read the EFI GPT to figure out how many partitions we need
3547c478bd9Sstevel@tonic-gate 	 * to deal with.
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
3577c478bd9Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
3587c478bd9Sstevel@tonic-gate 		label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
3597c478bd9Sstevel@tonic-gate 	} else {
3607c478bd9Sstevel@tonic-gate 		label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
361*af007057Syl 		    disk_info.dki_lbsize;
3627c478bd9Sstevel@tonic-gate 		if (label_len % disk_info.dki_lbsize) {
3637c478bd9Sstevel@tonic-gate 			/* pad to physical sector size */
3647c478bd9Sstevel@tonic-gate 			label_len += disk_info.dki_lbsize;
3657c478bd9Sstevel@tonic-gate 			label_len &= ~(disk_info.dki_lbsize - 1);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
3707c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
3717c478bd9Sstevel@tonic-gate 
372c7e1889fSyl 	dk_ioc.dki_length = disk_info.dki_lbsize;
3737c478bd9Sstevel@tonic-gate 	user_length = vtoc->efi_nparts;
3747c478bd9Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
3757c478bd9Sstevel@tonic-gate 	if (md_flag) {
376c7e1889fSyl 		dk_ioc.dki_length = label_len;
3777c478bd9Sstevel@tonic-gate 		if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
3787c478bd9Sstevel@tonic-gate 			switch (errno) {
3797c478bd9Sstevel@tonic-gate 			case EIO:
3807c478bd9Sstevel@tonic-gate 				return (VT_EIO);
3817c478bd9Sstevel@tonic-gate 			default:
3827c478bd9Sstevel@tonic-gate 				return (VT_ERROR);
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	} else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
38686bbaf93Scg 		/*
38786bbaf93Scg 		 * No valid label here; try the alternate. Note that here
38886bbaf93Scg 		 * we just read GPT header and save it into dk_ioc.data,
38986bbaf93Scg 		 * Later, we will read GUID partition entry array if we
39086bbaf93Scg 		 * can get valid GPT header.
39186bbaf93Scg 		 */
39286bbaf93Scg 
39386bbaf93Scg 		/*
39486bbaf93Scg 		 * This is a workaround for legacy systems. In the past, the
39586bbaf93Scg 		 * last sector of SCSI disk was invisible on x86 platform. At
39686bbaf93Scg 		 * that time, backup label was saved on the next to the last
39786bbaf93Scg 		 * sector. It is possible for users to move a disk from previous
39886bbaf93Scg 		 * solaris system to present system. Here, we attempt to search
39986bbaf93Scg 		 * legacy backup EFI label first.
40086bbaf93Scg 		 */
40186bbaf93Scg 		dk_ioc.dki_lba = disk_info.dki_capacity - 2;
4027c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = disk_info.dki_lbsize;
4034f0dea16Scg 		rval = check_label(fd, &dk_ioc);
40486bbaf93Scg 		if (rval == VT_EINVAL) {
4054f0dea16Scg 			/*
40686bbaf93Scg 			 * we didn't find legacy backup EFI label, try to
40786bbaf93Scg 			 * search backup EFI label in the last block.
4084f0dea16Scg 			 */
40986bbaf93Scg 			dk_ioc.dki_lba = disk_info.dki_capacity - 1;
4104f0dea16Scg 			dk_ioc.dki_length = disk_info.dki_lbsize;
4114f0dea16Scg 			rval = check_label(fd, &dk_ioc);
41286bbaf93Scg 			if (rval == 0) {
41386bbaf93Scg 				legacy_label = B_TRUE;
41486bbaf93Scg 				if (efi_debug)
41586bbaf93Scg 					(void) fprintf(stderr,
41686bbaf93Scg 					    "efi_read: primary label corrupt; "
41786bbaf93Scg 					    "using EFI backup label located on"
41886bbaf93Scg 					    " the last block\n");
4194f0dea16Scg 			}
42086bbaf93Scg 		} else {
42186bbaf93Scg 			if ((efi_debug) && (rval == 0))
42286bbaf93Scg 				(void) fprintf(stderr, "efi_read: primary label"
42386bbaf93Scg 				    " corrupt; using legacy EFI backup label "
42486bbaf93Scg 				    " located on the next to last block\n");
4254f0dea16Scg 		}
4264f0dea16Scg 
4274f0dea16Scg 		if (rval == 0) {
4287c478bd9Sstevel@tonic-gate 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4297c478bd9Sstevel@tonic-gate 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
4307c478bd9Sstevel@tonic-gate 			vtoc->efi_nparts =
4317c478bd9Sstevel@tonic-gate 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
43286bbaf93Scg 
4337c478bd9Sstevel@tonic-gate 			/*
43486bbaf93Scg 			 * Partition tables are between backup GPT header
43586bbaf93Scg 			 * table and ParitionEntryLBA (the starting LBA of
43686bbaf93Scg 			 * the GUID partition entries array). Now that we
43786bbaf93Scg 			 * already got valid GPT header and saved it in
43886bbaf93Scg 			 * dk_ioc.dki_data, we try to get GUID partition
43986bbaf93Scg 			 * entry array here.
4407c478bd9Sstevel@tonic-gate 			 */
4417c478bd9Sstevel@tonic-gate 			dk_ioc.dki_data++;
44286bbaf93Scg 			if (legacy_label)
44386bbaf93Scg 				dk_ioc.dki_length = disk_info.dki_capacity - 1 -
444*af007057Syl 				    dk_ioc.dki_lba;
44586bbaf93Scg 			else
44686bbaf93Scg 				dk_ioc.dki_length = disk_info.dki_capacity - 2 -
447*af007057Syl 				    dk_ioc.dki_lba;
4487c478bd9Sstevel@tonic-gate 			dk_ioc.dki_length *= disk_info.dki_lbsize;
44986bbaf93Scg 			if (dk_ioc.dki_length >
45086bbaf93Scg 			    ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
4517c478bd9Sstevel@tonic-gate 				rval = VT_EINVAL;
4527c478bd9Sstevel@tonic-gate 			} else {
45386bbaf93Scg 				/*
45486bbaf93Scg 				 * read GUID partition entry array
45586bbaf93Scg 				 */
4567c478bd9Sstevel@tonic-gate 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		}
459c7e1889fSyl 	} else {
460c7e1889fSyl 		dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
461c7e1889fSyl 		dk_ioc.dki_data++;
462c7e1889fSyl 		dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
463c7e1889fSyl 		rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	if (rval < 0) {
4667c478bd9Sstevel@tonic-gate 		free(efi);
4677c478bd9Sstevel@tonic-gate 		return (rval);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
471c7e1889fSyl 	efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * Assemble this into a "dk_gpt" struct for easier
4757c478bd9Sstevel@tonic-gate 	 * digestibility by applications.
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
4787c478bd9Sstevel@tonic-gate 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4797c478bd9Sstevel@tonic-gate 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
4807c478bd9Sstevel@tonic-gate 	vtoc->efi_lbasize = disk_info.dki_lbsize;
4817c478bd9Sstevel@tonic-gate 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
4827c478bd9Sstevel@tonic-gate 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
4837c478bd9Sstevel@tonic-gate 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
484*af007057Syl 	vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
4857c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * If the array the user passed in is too small, set the length
4897c478bd9Sstevel@tonic-gate 	 * to what it needs to be and return
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	if (user_length < vtoc->efi_nparts) {
4927c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
4967c478bd9Sstevel@tonic-gate 
497*af007057Syl 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
498*af007057Syl 		    efi_parts[i].efi_gpe_PartitionTypeGUID);
499*af007057Syl 
500*af007057Syl 		for (j = 0;
501*af007057Syl 		    j < sizeof (conversion_array)
502*af007057Syl 		    / sizeof (struct uuid_to_ptag); j++) {
503*af007057Syl 
504*af007057Syl 			if (bcmp(&vtoc->efi_parts[i].p_guid,
505*af007057Syl 			    &conversion_array[j].uuid,
506*af007057Syl 			    sizeof (struct uuid)) == 0) {
507*af007057Syl 				vtoc->efi_parts[i].p_tag = j;
508*af007057Syl 				break;
509*af007057Syl 			}
510*af007057Syl 		}
511*af007057Syl 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
512*af007057Syl 			continue;
513*af007057Syl 		vtoc->efi_parts[i].p_flag =
514*af007057Syl 		    LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
515*af007057Syl 		vtoc->efi_parts[i].p_start =
516*af007057Syl 		    LE_64(efi_parts[i].efi_gpe_StartingLBA);
517*af007057Syl 		vtoc->efi_parts[i].p_size =
518*af007057Syl 		    LE_64(efi_parts[i].efi_gpe_EndingLBA) -
5197c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_start + 1;
520*af007057Syl 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
521*af007057Syl 			vtoc->efi_parts[i].p_name[j] =
522*af007057Syl 			    (uchar_t)LE_16(
523*af007057Syl 			    efi_parts[i].efi_gpe_PartitionName[j]);
524*af007057Syl 		}
5257c478bd9Sstevel@tonic-gate 
526*af007057Syl 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
527*af007057Syl 		    efi_parts[i].efi_gpe_UniquePartitionGUID);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	free(efi);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	return (dki_info.dki_partition);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /* writes a "protective" MBR */
5357c478bd9Sstevel@tonic-gate static int
5367c478bd9Sstevel@tonic-gate write_pmbr(int fd, struct dk_gpt *vtoc)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
5397c478bd9Sstevel@tonic-gate 	struct mboot	mb;
5407c478bd9Sstevel@tonic-gate 	uchar_t		*cp;
5417c478bd9Sstevel@tonic-gate 	diskaddr_t	size_in_lba;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	mb.signature = LE_16(MBB_MAGIC);
5447c478bd9Sstevel@tonic-gate 	bzero(&mb.parts, sizeof (mb.parts));
5457c478bd9Sstevel@tonic-gate 	cp = (uchar_t *)&mb.parts[0];
5467c478bd9Sstevel@tonic-gate 	/* bootable or not */
5477c478bd9Sstevel@tonic-gate 	*cp++ = 0;
5487c478bd9Sstevel@tonic-gate 	/* beginning CHS; 0xffffff if not representable */
5497c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5507c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5517c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5527c478bd9Sstevel@tonic-gate 	/* OS type */
5537c478bd9Sstevel@tonic-gate 	*cp++ = EFI_PMBR;
5547c478bd9Sstevel@tonic-gate 	/* ending CHS; 0xffffff if not representable */
5557c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5567c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5577c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5587c478bd9Sstevel@tonic-gate 	/* starting LBA: 1 (little endian format) by EFI definition */
5597c478bd9Sstevel@tonic-gate 	*cp++ = 0x01;
5607c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
5617c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
5627c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
5637c478bd9Sstevel@tonic-gate 	/* ending LBA: last block on the disk (little endian format) */
5647c478bd9Sstevel@tonic-gate 	size_in_lba = vtoc->efi_last_lba;
5657c478bd9Sstevel@tonic-gate 	if (size_in_lba < 0xffffffff) {
5667c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x000000ff);
5677c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
5687c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
5697c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0xff000000) >> 24;
5707c478bd9Sstevel@tonic-gate 	} else {
5717c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
5727c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
5737c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
5747c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
5777c478bd9Sstevel@tonic-gate 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
5787c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 0;
5797c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length = sizeof (mb);
5807c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
5817c478bd9Sstevel@tonic-gate 		switch (errno) {
5827c478bd9Sstevel@tonic-gate 		case EIO:
5837c478bd9Sstevel@tonic-gate 			return (VT_EIO);
5847c478bd9Sstevel@tonic-gate 		case EINVAL:
5857c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
5867c478bd9Sstevel@tonic-gate 		default:
5877c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	return (0);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /* make sure the user specified something reasonable */
5947c478bd9Sstevel@tonic-gate static int
5957c478bd9Sstevel@tonic-gate check_input(struct dk_gpt *vtoc)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	int			resv_part = -1;
5987c478bd9Sstevel@tonic-gate 	int			i, j;
5997c478bd9Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * Sanity-check the input (make sure no partitions overlap)
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
6057c478bd9Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
6067c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
6077c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
6087c478bd9Sstevel@tonic-gate 			if (efi_debug) {
6097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6107c478bd9Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size of %llu",
6117c478bd9Sstevel@tonic-gate 				    i,
6127c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
6137c478bd9Sstevel@tonic-gate 			}
6147c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
617f2be5148Sszhou 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
618f2be5148Sszhou 				continue;
619f2be5148Sszhou 			/* we have encountered an unknown uuid */
620f2be5148Sszhou 			vtoc->efi_parts[i].p_tag = 0xff;
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
6237c478bd9Sstevel@tonic-gate 			if (resv_part != -1) {
6247c478bd9Sstevel@tonic-gate 				if (efi_debug) {
625*af007057Syl 					(void) fprintf(stderr,
6267c478bd9Sstevel@tonic-gate "found duplicate reserved partition at %d\n",
627*af007057Syl 					    i);
6287c478bd9Sstevel@tonic-gate 				}
6297c478bd9Sstevel@tonic-gate 				return (VT_EINVAL);
6307c478bd9Sstevel@tonic-gate 			}
6317c478bd9Sstevel@tonic-gate 			resv_part = i;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
6347c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
6357c478bd9Sstevel@tonic-gate 			if (efi_debug) {
6367c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6377c478bd9Sstevel@tonic-gate 				    "Partition %d starts at %llu.  ",
6387c478bd9Sstevel@tonic-gate 				    i,
6397c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start);
6407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6417c478bd9Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
6427c478bd9Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
6437c478bd9Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
6447c478bd9Sstevel@tonic-gate 			}
6457c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
6487c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
6497c478bd9Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
6507c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
6517c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
6527c478bd9Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
6537c478bd9Sstevel@tonic-gate 			if (efi_debug) {
6547c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6557c478bd9Sstevel@tonic-gate 				    "Partition %d ends at %llu.  ",
6567c478bd9Sstevel@tonic-gate 				    i,
6577c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start +
6587c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
6597c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6607c478bd9Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
6617c478bd9Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
6627c478bd9Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
6637c478bd9Sstevel@tonic-gate 			}
6647c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
6687c478bd9Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
6697c478bd9Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
6707c478bd9Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
6717c478bd9Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
6727c478bd9Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
6737c478bd9Sstevel@tonic-gate 				endsect = jstart + jsize -1;
6747c478bd9Sstevel@tonic-gate 				if ((jstart <= istart) &&
6757c478bd9Sstevel@tonic-gate 				    (istart <= endsect)) {
6767c478bd9Sstevel@tonic-gate 					if (efi_debug) {
6777c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6787c478bd9Sstevel@tonic-gate "Partition %d overlaps partition %d.",
6797c478bd9Sstevel@tonic-gate 						    i, j);
680*af007057Syl 					}
681*af007057Syl 					return (VT_EINVAL);
6827c478bd9Sstevel@tonic-gate 				}
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 	/* just a warning for now */
6877c478bd9Sstevel@tonic-gate 	if ((resv_part == -1) && efi_debug) {
6887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
689*af007057Syl 		    "no reserved partition found\n");
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	return (0);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
694*af007057Syl /*
695*af007057Syl  * add all the unallocated space to the current label
696*af007057Syl  */
697*af007057Syl int
698*af007057Syl efi_use_whole_disk(int fd)
699*af007057Syl {
700*af007057Syl 	struct dk_gpt		*efi_label;
701*af007057Syl 	int			rval;
702*af007057Syl 	int			i;
703*af007057Syl 	uint_t			phy_last_slice = 0;
704*af007057Syl 	diskaddr_t		pl_start = 0;
705*af007057Syl 	diskaddr_t		pl_size;
706*af007057Syl 
707*af007057Syl 	rval = efi_alloc_and_read(fd, &efi_label);
708*af007057Syl 	if (rval < 0) {
709*af007057Syl 		return (rval);
710*af007057Syl 	}
711*af007057Syl 
712*af007057Syl 	/* find the last physically non-zero partition */
713*af007057Syl 	for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
714*af007057Syl 		if (pl_start < efi_label->efi_parts[i].p_start) {
715*af007057Syl 			pl_start = efi_label->efi_parts[i].p_start;
716*af007057Syl 			phy_last_slice = i;
717*af007057Syl 		}
718*af007057Syl 	}
719*af007057Syl 	pl_size = efi_label->efi_parts[phy_last_slice].p_size;
720*af007057Syl 
721*af007057Syl 	/*
722*af007057Syl 	 * If alter_lba is 1, we are using the backup label.
723*af007057Syl 	 * Since we can locate the backup label by disk capacity,
724*af007057Syl 	 * there must be no unallocated space.
725*af007057Syl 	 */
726*af007057Syl 	if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
727*af007057Syl 	    >= efi_label->efi_last_lba)) {
728*af007057Syl 		if (efi_debug) {
729*af007057Syl 			(void) fprintf(stderr,
730*af007057Syl 			    "efi_use_whole_disk: requested space not found\n");
731*af007057Syl 		}
732*af007057Syl 		efi_free(efi_label);
733*af007057Syl 		return (VT_ENOSPC);
734*af007057Syl 	}
735*af007057Syl 
736*af007057Syl 	/*
737*af007057Syl 	 * If there is space between the last physically non-zero partition
738*af007057Syl 	 * and the reserved partition, just add the unallocated space to this
739*af007057Syl 	 * area. Otherwise, the unallocated space is added to the last
740*af007057Syl 	 * physically non-zero partition.
741*af007057Syl 	 */
742*af007057Syl 	if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
743*af007057Syl 	    EFI_MIN_RESV_SIZE) {
744*af007057Syl 		efi_label->efi_parts[phy_last_slice].p_size +=
745*af007057Syl 		    efi_label->efi_last_lba - efi_label->efi_altern_lba;
746*af007057Syl 	}
747*af007057Syl 
748*af007057Syl 	/*
749*af007057Syl 	 * Move the reserved partition. There is currently no data in
750*af007057Syl 	 * here except fabricated devids (which get generated via
751*af007057Syl 	 * efi_write()). So there is no need to copy data.
752*af007057Syl 	 */
753*af007057Syl 	efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
754*af007057Syl 	    efi_label->efi_last_lba - efi_label->efi_altern_lba;
755*af007057Syl 	efi_label->efi_last_u_lba += efi_label->efi_last_lba
756*af007057Syl 	    - efi_label->efi_altern_lba;
757*af007057Syl 
758*af007057Syl 	rval = efi_write(fd, efi_label);
759*af007057Syl 	if (rval < 0) {
760*af007057Syl 		if (efi_debug) {
761*af007057Syl 			(void) fprintf(stderr,
762*af007057Syl 			    "efi_use_whole_disk:fail to write label, rval=%d\n",
763*af007057Syl 			    rval);
764*af007057Syl 		}
765*af007057Syl 		efi_free(efi_label);
766*af007057Syl 		return (rval);
767*af007057Syl 	}
768*af007057Syl 
769*af007057Syl 	efi_free(efi_label);
770*af007057Syl 	return (0);
771*af007057Syl }
772*af007057Syl 
773*af007057Syl 
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate  * write EFI label and backup label
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate int
7787c478bd9Sstevel@tonic-gate efi_write(int fd, struct dk_gpt *vtoc)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
7817c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
7827c478bd9Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
7837c478bd9Sstevel@tonic-gate 	int			i, j;
7847c478bd9Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
7857c478bd9Sstevel@tonic-gate 	int			md_flag = 0;
78686bbaf93Scg 	int			nblocks;
78786bbaf93Scg 	diskaddr_t		lba_backup_gpt_hdr;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
7907c478bd9Sstevel@tonic-gate 		if (efi_debug)
7917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
7927c478bd9Sstevel@tonic-gate 		switch (errno) {
7937c478bd9Sstevel@tonic-gate 		case EIO:
7947c478bd9Sstevel@tonic-gate 			return (VT_EIO);
7957c478bd9Sstevel@tonic-gate 		case EINVAL:
7967c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
7977c478bd9Sstevel@tonic-gate 		default:
7987c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
7997c478bd9Sstevel@tonic-gate 		}
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/* check if we are dealing wih a metadevice */
8037c478bd9Sstevel@tonic-gate 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
8047c478bd9Sstevel@tonic-gate 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
8057c478bd9Sstevel@tonic-gate 		md_flag = 1;
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (check_input(vtoc)) {
8097c478bd9Sstevel@tonic-gate 		/*
8107c478bd9Sstevel@tonic-gate 		 * not valid; if it's a metadevice just pass it down
8117c478bd9Sstevel@tonic-gate 		 * because SVM will do its own checking
8127c478bd9Sstevel@tonic-gate 		 */
8137c478bd9Sstevel@tonic-gate 		if (md_flag == 0) {
8147c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
8197c478bd9Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
8207c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
8217c478bd9Sstevel@tonic-gate 	} else {
8227c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
823*af007057Syl 		    vtoc->efi_lbasize) *
824*af007057Syl 		    vtoc->efi_lbasize;
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
82786bbaf93Scg 	/*
82886bbaf93Scg 	 * the number of blocks occupied by GUID partition entry array
82986bbaf93Scg 	 */
83086bbaf93Scg 	nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
83186bbaf93Scg 
83286bbaf93Scg 	/*
83386bbaf93Scg 	 * Backup GPT header is located on the block after GUID
83486bbaf93Scg 	 * partition entry array. Here, we calculate the address
83586bbaf93Scg 	 * for backup GPT header.
83686bbaf93Scg 	 */
83786bbaf93Scg 	lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
83886bbaf93Scg 
8397c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
8407c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/* stuff user's input into EFI struct */
8457c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
8467c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
8477c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
8487c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Reserved1 = 0;
8497c478bd9Sstevel@tonic-gate 	efi->efi_gpt_MyLBA = LE_64(1ULL);
85086bbaf93Scg 	efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
8517c478bd9Sstevel@tonic-gate 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
8527c478bd9Sstevel@tonic-gate 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
8537c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
8547c478bd9Sstevel@tonic-gate 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
8557c478bd9Sstevel@tonic-gate 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
8567c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
8597c478bd9Sstevel@tonic-gate 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + sizeof (efi_gpt_t));
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
862*af007057Syl 		for (j = 0;
863*af007057Syl 		    j < sizeof (conversion_array) /
864*af007057Syl 		    sizeof (struct uuid_to_ptag); j++) {
865*af007057Syl 
866*af007057Syl 			if (vtoc->efi_parts[i].p_tag == j) {
867*af007057Syl 				UUID_LE_CONVERT(
868*af007057Syl 				    efi_parts[i].efi_gpe_PartitionTypeGUID,
869*af007057Syl 				    conversion_array[j].uuid);
870*af007057Syl 				break;
871*af007057Syl 			}
872*af007057Syl 		}
873*af007057Syl 
874*af007057Syl 		if (j == sizeof (conversion_array) /
875*af007057Syl 		    sizeof (struct uuid_to_ptag)) {
876*af007057Syl 			/*
877*af007057Syl 			 * If we didn't have a matching uuid match, bail here.
878*af007057Syl 			 * Don't write a label with unknown uuid.
879*af007057Syl 			 */
880*af007057Syl 			if (efi_debug) {
881*af007057Syl 				(void) fprintf(stderr,
882*af007057Syl 				    "Unknown uuid for p_tag %d\n",
883*af007057Syl 				    vtoc->efi_parts[i].p_tag);
884*af007057Syl 			}
885*af007057Syl 			return (VT_EINVAL);
886*af007057Syl 		}
887*af007057Syl 
888*af007057Syl 		efi_parts[i].efi_gpe_StartingLBA =
889*af007057Syl 		    LE_64(vtoc->efi_parts[i].p_start);
890*af007057Syl 		efi_parts[i].efi_gpe_EndingLBA =
891*af007057Syl 		    LE_64(vtoc->efi_parts[i].p_start +
892*af007057Syl 		    vtoc->efi_parts[i].p_size - 1);
893*af007057Syl 		efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
8947c478bd9Sstevel@tonic-gate 		    LE_16(vtoc->efi_parts[i].p_flag);
895*af007057Syl 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
896*af007057Syl 			efi_parts[i].efi_gpe_PartitionName[j] =
897*af007057Syl 			    LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
898*af007057Syl 		}
899*af007057Syl 		if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
900*af007057Syl 		    uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
901*af007057Syl 			(void) uuid_generate((uchar_t *)
902*af007057Syl 			    &vtoc->efi_parts[i].p_uguid);
903*af007057Syl 		}
904*af007057Syl 		bcopy(&vtoc->efi_parts[i].p_uguid,
905*af007057Syl 		    &efi_parts[i].efi_gpe_UniquePartitionGUID,
906*af007057Syl 		    sizeof (uuid_t));
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryArrayCRC32 =
9097c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi_parts,
9107c478bd9Sstevel@tonic-gate 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
9117c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
9127c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9157c478bd9Sstevel@tonic-gate 		free(dk_ioc.dki_data);
9167c478bd9Sstevel@tonic-gate 		switch (errno) {
9177c478bd9Sstevel@tonic-gate 		case EIO:
9187c478bd9Sstevel@tonic-gate 			return (VT_EIO);
9197c478bd9Sstevel@tonic-gate 		case EINVAL:
9207c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
9217c478bd9Sstevel@tonic-gate 		default:
9227c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	/* if it's a metadevice we're done */
9267c478bd9Sstevel@tonic-gate 	if (md_flag) {
9277c478bd9Sstevel@tonic-gate 		free(dk_ioc.dki_data);
9287c478bd9Sstevel@tonic-gate 		return (0);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 	/* write backup partition array */
9317c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
9327c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length -= vtoc->efi_lbasize;
9337c478bd9Sstevel@tonic-gate 	dk_ioc.dki_data++;
9347c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9357c478bd9Sstevel@tonic-gate 		/*
9367c478bd9Sstevel@tonic-gate 		 * we wrote the primary label okay, so don't fail
9377c478bd9Sstevel@tonic-gate 		 */
9387c478bd9Sstevel@tonic-gate 		if (efi_debug) {
9397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9407c478bd9Sstevel@tonic-gate 			    "write of backup partitions to block %llu "
9417c478bd9Sstevel@tonic-gate 			    "failed, errno %d\n",
9427c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba + 1,
9437c478bd9Sstevel@tonic-gate 			    errno);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * now swap MyLBA and AlternateLBA fields and write backup
9487c478bd9Sstevel@tonic-gate 	 * partition table header
9497c478bd9Sstevel@tonic-gate 	 */
95086bbaf93Scg 	dk_ioc.dki_lba = lba_backup_gpt_hdr;
9517c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length = vtoc->efi_lbasize;
9527c478bd9Sstevel@tonic-gate 	dk_ioc.dki_data--;
9537c478bd9Sstevel@tonic-gate 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
95486bbaf93Scg 	efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
9557c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
9567c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
9577c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
9587c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
9597c478bd9Sstevel@tonic-gate 	    sizeof (struct efi_gpt)));
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9627c478bd9Sstevel@tonic-gate 		if (efi_debug) {
9637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9647c478bd9Sstevel@tonic-gate 			    "write of backup header to block %llu failed, "
9657c478bd9Sstevel@tonic-gate 			    "errno %d\n",
96686bbaf93Scg 			    lba_backup_gpt_hdr,
9677c478bd9Sstevel@tonic-gate 			    errno);
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	/* write the PMBR */
9717c478bd9Sstevel@tonic-gate 	(void) write_pmbr(fd, vtoc);
9727c478bd9Sstevel@tonic-gate 	free(dk_ioc.dki_data);
9737c478bd9Sstevel@tonic-gate 	return (0);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate void
9777c478bd9Sstevel@tonic-gate efi_free(struct dk_gpt *ptr)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	free(ptr);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate  * Input: File descriptor
9847c478bd9Sstevel@tonic-gate  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate int
9877c478bd9Sstevel@tonic-gate efi_type(int fd)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	struct vtoc vtoc;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
9927c478bd9Sstevel@tonic-gate 		if (errno == ENOTSUP) {
9937c478bd9Sstevel@tonic-gate 			return (1);
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	return (0);
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate void
10007c478bd9Sstevel@tonic-gate efi_err_check(struct dk_gpt *vtoc)
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	int			resv_part = -1;
10037c478bd9Sstevel@tonic-gate 	int			i, j;
10047c478bd9Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
10057c478bd9Sstevel@tonic-gate 	int			overlap = 0;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	/*
10087c478bd9Sstevel@tonic-gate 	 * make sure no partitions overlap
10097c478bd9Sstevel@tonic-gate 	 */
10107c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
10117c478bd9Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
10127c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
10137c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
10147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10157c478bd9Sstevel@tonic-gate 			    "partition %d is \"unassigned\" but has a size "
10167c478bd9Sstevel@tonic-gate 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
10197c478bd9Sstevel@tonic-gate 			continue;
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
10227c478bd9Sstevel@tonic-gate 			if (resv_part != -1) {
10237c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10247c478bd9Sstevel@tonic-gate 				    "found duplicate reserved partition at "
10257c478bd9Sstevel@tonic-gate 				    "%d\n", i);
10267c478bd9Sstevel@tonic-gate 			}
10277c478bd9Sstevel@tonic-gate 			resv_part = i;
10287c478bd9Sstevel@tonic-gate 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
10297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10307c478bd9Sstevel@tonic-gate 				    "Warning: reserved partition size must "
10317c478bd9Sstevel@tonic-gate 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
10347c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
10357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10367c478bd9Sstevel@tonic-gate 			    "Partition %d starts at %llu\n",
10377c478bd9Sstevel@tonic-gate 			    i,
10387c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start);
10397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10407c478bd9Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
10417c478bd9Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
10427c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
10457c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
10467c478bd9Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
10477c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
10487c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
10497c478bd9Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
10507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10517c478bd9Sstevel@tonic-gate 			    "Partition %d ends at %llu\n",
10527c478bd9Sstevel@tonic-gate 			    i,
10537c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start +
10547c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_size);
10557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10567c478bd9Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
10577c478bd9Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
10587c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
10627c478bd9Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
10637c478bd9Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
10647c478bd9Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
10657c478bd9Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
10667c478bd9Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
10677c478bd9Sstevel@tonic-gate 				endsect = jstart + jsize -1;
10687c478bd9Sstevel@tonic-gate 				if ((jstart <= istart) &&
10697c478bd9Sstevel@tonic-gate 				    (istart <= endsect)) {
10707c478bd9Sstevel@tonic-gate 					if (!overlap) {
10717c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
10727c478bd9Sstevel@tonic-gate 					    "label error: EFI Labels do not "
10737c478bd9Sstevel@tonic-gate 					    "support overlapping partitions\n");
10747c478bd9Sstevel@tonic-gate 					}
10757c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
10767c478bd9Sstevel@tonic-gate 					    "Partition %d overlaps partition "
10777c478bd9Sstevel@tonic-gate 					    "%d.\n", i, j);
10787c478bd9Sstevel@tonic-gate 					overlap = 1;
10797c478bd9Sstevel@tonic-gate 				}
10807c478bd9Sstevel@tonic-gate 			}
10817c478bd9Sstevel@tonic-gate 		}
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 	/* make sure there is a reserved partition */
10847c478bd9Sstevel@tonic-gate 	if (resv_part == -1) {
10857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1086*af007057Syl 		    "no reserved partition found\n");
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate /*
10917c478bd9Sstevel@tonic-gate  * We need to get information necessary to construct a *new* efi
10927c478bd9Sstevel@tonic-gate  * label type
10937c478bd9Sstevel@tonic-gate  */
10947c478bd9Sstevel@tonic-gate int
10957c478bd9Sstevel@tonic-gate efi_auto_sense(int fd, struct dk_gpt **vtoc)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	int	i;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	/*
11017c478bd9Sstevel@tonic-gate 	 * Now build the default partition table
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
11047c478bd9Sstevel@tonic-gate 		if (efi_debug) {
11057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 		return (-1);
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
11117c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
11127c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
11137c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_start = 0;
11147c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_size = 0;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	/*
11177c478bd9Sstevel@tonic-gate 	 * Make constants first
11187c478bd9Sstevel@tonic-gate 	 * and variable partitions later
11197c478bd9Sstevel@tonic-gate 	 */
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/* root partition - s0 128 MB */
11227c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_start = 34;
11237c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_size = 262144;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/* partition - s1  128 MB */
11267c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_start = 262178;
11277c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_size = 262144;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/* partition -s2 is NOT the Backup disk */
11307c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	/* partition -s6 /usr partition - HOG */
11337c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_start = 524322;
11347c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
11357c478bd9Sstevel@tonic-gate 	    - (1024 * 16);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/* efi reserved partition - s9 16K */
11387c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
11397c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
11407c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
11417c478bd9Sstevel@tonic-gate 	return (0);
11427c478bd9Sstevel@tonic-gate }
1143