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 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2014 Toomas Soome <tsoome@me.com>
26 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
35 #include <uuid/uuid.h>
37 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/dktp/fdisk.h>
43 #include <sys/efi_partition.h>
44 #include <sys/byteorder.h>
47 static struct uuid_to_ptag
{
49 } conversion_array
[] = {
56 { 0 }, /* STAND is never used */
60 { 0 }, /* CACHE is never used */
78 { EFI_FREEBSD_VINUM
},
83 * Default vtoc information for non-SVr4 partitions
85 struct dk_map2 default_vtoc_map
[NDKMAP
] = {
86 { V_ROOT
, 0 }, /* a - 0 */
87 { V_SWAP
, V_UNMNT
}, /* b - 1 */
88 { V_BACKUP
, V_UNMNT
}, /* c - 2 */
89 { V_UNASSIGNED
, 0 }, /* d - 3 */
90 { V_UNASSIGNED
, 0 }, /* e - 4 */
91 { V_UNASSIGNED
, 0 }, /* f - 5 */
92 { V_USR
, 0 }, /* g - 6 */
93 { V_UNASSIGNED
, 0 }, /* h - 7 */
95 #if defined(_SUNOS_VTOC_16)
97 #if defined(i386) || defined(__amd64)
98 { V_BOOT
, V_UNMNT
}, /* i - 8 */
99 { V_ALTSCTR
, 0 }, /* j - 9 */
102 #error No VTOC format defined.
103 #endif /* defined(i386) */
105 { V_UNASSIGNED
, 0 }, /* k - 10 */
106 { V_UNASSIGNED
, 0 }, /* l - 11 */
107 { V_UNASSIGNED
, 0 }, /* m - 12 */
108 { V_UNASSIGNED
, 0 }, /* n - 13 */
109 { V_UNASSIGNED
, 0 }, /* o - 14 */
110 { V_UNASSIGNED
, 0 }, /* p - 15 */
111 #endif /* defined(_SUNOS_VTOC_16) */
120 #define EFI_FIXES_DB "/usr/share/hwdata/efi.fixes"
122 extern unsigned int efi_crc32(const unsigned char *, unsigned int);
123 static int efi_read(int, struct dk_gpt
*);
126 read_disk_info(int fd
, diskaddr_t
*capacity
, uint_t
*lbsize
)
128 struct dk_minfo disk_info
;
130 if ((ioctl(fd
, DKIOCGMEDIAINFO
, (caddr_t
)&disk_info
)) == -1)
132 *capacity
= disk_info
.dki_capacity
;
133 *lbsize
= disk_info
.dki_lbsize
;
138 * the number of blocks the EFI label takes up (round up to nearest
141 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
143 /* number of partitions -- limited by what we can malloc */
144 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
145 sizeof (struct dk_part))
148 efi_alloc_and_init(int fd
, uint32_t nparts
, struct dk_gpt
**vtoc
)
157 if (read_disk_info(fd
, &capacity
, &lbsize
) != 0) {
159 (void) fprintf(stderr
,
160 "couldn't read disk information\n");
164 nblocks
= NBLOCKS(nparts
, lbsize
);
165 if ((nblocks
* lbsize
) < EFI_MIN_ARRAY_SIZE
+ lbsize
) {
166 /* 16K plus one block for the GPT */
167 nblocks
= EFI_MIN_ARRAY_SIZE
/ lbsize
+ 1;
170 if (nparts
> MAX_PARTS
) {
172 (void) fprintf(stderr
,
173 "the maximum number of partitions supported is %lu\n",
179 length
= sizeof (struct dk_gpt
) +
180 sizeof (struct dk_part
) * (nparts
- 1);
182 if ((*vtoc
= calloc(length
, 1)) == NULL
)
187 vptr
->efi_version
= EFI_VERSION_CURRENT
;
188 vptr
->efi_lbasize
= lbsize
;
189 vptr
->efi_nparts
= nparts
;
191 * add one block here for the PMBR; on disks with a 512 byte
192 * block size and 128 or fewer partitions, efi_first_u_lba
193 * should work out to "34"
195 vptr
->efi_first_u_lba
= nblocks
+ 1;
196 vptr
->efi_last_lba
= capacity
- 1;
197 vptr
->efi_altern_lba
= capacity
-1;
198 vptr
->efi_last_u_lba
= vptr
->efi_last_lba
- nblocks
;
200 (void) uuid_generate((uchar_t
*)&uuid
);
201 UUID_LE_CONVERT(vptr
->efi_disk_uguid
, uuid
);
206 * Read EFI - return partition number upon success.
209 efi_alloc_and_read(int fd
, struct dk_gpt
**vtoc
)
220 if (read_disk_info(fd
, &capacity
, &lbsize
) != 0)
223 if ((mbr
= calloc(lbsize
, 1)) == NULL
)
226 if ((ioctl(fd
, DKIOCGMBOOT
, (caddr_t
)mbr
)) == -1) {
231 if (mbr
->signature
!= MBB_MAGIC
) {
235 ipart
= (struct ipart
*)(uintptr_t)mbr
->parts
;
237 /* Check if we have partition with ID EFI_PMBR */
238 for (i
= 0; i
< FD_NUMPART
; i
++) {
239 if (ipart
[i
].systid
== EFI_PMBR
)
246 /* figure out the number of entries that would fit into 16K */
247 nparts
= EFI_MIN_ARRAY_SIZE
/ sizeof (efi_gpe_t
);
248 length
= (int) sizeof (struct dk_gpt
) +
249 (int) sizeof (struct dk_part
) * (nparts
- 1);
250 if ((*vtoc
= calloc(length
, 1)) == NULL
)
253 (*vtoc
)->efi_nparts
= nparts
;
254 rval
= efi_read(fd
, *vtoc
);
256 if ((rval
== VT_EINVAL
) && (*vtoc
)->efi_nparts
> nparts
) {
258 length
= (int) sizeof (struct dk_gpt
) +
259 (int) sizeof (struct dk_part
) *
260 ((*vtoc
)->efi_nparts
- 1);
261 nparts
= (*vtoc
)->efi_nparts
;
262 if ((tmp
= realloc(*vtoc
, length
)) == NULL
) {
268 rval
= efi_read(fd
, *vtoc
);
274 (void) fprintf(stderr
,
275 "read of EFI table failed, rval=%d\n", rval
);
285 efi_ioctl(int fd
, int cmd
, dk_efi_t
*dk_ioc
)
287 void *data
= dk_ioc
->dki_data
;
290 dk_ioc
->dki_data_64
= (uint64_t)(uintptr_t)data
;
291 error
= ioctl(fd
, cmd
, (void *)dk_ioc
);
292 dk_ioc
->dki_data
= data
;
298 check_label(int fd
, dk_efi_t
*dk_ioc
)
303 if (efi_ioctl(fd
, DKIOCGETEFI
, dk_ioc
) == -1) {
311 efi
= dk_ioc
->dki_data
;
312 if (efi
->efi_gpt_Signature
!= LE_64(EFI_SIGNATURE
)) {
314 (void) fprintf(stderr
,
315 "Bad EFI signature: 0x%llx != 0x%llx\n",
316 (long long)efi
->efi_gpt_Signature
,
317 (long long)LE_64(EFI_SIGNATURE
));
322 * check CRC of the header; the size of the header should
323 * never be larger than one block
325 crc
= efi
->efi_gpt_HeaderCRC32
;
326 efi
->efi_gpt_HeaderCRC32
= 0;
328 if (((len_t
)LE_32(efi
->efi_gpt_HeaderSize
) > dk_ioc
->dki_length
) ||
329 crc
!= LE_32(efi_crc32((unsigned char *)efi
,
330 LE_32(efi
->efi_gpt_HeaderSize
)))) {
332 (void) fprintf(stderr
,
333 "Bad EFI CRC: 0x%x != 0x%x\n",
335 LE_32(efi_crc32((unsigned char *)efi
,
336 sizeof (struct efi_gpt
))));
344 efi_read(int fd
, struct dk_gpt
*vtoc
)
350 struct dk_minfo disk_info
;
353 efi_gpe_t
*efi_parts
;
354 struct dk_cinfo dki_info
;
355 uint32_t user_length
;
356 boolean_t legacy_label
= B_FALSE
;
359 * get the partition number for this file descriptor.
361 if (ioctl(fd
, DKIOCINFO
, (caddr_t
)&dki_info
) == -1) {
363 (void) fprintf(stderr
, "DKIOCINFO errno 0x%x\n", errno
);
375 if ((strncmp(dki_info
.dki_cname
, "vdc", 4) == 0) &&
376 (strncmp(dki_info
.dki_dname
, "vdc", 4) == 0)) {
378 * The controller and drive name "vdc" (virtual disk client)
379 * indicates a LDoms virtual disk.
384 /* get the LBA size */
385 if (ioctl(fd
, DKIOCGMEDIAINFO
, (caddr_t
)&disk_info
) == -1) {
387 (void) fprintf(stderr
,
388 "assuming LBA 512 bytes %d\n",
391 disk_info
.dki_lbsize
= DEV_BSIZE
;
393 if (disk_info
.dki_lbsize
== 0) {
395 (void) fprintf(stderr
,
396 "efi_read: assuming LBA 512 bytes\n");
398 disk_info
.dki_lbsize
= DEV_BSIZE
;
401 * Read the EFI GPT to figure out how many partitions we need
405 if (NBLOCKS(vtoc
->efi_nparts
, disk_info
.dki_lbsize
) < 34) {
406 label_len
= EFI_MIN_ARRAY_SIZE
+ disk_info
.dki_lbsize
;
408 label_len
= vtoc
->efi_nparts
* (int) sizeof (efi_gpe_t
) +
409 disk_info
.dki_lbsize
;
410 if (label_len
% disk_info
.dki_lbsize
) {
411 /* pad to physical sector size */
412 label_len
+= disk_info
.dki_lbsize
;
413 label_len
&= ~(disk_info
.dki_lbsize
- 1);
417 if ((dk_ioc
.dki_data
= calloc(label_len
, 1)) == NULL
)
420 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
421 user_length
= vtoc
->efi_nparts
;
422 efi
= dk_ioc
.dki_data
;
423 if ((rval
= check_label(fd
, &dk_ioc
)) == VT_EINVAL
) {
425 * No valid label here; try the alternate. Note that here
426 * we just read GPT header and save it into dk_ioc.data,
427 * Later, we will read GUID partition entry array if we
428 * can get valid GPT header.
432 * This is a workaround for legacy systems. In the past, the
433 * last sector of SCSI disk was invisible on x86 platform. At
434 * that time, backup label was saved on the next to the last
435 * sector. It is possible for users to move a disk from previous
436 * solaris system to present system. Here, we attempt to search
437 * legacy backup EFI label first.
439 dk_ioc
.dki_lba
= disk_info
.dki_capacity
- 2;
440 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
441 rval
= check_label(fd
, &dk_ioc
);
442 if (rval
== VT_EINVAL
) {
444 * we didn't find legacy backup EFI label, try to
445 * search backup EFI label in the last block.
447 dk_ioc
.dki_lba
= disk_info
.dki_capacity
- 1;
448 dk_ioc
.dki_length
= disk_info
.dki_lbsize
;
449 rval
= check_label(fd
, &dk_ioc
);
451 legacy_label
= B_TRUE
;
453 (void) fprintf(stderr
,
454 "efi_read: primary label corrupt; "
455 "using EFI backup label located on"
456 " the last block\n");
459 if ((efi_debug
) && (rval
== 0))
460 (void) fprintf(stderr
, "efi_read: primary label"
461 " corrupt; using legacy EFI backup label "
462 " located on the next to last block\n");
466 dk_ioc
.dki_lba
= LE_64(efi
->efi_gpt_PartitionEntryLBA
);
467 vtoc
->efi_flags
|= EFI_GPT_PRIMARY_CORRUPT
;
469 LE_32(efi
->efi_gpt_NumberOfPartitionEntries
);
471 * Partition tables are between backup GPT header
472 * table and ParitionEntryLBA (the starting LBA of
473 * the GUID partition entries array). Now that we
474 * already got valid GPT header and saved it in
475 * dk_ioc.dki_data, we try to get GUID partition
479 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
480 + disk_info
.dki_lbsize
);
482 dk_ioc
.dki_length
= disk_info
.dki_capacity
- 1 -
485 dk_ioc
.dki_length
= disk_info
.dki_capacity
- 2 -
487 dk_ioc
.dki_length
*= disk_info
.dki_lbsize
;
488 if (dk_ioc
.dki_length
>
489 ((len_t
)label_len
- sizeof (*dk_ioc
.dki_data
))) {
493 * read GUID partition entry array
495 rval
= efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
);
499 } else if (rval
== 0) {
501 dk_ioc
.dki_lba
= LE_64(efi
->efi_gpt_PartitionEntryLBA
);
503 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
504 + disk_info
.dki_lbsize
);
505 dk_ioc
.dki_length
= label_len
- disk_info
.dki_lbsize
;
506 rval
= efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
);
508 } else if (vdc_flag
&& rval
== VT_ERROR
&& errno
== EINVAL
) {
510 * When the device is a LDoms virtual disk, the DKIOCGETEFI
511 * ioctl can fail with EINVAL if the virtual disk backend
512 * is a ZFS volume serviced by a domain running an old version
513 * of Solaris. This is because the DKIOCGETEFI ioctl was
514 * initially incorrectly implemented for a ZFS volume and it
515 * expected the GPT and GPE to be retrieved with a single ioctl.
516 * So we try to read the GPT and the GPE using that old style
520 dk_ioc
.dki_length
= label_len
;
521 rval
= check_label(fd
, &dk_ioc
);
529 /* LINTED -- always longlong aligned */
530 efi_parts
= (efi_gpe_t
*)(((char *)efi
) + disk_info
.dki_lbsize
);
533 * Assemble this into a "dk_gpt" struct for easier
534 * digestibility by applications.
536 vtoc
->efi_version
= LE_32(efi
->efi_gpt_Revision
);
537 vtoc
->efi_nparts
= LE_32(efi
->efi_gpt_NumberOfPartitionEntries
);
538 vtoc
->efi_part_size
= LE_32(efi
->efi_gpt_SizeOfPartitionEntry
);
539 vtoc
->efi_lbasize
= disk_info
.dki_lbsize
;
540 vtoc
->efi_last_lba
= disk_info
.dki_capacity
- 1;
541 vtoc
->efi_first_u_lba
= LE_64(efi
->efi_gpt_FirstUsableLBA
);
542 vtoc
->efi_last_u_lba
= LE_64(efi
->efi_gpt_LastUsableLBA
);
543 vtoc
->efi_altern_lba
= LE_64(efi
->efi_gpt_AlternateLBA
);
544 UUID_LE_CONVERT(vtoc
->efi_disk_uguid
, efi
->efi_gpt_DiskGUID
);
547 * If the array the user passed in is too small, set the length
548 * to what it needs to be and return
550 if (user_length
< vtoc
->efi_nparts
) {
554 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
556 UUID_LE_CONVERT(vtoc
->efi_parts
[i
].p_guid
,
557 efi_parts
[i
].efi_gpe_PartitionTypeGUID
);
560 j
< sizeof (conversion_array
)
561 / sizeof (struct uuid_to_ptag
); j
++) {
563 if (bcmp(&vtoc
->efi_parts
[i
].p_guid
,
564 &conversion_array
[j
].uuid
,
565 sizeof (struct uuid
)) == 0) {
566 vtoc
->efi_parts
[i
].p_tag
= j
;
570 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
)
572 vtoc
->efi_parts
[i
].p_flag
=
573 LE_16(efi_parts
[i
].efi_gpe_Attributes
.PartitionAttrs
);
574 vtoc
->efi_parts
[i
].p_start
=
575 LE_64(efi_parts
[i
].efi_gpe_StartingLBA
);
576 vtoc
->efi_parts
[i
].p_size
=
577 LE_64(efi_parts
[i
].efi_gpe_EndingLBA
) -
578 vtoc
->efi_parts
[i
].p_start
+ 1;
579 for (j
= 0; j
< EFI_PART_NAME_LEN
; j
++) {
580 vtoc
->efi_parts
[i
].p_name
[j
] =
582 efi_parts
[i
].efi_gpe_PartitionName
[j
]);
585 UUID_LE_CONVERT(vtoc
->efi_parts
[i
].p_uguid
,
586 efi_parts
[i
].efi_gpe_UniquePartitionGUID
);
590 return (dki_info
.dki_partition
);
594 hardware_workarounds(int *slot
, int *active
)
596 smbios_struct_t s_sys
, s_mb
;
597 smbios_info_t sys
, mb
;
603 if ((fp
= fopen(EFI_FIXES_DB
, "rF")) == NULL
)
606 if ((shp
= smbios_open(NULL
, SMB_VERSION
, 0, &err
)) == NULL
) {
608 (void) fprintf(stderr
,
609 "libefi failed to load SMBIOS: %s\n",
615 if (smbios_lookup_type(shp
, SMB_TYPE_SYSTEM
, &s_sys
) == SMB_ERR
||
616 smbios_info_common(shp
, s_sys
.smbstr_id
, &sys
) == SMB_ERR
)
617 (void) memset(&sys
, '\0', sizeof (sys
));
618 if (smbios_lookup_type(shp
, SMB_TYPE_BASEBOARD
, &s_mb
) == SMB_ERR
||
619 smbios_info_common(shp
, s_mb
.smbstr_id
, &mb
) == SMB_ERR
)
620 (void) memset(&mb
, '\0', sizeof (mb
));
622 while (fgets(buf
, sizeof (buf
), fp
) != NULL
) {
623 char *tok
, *val
, *end
;
625 tok
= buf
+ strspn(buf
, " \t");
628 while (*tok
!= '\0') {
629 tok
+= strspn(tok
, " \t");
630 if ((val
= strchr(tok
, '=')) == NULL
)
634 end
= strchr(++val
, '"');
636 end
= strpbrk(val
, " \t\n");
641 if (strcmp(tok
, "sys.manufacturer") == 0 &&
642 (sys
.smbi_manufacturer
== NULL
||
643 strcasecmp(val
, sys
.smbi_manufacturer
)))
645 if (strcmp(tok
, "sys.product") == 0 &&
646 (sys
.smbi_product
== NULL
||
647 strcasecmp(val
, sys
.smbi_product
)))
649 if (strcmp(tok
, "sys.version") == 0 &&
650 (sys
.smbi_version
== NULL
||
651 strcasecmp(val
, sys
.smbi_version
)))
653 if (strcmp(tok
, "mb.manufacturer") == 0 &&
654 (mb
.smbi_manufacturer
== NULL
||
655 strcasecmp(val
, mb
.smbi_manufacturer
)))
657 if (strcmp(tok
, "mb.product") == 0 &&
658 (mb
.smbi_product
== NULL
||
659 strcasecmp(val
, mb
.smbi_product
)))
661 if (strcmp(tok
, "mb.version") == 0 &&
662 (mb
.smbi_version
== NULL
||
663 strcasecmp(val
, mb
.smbi_version
)))
666 if (strcmp(tok
, "pmbr_slot") == 0) {
668 if (*slot
< 0 || *slot
> 3)
671 (void) fprintf(stderr
,
672 "Using slot %d\n", *slot
);
675 if (strcmp(tok
, "pmbr_active") == 0) {
677 if (*active
< 0 || *active
> 1)
680 (void) fprintf(stderr
,
681 "Using active %d\n", *active
);
691 /* writes a "protective" MBR */
693 write_pmbr(int fd
, struct dk_gpt
*vtoc
)
698 diskaddr_t size_in_lba
;
700 int len
, slot
, active
;
704 hardware_workarounds(&slot
, &active
);
706 len
= (vtoc
->efi_lbasize
== 0) ? sizeof (mb
) : vtoc
->efi_lbasize
;
707 buf
= calloc(len
, 1);
710 * Preserve any boot code and disk signature if the first block is
714 dk_ioc
.dki_length
= len
;
715 /* LINTED -- always longlong aligned */
716 dk_ioc
.dki_data
= (efi_gpt_t
*)buf
;
717 if (efi_ioctl(fd
, DKIOCGETEFI
, &dk_ioc
) == -1) {
718 (void) memcpy(&mb
, buf
, sizeof (mb
));
719 bzero(&mb
, sizeof (mb
));
720 mb
.signature
= LE_16(MBB_MAGIC
);
722 (void) memcpy(&mb
, buf
, sizeof (mb
));
723 if (mb
.signature
!= LE_16(MBB_MAGIC
)) {
724 bzero(&mb
, sizeof (mb
));
725 mb
.signature
= LE_16(MBB_MAGIC
);
729 bzero(&mb
.parts
, sizeof (mb
.parts
));
730 cp
= (uchar_t
*)&mb
.parts
[slot
* sizeof (struct ipart
)];
731 /* bootable or not */
732 *cp
++ = active
? ACTIVE
: NOTACTIVE
;
733 /* beginning CHS; 0xffffff if not representable */
739 /* ending CHS; 0xffffff if not representable */
743 /* starting LBA: 1 (little endian format) by EFI definition */
748 /* ending LBA: last block on the disk (little endian format) */
749 size_in_lba
= vtoc
->efi_last_lba
;
750 if (size_in_lba
< 0xffffffff) {
751 *cp
++ = (size_in_lba
& 0x000000ff);
752 *cp
++ = (size_in_lba
& 0x0000ff00) >> 8;
753 *cp
++ = (size_in_lba
& 0x00ff0000) >> 16;
754 *cp
++ = (size_in_lba
& 0xff000000) >> 24;
762 (void) memcpy(buf
, &mb
, sizeof (mb
));
763 /* LINTED -- always longlong aligned */
764 dk_ioc
.dki_data
= (efi_gpt_t
*)buf
;
766 dk_ioc
.dki_length
= len
;
767 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
782 /* make sure the user specified something reasonable */
784 check_input(struct dk_gpt
*vtoc
)
788 diskaddr_t istart
, jstart
, isize
, jsize
, endsect
;
791 * Sanity-check the input (make sure no partitions overlap)
793 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
794 /* It can't be unassigned and have an actual size */
795 if ((vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) &&
796 (vtoc
->efi_parts
[i
].p_size
!= 0)) {
798 (void) fprintf(stderr
,
799 "partition %d is \"unassigned\" but has a size of %llu",
801 vtoc
->efi_parts
[i
].p_size
);
805 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) {
806 if (uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_guid
))
808 /* we have encountered an unknown uuid */
809 vtoc
->efi_parts
[i
].p_tag
= 0xff;
811 if (vtoc
->efi_parts
[i
].p_tag
== V_RESERVED
) {
812 if (resv_part
!= -1) {
814 (void) fprintf(stderr
,
815 "found duplicate reserved partition at %d\n",
822 if ((vtoc
->efi_parts
[i
].p_start
< vtoc
->efi_first_u_lba
) ||
823 (vtoc
->efi_parts
[i
].p_start
> vtoc
->efi_last_u_lba
)) {
825 (void) fprintf(stderr
,
826 "Partition %d starts at %llu. ",
828 vtoc
->efi_parts
[i
].p_start
);
829 (void) fprintf(stderr
,
830 "It must be between %llu and %llu.\n",
831 vtoc
->efi_first_u_lba
,
832 vtoc
->efi_last_u_lba
);
836 if ((vtoc
->efi_parts
[i
].p_start
+
837 vtoc
->efi_parts
[i
].p_size
<
838 vtoc
->efi_first_u_lba
) ||
839 (vtoc
->efi_parts
[i
].p_start
+
840 vtoc
->efi_parts
[i
].p_size
>
841 vtoc
->efi_last_u_lba
+ 1)) {
843 (void) fprintf(stderr
,
844 "Partition %d ends at %llu. ",
846 vtoc
->efi_parts
[i
].p_start
+
847 vtoc
->efi_parts
[i
].p_size
);
848 (void) fprintf(stderr
,
849 "It must be between %llu and %llu.\n",
850 vtoc
->efi_first_u_lba
,
851 vtoc
->efi_last_u_lba
);
856 for (j
= 0; j
< vtoc
->efi_nparts
; j
++) {
857 isize
= vtoc
->efi_parts
[i
].p_size
;
858 jsize
= vtoc
->efi_parts
[j
].p_size
;
859 istart
= vtoc
->efi_parts
[i
].p_start
;
860 jstart
= vtoc
->efi_parts
[j
].p_start
;
861 if ((i
!= j
) && (isize
!= 0) && (jsize
!= 0)) {
862 endsect
= jstart
+ jsize
-1;
863 if ((jstart
<= istart
) &&
864 (istart
<= endsect
)) {
866 (void) fprintf(stderr
,
867 "Partition %d overlaps partition %d.",
875 /* just a warning for now */
876 if ((resv_part
== -1) && efi_debug
) {
877 (void) fprintf(stderr
,
878 "no reserved partition found\n");
884 * add all the unallocated space to the current label
887 efi_use_whole_disk(int fd
)
889 struct dk_gpt
*efi_label
;
892 uint_t phy_last_slice
= 0;
893 diskaddr_t pl_start
= 0;
896 rval
= efi_alloc_and_read(fd
, &efi_label
);
901 /* find the last physically non-zero partition */
902 for (i
= 0; i
< efi_label
->efi_nparts
- 2; i
++) {
903 if (pl_start
< efi_label
->efi_parts
[i
].p_start
) {
904 pl_start
= efi_label
->efi_parts
[i
].p_start
;
908 pl_size
= efi_label
->efi_parts
[phy_last_slice
].p_size
;
911 * If alter_lba is 1, we are using the backup label.
912 * Since we can locate the backup label by disk capacity,
913 * there must be no unallocated space.
915 if ((efi_label
->efi_altern_lba
== 1) || (efi_label
->efi_altern_lba
916 >= efi_label
->efi_last_lba
)) {
918 (void) fprintf(stderr
,
919 "efi_use_whole_disk: requested space not found\n");
926 * If there is space between the last physically non-zero partition
927 * and the reserved partition, just add the unallocated space to this
928 * area. Otherwise, the unallocated space is added to the last
929 * physically non-zero partition.
931 if (pl_start
+ pl_size
- 1 == efi_label
->efi_last_u_lba
-
933 efi_label
->efi_parts
[phy_last_slice
].p_size
+=
934 efi_label
->efi_last_lba
- efi_label
->efi_altern_lba
;
938 * Move the reserved partition. There is currently no data in
939 * here except fabricated devids (which get generated via
940 * efi_write()). So there is no need to copy data.
942 efi_label
->efi_parts
[efi_label
->efi_nparts
- 1].p_start
+=
943 efi_label
->efi_last_lba
- efi_label
->efi_altern_lba
;
944 efi_label
->efi_last_u_lba
+= efi_label
->efi_last_lba
945 - efi_label
->efi_altern_lba
;
947 rval
= efi_write(fd
, efi_label
);
950 (void) fprintf(stderr
,
951 "efi_use_whole_disk:fail to write label, rval=%d\n",
964 * write EFI label and backup label
967 efi_write(int fd
, struct dk_gpt
*vtoc
)
971 efi_gpe_t
*efi_parts
;
973 struct dk_cinfo dki_info
;
975 diskaddr_t lba_backup_gpt_hdr
;
977 if (ioctl(fd
, DKIOCINFO
, (caddr_t
)&dki_info
) == -1) {
979 (void) fprintf(stderr
, "DKIOCINFO errno 0x%x\n", errno
);
990 if (check_input(vtoc
))
994 if (NBLOCKS(vtoc
->efi_nparts
, vtoc
->efi_lbasize
) < 34) {
995 dk_ioc
.dki_length
= EFI_MIN_ARRAY_SIZE
+ vtoc
->efi_lbasize
;
997 dk_ioc
.dki_length
= NBLOCKS(vtoc
->efi_nparts
,
1003 * the number of blocks occupied by GUID partition entry array
1005 nblocks
= dk_ioc
.dki_length
/ vtoc
->efi_lbasize
- 1;
1008 * Backup GPT header is located on the block after GUID
1009 * partition entry array. Here, we calculate the address
1010 * for backup GPT header.
1012 lba_backup_gpt_hdr
= vtoc
->efi_last_u_lba
+ 1 + nblocks
;
1013 if ((dk_ioc
.dki_data
= calloc(dk_ioc
.dki_length
, 1)) == NULL
)
1016 efi
= dk_ioc
.dki_data
;
1018 /* stuff user's input into EFI struct */
1019 efi
->efi_gpt_Signature
= LE_64(EFI_SIGNATURE
);
1020 efi
->efi_gpt_Revision
= LE_32(vtoc
->efi_version
); /* 0x02000100 */
1021 efi
->efi_gpt_HeaderSize
= LE_32(sizeof (struct efi_gpt
));
1022 efi
->efi_gpt_Reserved1
= 0;
1023 efi
->efi_gpt_MyLBA
= LE_64(1ULL);
1024 efi
->efi_gpt_AlternateLBA
= LE_64(lba_backup_gpt_hdr
);
1025 efi
->efi_gpt_FirstUsableLBA
= LE_64(vtoc
->efi_first_u_lba
);
1026 efi
->efi_gpt_LastUsableLBA
= LE_64(vtoc
->efi_last_u_lba
);
1027 efi
->efi_gpt_PartitionEntryLBA
= LE_64(2ULL);
1028 efi
->efi_gpt_NumberOfPartitionEntries
= LE_32(vtoc
->efi_nparts
);
1029 efi
->efi_gpt_SizeOfPartitionEntry
= LE_32(sizeof (struct efi_gpe
));
1030 UUID_LE_CONVERT(efi
->efi_gpt_DiskGUID
, vtoc
->efi_disk_uguid
);
1032 /* LINTED -- always longlong aligned */
1033 efi_parts
= (efi_gpe_t
*)((char *)dk_ioc
.dki_data
+ vtoc
->efi_lbasize
);
1035 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1037 j
< sizeof (conversion_array
) /
1038 sizeof (struct uuid_to_ptag
); j
++) {
1040 if (vtoc
->efi_parts
[i
].p_tag
== j
) {
1042 efi_parts
[i
].efi_gpe_PartitionTypeGUID
,
1043 conversion_array
[j
].uuid
);
1048 if (j
== sizeof (conversion_array
) /
1049 sizeof (struct uuid_to_ptag
)) {
1051 * If we didn't have a matching uuid match, bail here.
1052 * Don't write a label with unknown uuid.
1055 (void) fprintf(stderr
,
1056 "Unknown uuid for p_tag %d\n",
1057 vtoc
->efi_parts
[i
].p_tag
);
1062 efi_parts
[i
].efi_gpe_StartingLBA
=
1063 LE_64(vtoc
->efi_parts
[i
].p_start
);
1064 efi_parts
[i
].efi_gpe_EndingLBA
=
1065 LE_64(vtoc
->efi_parts
[i
].p_start
+
1066 vtoc
->efi_parts
[i
].p_size
- 1);
1067 efi_parts
[i
].efi_gpe_Attributes
.PartitionAttrs
=
1068 LE_16(vtoc
->efi_parts
[i
].p_flag
);
1069 for (j
= 0; j
< EFI_PART_NAME_LEN
; j
++) {
1070 efi_parts
[i
].efi_gpe_PartitionName
[j
] =
1071 LE_16((ushort_t
)vtoc
->efi_parts
[i
].p_name
[j
]);
1073 if ((vtoc
->efi_parts
[i
].p_tag
!= V_UNASSIGNED
) &&
1074 uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_uguid
)) {
1075 (void) uuid_generate((uchar_t
*)
1076 &vtoc
->efi_parts
[i
].p_uguid
);
1078 bcopy(&vtoc
->efi_parts
[i
].p_uguid
,
1079 &efi_parts
[i
].efi_gpe_UniquePartitionGUID
,
1082 efi
->efi_gpt_PartitionEntryArrayCRC32
=
1083 LE_32(efi_crc32((unsigned char *)efi_parts
,
1084 vtoc
->efi_nparts
* (int)sizeof (struct efi_gpe
)));
1085 efi
->efi_gpt_HeaderCRC32
=
1086 LE_32(efi_crc32((unsigned char *)efi
, sizeof (struct efi_gpt
)));
1088 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1089 free(dk_ioc
.dki_data
);
1100 /* write backup partition array */
1101 dk_ioc
.dki_lba
= vtoc
->efi_last_u_lba
+ 1;
1102 dk_ioc
.dki_length
-= vtoc
->efi_lbasize
;
1104 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
+
1107 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1109 * we wrote the primary label okay, so don't fail
1112 (void) fprintf(stderr
,
1113 "write of backup partitions to block %llu "
1114 "failed, errno %d\n",
1115 vtoc
->efi_last_u_lba
+ 1,
1120 * now swap MyLBA and AlternateLBA fields and write backup
1121 * partition table header
1123 dk_ioc
.dki_lba
= lba_backup_gpt_hdr
;
1124 dk_ioc
.dki_length
= vtoc
->efi_lbasize
;
1126 dk_ioc
.dki_data
= (efi_gpt_t
*)((char *)dk_ioc
.dki_data
-
1128 efi
->efi_gpt_AlternateLBA
= LE_64(1ULL);
1129 efi
->efi_gpt_MyLBA
= LE_64(lba_backup_gpt_hdr
);
1130 efi
->efi_gpt_PartitionEntryLBA
= LE_64(vtoc
->efi_last_u_lba
+ 1);
1131 efi
->efi_gpt_HeaderCRC32
= 0;
1132 efi
->efi_gpt_HeaderCRC32
=
1133 LE_32(efi_crc32((unsigned char *)dk_ioc
.dki_data
,
1134 sizeof (struct efi_gpt
)));
1136 if (efi_ioctl(fd
, DKIOCSETEFI
, &dk_ioc
) == -1) {
1138 (void) fprintf(stderr
,
1139 "write of backup header to block %llu failed, "
1145 /* write the PMBR */
1146 (void) write_pmbr(fd
, vtoc
);
1147 free(dk_ioc
.dki_data
);
1152 efi_free(struct dk_gpt
*ptr
)
1158 * Input: File descriptor
1159 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1166 struct extvtoc extvtoc
;
1168 if (ioctl(fd
, DKIOCGEXTVTOC
, &extvtoc
) == -1) {
1169 if (errno
== ENOTSUP
)
1171 else if (errno
== ENOTTY
) {
1172 if (ioctl(fd
, DKIOCGVTOC
, &vtoc
) == -1)
1173 if (errno
== ENOTSUP
)
1181 efi_err_check(struct dk_gpt
*vtoc
)
1185 diskaddr_t istart
, jstart
, isize
, jsize
, endsect
;
1189 * make sure no partitions overlap
1191 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
1192 /* It can't be unassigned and have an actual size */
1193 if ((vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) &&
1194 (vtoc
->efi_parts
[i
].p_size
!= 0)) {
1195 (void) fprintf(stderr
,
1196 "partition %d is \"unassigned\" but has a size "
1197 "of %llu\n", i
, vtoc
->efi_parts
[i
].p_size
);
1199 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
) {
1202 if (vtoc
->efi_parts
[i
].p_tag
== V_RESERVED
) {
1203 if (resv_part
!= -1) {
1204 (void) fprintf(stderr
,
1205 "found duplicate reserved partition at "
1209 if (vtoc
->efi_parts
[i
].p_size
!= EFI_MIN_RESV_SIZE
)
1210 (void) fprintf(stderr
,
1211 "Warning: reserved partition size must "
1212 "be %d sectors\n", EFI_MIN_RESV_SIZE
);
1214 if ((vtoc
->efi_parts
[i
].p_start
< vtoc
->efi_first_u_lba
) ||
1215 (vtoc
->efi_parts
[i
].p_start
> vtoc
->efi_last_u_lba
)) {
1216 (void) fprintf(stderr
,
1217 "Partition %d starts at %llu\n",
1219 vtoc
->efi_parts
[i
].p_start
);
1220 (void) fprintf(stderr
,
1221 "It must be between %llu and %llu.\n",
1222 vtoc
->efi_first_u_lba
,
1223 vtoc
->efi_last_u_lba
);
1225 if ((vtoc
->efi_parts
[i
].p_start
+
1226 vtoc
->efi_parts
[i
].p_size
<
1227 vtoc
->efi_first_u_lba
) ||
1228 (vtoc
->efi_parts
[i
].p_start
+
1229 vtoc
->efi_parts
[i
].p_size
>
1230 vtoc
->efi_last_u_lba
+ 1)) {
1231 (void) fprintf(stderr
,
1232 "Partition %d ends at %llu\n",
1234 vtoc
->efi_parts
[i
].p_start
+
1235 vtoc
->efi_parts
[i
].p_size
);
1236 (void) fprintf(stderr
,
1237 "It must be between %llu and %llu.\n",
1238 vtoc
->efi_first_u_lba
,
1239 vtoc
->efi_last_u_lba
);
1242 for (j
= 0; j
< vtoc
->efi_nparts
; j
++) {
1243 isize
= vtoc
->efi_parts
[i
].p_size
;
1244 jsize
= vtoc
->efi_parts
[j
].p_size
;
1245 istart
= vtoc
->efi_parts
[i
].p_start
;
1246 jstart
= vtoc
->efi_parts
[j
].p_start
;
1247 if ((i
!= j
) && (isize
!= 0) && (jsize
!= 0)) {
1248 endsect
= jstart
+ jsize
-1;
1249 if ((jstart
<= istart
) &&
1250 (istart
<= endsect
)) {
1252 (void) fprintf(stderr
,
1253 "label error: EFI Labels do not "
1254 "support overlapping partitions\n");
1256 (void) fprintf(stderr
,
1257 "Partition %d overlaps partition "
1264 /* make sure there is a reserved partition */
1265 if (resv_part
== -1) {
1266 (void) fprintf(stderr
,
1267 "no reserved partition found\n");
1272 * We need to get information necessary to construct a *new* efi
1276 efi_auto_sense(int fd
, struct dk_gpt
**vtoc
)
1282 * Now build the default partition table
1284 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, vtoc
) != 0) {
1286 (void) fprintf(stderr
, "efi_alloc_and_init failed.\n");
1291 for (i
= 0; i
< min((*vtoc
)->efi_nparts
, V_NUMPAR
); i
++) {
1292 (*vtoc
)->efi_parts
[i
].p_tag
= default_vtoc_map
[i
].p_tag
;
1293 (*vtoc
)->efi_parts
[i
].p_flag
= default_vtoc_map
[i
].p_flag
;
1294 (*vtoc
)->efi_parts
[i
].p_start
= 0;
1295 (*vtoc
)->efi_parts
[i
].p_size
= 0;
1298 * Make constants first
1299 * and variable partitions later
1302 /* root partition - s0 128 MB */
1303 (*vtoc
)->efi_parts
[0].p_start
= 34;
1304 (*vtoc
)->efi_parts
[0].p_size
= 262144;
1306 /* partition - s1 128 MB */
1307 (*vtoc
)->efi_parts
[1].p_start
= 262178;
1308 (*vtoc
)->efi_parts
[1].p_size
= 262144;
1310 /* partition -s2 is NOT the Backup disk */
1311 (*vtoc
)->efi_parts
[2].p_tag
= V_UNASSIGNED
;
1313 /* partition -s6 /usr partition - HOG */
1314 (*vtoc
)->efi_parts
[6].p_start
= 524322;
1315 (*vtoc
)->efi_parts
[6].p_size
= (*vtoc
)->efi_last_u_lba
- 524322
1318 /* efi reserved partition - s9 16K */
1319 (*vtoc
)->efi_parts
[8].p_start
= (*vtoc
)->efi_last_u_lba
- (1024 * 16);
1320 (*vtoc
)->efi_parts
[8].p_size
= (1024 * 16);
1321 (*vtoc
)->efi_parts
[8].p_tag
= V_RESERVED
;