2 * Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org>
5 * GUID Partition Table (GPT) support. Based on UEFI Specs 2.3.1
6 * Chapter 5: GUID Partition Table (GPT) Disk Layout (Jun 27th, 2012).
7 * Some ideas and inspiration from GNU parted and gptfdisk.
14 #include <sys/utsname.h>
15 #include <sys/types.h>
31 #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */
32 #define GPT_HEADER_REVISION_V1_02 0x00010200
33 #define GPT_HEADER_REVISION_V1_00 0x00010000
34 #define GPT_HEADER_REVISION_V0_99 0x00009900
35 #define GPT_HEADER_MINSZ 92 /* bytes */
37 #define GPT_PMBR_LBA 0
38 #define GPT_MBR_PROTECTIVE 1
39 #define GPT_MBR_HYBRID 2
41 #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001
43 #define EFI_PMBR_OSTYPE 0xEE
44 #define MSDOS_MBR_SIGNATURE 0xAA55
45 #define GPT_PART_NAME_LEN (72 / sizeof(uint16_t))
46 #define GPT_NPARTITIONS 128
48 /* Globally unique identifier */
52 uint16_t time_hi_and_version
;
54 uint8_t clock_seq_low
;
59 /* only checking that the GUID is 0 is enough to verify an empty partition. */
60 #define GPT_UNUSED_ENTRY_GUID \
61 ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
62 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
64 /* Linux native partition type */
65 #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
71 uint64_t required_to_function
:1;
72 uint64_t no_blockio_protocol
:1;
73 uint64_t legacy_bios_bootable
:1;
75 uint64_t guid_secific
:16;
76 } __attribute__ ((packed
));
78 /* The GPT Partition entry array contains an array of GPT entries. */
80 struct gpt_guid type
; /* purpose and type of the partition */
81 struct gpt_guid partition_guid
;
85 uint16_t name
[GPT_PART_NAME_LEN
];
86 } __attribute__ ((packed
));
90 uint64_t signature
; /* header identification */
91 uint32_t revision
; /* header version */
92 uint32_t size
; /* in bytes */
93 uint32_t crc32
; /* header CRC checksum */
94 uint32_t reserved1
; /* must be 0 */
95 uint64_t my_lba
; /* LBA that contains this struct (LBA 1) */
96 uint64_t alternative_lba
; /* backup GPT header */
97 uint64_t first_usable_lba
; /* first usable logical block for partitions */
98 uint64_t last_usable_lba
; /* last usable logical block for partitions */
99 struct gpt_guid disk_guid
; /* unique disk identifier */
100 uint64_t partition_entry_lba
; /* stat LBA of the partition entry array */
101 uint32_t npartition_entries
; /* total partition entries - normally 128 */
102 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
103 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
104 uint8_t reserved2
[512 - 92]; /* must be 0 */
105 } __attribute__ ((packed
));
108 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
109 uint8_t start_head
; /* unused by EFI, pt start in CHS */
110 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
112 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
113 uint8_t end_head
; /* unused by EFI, pt end in CHS */
114 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
115 uint8_t end_track
; /* unused by EFI, pt end in CHS */
116 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
117 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
118 } __attribute__ ((packed
));
120 /* Protected MBR and legacy MBR share same structure */
121 struct gpt_legacy_mbr
{
122 uint8_t boot_code
[440];
123 uint32_t unique_mbr_signature
;
125 struct gpt_record partition_record
[4];
127 } __attribute__ ((packed
));
131 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
133 #define DEF_GUID(_u, _n) \
139 static struct fdisk_parttype gpt_parttypes
[] =
142 DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")),
144 DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")),
145 /* Hah!IdontneedEFI */
146 DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot partition")),
149 DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
150 DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
151 DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")),
152 DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")),
153 DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery environment")),
154 DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")),
157 DEF_GUID("75894C1E-3AEB-11D3-B7C1-7B03A0000000", N_("HP-UX data partition")),
158 DEF_GUID("E2A1E728-32E3-11D6-A682-7B03A0000000", N_("HP-UX service partition")),
161 DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")),
162 DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")),
163 DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")),
164 DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")),
165 DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")),
168 DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")),
169 DEF_GUID("83BD6B9D-7F41-11DC-BE0B-001560B84F0F", N_("FreeBSD boot")),
170 DEF_GUID("516E7CB5-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD swap")),
171 DEF_GUID("516E7CB6-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD UFS")),
172 DEF_GUID("516E7CBA-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD ZFS")),
173 DEF_GUID("516E7CB8-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD Vinum")),
176 DEF_GUID("48465300-0000-11AA-AA11-00306543ECAC", N_("Apple HFS/HFS+")),
177 DEF_GUID("55465300-0000-11AA-AA11-00306543ECAC", N_("Apple UFS")),
178 DEF_GUID("52414944-0000-11AA-AA11-00306543ECAC", N_("Apple RAID")),
179 DEF_GUID("52414944-5F4F-11AA-AA11-00306543ECAC", N_("Apple RAID offline")),
180 DEF_GUID("426F6F74-0000-11AA-AA11-00306543ECAC", N_("Apple boot")),
181 DEF_GUID("4C616265-6C00-11AA-AA11-00306543ECAC", N_("Apple label")),
182 DEF_GUID("5265636F-7665-11AA-AA11-00306543ECAC", N_("Apple TV recovery")),
183 DEF_GUID("53746F72-6167-11AA-AA11-00306543ECAC", N_("Apple Core storage")),
186 DEF_GUID("6A82CB45-1DD2-11B2-99A6-080020736631", N_("Solaris boot")),
187 DEF_GUID("6A85CF4D-1DD2-11B2-99A6-080020736631", N_("Solaris root")),
188 /* same as Apple ZFS */
189 DEF_GUID("6A898CC3-1DD2-11B2-99A6-080020736631", N_("Solaris /usr & Apple ZFS")),
190 DEF_GUID("6A87C46F-1DD2-11B2-99A6-080020736631", N_("Solaris swap")),
191 DEF_GUID("6A8B642B-1DD2-11B2-99A6-080020736631", N_("Solaris backup")),
192 DEF_GUID("6A8EF2E9-1DD2-11B2-99A6-080020736631", N_("Solaris /var")),
193 DEF_GUID("6A90BA39-1DD2-11B2-99A6-080020736631", N_("Solaris /home")),
194 DEF_GUID("6A9283A5-1DD2-11B2-99A6-080020736631", N_("Solaris alternate sector")),
195 DEF_GUID("6A945A3B-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 1")),
196 DEF_GUID("6A9630D1-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 2")),
197 DEF_GUID("6A980767-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 3")),
198 DEF_GUID("6A96237F-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 4")),
199 DEF_GUID("6A8D2AC7-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 5")),
202 DEF_GUID("49F48D32-B10E-11DC-B99B-0019D1879648", N_("NetBSD swap")),
203 DEF_GUID("49F48D5A-B10E-11DC-B99B-0019D1879648", N_("NetBSD FFS")),
204 DEF_GUID("49F48D82-B10E-11DC-B99B-0019D1879648", N_("NetBSD LFS")),
205 DEF_GUID("2DB519C4-B10E-11DC-B99B-0019D1879648", N_("NetBSD concatenated")),
206 DEF_GUID("2DB519EC-B10E-11DC-B99B-0019D1879648", N_("NetBSD encrypted")),
207 DEF_GUID("49F48DAA-B10E-11DC-B99B-0019D1879648", N_("NetBSD RAID")),
210 DEF_GUID("FE3A2A5D-4F32-41A7-B725-ACCC3285A309", N_("ChromeOS kernel")),
211 DEF_GUID("3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", N_("ChromeOS root fs")),
212 DEF_GUID("2E0A753D-9E48-43B0-8337-B15192CB1B5E", N_("ChromeOS reserved")),
215 DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")),
216 DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")),
217 DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")),
218 DEF_GUID("0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
219 DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")),
220 DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")),
223 /* gpt_entry macros */
224 #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start)
225 #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end)
228 * in-memory fdisk GPT stuff
230 struct fdisk_gpt_label
{
231 struct fdisk_label head
; /* generic part */
233 /* gpt specific part */
234 struct gpt_header
*pheader
; /* primary header */
235 struct gpt_header
*bheader
; /* backup header */
236 struct gpt_entry
*ents
; /* entries (partitions) */
239 static void gpt_deinit(struct fdisk_label
*lb
);
240 static struct fdisk_parttype
*gpt_get_partition_type(struct fdisk_context
*cxt
, size_t i
);
242 static inline struct fdisk_gpt_label
*self_label(struct fdisk_context
*cxt
)
244 return (struct fdisk_gpt_label
*) cxt
->label
;
248 * Returns the partition length, or 0 if end is before beginning.
250 static uint64_t gpt_partition_size(const struct gpt_entry
*e
)
252 uint64_t start
= gpt_partition_start(e
);
253 uint64_t end
= gpt_partition_end(e
);
255 return start
> end
? 0 : end
- start
+ 1ULL;
258 #ifdef CONFIG_LIBFDISK_DEBUG
259 /* prints UUID in the real byte order! */
260 static void dbgprint_uuid(const char *mesg
, struct gpt_guid
*guid
)
262 const unsigned char *uuid
= (unsigned char *) guid
;
264 fprintf(stderr
, "%s: "
265 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
267 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
271 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
276 * UUID is traditionally 16 byte big-endian array, except Intel EFI
277 * specification where the UUID is a structure of little-endian fields.
279 static void swap_efi_guid(struct gpt_guid
*uid
)
281 uid
->time_low
= swab32(uid
->time_low
);
282 uid
->time_mid
= swab16(uid
->time_mid
);
283 uid
->time_hi_and_version
= swab16(uid
->time_hi_and_version
);
286 static int string_to_guid(const char *in
, struct gpt_guid
*guid
)
288 if (uuid_parse(in
, (unsigned char *) guid
)) /* BE */
290 swap_efi_guid(guid
); /* LE */
294 static char *guid_to_string(struct gpt_guid
*guid
, char *out
)
296 struct gpt_guid u
= *guid
; /* LE */
298 swap_efi_guid(&u
); /* BE */
299 uuid_unparse_upper((unsigned char *) &u
, out
);
304 static const char *gpt_get_header_revstr(struct gpt_header
*header
)
309 switch (header
->revision
) {
310 case GPT_HEADER_REVISION_V1_02
:
312 case GPT_HEADER_REVISION_V1_00
:
314 case GPT_HEADER_REVISION_V0_99
:
324 static inline int partition_unused(const struct gpt_entry
*e
)
326 return !memcmp(&e
->type
, &GPT_UNUSED_ENTRY_GUID
,
327 sizeof(struct gpt_guid
));
331 * Builds a clean new valid protective MBR - will wipe out any existing data.
332 * Returns 0 on success, otherwise < 0 on error.
334 static int gpt_mknew_pmbr(struct fdisk_context
*cxt
)
336 struct gpt_legacy_mbr
*pmbr
= NULL
;
338 if (!cxt
|| !cxt
->firstsector
)
341 fdisk_zeroize_firstsector(cxt
);
343 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
345 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
346 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
347 pmbr
->partition_record
[0].start_sector
= 1;
348 pmbr
->partition_record
[0].end_head
= 0xFE;
349 pmbr
->partition_record
[0].end_sector
= 0xFF;
350 pmbr
->partition_record
[0].end_track
= 0xFF;
351 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
352 pmbr
->partition_record
[0].size_in_lba
=
353 cpu_to_le32(min((uint32_t) cxt
->total_sectors
- 1, 0xFFFFFFFF));
358 /* some universal differences between the headers */
359 static void gpt_mknew_header_common(struct fdisk_context
*cxt
,
360 struct gpt_header
*header
, uint64_t lba
)
365 header
->my_lba
= cpu_to_le64(lba
);
367 if (lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
) { /* primary */
368 header
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1);
369 header
->partition_entry_lba
= cpu_to_le64(2);
370 } else { /* backup */
371 uint64_t esz
= le32_to_cpu(header
->npartition_entries
) * sizeof(struct gpt_entry
);
372 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
374 header
->alternative_lba
= cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA
);
375 header
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
380 * Builds a new GPT header (at sector lba) from a backup header2.
381 * If building a primary header, then backup is the secondary, and vice versa.
383 * Always pass a new (zeroized) header to build upon as we don't
384 * explicitly zero-set some values such as CRCs and reserved.
386 * Returns 0 on success, otherwise < 0 on error.
388 static int gpt_mknew_header_from_bkp(struct fdisk_context
*cxt
,
389 struct gpt_header
*header
,
391 struct gpt_header
*header2
)
393 if (!cxt
|| !header
|| !header2
)
396 header
->signature
= header2
->signature
;
397 header
->revision
= header2
->revision
;
398 header
->size
= header2
->size
;
399 header
->npartition_entries
= header2
->npartition_entries
;
400 header
->sizeof_partition_entry
= header2
->sizeof_partition_entry
;
401 header
->first_usable_lba
= header2
->first_usable_lba
;
402 header
->last_usable_lba
= header2
->last_usable_lba
;
404 memcpy(&header
->disk_guid
,
405 &header2
->disk_guid
, sizeof(header2
->disk_guid
));
406 gpt_mknew_header_common(cxt
, header
, lba
);
412 * Builds a clean new GPT header (currently under revision 1.0).
414 * Always pass a new (zeroized) header to build upon as we don't
415 * explicitly zero-set some values such as CRCs and reserved.
417 * Returns 0 on success, otherwise < 0 on error.
419 static int gpt_mknew_header(struct fdisk_context
*cxt
,
420 struct gpt_header
*header
, uint64_t lba
)
422 uint64_t esz
= 0, first
, last
;
427 esz
= sizeof(struct gpt_entry
) * GPT_NPARTITIONS
/ cxt
->sector_size
;
429 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
430 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
431 header
->size
= cpu_to_le32(sizeof(struct gpt_header
));
434 * 128 partitions is the default. It can go behond this, however,
435 * we're creating a de facto header here, so no funny business.
437 header
->npartition_entries
= cpu_to_le32(GPT_NPARTITIONS
);
438 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
440 last
= cxt
->total_sectors
- 2 - esz
;
443 if (first
< cxt
->first_lba
&& cxt
->first_lba
< last
)
444 /* Align according to topology */
445 first
= cxt
->first_lba
;
447 header
->first_usable_lba
= cpu_to_le64(first
);
448 header
->last_usable_lba
= cpu_to_le64(last
);
450 gpt_mknew_header_common(cxt
, header
, lba
);
451 uuid_generate_random((unsigned char *) &header
->disk_guid
);
452 swap_efi_guid(&header
->disk_guid
);
458 * Checks if there is a valid protective MBR partition table.
459 * Returns 0 if it is invalid or failure. Otherwise, return
460 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection.
462 static int valid_pmbr(struct fdisk_context
*cxt
)
464 int i
, part
= 0, ret
= 0; /* invalid by default */
465 struct gpt_legacy_mbr
*pmbr
= NULL
;
468 if (!cxt
->firstsector
)
471 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
473 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
476 /* LBA of the GPT partition header */
477 if (pmbr
->partition_record
[0].starting_lba
!=
478 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
481 /* seems like a valid MBR was found, check DOS primary partitions */
482 for (i
= 0; i
< 4; i
++) {
483 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
485 * Ok, we at least know that there's a protective MBR,
486 * now check if there are other partition types for
490 ret
= GPT_MBR_PROTECTIVE
;
495 if (ret
!= GPT_MBR_PROTECTIVE
)
498 for (i
= 0 ; i
< 4; i
++) {
499 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
500 (pmbr
->partition_record
[i
].os_type
!= 0x00))
501 ret
= GPT_MBR_HYBRID
;
505 * Protective MBRs take up the lesser of the whole disk
506 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
507 * Some partitioning programs, nonetheless, choose to set
508 * the size to the maximum 32-bit limitation, disregarding
511 * Hybrid MBRs do not necessarily comply with this.
513 * Consider a bad value here to be a warning to support dd-ing
514 * an image from a smaller disk to a bigger disk.
516 if (ret
== GPT_MBR_PROTECTIVE
) {
517 sz_lba
= le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
518 if (sz_lba
!= (uint32_t) cxt
->total_sectors
- 1 && sz_lba
!= 0xFFFFFFFF) {
519 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%u != %u) "
520 "will be corrected by w(rite)."),
522 (uint32_t) cxt
->total_sectors
- 1);
523 fdisk_label_set_changed(cxt
->label
, 1);
530 static uint64_t last_lba(struct fdisk_context
*cxt
)
534 memset(&s
, 0, sizeof(s
));
535 if (fstat(cxt
->dev_fd
, &s
) == -1) {
536 fdisk_warn(cxt
, _("gpt: stat() failed"));
540 if (S_ISBLK(s
.st_mode
))
541 return cxt
->total_sectors
- 1;
542 else if (S_ISREG(s
.st_mode
)) {
543 uint64_t sectors
= s
.st_size
>> cxt
->sector_size
;
544 return (sectors
/ cxt
->sector_size
) - 1ULL;
546 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
550 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
551 void *buffer
, const size_t bytes
)
553 off_t offset
= lba
* cxt
->sector_size
;
555 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
557 return read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
561 /* Returns the GPT entry array */
562 static struct gpt_entry
*gpt_read_entries(struct fdisk_context
*cxt
,
563 struct gpt_header
*header
)
566 struct gpt_entry
*ret
= NULL
;
572 sz
= le32_to_cpu(header
->npartition_entries
) *
573 le32_to_cpu(header
->sizeof_partition_entry
);
578 offset
= le64_to_cpu(header
->partition_entry_lba
) *
581 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
583 if (sz
!= read(cxt
->dev_fd
, ret
, sz
))
593 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
)
595 return (crc32(~0L, buf
, len
) ^ ~0L);
599 * Recompute header and partition array 32bit CRC checksums.
600 * This function does not fail - if there's corruption, then it
601 * will be reported when checksuming it again (ie: probing or verify).
603 static void gpt_recompute_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
613 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
614 header
->crc32
= cpu_to_le32(crc
);
616 /* partition entry array CRC */
617 header
->partition_entry_array_crc32
= 0;
618 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
619 le32_to_cpu(header
->sizeof_partition_entry
);
621 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
622 header
->partition_entry_array_crc32
= cpu_to_le32(crc
);
626 * Compute the 32bit CRC checksum of the partition table header.
627 * Returns 1 if it is valid, otherwise 0.
629 static int gpt_check_header_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
631 uint32_t crc
, orgcrc
= le32_to_cpu(header
->crc32
);
634 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
635 header
->crc32
= cpu_to_le32(orgcrc
);
637 if (crc
== le32_to_cpu(header
->crc32
))
641 * If we have checksum mismatch it may be due to stale data,
642 * like a partition being added or deleted. Recompute the CRC again
643 * and make sure this is not the case.
646 gpt_recompute_crc(header
, ents
);
647 orgcrc
= le32_to_cpu(header
->crc32
);
649 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
650 header
->crc32
= cpu_to_le32(orgcrc
);
652 return crc
== le32_to_cpu(header
->crc32
);
659 * It initializes the partition entry array.
660 * Returns 1 if the checksum is valid, otherwise 0.
662 static int gpt_check_entryarr_crc(struct gpt_header
*header
,
663 struct gpt_entry
*ents
)
669 if (!header
|| !ents
)
672 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
673 le32_to_cpu(header
->sizeof_partition_entry
);
678 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
679 ret
= (crc
== le32_to_cpu(header
->partition_entry_array_crc32
));
684 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
687 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
689 fu
= le64_to_cpu(header
->first_usable_lba
);
690 lu
= le64_to_cpu(header
->last_usable_lba
);
692 /* check if first and last usable LBA make sense */
694 DBG(LABEL
, dbgprint("error: header last LBA is before first LBA"));
698 /* check if first and last usable LBAs with the disk's last LBA */
699 if (fu
> lastlba
|| lu
> lastlba
) {
700 DBG(LABEL
, dbgprint("error: header LBAs are after the disk's last LBA"));
704 /* the header has to be outside usable range */
705 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
706 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
707 DBG(LABEL
, dbgprint("error: header outside of usable range"));
716 /* Check if there is a valid header signature */
717 static int gpt_check_signature(struct gpt_header
*header
)
719 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
723 * Return the specified GPT Header, or NULL upon failure/invalid.
724 * Note that all tests must pass to ensure a valid header,
725 * we do not rely on only testing the signature for a valid probe.
727 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
729 struct gpt_entry
**_ents
)
731 struct gpt_header
*header
= NULL
;
732 struct gpt_entry
*ents
= NULL
;
738 header
= calloc(1, sizeof(*header
));
742 /* read and verify header */
743 if (read_lba(cxt
, lba
, header
, sizeof(struct gpt_header
)) != 0)
746 if (!gpt_check_signature(header
))
749 if (!gpt_check_header_crc(header
, NULL
))
752 /* read and verify entries */
753 ents
= gpt_read_entries(cxt
, header
);
757 if (!gpt_check_entryarr_crc(header
, ents
))
760 if (!gpt_check_lba_sanity(cxt
, header
))
763 /* valid header must be at MyLBA */
764 if (le64_to_cpu(header
->my_lba
) != lba
)
767 /* make sure header size is between 92 and sector size bytes */
768 hsz
= le32_to_cpu(header
->size
);
769 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
785 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
786 const char **name
, off_t
*offset
, size_t *size
)
788 struct fdisk_gpt_label
*gpt
;
803 *name
= _("GPT Header");
804 *offset
= GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
805 *size
= sizeof(struct gpt_header
);
808 *name
= _("GPT Entries");
809 gpt
= self_label(cxt
);
810 *offset
= le64_to_cpu(gpt
->pheader
->partition_entry_lba
) * cxt
->sector_size
;
811 *size
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
812 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
815 return 1; /* no more chunks */
824 * Returns the number of partitions that are in use.
826 static unsigned partitions_in_use(struct gpt_header
*header
, struct gpt_entry
*e
)
828 uint32_t i
, used
= 0;
833 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
834 if (!partition_unused(&e
[i
]))
841 * Check if a partition is too big for the disk (sectors).
842 * Returns the faulting partition number, otherwise 0.
844 static uint32_t partition_check_too_big(struct gpt_header
*header
,
845 struct gpt_entry
*e
, uint64_t sectors
)
849 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
850 if (partition_unused(&e
[i
]))
852 if (gpt_partition_end(&e
[i
]) >= sectors
)
860 * Check if a partition ends before it begins
861 * Returns the faulting partition number, otherwise 0.
863 static uint32_t partition_start_after_end(struct gpt_header
*header
, struct gpt_entry
*e
)
867 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
868 if (partition_unused(&e
[i
]))
870 if (gpt_partition_start(&e
[i
]) > gpt_partition_end(&e
[i
]))
878 * Check if partition e1 overlaps with partition e2
880 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
882 uint64_t start1
= gpt_partition_start(e1
);
883 uint64_t end1
= gpt_partition_end(e1
);
884 uint64_t start2
= gpt_partition_start(e2
);
885 uint64_t end2
= gpt_partition_end(e2
);
887 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
891 * Find any paritions that overlap.
893 static uint32_t partition_check_overlaps(struct gpt_header
*header
, struct gpt_entry
*e
)
897 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
898 for (j
= 0; j
< i
; j
++) {
899 if (partition_unused(&e
[i
]) ||
900 partition_unused(&e
[j
]))
902 if (partition_overlap(&e
[i
], &e
[j
])) {
903 DBG(LABEL
, dbgprint("GPT partitions overlap detected [%u vs. %u]", i
, j
));
912 * Find the first available block after the starting point; returns 0 if
913 * there are no available blocks left, or error. From gdisk.
915 static uint64_t find_first_available(struct gpt_header
*header
,
916 struct gpt_entry
*e
, uint64_t start
)
919 uint32_t i
, first_moved
= 0;
926 fu
= le64_to_cpu(header
->first_usable_lba
);
927 lu
= le64_to_cpu(header
->last_usable_lba
);
930 * Begin from the specified starting point or from the first usable
931 * LBA, whichever is greater...
933 first
= start
< fu
? fu
: start
;
936 * Now search through all partitions; if first is within an
937 * existing partition, move it to the next sector after that
938 * partition and repeat. If first was moved, set firstMoved
939 * flag; repeat until firstMoved is not set, so as to catch
940 * cases where partitions are out of sequential order....
944 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
945 if (partition_unused(&e
[i
]))
947 if (first
< gpt_partition_start(&e
[i
]))
949 if (first
<= gpt_partition_end(&e
[i
])) {
950 first
= gpt_partition_end(&e
[i
]) + 1;
954 } while (first_moved
== 1);
963 /* Returns last available sector in the free space pointed to by start. From gdisk. */
964 static uint64_t find_last_free(struct gpt_header
*header
,
965 struct gpt_entry
*e
, uint64_t start
)
968 uint64_t nearest_start
;
973 nearest_start
= le64_to_cpu(header
->last_usable_lba
);
975 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
976 uint64_t ps
= gpt_partition_start(&e
[i
]);
978 if (nearest_start
> ps
&& ps
> start
)
979 nearest_start
= ps
- 1;
982 return nearest_start
;
985 /* Returns the last free sector on the disk. From gdisk. */
986 static uint64_t find_last_free_sector(struct gpt_header
*header
,
989 uint32_t i
, last_moved
;
995 /* start by assuming the last usable LBA is available */
996 last
= le64_to_cpu(header
->last_usable_lba
);
999 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1000 if ((last
>= gpt_partition_start(&e
[i
])) &&
1001 (last
<= gpt_partition_end(&e
[i
]))) {
1002 last
= gpt_partition_start(&e
[i
]) - 1;
1006 } while (last_moved
== 1);
1012 * Finds the first available sector in the largest block of unallocated
1013 * space on the disk. Returns 0 if there are no available blocks left.
1016 static uint64_t find_first_in_largest(struct gpt_header
*header
, struct gpt_entry
*e
)
1018 uint64_t start
= 0, first_sect
, last_sect
;
1019 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1025 first_sect
= find_first_available(header
, e
, start
);
1026 if (first_sect
!= 0) {
1027 last_sect
= find_last_free(header
, e
, first_sect
);
1028 segment_size
= last_sect
- first_sect
+ 1;
1030 if (segment_size
> selected_size
) {
1031 selected_size
= segment_size
;
1032 selected_segment
= first_sect
;
1034 start
= last_sect
+ 1;
1036 } while (first_sect
!= 0);
1039 return selected_segment
;
1043 * Find the total number of free sectors, the number of segments in which
1044 * they reside, and the size of the largest of those segments. From gdisk.
1046 static uint64_t get_free_sectors(struct fdisk_context
*cxt
, struct gpt_header
*header
,
1047 struct gpt_entry
*e
, uint32_t *nsegments
,
1048 uint64_t *largest_segment
)
1051 uint64_t first_sect
, last_sect
;
1052 uint64_t largest_seg
= 0, segment_sz
;
1053 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1055 if (!cxt
->total_sectors
)
1059 first_sect
= find_first_available(header
, e
, start
);
1061 last_sect
= find_last_free(header
, e
, first_sect
);
1062 segment_sz
= last_sect
- first_sect
+ 1;
1064 if (segment_sz
> largest_seg
)
1065 largest_seg
= segment_sz
;
1066 totfound
+= segment_sz
;
1068 start
= last_sect
+ 1;
1070 } while (first_sect
);
1075 if (largest_segment
)
1076 *largest_segment
= largest_seg
;
1081 static int gpt_probe_label(struct fdisk_context
*cxt
)
1084 struct fdisk_gpt_label
*gpt
;
1088 assert(fdisk_is_disklabel(cxt
, GPT
));
1090 gpt
= self_label(cxt
);
1092 mbr_type
= valid_pmbr(cxt
);
1096 DBG(LABEL
, dbgprint("found a %s MBR", mbr_type
== GPT_MBR_PROTECTIVE
?
1097 "protective" : "hybrid"));
1099 /* primary header */
1100 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1104 * TODO: If the primary GPT is corrupt, we must check the last LBA of the
1105 * device to see if it has a valid GPT Header and point to a valid GPT
1106 * Partition Entry Array.
1107 * If it points to a valid GPT Partition Entry Array, then software should
1108 * restore the primary GPT if allowed by platform policy settings.
1110 * For now we just abort GPT probing!
1112 if (!gpt
->pheader
|| !gpt
->ents
)
1115 /* OK, probing passed, now initialize backup header and fdisk variables. */
1116 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), NULL
);
1118 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1119 cxt
->label
->nparts_cur
= partitions_in_use(gpt
->pheader
, gpt
->ents
);
1122 DBG(LABEL
, dbgprint("GPT probe failed"));
1123 gpt_deinit(cxt
->label
);
1128 * Stolen from libblkid - can be removed once partition semantics
1129 * are added to the fdisk API.
1131 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1135 size_t i
, j
, len
= count
;
1137 dest
= calloc(1, count
);
1141 for (j
= i
= 0; i
+ 2 <= count
; i
+= 2) {
1142 /* always little endian */
1143 c
= (src
[i
+1] << 8) | src
[i
];
1147 } else if (c
< 0x80) {
1150 dest
[j
++] = (uint8_t) c
;
1151 } else if (c
< 0x800) {
1154 dest
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
1155 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1159 dest
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
1160 dest
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
1161 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1170 * List label partitions.
1171 * This function must currently exist to comply with standard fdisk
1172 * requirements, but once partition semantics are added to the fdisk
1173 * API it can be removed for custom implementation (see gpt_label struct).
1175 static int gpt_list_disklabel(struct fdisk_context
*cxt
)
1177 int rc
, trunc
= TT_FL_TRUNC
;
1179 struct fdisk_gpt_label
*gpt
;
1180 struct gpt_header
*h
;
1183 struct tt
*tb
= NULL
;
1187 assert(fdisk_is_disklabel(cxt
, GPT
));
1189 gpt
= self_label(cxt
);
1191 fu
= le64_to_cpu(gpt
->pheader
->first_usable_lba
);
1192 lu
= le64_to_cpu(gpt
->pheader
->last_usable_lba
);
1194 tb
= tt_new_table(TT_FL_FREEDATA
);
1198 /* don't trunc anything in expert mode */
1199 if (fdisk_context_display_details(cxt
)) {
1201 fdisk_colon(cxt
, _("First LBA: %ju"), h
->first_usable_lba
);
1202 fdisk_colon(cxt
, _("Last LBA: %ju"), h
->last_usable_lba
);
1203 fdisk_colon(cxt
, _("Alternative LBA: %ju"), h
->alternative_lba
);
1204 fdisk_colon(cxt
, _("Partitions entries LBA: %ju"), h
->partition_entry_lba
);
1205 fdisk_colon(cxt
, _("Allocated partition entries: %ju"), (uintmax_t) h
->npartition_entries
);
1207 tt_define_column(tb
, _("Device"), 0.1, 0);
1208 tt_define_column(tb
, _("Start"), 12, TT_FL_RIGHT
);
1209 tt_define_column(tb
, _("End"), 12, TT_FL_RIGHT
);
1210 tt_define_column(tb
, _("Size"), 6, TT_FL_RIGHT
);
1211 tt_define_column(tb
, _("Type"), 0.1, trunc
);
1213 if (fdisk_context_display_details(cxt
)) {
1214 tt_define_column(tb
, _("UUID"), 36, 0);
1215 tt_define_column(tb
, _("Name"), 0.2, trunc
);
1218 for (i
= 0; i
< le32_to_cpu(h
->npartition_entries
); i
++) {
1219 struct gpt_entry
*e
= &gpt
->ents
[i
];
1220 char *sizestr
= NULL
, *p
;
1221 uint64_t start
= gpt_partition_start(e
);
1222 uint64_t size
= gpt_partition_size(e
);
1223 struct fdisk_parttype
*t
;
1227 if (partition_unused(&gpt
->ents
[i
]) || start
== 0)
1229 /* the partition has to inside usable range */
1230 if (start
< fu
|| start
+ size
- 1 > lu
)
1232 ln
= tt_add_line(tb
, NULL
);
1236 if (fdisk_context_display_details(cxt
) &&
1237 asprintf(&p
, "%ju", size
* cxt
->sector_size
) > 0)
1240 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
,
1241 size
* cxt
->sector_size
);
1242 t
= fdisk_get_partition_type(cxt
, i
);
1245 p
= fdisk_partname(cxt
->dev_path
, i
+ 1);
1247 tt_line_set_data(ln
, 0, p
);
1248 if (asprintf(&p
, "%ju", start
) > 0)
1249 tt_line_set_data(ln
, 1, p
);
1250 if (asprintf(&p
, "%ju", gpt_partition_end(e
)) > 0)
1251 tt_line_set_data(ln
, 2, p
);
1253 tt_line_set_data(ln
, 3, sizestr
);
1255 tt_line_set_data(ln
, 4, strdup(t
->name
));
1257 /* expert menu column(s) */
1258 if (fdisk_context_display_details(cxt
)) {
1259 char *name
= encode_to_utf8(
1260 (unsigned char *)e
->name
,
1263 if (guid_to_string(&e
->partition_guid
, u_str
))
1264 tt_line_set_data(ln
, 5, strdup(u_str
));
1266 tt_line_set_data(ln
, 6, name
);
1269 fdisk_warn_alignment(cxt
, start
, i
);
1270 fdisk_free_parttype(t
);
1273 rc
= fdisk_print_table(cxt
, tb
);
1281 * Returns 0 on success, or corresponding error otherwise.
1283 static int gpt_write_partitions(struct fdisk_context
*cxt
,
1284 struct gpt_header
*header
, struct gpt_entry
*ents
)
1286 off_t offset
= le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
;
1287 uint32_t nparts
= le32_to_cpu(header
->npartition_entries
);
1288 uint32_t totwrite
= nparts
* le32_to_cpu(header
->sizeof_partition_entry
);
1291 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1294 rc
= write(cxt
->dev_fd
, ents
, totwrite
);
1295 if (rc
> 0 && totwrite
== (uint32_t) rc
)
1302 * Write a GPT header to a specified LBA
1303 * Returns 0 on success, or corresponding error otherwise.
1305 static int gpt_write_header(struct fdisk_context
*cxt
,
1306 struct gpt_header
*header
, uint64_t lba
)
1308 off_t offset
= lba
* cxt
->sector_size
;
1310 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1312 if (cxt
->sector_size
==
1313 (size_t) write(cxt
->dev_fd
, header
, cxt
->sector_size
))
1320 * Write the protective MBR.
1321 * Returns 0 on success, or corresponding error otherwise.
1323 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
1326 struct gpt_legacy_mbr
*pmbr
= NULL
;
1329 assert(cxt
->firstsector
);
1331 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
1333 /* zero out the legacy partitions */
1334 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
1336 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
1337 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
1338 pmbr
->partition_record
[0].start_sector
= 1;
1339 pmbr
->partition_record
[0].end_head
= 0xFE;
1340 pmbr
->partition_record
[0].end_sector
= 0xFF;
1341 pmbr
->partition_record
[0].end_track
= 0xFF;
1342 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
1345 * Set size_in_lba to the size of the disk minus one. If the size of the disk
1346 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
1348 if (cxt
->total_sectors
- 1 > 0xFFFFFFFFULL
)
1349 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
1351 pmbr
->partition_record
[0].size_in_lba
=
1352 cpu_to_le32(cxt
->total_sectors
- 1UL);
1354 offset
= GPT_PMBR_LBA
* cxt
->sector_size
;
1355 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1358 /* pMBR covers the first sector (LBA) of the disk */
1359 if (write_all(cxt
->dev_fd
, pmbr
, cxt
->sector_size
))
1367 * Writes in-memory GPT and pMBR data to disk.
1368 * Returns 0 if successful write, otherwise, a corresponding error.
1369 * Any indication of error will abort the operation.
1371 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
1373 struct fdisk_gpt_label
*gpt
;
1377 assert(fdisk_is_disklabel(cxt
, GPT
));
1379 gpt
= self_label(cxt
);
1381 /* we do not want to mess up hybrid MBRs by creating a valid pmbr */
1382 if (valid_pmbr(cxt
) == GPT_MBR_HYBRID
)
1385 /* check that disk is big enough to handle the backup header */
1386 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
1389 /* check that the backup header is properly placed */
1390 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1)
1391 /* TODO: correct this (with user authorization) and write */
1394 if (partition_check_overlaps(gpt
->pheader
, gpt
->ents
))
1397 /* recompute CRCs for both headers */
1398 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1399 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1402 * UEFI requires writing in this specific order:
1403 * 1) backup partition tables
1404 * 2) backup GPT header
1405 * 3) primary partition tables
1406 * 4) primary GPT header
1409 * If any write fails, we abort the rest.
1411 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
1413 if (gpt_write_header(cxt
, gpt
->bheader
,
1414 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
1416 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
1418 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
1420 if (gpt_write_pmbr(cxt
) != 0)
1423 DBG(LABEL
, dbgprint("GPT write success"));
1426 DBG(LABEL
, dbgprint("GPT write failed: incorrect input"));
1430 DBG(LABEL
, dbgprint("GPT write failed: %m"));
1435 * Verify data integrity and report any found problems for:
1436 * - primary and backup header validations
1437 * - paritition validations
1439 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
1443 struct fdisk_gpt_label
*gpt
;
1447 assert(fdisk_is_disklabel(cxt
, GPT
));
1449 gpt
= self_label(cxt
);
1451 if (!gpt
|| !gpt
->bheader
) {
1453 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
1456 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
1458 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
1460 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
1462 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
1465 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
1467 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
1470 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
1472 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
1474 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
1476 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
1479 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
1481 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
1483 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
1485 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
1488 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
1490 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
1494 * if the GPT is the primary table, check the alternateLBA
1495 * to see if it is a valid GPT
1497 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
1498 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
1500 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
1503 ptnum
= partition_check_overlaps(gpt
->pheader
, gpt
->ents
);
1506 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
1510 ptnum
= partition_check_too_big(gpt
->pheader
, gpt
->ents
, cxt
->total_sectors
);
1513 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
1517 ptnum
= partition_start_after_end(gpt
->pheader
, gpt
->ents
);
1520 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
1524 if (!nerror
) { /* yay :-) */
1525 uint32_t nsegments
= 0;
1526 uint64_t free_sectors
= 0, largest_segment
= 0;
1528 fdisk_info(cxt
, _("No errors detected."));
1529 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
1530 fdisk_info(cxt
, _("Using %u out of %d partitions."),
1531 partitions_in_use(gpt
->pheader
, gpt
->ents
),
1532 le32_to_cpu(gpt
->pheader
->npartition_entries
));
1534 free_sectors
= get_free_sectors(cxt
, gpt
->pheader
, gpt
->ents
,
1535 &nsegments
, &largest_segment
);
1537 P_("A total of %ld free sectors is available in %d segment.",
1538 "A total of %ld free sectors is available in %d segments "
1539 "(the largest is %ld).", nsegments
),
1540 free_sectors
, nsegments
, largest_segment
);
1543 P_("%d error detected.", "%d errors detected.", nerror
),
1549 /* Delete a single GPT partition, specified by partnum. */
1550 static int gpt_delete_partition(struct fdisk_context
*cxt
,
1553 struct fdisk_gpt_label
*gpt
;
1557 assert(fdisk_is_disklabel(cxt
, GPT
));
1559 gpt
= self_label(cxt
);
1561 if (partnum
>= cxt
->label
->nparts_max
1562 || partition_unused(&gpt
->ents
[partnum
]))
1565 /* hasta la vista, baby! */
1566 memset(&gpt
->ents
[partnum
], 0, sizeof(struct gpt_entry
));
1567 if (!partition_unused(&gpt
->ents
[partnum
]))
1570 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1571 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1572 cxt
->label
->nparts_cur
--;
1573 fdisk_label_set_changed(cxt
->label
, 1);
1579 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
1582 DBG(LABEL
, dbgprint_uuid("new type", &(e
->type
)));
1586 * Create a new GPT partition entry, specified by partnum, and with a range
1587 * of fsect to lsenct sectors, of type t.
1588 * Returns 0 on success, or negative upon failure.
1590 static int gpt_create_new_partition(struct fdisk_context
*cxt
,
1591 size_t partnum
, uint64_t fsect
, uint64_t lsect
,
1592 struct gpt_guid
*type
,
1593 struct gpt_entry
*entries
)
1595 struct gpt_entry
*e
= NULL
;
1596 struct fdisk_gpt_label
*gpt
;
1600 assert(fdisk_is_disklabel(cxt
, GPT
));
1602 gpt
= self_label(cxt
);
1604 if (fsect
> lsect
|| partnum
>= cxt
->label
->nparts_max
)
1607 e
= calloc(1, sizeof(*e
));
1610 e
->lba_end
= cpu_to_le64(lsect
);
1611 e
->lba_start
= cpu_to_le64(fsect
);
1613 gpt_entry_set_type(e
, type
);
1616 * Any time a new partition entry is created a new GUID must be
1617 * generated for that partition, and every partition is guaranteed
1618 * to have a unique GUID.
1620 uuid_generate_random((unsigned char *) &e
->partition_guid
);
1621 swap_efi_guid(&e
->partition_guid
);
1623 memcpy(&entries
[partnum
], e
, sizeof(*e
));
1625 gpt_recompute_crc(gpt
->pheader
, entries
);
1626 gpt_recompute_crc(gpt
->bheader
, entries
);
1632 /* Performs logical checks to add a new partition entry */
1633 static int gpt_add_partition(
1634 struct fdisk_context
*cxt
,
1636 struct fdisk_parttype
*t
)
1638 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
1639 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
1640 uint64_t dflt_f
, dflt_l
; /* largest segment (default) */
1641 struct gpt_guid
typeid;
1642 struct fdisk_gpt_label
*gpt
;
1643 struct gpt_header
*pheader
;
1644 struct gpt_entry
*ents
;
1645 struct fdisk_ask
*ask
= NULL
;
1650 assert(fdisk_is_disklabel(cxt
, GPT
));
1652 gpt
= self_label(cxt
);
1654 if (partnum
>= cxt
->label
->nparts_max
)
1657 pheader
= gpt
->pheader
;
1660 if (!partition_unused(&ents
[partnum
])) {
1661 fdisk_warnx(cxt
, _("Partition %zd is already defined. "
1662 "Delete it before re-adding it."), partnum
+1);
1665 if (le32_to_cpu(pheader
->npartition_entries
) ==
1666 partitions_in_use(pheader
, ents
)) {
1667 fdisk_warnx(cxt
, _("All partitions are already in use."));
1671 if (!get_free_sectors(cxt
, pheader
, ents
, NULL
, NULL
)) {
1672 fdisk_warnx(cxt
, _("No free sectors available."));
1676 disk_f
= find_first_available(pheader
, ents
, 0);
1677 disk_l
= find_last_free_sector(pheader
, ents
);
1679 /* the default is the largest free space */
1680 dflt_f
= find_first_in_largest(pheader
, ents
);
1681 dflt_l
= find_last_free(pheader
, ents
, dflt_f
);
1683 /* align the default in range <dflt_f,dflt_l>*/
1684 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
1686 string_to_guid(t
&& t
->typestr
? t
->typestr
: GPT_DEFAULT_ENTRY_TYPE
, &typeid);
1688 /* get user input for first and last sectors of the new partition */
1691 ask
= fdisk_new_ask();
1693 fdisk_reset_ask(ask
);
1696 fdisk_ask_set_query(ask
, _("First sector"));
1697 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
1698 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
1699 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
1700 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
1702 rc
= fdisk_do_ask(cxt
, ask
);
1706 user_f
= fdisk_ask_number_get_result(ask
);
1707 if (user_f
!= find_first_available(pheader
, ents
, user_f
)) {
1708 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
1712 fdisk_reset_ask(ask
);
1715 dflt_l
= find_last_free(pheader
, ents
, user_f
);
1717 fdisk_ask_set_query(ask
, _("Last sector, +sectors or +size{K,M,G,T,P}"));
1718 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
1719 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
1720 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
1721 fdisk_ask_number_set_high(ask
, dflt_l
); /* maximal */
1722 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
1723 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
1725 rc
= fdisk_do_ask(cxt
, ask
);
1729 user_l
= fdisk_ask_number_get_result(ask
);
1730 if (fdisk_ask_number_is_relative(ask
))
1731 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1732 if (user_l
> user_f
&& user_l
<= disk_l
)
1736 if (gpt_create_new_partition(cxt
, partnum
,
1737 user_f
, user_l
, &typeid, ents
) != 0)
1738 fdisk_warnx(cxt
, _("Could not create partition %zd"), partnum
+ 1);
1740 struct fdisk_parttype
*t
;
1742 cxt
->label
->nparts_cur
++;
1743 fdisk_label_set_changed(cxt
->label
, 1);
1745 t
= gpt_get_partition_type(cxt
, partnum
);
1746 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
1747 fdisk_free_parttype(t
);
1752 fdisk_free_ask(ask
);
1757 * Create a new GPT disklabel - destroys any previous data.
1759 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
1764 struct fdisk_gpt_label
*gpt
;
1768 assert(fdisk_is_disklabel(cxt
, GPT
));
1770 gpt
= self_label(cxt
);
1772 /* label private stuff has to be empty, see gpt_deinit() */
1773 assert(gpt
->pheader
== NULL
);
1774 assert(gpt
->bheader
== NULL
);
1777 * When no header, entries or pmbr is set, we're probably
1778 * dealing with a new, empty disk - so always allocate memory
1779 * to deal with the data structures whatever the case is.
1781 rc
= gpt_mknew_pmbr(cxt
);
1786 gpt
->pheader
= calloc(1, sizeof(*gpt
->pheader
));
1787 if (!gpt
->pheader
) {
1791 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
1795 /* backup ("copy" primary) */
1796 gpt
->bheader
= calloc(1, sizeof(*gpt
->bheader
));
1797 if (!gpt
->bheader
) {
1801 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
1802 last_lba(cxt
), gpt
->pheader
);
1806 esz
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
1807 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
1808 gpt
->ents
= calloc(1, esz
);
1813 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1814 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1816 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1817 cxt
->label
->nparts_cur
= 0;
1819 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
1820 fdisk_label_set_changed(cxt
->label
, 1);
1821 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
1822 _("Created a new GPT disklabel (GUID: %s)."), str
);
1827 static int gpt_get_disklabel_id(struct fdisk_context
*cxt
, char **id
)
1829 struct fdisk_gpt_label
*gpt
;
1835 assert(fdisk_is_disklabel(cxt
, GPT
));
1837 gpt
= self_label(cxt
);
1838 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
1846 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
)
1848 struct fdisk_gpt_label
*gpt
;
1849 struct gpt_guid uuid
;
1850 char *str
, *old
, *new;
1855 assert(fdisk_is_disklabel(cxt
, GPT
));
1857 gpt
= self_label(cxt
);
1858 if (fdisk_ask_string(cxt
,
1859 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str
))
1862 rc
= string_to_guid(str
, &uuid
);
1866 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
1870 gpt_get_disklabel_id(cxt
, &old
);
1872 gpt
->pheader
->disk_guid
= uuid
;
1873 gpt
->bheader
->disk_guid
= uuid
;
1875 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1876 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1878 gpt_get_disklabel_id(cxt
, &new);
1880 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
1881 _("Disk identifier changed from %s to %s."), old
, new);
1885 fdisk_label_set_changed(cxt
->label
, 1);
1890 static struct fdisk_parttype
*gpt_get_partition_type(
1891 struct fdisk_context
*cxt
,
1894 struct fdisk_parttype
*t
;
1896 struct fdisk_gpt_label
*gpt
;
1900 assert(fdisk_is_disklabel(cxt
, GPT
));
1902 gpt
= self_label(cxt
);
1904 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1907 guid_to_string(&gpt
->ents
[i
].type
, str
);
1908 t
= fdisk_get_parttype_from_string(cxt
, str
);
1910 t
= fdisk_new_unknown_parttype(0, str
);
1916 static int gpt_set_partition_type(
1917 struct fdisk_context
*cxt
,
1919 struct fdisk_parttype
*t
)
1921 struct gpt_guid uuid
;
1922 struct fdisk_gpt_label
*gpt
;
1926 assert(fdisk_is_disklabel(cxt
, GPT
));
1928 gpt
= self_label(cxt
);
1929 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
)
1930 || !t
|| !t
->typestr
|| string_to_guid(t
->typestr
, &uuid
) != 0)
1933 gpt_entry_set_type(&gpt
->ents
[i
], &uuid
);
1934 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1935 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1937 fdisk_label_set_changed(cxt
->label
, 1);
1941 static int gpt_get_partition_status(
1942 struct fdisk_context
*cxt
,
1946 struct fdisk_gpt_label
*gpt
;
1947 struct gpt_entry
*e
;
1951 assert(fdisk_is_disklabel(cxt
, GPT
));
1953 gpt
= self_label(cxt
);
1955 if (!status
|| (uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1959 *status
= FDISK_PARTSTAT_NONE
;
1961 if (!partition_unused(e
) || gpt_partition_start(e
))
1962 *status
= FDISK_PARTSTAT_USED
;
1967 int fdisk_gpt_partition_set_uuid(struct fdisk_context
*cxt
, size_t i
)
1969 struct fdisk_gpt_label
*gpt
;
1970 struct gpt_entry
*e
;
1971 struct gpt_guid uuid
;
1972 char *str
, new_u
[37], old_u
[37];
1977 assert(fdisk_is_disklabel(cxt
, GPT
));
1979 DBG(LABEL
, dbgprint("UUID change requested partno=%zd", i
));
1981 gpt
= self_label(cxt
);
1983 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1986 if (fdisk_ask_string(cxt
,
1987 _("New UUID (in 8-4-4-4-12 format)"), &str
))
1990 rc
= string_to_guid(str
, &uuid
);
1994 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2000 guid_to_string(&e
->partition_guid
, old_u
);
2001 guid_to_string(&uuid
, new_u
);
2003 e
->partition_guid
= uuid
;
2004 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2005 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2006 fdisk_label_set_changed(cxt
->label
, 1);
2008 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2009 _("Partition UUID changed from %s to %s."),
2014 int fdisk_gpt_partition_set_name(struct fdisk_context
*cxt
, size_t i
)
2016 struct fdisk_gpt_label
*gpt
;
2017 struct gpt_entry
*e
;
2018 char *str
, *old
, name
[GPT_PART_NAME_LEN
] = { 0 };
2023 assert(fdisk_is_disklabel(cxt
, GPT
));
2025 DBG(LABEL
, dbgprint("NAME change requested partno=%zd", i
));
2027 gpt
= self_label(cxt
);
2029 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2032 if (fdisk_ask_string(cxt
, _("New name"), &str
))
2036 old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
2040 if (sz
> GPT_PART_NAME_LEN
)
2041 sz
= GPT_PART_NAME_LEN
;
2042 memcpy(name
, str
, sz
);
2045 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
2046 e
->name
[i
] = cpu_to_le16((uint16_t) name
[i
]);
2048 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2049 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2051 fdisk_label_set_changed(cxt
->label
, 1);
2053 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2054 _("Partition name changed from '%s' to '%.*s'."),
2055 old
, (int) GPT_PART_NAME_LEN
, str
);
2064 * Deinitialize fdisk-specific variables
2066 static void gpt_deinit(struct fdisk_label
*lb
)
2068 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
2078 gpt
->pheader
= NULL
;
2079 gpt
->bheader
= NULL
;
2082 static const struct fdisk_label_operations gpt_operations
=
2084 .probe
= gpt_probe_label
,
2085 .write
= gpt_write_disklabel
,
2086 .verify
= gpt_verify_disklabel
,
2087 .create
= gpt_create_disklabel
,
2088 .list
= gpt_list_disklabel
,
2089 .locate
= gpt_locate_disklabel
,
2090 .get_id
= gpt_get_disklabel_id
,
2091 .set_id
= gpt_set_disklabel_id
,
2093 .part_add
= gpt_add_partition
,
2094 .part_delete
= gpt_delete_partition
,
2095 .part_get_type
= gpt_get_partition_type
,
2096 .part_set_type
= gpt_set_partition_type
,
2098 .part_get_status
= gpt_get_partition_status
,
2100 .deinit
= gpt_deinit
2104 * allocates GPT in-memory stuff
2106 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
)
2108 struct fdisk_label
*lb
;
2109 struct fdisk_gpt_label
*gpt
;
2113 gpt
= calloc(1, sizeof(*gpt
));
2117 /* initialize generic part of the driver */
2118 lb
= (struct fdisk_label
*) gpt
;
2120 lb
->id
= FDISK_DISKLABEL_GPT
;
2121 lb
->op
= &gpt_operations
;
2122 lb
->parttypes
= gpt_parttypes
;
2123 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);