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  */
21342440ecSPrasad Singamsetty 
227c478bd9Sstevel@tonic-gate /*
2380c797c0SSharath M Srinivasan  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24bd93c05dSAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25e088753cSToomas Soome  * Copyright 2014 Toomas Soome <tsoome@me.com>
267c478bd9Sstevel@tonic-gate  */
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 },
58bd93c05dSAlexander Eremin 	{ 0 },			/* CACHE is never used */
597c478bd9Sstevel@tonic-gate 	{ EFI_RESERVED },
607c478bd9Sstevel@tonic-gate 	{ EFI_SYSTEM },
617c478bd9Sstevel@tonic-gate 	{ EFI_LEGACY_MBR },
6280c797c0SSharath M Srinivasan 	{ EFI_SYMC_PUB },
6380c797c0SSharath M Srinivasan 	{ EFI_SYMC_CDS },
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 },
71e21ea675SYuri Pankov 	{ EFI_AAPL_UFS },
72e088753cSToomas Soome 	{ EFI_BIOS_BOOT },
73e21ea675SYuri Pankov 	{ EFI_FREEBSD_BOOT },
74e21ea675SYuri Pankov 	{ EFI_FREEBSD_SWAP },
75e21ea675SYuri Pankov 	{ EFI_FREEBSD_UFS },
76e21ea675SYuri Pankov 	{ EFI_FREEBSD_VINUM },
77e21ea675SYuri Pankov 	{ EFI_FREEBSD_ZFS }
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Default vtoc information for non-SVr4 partitions
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate struct dk_map2  default_vtoc_map[NDKMAP] = {
847c478bd9Sstevel@tonic-gate 	{	V_ROOT,		0	},		/* a - 0 */
857c478bd9Sstevel@tonic-gate 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
867c478bd9Sstevel@tonic-gate 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
877c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* d - 3 */
887c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* e - 4 */
897c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* f - 5 */
907c478bd9Sstevel@tonic-gate 	{	V_USR,		0	},		/* g - 6 */
917c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* h - 7 */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
967c478bd9Sstevel@tonic-gate 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
977c478bd9Sstevel@tonic-gate 	{	V_ALTSCTR,	0	},		/* j - 9 */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #else
1007c478bd9Sstevel@tonic-gate #error No VTOC format defined.
1017c478bd9Sstevel@tonic-gate #endif			/* defined(i386) */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* k - 10 */
1047c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* l - 11 */
1057c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* m - 12 */
1067c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* n - 13 */
1077c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* o - 14 */
1087c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* p - 15 */
1097c478bd9Sstevel@tonic-gate #endif			/* defined(_SUNOS_VTOC_16) */
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #ifdef DEBUG
1137c478bd9Sstevel@tonic-gate int efi_debug = 1;
1147c478bd9Sstevel@tonic-gate #else
1157c478bd9Sstevel@tonic-gate int efi_debug = 0;
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate extern unsigned int	efi_crc32(const unsigned char *, unsigned int);
1197c478bd9Sstevel@tonic-gate static int		efi_read(int, struct dk_gpt *);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static int
1227c478bd9Sstevel@tonic-gate read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	struct dk_minfo		disk_info;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
1277c478bd9Sstevel@tonic-gate 		return (errno);
1287c478bd9Sstevel@tonic-gate 	*capacity = disk_info.dki_capacity;
1297c478bd9Sstevel@tonic-gate 	*lbsize = disk_info.dki_lbsize;
1307c478bd9Sstevel@tonic-gate 	return (0);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * the number of blocks the EFI label takes up (round up to nearest
1357c478bd9Sstevel@tonic-gate  * block)
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate #define	NBLOCKS(p, l)	(1 + ((((p) * (int)sizeof (efi_gpe_t))  + \
1387c478bd9Sstevel@tonic-gate 				((l) - 1)) / (l)))
1397c478bd9Sstevel@tonic-gate /* number of partitions -- limited by what we can malloc */
1407c478bd9Sstevel@tonic-gate #define	MAX_PARTS	((4294967295UL - sizeof (struct dk_gpt)) / \
1417c478bd9Sstevel@tonic-gate 			    sizeof (struct dk_part))
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
1447c478bd9Sstevel@tonic-gate efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	diskaddr_t	capacity;
1477c478bd9Sstevel@tonic-gate 	uint_t		lbsize;
1487c478bd9Sstevel@tonic-gate 	uint_t		nblocks;
1497c478bd9Sstevel@tonic-gate 	size_t		length;
1507c478bd9Sstevel@tonic-gate 	struct dk_gpt	*vptr;
1517c478bd9Sstevel@tonic-gate 	struct uuid	uuid;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (read_disk_info(fd, &capacity, &lbsize) != 0) {
1547c478bd9Sstevel@tonic-gate 		if (efi_debug)
1557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1567c478bd9Sstevel@tonic-gate 			    "couldn't read disk information\n");
1577c478bd9Sstevel@tonic-gate 		return (-1);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	nblocks = NBLOCKS(nparts, lbsize);
1617c478bd9Sstevel@tonic-gate 	if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
1627c478bd9Sstevel@tonic-gate 		/* 16K plus one block for the GPT */
1637c478bd9Sstevel@tonic-gate 		nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (nparts > MAX_PARTS) {
1677c478bd9Sstevel@tonic-gate 		if (efi_debug) {
1687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1697c478bd9Sstevel@tonic-gate 			"the maximum number of partitions supported is %lu\n",
1707c478bd9Sstevel@tonic-gate 			    MAX_PARTS);
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		return (-1);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	length = sizeof (struct dk_gpt) +
1767c478bd9Sstevel@tonic-gate 	    sizeof (struct dk_part) * (nparts - 1);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
1797c478bd9Sstevel@tonic-gate 		return (-1);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	vptr = *vtoc;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	vptr->efi_version = EFI_VERSION_CURRENT;
1847c478bd9Sstevel@tonic-gate 	vptr->efi_lbasize = lbsize;
1857c478bd9Sstevel@tonic-gate 	vptr->efi_nparts = nparts;
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * add one block here for the PMBR; on disks with a 512 byte
1887c478bd9Sstevel@tonic-gate 	 * block size and 128 or fewer partitions, efi_first_u_lba
1897c478bd9Sstevel@tonic-gate 	 * should work out to "34"
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	vptr->efi_first_u_lba = nblocks + 1;
1927c478bd9Sstevel@tonic-gate 	vptr->efi_last_lba = capacity - 1;
193af007057Syl 	vptr->efi_altern_lba = capacity -1;
1947c478bd9Sstevel@tonic-gate 	vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
19565908c77Syu, larry liu - Sun Microsystems - Beijing China 
1967c478bd9Sstevel@tonic-gate 	(void) uuid_generate((uchar_t *)&uuid);
1977c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
1987c478bd9Sstevel@tonic-gate 	return (0);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Read EFI - return partition number upon success.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate int
2057c478bd9Sstevel@tonic-gate efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	int			rval;
2087c478bd9Sstevel@tonic-gate 	uint32_t		nparts;
2097c478bd9Sstevel@tonic-gate 	int			length;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* figure out the number of entries that would fit into 16K */
2127c478bd9Sstevel@tonic-gate 	nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
2137c478bd9Sstevel@tonic-gate 	length = (int) sizeof (struct dk_gpt) +
214af007057Syl 	    (int) sizeof (struct dk_part) * (nparts - 1);
2157c478bd9Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
2167c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_nparts = nparts;
2197c478bd9Sstevel@tonic-gate 	rval = efi_read(fd, *vtoc);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
2227c478bd9Sstevel@tonic-gate 		void *tmp;
2237c478bd9Sstevel@tonic-gate 		length = (int) sizeof (struct dk_gpt) +
224af007057Syl 		    (int) sizeof (struct dk_part) *
225af007057Syl 		    ((*vtoc)->efi_nparts - 1);
2267c478bd9Sstevel@tonic-gate 		nparts = (*vtoc)->efi_nparts;
2277c478bd9Sstevel@tonic-gate 		if ((tmp = realloc(*vtoc, length)) == NULL) {
2287c478bd9Sstevel@tonic-gate 			free (*vtoc);
2297c478bd9Sstevel@tonic-gate 			*vtoc = NULL;
2307c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
2317c478bd9Sstevel@tonic-gate 		} else {
2327c478bd9Sstevel@tonic-gate 			*vtoc = tmp;
2337c478bd9Sstevel@tonic-gate 			rval = efi_read(fd, *vtoc);
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (rval < 0) {
2387c478bd9Sstevel@tonic-gate 		if (efi_debug) {
2397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2407c478bd9Sstevel@tonic-gate 			    "read of EFI table failed, rval=%d\n", rval);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		free (*vtoc);
2437c478bd9Sstevel@tonic-gate 		*vtoc = NULL;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (rval);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int
2507c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
2537c478bd9Sstevel@tonic-gate 	int error;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
2567c478bd9Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
2577c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data = data;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	return (error);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static int
2637c478bd9Sstevel@tonic-gate check_label(int fd, dk_efi_t *dk_ioc)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
2667c478bd9Sstevel@tonic-gate 	uint_t			crc;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
2697c478bd9Sstevel@tonic-gate 		switch (errno) {
2707c478bd9Sstevel@tonic-gate 		case EIO:
2717c478bd9Sstevel@tonic-gate 			return (VT_EIO);
2727c478bd9Sstevel@tonic-gate 		default:
2737c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	efi = dk_ioc->dki_data;
2777c478bd9Sstevel@tonic-gate 	if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
2787c478bd9Sstevel@tonic-gate 		if (efi_debug)
2797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2807c478bd9Sstevel@tonic-gate 			    "Bad EFI signature: 0x%llx != 0x%llx\n",
2817c478bd9Sstevel@tonic-gate 			    (long long)efi->efi_gpt_Signature,
2827c478bd9Sstevel@tonic-gate 			    (long long)LE_64(EFI_SIGNATURE));
2837c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * check CRC of the header; the size of the header should
2887c478bd9Sstevel@tonic-gate 	 * never be larger than one block
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	crc = efi->efi_gpt_HeaderCRC32;
2917c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
2947c478bd9Sstevel@tonic-gate 	    crc != LE_32(efi_crc32((unsigned char *)efi,
2957c478bd9Sstevel@tonic-gate 	    LE_32(efi->efi_gpt_HeaderSize)))) {
2967c478bd9Sstevel@tonic-gate 		if (efi_debug)
2977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
298af007057Syl 			    "Bad EFI CRC: 0x%x != 0x%x\n",
299af007057Syl 			    crc,
300af007057Syl 			    LE_32(efi_crc32((unsigned char *)efi,
301af007057Syl 			    sizeof (struct efi_gpt))));
3027c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (0);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static int
3097c478bd9Sstevel@tonic-gate efi_read(int fd, struct dk_gpt *vtoc)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	int			i, j;
3127c478bd9Sstevel@tonic-gate 	int			label_len;
3137c478bd9Sstevel@tonic-gate 	int			rval = 0;
314d84f0041SAlexandre Chartre 	int			vdc_flag = 0;
3157c478bd9Sstevel@tonic-gate 	struct dk_minfo		disk_info;
3167c478bd9Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
3177c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
3187c478bd9Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
3197c478bd9Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
3207c478bd9Sstevel@tonic-gate 	uint32_t		user_length;
32186bbaf93Scg 	boolean_t		legacy_label = B_FALSE;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * get the partition number for this file descriptor.
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
327af007057Syl 		if (efi_debug) {
328af007057Syl 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
329af007057Syl 		}
3307c478bd9Sstevel@tonic-gate 		switch (errno) {
3317c478bd9Sstevel@tonic-gate 		case EIO:
3327c478bd9Sstevel@tonic-gate 			return (VT_EIO);
3337c478bd9Sstevel@tonic-gate 		case EINVAL:
3347c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
3357c478bd9Sstevel@tonic-gate 		default:
3367c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	}
339*5f10ef69SYuri Pankov 
340*5f10ef69SYuri Pankov 	if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
341d84f0041SAlexandre Chartre 	    (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
342d84f0041SAlexandre Chartre 		/*
343d84f0041SAlexandre Chartre 		 * The controller and drive name "vdc" (virtual disk client)
344d84f0041SAlexandre Chartre 		 * indicates a LDoms virtual disk.
345d84f0041SAlexandre Chartre 		 */
346d84f0041SAlexandre Chartre 		vdc_flag++;
3477c478bd9Sstevel@tonic-gate 	}
348d84f0041SAlexandre Chartre 
3497c478bd9Sstevel@tonic-gate 	/* get the LBA size */
3507c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
3517c478bd9Sstevel@tonic-gate 		if (efi_debug) {
3527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3537c478bd9Sstevel@tonic-gate 			    "assuming LBA 512 bytes %d\n",
3547c478bd9Sstevel@tonic-gate 			    errno);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	if (disk_info.dki_lbsize == 0) {
3597c478bd9Sstevel@tonic-gate 		if (efi_debug) {
3607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3617c478bd9Sstevel@tonic-gate 			    "efi_read: assuming LBA 512 bytes\n");
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * Read the EFI GPT to figure out how many partitions we need
3677c478bd9Sstevel@tonic-gate 	 * to deal with.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
3707c478bd9Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
3717c478bd9Sstevel@tonic-gate 		label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
3727c478bd9Sstevel@tonic-gate 	} else {
3737c478bd9Sstevel@tonic-gate 		label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
374af007057Syl 		    disk_info.dki_lbsize;
3757c478bd9Sstevel@tonic-gate 		if (label_len % disk_info.dki_lbsize) {
3767c478bd9Sstevel@tonic-gate 			/* pad to physical sector size */
3777c478bd9Sstevel@tonic-gate 			label_len += disk_info.dki_lbsize;
3787c478bd9Sstevel@tonic-gate 			label_len &= ~(disk_info.dki_lbsize - 1);
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
3837c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
3847c478bd9Sstevel@tonic-gate 
385c7e1889fSyl 	dk_ioc.dki_length = disk_info.dki_lbsize;
3867c478bd9Sstevel@tonic-gate 	user_length = vtoc->efi_nparts;
3877c478bd9Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
388*5f10ef69SYuri Pankov 	if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
38986bbaf93Scg 		/*
39086bbaf93Scg 		 * No valid label here; try the alternate. Note that here
39186bbaf93Scg 		 * we just read GPT header and save it into dk_ioc.data,
39286bbaf93Scg 		 * Later, we will read GUID partition entry array if we
39386bbaf93Scg 		 * can get valid GPT header.
39486bbaf93Scg 		 */
39586bbaf93Scg 
39686bbaf93Scg 		/*
39786bbaf93Scg 		 * This is a workaround for legacy systems. In the past, the
39886bbaf93Scg 		 * last sector of SCSI disk was invisible on x86 platform. At
39986bbaf93Scg 		 * that time, backup label was saved on the next to the last
40086bbaf93Scg 		 * sector. It is possible for users to move a disk from previous
40186bbaf93Scg 		 * solaris system to present system. Here, we attempt to search
40286bbaf93Scg 		 * legacy backup EFI label first.
40386bbaf93Scg 		 */
40486bbaf93Scg 		dk_ioc.dki_lba = disk_info.dki_capacity - 2;
4057c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = disk_info.dki_lbsize;
4064f0dea16Scg 		rval = check_label(fd, &dk_ioc);
40786bbaf93Scg 		if (rval == VT_EINVAL) {
4084f0dea16Scg 			/*
40986bbaf93Scg 			 * we didn't find legacy backup EFI label, try to
41086bbaf93Scg 			 * search backup EFI label in the last block.
4114f0dea16Scg 			 */
41286bbaf93Scg 			dk_ioc.dki_lba = disk_info.dki_capacity - 1;
4134f0dea16Scg 			dk_ioc.dki_length = disk_info.dki_lbsize;
4144f0dea16Scg 			rval = check_label(fd, &dk_ioc);
41586bbaf93Scg 			if (rval == 0) {
41686bbaf93Scg 				legacy_label = B_TRUE;
41786bbaf93Scg 				if (efi_debug)
41886bbaf93Scg 					(void) fprintf(stderr,
41986bbaf93Scg 					    "efi_read: primary label corrupt; "
42086bbaf93Scg 					    "using EFI backup label located on"
42186bbaf93Scg 					    " the last block\n");
4224f0dea16Scg 			}
42386bbaf93Scg 		} else {
42486bbaf93Scg 			if ((efi_debug) && (rval == 0))
42586bbaf93Scg 				(void) fprintf(stderr, "efi_read: primary label"
42686bbaf93Scg 				    " corrupt; using legacy EFI backup label "
42786bbaf93Scg 				    " located on the next to last block\n");
4284f0dea16Scg 		}
4294f0dea16Scg 
4304f0dea16Scg 		if (rval == 0) {
4317c478bd9Sstevel@tonic-gate 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4327c478bd9Sstevel@tonic-gate 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
4337c478bd9Sstevel@tonic-gate 			vtoc->efi_nparts =
4347c478bd9Sstevel@tonic-gate 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4357c478bd9Sstevel@tonic-gate 			/*
43686bbaf93Scg 			 * Partition tables are between backup GPT header
43786bbaf93Scg 			 * table and ParitionEntryLBA (the starting LBA of
43886bbaf93Scg 			 * the GUID partition entries array). Now that we
43986bbaf93Scg 			 * already got valid GPT header and saved it in
44086bbaf93Scg 			 * dk_ioc.dki_data, we try to get GUID partition
44186bbaf93Scg 			 * entry array here.
4427c478bd9Sstevel@tonic-gate 			 */
44365908c77Syu, larry liu - Sun Microsystems - Beijing China 			/* LINTED */
44465908c77Syu, larry liu - Sun Microsystems - Beijing China 			dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
44565908c77Syu, larry liu - Sun Microsystems - Beijing China 			    + disk_info.dki_lbsize);
44686bbaf93Scg 			if (legacy_label)
44786bbaf93Scg 				dk_ioc.dki_length = disk_info.dki_capacity - 1 -
448af007057Syl 				    dk_ioc.dki_lba;
44986bbaf93Scg 			else
45086bbaf93Scg 				dk_ioc.dki_length = disk_info.dki_capacity - 2 -
451af007057Syl 				    dk_ioc.dki_lba;
4527c478bd9Sstevel@tonic-gate 			dk_ioc.dki_length *= disk_info.dki_lbsize;
45386bbaf93Scg 			if (dk_ioc.dki_length >
45486bbaf93Scg 			    ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
4557c478bd9Sstevel@tonic-gate 				rval = VT_EINVAL;
4567c478bd9Sstevel@tonic-gate 			} else {
45786bbaf93Scg 				/*
45886bbaf93Scg 				 * read GUID partition entry array
45986bbaf93Scg 				 */
4607c478bd9Sstevel@tonic-gate 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 		}
463d84f0041SAlexandre Chartre 
464d84f0041SAlexandre Chartre 	} else if (rval == 0) {
465d84f0041SAlexandre Chartre 
466c7e1889fSyl 		dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
46765908c77Syu, larry liu - Sun Microsystems - Beijing China 		/* LINTED */
46865908c77Syu, larry liu - Sun Microsystems - Beijing China 		dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
46965908c77Syu, larry liu - Sun Microsystems - Beijing China 		    + disk_info.dki_lbsize);
470c7e1889fSyl 		dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
471c7e1889fSyl 		rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
472d84f0041SAlexandre Chartre 
473d84f0041SAlexandre Chartre 	} else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
474d84f0041SAlexandre Chartre 		/*
475d84f0041SAlexandre Chartre 		 * When the device is a LDoms virtual disk, the DKIOCGETEFI
476d84f0041SAlexandre Chartre 		 * ioctl can fail with EINVAL if the virtual disk backend
477d84f0041SAlexandre Chartre 		 * is a ZFS volume serviced by a domain running an old version
478d84f0041SAlexandre Chartre 		 * of Solaris. This is because the DKIOCGETEFI ioctl was
479d84f0041SAlexandre Chartre 		 * initially incorrectly implemented for a ZFS volume and it
480d84f0041SAlexandre Chartre 		 * expected the GPT and GPE to be retrieved with a single ioctl.
481d84f0041SAlexandre Chartre 		 * So we try to read the GPT and the GPE using that old style
482d84f0041SAlexandre Chartre 		 * ioctl.
483d84f0041SAlexandre Chartre 		 */
484d84f0041SAlexandre Chartre 		dk_ioc.dki_lba = 1;
485d84f0041SAlexandre Chartre 		dk_ioc.dki_length = label_len;
486d84f0041SAlexandre Chartre 		rval = check_label(fd, &dk_ioc);
4877c478bd9Sstevel@tonic-gate 	}
488d84f0041SAlexandre Chartre 
4897c478bd9Sstevel@tonic-gate 	if (rval < 0) {
4907c478bd9Sstevel@tonic-gate 		free(efi);
4917c478bd9Sstevel@tonic-gate 		return (rval);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
495c7e1889fSyl 	efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * Assemble this into a "dk_gpt" struct for easier
4997c478bd9Sstevel@tonic-gate 	 * digestibility by applications.
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
5027c478bd9Sstevel@tonic-gate 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
5037c478bd9Sstevel@tonic-gate 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
5047c478bd9Sstevel@tonic-gate 	vtoc->efi_lbasize = disk_info.dki_lbsize;
5057c478bd9Sstevel@tonic-gate 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
5067c478bd9Sstevel@tonic-gate 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
5077c478bd9Sstevel@tonic-gate 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
508af007057Syl 	vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
5097c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * If the array the user passed in is too small, set the length
5137c478bd9Sstevel@tonic-gate 	 * to what it needs to be and return
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	if (user_length < vtoc->efi_nparts) {
5167c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
5207c478bd9Sstevel@tonic-gate 
521af007057Syl 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
522af007057Syl 		    efi_parts[i].efi_gpe_PartitionTypeGUID);
523af007057Syl 
524af007057Syl 		for (j = 0;
525af007057Syl 		    j < sizeof (conversion_array)
526af007057Syl 		    / sizeof (struct uuid_to_ptag); j++) {
527af007057Syl 
528af007057Syl 			if (bcmp(&vtoc->efi_parts[i].p_guid,
529af007057Syl 			    &conversion_array[j].uuid,
530af007057Syl 			    sizeof (struct uuid)) == 0) {
531af007057Syl 				vtoc->efi_parts[i].p_tag = j;
532af007057Syl 				break;
533af007057Syl 			}
534af007057Syl 		}
535af007057Syl 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
536af007057Syl 			continue;
537af007057Syl 		vtoc->efi_parts[i].p_flag =
538af007057Syl 		    LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
539af007057Syl 		vtoc->efi_parts[i].p_start =
540af007057Syl 		    LE_64(efi_parts[i].efi_gpe_StartingLBA);
541af007057Syl 		vtoc->efi_parts[i].p_size =
542af007057Syl 		    LE_64(efi_parts[i].efi_gpe_EndingLBA) -
5437c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_start + 1;
544af007057Syl 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
545af007057Syl 			vtoc->efi_parts[i].p_name[j] =
546af007057Syl 			    (uchar_t)LE_16(
547af007057Syl 			    efi_parts[i].efi_gpe_PartitionName[j]);
548af007057Syl 		}
5497c478bd9Sstevel@tonic-gate 
550af007057Syl 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
551af007057Syl 		    efi_parts[i].efi_gpe_UniquePartitionGUID);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	free(efi);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	return (dki_info.dki_partition);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /* writes a "protective" MBR */
5597c478bd9Sstevel@tonic-gate static int
5607c478bd9Sstevel@tonic-gate write_pmbr(int fd, struct dk_gpt *vtoc)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
5637c478bd9Sstevel@tonic-gate 	struct mboot	mb;
5647c478bd9Sstevel@tonic-gate 	uchar_t		*cp;
5657c478bd9Sstevel@tonic-gate 	diskaddr_t	size_in_lba;
56665908c77Syu, larry liu - Sun Microsystems - Beijing China 	uchar_t		*buf;
56765908c77Syu, larry liu - Sun Microsystems - Beijing China 	int		len;
56865908c77Syu, larry liu - Sun Microsystems - Beijing China 
56965908c77Syu, larry liu - Sun Microsystems - Beijing China 	len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
57065908c77Syu, larry liu - Sun Microsystems - Beijing China 	buf = calloc(len, 1);
5717c478bd9Sstevel@tonic-gate 
572987e90dcSxl 	/*
573987e90dcSxl 	 * Preserve any boot code and disk signature if the first block is
574987e90dcSxl 	 * already an MBR.
575987e90dcSxl 	 */
576987e90dcSxl 	dk_ioc.dki_lba = 0;
57765908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_length = len;
578987e90dcSxl 	/* LINTED -- always longlong aligned */
57965908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)buf;
58065908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
581286eb3edSRichard Yao 		(void) memcpy(&mb, buf, sizeof (mb));
582987e90dcSxl 		bzero(&mb, sizeof (mb));
583987e90dcSxl 		mb.signature = LE_16(MBB_MAGIC);
58465908c77Syu, larry liu - Sun Microsystems - Beijing China 	} else {
585286eb3edSRichard Yao 		(void) memcpy(&mb, buf, sizeof (mb));
58665908c77Syu, larry liu - Sun Microsystems - Beijing China 		if (mb.signature != LE_16(MBB_MAGIC)) {
58765908c77Syu, larry liu - Sun Microsystems - Beijing China 			bzero(&mb, sizeof (mb));
58865908c77Syu, larry liu - Sun Microsystems - Beijing China 			mb.signature = LE_16(MBB_MAGIC);
58965908c77Syu, larry liu - Sun Microsystems - Beijing China 		}
590987e90dcSxl 	}
59165908c77Syu, larry liu - Sun Microsystems - Beijing China 
5927c478bd9Sstevel@tonic-gate 	bzero(&mb.parts, sizeof (mb.parts));
5937c478bd9Sstevel@tonic-gate 	cp = (uchar_t *)&mb.parts[0];
5947c478bd9Sstevel@tonic-gate 	/* bootable or not */
5957c478bd9Sstevel@tonic-gate 	*cp++ = 0;
5967c478bd9Sstevel@tonic-gate 	/* beginning CHS; 0xffffff if not representable */
5977c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5987c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
5997c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
6007c478bd9Sstevel@tonic-gate 	/* OS type */
6017c478bd9Sstevel@tonic-gate 	*cp++ = EFI_PMBR;
6027c478bd9Sstevel@tonic-gate 	/* ending CHS; 0xffffff if not representable */
6037c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
6047c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
6057c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
6067c478bd9Sstevel@tonic-gate 	/* starting LBA: 1 (little endian format) by EFI definition */
6077c478bd9Sstevel@tonic-gate 	*cp++ = 0x01;
6087c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
6097c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
6107c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
6117c478bd9Sstevel@tonic-gate 	/* ending LBA: last block on the disk (little endian format) */
6127c478bd9Sstevel@tonic-gate 	size_in_lba = vtoc->efi_last_lba;
6137c478bd9Sstevel@tonic-gate 	if (size_in_lba < 0xffffffff) {
6147c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x000000ff);
6157c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
6167c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
6177c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0xff000000) >> 24;
6187c478bd9Sstevel@tonic-gate 	} else {
6197c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
6207c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
6217c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
6227c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
6237c478bd9Sstevel@tonic-gate 	}
62465908c77Syu, larry liu - Sun Microsystems - Beijing China 
625286eb3edSRichard Yao 	(void) memcpy(buf, &mb, sizeof (mb));
6267c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
62765908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)buf;
6287c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 0;
62965908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_length = len;
6307c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
63165908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(buf);
6327c478bd9Sstevel@tonic-gate 		switch (errno) {
6337c478bd9Sstevel@tonic-gate 		case EIO:
6347c478bd9Sstevel@tonic-gate 			return (VT_EIO);
6357c478bd9Sstevel@tonic-gate 		case EINVAL:
6367c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
6377c478bd9Sstevel@tonic-gate 		default:
6387c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 	}
64165908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(buf);
6427c478bd9Sstevel@tonic-gate 	return (0);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate /* make sure the user specified something reasonable */
6467c478bd9Sstevel@tonic-gate static int
6477c478bd9Sstevel@tonic-gate check_input(struct dk_gpt *vtoc)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	int			resv_part = -1;
6507c478bd9Sstevel@tonic-gate 	int			i, j;
6517c478bd9Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * Sanity-check the input (make sure no partitions overlap)
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
6577c478bd9Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
6587c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
6597c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
6607c478bd9Sstevel@tonic-gate 			if (efi_debug) {
6617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6627c478bd9Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size of %llu",
6637c478bd9Sstevel@tonic-gate 				    i,
6647c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
6657c478bd9Sstevel@tonic-gate 			}
6667c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
669f2be5148Sszhou 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
670f2be5148Sszhou 				continue;
671f2be5148Sszhou 			/* we have encountered an unknown uuid */
672f2be5148Sszhou 			vtoc->efi_parts[i].p_tag = 0xff;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
6757c478bd9Sstevel@tonic-gate 			if (resv_part != -1) {
6767c478bd9Sstevel@tonic-gate 				if (efi_debug) {
677af007057Syl 					(void) fprintf(stderr,
6787c478bd9Sstevel@tonic-gate "found duplicate reserved partition at %d\n",
679af007057Syl 					    i);
6807c478bd9Sstevel@tonic-gate 				}
6817c478bd9Sstevel@tonic-gate 				return (VT_EINVAL);
6827c478bd9Sstevel@tonic-gate 			}
6837c478bd9Sstevel@tonic-gate 			resv_part = i;
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
6867c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
6877c478bd9Sstevel@tonic-gate 			if (efi_debug) {
6887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6897c478bd9Sstevel@tonic-gate 				    "Partition %d starts at %llu.  ",
6907c478bd9Sstevel@tonic-gate 				    i,
6917c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start);
6927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6937c478bd9Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
6947c478bd9Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
6957c478bd9Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
7007c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
7017c478bd9Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
7027c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
7037c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
7047c478bd9Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
7057c478bd9Sstevel@tonic-gate 			if (efi_debug) {
7067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7077c478bd9Sstevel@tonic-gate 				    "Partition %d ends at %llu.  ",
7087c478bd9Sstevel@tonic-gate 				    i,
7097c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start +
7107c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
7117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7127c478bd9Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
7137c478bd9Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
7147c478bd9Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
7157c478bd9Sstevel@tonic-gate 			}
7167c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
7207c478bd9Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
7217c478bd9Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
7227c478bd9Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
7237c478bd9Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
7247c478bd9Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
7257c478bd9Sstevel@tonic-gate 				endsect = jstart + jsize -1;
7267c478bd9Sstevel@tonic-gate 				if ((jstart <= istart) &&
7277c478bd9Sstevel@tonic-gate 				    (istart <= endsect)) {
7287c478bd9Sstevel@tonic-gate 					if (efi_debug) {
7297c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
7307c478bd9Sstevel@tonic-gate "Partition %d overlaps partition %d.",
7317c478bd9Sstevel@tonic-gate 						    i, j);
732af007057Syl 					}
733af007057Syl 					return (VT_EINVAL);
7347c478bd9Sstevel@tonic-gate 				}
7357c478bd9Sstevel@tonic-gate 			}
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	/* just a warning for now */
7397c478bd9Sstevel@tonic-gate 	if ((resv_part == -1) && efi_debug) {
7407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
741af007057Syl 		    "no reserved partition found\n");
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	return (0);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
746af007057Syl /*
747af007057Syl  * add all the unallocated space to the current label
748af007057Syl  */
749af007057Syl int
750af007057Syl efi_use_whole_disk(int fd)
751af007057Syl {
752af007057Syl 	struct dk_gpt		*efi_label;
753af007057Syl 	int			rval;
754af007057Syl 	int			i;
755af007057Syl 	uint_t			phy_last_slice = 0;
756af007057Syl 	diskaddr_t		pl_start = 0;
757af007057Syl 	diskaddr_t		pl_size;
758af007057Syl 
759af007057Syl 	rval = efi_alloc_and_read(fd, &efi_label);
760af007057Syl 	if (rval < 0) {
761af007057Syl 		return (rval);
762af007057Syl 	}
763af007057Syl 
764af007057Syl 	/* find the last physically non-zero partition */
765af007057Syl 	for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
766af007057Syl 		if (pl_start < efi_label->efi_parts[i].p_start) {
767af007057Syl 			pl_start = efi_label->efi_parts[i].p_start;
768af007057Syl 			phy_last_slice = i;
769af007057Syl 		}
770af007057Syl 	}
771af007057Syl 	pl_size = efi_label->efi_parts[phy_last_slice].p_size;
772af007057Syl 
773af007057Syl 	/*
774af007057Syl 	 * If alter_lba is 1, we are using the backup label.
775af007057Syl 	 * Since we can locate the backup label by disk capacity,
776af007057Syl 	 * there must be no unallocated space.
777af007057Syl 	 */
778af007057Syl 	if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
779af007057Syl 	    >= efi_label->efi_last_lba)) {
780af007057Syl 		if (efi_debug) {
781af007057Syl 			(void) fprintf(stderr,
782af007057Syl 			    "efi_use_whole_disk: requested space not found\n");
783af007057Syl 		}
784af007057Syl 		efi_free(efi_label);
785af007057Syl 		return (VT_ENOSPC);
786af007057Syl 	}
787af007057Syl 
788af007057Syl 	/*
789af007057Syl 	 * If there is space between the last physically non-zero partition
790af007057Syl 	 * and the reserved partition, just add the unallocated space to this
791af007057Syl 	 * area. Otherwise, the unallocated space is added to the last
792af007057Syl 	 * physically non-zero partition.
793af007057Syl 	 */
794af007057Syl 	if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
795af007057Syl 	    EFI_MIN_RESV_SIZE) {
796af007057Syl 		efi_label->efi_parts[phy_last_slice].p_size +=
797af007057Syl 		    efi_label->efi_last_lba - efi_label->efi_altern_lba;
798af007057Syl 	}
799af007057Syl 
800af007057Syl 	/*
801af007057Syl 	 * Move the reserved partition. There is currently no data in
802af007057Syl 	 * here except fabricated devids (which get generated via
803af007057Syl 	 * efi_write()). So there is no need to copy data.
804af007057Syl 	 */
805af007057Syl 	efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
806af007057Syl 	    efi_label->efi_last_lba - efi_label->efi_altern_lba;
807af007057Syl 	efi_label->efi_last_u_lba += efi_label->efi_last_lba
808af007057Syl 	    - efi_label->efi_altern_lba;
809af007057Syl 
810af007057Syl 	rval = efi_write(fd, efi_label);
811af007057Syl 	if (rval < 0) {
812af007057Syl 		if (efi_debug) {
813af007057Syl 			(void) fprintf(stderr,
814af007057Syl 			    "efi_use_whole_disk:fail to write label, rval=%d\n",
815af007057Syl 			    rval);
816af007057Syl 		}
817af007057Syl 		efi_free(efi_label);
818af007057Syl 		return (rval);
819af007057Syl 	}
820af007057Syl 
821af007057Syl 	efi_free(efi_label);
822af007057Syl 	return (0);
823af007057Syl }
824af007057Syl 
825af007057Syl 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * write EFI label and backup label
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate int
8307c478bd9Sstevel@tonic-gate efi_write(int fd, struct dk_gpt *vtoc)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
8337c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
8347c478bd9Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
8357c478bd9Sstevel@tonic-gate 	int			i, j;
8367c478bd9Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
83786bbaf93Scg 	int			nblocks;
83886bbaf93Scg 	diskaddr_t		lba_backup_gpt_hdr;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
8417c478bd9Sstevel@tonic-gate 		if (efi_debug)
8427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
8437c478bd9Sstevel@tonic-gate 		switch (errno) {
8447c478bd9Sstevel@tonic-gate 		case EIO:
8457c478bd9Sstevel@tonic-gate 			return (VT_EIO);
8467c478bd9Sstevel@tonic-gate 		case EINVAL:
8477c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
8487c478bd9Sstevel@tonic-gate 		default:
8497c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
853*5f10ef69SYuri Pankov 	if (check_input(vtoc))
854*5f10ef69SYuri Pankov 		return (VT_EINVAL);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
8577c478bd9Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
8587c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
8597c478bd9Sstevel@tonic-gate 	} else {
8607c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
861af007057Syl 		    vtoc->efi_lbasize) *
862af007057Syl 		    vtoc->efi_lbasize;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
86586bbaf93Scg 	/*
86686bbaf93Scg 	 * the number of blocks occupied by GUID partition entry array
86786bbaf93Scg 	 */
86886bbaf93Scg 	nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
86986bbaf93Scg 
87086bbaf93Scg 	/*
87186bbaf93Scg 	 * Backup GPT header is located on the block after GUID
87286bbaf93Scg 	 * partition entry array. Here, we calculate the address
87386bbaf93Scg 	 * for backup GPT header.
87486bbaf93Scg 	 */
87586bbaf93Scg 	lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
8767c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
8777c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/* stuff user's input into EFI struct */
8827c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
8837c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
8847c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
8857c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Reserved1 = 0;
8867c478bd9Sstevel@tonic-gate 	efi->efi_gpt_MyLBA = LE_64(1ULL);
88786bbaf93Scg 	efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
8887c478bd9Sstevel@tonic-gate 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
8897c478bd9Sstevel@tonic-gate 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
8907c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
8917c478bd9Sstevel@tonic-gate 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
8927c478bd9Sstevel@tonic-gate 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
8937c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
89665908c77Syu, larry liu - Sun Microsystems - Beijing China 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
899af007057Syl 		for (j = 0;
900af007057Syl 		    j < sizeof (conversion_array) /
901af007057Syl 		    sizeof (struct uuid_to_ptag); j++) {
902af007057Syl 
903af007057Syl 			if (vtoc->efi_parts[i].p_tag == j) {
904af007057Syl 				UUID_LE_CONVERT(
905af007057Syl 				    efi_parts[i].efi_gpe_PartitionTypeGUID,
906af007057Syl 				    conversion_array[j].uuid);
907af007057Syl 				break;
908af007057Syl 			}
909af007057Syl 		}
910af007057Syl 
911af007057Syl 		if (j == sizeof (conversion_array) /
912af007057Syl 		    sizeof (struct uuid_to_ptag)) {
913af007057Syl 			/*
914af007057Syl 			 * If we didn't have a matching uuid match, bail here.
915af007057Syl 			 * Don't write a label with unknown uuid.
916af007057Syl 			 */
917af007057Syl 			if (efi_debug) {
918af007057Syl 				(void) fprintf(stderr,
919af007057Syl 				    "Unknown uuid for p_tag %d\n",
920af007057Syl 				    vtoc->efi_parts[i].p_tag);
921af007057Syl 			}
922af007057Syl 			return (VT_EINVAL);
923af007057Syl 		}
924af007057Syl 
925af007057Syl 		efi_parts[i].efi_gpe_StartingLBA =
926af007057Syl 		    LE_64(vtoc->efi_parts[i].p_start);
927af007057Syl 		efi_parts[i].efi_gpe_EndingLBA =
928af007057Syl 		    LE_64(vtoc->efi_parts[i].p_start +
929af007057Syl 		    vtoc->efi_parts[i].p_size - 1);
930af007057Syl 		efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
9317c478bd9Sstevel@tonic-gate 		    LE_16(vtoc->efi_parts[i].p_flag);
932af007057Syl 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
933af007057Syl 			efi_parts[i].efi_gpe_PartitionName[j] =
934af007057Syl 			    LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
935af007057Syl 		}
936af007057Syl 		if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
937af007057Syl 		    uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
938af007057Syl 			(void) uuid_generate((uchar_t *)
939af007057Syl 			    &vtoc->efi_parts[i].p_uguid);
940af007057Syl 		}
941af007057Syl 		bcopy(&vtoc->efi_parts[i].p_uguid,
942af007057Syl 		    &efi_parts[i].efi_gpe_UniquePartitionGUID,
943af007057Syl 		    sizeof (uuid_t));
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryArrayCRC32 =
9467c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi_parts,
9477c478bd9Sstevel@tonic-gate 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
9487c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
9497c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9527c478bd9Sstevel@tonic-gate 		free(dk_ioc.dki_data);
9537c478bd9Sstevel@tonic-gate 		switch (errno) {
9547c478bd9Sstevel@tonic-gate 		case EIO:
9557c478bd9Sstevel@tonic-gate 			return (VT_EIO);
9567c478bd9Sstevel@tonic-gate 		case EINVAL:
9577c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
9587c478bd9Sstevel@tonic-gate 		default:
9597c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
96265908c77Syu, larry liu - Sun Microsystems - Beijing China 
9637c478bd9Sstevel@tonic-gate 	/* write backup partition array */
9647c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
9657c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length -= vtoc->efi_lbasize;
96665908c77Syu, larry liu - Sun Microsystems - Beijing China 	/* LINTED */
96765908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
96865908c77Syu, larry liu - Sun Microsystems - Beijing China 	    vtoc->efi_lbasize);
96965908c77Syu, larry liu - Sun Microsystems - Beijing China 
9707c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
9717c478bd9Sstevel@tonic-gate 		/*
9727c478bd9Sstevel@tonic-gate 		 * we wrote the primary label okay, so don't fail
9737c478bd9Sstevel@tonic-gate 		 */
9747c478bd9Sstevel@tonic-gate 		if (efi_debug) {
9757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9767c478bd9Sstevel@tonic-gate 			    "write of backup partitions to block %llu "
9777c478bd9Sstevel@tonic-gate 			    "failed, errno %d\n",
9787c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba + 1,
9797c478bd9Sstevel@tonic-gate 			    errno);
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * now swap MyLBA and AlternateLBA fields and write backup
9847c478bd9Sstevel@tonic-gate 	 * partition table header
9857c478bd9Sstevel@tonic-gate 	 */
98686bbaf93Scg 	dk_ioc.dki_lba = lba_backup_gpt_hdr;
9877c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length = vtoc->efi_lbasize;
98865908c77Syu, larry liu - Sun Microsystems - Beijing China 	/* LINTED */
98965908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
99065908c77Syu, larry liu - Sun Microsystems - Beijing China 	    vtoc->efi_lbasize);
9917c478bd9Sstevel@tonic-gate 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
99286bbaf93Scg 	efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
9937c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
9947c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
9957c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
9967c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
9977c478bd9Sstevel@tonic-gate 	    sizeof (struct efi_gpt)));
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
10007c478bd9Sstevel@tonic-gate 		if (efi_debug) {
10017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10027c478bd9Sstevel@tonic-gate 			    "write of backup header to block %llu failed, "
10037c478bd9Sstevel@tonic-gate 			    "errno %d\n",
100486bbaf93Scg 			    lba_backup_gpt_hdr,
10057c478bd9Sstevel@tonic-gate 			    errno);
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 	/* write the PMBR */
10097c478bd9Sstevel@tonic-gate 	(void) write_pmbr(fd, vtoc);
10107c478bd9Sstevel@tonic-gate 	free(dk_ioc.dki_data);
10117c478bd9Sstevel@tonic-gate 	return (0);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate void
10157c478bd9Sstevel@tonic-gate efi_free(struct dk_gpt *ptr)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	free(ptr);
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /*
10217c478bd9Sstevel@tonic-gate  * Input: File descriptor
1022342440ecSPrasad Singamsetty  * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1023342440ecSPrasad Singamsetty  * Otherwise 0.
10247c478bd9Sstevel@tonic-gate  */
10257c478bd9Sstevel@tonic-gate int
10267c478bd9Sstevel@tonic-gate efi_type(int fd)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	struct vtoc vtoc;
1029342440ecSPrasad Singamsetty 	struct extvtoc extvtoc;
10307c478bd9Sstevel@tonic-gate 
1031342440ecSPrasad Singamsetty 	if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1032342440ecSPrasad Singamsetty 		if (errno == ENOTSUP)
10337c478bd9Sstevel@tonic-gate 			return (1);
1034342440ecSPrasad Singamsetty 		else if (errno == ENOTTY) {
1035342440ecSPrasad Singamsetty 			if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1036342440ecSPrasad Singamsetty 				if (errno == ENOTSUP)
1037342440ecSPrasad Singamsetty 					return (1);
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 	return (0);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate void
10447c478bd9Sstevel@tonic-gate efi_err_check(struct dk_gpt *vtoc)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	int			resv_part = -1;
10477c478bd9Sstevel@tonic-gate 	int			i, j;
10487c478bd9Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
10497c478bd9Sstevel@tonic-gate 	int			overlap = 0;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	/*
10527c478bd9Sstevel@tonic-gate 	 * make sure no partitions overlap
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
10557c478bd9Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
10567c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
10577c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
10587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10597c478bd9Sstevel@tonic-gate 			    "partition %d is \"unassigned\" but has a size "
10607c478bd9Sstevel@tonic-gate 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
10617c478bd9Sstevel@tonic-gate 		}
10627c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
10637c478bd9Sstevel@tonic-gate 			continue;
10647c478bd9Sstevel@tonic-gate 		}
10657c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
10667c478bd9Sstevel@tonic-gate 			if (resv_part != -1) {
10677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10687c478bd9Sstevel@tonic-gate 				    "found duplicate reserved partition at "
10697c478bd9Sstevel@tonic-gate 				    "%d\n", i);
10707c478bd9Sstevel@tonic-gate 			}
10717c478bd9Sstevel@tonic-gate 			resv_part = i;
10727c478bd9Sstevel@tonic-gate 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
10737c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10747c478bd9Sstevel@tonic-gate 				    "Warning: reserved partition size must "
10757c478bd9Sstevel@tonic-gate 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
10787c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
10797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10807c478bd9Sstevel@tonic-gate 			    "Partition %d starts at %llu\n",
10817c478bd9Sstevel@tonic-gate 			    i,
10827c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start);
10837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10847c478bd9Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
10857c478bd9Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
10867c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
10897c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
10907c478bd9Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
10917c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
10927c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
10937c478bd9Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
10947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10957c478bd9Sstevel@tonic-gate 			    "Partition %d ends at %llu\n",
10967c478bd9Sstevel@tonic-gate 			    i,
10977c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start +
10987c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_size);
10997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11007c478bd9Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
11017c478bd9Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
11027c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
11067c478bd9Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
11077c478bd9Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
11087c478bd9Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
11097c478bd9Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
11107c478bd9Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
11117c478bd9Sstevel@tonic-gate 				endsect = jstart + jsize -1;
11127c478bd9Sstevel@tonic-gate 				if ((jstart <= istart) &&
11137c478bd9Sstevel@tonic-gate 				    (istart <= endsect)) {
11147c478bd9Sstevel@tonic-gate 					if (!overlap) {
11157c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
11167c478bd9Sstevel@tonic-gate 					    "label error: EFI Labels do not "
11177c478bd9Sstevel@tonic-gate 					    "support overlapping partitions\n");
11187c478bd9Sstevel@tonic-gate 					}
11197c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
11207c478bd9Sstevel@tonic-gate 					    "Partition %d overlaps partition "
11217c478bd9Sstevel@tonic-gate 					    "%d.\n", i, j);
11227c478bd9Sstevel@tonic-gate 					overlap = 1;
11237c478bd9Sstevel@tonic-gate 				}
11247c478bd9Sstevel@tonic-gate 			}
11257c478bd9Sstevel@tonic-gate 		}
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	/* make sure there is a reserved partition */
11287c478bd9Sstevel@tonic-gate 	if (resv_part == -1) {
11297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1130af007057Syl 		    "no reserved partition found\n");
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate /*
11357c478bd9Sstevel@tonic-gate  * We need to get information necessary to construct a *new* efi
11367c478bd9Sstevel@tonic-gate  * label type
11377c478bd9Sstevel@tonic-gate  */
11387c478bd9Sstevel@tonic-gate int
11397c478bd9Sstevel@tonic-gate efi_auto_sense(int fd, struct dk_gpt **vtoc)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	int	i;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	/*
11457c478bd9Sstevel@tonic-gate 	 * Now build the default partition table
11467c478bd9Sstevel@tonic-gate 	 */
11477c478bd9Sstevel@tonic-gate 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
11487c478bd9Sstevel@tonic-gate 		if (efi_debug) {
11497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 		return (-1);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
11557c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
11567c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
11577c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_start = 0;
11587c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_size = 0;
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	/*
11617c478bd9Sstevel@tonic-gate 	 * Make constants first
11627c478bd9Sstevel@tonic-gate 	 * and variable partitions later
11637c478bd9Sstevel@tonic-gate 	 */
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	/* root partition - s0 128 MB */
11667c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_start = 34;
11677c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_size = 262144;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	/* partition - s1  128 MB */
11707c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_start = 262178;
11717c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_size = 262144;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	/* partition -s2 is NOT the Backup disk */
11747c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	/* partition -s6 /usr partition - HOG */
11777c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_start = 524322;
11787c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
11797c478bd9Sstevel@tonic-gate 	    - (1024 * 16);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/* efi reserved partition - s9 16K */
11827c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
11837c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
11847c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
11857c478bd9Sstevel@tonic-gate 	return (0);
11867c478bd9Sstevel@tonic-gate }
1187