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.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357e934d3aSAndy Fiddaman #include <smbios.h>
367c478bd9Sstevel@tonic-gate #include <uuid/uuid.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
407c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
417c478bd9Sstevel@tonic-gate #include <sys/mhd.h>
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
447c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
457c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
477c478bd9Sstevel@tonic-gate
48d06952d0SToomas Soome /*
49d06952d0SToomas Soome * The original conversion array used simple array index, but since
50d06952d0SToomas Soome * we do need to take account of VTOC tag numbers from other systems,
51d06952d0SToomas Soome * we need to provide tag values too, or the array will grow too large.
52d06952d0SToomas Soome *
53d06952d0SToomas Soome * Still we will fabricate the missing p_tag values.
54d06952d0SToomas Soome */
557c478bd9Sstevel@tonic-gate static struct uuid_to_ptag {
567c478bd9Sstevel@tonic-gate struct uuid uuid;
57d06952d0SToomas Soome ushort_t p_tag;
587c478bd9Sstevel@tonic-gate } conversion_array[] = {
59d06952d0SToomas Soome { EFI_UNUSED, V_UNASSIGNED },
60d06952d0SToomas Soome { EFI_BOOT, V_BOOT },
61d06952d0SToomas Soome { EFI_ROOT, V_ROOT },
62d06952d0SToomas Soome { EFI_SWAP, V_SWAP },
63d06952d0SToomas Soome { EFI_USR, V_USR },
64d06952d0SToomas Soome { EFI_BACKUP, V_BACKUP },
65d06952d0SToomas Soome { EFI_VAR, V_VAR },
66d06952d0SToomas Soome { EFI_HOME, V_HOME },
67d06952d0SToomas Soome { EFI_ALTSCTR, V_ALTSCTR },
68d06952d0SToomas Soome { EFI_RESERVED, V_RESERVED },
69d06952d0SToomas Soome { EFI_SYSTEM, V_SYSTEM }, /* V_SYSTEM is 0xc */
70d06952d0SToomas Soome { EFI_LEGACY_MBR, 0x10 },
71d06952d0SToomas Soome { EFI_SYMC_PUB, 0x11 },
72d06952d0SToomas Soome { EFI_SYMC_CDS, 0x12 },
73d06952d0SToomas Soome { EFI_MSFT_RESV, 0x13 },
74d06952d0SToomas Soome { EFI_DELL_BASIC, 0x14 },
75d06952d0SToomas Soome { EFI_DELL_RAID, 0x15 },
76d06952d0SToomas Soome { EFI_DELL_SWAP, 0x16 },
77d06952d0SToomas Soome { EFI_DELL_LVM, 0x17 },
78d06952d0SToomas Soome { EFI_DELL_RESV, 0x19 },
79d06952d0SToomas Soome { EFI_AAPL_HFS, 0x1a },
80d06952d0SToomas Soome { EFI_AAPL_UFS, 0x1b },
81d06952d0SToomas Soome { EFI_AAPL_ZFS, 0x1c },
82d06952d0SToomas Soome { EFI_AAPL_APFS, 0x1d },
83d06952d0SToomas Soome { EFI_BIOS_BOOT, V_BIOS_BOOT }, /* V_BIOS_BOOT is 0x18 */
84d06952d0SToomas Soome { EFI_FREEBSD_BOOT, V_FREEBSD_BOOT },
85d06952d0SToomas Soome { EFI_FREEBSD_SWAP, V_FREEBSD_SWAP },
86d06952d0SToomas Soome { EFI_FREEBSD_UFS, V_FREEBSD_UFS },
87d06952d0SToomas Soome { EFI_FREEBSD_VINUM, V_FREEBSD_VINUM },
88d06952d0SToomas Soome { EFI_FREEBSD_ZFS, V_FREEBSD_ZFS },
89d06952d0SToomas Soome { EFI_FREEBSD_NANDFS, V_FREEBSD_NANDFS }
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * Default vtoc information for non-SVr4 partitions
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate struct dk_map2 default_vtoc_map[NDKMAP] = {
967c478bd9Sstevel@tonic-gate { V_ROOT, 0 }, /* a - 0 */
977c478bd9Sstevel@tonic-gate { V_SWAP, V_UNMNT }, /* b - 1 */
987c478bd9Sstevel@tonic-gate { V_BACKUP, V_UNMNT }, /* c - 2 */
997c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* d - 3 */
1007c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* e - 4 */
1017c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* f - 5 */
1027c478bd9Sstevel@tonic-gate { V_USR, 0 }, /* g - 6 */
1037c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* h - 7 */
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate #if defined(i386) || defined(__amd64)
1087c478bd9Sstevel@tonic-gate { V_BOOT, V_UNMNT }, /* i - 8 */
1097c478bd9Sstevel@tonic-gate { V_ALTSCTR, 0 }, /* j - 9 */
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate #else
1127c478bd9Sstevel@tonic-gate #error No VTOC format defined.
1137c478bd9Sstevel@tonic-gate #endif /* defined(i386) */
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* k - 10 */
1167c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* l - 11 */
1177c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* m - 12 */
1187c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* n - 13 */
1197c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* o - 14 */
1207c478bd9Sstevel@tonic-gate { V_UNASSIGNED, 0 }, /* p - 15 */
1217c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate #ifdef DEBUG
1257c478bd9Sstevel@tonic-gate int efi_debug = 1;
1267c478bd9Sstevel@tonic-gate #else
1277c478bd9Sstevel@tonic-gate int efi_debug = 0;
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate
1307e934d3aSAndy Fiddaman #define EFI_FIXES_DB "/usr/share/hwdata/efi.fixes"
1317e934d3aSAndy Fiddaman
1327c478bd9Sstevel@tonic-gate extern unsigned int efi_crc32(const unsigned char *, unsigned int);
1337c478bd9Sstevel@tonic-gate static int efi_read(int, struct dk_gpt *);
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate static int
read_disk_info(int fd,diskaddr_t * capacity,uint_t * lbsize)1367c478bd9Sstevel@tonic-gate read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate struct dk_minfo disk_info;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
1417c478bd9Sstevel@tonic-gate return (errno);
1427c478bd9Sstevel@tonic-gate *capacity = disk_info.dki_capacity;
1437c478bd9Sstevel@tonic-gate *lbsize = disk_info.dki_lbsize;
1447c478bd9Sstevel@tonic-gate return (0);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * the number of blocks the EFI label takes up (round up to nearest
1497c478bd9Sstevel@tonic-gate * block)
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
1527c478bd9Sstevel@tonic-gate ((l) - 1)) / (l)))
1537c478bd9Sstevel@tonic-gate /* number of partitions -- limited by what we can malloc */
1547c478bd9Sstevel@tonic-gate #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
1557c478bd9Sstevel@tonic-gate sizeof (struct dk_part))
1567c478bd9Sstevel@tonic-gate
157*62ce8e2eSToomas Soome /*
158*62ce8e2eSToomas Soome * The EFI reserved partition size is 8 MiB. This calculates the number of
159*62ce8e2eSToomas Soome * sectors required to store 8 MiB, taking into account the device's sector
160*62ce8e2eSToomas Soome * size.
161*62ce8e2eSToomas Soome */
162*62ce8e2eSToomas Soome uint_t
efi_reserved_sectors(dk_gpt_t * efi)163*62ce8e2eSToomas Soome efi_reserved_sectors(dk_gpt_t *efi)
164*62ce8e2eSToomas Soome {
165*62ce8e2eSToomas Soome /* roundup to sector size */
166*62ce8e2eSToomas Soome return ((EFI_MIN_RESV_SIZE * DEV_BSIZE + efi->efi_lbasize - 1) /
167*62ce8e2eSToomas Soome efi->efi_lbasize);
168*62ce8e2eSToomas Soome }
169*62ce8e2eSToomas Soome
1707c478bd9Sstevel@tonic-gate int
efi_alloc_and_init(int fd,uint32_t nparts,struct dk_gpt ** vtoc)1717c478bd9Sstevel@tonic-gate efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate diskaddr_t capacity;
1747c478bd9Sstevel@tonic-gate uint_t lbsize;
1757c478bd9Sstevel@tonic-gate uint_t nblocks;
1767c478bd9Sstevel@tonic-gate size_t length;
1777c478bd9Sstevel@tonic-gate struct dk_gpt *vptr;
1787c478bd9Sstevel@tonic-gate struct uuid uuid;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate if (read_disk_info(fd, &capacity, &lbsize) != 0) {
1817c478bd9Sstevel@tonic-gate if (efi_debug)
1827c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1837c478bd9Sstevel@tonic-gate "couldn't read disk information\n");
1847c478bd9Sstevel@tonic-gate return (-1);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate nblocks = NBLOCKS(nparts, lbsize);
1887c478bd9Sstevel@tonic-gate if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
1897c478bd9Sstevel@tonic-gate /* 16K plus one block for the GPT */
1907c478bd9Sstevel@tonic-gate nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate if (nparts > MAX_PARTS) {
1947c478bd9Sstevel@tonic-gate if (efi_debug) {
1957c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1967c478bd9Sstevel@tonic-gate "the maximum number of partitions supported is %lu\n",
1977c478bd9Sstevel@tonic-gate MAX_PARTS);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate return (-1);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate length = sizeof (struct dk_gpt) +
2037c478bd9Sstevel@tonic-gate sizeof (struct dk_part) * (nparts - 1);
2047c478bd9Sstevel@tonic-gate
2051b58875aSJohn Levon if ((*vtoc = calloc(1, length)) == NULL)
2067c478bd9Sstevel@tonic-gate return (-1);
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate vptr = *vtoc;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate vptr->efi_version = EFI_VERSION_CURRENT;
2117c478bd9Sstevel@tonic-gate vptr->efi_lbasize = lbsize;
2127c478bd9Sstevel@tonic-gate vptr->efi_nparts = nparts;
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * add one block here for the PMBR; on disks with a 512 byte
2157c478bd9Sstevel@tonic-gate * block size and 128 or fewer partitions, efi_first_u_lba
2167c478bd9Sstevel@tonic-gate * should work out to "34"
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate vptr->efi_first_u_lba = nblocks + 1;
2197c478bd9Sstevel@tonic-gate vptr->efi_last_lba = capacity - 1;
220af007057Syl vptr->efi_altern_lba = capacity -1;
2217c478bd9Sstevel@tonic-gate vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
22265908c77Syu, larry liu - Sun Microsystems - Beijing China
2237c478bd9Sstevel@tonic-gate (void) uuid_generate((uchar_t *)&uuid);
2247c478bd9Sstevel@tonic-gate UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
2257c478bd9Sstevel@tonic-gate return (0);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate * Read EFI - return partition number upon success.
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate int
efi_alloc_and_read(int fd,struct dk_gpt ** vtoc)2327c478bd9Sstevel@tonic-gate efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate int rval;
2357c478bd9Sstevel@tonic-gate uint32_t nparts;
2367c478bd9Sstevel@tonic-gate int length;
237fe12dc75SToomas Soome struct mboot *mbr;
238fe12dc75SToomas Soome struct ipart *ipart;
239fe12dc75SToomas Soome diskaddr_t capacity;
240fe12dc75SToomas Soome uint_t lbsize;
241fe12dc75SToomas Soome int i;
242fe12dc75SToomas Soome
243fe12dc75SToomas Soome if (read_disk_info(fd, &capacity, &lbsize) != 0)
244fe12dc75SToomas Soome return (VT_ERROR);
245fe12dc75SToomas Soome
2461b58875aSJohn Levon if ((mbr = calloc(1, lbsize)) == NULL)
247fe12dc75SToomas Soome return (VT_ERROR);
248fe12dc75SToomas Soome
249fe12dc75SToomas Soome if ((ioctl(fd, DKIOCGMBOOT, (caddr_t)mbr)) == -1) {
250fe12dc75SToomas Soome free(mbr);
251fe12dc75SToomas Soome return (VT_ERROR);
252fe12dc75SToomas Soome }
253fe12dc75SToomas Soome
254fe12dc75SToomas Soome if (mbr->signature != MBB_MAGIC) {
255fe12dc75SToomas Soome free(mbr);
256fe12dc75SToomas Soome return (VT_EINVAL);
257fe12dc75SToomas Soome }
258fe12dc75SToomas Soome ipart = (struct ipart *)(uintptr_t)mbr->parts;
259fe12dc75SToomas Soome
260fe12dc75SToomas Soome /* Check if we have partition with ID EFI_PMBR */
261fe12dc75SToomas Soome for (i = 0; i < FD_NUMPART; i++) {
262fe12dc75SToomas Soome if (ipart[i].systid == EFI_PMBR)
263fe12dc75SToomas Soome break;
264fe12dc75SToomas Soome }
265fe12dc75SToomas Soome free(mbr);
266fe12dc75SToomas Soome if (i == FD_NUMPART)
267fe12dc75SToomas Soome return (VT_EINVAL);
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /* figure out the number of entries that would fit into 16K */
2707c478bd9Sstevel@tonic-gate nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
2717c478bd9Sstevel@tonic-gate length = (int) sizeof (struct dk_gpt) +
272af007057Syl (int) sizeof (struct dk_part) * (nparts - 1);
2731b58875aSJohn Levon if ((*vtoc = calloc(1, length)) == NULL)
2747c478bd9Sstevel@tonic-gate return (VT_ERROR);
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate (*vtoc)->efi_nparts = nparts;
2777c478bd9Sstevel@tonic-gate rval = efi_read(fd, *vtoc);
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
2807c478bd9Sstevel@tonic-gate void *tmp;
2817c478bd9Sstevel@tonic-gate length = (int) sizeof (struct dk_gpt) +
282af007057Syl (int) sizeof (struct dk_part) *
283af007057Syl ((*vtoc)->efi_nparts - 1);
2847c478bd9Sstevel@tonic-gate nparts = (*vtoc)->efi_nparts;
2857c478bd9Sstevel@tonic-gate if ((tmp = realloc(*vtoc, length)) == NULL) {
2867c478bd9Sstevel@tonic-gate free (*vtoc);
2877c478bd9Sstevel@tonic-gate *vtoc = NULL;
2887c478bd9Sstevel@tonic-gate return (VT_ERROR);
2897c478bd9Sstevel@tonic-gate } else {
2907c478bd9Sstevel@tonic-gate *vtoc = tmp;
2917c478bd9Sstevel@tonic-gate rval = efi_read(fd, *vtoc);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (rval < 0) {
2967c478bd9Sstevel@tonic-gate if (efi_debug) {
2977c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2987c478bd9Sstevel@tonic-gate "read of EFI table failed, rval=%d\n", rval);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate free (*vtoc);
3017c478bd9Sstevel@tonic-gate *vtoc = NULL;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate return (rval);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate static int
efi_ioctl(int fd,int cmd,dk_efi_t * dk_ioc)3087c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate void *data = dk_ioc->dki_data;
3117c478bd9Sstevel@tonic-gate int error;
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
3147c478bd9Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc);
3157c478bd9Sstevel@tonic-gate dk_ioc->dki_data = data;
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate return (error);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate static int
check_label(int fd,dk_efi_t * dk_ioc)3217c478bd9Sstevel@tonic-gate check_label(int fd, dk_efi_t *dk_ioc)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate efi_gpt_t *efi;
3247c478bd9Sstevel@tonic-gate uint_t crc;
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
3277c478bd9Sstevel@tonic-gate switch (errno) {
3287c478bd9Sstevel@tonic-gate case EIO:
3297c478bd9Sstevel@tonic-gate return (VT_EIO);
3307c478bd9Sstevel@tonic-gate default:
3317c478bd9Sstevel@tonic-gate return (VT_ERROR);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate efi = dk_ioc->dki_data;
3357c478bd9Sstevel@tonic-gate if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
3367c478bd9Sstevel@tonic-gate if (efi_debug)
3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3387c478bd9Sstevel@tonic-gate "Bad EFI signature: 0x%llx != 0x%llx\n",
3397c478bd9Sstevel@tonic-gate (long long)efi->efi_gpt_Signature,
3407c478bd9Sstevel@tonic-gate (long long)LE_64(EFI_SIGNATURE));
3417c478bd9Sstevel@tonic-gate return (VT_EINVAL);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * check CRC of the header; the size of the header should
3467c478bd9Sstevel@tonic-gate * never be larger than one block
3477c478bd9Sstevel@tonic-gate */
3487c478bd9Sstevel@tonic-gate crc = efi->efi_gpt_HeaderCRC32;
3497c478bd9Sstevel@tonic-gate efi->efi_gpt_HeaderCRC32 = 0;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
3527c478bd9Sstevel@tonic-gate crc != LE_32(efi_crc32((unsigned char *)efi,
3537c478bd9Sstevel@tonic-gate LE_32(efi->efi_gpt_HeaderSize)))) {
3547c478bd9Sstevel@tonic-gate if (efi_debug)
3557c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
356af007057Syl "Bad EFI CRC: 0x%x != 0x%x\n",
357fd797736SJohn Levon crc, LE_32(efi_crc32((unsigned char *)efi,
358fd797736SJohn Levon LE_32(efi->efi_gpt_HeaderSize))));
3597c478bd9Sstevel@tonic-gate return (VT_EINVAL);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate return (0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate static int
efi_read(int fd,struct dk_gpt * vtoc)3667c478bd9Sstevel@tonic-gate efi_read(int fd, struct dk_gpt *vtoc)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate int i, j;
3697c478bd9Sstevel@tonic-gate int label_len;
3707c478bd9Sstevel@tonic-gate int rval = 0;
371d84f0041SAlexandre Chartre int vdc_flag = 0;
3727c478bd9Sstevel@tonic-gate struct dk_minfo disk_info;
3737c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc;
3747c478bd9Sstevel@tonic-gate efi_gpt_t *efi;
3757c478bd9Sstevel@tonic-gate efi_gpe_t *efi_parts;
3767c478bd9Sstevel@tonic-gate struct dk_cinfo dki_info;
3777c478bd9Sstevel@tonic-gate uint32_t user_length;
37886bbaf93Scg boolean_t legacy_label = B_FALSE;
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate * get the partition number for this file descriptor.
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
384af007057Syl if (efi_debug) {
385af007057Syl (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
386af007057Syl }
3877c478bd9Sstevel@tonic-gate switch (errno) {
3887c478bd9Sstevel@tonic-gate case EIO:
3897c478bd9Sstevel@tonic-gate return (VT_EIO);
3907c478bd9Sstevel@tonic-gate case EINVAL:
3917c478bd9Sstevel@tonic-gate return (VT_EINVAL);
3927c478bd9Sstevel@tonic-gate default:
3937c478bd9Sstevel@tonic-gate return (VT_ERROR);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate }
3965f10ef69SYuri Pankov
3975f10ef69SYuri Pankov if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
398d84f0041SAlexandre Chartre (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
399d84f0041SAlexandre Chartre /*
400d84f0041SAlexandre Chartre * The controller and drive name "vdc" (virtual disk client)
401d84f0041SAlexandre Chartre * indicates a LDoms virtual disk.
402d84f0041SAlexandre Chartre */
403d84f0041SAlexandre Chartre vdc_flag++;
4047c478bd9Sstevel@tonic-gate }
405d84f0041SAlexandre Chartre
4067c478bd9Sstevel@tonic-gate /* get the LBA size */
4077c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
4087c478bd9Sstevel@tonic-gate if (efi_debug) {
4097c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4107c478bd9Sstevel@tonic-gate "assuming LBA 512 bytes %d\n",
4117c478bd9Sstevel@tonic-gate errno);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate disk_info.dki_lbsize = DEV_BSIZE;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate if (disk_info.dki_lbsize == 0) {
4167c478bd9Sstevel@tonic-gate if (efi_debug) {
4177c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4187c478bd9Sstevel@tonic-gate "efi_read: assuming LBA 512 bytes\n");
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate disk_info.dki_lbsize = DEV_BSIZE;
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * Read the EFI GPT to figure out how many partitions we need
4247c478bd9Sstevel@tonic-gate * to deal with.
4257c478bd9Sstevel@tonic-gate */
4267c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 1;
4277c478bd9Sstevel@tonic-gate if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
4287c478bd9Sstevel@tonic-gate label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
4297c478bd9Sstevel@tonic-gate } else {
4307c478bd9Sstevel@tonic-gate label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
431af007057Syl disk_info.dki_lbsize;
4327c478bd9Sstevel@tonic-gate if (label_len % disk_info.dki_lbsize) {
4337c478bd9Sstevel@tonic-gate /* pad to physical sector size */
4347c478bd9Sstevel@tonic-gate label_len += disk_info.dki_lbsize;
4357c478bd9Sstevel@tonic-gate label_len &= ~(disk_info.dki_lbsize - 1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate
4391b58875aSJohn Levon if ((dk_ioc.dki_data = calloc(1, label_len)) == NULL)
4407c478bd9Sstevel@tonic-gate return (VT_ERROR);
4417c478bd9Sstevel@tonic-gate
442c7e1889fSyl dk_ioc.dki_length = disk_info.dki_lbsize;
4437c478bd9Sstevel@tonic-gate user_length = vtoc->efi_nparts;
4447c478bd9Sstevel@tonic-gate efi = dk_ioc.dki_data;
4455f10ef69SYuri Pankov if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
44686bbaf93Scg /*
44786bbaf93Scg * No valid label here; try the alternate. Note that here
44886bbaf93Scg * we just read GPT header and save it into dk_ioc.data,
44986bbaf93Scg * Later, we will read GUID partition entry array if we
45086bbaf93Scg * can get valid GPT header.
45186bbaf93Scg */
45286bbaf93Scg
45386bbaf93Scg /*
45486bbaf93Scg * This is a workaround for legacy systems. In the past, the
45586bbaf93Scg * last sector of SCSI disk was invisible on x86 platform. At
45686bbaf93Scg * that time, backup label was saved on the next to the last
45786bbaf93Scg * sector. It is possible for users to move a disk from previous
45886bbaf93Scg * solaris system to present system. Here, we attempt to search
45986bbaf93Scg * legacy backup EFI label first.
46086bbaf93Scg */
46186bbaf93Scg dk_ioc.dki_lba = disk_info.dki_capacity - 2;
4627c478bd9Sstevel@tonic-gate dk_ioc.dki_length = disk_info.dki_lbsize;
4634f0dea16Scg rval = check_label(fd, &dk_ioc);
46486bbaf93Scg if (rval == VT_EINVAL) {
4654f0dea16Scg /*
46686bbaf93Scg * we didn't find legacy backup EFI label, try to
46786bbaf93Scg * search backup EFI label in the last block.
4684f0dea16Scg */
46986bbaf93Scg dk_ioc.dki_lba = disk_info.dki_capacity - 1;
4704f0dea16Scg dk_ioc.dki_length = disk_info.dki_lbsize;
4714f0dea16Scg rval = check_label(fd, &dk_ioc);
47286bbaf93Scg if (rval == 0) {
47386bbaf93Scg legacy_label = B_TRUE;
47486bbaf93Scg if (efi_debug)
47586bbaf93Scg (void) fprintf(stderr,
47686bbaf93Scg "efi_read: primary label corrupt; "
47786bbaf93Scg "using EFI backup label located on"
47886bbaf93Scg " the last block\n");
4794f0dea16Scg }
48086bbaf93Scg } else {
48186bbaf93Scg if ((efi_debug) && (rval == 0))
48286bbaf93Scg (void) fprintf(stderr, "efi_read: primary label"
48386bbaf93Scg " corrupt; using legacy EFI backup label "
48486bbaf93Scg " located on the next to last block\n");
4854f0dea16Scg }
4864f0dea16Scg
4874f0dea16Scg if (rval == 0) {
4887c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
4897c478bd9Sstevel@tonic-gate vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
4907c478bd9Sstevel@tonic-gate vtoc->efi_nparts =
4917c478bd9Sstevel@tonic-gate LE_32(efi->efi_gpt_NumberOfPartitionEntries);
4927c478bd9Sstevel@tonic-gate /*
49386bbaf93Scg * Partition tables are between backup GPT header
49486bbaf93Scg * table and ParitionEntryLBA (the starting LBA of
49586bbaf93Scg * the GUID partition entries array). Now that we
49686bbaf93Scg * already got valid GPT header and saved it in
49786bbaf93Scg * dk_ioc.dki_data, we try to get GUID partition
49886bbaf93Scg * entry array here.
4997c478bd9Sstevel@tonic-gate */
50065908c77Syu, larry liu - Sun Microsystems - Beijing China /* LINTED */
50165908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
50265908c77Syu, larry liu - Sun Microsystems - Beijing China + disk_info.dki_lbsize);
50386bbaf93Scg if (legacy_label)
50486bbaf93Scg dk_ioc.dki_length = disk_info.dki_capacity - 1 -
505af007057Syl dk_ioc.dki_lba;
50686bbaf93Scg else
50786bbaf93Scg dk_ioc.dki_length = disk_info.dki_capacity - 2 -
508af007057Syl dk_ioc.dki_lba;
5097c478bd9Sstevel@tonic-gate dk_ioc.dki_length *= disk_info.dki_lbsize;
51086bbaf93Scg if (dk_ioc.dki_length >
51186bbaf93Scg ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
5127c478bd9Sstevel@tonic-gate rval = VT_EINVAL;
5137c478bd9Sstevel@tonic-gate } else {
51486bbaf93Scg /*
51586bbaf93Scg * read GUID partition entry array
51686bbaf93Scg */
5177c478bd9Sstevel@tonic-gate rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate }
520d84f0041SAlexandre Chartre
521d84f0041SAlexandre Chartre } else if (rval == 0) {
522d84f0041SAlexandre Chartre
523c7e1889fSyl dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
52465908c77Syu, larry liu - Sun Microsystems - Beijing China /* LINTED */
52565908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
52665908c77Syu, larry liu - Sun Microsystems - Beijing China + disk_info.dki_lbsize);
527c7e1889fSyl dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
528c7e1889fSyl rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
529d84f0041SAlexandre Chartre
530d84f0041SAlexandre Chartre } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
531d84f0041SAlexandre Chartre /*
532d84f0041SAlexandre Chartre * When the device is a LDoms virtual disk, the DKIOCGETEFI
533d84f0041SAlexandre Chartre * ioctl can fail with EINVAL if the virtual disk backend
534d84f0041SAlexandre Chartre * is a ZFS volume serviced by a domain running an old version
535d84f0041SAlexandre Chartre * of Solaris. This is because the DKIOCGETEFI ioctl was
536d84f0041SAlexandre Chartre * initially incorrectly implemented for a ZFS volume and it
537d84f0041SAlexandre Chartre * expected the GPT and GPE to be retrieved with a single ioctl.
538d84f0041SAlexandre Chartre * So we try to read the GPT and the GPE using that old style
539d84f0041SAlexandre Chartre * ioctl.
540d84f0041SAlexandre Chartre */
541d84f0041SAlexandre Chartre dk_ioc.dki_lba = 1;
542d84f0041SAlexandre Chartre dk_ioc.dki_length = label_len;
543d84f0041SAlexandre Chartre rval = check_label(fd, &dk_ioc);
5447c478bd9Sstevel@tonic-gate }
545d84f0041SAlexandre Chartre
5467c478bd9Sstevel@tonic-gate if (rval < 0) {
5477c478bd9Sstevel@tonic-gate free(efi);
5487c478bd9Sstevel@tonic-gate return (rval);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate /* LINTED -- always longlong aligned */
552c7e1889fSyl efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate * Assemble this into a "dk_gpt" struct for easier
5567c478bd9Sstevel@tonic-gate * digestibility by applications.
5577c478bd9Sstevel@tonic-gate */
5587c478bd9Sstevel@tonic-gate vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
5597c478bd9Sstevel@tonic-gate vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
5607c478bd9Sstevel@tonic-gate vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
5617c478bd9Sstevel@tonic-gate vtoc->efi_lbasize = disk_info.dki_lbsize;
5627c478bd9Sstevel@tonic-gate vtoc->efi_last_lba = disk_info.dki_capacity - 1;
5637c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
5647c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
565af007057Syl vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
5667c478bd9Sstevel@tonic-gate UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate * If the array the user passed in is too small, set the length
5707c478bd9Sstevel@tonic-gate * to what it needs to be and return
5717c478bd9Sstevel@tonic-gate */
5727c478bd9Sstevel@tonic-gate if (user_length < vtoc->efi_nparts) {
5737c478bd9Sstevel@tonic-gate return (VT_EINVAL);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate for (i = 0; i < vtoc->efi_nparts; i++) {
5777c478bd9Sstevel@tonic-gate
578af007057Syl UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
579af007057Syl efi_parts[i].efi_gpe_PartitionTypeGUID);
580af007057Syl
581af007057Syl for (j = 0;
582af007057Syl j < sizeof (conversion_array)
583af007057Syl / sizeof (struct uuid_to_ptag); j++) {
584af007057Syl
585af007057Syl if (bcmp(&vtoc->efi_parts[i].p_guid,
586af007057Syl &conversion_array[j].uuid,
587af007057Syl sizeof (struct uuid)) == 0) {
588d06952d0SToomas Soome vtoc->efi_parts[i].p_tag =
589d06952d0SToomas Soome conversion_array[j].p_tag;
590af007057Syl break;
591af007057Syl }
592af007057Syl }
593af007057Syl if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
594af007057Syl continue;
595af007057Syl vtoc->efi_parts[i].p_flag =
596af007057Syl LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
597af007057Syl vtoc->efi_parts[i].p_start =
598af007057Syl LE_64(efi_parts[i].efi_gpe_StartingLBA);
599af007057Syl vtoc->efi_parts[i].p_size =
600af007057Syl LE_64(efi_parts[i].efi_gpe_EndingLBA) -
6017c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start + 1;
602af007057Syl for (j = 0; j < EFI_PART_NAME_LEN; j++) {
603af007057Syl vtoc->efi_parts[i].p_name[j] =
604af007057Syl (uchar_t)LE_16(
605af007057Syl efi_parts[i].efi_gpe_PartitionName[j]);
606af007057Syl }
6077c478bd9Sstevel@tonic-gate
608af007057Syl UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
609af007057Syl efi_parts[i].efi_gpe_UniquePartitionGUID);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate free(efi);
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate return (dki_info.dki_partition);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate
6167e934d3aSAndy Fiddaman static void
hardware_workarounds(int * slot,int * active)6177e934d3aSAndy Fiddaman hardware_workarounds(int *slot, int *active)
6187e934d3aSAndy Fiddaman {
6197e934d3aSAndy Fiddaman smbios_struct_t s_sys, s_mb;
6207e934d3aSAndy Fiddaman smbios_info_t sys, mb;
6217e934d3aSAndy Fiddaman smbios_hdl_t *shp;
6227e934d3aSAndy Fiddaman char buf[0x400];
6237e934d3aSAndy Fiddaman FILE *fp;
6247e934d3aSAndy Fiddaman int err;
6257e934d3aSAndy Fiddaman
6267e934d3aSAndy Fiddaman if ((fp = fopen(EFI_FIXES_DB, "rF")) == NULL)
6277e934d3aSAndy Fiddaman return;
6287e934d3aSAndy Fiddaman
6297e934d3aSAndy Fiddaman if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL) {
6307e934d3aSAndy Fiddaman if (efi_debug)
6317e934d3aSAndy Fiddaman (void) fprintf(stderr,
6327e934d3aSAndy Fiddaman "libefi failed to load SMBIOS: %s\n",
6337e934d3aSAndy Fiddaman smbios_errmsg(err));
6347e934d3aSAndy Fiddaman (void) fclose(fp);
6357e934d3aSAndy Fiddaman return;
6367e934d3aSAndy Fiddaman }
6377e934d3aSAndy Fiddaman
6387e934d3aSAndy Fiddaman if (smbios_lookup_type(shp, SMB_TYPE_SYSTEM, &s_sys) == SMB_ERR ||
6397e934d3aSAndy Fiddaman smbios_info_common(shp, s_sys.smbstr_id, &sys) == SMB_ERR)
6407e934d3aSAndy Fiddaman (void) memset(&sys, '\0', sizeof (sys));
6417e934d3aSAndy Fiddaman if (smbios_lookup_type(shp, SMB_TYPE_BASEBOARD, &s_mb) == SMB_ERR ||
6427e934d3aSAndy Fiddaman smbios_info_common(shp, s_mb.smbstr_id, &mb) == SMB_ERR)
6437e934d3aSAndy Fiddaman (void) memset(&mb, '\0', sizeof (mb));
6447e934d3aSAndy Fiddaman
6457e934d3aSAndy Fiddaman while (fgets(buf, sizeof (buf), fp) != NULL) {
6467e934d3aSAndy Fiddaman char *tok, *val, *end;
6477e934d3aSAndy Fiddaman
6487e934d3aSAndy Fiddaman tok = buf + strspn(buf, " \t");
6497e934d3aSAndy Fiddaman if (*tok == '#')
6507e934d3aSAndy Fiddaman continue;
6517e934d3aSAndy Fiddaman while (*tok != '\0') {
6527e934d3aSAndy Fiddaman tok += strspn(tok, " \t");
6537e934d3aSAndy Fiddaman if ((val = strchr(tok, '=')) == NULL)
6547e934d3aSAndy Fiddaman break;
6557e934d3aSAndy Fiddaman *val++ = '\0';
6567e934d3aSAndy Fiddaman if (*val == '"')
6577e934d3aSAndy Fiddaman end = strchr(++val, '"');
6587e934d3aSAndy Fiddaman else
6597e934d3aSAndy Fiddaman end = strpbrk(val, " \t\n");
6607e934d3aSAndy Fiddaman if (end == NULL)
6617e934d3aSAndy Fiddaman break;
6627e934d3aSAndy Fiddaman *end++ = '\0';
6637e934d3aSAndy Fiddaman
6647e934d3aSAndy Fiddaman if (strcmp(tok, "sys.manufacturer") == 0 &&
6657e934d3aSAndy Fiddaman (sys.smbi_manufacturer == NULL ||
6667e934d3aSAndy Fiddaman strcasecmp(val, sys.smbi_manufacturer)))
6677e934d3aSAndy Fiddaman break;
6687e934d3aSAndy Fiddaman if (strcmp(tok, "sys.product") == 0 &&
6697e934d3aSAndy Fiddaman (sys.smbi_product == NULL ||
6707e934d3aSAndy Fiddaman strcasecmp(val, sys.smbi_product)))
6717e934d3aSAndy Fiddaman break;
6727e934d3aSAndy Fiddaman if (strcmp(tok, "sys.version") == 0 &&
6737e934d3aSAndy Fiddaman (sys.smbi_version == NULL ||
6747e934d3aSAndy Fiddaman strcasecmp(val, sys.smbi_version)))
6757e934d3aSAndy Fiddaman break;
6767e934d3aSAndy Fiddaman if (strcmp(tok, "mb.manufacturer") == 0 &&
6777e934d3aSAndy Fiddaman (mb.smbi_manufacturer == NULL ||
6787e934d3aSAndy Fiddaman strcasecmp(val, mb.smbi_manufacturer)))
6797e934d3aSAndy Fiddaman break;
6807e934d3aSAndy Fiddaman if (strcmp(tok, "mb.product") == 0 &&
6817e934d3aSAndy Fiddaman (mb.smbi_product == NULL ||
6827e934d3aSAndy Fiddaman strcasecmp(val, mb.smbi_product)))
6837e934d3aSAndy Fiddaman break;
6847e934d3aSAndy Fiddaman if (strcmp(tok, "mb.version") == 0 &&
6857e934d3aSAndy Fiddaman (mb.smbi_version == NULL ||
6867e934d3aSAndy Fiddaman strcasecmp(val, mb.smbi_version)))
6877e934d3aSAndy Fiddaman break;
6887e934d3aSAndy Fiddaman
6897e934d3aSAndy Fiddaman if (strcmp(tok, "pmbr_slot") == 0) {
6907e934d3aSAndy Fiddaman *slot = atoi(val);
6917e934d3aSAndy Fiddaman if (*slot < 0 || *slot > 3)
6927e934d3aSAndy Fiddaman *slot = 0;
6937e934d3aSAndy Fiddaman if (efi_debug)
6947e934d3aSAndy Fiddaman (void) fprintf(stderr,
6957e934d3aSAndy Fiddaman "Using slot %d\n", *slot);
6967e934d3aSAndy Fiddaman }
6977e934d3aSAndy Fiddaman
6987e934d3aSAndy Fiddaman if (strcmp(tok, "pmbr_active") == 0) {
6997e934d3aSAndy Fiddaman *active = atoi(val);
7007e934d3aSAndy Fiddaman if (*active < 0 || *active > 1)
7017e934d3aSAndy Fiddaman *active = 0;
7027e934d3aSAndy Fiddaman if (efi_debug)
7037e934d3aSAndy Fiddaman (void) fprintf(stderr,
7047e934d3aSAndy Fiddaman "Using active %d\n", *active);
7057e934d3aSAndy Fiddaman }
7067e934d3aSAndy Fiddaman
7077e934d3aSAndy Fiddaman tok = end;
7087e934d3aSAndy Fiddaman }
7097e934d3aSAndy Fiddaman }
7107e934d3aSAndy Fiddaman (void) fclose(fp);
7117e934d3aSAndy Fiddaman smbios_close(shp);
7127e934d3aSAndy Fiddaman }
7137e934d3aSAndy Fiddaman
7147c478bd9Sstevel@tonic-gate /* writes a "protective" MBR */
7157c478bd9Sstevel@tonic-gate static int
write_pmbr(int fd,struct dk_gpt * vtoc)7167c478bd9Sstevel@tonic-gate write_pmbr(int fd, struct dk_gpt *vtoc)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc;
7197c478bd9Sstevel@tonic-gate struct mboot mb;
7207c478bd9Sstevel@tonic-gate uchar_t *cp;
7217c478bd9Sstevel@tonic-gate diskaddr_t size_in_lba;
72265908c77Syu, larry liu - Sun Microsystems - Beijing China uchar_t *buf;
7237e934d3aSAndy Fiddaman int len, slot, active;
7247e934d3aSAndy Fiddaman
7257e934d3aSAndy Fiddaman slot = active = 0;
7267e934d3aSAndy Fiddaman
7277e934d3aSAndy Fiddaman hardware_workarounds(&slot, &active);
72865908c77Syu, larry liu - Sun Microsystems - Beijing China
72965908c77Syu, larry liu - Sun Microsystems - Beijing China len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
730fd797736SJohn Levon buf = calloc(1, len);
7317c478bd9Sstevel@tonic-gate
732987e90dcSxl /*
733987e90dcSxl * Preserve any boot code and disk signature if the first block is
734987e90dcSxl * already an MBR.
735987e90dcSxl */
736987e90dcSxl dk_ioc.dki_lba = 0;
73765908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_length = len;
738987e90dcSxl /* LINTED -- always longlong aligned */
73965908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)buf;
74065908c77Syu, larry liu - Sun Microsystems - Beijing China if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
741286eb3edSRichard Yao (void) memcpy(&mb, buf, sizeof (mb));
742987e90dcSxl bzero(&mb, sizeof (mb));
743987e90dcSxl mb.signature = LE_16(MBB_MAGIC);
74465908c77Syu, larry liu - Sun Microsystems - Beijing China } else {
745286eb3edSRichard Yao (void) memcpy(&mb, buf, sizeof (mb));
74665908c77Syu, larry liu - Sun Microsystems - Beijing China if (mb.signature != LE_16(MBB_MAGIC)) {
74765908c77Syu, larry liu - Sun Microsystems - Beijing China bzero(&mb, sizeof (mb));
74865908c77Syu, larry liu - Sun Microsystems - Beijing China mb.signature = LE_16(MBB_MAGIC);
74965908c77Syu, larry liu - Sun Microsystems - Beijing China }
750987e90dcSxl }
75165908c77Syu, larry liu - Sun Microsystems - Beijing China
7527c478bd9Sstevel@tonic-gate bzero(&mb.parts, sizeof (mb.parts));
7537e934d3aSAndy Fiddaman cp = (uchar_t *)&mb.parts[slot * sizeof (struct ipart)];
7547c478bd9Sstevel@tonic-gate /* bootable or not */
7557e934d3aSAndy Fiddaman *cp++ = active ? ACTIVE : NOTACTIVE;
756fd797736SJohn Levon /* beginning CHS; same as starting LBA (but one-based) */
757fd797736SJohn Levon *cp++ = 0x0;
758fd797736SJohn Levon *cp++ = 0x2;
759fd797736SJohn Levon *cp++ = 0x0;
7607c478bd9Sstevel@tonic-gate /* OS type */
7617c478bd9Sstevel@tonic-gate *cp++ = EFI_PMBR;
7627c478bd9Sstevel@tonic-gate /* ending CHS; 0xffffff if not representable */
7637c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7647c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7657c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7667c478bd9Sstevel@tonic-gate /* starting LBA: 1 (little endian format) by EFI definition */
7677c478bd9Sstevel@tonic-gate *cp++ = 0x01;
7687c478bd9Sstevel@tonic-gate *cp++ = 0x00;
7697c478bd9Sstevel@tonic-gate *cp++ = 0x00;
7707c478bd9Sstevel@tonic-gate *cp++ = 0x00;
7717c478bd9Sstevel@tonic-gate /* ending LBA: last block on the disk (little endian format) */
7727c478bd9Sstevel@tonic-gate size_in_lba = vtoc->efi_last_lba;
7737c478bd9Sstevel@tonic-gate if (size_in_lba < 0xffffffff) {
7747c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0x000000ff);
7757c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0x0000ff00) >> 8;
7767c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0x00ff0000) >> 16;
7777c478bd9Sstevel@tonic-gate *cp++ = (size_in_lba & 0xff000000) >> 24;
7787c478bd9Sstevel@tonic-gate } else {
7797c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7807c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7817c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7827c478bd9Sstevel@tonic-gate *cp++ = 0xff;
7837c478bd9Sstevel@tonic-gate }
78465908c77Syu, larry liu - Sun Microsystems - Beijing China
785286eb3edSRichard Yao (void) memcpy(buf, &mb, sizeof (mb));
7867c478bd9Sstevel@tonic-gate /* LINTED -- always longlong aligned */
78765908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)buf;
7887c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 0;
78965908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_length = len;
7907c478bd9Sstevel@tonic-gate if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
79165908c77Syu, larry liu - Sun Microsystems - Beijing China free(buf);
7927c478bd9Sstevel@tonic-gate switch (errno) {
7937c478bd9Sstevel@tonic-gate case EIO:
7947c478bd9Sstevel@tonic-gate return (VT_EIO);
7957c478bd9Sstevel@tonic-gate case EINVAL:
7967c478bd9Sstevel@tonic-gate return (VT_EINVAL);
7977c478bd9Sstevel@tonic-gate default:
7987c478bd9Sstevel@tonic-gate return (VT_ERROR);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate }
80165908c77Syu, larry liu - Sun Microsystems - Beijing China free(buf);
8027c478bd9Sstevel@tonic-gate return (0);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate
8057c478bd9Sstevel@tonic-gate /* make sure the user specified something reasonable */
8067c478bd9Sstevel@tonic-gate static int
check_input(struct dk_gpt * vtoc)8077c478bd9Sstevel@tonic-gate check_input(struct dk_gpt *vtoc)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate int resv_part = -1;
8107c478bd9Sstevel@tonic-gate int i, j;
8117c478bd9Sstevel@tonic-gate diskaddr_t istart, jstart, isize, jsize, endsect;
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate * Sanity-check the input (make sure no partitions overlap)
8157c478bd9Sstevel@tonic-gate */
8167c478bd9Sstevel@tonic-gate for (i = 0; i < vtoc->efi_nparts; i++) {
8177c478bd9Sstevel@tonic-gate /* It can't be unassigned and have an actual size */
8187c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
8197c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_size != 0)) {
8207c478bd9Sstevel@tonic-gate if (efi_debug) {
8217c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8227c478bd9Sstevel@tonic-gate "partition %d is \"unassigned\" but has a size of %llu",
8237c478bd9Sstevel@tonic-gate i,
8247c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
829f2be5148Sszhou if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
830f2be5148Sszhou continue;
831f2be5148Sszhou /* we have encountered an unknown uuid */
832f2be5148Sszhou vtoc->efi_parts[i].p_tag = 0xff;
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
8357c478bd9Sstevel@tonic-gate if (resv_part != -1) {
8367c478bd9Sstevel@tonic-gate if (efi_debug) {
837af007057Syl (void) fprintf(stderr,
8387c478bd9Sstevel@tonic-gate "found duplicate reserved partition at %d\n",
839af007057Syl i);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate resv_part = i;
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
8467c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
8477c478bd9Sstevel@tonic-gate if (efi_debug) {
8487c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8497c478bd9Sstevel@tonic-gate "Partition %d starts at %llu. ",
8507c478bd9Sstevel@tonic-gate i,
8517c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start);
8527c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8537c478bd9Sstevel@tonic-gate "It must be between %llu and %llu.\n",
8547c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba,
8557c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate if ((vtoc->efi_parts[i].p_start +
8607c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size <
8617c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba) ||
8627c478bd9Sstevel@tonic-gate (vtoc->efi_parts[i].p_start +
8637c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size >
8647c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba + 1)) {
8657c478bd9Sstevel@tonic-gate if (efi_debug) {
8667c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8677c478bd9Sstevel@tonic-gate "Partition %d ends at %llu. ",
8687c478bd9Sstevel@tonic-gate i,
8697c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_start +
8707c478bd9Sstevel@tonic-gate vtoc->efi_parts[i].p_size);
8717c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8727c478bd9Sstevel@tonic-gate "It must be between %llu and %llu.\n",
8737c478bd9Sstevel@tonic-gate vtoc->efi_first_u_lba,
8747c478bd9Sstevel@tonic-gate vtoc->efi_last_u_lba);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate return (VT_EINVAL);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate for (j = 0; j < vtoc->efi_nparts; j++) {
8807c478bd9Sstevel@tonic-gate isize = vtoc->efi_parts[i].p_size;
8817c478bd9Sstevel@tonic-gate jsize = vtoc->efi_parts[j].p_size;
8827c478bd9Sstevel@tonic-gate istart = vtoc->efi_parts[i].p_start;
8837c478bd9Sstevel@tonic-gate jstart = vtoc->efi_parts[j].p_start;
8847c478bd9Sstevel@tonic-gate if ((i != j) && (isize != 0) && (jsize != 0)) {
8857c478bd9Sstevel@tonic-gate endsect = jstart + jsize -1;
8867c478bd9Sstevel@tonic-gate if ((jstart <= istart) &&
8877c478bd9Sstevel@tonic-gate (istart <= endsect)) {
8887c478bd9Sstevel@tonic-gate if (efi_debug) {
8897c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8907c478bd9Sstevel@tonic-gate "Partition %d overlaps partition %d.",
8917c478bd9Sstevel@tonic-gate i, j);
892af007057Syl }
893af007057Syl return (VT_EINVAL);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate /* just a warning for now */
8997c478bd9Sstevel@tonic-gate if ((resv_part == -1) && efi_debug) {
9007c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
901af007057Syl "no reserved partition found\n");
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate return (0);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate
906af007057Syl /*
907af007057Syl * add all the unallocated space to the current label
908af007057Syl */
909af007057Syl int
efi_use_whole_disk(int fd)910af007057Syl efi_use_whole_disk(int fd)
911af007057Syl {
912af007057Syl struct dk_gpt *efi_label;
913af007057Syl int rval;
914af007057Syl int i;
915af007057Syl uint_t phy_last_slice = 0;
916af007057Syl diskaddr_t pl_start = 0;
917af007057Syl diskaddr_t pl_size;
918af007057Syl
919af007057Syl rval = efi_alloc_and_read(fd, &efi_label);
920af007057Syl if (rval < 0) {
921af007057Syl return (rval);
922af007057Syl }
923af007057Syl
924af007057Syl /* find the last physically non-zero partition */
925af007057Syl for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
926af007057Syl if (pl_start < efi_label->efi_parts[i].p_start) {
927af007057Syl pl_start = efi_label->efi_parts[i].p_start;
928af007057Syl phy_last_slice = i;
929af007057Syl }
930af007057Syl }
931af007057Syl pl_size = efi_label->efi_parts[phy_last_slice].p_size;
932af007057Syl
933af007057Syl /*
934af007057Syl * If alter_lba is 1, we are using the backup label.
935af007057Syl * Since we can locate the backup label by disk capacity,
936af007057Syl * there must be no unallocated space.
937af007057Syl */
938af007057Syl if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
939af007057Syl >= efi_label->efi_last_lba)) {
940af007057Syl if (efi_debug) {
941af007057Syl (void) fprintf(stderr,
942af007057Syl "efi_use_whole_disk: requested space not found\n");
943af007057Syl }
944af007057Syl efi_free(efi_label);
945af007057Syl return (VT_ENOSPC);
946af007057Syl }
947af007057Syl
948af007057Syl /*
949af007057Syl * If there is space between the last physically non-zero partition
950af007057Syl * and the reserved partition, just add the unallocated space to this
951af007057Syl * area. Otherwise, the unallocated space is added to the last
952af007057Syl * physically non-zero partition.
953af007057Syl */
954af007057Syl if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
955*62ce8e2eSToomas Soome efi_reserved_sectors(efi_label)) {
956af007057Syl efi_label->efi_parts[phy_last_slice].p_size +=
957af007057Syl efi_label->efi_last_lba - efi_label->efi_altern_lba;
958af007057Syl }
959af007057Syl
960af007057Syl /*
961af007057Syl * Move the reserved partition. There is currently no data in
962af007057Syl * here except fabricated devids (which get generated via
963af007057Syl * efi_write()). So there is no need to copy data.
964af007057Syl */
965af007057Syl efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
966af007057Syl efi_label->efi_last_lba - efi_label->efi_altern_lba;
967af007057Syl efi_label->efi_last_u_lba += efi_label->efi_last_lba
968af007057Syl - efi_label->efi_altern_lba