4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <uuid/uuid.h>
34 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/efi_partition.h>
41 #include <sys/byteorder.h>
42 #if defined(__linux__)
46 static struct uuid_to_ptag {
48 } conversion_array[] = {
55 { EFI_UNUSED }, /* STAND is never used */
59 { EFI_UNUSED }, /* CACHE (cachefs) is never used */
76 * Default vtoc information for non-SVr4 partitions
78 struct dk_map2 default_vtoc_map[NDKMAP] = {
79 { V_ROOT, 0 }, /* a - 0 */
80 { V_SWAP, V_UNMNT }, /* b - 1 */
81 { V_BACKUP, V_UNMNT }, /* c - 2 */
82 { V_UNASSIGNED, 0 }, /* d - 3 */
83 { V_UNASSIGNED, 0 }, /* e - 4 */
84 { V_UNASSIGNED, 0 }, /* f - 5 */
85 { V_USR, 0 }, /* g - 6 */
86 { V_UNASSIGNED, 0 }, /* h - 7 */
88 #if defined(_SUNOS_VTOC_16)
90 #if defined(i386) || defined(__amd64) || defined(__arm)
91 { V_BOOT, V_UNMNT }, /* i - 8 */
92 { V_ALTSCTR, 0 }, /* j - 9 */
95 #error No VTOC format defined.
96 #endif /* defined(i386) */
98 { V_UNASSIGNED, 0 }, /* k - 10 */
99 { V_UNASSIGNED, 0 }, /* l - 11 */
100 { V_UNASSIGNED, 0 }, /* m - 12 */
101 { V_UNASSIGNED, 0 }, /* n - 13 */
102 { V_UNASSIGNED, 0 }, /* o - 14 */
103 { V_UNASSIGNED, 0 }, /* p - 15 */
104 #endif /* defined(_SUNOS_VTOC_16) */
113 static int efi_read(int, struct dk_gpt *);
116 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
117 * one's conditioning will be handled by crc32() internally.
120 efi_crc32(const unsigned char *buf, unsigned int size)
122 uint32_t crc = crc32(0, Z_NULL, 0);
124 crc = crc32(crc, buf, size);
130 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
133 unsigned long long capacity_size;
135 if (ioctl(fd, BLKSSZGET, §or_size) < 0)
138 if (ioctl(fd, BLKGETSIZE64, &capacity_size) < 0)
141 *lbsize = (uint_t)sector_size;
142 *capacity = (diskaddr_t)(capacity_size / sector_size);
148 efi_get_info(int fd, struct dk_cinfo *dki_info)
150 #if defined(__linux__)
155 memset(dki_info, 0, sizeof(*dki_info));
157 path = calloc(PATH_MAX, 1);
162 * The simplest way to get the partition number under linux is
163 * to parse it out of the /dev/<disk><parition> block device name.
164 * The kernel creates this using the partition number when it
165 * populates /dev/ so it may be trusted. The tricky bit here is
166 * that the naming convention is based on the block device type.
167 * So we need to take this in to account when parsing out the
168 * partition information. Another issue is that the libefi API
169 * API only provides the open fd and not the file path. To handle
170 * this realpath(3) is used to resolve the block device name from
171 * /proc/self/fd/<fd>. Aside from the partition number we collect
172 * some additional device info.
174 (void) sprintf(path, "/proc/self/fd/%d", fd);
175 dev_path = realpath(path, NULL);
178 if (dev_path == NULL)
181 if ((strncmp(dev_path, "/dev/sd", 7) == 0)) {
182 strcpy(dki_info->dki_cname, "sd");
183 dki_info->dki_ctype = DKC_SCSI_CCS;
184 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
186 &dki_info->dki_partition);
187 } else if ((strncmp(dev_path, "/dev/hd", 7) == 0)) {
188 strcpy(dki_info->dki_cname, "hd");
189 dki_info->dki_ctype = DKC_DIRECT;
190 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
192 &dki_info->dki_partition);
193 } else if ((strncmp(dev_path, "/dev/md", 7) == 0)) {
194 strcpy(dki_info->dki_cname, "pseudo");
195 dki_info->dki_ctype = DKC_MD;
196 rval = sscanf(dev_path, "/dev/%[a-zA-Z0-9]p%hu",
198 &dki_info->dki_partition);
199 } else if ((strncmp(dev_path, "/dev/vd", 7) == 0)) {
200 strcpy(dki_info->dki_cname, "vd");
201 dki_info->dki_ctype = DKC_MD;
202 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
204 &dki_info->dki_partition);
205 } else if ((strncmp(dev_path, "/dev/dm-", 8) == 0)) {
206 strcpy(dki_info->dki_cname, "pseudo");
207 dki_info->dki_ctype = DKC_VBD;
208 rval = sscanf(dev_path, "/dev/%[a-zA-Z0-9-]p%hu",
210 &dki_info->dki_partition);
211 } else if ((strncmp(dev_path, "/dev/ram", 8) == 0)) {
212 strcpy(dki_info->dki_cname, "pseudo");
213 dki_info->dki_ctype = DKC_PCMCIA_MEM;
214 rval = sscanf(dev_path, "/dev/%[a-zA-Z0-9]p%hu",
216 &dki_info->dki_partition);
217 } else if ((strncmp(dev_path, "/dev/loop", 9) == 0)) {
218 strcpy(dki_info->dki_cname, "pseudo");
219 dki_info->dki_ctype = DKC_VBD;
220 rval = sscanf(dev_path, "/dev/%[a-zA-Z0-9]p%hu",
222 &dki_info->dki_partition);
224 strcpy(dki_info->dki_dname, "unknown");
225 strcpy(dki_info->dki_cname, "unknown");
226 dki_info->dki_ctype = DKC_UNKNOWN;
234 dki_info->dki_partition = 0;
239 if (ioctl(fd, DKIOCINFO, (caddr_t)dki_info) == -1)
245 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
258 * the number of blocks the EFI label takes up (round up to nearest
261 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
263 /* number of partitions -- limited by what we can malloc */
264 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
265 sizeof (struct dk_part))
268 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
270 diskaddr_t capacity = 0;
276 struct dk_cinfo dki_info;
278 if (read_disk_info(fd, &capacity, &lbsize) != 0)
281 #if defined(__linux__)
282 if (efi_get_info(fd, &dki_info) != 0)
285 if (dki_info.dki_partition != 0)
288 if ((dki_info.dki_ctype == DKC_PCMCIA_MEM) ||
289 (dki_info.dki_ctype == DKC_VBD) ||
290 (dki_info.dki_ctype == DKC_UNKNOWN))
294 nblocks = NBLOCKS(nparts, lbsize);
295 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
296 /* 16K plus one block for the GPT */
297 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
300 if (nparts > MAX_PARTS) {
302 (void) fprintf(stderr,
303 "the maximum number of partitions supported is %lu\n",
309 length = sizeof (struct dk_gpt) +
310 sizeof (struct dk_part) * (nparts - 1);
312 if ((*vtoc = calloc(length, 1)) == NULL)
317 vptr->efi_version = EFI_VERSION_CURRENT;
318 vptr->efi_lbasize = lbsize;
319 vptr->efi_nparts = nparts;
321 * add one block here for the PMBR; on disks with a 512 byte
322 * block size and 128 or fewer partitions, efi_first_u_lba
323 * should work out to "34"
325 vptr->efi_first_u_lba = nblocks + 1;
326 vptr->efi_last_lba = capacity - 1;
327 vptr->efi_altern_lba = capacity -1;
328 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
330 (void) uuid_generate((uchar_t *)&uuid);
331 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
336 * Read EFI - return partition number upon success.
339 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
345 /* figure out the number of entries that would fit into 16K */
346 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
347 length = (int) sizeof (struct dk_gpt) +
348 (int) sizeof (struct dk_part) * (nparts - 1);
349 if ((*vtoc = calloc(length, 1)) == NULL)
352 (*vtoc)->efi_nparts = nparts;
353 rval = efi_read(fd, *vtoc);
355 if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
357 length = (int) sizeof (struct dk_gpt) +
358 (int) sizeof (struct dk_part) *
359 ((*vtoc)->efi_nparts - 1);
360 nparts = (*vtoc)->efi_nparts;
361 if ((tmp = realloc(*vtoc, length)) == NULL) {
367 rval = efi_read(fd, *vtoc);
373 (void) fprintf(stderr,
374 "read of EFI table failed, rval=%d\n", rval);
384 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
386 void *data = dk_ioc->dki_data;
388 #if defined(__linux__)
393 * When the IO is not being performed in kernel as an ioctl we need
394 * to know the sector size so we can seek to the proper byte offset.
396 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
398 fprintf(stderr,"unable to read disk info: %d",errno);
408 (void) fprintf(stderr, "DKIOCGETEFI assuming "
409 "LBA %d bytes\n", DEV_BSIZE);
414 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
417 (void) fprintf(stderr, "DKIOCGETEFI lseek "
418 "error: %d\n", errno);
422 error = read(fd, data, dk_ioc->dki_length);
425 (void) fprintf(stderr, "DKIOCGETEFI read "
426 "error: %d\n", errno);
430 if (error != dk_ioc->dki_length) {
432 (void) fprintf(stderr, "DKIOCGETEFI short "
433 "read of %d bytes\n", error);
443 (void) fprintf(stderr, "DKIOCSETEFI unknown "
449 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
452 (void) fprintf(stderr, "DKIOCSETEFI lseek "
453 "error: %d\n", errno);
457 error = write(fd, data, dk_ioc->dki_length);
460 (void) fprintf(stderr, "DKIOCSETEFI write "
461 "error: %d\n", errno);
465 if (error != dk_ioc->dki_length) {
467 (void) fprintf(stderr, "DKIOCSETEFI short "
468 "write of %d bytes\n", error);
473 /* Sync the new EFI table to disk */
478 /* Ensure any local disk cache is also flushed */
479 if (ioctl(fd, BLKFLSBUF, 0) == -1)
487 (void) fprintf(stderr, "unsupported ioctl()\n");
493 dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
494 error = ioctl(fd, cmd, (void *)dk_ioc);
495 dk_ioc->dki_data = data;
500 #if defined(__linux__)
507 /* Notify the kernel a devices partition table has been updated */
508 while ((error = ioctl(fd, BLKRRPART)) != 0) {
510 (void) fprintf(stderr, "the kernel failed to rescan "
511 "the partition table: %d\n", errno);
521 check_label(int fd, dk_efi_t *dk_ioc)
526 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
534 efi = dk_ioc->dki_data;
535 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
537 (void) fprintf(stderr,
538 "Bad EFI signature: 0x%llx != 0x%llx\n",
539 (long long)efi->efi_gpt_Signature,
540 (long long)LE_64(EFI_SIGNATURE));
545 * check CRC of the header; the size of the header should
546 * never be larger than one block
548 crc = efi->efi_gpt_HeaderCRC32;
549 efi->efi_gpt_HeaderCRC32 = 0;
550 len_t headerSize = (len_t)LE_32(efi->efi_gpt_HeaderSize);
552 if(headerSize < EFI_MIN_LABEL_SIZE || headerSize > EFI_LABEL_SIZE) {
554 (void) fprintf(stderr,
555 "Invalid EFI HeaderSize %llu. Assuming %d.\n",
556 headerSize, EFI_MIN_LABEL_SIZE);
559 if ((headerSize > dk_ioc->dki_length) ||
560 crc != LE_32(efi_crc32((unsigned char *)efi, headerSize))) {
562 (void) fprintf(stderr,
563 "Bad EFI CRC: 0x%x != 0x%x\n",
564 crc, LE_32(efi_crc32((unsigned char *)efi,
573 efi_read(int fd, struct dk_gpt *vtoc)
580 diskaddr_t capacity = 0;
582 struct dk_minfo disk_info;
585 efi_gpe_t *efi_parts;
586 struct dk_cinfo dki_info;
587 uint32_t user_length;
588 boolean_t legacy_label = B_FALSE;
591 * get the partition number for this file descriptor.
593 if ((rval = efi_get_info(fd, &dki_info)) != 0)
596 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
597 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
599 } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
600 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
602 * The controller and drive name "vdc" (virtual disk client)
603 * indicates a LDoms virtual disk.
608 /* get the LBA size */
609 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
611 (void) fprintf(stderr,
612 "unable to read disk info: %d",
618 disk_info.dki_lbsize = lbsize;
619 disk_info.dki_capacity = capacity;
621 if (disk_info.dki_lbsize == 0) {
623 (void) fprintf(stderr,
624 "efi_read: assuming LBA 512 bytes\n");
626 disk_info.dki_lbsize = DEV_BSIZE;
629 * Read the EFI GPT to figure out how many partitions we need
633 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
634 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
636 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
637 disk_info.dki_lbsize;
638 if (label_len % disk_info.dki_lbsize) {
639 /* pad to physical sector size */
640 label_len += disk_info.dki_lbsize;
641 label_len &= ~(disk_info.dki_lbsize - 1);
645 if (posix_memalign((void **)&dk_ioc.dki_data,
646 disk_info.dki_lbsize, label_len))
649 memset(dk_ioc.dki_data, 0, label_len);
650 dk_ioc.dki_length = disk_info.dki_lbsize;
651 user_length = vtoc->efi_nparts;
652 efi = dk_ioc.dki_data;
654 dk_ioc.dki_length = label_len;
655 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
663 } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
665 * No valid label here; try the alternate. Note that here
666 * we just read GPT header and save it into dk_ioc.data,
667 * Later, we will read GUID partition entry array if we
668 * can get valid GPT header.
672 * This is a workaround for legacy systems. In the past, the
673 * last sector of SCSI disk was invisible on x86 platform. At
674 * that time, backup label was saved on the next to the last
675 * sector. It is possible for users to move a disk from previous
676 * solaris system to present system. Here, we attempt to search
677 * legacy backup EFI label first.
679 dk_ioc.dki_lba = disk_info.dki_capacity - 2;
680 dk_ioc.dki_length = disk_info.dki_lbsize;
681 rval = check_label(fd, &dk_ioc);
682 if (rval == VT_EINVAL) {
684 * we didn't find legacy backup EFI label, try to
685 * search backup EFI label in the last block.
687 dk_ioc.dki_lba = disk_info.dki_capacity - 1;
688 dk_ioc.dki_length = disk_info.dki_lbsize;
689 rval = check_label(fd, &dk_ioc);
691 legacy_label = B_TRUE;
693 (void) fprintf(stderr,
694 "efi_read: primary label corrupt; "
695 "using EFI backup label located on"
696 " the last block\n");
699 if ((efi_debug) && (rval == 0))
700 (void) fprintf(stderr, "efi_read: primary label"
701 " corrupt; using legacy EFI backup label "
702 " located on the next to last block\n");
706 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
707 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
709 LE_32(efi->efi_gpt_NumberOfPartitionEntries);
711 * Partition tables are between backup GPT header
712 * table and ParitionEntryLBA (the starting LBA of
713 * the GUID partition entries array). Now that we
714 * already got valid GPT header and saved it in
715 * dk_ioc.dki_data, we try to get GUID partition
719 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
720 + disk_info.dki_lbsize);
722 dk_ioc.dki_length = disk_info.dki_capacity - 1 -
725 dk_ioc.dki_length = disk_info.dki_capacity - 2 -
727 dk_ioc.dki_length *= disk_info.dki_lbsize;
728 if (dk_ioc.dki_length >
729 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
733 * read GUID partition entry array
735 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
739 } else if (rval == 0) {
741 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
743 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
744 + disk_info.dki_lbsize);
745 dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
746 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
748 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
750 * When the device is a LDoms virtual disk, the DKIOCGETEFI
751 * ioctl can fail with EINVAL if the virtual disk backend
752 * is a ZFS volume serviced by a domain running an old version
753 * of Solaris. This is because the DKIOCGETEFI ioctl was
754 * initially incorrectly implemented for a ZFS volume and it
755 * expected the GPT and GPE to be retrieved with a single ioctl.
756 * So we try to read the GPT and the GPE using that old style
760 dk_ioc.dki_length = label_len;
761 rval = check_label(fd, &dk_ioc);
769 /* LINTED -- always longlong aligned */
770 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
773 * Assemble this into a "dk_gpt" struct for easier
774 * digestibility by applications.
776 vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
777 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
778 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
779 vtoc->efi_lbasize = disk_info.dki_lbsize;
780 vtoc->efi_last_lba = disk_info.dki_capacity - 1;
781 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
782 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
783 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
784 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
787 * If the array the user passed in is too small, set the length
788 * to what it needs to be and return
790 if (user_length < vtoc->efi_nparts) {
794 for (i = 0; i < vtoc->efi_nparts; i++) {
796 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
797 efi_parts[i].efi_gpe_PartitionTypeGUID);
800 j < sizeof (conversion_array)
801 / sizeof (struct uuid_to_ptag); j++) {
803 if (bcmp(&vtoc->efi_parts[i].p_guid,
804 &conversion_array[j].uuid,
805 sizeof (struct uuid)) == 0) {
806 vtoc->efi_parts[i].p_tag = j;
810 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
812 vtoc->efi_parts[i].p_flag =
813 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
814 vtoc->efi_parts[i].p_start =
815 LE_64(efi_parts[i].efi_gpe_StartingLBA);
816 vtoc->efi_parts[i].p_size =
817 LE_64(efi_parts[i].efi_gpe_EndingLBA) -
818 vtoc->efi_parts[i].p_start + 1;
819 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
820 vtoc->efi_parts[i].p_name[j] =
822 efi_parts[i].efi_gpe_PartitionName[j]);
825 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
826 efi_parts[i].efi_gpe_UniquePartitionGUID);
830 return (dki_info.dki_partition);
833 /* writes a "protective" MBR */
835 write_pmbr(int fd, struct dk_gpt *vtoc)
840 diskaddr_t size_in_lba;
844 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
845 if (posix_memalign((void **)&buf, len, len))
849 * Preserve any boot code and disk signature if the first block is
854 dk_ioc.dki_length = len;
855 /* LINTED -- always longlong aligned */
856 dk_ioc.dki_data = (efi_gpt_t *)buf;
857 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
858 (void *) memcpy(&mb, buf, sizeof (mb));
859 bzero(&mb, sizeof (mb));
860 mb.signature = LE_16(MBB_MAGIC);
862 (void *) memcpy(&mb, buf, sizeof (mb));
863 if (mb.signature != LE_16(MBB_MAGIC)) {
864 bzero(&mb, sizeof (mb));
865 mb.signature = LE_16(MBB_MAGIC);
869 bzero(&mb.parts, sizeof (mb.parts));
870 cp = (uchar_t *)&mb.parts[0];
871 /* bootable or not */
873 /* beginning CHS; 0xffffff if not representable */
879 /* ending CHS; 0xffffff if not representable */
883 /* starting LBA: 1 (little endian format) by EFI definition */
888 /* ending LBA: last block on the disk (little endian format) */
889 size_in_lba = vtoc->efi_last_lba;
890 if (size_in_lba < 0xffffffff) {
891 *cp++ = (size_in_lba & 0x000000ff);
892 *cp++ = (size_in_lba & 0x0000ff00) >> 8;
893 *cp++ = (size_in_lba & 0x00ff0000) >> 16;
894 *cp++ = (size_in_lba & 0xff000000) >> 24;
902 (void *) memcpy(buf, &mb, sizeof (mb));
903 /* LINTED -- always longlong aligned */
904 dk_ioc.dki_data = (efi_gpt_t *)buf;
906 dk_ioc.dki_length = len;
907 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
922 /* make sure the user specified something reasonable */
924 check_input(struct dk_gpt *vtoc)
928 diskaddr_t istart, jstart, isize, jsize, endsect;
931 * Sanity-check the input (make sure no partitions overlap)
933 for (i = 0; i < vtoc->efi_nparts; i++) {
934 /* It can't be unassigned and have an actual size */
935 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
936 (vtoc->efi_parts[i].p_size != 0)) {
938 (void) fprintf(stderr, "partition %d is "
939 "\"unassigned\" but has a size of %llu",
940 i, vtoc->efi_parts[i].p_size);
944 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
945 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
947 /* we have encountered an unknown uuid */
948 vtoc->efi_parts[i].p_tag = 0xff;
950 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
951 if (resv_part != -1) {
953 (void) fprintf(stderr, "found "
954 "duplicate reserved partition "
961 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
962 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
964 (void) fprintf(stderr,
965 "Partition %d starts at %llu. ",
967 vtoc->efi_parts[i].p_start);
968 (void) fprintf(stderr,
969 "It must be between %llu and %llu.\n",
970 vtoc->efi_first_u_lba,
971 vtoc->efi_last_u_lba);
975 if ((vtoc->efi_parts[i].p_start +
976 vtoc->efi_parts[i].p_size <
977 vtoc->efi_first_u_lba) ||
978 (vtoc->efi_parts[i].p_start +
979 vtoc->efi_parts[i].p_size >
980 vtoc->efi_last_u_lba + 1)) {
982 (void) fprintf(stderr,
983 "Partition %d ends at %llu. ",
985 vtoc->efi_parts[i].p_start +
986 vtoc->efi_parts[i].p_size);
987 (void) fprintf(stderr,
988 "It must be between %llu and %llu.\n",
989 vtoc->efi_first_u_lba,
990 vtoc->efi_last_u_lba);
995 for (j = 0; j < vtoc->efi_nparts; j++) {
996 isize = vtoc->efi_parts[i].p_size;
997 jsize = vtoc->efi_parts[j].p_size;
998 istart = vtoc->efi_parts[i].p_start;
999 jstart = vtoc->efi_parts[j].p_start;
1000 if ((i != j) && (isize != 0) && (jsize != 0)) {
1001 endsect = jstart + jsize -1;
1002 if ((jstart <= istart) &&
1003 (istart <= endsect)) {
1005 (void) fprintf(stderr,
1006 "Partition %d overlaps "
1007 "partition %d.", i, j);
1014 /* just a warning for now */
1015 if ((resv_part == -1) && efi_debug) {
1016 (void) fprintf(stderr,
1017 "no reserved partition found\n");
1023 * add all the unallocated space to the current label
1026 efi_use_whole_disk(int fd)
1028 struct dk_gpt *efi_label;
1031 uint_t phy_last_slice = 0;
1032 diskaddr_t pl_start = 0;
1035 rval = efi_alloc_and_read(fd, &efi_label);
1040 /* find the last physically non-zero partition */
1041 for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
1042 if (pl_start < efi_label->efi_parts[i].p_start) {
1043 pl_start = efi_label->efi_parts[i].p_start;
1047 pl_size = efi_label->efi_parts[phy_last_slice].p_size;
1050 * If alter_lba is 1, we are using the backup label.
1051 * Since we can locate the backup label by disk capacity,
1052 * there must be no unallocated space.
1054 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
1055 >= efi_label->efi_last_lba)) {
1057 (void) fprintf(stderr,
1058 "efi_use_whole_disk: requested space not found\n");
1060 efi_free(efi_label);
1065 * If there is space between the last physically non-zero partition
1066 * and the reserved partition, just add the unallocated space to this
1067 * area. Otherwise, the unallocated space is added to the last
1068 * physically non-zero partition.
1070 if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
1071 EFI_MIN_RESV_SIZE) {
1072 efi_label->efi_parts[phy_last_slice].p_size +=
1073 efi_label->efi_last_lba - efi_label->efi_altern_lba;
1077 * Move the reserved partition. There is currently no data in
1078 * here except fabricated devids (which get generated via
1079 * efi_write()). So there is no need to copy data.
1081 efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
1082 efi_label->efi_last_lba - efi_label->efi_altern_lba;
1083 efi_label->efi_last_u_lba += efi_label->efi_last_lba
1084 - efi_label->efi_altern_lba;
1086 rval = efi_write(fd, efi_label);
1089 (void) fprintf(stderr,
1090 "efi_use_whole_disk:fail to write label, rval=%d\n",
1093 efi_free(efi_label);
1097 efi_free(efi_label);
1103 * write EFI label and backup label
1106 efi_write(int fd, struct dk_gpt *vtoc)
1110 efi_gpe_t *efi_parts;
1112 struct dk_cinfo dki_info;
1116 diskaddr_t lba_backup_gpt_hdr;
1118 if ((rval = efi_get_info(fd, &dki_info)) != 0)
1121 /* check if we are dealing wih a metadevice */
1122 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
1123 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
1127 if (check_input(vtoc)) {
1129 * not valid; if it's a metadevice just pass it down
1130 * because SVM will do its own checking
1138 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
1139 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
1141 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
1142 vtoc->efi_lbasize) *
1147 * the number of blocks occupied by GUID partition entry array
1149 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
1152 * Backup GPT header is located on the block after GUID
1153 * partition entry array. Here, we calculate the address
1154 * for backup GPT header.
1156 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
1157 if (posix_memalign((void **)&dk_ioc.dki_data,
1158 vtoc->efi_lbasize, dk_ioc.dki_length))
1161 memset(dk_ioc.dki_data, 0, dk_ioc.dki_length);
1162 efi = dk_ioc.dki_data;
1164 /* stuff user's input into EFI struct */
1165 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1166 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
1167 efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt) - LEN_EFI_PAD);
1168 efi->efi_gpt_Reserved1 = 0;
1169 efi->efi_gpt_MyLBA = LE_64(1ULL);
1170 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
1171 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
1172 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
1173 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1174 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
1175 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
1176 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
1178 /* LINTED -- always longlong aligned */
1179 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
1181 for (i = 0; i < vtoc->efi_nparts; i++) {
1183 j < sizeof (conversion_array) /
1184 sizeof (struct uuid_to_ptag); j++) {
1186 if (vtoc->efi_parts[i].p_tag == j) {
1188 efi_parts[i].efi_gpe_PartitionTypeGUID,
1189 conversion_array[j].uuid);
1194 if (j == sizeof (conversion_array) /
1195 sizeof (struct uuid_to_ptag)) {
1197 * If we didn't have a matching uuid match, bail here.
1198 * Don't write a label with unknown uuid.
1201 (void) fprintf(stderr,
1202 "Unknown uuid for p_tag %d\n",
1203 vtoc->efi_parts[i].p_tag);
1208 /* Zero's should be written for empty partitions */
1209 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
1212 efi_parts[i].efi_gpe_StartingLBA =
1213 LE_64(vtoc->efi_parts[i].p_start);
1214 efi_parts[i].efi_gpe_EndingLBA =
1215 LE_64(vtoc->efi_parts[i].p_start +
1216 vtoc->efi_parts[i].p_size - 1);
1217 efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
1218 LE_16(vtoc->efi_parts[i].p_flag);
1219 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1220 efi_parts[i].efi_gpe_PartitionName[j] =
1221 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1223 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1224 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1225 (void) uuid_generate((uchar_t *)
1226 &vtoc->efi_parts[i].p_uguid);
1228 bcopy(&vtoc->efi_parts[i].p_uguid,
1229 &efi_parts[i].efi_gpe_UniquePartitionGUID,
1232 efi->efi_gpt_PartitionEntryArrayCRC32 =
1233 LE_32(efi_crc32((unsigned char *)efi_parts,
1234 vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1235 efi->efi_gpt_HeaderCRC32 =
1236 LE_32(efi_crc32((unsigned char *)efi,
1237 LE_32(efi->efi_gpt_HeaderSize)));
1239 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1240 free(dk_ioc.dki_data);
1250 /* if it's a metadevice we're done */
1252 free(dk_ioc.dki_data);
1256 /* write backup partition array */
1257 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
1258 dk_ioc.dki_length -= vtoc->efi_lbasize;
1260 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
1263 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1265 * we wrote the primary label okay, so don't fail
1268 (void) fprintf(stderr,
1269 "write of backup partitions to block %llu "
1270 "failed, errno %d\n",
1271 vtoc->efi_last_u_lba + 1,
1276 * now swap MyLBA and AlternateLBA fields and write backup
1277 * partition table header
1279 dk_ioc.dki_lba = lba_backup_gpt_hdr;
1280 dk_ioc.dki_length = vtoc->efi_lbasize;
1282 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1284 efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1285 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1286 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1287 efi->efi_gpt_HeaderCRC32 = 0;
1288 efi->efi_gpt_HeaderCRC32 =
1289 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1290 LE_32(efi->efi_gpt_HeaderSize)));
1292 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1294 (void) fprintf(stderr,
1295 "write of backup header to block %llu failed, "
1301 /* write the PMBR */
1302 (void) write_pmbr(fd, vtoc);
1303 free(dk_ioc.dki_data);
1305 #if defined(__linux__)
1306 rval = efi_rescan(fd);
1315 efi_free(struct dk_gpt *ptr)
1321 * Input: File descriptor
1322 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1330 struct extvtoc extvtoc;
1332 if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1333 if (errno == ENOTSUP)
1335 else if (errno == ENOTTY) {
1336 if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1337 if (errno == ENOTSUP)
1348 efi_err_check(struct dk_gpt *vtoc)
1352 diskaddr_t istart, jstart, isize, jsize, endsect;
1356 * make sure no partitions overlap
1358 for (i = 0; i < vtoc->efi_nparts; i++) {
1359 /* It can't be unassigned and have an actual size */
1360 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1361 (vtoc->efi_parts[i].p_size != 0)) {
1362 (void) fprintf(stderr,
1363 "partition %d is \"unassigned\" but has a size "
1364 "of %llu\n", i, vtoc->efi_parts[i].p_size);
1366 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1369 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1370 if (resv_part != -1) {
1371 (void) fprintf(stderr,
1372 "found duplicate reserved partition at "
1376 if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1377 (void) fprintf(stderr,
1378 "Warning: reserved partition size must "
1379 "be %d sectors\n", EFI_MIN_RESV_SIZE);
1381 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1382 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1383 (void) fprintf(stderr,
1384 "Partition %d starts at %llu\n",
1386 vtoc->efi_parts[i].p_start);
1387 (void) fprintf(stderr,
1388 "It must be between %llu and %llu.\n",
1389 vtoc->efi_first_u_lba,
1390 vtoc->efi_last_u_lba);
1392 if ((vtoc->efi_parts[i].p_start +
1393 vtoc->efi_parts[i].p_size <
1394 vtoc->efi_first_u_lba) ||
1395 (vtoc->efi_parts[i].p_start +
1396 vtoc->efi_parts[i].p_size >
1397 vtoc->efi_last_u_lba + 1)) {
1398 (void) fprintf(stderr,
1399 "Partition %d ends at %llu\n",
1401 vtoc->efi_parts[i].p_start +
1402 vtoc->efi_parts[i].p_size);
1403 (void) fprintf(stderr,
1404 "It must be between %llu and %llu.\n",
1405 vtoc->efi_first_u_lba,
1406 vtoc->efi_last_u_lba);
1409 for (j = 0; j < vtoc->efi_nparts; j++) {
1410 isize = vtoc->efi_parts[i].p_size;
1411 jsize = vtoc->efi_parts[j].p_size;
1412 istart = vtoc->efi_parts[i].p_start;
1413 jstart = vtoc->efi_parts[j].p_start;
1414 if ((i != j) && (isize != 0) && (jsize != 0)) {
1415 endsect = jstart + jsize -1;
1416 if ((jstart <= istart) &&
1417 (istart <= endsect)) {
1419 (void) fprintf(stderr,
1420 "label error: EFI Labels do not "
1421 "support overlapping partitions\n");
1423 (void) fprintf(stderr,
1424 "Partition %d overlaps partition "
1431 /* make sure there is a reserved partition */
1432 if (resv_part == -1) {
1433 (void) fprintf(stderr,
1434 "no reserved partition found\n");
1439 * We need to get information necessary to construct a *new* efi
1443 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1449 * Now build the default partition table
1451 if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1453 (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1458 for (i = 0; i < MIN((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1459 (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1460 (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1461 (*vtoc)->efi_parts[i].p_start = 0;
1462 (*vtoc)->efi_parts[i].p_size = 0;
1465 * Make constants first
1466 * and variable partitions later
1469 /* root partition - s0 128 MB */
1470 (*vtoc)->efi_parts[0].p_start = 34;
1471 (*vtoc)->efi_parts[0].p_size = 262144;
1473 /* partition - s1 128 MB */
1474 (*vtoc)->efi_parts[1].p_start = 262178;
1475 (*vtoc)->efi_parts[1].p_size = 262144;
1477 /* partition -s2 is NOT the Backup disk */
1478 (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1480 /* partition -s6 /usr partition - HOG */
1481 (*vtoc)->efi_parts[6].p_start = 524322;
1482 (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1485 /* efi reserved partition - s9 16K */
1486 (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1487 (*vtoc)->efi_parts[8].p_size = (1024 * 16);
1488 (*vtoc)->efi_parts[8].p_tag = V_RESERVED;