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.
32 #include <uuid/uuid.h>
35 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/dktp/fdisk.h>
41 #include <sys/efi_partition.h>
42 #include <sys/byteorder.h>
43 #if defined(__linux__)
47 static struct uuid_to_ptag
{
49 } conversion_array
[] = {
56 { EFI_UNUSED
}, /* STAND is never used */
60 { EFI_UNUSED
}, /* CACHE (cachefs) is never used */
77 { EFI_FREEBSD_VINUM
},
87 { EFI_MSFT_STORAGESPACES
},
95 { EFI_FREEBSD_DISKLABEL
},
97 { EFI_AAPL_RAIDOFFLINE
},
100 { EFI_AAPL_TVRECOVERY
},
101 { EFI_AAPL_CORESTORAGE
},
107 { EFI_NETBSD_CRYPT
},
112 { EFI_MIDNIGHTBSD_BOOT
},
113 { EFI_MIDNIGHTBSD_DATA
},
114 { EFI_MIDNIGHTBSD_SWAP
},
115 { EFI_MIDNIGHTBSD_UFS
},
116 { EFI_MIDNIGHTBSD_VINUM
},
117 { EFI_MIDNIGHTBSD_ZFS
},
118 { EFI_CEPH_JOURNAL
},
119 { EFI_CEPH_DMCRYPTJOURNAL
},
121 { EFI_CEPH_DMCRYPTOSD
},
123 { EFI_CEPH_DMCRYPTCREATE
},
124 { EFI_OPENBSD_DISKLABEL
},
131 { EFI_RHT_ROOTAMD64
},
133 { EFI_RHT_ROOTARM64
},
134 { EFI_ACRONIS_SECUREZONE
},
137 { EFI_IBM_PPRPBOOT
},
138 { EFI_FREEDESKTOP_BOOT
}
142 * Default vtoc information for non-SVr4 partitions
144 struct dk_map2 default_vtoc_map
[NDKMAP
] = {
145 { V_ROOT
, 0 }, /* a - 0 */
146 { V_SWAP
, V_UNMNT
}, /* b - 1 */
147 { V_BACKUP
, V_UNMNT
}, /* c - 2 */
148 { V_UNASSIGNED
, 0 }, /* d - 3 */
149 { V_UNASSIGNED
, 0 }, /* e - 4 */
150 { V_UNASSIGNED
, 0 }, /* f - 5 */
151 { V_USR
, 0 }, /* g - 6 */
152 { V_UNASSIGNED
, 0 }, /* h - 7 */
154 #if defined(_SUNOS_VTOC_16)
156 #if defined(i386) || defined(__amd64) || defined(__arm) || \
157 defined(__powerpc) || defined(__sparc) || defined(__s390__) || \
159 { V_BOOT
, V_UNMNT
}, /* i - 8 */
160 { V_ALTSCTR
, 0 }, /* j - 9 */
163 #error No VTOC format defined.
164 #endif /* defined(i386) */
166 { V_UNASSIGNED
, 0 }, /* k - 10 */
167 { V_UNASSIGNED
, 0 }, /* l - 11 */
168 { V_UNASSIGNED
, 0 }, /* m - 12 */
169 { V_UNASSIGNED
, 0 }, /* n - 13 */
170 { V_UNASSIGNED
, 0 }, /* o - 14 */
171 { V_UNASSIGNED
, 0 }, /* p - 15 */
172 #endif /* defined(_SUNOS_VTOC_16) */
177 static int efi_read(int, struct dk_gpt
*);
180 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
181 * one's conditioning will be handled by crc32() internally.
184 efi_crc32(const unsigned char *buf
, unsigned int size
)
186 uint32_t crc
= crc32(0, Z_NULL
, 0);
188 crc
= crc32(crc
, buf
, size
);
194 read_disk_info(int fd
, diskaddr_t
*capacity
, uint_t
*lbsize
)
197 unsigned long long capacity_size
;
199 if (ioctl(fd
, BLKSSZGET
, §or_size
) < 0)
202 if (ioctl(fd
, BLKGETSIZE64
, &capacity_size
) < 0)
205 *lbsize
= (uint_t
)sector_size
;
206 *capacity
= (diskaddr_t
)(capacity_size
/ sector_size
);
212 efi_get_info(int fd
, struct dk_cinfo
*dki_info
)
214 #if defined(__linux__)
219 memset(dki_info
, 0, sizeof (*dki_info
));
221 path
= calloc(PATH_MAX
, 1);
226 * The simplest way to get the partition number under linux is
227 * to parse it out of the /dev/<disk><parition> block device name.
228 * The kernel creates this using the partition number when it
229 * populates /dev/ so it may be trusted. The tricky bit here is
230 * that the naming convention is based on the block device type.
231 * So we need to take this in to account when parsing out the
232 * partition information. Another issue is that the libefi API
233 * API only provides the open fd and not the file path. To handle
234 * this realpath(3) is used to resolve the block device name from
235 * /proc/self/fd/<fd>. Aside from the partition number we collect
236 * some additional device info.
238 (void) sprintf(path
, "/proc/self/fd/%d", fd
);
239 dev_path
= realpath(path
, NULL
);
242 if (dev_path
== NULL
)
245 if ((strncmp(dev_path
, "/dev/sd", 7) == 0)) {
246 strcpy(dki_info
->dki_cname
, "sd");
247 dki_info
->dki_ctype
= DKC_SCSI_CCS
;
248 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
250 &dki_info
->dki_partition
);
251 } else if ((strncmp(dev_path
, "/dev/hd", 7) == 0)) {
252 strcpy(dki_info
->dki_cname
, "hd");
253 dki_info
->dki_ctype
= DKC_DIRECT
;
254 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
256 &dki_info
->dki_partition
);
257 } else if ((strncmp(dev_path
, "/dev/md", 7) == 0)) {
258 strcpy(dki_info
->dki_cname
, "pseudo");
259 dki_info
->dki_ctype
= DKC_MD
;
260 strcpy(dki_info
->dki_dname
, "md");
261 rval
= sscanf(dev_path
, "/dev/md%[0-9]p%hu",
262 dki_info
->dki_dname
+ 2,
263 &dki_info
->dki_partition
);
264 } else if ((strncmp(dev_path
, "/dev/vd", 7) == 0)) {
265 strcpy(dki_info
->dki_cname
, "vd");
266 dki_info
->dki_ctype
= DKC_MD
;
267 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
269 &dki_info
->dki_partition
);
270 } else if ((strncmp(dev_path
, "/dev/xvd", 8) == 0)) {
271 strcpy(dki_info
->dki_cname
, "xvd");
272 dki_info
->dki_ctype
= DKC_MD
;
273 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
275 &dki_info
->dki_partition
);
276 } else if ((strncmp(dev_path
, "/dev/zd", 7) == 0)) {
277 strcpy(dki_info
->dki_cname
, "zd");
278 dki_info
->dki_ctype
= DKC_MD
;
279 rval
= sscanf(dev_path
, "/dev/%[a-zA-Z]%hu",
281 &dki_info
->dki_partition
);
282 } else if ((strncmp(dev_path
, "/dev/dm-", 8) == 0)) {
283 strcpy(dki_info
->dki_cname
, "pseudo");
284 dki_info
->dki_ctype
= DKC_VBD
;
285 strcpy(dki_info
->dki_dname
, "dm-");
286 rval
= sscanf(dev_path
, "/dev/dm-%[0-9]p%hu",
287 dki_info
->dki_dname
+ 3,
288 &dki_info
->dki_partition
);
289 } else if ((strncmp(dev_path
, "/dev/ram", 8) == 0)) {
290 strcpy(dki_info
->dki_cname
, "pseudo");
291 dki_info
->dki_ctype
= DKC_PCMCIA_MEM
;
292 strcpy(dki_info
->dki_dname
, "ram");
293 rval
= sscanf(dev_path
, "/dev/ram%[0-9]p%hu",
294 dki_info
->dki_dname
+ 3,
295 &dki_info
->dki_partition
);
296 } else if ((strncmp(dev_path
, "/dev/loop", 9) == 0)) {
297 strcpy(dki_info
->dki_cname
, "pseudo");
298 dki_info
->dki_ctype
= DKC_VBD
;
299 strcpy(dki_info
->dki_dname
, "loop");
300 rval
= sscanf(dev_path
, "/dev/loop%[0-9]p%hu",
301 dki_info
->dki_dname
+ 4,
302 &dki_info
->dki_partition
);
304 strcpy(dki_info
->dki_dname
, "unknown");
305 strcpy(dki_info
->dki_cname
, "unknown");
306 dki_info
->dki_ctype
= DKC_UNKNOWN
;
314 dki_info
->dki_partition
= 0;
319 if (ioctl(fd
, DKIOCINFO
, (caddr_t
)dki_info
) == -1)
325 (void) fprintf(stderr
, "DKIOCINFO errno 0x%x\n", errno
);
338 * the number of blocks the EFI label takes up (round up to nearest
341 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
343 /* number of partitions -- limited by what we can malloc */
344 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
345 sizeof (struct dk_part))
348 efi_alloc_and_init(int fd
, uint32_t nparts
, struct dk_gpt
**vtoc
)
350 diskaddr_t capacity
= 0;
356 struct dk_cinfo dki_info
;
358 if (read_disk_info(fd
, &capacity
, &lbsize
) != 0)
361 #if defined(__linux__)
362 if (efi_get_info(fd
, &dki_info
) != 0)
365 if (dki_info
.dki_partition
!= 0)
368 if ((dki_info
.dki_ctype
== DKC_PCMCIA_MEM
) ||
369 (dki_info
.dki_ctype
== DKC_VBD
) ||
370 (dki_info
.dki_ctype
== DKC_UNKNOWN
))
374 nblocks
= NBLOCKS(nparts
, lbsize
);
375 if ((nblocks
* lbsize
) < EFI_MIN_ARRAY_SIZE
+ lbsize
) {
376 /* 16K plus one block for the GPT */
377 nblocks
= EFI_MIN_ARRAY_SIZE
/ lbsize
+ 1;
380 if (nparts
> MAX_PARTS
) {
382 (void) fprintf(stderr
,
383 "the maximum number of partitions supported is %lu\n",
389 length
= sizeof (struct dk_gpt
) +
390 sizeof (struct dk_part
) * (nparts
- 1);
392 if ((*vtoc
= calloc(length
, 1)) == NULL
)
397 vptr
->efi_version
= EFI_VERSION_CURRENT
;
398 vptr
->efi_lbasize
= lbsize
;
399 vptr
->efi_nparts
= nparts
;
401 * add one block here for the PMBR; on disks with a 512 byte
402 * block size and 128 or fewer partitions, efi_first_u_lba
403 * should work out to "34"
405 vptr
->efi_first_u_lba
= nblocks
+ 1;
406 vptr
->efi_last_lba
= capacity
- 1;
407 vptr
->efi_altern_lba
= capacity
-1;
408 vptr
->efi_last_u_lba
= vptr
->efi_last_lba
- nblocks
;
410 (void) uuid_generate((uchar_t
*)&uuid
);
411 UUID_LE_CONVERT(vptr
->efi_disk_uguid
, uuid
);
416 * Read EFI - return partition number upon success.
419 efi_alloc_and_read(int fd
, struct dk_gpt
**vtoc
)
425 /* figure out the number of entries that would fit into 16K */
426 nparts
= EFI_MIN_ARRAY_SIZE
/ sizeof (efi_gpe_t
);
427 length
= (int) sizeof (struct dk_gpt
) +
428 (int) sizeof (struct dk_part
) * (nparts
- 1);
429 if ((*vtoc
= calloc(length
, 1)) == NULL
)
432 (*vtoc
)->efi_nparts
= nparts
;
433 rval
= efi_read(fd
, *vtoc
);
435 if ((rval
== VT_EINVAL
) && (*vtoc
)->efi_nparts
> nparts
) {
437 length
= (int) sizeof (struct dk_gpt
) +
438 (int) sizeof (struct dk_part
) *
439 ((*vtoc
)->efi_nparts
- 1);
440 nparts
= (*vtoc
)->efi_nparts
;
441 if ((tmp
= realloc(*vtoc
, length
)) == NULL
) {
447 rval
= efi_read(fd
, *vtoc
);
453 (void) fprintf(stderr
,
454 "read of EFI table failed, rval=%d\n", rval
);
464 efi_ioctl(int fd
, int cmd
, dk_efi_t
*dk_ioc
)
466 void *data
= dk_ioc
->dki_data
;
468 #if defined(__linux__)
473 * When the IO is not being performed in kernel as an ioctl we need
474 * to know the sector size so we can seek to the proper byte offset.
476 if (read_disk_info(fd
, &capacity
, &lbsize
) == -1) {
478 fprintf(stderr
, "unable to read disk info: %d", errno
);
488 (void) fprintf(stderr
, "DKIOCGETEFI assuming "
489 "LBA %d bytes\n", DEV_BSIZE
);
494 error
= lseek(fd
, dk_ioc
->dki_lba
* lbsize
, SEEK_SET
);
497 (void) fprintf(stderr
, "DKIOCGETEFI lseek "
498 "error: %d\n", errno
);
502 error
= read(fd
, data
, dk_ioc
->dki_length
);
505 (void) fprintf(stderr
, "DKIOCGETEFI read "
506 "error: %d\n", errno
);
510 if (error
!= dk_ioc
->dki_length
) {
512 (void) fprintf(stderr
, "DKIOCGETEFI short "
513 "read of %d bytes\n", error
);
523 (void) fprintf(stderr
, "DKIOCSETEFI unknown "
529 error
= lseek(fd
, dk_ioc
->dki_lba
* lbsize
, SEEK_SET
);
532 (void) fprintf(stderr
, "DKIOCSETEFI lseek "
533 "error: %d\n", errno
);
537 error
= write(fd
, data
, dk_ioc
->dki_length
);
540 (void) fprintf(stderr
, "DKIOCSETEFI write "
541 "error: %d\n", errno
);
545 if (error
!= dk_ioc
->dki_length
) {
547 (void) fprintf(stderr
, "DKIOCSETEFI short "
548 "write of %d bytes\n", error
);
553 /* Sync the new EFI table to disk */
558 /* Ensure any local disk cache is also flushed */
559 if (ioctl(fd
, BLKFLSBUF
, 0) == -1)
567 (void) fprintf(stderr
, "unsupported ioctl()\n");
573 dk_ioc
->dki_data_64
= (uint64_t)(uintptr_t)data
;
574 error
= ioctl(fd
, cmd
, (void *)dk_ioc
);
575 dk_ioc
->dki_data
= data
;
583 #if defined(__linux__)
587 /* Notify the kernel a devices partition table has been updated */
588 while ((error
= ioctl(fd
, BLKRRPART
)) != 0) {
589 if ((--retry
== 0) || (errno
!= EBUSY
)) {
590 (void) fprintf(stderr
, "the kernel failed to rescan "
591 "the partition table: %d\n", errno
);
602 check_label(int fd
, dk_efi_t
*dk_ioc
)
607 if (efi_ioctl(fd
, DKIOCGETEFI
, dk_ioc
) == -1) {
615 efi
= dk_ioc
->dki_data
;
616 if (efi
->efi_gpt_Signature
!= LE_64(EFI_SIGNATURE
)) {
618 (void) fprintf(stderr
,
619 "Bad EFI signature: 0x%llx != 0x%llx\n",
620 (long long)efi
->efi_gpt_Signature
,
621 (long long)LE_64(EFI_SIGNATURE
));
626 * check CRC of the header; the size of the header should
627 * never be larger than one block
629 crc
= efi
->efi_gpt_HeaderCRC32
;
630 efi
->efi_gpt_HeaderCRC32
= 0;
631 len_t headerSize
= (len_t
)LE_32(efi
->efi_gpt_HeaderSize
);
633 if (headerSize
< EFI_MIN_LABEL_SIZE
|| headerSize
> EFI_LABEL_SIZE
) {
635 (void) fprintf(stderr
,
636 "Invalid EFI HeaderSize %llu. Assuming %d.\n",
637 headerSize
, EFI_MIN_LABEL_SIZE
);
640 if ((headerSize
> dk_ioc
->dki_length
) ||
641 crc
!= LE_32(efi_crc32((unsigned char *)efi
, headerSize
))) {
643 (void) fprintf(stderr
,
644 "Bad EFI CRC: 0x%x != 0x%x\n",
645 crc
, LE_32(efi_crc32((unsigned char *)efi
,
654 efi_read(int fd
, struct dk_gpt
*vtoc
)
661 diskaddr_t capacity
= 0;
663 struct dk_minfo disk_info
;
666 efi_gpe_t
*efi_parts
;
667 struct dk_cinfo dki_info
;
668 uint32_t user_length
;
669 boolean_t legacy_label
= B_FALSE
;
672 * get the partition number for this file descriptor.
674 if ((rval
= efi_get_info(fd
, &dki_info
)) != 0)
677 if ((strncmp(dki_info
.dki_cname
, "pseudo", 7) == 0) &&
678 (strncmp(dki_info
.dki_dname
, "md", 3) == 0)) {
680 } else if ((strncmp(dki_info
.dki_cname
, "vdc", 4) == 0) &&
681 (strncmp(dki_info
.dki_dname
, "vdc", 4) == 0)) {
683 * The controller and drive name "vdc" (virtual disk client)
684 * indicates a LDoms virtual disk.
689 /* get the LBA size */
690 if (read_disk_info(fd
, &capacity
, &lbsize
) == -1) {
692 (void) fprintf(stderr
,
693 "unable to read disk info: %d",
699 disk_info
.dki_lbsize
= lbsize
;
700 disk_info
.dki_capacity
= capacity
;
702 if (disk_info
.dki_lbsize
== 0) {
704 (void) fprintf(stderr
,
705 "efi_read: assuming LBA 512 bytes\n");
707 disk_info
.dki_lbsize
= DEV_BSIZE
;
710 * Read the EFI GPT to figure out how many partitions we need
714 if (NBLOCKS(vtoc
->efi_nparts
, disk_info
.dki_lbsize
) < 34) {
715 label_len
= EFI_MIN_ARRAY_SIZE
+ disk_info
.dki_lbsize
;
717 label_len
= vtoc
->efi_nparts
* (int) sizeof (efi_gpe_t
) +
718 disk_info
.dki_lbsize
;
719 if (label_len
% disk_info
.dki_lbsize
) {
720 /* pad to physical sector size */
721 label_len
+= disk_info
.dki_lbsize
;
722 label_len
&= ~(disk_info
.dki_lbsize
- 1);
726 if (posix_memalign((void **)&dk_ioc
.dki_data
,
727 disk_info
.dki_lbsize
, label_len
))
730 memset(dk_ioc
.dki_data
, 0, label_len
);
731 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
732 user_length
= vtoc
->efi_nparts
;
733 efi
= dk_ioc
.dki_data
;
735 dk_ioc
.dki_length
= label_len
;
736 if (efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
) == -1) {
744 } else if ((rval
= check_label(fd
, &dk_ioc
)) == VT_EINVAL
) {
746 * No valid label here; try the alternate. Note that here
747 * we just read GPT header and save it into dk_ioc.data,
748 * Later, we will read GUID partition entry array if we
749 * can get valid GPT header.
753 * This is a workaround for legacy systems. In the past, the
754 * last sector of SCSI disk was invisible on x86 platform. At
755 * that time, backup label was saved on the next to the last
756 * sector. It is possible for users to move a disk from previous
757 * solaris system to present system. Here, we attempt to search
758 * legacy backup EFI label first.
760 dk_ioc
.dki_lba
= disk_info
.dki_capacity
- 2;
761 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
762 rval
= check_label(fd
, &dk_ioc
);
763 if (rval
== VT_EINVAL
) {
765 * we didn't find legacy backup EFI label, try to
766 * search backup EFI label in the last block.
768 dk_ioc
.dki_lba
= disk_info
.dki_capacity
- 1;
769 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
770 rval
= check_label(fd
, &dk_ioc
);
772 legacy_label
= B_TRUE
;
774 (void) fprintf(stderr
,
775 "efi_read: primary label corrupt; "
776 "using EFI backup label located on"
777 " the last block\n");
780 if ((efi_debug
) && (rval
== 0))
781 (void) fprintf(stderr
, "efi_read: primary label"
782 " corrupt; using legacy EFI backup label "
783 " located on the next to last block\n");
787 dk_ioc
.dki_lba
= LE_64(efi
->efi_gpt_PartitionEntryLBA
);
788 vtoc
->efi_flags
|= EFI_GPT_PRIMARY_CORRUPT
;
790 LE_32(efi
->efi_gpt_NumberOfPartitionEntries
);
792 * Partition tables are between backup GPT header
793 * table and ParitionEntryLBA (the starting LBA of
794 * the GUID partition entries array). Now that we
795 * already got valid GPT header and saved it in
796 * dk_ioc.dki_data, we try to get GUID partition
800 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
801 + disk_info
.dki_lbsize
);
803 dk_ioc
.dki_length
= disk_info
.dki_capacity
- 1 -
806 dk_ioc
.dki_length
= disk_info
.dki_capacity
- 2 -
808 dk_ioc
.dki_length
*= disk_info
.dki_lbsize
;
809 if (dk_ioc
.dki_length
>
810 ((len_t
)label_len
- sizeof (*dk_ioc
.dki_data
))) {
814 * read GUID partition entry array
816 rval
= efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
);
820 } else if (rval
== 0) {
822 dk_ioc
.dki_lba
= LE_64(efi
->efi_gpt_PartitionEntryLBA
);
824 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
825 + disk_info
.dki_lbsize
);
826 dk_ioc
.dki_length
= label_len
- disk_info
.dki_lbsize
;
827 rval
= efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
);
829 } else if (vdc_flag
&& rval
== VT_ERROR
&& errno
== EINVAL
) {
831 * When the device is a LDoms virtual disk, the DKIOCGETEFI
832 * ioctl can fail with EINVAL if the virtual disk backend
833 * is a ZFS volume serviced by a domain running an old version
834 * of Solaris. This is because the DKIOCGETEFI ioctl was
835 * initially incorrectly implemented for a ZFS volume and it
836 * expected the GPT and GPE to be retrieved with a single ioctl.
837 * So we try to read the GPT and the GPE using that old style
841 dk_ioc
.dki_length
= label_len
;
842 rval
= check_label(fd
, &dk_ioc
);
850 /* LINTED -- always longlong aligned */
851 efi_parts
= (efi_gpe_t
*)(((char *)efi
) + disk_info
.dki_lbsize
);
854 * Assemble this into a "dk_gpt" struct for easier
855 * digestibility by applications.
857 vtoc
->efi_version
= LE_32(efi
->efi_gpt_Revision
);
858 vtoc
->efi_nparts
= LE_32(efi
->efi_gpt_NumberOfPartitionEntries
);
859 vtoc
->efi_part_size
= LE_32(efi
->efi_gpt_SizeOfPartitionEntry
);
860 vtoc
->efi_lbasize
= disk_info
.dki_lbsize
;
861 vtoc
->efi_last_lba
= disk_info
.dki_capacity
- 1;
862 vtoc
->efi_first_u_lba
= LE_64(efi
->efi_gpt_FirstUsableLBA
);
863 vtoc
->efi_last_u_lba
= LE_64(efi
->efi_gpt_LastUsableLBA
);
864 vtoc
->efi_altern_lba
= LE_64(efi
->efi_gpt_AlternateLBA
);
865 UUID_LE_CONVERT(vtoc
->efi_disk_uguid
, efi
->efi_gpt_DiskGUID
);
868 * If the array the user passed in is too small, set the length
869 * to what it needs to be and return
871 if (user_length
< vtoc
->efi_nparts
) {
875 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
877 UUID_LE_CONVERT(vtoc
->efi_parts
[i
].p_guid
,
878 efi_parts
[i
].efi_gpe_PartitionTypeGUID
);
881 j
< sizeof (conversion_array
)
882 / sizeof (struct uuid_to_ptag
); j
++) {
884 if (bcmp(&vtoc
->efi_parts
[i
].p_guid
,
885 &conversion_array
[j
].uuid
,
886 sizeof (struct uuid
)) == 0) {
887 vtoc
->efi_parts
[i
].p_tag
= j
;
891 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
)
893 vtoc
->efi_parts
[i
].p_flag
=
894 LE_16(efi_parts
[i
].efi_gpe_Attributes
.PartitionAttrs
);
895 vtoc
->efi_parts
[i
].p_start
=
896 LE_64(efi_parts
[i
].efi_gpe_StartingLBA
);
897 vtoc
->efi_parts
[i
].p_size
=
898 LE_64(efi_parts
[i
].efi_gpe_EndingLBA
) -
899 vtoc
->efi_parts
[i
].p_start
+ 1;
900 for (j
= 0; j
< EFI_PART_NAME_LEN
; j
++) {
901 vtoc
->efi_parts
[i
].p_name
[j
] =
903 efi_parts
[i
].efi_gpe_PartitionName
[j
]);
906 UUID_LE_CONVERT(vtoc
->efi_parts
[i
].p_uguid
,
907 efi_parts
[i
].efi_gpe_UniquePartitionGUID
);
911 return (dki_info
.dki_partition
);
914 /* writes a "protective" MBR */
916 write_pmbr(int fd
, struct dk_gpt
*vtoc
)
921 diskaddr_t size_in_lba
;
925 len
= (vtoc
->efi_lbasize
== 0) ? sizeof (mb
) : vtoc
->efi_lbasize
;
926 if (posix_memalign((void **)&buf
, len
, len
))
930 * Preserve any boot code and disk signature if the first block is
935 dk_ioc
.dki_length
= len
;
936 /* LINTED -- always longlong aligned */
937 dk_ioc
.dki_data
= (efi_gpt_t
*)buf
;
938 if (efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
) == -1) {
939 (void) memcpy(&mb
, buf
, sizeof (mb
));
940 bzero(&mb
, sizeof (mb
));
941 mb
.signature
= LE_16(MBB_MAGIC
);
943 (void) memcpy(&mb
, buf
, sizeof (mb
));
944 if (mb
.signature
!= LE_16(MBB_MAGIC
)) {
945 bzero(&mb
, sizeof (mb
));
946 mb
.signature
= LE_16(MBB_MAGIC
);
950 bzero(&mb
.parts
, sizeof (mb
.parts
));
951 cp
= (uchar_t
*)&mb
.parts
[0];
952 /* bootable or not */
954 /* beginning CHS; 0xffffff if not representable */
960 /* ending CHS; 0xffffff if not representable */
964 /* starting LBA: 1 (little endian format) by EFI definition */
969 /* ending LBA: last block on the disk (little endian format) */
970 size_in_lba
= vtoc
->efi_last_lba
;
971 if (size_in_lba
< 0xffffffff) {
972 *cp
++ = (size_in_lba
& 0x000000ff);
973 *cp
++ = (size_in_lba
& 0x0000ff00) >> 8;
974 *cp
++ = (size_in_lba
& 0x00ff0000) >> 16;
975 *cp
++ = (size_in_lba
& 0xff000000) >> 24;
983 (void) memcpy(buf
, &mb
, sizeof (mb
));
984 /* LINTED -- always longlong aligned */
985 dk_ioc
.dki_data
= (efi_gpt_t
*)buf
;
987 dk_ioc
.dki_length
= len
;
988 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1003 /* make sure the user specified something reasonable */
1005 check_input(struct dk_gpt
*vtoc
)
1009 diskaddr_t istart
, jstart
, isize
, jsize
, endsect
;
1012 * Sanity-check the input (make sure no partitions overlap)
1014 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1015 /* It can't be unassigned and have an actual size */
1016 if ((vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) &&
1017 (vtoc
->efi_parts
[i
].p_size
!= 0)) {
1019 (void) fprintf(stderr
, "partition %d is "
1020 "\"unassigned\" but has a size of %llu",
1021 i
, vtoc
->efi_parts
[i
].p_size
);
1025 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) {
1026 if (uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_guid
))
1028 /* we have encountered an unknown uuid */
1029 vtoc
->efi_parts
[i
].p_tag
= 0xff;
1031 if (vtoc
->efi_parts
[i
].p_tag
== V_RESERVED
) {
1032 if (resv_part
!= -1) {
1034 (void) fprintf(stderr
, "found "
1035 "duplicate reserved partition "
1042 if ((vtoc
->efi_parts
[i
].p_start
< vtoc
->efi_first_u_lba
) ||
1043 (vtoc
->efi_parts
[i
].p_start
> vtoc
->efi_last_u_lba
)) {
1045 (void) fprintf(stderr
,
1046 "Partition %d starts at %llu. ",
1048 vtoc
->efi_parts
[i
].p_start
);
1049 (void) fprintf(stderr
,
1050 "It must be between %llu and %llu.\n",
1051 vtoc
->efi_first_u_lba
,
1052 vtoc
->efi_last_u_lba
);
1056 if ((vtoc
->efi_parts
[i
].p_start
+
1057 vtoc
->efi_parts
[i
].p_size
<
1058 vtoc
->efi_first_u_lba
) ||
1059 (vtoc
->efi_parts
[i
].p_start
+
1060 vtoc
->efi_parts
[i
].p_size
>
1061 vtoc
->efi_last_u_lba
+ 1)) {
1063 (void) fprintf(stderr
,
1064 "Partition %d ends at %llu. ",
1066 vtoc
->efi_parts
[i
].p_start
+
1067 vtoc
->efi_parts
[i
].p_size
);
1068 (void) fprintf(stderr
,
1069 "It must be between %llu and %llu.\n",
1070 vtoc
->efi_first_u_lba
,
1071 vtoc
->efi_last_u_lba
);
1076 for (j
= 0; j
< vtoc
->efi_nparts
; j
++) {
1077 isize
= vtoc
->efi_parts
[i
].p_size
;
1078 jsize
= vtoc
->efi_parts
[j
].p_size
;
1079 istart
= vtoc
->efi_parts
[i
].p_start
;
1080 jstart
= vtoc
->efi_parts
[j
].p_start
;
1081 if ((i
!= j
) && (isize
!= 0) && (jsize
!= 0)) {
1082 endsect
= jstart
+ jsize
-1;
1083 if ((jstart
<= istart
) &&
1084 (istart
<= endsect
)) {
1086 (void) fprintf(stderr
,
1087 "Partition %d overlaps "
1088 "partition %d.", i
, j
);
1095 /* just a warning for now */
1096 if ((resv_part
== -1) && efi_debug
) {
1097 (void) fprintf(stderr
,
1098 "no reserved partition found\n");
1104 * add all the unallocated space to the current label
1107 efi_use_whole_disk(int fd
)
1109 struct dk_gpt
*efi_label
= NULL
;
1112 uint_t resv_index
= 0, data_index
= 0;
1113 diskaddr_t resv_start
= 0, data_start
= 0;
1114 diskaddr_t difference
;
1116 rval
= efi_alloc_and_read(fd
, &efi_label
);
1118 if (efi_label
!= NULL
)
1119 efi_free(efi_label
);
1124 * If alter_lba is 1, we are using the backup label.
1125 * Since we can locate the backup label by disk capacity,
1126 * there must be no unallocated space.
1128 if ((efi_label
->efi_altern_lba
== 1) || (efi_label
->efi_altern_lba
1129 >= efi_label
->efi_last_lba
)) {
1131 (void) fprintf(stderr
,
1132 "efi_use_whole_disk: requested space not found\n");
1134 efi_free(efi_label
);
1138 difference
= efi_label
->efi_last_lba
- efi_label
->efi_altern_lba
;
1141 * Find the last physically non-zero partition.
1142 * This is the reserved partition.
1144 for (i
= 0; i
< efi_label
->efi_nparts
; i
++) {
1145 if (resv_start
< efi_label
->efi_parts
[i
].p_start
) {
1146 resv_start
= efi_label
->efi_parts
[i
].p_start
;
1152 * Find the last physically non-zero partition before that.
1153 * This is the data partition.
1155 for (i
= 0; i
< resv_index
; i
++) {
1156 if (data_start
< efi_label
->efi_parts
[i
].p_start
) {
1157 data_start
= efi_label
->efi_parts
[i
].p_start
;
1163 * Move the reserved partition. There is currently no data in
1164 * here except fabricated devids (which get generated via
1165 * efi_write()). So there is no need to copy data.
1167 efi_label
->efi_parts
[data_index
].p_size
+= difference
;
1168 efi_label
->efi_parts
[resv_index
].p_start
+= difference
;
1169 efi_label
->efi_last_u_lba
+= difference
;
1171 rval
= efi_write(fd
, efi_label
);
1174 (void) fprintf(stderr
,
1175 "efi_use_whole_disk:fail to write label, rval=%d\n",
1178 efi_free(efi_label
);
1182 efi_free(efi_label
);
1188 * write EFI label and backup label
1191 efi_write(int fd
, struct dk_gpt
*vtoc
)
1195 efi_gpe_t
*efi_parts
;
1197 struct dk_cinfo dki_info
;
1201 diskaddr_t lba_backup_gpt_hdr
;
1203 if ((rval
= efi_get_info(fd
, &dki_info
)) != 0)
1206 /* check if we are dealing wih a metadevice */
1207 if ((strncmp(dki_info
.dki_cname
, "pseudo", 7) == 0) &&
1208 (strncmp(dki_info
.dki_dname
, "md", 3) == 0)) {
1212 if (check_input(vtoc
)) {
1214 * not valid; if it's a metadevice just pass it down
1215 * because SVM will do its own checking
1223 if (NBLOCKS(vtoc
->efi_nparts
, vtoc
->efi_lbasize
) < 34) {
1224 dk_ioc
.dki_length
= EFI_MIN_ARRAY_SIZE
+ vtoc
->efi_lbasize
;
1226 dk_ioc
.dki_length
= NBLOCKS(vtoc
->efi_nparts
,
1227 vtoc
->efi_lbasize
) *
1232 * the number of blocks occupied by GUID partition entry array
1234 nblocks
= dk_ioc
.dki_length
/ vtoc
->efi_lbasize
- 1;
1237 * Backup GPT header is located on the block after GUID
1238 * partition entry array. Here, we calculate the address
1239 * for backup GPT header.
1241 lba_backup_gpt_hdr
= vtoc
->efi_last_u_lba
+ 1 + nblocks
;
1242 if (posix_memalign((void **)&dk_ioc
.dki_data
,
1243 vtoc
->efi_lbasize
, dk_ioc
.dki_length
))
1246 memset(dk_ioc
.dki_data
, 0, dk_ioc
.dki_length
);
1247 efi
= dk_ioc
.dki_data
;
1249 /* stuff user's input into EFI struct */
1250 efi
->efi_gpt_Signature
= LE_64(EFI_SIGNATURE
);
1251 efi
->efi_gpt_Revision
= LE_32(vtoc
->efi_version
); /* 0x02000100 */
1252 efi
->efi_gpt_HeaderSize
= LE_32(sizeof (struct efi_gpt
) - LEN_EFI_PAD
);
1253 efi
->efi_gpt_Reserved1
= 0;
1254 efi
->efi_gpt_MyLBA
= LE_64(1ULL);
1255 efi
->efi_gpt_AlternateLBA
= LE_64(lba_backup_gpt_hdr
);
1256 efi
->efi_gpt_FirstUsableLBA
= LE_64(vtoc
->efi_first_u_lba
);
1257 efi
->efi_gpt_LastUsableLBA
= LE_64(vtoc
->efi_last_u_lba
);
1258 efi
->efi_gpt_PartitionEntryLBA
= LE_64(2ULL);
1259 efi
->efi_gpt_NumberOfPartitionEntries
= LE_32(vtoc
->efi_nparts
);
1260 efi
->efi_gpt_SizeOfPartitionEntry
= LE_32(sizeof (struct efi_gpe
));
1261 UUID_LE_CONVERT(efi
->efi_gpt_DiskGUID
, vtoc
->efi_disk_uguid
);
1263 /* LINTED -- always longlong aligned */
1264 efi_parts
= (efi_gpe_t
*)((char *)dk_ioc
.dki_data
+ vtoc
->efi_lbasize
);
1266 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1268 j
< sizeof (conversion_array
) /
1269 sizeof (struct uuid_to_ptag
); j
++) {
1271 if (vtoc
->efi_parts
[i
].p_tag
== j
) {
1273 efi_parts
[i
].efi_gpe_PartitionTypeGUID
,
1274 conversion_array
[j
].uuid
);
1279 if (j
== sizeof (conversion_array
) /
1280 sizeof (struct uuid_to_ptag
)) {
1282 * If we didn't have a matching uuid match, bail here.
1283 * Don't write a label with unknown uuid.
1286 (void) fprintf(stderr
,
1287 "Unknown uuid for p_tag %d\n",
1288 vtoc
->efi_parts
[i
].p_tag
);
1293 /* Zero's should be written for empty partitions */
1294 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
)
1297 efi_parts
[i
].efi_gpe_StartingLBA
=
1298 LE_64(vtoc
->efi_parts
[i
].p_start
);
1299 efi_parts
[i
].efi_gpe_EndingLBA
=
1300 LE_64(vtoc
->efi_parts
[i
].p_start
+
1301 vtoc
->efi_parts
[i
].p_size
- 1);
1302 efi_parts
[i
].efi_gpe_Attributes
.PartitionAttrs
=
1303 LE_16(vtoc
->efi_parts
[i
].p_flag
);
1304 for (j
= 0; j
< EFI_PART_NAME_LEN
; j
++) {
1305 efi_parts
[i
].efi_gpe_PartitionName
[j
] =
1306 LE_16((ushort_t
)vtoc
->efi_parts
[i
].p_name
[j
]);
1308 if ((vtoc
->efi_parts
[i
].p_tag
!= V_UNASSIGNED
) &&
1309 uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_uguid
)) {
1310 (void) uuid_generate((uchar_t
*)
1311 &vtoc
->efi_parts
[i
].p_uguid
);
1313 bcopy(&vtoc
->efi_parts
[i
].p_uguid
,
1314 &efi_parts
[i
].efi_gpe_UniquePartitionGUID
,
1317 efi
->efi_gpt_PartitionEntryArrayCRC32
=
1318 LE_32(efi_crc32((unsigned char *)efi_parts
,
1319 vtoc
->efi_nparts
* (int)sizeof (struct efi_gpe
)));
1320 efi
->efi_gpt_HeaderCRC32
=
1321 LE_32(efi_crc32((unsigned char *)efi
,
1322 LE_32(efi
->efi_gpt_HeaderSize
)));
1324 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1325 free(dk_ioc
.dki_data
);
1335 /* if it's a metadevice we're done */
1337 free(dk_ioc
.dki_data
);
1341 /* write backup partition array */
1342 dk_ioc
.dki_lba
= vtoc
->efi_last_u_lba
+ 1;
1343 dk_ioc
.dki_length
-= vtoc
->efi_lbasize
;
1345 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
+
1348 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1350 * we wrote the primary label okay, so don't fail
1353 (void) fprintf(stderr
,
1354 "write of backup partitions to block %llu "
1355 "failed, errno %d\n",
1356 vtoc
->efi_last_u_lba
+ 1,
1361 * now swap MyLBA and AlternateLBA fields and write backup
1362 * partition table header
1364 dk_ioc
.dki_lba
= lba_backup_gpt_hdr
;
1365 dk_ioc
.dki_length
= vtoc
->efi_lbasize
;
1367 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
-
1369 efi
->efi_gpt_AlternateLBA
= LE_64(1ULL);
1370 efi
->efi_gpt_MyLBA
= LE_64(lba_backup_gpt_hdr
);
1371 efi
->efi_gpt_PartitionEntryLBA
= LE_64(vtoc
->efi_last_u_lba
+ 1);
1372 efi
->efi_gpt_HeaderCRC32
= 0;
1373 efi
->efi_gpt_HeaderCRC32
=
1374 LE_32(efi_crc32((unsigned char *)dk_ioc
.dki_data
,
1375 LE_32(efi
->efi_gpt_HeaderSize
)));
1377 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1379 (void) fprintf(stderr
,
1380 "write of backup header to block %llu failed, "
1386 /* write the PMBR */
1387 (void) write_pmbr(fd
, vtoc
);
1388 free(dk_ioc
.dki_data
);
1394 efi_free(struct dk_gpt
*ptr
)
1400 * Input: File descriptor
1401 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1409 struct extvtoc extvtoc
;
1411 if (ioctl(fd
, DKIOCGEXTVTOC
, &extvtoc
) == -1) {
1412 if (errno
== ENOTSUP
)
1414 else if (errno
== ENOTTY
) {
1415 if (ioctl(fd
, DKIOCGVTOC
, &vtoc
) == -1)
1416 if (errno
== ENOTSUP
)
1427 efi_err_check(struct dk_gpt
*vtoc
)
1431 diskaddr_t istart
, jstart
, isize
, jsize
, endsect
;
1435 * make sure no partitions overlap
1437 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1438 /* It can't be unassigned and have an actual size */
1439 if ((vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) &&
1440 (vtoc
->efi_parts
[i
].p_size
!= 0)) {
1441 (void) fprintf(stderr
,
1442 "partition %d is \"unassigned\" but has a size "
1443 "of %llu\n", i
, vtoc
->efi_parts
[i
].p_size
);
1445 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) {
1448 if (vtoc
->efi_parts
[i
].p_tag
== V_RESERVED
) {
1449 if (resv_part
!= -1) {
1450 (void) fprintf(stderr
,
1451 "found duplicate reserved partition at "
1455 if (vtoc
->efi_parts
[i
].p_size
!= EFI_MIN_RESV_SIZE
)
1456 (void) fprintf(stderr
,
1457 "Warning: reserved partition size must "
1458 "be %d sectors\n", EFI_MIN_RESV_SIZE
);
1460 if ((vtoc
->efi_parts
[i
].p_start
< vtoc
->efi_first_u_lba
) ||
1461 (vtoc
->efi_parts
[i
].p_start
> vtoc
->efi_last_u_lba
)) {
1462 (void) fprintf(stderr
,
1463 "Partition %d starts at %llu\n",
1465 vtoc
->efi_parts
[i
].p_start
);
1466 (void) fprintf(stderr
,
1467 "It must be between %llu and %llu.\n",
1468 vtoc
->efi_first_u_lba
,
1469 vtoc
->efi_last_u_lba
);
1471 if ((vtoc
->efi_parts
[i
].p_start
+
1472 vtoc
->efi_parts
[i
].p_size
<
1473 vtoc
->efi_first_u_lba
) ||
1474 (vtoc
->efi_parts
[i
].p_start
+
1475 vtoc
->efi_parts
[i
].p_size
>
1476 vtoc
->efi_last_u_lba
+ 1)) {
1477 (void) fprintf(stderr
,
1478 "Partition %d ends at %llu\n",
1480 vtoc
->efi_parts
[i
].p_start
+
1481 vtoc
->efi_parts
[i
].p_size
);
1482 (void) fprintf(stderr
,
1483 "It must be between %llu and %llu.\n",
1484 vtoc
->efi_first_u_lba
,
1485 vtoc
->efi_last_u_lba
);
1488 for (j
= 0; j
< vtoc
->efi_nparts
; j
++) {
1489 isize
= vtoc
->efi_parts
[i
].p_size
;
1490 jsize
= vtoc
->efi_parts
[j
].p_size
;
1491 istart
= vtoc
->efi_parts
[i
].p_start
;
1492 jstart
= vtoc
->efi_parts
[j
].p_start
;
1493 if ((i
!= j
) && (isize
!= 0) && (jsize
!= 0)) {
1494 endsect
= jstart
+ jsize
-1;
1495 if ((jstart
<= istart
) &&
1496 (istart
<= endsect
)) {
1498 (void) fprintf(stderr
,
1499 "label error: EFI Labels do not "
1500 "support overlapping partitions\n");
1502 (void) fprintf(stderr
,
1503 "Partition %d overlaps partition "
1510 /* make sure there is a reserved partition */
1511 if (resv_part
== -1) {
1512 (void) fprintf(stderr
,
1513 "no reserved partition found\n");
1518 * We need to get information necessary to construct a *new* efi
1522 efi_auto_sense(int fd
, struct dk_gpt
**vtoc
)
1528 * Now build the default partition table
1530 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, vtoc
) != 0) {
1532 (void) fprintf(stderr
, "efi_alloc_and_init failed.\n");
1537 for (i
= 0; i
< MIN((*vtoc
)->efi_nparts
, V_NUMPAR
); i
++) {
1538 (*vtoc
)->efi_parts
[i
].p_tag
= default_vtoc_map
[i
].p_tag
;
1539 (*vtoc
)->efi_parts
[i
].p_flag
= default_vtoc_map
[i
].p_flag
;
1540 (*vtoc
)->efi_parts
[i
].p_start
= 0;
1541 (*vtoc
)->efi_parts
[i
].p_size
= 0;
1544 * Make constants first
1545 * and variable partitions later
1548 /* root partition - s0 128 MB */
1549 (*vtoc
)->efi_parts
[0].p_start
= 34;
1550 (*vtoc
)->efi_parts
[0].p_size
= 262144;
1552 /* partition - s1 128 MB */
1553 (*vtoc
)->efi_parts
[1].p_start
= 262178;
1554 (*vtoc
)->efi_parts
[1].p_size
= 262144;
1556 /* partition -s2 is NOT the Backup disk */
1557 (*vtoc
)->efi_parts
[2].p_tag
= V_UNASSIGNED
;
1559 /* partition -s6 /usr partition - HOG */
1560 (*vtoc
)->efi_parts
[6].p_start
= 524322;
1561 (*vtoc
)->efi_parts
[6].p_size
= (*vtoc
)->efi_last_u_lba
- 524322
1564 /* efi reserved partition - s9 16K */
1565 (*vtoc
)->efi_parts
[8].p_start
= (*vtoc
)->efi_last_u_lba
- (1024 * 16);
1566 (*vtoc
)->efi_parts
[8].p_size
= (1024 * 16);
1567 (*vtoc
)->efi_parts
[8].p_tag
= V_RESERVED
;