4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <uuid/uuid.h>
33 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/dktp/fdisk.h>
39 #include <sys/efi_partition.h>
40 #include <sys/byteorder.h>
43 static struct uuid_to_ptag {
45 } conversion_array[] = {
52 { 0 }, /* STAND is never used */
56 { 0 }, /* CACHE (cachefs) is never used */
73 * Default vtoc information for non-SVr4 partitions
75 struct dk_map2 default_vtoc_map[NDKMAP] = {
76 { V_ROOT, 0 }, /* a - 0 */
77 { V_SWAP, V_UNMNT }, /* b - 1 */
78 { V_BACKUP, V_UNMNT }, /* c - 2 */
79 { V_UNASSIGNED, 0 }, /* d - 3 */
80 { V_UNASSIGNED, 0 }, /* e - 4 */
81 { V_UNASSIGNED, 0 }, /* f - 5 */
82 { V_USR, 0 }, /* g - 6 */
83 { V_UNASSIGNED, 0 }, /* h - 7 */
85 #if defined(_SUNOS_VTOC_16)
87 #if defined(i386) || defined(__amd64)
88 { V_BOOT, V_UNMNT }, /* i - 8 */
89 { V_ALTSCTR, 0 }, /* j - 9 */
92 #error No VTOC format defined.
93 #endif /* defined(i386) */
95 { V_UNASSIGNED, 0 }, /* k - 10 */
96 { V_UNASSIGNED, 0 }, /* l - 11 */
97 { V_UNASSIGNED, 0 }, /* m - 12 */
98 { V_UNASSIGNED, 0 }, /* n - 13 */
99 { V_UNASSIGNED, 0 }, /* o - 14 */
100 { V_UNASSIGNED, 0 }, /* p - 15 */
101 #endif /* defined(_SUNOS_VTOC_16) */
110 extern unsigned int efi_crc32(const unsigned char *, unsigned int);
111 static int efi_read(int, struct dk_gpt *);
114 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
116 struct dk_minfo disk_info;
118 if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
120 *capacity = disk_info.dki_capacity;
121 *lbsize = disk_info.dki_lbsize;
126 * the number of blocks the EFI label takes up (round up to nearest
129 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
131 /* number of partitions -- limited by what we can malloc */
132 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
133 sizeof (struct dk_part))
136 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
145 if (read_disk_info(fd, &capacity, &lbsize) != 0) {
147 (void) fprintf(stderr,
148 "couldn't read disk information\n");
152 nblocks = NBLOCKS(nparts, lbsize);
153 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
154 /* 16K plus one block for the GPT */
155 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
158 if (nparts > MAX_PARTS) {
160 (void) fprintf(stderr,
161 "the maximum number of partitions supported is %lu\n",
167 length = sizeof (struct dk_gpt) +
168 sizeof (struct dk_part) * (nparts - 1);
170 if ((*vtoc = calloc(length, 1)) == NULL)
175 vptr->efi_version = EFI_VERSION_CURRENT;
176 vptr->efi_lbasize = lbsize;
177 vptr->efi_nparts = nparts;
179 * add one block here for the PMBR; on disks with a 512 byte
180 * block size and 128 or fewer partitions, efi_first_u_lba
181 * should work out to "34"
183 vptr->efi_first_u_lba = nblocks + 1;
184 vptr->efi_last_lba = capacity - 1;
185 vptr->efi_altern_lba = capacity -1;
186 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
188 (void) uuid_generate((uchar_t *)&uuid);
189 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
194 * Read EFI - return partition number upon success.
197 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
203 /* figure out the number of entries that would fit into 16K */
204 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
205 length = (int) sizeof (struct dk_gpt) +
206 (int) sizeof (struct dk_part) * (nparts - 1);
207 if ((*vtoc = calloc(length, 1)) == NULL)
210 (*vtoc)->efi_nparts = nparts;
211 rval = efi_read(fd, *vtoc);
213 if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
215 length = (int) sizeof (struct dk_gpt) +
216 (int) sizeof (struct dk_part) *
217 ((*vtoc)->efi_nparts - 1);
218 nparts = (*vtoc)->efi_nparts;
219 if ((tmp = realloc(*vtoc, length)) == NULL) {
225 rval = efi_read(fd, *vtoc);
231 (void) fprintf(stderr,
232 "read of EFI table failed, rval=%d\n", rval);
242 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
244 void *data = dk_ioc->dki_data;
247 dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
248 error = ioctl(fd, cmd, (void *)dk_ioc);
249 dk_ioc->dki_data = data;
255 check_label(int fd, dk_efi_t *dk_ioc)
260 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
268 efi = dk_ioc->dki_data;
269 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
271 (void) fprintf(stderr,
272 "Bad EFI signature: 0x%llx != 0x%llx\n",
273 (long long)efi->efi_gpt_Signature,
274 (long long)LE_64(EFI_SIGNATURE));
279 * check CRC of the header; the size of the header should
280 * never be larger than one block
282 crc = efi->efi_gpt_HeaderCRC32;
283 efi->efi_gpt_HeaderCRC32 = 0;
285 if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
286 crc != LE_32(efi_crc32((unsigned char *)efi,
287 LE_32(efi->efi_gpt_HeaderSize)))) {
289 (void) fprintf(stderr,
290 "Bad EFI CRC: 0x%x != 0x%x\n",
292 LE_32(efi_crc32((unsigned char *)efi,
293 sizeof (struct efi_gpt))));
301 efi_read(int fd, struct dk_gpt *vtoc)
308 struct dk_minfo disk_info;
311 efi_gpe_t *efi_parts;
312 struct dk_cinfo dki_info;
313 uint32_t user_length;
314 boolean_t legacy_label = B_FALSE;
317 * get the partition number for this file descriptor.
319 if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
321 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
332 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
333 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
335 } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
336 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
338 * The controller and drive name "vdc" (virtual disk client)
339 * indicates a LDoms virtual disk.
344 /* get the LBA size */
345 if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
347 (void) fprintf(stderr,
348 "assuming LBA 512 bytes %d\n",
351 disk_info.dki_lbsize = DEV_BSIZE;
353 if (disk_info.dki_lbsize == 0) {
355 (void) fprintf(stderr,
356 "efi_read: assuming LBA 512 bytes\n");
358 disk_info.dki_lbsize = DEV_BSIZE;
361 * Read the EFI GPT to figure out how many partitions we need
365 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
366 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
368 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
369 disk_info.dki_lbsize;
370 if (label_len % disk_info.dki_lbsize) {
371 /* pad to physical sector size */
372 label_len += disk_info.dki_lbsize;
373 label_len &= ~(disk_info.dki_lbsize - 1);
377 if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
380 dk_ioc.dki_length = disk_info.dki_lbsize;
381 user_length = vtoc->efi_nparts;
382 efi = dk_ioc.dki_data;
384 dk_ioc.dki_length = label_len;
385 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
393 } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
395 * No valid label here; try the alternate. Note that here
396 * we just read GPT header and save it into dk_ioc.data,
397 * Later, we will read GUID partition entry array if we
398 * can get valid GPT header.
402 * This is a workaround for legacy systems. In the past, the
403 * last sector of SCSI disk was invisible on x86 platform. At
404 * that time, backup label was saved on the next to the last
405 * sector. It is possible for users to move a disk from previous
406 * solaris system to present system. Here, we attempt to search
407 * legacy backup EFI label first.
409 dk_ioc.dki_lba = disk_info.dki_capacity - 2;
410 dk_ioc.dki_length = disk_info.dki_lbsize;
411 rval = check_label(fd, &dk_ioc);
412 if (rval == VT_EINVAL) {
414 * we didn't find legacy backup EFI label, try to
415 * search backup EFI label in the last block.
417 dk_ioc.dki_lba = disk_info.dki_capacity - 1;
418 dk_ioc.dki_length = disk_info.dki_lbsize;
419 rval = check_label(fd, &dk_ioc);
421 legacy_label = B_TRUE;
423 (void) fprintf(stderr,
424 "efi_read: primary label corrupt; "
425 "using EFI backup label located on"
426 " the last block\n");
429 if ((efi_debug) && (rval == 0))
430 (void) fprintf(stderr, "efi_read: primary label"
431 " corrupt; using legacy EFI backup label "
432 " located on the next to last block\n");
436 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
437 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
439 LE_32(efi->efi_gpt_NumberOfPartitionEntries);
441 * Partition tables are between backup GPT header
442 * table and ParitionEntryLBA (the starting LBA of
443 * the GUID partition entries array). Now that we
444 * already got valid GPT header and saved it in
445 * dk_ioc.dki_data, we try to get GUID partition
449 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
450 + disk_info.dki_lbsize);
452 dk_ioc.dki_length = disk_info.dki_capacity - 1 -
455 dk_ioc.dki_length = disk_info.dki_capacity - 2 -
457 dk_ioc.dki_length *= disk_info.dki_lbsize;
458 if (dk_ioc.dki_length >
459 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
463 * read GUID partition entry array
465 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
469 } else if (rval == 0) {
471 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
473 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
474 + disk_info.dki_lbsize);
475 dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
476 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
478 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
480 * When the device is a LDoms virtual disk, the DKIOCGETEFI
481 * ioctl can fail with EINVAL if the virtual disk backend
482 * is a ZFS volume serviced by a domain running an old version
483 * of Solaris. This is because the DKIOCGETEFI ioctl was
484 * initially incorrectly implemented for a ZFS volume and it
485 * expected the GPT and GPE to be retrieved with a single ioctl.
486 * So we try to read the GPT and the GPE using that old style
490 dk_ioc.dki_length = label_len;
491 rval = check_label(fd, &dk_ioc);
499 /* LINTED -- always longlong aligned */
500 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
503 * Assemble this into a "dk_gpt" struct for easier
504 * digestibility by applications.
506 vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
507 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
508 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
509 vtoc->efi_lbasize = disk_info.dki_lbsize;
510 vtoc->efi_last_lba = disk_info.dki_capacity - 1;
511 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
512 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
513 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
514 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
517 * If the array the user passed in is too small, set the length
518 * to what it needs to be and return
520 if (user_length < vtoc->efi_nparts) {
524 for (i = 0; i < vtoc->efi_nparts; i++) {
526 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
527 efi_parts[i].efi_gpe_PartitionTypeGUID);
530 j < sizeof (conversion_array)
531 / sizeof (struct uuid_to_ptag); j++) {
533 if (bcmp(&vtoc->efi_parts[i].p_guid,
534 &conversion_array[j].uuid,
535 sizeof (struct uuid)) == 0) {
536 vtoc->efi_parts[i].p_tag = j;
540 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
542 vtoc->efi_parts[i].p_flag =
543 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
544 vtoc->efi_parts[i].p_start =
545 LE_64(efi_parts[i].efi_gpe_StartingLBA);
546 vtoc->efi_parts[i].p_size =
547 LE_64(efi_parts[i].efi_gpe_EndingLBA) -
548 vtoc->efi_parts[i].p_start + 1;
549 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
550 vtoc->efi_parts[i].p_name[j] =
552 efi_parts[i].efi_gpe_PartitionName[j]);
555 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
556 efi_parts[i].efi_gpe_UniquePartitionGUID);
560 return (dki_info.dki_partition);
563 /* writes a "protective" MBR */
565 write_pmbr(int fd, struct dk_gpt *vtoc)
570 diskaddr_t size_in_lba;
574 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
575 buf = calloc(len, 1);
578 * Preserve any boot code and disk signature if the first block is
582 dk_ioc.dki_length = len;
583 /* LINTED -- always longlong aligned */
584 dk_ioc.dki_data = (efi_gpt_t *)buf;
585 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
586 (void *) memcpy(&mb, buf, sizeof (mb));
587 bzero(&mb, sizeof (mb));
588 mb.signature = LE_16(MBB_MAGIC);
590 (void *) memcpy(&mb, buf, sizeof (mb));
591 if (mb.signature != LE_16(MBB_MAGIC)) {
592 bzero(&mb, sizeof (mb));
593 mb.signature = LE_16(MBB_MAGIC);
597 bzero(&mb.parts, sizeof (mb.parts));
598 cp = (uchar_t *)&mb.parts[0];
599 /* bootable or not */
601 /* beginning CHS; 0xffffff if not representable */
607 /* ending CHS; 0xffffff if not representable */
611 /* starting LBA: 1 (little endian format) by EFI definition */
616 /* ending LBA: last block on the disk (little endian format) */
617 size_in_lba = vtoc->efi_last_lba;
618 if (size_in_lba < 0xffffffff) {
619 *cp++ = (size_in_lba & 0x000000ff);
620 *cp++ = (size_in_lba & 0x0000ff00) >> 8;
621 *cp++ = (size_in_lba & 0x00ff0000) >> 16;
622 *cp++ = (size_in_lba & 0xff000000) >> 24;
630 (void *) memcpy(buf, &mb, sizeof (mb));
631 /* LINTED -- always longlong aligned */
632 dk_ioc.dki_data = (efi_gpt_t *)buf;
634 dk_ioc.dki_length = len;
635 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
650 /* make sure the user specified something reasonable */
652 check_input(struct dk_gpt *vtoc)
656 diskaddr_t istart, jstart, isize, jsize, endsect;
659 * Sanity-check the input (make sure no partitions overlap)
661 for (i = 0; i < vtoc->efi_nparts; i++) {
662 /* It can't be unassigned and have an actual size */
663 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
664 (vtoc->efi_parts[i].p_size != 0)) {
666 (void) fprintf(stderr,
667 "partition %d is \"unassigned\" but has a size of %llu",
669 vtoc->efi_parts[i].p_size);
673 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
674 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
676 /* we have encountered an unknown uuid */
677 vtoc->efi_parts[i].p_tag = 0xff;
679 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
680 if (resv_part != -1) {
682 (void) fprintf(stderr,
683 "found duplicate reserved partition at %d\n",
690 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
691 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
693 (void) fprintf(stderr,
694 "Partition %d starts at %llu. ",
696 vtoc->efi_parts[i].p_start);
697 (void) fprintf(stderr,
698 "It must be between %llu and %llu.\n",
699 vtoc->efi_first_u_lba,
700 vtoc->efi_last_u_lba);
704 if ((vtoc->efi_parts[i].p_start +
705 vtoc->efi_parts[i].p_size <
706 vtoc->efi_first_u_lba) ||
707 (vtoc->efi_parts[i].p_start +
708 vtoc->efi_parts[i].p_size >
709 vtoc->efi_last_u_lba + 1)) {
711 (void) fprintf(stderr,
712 "Partition %d ends at %llu. ",
714 vtoc->efi_parts[i].p_start +
715 vtoc->efi_parts[i].p_size);
716 (void) fprintf(stderr,
717 "It must be between %llu and %llu.\n",
718 vtoc->efi_first_u_lba,
719 vtoc->efi_last_u_lba);
724 for (j = 0; j < vtoc->efi_nparts; j++) {
725 isize = vtoc->efi_parts[i].p_size;
726 jsize = vtoc->efi_parts[j].p_size;
727 istart = vtoc->efi_parts[i].p_start;
728 jstart = vtoc->efi_parts[j].p_start;
729 if ((i != j) && (isize != 0) && (jsize != 0)) {
730 endsect = jstart + jsize -1;
731 if ((jstart <= istart) &&
732 (istart <= endsect)) {
734 (void) fprintf(stderr,
735 "Partition %d overlaps partition %d.",
743 /* just a warning for now */
744 if ((resv_part == -1) && efi_debug) {
745 (void) fprintf(stderr,
746 "no reserved partition found\n");
752 * add all the unallocated space to the current label
755 efi_use_whole_disk(int fd)
757 struct dk_gpt *efi_label;
760 uint_t phy_last_slice = 0;
761 diskaddr_t pl_start = 0;
764 rval = efi_alloc_and_read(fd, &efi_label);
769 /* find the last physically non-zero partition */
770 for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
771 if (pl_start < efi_label->efi_parts[i].p_start) {
772 pl_start = efi_label->efi_parts[i].p_start;
776 pl_size = efi_label->efi_parts[phy_last_slice].p_size;
779 * If alter_lba is 1, we are using the backup label.
780 * Since we can locate the backup label by disk capacity,
781 * there must be no unallocated space.
783 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
784 >= efi_label->efi_last_lba)) {
786 (void) fprintf(stderr,
787 "efi_use_whole_disk: requested space not found\n");
794 * If there is space between the last physically non-zero partition
795 * and the reserved partition, just add the unallocated space to this
796 * area. Otherwise, the unallocated space is added to the last
797 * physically non-zero partition.
799 if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
801 efi_label->efi_parts[phy_last_slice].p_size +=
802 efi_label->efi_last_lba - efi_label->efi_altern_lba;
806 * Move the reserved partition. There is currently no data in
807 * here except fabricated devids (which get generated via
808 * efi_write()). So there is no need to copy data.
810 efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
811 efi_label->efi_last_lba - efi_label->efi_altern_lba;
812 efi_label->efi_last_u_lba += efi_label->efi_last_lba
813 - efi_label->efi_altern_lba;
815 rval = efi_write(fd, efi_label);
818 (void) fprintf(stderr,
819 "efi_use_whole_disk:fail to write label, rval=%d\n",
832 * write EFI label and backup label
835 efi_write(int fd, struct dk_gpt *vtoc)
839 efi_gpe_t *efi_parts;
841 struct dk_cinfo dki_info;
844 diskaddr_t lba_backup_gpt_hdr;
846 if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
848 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
859 /* check if we are dealing wih a metadevice */
860 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
861 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
865 if (check_input(vtoc)) {
867 * not valid; if it's a metadevice just pass it down
868 * because SVM will do its own checking
876 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
877 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
879 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
885 * the number of blocks occupied by GUID partition entry array
887 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
890 * Backup GPT header is located on the block after GUID
891 * partition entry array. Here, we calculate the address
892 * for backup GPT header.
894 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
895 if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
898 efi = dk_ioc.dki_data;
900 /* stuff user's input into EFI struct */
901 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
902 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
903 efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
904 efi->efi_gpt_Reserved1 = 0;
905 efi->efi_gpt_MyLBA = LE_64(1ULL);
906 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
907 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
908 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
909 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
910 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
911 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
912 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
914 /* LINTED -- always longlong aligned */
915 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
917 for (i = 0; i < vtoc->efi_nparts; i++) {
919 j < sizeof (conversion_array) /
920 sizeof (struct uuid_to_ptag); j++) {
922 if (vtoc->efi_parts[i].p_tag == j) {
924 efi_parts[i].efi_gpe_PartitionTypeGUID,
925 conversion_array[j].uuid);
930 if (j == sizeof (conversion_array) /
931 sizeof (struct uuid_to_ptag)) {
933 * If we didn't have a matching uuid match, bail here.
934 * Don't write a label with unknown uuid.
937 (void) fprintf(stderr,
938 "Unknown uuid for p_tag %d\n",
939 vtoc->efi_parts[i].p_tag);
944 efi_parts[i].efi_gpe_StartingLBA =
945 LE_64(vtoc->efi_parts[i].p_start);
946 efi_parts[i].efi_gpe_EndingLBA =
947 LE_64(vtoc->efi_parts[i].p_start +
948 vtoc->efi_parts[i].p_size - 1);
949 efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
950 LE_16(vtoc->efi_parts[i].p_flag);
951 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
952 efi_parts[i].efi_gpe_PartitionName[j] =
953 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
955 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
956 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
957 (void) uuid_generate((uchar_t *)
958 &vtoc->efi_parts[i].p_uguid);
960 bcopy(&vtoc->efi_parts[i].p_uguid,
961 &efi_parts[i].efi_gpe_UniquePartitionGUID,
964 efi->efi_gpt_PartitionEntryArrayCRC32 =
965 LE_32(efi_crc32((unsigned char *)efi_parts,
966 vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
967 efi->efi_gpt_HeaderCRC32 =
968 LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
970 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
971 free(dk_ioc.dki_data);
981 /* if it's a metadevice we're done */
983 free(dk_ioc.dki_data);
987 /* write backup partition array */
988 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
989 dk_ioc.dki_length -= vtoc->efi_lbasize;
991 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
994 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
996 * we wrote the primary label okay, so don't fail
999 (void) fprintf(stderr,
1000 "write of backup partitions to block %llu "
1001 "failed, errno %d\n",
1002 vtoc->efi_last_u_lba + 1,
1007 * now swap MyLBA and AlternateLBA fields and write backup
1008 * partition table header
1010 dk_ioc.dki_lba = lba_backup_gpt_hdr;
1011 dk_ioc.dki_length = vtoc->efi_lbasize;
1013 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1015 efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1016 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1017 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1018 efi->efi_gpt_HeaderCRC32 = 0;
1019 efi->efi_gpt_HeaderCRC32 =
1020 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1021 sizeof (struct efi_gpt)));
1023 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1025 (void) fprintf(stderr,
1026 "write of backup header to block %llu failed, "
1032 /* write the PMBR */
1033 (void) write_pmbr(fd, vtoc);
1034 free(dk_ioc.dki_data);
1039 efi_free(struct dk_gpt *ptr)
1045 * Input: File descriptor
1046 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1053 struct extvtoc extvtoc;
1055 if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1056 if (errno == ENOTSUP)
1058 else if (errno == ENOTTY) {
1059 if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1060 if (errno == ENOTSUP)
1068 efi_err_check(struct dk_gpt *vtoc)
1072 diskaddr_t istart, jstart, isize, jsize, endsect;
1076 * make sure no partitions overlap
1078 for (i = 0; i < vtoc->efi_nparts; i++) {
1079 /* It can't be unassigned and have an actual size */
1080 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1081 (vtoc->efi_parts[i].p_size != 0)) {
1082 (void) fprintf(stderr,
1083 "partition %d is \"unassigned\" but has a size "
1084 "of %llu\n", i, vtoc->efi_parts[i].p_size);
1086 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1089 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1090 if (resv_part != -1) {
1091 (void) fprintf(stderr,
1092 "found duplicate reserved partition at "
1096 if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1097 (void) fprintf(stderr,
1098 "Warning: reserved partition size must "
1099 "be %d sectors\n", EFI_MIN_RESV_SIZE);
1101 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1102 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1103 (void) fprintf(stderr,
1104 "Partition %d starts at %llu\n",
1106 vtoc->efi_parts[i].p_start);
1107 (void) fprintf(stderr,
1108 "It must be between %llu and %llu.\n",
1109 vtoc->efi_first_u_lba,
1110 vtoc->efi_last_u_lba);
1112 if ((vtoc->efi_parts[i].p_start +
1113 vtoc->efi_parts[i].p_size <
1114 vtoc->efi_first_u_lba) ||
1115 (vtoc->efi_parts[i].p_start +
1116 vtoc->efi_parts[i].p_size >
1117 vtoc->efi_last_u_lba + 1)) {
1118 (void) fprintf(stderr,
1119 "Partition %d ends at %llu\n",
1121 vtoc->efi_parts[i].p_start +
1122 vtoc->efi_parts[i].p_size);
1123 (void) fprintf(stderr,
1124 "It must be between %llu and %llu.\n",
1125 vtoc->efi_first_u_lba,
1126 vtoc->efi_last_u_lba);
1129 for (j = 0; j < vtoc->efi_nparts; j++) {
1130 isize = vtoc->efi_parts[i].p_size;
1131 jsize = vtoc->efi_parts[j].p_size;
1132 istart = vtoc->efi_parts[i].p_start;
1133 jstart = vtoc->efi_parts[j].p_start;
1134 if ((i != j) && (isize != 0) && (jsize != 0)) {
1135 endsect = jstart + jsize -1;
1136 if ((jstart <= istart) &&
1137 (istart <= endsect)) {
1139 (void) fprintf(stderr,
1140 "label error: EFI Labels do not "
1141 "support overlapping partitions\n");
1143 (void) fprintf(stderr,
1144 "Partition %d overlaps partition "
1151 /* make sure there is a reserved partition */
1152 if (resv_part == -1) {
1153 (void) fprintf(stderr,
1154 "no reserved partition found\n");
1159 * We need to get information necessary to construct a *new* efi
1163 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1169 * Now build the default partition table
1171 if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1173 (void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1178 for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1179 (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1180 (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1181 (*vtoc)->efi_parts[i].p_start = 0;
1182 (*vtoc)->efi_parts[i].p_size = 0;
1185 * Make constants first
1186 * and variable partitions later
1189 /* root partition - s0 128 MB */
1190 (*vtoc)->efi_parts[0].p_start = 34;
1191 (*vtoc)->efi_parts[0].p_size = 262144;
1193 /* partition - s1 128 MB */
1194 (*vtoc)->efi_parts[1].p_start = 262178;
1195 (*vtoc)->efi_parts[1].p_size = 262144;
1197 /* partition -s2 is NOT the Backup disk */
1198 (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1200 /* partition -s6 /usr partition - HOG */
1201 (*vtoc)->efi_parts[6].p_start = 524322;
1202 (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1205 /* efi reserved partition - s9 16K */
1206 (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1207 (*vtoc)->efi_parts[8].p_size = (1024 * 16);
1208 (*vtoc)->efi_parts[8].p_tag = V_RESERVED;