2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <errno.h>
2N/A#include <strings.h>
2N/A#include <unistd.h>
2N/A#include <uuid/uuid.h>
2N/A#include <libintl.h>
2N/A#include <sys/types.h>
2N/A#include <fcntl.h>
2N/A#include <sys/dkio.h>
2N/A#include <sys/vtoc.h>
2N/A#include <sys/mhd.h>
2N/A#include <sys/param.h>
2N/A#include <sys/dktp/fdisk.h>
2N/A#include <sys/efi_partition.h>
2N/A#include <sys/byteorder.h>
2N/A#include <sys/ddi.h>
2N/A
2N/Astatic struct uuid_to_ptag {
2N/A struct uuid uuid;
2N/A} conversion_array[] = {
2N/A { EFI_UNUSED },
2N/A { EFI_BOOT },
2N/A { EFI_ROOT },
2N/A { EFI_SWAP },
2N/A { EFI_USR },
2N/A { EFI_BACKUP },
2N/A { 0 }, /* STAND is never used */
2N/A { EFI_VAR },
2N/A { EFI_HOME },
2N/A { EFI_ALTSCTR },
2N/A { 0 }, /* is never used (was used by cachefs) */
2N/A { EFI_RESERVED },
2N/A { EFI_SYSTEM },
2N/A { EFI_LEGACY_MBR },
2N/A { EFI_SYMC_PUB },
2N/A { EFI_SYMC_CDS },
2N/A { EFI_MSFT_RESV },
2N/A { EFI_DELL_BASIC },
2N/A { EFI_DELL_RAID },
2N/A { EFI_DELL_SWAP },
2N/A { EFI_DELL_LVM },
2N/A { EFI_DELL_RESV },
2N/A { EFI_AAPL_HFS },
2N/A { EFI_AAPL_UFS },
2N/A { EFI_BIOS_BOOT }
2N/A};
2N/A
2N/A/*
2N/A * Default vtoc information for non-SVr4 partitions
2N/A */
2N/Astruct dk_map2 default_vtoc_map[NDKMAP] = {
2N/A { V_ROOT, 0 }, /* a - 0 */
2N/A { V_SWAP, V_UNMNT }, /* b - 1 */
2N/A { V_BACKUP, V_UNMNT }, /* c - 2 */
2N/A { V_UNASSIGNED, 0 }, /* d - 3 */
2N/A { V_UNASSIGNED, 0 }, /* e - 4 */
2N/A { V_UNASSIGNED, 0 }, /* f - 5 */
2N/A { V_USR, 0 }, /* g - 6 */
2N/A { V_UNASSIGNED, 0 }, /* h - 7 */
2N/A
2N/A#if defined(_SUNOS_VTOC_16)
2N/A
2N/A#if defined(i386) || defined(__amd64)
2N/A { V_BOOT, V_UNMNT }, /* i - 8 */
2N/A { V_ALTSCTR, 0 }, /* j - 9 */
2N/A
2N/A#else
2N/A#error No VTOC format defined.
2N/A#endif /* defined(i386) */
2N/A
2N/A { V_UNASSIGNED, 0 }, /* k - 10 */
2N/A { V_UNASSIGNED, 0 }, /* l - 11 */
2N/A { V_UNASSIGNED, 0 }, /* m - 12 */
2N/A { V_UNASSIGNED, 0 }, /* n - 13 */
2N/A { V_UNASSIGNED, 0 }, /* o - 14 */
2N/A { V_UNASSIGNED, 0 }, /* p - 15 */
2N/A#endif /* defined(_SUNOS_VTOC_16) */
2N/A};
2N/A
2N/A#define SOLARIS_WD_NODE 7
2N/A
2N/A#ifdef DEBUG
2N/Aint efi_debug = 1;
2N/A#else
2N/Aint efi_debug = 0;
2N/A#endif
2N/A
2N/Aextern unsigned int efi_crc32(const unsigned char *, unsigned int);
2N/Astatic int efi_read(int, struct dk_gpt *);
2N/A
2N/Astatic int
2N/Aread_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
2N/A{
2N/A struct dk_minfo disk_info;
2N/A
2N/A if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
2N/A return (errno);
2N/A *capacity = disk_info.dki_capacity;
2N/A *lbsize = disk_info.dki_lbsize;
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * the number of blocks the EFI label takes up (round up to nearest
2N/A * block)
2N/A */
2N/A#define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
2N/A ((l) - 1)) / (l)))
2N/A/* number of partitions -- limited by what we can malloc */
2N/A#define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
2N/A sizeof (struct dk_part))
2N/A
2N/Aint
2N/Aefi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
2N/A{
2N/A diskaddr_t capacity;
2N/A uint_t lbsize;
2N/A uint_t nblocks;
2N/A uint_t nentries;
2N/A size_t length;
2N/A struct dk_gpt *vptr;
2N/A struct uuid uuid;
2N/A
2N/A if (read_disk_info(fd, &capacity, &lbsize) != 0) {
2N/A if (efi_debug)
2N/A (void) fprintf(stderr,
2N/A "couldn't read disk information\n");
2N/A return (-1);
2N/A }
2N/A
2N/A if (nparts < EFI_MIN_NUMBER_PARTITION_ENTRIES)
2N/A nentries = EFI_MIN_NUMBER_PARTITION_ENTRIES;
2N/A else
2N/A nentries = nparts;
2N/A
2N/A nblocks = NBLOCKS(nentries, lbsize);
2N/A
2N/A if (nentries > MAX_PARTS) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "the maximum number of partitions supported is %lu\n",
2N/A MAX_PARTS);
2N/A }
2N/A return (-1);
2N/A }
2N/A
2N/A length = sizeof (struct dk_gpt) +
2N/A sizeof (struct dk_part) * (nentries - 1);
2N/A
2N/A if ((*vtoc = calloc(length, 1)) == NULL)
2N/A return (-1);
2N/A
2N/A vptr = *vtoc;
2N/A
2N/A vptr->efi_version = EFI_VERSION_CURRENT;
2N/A vptr->efi_lbasize = lbsize;
2N/A vptr->efi_nparts = nparts;
2N/A vptr->efi_num_of_partition_entries = nentries;
2N/A /*
2N/A * add one block here for the PMBR; on disks with a 512 byte
2N/A * block size and 128 or fewer partitions, efi_first_u_lba
2N/A * should work out to "34"
2N/A */
2N/A vptr->efi_first_u_lba = nblocks + 1;
2N/A vptr->efi_last_lba = capacity - 1;
2N/A vptr->efi_altern_lba = capacity -1;
2N/A vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
2N/A
2N/A (void) uuid_generate((uchar_t *)&uuid);
2N/A UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Read EFI - return partition number upon success.
2N/A */
2N/Aint
2N/Aefi_alloc_and_read(int fd, struct dk_gpt **vtoc)
2N/A{
2N/A int rval;
2N/A uint32_t nentries;
2N/A int length;
2N/A
2N/A /* figure out the number of entries that would fit into 16K */
2N/A nentries = EFI_MIN_NUMBER_PARTITION_ENTRIES;
2N/A length = (int) sizeof (struct dk_gpt) +
2N/A (int) sizeof (struct dk_part) * (nentries - 1);
2N/A if ((*vtoc = calloc(length, 1)) == NULL)
2N/A return (VT_ERROR);
2N/A
2N/A (*vtoc)->efi_num_of_partition_entries = nentries;
2N/A rval = efi_read(fd, *vtoc);
2N/A
2N/A if ((rval == VT_EINVAL) &&
2N/A ((*vtoc)->efi_num_of_partition_entries > nentries)) {
2N/A void *tmp;
2N/A length = (int) sizeof (struct dk_gpt) +
2N/A (int) sizeof (struct dk_part) *
2N/A ((*vtoc)->efi_num_of_partition_entries - 1);
2N/A nentries = (*vtoc)->efi_num_of_partition_entries;
2N/A if ((tmp = realloc(*vtoc, length)) == NULL) {
2N/A free (*vtoc);
2N/A *vtoc = NULL;
2N/A return (VT_ERROR);
2N/A } else {
2N/A *vtoc = tmp;
2N/A rval = efi_read(fd, *vtoc);
2N/A }
2N/A }
2N/A
2N/A if (rval < 0) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "read of EFI table failed, rval=%d\n", rval);
2N/A }
2N/A free (*vtoc);
2N/A *vtoc = NULL;
2N/A }
2N/A
2N/A return (rval);
2N/A}
2N/A
2N/Astatic int
2N/Aefi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
2N/A{
2N/A void *data = dk_ioc->dki_data;
2N/A int error;
2N/A
2N/A dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
2N/A error = ioctl(fd, cmd, (void *)dk_ioc);
2N/A dk_ioc->dki_data = data;
2N/A
2N/A return (error);
2N/A}
2N/A
2N/Astatic int
2N/Acheck_label(int fd, dk_efi_t *dk_ioc)
2N/A{
2N/A efi_gpt_t *efi;
2N/A uint_t crc;
2N/A
2N/A if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
2N/A switch (errno) {
2N/A case EIO:
2N/A return (VT_EIO);
2N/A default:
2N/A return (VT_ERROR);
2N/A }
2N/A }
2N/A efi = dk_ioc->dki_data;
2N/A if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
2N/A if (efi_debug)
2N/A (void) fprintf(stderr,
2N/A "Bad EFI signature: 0x%llx != 0x%llx\n",
2N/A (long long)efi->efi_gpt_Signature,
2N/A (long long)LE_64(EFI_SIGNATURE));
2N/A return (VT_EINVAL);
2N/A }
2N/A
2N/A /*
2N/A * check CRC of the header; the size of the header should
2N/A * never be larger than one block
2N/A */
2N/A crc = efi->efi_gpt_HeaderCRC32;
2N/A efi->efi_gpt_HeaderCRC32 = 0;
2N/A
2N/A if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
2N/A crc != LE_32(efi_crc32((unsigned char *)efi,
2N/A LE_32(efi->efi_gpt_HeaderSize)))) {
2N/A if (efi_debug)
2N/A (void) fprintf(stderr,
2N/A "Bad EFI CRC: 0x%x != 0x%x\n",
2N/A crc,
2N/A LE_32(efi_crc32((unsigned char *)efi,
2N/A sizeof (struct efi_gpt))));
2N/A return (VT_EINVAL);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Aefi_read(int fd, struct dk_gpt *vtoc)
2N/A{
2N/A int i, j;
2N/A int label_len;
2N/A int rval = 0;
2N/A int md_flag = 0;
2N/A int vdc_flag = 0;
2N/A struct dk_minfo disk_info;
2N/A dk_efi_t dk_ioc;
2N/A efi_gpt_t *efi;
2N/A efi_gpe_t *efi_parts;
2N/A struct dk_cinfo dki_info;
2N/A uint32_t user_length;
2N/A boolean_t legacy_label = B_FALSE;
2N/A
2N/A /*
2N/A * get the partition number for this file descriptor.
2N/A */
2N/A if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
2N/A }
2N/A switch (errno) {
2N/A case EIO:
2N/A return (VT_EIO);
2N/A case EINVAL:
2N/A return (VT_EINVAL);
2N/A default:
2N/A return (VT_ERROR);
2N/A }
2N/A }
2N/A if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
2N/A (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
2N/A md_flag++;
2N/A } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
2N/A (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
2N/A /*
2N/A * The controller and drive name "vdc" (virtual disk client)
2N/A * indicates a LDoms virtual disk.
2N/A */
2N/A vdc_flag++;
2N/A }
2N/A
2N/A /* get the LBA size */
2N/A if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "assuming LBA 512 bytes %d\n",
2N/A errno);
2N/A }
2N/A disk_info.dki_lbsize = DEV_BSIZE;
2N/A }
2N/A if (disk_info.dki_lbsize == 0) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "efi_read: assuming LBA 512 bytes\n");
2N/A }
2N/A disk_info.dki_lbsize = DEV_BSIZE;
2N/A }
2N/A /*
2N/A * Read the EFI GPT to figure out how many partitions we need
2N/A * to deal with.
2N/A */
2N/A dk_ioc.dki_lba = 1;
2N/A if (NBLOCKS(vtoc->efi_num_of_partition_entries,
2N/A disk_info.dki_lbsize) < 34) {
2N/A label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
2N/A vtoc->efi_num_of_partition_entries =
2N/A EFI_MIN_NUMBER_PARTITION_ENTRIES;
2N/A } else {
2N/A label_len = vtoc->efi_num_of_partition_entries
2N/A * (int) sizeof (efi_gpe_t) + disk_info.dki_lbsize;
2N/A if (label_len % disk_info.dki_lbsize) {
2N/A /* pad to physical sector size */
2N/A label_len += disk_info.dki_lbsize;
2N/A label_len &= ~(disk_info.dki_lbsize - 1);
2N/A }
2N/A }
2N/A
2N/A if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
2N/A return (VT_ERROR);
2N/A
2N/A dk_ioc.dki_length = disk_info.dki_lbsize;
2N/A user_length = vtoc->efi_num_of_partition_entries;
2N/A efi = dk_ioc.dki_data;
2N/A if (md_flag) {
2N/A dk_ioc.dki_length = label_len;
2N/A if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
2N/A switch (errno) {
2N/A case EIO:
2N/A return (VT_EIO);
2N/A default:
2N/A return (VT_ERROR);
2N/A }
2N/A }
2N/A } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
2N/A /*
2N/A * No valid label here; try the alternate. Note that here
2N/A * we just read GPT header and save it into dk_ioc.data,
2N/A * Later, we will read GUID partition entry array if we
2N/A * can get valid GPT header.
2N/A */
2N/A
2N/A /*
2N/A * This is a workaround for legacy systems. In the past, the
2N/A * last sector of SCSI disk was invisible on x86 platform. At
2N/A * that time, backup label was saved on the next to the last
2N/A * sector. It is possible for users to move a disk from previous
2N/A * solaris system to present system. Here, we attempt to search
2N/A * legacy backup EFI label first.
2N/A */
2N/A dk_ioc.dki_lba = disk_info.dki_capacity - 2;
2N/A dk_ioc.dki_length = disk_info.dki_lbsize;
2N/A rval = check_label(fd, &dk_ioc);
2N/A if (rval == VT_EINVAL) {
2N/A /*
2N/A * we didn't find legacy backup EFI label, try to
2N/A * search backup EFI label in the last block.
2N/A */
2N/A dk_ioc.dki_lba = disk_info.dki_capacity - 1;
2N/A dk_ioc.dki_length = disk_info.dki_lbsize;
2N/A rval = check_label(fd, &dk_ioc);
2N/A if (rval == 0) {
2N/A legacy_label = B_TRUE;
2N/A if (efi_debug)
2N/A (void) fprintf(stderr,
2N/A "efi_read: primary label corrupt; "
2N/A "using EFI backup label located on"
2N/A " the last block\n");
2N/A }
2N/A } else {
2N/A if ((efi_debug) && (rval == 0))
2N/A (void) fprintf(stderr, "efi_read: primary label"
2N/A " corrupt; using legacy EFI backup label "
2N/A " located on the next to last block\n");
2N/A }
2N/A
2N/A if (rval == 0) {
2N/A dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
2N/A vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
2N/A vtoc->efi_num_of_partition_entries =
2N/A LE_32(efi->efi_gpt_NumberOfPartitionEntries);
2N/A /*
2N/A * Partition tables are between backup GPT header
2N/A * table and ParitionEntryLBA (the starting LBA of
2N/A * the GUID partition entries array). Now that we
2N/A * already got valid GPT header and saved it in
2N/A * dk_ioc.dki_data, we try to get GUID partition
2N/A * entry array here.
2N/A */
2N/A /* LINTED */
2N/A dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
2N/A + disk_info.dki_lbsize);
2N/A if (legacy_label)
2N/A dk_ioc.dki_length = disk_info.dki_capacity - 1 -
2N/A dk_ioc.dki_lba;
2N/A else
2N/A dk_ioc.dki_length = disk_info.dki_capacity - 2 -
2N/A dk_ioc.dki_lba;
2N/A dk_ioc.dki_length *= disk_info.dki_lbsize;
2N/A if (dk_ioc.dki_length >
2N/A ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
2N/A rval = VT_EINVAL;
2N/A } else {
2N/A /*
2N/A * read GUID partition entry array
2N/A */
2N/A rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
2N/A }
2N/A }
2N/A
2N/A } else if (rval == 0) {
2N/A
2N/A dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
2N/A /* LINTED */
2N/A dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
2N/A + disk_info.dki_lbsize);
2N/A dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
2N/A rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
2N/A
2N/A } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
2N/A /*
2N/A * When the device is a LDoms virtual disk, the DKIOCGETEFI
2N/A * ioctl can fail with EINVAL if the virtual disk backend
2N/A * is a ZFS volume serviced by a domain running an old version
2N/A * of Solaris. This is because the DKIOCGETEFI ioctl was
2N/A * initially incorrectly implemented for a ZFS volume and it
2N/A * expected the GPT and GPE to be retrieved with a single ioctl.
2N/A * So we try to read the GPT and the GPE using that old style
2N/A * ioctl.
2N/A */
2N/A dk_ioc.dki_lba = 1;
2N/A dk_ioc.dki_length = label_len;
2N/A rval = check_label(fd, &dk_ioc);
2N/A }
2N/A
2N/A if (rval < 0) {
2N/A free(efi);
2N/A return (rval);
2N/A }
2N/A
2N/A /* LINTED -- always longlong aligned */
2N/A efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
2N/A
2N/A /*
2N/A * Assemble this into a "dk_gpt" struct for easier
2N/A * digestibility by applications.
2N/A */
2N/A vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
2N/A vtoc->efi_num_of_partition_entries =
2N/A LE_32(efi->efi_gpt_NumberOfPartitionEntries);
2N/A vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
2N/A vtoc->efi_lbasize = disk_info.dki_lbsize;
2N/A vtoc->efi_last_lba = disk_info.dki_capacity - 1;
2N/A vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
2N/A vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
2N/A vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
2N/A UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
2N/A
2N/A /*
2N/A * If the array the user passed in is too small, set the length
2N/A * to what it needs to be and return
2N/A */
2N/A if (user_length < vtoc->efi_num_of_partition_entries) {
2N/A return (VT_EINVAL);
2N/A }
2N/A
2N/A if (vtoc->efi_num_of_partition_entries <
2N/A EFI_MIN_NUMBER_PARTITION_ENTRIES) {
2N/A vtoc->efi_num_of_partition_entries =
2N/A EFI_MIN_NUMBER_PARTITION_ENTRIES;
2N/A }
2N/A
2N/A for (i = 0; i < vtoc->efi_num_of_partition_entries; i++) {
2N/A
2N/A UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
2N/A efi_parts[i].efi_gpe_PartitionTypeGUID);
2N/A
2N/A for (j = 0;
2N/A j < sizeof (conversion_array)
2N/A / sizeof (struct uuid_to_ptag); j++) {
2N/A
2N/A if (bcmp(&vtoc->efi_parts[i].p_guid,
2N/A &conversion_array[j].uuid,
2N/A sizeof (struct uuid)) == 0) {
2N/A vtoc->efi_parts[i].p_tag = j;
2N/A break;
2N/A }
2N/A }
2N/A if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED)) {
2N/A if (!uuid_is_null(
2N/A (uchar_t *)&vtoc->efi_parts[i].p_guid)) {
2N/A vtoc->efi_parts[i].p_tag = V_UNKNOWN;
2N/A
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "Found a partition "
2N/A "with unknown uuid,"
2N/A "set its p_tag to %d.\n",
2N/A V_UNKNOWN);
2N/A }
2N/A } else {
2N/A continue;
2N/A }
2N/A }
2N/A vtoc->efi_nparts = i;
2N/A vtoc->efi_parts[i].p_flag =
2N/A LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
2N/A vtoc->efi_parts[i].p_start =
2N/A LE_64(efi_parts[i].efi_gpe_StartingLBA);
2N/A
2N/A if (LE_64(efi_parts[i].efi_gpe_EndingLBA) == 0)
2N/A vtoc->efi_parts[i].p_size = 0;
2N/A else {
2N/A vtoc->efi_parts[i].p_size =
2N/A LE_64(efi_parts[i].efi_gpe_EndingLBA) -
2N/A vtoc->efi_parts[i].p_start + 1;
2N/A }
2N/A for (j = 0; j < EFI_PART_NAME_LEN; j++) {
2N/A vtoc->efi_parts[i].p_name[j] =
2N/A (uchar_t)LE_16(
2N/A efi_parts[i].efi_gpe_PartitionName[j]);
2N/A }
2N/A
2N/A UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
2N/A efi_parts[i].efi_gpe_UniquePartitionGUID);
2N/A }
2N/A vtoc->efi_nparts++;
2N/A free(efi);
2N/A
2N/A return (dki_info.dki_partition);
2N/A}
2N/A
2N/A#define MAX_URANDOM_RETRY 10
2N/A/* generate a unique 32-bit signature */
2N/Astatic void
2N/Apmbr_generate_unique_sig(uint32_t *sig)
2N/A{
2N/A int i, fd, retries = 0;
2N/A int nbytes = sizeof (uint32_t);
2N/A uchar_t *sigp = (uchar_t *)sig;
2N/A
2N/A if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
2N/A *sig = (uint32_t)gethrtime();
2N/A return;
2N/A }
2N/A
2N/A while (nbytes > 0) {
2N/A i = read(fd, sigp, nbytes);
2N/A if (i <= 0) {
2N/A if (retries++ == MAX_URANDOM_RETRY)
2N/A break;
2N/A continue;
2N/A }
2N/A
2N/A nbytes -= i;
2N/A sigp += i;
2N/A retries = 0;
2N/A }
2N/A
2N/A (void) close(fd);
2N/A
2N/A if (nbytes != 0)
2N/A *sig = (uint32_t)gethrtime();
2N/A}
2N/A
2N/A
2N/A
2N/A/* writes a "protective" MBR */
2N/Astatic int
2N/Awrite_pmbr(int fd, struct dk_gpt *vtoc)
2N/A{
2N/A dk_efi_t dk_ioc;
2N/A struct mboot mb;
2N/A uchar_t *cp;
2N/A diskaddr_t size_in_lba;
2N/A uchar_t *buf;
2N/A int len;
2N/A
2N/A len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
2N/A buf = calloc(len, 1);
2N/A
2N/A /*
2N/A * Preserve any boot code and disk signature if the first block is
2N/A * already an MBR.
2N/A */
2N/A dk_ioc.dki_lba = 0;
2N/A dk_ioc.dki_length = len;
2N/A /* LINTED -- always longlong aligned */
2N/A dk_ioc.dki_data = (efi_gpt_t *)buf;
2N/A if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
2N/A (void *) memcpy(&mb, buf, sizeof (mb));
2N/A bzero(&mb, sizeof (mb));
2N/A mb.signature = LE_16(MBB_MAGIC);
2N/A } else {
2N/A (void *) memcpy(&mb, buf, sizeof (mb));
2N/A if (mb.signature != LE_16(MBB_MAGIC)) {
2N/A bzero(&mb, sizeof (mb));
2N/A mb.signature = LE_16(MBB_MAGIC);
2N/A }
2N/A }
2N/A
2N/A if (mb.win_volserno == 0)
2N/A pmbr_generate_unique_sig(&mb.win_volserno);
2N/A
2N/A bzero(&mb.parts, sizeof (mb.parts));
2N/A cp = (uchar_t *)&mb.parts[0];
2N/A /* bootable or not */
2N/A *cp++ = 0;
2N/A /* beginning CHS; 0xffffff if not representable */
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A /* OS type */
2N/A *cp++ = EFI_PMBR;
2N/A /* ending CHS; 0xffffff if not representable */
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A /* starting LBA: 1 (little endian format) by EFI definition */
2N/A *cp++ = 0x01;
2N/A *cp++ = 0x00;
2N/A *cp++ = 0x00;
2N/A *cp++ = 0x00;
2N/A /* ending LBA: last block on the disk (little endian format) */
2N/A size_in_lba = vtoc->efi_last_lba;
2N/A if (size_in_lba < 0xffffffff) {
2N/A *cp++ = (size_in_lba & 0x000000ff);
2N/A *cp++ = (size_in_lba & 0x0000ff00) >> 8;
2N/A *cp++ = (size_in_lba & 0x00ff0000) >> 16;
2N/A *cp++ = (size_in_lba & 0xff000000) >> 24;
2N/A } else {
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A *cp++ = 0xff;
2N/A }
2N/A
2N/A (void *) memcpy(buf, &mb, sizeof (mb));
2N/A /* LINTED -- always longlong aligned */
2N/A dk_ioc.dki_data = (efi_gpt_t *)buf;
2N/A dk_ioc.dki_lba = 0;
2N/A dk_ioc.dki_length = len;
2N/A if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
2N/A free(buf);
2N/A switch (errno) {
2N/A case EIO:
2N/A return (VT_EIO);
2N/A case EINVAL:
2N/A return (VT_EINVAL);
2N/A default:
2N/A return (VT_ERROR);
2N/A }
2N/A }
2N/A free(buf);
2N/A return (0);
2N/A}
2N/A
2N/A/* make sure the user specified something reasonable */
2N/Astatic int
2N/Acheck_input(struct dk_gpt *vtoc)
2N/A{
2N/A int resv_part = -1;
2N/A int i, j;
2N/A diskaddr_t istart, jstart, isize, jsize, endsect;
2N/A int k, tmpp_tag;
2N/A
2N/A /* Sanity-check the input */
2N/A for (i = 0; i < vtoc->efi_nparts; i++) {
2N/A tmpp_tag = vtoc->efi_parts[i].p_tag;
2N/A
2N/A /* if the p_tag is V_UNKNOWN, we get it from p_guid */
2N/A if (tmpp_tag == V_UNKNOWN) {
2N/A for (k = 0;
2N/A k < sizeof (conversion_array)
2N/A / sizeof (struct uuid_to_ptag); k++) {
2N/A if (bcmp(&vtoc->efi_parts[i].p_guid,
2N/A &conversion_array[k].uuid,
2N/A sizeof (struct uuid)) == 0) {
2N/A tmpp_tag = k;
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* It can't be unassigned and have an actual size */
2N/A if ((tmpp_tag == V_UNASSIGNED) &&
2N/A (vtoc->efi_parts[i].p_size != 0)) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "partition %d is \"unassigned\" "
2N/A "but has a size of %llu", i,
2N/A vtoc->efi_parts[i].p_size);
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A
2N/A if (tmpp_tag == V_UNASSIGNED) {
2N/A if (!uuid_is_null(
2N/A (uchar_t *)&vtoc->efi_parts[i].p_guid)) {
2N/A if (efi_debug)
2N/A (void) fprintf(stderr,
2N/A "Mismatch between unassigned tag "
2N/A " and non-null uuid at partition "
2N/A " %d\n", i);
2N/A return (VT_EINVAL);
2N/A }
2N/A continue;
2N/A }
2N/A
2N/A if (tmpp_tag != V_RESERVED) {
2N/A if (i > 6) {
2N/A (void) fprintf(stderr, "Warning: Partition is "
2N/A "defined at index %d.\n Only "
2N/A "partitions 0 to 6 are directly accessible "
2N/A "through device minor nodes\n", i);
2N/A }
2N/A } else {
2N/A if (resv_part != -1) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr, "found "
2N/A "duplicate reserved partition at "
2N/A "%d\n", i);
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A resv_part = i;
2N/A if ((i == SOLARIS_WD_NODE) || (i > 8)) {
2N/A (void) fprintf(stderr, "Error: The Solaris "
2N/A "reserved partition (at %d) must be either "
2N/A "at partition 8 or partition 0 to 6\n", i);
2N/A return (VT_EINVAL);
2N/A }
2N/A }
2N/A
2N/A if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
2N/A (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "Partition %d starts at %llu. ",
2N/A i,
2N/A vtoc->efi_parts[i].p_start);
2N/A (void) fprintf(stderr,
2N/A "It must be between %llu and %llu.\n",
2N/A vtoc->efi_first_u_lba,
2N/A vtoc->efi_last_u_lba);
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A if ((vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size <
2N/A vtoc->efi_first_u_lba) ||
2N/A (vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size >
2N/A vtoc->efi_last_u_lba + 1)) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "Partition %d ends at %llu. ",
2N/A i,
2N/A vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size);
2N/A (void) fprintf(stderr,
2N/A "It must be between %llu and %llu.\n",
2N/A vtoc->efi_first_u_lba,
2N/A vtoc->efi_last_u_lba);
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A
2N/A for (j = 0; j < vtoc->efi_nparts; j++) {
2N/A isize = vtoc->efi_parts[i].p_size;
2N/A jsize = vtoc->efi_parts[j].p_size;
2N/A istart = vtoc->efi_parts[i].p_start;
2N/A jstart = vtoc->efi_parts[j].p_start;
2N/A if ((i != j) && (isize != 0) && (jsize != 0)) {
2N/A endsect = jstart + jsize -1;
2N/A if ((jstart <= istart) &&
2N/A (istart <= endsect)) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "Partition %d overlaps "
2N/A "partition %d.", i, j);
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A }
2N/A }
2N/A }
2N/A /* just a warning for now */
2N/A if ((resv_part == -1) && efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "no reserved partition found\n");
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * add all the unallocated space to the current label
2N/A */
2N/Aint
2N/Aefi_use_whole_disk(int fd)
2N/A{
2N/A struct dk_gpt *efi_label;
2N/A int rval;
2N/A int i;
2N/A int reserved_slice = -1;
2N/A int phy_last_slice = -1;
2N/A diskaddr_t pl_start = 0;
2N/A diskaddr_t pl_size = 0;
2N/A diskaddr_t reserved_start = 0;
2N/A diskaddr_t reserved_size = 0;
2N/A
2N/A rval = efi_alloc_and_read(fd, &efi_label);
2N/A if (rval < 0) {
2N/A return (rval);
2N/A }
2N/A
2N/A /* find the reserved partition */
2N/A for (i = 0; i < efi_label->efi_nparts; i++) {
2N/A if (efi_label->efi_parts[i].p_tag == V_RESERVED) {
2N/A reserved_slice = i;
2N/A reserved_start = efi_label->efi_parts[i].p_start;
2N/A reserved_size = efi_label->efi_parts[i].p_size;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (reserved_slice == -1) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "efi_use_whole_disk:reserved partition "
2N/A "not found\n");
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * find the last physically non-zero partition other than
2N/A * the reserved partition
2N/A */
2N/A for (i = 0; i < efi_label->efi_nparts; i++) {
2N/A if ((pl_start < efi_label->efi_parts[i].p_start) &&
2N/A (i != reserved_slice)) {
2N/A pl_start = efi_label->efi_parts[i].p_start;
2N/A pl_size = efi_label->efi_parts[i].p_size;
2N/A phy_last_slice = i;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If alter_lba is 1, we are using the backup label.
2N/A * Since we can locate the backup label by disk capacity,
2N/A * there must be no unallocated space.
2N/A */
2N/A if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
2N/A >= efi_label->efi_last_lba)) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "efi_use_whole_disk: requested space not found\n");
2N/A }
2N/A efi_free(efi_label);
2N/A return (VT_ENOSPC);
2N/A }
2N/A
2N/A /*
2N/A * If the reserved partition is right before the backup label, add the
2N/A * new space to the last physically non-zero area before the reserved
2N/A * partition.
2N/A */
2N/A if ((reserved_slice != -1) && (reserved_start + reserved_size - 1
2N/A == efi_label->efi_last_u_lba)) {
2N/A if ((phy_last_slice != -1) && (pl_start +
2N/A pl_size == reserved_start)) {
2N/A efi_label->efi_parts[phy_last_slice].p_size +=
2N/A efi_label->efi_last_lba - efi_label->efi_altern_lba;
2N/A }
2N/A /*
2N/A * Move the reserved partition. There is currently no data in
2N/A * here except fabricated devids (which get generated via
2N/A * efi_write()). So there is no need to copy data.
2N/A */
2N/A efi_label->efi_parts[reserved_slice].p_start +=
2N/A efi_label->efi_last_lba - efi_label->efi_altern_lba;
2N/A } else {
2N/A /*
2N/A * Otherwise, add the new space to the last physically non-zero
2N/A * area before the backup label.
2N/A */
2N/A if ((phy_last_slice != -1) && (pl_start + pl_size - 1
2N/A == efi_label->efi_last_u_lba)) {
2N/A efi_label->efi_parts[phy_last_slice].p_size +=
2N/A efi_label->efi_last_lba - efi_label->efi_altern_lba;
2N/A }
2N/A }
2N/A
2N/A efi_label->efi_last_u_lba += efi_label->efi_last_lba
2N/A - efi_label->efi_altern_lba;
2N/A
2N/A rval = efi_write(fd, efi_label);
2N/A if (rval < 0) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "efi_use_whole_disk:fail to write label, rval=%d\n",
2N/A rval);
2N/A }
2N/A efi_free(efi_label);
2N/A return (rval);
2N/A }
2N/A
2N/A efi_free(efi_label);
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * write EFI label and backup label
2N/A */
2N/Aint
2N/Aefi_write(int fd, struct dk_gpt *vtoc)
2N/A{
2N/A dk_efi_t dk_ioc;
2N/A efi_gpt_t *efi;
2N/A efi_gpe_t *efi_parts;
2N/A int i, j;
2N/A struct dk_cinfo dki_info;
2N/A int md_flag = 0;
2N/A uint_t num_of_partition_entries;
2N/A uint_t nparts;
2N/A int nblocks;
2N/A diskaddr_t lba_backup_gpt_hdr;
2N/A
2N/A if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
2N/A if (efi_debug)
2N/A (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
2N/A switch (errno) {
2N/A case EIO:
2N/A return (VT_EIO);
2N/A case EINVAL:
2N/A return (VT_EINVAL);
2N/A default:
2N/A return (VT_ERROR);
2N/A }
2N/A }
2N/A
2N/A /* check if we are dealing wih a metadevice */
2N/A if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
2N/A (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
2N/A md_flag = 1;
2N/A }
2N/A
2N/A if (check_input(vtoc)) {
2N/A /*
2N/A * not valid; if it's a metadevice just pass it down
2N/A * because SVM will do its own checking
2N/A */
2N/A if (md_flag == 0) {
2N/A return (VT_EINVAL);
2N/A }
2N/A }
2N/A
2N/A if ((vtoc->efi_nparts > vtoc->efi_num_of_partition_entries) &&
2N/A (vtoc->efi_num_of_partition_entries != 0)) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr, "The number of partitions in "
2N/A "label is out of the entries allocated on disk\n");
2N/A }
2N/A return (VT_EINVAL);
2N/A } else if (vtoc->efi_num_of_partition_entries == 0) {
2N/A nparts = vtoc->efi_nparts;
2N/A if (vtoc->efi_nparts <= EFI_MIN_NUMBER_PARTITION_ENTRIES) {
2N/A num_of_partition_entries =
2N/A EFI_MIN_NUMBER_PARTITION_ENTRIES;
2N/A } else {
2N/A num_of_partition_entries = vtoc->efi_nparts;
2N/A }
2N/A } else {
2N/A nparts = vtoc->efi_num_of_partition_entries;
2N/A num_of_partition_entries =
2N/A vtoc->efi_num_of_partition_entries;
2N/A }
2N/A for (i = 0; i < sizeof (vtoc->efi_reserved) / sizeof (uint_t); i++) {
2N/A if (vtoc->efi_reserved[i] != 0) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "The value of efi_reserved[12] "
2N/A "in the label must be set to zero\n");
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A }
2N/A
2N/A dk_ioc.dki_lba = 1;
2N/A if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
2N/A dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
2N/A } else {
2N/A dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
2N/A vtoc->efi_lbasize) *
2N/A vtoc->efi_lbasize;
2N/A }
2N/A
2N/A /*
2N/A * the number of blocks occupied by GUID partition entry array
2N/A */
2N/A nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
2N/A
2N/A /*
2N/A * Backup GPT header is located on the block after GUID
2N/A * partition entry array. Here, we calculate the address
2N/A * for backup GPT header.
2N/A */
2N/A lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
2N/A if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
2N/A return (VT_ERROR);
2N/A
2N/A efi = dk_ioc.dki_data;
2N/A
2N/A /* stuff user's input into EFI struct */
2N/A efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
2N/A efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
2N/A efi->efi_gpt_HeaderSize = LE_32(GPT_HEADER_SIZE);
2N/A efi->efi_gpt_Reserved1 = 0;
2N/A efi->efi_gpt_MyLBA = LE_64(1ULL);
2N/A efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
2N/A efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
2N/A efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
2N/A efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
2N/A efi->efi_gpt_NumberOfPartitionEntries =
2N/A LE_32(num_of_partition_entries);
2N/A efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
2N/A UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
2N/A
2N/A /* LINTED -- always longlong aligned */
2N/A efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
2N/A
2N/A for (i = 0; i < nparts; i++) {
2N/A
2N/A if (vtoc->efi_parts[i].p_tag == V_UNKNOWN) {
2N/A /* use uuid directly */
2N/A UUID_LE_CONVERT(efi_parts[i].efi_gpe_PartitionTypeGUID,
2N/A vtoc->efi_parts[i].p_guid);
2N/A } else {
2N/A
2N/A for (j = 0; j < sizeof (conversion_array) /
2N/A sizeof (struct uuid_to_ptag); j++) {
2N/A
2N/A if (vtoc->efi_parts[i].p_tag != j)
2N/A continue;
2N/A
2N/A UUID_LE_CONVERT(
2N/A efi_parts[i].efi_gpe_PartitionTypeGUID,
2N/A conversion_array[j].uuid);
2N/A break;
2N/A }
2N/A
2N/A if (j == sizeof (conversion_array) /
2N/A sizeof (struct uuid_to_ptag)) {
2N/A /*
2N/A * If we didn't have a matching uuid match,
2N/A * bail here.
2N/A * Don't write a label with unknown uuid.
2N/A */
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "Unknown uuid for p_tag %d\n",
2N/A vtoc->efi_parts[i].p_tag);
2N/A }
2N/A return (VT_EINVAL);
2N/A }
2N/A }
2N/A
2N/A efi_parts[i].efi_gpe_StartingLBA =
2N/A LE_64(vtoc->efi_parts[i].p_start);
2N/A efi_parts[i].efi_gpe_EndingLBA = 0;
2N/A if (vtoc->efi_parts[i].p_size != 0) {
2N/A efi_parts[i].efi_gpe_EndingLBA =
2N/A LE_64(vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size - 1);
2N/A }
2N/A efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
2N/A LE_16(vtoc->efi_parts[i].p_flag);
2N/A for (j = 0; j < EFI_PART_NAME_LEN; j++) {
2N/A efi_parts[i].efi_gpe_PartitionName[j] =
2N/A LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
2N/A }
2N/A if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
2N/A uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
2N/A (void) uuid_generate((uchar_t *)
2N/A &vtoc->efi_parts[i].p_uguid);
2N/A }
2N/A bcopy(&vtoc->efi_parts[i].p_uguid,
2N/A &efi_parts[i].efi_gpe_UniquePartitionGUID,
2N/A sizeof (uuid_t));
2N/A }
2N/A
2N/A efi->efi_gpt_PartitionEntryArrayCRC32 =
2N/A LE_32(efi_crc32((unsigned char *)efi_parts,
2N/A num_of_partition_entries * (int)sizeof (struct efi_gpe)));
2N/A efi->efi_gpt_HeaderCRC32 =
2N/A LE_32(efi_crc32((unsigned char *)efi, GPT_HEADER_SIZE));
2N/A
2N/A
2N/A if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
2N/A free(dk_ioc.dki_data);
2N/A switch (errno) {
2N/A case EIO:
2N/A return (VT_EIO);
2N/A case EINVAL:
2N/A return (VT_EINVAL);
2N/A default:
2N/A return (VT_ERROR);
2N/A }
2N/A }
2N/A /* if it's a metadevice we're done */
2N/A if (md_flag) {
2N/A free(dk_ioc.dki_data);
2N/A return (0);
2N/A }
2N/A
2N/A /* write backup partition array */
2N/A dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
2N/A dk_ioc.dki_length -= vtoc->efi_lbasize;
2N/A /* LINTED */
2N/A dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
2N/A vtoc->efi_lbasize);
2N/A
2N/A if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
2N/A /*
2N/A * we wrote the primary label okay, so don't fail
2N/A */
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "write of backup partitions to block %llu "
2N/A "failed, errno %d\n",
2N/A vtoc->efi_last_u_lba + 1,
2N/A errno);
2N/A }
2N/A }
2N/A /*
2N/A * now swap MyLBA and AlternateLBA fields and write backup
2N/A * partition table header
2N/A */
2N/A dk_ioc.dki_lba = lba_backup_gpt_hdr;
2N/A dk_ioc.dki_length = vtoc->efi_lbasize;
2N/A /* LINTED */
2N/A dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
2N/A vtoc->efi_lbasize);
2N/A efi->efi_gpt_AlternateLBA = LE_64(1ULL);
2N/A efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
2N/A efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
2N/A efi->efi_gpt_HeaderCRC32 = 0;
2N/A efi->efi_gpt_HeaderCRC32 =
2N/A LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
2N/A GPT_HEADER_SIZE));
2N/A
2N/A if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr,
2N/A "write of backup header to block %llu failed, "
2N/A "errno %d\n",
2N/A lba_backup_gpt_hdr,
2N/A errno);
2N/A }
2N/A }
2N/A /* write the PMBR */
2N/A (void) write_pmbr(fd, vtoc);
2N/A free(dk_ioc.dki_data);
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Aefi_free(struct dk_gpt *ptr)
2N/A{
2N/A free(ptr);
2N/A}
2N/A
2N/A/*
2N/A * Input: File descriptor
2N/A * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
2N/A * Otherwise 0.
2N/A */
2N/Aint
2N/Aefi_type(int fd)
2N/A{
2N/A struct vtoc vtoc;
2N/A struct extvtoc extvtoc;
2N/A
2N/A if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
2N/A if (errno == ENOTSUP)
2N/A return (1);
2N/A else if (errno == ENOTTY) {
2N/A if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
2N/A if (errno == ENOTSUP)
2N/A return (1);
2N/A }
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Aefi_err_check(struct dk_gpt *vtoc)
2N/A{
2N/A int resv_part = -1;
2N/A int i, j;
2N/A diskaddr_t istart, jstart, isize, jsize, endsect;
2N/A int overlap = 0;
2N/A
2N/A /*
2N/A * make sure no partitions overlap
2N/A */
2N/A for (i = 0; i < vtoc->efi_nparts; i++) {
2N/A /* It can't be unassigned and have an actual size */
2N/A if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
2N/A (vtoc->efi_parts[i].p_size != 0)) {
2N/A (void) fprintf(stderr,
2N/A "partition %d is \"unassigned\" but has a size "
2N/A "of %llu\n", i, vtoc->efi_parts[i].p_size);
2N/A }
2N/A if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
2N/A continue;
2N/A }
2N/A if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
2N/A if (resv_part != -1) {
2N/A (void) fprintf(stderr,
2N/A "found duplicate reserved partition at "
2N/A "%d\n", i);
2N/A }
2N/A resv_part = i;
2N/A if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
2N/A (void) fprintf(stderr,
2N/A "Warning: reserved partition size must "
2N/A "be %d sectors\n", EFI_MIN_RESV_SIZE);
2N/A }
2N/A if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
2N/A (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
2N/A (void) fprintf(stderr,
2N/A "Partition %d starts at %llu\n",
2N/A i,
2N/A vtoc->efi_parts[i].p_start);
2N/A (void) fprintf(stderr,
2N/A "It must be between %llu and %llu.\n",
2N/A vtoc->efi_first_u_lba,
2N/A vtoc->efi_last_u_lba);
2N/A }
2N/A if ((vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size <
2N/A vtoc->efi_first_u_lba) ||
2N/A (vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size >
2N/A vtoc->efi_last_u_lba + 1)) {
2N/A (void) fprintf(stderr,
2N/A "Partition %d ends at %llu\n",
2N/A i,
2N/A vtoc->efi_parts[i].p_start +
2N/A vtoc->efi_parts[i].p_size);
2N/A (void) fprintf(stderr,
2N/A "It must be between %llu and %llu.\n",
2N/A vtoc->efi_first_u_lba,
2N/A vtoc->efi_last_u_lba);
2N/A }
2N/A
2N/A for (j = 0; j < vtoc->efi_nparts; j++) {
2N/A isize = vtoc->efi_parts[i].p_size;
2N/A jsize = vtoc->efi_parts[j].p_size;
2N/A istart = vtoc->efi_parts[i].p_start;
2N/A jstart = vtoc->efi_parts[j].p_start;
2N/A if ((i != j) && (isize != 0) && (jsize != 0)) {
2N/A endsect = jstart + jsize -1;
2N/A if ((jstart <= istart) &&
2N/A (istart <= endsect)) {
2N/A if (!overlap) {
2N/A (void) fprintf(stderr,
2N/A "label error: EFI Labels do not "
2N/A "support overlapping partitions\n");
2N/A }
2N/A (void) fprintf(stderr,
2N/A "Partition %d overlaps partition "
2N/A "%d.\n", i, j);
2N/A overlap = 1;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A /* make sure there is a reserved partition */
2N/A if (resv_part == -1) {
2N/A (void) fprintf(stderr,
2N/A "no reserved partition found\n");
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * We need to get information necessary to construct a *new* efi
2N/A * label type
2N/A */
2N/Aint
2N/Aefi_auto_sense(int fd, struct dk_gpt **vtoc)
2N/A{
2N/A
2N/A int i;
2N/A
2N/A /*
2N/A * Now build the default partition table
2N/A */
2N/A if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
2N/A if (efi_debug) {
2N/A (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
2N/A }
2N/A return (-1);
2N/A }
2N/A
2N/A for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
2N/A (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
2N/A (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
2N/A (*vtoc)->efi_parts[i].p_start = 0;
2N/A (*vtoc)->efi_parts[i].p_size = 0;
2N/A }
2N/A /*
2N/A * Make constants first
2N/A * and variable partitions later
2N/A */
2N/A
2N/A /* root partition - s0 128 MB */
2N/A (*vtoc)->efi_parts[0].p_start = 34;
2N/A (*vtoc)->efi_parts[0].p_size = 262144;
2N/A
2N/A /* partition - s1 128 MB */
2N/A (*vtoc)->efi_parts[1].p_start = 262178;
2N/A (*vtoc)->efi_parts[1].p_size = 262144;
2N/A
2N/A /* partition -s2 is NOT the Backup disk */
2N/A (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
2N/A
2N/A /* partition -s6 /usr partition - HOG */
2N/A (*vtoc)->efi_parts[6].p_start = 524322;
2N/A (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
2N/A - (1024 * 16);
2N/A
2N/A /* efi reserved partition - s9 16K */
2N/A (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
2N/A (*vtoc)->efi_parts[8].p_size = (1024 * 16);
2N/A (*vtoc)->efi_parts[8].p_tag = V_RESERVED;
2N/A return (0);
2N/A}