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>
45 #include <sys/vdev_disk.h>
48 static struct uuid_to_ptag {
50 } conversion_array[] = {
57 { EFI_UNUSED }, /* STAND is never used */
61 { EFI_UNUSED }, /* CACHE (cachefs) is never used */
78 { EFI_FREEBSD_VINUM },
88 { EFI_MSFT_STORAGESPACES },
96 { EFI_FREEBSD_DISKLABEL },
98 { EFI_AAPL_RAIDOFFLINE },
101 { EFI_AAPL_TVRECOVERY },
102 { EFI_AAPL_CORESTORAGE },
108 { EFI_NETBSD_CRYPT },
113 { EFI_MIDNIGHTBSD_BOOT },
114 { EFI_MIDNIGHTBSD_DATA },
115 { EFI_MIDNIGHTBSD_SWAP },
116 { EFI_MIDNIGHTBSD_UFS },
117 { EFI_MIDNIGHTBSD_VINUM },
118 { EFI_MIDNIGHTBSD_ZFS },
119 { EFI_CEPH_JOURNAL },
120 { EFI_CEPH_DMCRYPTJOURNAL },
122 { EFI_CEPH_DMCRYPTOSD },
124 { EFI_CEPH_DMCRYPTCREATE },
125 { EFI_OPENBSD_DISKLABEL },
132 { EFI_RHT_ROOTAMD64 },
134 { EFI_RHT_ROOTARM64 },
135 { EFI_ACRONIS_SECUREZONE },
138 { EFI_IBM_PPRPBOOT },
139 { EFI_FREEDESKTOP_BOOT }
143 * Default vtoc information for non-SVr4 partitions
145 struct dk_map2 default_vtoc_map[NDKMAP] = {
146 { V_ROOT, 0 }, /* a - 0 */
147 { V_SWAP, V_UNMNT }, /* b - 1 */
148 { V_BACKUP, V_UNMNT }, /* c - 2 */
149 { V_UNASSIGNED, 0 }, /* d - 3 */
150 { V_UNASSIGNED, 0 }, /* e - 4 */
151 { V_UNASSIGNED, 0 }, /* f - 5 */
152 { V_USR, 0 }, /* g - 6 */
153 { V_UNASSIGNED, 0 }, /* h - 7 */
155 #if defined(_SUNOS_VTOC_16)
157 #if defined(i386) || defined(__amd64) || defined(__arm) || \
158 defined(__powerpc) || defined(__sparc) || defined(__s390__) || \
159 defined(__mips__) || defined(__rv64g__)
160 { V_BOOT, V_UNMNT }, /* i - 8 */
161 { V_ALTSCTR, 0 }, /* j - 9 */
164 #error No VTOC format defined.
165 #endif /* defined(i386) */
167 { V_UNASSIGNED, 0 }, /* k - 10 */
168 { V_UNASSIGNED, 0 }, /* l - 11 */
169 { V_UNASSIGNED, 0 }, /* m - 12 */
170 { V_UNASSIGNED, 0 }, /* n - 13 */
171 { V_UNASSIGNED, 0 }, /* o - 14 */
172 { V_UNASSIGNED, 0 }, /* p - 15 */
173 #endif /* defined(_SUNOS_VTOC_16) */
178 static int efi_read(int, struct dk_gpt *);
181 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
182 * one's conditioning will be handled by crc32() internally.
185 efi_crc32(const unsigned char *buf, unsigned int size)
187 uint32_t crc = crc32(0, Z_NULL, 0);
189 crc = crc32(crc, buf, size);
195 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
198 unsigned long long capacity_size;
200 if (ioctl(fd, BLKSSZGET, §or_size) < 0)
203 if (ioctl(fd, BLKGETSIZE64, &capacity_size) < 0)
206 *lbsize = (uint_t)sector_size;
207 *capacity = (diskaddr_t)(capacity_size / sector_size);
213 efi_get_info(int fd, struct dk_cinfo *dki_info)
219 memset(dki_info, 0, sizeof (*dki_info));
221 path = calloc(1, PATH_MAX);
226 * The simplest way to get the partition number under linux is
227 * to parse it out of the /dev/<disk><partition> block device name.
228 * The kernel creates this using the partition number when it
229 * populates /dev/ so it may be trusted. The tricky bit here is
230 * that the naming convention is based on the block device type.
231 * So we need to take this in to account when parsing out the
232 * partition information. Another issue is that the libefi API
233 * API only provides the open fd and not the file path. To handle
234 * this realpath(3) is used to resolve the block device name from
235 * /proc/self/fd/<fd>. Aside from the partition number we collect
236 * some additional device info.
238 (void) sprintf(path, "/proc/self/fd/%d", fd);
239 dev_path = realpath(path, NULL);
242 if (dev_path == NULL)
245 if ((strncmp(dev_path, "/dev/sd", 7) == 0)) {
246 strcpy(dki_info->dki_cname, "sd");
247 dki_info->dki_ctype = DKC_SCSI_CCS;
248 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
250 &dki_info->dki_partition);
251 } else if ((strncmp(dev_path, "/dev/hd", 7) == 0)) {
252 strcpy(dki_info->dki_cname, "hd");
253 dki_info->dki_ctype = DKC_DIRECT;
254 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
256 &dki_info->dki_partition);
257 } else if ((strncmp(dev_path, "/dev/md", 7) == 0)) {
258 strcpy(dki_info->dki_cname, "pseudo");
259 dki_info->dki_ctype = DKC_MD;
260 strcpy(dki_info->dki_dname, "md");
261 rval = sscanf(dev_path, "/dev/md%[0-9]p%hu",
262 dki_info->dki_dname + 2,
263 &dki_info->dki_partition);
264 } else if ((strncmp(dev_path, "/dev/vd", 7) == 0)) {
265 strcpy(dki_info->dki_cname, "vd");
266 dki_info->dki_ctype = DKC_MD;
267 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
269 &dki_info->dki_partition);
270 } else if ((strncmp(dev_path, "/dev/xvd", 8) == 0)) {
271 strcpy(dki_info->dki_cname, "xvd");
272 dki_info->dki_ctype = DKC_MD;
273 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
275 &dki_info->dki_partition);
276 } else if ((strncmp(dev_path, "/dev/zd", 7) == 0)) {
277 strcpy(dki_info->dki_cname, "zd");
278 dki_info->dki_ctype = DKC_MD;
279 strcpy(dki_info->dki_dname, "zd");
280 rval = sscanf(dev_path, "/dev/zd%[0-9]p%hu",
281 dki_info->dki_dname + 2,
282 &dki_info->dki_partition);
283 } else if ((strncmp(dev_path, "/dev/dm-", 8) == 0)) {
284 strcpy(dki_info->dki_cname, "pseudo");
285 dki_info->dki_ctype = DKC_VBD;
286 strcpy(dki_info->dki_dname, "dm-");
287 rval = sscanf(dev_path, "/dev/dm-%[0-9]p%hu",
288 dki_info->dki_dname + 3,
289 &dki_info->dki_partition);
290 } else if ((strncmp(dev_path, "/dev/ram", 8) == 0)) {
291 strcpy(dki_info->dki_cname, "pseudo");
292 dki_info->dki_ctype = DKC_PCMCIA_MEM;
293 strcpy(dki_info->dki_dname, "ram");
294 rval = sscanf(dev_path, "/dev/ram%[0-9]p%hu",
295 dki_info->dki_dname + 3,
296 &dki_info->dki_partition);
297 } else if ((strncmp(dev_path, "/dev/loop", 9) == 0)) {
298 strcpy(dki_info->dki_cname, "pseudo");
299 dki_info->dki_ctype = DKC_VBD;
300 strcpy(dki_info->dki_dname, "loop");
301 rval = sscanf(dev_path, "/dev/loop%[0-9]p%hu",
302 dki_info->dki_dname + 4,
303 &dki_info->dki_partition);
304 } else if ((strncmp(dev_path, "/dev/nvme", 9) == 0)) {
305 strcpy(dki_info->dki_cname, "nvme");
306 dki_info->dki_ctype = DKC_SCSI_CCS;
307 strcpy(dki_info->dki_dname, "nvme");
308 (void) sscanf(dev_path, "/dev/nvme%[0-9]",
309 dki_info->dki_dname + 4);
310 size_t controller_length = strlen(
311 dki_info->dki_dname);
312 strcpy(dki_info->dki_dname + controller_length,
314 rval = sscanf(dev_path,
315 "/dev/nvme%*[0-9]n%[0-9]p%hu",
316 dki_info->dki_dname + controller_length + 1,
317 &dki_info->dki_partition);
319 strcpy(dki_info->dki_dname, "unknown");
320 strcpy(dki_info->dki_cname, "unknown");
321 dki_info->dki_ctype = DKC_UNKNOWN;
329 dki_info->dki_partition = 0;
337 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
350 * the number of blocks the EFI label takes up (round up to nearest
353 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
355 /* number of partitions -- limited by what we can malloc */
356 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
357 sizeof (struct dk_part))
360 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
362 diskaddr_t capacity = 0;
368 struct dk_cinfo dki_info;
370 if (read_disk_info(fd, &capacity, &lbsize) != 0)
373 if (efi_get_info(fd, &dki_info) != 0)
376 if (dki_info.dki_partition != 0)
379 if ((dki_info.dki_ctype == DKC_PCMCIA_MEM) ||
380 (dki_info.dki_ctype == DKC_VBD) ||
381 (dki_info.dki_ctype == DKC_UNKNOWN))
384 nblocks = NBLOCKS(nparts, lbsize);
385 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
386 /* 16K plus one block for the GPT */
387 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
390 if (nparts > MAX_PARTS) {
392 (void) fprintf(stderr,
393 "the maximum number of partitions supported is %lu\n",
399 length = sizeof (struct dk_gpt) +
400 sizeof (struct dk_part) * (nparts - 1);
402 vptr = calloc(1, length);
408 vptr->efi_version = EFI_VERSION_CURRENT;
409 vptr->efi_lbasize = lbsize;
410 vptr->efi_nparts = nparts;
412 * add one block here for the PMBR; on disks with a 512 byte
413 * block size and 128 or fewer partitions, efi_first_u_lba
414 * should work out to "34"
416 vptr->efi_first_u_lba = nblocks + 1;
417 vptr->efi_last_lba = capacity - 1;
418 vptr->efi_altern_lba = capacity -1;
419 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
421 (void) uuid_generate((uchar_t *)&uuid);
422 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
427 * Read EFI - return partition number upon success.
430 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
437 /* figure out the number of entries that would fit into 16K */
438 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
439 length = (int) sizeof (struct dk_gpt) +
440 (int) sizeof (struct dk_part) * (nparts - 1);
441 vptr = calloc(1, length);
446 vptr->efi_nparts = nparts;
447 rval = efi_read(fd, vptr);
449 if ((rval == VT_EINVAL) && vptr->efi_nparts > nparts) {
451 length = (int) sizeof (struct dk_gpt) +
452 (int) sizeof (struct dk_part) * (vptr->efi_nparts - 1);
453 nparts = vptr->efi_nparts;
454 if ((tmp = realloc(vptr, length)) == NULL) {
460 rval = efi_read(fd, vptr);
466 (void) fprintf(stderr,
467 "read of EFI table failed, rval=%d\n", rval);
479 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
481 void *data = dk_ioc->dki_data;
487 * When the IO is not being performed in kernel as an ioctl we need
488 * to know the sector size so we can seek to the proper byte offset.
490 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
492 fprintf(stderr, "unable to read disk info: %d", errno);
502 (void) fprintf(stderr, "DKIOCGETEFI assuming "
503 "LBA %d bytes\n", DEV_BSIZE);
508 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
511 (void) fprintf(stderr, "DKIOCGETEFI lseek "
512 "error: %d\n", errno);
516 error = read(fd, data, dk_ioc->dki_length);
519 (void) fprintf(stderr, "DKIOCGETEFI read "
520 "error: %d\n", errno);
524 if (error != dk_ioc->dki_length) {
526 (void) fprintf(stderr, "DKIOCGETEFI short "
527 "read of %d bytes\n", error);
537 (void) fprintf(stderr, "DKIOCSETEFI unknown "
543 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
546 (void) fprintf(stderr, "DKIOCSETEFI lseek "
547 "error: %d\n", errno);
551 error = write(fd, data, dk_ioc->dki_length);
554 (void) fprintf(stderr, "DKIOCSETEFI write "
555 "error: %d\n", errno);
559 if (error != dk_ioc->dki_length) {
561 (void) fprintf(stderr, "DKIOCSETEFI short "
562 "write of %d bytes\n", error);
567 /* Sync the new EFI table to disk */
572 /* Ensure any local disk cache is also flushed */
573 if (ioctl(fd, BLKFLSBUF, 0) == -1)
581 (void) fprintf(stderr, "unsupported ioctl()\n");
596 /* Notify the kernel a devices partition table has been updated */
597 while ((error = ioctl(fd, BLKRRPART)) != 0) {
598 if ((--retry == 0) || (errno != EBUSY)) {
599 (void) fprintf(stderr, "the kernel failed to rescan "
600 "the partition table: %d\n", errno);
610 check_label(int fd, dk_efi_t *dk_ioc)
615 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
623 efi = dk_ioc->dki_data;
624 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
626 (void) fprintf(stderr,
627 "Bad EFI signature: 0x%llx != 0x%llx\n",
628 (long long)efi->efi_gpt_Signature,
629 (long long)LE_64(EFI_SIGNATURE));
634 * check CRC of the header; the size of the header should
635 * never be larger than one block
637 crc = efi->efi_gpt_HeaderCRC32;
638 efi->efi_gpt_HeaderCRC32 = 0;
639 len_t headerSize = (len_t)LE_32(efi->efi_gpt_HeaderSize);
641 if (headerSize < EFI_MIN_LABEL_SIZE || headerSize > EFI_LABEL_SIZE) {
643 (void) fprintf(stderr,
644 "Invalid EFI HeaderSize %llu. Assuming %d.\n",
645 headerSize, EFI_MIN_LABEL_SIZE);
648 if ((headerSize > dk_ioc->dki_length) ||
649 crc != LE_32(efi_crc32((unsigned char *)efi, headerSize))) {
651 (void) fprintf(stderr,
652 "Bad EFI CRC: 0x%x != 0x%x\n",
653 crc, LE_32(efi_crc32((unsigned char *)efi,
662 efi_read(int fd, struct dk_gpt *vtoc)
669 diskaddr_t capacity = 0;
671 struct dk_minfo disk_info;
674 efi_gpe_t *efi_parts;
675 struct dk_cinfo dki_info;
676 uint32_t user_length;
677 boolean_t legacy_label = B_FALSE;
680 * get the partition number for this file descriptor.
682 if ((rval = efi_get_info(fd, &dki_info)) != 0)
685 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
686 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
688 } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
689 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
691 * The controller and drive name "vdc" (virtual disk client)
692 * indicates a LDoms virtual disk.
697 /* get the LBA size */
698 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
700 (void) fprintf(stderr,
701 "unable to read disk info: %d",
707 disk_info.dki_lbsize = lbsize;
708 disk_info.dki_capacity = capacity;
710 if (disk_info.dki_lbsize == 0) {
712 (void) fprintf(stderr,
713 "efi_read: assuming LBA 512 bytes\n");
715 disk_info.dki_lbsize = DEV_BSIZE;
718 * Read the EFI GPT to figure out how many partitions we need
722 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
723 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
725 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
726 disk_info.dki_lbsize;
727 if (label_len % disk_info.dki_lbsize) {
728 /* pad to physical sector size */
729 label_len += disk_info.dki_lbsize;
730 label_len &= ~(disk_info.dki_lbsize - 1);
734 if (posix_memalign((void **)&dk_ioc.dki_data,
735 disk_info.dki_lbsize, label_len))
738 memset(dk_ioc.dki_data, 0, label_len);
739 dk_ioc.dki_length = disk_info.dki_lbsize;
740 user_length = vtoc->efi_nparts;
741 efi = dk_ioc.dki_data;
743 dk_ioc.dki_length = label_len;
744 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
752 } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
754 * No valid label here; try the alternate. Note that here
755 * we just read GPT header and save it into dk_ioc.data,
756 * Later, we will read GUID partition entry array if we
757 * can get valid GPT header.
761 * This is a workaround for legacy systems. In the past, the
762 * last sector of SCSI disk was invisible on x86 platform. At
763 * that time, backup label was saved on the next to the last
764 * sector. It is possible for users to move a disk from previous
765 * solaris system to present system. Here, we attempt to search
766 * legacy backup EFI label first.
768 dk_ioc.dki_lba = disk_info.dki_capacity - 2;
769 dk_ioc.dki_length = disk_info.dki_lbsize;
770 rval = check_label(fd, &dk_ioc);
771 if (rval == VT_EINVAL) {
773 * we didn't find legacy backup EFI label, try to
774 * search backup EFI label in the last block.
776 dk_ioc.dki_lba = disk_info.dki_capacity - 1;
777 dk_ioc.dki_length = disk_info.dki_lbsize;
778 rval = check_label(fd, &dk_ioc);
780 legacy_label = B_TRUE;
782 (void) fprintf(stderr,
783 "efi_read: primary label corrupt; "
784 "using EFI backup label located on"
785 " the last block\n");
788 if ((efi_debug) && (rval == 0))
789 (void) fprintf(stderr, "efi_read: primary label"
790 " corrupt; using legacy EFI backup label "
791 " located on the next to last block\n");
795 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
796 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
798 LE_32(efi->efi_gpt_NumberOfPartitionEntries);
800 * Partition tables are between backup GPT header
801 * table and ParitionEntryLBA (the starting LBA of
802 * the GUID partition entries array). Now that we
803 * already got valid GPT header and saved it in
804 * dk_ioc.dki_data, we try to get GUID partition
808 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
809 + disk_info.dki_lbsize);
811 dk_ioc.dki_length = disk_info.dki_capacity - 1 -
814 dk_ioc.dki_length = disk_info.dki_capacity - 2 -
816 dk_ioc.dki_length *= disk_info.dki_lbsize;
817 if (dk_ioc.dki_length >
818 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
822 * read GUID partition entry array
824 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
828 } else if (rval == 0) {
830 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
832 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
833 + disk_info.dki_lbsize);
834 dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
835 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
837 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
839 * When the device is a LDoms virtual disk, the DKIOCGETEFI
840 * ioctl can fail with EINVAL if the virtual disk backend
841 * is a ZFS volume serviced by a domain running an old version
842 * of Solaris. This is because the DKIOCGETEFI ioctl was
843 * initially incorrectly implemented for a ZFS volume and it
844 * expected the GPT and GPE to be retrieved with a single ioctl.
845 * So we try to read the GPT and the GPE using that old style
849 dk_ioc.dki_length = label_len;
850 rval = check_label(fd, &dk_ioc);
858 /* LINTED -- always longlong aligned */
859 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
862 * Assemble this into a "dk_gpt" struct for easier
863 * digestibility by applications.
865 vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
866 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
867 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
868 vtoc->efi_lbasize = disk_info.dki_lbsize;
869 vtoc->efi_last_lba = disk_info.dki_capacity - 1;
870 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
871 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
872 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
873 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
876 * If the array the user passed in is too small, set the length
877 * to what it needs to be and return
879 if (user_length < vtoc->efi_nparts) {
883 for (i = 0; i < vtoc->efi_nparts; i++) {
885 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
886 efi_parts[i].efi_gpe_PartitionTypeGUID);
889 j < sizeof (conversion_array)
890 / sizeof (struct uuid_to_ptag); j++) {
892 if (bcmp(&vtoc->efi_parts[i].p_guid,
893 &conversion_array[j].uuid,
894 sizeof (struct uuid)) == 0) {
895 vtoc->efi_parts[i].p_tag = j;
899 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
901 vtoc->efi_parts[i].p_flag =
902 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
903 vtoc->efi_parts[i].p_start =
904 LE_64(efi_parts[i].efi_gpe_StartingLBA);
905 vtoc->efi_parts[i].p_size =
906 LE_64(efi_parts[i].efi_gpe_EndingLBA) -
907 vtoc->efi_parts[i].p_start + 1;
908 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
909 vtoc->efi_parts[i].p_name[j] =
911 efi_parts[i].efi_gpe_PartitionName[j]);
914 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
915 efi_parts[i].efi_gpe_UniquePartitionGUID);
919 return (dki_info.dki_partition);
922 /* writes a "protective" MBR */
924 write_pmbr(int fd, struct dk_gpt *vtoc)
929 diskaddr_t size_in_lba;
933 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
934 if (posix_memalign((void **)&buf, len, len))
938 * Preserve any boot code and disk signature if the first block is
943 dk_ioc.dki_length = len;
944 /* LINTED -- always longlong aligned */
945 dk_ioc.dki_data = (efi_gpt_t *)buf;
946 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
947 (void) memcpy(&mb, buf, sizeof (mb));
948 bzero(&mb, sizeof (mb));
949 mb.signature = LE_16(MBB_MAGIC);
951 (void) memcpy(&mb, buf, sizeof (mb));
952 if (mb.signature != LE_16(MBB_MAGIC)) {
953 bzero(&mb, sizeof (mb));
954 mb.signature = LE_16(MBB_MAGIC);
958 bzero(&mb.parts, sizeof (mb.parts));
959 cp = (uchar_t *)&mb.parts[0];
960 /* bootable or not */
962 /* beginning CHS; 0xffffff if not representable */
968 /* ending CHS; 0xffffff if not representable */
972 /* starting LBA: 1 (little endian format) by EFI definition */
977 /* ending LBA: last block on the disk (little endian format) */
978 size_in_lba = vtoc->efi_last_lba;
979 if (size_in_lba < 0xffffffff) {
980 *cp++ = (size_in_lba & 0x000000ff);
981 *cp++ = (size_in_lba & 0x0000ff00) >> 8;
982 *cp++ = (size_in_lba & 0x00ff0000) >> 16;
983 *cp++ = (size_in_lba & 0xff000000) >> 24;
991 (void) memcpy(buf, &mb, sizeof (mb));
992 /* LINTED -- always longlong aligned */
993 dk_ioc.dki_data = (efi_gpt_t *)buf;
995 dk_ioc.dki_length = len;
996 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1011 /* make sure the user specified something reasonable */
1013 check_input(struct dk_gpt *vtoc)
1017 diskaddr_t istart, jstart, isize, jsize, endsect;
1020 * Sanity-check the input (make sure no partitions overlap)
1022 for (i = 0; i < vtoc->efi_nparts; i++) {
1023 /* It can't be unassigned and have an actual size */
1024 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1025 (vtoc->efi_parts[i].p_size != 0)) {
1027 (void) fprintf(stderr, "partition %d is "
1028 "\"unassigned\" but has a size of %llu",
1029 i, vtoc->efi_parts[i].p_size);
1033 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1034 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
1036 /* we have encountered an unknown uuid */
1037 vtoc->efi_parts[i].p_tag = 0xff;
1039 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1040 if (resv_part != -1) {
1042 (void) fprintf(stderr, "found "
1043 "duplicate reserved partition "
1050 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1051 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1053 (void) fprintf(stderr,
1054 "Partition %d starts at %llu. ",
1056 vtoc->efi_parts[i].p_start);
1057 (void) fprintf(stderr,
1058 "It must be between %llu and %llu.\n",
1059 vtoc->efi_first_u_lba,
1060 vtoc->efi_last_u_lba);
1064 if ((vtoc->efi_parts[i].p_start +
1065 vtoc->efi_parts[i].p_size <
1066 vtoc->efi_first_u_lba) ||
1067 (vtoc->efi_parts[i].p_start +
1068 vtoc->efi_parts[i].p_size >
1069 vtoc->efi_last_u_lba + 1)) {
1071 (void) fprintf(stderr,
1072 "Partition %d ends at %llu. ",
1074 vtoc->efi_parts[i].p_start +
1075 vtoc->efi_parts[i].p_size);
1076 (void) fprintf(stderr,
1077 "It must be between %llu and %llu.\n",
1078 vtoc->efi_first_u_lba,
1079 vtoc->efi_last_u_lba);
1084 for (j = 0; j < vtoc->efi_nparts; j++) {
1085 isize = vtoc->efi_parts[i].p_size;
1086 jsize = vtoc->efi_parts[j].p_size;
1087 istart = vtoc->efi_parts[i].p_start;
1088 jstart = vtoc->efi_parts[j].p_start;
1089 if ((i != j) && (isize != 0) && (jsize != 0)) {
1090 endsect = jstart + jsize -1;
1091 if ((jstart <= istart) &&
1092 (istart <= endsect)) {
1094 (void) fprintf(stderr,
1095 "Partition %d overlaps "
1096 "partition %d.", i, j);
1103 /* just a warning for now */
1104 if ((resv_part == -1) && efi_debug) {
1105 (void) fprintf(stderr,
1106 "no reserved partition found\n");
1112 * add all the unallocated space to the current label
1115 efi_use_whole_disk(int fd)
1117 struct dk_gpt *efi_label = NULL;
1120 uint_t resv_index = 0, data_index = 0;
1121 diskaddr_t resv_start = 0, data_start = 0;
1122 diskaddr_t data_size, limit, difference;
1123 boolean_t sync_needed = B_FALSE;
1126 rval = efi_alloc_and_read(fd, &efi_label);
1128 if (efi_label != NULL)
1129 efi_free(efi_label);
1134 * Find the last physically non-zero partition.
1135 * This should be the reserved partition.
1137 for (i = 0; i < efi_label->efi_nparts; i ++) {
1138 if (resv_start < efi_label->efi_parts[i].p_start) {
1139 resv_start = efi_label->efi_parts[i].p_start;
1145 * Find the last physically non-zero partition before that.
1146 * This is the data partition.
1148 for (i = 0; i < resv_index; i ++) {
1149 if (data_start < efi_label->efi_parts[i].p_start) {
1150 data_start = efi_label->efi_parts[i].p_start;
1154 data_size = efi_label->efi_parts[data_index].p_size;
1157 * See the "efi_alloc_and_init" function for more information
1158 * about where this "nblocks" value comes from.
1160 nblocks = efi_label->efi_first_u_lba - 1;
1163 * Determine if the EFI label is out of sync. We check that:
1165 * 1. the data partition ends at the limit we set, and
1166 * 2. the reserved partition starts at the limit we set.
1168 * If either of these conditions is not met, then we need to
1169 * resync the EFI label.
1171 * The limit is the last usable LBA, determined by the last LBA
1172 * and the first usable LBA fields on the EFI label of the disk
1173 * (see the lines directly above). Additionally, we factor in
1174 * EFI_MIN_RESV_SIZE (per its use in "zpool_label_disk") and
1175 * P2ALIGN it to ensure the partition boundaries are aligned
1176 * (for performance reasons). The alignment should match the
1177 * alignment used by the "zpool_label_disk" function.
1179 limit = P2ALIGN(efi_label->efi_last_lba - nblocks - EFI_MIN_RESV_SIZE,
1180 PARTITION_END_ALIGNMENT);
1181 if (data_start + data_size != limit || resv_start != limit)
1182 sync_needed = B_TRUE;
1184 if (efi_debug && sync_needed)
1185 (void) fprintf(stderr, "efi_use_whole_disk: sync needed\n");
1188 * If alter_lba is 1, we are using the backup label.
1189 * Since we can locate the backup label by disk capacity,
1190 * there must be no unallocated space.
1192 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
1193 >= efi_label->efi_last_lba && !sync_needed)) {
1195 (void) fprintf(stderr,
1196 "efi_use_whole_disk: requested space not found\n");
1198 efi_free(efi_label);
1203 * Verify that we've found the reserved partition by checking
1204 * that it looks the way it did when we created it in zpool_label_disk.
1205 * If we've found the incorrect partition, then we know that this
1206 * device was reformatted and no longer is solely used by ZFS.
1208 if ((efi_label->efi_parts[resv_index].p_size != EFI_MIN_RESV_SIZE) ||
1209 (efi_label->efi_parts[resv_index].p_tag != V_RESERVED) ||
1210 (resv_index != 8)) {
1212 (void) fprintf(stderr,
1213 "efi_use_whole_disk: wholedisk not available\n");
1215 efi_free(efi_label);
1219 if (data_start + data_size != resv_start) {
1221 (void) fprintf(stderr,
1222 "efi_use_whole_disk: "
1223 "data_start (%lli) + "
1224 "data_size (%lli) != "
1225 "resv_start (%lli)\n",
1226 data_start, data_size, resv_start);
1232 if (limit < resv_start) {
1234 (void) fprintf(stderr,
1235 "efi_use_whole_disk: "
1236 "limit (%lli) < resv_start (%lli)\n",
1243 difference = limit - resv_start;
1246 (void) fprintf(stderr,
1247 "efi_use_whole_disk: difference is %lli\n", difference);
1250 * Move the reserved partition. There is currently no data in
1251 * here except fabricated devids (which get generated via
1252 * efi_write()). So there is no need to copy data.
1254 efi_label->efi_parts[data_index].p_size += difference;
1255 efi_label->efi_parts[resv_index].p_start += difference;
1256 efi_label->efi_last_u_lba = efi_label->efi_last_lba - nblocks;
1258 rval = efi_write(fd, efi_label);
1261 (void) fprintf(stderr,
1262 "efi_use_whole_disk:fail to write label, rval=%d\n",
1265 efi_free(efi_label);
1269 efi_free(efi_label);
1274 * write EFI label and backup label
1277 efi_write(int fd, struct dk_gpt *vtoc)
1281 efi_gpe_t *efi_parts;
1283 struct dk_cinfo dki_info;
1287 diskaddr_t lba_backup_gpt_hdr;
1289 if ((rval = efi_get_info(fd, &dki_info)) != 0)
1292 /* check if we are dealing with a metadevice */
1293 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
1294 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
1298 if (check_input(vtoc)) {
1300 * not valid; if it's a metadevice just pass it down
1301 * because SVM will do its own checking
1309 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
1310 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
1312 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
1313 vtoc->efi_lbasize) *
1318 * the number of blocks occupied by GUID partition entry array
1320 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
1323 * Backup GPT header is located on the block after GUID
1324 * partition entry array. Here, we calculate the address
1325 * for backup GPT header.
1327 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
1328 if (posix_memalign((void **)&dk_ioc.dki_data,
1329 vtoc->efi_lbasize, dk_ioc.dki_length))
1332 memset(dk_ioc.dki_data, 0, dk_ioc.dki_length);
1333 efi = dk_ioc.dki_data;
1335 /* stuff user's input into EFI struct */
1336 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1337 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
1338 efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt) - LEN_EFI_PAD);
1339 efi->efi_gpt_Reserved1 = 0;
1340 efi->efi_gpt_MyLBA = LE_64(1ULL);
1341 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
1342 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
1343 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
1344 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1345 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
1346 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
1347 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
1349 /* LINTED -- always longlong aligned */
1350 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
1352 for (i = 0; i < vtoc->efi_nparts; i++) {
1354 j < sizeof (conversion_array) /
1355 sizeof (struct uuid_to_ptag); j++) {
1357 if (vtoc->efi_parts[i].p_tag == j) {
1359 efi_parts[i].efi_gpe_PartitionTypeGUID,
1360 conversion_array[j].uuid);
1365 if (j == sizeof (conversion_array) /
1366 sizeof (struct uuid_to_ptag)) {
1368 * If we didn't have a matching uuid match, bail here.
1369 * Don't write a label with unknown uuid.
1372 (void) fprintf(stderr,
1373 "Unknown uuid for p_tag %d\n",
1374 vtoc->efi_parts[i].p_tag);
1379 /* Zero's should be written for empty partitions */
1380 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
1383 efi_parts[i].efi_gpe_StartingLBA =
1384 LE_64(vtoc->efi_parts[i].p_start);
1385 efi_parts[i].efi_gpe_EndingLBA =
1386 LE_64(vtoc->efi_parts[i].p_start +
1387 vtoc->efi_parts[i].p_size - 1);
1388 efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
1389 LE_16(vtoc->efi_parts[i].p_flag);
1390 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1391 efi_parts[i].efi_gpe_PartitionName[j] =
1392 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1394 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1395 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1396 (void) uuid_generate((uchar_t *)
1397 &vtoc->efi_parts[i].p_uguid);
1399 bcopy(&vtoc->efi_parts[i].p_uguid,
1400 &efi_parts[i].efi_gpe_UniquePartitionGUID,
1403 efi->efi_gpt_PartitionEntryArrayCRC32 =
1404 LE_32(efi_crc32((unsigned char *)efi_parts,
1405 vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1406 efi->efi_gpt_HeaderCRC32 =
1407 LE_32(efi_crc32((unsigned char *)efi,
1408 LE_32(efi->efi_gpt_HeaderSize)));
1410 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1411 free(dk_ioc.dki_data);
1421 /* if it's a metadevice we're done */
1423 free(dk_ioc.dki_data);
1427 /* write backup partition array */
1428 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
1429 dk_ioc.dki_length -= vtoc->efi_lbasize;
1431 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
1434 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1436 * we wrote the primary label okay, so don't fail
1439 (void) fprintf(stderr,
1440 "write of backup partitions to block %llu "
1441 "failed, errno %d\n",
1442 vtoc->efi_last_u_lba + 1,
1447 * now swap MyLBA and AlternateLBA fields and write backup
1448 * partition table header
1450 dk_ioc.dki_lba = lba_backup_gpt_hdr;
1451 dk_ioc.dki_length = vtoc->efi_lbasize;
1453 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1455 efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1456 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1457 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1458 efi->efi_gpt_HeaderCRC32 = 0;
1459 efi->efi_gpt_HeaderCRC32 =
1460 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1461 LE_32(efi->efi_gpt_HeaderSize)));
1463 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1465 (void) fprintf(stderr,
1466 "write of backup header to block %llu failed, "
1472 /* write the PMBR */
1473 (void) write_pmbr(fd, vtoc);
1474 free(dk_ioc.dki_data);
1480 efi_free(struct dk_gpt *ptr)
1486 * Input: File descriptor
1487 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1495 struct extvtoc extvtoc;
1497 if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1498 if (errno == ENOTSUP)
1500 else if (errno == ENOTTY) {
1501 if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1502 if (errno == ENOTSUP)
1513 efi_err_check(struct dk_gpt *vtoc)
1517 diskaddr_t istart, jstart, isize, jsize, endsect;
1521 * make sure no partitions overlap
1523 for (i = 0; i < vtoc->efi_nparts; i++) {
1524 /* It can't be unassigned and have an actual size */
1525 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1526 (vtoc->efi_parts[i].p_size != 0)) {
1527 (void) fprintf(stderr,
1528 "partition %d is \"unassigned\" but has a size "
1529 "of %llu\n", i, vtoc->efi_parts[i].p_size);
1531 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1534 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1535 if (resv_part != -1) {
1536 (void) fprintf(stderr,
1537 "found duplicate reserved partition at "
1541 if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1542 (void) fprintf(stderr,
1543 "Warning: reserved partition size must "
1544 "be %d sectors\n", EFI_MIN_RESV_SIZE);
1546 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1547 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1548 (void) fprintf(stderr,
1549 "Partition %d starts at %llu\n",
1551 vtoc->efi_parts[i].p_start);
1552 (void) fprintf(stderr,
1553 "It must be between %llu and %llu.\n",
1554 vtoc->efi_first_u_lba,
1555 vtoc->efi_last_u_lba);
1557 if ((vtoc->efi_parts[i].p_start +
1558 vtoc->efi_parts[i].p_size <
1559 vtoc->efi_first_u_lba) ||
1560 (vtoc->efi_parts[i].p_start +
1561 vtoc->efi_parts[i].p_size >
1562 vtoc->efi_last_u_lba + 1)) {
1563 (void) fprintf(stderr,
1564 "Partition %d ends at %llu\n",
1566 vtoc->efi_parts[i].p_start +
1567 vtoc->efi_parts[i].p_size);
1568 (void) fprintf(stderr,
1569 "It must be between %llu and %llu.\n",
1570 vtoc->efi_first_u_lba,
1571 vtoc->efi_last_u_lba);
1574 for (j = 0; j < vtoc->efi_nparts; j++) {
1575 isize = vtoc->efi_parts[i].p_size;
1576 jsize = vtoc->efi_parts[j].p_size;
1577 istart = vtoc->efi_parts[i].p_start;
1578 jstart = vtoc->efi_parts[j].p_start;
1579 if ((i != j) && (isize != 0) && (jsize != 0)) {
1580 endsect = jstart + jsize -1;
1581 if ((jstart <= istart) &&
1582 (istart <= endsect)) {
1584 (void) fprintf(stderr,
1585 "label error: EFI Labels do not "
1586 "support overlapping partitions\n");
1588 (void) fprintf(stderr,
1589 "Partition %d overlaps partition "
1596 /* make sure there is a reserved partition */
1597 if (resv_part == -1) {
1598 (void) fprintf(stderr,
1599 "no reserved partition found\n");
1604 * We need to get information necessary to construct a *new* efi
1608 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1614 * Now build the default partition table
1616 if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1618 (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1623 for (i = 0; i < MIN((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1624 (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1625 (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1626 (*vtoc)->efi_parts[i].p_start = 0;
1627 (*vtoc)->efi_parts[i].p_size = 0;
1630 * Make constants first
1631 * and variable partitions later
1634 /* root partition - s0 128 MB */
1635 (*vtoc)->efi_parts[0].p_start = 34;
1636 (*vtoc)->efi_parts[0].p_size = 262144;
1638 /* partition - s1 128 MB */
1639 (*vtoc)->efi_parts[1].p_start = 262178;
1640 (*vtoc)->efi_parts[1].p_size = 262144;
1642 /* partition -s2 is NOT the Backup disk */
1643 (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1645 /* partition -s6 /usr partition - HOG */
1646 (*vtoc)->efi_parts[6].p_start = 524322;
1647 (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1650 /* efi reserved partition - s9 16K */
1651 (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1652 (*vtoc)->efi_parts[8].p_size = (1024 * 16);
1653 (*vtoc)->efi_parts[8].p_tag = V_RESERVED;