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>
267e934d3aSAndy Fiddaman  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347e934d3aSAndy Fiddaman #include <smbios.h>
357c478bd9Sstevel@tonic-gate #include <uuid/uuid.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
397c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
407c478bd9Sstevel@tonic-gate #include <sys/mhd.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
437c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
447c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
457c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static struct uuid_to_ptag {
487c478bd9Sstevel@tonic-gate 	struct uuid	uuid;
497c478bd9Sstevel@tonic-gate } conversion_array[] = {
507c478bd9Sstevel@tonic-gate 	{ EFI_UNUSED },
517c478bd9Sstevel@tonic-gate 	{ EFI_BOOT },
527c478bd9Sstevel@tonic-gate 	{ EFI_ROOT },
537c478bd9Sstevel@tonic-gate 	{ EFI_SWAP },
547c478bd9Sstevel@tonic-gate 	{ EFI_USR },
557c478bd9Sstevel@tonic-gate 	{ EFI_BACKUP },
567c478bd9Sstevel@tonic-gate 	{ 0 },			/* STAND is never used */
577c478bd9Sstevel@tonic-gate 	{ EFI_VAR },
587c478bd9Sstevel@tonic-gate 	{ EFI_HOME },
597c478bd9Sstevel@tonic-gate 	{ EFI_ALTSCTR },
60bd93c05dSAlexander Eremin 	{ 0 },			/* CACHE is never used */
617c478bd9Sstevel@tonic-gate 	{ EFI_RESERVED },
627c478bd9Sstevel@tonic-gate 	{ EFI_SYSTEM },
637c478bd9Sstevel@tonic-gate 	{ EFI_LEGACY_MBR },
6480c797c0SSharath M Srinivasan 	{ EFI_SYMC_PUB },
6580c797c0SSharath M Srinivasan 	{ EFI_SYMC_CDS },
667c478bd9Sstevel@tonic-gate 	{ EFI_MSFT_RESV },
677c478bd9Sstevel@tonic-gate 	{ EFI_DELL_BASIC },
687c478bd9Sstevel@tonic-gate 	{ EFI_DELL_RAID },
697c478bd9Sstevel@tonic-gate 	{ EFI_DELL_SWAP },
707c478bd9Sstevel@tonic-gate 	{ EFI_DELL_LVM },
71f2be5148Sszhou 	{ EFI_DELL_RESV },
72f2be5148Sszhou 	{ EFI_AAPL_HFS },
73e21ea675SYuri Pankov 	{ EFI_AAPL_UFS },
74e088753cSToomas Soome 	{ EFI_BIOS_BOOT },
75e21ea675SYuri Pankov 	{ EFI_FREEBSD_BOOT },
76e21ea675SYuri Pankov 	{ EFI_FREEBSD_SWAP },
77e21ea675SYuri Pankov 	{ EFI_FREEBSD_UFS },
78e21ea675SYuri Pankov 	{ EFI_FREEBSD_VINUM },
79e21ea675SYuri Pankov 	{ EFI_FREEBSD_ZFS }
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Default vtoc information for non-SVr4 partitions
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate struct dk_map2  default_vtoc_map[NDKMAP] = {
867c478bd9Sstevel@tonic-gate 	{	V_ROOT,		0	},		/* a - 0 */
877c478bd9Sstevel@tonic-gate 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
887c478bd9Sstevel@tonic-gate 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
897c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* d - 3 */
907c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* e - 4 */
917c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* f - 5 */
927c478bd9Sstevel@tonic-gate 	{	V_USR,		0	},		/* g - 6 */
937c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* h - 7 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
987c478bd9Sstevel@tonic-gate 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
997c478bd9Sstevel@tonic-gate 	{	V_ALTSCTR,	0	},		/* j - 9 */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #else
1027c478bd9Sstevel@tonic-gate #error No VTOC format defined.
1037c478bd9Sstevel@tonic-gate #endif			/* defined(i386) */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* k - 10 */
1067c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* l - 11 */
1077c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* m - 12 */
1087c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* n - 13 */
1097c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* o - 14 */
1107c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* p - 15 */
1117c478bd9Sstevel@tonic-gate #endif			/* defined(_SUNOS_VTOC_16) */
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifdef DEBUG
1157c478bd9Sstevel@tonic-gate int efi_debug = 1;
1167c478bd9Sstevel@tonic-gate #else
1177c478bd9Sstevel@tonic-gate int efi_debug = 0;
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate 
1207e934d3aSAndy Fiddaman #define	EFI_FIXES_DB "/usr/share/hwdata/efi.fixes"
1217e934d3aSAndy Fiddaman 
1227c478bd9Sstevel@tonic-gate extern unsigned int	efi_crc32(const unsigned char *, unsigned int);
1237c478bd9Sstevel@tonic-gate static int		efi_read(int, struct dk_gpt *);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static int
1267c478bd9Sstevel@tonic-gate read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	struct dk_minfo		disk_info;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
1317c478bd9Sstevel@tonic-gate 		return (errno);
1327c478bd9Sstevel@tonic-gate 	*capacity = disk_info.dki_capacity;
1337c478bd9Sstevel@tonic-gate 	*lbsize = disk_info.dki_lbsize;
1347c478bd9Sstevel@tonic-gate 	return (0);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * the number of blocks the EFI label takes up (round up to nearest
1397c478bd9Sstevel@tonic-gate  * block)
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate #define	NBLOCKS(p, l)	(1 + ((((p) * (int)sizeof (efi_gpe_t))  + \
1427c478bd9Sstevel@tonic-gate 				((l) - 1)) / (l)))
1437c478bd9Sstevel@tonic-gate /* number of partitions -- limited by what we can malloc */
1447c478bd9Sstevel@tonic-gate #define	MAX_PARTS	((4294967295UL - sizeof (struct dk_gpt)) / \
1457c478bd9Sstevel@tonic-gate 			    sizeof (struct dk_part))
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate int
1487c478bd9Sstevel@tonic-gate efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	diskaddr_t	capacity;
1517c478bd9Sstevel@tonic-gate 	uint_t		lbsize;
1527c478bd9Sstevel@tonic-gate 	uint_t		nblocks;
1537c478bd9Sstevel@tonic-gate 	size_t		length;
1547c478bd9Sstevel@tonic-gate 	struct dk_gpt	*vptr;
1557c478bd9Sstevel@tonic-gate 	struct uuid	uuid;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (read_disk_info(fd, &capacity, &lbsize) != 0) {
1587c478bd9Sstevel@tonic-gate 		if (efi_debug)
1597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1607c478bd9Sstevel@tonic-gate 			    "couldn't read disk information\n");
1617c478bd9Sstevel@tonic-gate 		return (-1);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	nblocks = NBLOCKS(nparts, lbsize);
1657c478bd9Sstevel@tonic-gate 	if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
1667c478bd9Sstevel@tonic-gate 		/* 16K plus one block for the GPT */
1677c478bd9Sstevel@tonic-gate 		nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (nparts > MAX_PARTS) {
1717c478bd9Sstevel@tonic-gate 		if (efi_debug) {
1727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1737c478bd9Sstevel@tonic-gate 			"the maximum number of partitions supported is %lu\n",
1747c478bd9Sstevel@tonic-gate 			    MAX_PARTS);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 		return (-1);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	length = sizeof (struct dk_gpt) +
1807c478bd9Sstevel@tonic-gate 	    sizeof (struct dk_part) * (nparts - 1);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
1837c478bd9Sstevel@tonic-gate 		return (-1);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	vptr = *vtoc;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	vptr->efi_version = EFI_VERSION_CURRENT;
1887c478bd9Sstevel@tonic-gate 	vptr->efi_lbasize = lbsize;
1897c478bd9Sstevel@tonic-gate 	vptr->efi_nparts = nparts;
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * add one block here for the PMBR; on disks with a 512 byte
1927c478bd9Sstevel@tonic-gate 	 * block size and 128 or fewer partitions, efi_first_u_lba
1937c478bd9Sstevel@tonic-gate 	 * should work out to "34"
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	vptr->efi_first_u_lba = nblocks + 1;
1967c478bd9Sstevel@tonic-gate 	vptr->efi_last_lba = capacity - 1;
197af007057Syl 	vptr->efi_altern_lba = capacity -1;
1987c478bd9Sstevel@tonic-gate 	vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
19965908c77Syu, larry liu - Sun Microsystems - Beijing China 
2007c478bd9Sstevel@tonic-gate 	(void) uuid_generate((uchar_t *)&uuid);
2017c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
2027c478bd9Sstevel@tonic-gate 	return (0);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * Read EFI - return partition number upon success.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate int
2097c478bd9Sstevel@tonic-gate efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	int			rval;
2127c478bd9Sstevel@tonic-gate 	uint32_t		nparts;
2137c478bd9Sstevel@tonic-gate 	int			length;
214*fe12dc75SToomas Soome 	struct mboot		*mbr;
215*fe12dc75SToomas Soome 	struct ipart		*ipart;
216*fe12dc75SToomas Soome 	diskaddr_t		capacity;
217*fe12dc75SToomas Soome 	uint_t			lbsize;
218*fe12dc75SToomas Soome 	int			i;
219*fe12dc75SToomas Soome 
220*fe12dc75SToomas Soome 	if (read_disk_info(fd, &capacity, &lbsize) != 0)
221*fe12dc75SToomas Soome 		return (VT_ERROR);
222*fe12dc75SToomas Soome 
223*fe12dc75SToomas Soome 	if ((mbr = calloc(lbsize, 1)) == NULL)
224*fe12dc75SToomas Soome 		return (VT_ERROR);
225*fe12dc75SToomas Soome 
226*fe12dc75SToomas Soome 	if ((ioctl(fd, DKIOCGMBOOT, (caddr_t)mbr)) == -1) {
227*fe12dc75SToomas Soome 		free(mbr);
228*fe12dc75SToomas Soome 		return (VT_ERROR);
229*fe12dc75SToomas Soome 	}
230*fe12dc75SToomas Soome 
231*fe12dc75SToomas Soome 	if (mbr->signature != MBB_MAGIC) {
232*fe12dc75SToomas Soome 		free(mbr);
233*fe12dc75SToomas Soome 		return (VT_EINVAL);
234*fe12dc75SToomas Soome 	}
235*fe12dc75SToomas Soome 	ipart = (struct ipart *)(uintptr_t)mbr->parts;
236*fe12dc75SToomas Soome 
237*fe12dc75SToomas Soome 	/* Check if we have partition with ID EFI_PMBR */
238*fe12dc75SToomas Soome 	for (i = 0; i < FD_NUMPART; i++) {
239*fe12dc75SToomas Soome 		if (ipart[i].systid == EFI_PMBR)
240*fe12dc75SToomas Soome 			break;
241*fe12dc75SToomas Soome 	}
242*fe12dc75SToomas Soome 	free(mbr);
243*fe12dc75SToomas Soome 	if (i == FD_NUMPART)
244*fe12dc75SToomas Soome 		return (VT_EINVAL);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* figure out the number of entries that would fit into 16K */
2477c478bd9Sstevel@tonic-gate 	nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
2487c478bd9Sstevel@tonic-gate 	length = (int) sizeof (struct dk_gpt) +
249af007057Syl 	    (int) sizeof (struct dk_part) * (nparts - 1);
2507c478bd9Sstevel@tonic-gate 	if ((*vtoc = calloc(length, 1)) == NULL)
2517c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_nparts = nparts;
2547c478bd9Sstevel@tonic-gate 	rval = efi_read(fd, *vtoc);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
2577c478bd9Sstevel@tonic-gate 		void *tmp;
2587c478bd9Sstevel@tonic-gate 		length = (int) sizeof (struct dk_gpt) +
259af007057Syl 		    (int) sizeof (struct dk_part) *
260af007057Syl 		    ((*vtoc)->efi_nparts - 1);
2617c478bd9Sstevel@tonic-gate 		nparts = (*vtoc)->efi_nparts;
2627c478bd9Sstevel@tonic-gate 		if ((tmp = realloc(*vtoc, length)) == NULL) {
2637c478bd9Sstevel@tonic-gate 			free (*vtoc);
2647c478bd9Sstevel@tonic-gate 			*vtoc = NULL;
2657c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
2667c478bd9Sstevel@tonic-gate 		} else {
2677c478bd9Sstevel@tonic-gate 			*vtoc = tmp;
2687c478bd9Sstevel@tonic-gate 			rval = efi_read(fd, *vtoc);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (rval < 0) {
2737c478bd9Sstevel@tonic-gate 		if (efi_debug) {
2747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2757c478bd9Sstevel@tonic-gate 			    "read of EFI table failed, rval=%d\n", rval);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		free (*vtoc);
2787c478bd9Sstevel@tonic-gate 		*vtoc = NULL;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (rval);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static int
2857c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
2887c478bd9Sstevel@tonic-gate 	int error;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
2917c478bd9Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
2927c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data = data;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	return (error);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static int
2987c478bd9Sstevel@tonic-gate check_label(int fd, dk_efi_t *dk_ioc)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
3017c478bd9Sstevel@tonic-gate 	uint_t			crc;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
3047c478bd9Sstevel@tonic-gate 		switch (errno) {
3057c478bd9Sstevel@tonic-gate 		case EIO:
3067c478bd9Sstevel@tonic-gate 			return (VT_EIO);
3077c478bd9Sstevel@tonic-gate 		default:
3087c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	efi = dk_ioc->dki_data;
3127c478bd9Sstevel@tonic-gate 	if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
3137c478bd9Sstevel@tonic-gate 		if (efi_debug)
3147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3157c478bd9Sstevel@tonic-gate 			    "Bad EFI signature: 0x%llx != 0x%llx\n",
3167c478bd9Sstevel@tonic-gate 			    (long long)efi->efi_gpt_Signature,
3177c478bd9Sstevel@tonic-gate 			    (long long)LE_64(EFI_SIGNATURE));
3187c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * check CRC of the header; the size of the header should
3237c478bd9Sstevel@tonic-gate 	 * never be larger than one block
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	crc = efi->efi_gpt_HeaderCRC32;
3267c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
3297c478bd9Sstevel@tonic-gate 	    crc != LE_32(efi_crc32((unsigned char *)efi,
3307c478bd9Sstevel@tonic-gate 	    LE_32(efi->efi_gpt_HeaderSize)))) {
3317c478bd9Sstevel@tonic-gate 		if (efi_debug)
3327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
333af007057Syl 			    "Bad EFI CRC: 0x%x != 0x%x\n",
334af007057Syl 			    crc,
335af007057Syl 			    LE_32(efi_crc32((unsigned char *)efi,
336af007057Syl 			    sizeof (struct efi_gpt))));
3377c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	return (0);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate static int
3447c478bd9Sstevel@tonic-gate efi_read(int fd, struct dk_gpt *vtoc)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	int			i, j;
3477c478bd9Sstevel@tonic-gate 	int			label_len;
3487c478bd9Sstevel@tonic-gate 	int			rval = 0;
349d84f0041SAlexandre Chartre 	int			vdc_flag = 0;
3507c478bd9Sstevel@tonic-gate 	struct dk_minfo		disk_info;
3517c478bd9Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
3527c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
3537c478bd9Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
3547c478bd9Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
3557c478bd9Sstevel@tonic-gate 	uint32_t		user_length;
35686bbaf93Scg 	boolean_t		legacy_label = B_FALSE;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * get the partition number for this file descriptor.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
362af007057Syl 		if (efi_debug) {
363af007057Syl 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
364af007057Syl 		}
3657c478bd9Sstevel@tonic-gate 		switch (errno) {
3667c478bd9Sstevel@tonic-gate 		case EIO:
3677c478bd9Sstevel@tonic-gate 			return (VT_EIO);
3687c478bd9Sstevel@tonic-gate 		case EINVAL:
3697c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
3707c478bd9Sstevel@tonic-gate 		default:
3717c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 	}
3745f10ef69SYuri Pankov 
3755f10ef69SYuri Pankov 	if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
376d84f0041SAlexandre Chartre 	    (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
377d84f0041SAlexandre Chartre 		/*
378d84f0041SAlexandre Chartre 		 * The controller and drive name "vdc" (virtual disk client)
379d84f0041SAlexandre Chartre 		 * indicates a LDoms virtual disk.
380d84f0041SAlexandre Chartre 		 */
381d84f0041SAlexandre Chartre 		vdc_flag++;
3827c478bd9Sstevel@tonic-gate 	}
383d84f0041SAlexandre Chartre 
3847c478bd9Sstevel@tonic-gate 	/* get the LBA size */
3857c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
3867c478bd9Sstevel@tonic-gate 		if (efi_debug) {
3877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3887c478bd9Sstevel@tonic-gate 			    "assuming LBA 512 bytes %d\n",
3897c478bd9Sstevel@tonic-gate 			    errno);
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	if (disk_info.dki_lbsize == 0) {
3947c478bd9Sstevel@tonic-gate 		if (efi_debug) {
3957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3967c478bd9Sstevel@tonic-gate 			    "efi_read: assuming LBA 512 bytes\n");
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		disk_info.dki_lbsize = DEV_BSIZE;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * Read the EFI GPT to figure out how many partitions we need
4027c478bd9Sstevel@tonic-gate 	 * to deal with.
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
4057c478bd9Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
4067c478bd9Sstevel@tonic-gate 		label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
4077c478bd9Sstevel@tonic-gate 	} else {
4087c478bd9Sstevel@tonic-gate 		label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
409af007057Syl 		    disk_info.dki_lbsize;
4107c478bd9Sstevel@tonic-gate 		if (label_len % disk_info.dki_lbsize) {
4117c478bd9Sstevel@tonic-gate 			/* pad to physical sector size */
4127c478bd9Sstevel@tonic-gate 			label_len += disk_info.dki_lbsize;
4137c478bd9Sstevel@tonic-gate 			label_len &= ~(disk_info.dki_lbsize - 1);
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
4187c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
4197c478bd9Sstevel@tonic-gate 
420c7e1889fSyl 	dk_ioc.dki_length = disk_info.dki_lbsize;
4217c478bd9Sstevel@tonic-gate 	user_length = vtoc->efi_nparts;
4227c478bd9Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
4235f10ef69SYuri Pankov 	if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
42486bbaf93Scg 		/*
42586bbaf93Scg 		 * No valid label here; try the alternate. Note that here
42686bbaf93Scg 		 * we just read GPT header and save it into dk_ioc.data,
42786bbaf93Scg 		 * Later, we will read GUID partition entry array if we
42886bbaf93Scg 		 * can get valid GPT header.
42986bbaf93Scg 		 */
43086bbaf93Scg 
43186bbaf93Scg 		/*
43286bbaf93Scg 		 * This is a workaround for legacy systems. In the past, the
43386bbaf93Scg 		 * last sector of SCSI disk was invisible on x86 platform. At
43486bbaf93Scg 		 * that time, backup label was saved on the next to the last
43586bbaf93Scg 		 * sector. It is possible for users to move a disk from previous
43686bbaf93Scg 		 * solaris system to present system. Here, we attempt to search
43786bbaf93Scg 		 * legacy backup EFI label first.
43886bbaf93Scg 		 */
43986bbaf93Scg 		dk_ioc.dki_lba = disk_info.dki_capacity - 2;
4407c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = disk_info.dki_lbsize;
4414f0dea16Scg 		rval = check_label(fd, &dk_ioc);
44286bbaf93Scg 		if (rval == VT_EINVAL) {
4434f0dea16Scg 			/*
44486bbaf93Scg 			 * we didn't find legacy backup EFI label, try to
44586bbaf93Scg 			 * search backup EFI label in the last block.
4464f0dea16Scg 			 */
44786bbaf93Scg 			dk_ioc.dki_lba = disk_info.dki_capacity - 1;
4484f0dea16Scg 			dk_ioc.dki_length = disk_info.dki_lbsize;
4494f0dea16Scg 			rval = check_label(fd, &dk_ioc);
45086bbaf93Scg 			if (rval == 0) {
45186bbaf93Scg 				legacy_label = B_TRUE;
45286bbaf93Scg 				if (efi_debug)
45386bbaf93Scg 					(void) fprintf(stderr,
45486bbaf93Scg 					    "efi_read: primary label corrupt; "
45586bbaf93Scg 					    "using EFI backup label located on"
45686bbaf93Scg 					    " the last block\n");
4574f0dea16Scg 			}
45886bbaf93Scg 		} else {
45986bbaf93Scg 			if ((efi_debug) && (rval == 0))
46086bbaf93Scg 				(void) fprintf(stderr, "efi_read: primary label"
46186bbaf93Scg 				    " corrupt; using legacy EFI backup label "
46286bbaf93Scg 				    " located on the next to last block\n");
4634f0dea16Scg 		}
4644f0dea16Scg 
4654f0dea16Scg 		if (rval == 0) {
4667c478bd9Sstevel@tonic-gate 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4677c478bd9Sstevel@tonic-gate 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
4687c478bd9Sstevel@tonic-gate 			vtoc->efi_nparts =
4697c478bd9Sstevel@tonic-gate 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4707c478bd9Sstevel@tonic-gate 			/*
47186bbaf93Scg 			 * Partition tables are between backup GPT header
47286bbaf93Scg 			 * table and ParitionEntryLBA (the starting LBA of
47386bbaf93Scg 			 * the GUID partition entries array). Now that we
47486bbaf93Scg 			 * already got valid GPT header and saved it in
47586bbaf93Scg 			 * dk_ioc.dki_data, we try to get GUID partition
47686bbaf93Scg 			 * entry array here.
4777c478bd9Sstevel@tonic-gate 			 */
47865908c77Syu, larry liu - Sun Microsystems - Beijing China 			/* LINTED */
47965908c77Syu, larry liu - Sun Microsystems - Beijing China 			dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
48065908c77Syu, larry liu - Sun Microsystems - Beijing China 			    + disk_info.dki_lbsize);
48186bbaf93Scg 			if (legacy_label)
48286bbaf93Scg 				dk_ioc.dki_length = disk_info.dki_capacity - 1 -
483af007057Syl 				    dk_ioc.dki_lba;
48486bbaf93Scg 			else
48586bbaf93Scg 				dk_ioc.dki_length = disk_info.dki_capacity - 2 -
486af007057Syl 				    dk_ioc.dki_lba;
4877c478bd9Sstevel@tonic-gate 			dk_ioc.dki_length *= disk_info.dki_lbsize;
48886bbaf93Scg 			if (dk_ioc.dki_length >
48986bbaf93Scg 			    ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
4907c478bd9Sstevel@tonic-gate 				rval = VT_EINVAL;
4917c478bd9Sstevel@tonic-gate 			} else {
49286bbaf93Scg 				/*
49386bbaf93Scg 				 * read GUID partition entry array
49486bbaf93Scg 				 */
4957c478bd9Sstevel@tonic-gate 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
4967c478bd9Sstevel@tonic-gate 			}
4977c478bd9Sstevel@tonic-gate 		}
498d84f0041SAlexandre Chartre 
499d84f0041SAlexandre Chartre 	} else if (rval == 0) {
500d84f0041SAlexandre Chartre 
501c7e1889fSyl 		dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
50265908c77Syu, larry liu - Sun Microsystems - Beijing China 		/* LINTED */
50365908c77Syu, larry liu - Sun Microsystems - Beijing China 		dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
50465908c77Syu, larry liu - Sun Microsystems - Beijing China 		    + disk_info.dki_lbsize);
505c7e1889fSyl 		dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
506c7e1889fSyl 		rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
507d84f0041SAlexandre Chartre 
508d84f0041SAlexandre Chartre 	} else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
509d84f0041SAlexandre Chartre 		/*
510d84f0041SAlexandre Chartre 		 * When the device is a LDoms virtual disk, the DKIOCGETEFI
511d84f0041SAlexandre Chartre 		 * ioctl can fail with EINVAL if the virtual disk backend
512d84f0041SAlexandre Chartre 		 * is a ZFS volume serviced by a domain running an old version
513d84f0041SAlexandre Chartre 		 * of Solaris. This is because the DKIOCGETEFI ioctl was
514d84f0041SAlexandre Chartre 		 * initially incorrectly implemented for a ZFS volume and it
515d84f0041SAlexandre Chartre 		 * expected the GPT and GPE to be retrieved with a single ioctl.
516d84f0041SAlexandre Chartre 		 * So we try to read the GPT and the GPE using that old style
517d84f0041SAlexandre Chartre 		 * ioctl.
518d84f0041SAlexandre Chartre 		 */
519d84f0041SAlexandre Chartre 		dk_ioc.dki_lba = 1;
520d84f0041SAlexandre Chartre 		dk_ioc.dki_length = label_len;
521d84f0041SAlexandre Chartre 		rval = check_label(fd, &dk_ioc);
5227c478bd9Sstevel@tonic-gate 	}
523d84f0041SAlexandre Chartre 
5247c478bd9Sstevel@tonic-gate 	if (rval < 0) {
5257c478bd9Sstevel@tonic-gate 		free(efi);
5267c478bd9Sstevel@tonic-gate 		return (rval);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
530c7e1889fSyl 	efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Assemble this into a "dk_gpt" struct for easier
5347c478bd9Sstevel@tonic-gate 	 * digestibility by applications.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
5377c478bd9Sstevel@tonic-gate 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
5387c478bd9Sstevel@tonic-gate 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
5397c478bd9Sstevel@tonic-gate 	vtoc->efi_lbasize = disk_info.dki_lbsize;
5407c478bd9Sstevel@tonic-gate 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
5417c478bd9Sstevel@tonic-gate 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
5427c478bd9Sstevel@tonic-gate 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
543af007057Syl 	vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
5447c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/*
5477c478bd9Sstevel@tonic-gate 	 * If the array the user passed in is too small, set the length
5487c478bd9Sstevel@tonic-gate 	 * to what it needs to be and return
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (user_length < vtoc->efi_nparts) {
5517c478bd9Sstevel@tonic-gate 		return (VT_EINVAL);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
5557c478bd9Sstevel@tonic-gate 
556af007057Syl 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
557af007057Syl 		    efi_parts[i].efi_gpe_PartitionTypeGUID);
558af007057Syl 
559af007057Syl 		for (j = 0;
560af007057Syl 		    j < sizeof (conversion_array)
561af007057Syl 		    / sizeof (struct uuid_to_ptag); j++) {
562af007057Syl 
563af007057Syl 			if (bcmp(&vtoc->efi_parts[i].p_guid,
564af007057Syl 			    &conversion_array[j].uuid,
565af007057Syl 			    sizeof (struct uuid)) == 0) {
566af007057Syl 				vtoc->efi_parts[i].p_tag = j;
567af007057Syl 				break;
568af007057Syl 			}
569af007057Syl 		}
570af007057Syl 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
571af007057Syl 			continue;
572af007057Syl 		vtoc->efi_parts[i].p_flag =
573af007057Syl 		    LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
574af007057Syl 		vtoc->efi_parts[i].p_start =
575af007057Syl 		    LE_64(efi_parts[i].efi_gpe_StartingLBA);
576af007057Syl 		vtoc->efi_parts[i].p_size =
577af007057Syl 		    LE_64(efi_parts[i].efi_gpe_EndingLBA) -
5787c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_start + 1;
579af007057Syl 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
580af007057Syl 			vtoc->efi_parts[i].p_name[j] =
581af007057Syl 			    (uchar_t)LE_16(
582af007057Syl 			    efi_parts[i].efi_gpe_PartitionName[j]);
583af007057Syl 		}
5847c478bd9Sstevel@tonic-gate 
585af007057Syl 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
586af007057Syl 		    efi_parts[i].efi_gpe_UniquePartitionGUID);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	free(efi);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	return (dki_info.dki_partition);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937e934d3aSAndy Fiddaman static void
5947e934d3aSAndy Fiddaman hardware_workarounds(int *slot, int *active)
5957e934d3aSAndy Fiddaman {
5967e934d3aSAndy Fiddaman 	smbios_struct_t s_sys, s_mb;
5977e934d3aSAndy Fiddaman 	smbios_info_t sys, mb;
5987e934d3aSAndy Fiddaman 	smbios_hdl_t *shp;
5997e934d3aSAndy Fiddaman 	char buf[0x400];
6007e934d3aSAndy Fiddaman 	FILE *fp;
6017e934d3aSAndy Fiddaman 	int err;
6027e934d3aSAndy Fiddaman 
6037e934d3aSAndy Fiddaman 	if ((fp = fopen(EFI_FIXES_DB, "rF")) == NULL)
6047e934d3aSAndy Fiddaman 		return;
6057e934d3aSAndy Fiddaman 
6067e934d3aSAndy Fiddaman 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL) {
6077e934d3aSAndy Fiddaman 		if (efi_debug)
6087e934d3aSAndy Fiddaman 			(void) fprintf(stderr,
6097e934d3aSAndy Fiddaman 			    "libefi failed to load SMBIOS: %s\n",
6107e934d3aSAndy Fiddaman 			    smbios_errmsg(err));
6117e934d3aSAndy Fiddaman 		(void) fclose(fp);
6127e934d3aSAndy Fiddaman 		return;
6137e934d3aSAndy Fiddaman 	}
6147e934d3aSAndy Fiddaman 
6157e934d3aSAndy Fiddaman 	if (smbios_lookup_type(shp, SMB_TYPE_SYSTEM, &s_sys) == SMB_ERR ||
6167e934d3aSAndy Fiddaman 	    smbios_info_common(shp, s_sys.smbstr_id, &sys) == SMB_ERR)
6177e934d3aSAndy Fiddaman 		(void) memset(&sys, '\0', sizeof (sys));
6187e934d3aSAndy Fiddaman 	if (smbios_lookup_type(shp, SMB_TYPE_BASEBOARD, &s_mb) == SMB_ERR ||
6197e934d3aSAndy Fiddaman 	    smbios_info_common(shp, s_mb.smbstr_id, &mb) == SMB_ERR)
6207e934d3aSAndy Fiddaman 		(void) memset(&mb, '\0', sizeof (mb));
6217e934d3aSAndy Fiddaman 
6227e934d3aSAndy Fiddaman 	while (fgets(buf, sizeof (buf), fp) != NULL) {
6237e934d3aSAndy Fiddaman 		char *tok, *val, *end;
6247e934d3aSAndy Fiddaman 
6257e934d3aSAndy Fiddaman 		tok = buf + strspn(buf, " \t");
6267e934d3aSAndy Fiddaman 		if (*tok == '#')
6277e934d3aSAndy Fiddaman 			continue;
6287e934d3aSAndy Fiddaman 		while (*tok != '\0') {
6297e934d3aSAndy Fiddaman 			tok += strspn(tok, " \t");
6307e934d3aSAndy Fiddaman 			if ((val = strchr(tok, '=')) == NULL)
6317e934d3aSAndy Fiddaman 				break;
6327e934d3aSAndy Fiddaman 			*val++ = '\0';
6337e934d3aSAndy Fiddaman 			if (*val == '"')
6347e934d3aSAndy Fiddaman 				end = strchr(++val, '"');
6357e934d3aSAndy Fiddaman 			else
6367e934d3aSAndy Fiddaman 				end = strpbrk(val, " \t\n");
6377e934d3aSAndy Fiddaman 			if (end == NULL)
6387e934d3aSAndy Fiddaman 				break;
6397e934d3aSAndy Fiddaman 			*end++ = '\0';
6407e934d3aSAndy Fiddaman 
6417e934d3aSAndy Fiddaman 			if (strcmp(tok, "sys.manufacturer") == 0 &&
6427e934d3aSAndy Fiddaman 			    (sys.smbi_manufacturer == NULL ||
6437e934d3aSAndy Fiddaman 			    strcasecmp(val, sys.smbi_manufacturer)))
6447e934d3aSAndy Fiddaman 				break;
6457e934d3aSAndy Fiddaman 			if (strcmp(tok, "sys.product") == 0 &&
6467e934d3aSAndy Fiddaman 			    (sys.smbi_product == NULL ||
6477e934d3aSAndy Fiddaman 			    strcasecmp(val, sys.smbi_product)))
6487e934d3aSAndy Fiddaman 				break;
6497e934d3aSAndy Fiddaman 			if (strcmp(tok, "sys.version") == 0 &&
6507e934d3aSAndy Fiddaman 			    (sys.smbi_version == NULL ||
6517e934d3aSAndy Fiddaman 			    strcasecmp(val, sys.smbi_version)))
6527e934d3aSAndy Fiddaman 				break;
6537e934d3aSAndy Fiddaman 			if (strcmp(tok, "mb.manufacturer") == 0 &&
6547e934d3aSAndy Fiddaman 			    (mb.smbi_manufacturer == NULL ||
6557e934d3aSAndy Fiddaman 			    strcasecmp(val, mb.smbi_manufacturer)))
6567e934d3aSAndy Fiddaman 				break;
6577e934d3aSAndy Fiddaman 			if (strcmp(tok, "mb.product") == 0 &&
6587e934d3aSAndy Fiddaman 			    (mb.smbi_product == NULL ||
6597e934d3aSAndy Fiddaman 			    strcasecmp(val, mb.smbi_product)))
6607e934d3aSAndy Fiddaman 				break;
6617e934d3aSAndy Fiddaman 			if (strcmp(tok, "mb.version") == 0 &&
6627e934d3aSAndy Fiddaman 			    (mb.smbi_version == NULL ||
6637e934d3aSAndy Fiddaman 			    strcasecmp(val, mb.smbi_version)))
6647e934d3aSAndy Fiddaman 				break;
6657e934d3aSAndy Fiddaman 
6667e934d3aSAndy Fiddaman 			if (strcmp(tok, "pmbr_slot") == 0) {
6677e934d3aSAndy Fiddaman 				*slot = atoi(val);
6687e934d3aSAndy Fiddaman 				if (*slot < 0 || *slot > 3)
6697e934d3aSAndy Fiddaman 					*slot = 0;
6707e934d3aSAndy Fiddaman 				if (efi_debug)
6717e934d3aSAndy Fiddaman 					(void) fprintf(stderr,
6727e934d3aSAndy Fiddaman 					    "Using slot %d\n", *slot);
6737e934d3aSAndy Fiddaman 			}
6747e934d3aSAndy Fiddaman 
6757e934d3aSAndy Fiddaman 			if (strcmp(tok, "pmbr_active") == 0) {
6767e934d3aSAndy Fiddaman 				*active = atoi(val);
6777e934d3aSAndy Fiddaman 				if (*active < 0 || *active > 1)
6787e934d3aSAndy Fiddaman 					*active = 0;
6797e934d3aSAndy Fiddaman 				if (efi_debug)
6807e934d3aSAndy Fiddaman 					(void) fprintf(stderr,
6817e934d3aSAndy Fiddaman 					    "Using active %d\n", *active);
6827e934d3aSAndy Fiddaman 			}
6837e934d3aSAndy Fiddaman 
6847e934d3aSAndy Fiddaman 			tok = end;
6857e934d3aSAndy Fiddaman 		}
6867e934d3aSAndy Fiddaman 	}
6877e934d3aSAndy Fiddaman 	(void) fclose(fp);
6887e934d3aSAndy Fiddaman 	smbios_close(shp);
6897e934d3aSAndy Fiddaman }
6907e934d3aSAndy Fiddaman 
6917c478bd9Sstevel@tonic-gate /* writes a "protective" MBR */
6927c478bd9Sstevel@tonic-gate static int
6937c478bd9Sstevel@tonic-gate write_pmbr(int fd, struct dk_gpt *vtoc)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
6967c478bd9Sstevel@tonic-gate 	struct mboot	mb;
6977c478bd9Sstevel@tonic-gate 	uchar_t		*cp;
6987c478bd9Sstevel@tonic-gate 	diskaddr_t	size_in_lba;
69965908c77Syu, larry liu - Sun Microsystems - Beijing China 	uchar_t		*buf;
7007e934d3aSAndy Fiddaman 	int		len, slot, active;
7017e934d3aSAndy Fiddaman 
7027e934d3aSAndy Fiddaman 	slot = active = 0;
7037e934d3aSAndy Fiddaman 
7047e934d3aSAndy Fiddaman 	hardware_workarounds(&slot, &active);
70565908c77Syu, larry liu - Sun Microsystems - Beijing China 
70665908c77Syu, larry liu - Sun Microsystems - Beijing China 	len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
70765908c77Syu, larry liu - Sun Microsystems - Beijing China 	buf = calloc(len, 1);
7087c478bd9Sstevel@tonic-gate 
709987e90dcSxl 	/*
710987e90dcSxl 	 * Preserve any boot code and disk signature if the first block is
711987e90dcSxl 	 * already an MBR.
712987e90dcSxl 	 */
713987e90dcSxl 	dk_ioc.dki_lba = 0;
71465908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_length = len;
715987e90dcSxl 	/* LINTED -- always longlong aligned */
71665908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)buf;
71765908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
718286eb3edSRichard Yao 		(void) memcpy(&mb, buf, sizeof (mb));
719987e90dcSxl 		bzero(&mb, sizeof (mb));
720987e90dcSxl 		mb.signature = LE_16(MBB_MAGIC);
72165908c77Syu, larry liu - Sun Microsystems - Beijing China 	} else {
722286eb3edSRichard Yao 		(void) memcpy(&mb, buf, sizeof (mb));
72365908c77Syu, larry liu - Sun Microsystems - Beijing China 		if (mb.signature != LE_16(MBB_MAGIC)) {
72465908c77Syu, larry liu - Sun Microsystems - Beijing China 			bzero(&mb, sizeof (mb));
72565908c77Syu, larry liu - Sun Microsystems - Beijing China 			mb.signature = LE_16(MBB_MAGIC);
72665908c77Syu, larry liu - Sun Microsystems - Beijing China 		}
727987e90dcSxl 	}
72865908c77Syu, larry liu - Sun Microsystems - Beijing China 
7297c478bd9Sstevel@tonic-gate 	bzero(&mb.parts, sizeof (mb.parts));
7307e934d3aSAndy Fiddaman 	cp = (uchar_t *)&mb.parts[slot * sizeof (struct ipart)];
7317c478bd9Sstevel@tonic-gate 	/* bootable or not */
7327e934d3aSAndy Fiddaman 	*cp++ = active ? ACTIVE : NOTACTIVE;
7337c478bd9Sstevel@tonic-gate 	/* beginning CHS; 0xffffff if not representable */
7347c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
7357c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
7367c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
7377c478bd9Sstevel@tonic-gate 	/* OS type */
7387c478bd9Sstevel@tonic-gate 	*cp++ = EFI_PMBR;
7397c478bd9Sstevel@tonic-gate 	/* ending CHS; 0xffffff if not representable */
7407c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
7417c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
7427c478bd9Sstevel@tonic-gate 	*cp++ = 0xff;
7437c478bd9Sstevel@tonic-gate 	/* starting LBA: 1 (little endian format) by EFI definition */
7447c478bd9Sstevel@tonic-gate 	*cp++ = 0x01;
7457c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
7467c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
7477c478bd9Sstevel@tonic-gate 	*cp++ = 0x00;
7487c478bd9Sstevel@tonic-gate 	/* ending LBA: last block on the disk (little endian format) */
7497c478bd9Sstevel@tonic-gate 	size_in_lba = vtoc->efi_last_lba;
7507c478bd9Sstevel@tonic-gate 	if (size_in_lba < 0xffffffff) {
7517c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x000000ff);
7527c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
7537c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
7547c478bd9Sstevel@tonic-gate 		*cp++ = (size_in_lba & 0xff000000) >> 24;
7557c478bd9Sstevel@tonic-gate 	} else {
7567c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
7577c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
7587c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
7597c478bd9Sstevel@tonic-gate 		*cp++ = 0xff;
7607c478bd9Sstevel@tonic-gate 	}
76165908c77Syu, larry liu - Sun Microsystems - Beijing China 
762286eb3edSRichard Yao 	(void) memcpy(buf, &mb, sizeof (mb));
7637c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
76465908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)buf;
7657c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 0;
76665908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_length = len;
7677c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
76865908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(buf);
7697c478bd9Sstevel@tonic-gate 		switch (errno) {
7707c478bd9Sstevel@tonic-gate 		case EIO:
7717c478bd9Sstevel@tonic-gate 			return (VT_EIO);
7727c478bd9Sstevel@tonic-gate 		case EINVAL:
7737c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
7747c478bd9Sstevel@tonic-gate 		default:
7757c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
77865908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(buf);
7797c478bd9Sstevel@tonic-gate 	return (0);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /* make sure the user specified something reasonable */
7837c478bd9Sstevel@tonic-gate static int
7847c478bd9Sstevel@tonic-gate check_input(struct dk_gpt *vtoc)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	int			resv_part = -1;
7877c478bd9Sstevel@tonic-gate 	int			i, j;
7887c478bd9Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/*
7917c478bd9Sstevel@tonic-gate 	 * Sanity-check the input (make sure no partitions overlap)
7927c478bd9Sstevel@tonic-gate 	 */
7937c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
7947c478bd9Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
7957c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
7967c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
7977c478bd9Sstevel@tonic-gate 			if (efi_debug) {
7987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7997c478bd9Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size of %llu",
8007c478bd9Sstevel@tonic-gate 				    i,
8017c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
8027c478bd9Sstevel@tonic-gate 			}
8037c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
806f2be5148Sszhou 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
807f2be5148Sszhou 				continue;
808f2be5148Sszhou 			/* we have encountered an unknown uuid */
809f2be5148Sszhou 			vtoc->efi_parts[i].p_tag = 0xff;
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
8127c478bd9Sstevel@tonic-gate 			if (resv_part != -1) {
8137c478bd9Sstevel@tonic-gate 				if (efi_debug) {
814af007057Syl 					(void) fprintf(stderr,
8157c478bd9Sstevel@tonic-gate "found duplicate reserved partition at %d\n",
816af007057Syl 					    i);
8177c478bd9Sstevel@tonic-gate 				}
8187c478bd9Sstevel@tonic-gate 				return (VT_EINVAL);
8197c478bd9Sstevel@tonic-gate 			}
8207c478bd9Sstevel@tonic-gate 			resv_part = i;
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
8237c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
8247c478bd9Sstevel@tonic-gate 			if (efi_debug) {
8257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8267c478bd9Sstevel@tonic-gate 				    "Partition %d starts at %llu.  ",
8277c478bd9Sstevel@tonic-gate 				    i,
8287c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start);
8297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8307c478bd9Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
8317c478bd9Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
8327c478bd9Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
8337c478bd9Sstevel@tonic-gate 			}
8347c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
8377c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
8387c478bd9Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
8397c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
8407c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
8417c478bd9Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
8427c478bd9Sstevel@tonic-gate 			if (efi_debug) {
8437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8447c478bd9Sstevel@tonic-gate 				    "Partition %d ends at %llu.  ",
8457c478bd9Sstevel@tonic-gate 				    i,
8467c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_start +
8477c478bd9Sstevel@tonic-gate 				    vtoc->efi_parts[i].p_size);
8487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8497c478bd9Sstevel@tonic-gate 				    "It must be between %llu and %llu.\n",
8507c478bd9Sstevel@tonic-gate 				    vtoc->efi_first_u_lba,
8517c478bd9Sstevel@tonic-gate 				    vtoc->efi_last_u_lba);
8527c478bd9Sstevel@tonic-gate 			}
8537c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
8577c478bd9Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
8587c478bd9Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
8597c478bd9Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
8607c478bd9Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
8617c478bd9Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
8627c478bd9Sstevel@tonic-gate 				endsect = jstart + jsize -1;
8637c478bd9Sstevel@tonic-gate 				if ((jstart <= istart) &&
8647c478bd9Sstevel@tonic-gate 				    (istart <= endsect)) {
8657c478bd9Sstevel@tonic-gate 					if (efi_debug) {
8667c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
8677c478bd9Sstevel@tonic-gate "Partition %d overlaps partition %d.",
8687c478bd9Sstevel@tonic-gate 						    i, j);
869af007057Syl 					}
870af007057Syl 					return (VT_EINVAL);
8717c478bd9Sstevel@tonic-gate 				}
8727c478bd9Sstevel@tonic-gate 			}
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	/* just a warning for now */
8767c478bd9Sstevel@tonic-gate 	if ((resv_part == -1) && efi_debug) {
8777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
878af007057Syl 		    "no reserved partition found\n");
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	return (0);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
883af007057Syl /*
884af007057Syl  * add all the unallocated space to the current label
885af007057Syl  */
886af007057Syl int
887af007057Syl efi_use_whole_disk(int fd)
888af007057Syl {
889af007057Syl 	struct dk_gpt		*efi_label;
890af007057Syl 	int			rval;
891af007057Syl 	int			i;
892af007057Syl 	uint_t			phy_last_slice = 0;
893af007057Syl 	diskaddr_t		pl_start = 0;
894af007057Syl 	diskaddr_t		pl_size;
895af007057Syl 
896af007057Syl 	rval = efi_alloc_and_read(fd, &efi_label);
897af007057Syl 	if (rval < 0) {
898af007057Syl 		return (rval);
899af007057Syl 	}
900af007057Syl 
901af007057Syl 	/* find the last physically non-zero partition */
902af007057Syl 	for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
903af007057Syl 		if (pl_start < efi_label->efi_parts[i].p_start) {
904af007057Syl 			pl_start = efi_label->efi_parts[i].p_start;
905af007057Syl 			phy_last_slice = i;
906af007057Syl 		}
907af007057Syl 	}
908af007057Syl 	pl_size = efi_label->efi_parts[phy_last_slice].p_size;
909af007057Syl 
910af007057Syl 	/*
911af007057Syl 	 * If alter_lba is 1, we are using the backup label.
912af007057Syl 	 * Since we can locate the backup label by disk capacity,
913af007057Syl 	 * there must be no unallocated space.
914af007057Syl 	 */
915af007057Syl 	if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
916af007057Syl 	    >= efi_label->efi_last_lba)) {
917af007057Syl 		if (efi_debug) {
918af007057Syl 			(void) fprintf(stderr,
919af007057Syl 			    "efi_use_whole_disk: requested space not found\n");
920af007057Syl 		}
921af007057Syl 		efi_free(efi_label);
922af007057Syl 		return (VT_ENOSPC);
923af007057Syl 	}
924af007057Syl 
925af007057Syl 	/*
926af007057Syl 	 * If there is space between the last physically non-zero partition
927af007057Syl 	 * and the reserved partition, just add the unallocated space to this
928af007057Syl 	 * area. Otherwise, the unallocated space is added to the last
929af007057Syl 	 * physically non-zero partition.
930af007057Syl 	 */
931af007057Syl 	if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
932af007057Syl 	    EFI_MIN_RESV_SIZE) {
933af007057Syl 		efi_label->efi_parts[phy_last_slice].p_size +=
934af007057Syl 		    efi_label->efi_last_lba - efi_label->efi_altern_lba;
935af007057Syl 	}
936af007057Syl 
937af007057Syl 	/*
938af007057Syl 	 * Move the reserved partition. There is currently no data in
939af007057Syl 	 * here except fabricated devids (which get generated via
940af007057Syl 	 * efi_write()). So there is no need to copy data.
941af007057Syl 	 */
942af007057Syl 	efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
943af007057Syl 	    efi_label->efi_last_lba - efi_label->efi_altern_lba;
944af007057Syl 	efi_label->efi_last_u_lba += efi_label->efi_last_lba
945af007057Syl 	    - efi_label->efi_altern_lba;
946af007057Syl 
947af007057Syl 	rval = efi_write(fd, efi_label);
948af007057Syl 	if (rval < 0) {
949af007057Syl 		if (efi_debug) {
950af007057Syl 			(void) fprintf(stderr,
951af007057Syl 			    "efi_use_whole_disk:fail to write label, rval=%d\n",
952af007057Syl 			    rval);
953af007057Syl 		}
954af007057Syl 		efi_free(efi_label);
955af007057Syl 		return (rval);
956af007057Syl 	}
957af007057Syl 
958af007057Syl 	efi_free(efi_label);
959af007057Syl 	return (0);
960af007057Syl }
961af007057Syl 
962af007057Syl 
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate  * write EFI label and backup label
9657c478bd9Sstevel@tonic-gate  */
9667c478bd9Sstevel@tonic-gate int
9677c478bd9Sstevel@tonic-gate efi_write(int fd, struct dk_gpt *vtoc)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	dk_efi_t		dk_ioc;
9707c478bd9Sstevel@tonic-gate 	efi_gpt_t		*efi;
9717c478bd9Sstevel@tonic-gate 	efi_gpe_t		*efi_parts;
9727c478bd9Sstevel@tonic-gate 	int			i, j;
9737c478bd9Sstevel@tonic-gate 	struct dk_cinfo		dki_info;
97486bbaf93Scg 	int			nblocks;
97586bbaf93Scg 	diskaddr_t		lba_backup_gpt_hdr;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
9787c478bd9Sstevel@tonic-gate 		if (efi_debug)
9797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
9807c478bd9Sstevel@tonic-gate 		switch (errno) {
9817c478bd9Sstevel@tonic-gate 		case EIO:
9827c478bd9Sstevel@tonic-gate 			return (VT_EIO);
9837c478bd9Sstevel@tonic-gate 		case EINVAL:
9847c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
9857c478bd9Sstevel@tonic-gate 		default:
9867c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
9877c478bd9Sstevel@tonic-gate 		}
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9905f10ef69SYuri Pankov 	if (check_input(vtoc))
9915f10ef69SYuri Pankov 		return (VT_EINVAL);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
9947c478bd9Sstevel@tonic-gate 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
9957c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
9967c478bd9Sstevel@tonic-gate 	} else {
9977c478bd9Sstevel@tonic-gate 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
998af007057Syl 		    vtoc->efi_lbasize) *
999af007057Syl 		    vtoc->efi_lbasize;
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
100286bbaf93Scg 	/*
100386bbaf93Scg 	 * the number of blocks occupied by GUID partition entry array
100486bbaf93Scg 	 */
100586bbaf93Scg 	nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
100686bbaf93Scg 
100786bbaf93Scg 	/*
100886bbaf93Scg 	 * Backup GPT header is located on the block after GUID
100986bbaf93Scg 	 * partition entry array. Here, we calculate the address
101086bbaf93Scg 	 * for backup GPT header.
101186bbaf93Scg 	 */
101286bbaf93Scg 	lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
10137c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
10147c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	efi = dk_ioc.dki_data;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/* stuff user's input into EFI struct */
10197c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
10207c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
10217c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
10227c478bd9Sstevel@tonic-gate 	efi->efi_gpt_Reserved1 = 0;
10237c478bd9Sstevel@tonic-gate 	efi->efi_gpt_MyLBA = LE_64(1ULL);
102486bbaf93Scg 	efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
10257c478bd9Sstevel@tonic-gate 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
10267c478bd9Sstevel@tonic-gate 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
10277c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
10287c478bd9Sstevel@tonic-gate 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
10297c478bd9Sstevel@tonic-gate 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
10307c478bd9Sstevel@tonic-gate 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	/* LINTED -- always longlong aligned */
103365908c77Syu, larry liu - Sun Microsystems - Beijing China 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
1036af007057Syl 		for (j = 0;
1037af007057Syl 		    j < sizeof (conversion_array) /
1038af007057Syl 		    sizeof (struct uuid_to_ptag); j++) {
1039af007057Syl 
1040af007057Syl 			if (vtoc->efi_parts[i].p_tag == j) {
1041af007057Syl 				UUID_LE_CONVERT(
1042af007057Syl 				    efi_parts[i].efi_gpe_PartitionTypeGUID,
1043af007057Syl 				    conversion_array[j].uuid);
1044af007057Syl 				break;
1045af007057Syl 			}
1046af007057Syl 		}
1047af007057Syl 
1048af007057Syl 		if (j == sizeof (conversion_array) /
1049af007057Syl 		    sizeof (struct uuid_to_ptag)) {
1050af007057Syl 			/*
1051af007057Syl 			 * If we didn't have a matching uuid match, bail here.
1052af007057Syl 			 * Don't write a label with unknown uuid.
1053af007057Syl 			 */
1054af007057Syl 			if (efi_debug) {
1055af007057Syl 				(void) fprintf(stderr,
1056af007057Syl 				    "Unknown uuid for p_tag %d\n",
1057af007057Syl 				    vtoc->efi_parts[i].p_tag);
1058af007057Syl 			}
1059af007057Syl 			return (VT_EINVAL);
1060af007057Syl 		}
1061af007057Syl 
1062af007057Syl 		efi_parts[i].efi_gpe_StartingLBA =
1063af007057Syl 		    LE_64(vtoc->efi_parts[i].p_start);
1064af007057Syl 		efi_parts[i].efi_gpe_EndingLBA =
1065af007057Syl 		    LE_64(vtoc->efi_parts[i].p_start +
1066af007057Syl 		    vtoc->efi_parts[i].p_size - 1);
1067af007057Syl 		efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
10687c478bd9Sstevel@tonic-gate 		    LE_16(vtoc->efi_parts[i].p_flag);
1069af007057Syl 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1070af007057Syl 			efi_parts[i].efi_gpe_PartitionName[j] =
1071af007057Syl 			    LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1072af007057Syl 		}
1073af007057Syl 		if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1074af007057Syl 		    uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1075af007057Syl 			(void) uuid_generate((uchar_t *)
1076af007057Syl 			    &vtoc->efi_parts[i].p_uguid);
1077af007057Syl 		}
1078af007057Syl 		bcopy(&vtoc->efi_parts[i].p_uguid,
1079af007057Syl 		    &efi_parts[i].efi_gpe_UniquePartitionGUID,
1080af007057Syl 		    sizeof (uuid_t));
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryArrayCRC32 =
10837c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi_parts,
10847c478bd9Sstevel@tonic-gate 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
10857c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
10867c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
10897c478bd9Sstevel@tonic-gate 		free(dk_ioc.dki_data);
10907c478bd9Sstevel@tonic-gate 		switch (errno) {
10917c478bd9Sstevel@tonic-gate 		case EIO:
10927c478bd9Sstevel@tonic-gate 			return (VT_EIO);
10937c478bd9Sstevel@tonic-gate 		case EINVAL:
10947c478bd9Sstevel@tonic-gate 			return (VT_EINVAL);
10957c478bd9Sstevel@tonic-gate 		default:
10967c478bd9Sstevel@tonic-gate 			return (VT_ERROR);
10977c478bd9Sstevel@tonic-gate 		}
10987c478bd9Sstevel@tonic-gate 	}
109965908c77Syu, larry liu - Sun Microsystems - Beijing China 
11007c478bd9Sstevel@tonic-gate 	/* write backup partition array */
11017c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
11027c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length -= vtoc->efi_lbasize;
110365908c77Syu, larry liu - Sun Microsystems - Beijing China 	/* LINTED */
110465908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
110565908c77Syu, larry liu - Sun Microsystems - Beijing China 	    vtoc->efi_lbasize);
110665908c77Syu, larry liu - Sun Microsystems - Beijing China 
11077c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
11087c478bd9Sstevel@tonic-gate 		/*
11097c478bd9Sstevel@tonic-gate 		 * we wrote the primary label okay, so don't fail
11107c478bd9Sstevel@tonic-gate 		 */
11117c478bd9Sstevel@tonic-gate 		if (efi_debug) {
11127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11137c478bd9Sstevel@tonic-gate 			    "write of backup partitions to block %llu "
11147c478bd9Sstevel@tonic-gate 			    "failed, errno %d\n",
11157c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba + 1,
11167c478bd9Sstevel@tonic-gate 			    errno);
11177c478bd9Sstevel@tonic-gate 		}
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 	/*
11207c478bd9Sstevel@tonic-gate 	 * now swap MyLBA and AlternateLBA fields and write backup
11217c478bd9Sstevel@tonic-gate 	 * partition table header
11227c478bd9Sstevel@tonic-gate 	 */
112386bbaf93Scg 	dk_ioc.dki_lba = lba_backup_gpt_hdr;
11247c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length = vtoc->efi_lbasize;
112565908c77Syu, larry liu - Sun Microsystems - Beijing China 	/* LINTED */
112665908c77Syu, larry liu - Sun Microsystems - Beijing China 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
112765908c77Syu, larry liu - Sun Microsystems - Beijing China 	    vtoc->efi_lbasize);
11287c478bd9Sstevel@tonic-gate 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
112986bbaf93Scg 	efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
11307c478bd9Sstevel@tonic-gate 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
11317c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 = 0;
11327c478bd9Sstevel@tonic-gate 	efi->efi_gpt_HeaderCRC32 =
11337c478bd9Sstevel@tonic-gate 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
11347c478bd9Sstevel@tonic-gate 	    sizeof (struct efi_gpt)));
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
11377c478bd9Sstevel@tonic-gate 		if (efi_debug) {
11387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11397c478bd9Sstevel@tonic-gate 			    "write of backup header to block %llu failed, "
11407c478bd9Sstevel@tonic-gate 			    "errno %d\n",
114186bbaf93Scg 			    lba_backup_gpt_hdr,
11427c478bd9Sstevel@tonic-gate 			    errno);
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 	/* write the PMBR */
11467c478bd9Sstevel@tonic-gate 	(void) write_pmbr(fd, vtoc);
11477c478bd9Sstevel@tonic-gate 	free(dk_ioc.dki_data);
11487c478bd9Sstevel@tonic-gate 	return (0);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate void
11527c478bd9Sstevel@tonic-gate efi_free(struct dk_gpt *ptr)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	free(ptr);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate /*
11587c478bd9Sstevel@tonic-gate  * Input: File descriptor
1159342440ecSPrasad Singamsetty  * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1160342440ecSPrasad Singamsetty  * Otherwise 0.
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate int
11637c478bd9Sstevel@tonic-gate efi_type(int fd)
11647c478bd9Sstevel@tonic-gate {
11657c478bd9Sstevel@tonic-gate 	struct vtoc vtoc;
1166342440ecSPrasad Singamsetty 	struct extvtoc extvtoc;
11677c478bd9Sstevel@tonic-gate 
1168342440ecSPrasad Singamsetty 	if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1169342440ecSPrasad Singamsetty 		if (errno == ENOTSUP)
11707c478bd9Sstevel@tonic-gate 			return (1);
1171342440ecSPrasad Singamsetty 		else if (errno == ENOTTY) {
1172342440ecSPrasad Singamsetty 			if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1173342440ecSPrasad Singamsetty 				if (errno == ENOTSUP)
1174342440ecSPrasad Singamsetty 					return (1);
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	return (0);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate void
11817c478bd9Sstevel@tonic-gate efi_err_check(struct dk_gpt *vtoc)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	int			resv_part = -1;
11847c478bd9Sstevel@tonic-gate 	int			i, j;
11857c478bd9Sstevel@tonic-gate 	diskaddr_t		istart, jstart, isize, jsize, endsect;
11867c478bd9Sstevel@tonic-gate 	int			overlap = 0;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/*
11897c478bd9Sstevel@tonic-gate 	 * make sure no partitions overlap
11907c478bd9Sstevel@tonic-gate 	 */
11917c478bd9Sstevel@tonic-gate 	for (i = 0; i < vtoc->efi_nparts; i++) {
11927c478bd9Sstevel@tonic-gate 		/* It can't be unassigned and have an actual size */
11937c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
11947c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_size != 0)) {
11957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11967c478bd9Sstevel@tonic-gate 			    "partition %d is \"unassigned\" but has a size "
11977c478bd9Sstevel@tonic-gate 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
11987c478bd9Sstevel@tonic-gate 		}
11997c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
12007c478bd9Sstevel@tonic-gate 			continue;
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
12037c478bd9Sstevel@tonic-gate 			if (resv_part != -1) {
12047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12057c478bd9Sstevel@tonic-gate 				    "found duplicate reserved partition at "
12067c478bd9Sstevel@tonic-gate 				    "%d\n", i);
12077c478bd9Sstevel@tonic-gate 			}
12087c478bd9Sstevel@tonic-gate 			resv_part = i;
12097c478bd9Sstevel@tonic-gate 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
12107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12117c478bd9Sstevel@tonic-gate 				    "Warning: reserved partition size must "
12127c478bd9Sstevel@tonic-gate 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
12157c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
12167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12177c478bd9Sstevel@tonic-gate 			    "Partition %d starts at %llu\n",
12187c478bd9Sstevel@tonic-gate 			    i,
12197c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start);
12207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12217c478bd9Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
12227c478bd9Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
12237c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 		if ((vtoc->efi_parts[i].p_start +
12267c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size <
12277c478bd9Sstevel@tonic-gate 		    vtoc->efi_first_u_lba) ||
12287c478bd9Sstevel@tonic-gate 		    (vtoc->efi_parts[i].p_start +
12297c478bd9Sstevel@tonic-gate 		    vtoc->efi_parts[i].p_size >
12307c478bd9Sstevel@tonic-gate 		    vtoc->efi_last_u_lba + 1)) {
12317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12327c478bd9Sstevel@tonic-gate 			    "Partition %d ends at %llu\n",
12337c478bd9Sstevel@tonic-gate 			    i,
12347c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_start +
12357c478bd9Sstevel@tonic-gate 			    vtoc->efi_parts[i].p_size);
12367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12377c478bd9Sstevel@tonic-gate 			    "It must be between %llu and %llu.\n",
12387c478bd9Sstevel@tonic-gate 			    vtoc->efi_first_u_lba,
12397c478bd9Sstevel@tonic-gate 			    vtoc->efi_last_u_lba);
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		for (j = 0; j < vtoc->efi_nparts; j++) {
12437c478bd9Sstevel@tonic-gate 			isize = vtoc->efi_parts[i].p_size;
12447c478bd9Sstevel@tonic-gate 			jsize = vtoc->efi_parts[j].p_size;
12457c478bd9Sstevel@tonic-gate 			istart = vtoc->efi_parts[i].p_start;
12467c478bd9Sstevel@tonic-gate 			jstart = vtoc->efi_parts[j].p_start;
12477c478bd9Sstevel@tonic-gate 			if ((i != j) && (isize != 0) && (jsize != 0)) {
12487c478bd9Sstevel@tonic-gate 				endsect = jstart + jsize -1;
12497c478bd9Sstevel@tonic-gate 				if ((jstart <= istart) &&
12507c478bd9Sstevel@tonic-gate 				    (istart <= endsect)) {
12517c478bd9Sstevel@tonic-gate 					if (!overlap) {
12527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12537c478bd9Sstevel@tonic-gate 					    "label error: EFI Labels do not "
12547c478bd9Sstevel@tonic-gate 					    "support overlapping partitions\n");
12557c478bd9Sstevel@tonic-gate 					}
12567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12577c478bd9Sstevel@tonic-gate 					    "Partition %d overlaps partition "
12587c478bd9Sstevel@tonic-gate 					    "%d.\n", i, j);
12597c478bd9Sstevel@tonic-gate 					overlap = 1;
12607c478bd9Sstevel@tonic-gate 				}
12617c478bd9Sstevel@tonic-gate 			}
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 	/* make sure there is a reserved partition */
12657c478bd9Sstevel@tonic-gate 	if (resv_part == -1) {
12667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1267af007057Syl 		    "no reserved partition found\n");
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate /*
12727c478bd9Sstevel@tonic-gate  * We need to get information necessary to construct a *new* efi
12737c478bd9Sstevel@tonic-gate  * label type
12747c478bd9Sstevel@tonic-gate  */
12757c478bd9Sstevel@tonic-gate int
12767c478bd9Sstevel@tonic-gate efi_auto_sense(int fd, struct dk_gpt **vtoc)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	int	i;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
12827c478bd9Sstevel@tonic-gate 	 * Now build the default partition table
12837c478bd9Sstevel@tonic-gate 	 */
12847c478bd9Sstevel@tonic-gate 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
12857c478bd9Sstevel@tonic-gate 		if (efi_debug) {
12867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 		return (-1);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
12927c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
12937c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
12947c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_start = 0;
12957c478bd9Sstevel@tonic-gate 		(*vtoc)->efi_parts[i].p_size = 0;
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 	/*
12987c478bd9Sstevel@tonic-gate 	 * Make constants first
12997c478bd9Sstevel@tonic-gate 	 * and variable partitions later
13007c478bd9Sstevel@tonic-gate 	 */
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	/* root partition - s0 128 MB */
13037c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_start = 34;
13047c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[0].p_size = 262144;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/* partition - s1  128 MB */
13077c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_start = 262178;
13087c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[1].p_size = 262144;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	/* partition -s2 is NOT the Backup disk */
13117c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/* partition -s6 /usr partition - HOG */
13147c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_start = 524322;
13157c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
13167c478bd9Sstevel@tonic-gate 	    - (1024 * 16);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	/* efi reserved partition - s9 16K */
13197c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
13207c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
13217c478bd9Sstevel@tonic-gate 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
13227c478bd9Sstevel@tonic-gate 	return (0);
13237c478bd9Sstevel@tonic-gate }
1324