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.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2018 by Delphix. All rights reserved.
34 #include <uuid/uuid.h>
37 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/dktp/fdisk.h>
43 #include <sys/efi_partition.h>
44 #include <sys/byteorder.h>
47 static struct uuid_to_ptag {
49 } conversion_array[] = {
56 { EFI_UNUSED }, /* STAND is never used */
60 { EFI_UNUSED }, /* CACHE (cachefs) is never used */
77 { EFI_FREEBSD_VINUM },
87 { EFI_MSFT_STORAGESPACES },
95 { EFI_FREEBSD_DISKLABEL },
97 { EFI_AAPL_RAIDOFFLINE },
100 { EFI_AAPL_TVRECOVERY },
101 { EFI_AAPL_CORESTORAGE },
107 { EFI_NETBSD_CRYPT },
112 { EFI_MIDNIGHTBSD_BOOT },
113 { EFI_MIDNIGHTBSD_DATA },
114 { EFI_MIDNIGHTBSD_SWAP },
115 { EFI_MIDNIGHTBSD_UFS },
116 { EFI_MIDNIGHTBSD_VINUM },
117 { EFI_MIDNIGHTBSD_ZFS },
118 { EFI_CEPH_JOURNAL },
119 { EFI_CEPH_DMCRYPTJOURNAL },
121 { EFI_CEPH_DMCRYPTOSD },
123 { EFI_CEPH_DMCRYPTCREATE },
124 { EFI_OPENBSD_DISKLABEL },
131 { EFI_RHT_ROOTAMD64 },
133 { EFI_RHT_ROOTARM64 },
134 { EFI_ACRONIS_SECUREZONE },
137 { EFI_IBM_PPRPBOOT },
138 { EFI_FREEDESKTOP_BOOT }
142 * Default vtoc information for non-SVr4 partitions
144 struct dk_map2 default_vtoc_map[NDKMAP] = {
145 { V_ROOT, 0 }, /* a - 0 */
146 { V_SWAP, V_UNMNT }, /* b - 1 */
147 { V_BACKUP, V_UNMNT }, /* c - 2 */
148 { V_UNASSIGNED, 0 }, /* d - 3 */
149 { V_UNASSIGNED, 0 }, /* e - 4 */
150 { V_UNASSIGNED, 0 }, /* f - 5 */
151 { V_USR, 0 }, /* g - 6 */
152 { V_UNASSIGNED, 0 }, /* h - 7 */
154 #if defined(_SUNOS_VTOC_16)
156 #if defined(i386) || defined(__amd64) || defined(__arm) || \
157 defined(__powerpc) || defined(__sparc) || defined(__s390__) || \
159 { V_BOOT, V_UNMNT }, /* i - 8 */
160 { V_ALTSCTR, 0 }, /* j - 9 */
163 #error No VTOC format defined.
164 #endif /* defined(i386) */
166 { V_UNASSIGNED, 0 }, /* k - 10 */
167 { V_UNASSIGNED, 0 }, /* l - 11 */
168 { V_UNASSIGNED, 0 }, /* m - 12 */
169 { V_UNASSIGNED, 0 }, /* n - 13 */
170 { V_UNASSIGNED, 0 }, /* o - 14 */
171 { V_UNASSIGNED, 0 }, /* p - 15 */
172 #endif /* defined(_SUNOS_VTOC_16) */
177 static int efi_read(int, struct dk_gpt *);
180 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
181 * one's conditioning will be handled by crc32() internally.
184 efi_crc32(const unsigned char *buf, unsigned int size)
186 uint32_t crc = crc32(0, Z_NULL, 0);
188 crc = crc32(crc, buf, size);
194 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
197 unsigned long long capacity_size;
199 if (ioctl(fd, BLKSSZGET, §or_size) < 0)
202 if (ioctl(fd, BLKGETSIZE64, &capacity_size) < 0)
205 *lbsize = (uint_t)sector_size;
206 *capacity = (diskaddr_t)(capacity_size / sector_size);
212 efi_get_info(int fd, struct dk_cinfo *dki_info)
218 memset(dki_info, 0, sizeof (*dki_info));
220 path = calloc(1, PATH_MAX);
225 * The simplest way to get the partition number under linux is
226 * to parse it out of the /dev/<disk><parition> block device name.
227 * The kernel creates this using the partition number when it
228 * populates /dev/ so it may be trusted. The tricky bit here is
229 * that the naming convention is based on the block device type.
230 * So we need to take this in to account when parsing out the
231 * partition information. Another issue is that the libefi API
232 * API only provides the open fd and not the file path. To handle
233 * this realpath(3) is used to resolve the block device name from
234 * /proc/self/fd/<fd>. Aside from the partition number we collect
235 * some additional device info.
237 (void) sprintf(path, "/proc/self/fd/%d", fd);
238 dev_path = realpath(path, NULL);
241 if (dev_path == NULL)
244 if ((strncmp(dev_path, "/dev/sd", 7) == 0)) {
245 strcpy(dki_info->dki_cname, "sd");
246 dki_info->dki_ctype = DKC_SCSI_CCS;
247 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
249 &dki_info->dki_partition);
250 } else if ((strncmp(dev_path, "/dev/hd", 7) == 0)) {
251 strcpy(dki_info->dki_cname, "hd");
252 dki_info->dki_ctype = DKC_DIRECT;
253 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
255 &dki_info->dki_partition);
256 } else if ((strncmp(dev_path, "/dev/md", 7) == 0)) {
257 strcpy(dki_info->dki_cname, "pseudo");
258 dki_info->dki_ctype = DKC_MD;
259 strcpy(dki_info->dki_dname, "md");
260 rval = sscanf(dev_path, "/dev/md%[0-9]p%hu",
261 dki_info->dki_dname + 2,
262 &dki_info->dki_partition);
263 } else if ((strncmp(dev_path, "/dev/vd", 7) == 0)) {
264 strcpy(dki_info->dki_cname, "vd");
265 dki_info->dki_ctype = DKC_MD;
266 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
268 &dki_info->dki_partition);
269 } else if ((strncmp(dev_path, "/dev/xvd", 8) == 0)) {
270 strcpy(dki_info->dki_cname, "xvd");
271 dki_info->dki_ctype = DKC_MD;
272 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
274 &dki_info->dki_partition);
275 } else if ((strncmp(dev_path, "/dev/zd", 7) == 0)) {
276 strcpy(dki_info->dki_cname, "zd");
277 dki_info->dki_ctype = DKC_MD;
278 strcpy(dki_info->dki_dname, "zd");
279 rval = sscanf(dev_path, "/dev/zd%[0-9]p%hu",
280 dki_info->dki_dname + 2,
281 &dki_info->dki_partition);
282 } else if ((strncmp(dev_path, "/dev/dm-", 8) == 0)) {
283 strcpy(dki_info->dki_cname, "pseudo");
284 dki_info->dki_ctype = DKC_VBD;
285 strcpy(dki_info->dki_dname, "dm-");
286 rval = sscanf(dev_path, "/dev/dm-%[0-9]p%hu",
287 dki_info->dki_dname + 3,
288 &dki_info->dki_partition);
289 } else if ((strncmp(dev_path, "/dev/ram", 8) == 0)) {
290 strcpy(dki_info->dki_cname, "pseudo");
291 dki_info->dki_ctype = DKC_PCMCIA_MEM;
292 strcpy(dki_info->dki_dname, "ram");
293 rval = sscanf(dev_path, "/dev/ram%[0-9]p%hu",
294 dki_info->dki_dname + 3,
295 &dki_info->dki_partition);
296 } else if ((strncmp(dev_path, "/dev/loop", 9) == 0)) {
297 strcpy(dki_info->dki_cname, "pseudo");
298 dki_info->dki_ctype = DKC_VBD;
299 strcpy(dki_info->dki_dname, "loop");
300 rval = sscanf(dev_path, "/dev/loop%[0-9]p%hu",
301 dki_info->dki_dname + 4,
302 &dki_info->dki_partition);
303 } else if ((strncmp(dev_path, "/dev/nvme", 9) == 0)) {
304 strcpy(dki_info->dki_cname, "nvme");
305 dki_info->dki_ctype = DKC_SCSI_CCS;
306 strcpy(dki_info->dki_dname, "nvme");
307 (void) sscanf(dev_path, "/dev/nvme%[0-9]",
308 dki_info->dki_dname + 4);
309 size_t controller_length = strlen(
310 dki_info->dki_dname);
311 strcpy(dki_info->dki_dname + controller_length,
313 rval = sscanf(dev_path,
314 "/dev/nvme%*[0-9]n%[0-9]p%hu",
315 dki_info->dki_dname + controller_length + 1,
316 &dki_info->dki_partition);
318 strcpy(dki_info->dki_dname, "unknown");
319 strcpy(dki_info->dki_cname, "unknown");
320 dki_info->dki_ctype = DKC_UNKNOWN;
328 dki_info->dki_partition = 0;
336 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
349 * the number of blocks the EFI label takes up (round up to nearest
352 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
354 /* number of partitions -- limited by what we can malloc */
355 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
356 sizeof (struct dk_part))
359 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
361 diskaddr_t capacity = 0;
367 struct dk_cinfo dki_info;
369 if (read_disk_info(fd, &capacity, &lbsize) != 0)
372 if (efi_get_info(fd, &dki_info) != 0)
375 if (dki_info.dki_partition != 0)
378 if ((dki_info.dki_ctype == DKC_PCMCIA_MEM) ||
379 (dki_info.dki_ctype == DKC_VBD) ||
380 (dki_info.dki_ctype == DKC_UNKNOWN))
383 nblocks = NBLOCKS(nparts, lbsize);
384 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
385 /* 16K plus one block for the GPT */
386 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
389 if (nparts > MAX_PARTS) {
391 (void) fprintf(stderr,
392 "the maximum number of partitions supported is %lu\n",
398 length = sizeof (struct dk_gpt) +
399 sizeof (struct dk_part) * (nparts - 1);
401 if ((*vtoc = calloc(1, length)) == NULL)
406 vptr->efi_version = EFI_VERSION_CURRENT;
407 vptr->efi_lbasize = lbsize;
408 vptr->efi_nparts = nparts;
410 * add one block here for the PMBR; on disks with a 512 byte
411 * block size and 128 or fewer partitions, efi_first_u_lba
412 * should work out to "34"
414 vptr->efi_first_u_lba = nblocks + 1;
415 vptr->efi_last_lba = capacity - 1;
416 vptr->efi_altern_lba = capacity -1;
417 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
419 (void) uuid_generate((uchar_t *)&uuid);
420 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
425 * Read EFI - return partition number upon success.
428 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
434 /* figure out the number of entries that would fit into 16K */
435 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
436 length = (int) sizeof (struct dk_gpt) +
437 (int) sizeof (struct dk_part) * (nparts - 1);
438 if ((*vtoc = calloc(1, length)) == NULL)
441 (*vtoc)->efi_nparts = nparts;
442 rval = efi_read(fd, *vtoc);
444 if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
446 length = (int) sizeof (struct dk_gpt) +
447 (int) sizeof (struct dk_part) *
448 ((*vtoc)->efi_nparts - 1);
449 nparts = (*vtoc)->efi_nparts;
450 if ((tmp = realloc(*vtoc, length)) == NULL) {
456 rval = efi_read(fd, *vtoc);
462 (void) fprintf(stderr,
463 "read of EFI table failed, rval=%d\n", rval);
473 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
475 void *data = dk_ioc->dki_data;
481 * When the IO is not being performed in kernel as an ioctl we need
482 * to know the sector size so we can seek to the proper byte offset.
484 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
486 fprintf(stderr, "unable to read disk info: %d", errno);
496 (void) fprintf(stderr, "DKIOCGETEFI assuming "
497 "LBA %d bytes\n", DEV_BSIZE);
502 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
505 (void) fprintf(stderr, "DKIOCGETEFI lseek "
506 "error: %d\n", errno);
510 error = read(fd, data, dk_ioc->dki_length);
513 (void) fprintf(stderr, "DKIOCGETEFI read "
514 "error: %d\n", errno);
518 if (error != dk_ioc->dki_length) {
520 (void) fprintf(stderr, "DKIOCGETEFI short "
521 "read of %d bytes\n", error);
531 (void) fprintf(stderr, "DKIOCSETEFI unknown "
537 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
540 (void) fprintf(stderr, "DKIOCSETEFI lseek "
541 "error: %d\n", errno);
545 error = write(fd, data, dk_ioc->dki_length);
548 (void) fprintf(stderr, "DKIOCSETEFI write "
549 "error: %d\n", errno);
553 if (error != dk_ioc->dki_length) {
555 (void) fprintf(stderr, "DKIOCSETEFI short "
556 "write of %d bytes\n", error);
561 /* Sync the new EFI table to disk */
566 /* Ensure any local disk cache is also flushed */
567 if (ioctl(fd, BLKFLSBUF, 0) == -1)
575 (void) fprintf(stderr, "unsupported ioctl()\n");
590 /* Notify the kernel a devices partition table has been updated */
591 while ((error = ioctl(fd, BLKRRPART)) != 0) {
592 if ((--retry == 0) || (errno != EBUSY)) {
593 (void) fprintf(stderr, "the kernel failed to rescan "
594 "the partition table: %d\n", errno);
604 check_label(int fd, dk_efi_t *dk_ioc)
609 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
617 efi = dk_ioc->dki_data;
618 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
620 (void) fprintf(stderr,
621 "Bad EFI signature: 0x%llx != 0x%llx\n",
622 (long long)efi->efi_gpt_Signature,
623 (long long)LE_64(EFI_SIGNATURE));
628 * check CRC of the header; the size of the header should
629 * never be larger than one block
631 crc = efi->efi_gpt_HeaderCRC32;
632 efi->efi_gpt_HeaderCRC32 = 0;
633 len_t headerSize = (len_t)LE_32(efi->efi_gpt_HeaderSize);
635 if (headerSize < EFI_MIN_LABEL_SIZE || headerSize > EFI_LABEL_SIZE) {
637 (void) fprintf(stderr,
638 "Invalid EFI HeaderSize %llu. Assuming %d.\n",
639 headerSize, EFI_MIN_LABEL_SIZE);
642 if ((headerSize > dk_ioc->dki_length) ||
643 crc != LE_32(efi_crc32((unsigned char *)efi, headerSize))) {
645 (void) fprintf(stderr,
646 "Bad EFI CRC: 0x%x != 0x%x\n",
647 crc, LE_32(efi_crc32((unsigned char *)efi,
656 efi_read(int fd, struct dk_gpt *vtoc)
663 diskaddr_t capacity = 0;
665 struct dk_minfo disk_info;
668 efi_gpe_t *efi_parts;
669 struct dk_cinfo dki_info;
670 uint32_t user_length;
671 boolean_t legacy_label = B_FALSE;
674 * get the partition number for this file descriptor.
676 if ((rval = efi_get_info(fd, &dki_info)) != 0)
679 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
680 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
682 } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
683 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
685 * The controller and drive name "vdc" (virtual disk client)
686 * indicates a LDoms virtual disk.
691 /* get the LBA size */
692 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
694 (void) fprintf(stderr,
695 "unable to read disk info: %d",
701 disk_info.dki_lbsize = lbsize;
702 disk_info.dki_capacity = capacity;
704 if (disk_info.dki_lbsize == 0) {
706 (void) fprintf(stderr,
707 "efi_read: assuming LBA 512 bytes\n");
709 disk_info.dki_lbsize = DEV_BSIZE;
712 * Read the EFI GPT to figure out how many partitions we need
716 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
717 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
719 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
720 disk_info.dki_lbsize;
721 if (label_len % disk_info.dki_lbsize) {
722 /* pad to physical sector size */
723 label_len += disk_info.dki_lbsize;
724 label_len &= ~(disk_info.dki_lbsize - 1);
728 if (posix_memalign((void **)&dk_ioc.dki_data,
729 disk_info.dki_lbsize, label_len))
732 memset(dk_ioc.dki_data, 0, label_len);
733 dk_ioc.dki_length = disk_info.dki_lbsize;
734 user_length = vtoc->efi_nparts;
735 efi = dk_ioc.dki_data;
737 dk_ioc.dki_length = label_len;
738 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
746 } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
748 * No valid label here; try the alternate. Note that here
749 * we just read GPT header and save it into dk_ioc.data,
750 * Later, we will read GUID partition entry array if we
751 * can get valid GPT header.
755 * This is a workaround for legacy systems. In the past, the
756 * last sector of SCSI disk was invisible on x86 platform. At
757 * that time, backup label was saved on the next to the last
758 * sector. It is possible for users to move a disk from previous
759 * solaris system to present system. Here, we attempt to search
760 * legacy backup EFI label first.
762 dk_ioc.dki_lba = disk_info.dki_capacity - 2;
763 dk_ioc.dki_length = disk_info.dki_lbsize;
764 rval = check_label(fd, &dk_ioc);
765 if (rval == VT_EINVAL) {
767 * we didn't find legacy backup EFI label, try to
768 * search backup EFI label in the last block.
770 dk_ioc.dki_lba = disk_info.dki_capacity - 1;
771 dk_ioc.dki_length = disk_info.dki_lbsize;
772 rval = check_label(fd, &dk_ioc);
774 legacy_label = B_TRUE;
776 (void) fprintf(stderr,
777 "efi_read: primary label corrupt; "
778 "using EFI backup label located on"
779 " the last block\n");
782 if ((efi_debug) && (rval == 0))
783 (void) fprintf(stderr, "efi_read: primary label"
784 " corrupt; using legacy EFI backup label "
785 " located on the next to last block\n");
789 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
790 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
792 LE_32(efi->efi_gpt_NumberOfPartitionEntries);
794 * Partition tables are between backup GPT header
795 * table and ParitionEntryLBA (the starting LBA of
796 * the GUID partition entries array). Now that we
797 * already got valid GPT header and saved it in
798 * dk_ioc.dki_data, we try to get GUID partition
802 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
803 + disk_info.dki_lbsize);
805 dk_ioc.dki_length = disk_info.dki_capacity - 1 -
808 dk_ioc.dki_length = disk_info.dki_capacity - 2 -
810 dk_ioc.dki_length *= disk_info.dki_lbsize;
811 if (dk_ioc.dki_length >
812 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
816 * read GUID partition entry array
818 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
822 } else if (rval == 0) {
824 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
826 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
827 + disk_info.dki_lbsize);
828 dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
829 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
831 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
833 * When the device is a LDoms virtual disk, the DKIOCGETEFI
834 * ioctl can fail with EINVAL if the virtual disk backend
835 * is a ZFS volume serviced by a domain running an old version
836 * of Solaris. This is because the DKIOCGETEFI ioctl was
837 * initially incorrectly implemented for a ZFS volume and it
838 * expected the GPT and GPE to be retrieved with a single ioctl.
839 * So we try to read the GPT and the GPE using that old style
843 dk_ioc.dki_length = label_len;
844 rval = check_label(fd, &dk_ioc);
852 /* LINTED -- always longlong aligned */
853 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
856 * Assemble this into a "dk_gpt" struct for easier
857 * digestibility by applications.
859 vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
860 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
861 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
862 vtoc->efi_lbasize = disk_info.dki_lbsize;
863 vtoc->efi_last_lba = disk_info.dki_capacity - 1;
864 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
865 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
866 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
867 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
870 * If the array the user passed in is too small, set the length
871 * to what it needs to be and return
873 if (user_length < vtoc->efi_nparts) {
877 for (i = 0; i < vtoc->efi_nparts; i++) {
879 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
880 efi_parts[i].efi_gpe_PartitionTypeGUID);
883 j < sizeof (conversion_array)
884 / sizeof (struct uuid_to_ptag); j++) {
886 if (bcmp(&vtoc->efi_parts[i].p_guid,
887 &conversion_array[j].uuid,
888 sizeof (struct uuid)) == 0) {
889 vtoc->efi_parts[i].p_tag = j;
893 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
895 vtoc->efi_parts[i].p_flag =
896 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
897 vtoc->efi_parts[i].p_start =
898 LE_64(efi_parts[i].efi_gpe_StartingLBA);
899 vtoc->efi_parts[i].p_size =
900 LE_64(efi_parts[i].efi_gpe_EndingLBA) -
901 vtoc->efi_parts[i].p_start + 1;
902 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
903 vtoc->efi_parts[i].p_name[j] =
905 efi_parts[i].efi_gpe_PartitionName[j]);
908 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
909 efi_parts[i].efi_gpe_UniquePartitionGUID);
913 return (dki_info.dki_partition);
916 /* writes a "protective" MBR */
918 write_pmbr(int fd, struct dk_gpt *vtoc)
923 diskaddr_t size_in_lba;
927 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
928 if (posix_memalign((void **)&buf, len, len))
932 * Preserve any boot code and disk signature if the first block is
937 dk_ioc.dki_length = len;
938 /* LINTED -- always longlong aligned */
939 dk_ioc.dki_data = (efi_gpt_t *)buf;
940 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
941 (void) memcpy(&mb, buf, sizeof (mb));
942 bzero(&mb, sizeof (mb));
943 mb.signature = LE_16(MBB_MAGIC);
945 (void) memcpy(&mb, buf, sizeof (mb));
946 if (mb.signature != LE_16(MBB_MAGIC)) {
947 bzero(&mb, sizeof (mb));
948 mb.signature = LE_16(MBB_MAGIC);
952 bzero(&mb.parts, sizeof (mb.parts));
953 cp = (uchar_t *)&mb.parts[0];
954 /* bootable or not */
956 /* beginning CHS; 0xffffff if not representable */
962 /* ending CHS; 0xffffff if not representable */
966 /* starting LBA: 1 (little endian format) by EFI definition */
971 /* ending LBA: last block on the disk (little endian format) */
972 size_in_lba = vtoc->efi_last_lba;
973 if (size_in_lba < 0xffffffff) {
974 *cp++ = (size_in_lba & 0x000000ff);
975 *cp++ = (size_in_lba & 0x0000ff00) >> 8;
976 *cp++ = (size_in_lba & 0x00ff0000) >> 16;
977 *cp++ = (size_in_lba & 0xff000000) >> 24;
985 (void) memcpy(buf, &mb, sizeof (mb));
986 /* LINTED -- always longlong aligned */
987 dk_ioc.dki_data = (efi_gpt_t *)buf;
989 dk_ioc.dki_length = len;
990 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1005 /* make sure the user specified something reasonable */
1007 check_input(struct dk_gpt *vtoc)
1011 diskaddr_t istart, jstart, isize, jsize, endsect;
1014 * Sanity-check the input (make sure no partitions overlap)
1016 for (i = 0; i < vtoc->efi_nparts; i++) {
1017 /* It can't be unassigned and have an actual size */
1018 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1019 (vtoc->efi_parts[i].p_size != 0)) {
1021 (void) fprintf(stderr, "partition %d is "
1022 "\"unassigned\" but has a size of %llu",
1023 i, vtoc->efi_parts[i].p_size);
1027 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1028 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
1030 /* we have encountered an unknown uuid */
1031 vtoc->efi_parts[i].p_tag = 0xff;
1033 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1034 if (resv_part != -1) {
1036 (void) fprintf(stderr, "found "
1037 "duplicate reserved partition "
1044 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1045 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1047 (void) fprintf(stderr,
1048 "Partition %d starts at %llu. ",
1050 vtoc->efi_parts[i].p_start);
1051 (void) fprintf(stderr,
1052 "It must be between %llu and %llu.\n",
1053 vtoc->efi_first_u_lba,
1054 vtoc->efi_last_u_lba);
1058 if ((vtoc->efi_parts[i].p_start +
1059 vtoc->efi_parts[i].p_size <
1060 vtoc->efi_first_u_lba) ||
1061 (vtoc->efi_parts[i].p_start +
1062 vtoc->efi_parts[i].p_size >
1063 vtoc->efi_last_u_lba + 1)) {
1065 (void) fprintf(stderr,
1066 "Partition %d ends at %llu. ",
1068 vtoc->efi_parts[i].p_start +
1069 vtoc->efi_parts[i].p_size);
1070 (void) fprintf(stderr,
1071 "It must be between %llu and %llu.\n",
1072 vtoc->efi_first_u_lba,
1073 vtoc->efi_last_u_lba);
1078 for (j = 0; j < vtoc->efi_nparts; j++) {
1079 isize = vtoc->efi_parts[i].p_size;
1080 jsize = vtoc->efi_parts[j].p_size;
1081 istart = vtoc->efi_parts[i].p_start;
1082 jstart = vtoc->efi_parts[j].p_start;
1083 if ((i != j) && (isize != 0) && (jsize != 0)) {
1084 endsect = jstart + jsize -1;
1085 if ((jstart <= istart) &&
1086 (istart <= endsect)) {
1088 (void) fprintf(stderr,
1089 "Partition %d overlaps "
1090 "partition %d.", i, j);
1097 /* just a warning for now */
1098 if ((resv_part == -1) && efi_debug) {
1099 (void) fprintf(stderr,
1100 "no reserved partition found\n");
1106 * add all the unallocated space to the current label
1109 efi_use_whole_disk(int fd)
1111 struct dk_gpt *efi_label = NULL;
1114 uint_t resv_index = 0, data_index = 0;
1115 diskaddr_t resv_start = 0, data_start = 0;
1116 diskaddr_t difference;
1118 rval = efi_alloc_and_read(fd, &efi_label);
1120 if (efi_label != NULL)
1121 efi_free(efi_label);
1126 * If alter_lba is 1, we are using the backup label.
1127 * Since we can locate the backup label by disk capacity,
1128 * there must be no unallocated space.
1130 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
1131 >= efi_label->efi_last_lba)) {
1133 (void) fprintf(stderr,
1134 "efi_use_whole_disk: requested space not found\n");
1136 efi_free(efi_label);
1140 difference = efi_label->efi_last_lba - efi_label->efi_altern_lba;
1143 * Find the last physically non-zero partition.
1144 * This should be the reserved partition.
1146 for (i = 0; i < efi_label->efi_nparts; i ++) {
1147 if (resv_start < efi_label->efi_parts[i].p_start) {
1148 resv_start = efi_label->efi_parts[i].p_start;
1154 * Verify that we've found the reserved partition by checking
1155 * that it looks the way it did when we created it in zpool_label_disk.
1156 * If we've found the incorrect partition, then we know that this
1157 * device was reformatted and no longer is soley used by ZFS.
1159 if ((efi_label->efi_parts[resv_index].p_size != EFI_MIN_RESV_SIZE) ||
1160 (efi_label->efi_parts[resv_index].p_tag != V_RESERVED) ||
1161 (resv_index != 8)) {
1163 (void) fprintf(stderr,
1164 "efi_use_whole_disk: wholedisk not available\n");
1166 efi_free(efi_label);
1171 * Find the last physically non-zero partition before that.
1172 * This is the data partition.
1174 for (i = 0; i < resv_index; i ++) {
1175 if (data_start < efi_label->efi_parts[i].p_start) {
1176 data_start = efi_label->efi_parts[i].p_start;
1182 * Move the reserved partition. There is currently no data in
1183 * here except fabricated devids (which get generated via
1184 * efi_write()). So there is no need to copy data.
1186 efi_label->efi_parts[data_index].p_size += difference;
1187 efi_label->efi_parts[resv_index].p_start += difference;
1188 efi_label->efi_last_u_lba += difference;
1190 rval = efi_write(fd, efi_label);
1193 (void) fprintf(stderr,
1194 "efi_use_whole_disk:fail to write label, rval=%d\n",
1197 efi_free(efi_label);
1201 efi_free(efi_label);
1207 * write EFI label and backup label
1210 efi_write(int fd, struct dk_gpt *vtoc)
1214 efi_gpe_t *efi_parts;
1216 struct dk_cinfo dki_info;
1220 diskaddr_t lba_backup_gpt_hdr;
1222 if ((rval = efi_get_info(fd, &dki_info)) != 0)
1225 /* check if we are dealing wih a metadevice */
1226 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
1227 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
1231 if (check_input(vtoc)) {
1233 * not valid; if it's a metadevice just pass it down
1234 * because SVM will do its own checking
1242 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
1243 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
1245 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
1246 vtoc->efi_lbasize) *
1251 * the number of blocks occupied by GUID partition entry array
1253 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
1256 * Backup GPT header is located on the block after GUID
1257 * partition entry array. Here, we calculate the address
1258 * for backup GPT header.
1260 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
1261 if (posix_memalign((void **)&dk_ioc.dki_data,
1262 vtoc->efi_lbasize, dk_ioc.dki_length))
1265 memset(dk_ioc.dki_data, 0, dk_ioc.dki_length);
1266 efi = dk_ioc.dki_data;
1268 /* stuff user's input into EFI struct */
1269 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1270 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
1271 efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt) - LEN_EFI_PAD);
1272 efi->efi_gpt_Reserved1 = 0;
1273 efi->efi_gpt_MyLBA = LE_64(1ULL);
1274 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
1275 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
1276 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
1277 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1278 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
1279 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
1280 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
1282 /* LINTED -- always longlong aligned */
1283 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
1285 for (i = 0; i < vtoc->efi_nparts; i++) {
1287 j < sizeof (conversion_array) /
1288 sizeof (struct uuid_to_ptag); j++) {
1290 if (vtoc->efi_parts[i].p_tag == j) {
1292 efi_parts[i].efi_gpe_PartitionTypeGUID,
1293 conversion_array[j].uuid);
1298 if (j == sizeof (conversion_array) /
1299 sizeof (struct uuid_to_ptag)) {
1301 * If we didn't have a matching uuid match, bail here.
1302 * Don't write a label with unknown uuid.
1305 (void) fprintf(stderr,
1306 "Unknown uuid for p_tag %d\n",
1307 vtoc->efi_parts[i].p_tag);
1312 /* Zero's should be written for empty partitions */
1313 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
1316 efi_parts[i].efi_gpe_StartingLBA =
1317 LE_64(vtoc->efi_parts[i].p_start);
1318 efi_parts[i].efi_gpe_EndingLBA =
1319 LE_64(vtoc->efi_parts[i].p_start +
1320 vtoc->efi_parts[i].p_size - 1);
1321 efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
1322 LE_16(vtoc->efi_parts[i].p_flag);
1323 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1324 efi_parts[i].efi_gpe_PartitionName[j] =
1325 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1327 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1328 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1329 (void) uuid_generate((uchar_t *)
1330 &vtoc->efi_parts[i].p_uguid);
1332 bcopy(&vtoc->efi_parts[i].p_uguid,
1333 &efi_parts[i].efi_gpe_UniquePartitionGUID,
1336 efi->efi_gpt_PartitionEntryArrayCRC32 =
1337 LE_32(efi_crc32((unsigned char *)efi_parts,
1338 vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1339 efi->efi_gpt_HeaderCRC32 =
1340 LE_32(efi_crc32((unsigned char *)efi,
1341 LE_32(efi->efi_gpt_HeaderSize)));
1343 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1344 free(dk_ioc.dki_data);
1354 /* if it's a metadevice we're done */
1356 free(dk_ioc.dki_data);
1360 /* write backup partition array */
1361 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
1362 dk_ioc.dki_length -= vtoc->efi_lbasize;
1364 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
1367 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1369 * we wrote the primary label okay, so don't fail
1372 (void) fprintf(stderr,
1373 "write of backup partitions to block %llu "
1374 "failed, errno %d\n",
1375 vtoc->efi_last_u_lba + 1,
1380 * now swap MyLBA and AlternateLBA fields and write backup
1381 * partition table header
1383 dk_ioc.dki_lba = lba_backup_gpt_hdr;
1384 dk_ioc.dki_length = vtoc->efi_lbasize;
1386 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1388 efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1389 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1390 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1391 efi->efi_gpt_HeaderCRC32 = 0;
1392 efi->efi_gpt_HeaderCRC32 =
1393 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1394 LE_32(efi->efi_gpt_HeaderSize)));
1396 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1398 (void) fprintf(stderr,
1399 "write of backup header to block %llu failed, "
1405 /* write the PMBR */
1406 (void) write_pmbr(fd, vtoc);
1407 free(dk_ioc.dki_data);
1413 efi_free(struct dk_gpt *ptr)
1419 * Input: File descriptor
1420 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1428 struct extvtoc extvtoc;
1430 if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1431 if (errno == ENOTSUP)
1433 else if (errno == ENOTTY) {
1434 if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1435 if (errno == ENOTSUP)
1446 efi_err_check(struct dk_gpt *vtoc)
1450 diskaddr_t istart, jstart, isize, jsize, endsect;
1454 * make sure no partitions overlap
1456 for (i = 0; i < vtoc->efi_nparts; i++) {
1457 /* It can't be unassigned and have an actual size */
1458 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1459 (vtoc->efi_parts[i].p_size != 0)) {
1460 (void) fprintf(stderr,
1461 "partition %d is \"unassigned\" but has a size "
1462 "of %llu\n", i, vtoc->efi_parts[i].p_size);
1464 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1467 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1468 if (resv_part != -1) {
1469 (void) fprintf(stderr,
1470 "found duplicate reserved partition at "
1474 if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1475 (void) fprintf(stderr,
1476 "Warning: reserved partition size must "
1477 "be %d sectors\n", EFI_MIN_RESV_SIZE);
1479 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1480 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1481 (void) fprintf(stderr,
1482 "Partition %d starts at %llu\n",
1484 vtoc->efi_parts[i].p_start);
1485 (void) fprintf(stderr,
1486 "It must be between %llu and %llu.\n",
1487 vtoc->efi_first_u_lba,
1488 vtoc->efi_last_u_lba);
1490 if ((vtoc->efi_parts[i].p_start +
1491 vtoc->efi_parts[i].p_size <
1492 vtoc->efi_first_u_lba) ||
1493 (vtoc->efi_parts[i].p_start +
1494 vtoc->efi_parts[i].p_size >
1495 vtoc->efi_last_u_lba + 1)) {
1496 (void) fprintf(stderr,
1497 "Partition %d ends at %llu\n",
1499 vtoc->efi_parts[i].p_start +
1500 vtoc->efi_parts[i].p_size);
1501 (void) fprintf(stderr,
1502 "It must be between %llu and %llu.\n",
1503 vtoc->efi_first_u_lba,
1504 vtoc->efi_last_u_lba);
1507 for (j = 0; j < vtoc->efi_nparts; j++) {
1508 isize = vtoc->efi_parts[i].p_size;
1509 jsize = vtoc->efi_parts[j].p_size;
1510 istart = vtoc->efi_parts[i].p_start;
1511 jstart = vtoc->efi_parts[j].p_start;
1512 if ((i != j) && (isize != 0) && (jsize != 0)) {
1513 endsect = jstart + jsize -1;
1514 if ((jstart <= istart) &&
1515 (istart <= endsect)) {
1517 (void) fprintf(stderr,
1518 "label error: EFI Labels do not "
1519 "support overlapping partitions\n");
1521 (void) fprintf(stderr,
1522 "Partition %d overlaps partition "
1529 /* make sure there is a reserved partition */
1530 if (resv_part == -1) {
1531 (void) fprintf(stderr,
1532 "no reserved partition found\n");
1537 * We need to get information necessary to construct a *new* efi
1541 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1547 * Now build the default partition table
1549 if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1551 (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1556 for (i = 0; i < MIN((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1557 (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1558 (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1559 (*vtoc)->efi_parts[i].p_start = 0;
1560 (*vtoc)->efi_parts[i].p_size = 0;
1563 * Make constants first
1564 * and variable partitions later
1567 /* root partition - s0 128 MB */
1568 (*vtoc)->efi_parts[0].p_start = 34;
1569 (*vtoc)->efi_parts[0].p_size = 262144;
1571 /* partition - s1 128 MB */
1572 (*vtoc)->efi_parts[1].p_start = 262178;
1573 (*vtoc)->efi_parts[1].p_size = 262144;
1575 /* partition -s2 is NOT the Backup disk */
1576 (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1578 /* partition -s6 /usr partition - HOG */
1579 (*vtoc)->efi_parts[6].p_start = 524322;
1580 (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1583 /* efi reserved partition - s9 16K */
1584 (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1585 (*vtoc)->efi_parts[8].p_size = (1024 * 16);
1586 (*vtoc)->efi_parts[8].p_tag = V_RESERVED;