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>
47 #include <linux/blkpg.h>
49 static struct uuid_to_ptag {
51 } conversion_array[] = {
58 { EFI_UNUSED }, /* STAND is never used */
62 { EFI_UNUSED }, /* CACHE (cachefs) is never used */
79 { EFI_FREEBSD_VINUM },
89 { EFI_MSFT_STORAGESPACES },
97 { EFI_FREEBSD_DISKLABEL },
99 { EFI_AAPL_RAIDOFFLINE },
102 { EFI_AAPL_TVRECOVERY },
103 { EFI_AAPL_CORESTORAGE },
109 { EFI_NETBSD_CRYPT },
114 { EFI_MIDNIGHTBSD_BOOT },
115 { EFI_MIDNIGHTBSD_DATA },
116 { EFI_MIDNIGHTBSD_SWAP },
117 { EFI_MIDNIGHTBSD_UFS },
118 { EFI_MIDNIGHTBSD_VINUM },
119 { EFI_MIDNIGHTBSD_ZFS },
120 { EFI_CEPH_JOURNAL },
121 { EFI_CEPH_DMCRYPTJOURNAL },
123 { EFI_CEPH_DMCRYPTOSD },
125 { EFI_CEPH_DMCRYPTCREATE },
126 { EFI_OPENBSD_DISKLABEL },
133 { EFI_RHT_ROOTAMD64 },
135 { EFI_RHT_ROOTARM64 },
136 { EFI_ACRONIS_SECUREZONE },
139 { EFI_IBM_PPRPBOOT },
140 { EFI_FREEDESKTOP_BOOT }
144 * Default vtoc information for non-SVr4 partitions
146 struct dk_map2 default_vtoc_map[NDKMAP] = {
147 { V_ROOT, 0 }, /* a - 0 */
148 { V_SWAP, V_UNMNT }, /* b - 1 */
149 { V_BACKUP, V_UNMNT }, /* c - 2 */
150 { V_UNASSIGNED, 0 }, /* d - 3 */
151 { V_UNASSIGNED, 0 }, /* e - 4 */
152 { V_UNASSIGNED, 0 }, /* f - 5 */
153 { V_USR, 0 }, /* g - 6 */
154 { V_UNASSIGNED, 0 }, /* h - 7 */
156 #if defined(_SUNOS_VTOC_16)
158 #if defined(i386) || defined(__amd64) || defined(__arm) || \
159 defined(__powerpc) || defined(__sparc) || defined(__s390__) || \
160 defined(__mips__) || defined(__rv64g__)
161 { V_BOOT, V_UNMNT }, /* i - 8 */
162 { V_ALTSCTR, 0 }, /* j - 9 */
165 #error No VTOC format defined.
166 #endif /* defined(i386) */
168 { V_UNASSIGNED, 0 }, /* k - 10 */
169 { V_UNASSIGNED, 0 }, /* l - 11 */
170 { V_UNASSIGNED, 0 }, /* m - 12 */
171 { V_UNASSIGNED, 0 }, /* n - 13 */
172 { V_UNASSIGNED, 0 }, /* o - 14 */
173 { V_UNASSIGNED, 0 }, /* p - 15 */
174 #endif /* defined(_SUNOS_VTOC_16) */
179 static int efi_read(int, struct dk_gpt *);
182 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
183 * one's conditioning will be handled by crc32() internally.
186 efi_crc32(const unsigned char *buf, unsigned int size)
188 uint32_t crc = crc32(0, Z_NULL, 0);
190 crc = crc32(crc, buf, size);
196 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
199 unsigned long long capacity_size;
201 if (ioctl(fd, BLKSSZGET, §or_size) < 0)
204 if (ioctl(fd, BLKGETSIZE64, &capacity_size) < 0)
207 *lbsize = (uint_t)sector_size;
208 *capacity = (diskaddr_t)(capacity_size / sector_size);
214 * Return back the device name associated with the file descriptor. The
215 * caller is responsible for freeing the memory associated with the
219 efi_get_devname(int fd)
224 path = calloc(1, PATH_MAX);
229 * The libefi API only provides the open fd and not the file path.
230 * To handle this realpath(3) is used to resolve the block device
231 * name from /proc/self/fd/<fd>.
233 (void) sprintf(path, "/proc/self/fd/%d", fd);
234 dev_name = realpath(path, NULL);
240 efi_get_info(int fd, struct dk_cinfo *dki_info)
245 memset(dki_info, 0, sizeof (*dki_info));
248 * The simplest way to get the partition number under linux is
249 * to parse it out of the /dev/<disk><partition> block device name.
250 * The kernel creates this using the partition number when it
251 * populates /dev/ so it may be trusted. The tricky bit here is
252 * that the naming convention is based on the block device type.
253 * So we need to take this in to account when parsing out the
254 * partition information. Aside from the partition number we collect
255 * some additional device info.
257 dev_path = efi_get_devname(fd);
258 if (dev_path == NULL)
261 if ((strncmp(dev_path, "/dev/sd", 7) == 0)) {
262 strcpy(dki_info->dki_cname, "sd");
263 dki_info->dki_ctype = DKC_SCSI_CCS;
264 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
266 &dki_info->dki_partition);
267 } else if ((strncmp(dev_path, "/dev/hd", 7) == 0)) {
268 strcpy(dki_info->dki_cname, "hd");
269 dki_info->dki_ctype = DKC_DIRECT;
270 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
272 &dki_info->dki_partition);
273 } else if ((strncmp(dev_path, "/dev/md", 7) == 0)) {
274 strcpy(dki_info->dki_cname, "pseudo");
275 dki_info->dki_ctype = DKC_MD;
276 strcpy(dki_info->dki_dname, "md");
277 rval = sscanf(dev_path, "/dev/md%[0-9]p%hu",
278 dki_info->dki_dname + 2,
279 &dki_info->dki_partition);
280 } else if ((strncmp(dev_path, "/dev/vd", 7) == 0)) {
281 strcpy(dki_info->dki_cname, "vd");
282 dki_info->dki_ctype = DKC_MD;
283 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
285 &dki_info->dki_partition);
286 } else if ((strncmp(dev_path, "/dev/xvd", 8) == 0)) {
287 strcpy(dki_info->dki_cname, "xvd");
288 dki_info->dki_ctype = DKC_MD;
289 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
291 &dki_info->dki_partition);
292 } else if ((strncmp(dev_path, "/dev/zd", 7) == 0)) {
293 strcpy(dki_info->dki_cname, "zd");
294 dki_info->dki_ctype = DKC_MD;
295 strcpy(dki_info->dki_dname, "zd");
296 rval = sscanf(dev_path, "/dev/zd%[0-9]p%hu",
297 dki_info->dki_dname + 2,
298 &dki_info->dki_partition);
299 } else if ((strncmp(dev_path, "/dev/dm-", 8) == 0)) {
300 strcpy(dki_info->dki_cname, "pseudo");
301 dki_info->dki_ctype = DKC_VBD;
302 strcpy(dki_info->dki_dname, "dm-");
303 rval = sscanf(dev_path, "/dev/dm-%[0-9]p%hu",
304 dki_info->dki_dname + 3,
305 &dki_info->dki_partition);
306 } else if ((strncmp(dev_path, "/dev/ram", 8) == 0)) {
307 strcpy(dki_info->dki_cname, "pseudo");
308 dki_info->dki_ctype = DKC_PCMCIA_MEM;
309 strcpy(dki_info->dki_dname, "ram");
310 rval = sscanf(dev_path, "/dev/ram%[0-9]p%hu",
311 dki_info->dki_dname + 3,
312 &dki_info->dki_partition);
313 } else if ((strncmp(dev_path, "/dev/loop", 9) == 0)) {
314 strcpy(dki_info->dki_cname, "pseudo");
315 dki_info->dki_ctype = DKC_VBD;
316 strcpy(dki_info->dki_dname, "loop");
317 rval = sscanf(dev_path, "/dev/loop%[0-9]p%hu",
318 dki_info->dki_dname + 4,
319 &dki_info->dki_partition);
320 } else if ((strncmp(dev_path, "/dev/nvme", 9) == 0)) {
321 strcpy(dki_info->dki_cname, "nvme");
322 dki_info->dki_ctype = DKC_SCSI_CCS;
323 strcpy(dki_info->dki_dname, "nvme");
324 (void) sscanf(dev_path, "/dev/nvme%[0-9]",
325 dki_info->dki_dname + 4);
326 size_t controller_length = strlen(
327 dki_info->dki_dname);
328 strcpy(dki_info->dki_dname + controller_length,
330 rval = sscanf(dev_path,
331 "/dev/nvme%*[0-9]n%[0-9]p%hu",
332 dki_info->dki_dname + controller_length + 1,
333 &dki_info->dki_partition);
335 strcpy(dki_info->dki_dname, "unknown");
336 strcpy(dki_info->dki_cname, "unknown");
337 dki_info->dki_ctype = DKC_UNKNOWN;
345 dki_info->dki_partition = 0;
353 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
366 * the number of blocks the EFI label takes up (round up to nearest
369 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
371 /* number of partitions -- limited by what we can malloc */
372 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
373 sizeof (struct dk_part))
376 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
378 diskaddr_t capacity = 0;
384 struct dk_cinfo dki_info;
386 if (read_disk_info(fd, &capacity, &lbsize) != 0)
389 if (efi_get_info(fd, &dki_info) != 0)
392 if (dki_info.dki_partition != 0)
395 if ((dki_info.dki_ctype == DKC_PCMCIA_MEM) ||
396 (dki_info.dki_ctype == DKC_VBD) ||
397 (dki_info.dki_ctype == DKC_UNKNOWN))
400 nblocks = NBLOCKS(nparts, lbsize);
401 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
402 /* 16K plus one block for the GPT */
403 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
406 if (nparts > MAX_PARTS) {
408 (void) fprintf(stderr,
409 "the maximum number of partitions supported is %lu\n",
415 length = sizeof (struct dk_gpt) +
416 sizeof (struct dk_part) * (nparts - 1);
418 vptr = calloc(1, length);
424 vptr->efi_version = EFI_VERSION_CURRENT;
425 vptr->efi_lbasize = lbsize;
426 vptr->efi_nparts = nparts;
428 * add one block here for the PMBR; on disks with a 512 byte
429 * block size and 128 or fewer partitions, efi_first_u_lba
430 * should work out to "34"
432 vptr->efi_first_u_lba = nblocks + 1;
433 vptr->efi_last_lba = capacity - 1;
434 vptr->efi_altern_lba = capacity -1;
435 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
437 (void) uuid_generate((uchar_t *)&uuid);
438 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
443 * Read EFI - return partition number upon success.
446 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
453 /* figure out the number of entries that would fit into 16K */
454 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
455 length = (int) sizeof (struct dk_gpt) +
456 (int) sizeof (struct dk_part) * (nparts - 1);
457 vptr = calloc(1, length);
462 vptr->efi_nparts = nparts;
463 rval = efi_read(fd, vptr);
465 if ((rval == VT_EINVAL) && vptr->efi_nparts > nparts) {
467 length = (int) sizeof (struct dk_gpt) +
468 (int) sizeof (struct dk_part) * (vptr->efi_nparts - 1);
469 nparts = vptr->efi_nparts;
470 if ((tmp = realloc(vptr, length)) == NULL) {
476 rval = efi_read(fd, vptr);
482 (void) fprintf(stderr,
483 "read of EFI table failed, rval=%d\n", rval);
495 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
497 void *data = dk_ioc->dki_data;
503 * When the IO is not being performed in kernel as an ioctl we need
504 * to know the sector size so we can seek to the proper byte offset.
506 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
508 fprintf(stderr, "unable to read disk info: %d", errno);
518 (void) fprintf(stderr, "DKIOCGETEFI assuming "
519 "LBA %d bytes\n", DEV_BSIZE);
524 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
527 (void) fprintf(stderr, "DKIOCGETEFI lseek "
528 "error: %d\n", errno);
532 error = read(fd, data, dk_ioc->dki_length);
535 (void) fprintf(stderr, "DKIOCGETEFI read "
536 "error: %d\n", errno);
540 if (error != dk_ioc->dki_length) {
542 (void) fprintf(stderr, "DKIOCGETEFI short "
543 "read of %d bytes\n", error);
553 (void) fprintf(stderr, "DKIOCSETEFI unknown "
559 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
562 (void) fprintf(stderr, "DKIOCSETEFI lseek "
563 "error: %d\n", errno);
567 error = write(fd, data, dk_ioc->dki_length);
570 (void) fprintf(stderr, "DKIOCSETEFI write "
571 "error: %d\n", errno);
575 if (error != dk_ioc->dki_length) {
577 (void) fprintf(stderr, "DKIOCSETEFI short "
578 "write of %d bytes\n", error);
583 /* Sync the new EFI table to disk */
588 /* Ensure any local disk cache is also flushed */
589 if (ioctl(fd, BLKFLSBUF, 0) == -1)
597 (void) fprintf(stderr, "unsupported ioctl()\n");
612 /* Notify the kernel a devices partition table has been updated */
613 while ((error = ioctl(fd, BLKRRPART)) != 0) {
614 if ((--retry == 0) || (errno != EBUSY)) {
615 (void) fprintf(stderr, "the kernel failed to rescan "
616 "the partition table: %d\n", errno);
626 check_label(int fd, dk_efi_t *dk_ioc)
631 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
639 efi = dk_ioc->dki_data;
640 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
642 (void) fprintf(stderr,
643 "Bad EFI signature: 0x%llx != 0x%llx\n",
644 (long long)efi->efi_gpt_Signature,
645 (long long)LE_64(EFI_SIGNATURE));
650 * check CRC of the header; the size of the header should
651 * never be larger than one block
653 crc = efi->efi_gpt_HeaderCRC32;
654 efi->efi_gpt_HeaderCRC32 = 0;
655 len_t headerSize = (len_t)LE_32(efi->efi_gpt_HeaderSize);
657 if (headerSize < EFI_MIN_LABEL_SIZE || headerSize > EFI_LABEL_SIZE) {
659 (void) fprintf(stderr,
660 "Invalid EFI HeaderSize %llu. Assuming %d.\n",
661 headerSize, EFI_MIN_LABEL_SIZE);
664 if ((headerSize > dk_ioc->dki_length) ||
665 crc != LE_32(efi_crc32((unsigned char *)efi, headerSize))) {
667 (void) fprintf(stderr,
668 "Bad EFI CRC: 0x%x != 0x%x\n",
669 crc, LE_32(efi_crc32((unsigned char *)efi,
678 efi_read(int fd, struct dk_gpt *vtoc)
685 diskaddr_t capacity = 0;
687 struct dk_minfo disk_info;
690 efi_gpe_t *efi_parts;
691 struct dk_cinfo dki_info;
692 uint32_t user_length;
693 boolean_t legacy_label = B_FALSE;
696 * get the partition number for this file descriptor.
698 if ((rval = efi_get_info(fd, &dki_info)) != 0)
701 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
702 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
704 } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
705 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
707 * The controller and drive name "vdc" (virtual disk client)
708 * indicates a LDoms virtual disk.
713 /* get the LBA size */
714 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
716 (void) fprintf(stderr,
717 "unable to read disk info: %d",
723 disk_info.dki_lbsize = lbsize;
724 disk_info.dki_capacity = capacity;
726 if (disk_info.dki_lbsize == 0) {
728 (void) fprintf(stderr,
729 "efi_read: assuming LBA 512 bytes\n");
731 disk_info.dki_lbsize = DEV_BSIZE;
734 * Read the EFI GPT to figure out how many partitions we need
738 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
739 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
741 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
742 disk_info.dki_lbsize;
743 if (label_len % disk_info.dki_lbsize) {
744 /* pad to physical sector size */
745 label_len += disk_info.dki_lbsize;
746 label_len &= ~(disk_info.dki_lbsize - 1);
750 if (posix_memalign((void **)&dk_ioc.dki_data,
751 disk_info.dki_lbsize, label_len))
754 memset(dk_ioc.dki_data, 0, label_len);
755 dk_ioc.dki_length = disk_info.dki_lbsize;
756 user_length = vtoc->efi_nparts;
757 efi = dk_ioc.dki_data;
759 dk_ioc.dki_length = label_len;
760 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
768 } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
770 * No valid label here; try the alternate. Note that here
771 * we just read GPT header and save it into dk_ioc.data,
772 * Later, we will read GUID partition entry array if we
773 * can get valid GPT header.
777 * This is a workaround for legacy systems. In the past, the
778 * last sector of SCSI disk was invisible on x86 platform. At
779 * that time, backup label was saved on the next to the last
780 * sector. It is possible for users to move a disk from previous
781 * solaris system to present system. Here, we attempt to search
782 * legacy backup EFI label first.
784 dk_ioc.dki_lba = disk_info.dki_capacity - 2;
785 dk_ioc.dki_length = disk_info.dki_lbsize;
786 rval = check_label(fd, &dk_ioc);
787 if (rval == VT_EINVAL) {
789 * we didn't find legacy backup EFI label, try to
790 * search backup EFI label in the last block.
792 dk_ioc.dki_lba = disk_info.dki_capacity - 1;
793 dk_ioc.dki_length = disk_info.dki_lbsize;
794 rval = check_label(fd, &dk_ioc);
796 legacy_label = B_TRUE;
798 (void) fprintf(stderr,
799 "efi_read: primary label corrupt; "
800 "using EFI backup label located on"
801 " the last block\n");
804 if ((efi_debug) && (rval == 0))
805 (void) fprintf(stderr, "efi_read: primary label"
806 " corrupt; using legacy EFI backup label "
807 " located on the next to last block\n");
811 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
812 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
814 LE_32(efi->efi_gpt_NumberOfPartitionEntries);
816 * Partition tables are between backup GPT header
817 * table and ParitionEntryLBA (the starting LBA of
818 * the GUID partition entries array). Now that we
819 * already got valid GPT header and saved it in
820 * dk_ioc.dki_data, we try to get GUID partition
824 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
825 + disk_info.dki_lbsize);
827 dk_ioc.dki_length = disk_info.dki_capacity - 1 -
830 dk_ioc.dki_length = disk_info.dki_capacity - 2 -
832 dk_ioc.dki_length *= disk_info.dki_lbsize;
833 if (dk_ioc.dki_length >
834 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
838 * read GUID partition entry array
840 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
844 } else if (rval == 0) {
846 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
848 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
849 + disk_info.dki_lbsize);
850 dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
851 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
853 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
855 * When the device is a LDoms virtual disk, the DKIOCGETEFI
856 * ioctl can fail with EINVAL if the virtual disk backend
857 * is a ZFS volume serviced by a domain running an old version
858 * of Solaris. This is because the DKIOCGETEFI ioctl was
859 * initially incorrectly implemented for a ZFS volume and it
860 * expected the GPT and GPE to be retrieved with a single ioctl.
861 * So we try to read the GPT and the GPE using that old style
865 dk_ioc.dki_length = label_len;
866 rval = check_label(fd, &dk_ioc);
874 /* LINTED -- always longlong aligned */
875 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
878 * Assemble this into a "dk_gpt" struct for easier
879 * digestibility by applications.
881 vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
882 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
883 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
884 vtoc->efi_lbasize = disk_info.dki_lbsize;
885 vtoc->efi_last_lba = disk_info.dki_capacity - 1;
886 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
887 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
888 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
889 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
892 * If the array the user passed in is too small, set the length
893 * to what it needs to be and return
895 if (user_length < vtoc->efi_nparts) {
899 for (i = 0; i < vtoc->efi_nparts; i++) {
901 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
902 efi_parts[i].efi_gpe_PartitionTypeGUID);
905 j < sizeof (conversion_array)
906 / sizeof (struct uuid_to_ptag); j++) {
908 if (bcmp(&vtoc->efi_parts[i].p_guid,
909 &conversion_array[j].uuid,
910 sizeof (struct uuid)) == 0) {
911 vtoc->efi_parts[i].p_tag = j;
915 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
917 vtoc->efi_parts[i].p_flag =
918 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
919 vtoc->efi_parts[i].p_start =
920 LE_64(efi_parts[i].efi_gpe_StartingLBA);
921 vtoc->efi_parts[i].p_size =
922 LE_64(efi_parts[i].efi_gpe_EndingLBA) -
923 vtoc->efi_parts[i].p_start + 1;
924 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
925 vtoc->efi_parts[i].p_name[j] =
927 efi_parts[i].efi_gpe_PartitionName[j]);
930 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
931 efi_parts[i].efi_gpe_UniquePartitionGUID);
935 return (dki_info.dki_partition);
938 /* writes a "protective" MBR */
940 write_pmbr(int fd, struct dk_gpt *vtoc)
945 diskaddr_t size_in_lba;
949 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
950 if (posix_memalign((void **)&buf, len, len))
954 * Preserve any boot code and disk signature if the first block is
959 dk_ioc.dki_length = len;
960 /* LINTED -- always longlong aligned */
961 dk_ioc.dki_data = (efi_gpt_t *)buf;
962 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
963 (void) memcpy(&mb, buf, sizeof (mb));
964 bzero(&mb, sizeof (mb));
965 mb.signature = LE_16(MBB_MAGIC);
967 (void) memcpy(&mb, buf, sizeof (mb));
968 if (mb.signature != LE_16(MBB_MAGIC)) {
969 bzero(&mb, sizeof (mb));
970 mb.signature = LE_16(MBB_MAGIC);
974 bzero(&mb.parts, sizeof (mb.parts));
975 cp = (uchar_t *)&mb.parts[0];
976 /* bootable or not */
978 /* beginning CHS; 0xffffff if not representable */
984 /* ending CHS; 0xffffff if not representable */
988 /* starting LBA: 1 (little endian format) by EFI definition */
993 /* ending LBA: last block on the disk (little endian format) */
994 size_in_lba = vtoc->efi_last_lba;
995 if (size_in_lba < 0xffffffff) {
996 *cp++ = (size_in_lba & 0x000000ff);
997 *cp++ = (size_in_lba & 0x0000ff00) >> 8;
998 *cp++ = (size_in_lba & 0x00ff0000) >> 16;
999 *cp++ = (size_in_lba & 0xff000000) >> 24;
1007 (void) memcpy(buf, &mb, sizeof (mb));
1008 /* LINTED -- always longlong aligned */
1009 dk_ioc.dki_data = (efi_gpt_t *)buf;
1011 dk_ioc.dki_length = len;
1012 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1027 /* make sure the user specified something reasonable */
1029 check_input(struct dk_gpt *vtoc)
1033 diskaddr_t istart, jstart, isize, jsize, endsect;
1036 * Sanity-check the input (make sure no partitions overlap)
1038 for (i = 0; i < vtoc->efi_nparts; i++) {
1039 /* It can't be unassigned and have an actual size */
1040 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1041 (vtoc->efi_parts[i].p_size != 0)) {
1043 (void) fprintf(stderr, "partition %d is "
1044 "\"unassigned\" but has a size of %llu",
1045 i, vtoc->efi_parts[i].p_size);
1049 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1050 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
1052 /* we have encountered an unknown uuid */
1053 vtoc->efi_parts[i].p_tag = 0xff;
1055 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1056 if (resv_part != -1) {
1058 (void) fprintf(stderr, "found "
1059 "duplicate reserved partition "
1066 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1067 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1069 (void) fprintf(stderr,
1070 "Partition %d starts at %llu. ",
1072 vtoc->efi_parts[i].p_start);
1073 (void) fprintf(stderr,
1074 "It must be between %llu and %llu.\n",
1075 vtoc->efi_first_u_lba,
1076 vtoc->efi_last_u_lba);
1080 if ((vtoc->efi_parts[i].p_start +
1081 vtoc->efi_parts[i].p_size <
1082 vtoc->efi_first_u_lba) ||
1083 (vtoc->efi_parts[i].p_start +
1084 vtoc->efi_parts[i].p_size >
1085 vtoc->efi_last_u_lba + 1)) {
1087 (void) fprintf(stderr,
1088 "Partition %d ends at %llu. ",
1090 vtoc->efi_parts[i].p_start +
1091 vtoc->efi_parts[i].p_size);
1092 (void) fprintf(stderr,
1093 "It must be between %llu and %llu.\n",
1094 vtoc->efi_first_u_lba,
1095 vtoc->efi_last_u_lba);
1100 for (j = 0; j < vtoc->efi_nparts; j++) {
1101 isize = vtoc->efi_parts[i].p_size;
1102 jsize = vtoc->efi_parts[j].p_size;
1103 istart = vtoc->efi_parts[i].p_start;
1104 jstart = vtoc->efi_parts[j].p_start;
1105 if ((i != j) && (isize != 0) && (jsize != 0)) {
1106 endsect = jstart + jsize -1;
1107 if ((jstart <= istart) &&
1108 (istart <= endsect)) {
1110 (void) fprintf(stderr,
1111 "Partition %d overlaps "
1112 "partition %d.", i, j);
1119 /* just a warning for now */
1120 if ((resv_part == -1) && efi_debug) {
1121 (void) fprintf(stderr,
1122 "no reserved partition found\n");
1128 call_blkpg_ioctl(int fd, int command, diskaddr_t start,
1129 diskaddr_t size, uint_t pno)
1131 struct blkpg_ioctl_arg ioctl_arg;
1132 struct blkpg_partition linux_part;
1133 memset(&linux_part, 0, sizeof (linux_part));
1135 char *path = efi_get_devname(fd);
1137 (void) fprintf(stderr, "failed to retrieve device name\n");
1141 linux_part.start = start;
1142 linux_part.length = size;
1143 linux_part.pno = pno;
1144 snprintf(linux_part.devname, BLKPG_DEVNAMELTH - 1, "%s%u", path, pno);
1145 linux_part.devname[BLKPG_DEVNAMELTH - 1] = '\0';
1148 ioctl_arg.op = command;
1149 ioctl_arg.flags = 0;
1150 ioctl_arg.datalen = sizeof (struct blkpg_partition);
1151 ioctl_arg.data = &linux_part;
1153 return (ioctl(fd, BLKPG, &ioctl_arg));
1157 * add all the unallocated space to the current label
1160 efi_use_whole_disk(int fd)
1162 struct dk_gpt *efi_label = NULL;
1165 uint_t resv_index = 0, data_index = 0;
1166 diskaddr_t resv_start = 0, data_start = 0;
1167 diskaddr_t data_size, limit, difference;
1168 boolean_t sync_needed = B_FALSE;
1171 rval = efi_alloc_and_read(fd, &efi_label);
1173 if (efi_label != NULL)
1174 efi_free(efi_label);
1179 * Find the last physically non-zero partition.
1180 * This should be the reserved partition.
1182 for (i = 0; i < efi_label->efi_nparts; i ++) {
1183 if (resv_start < efi_label->efi_parts[i].p_start) {
1184 resv_start = efi_label->efi_parts[i].p_start;
1190 * Find the last physically non-zero partition before that.
1191 * This is the data partition.
1193 for (i = 0; i < resv_index; i ++) {
1194 if (data_start < efi_label->efi_parts[i].p_start) {
1195 data_start = efi_label->efi_parts[i].p_start;
1199 data_size = efi_label->efi_parts[data_index].p_size;
1202 * See the "efi_alloc_and_init" function for more information
1203 * about where this "nblocks" value comes from.
1205 nblocks = efi_label->efi_first_u_lba - 1;
1208 * Determine if the EFI label is out of sync. We check that:
1210 * 1. the data partition ends at the limit we set, and
1211 * 2. the reserved partition starts at the limit we set.
1213 * If either of these conditions is not met, then we need to
1214 * resync the EFI label.
1216 * The limit is the last usable LBA, determined by the last LBA
1217 * and the first usable LBA fields on the EFI label of the disk
1218 * (see the lines directly above). Additionally, we factor in
1219 * EFI_MIN_RESV_SIZE (per its use in "zpool_label_disk") and
1220 * P2ALIGN it to ensure the partition boundaries are aligned
1221 * (for performance reasons). The alignment should match the
1222 * alignment used by the "zpool_label_disk" function.
1224 limit = P2ALIGN(efi_label->efi_last_lba - nblocks - EFI_MIN_RESV_SIZE,
1225 PARTITION_END_ALIGNMENT);
1226 if (data_start + data_size != limit || resv_start != limit)
1227 sync_needed = B_TRUE;
1229 if (efi_debug && sync_needed)
1230 (void) fprintf(stderr, "efi_use_whole_disk: sync needed\n");
1233 * If alter_lba is 1, we are using the backup label.
1234 * Since we can locate the backup label by disk capacity,
1235 * there must be no unallocated space.
1237 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
1238 >= efi_label->efi_last_lba && !sync_needed)) {
1240 (void) fprintf(stderr,
1241 "efi_use_whole_disk: requested space not found\n");
1243 efi_free(efi_label);
1248 * Verify that we've found the reserved partition by checking
1249 * that it looks the way it did when we created it in zpool_label_disk.
1250 * If we've found the incorrect partition, then we know that this
1251 * device was reformatted and no longer is solely used by ZFS.
1253 if ((efi_label->efi_parts[resv_index].p_size != EFI_MIN_RESV_SIZE) ||
1254 (efi_label->efi_parts[resv_index].p_tag != V_RESERVED) ||
1255 (resv_index != 8)) {
1257 (void) fprintf(stderr,
1258 "efi_use_whole_disk: wholedisk not available\n");
1260 efi_free(efi_label);
1264 if (data_start + data_size != resv_start) {
1266 (void) fprintf(stderr,
1267 "efi_use_whole_disk: "
1268 "data_start (%lli) + "
1269 "data_size (%lli) != "
1270 "resv_start (%lli)\n",
1271 data_start, data_size, resv_start);
1277 if (limit < resv_start) {
1279 (void) fprintf(stderr,
1280 "efi_use_whole_disk: "
1281 "limit (%lli) < resv_start (%lli)\n",
1288 difference = limit - resv_start;
1291 (void) fprintf(stderr,
1292 "efi_use_whole_disk: difference is %lli\n", difference);
1295 * Move the reserved partition. There is currently no data in
1296 * here except fabricated devids (which get generated via
1297 * efi_write()). So there is no need to copy data.
1299 efi_label->efi_parts[data_index].p_size += difference;
1300 efi_label->efi_parts[resv_index].p_start += difference;
1301 efi_label->efi_last_u_lba = efi_label->efi_last_lba - nblocks;
1304 * Rescanning the partition table in the kernel can result
1305 * in the device links to be removed (see comment in vdev_disk_open).
1306 * If BLKPG_RESIZE_PARTITION is available, then we can resize
1307 * the partition table online and avoid having to remove the device
1308 * links used by the pool. This provides a very deterministic
1309 * approach to resizing devices and does not require any
1310 * loops waiting for devices to reappear.
1312 #ifdef BLKPG_RESIZE_PARTITION
1314 * Delete the reserved partition since we're about to expand
1315 * the data partition and it would overlap with the reserved
1317 * NOTE: The starting index for the ioctl is 1 while for the
1318 * EFI partitions it's 0. For that reason we have to add one
1319 * whenever we make an ioctl call.
1321 rval = call_blkpg_ioctl(fd, BLKPG_DEL_PARTITION, 0, 0, resv_index + 1);
1326 * Expand the data partition
1328 rval = call_blkpg_ioctl(fd, BLKPG_RESIZE_PARTITION,
1329 efi_label->efi_parts[data_index].p_start * efi_label->efi_lbasize,
1330 efi_label->efi_parts[data_index].p_size * efi_label->efi_lbasize,
1333 (void) fprintf(stderr, "Unable to resize data "
1334 "partition: %d\n", rval);
1336 * Since we failed to resize, we need to reset the start
1337 * of the reserve partition and re-create it.
1339 efi_label->efi_parts[resv_index].p_start -= difference;
1343 * Re-add the reserved partition. If we've expanded the data partition
1344 * then we'll move the reserve partition to the end of the data
1345 * partition. Otherwise, we'll recreate the partition in its original
1346 * location. Note that we do this as best-effort and ignore any
1347 * errors that may arise here. This will ensure that we finish writing
1350 (void) call_blkpg_ioctl(fd, BLKPG_ADD_PARTITION,
1351 efi_label->efi_parts[resv_index].p_start * efi_label->efi_lbasize,
1352 efi_label->efi_parts[resv_index].p_size * efi_label->efi_lbasize,
1357 * We're now ready to write the EFI label.
1360 rval = efi_write(fd, efi_label);
1361 if (rval < 0 && efi_debug) {
1362 (void) fprintf(stderr, "efi_use_whole_disk:fail "
1363 "to write label, rval=%d\n", rval);
1368 efi_free(efi_label);
1373 * write EFI label and backup label
1376 efi_write(int fd, struct dk_gpt *vtoc)
1380 efi_gpe_t *efi_parts;
1382 struct dk_cinfo dki_info;
1386 diskaddr_t lba_backup_gpt_hdr;
1388 if ((rval = efi_get_info(fd, &dki_info)) != 0)
1391 /* check if we are dealing with a metadevice */
1392 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
1393 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
1397 if (check_input(vtoc)) {
1399 * not valid; if it's a metadevice just pass it down
1400 * because SVM will do its own checking
1408 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
1409 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
1411 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
1412 vtoc->efi_lbasize) *
1417 * the number of blocks occupied by GUID partition entry array
1419 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
1422 * Backup GPT header is located on the block after GUID
1423 * partition entry array. Here, we calculate the address
1424 * for backup GPT header.
1426 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
1427 if (posix_memalign((void **)&dk_ioc.dki_data,
1428 vtoc->efi_lbasize, dk_ioc.dki_length))
1431 memset(dk_ioc.dki_data, 0, dk_ioc.dki_length);
1432 efi = dk_ioc.dki_data;
1434 /* stuff user's input into EFI struct */
1435 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1436 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
1437 efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt) - LEN_EFI_PAD);
1438 efi->efi_gpt_Reserved1 = 0;
1439 efi->efi_gpt_MyLBA = LE_64(1ULL);
1440 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
1441 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
1442 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
1443 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1444 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
1445 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
1446 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
1448 /* LINTED -- always longlong aligned */
1449 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
1451 for (i = 0; i < vtoc->efi_nparts; i++) {
1453 j < sizeof (conversion_array) /
1454 sizeof (struct uuid_to_ptag); j++) {
1456 if (vtoc->efi_parts[i].p_tag == j) {
1458 efi_parts[i].efi_gpe_PartitionTypeGUID,
1459 conversion_array[j].uuid);
1464 if (j == sizeof (conversion_array) /
1465 sizeof (struct uuid_to_ptag)) {
1467 * If we didn't have a matching uuid match, bail here.
1468 * Don't write a label with unknown uuid.
1471 (void) fprintf(stderr,
1472 "Unknown uuid for p_tag %d\n",
1473 vtoc->efi_parts[i].p_tag);
1478 /* Zero's should be written for empty partitions */
1479 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
1482 efi_parts[i].efi_gpe_StartingLBA =
1483 LE_64(vtoc->efi_parts[i].p_start);
1484 efi_parts[i].efi_gpe_EndingLBA =
1485 LE_64(vtoc->efi_parts[i].p_start +
1486 vtoc->efi_parts[i].p_size - 1);
1487 efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
1488 LE_16(vtoc->efi_parts[i].p_flag);
1489 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1490 efi_parts[i].efi_gpe_PartitionName[j] =
1491 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1493 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1494 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1495 (void) uuid_generate((uchar_t *)
1496 &vtoc->efi_parts[i].p_uguid);
1498 bcopy(&vtoc->efi_parts[i].p_uguid,
1499 &efi_parts[i].efi_gpe_UniquePartitionGUID,
1502 efi->efi_gpt_PartitionEntryArrayCRC32 =
1503 LE_32(efi_crc32((unsigned char *)efi_parts,
1504 vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1505 efi->efi_gpt_HeaderCRC32 =
1506 LE_32(efi_crc32((unsigned char *)efi,
1507 LE_32(efi->efi_gpt_HeaderSize)));
1509 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1510 free(dk_ioc.dki_data);
1520 /* if it's a metadevice we're done */
1522 free(dk_ioc.dki_data);
1526 /* write backup partition array */
1527 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
1528 dk_ioc.dki_length -= vtoc->efi_lbasize;
1530 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
1533 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1535 * we wrote the primary label okay, so don't fail
1538 (void) fprintf(stderr,
1539 "write of backup partitions to block %llu "
1540 "failed, errno %d\n",
1541 vtoc->efi_last_u_lba + 1,
1546 * now swap MyLBA and AlternateLBA fields and write backup
1547 * partition table header
1549 dk_ioc.dki_lba = lba_backup_gpt_hdr;
1550 dk_ioc.dki_length = vtoc->efi_lbasize;
1552 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1554 efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1555 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1556 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1557 efi->efi_gpt_HeaderCRC32 = 0;
1558 efi->efi_gpt_HeaderCRC32 =
1559 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1560 LE_32(efi->efi_gpt_HeaderSize)));
1562 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1564 (void) fprintf(stderr,
1565 "write of backup header to block %llu failed, "
1571 /* write the PMBR */
1572 (void) write_pmbr(fd, vtoc);
1573 free(dk_ioc.dki_data);
1579 efi_free(struct dk_gpt *ptr)
1585 * Input: File descriptor
1586 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1594 struct extvtoc extvtoc;
1596 if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1597 if (errno == ENOTSUP)
1599 else if (errno == ENOTTY) {
1600 if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1601 if (errno == ENOTSUP)
1612 efi_err_check(struct dk_gpt *vtoc)
1616 diskaddr_t istart, jstart, isize, jsize, endsect;
1620 * make sure no partitions overlap
1622 for (i = 0; i < vtoc->efi_nparts; i++) {
1623 /* It can't be unassigned and have an actual size */
1624 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1625 (vtoc->efi_parts[i].p_size != 0)) {
1626 (void) fprintf(stderr,
1627 "partition %d is \"unassigned\" but has a size "
1628 "of %llu\n", i, vtoc->efi_parts[i].p_size);
1630 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1633 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1634 if (resv_part != -1) {
1635 (void) fprintf(stderr,
1636 "found duplicate reserved partition at "
1640 if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1641 (void) fprintf(stderr,
1642 "Warning: reserved partition size must "
1643 "be %d sectors\n", EFI_MIN_RESV_SIZE);
1645 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1646 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1647 (void) fprintf(stderr,
1648 "Partition %d starts at %llu\n",
1650 vtoc->efi_parts[i].p_start);
1651 (void) fprintf(stderr,
1652 "It must be between %llu and %llu.\n",
1653 vtoc->efi_first_u_lba,
1654 vtoc->efi_last_u_lba);
1656 if ((vtoc->efi_parts[i].p_start +
1657 vtoc->efi_parts[i].p_size <
1658 vtoc->efi_first_u_lba) ||
1659 (vtoc->efi_parts[i].p_start +
1660 vtoc->efi_parts[i].p_size >
1661 vtoc->efi_last_u_lba + 1)) {
1662 (void) fprintf(stderr,
1663 "Partition %d ends at %llu\n",
1665 vtoc->efi_parts[i].p_start +
1666 vtoc->efi_parts[i].p_size);
1667 (void) fprintf(stderr,
1668 "It must be between %llu and %llu.\n",
1669 vtoc->efi_first_u_lba,
1670 vtoc->efi_last_u_lba);
1673 for (j = 0; j < vtoc->efi_nparts; j++) {
1674 isize = vtoc->efi_parts[i].p_size;
1675 jsize = vtoc->efi_parts[j].p_size;
1676 istart = vtoc->efi_parts[i].p_start;
1677 jstart = vtoc->efi_parts[j].p_start;
1678 if ((i != j) && (isize != 0) && (jsize != 0)) {
1679 endsect = jstart + jsize -1;
1680 if ((jstart <= istart) &&
1681 (istart <= endsect)) {
1683 (void) fprintf(stderr,
1684 "label error: EFI Labels do not "
1685 "support overlapping partitions\n");
1687 (void) fprintf(stderr,
1688 "Partition %d overlaps partition "
1695 /* make sure there is a reserved partition */
1696 if (resv_part == -1) {
1697 (void) fprintf(stderr,
1698 "no reserved partition found\n");
1703 * We need to get information necessary to construct a *new* efi
1707 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1713 * Now build the default partition table
1715 if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1717 (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1722 for (i = 0; i < MIN((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1723 (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1724 (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1725 (*vtoc)->efi_parts[i].p_start = 0;
1726 (*vtoc)->efi_parts[i].p_size = 0;
1729 * Make constants first
1730 * and variable partitions later
1733 /* root partition - s0 128 MB */
1734 (*vtoc)->efi_parts[0].p_start = 34;
1735 (*vtoc)->efi_parts[0].p_size = 262144;
1737 /* partition - s1 128 MB */
1738 (*vtoc)->efi_parts[1].p_start = 262178;
1739 (*vtoc)->efi_parts[1].p_size = 262144;
1741 /* partition -s2 is NOT the Backup disk */
1742 (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1744 /* partition -s6 /usr partition - HOG */
1745 (*vtoc)->efi_parts[6].p_start = 524322;
1746 (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1749 /* efi reserved partition - s9 16K */
1750 (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1751 (*vtoc)->efi_parts[8].p_size = (1024 * 16);
1752 (*vtoc)->efi_parts[8].p_tag = V_RESERVED;