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.
27fd797736SJohn Levon * Copyright 2019 Joyent, Inc.
28*5db3bdb0SJason King * Copyright 2022 Jason King
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367e934d3aSAndy Fiddaman #include <smbios.h>
377c478bd9Sstevel@tonic-gate #include <uuid/uuid.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
39*5db3bdb0SJason King #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
427c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
437c478bd9Sstevel@tonic-gate #include <sys/mhd.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
467c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
477c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
487c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
497c478bd9Sstevel@tonic-gate
50d06952d0SToomas Soome /*
51d06952d0SToomas Soome * The original conversion array used simple array index, but since
52d06952d0SToomas Soome * we do need to take account of VTOC tag numbers from other systems,
53d06952d0SToomas Soome * we need to provide tag values too, or the array will grow too large.
54d06952d0SToomas Soome *
55d06952d0SToomas Soome * Still we will fabricate the missing p_tag values.
56d06952d0SToomas Soome */
577c478bd9Sstevel@tonic-gate static struct uuid_to_ptag {
587c478bd9Sstevel@tonic-gate struct uuid uuid;
59d06952d0SToomas Soome ushort_t p_tag;
607c478bd9Sstevel@tonic-gate } conversion_array[] = {
61d06952d0SToomas Soome { EFI_UNUSED, V_UNASSIGNED },
62d06952d0SToomas Soome { EFI_BOOT, V_BOOT },
63d06952d0SToomas Soome { EFI_ROOT, V_ROOT },
64d06952d0SToomas Soome { EFI_SWAP, V_SWAP },
65d06952d0SToomas Soome { EFI_USR, V_USR },
66d06952d0SToomas Soome { EFI_BACKUP, V_BACKUP },
67d06952d0SToomas Soome { EFI_VAR, V_VAR },
68d06952d0SToomas Soome { EFI_HOME, V_HOME },
69d06952d0SToomas Soome { EFI_ALTSCTR, V_ALTSCTR },
70d06952d0SToomas Soome { EFI_RESERVED, V_RESERVED },
71d06952d0SToomas Soome { EFI_SYSTEM, V_SYSTEM }, /* V_SYSTEM is 0xc */
72d06952d0SToomas Soome { EFI_LEGACY_MBR, 0x10 },
73d06952d0SToomas Soome { EFI_SYMC_PUB, 0x11 },
74d06952d0SToomas Soome { EFI_SYMC_CDS, 0x12 },
75d06952d0SToomas Soome { EFI_MSFT_RESV, 0x13 },
76d06952d0SToomas Soome { EFI_DELL_BASIC, 0x14 },
77d06952d0SToomas Soome { EFI_DELL_RAID, 0x15 },
78d06952d0SToomas Soome { EFI_DELL_SWAP, 0x16 },
79d06952d0SToomas Soome { EFI_DELL_LVM, 0x17 },
80d06952d0SToomas Soome { EFI_DELL_RESV, 0x19 },
81d06952d0SToomas Soome { EFI_AAPL_HFS, 0x1a },
82d06952d0SToomas Soome { EFI_AAPL_UFS, 0x1b },
83d06952d0SToomas Soome { EFI_AAPL_ZFS, 0x1c },
84d06952d0SToomas Soome { EFI_AAPL_APFS, 0x1d },
85d06952d0SToomas Soome { EFI_BIOS_BOOT, V_BIOS_BOOT }, /* V_BIOS_BOOT is 0x18 */
86d06952d0SToomas Soome { EFI_FREEBSD_BOOT, V_FREEBSD_BOOT },
87d06952d0SToomas Soome { EFI_FREEBSD_SWAP, V_FREEBSD_SWAP },
88d06952d0SToomas Soome { EFI_FREEBSD_UFS, V_FREEBSD_UFS },
89d06952d0SToomas Soome { EFI_FREEBSD_VINUM, V_FREEBSD_VINUM },
90d06952d0SToomas Soome { EFI_FREEBSD_ZFS, V_FREEBSD_ZFS },
91d06952d0SToomas Soome { EFI_FREEBSD_NANDFS, V_FREEBSD_NANDFS }
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * Default vtoc information for non-SVr4 partitions
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate struct dk_map2 default_vtoc_map[NDKMAP] = {
987c478bd9Sstevel@tonic-gate { V_ROOT, 0 }, /* a - 0 */
997c478bd9Sstevel@tonic-gate { V_SWAP, V_UNMNT }, /* b - 1 */
1007c478bd9Sstevel@tonic-gate { V_BACKUP, V_UNMNT }, /* c - 2 */
1017c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* d - 3 */
1027c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* e - 4 */
1037c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* f - 5 */
1047c478bd9Sstevel@tonic-gate { V_USR, 0 }, /* g - 6 */
1057c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* h - 7 */
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
1107c478bd9Sstevel@tonic-gate { V_BOOT, V_UNMNT }, /* i - 8 */
1117c478bd9Sstevel@tonic-gate { V_ALTSCTR, 0 }, /* j - 9 */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate #else
1147c478bd9Sstevel@tonic-gate #error No VTOC format defined.
1157c478bd9Sstevel@tonic-gate #endif /* defined(i386) */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* k - 10 */
1187c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* l - 11 */
1197c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* m - 12 */
1207c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* n - 13 */
1217c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* o - 14 */
1227c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* p - 15 */
1237c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate #ifdef DEBUG
1277c478bd9Sstevel@tonic-gate int efi_debug = 1;
1287c478bd9Sstevel@tonic-gate #else
1297c478bd9Sstevel@tonic-gate int efi_debug = 0;
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate
1327e934d3aSAndy Fiddaman #define EFI_FIXES_DB "/usr/share/hwdata/efi.fixes"
1337e934d3aSAndy Fiddaman
1347c478bd9Sstevel@tonic-gate extern unsigned int efi_crc32(const unsigned char *, unsigned int);
1357c478bd9Sstevel@tonic-gate static int efi_read(int, struct dk_gpt *);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate static int
read_disk_info(int fd,diskaddr_t * capacity,uint_t * lbsize)1387c478bd9Sstevel@tonic-gate read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate struct dk_minfo disk_info;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
1437c478bd9Sstevel@tonic-gate return (errno);
1447c478bd9Sstevel@tonic-gate *capacity = disk_info.dki_capacity;
1457c478bd9Sstevel@tonic-gate *lbsize = disk_info.dki_lbsize;
1467c478bd9Sstevel@tonic-gate return (0);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * the number of blocks the EFI label takes up (round up to nearest
1517c478bd9Sstevel@tonic-gate * block)
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
1547c478bd9Sstevel@tonic-gate ((l) - 1)) / (l)))
1557c478bd9Sstevel@tonic-gate /* number of partitions -- limited by what we can malloc */
1567c478bd9Sstevel@tonic-gate #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
1577c478bd9Sstevel@tonic-gate sizeof (struct dk_part))
1587c478bd9Sstevel@tonic-gate
15962ce8e2eSToomas Soome /*
16062ce8e2eSToomas Soome * The EFI reserved partition size is 8 MiB. This calculates the number of
16162ce8e2eSToomas Soome * sectors required to store 8 MiB, taking into account the device's sector
16262ce8e2eSToomas Soome * size.
16362ce8e2eSToomas Soome */
16462ce8e2eSToomas Soome uint_t
efi_reserved_sectors(dk_gpt_t * efi)16562ce8e2eSToomas Soome efi_reserved_sectors(dk_gpt_t *efi)
16662ce8e2eSToomas Soome {
16762ce8e2eSToomas Soome /* roundup to sector size */
16862ce8e2eSToomas Soome return ((EFI_MIN_RESV_SIZE * DEV_BSIZE + efi->efi_lbasize - 1) /
16962ce8e2eSToomas Soome efi->efi_lbasize);
17062ce8e2eSToomas Soome }
17162ce8e2eSToomas Soome
1727c478bd9Sstevel@tonic-gate int
efi_alloc_and_init(int fd,uint32_t nparts,struct dk_gpt ** vtoc)1737c478bd9Sstevel@tonic-gate efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate diskaddr_t capacity;
1767c478bd9Sstevel@tonic-gate uint_t lbsize;
1777c478bd9Sstevel@tonic-gate uint_t nblocks;
1787c478bd9Sstevel@tonic-gate size_t length;
1797c478bd9Sstevel@tonic-gate struct dk_gpt *vptr;
1807c478bd9Sstevel@tonic-gate struct uuid uuid;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate if (read_disk_info(fd, &capacity, &lbsize) != 0) {
1837c478bd9Sstevel@tonic-gate if (efi_debug)
1847c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1857c478bd9Sstevel@tonic-gate "couldn't read disk information\n");
1867c478bd9Sstevel@tonic-gate return (-1);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate nblocks = NBLOCKS(nparts, lbsize);
1907c478bd9Sstevel@tonic-gate if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
1917c478bd9Sstevel@tonic-gate /* 16K plus one block for the GPT */
1927c478bd9Sstevel@tonic-gate nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if (nparts > MAX_PARTS) {
1967c478bd9Sstevel@tonic-gate if (efi_debug) {
1977c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1987c478bd9Sstevel@tonic-gate "the maximum number of partitions supported is %lu\n",
1997c478bd9Sstevel@tonic-gate MAX_PARTS);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate return (-1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate length = sizeof (struct dk_gpt) +
2057c478bd9Sstevel@tonic-gate sizeof (struct dk_part) * (nparts - 1);
2067c478bd9Sstevel@tonic-gate
2071b58875aSJohn Levon if ((*vtoc = calloc(1, length)) == NULL)
2087c478bd9Sstevel@tonic-gate return (-1);
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate vptr = *vtoc;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate vptr->efi_version = EFI_VERSION_CURRENT;
2137c478bd9Sstevel@tonic-gate vptr->efi_lbasize = lbsize;
2147c478bd9Sstevel@tonic-gate vptr->efi_nparts = nparts;
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate * add one block here for the PMBR; on disks with a 512 byte
2177c478bd9Sstevel@tonic-gate * block size and 128 or fewer partitions, efi_first_u_lba
2187c478bd9Sstevel@tonic-gate * should work out to "34"
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate vptr->efi_first_u_lba = nblocks + 1;
2217c478bd9Sstevel@tonic-gate vptr->efi_last_lba = capacity - 1;
222*5db3bdb0SJason King vptr->efi_altern_lba = capacity - 1;
2237c478bd9Sstevel@tonic-gate vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
22465908c77Syu, larry liu - Sun Microsystems - Beijing China
2257c478bd9Sstevel@tonic-gate (void) uuid_generate((uchar_t *)&uuid);
2267c478bd9Sstevel@tonic-gate UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
2277c478bd9Sstevel@tonic-gate return (0);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * Read EFI - return partition number upon success.
2327c478bd9Sstevel@tonic-gate */
2337c478bd9Sstevel@tonic-gate int
efi_alloc_and_read(int fd,struct dk_gpt ** vtoc)2347c478bd9Sstevel@tonic-gate efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate int rval;
2377c478bd9Sstevel@tonic-gate uint32_t nparts;
2387c478bd9Sstevel@tonic-gate int length;
239fe12dc75SToomas Soome struct mboot *mbr;
240fe12dc75SToomas Soome struct ipart *ipart;
241fe12dc75SToomas Soome diskaddr_t capacity;
242fe12dc75SToomas Soome uint_t lbsize;
243fe12dc75SToomas Soome int i;
244fe12dc75SToomas Soome
245fe12dc75SToomas Soome if (read_disk_info(fd, &capacity, &lbsize) != 0)
246fe12dc75SToomas Soome return (VT_ERROR);
247fe12dc75SToomas Soome
2481b58875aSJohn Levon if ((mbr = calloc(1, lbsize)) == NULL)
249fe12dc75SToomas Soome return (VT_ERROR);
250fe12dc75SToomas Soome
251fe12dc75SToomas Soome if ((ioctl(fd, DKIOCGMBOOT, (caddr_t)mbr)) == -1) {
252fe12dc75SToomas Soome free(mbr);
253fe12dc75SToomas Soome return (VT_ERROR);
254fe12dc75SToomas Soome }
255fe12dc75SToomas Soome
256fe12dc75SToomas Soome if (mbr->signature != MBB_MAGIC) {
257fe12dc75SToomas Soome free(mbr);
258fe12dc75SToomas Soome return (VT_EINVAL);
259fe12dc75SToomas Soome }
260fe12dc75SToomas Soome ipart = (struct ipart *)(uintptr_t)mbr->parts;
261fe12dc75SToomas Soome
262fe12dc75SToomas Soome /* Check if we have partition with ID EFI_PMBR */
263fe12dc75SToomas Soome for (i = 0; i < FD_NUMPART; i++) {
264fe12dc75SToomas Soome if (ipart[i].systid == EFI_PMBR)
265fe12dc75SToomas Soome break;
266fe12dc75SToomas Soome }
267fe12dc75SToomas Soome free(mbr);
268fe12dc75SToomas Soome if (i == FD_NUMPART)
269fe12dc75SToomas Soome return (VT_EINVAL);
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate /* figure out the number of entries that would fit into 16K */
2727c478bd9Sstevel@tonic-gate nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
2737c478bd9Sstevel@tonic-gate length = (int) sizeof (struct dk_gpt) +
274af007057Syl (int) sizeof (struct dk_part) * (nparts - 1);
2751b58875aSJohn Levon if ((*vtoc = calloc(1, length)) == NULL)
2767c478bd9Sstevel@tonic-gate return (VT_ERROR);
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate (*vtoc)->efi_nparts = nparts;
2797c478bd9Sstevel@tonic-gate rval = efi_read(fd, *vtoc);
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
2827c478bd9Sstevel@tonic-gate void *tmp;
2837c478bd9Sstevel@tonic-gate length = (int) sizeof (struct dk_gpt) +
284af007057Syl (int) sizeof (struct dk_part) *
285af007057Syl ((*vtoc)->efi_nparts - 1);
2867c478bd9Sstevel@tonic-gate nparts = (*vtoc)->efi_nparts;
2877c478bd9Sstevel@tonic-gate if ((tmp = realloc(*vtoc, length)) == NULL) {
2887c478bd9Sstevel@tonic-gate free (*vtoc);
2897c478bd9Sstevel@tonic-gate *vtoc = NULL;
2907c478bd9Sstevel@tonic-gate return (VT_ERROR);
2917c478bd9Sstevel@tonic-gate } else {
2927c478bd9Sstevel@tonic-gate *vtoc = tmp;
2937c478bd9Sstevel@tonic-gate rval = efi_read(fd, *vtoc);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate if (rval < 0) {
2987c478bd9Sstevel@tonic-gate if (efi_debug) {
2997c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3007c478bd9Sstevel@tonic-gate "read of EFI table failed, rval=%d\n", rval);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate free (*vtoc);
3037c478bd9Sstevel@tonic-gate *vtoc = NULL;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate return (rval);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate static int
efi_ioctl(int fd,int cmd,dk_efi_t * dk_ioc)3107c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate void *data = dk_ioc->dki_data;
3137c478bd9Sstevel@tonic-gate int error;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
3167c478bd9Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc);
3177c478bd9Sstevel@tonic-gate dk_ioc->dki_data = data;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate return (error);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate static int
check_label(int fd,dk_efi_t * dk_ioc)3237c478bd9Sstevel@tonic-gate check_label(int fd, dk_efi_t *dk_ioc)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate efi_gpt_t *efi;
3267c478bd9Sstevel@tonic-gate uint_t crc;
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
3297c478bd9Sstevel@tonic-gate switch (errno) {
3307c478bd9Sstevel@tonic-gate case EIO:
3317c478bd9Sstevel@tonic-gate return (VT_EIO);
3327c478bd9Sstevel@tonic-gate default:
3337c478bd9Sstevel@tonic-gate return (VT_ERROR);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate efi = dk_ioc->dki_data;
3377c478bd9Sstevel@tonic-gate if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
3387c478bd9Sstevel@tonic-gate if (efi_debug)
3397c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3407c478bd9Sstevel@tonic-gate "Bad EFI signature: 0x%llx != 0x%llx\n",
3417c478bd9Sstevel@tonic-gate (long long)efi->efi_gpt_Signature,
3427c478bd9Sstevel@tonic-gate (long long)LE_64(EFI_SIGNATURE));
3437c478bd9Sstevel@tonic-gate return (VT_EINVAL);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * check CRC of the header; the size of the header should
3487c478bd9Sstevel@tonic-gate * never be larger than one block
3497c478bd9Sstevel@tonic-gate */
3507c478bd9Sstevel@tonic-gate crc = efi->efi_gpt_HeaderCRC32;
3517c478bd9Sstevel@tonic-gate efi->efi_gpt_HeaderCRC32 = 0;
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
3547c478bd9Sstevel@tonic-gate crc != LE_32(efi_crc32((unsigned char *)efi,
3557c478bd9Sstevel@tonic-gate LE_32(efi->efi_gpt_HeaderSize)))) {
3567c478bd9Sstevel@tonic-gate if (efi_debug)
3577c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
358af007057Syl "Bad EFI CRC: 0x%x != 0x%x\n",
359fd797736SJohn Levon crc, LE_32(efi_crc32((unsigned char *)efi,
360fd797736SJohn Levon LE_32(efi->efi_gpt_HeaderSize))));
3617c478bd9Sstevel@tonic-gate return (VT_EINVAL);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate return (0);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate static int
efi_read(int fd,struct dk_gpt * vtoc)3687c478bd9Sstevel@tonic-gate efi_read(int fd, struct dk_gpt *vtoc)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate int i, j;
3717c478bd9Sstevel@tonic-gate int label_len;
3727c478bd9Sstevel@tonic-gate int rval = 0;
373d84f0041SAlexandre Chartre int vdc_flag = 0;
3747c478bd9Sstevel@tonic-gate struct dk_minfo disk_info;
3757c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc;
3767c478bd9Sstevel@tonic-gate efi_gpt_t *efi;
3777c478bd9Sstevel@tonic-gate efi_gpe_t *efi_parts;
3787c478bd9Sstevel@tonic-gate struct dk_cinfo dki_info;
3797c478bd9Sstevel@tonic-gate uint32_t user_length;
38086bbaf93Scg boolean_t legacy_label = B_FALSE;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * get the partition number for this file descriptor.
3847c478bd9Sstevel@tonic-gate */
3857c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
386af007057Syl if (efi_debug) {
387af007057Syl (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
388af007057Syl }
3897c478bd9Sstevel@tonic-gate switch (errno) {
3907c478bd9Sstevel@tonic-gate case EIO:
3917c478bd9Sstevel@tonic-gate return (VT_EIO);
3927c478bd9Sstevel@tonic-gate case EINVAL:
3937c478bd9Sstevel@tonic-gate return (VT_EINVAL);
3947c478bd9Sstevel@tonic-gate default:
3957c478bd9Sstevel@tonic-gate return (VT_ERROR);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate }
3985f10ef69SYuri Pankov
3995f10ef69SYuri Pankov if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
400d84f0041SAlexandre Chartre (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
401d84f0041SAlexandre Chartre /*
402d84f0041SAlexandre Chartre * The controller and drive name "vdc" (virtual disk client)
403d84f0041SAlexandre Chartre * indicates a LDoms virtual disk.
404d84f0041SAlexandre Chartre */
405d84f0041SAlexandre Chartre vdc_flag++;
4067c478bd9Sstevel@tonic-gate }
407d84f0041SAlexandre Chartre
4087c478bd9Sstevel@tonic-gate /* get the LBA size */
4097c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
4107c478bd9Sstevel@tonic-gate if (efi_debug) {
4117c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4127c478bd9Sstevel@tonic-gate "assuming LBA 512 bytes %d\n",
4137c478bd9Sstevel@tonic-gate errno);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate disk_info.dki_lbsize = DEV_BSIZE;
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate if (disk_info.dki_lbsize == 0) {
4187c478bd9Sstevel@tonic-gate if (efi_debug) {
4197c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4207c478bd9Sstevel@tonic-gate "efi_read: assuming LBA 512 bytes\n");
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate disk_info.dki_lbsize = DEV_BSIZE;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate * Read the EFI GPT to figure out how many partitions we need
4267c478bd9Sstevel@tonic-gate * to deal with.
4277c478bd9Sstevel@tonic-gate */
4287c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 1;
4297c478bd9Sstevel@tonic-gate if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
4307c478bd9Sstevel@tonic-gate label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
4317c478bd9Sstevel@tonic-gate } else {
4327c478bd9Sstevel@tonic-gate label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
433af007057Syl disk_info.dki_lbsize;
4347c478bd9Sstevel@tonic-gate if (label_len % disk_info.dki_lbsize) {
4357c478bd9Sstevel@tonic-gate /* pad to physical sector size */
4367c478bd9Sstevel@tonic-gate label_len += disk_info.dki_lbsize;
4377c478bd9Sstevel@tonic-gate label_len &= ~(disk_info.dki_lbsize - 1);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
4411b58875aSJohn Levon if ((dk_ioc.dki_data = calloc(1, label_len)) == NULL)
4427c478bd9Sstevel@tonic-gate return (VT_ERROR);
4437c478bd9Sstevel@tonic-gate
444c7e1889fSyl dk_ioc.dki_length = disk_info.dki_lbsize;
4457c478bd9Sstevel@tonic-gate user_length = vtoc->efi_nparts;
4467c478bd9Sstevel@tonic-gate efi = dk_ioc.dki_data;
4475f10ef69SYuri Pankov if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
44886bbaf93Scg /*
44986bbaf93Scg * No valid label here; try the alternate. Note that here
45086bbaf93Scg * we just read GPT header and save it into dk_ioc.data,
45186bbaf93Scg * Later, we will read GUID partition entry array if we
45286bbaf93Scg * can get valid GPT header.
45386bbaf93Scg */
45486bbaf93Scg
45586bbaf93Scg /*
45686bbaf93Scg * This is a workaround for legacy systems. In the past, the
45786bbaf93Scg * last sector of SCSI disk was invisible on x86 platform. At
45886bbaf93Scg * that time, backup label was saved on the next to the last
45986bbaf93Scg * sector. It is possible for users to move a disk from previous
46086bbaf93Scg * solaris system to present system. Here, we attempt to search
46186bbaf93Scg * legacy backup EFI label first.
46286bbaf93Scg */
46386bbaf93Scg dk_ioc.dki_lba = disk_info.dki_capacity - 2;
4647c478bd9Sstevel@tonic-gate dk_ioc.dki_length = disk_info.dki_lbsize;
4654f0dea16Scg rval = check_label(fd, &dk_ioc);
46686bbaf93Scg if (rval == VT_EINVAL) {
4674f0dea16Scg /*
46886bbaf93Scg * we didn't find legacy backup EFI label, try to
46986bbaf93Scg * search backup EFI label in the last block.
4704f0dea16Scg */
47186bbaf93Scg dk_ioc.dki_lba = disk_info.dki_capacity - 1;
4724f0dea16Scg dk_ioc.dki_length = disk_info.dki_lbsize;
4734f0dea16Scg rval = check_label(fd, &dk_ioc);
47486bbaf93Scg if (rval == 0) {
47586bbaf93Scg legacy_label = B_TRUE;
47686bbaf93Scg if (efi_debug)
47786bbaf93Scg (void) fprintf(stderr,
47886bbaf93Scg "efi_read: primary label corrupt; "
47986bbaf93Scg "using EFI backup label located on"
48086bbaf93Scg " the last block\n");
4814f0dea16Scg }
48286bbaf93Scg } else {
48386bbaf93Scg if ((efi_debug) && (rval == 0))
48486bbaf93Scg (void) fprintf(stderr, "efi_read: primary label"
48586bbaf93Scg " corrupt; using legacy EFI backup label "
48686bbaf93Scg " located on the next to last block\n");
4874f0dea16Scg }
4884f0dea16Scg
4894f0dea16Scg if (rval == 0) {
4907c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4917c478bd9Sstevel@tonic-gate vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
4927c478bd9Sstevel@tonic-gate vtoc->efi_nparts =
4937c478bd9Sstevel@tonic-gate LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4947c478bd9Sstevel@tonic-gate /*
49586bbaf93Scg * Partition tables are between backup GPT header
49686bbaf93Scg * table and ParitionEntryLBA (the starting LBA of
49786bbaf93Scg * the GUID partition entries array). Now that we
49886bbaf93Scg * already got valid GPT header and saved it in
49986bbaf93Scg * dk_ioc.dki_data, we try to get GUID partition
50086bbaf93Scg * entry array here.
5017c478bd9Sstevel@tonic-gate */
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);
50586bbaf93Scg if (legacy_label)
50686bbaf93Scg dk_ioc.dki_length = disk_info.dki_capacity - 1 -
507af007057Syl dk_ioc.dki_lba;
50886bbaf93Scg else
50986bbaf93Scg dk_ioc.dki_length = disk_info.dki_capacity - 2 -
510af007057Syl dk_ioc.dki_lba;
5117c478bd9Sstevel@tonic-gate dk_ioc.dki_length *= disk_info.dki_lbsize;
51286bbaf93Scg if (dk_ioc.dki_length >
51386bbaf93Scg ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
5147c478bd9Sstevel@tonic-gate rval = VT_EINVAL;
5157c478bd9Sstevel@tonic-gate } else {
51686bbaf93Scg /*
51786bbaf93Scg * read GUID partition entry array
51886bbaf93Scg */
5197c478bd9Sstevel@tonic-gate rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate }
522d84f0041SAlexandre Chartre
523d84f0041SAlexandre Chartre } else if (rval == 0) {
524d84f0041SAlexandre Chartre
525c7e1889fSyl dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
52665908c77Syu, larry liu - Sun Microsystems - Beijing China /* LINTED */
52765908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
52865908c77Syu, larry liu - Sun Microsystems - Beijing China + disk_info.dki_lbsize);
529c7e1889fSyl dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
530c7e1889fSyl rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
531d84f0041SAlexandre Chartre
532d84f0041SAlexandre Chartre } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
533d84f0041SAlexandre Chartre /*
534d84f0041SAlexandre Chartre * When the device is a LDoms virtual disk, the DKIOCGETEFI
535d84f0041SAlexandre Chartre * ioctl can fail with EINVAL if the virtual disk backend
536d84f0041SAlexandre Chartre * is a ZFS volume serviced by a domain running an old version
537d84f0041SAlexandre Chartre * of Solaris. This is because the DKIOCGETEFI ioctl was
538d84f0041SAlexandre Chartre * initially incorrectly implemented for a ZFS volume and it
539d84f0041SAlexandre Chartre * expected the GPT and GPE to be retrieved with a single ioctl.
540d84f0041SAlexandre Chartre * So we try to read the GPT and the GPE using that old style
541d84f0041SAlexandre Chartre * ioctl.
542d84f0041SAlexandre Chartre */
543d84f0041SAlexandre Chartre dk_ioc.dki_lba = 1;
544d84f0041SAlexandre Chartre dk_ioc.dki_length = label_len;
545d84f0041SAlexandre Chartre rval = check_label(fd, &dk_ioc);
5467c478bd9Sstevel@tonic-gate }
547d84f0041SAlexandre Chartre
5487c478bd9Sstevel@tonic-gate if (rval < 0) {
5497c478bd9Sstevel@tonic-gate free(efi);
5507c478bd9Sstevel@tonic-gate return (rval);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate /* LINTED -- always longlong aligned */
554c7e1889fSyl efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate * Assemble this into a "dk_gpt" struct for easier
5587c478bd9Sstevel@tonic-gate * digestibility by applications.
5597c478bd9Sstevel@tonic-gate */
5607c478bd9Sstevel@tonic-gate vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
5617c478bd9Sstevel@tonic-gate vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
5627c478bd9Sstevel@tonic-gate vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
5637c478bd9Sstevel@tonic-gate vtoc->efi_lbasize = disk_info.dki_lbsize;
5647c478bd9Sstevel@tonic-gate vtoc->efi_last_lba = disk_info.dki_capacity - 1;
5657c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
5667c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
567af007057Syl vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
5687c478bd9Sstevel@tonic-gate UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate * If the array the user passed in is too small, set the length
5727c478bd9Sstevel@tonic-gate * to what it needs to be and return
5737c478bd9Sstevel@tonic-gate */
5747c478bd9Sstevel@tonic-gate if (user_length < vtoc->efi_nparts) {
5757c478bd9Sstevel@tonic-gate return (VT_EINVAL);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate for (i = 0; i < vtoc->efi_nparts; i++) {
5797c478bd9Sstevel@tonic-gate
580af007057Syl UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
581af007057Syl efi_parts[i].efi_gpe_PartitionTypeGUID);
582af007057Syl
583af007057Syl for (j = 0;
584af007057Syl j < sizeof (conversion_array)
585af007057Syl / sizeof (struct uuid_to_ptag); j++) {
586af007057Syl
587af007057Syl if (bcmp(&vtoc->efi_parts[i].p_guid,
588af007057Syl &conversion_array[j].uuid,
589af007057Syl sizeof (struct uuid)) == 0) {
590d06952d0SToomas Soome vtoc->efi_parts[i].p_tag =
591d06952d0SToomas Soome conversion_array[j].p_tag;
592af007057Syl break;
593af007057Syl }
594af007057Syl }
595af007057Syl if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
596af007057Syl continue;
597af007057Syl vtoc->efi_parts[i].p_flag =
598af007057Syl LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
599af007057Syl vtoc->efi_parts[i].p_start =
600af007057Syl LE_64(efi_parts[i].efi_gpe_StartingLBA);
601af007057Syl vtoc->efi_parts[i].p_size =
602af007057Syl LE_64(efi_parts[i].efi_gpe_EndingLBA) -
6037c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start + 1;
604af007057Syl for (j = 0; j < EFI_PART_NAME_LEN; j++) {
605af007057Syl vtoc->efi_parts[i].p_name[j] =
606af007057Syl (uchar_t)LE_16(
607af007057Syl efi_parts[i].efi_gpe_PartitionName[j]);
608af007057Syl }
6097c478bd9Sstevel@tonic-gate
610af007057Syl UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
611af007057Syl efi_parts[i].efi_gpe_UniquePartitionGUID);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate free(efi);
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate return (dki_info.dki_partition);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate
6187e934d3aSAndy Fiddaman static void
hardware_workarounds(int * slot,int * active)6197e934d3aSAndy Fiddaman hardware_workarounds(int *slot, int *active)
6207e934d3aSAndy Fiddaman {
6217e934d3aSAndy Fiddaman smbios_struct_t s_sys, s_mb;
6227e934d3aSAndy Fiddaman smbios_info_t sys, mb;
6237e934d3aSAndy Fiddaman smbios_hdl_t *shp;
6247e934d3aSAndy Fiddaman char buf[0x400];
6257e934d3aSAndy Fiddaman FILE *fp;
6267e934d3aSAndy Fiddaman int err;
6277e934d3aSAndy Fiddaman
6287e934d3aSAndy Fiddaman if ((fp = fopen(EFI_FIXES_DB, "rF")) == NULL)
6297e934d3aSAndy Fiddaman return;
6307e934d3aSAndy Fiddaman
6317e934d3aSAndy Fiddaman if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL) {
6327e934d3aSAndy Fiddaman if (efi_debug)
6337e934d3aSAndy Fiddaman (void) fprintf(stderr,
6347e934d3aSAndy Fiddaman "libefi failed to load SMBIOS: %s\n",
6357e934d3aSAndy Fiddaman smbios_errmsg(err));
6367e934d3aSAndy Fiddaman (void) fclose(fp);
6377e934d3aSAndy Fiddaman return;
6387e934d3aSAndy Fiddaman }
6397e934d3aSAndy Fiddaman
6407e934d3aSAndy Fiddaman if (smbios_lookup_type(shp, SMB_TYPE_SYSTEM, &s_sys) == SMB_ERR ||
6417e934d3aSAndy Fiddaman smbios_info_common(shp, s_sys.smbstr_id, &sys) == SMB_ERR)
6427e934d3aSAndy Fiddaman (void) memset(&sys, '\0', sizeof (sys));
6437e934d3aSAndy Fiddaman if (smbios_lookup_type(shp, SMB_TYPE_BASEBOARD, &s_mb) == SMB_ERR ||
6447e934d3aSAndy Fiddaman smbios_info_common(shp, s_mb.smbstr_id, &mb) == SMB_ERR)
6457e934d3aSAndy Fiddaman (void) memset(&mb, '\0', sizeof (mb));
6467e934d3aSAndy Fiddaman
6477e934d3aSAndy Fiddaman while (fgets(buf, sizeof (buf), fp) != NULL) {
6487e934d3aSAndy Fiddaman char *tok, *val, *end;
6497e934d3aSAndy Fiddaman
6507e934d3aSAndy Fiddaman tok = buf + strspn(buf, " \t");
6517e934d3aSAndy Fiddaman if (*tok == '#')
6527e934d3aSAndy Fiddaman continue;
6537e934d3aSAndy Fiddaman while (*tok != '\0') {
6547e934d3aSAndy Fiddaman tok += strspn(tok, " \t");
6557e934d3aSAndy Fiddaman if ((val = strchr(tok, '=')) == NULL)
6567e934d3aSAndy Fiddaman break;
6577e934d3aSAndy Fiddaman *val++ = '\0';
6587e934d3aSAndy Fiddaman if (*val == '"')
6597e934d3aSAndy Fiddaman end = strchr(++val, '"');
6607e934d3aSAndy Fiddaman else
6617e934d3aSAndy Fiddaman end = strpbrk(val, " \t\n");
6627e934d3aSAndy Fiddaman if (end == NULL)
6637e934d3aSAndy Fiddaman break;
6647e934d3aSAndy Fiddaman *end++ = '\0';
6657e934d3aSAndy Fiddaman
6667e934d3aSAndy Fiddaman if (strcmp(tok, "sys.manufacturer") == 0 &&
6677e934d3aSAndy Fiddaman (sys.smbi_manufacturer == NULL ||
6687e934d3aSAndy Fiddaman strcasecmp(val, sys.smbi_manufacturer)))
6697e934d3aSAndy Fiddaman break;
6707e934d3aSAndy Fiddaman if (strcmp(tok, "sys.product") == 0 &&
6717e934d3aSAndy Fiddaman (sys.smbi_product == NULL ||
6727e934d3aSAndy Fiddaman strcasecmp(val, sys.smbi_product)))
6737e934d3aSAndy Fiddaman break;
6747e934d3aSAndy Fiddaman if (strcmp(tok, "sys.version") == 0 &&
6757e934d3aSAndy Fiddaman (sys.smbi_version == NULL ||
6767e934d3aSAndy Fiddaman strcasecmp(val, sys.smbi_version)))
6777e934d3aSAndy Fiddaman break;
6787e934d3aSAndy Fiddaman if (strcmp(tok, "mb.manufacturer") == 0 &&
6797e934d3aSAndy Fiddaman (mb.smbi_manufacturer == NULL ||
6807e934d3aSAndy Fiddaman strcasecmp(val, mb.smbi_manufacturer)))
6817e934d3aSAndy Fiddaman break;
6827e934d3aSAndy Fiddaman if (strcmp(tok, "mb.product") == 0 &&
6837e934d3aSAndy Fiddaman (mb.smbi_product == NULL ||
6847e934d3aSAndy Fiddaman strcasecmp(val, mb.smbi_product)))
6857e934d3aSAndy Fiddaman break;
6867e934d3aSAndy Fiddaman if (strcmp(tok, "mb.version") == 0 &&
6877e934d3aSAndy Fiddaman (mb.smbi_version == NULL ||
6887e934d3aSAndy Fiddaman strcasecmp(val, mb.smbi_version)))
6897e934d3aSAndy Fiddaman break;
6907e934d3aSAndy Fiddaman
6917e934d3aSAndy Fiddaman if (strcmp(tok, "pmbr_slot") == 0) {
6927e934d3aSAndy Fiddaman *slot = atoi(val);
6937e934d3aSAndy Fiddaman if (*slot < 0 || *slot > 3)
6947e934d3aSAndy Fiddaman *slot = 0;
6957e934d3aSAndy Fiddaman if (efi_debug)
6967e934d3aSAndy Fiddaman (void) fprintf(stderr,
6977e934d3aSAndy Fiddaman "Using slot %d\n", *slot);
6987e934d3aSAndy Fiddaman }
6997e934d3aSAndy Fiddaman
7007e934d3aSAndy Fiddaman if (strcmp(tok, "pmbr_active") == 0) {
7017e934d3aSAndy Fiddaman *active = atoi(val);
7027e934d3aSAndy Fiddaman if (*active < 0 || *active > 1)
7037e934d3aSAndy Fiddaman *active = 0;
7047e934d3aSAndy Fiddaman if (efi_debug)
7057e934d3aSAndy Fiddaman (void) fprintf(stderr,
7067e934d3aSAndy Fiddaman "Using active %d\n", *active);
7077e934d3aSAndy Fiddaman }
7087e934d3aSAndy Fiddaman
7097e934d3aSAndy Fiddaman tok = end;
7107e934d3aSAndy Fiddaman }
7117e934d3aSAndy Fiddaman }
7127e934d3aSAndy Fiddaman (void) fclose(fp);
7137e934d3aSAndy Fiddaman smbios_close(shp);
7147e934d3aSAndy Fiddaman }
7157e934d3aSAndy Fiddaman
7167c478bd9Sstevel@tonic-gate /* writes a "protective" MBR */
7177c478bd9Sstevel@tonic-gate static int
write_pmbr(int fd,struct dk_gpt * vtoc)7187c478bd9Sstevel@tonic-gate write_pmbr(int fd, struct dk_gpt *vtoc)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc;
7217c478bd9Sstevel@tonic-gate struct mboot mb;
7227c478bd9Sstevel@tonic-gate uchar_t *cp;
7237c478bd9Sstevel@tonic-gate diskaddr_t size_in_lba;
72465908c77Syu, larry liu - Sun Microsystems - Beijing China uchar_t *buf;
7257e934d3aSAndy Fiddaman int len, slot, active;
7267e934d3aSAndy Fiddaman
7277e934d3aSAndy Fiddaman slot = active = 0;
7287e934d3aSAndy Fiddaman
7297e934d3aSAndy Fiddaman hardware_workarounds(&slot, &active);
73065908c77Syu, larry liu - Sun Microsystems - Beijing China
73165908c77Syu, larry liu - Sun Microsystems - Beijing China len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
732fd797736SJohn Levon buf = calloc(1, len);
7337c478bd9Sstevel@tonic-gate
734987e90dcSxl /*
735987e90dcSxl * Preserve any boot code and disk signature if the first block is
736987e90dcSxl * already an MBR.
737987e90dcSxl */
738987e90dcSxl dk_ioc.dki_lba = 0;
73965908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_length = len;
740987e90dcSxl /* LINTED -- always longlong aligned */
74165908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)buf;
74265908c77Syu, larry liu - Sun Microsystems - Beijing China if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
743286eb3edSRichard Yao (void) memcpy(&mb, buf, sizeof (mb));
744987e90dcSxl bzero(&mb, sizeof (mb));
745987e90dcSxl mb.signature = LE_16(MBB_MAGIC);
74665908c77Syu, larry liu - Sun Microsystems - Beijing China } else {
747286eb3edSRichard Yao (void) memcpy(&mb, buf, sizeof (mb));
74865908c77Syu, larry liu - Sun Microsystems - Beijing China if (mb.signature != LE_16(MBB_MAGIC)) {
74965908c77Syu, larry liu - Sun Microsystems - Beijing China bzero(&mb, sizeof (mb));
75065908c77Syu, larry liu - Sun Microsystems - Beijing China mb.signature = LE_16(MBB_MAGIC);
75165908c77Syu, larry liu - Sun Microsystems - Beijing China }
752987e90dcSxl }
75365908c77Syu, larry liu - Sun Microsystems - Beijing China
7547c478bd9Sstevel@tonic-gate bzero(&mb.parts, sizeof (mb.parts));
7557e934d3aSAndy Fiddaman cp = (uchar_t *)&mb.parts[slot * sizeof (struct ipart)];
7567c478bd9Sstevel@tonic-gate /* bootable or not */
7577e934d3aSAndy Fiddaman *cp++ = active ? ACTIVE : NOTACTIVE;
758fd797736SJohn Levon /* beginning CHS; same as starting LBA (but one-based) */
759fd797736SJohn Levon *cp++ = 0x0;
760fd797736SJohn Levon *cp++ = 0x2;
761fd797736SJohn Levon *cp++ = 0x0;
7627c478bd9Sstevel@tonic-gate /* OS type */
7637c478bd9Sstevel@tonic-gate *cp++ = EFI_PMBR;
7647c478bd9Sstevel@tonic-gate /* ending CHS; 0xffffff if not representable */
7657c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7667c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7677c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7687c478bd9Sstevel@tonic-gate /* starting LBA: 1 (little endian format) by EFI definition */
7697c478bd9Sstevel@tonic-gate *cp++ = 0x01;
7707c478bd9Sstevel@tonic-gate *cp++ = 0x00;
7717c478bd9Sstevel@tonic-gate *cp++ = 0x00;
7727c478bd9Sstevel@tonic-gate *cp++ = 0x00;
7737c478bd9Sstevel@tonic-gate /* ending LBA: last block on the disk (little endian format) */
7747c478bd9Sstevel@tonic-gate size_in_lba = vtoc->efi_last_lba;
7757c478bd9Sstevel@tonic-gate if (size_in_lba < 0xffffffff) {
7767c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0x000000ff);
7777c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0x0000ff00) >> 8;
7787c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0x00ff0000) >> 16;
7797c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0xff000000) >> 24;
7807c478bd9Sstevel@tonic-gate } else {
7817c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7827c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7837c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7847c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7857c478bd9Sstevel@tonic-gate }
78665908c77Syu, larry liu - Sun Microsystems - Beijing China
787286eb3edSRichard Yao (void) memcpy(buf, &mb, sizeof (mb));
7887c478bd9Sstevel@tonic-gate /* LINTED -- always longlong aligned */
78965908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)buf;
7907c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 0;
79165908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_length = len;
7927c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
79365908c77Syu, larry liu - Sun Microsystems - Beijing China free(buf);
7947c478bd9Sstevel@tonic-gate switch (errno) {
7957c478bd9Sstevel@tonic-gate case EIO:
7967c478bd9Sstevel@tonic-gate return (VT_EIO);
7977c478bd9Sstevel@tonic-gate case EINVAL:
7987c478bd9Sstevel@tonic-gate return (VT_EINVAL);
7997c478bd9Sstevel@tonic-gate default:
8007c478bd9Sstevel@tonic-gate return (VT_ERROR);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate }
80365908c77Syu, larry liu - Sun Microsystems - Beijing China free(buf);
8047c478bd9Sstevel@tonic-gate return (0);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /* make sure the user specified something reasonable */
8087c478bd9Sstevel@tonic-gate static int
check_input(struct dk_gpt * vtoc)8097c478bd9Sstevel@tonic-gate check_input(struct dk_gpt *vtoc)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate int resv_part = -1;
8127c478bd9Sstevel@tonic-gate int i, j;
8137c478bd9Sstevel@tonic-gate diskaddr_t istart, jstart, isize, jsize, endsect;
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate * Sanity-check the input (make sure no partitions overlap)
8177c478bd9Sstevel@tonic-gate */
8187c478bd9Sstevel@tonic-gate for (i = 0; i < vtoc->efi_nparts; i++) {
8197c478bd9Sstevel@tonic-gate /* It can't be unassigned and have an actual size */
8207c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
8217c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_size != 0)) {
8227c478bd9Sstevel@tonic-gate if (efi_debug) {
8237c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8247c478bd9Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size of %llu",
8257c478bd9Sstevel@tonic-gate i,
8267c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
831f2be5148Sszhou if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
832f2be5148Sszhou continue;
833f2be5148Sszhou /* we have encountered an unknown uuid */
834f2be5148Sszhou vtoc->efi_parts[i].p_tag = 0xff;
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
8377c478bd9Sstevel@tonic-gate if (resv_part != -1) {
8387c478bd9Sstevel@tonic-gate if (efi_debug) {
839af007057Syl (void) fprintf(stderr,
8407c478bd9Sstevel@tonic-gate "found duplicate reserved partition at %d\n",
841af007057Syl i);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate resv_part = i;
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
8487c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
8497c478bd9Sstevel@tonic-gate if (efi_debug) {
8507c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8517c478bd9Sstevel@tonic-gate "Partition %d starts at %llu. ",
8527c478bd9Sstevel@tonic-gate i,
8537c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start);
8547c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8557c478bd9Sstevel@tonic-gate "It must be between %llu and %llu.\n",
8567c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba,
8577c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_start +
8627c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size <
8637c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba) ||
8647c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_start +
8657c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size >
8667c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba + 1)) {
8677c478bd9Sstevel@tonic-gate if (efi_debug) {
8687c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8697c478bd9Sstevel@tonic-gate "Partition %d ends at %llu. ",
8707c478bd9Sstevel@tonic-gate i,
8717c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start +
8727c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size);
8737c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8747c478bd9Sstevel@tonic-gate "It must be between %llu and %llu.\n",
8757c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba,
8767c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate
8817c478bd9Sstevel@tonic-gate for (j = 0; j < vtoc->efi_nparts; j++) {
8827c478bd9Sstevel@tonic-gate isize = vtoc->efi_parts[i].p_size;
8837c478bd9Sstevel@tonic-gate jsize = vtoc->efi_parts[j].p_size;
8847c478bd9Sstevel@tonic-gate istart = vtoc->efi_parts[i].p_start;
8857c478bd9Sstevel@tonic-gate jstart = vtoc->efi_parts[j].p_start;
8867c478bd9Sstevel@tonic-gate if ((i != j) && (isize != 0) && (jsize != 0)) {
8877c478bd9Sstevel@tonic-gate endsect = jstart + jsize -1;
8887c478bd9Sstevel@tonic-gate if ((jstart <= istart) &&
8897c478bd9Sstevel@tonic-gate (istart <= endsect)) {
8907c478bd9Sstevel@tonic-gate if (efi_debug) {
8917c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8927c478bd9Sstevel@tonic-gate "Partition %d overlaps partition %d.",
8937c478bd9Sstevel@tonic-gate i, j);
894af007057Syl }
895af007057Syl return (VT_EINVAL);
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate /* just a warning for now */
9017c478bd9Sstevel@tonic-gate if ((resv_part == -1) && efi_debug) {
9027c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
903af007057Syl "no reserved partition found\n");
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate return (0);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate
908*5db3bdb0SJason King /*
909*5db3bdb0SJason King * Set *lastp_p to the last non-reserved partition with the last (highest)
910*5db3bdb0SJason King * LBA (and set *last_lbap to the last used LBA). We also will fail if the
911*5db3bdb0SJason King * partition layout isn't as expected (reserved partiton last, no overlap
912*5db3bdb0SJason King * with the last partiton).
913*5db3bdb0SJason King */
914*5db3bdb0SJason King static int
efi_use_whole_disk_get_last(struct dk_gpt * l,struct dk_part ** lastp_p,diskaddr_t * last_lbap)915*5db3bdb0SJason King efi_use_whole_disk_get_last(struct dk_gpt *l, struct dk_part **lastp_p,
916*5db3bdb0SJason King diskaddr_t *last_lbap)
917*5db3bdb0SJason King {
918*5db3bdb0SJason King struct dk_part *last_p = NULL;
919*5db3bdb0SJason King struct dk_part *resv_p = NULL;
920*5db3bdb0SJason King diskaddr_t last_ulba = 0;
921*5db3bdb0SJason King uint_t i;
922*5db3bdb0SJason King
923*5db3bdb0SJason King if (l->efi_nparts < 2) {
924*5db3bdb0SJason King if (efi_debug) {
925*5db3bdb0SJason King (void) fprintf(stderr, "%s: too few (%u) partitions",
926*5db3bdb0SJason King __func__, l->efi_nparts);
927*5db3bdb0SJason King }
928*5db3bdb0SJason King return (-1);
929*5db3bdb0SJason King }
930*5db3bdb0SJason King
931*5db3bdb0SJason King /*
932*5db3bdb0SJason King * Look for the last (highest) used LBA. We ignore the last
933*5db3bdb0SJason King * (efi_nparts - 1) partition since that should be the reserved
934*5db3bdb0SJason King * partition (which is checked later).
935*5db3bdb0SJason King */
936*5db3bdb0SJason King for (i = 0; i < l->efi_nparts - 1; i++) {
937*5db3bdb0SJason King struct dk_part *p = &l->efi_parts[i];
938*5db3bdb0SJason King diskaddr_t end;
939*5db3bdb0SJason King
940*5db3bdb0SJason King if (p->p_tag == V_RESERVED) {
941*5db3bdb0SJason King if (efi_debug) {
942*5db3bdb0SJason King /*
943*5db3bdb0SJason King * Output the error message now so we can
944*5db3bdb0SJason King * indicate which partition is the problem.
945*5db3bdb0SJason King * We'll return failure later.
946*5db3bdb0SJason King */
947*5db3bdb0SJason King (void) fprintf(stderr, "%s: reserved partition "
948*5db3bdb0SJason King "found at unexpected position (%u)\n",
949*5db3bdb0SJason King __func__, i);
950*5db3bdb0SJason King }
951*5db3bdb0SJason King return (-1);
952*5db3bdb0SJason King }
953*5db3bdb0SJason King
954*5db3bdb0SJason King /* Ignore empty partitions */
955*5db3bdb0SJason King if (p->p_size == 0)
956*5db3bdb0SJason King continue;
957*5db3bdb0SJason King
958*5db3bdb0SJason King end = p->p_start + p->p_size - 1;
959*5db3bdb0SJason King if (last_ulba < end) {
960*5db3bdb0SJason King last_p = p;
961*5db3bdb0SJason King last_ulba = end;
962*5db3bdb0SJason King }
963*5db3bdb0SJason King }
964*5db3bdb0SJason King
965*5db3bdb0SJason King if (l->efi_parts[l->efi_nparts - 1].p_tag != V_RESERVED) {
966*5db3bdb0SJason King if (efi_debug) {
967*5db3bdb0SJason King (void) fprintf(stderr, "%s: no reserved partition\n",
968*5db3bdb0SJason King __func__);
969*5db3bdb0SJason King }
970*5db3bdb0SJason King return (-1);
971*5db3bdb0SJason King }
972*5db3bdb0SJason King
973*5db3bdb0SJason King resv_p = &l->efi_parts[l->efi_nparts - 1];
974*5db3bdb0SJason King
975*5db3bdb0SJason King /*
976*5db3bdb0SJason King * The reserved partition should start after the last (highest)
977*5db3bdb0SJason King * LBA used by any other partition.
978*5db3bdb0SJason King */
979*5db3bdb0SJason King if (resv_p->p_start <= last_ulba) {
980*5db3bdb0SJason King if (efi_debug) {
981*5db3bdb0SJason King (void) fprintf(stderr, "%s: reserved partition not "
982*5db3bdb0SJason King "after other partitions\n", __func__);
983*5db3bdb0SJason King }
984*5db3bdb0SJason King return (-1);
985*5db3bdb0SJason King }
986*5db3bdb0SJason King
987*5db3bdb0SJason King *lastp_p = last_p;
988*5db3bdb0SJason King *last_lbap = last_ulba;
989*5db3bdb0SJason King return (0);
990*5db3bdb0SJason King }
991*5db3bdb0SJason King
992af007057Syl /*
993af007057Syl * add all the unallocated space to the current label
994af007057Syl */
995af007057Syl int
efi_use_whole_disk(int fd)996af007057Syl efi_use_whole_disk(int fd)
997af007057Syl {
998af007057Syl struct dk_gpt *efi_label;
999*5db3bdb0SJason King struct dk_part *resv_p = NULL;
1000*5db3bdb0SJason King struct dk_part *last_p = NULL;
1001*5db3bdb0SJason King diskaddr_t last_lba = 0;
1002af007057Syl int rval;
1003*5db3bdb0SJason King uint_t nblocks;
1004*5db3bdb0SJason King boolean_t save = B_FALSE;
1005af007057Syl
1006af007057Syl rval = efi_alloc_and_read(fd, &efi_label);
1007af007057Syl if (rval < 0) {
1008af007057Syl return (rval);
1009af007057Syl }
1010af007057Syl
1011*5db3bdb0SJason King rval = efi_use_whole_disk_get_last(efi_label, &last_p, &last_lba);
1012*5db3bdb0SJason King if (rval < 0) {
1013*5db3bdb0SJason King efi_free(efi_label);
1014*5db3bdb0SJason King return (VT_EINVAL);
1015af007057Syl }
1016*5db3bdb0SJason King resv_p = &efi_label->efi_parts[efi_label->efi_nparts - 1];
1017*5db3bdb0SJason King ASSERT3U(resv_p->p_tag, ==, V_RESERVED);
1018af007057Syl
1019af007057Syl /*
1020*5db3bdb0SJason King * If we aren't using the backup label (efi_altern_lba == 1)
1021*5db3bdb0SJason King * and the backup label isn't at the end of the disk, move the backup
1022*5db3bdb0SJason King * label to the end of the disk. efi_read() sets efi_last_lba based
1023*5db3bdb0SJason King * on the capacity of the disk, so we don't need to re-read the
1024*5db3bdb0SJason King * capacity again to get the last LBA.
1025af007057Syl */
1026*5db3bdb0SJason King if (efi_label->efi_altern_lba != 1 &&
1027*5db3bdb0SJason King efi_label->efi_altern_lba != efi_label->efi_last_lba) {
1028*5db3bdb0SJason King efi_label->efi_altern_lba = efi_label->efi_last_lba;
1029*5db3bdb0SJason King save = B_TRUE;
1030af007057Syl }
1031af007057Syl
1032af007057Syl /*
1033*5db3bdb0SJason King * This is similar to the logic used in efi_alloc_and_init(). Based
1034*5db3bdb0SJason King * on the number of partitions (and the minimum number of entries
1035*5db3bdb0SJason King * required for an EFI label), determine the size of the backup label.
1036af007057Syl */
1037*5db3bdb0SJason King nblocks = NBLOCKS(efi_label->efi_nparts, efi_label->efi_lbasize);
1038*5db3bdb0SJason King if ((nblocks * efi_label->efi_lbasize) < EFI_MIN_ARRAY_SIZE +
1039*5db3bdb0SJason King efi_label->efi_lbasize) {
1040*5db3bdb0SJason King nblocks = EFI_MIN_ARRAY_SIZE / efi_label->efi_lbasize + 1;
1041*5db3bdb0SJason King }
1042*5db3bdb0SJason King
1043*5db3bdb0SJason King /* efi_last_u_lba should be the last LBA before the backup label */
1044*5db3bdb0SJason King if (efi_label->efi_last_u_lba < efi_label->efi_last_lba - nblocks) {
1045*5db3bdb0SJason King efi_label->efi_last_u_lba = efi_label->efi_last_lba - nblocks;
1046*5db3bdb0SJason King save = B_TRUE;
1047*5db3bdb0SJason King }
1048*5db3bdb0SJason King
1049*5db3bdb0SJason King /*
1050*5db3bdb0SJason King * If there is unused space after the reserved partition, move it to
1051*5db3bdb0SJason King * the end of the disk. There is currently no data in here except
1052*5db3bdb0SJason King * fabricated devids (which are generated via efi_write()). Therefore,
1053*5db3bdb0SJason King * there is no need to copy the contents.
1054*5db3bdb0SJason King */
1055*5db3bdb0SJason King if (resv_p->p_start + resv_p->p_size - 1 < efi_label->efi_last_u_lba) {
1056*5db3bdb0SJason King diskaddr_t new_start =
1057*5db3bdb0SJason King efi_label->efi_last_u_lba - resv_p->p_size + 1;
1058*5db3bdb0SJason King
1059*5db3bdb0SJason King if (resv_p->p_start > new_start) {
1060*5db3bdb0SJason King if (efi_debug) {
1061*5db3bdb0SJason King (void) fprintf(stderr, "%s: reserved partition "
1062*5db3bdb0SJason King "size mismatch\n", __func__);
1063*5db3bdb0SJason King }
1064*5db3bdb0SJason King efi_free(efi_label);
1065*5db3bdb0SJason King return (VT_EINVAL);
1066*5db3bdb0SJason King }
1067*5db3bdb0SJason King
1068*5db3bdb0SJason King resv_p->p_start = new_start;
1069*5db3bdb0SJason King save = B_TRUE;
1070af007057Syl }
1071af007057Syl
1072af007057Syl /*
1073*5db3bdb0SJason King * If there is space between the last (non-reserved) partition and
1074*5db3bdb0SJason King * the reserved partition, grow the last partition.
1075af007057Syl */
1076*5db3bdb0SJason King if (last_lba < resv_p->p_start) {
1077*5db3bdb0SJason King last_p->p_size += resv_p->p_start - last_lba - 1;
1078*5db3bdb0SJason King save = B_TRUE;
1079*5db3bdb0SJason King }
1080*5db3bdb0SJason King
1081*5db3bdb0SJason King if (!save) {
1082*5db3bdb0SJason King efi_free(efi_label);
1083*5db3bdb0SJason King return (0);
1084*5db3bdb0SJason King }
1085af007057Syl
1086af007057Syl rval = efi_write(fd, efi_label);
1087af007057Syl if (rval < 0) {
1088af007057Syl if (efi_debug) {
1089af007057Syl (void) fprintf(stderr,
1090af007057Syl "efi_use_whole_disk:fail to write label, rval=%d\n",
1091af007057Syl rval);
1092af007057Syl }
1093af007057Syl efi_free(efi_label);
1094af007057Syl return (rval);
1095af007057Syl }
1096af007057Syl
1097af007057Syl efi_free(efi_label);
1098af007057Syl return (0);
1099af007057Syl }
1100af007057Syl
1101af007057Syl
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate * write EFI label and backup label
11047c478bd9Sstevel@tonic-gate */
11057c478bd9Sstevel@tonic-gate int
efi_write(int fd,struct dk_gpt * vtoc)11067c478bd9Sstevel@tonic-gate efi_write(int fd, struct dk_gpt *vtoc)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc;
11097c478bd9Sstevel@tonic-gate efi_gpt_t *efi;
11107c478bd9Sstevel@tonic-gate efi_gpe_t *efi_parts;
11117c478bd9Sstevel@tonic-gate int i, j;
11127c478bd9Sstevel@tonic-gate struct dk_cinfo dki_info;
111386bbaf93Scg int nblocks;
111486bbaf93Scg diskaddr_t lba_backup_gpt_hdr;
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
11177c478bd9Sstevel@tonic-gate if (efi_debug)
11187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
11197c478bd9Sstevel@tonic-gate switch (errno) {
11207c478bd9Sstevel@tonic-gate case EIO:
11217c478bd9Sstevel@tonic-gate return (VT_EIO);
11227c478bd9Sstevel@tonic-gate case EINVAL:
11237c478bd9Sstevel@tonic-gate return (VT_EINVAL);
11247c478bd9Sstevel@tonic-gate default:
11257c478bd9Sstevel@tonic-gate return (VT_ERROR);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate
11295f10ef69SYuri Pankov if (check_input(vtoc))
11305f10ef69SYuri Pankov return (VT_EINVAL);
11317c478bd9Sstevel@tonic-gate
11327c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 1;
11337c478bd9Sstevel@tonic-gate if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
11347c478bd9Sstevel@tonic-gate dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
11357c478bd9Sstevel@tonic-gate } else {
11367c478bd9Sstevel@tonic-gate dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
1137af007057Syl vtoc->efi_lbasize) *
1138af007057Syl vtoc->efi_lbasize;
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate
114186bbaf93Scg /*
114286bbaf93Scg * the number of blocks occupied by GUID partition entry array
114386bbaf93Scg */
114486bbaf93Scg nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
114586bbaf93Scg
114686bbaf93Scg /*
114786bbaf93Scg * Backup GPT header is located on the block after GUID
114886bbaf93Scg * partition entry array. Here, we calculate the address
114986bbaf93Scg * for backup GPT header.
115086bbaf93Scg */
115186bbaf93Scg lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
11521b58875aSJohn Levon if ((dk_ioc.dki_data = calloc(1, dk_ioc.dki_length)) == NULL)
11537c478bd9Sstevel@tonic-gate return (VT_ERROR);
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate efi = dk_ioc.dki_data;
11567c478bd9Sstevel@tonic-gate
11577c478bd9Sstevel@tonic-gate /* stuff user's input into EFI struct */
11587c478bd9Sstevel@tonic-gate efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
11597c478bd9Sstevel@tonic-gate efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
1160fd797736SJohn Levon efi->efi_gpt_HeaderSize = LE_32(EFI_HEADER_SIZE);
11617c478bd9Sstevel@tonic-gate efi->efi_gpt_Reserved1 = 0;
11627c478bd9Sstevel@tonic-gate efi->efi_gpt_MyLBA = LE_64(1ULL);
116386bbaf93Scg efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
11647c478bd9Sstevel@tonic-gate efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
11657c478bd9Sstevel@tonic-gate efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
11667c478bd9Sstevel@tonic-gate efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
11677c478bd9Sstevel@tonic-gate efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
11687c478bd9Sstevel@tonic-gate efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
11697c478bd9Sstevel@tonic-gate UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
11707c478bd9Sstevel@tonic-gate
11717c478bd9Sstevel@tonic-gate /* LINTED -- always longlong aligned */
117265908c77Syu, larry liu - Sun Microsystems - Beijing China efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
11737c478bd9Sstevel@tonic-gate
11747c478bd9Sstevel@tonic-gate for (i = 0; i < vtoc->efi_nparts; i++) {
1175af007057Syl for (j = 0;
1176af007057Syl j < sizeof (conversion_array) /
1177af007057Syl sizeof (struct uuid_to_ptag); j++) {
1178af007057Syl
1179d06952d0SToomas Soome if (vtoc->efi_parts[i].p_tag ==
1180d06952d0SToomas Soome conversion_array[j].p_tag) {
1181af007057Syl UUID_LE_CONVERT(
1182af007057Syl efi_parts[i].efi_gpe_PartitionTypeGUID,
1183af007057Syl conversion_array[j].uuid);
1184af007057Syl break;
1185af007057Syl }
1186af007057Syl }
1187af007057Syl
1188af007057Syl if (j == sizeof (conversion_array) /
1189af007057Syl sizeof (struct uuid_to_ptag)) {
1190af007057Syl /*
1191af007057Syl * If we didn't have a matching uuid match, bail here.
1192af007057Syl * Don't write a label with unknown uuid.
1193af007057Syl */
1194af007057Syl if (efi_debug) {
1195af007057Syl (void) fprintf(stderr,
1196af007057Syl "Unknown uuid for p_tag %d\n",
1197af007057Syl vtoc->efi_parts[i].p_tag);
1198af007057Syl }
1199af007057Syl return (VT_EINVAL);
1200af007057Syl }
1201af007057Syl
1202af007057Syl efi_parts[i].efi_gpe_StartingLBA =
1203af007057Syl LE_64(vtoc->efi_parts[i].p_start);
1204af007057Syl efi_parts[i].efi_gpe_EndingLBA =
1205af007057Syl LE_64(vtoc->efi_parts[i].p_start +
1206af007057Syl vtoc->efi_parts[i].p_size - 1);
1207af007057Syl efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
12087c478bd9Sstevel@tonic-gate LE_16(vtoc->efi_parts[i].p_flag);
1209af007057Syl for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1210af007057Syl efi_parts[i].efi_gpe_PartitionName[j] =
1211af007057Syl LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1212af007057Syl }
1213af007057Syl if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1214af007057Syl uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1215af007057Syl (void) uuid_generate((uchar_t *)
1216af007057Syl &vtoc->efi_parts[i].p_uguid);
1217af007057Syl }
1218af007057Syl bcopy(&vtoc->efi_parts[i].p_uguid,
1219af007057Syl &efi_parts[i].efi_gpe_UniquePartitionGUID,
1220af007057Syl sizeof (uuid_t));
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate efi->efi_gpt_PartitionEntryArrayCRC32 =
12237c478bd9Sstevel@tonic-gate LE_32(efi_crc32((unsigned char *)efi_parts,
12247c478bd9Sstevel@tonic-gate vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1225fd797736SJohn Levon efi->efi_gpt_HeaderCRC32 = LE_32(efi_crc32((unsigned char *)efi,
1226fd797736SJohn Levon EFI_HEADER_SIZE));
12277c478bd9Sstevel@tonic-gate
12287c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
12297c478bd9Sstevel@tonic-gate free(dk_ioc.dki_data);
12307c478bd9Sstevel@tonic-gate switch (errno) {
12317c478bd9Sstevel@tonic-gate case EIO:
12327c478bd9Sstevel@tonic-gate return (VT_EIO);
12337c478bd9Sstevel@tonic-gate case EINVAL:
12347c478bd9Sstevel@tonic-gate return (VT_EINVAL);
12357c478bd9Sstevel@tonic-gate default:
12367c478bd9Sstevel@tonic-gate return (VT_ERROR);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate }
123965908c77Syu, larry liu - Sun Microsystems - Beijing China
12407c478bd9Sstevel@tonic-gate /* write backup partition array */
12417c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
12427c478bd9Sstevel@tonic-gate dk_ioc.dki_length -= vtoc->efi_lbasize;
124365908c77Syu, larry liu - Sun Microsystems - Beijing China /* LINTED */
124465908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
124565908c77Syu, larry liu - Sun Microsystems - Beijing China vtoc->efi_lbasize);
124665908c77Syu, larry liu - Sun Microsystems - Beijing China
12477c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
12487c478bd9Sstevel@tonic-gate /*
12497c478bd9Sstevel@tonic-gate * we wrote the primary label okay, so don't fail
12507c478bd9Sstevel@tonic-gate */
12517c478bd9Sstevel@tonic-gate if (efi_debug) {
12527c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
12537c478bd9Sstevel@tonic-gate "write of backup partitions to block %llu "
12547c478bd9Sstevel@tonic-gate "failed, errno %d\n",
12557c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba + 1,
12567c478bd9Sstevel@tonic-gate errno);
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate /*
12607c478bd9Sstevel@tonic-gate * now swap MyLBA and AlternateLBA fields and write backup
12617c478bd9Sstevel@tonic-gate * partition table header
12627c478bd9Sstevel@tonic-gate */
126386bbaf93Scg dk_ioc.dki_lba = lba_backup_gpt_hdr;
12647c478bd9Sstevel@tonic-gate dk_ioc.dki_length = vtoc->efi_lbasize;
126565908c77Syu, larry liu - Sun Microsystems - Beijing China /* LINTED */
126665908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
126765908c77Syu, larry liu - Sun Microsystems - Beijing China vtoc->efi_lbasize);
12687c478bd9Sstevel@tonic-gate efi->efi_gpt_AlternateLBA = LE_64(1ULL);
126986bbaf93Scg efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
12707c478bd9Sstevel@tonic-gate efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
12717c478bd9Sstevel@tonic-gate efi->efi_gpt_HeaderCRC32 = 0;
12727c478bd9Sstevel@tonic-gate efi->efi_gpt_HeaderCRC32 =
1273fd797736SJohn Levon LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data, EFI_HEADER_SIZE));
12747c478bd9Sstevel@tonic-gate
12757c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
12767c478bd9Sstevel@tonic-gate if (efi_debug) {
12777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
12787c478bd9Sstevel@tonic-gate "write of backup header to block %llu failed, "
12797c478bd9Sstevel@tonic-gate "errno %d\n",
128086bbaf93Scg lba_backup_gpt_hdr,
12817c478bd9Sstevel@tonic-gate errno);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate /* write the PMBR */
12857c478bd9Sstevel@tonic-gate (void) write_pmbr(fd, vtoc);
12867c478bd9Sstevel@tonic-gate free(dk_ioc.dki_data);
12877c478bd9Sstevel@tonic-gate return (0);
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate void
efi_free(struct dk_gpt * ptr)12917c478bd9Sstevel@tonic-gate efi_free(struct dk_gpt *ptr)
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate free(ptr);
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate
12967c478bd9Sstevel@tonic-gate /*
12977c478bd9Sstevel@tonic-gate * Input: File descriptor
1298342440ecSPrasad Singamsetty * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1299342440ecSPrasad Singamsetty * Otherwise 0.
13007c478bd9Sstevel@tonic-gate */
13017c478bd9Sstevel@tonic-gate int
efi_type(int fd)13027c478bd9Sstevel@tonic-gate efi_type(int fd)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate struct vtoc vtoc;
1305342440ecSPrasad Singamsetty struct extvtoc extvtoc;
13067c478bd9Sstevel@tonic-gate
1307342440ecSPrasad Singamsetty if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1308342440ecSPrasad Singamsetty if (errno == ENOTSUP)
13097c478bd9Sstevel@tonic-gate return (1);
1310342440ecSPrasad Singamsetty else if (errno == ENOTTY) {
1311342440ecSPrasad Singamsetty if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1312342440ecSPrasad Singamsetty if (errno == ENOTSUP)
1313342440ecSPrasad Singamsetty return (1);
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate }
13167c478bd9Sstevel@tonic-gate return (0);
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate
13197c478bd9Sstevel@tonic-gate void
efi_err_check(struct dk_gpt * vtoc)13207c478bd9Sstevel@tonic-gate efi_err_check(struct dk_gpt *vtoc)
13217c478bd9Sstevel@tonic-gate {
13227c478bd9Sstevel@tonic-gate int resv_part = -1;
13237c478bd9Sstevel@tonic-gate int i, j;
13247c478bd9Sstevel@tonic-gate diskaddr_t istart, jstart, isize, jsize, endsect;
13257c478bd9Sstevel@tonic-gate int overlap = 0;
132662ce8e2eSToomas Soome uint_t reserved;
13277c478bd9Sstevel@tonic-gate
13287c478bd9Sstevel@tonic-gate /*
13297c478bd9Sstevel@tonic-gate * make sure no partitions overlap
13307c478bd9Sstevel@tonic-gate */
133162ce8e2eSToomas Soome reserved = efi_reserved_sectors(vtoc);
13327c478bd9Sstevel@tonic-gate for (i = 0; i < vtoc->efi_nparts; i++) {
13337c478bd9Sstevel@tonic-gate /* It can't be unassigned and have an actual size */
13347c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
13357c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_size != 0)) {
13367c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13377c478bd9Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size "
13387c478bd9Sstevel@tonic-gate "of %llu\n", i, vtoc->efi_parts[i].p_size);
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
13417c478bd9Sstevel@tonic-gate continue;
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
13447c478bd9Sstevel@tonic-gate if (resv_part != -1) {
13457c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13467c478bd9Sstevel@tonic-gate "found duplicate reserved partition at "
13477c478bd9Sstevel@tonic-gate "%d\n", i);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate resv_part = i;
135062ce8e2eSToomas Soome if (vtoc->efi_parts[i].p_size != reserved)
13517c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13527c478bd9Sstevel@tonic-gate "Warning: reserved partition size must "
135362ce8e2eSToomas Soome "be %u sectors\n", reserved);
13547c478bd9Sstevel@tonic-gate }
13557c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
13567c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
13577c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13587c478bd9Sstevel@tonic-gate "Partition %d starts at %llu\n",
13597c478bd9Sstevel@tonic-gate i,
13607c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start);
13617c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13627c478bd9Sstevel@tonic-gate "It must be between %llu and %llu.\n",
13637c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba,
13647c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba);
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_start +
13677c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size <
13687c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba) ||
13697c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_start +
13707c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size >
13717c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba + 1)) {
13727c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13737c478bd9Sstevel@tonic-gate "Partition %d ends at %llu\n",
13747c478bd9Sstevel@tonic-gate i,
13757c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start +
13767c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size);
13777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13787c478bd9Sstevel@tonic-gate "It must be between %llu and %llu.\n",
13797c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba,
13807c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba);
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate
13837c478bd9Sstevel@tonic-gate for (j = 0; j < vtoc->efi_nparts; j++) {
13847c478bd9Sstevel@tonic-gate isize = vtoc->efi_parts[i].p_size;
13857c478bd9Sstevel@tonic-gate jsize = vtoc->efi_parts[j].p_size;
13867c478bd9Sstevel@tonic-gate istart = vtoc->efi_parts[i].p_start;
13877c478bd9Sstevel@tonic-gate jstart = vtoc->efi_parts[j].p_start;
13887c478bd9Sstevel@tonic-gate if ((i != j) && (isize != 0) && (jsize != 0)) {
13897c478bd9Sstevel@tonic-gate endsect = jstart + jsize -1;
13907c478bd9Sstevel@tonic-gate if ((jstart <= istart) &&
13917c478bd9Sstevel@tonic-gate (istart <= endsect)) {
13927c478bd9Sstevel@tonic-gate if (!overlap) {
13937c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13947c478bd9Sstevel@tonic-gate "label error: EFI Labels do not "
13957c478bd9Sstevel@tonic-gate "support overlapping partitions\n");
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13987c478bd9Sstevel@tonic-gate "Partition %d overlaps partition "
13997c478bd9Sstevel@tonic-gate "%d.\n", i, j);
14007c478bd9Sstevel@tonic-gate overlap = 1;
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate }
14037c478bd9Sstevel@tonic-gate }
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate /* make sure there is a reserved partition */
14067c478bd9Sstevel@tonic-gate if (resv_part == -1) {
14077c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1408af007057Syl "no reserved partition found\n");
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate
14127c478bd9Sstevel@tonic-gate /*
14137c478bd9Sstevel@tonic-gate * We need to get information necessary to construct a *new* efi
14147c478bd9Sstevel@tonic-gate * label type
14157c478bd9Sstevel@tonic-gate */
14167c478bd9Sstevel@tonic-gate int
efi_auto_sense(int fd,struct dk_gpt ** vtoc)14177c478bd9Sstevel@tonic-gate efi_auto_sense(int fd, struct dk_gpt **vtoc)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate
14207c478bd9Sstevel@tonic-gate int i;
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate /*
14237c478bd9Sstevel@tonic-gate * Now build the default partition table
14247c478bd9Sstevel@tonic-gate */
14257c478bd9Sstevel@tonic-gate if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
14267c478bd9Sstevel@tonic-gate if (efi_debug) {
14277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate return (-1);
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate
14327c478bd9Sstevel@tonic-gate for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
14337c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
14347c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
14357c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[i].p_start = 0;
14367c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[i].p_size = 0;
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate /*
14397c478bd9Sstevel@tonic-gate * Make constants first
14407c478bd9Sstevel@tonic-gate * and variable partitions later
14417c478bd9Sstevel@tonic-gate */
14427c478bd9Sstevel@tonic-gate
14437c478bd9Sstevel@tonic-gate /* root partition - s0 128 MB */
14447c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[0].p_start = 34;
14457c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[0].p_size = 262144;
14467c478bd9Sstevel@tonic-gate
14477c478bd9Sstevel@tonic-gate /* partition - s1 128 MB */
14487c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[1].p_start = 262178;
14497c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[1].p_size = 262144;
14507c478bd9Sstevel@tonic-gate
14517c478bd9Sstevel@tonic-gate /* partition -s2 is NOT the Backup disk */
14527c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
14537c478bd9Sstevel@tonic-gate
14547c478bd9Sstevel@tonic-gate /* partition -s6 /usr partition - HOG */
14557c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[6].p_start = 524322;
14567c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
14577c478bd9Sstevel@tonic-gate - (1024 * 16);
14587c478bd9Sstevel@tonic-gate
14597c478bd9Sstevel@tonic-gate /* efi reserved partition - s9 16K */
14607c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
14617c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[8].p_size = (1024 * 16);
14627c478bd9Sstevel@tonic-gate (*vtoc)->efi_parts[8].p_tag = V_RESERVED;
14637c478bd9Sstevel@tonic-gate return (0);
14647c478bd9Sstevel@tonic-gate }
1465