4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <uuid/uuid.h>
34 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/efi_partition.h>
41 #include <sys/byteorder.h>
42 #if defined(__linux__)
46 static struct uuid_to_ptag
{
48 } conversion_array
[] = {
55 { EFI_UNUSED
}, /* STAND is never used */
59 { EFI_UNUSED
}, /* CACHE (cachefs) is never used */
76 * Default vtoc information for non-SVr4 partitions
78 struct dk_map2 default_vtoc_map
[NDKMAP
] = {
79 { V_ROOT
, 0 }, /* a - 0 */
80 { V_SWAP
, V_UNMNT
}, /* b - 1 */
81 { V_BACKUP
, V_UNMNT
}, /* c - 2 */
82 { V_UNASSIGNED
, 0 }, /* d - 3 */
83 { V_UNASSIGNED
, 0 }, /* e - 4 */
84 { V_UNASSIGNED
, 0 }, /* f - 5 */
85 { V_USR
, 0 }, /* g - 6 */
86 { V_UNASSIGNED
, 0 }, /* h - 7 */
88 #if defined(_SUNOS_VTOC_16)
90 #if defined(i386) || defined(__amd64)
91 { V_BOOT
, V_UNMNT
}, /* i - 8 */
92 { V_ALTSCTR
, 0 }, /* j - 9 */
95 #error No VTOC format defined.
96 #endif /* defined(i386) */
98 { V_UNASSIGNED
, 0 }, /* k - 10 */
99 { V_UNASSIGNED
, 0 }, /* l - 11 */
100 { V_UNASSIGNED
, 0 }, /* m - 12 */
101 { V_UNASSIGNED
, 0 }, /* n - 13 */
102 { V_UNASSIGNED
, 0 }, /* o - 14 */
103 { V_UNASSIGNED
, 0 }, /* p - 15 */
104 #endif /* defined(_SUNOS_VTOC_16) */
113 static int efi_read(int, struct dk_gpt
*);
116 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
117 * one's conditioning will be handled by crc32() internally.
120 efi_crc32(const unsigned char *buf
, unsigned int size
)
122 uint32_t crc
= crc32(0, Z_NULL
, 0);
124 crc
= crc32(crc
, buf
, size
);
130 read_disk_info(int fd
, diskaddr_t
*capacity
, uint_t
*lbsize
)
133 unsigned long long capacity_size
;
135 if (ioctl(fd
, BLKSSZGET
, §or_size
) < 0)
138 if (ioctl(fd
, BLKGETSIZE64
, &capacity_size
) < 0)
141 *lbsize
= (uint_t
)sector_size
;
142 *capacity
= (diskaddr_t
)(capacity_size
/ sector_size
);
148 efi_get_info(int fd
, struct dk_cinfo
*dki_info
)
150 #if defined(__linux__)
155 memset(dki_info
, 0, sizeof(*dki_info
));
157 path
= calloc(PATH_MAX
, 1);
162 * The simplest way to get the partition number under linux is
163 * to parse it out of the /dev/<disk><parition> block device name.
164 * The kernel creates this using the partition number when it
165 * populates /dev/ so it may be trusted. The tricky bit here is
166 * that the naming convention is based on the block device type.
167 * So we need to take this in to account when parsing out the
168 * partition information. Another issue is that the libefi API
169 * API only provides the open fd and not the file path. To handle
170 * this realpath(3) is used to resolve the block device name from
171 * /proc/self/fd/<fd>. Aside from the partition number we collect
172 * some additional device info.
174 (void) sprintf(path
, "/proc/self/fd/%d", fd
);
175 dev_path
= realpath(path
, NULL
);
178 if (dev_path
== NULL
)
181 if ((strncmp(dev_path
, "/dev/sd", 7) == 0)) {
182 strcpy(dki_info
->dki_cname
, "sd");
183 dki_info
->dki_ctype
= DKC_SCSI_CCS
;
184 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
186 &dki_info
->dki_partition
);
187 } else if ((strncmp(dev_path
, "/dev/hd", 7) == 0)) {
188 strcpy(dki_info
->dki_cname
, "hd");
189 dki_info
->dki_ctype
= DKC_DIRECT
;
190 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
192 &dki_info
->dki_partition
);
193 } else if ((strncmp(dev_path
, "/dev/md", 7) == 0)) {
194 strcpy(dki_info
->dki_cname
, "pseudo");
195 dki_info
->dki_ctype
= DKC_MD
;
196 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z0-9]p%hu",
198 &dki_info
->dki_partition
);
199 } else if ((strncmp(dev_path
, "/dev/dm-", 8) == 0)) {
200 strcpy(dki_info
->dki_cname
, "pseudo");
201 dki_info
->dki_ctype
= DKC_VBD
;
202 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z0-9-]p%hu",
204 &dki_info
->dki_partition
);
205 } else if ((strncmp(dev_path
, "/dev/ram", 8) == 0)) {
206 strcpy(dki_info
->dki_cname
, "pseudo");
207 dki_info
->dki_ctype
= DKC_PCMCIA_MEM
;
208 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z0-9]p%hu",
210 &dki_info
->dki_partition
);
211 } else if ((strncmp(dev_path
, "/dev/loop", 9) == 0)) {
212 strcpy(dki_info
->dki_cname
, "pseudo");
213 dki_info
->dki_ctype
= DKC_VBD
;
214 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z0-9]p%hu",
216 &dki_info
->dki_partition
);
218 strcpy(dki_info
->dki_dname
, "unknown");
219 strcpy(dki_info
->dki_cname
, "unknown");
220 dki_info
->dki_ctype
= DKC_UNKNOWN
;
228 dki_info
->dki_partition
= 0;
233 if (ioctl(fd
, DKIOCINFO
, (caddr_t
)dki_info
) == -1)
239 (void) fprintf(stderr
, "DKIOCINFO errno 0x%x\n", errno
);
252 * the number of blocks the EFI label takes up (round up to nearest
255 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
257 /* number of partitions -- limited by what we can malloc */
258 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
259 sizeof (struct dk_part))
262 efi_alloc_and_init(int fd
, uint32_t nparts
, struct dk_gpt
**vtoc
)
264 diskaddr_t capacity
= 0;
270 struct dk_cinfo dki_info
;
272 if (read_disk_info(fd
, &capacity
, &lbsize
) != 0)
275 #if defined(__linux__)
276 if (efi_get_info(fd
, &dki_info
) != 0)
279 if (dki_info
.dki_partition
!= 0)
282 if ((dki_info
.dki_ctype
== DKC_PCMCIA_MEM
) ||
283 (dki_info
.dki_ctype
== DKC_VBD
) ||
284 (dki_info
.dki_ctype
== DKC_UNKNOWN
))
288 nblocks
= NBLOCKS(nparts
, lbsize
);
289 if ((nblocks
* lbsize
) < EFI_MIN_ARRAY_SIZE
+ lbsize
) {
290 /* 16K plus one block for the GPT */
291 nblocks
= EFI_MIN_ARRAY_SIZE
/ lbsize
+ 1;
294 if (nparts
> MAX_PARTS
) {
296 (void) fprintf(stderr
,
297 "the maximum number of partitions supported is %lu\n",
303 length
= sizeof (struct dk_gpt
) +
304 sizeof (struct dk_part
) * (nparts
- 1);
306 if ((*vtoc
= calloc(length
, 1)) == NULL
)
311 vptr
->efi_version
= EFI_VERSION_CURRENT
;
312 vptr
->efi_lbasize
= lbsize
;
313 vptr
->efi_nparts
= nparts
;
315 * add one block here for the PMBR; on disks with a 512 byte
316 * block size and 128 or fewer partitions, efi_first_u_lba
317 * should work out to "34"
319 vptr
->efi_first_u_lba
= nblocks
+ 1;
320 vptr
->efi_last_lba
= capacity
- 1;
321 vptr
->efi_altern_lba
= capacity
-1;
322 vptr
->efi_last_u_lba
= vptr
->efi_last_lba
- nblocks
;
324 (void) uuid_generate((uchar_t
*)&uuid
);
325 UUID_LE_CONVERT(vptr
->efi_disk_uguid
, uuid
);
330 * Read EFI - return partition number upon success.
333 efi_alloc_and_read(int fd
, struct dk_gpt
**vtoc
)
339 /* figure out the number of entries that would fit into 16K */
340 nparts
= EFI_MIN_ARRAY_SIZE
/ sizeof (efi_gpe_t
);
341 length
= (int) sizeof (struct dk_gpt
) +
342 (int) sizeof (struct dk_part
) * (nparts
- 1);
343 if ((*vtoc
= calloc(length
, 1)) == NULL
)
346 (*vtoc
)->efi_nparts
= nparts
;
347 rval
= efi_read(fd
, *vtoc
);
349 if ((rval
== VT_EINVAL
) && (*vtoc
)->efi_nparts
> nparts
) {
351 length
= (int) sizeof (struct dk_gpt
) +
352 (int) sizeof (struct dk_part
) *
353 ((*vtoc
)->efi_nparts
- 1);
354 nparts
= (*vtoc
)->efi_nparts
;
355 if ((tmp
= realloc(*vtoc
, length
)) == NULL
) {
361 rval
= efi_read(fd
, *vtoc
);
367 (void) fprintf(stderr
,
368 "read of EFI table failed, rval=%d\n", rval
);
378 efi_ioctl(int fd
, int cmd
, dk_efi_t
*dk_ioc
)
380 void *data
= dk_ioc
->dki_data
;
382 #if defined(__linux__)
387 * When the IO is not being performed in kernel as an ioctl we need
388 * to know the sector size so we can seek to the proper byte offset.
390 if (read_disk_info(fd
, &capacity
, &lbsize
) == -1) {
392 fprintf(stderr
,"unable to read disk info: %d",errno
);
402 (void) fprintf(stderr
, "DKIOCGETEFI assuming "
403 "LBA %d bytes\n", DEV_BSIZE
);
408 error
= lseek(fd
, dk_ioc
->dki_lba
* lbsize
, SEEK_SET
);
411 (void) fprintf(stderr
, "DKIOCGETEFI lseek "
412 "error: %d\n", errno
);
416 error
= read(fd
, data
, dk_ioc
->dki_length
);
419 (void) fprintf(stderr
, "DKIOCGETEFI read "
420 "error: %d\n", errno
);
424 if (error
!= dk_ioc
->dki_length
) {
426 (void) fprintf(stderr
, "DKIOCGETEFI short "
427 "read of %d bytes\n", error
);
437 (void) fprintf(stderr
, "DKIOCSETEFI unknown "
443 error
= lseek(fd
, dk_ioc
->dki_lba
* lbsize
, SEEK_SET
);
446 (void) fprintf(stderr
, "DKIOCSETEFI lseek "
447 "error: %d\n", errno
);
451 error
= write(fd
, data
, dk_ioc
->dki_length
);
454 (void) fprintf(stderr
, "DKIOCSETEFI write "
455 "error: %d\n", errno
);
459 if (error
!= dk_ioc
->dki_length
) {
461 (void) fprintf(stderr
, "DKIOCSETEFI short "
462 "write of %d bytes\n", error
);
467 /* Sync the new EFI table to disk */
472 /* Ensure any local disk cache is also flushed */
473 if (ioctl(fd
, BLKFLSBUF
, 0) == -1)
481 (void) fprintf(stderr
, "unsupported ioctl()\n");
487 dk_ioc
->dki_data_64
= (uint64_t)(uintptr_t)data
;
488 error
= ioctl(fd
, cmd
, (void *)dk_ioc
);
489 dk_ioc
->dki_data
= data
;
494 #if defined(__linux__)
501 /* Notify the kernel a devices partition table has been updated */
502 while ((error
= ioctl(fd
, BLKRRPART
)) != 0) {
504 (void) fprintf(stderr
, "the kernel failed to rescan "
505 "the partition table: %d\n", errno
);
515 check_label(int fd
, dk_efi_t
*dk_ioc
)
520 if (efi_ioctl(fd
, DKIOCGETEFI
, dk_ioc
) == -1) {
528 efi
= dk_ioc
->dki_data
;
529 if (efi
->efi_gpt_Signature
!= LE_64(EFI_SIGNATURE
)) {
531 (void) fprintf(stderr
,
532 "Bad EFI signature: 0x%llx != 0x%llx\n",
533 (long long)efi
->efi_gpt_Signature
,
534 (long long)LE_64(EFI_SIGNATURE
));
539 * check CRC of the header; the size of the header should
540 * never be larger than one block
542 crc
= efi
->efi_gpt_HeaderCRC32
;
543 efi
->efi_gpt_HeaderCRC32
= 0;
545 if (((len_t
)LE_32(efi
->efi_gpt_HeaderSize
) > dk_ioc
->dki_length
) ||
546 crc
!= LE_32(efi_crc32((unsigned char *)efi
,
547 LE_32(efi
->efi_gpt_HeaderSize
)))) {
549 (void) fprintf(stderr
,
550 "Bad EFI CRC: 0x%x != 0x%x\n",
552 LE_32(efi_crc32((unsigned char *)efi
,
553 sizeof (struct efi_gpt
))));
561 efi_read(int fd
, struct dk_gpt
*vtoc
)
568 diskaddr_t capacity
= 0;
570 struct dk_minfo disk_info
;
573 efi_gpe_t
*efi_parts
;
574 struct dk_cinfo dki_info
;
575 uint32_t user_length
;
576 boolean_t legacy_label
= B_FALSE
;
579 * get the partition number for this file descriptor.
581 if ((rval
= efi_get_info(fd
, &dki_info
)) != 0)
584 if ((strncmp(dki_info
.dki_cname
, "pseudo", 7) == 0) &&
585 (strncmp(dki_info
.dki_dname
, "md", 3) == 0)) {
587 } else if ((strncmp(dki_info
.dki_cname
, "vdc", 4) == 0) &&
588 (strncmp(dki_info
.dki_dname
, "vdc", 4) == 0)) {
590 * The controller and drive name "vdc" (virtual disk client)
591 * indicates a LDoms virtual disk.
596 /* get the LBA size */
597 if (read_disk_info(fd
, &capacity
, &lbsize
) == -1) {
599 (void) fprintf(stderr
,
600 "unable to read disk info: %d",
606 disk_info
.dki_lbsize
= lbsize
;
607 disk_info
.dki_capacity
= capacity
;
609 if (disk_info
.dki_lbsize
== 0) {
611 (void) fprintf(stderr
,
612 "efi_read: assuming LBA 512 bytes\n");
614 disk_info
.dki_lbsize
= DEV_BSIZE
;
617 * Read the EFI GPT to figure out how many partitions we need
621 if (NBLOCKS(vtoc
->efi_nparts
, disk_info
.dki_lbsize
) < 34) {
622 label_len
= EFI_MIN_ARRAY_SIZE
+ disk_info
.dki_lbsize
;
624 label_len
= vtoc
->efi_nparts
* (int) sizeof (efi_gpe_t
) +
625 disk_info
.dki_lbsize
;
626 if (label_len
% disk_info
.dki_lbsize
) {
627 /* pad to physical sector size */
628 label_len
+= disk_info
.dki_lbsize
;
629 label_len
&= ~(disk_info
.dki_lbsize
- 1);
633 if (posix_memalign((void **)&dk_ioc
.dki_data
,
634 disk_info
.dki_lbsize
, label_len
))
637 memset(dk_ioc
.dki_data
, 0, label_len
);
638 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
639 user_length
= vtoc
->efi_nparts
;
640 efi
= dk_ioc
.dki_data
;
642 dk_ioc
.dki_length
= label_len
;
643 if (efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
) == -1) {
651 } else if ((rval
= check_label(fd
, &dk_ioc
)) == VT_EINVAL
) {
653 * No valid label here; try the alternate. Note that here
654 * we just read GPT header and save it into dk_ioc.data,
655 * Later, we will read GUID partition entry array if we
656 * can get valid GPT header.
660 * This is a workaround for legacy systems. In the past, the
661 * last sector of SCSI disk was invisible on x86 platform. At
662 * that time, backup label was saved on the next to the last
663 * sector. It is possible for users to move a disk from previous
664 * solaris system to present system. Here, we attempt to search
665 * legacy backup EFI label first.
667 dk_ioc
.dki_lba
= disk_info
.dki_capacity
- 2;
668 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
669 rval
= check_label(fd
, &dk_ioc
);
670 if (rval
== VT_EINVAL
) {
672 * we didn't find legacy backup EFI label, try to
673 * search backup EFI label in the last block.
675 dk_ioc
.dki_lba
= disk_info
.dki_capacity
- 1;
676 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
677 rval
= check_label(fd
, &dk_ioc
);
679 legacy_label
= B_TRUE
;
681 (void) fprintf(stderr
,
682 "efi_read: primary label corrupt; "
683 "using EFI backup label located on"
684 " the last block\n");
687 if ((efi_debug
) && (rval
== 0))
688 (void) fprintf(stderr
, "efi_read: primary label"
689 " corrupt; using legacy EFI backup label "
690 " located on the next to last block\n");
694 dk_ioc
.dki_lba
= LE_64(efi
->efi_gpt_PartitionEntryLBA
);
695 vtoc
->efi_flags
|= EFI_GPT_PRIMARY_CORRUPT
;
697 LE_32(efi
->efi_gpt_NumberOfPartitionEntries
);
699 * Partition tables are between backup GPT header
700 * table and ParitionEntryLBA (the starting LBA of
701 * the GUID partition entries array). Now that we
702 * already got valid GPT header and saved it in
703 * dk_ioc.dki_data, we try to get GUID partition
707 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
708 + disk_info
.dki_lbsize
);
710 dk_ioc
.dki_length
= disk_info
.dki_capacity
- 1 -
713 dk_ioc
.dki_length
= disk_info
.dki_capacity
- 2 -
715 dk_ioc
.dki_length
*= disk_info
.dki_lbsize
;
716 if (dk_ioc
.dki_length
>
717 ((len_t
)label_len
- sizeof (*dk_ioc
.dki_data
))) {
721 * read GUID partition entry array
723 rval
= efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
);
727 } else if (rval
== 0) {
729 dk_ioc
.dki_lba
= LE_64(efi
->efi_gpt_PartitionEntryLBA
);
731 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
732 + disk_info
.dki_lbsize
);
733 dk_ioc
.dki_length
= label_len
- disk_info
.dki_lbsize
;
734 rval
= efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
);
736 } else if (vdc_flag
&& rval
== VT_ERROR
&& errno
== EINVAL
) {
738 * When the device is a LDoms virtual disk, the DKIOCGETEFI
739 * ioctl can fail with EINVAL if the virtual disk backend
740 * is a ZFS volume serviced by a domain running an old version
741 * of Solaris. This is because the DKIOCGETEFI ioctl was
742 * initially incorrectly implemented for a ZFS volume and it
743 * expected the GPT and GPE to be retrieved with a single ioctl.
744 * So we try to read the GPT and the GPE using that old style
748 dk_ioc
.dki_length
= label_len
;
749 rval
= check_label(fd
, &dk_ioc
);
757 /* LINTED -- always longlong aligned */
758 efi_parts
= (efi_gpe_t
*)(((char *)efi
) + disk_info
.dki_lbsize
);
761 * Assemble this into a "dk_gpt" struct for easier
762 * digestibility by applications.
764 vtoc
->efi_version
= LE_32(efi
->efi_gpt_Revision
);
765 vtoc
->efi_nparts
= LE_32(efi
->efi_gpt_NumberOfPartitionEntries
);
766 vtoc
->efi_part_size
= LE_32(efi
->efi_gpt_SizeOfPartitionEntry
);
767 vtoc
->efi_lbasize
= disk_info
.dki_lbsize
;
768 vtoc
->efi_last_lba
= disk_info
.dki_capacity
- 1;
769 vtoc
->efi_first_u_lba
= LE_64(efi
->efi_gpt_FirstUsableLBA
);
770 vtoc
->efi_last_u_lba
= LE_64(efi
->efi_gpt_LastUsableLBA
);
771 vtoc
->efi_altern_lba
= LE_64(efi
->efi_gpt_AlternateLBA
);
772 UUID_LE_CONVERT(vtoc
->efi_disk_uguid
, efi
->efi_gpt_DiskGUID
);
775 * If the array the user passed in is too small, set the length
776 * to what it needs to be and return
778 if (user_length
< vtoc
->efi_nparts
) {
782 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
784 UUID_LE_CONVERT(vtoc
->efi_parts
[i
].p_guid
,
785 efi_parts
[i
].efi_gpe_PartitionTypeGUID
);
788 j
< sizeof (conversion_array
)
789 / sizeof (struct uuid_to_ptag
); j
++) {
791 if (bcmp(&vtoc
->efi_parts
[i
].p_guid
,
792 &conversion_array
[j
].uuid
,
793 sizeof (struct uuid
)) == 0) {
794 vtoc
->efi_parts
[i
].p_tag
= j
;
798 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
)
800 vtoc
->efi_parts
[i
].p_flag
=
801 LE_16(efi_parts
[i
].efi_gpe_Attributes
.PartitionAttrs
);
802 vtoc
->efi_parts
[i
].p_start
=
803 LE_64(efi_parts
[i
].efi_gpe_StartingLBA
);
804 vtoc
->efi_parts
[i
].p_size
=
805 LE_64(efi_parts
[i
].efi_gpe_EndingLBA
) -
806 vtoc
->efi_parts
[i
].p_start
+ 1;
807 for (j
= 0; j
< EFI_PART_NAME_LEN
; j
++) {
808 vtoc
->efi_parts
[i
].p_name
[j
] =
810 efi_parts
[i
].efi_gpe_PartitionName
[j
]);
813 UUID_LE_CONVERT(vtoc
->efi_parts
[i
].p_uguid
,
814 efi_parts
[i
].efi_gpe_UniquePartitionGUID
);
818 return (dki_info
.dki_partition
);
821 /* writes a "protective" MBR */
823 write_pmbr(int fd
, struct dk_gpt
*vtoc
)
828 diskaddr_t size_in_lba
;
832 len
= (vtoc
->efi_lbasize
== 0) ? sizeof (mb
) : vtoc
->efi_lbasize
;
833 if (posix_memalign((void **)&buf
, len
, len
))
837 * Preserve any boot code and disk signature if the first block is
842 dk_ioc
.dki_length
= len
;
843 /* LINTED -- always longlong aligned */
844 dk_ioc
.dki_data
= (efi_gpt_t
*)buf
;
845 if (efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
) == -1) {
846 (void *) memcpy(&mb
, buf
, sizeof (mb
));
847 bzero(&mb
, sizeof (mb
));
848 mb
.signature
= LE_16(MBB_MAGIC
);
850 (void *) memcpy(&mb
, buf
, sizeof (mb
));
851 if (mb
.signature
!= LE_16(MBB_MAGIC
)) {
852 bzero(&mb
, sizeof (mb
));
853 mb
.signature
= LE_16(MBB_MAGIC
);
857 bzero(&mb
.parts
, sizeof (mb
.parts
));
858 cp
= (uchar_t
*)&mb
.parts
[0];
859 /* bootable or not */
861 /* beginning CHS; 0xffffff if not representable */
867 /* ending CHS; 0xffffff if not representable */
871 /* starting LBA: 1 (little endian format) by EFI definition */
876 /* ending LBA: last block on the disk (little endian format) */
877 size_in_lba
= vtoc
->efi_last_lba
;
878 if (size_in_lba
< 0xffffffff) {
879 *cp
++ = (size_in_lba
& 0x000000ff);
880 *cp
++ = (size_in_lba
& 0x0000ff00) >> 8;
881 *cp
++ = (size_in_lba
& 0x00ff0000) >> 16;
882 *cp
++ = (size_in_lba
& 0xff000000) >> 24;
890 (void *) memcpy(buf
, &mb
, sizeof (mb
));
891 /* LINTED -- always longlong aligned */
892 dk_ioc
.dki_data
= (efi_gpt_t
*)buf
;
894 dk_ioc
.dki_length
= len
;
895 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
910 /* make sure the user specified something reasonable */
912 check_input(struct dk_gpt
*vtoc
)
916 diskaddr_t istart
, jstart
, isize
, jsize
, endsect
;
919 * Sanity-check the input (make sure no partitions overlap)
921 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
922 /* It can't be unassigned and have an actual size */
923 if ((vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) &&
924 (vtoc
->efi_parts
[i
].p_size
!= 0)) {
926 (void) fprintf(stderr
, "partition %d is "
927 "\"unassigned\" but has a size of %llu",
928 i
, vtoc
->efi_parts
[i
].p_size
);
932 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) {
933 if (uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_guid
))
935 /* we have encountered an unknown uuid */
936 vtoc
->efi_parts
[i
].p_tag
= 0xff;
938 if (vtoc
->efi_parts
[i
].p_tag
== V_RESERVED
) {
939 if (resv_part
!= -1) {
941 (void) fprintf(stderr
, "found "
942 "duplicate reserved partition "
949 if ((vtoc
->efi_parts
[i
].p_start
< vtoc
->efi_first_u_lba
) ||
950 (vtoc
->efi_parts
[i
].p_start
> vtoc
->efi_last_u_lba
)) {
952 (void) fprintf(stderr
,
953 "Partition %d starts at %llu. ",
955 vtoc
->efi_parts
[i
].p_start
);
956 (void) fprintf(stderr
,
957 "It must be between %llu and %llu.\n",
958 vtoc
->efi_first_u_lba
,
959 vtoc
->efi_last_u_lba
);
963 if ((vtoc
->efi_parts
[i
].p_start
+
964 vtoc
->efi_parts
[i
].p_size
<
965 vtoc
->efi_first_u_lba
) ||
966 (vtoc
->efi_parts
[i
].p_start
+
967 vtoc
->efi_parts
[i
].p_size
>
968 vtoc
->efi_last_u_lba
+ 1)) {
970 (void) fprintf(stderr
,
971 "Partition %d ends at %llu. ",
973 vtoc
->efi_parts
[i
].p_start
+
974 vtoc
->efi_parts
[i
].p_size
);
975 (void) fprintf(stderr
,
976 "It must be between %llu and %llu.\n",
977 vtoc
->efi_first_u_lba
,
978 vtoc
->efi_last_u_lba
);
983 for (j
= 0; j
< vtoc
->efi_nparts
; j
++) {
984 isize
= vtoc
->efi_parts
[i
].p_size
;
985 jsize
= vtoc
->efi_parts
[j
].p_size
;
986 istart
= vtoc
->efi_parts
[i
].p_start
;
987 jstart
= vtoc
->efi_parts
[j
].p_start
;
988 if ((i
!= j
) && (isize
!= 0) && (jsize
!= 0)) {
989 endsect
= jstart
+ jsize
-1;
990 if ((jstart
<= istart
) &&
991 (istart
<= endsect
)) {
993 (void) fprintf(stderr
,
994 "Partition %d overlaps "
995 "partition %d.", i
, j
);
1002 /* just a warning for now */
1003 if ((resv_part
== -1) && efi_debug
) {
1004 (void) fprintf(stderr
,
1005 "no reserved partition found\n");
1011 * add all the unallocated space to the current label
1014 efi_use_whole_disk(int fd
)
1016 struct dk_gpt
*efi_label
;
1019 uint_t phy_last_slice
= 0;
1020 diskaddr_t pl_start
= 0;
1023 rval
= efi_alloc_and_read(fd
, &efi_label
);
1028 /* find the last physically non-zero partition */
1029 for (i
= 0; i
< efi_label
->efi_nparts
- 2; i
++) {
1030 if (pl_start
< efi_label
->efi_parts
[i
].p_start
) {
1031 pl_start
= efi_label
->efi_parts
[i
].p_start
;
1035 pl_size
= efi_label
->efi_parts
[phy_last_slice
].p_size
;
1038 * If alter_lba is 1, we are using the backup label.
1039 * Since we can locate the backup label by disk capacity,
1040 * there must be no unallocated space.
1042 if ((efi_label
->efi_altern_lba
== 1) || (efi_label
->efi_altern_lba
1043 >= efi_label
->efi_last_lba
)) {
1045 (void) fprintf(stderr
,
1046 "efi_use_whole_disk: requested space not found\n");
1048 efi_free(efi_label
);
1053 * If there is space between the last physically non-zero partition
1054 * and the reserved partition, just add the unallocated space to this
1055 * area. Otherwise, the unallocated space is added to the last
1056 * physically non-zero partition.
1058 if (pl_start
+ pl_size
- 1 == efi_label
->efi_last_u_lba
-
1059 EFI_MIN_RESV_SIZE
) {
1060 efi_label
->efi_parts
[phy_last_slice
].p_size
+=
1061 efi_label
->efi_last_lba
- efi_label
->efi_altern_lba
;
1065 * Move the reserved partition. There is currently no data in
1066 * here except fabricated devids (which get generated via
1067 * efi_write()). So there is no need to copy data.
1069 efi_label
->efi_parts
[efi_label
->efi_nparts
- 1].p_start
+=
1070 efi_label
->efi_last_lba
- efi_label
->efi_altern_lba
;
1071 efi_label
->efi_last_u_lba
+= efi_label
->efi_last_lba
1072 - efi_label
->efi_altern_lba
;
1074 rval
= efi_write(fd
, efi_label
);
1077 (void) fprintf(stderr
,
1078 "efi_use_whole_disk:fail to write label, rval=%d\n",
1081 efi_free(efi_label
);
1085 efi_free(efi_label
);
1091 * write EFI label and backup label
1094 efi_write(int fd
, struct dk_gpt
*vtoc
)
1098 efi_gpe_t
*efi_parts
;
1100 struct dk_cinfo dki_info
;
1104 diskaddr_t lba_backup_gpt_hdr
;
1106 if ((rval
= efi_get_info(fd
, &dki_info
)) != 0)
1109 /* check if we are dealing wih a metadevice */
1110 if ((strncmp(dki_info
.dki_cname
, "pseudo", 7) == 0) &&
1111 (strncmp(dki_info
.dki_dname
, "md", 3) == 0)) {
1115 if (check_input(vtoc
)) {
1117 * not valid; if it's a metadevice just pass it down
1118 * because SVM will do its own checking
1126 if (NBLOCKS(vtoc
->efi_nparts
, vtoc
->efi_lbasize
) < 34) {
1127 dk_ioc
.dki_length
= EFI_MIN_ARRAY_SIZE
+ vtoc
->efi_lbasize
;
1129 dk_ioc
.dki_length
= NBLOCKS(vtoc
->efi_nparts
,
1130 vtoc
->efi_lbasize
) *
1135 * the number of blocks occupied by GUID partition entry array
1137 nblocks
= dk_ioc
.dki_length
/ vtoc
->efi_lbasize
- 1;
1140 * Backup GPT header is located on the block after GUID
1141 * partition entry array. Here, we calculate the address
1142 * for backup GPT header.
1144 lba_backup_gpt_hdr
= vtoc
->efi_last_u_lba
+ 1 + nblocks
;
1145 if (posix_memalign((void **)&dk_ioc
.dki_data
,
1146 vtoc
->efi_lbasize
, dk_ioc
.dki_length
))
1149 memset(dk_ioc
.dki_data
, 0, dk_ioc
.dki_length
);
1150 efi
= dk_ioc
.dki_data
;
1152 /* stuff user's input into EFI struct */
1153 efi
->efi_gpt_Signature
= LE_64(EFI_SIGNATURE
);
1154 efi
->efi_gpt_Revision
= LE_32(vtoc
->efi_version
); /* 0x02000100 */
1155 efi
->efi_gpt_HeaderSize
= LE_32(sizeof (struct efi_gpt
));
1156 efi
->efi_gpt_Reserved1
= 0;
1157 efi
->efi_gpt_MyLBA
= LE_64(1ULL);
1158 efi
->efi_gpt_AlternateLBA
= LE_64(lba_backup_gpt_hdr
);
1159 efi
->efi_gpt_FirstUsableLBA
= LE_64(vtoc
->efi_first_u_lba
);
1160 efi
->efi_gpt_LastUsableLBA
= LE_64(vtoc
->efi_last_u_lba
);
1161 efi
->efi_gpt_PartitionEntryLBA
= LE_64(2ULL);
1162 efi
->efi_gpt_NumberOfPartitionEntries
= LE_32(vtoc
->efi_nparts
);
1163 efi
->efi_gpt_SizeOfPartitionEntry
= LE_32(sizeof (struct efi_gpe
));
1164 UUID_LE_CONVERT(efi
->efi_gpt_DiskGUID
, vtoc
->efi_disk_uguid
);
1166 /* LINTED -- always longlong aligned */
1167 efi_parts
= (efi_gpe_t
*)((char *)dk_ioc
.dki_data
+ vtoc
->efi_lbasize
);
1169 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1171 j
< sizeof (conversion_array
) /
1172 sizeof (struct uuid_to_ptag
); j
++) {
1174 if (vtoc
->efi_parts
[i
].p_tag
== j
) {
1176 efi_parts
[i
].efi_gpe_PartitionTypeGUID
,
1177 conversion_array
[j
].uuid
);
1182 if (j
== sizeof (conversion_array
) /
1183 sizeof (struct uuid_to_ptag
)) {
1185 * If we didn't have a matching uuid match, bail here.
1186 * Don't write a label with unknown uuid.
1189 (void) fprintf(stderr
,
1190 "Unknown uuid for p_tag %d\n",
1191 vtoc
->efi_parts
[i
].p_tag
);
1196 /* Zero's should be written for empty partitions */
1197 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
)
1200 efi_parts
[i
].efi_gpe_StartingLBA
=
1201 LE_64(vtoc
->efi_parts
[i
].p_start
);
1202 efi_parts
[i
].efi_gpe_EndingLBA
=
1203 LE_64(vtoc
->efi_parts
[i
].p_start
+
1204 vtoc
->efi_parts
[i
].p_size
- 1);
1205 efi_parts
[i
].efi_gpe_Attributes
.PartitionAttrs
=
1206 LE_16(vtoc
->efi_parts
[i
].p_flag
);
1207 for (j
= 0; j
< EFI_PART_NAME_LEN
; j
++) {
1208 efi_parts
[i
].efi_gpe_PartitionName
[j
] =
1209 LE_16((ushort_t
)vtoc
->efi_parts
[i
].p_name
[j
]);
1211 if ((vtoc
->efi_parts
[i
].p_tag
!= V_UNASSIGNED
) &&
1212 uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_uguid
)) {
1213 (void) uuid_generate((uchar_t
*)
1214 &vtoc
->efi_parts
[i
].p_uguid
);
1216 bcopy(&vtoc
->efi_parts
[i
].p_uguid
,
1217 &efi_parts
[i
].efi_gpe_UniquePartitionGUID
,
1220 efi
->efi_gpt_PartitionEntryArrayCRC32
=
1221 LE_32(efi_crc32((unsigned char *)efi_parts
,
1222 vtoc
->efi_nparts
* (int)sizeof (struct efi_gpe
)));
1223 efi
->efi_gpt_HeaderCRC32
=
1224 LE_32(efi_crc32((unsigned char *)efi
, sizeof (struct efi_gpt
)));
1226 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1227 free(dk_ioc
.dki_data
);
1237 /* if it's a metadevice we're done */
1239 free(dk_ioc
.dki_data
);
1243 /* write backup partition array */
1244 dk_ioc
.dki_lba
= vtoc
->efi_last_u_lba
+ 1;
1245 dk_ioc
.dki_length
-= vtoc
->efi_lbasize
;
1247 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
+
1250 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1252 * we wrote the primary label okay, so don't fail
1255 (void) fprintf(stderr
,
1256 "write of backup partitions to block %llu "
1257 "failed, errno %d\n",
1258 vtoc
->efi_last_u_lba
+ 1,
1263 * now swap MyLBA and AlternateLBA fields and write backup
1264 * partition table header
1266 dk_ioc
.dki_lba
= lba_backup_gpt_hdr
;
1267 dk_ioc
.dki_length
= vtoc
->efi_lbasize
;
1269 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
-
1271 efi
->efi_gpt_AlternateLBA
= LE_64(1ULL);
1272 efi
->efi_gpt_MyLBA
= LE_64(lba_backup_gpt_hdr
);
1273 efi
->efi_gpt_PartitionEntryLBA
= LE_64(vtoc
->efi_last_u_lba
+ 1);
1274 efi
->efi_gpt_HeaderCRC32
= 0;
1275 efi
->efi_gpt_HeaderCRC32
=
1276 LE_32(efi_crc32((unsigned char *)dk_ioc
.dki_data
,
1277 sizeof (struct efi_gpt
)));
1279 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1281 (void) fprintf(stderr
,
1282 "write of backup header to block %llu failed, "
1288 /* write the PMBR */
1289 (void) write_pmbr(fd
, vtoc
);
1290 free(dk_ioc
.dki_data
);
1292 #if defined(__linux__)
1293 rval
= efi_rescan(fd
);
1302 efi_free(struct dk_gpt
*ptr
)
1308 * Input: File descriptor
1309 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1317 struct extvtoc extvtoc
;
1319 if (ioctl(fd
, DKIOCGEXTVTOC
, &extvtoc
) == -1) {
1320 if (errno
== ENOTSUP
)
1322 else if (errno
== ENOTTY
) {
1323 if (ioctl(fd
, DKIOCGVTOC
, &vtoc
) == -1)
1324 if (errno
== ENOTSUP
)
1335 efi_err_check(struct dk_gpt
*vtoc
)
1339 diskaddr_t istart
, jstart
, isize
, jsize
, endsect
;
1343 * make sure no partitions overlap
1345 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1346 /* It can't be unassigned and have an actual size */
1347 if ((vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) &&
1348 (vtoc
->efi_parts
[i
].p_size
!= 0)) {
1349 (void) fprintf(stderr
,
1350 "partition %d is \"unassigned\" but has a size "
1351 "of %llu\n", i
, vtoc
->efi_parts
[i
].p_size
);
1353 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) {
1356 if (vtoc
->efi_parts
[i
].p_tag
== V_RESERVED
) {
1357 if (resv_part
!= -1) {
1358 (void) fprintf(stderr
,
1359 "found duplicate reserved partition at "
1363 if (vtoc
->efi_parts
[i
].p_size
!= EFI_MIN_RESV_SIZE
)
1364 (void) fprintf(stderr
,
1365 "Warning: reserved partition size must "
1366 "be %d sectors\n", EFI_MIN_RESV_SIZE
);
1368 if ((vtoc
->efi_parts
[i
].p_start
< vtoc
->efi_first_u_lba
) ||
1369 (vtoc
->efi_parts
[i
].p_start
> vtoc
->efi_last_u_lba
)) {
1370 (void) fprintf(stderr
,
1371 "Partition %d starts at %llu\n",
1373 vtoc
->efi_parts
[i
].p_start
);
1374 (void) fprintf(stderr
,
1375 "It must be between %llu and %llu.\n",
1376 vtoc
->efi_first_u_lba
,
1377 vtoc
->efi_last_u_lba
);
1379 if ((vtoc
->efi_parts
[i
].p_start
+
1380 vtoc
->efi_parts
[i
].p_size
<
1381 vtoc
->efi_first_u_lba
) ||
1382 (vtoc
->efi_parts
[i
].p_start
+
1383 vtoc
->efi_parts
[i
].p_size
>
1384 vtoc
->efi_last_u_lba
+ 1)) {
1385 (void) fprintf(stderr
,
1386 "Partition %d ends at %llu\n",
1388 vtoc
->efi_parts
[i
].p_start
+
1389 vtoc
->efi_parts
[i
].p_size
);
1390 (void) fprintf(stderr
,
1391 "It must be between %llu and %llu.\n",
1392 vtoc
->efi_first_u_lba
,
1393 vtoc
->efi_last_u_lba
);
1396 for (j
= 0; j
< vtoc
->efi_nparts
; j
++) {
1397 isize
= vtoc
->efi_parts
[i
].p_size
;
1398 jsize
= vtoc
->efi_parts
[j
].p_size
;
1399 istart
= vtoc
->efi_parts
[i
].p_start
;
1400 jstart
= vtoc
->efi_parts
[j
].p_start
;
1401 if ((i
!= j
) && (isize
!= 0) && (jsize
!= 0)) {
1402 endsect
= jstart
+ jsize
-1;
1403 if ((jstart
<= istart
) &&
1404 (istart
<= endsect
)) {
1406 (void) fprintf(stderr
,
1407 "label error: EFI Labels do not "
1408 "support overlapping partitions\n");
1410 (void) fprintf(stderr
,
1411 "Partition %d overlaps partition "
1418 /* make sure there is a reserved partition */
1419 if (resv_part
== -1) {
1420 (void) fprintf(stderr
,
1421 "no reserved partition found\n");
1426 * We need to get information necessary to construct a *new* efi
1430 efi_auto_sense(int fd
, struct dk_gpt
**vtoc
)
1436 * Now build the default partition table
1438 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, vtoc
) != 0) {
1440 (void) fprintf(stderr
, "efi_alloc_and_init failed.\n");
1445 for (i
= 0; i
< MIN((*vtoc
)->efi_nparts
, V_NUMPAR
); i
++) {
1446 (*vtoc
)->efi_parts
[i
].p_tag
= default_vtoc_map
[i
].p_tag
;
1447 (*vtoc
)->efi_parts
[i
].p_flag
= default_vtoc_map
[i
].p_flag
;
1448 (*vtoc
)->efi_parts
[i
].p_start
= 0;
1449 (*vtoc
)->efi_parts
[i
].p_size
= 0;
1452 * Make constants first
1453 * and variable partitions later
1456 /* root partition - s0 128 MB */
1457 (*vtoc
)->efi_parts
[0].p_start
= 34;
1458 (*vtoc
)->efi_parts
[0].p_size
= 262144;
1460 /* partition - s1 128 MB */
1461 (*vtoc
)->efi_parts
[1].p_start
= 262178;
1462 (*vtoc
)->efi_parts
[1].p_size
= 262144;
1464 /* partition -s2 is NOT the Backup disk */
1465 (*vtoc
)->efi_parts
[2].p_tag
= V_UNASSIGNED
;
1467 /* partition -s6 /usr partition - HOG */
1468 (*vtoc
)->efi_parts
[6].p_start
= 524322;
1469 (*vtoc
)->efi_parts
[6].p_size
= (*vtoc
)->efi_last_u_lba
- 524322
1472 /* efi reserved partition - s9 16K */
1473 (*vtoc
)->efi_parts
[8].p_start
= (*vtoc
)->efi_last_u_lba
- (1024 * 16);
1474 (*vtoc
)->efi_parts
[8].p_size
= (1024 * 16);
1475 (*vtoc
)->efi_parts
[8].p_tag
= V_RESERVED
;