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
));
81 /* The GPT Partition entry array contains an array of GPT entries. */
83 struct gpt_guid type
; /* purpose and type of the partition */
84 struct gpt_guid partition_guid
;
88 uint16_t name
[GPT_PART_NAME_LEN
];
89 } __attribute__ ((packed
));
93 uint64_t signature
; /* header identification */
94 uint32_t revision
; /* header version */
95 uint32_t size
; /* in bytes */
96 uint32_t crc32
; /* header CRC checksum */
97 uint32_t reserved1
; /* must be 0 */
98 uint64_t my_lba
; /* LBA that contains this struct (LBA 1) */
99 uint64_t alternative_lba
; /* backup GPT header */
100 uint64_t first_usable_lba
; /* first usable logical block for partitions */
101 uint64_t last_usable_lba
; /* last usable logical block for partitions */
102 struct gpt_guid disk_guid
; /* unique disk identifier */
103 uint64_t partition_entry_lba
; /* stat LBA of the partition entry array */
104 uint32_t npartition_entries
; /* total partition entries - normally 128 */
105 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
106 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
107 uint8_t reserved2
[512 - 92]; /* must be 0 */
108 } __attribute__ ((packed
));
111 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
112 uint8_t start_head
; /* unused by EFI, pt start in CHS */
113 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
115 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
116 uint8_t end_head
; /* unused by EFI, pt end in CHS */
117 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
118 uint8_t end_track
; /* unused by EFI, pt end in CHS */
119 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
120 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
121 } __attribute__ ((packed
));
123 /* Protected MBR and legacy MBR share same structure */
124 struct gpt_legacy_mbr
{
125 uint8_t boot_code
[440];
126 uint32_t unique_mbr_signature
;
128 struct gpt_record partition_record
[4];
130 } __attribute__ ((packed
));
134 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
136 #define DEF_GUID(_u, _n) \
142 static struct fdisk_parttype gpt_parttypes
[] =
145 DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")),
147 DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")),
148 DEF_GUID("D3BFE2DE-3DAF-11DF-BA40-E3A556D89593", N_("Intel Fast Flash")),
150 /* Hah!IdontneedEFI */
151 DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot partition")),
154 DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
155 DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
156 DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")),
157 DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")),
158 DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery environment")),
159 DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")),
162 DEF_GUID("75894C1E-3AEB-11D3-B7C1-7B03A0000000", N_("HP-UX data partition")),
163 DEF_GUID("E2A1E728-32E3-11D6-A682-7B03A0000000", N_("HP-UX service partition")),
166 DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")),
167 DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")),
168 DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")),
169 DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")),
170 DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")),
171 DEF_GUID("933AC7E1-2EB4-4F13-B844-0E14E2AEF915", N_("Linux /home partition")),
174 DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")),
175 DEF_GUID("83BD6B9D-7F41-11DC-BE0B-001560B84F0F", N_("FreeBSD boot")),
176 DEF_GUID("516E7CB5-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD swap")),
177 DEF_GUID("516E7CB6-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD UFS")),
178 DEF_GUID("516E7CBA-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD ZFS")),
179 DEF_GUID("516E7CB8-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD Vinum")),
182 DEF_GUID("48465300-0000-11AA-AA11-00306543ECAC", N_("Apple HFS/HFS+")),
183 DEF_GUID("55465300-0000-11AA-AA11-00306543ECAC", N_("Apple UFS")),
184 DEF_GUID("52414944-0000-11AA-AA11-00306543ECAC", N_("Apple RAID")),
185 DEF_GUID("52414944-5F4F-11AA-AA11-00306543ECAC", N_("Apple RAID offline")),
186 DEF_GUID("426F6F74-0000-11AA-AA11-00306543ECAC", N_("Apple boot")),
187 DEF_GUID("4C616265-6C00-11AA-AA11-00306543ECAC", N_("Apple label")),
188 DEF_GUID("5265636F-7665-11AA-AA11-00306543ECAC", N_("Apple TV recovery")),
189 DEF_GUID("53746F72-6167-11AA-AA11-00306543ECAC", N_("Apple Core storage")),
192 DEF_GUID("6A82CB45-1DD2-11B2-99A6-080020736631", N_("Solaris boot")),
193 DEF_GUID("6A85CF4D-1DD2-11B2-99A6-080020736631", N_("Solaris root")),
194 /* same as Apple ZFS */
195 DEF_GUID("6A898CC3-1DD2-11B2-99A6-080020736631", N_("Solaris /usr & Apple ZFS")),
196 DEF_GUID("6A87C46F-1DD2-11B2-99A6-080020736631", N_("Solaris swap")),
197 DEF_GUID("6A8B642B-1DD2-11B2-99A6-080020736631", N_("Solaris backup")),
198 DEF_GUID("6A8EF2E9-1DD2-11B2-99A6-080020736631", N_("Solaris /var")),
199 DEF_GUID("6A90BA39-1DD2-11B2-99A6-080020736631", N_("Solaris /home")),
200 DEF_GUID("6A9283A5-1DD2-11B2-99A6-080020736631", N_("Solaris alternate sector")),
201 DEF_GUID("6A945A3B-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 1")),
202 DEF_GUID("6A9630D1-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 2")),
203 DEF_GUID("6A980767-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 3")),
204 DEF_GUID("6A96237F-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 4")),
205 DEF_GUID("6A8D2AC7-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 5")),
208 DEF_GUID("49F48D32-B10E-11DC-B99B-0019D1879648", N_("NetBSD swap")),
209 DEF_GUID("49F48D5A-B10E-11DC-B99B-0019D1879648", N_("NetBSD FFS")),
210 DEF_GUID("49F48D82-B10E-11DC-B99B-0019D1879648", N_("NetBSD LFS")),
211 DEF_GUID("2DB519C4-B10E-11DC-B99B-0019D1879648", N_("NetBSD concatenated")),
212 DEF_GUID("2DB519EC-B10E-11DC-B99B-0019D1879648", N_("NetBSD encrypted")),
213 DEF_GUID("49F48DAA-B10E-11DC-B99B-0019D1879648", N_("NetBSD RAID")),
216 DEF_GUID("FE3A2A5D-4F32-41A7-B725-ACCC3285A309", N_("ChromeOS kernel")),
217 DEF_GUID("3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", N_("ChromeOS root fs")),
218 DEF_GUID("2E0A753D-9E48-43B0-8337-B15192CB1B5E", N_("ChromeOS reserved")),
221 DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")),
222 DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")),
223 DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")),
224 DEF_GUID("0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
225 DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")),
226 DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")),
229 /* gpt_entry macros */
230 #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start)
231 #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end)
234 * in-memory fdisk GPT stuff
236 struct fdisk_gpt_label
{
237 struct fdisk_label head
; /* generic part */
239 /* gpt specific part */
240 struct gpt_header
*pheader
; /* primary header */
241 struct gpt_header
*bheader
; /* backup header */
242 struct gpt_entry
*ents
; /* entries (partitions) */
245 static void gpt_deinit(struct fdisk_label
*lb
);
247 static inline struct fdisk_gpt_label
*self_label(struct fdisk_context
*cxt
)
249 return (struct fdisk_gpt_label
*) cxt
->label
;
253 * Returns the partition length, or 0 if end is before beginning.
255 static uint64_t gpt_partition_size(const struct gpt_entry
*e
)
257 uint64_t start
= gpt_partition_start(e
);
258 uint64_t end
= gpt_partition_end(e
);
260 return start
> end
? 0 : end
- start
+ 1ULL;
263 #ifdef CONFIG_LIBFDISK_DEBUG
264 /* prints UUID in the real byte order! */
265 static void dbgprint_uuid(const char *mesg
, struct gpt_guid
*guid
)
267 const unsigned char *uuid
= (unsigned char *) guid
;
269 fprintf(stderr
, "%s: "
270 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
272 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
276 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
281 * UUID is traditionally 16 byte big-endian array, except Intel EFI
282 * specification where the UUID is a structure of little-endian fields.
284 static void swap_efi_guid(struct gpt_guid
*uid
)
286 uid
->time_low
= swab32(uid
->time_low
);
287 uid
->time_mid
= swab16(uid
->time_mid
);
288 uid
->time_hi_and_version
= swab16(uid
->time_hi_and_version
);
291 static int string_to_guid(const char *in
, struct gpt_guid
*guid
)
293 if (uuid_parse(in
, (unsigned char *) guid
)) /* BE */
295 swap_efi_guid(guid
); /* LE */
299 static char *guid_to_string(const struct gpt_guid
*guid
, char *out
)
301 struct gpt_guid u
= *guid
; /* LE */
303 swap_efi_guid(&u
); /* BE */
304 uuid_unparse_upper((unsigned char *) &u
, out
);
309 static struct fdisk_parttype
*gpt_partition_parttype(
310 struct fdisk_context
*cxt
,
311 const struct gpt_entry
*e
)
313 struct fdisk_parttype
*t
;
316 guid_to_string(&e
->type
, str
);
317 t
= fdisk_get_parttype_from_string(cxt
, str
);
318 return t
? : fdisk_new_unknown_parttype(0, str
);
323 static const char *gpt_get_header_revstr(struct gpt_header
*header
)
328 switch (header
->revision
) {
329 case GPT_HEADER_REVISION_V1_02
:
331 case GPT_HEADER_REVISION_V1_00
:
333 case GPT_HEADER_REVISION_V0_99
:
343 static inline int partition_unused(const struct gpt_entry
*e
)
345 return !memcmp(&e
->type
, &GPT_UNUSED_ENTRY_GUID
,
346 sizeof(struct gpt_guid
));
350 * Builds a clean new valid protective MBR - will wipe out any existing data.
351 * Returns 0 on success, otherwise < 0 on error.
353 static int gpt_mknew_pmbr(struct fdisk_context
*cxt
)
355 struct gpt_legacy_mbr
*pmbr
= NULL
;
357 if (!cxt
|| !cxt
->firstsector
)
360 fdisk_zeroize_firstsector(cxt
);
362 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
364 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
365 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
366 pmbr
->partition_record
[0].start_sector
= 1;
367 pmbr
->partition_record
[0].end_head
= 0xFE;
368 pmbr
->partition_record
[0].end_sector
= 0xFF;
369 pmbr
->partition_record
[0].end_track
= 0xFF;
370 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
371 pmbr
->partition_record
[0].size_in_lba
=
372 cpu_to_le32(min((uint32_t) cxt
->total_sectors
- 1, 0xFFFFFFFF));
377 /* some universal differences between the headers */
378 static void gpt_mknew_header_common(struct fdisk_context
*cxt
,
379 struct gpt_header
*header
, uint64_t lba
)
384 header
->my_lba
= cpu_to_le64(lba
);
386 if (lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
) { /* primary */
387 header
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1);
388 header
->partition_entry_lba
= cpu_to_le64(2);
389 } else { /* backup */
390 uint64_t esz
= le32_to_cpu(header
->npartition_entries
) * sizeof(struct gpt_entry
);
391 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
393 header
->alternative_lba
= cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA
);
394 header
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
399 * Builds a new GPT header (at sector lba) from a backup header2.
400 * If building a primary header, then backup is the secondary, and vice versa.
402 * Always pass a new (zeroized) header to build upon as we don't
403 * explicitly zero-set some values such as CRCs and reserved.
405 * Returns 0 on success, otherwise < 0 on error.
407 static int gpt_mknew_header_from_bkp(struct fdisk_context
*cxt
,
408 struct gpt_header
*header
,
410 struct gpt_header
*header2
)
412 if (!cxt
|| !header
|| !header2
)
415 header
->signature
= header2
->signature
;
416 header
->revision
= header2
->revision
;
417 header
->size
= header2
->size
;
418 header
->npartition_entries
= header2
->npartition_entries
;
419 header
->sizeof_partition_entry
= header2
->sizeof_partition_entry
;
420 header
->first_usable_lba
= header2
->first_usable_lba
;
421 header
->last_usable_lba
= header2
->last_usable_lba
;
423 memcpy(&header
->disk_guid
,
424 &header2
->disk_guid
, sizeof(header2
->disk_guid
));
425 gpt_mknew_header_common(cxt
, header
, lba
);
430 static struct gpt_header
*gpt_copy_header(struct fdisk_context
*cxt
,
431 struct gpt_header
*src
)
433 struct gpt_header
*res
;
438 res
= calloc(1, sizeof(*res
));
440 fdisk_warn(cxt
, _("failed to allocate GPT header"));
444 res
->my_lba
= src
->alternative_lba
;
445 res
->alternative_lba
= src
->my_lba
;
447 res
->signature
= src
->signature
;
448 res
->revision
= src
->revision
;
449 res
->size
= src
->size
;
450 res
->npartition_entries
= src
->npartition_entries
;
451 res
->sizeof_partition_entry
= src
->sizeof_partition_entry
;
452 res
->first_usable_lba
= src
->first_usable_lba
;
453 res
->last_usable_lba
= src
->last_usable_lba
;
455 memcpy(&res
->disk_guid
, &src
->disk_guid
, sizeof(src
->disk_guid
));
458 if (res
->my_lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
)
459 res
->partition_entry_lba
= cpu_to_le64(2);
461 uint64_t esz
= le32_to_cpu(src
->npartition_entries
) * sizeof(struct gpt_entry
);
462 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
464 res
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
471 * Builds a clean new GPT header (currently under revision 1.0).
473 * Always pass a new (zeroized) header to build upon as we don't
474 * explicitly zero-set some values such as CRCs and reserved.
476 * Returns 0 on success, otherwise < 0 on error.
478 static int gpt_mknew_header(struct fdisk_context
*cxt
,
479 struct gpt_header
*header
, uint64_t lba
)
481 uint64_t esz
= 0, first
, last
;
486 esz
= sizeof(struct gpt_entry
) * GPT_NPARTITIONS
/ cxt
->sector_size
;
488 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
489 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
490 header
->size
= cpu_to_le32(sizeof(struct gpt_header
));
493 * 128 partitions is the default. It can go behond this, however,
494 * we're creating a de facto header here, so no funny business.
496 header
->npartition_entries
= cpu_to_le32(GPT_NPARTITIONS
);
497 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
499 last
= cxt
->total_sectors
- 2 - esz
;
502 if (first
< cxt
->first_lba
&& cxt
->first_lba
< last
)
503 /* Align according to topology */
504 first
= cxt
->first_lba
;
506 header
->first_usable_lba
= cpu_to_le64(first
);
507 header
->last_usable_lba
= cpu_to_le64(last
);
509 gpt_mknew_header_common(cxt
, header
, lba
);
510 uuid_generate_random((unsigned char *) &header
->disk_guid
);
511 swap_efi_guid(&header
->disk_guid
);
517 * Checks if there is a valid protective MBR partition table.
518 * Returns 0 if it is invalid or failure. Otherwise, return
519 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection.
521 static int valid_pmbr(struct fdisk_context
*cxt
)
523 int i
, part
= 0, ret
= 0; /* invalid by default */
524 struct gpt_legacy_mbr
*pmbr
= NULL
;
527 if (!cxt
->firstsector
)
530 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
532 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
535 /* LBA of the GPT partition header */
536 if (pmbr
->partition_record
[0].starting_lba
!=
537 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
540 /* seems like a valid MBR was found, check DOS primary partitions */
541 for (i
= 0; i
< 4; i
++) {
542 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
544 * Ok, we at least know that there's a protective MBR,
545 * now check if there are other partition types for
549 ret
= GPT_MBR_PROTECTIVE
;
554 if (ret
!= GPT_MBR_PROTECTIVE
)
557 for (i
= 0 ; i
< 4; i
++) {
558 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
559 (pmbr
->partition_record
[i
].os_type
!= 0x00))
560 ret
= GPT_MBR_HYBRID
;
564 * Protective MBRs take up the lesser of the whole disk
565 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
566 * Some partitioning programs, nonetheless, choose to set
567 * the size to the maximum 32-bit limitation, disregarding
570 * Hybrid MBRs do not necessarily comply with this.
572 * Consider a bad value here to be a warning to support dd-ing
573 * an image from a smaller disk to a bigger disk.
575 if (ret
== GPT_MBR_PROTECTIVE
) {
576 sz_lba
= le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
577 if (sz_lba
!= (uint32_t) cxt
->total_sectors
- 1 && sz_lba
!= 0xFFFFFFFF) {
578 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%u != %u) "
579 "will be corrected by w(rite)."),
581 (uint32_t) cxt
->total_sectors
- 1);
582 fdisk_label_set_changed(cxt
->label
, 1);
589 static uint64_t last_lba(struct fdisk_context
*cxt
)
593 memset(&s
, 0, sizeof(s
));
594 if (fstat(cxt
->dev_fd
, &s
) == -1) {
595 fdisk_warn(cxt
, _("gpt: stat() failed"));
599 if (S_ISBLK(s
.st_mode
))
600 return cxt
->total_sectors
- 1;
601 else if (S_ISREG(s
.st_mode
)) {
602 uint64_t sectors
= s
.st_size
>> cxt
->sector_size
;
603 return (sectors
/ cxt
->sector_size
) - 1ULL;
605 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
609 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
610 void *buffer
, const size_t bytes
)
612 off_t offset
= lba
* cxt
->sector_size
;
614 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
616 return read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
620 /* Returns the GPT entry array */
621 static struct gpt_entry
*gpt_read_entries(struct fdisk_context
*cxt
,
622 struct gpt_header
*header
)
625 struct gpt_entry
*ret
= NULL
;
631 sz
= le32_to_cpu(header
->npartition_entries
) *
632 le32_to_cpu(header
->sizeof_partition_entry
);
637 offset
= le64_to_cpu(header
->partition_entry_lba
) *
640 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
642 if (sz
!= read(cxt
->dev_fd
, ret
, sz
))
652 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
)
654 return (crc32(~0L, buf
, len
) ^ ~0L);
658 * Recompute header and partition array 32bit CRC checksums.
659 * This function does not fail - if there's corruption, then it
660 * will be reported when checksuming it again (ie: probing or verify).
662 static void gpt_recompute_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
672 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
673 header
->crc32
= cpu_to_le32(crc
);
675 /* partition entry array CRC */
676 header
->partition_entry_array_crc32
= 0;
677 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
678 le32_to_cpu(header
->sizeof_partition_entry
);
680 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
681 header
->partition_entry_array_crc32
= cpu_to_le32(crc
);
685 * Compute the 32bit CRC checksum of the partition table header.
686 * Returns 1 if it is valid, otherwise 0.
688 static int gpt_check_header_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
690 uint32_t crc
, orgcrc
= le32_to_cpu(header
->crc32
);
693 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
694 header
->crc32
= cpu_to_le32(orgcrc
);
696 if (crc
== le32_to_cpu(header
->crc32
))
700 * If we have checksum mismatch it may be due to stale data,
701 * like a partition being added or deleted. Recompute the CRC again
702 * and make sure this is not the case.
705 gpt_recompute_crc(header
, ents
);
706 orgcrc
= le32_to_cpu(header
->crc32
);
708 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
709 header
->crc32
= cpu_to_le32(orgcrc
);
711 return crc
== le32_to_cpu(header
->crc32
);
718 * It initializes the partition entry array.
719 * Returns 1 if the checksum is valid, otherwise 0.
721 static int gpt_check_entryarr_crc(struct gpt_header
*header
,
722 struct gpt_entry
*ents
)
728 if (!header
|| !ents
)
731 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
732 le32_to_cpu(header
->sizeof_partition_entry
);
737 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
738 ret
= (crc
== le32_to_cpu(header
->partition_entry_array_crc32
));
743 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
746 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
748 fu
= le64_to_cpu(header
->first_usable_lba
);
749 lu
= le64_to_cpu(header
->last_usable_lba
);
751 /* check if first and last usable LBA make sense */
753 DBG(LABEL
, dbgprint("error: header last LBA is before first LBA"));
757 /* check if first and last usable LBAs with the disk's last LBA */
758 if (fu
> lastlba
|| lu
> lastlba
) {
759 DBG(LABEL
, dbgprint("error: header LBAs are after the disk's last LBA"));
763 /* the header has to be outside usable range */
764 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
765 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
766 DBG(LABEL
, dbgprint("error: header outside of usable range"));
775 /* Check if there is a valid header signature */
776 static int gpt_check_signature(struct gpt_header
*header
)
778 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
782 * Return the specified GPT Header, or NULL upon failure/invalid.
783 * Note that all tests must pass to ensure a valid header,
784 * we do not rely on only testing the signature for a valid probe.
786 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
788 struct gpt_entry
**_ents
)
790 struct gpt_header
*header
= NULL
;
791 struct gpt_entry
*ents
= NULL
;
797 header
= calloc(1, sizeof(*header
));
801 /* read and verify header */
802 if (read_lba(cxt
, lba
, header
, sizeof(struct gpt_header
)) != 0)
805 if (!gpt_check_signature(header
))
808 if (!gpt_check_header_crc(header
, NULL
))
811 /* read and verify entries */
812 ents
= gpt_read_entries(cxt
, header
);
816 if (!gpt_check_entryarr_crc(header
, ents
))
819 if (!gpt_check_lba_sanity(cxt
, header
))
822 /* valid header must be at MyLBA */
823 if (le64_to_cpu(header
->my_lba
) != lba
)
826 /* make sure header size is between 92 and sector size bytes */
827 hsz
= le32_to_cpu(header
->size
);
828 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
836 DBG(LABEL
, dbgprint("found valid GPT Header on LBA %ju", lba
));
842 DBG(LABEL
, dbgprint("read GPT Header on LBA %ju failed", lba
));
847 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
848 const char **name
, off_t
*offset
, size_t *size
)
850 struct fdisk_gpt_label
*gpt
;
865 *name
= _("GPT Header");
866 *offset
= GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
867 *size
= sizeof(struct gpt_header
);
870 *name
= _("GPT Entries");
871 gpt
= self_label(cxt
);
872 *offset
= le64_to_cpu(gpt
->pheader
->partition_entry_lba
) * cxt
->sector_size
;
873 *size
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
874 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
877 return 1; /* no more chunks */
886 * Returns the number of partitions that are in use.
888 static unsigned partitions_in_use(struct gpt_header
*header
, struct gpt_entry
*e
)
890 uint32_t i
, used
= 0;
895 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
896 if (!partition_unused(&e
[i
]))
903 * Check if a partition is too big for the disk (sectors).
904 * Returns the faulting partition number, otherwise 0.
906 static uint32_t partition_check_too_big(struct gpt_header
*header
,
907 struct gpt_entry
*e
, uint64_t sectors
)
911 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
912 if (partition_unused(&e
[i
]))
914 if (gpt_partition_end(&e
[i
]) >= sectors
)
922 * Check if a partition ends before it begins
923 * Returns the faulting partition number, otherwise 0.
925 static uint32_t partition_start_after_end(struct gpt_header
*header
, struct gpt_entry
*e
)
929 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
930 if (partition_unused(&e
[i
]))
932 if (gpt_partition_start(&e
[i
]) > gpt_partition_end(&e
[i
]))
940 * Check if partition e1 overlaps with partition e2
942 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
944 uint64_t start1
= gpt_partition_start(e1
);
945 uint64_t end1
= gpt_partition_end(e1
);
946 uint64_t start2
= gpt_partition_start(e2
);
947 uint64_t end2
= gpt_partition_end(e2
);
949 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
953 * Find any paritions that overlap.
955 static uint32_t partition_check_overlaps(struct gpt_header
*header
, struct gpt_entry
*e
)
959 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
960 for (j
= 0; j
< i
; j
++) {
961 if (partition_unused(&e
[i
]) ||
962 partition_unused(&e
[j
]))
964 if (partition_overlap(&e
[i
], &e
[j
])) {
965 DBG(LABEL
, dbgprint("GPT partitions overlap detected [%u vs. %u]", i
, j
));
974 * Find the first available block after the starting point; returns 0 if
975 * there are no available blocks left, or error. From gdisk.
977 static uint64_t find_first_available(struct gpt_header
*header
,
978 struct gpt_entry
*e
, uint64_t start
)
981 uint32_t i
, first_moved
= 0;
988 fu
= le64_to_cpu(header
->first_usable_lba
);
989 lu
= le64_to_cpu(header
->last_usable_lba
);
992 * Begin from the specified starting point or from the first usable
993 * LBA, whichever is greater...
995 first
= start
< fu
? fu
: start
;
998 * Now search through all partitions; if first is within an
999 * existing partition, move it to the next sector after that
1000 * partition and repeat. If first was moved, set firstMoved
1001 * flag; repeat until firstMoved is not set, so as to catch
1002 * cases where partitions are out of sequential order....
1006 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1007 if (partition_unused(&e
[i
]))
1009 if (first
< gpt_partition_start(&e
[i
]))
1011 if (first
<= gpt_partition_end(&e
[i
])) {
1012 first
= gpt_partition_end(&e
[i
]) + 1;
1016 } while (first_moved
== 1);
1025 /* Returns last available sector in the free space pointed to by start. From gdisk. */
1026 static uint64_t find_last_free(struct gpt_header
*header
,
1027 struct gpt_entry
*e
, uint64_t start
)
1030 uint64_t nearest_start
;
1035 nearest_start
= le64_to_cpu(header
->last_usable_lba
);
1037 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1038 uint64_t ps
= gpt_partition_start(&e
[i
]);
1040 if (nearest_start
> ps
&& ps
> start
)
1041 nearest_start
= ps
- 1;
1044 return nearest_start
;
1047 /* Returns the last free sector on the disk. From gdisk. */
1048 static uint64_t find_last_free_sector(struct gpt_header
*header
,
1049 struct gpt_entry
*e
)
1051 uint32_t i
, last_moved
;
1057 /* start by assuming the last usable LBA is available */
1058 last
= le64_to_cpu(header
->last_usable_lba
);
1061 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1062 if ((last
>= gpt_partition_start(&e
[i
])) &&
1063 (last
<= gpt_partition_end(&e
[i
]))) {
1064 last
= gpt_partition_start(&e
[i
]) - 1;
1068 } while (last_moved
== 1);
1074 * Finds the first available sector in the largest block of unallocated
1075 * space on the disk. Returns 0 if there are no available blocks left.
1078 static uint64_t find_first_in_largest(struct gpt_header
*header
, struct gpt_entry
*e
)
1080 uint64_t start
= 0, first_sect
, last_sect
;
1081 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1087 first_sect
= find_first_available(header
, e
, start
);
1088 if (first_sect
!= 0) {
1089 last_sect
= find_last_free(header
, e
, first_sect
);
1090 segment_size
= last_sect
- first_sect
+ 1;
1092 if (segment_size
> selected_size
) {
1093 selected_size
= segment_size
;
1094 selected_segment
= first_sect
;
1096 start
= last_sect
+ 1;
1098 } while (first_sect
!= 0);
1101 return selected_segment
;
1105 * Find the total number of free sectors, the number of segments in which
1106 * they reside, and the size of the largest of those segments. From gdisk.
1108 static uint64_t get_free_sectors(struct fdisk_context
*cxt
, struct gpt_header
*header
,
1109 struct gpt_entry
*e
, uint32_t *nsegments
,
1110 uint64_t *largest_segment
)
1113 uint64_t first_sect
, last_sect
;
1114 uint64_t largest_seg
= 0, segment_sz
;
1115 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1117 if (!cxt
->total_sectors
)
1121 first_sect
= find_first_available(header
, e
, start
);
1123 last_sect
= find_last_free(header
, e
, first_sect
);
1124 segment_sz
= last_sect
- first_sect
+ 1;
1126 if (segment_sz
> largest_seg
)
1127 largest_seg
= segment_sz
;
1128 totfound
+= segment_sz
;
1130 start
= last_sect
+ 1;
1132 } while (first_sect
);
1137 if (largest_segment
)
1138 *largest_segment
= largest_seg
;
1143 static int gpt_probe_label(struct fdisk_context
*cxt
)
1146 struct fdisk_gpt_label
*gpt
;
1150 assert(fdisk_is_disklabel(cxt
, GPT
));
1152 gpt
= self_label(cxt
);
1154 /* TODO: it would be nice to support scenario when GPT headers are OK,
1155 * but PMBR is corrupt */
1156 mbr_type
= valid_pmbr(cxt
);
1160 DBG(LABEL
, dbgprint("found a %s MBR", mbr_type
== GPT_MBR_PROTECTIVE
?
1161 "protective" : "hybrid"));
1163 /* primary header */
1164 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1168 /* primary OK, try backup from alternative LBA */
1169 gpt
->bheader
= gpt_read_header(cxt
,
1170 le64_to_cpu(gpt
->pheader
->alternative_lba
),
1173 /* primary corrupted -- try last LBA */
1174 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), &gpt
->ents
);
1176 if (!gpt
->pheader
&& !gpt
->bheader
)
1179 /* primary OK, backup corrupted -- recovery */
1180 if (gpt
->pheader
&& !gpt
->bheader
) {
1181 fdisk_warnx(cxt
, _("The backup GPT table is corrupt, but the "
1182 "primary appears OK, so that will be used."));
1183 gpt
->bheader
= gpt_copy_header(cxt
, gpt
->pheader
);
1186 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1188 /* primary corrupted, backup OK -- recovery */
1189 } else if (!gpt
->pheader
&& gpt
->bheader
) {
1190 fdisk_warnx(cxt
, _("The primary GPT table is corrupt, but the "
1191 "backup appears OK, so that will be used."));
1192 gpt
->pheader
= gpt_copy_header(cxt
, gpt
->bheader
);
1195 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1198 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1199 cxt
->label
->nparts_cur
= partitions_in_use(gpt
->pheader
, gpt
->ents
);
1202 DBG(LABEL
, dbgprint("GPT probe failed"));
1203 gpt_deinit(cxt
->label
);
1208 * Stolen from libblkid - can be removed once partition semantics
1209 * are added to the fdisk API.
1211 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1215 size_t i
, j
, len
= count
;
1217 dest
= calloc(1, count
);
1221 for (j
= i
= 0; i
+ 2 <= count
; i
+= 2) {
1222 /* always little endian */
1223 c
= (src
[i
+1] << 8) | src
[i
];
1227 } else if (c
< 0x80) {
1230 dest
[j
++] = (uint8_t) c
;
1231 } else if (c
< 0x800) {
1234 dest
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
1235 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1239 dest
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
1240 dest
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
1241 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1249 /* convert GUID Specific attributes to string, result is a list of the enabled
1250 * bits (e.g. "60,62,63" for enabled bits 60, 62 and 63).
1252 * Returns newly allocated string or NULL in case of error.
1254 * see struct gpt_attr definition for more details.
1256 static char *guid_attrs_to_string(struct gpt_attr
*attr
, char **res
)
1258 char *bits
= (char *) attr
, *end
;
1259 size_t i
, count
= 0, len
;
1261 end
= *res
= calloc(1, 16 * 3 + 6); /* three bytes for one bit + \0 */
1265 for (i
= 48; i
< 64; i
++) {
1266 if (!isset(bits
, i
))
1270 len
= snprintf(end
, 4, ",%zu", i
);
1272 len
= snprintf(end
, 8, "GUID:%zu", i
);
1279 static int gpt_get_partition(struct fdisk_context
*cxt
, size_t n
,
1280 struct fdisk_partition
*pa
)
1282 struct fdisk_gpt_label
*gpt
;
1283 struct gpt_entry
*e
;
1284 char u_str
[37], *buf
= NULL
;
1288 assert(fdisk_is_disklabel(cxt
, GPT
));
1290 gpt
= self_label(cxt
);
1292 if ((uint32_t) n
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1295 gpt
= self_label(cxt
);
1298 pa
->used
= !partition_unused(e
) || gpt_partition_start(e
);
1302 pa
->start
= gpt_partition_start(e
);
1303 pa
->end
= gpt_partition_end(e
);
1304 pa
->size
= gpt_partition_size(e
);
1305 pa
->type
= gpt_partition_parttype(cxt
, e
);
1307 if (guid_to_string(&e
->partition_guid
, u_str
)) {
1308 pa
->uuid
= strdup(u_str
);
1314 if (asprintf(&pa
->attrs
, "%s%s%s%s",
1315 e
->attr
.required_to_function
? "Required " : "",
1316 e
->attr
.legacy_bios_bootable
? "LegacyBoot " : "",
1317 e
->attr
.no_blockio_protocol
? "NoBlockIO " : "",
1318 guid_attrs_to_string(&e
->attr
, &buf
)) < 0)
1321 pa
->name
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1325 fdisk_reset_partition(pa
);
1330 * List label partitions.
1332 static int gpt_list_disklabel(struct fdisk_context
*cxt
)
1336 assert(fdisk_is_disklabel(cxt
, GPT
));
1338 if (fdisk_context_display_details(cxt
)) {
1339 struct gpt_header
*h
= self_label(cxt
)->pheader
;
1341 fdisk_colon(cxt
, _("First LBA: %ju"), h
->first_usable_lba
);
1342 fdisk_colon(cxt
, _("Last LBA: %ju"), h
->last_usable_lba
);
1343 fdisk_colon(cxt
, _("Alternative LBA: %ju"), h
->alternative_lba
);
1344 fdisk_colon(cxt
, _("Partitions entries LBA: %ju"), h
->partition_entry_lba
);
1345 fdisk_colon(cxt
, _("Allocated partition entries: %u"), h
->npartition_entries
);
1348 return fdisk_list_partitions(cxt
, NULL
, 0);
1353 * Returns 0 on success, or corresponding error otherwise.
1355 static int gpt_write_partitions(struct fdisk_context
*cxt
,
1356 struct gpt_header
*header
, struct gpt_entry
*ents
)
1358 off_t offset
= le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
;
1359 uint32_t nparts
= le32_to_cpu(header
->npartition_entries
);
1360 uint32_t totwrite
= nparts
* le32_to_cpu(header
->sizeof_partition_entry
);
1363 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1366 rc
= write(cxt
->dev_fd
, ents
, totwrite
);
1367 if (rc
> 0 && totwrite
== (uint32_t) rc
)
1374 * Write a GPT header to a specified LBA
1375 * Returns 0 on success, or corresponding error otherwise.
1377 static int gpt_write_header(struct fdisk_context
*cxt
,
1378 struct gpt_header
*header
, uint64_t lba
)
1380 off_t offset
= lba
* cxt
->sector_size
;
1382 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1384 if (cxt
->sector_size
==
1385 (size_t) write(cxt
->dev_fd
, header
, cxt
->sector_size
))
1392 * Write the protective MBR.
1393 * Returns 0 on success, or corresponding error otherwise.
1395 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
1398 struct gpt_legacy_mbr
*pmbr
= NULL
;
1401 assert(cxt
->firstsector
);
1403 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
1405 /* zero out the legacy partitions */
1406 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
1408 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
1409 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
1410 pmbr
->partition_record
[0].start_sector
= 1;
1411 pmbr
->partition_record
[0].end_head
= 0xFE;
1412 pmbr
->partition_record
[0].end_sector
= 0xFF;
1413 pmbr
->partition_record
[0].end_track
= 0xFF;
1414 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
1417 * Set size_in_lba to the size of the disk minus one. If the size of the disk
1418 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
1420 if (cxt
->total_sectors
- 1 > 0xFFFFFFFFULL
)
1421 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
1423 pmbr
->partition_record
[0].size_in_lba
=
1424 cpu_to_le32(cxt
->total_sectors
- 1UL);
1426 offset
= GPT_PMBR_LBA
* cxt
->sector_size
;
1427 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1430 /* pMBR covers the first sector (LBA) of the disk */
1431 if (write_all(cxt
->dev_fd
, pmbr
, cxt
->sector_size
))
1439 * Writes in-memory GPT and pMBR data to disk.
1440 * Returns 0 if successful write, otherwise, a corresponding error.
1441 * Any indication of error will abort the operation.
1443 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
1445 struct fdisk_gpt_label
*gpt
;
1450 assert(fdisk_is_disklabel(cxt
, GPT
));
1452 gpt
= self_label(cxt
);
1453 mbr_type
= valid_pmbr(cxt
);
1455 /* check that disk is big enough to handle the backup header */
1456 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
1459 /* check that the backup header is properly placed */
1460 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1)
1461 /* TODO: correct this (with user authorization) and write */
1464 if (partition_check_overlaps(gpt
->pheader
, gpt
->ents
))
1467 /* recompute CRCs for both headers */
1468 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1469 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1472 * UEFI requires writing in this specific order:
1473 * 1) backup partition tables
1474 * 2) backup GPT header
1475 * 3) primary partition tables
1476 * 4) primary GPT header
1479 * If any write fails, we abort the rest.
1481 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
1483 if (gpt_write_header(cxt
, gpt
->bheader
,
1484 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
1486 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
1488 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
1491 if (mbr_type
== GPT_MBR_HYBRID
)
1492 fdisk_warnx(cxt
, _("The device contains hybrid MBR -- writing GPT only. "
1493 "You have to sync the MBR manually."));
1494 else if (gpt_write_pmbr(cxt
) != 0)
1497 DBG(LABEL
, dbgprint("GPT write success"));
1500 DBG(LABEL
, dbgprint("GPT write failed: incorrect input"));
1504 DBG(LABEL
, dbgprint("GPT write failed: %m"));
1509 * Verify data integrity and report any found problems for:
1510 * - primary and backup header validations
1511 * - paritition validations
1513 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
1517 struct fdisk_gpt_label
*gpt
;
1521 assert(fdisk_is_disklabel(cxt
, GPT
));
1523 gpt
= self_label(cxt
);
1525 if (!gpt
|| !gpt
->bheader
) {
1527 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
1530 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
1532 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
1534 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
1536 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
1539 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
1541 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
1544 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
1546 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
1548 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
1550 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
1553 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
1555 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
1557 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
1559 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
1562 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
1564 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
1568 * if the GPT is the primary table, check the alternateLBA
1569 * to see if it is a valid GPT
1571 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
1572 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
1574 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
1577 ptnum
= partition_check_overlaps(gpt
->pheader
, gpt
->ents
);
1580 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
1584 ptnum
= partition_check_too_big(gpt
->pheader
, gpt
->ents
, cxt
->total_sectors
);
1587 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
1591 ptnum
= partition_start_after_end(gpt
->pheader
, gpt
->ents
);
1594 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
1598 if (!nerror
) { /* yay :-) */
1599 uint32_t nsegments
= 0;
1600 uint64_t free_sectors
= 0, largest_segment
= 0;
1603 fdisk_info(cxt
, _("No errors detected."));
1604 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
1605 fdisk_info(cxt
, _("Using %u out of %d partitions."),
1606 partitions_in_use(gpt
->pheader
, gpt
->ents
),
1607 le32_to_cpu(gpt
->pheader
->npartition_entries
));
1609 free_sectors
= get_free_sectors(cxt
, gpt
->pheader
, gpt
->ents
,
1610 &nsegments
, &largest_segment
);
1611 if (largest_segment
)
1612 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
| SIZE_SUFFIX_3LETTER
,
1613 largest_segment
* cxt
->sector_size
);
1616 P_("A total of %ju free sectors is available in %u segment.",
1617 "A total of %ju free sectors is available in %u segments "
1618 "(the largest is %s).", nsegments
),
1619 free_sectors
, nsegments
, strsz
);
1624 P_("%d error detected.", "%d errors detected.", nerror
),
1630 /* Delete a single GPT partition, specified by partnum. */
1631 static int gpt_delete_partition(struct fdisk_context
*cxt
,
1634 struct fdisk_gpt_label
*gpt
;
1638 assert(fdisk_is_disklabel(cxt
, GPT
));
1640 gpt
= self_label(cxt
);
1642 if (partnum
>= cxt
->label
->nparts_max
1643 || partition_unused(&gpt
->ents
[partnum
]))
1646 /* hasta la vista, baby! */
1647 memset(&gpt
->ents
[partnum
], 0, sizeof(struct gpt_entry
));
1648 if (!partition_unused(&gpt
->ents
[partnum
]))
1651 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1652 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1653 cxt
->label
->nparts_cur
--;
1654 fdisk_label_set_changed(cxt
->label
, 1);
1660 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
1663 DBG(LABEL
, dbgprint_uuid("new type", &(e
->type
)));
1667 * Create a new GPT partition entry, specified by partnum, and with a range
1668 * of fsect to lsenct sectors, of type t.
1669 * Returns 0 on success, or negative upon failure.
1671 static int gpt_create_new_partition(struct fdisk_context
*cxt
,
1672 size_t partnum
, uint64_t fsect
, uint64_t lsect
,
1673 struct gpt_guid
*type
,
1674 struct gpt_entry
*entries
)
1676 struct gpt_entry
*e
= NULL
;
1677 struct fdisk_gpt_label
*gpt
;
1681 assert(fdisk_is_disklabel(cxt
, GPT
));
1683 gpt
= self_label(cxt
);
1685 if (fsect
> lsect
|| partnum
>= cxt
->label
->nparts_max
)
1688 e
= calloc(1, sizeof(*e
));
1691 e
->lba_end
= cpu_to_le64(lsect
);
1692 e
->lba_start
= cpu_to_le64(fsect
);
1694 gpt_entry_set_type(e
, type
);
1697 * Any time a new partition entry is created a new GUID must be
1698 * generated for that partition, and every partition is guaranteed
1699 * to have a unique GUID.
1701 uuid_generate_random((unsigned char *) &e
->partition_guid
);
1702 swap_efi_guid(&e
->partition_guid
);
1704 memcpy(&entries
[partnum
], e
, sizeof(*e
));
1706 gpt_recompute_crc(gpt
->pheader
, entries
);
1707 gpt_recompute_crc(gpt
->bheader
, entries
);
1713 /* Performs logical checks to add a new partition entry */
1714 static int gpt_add_partition(
1715 struct fdisk_context
*cxt
,
1716 struct fdisk_partition
*pa
)
1718 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
1719 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
1720 uint64_t dflt_f
, dflt_l
; /* largest segment (default) */
1721 struct gpt_guid
typeid;
1722 struct fdisk_gpt_label
*gpt
;
1723 struct gpt_header
*pheader
;
1724 struct gpt_entry
*ents
;
1725 struct fdisk_ask
*ask
= NULL
;
1731 assert(fdisk_is_disklabel(cxt
, GPT
));
1733 gpt
= self_label(cxt
);
1734 pheader
= gpt
->pheader
;
1737 rc
= fdisk_partition_next_partno(cxt
, pa
, &partnum
);
1741 if (!partition_unused(&ents
[partnum
])) {
1742 fdisk_warnx(cxt
, _("Partition %zu is already defined. "
1743 "Delete it before re-adding it."), partnum
+1);
1746 if (le32_to_cpu(pheader
->npartition_entries
) ==
1747 partitions_in_use(pheader
, ents
)) {
1748 fdisk_warnx(cxt
, _("All partitions are already in use."));
1751 if (!get_free_sectors(cxt
, pheader
, ents
, NULL
, NULL
)) {
1752 fdisk_warnx(cxt
, _("No free sectors available."));
1756 string_to_guid(pa
&& pa
->type
&& pa
->type
->typestr
?
1758 GPT_DEFAULT_ENTRY_TYPE
, &typeid);
1760 disk_f
= find_first_available(pheader
, ents
, 0);
1761 disk_l
= find_last_free_sector(pheader
, ents
);
1763 /* the default is the largest free space */
1764 dflt_f
= find_first_in_largest(pheader
, ents
);
1765 dflt_l
= find_last_free(pheader
, ents
, dflt_f
);
1767 /* align the default in range <dflt_f,dflt_l>*/
1768 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
1771 if (pa
&& pa
->start
) {
1772 if (pa
->start
!= find_first_available(pheader
, ents
, pa
->start
)) {
1773 fdisk_warnx(cxt
, _("Sector %ju already used."), pa
->start
);
1777 } else if (pa
&& pa
->start_follow_default
) {
1783 ask
= fdisk_new_ask();
1785 fdisk_reset_ask(ask
);
1788 fdisk_ask_set_query(ask
, _("First sector"));
1789 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
1790 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
1791 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
1792 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
1794 rc
= fdisk_do_ask(cxt
, ask
);
1798 user_f
= fdisk_ask_number_get_result(ask
);
1799 if (user_f
!= find_first_available(pheader
, ents
, user_f
)) {
1800 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
1808 dflt_l
= find_last_free(pheader
, ents
, user_f
);
1810 if (pa
&& pa
->size
) {
1811 if (pa
->size
+ user_f
> dflt_l
)
1813 user_l
= user_f
+ pa
->size
;
1814 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1816 } else if (pa
&& pa
->end_follow_default
) {
1821 ask
= fdisk_new_ask();
1823 fdisk_reset_ask(ask
);
1825 fdisk_ask_set_query(ask
, _("Last sector, +sectors or +size{K,M,G,T,P}"));
1826 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
1827 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
1828 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
1829 fdisk_ask_number_set_high(ask
, dflt_l
); /* maximal */
1830 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
1831 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
1833 rc
= fdisk_do_ask(cxt
, ask
);
1837 user_l
= fdisk_ask_number_get_result(ask
);
1838 if (fdisk_ask_number_is_relative(ask
))
1839 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1840 if (user_l
> user_f
&& user_l
<= disk_l
)
1845 if (gpt_create_new_partition(cxt
, partnum
,
1846 user_f
, user_l
, &typeid, ents
) != 0)
1847 fdisk_warnx(cxt
, _("Could not create partition %ju"), partnum
+ 1);
1849 struct fdisk_parttype
*t
;
1851 cxt
->label
->nparts_cur
++;
1852 fdisk_label_set_changed(cxt
->label
, 1);
1854 t
= gpt_partition_parttype(cxt
, &ents
[partnum
]);
1855 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
1856 fdisk_free_parttype(t
);
1861 fdisk_free_ask(ask
);
1866 * Create a new GPT disklabel - destroys any previous data.
1868 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
1873 struct fdisk_gpt_label
*gpt
;
1877 assert(fdisk_is_disklabel(cxt
, GPT
));
1879 gpt
= self_label(cxt
);
1881 /* label private stuff has to be empty, see gpt_deinit() */
1882 assert(gpt
->pheader
== NULL
);
1883 assert(gpt
->bheader
== NULL
);
1886 * When no header, entries or pmbr is set, we're probably
1887 * dealing with a new, empty disk - so always allocate memory
1888 * to deal with the data structures whatever the case is.
1890 rc
= gpt_mknew_pmbr(cxt
);
1895 gpt
->pheader
= calloc(1, sizeof(*gpt
->pheader
));
1896 if (!gpt
->pheader
) {
1900 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
1904 /* backup ("copy" primary) */
1905 gpt
->bheader
= calloc(1, sizeof(*gpt
->bheader
));
1906 if (!gpt
->bheader
) {
1910 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
1911 last_lba(cxt
), gpt
->pheader
);
1915 esz
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
1916 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
1917 gpt
->ents
= calloc(1, esz
);
1922 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1923 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1925 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1926 cxt
->label
->nparts_cur
= 0;
1928 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
1929 fdisk_label_set_changed(cxt
->label
, 1);
1930 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
1931 _("Created a new GPT disklabel (GUID: %s)."), str
);
1936 static int gpt_get_disklabel_id(struct fdisk_context
*cxt
, char **id
)
1938 struct fdisk_gpt_label
*gpt
;
1944 assert(fdisk_is_disklabel(cxt
, GPT
));
1946 gpt
= self_label(cxt
);
1947 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
1955 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
)
1957 struct fdisk_gpt_label
*gpt
;
1958 struct gpt_guid uuid
;
1959 char *str
, *old
, *new;
1964 assert(fdisk_is_disklabel(cxt
, GPT
));
1966 gpt
= self_label(cxt
);
1967 if (fdisk_ask_string(cxt
,
1968 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str
))
1971 rc
= string_to_guid(str
, &uuid
);
1975 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
1979 gpt_get_disklabel_id(cxt
, &old
);
1981 gpt
->pheader
->disk_guid
= uuid
;
1982 gpt
->bheader
->disk_guid
= uuid
;
1984 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1985 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1987 gpt_get_disklabel_id(cxt
, &new);
1989 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
1990 _("Disk identifier changed from %s to %s."), old
, new);
1994 fdisk_label_set_changed(cxt
->label
, 1);
1998 static int gpt_set_partition_type(
1999 struct fdisk_context
*cxt
,
2001 struct fdisk_parttype
*t
)
2003 struct gpt_guid uuid
;
2004 struct fdisk_gpt_label
*gpt
;
2008 assert(fdisk_is_disklabel(cxt
, GPT
));
2010 gpt
= self_label(cxt
);
2011 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
)
2012 || !t
|| !t
->typestr
|| string_to_guid(t
->typestr
, &uuid
) != 0)
2015 gpt_entry_set_type(&gpt
->ents
[i
], &uuid
);
2016 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2017 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2019 fdisk_label_set_changed(cxt
->label
, 1);
2023 static int gpt_part_is_used(struct fdisk_context
*cxt
, size_t i
)
2025 struct fdisk_gpt_label
*gpt
;
2026 struct gpt_entry
*e
;
2030 assert(fdisk_is_disklabel(cxt
, GPT
));
2032 gpt
= self_label(cxt
);
2034 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2038 return !partition_unused(e
) || gpt_partition_start(e
);
2041 int fdisk_gpt_partition_set_uuid(struct fdisk_context
*cxt
, size_t i
)
2043 struct fdisk_gpt_label
*gpt
;
2044 struct gpt_entry
*e
;
2045 struct gpt_guid uuid
;
2046 char *str
, new_u
[37], old_u
[37];
2051 assert(fdisk_is_disklabel(cxt
, GPT
));
2053 DBG(LABEL
, dbgprint("UUID change requested partno=%zu", i
));
2055 gpt
= self_label(cxt
);
2057 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2060 if (fdisk_ask_string(cxt
,
2061 _("New UUID (in 8-4-4-4-12 format)"), &str
))
2064 rc
= string_to_guid(str
, &uuid
);
2068 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2074 guid_to_string(&e
->partition_guid
, old_u
);
2075 guid_to_string(&uuid
, new_u
);
2077 e
->partition_guid
= uuid
;
2078 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2079 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2080 fdisk_label_set_changed(cxt
->label
, 1);
2082 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2083 _("Partition UUID changed from %s to %s."),
2088 int fdisk_gpt_partition_set_name(struct fdisk_context
*cxt
, size_t i
)
2090 struct fdisk_gpt_label
*gpt
;
2091 struct gpt_entry
*e
;
2092 char *str
, *old
, name
[GPT_PART_NAME_LEN
] = { 0 };
2097 assert(fdisk_is_disklabel(cxt
, GPT
));
2099 DBG(LABEL
, dbgprint("NAME change requested partno=%zu", i
));
2101 gpt
= self_label(cxt
);
2103 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2106 if (fdisk_ask_string(cxt
, _("New name"), &str
))
2110 old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
2114 if (sz
> GPT_PART_NAME_LEN
)
2115 sz
= GPT_PART_NAME_LEN
;
2116 memcpy(name
, str
, sz
);
2119 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
2120 e
->name
[i
] = cpu_to_le16((uint16_t) name
[i
]);
2122 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2123 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2125 fdisk_label_set_changed(cxt
->label
, 1);
2127 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2128 _("Partition name changed from '%s' to '%.*s'."),
2129 old
, (int) GPT_PART_NAME_LEN
, str
);
2136 int fdisk_gpt_is_hybrid(struct fdisk_context
*cxt
)
2139 return valid_pmbr(cxt
) == GPT_MBR_HYBRID
;
2142 static int gpt_toggle_partition_flag(
2143 struct fdisk_context
*cxt
,
2147 struct fdisk_gpt_label
*gpt
;
2148 struct gpt_entry
*e
;
2152 assert(fdisk_is_disklabel(cxt
, GPT
));
2154 DBG(LABEL
, dbgprint("GPT entry attribute change requested partno=%zu", i
));
2156 gpt
= self_label(cxt
);
2158 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2164 case GPT_FLAG_REQUIRED
:
2165 e
->attr
.required_to_function
= !e
->attr
.required_to_function
;
2166 fdisk_label_set_changed(cxt
->label
, 1);
2167 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2168 e
->attr
.required_to_function
?
2169 _("The RequiredPartiton flag on partition %zu is enabled now.") :
2170 _("The RequiredPartiton flag on partition %zu is disabled now."),
2173 case GPT_FLAG_NOBLOCK
:
2174 e
->attr
.no_blockio_protocol
= !e
->attr
.no_blockio_protocol
;
2175 fdisk_label_set_changed(cxt
->label
, 1);
2176 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2177 e
->attr
.no_blockio_protocol
?
2178 _("The NoBlockIOProtocol flag on partition %zu is enabled now.") :
2179 _("The NoBlockIOProtocol flag on partition %zu is disabled now."),
2182 case GPT_FLAG_LEGACYBOOT
:
2183 e
->attr
.legacy_bios_bootable
= !e
->attr
.legacy_bios_bootable
;
2184 fdisk_label_set_changed(cxt
->label
, 1);
2185 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2186 e
->attr
.legacy_bios_bootable
?
2187 _("The LegacyBIOSBootable flag on partition %zu is enabled now.") :
2188 _("The LegacyBIOSBootable flag on partition %zu is disabled now."),
2191 case GPT_FLAG_GUIDSPECIFIC
:
2193 char *attrs
= (char *) &e
->attr
;
2195 int rc
= fdisk_ask_number(cxt
, 48, 48, 63,
2196 _("Enter GUID specific bit"),
2200 if (!isset(attrs
, bit
))
2205 fdisk_label_set_changed(cxt
->label
, 1);
2206 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2208 _("The GUID specific bit %ju on partition %zu is enabled now.") :
2209 _("The GUID specific bit %ju on partition %zu is disabled now."),
2217 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2218 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2224 * Deinitialize fdisk-specific variables
2226 static void gpt_deinit(struct fdisk_label
*lb
)
2228 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
2238 gpt
->pheader
= NULL
;
2239 gpt
->bheader
= NULL
;
2242 static const struct fdisk_label_operations gpt_operations
=
2244 .probe
= gpt_probe_label
,
2245 .write
= gpt_write_disklabel
,
2246 .verify
= gpt_verify_disklabel
,
2247 .create
= gpt_create_disklabel
,
2248 .list
= gpt_list_disklabel
,
2249 .locate
= gpt_locate_disklabel
,
2250 .get_id
= gpt_get_disklabel_id
,
2251 .set_id
= gpt_set_disklabel_id
,
2253 .get_part
= gpt_get_partition
,
2254 .add_part
= gpt_add_partition
,
2256 .part_delete
= gpt_delete_partition
,
2258 .part_is_used
= gpt_part_is_used
,
2259 .part_set_type
= gpt_set_partition_type
,
2260 .part_toggle_flag
= gpt_toggle_partition_flag
,
2262 .deinit
= gpt_deinit
2265 static const struct fdisk_column gpt_columns
[] =
2268 { FDISK_COL_DEVICE
, N_("Device"), 10, 0 },
2269 { FDISK_COL_START
, N_("Start"), 5, TT_FL_RIGHT
},
2270 { FDISK_COL_END
, N_("End"), 5, TT_FL_RIGHT
},
2271 { FDISK_COL_SECTORS
, N_("Sectors"), 5, TT_FL_RIGHT
},
2272 { FDISK_COL_SIZE
, N_("Size"), 5, TT_FL_RIGHT
, FDISK_COLFL_EYECANDY
},
2273 { FDISK_COL_TYPE
, N_("Type"), 0.1, TT_FL_TRUNC
, FDISK_COLFL_EYECANDY
},
2275 { FDISK_COL_TYPEID
, N_("Type-UUID"), 36, 0, FDISK_COLFL_DETAIL
},
2276 { FDISK_COL_UUID
, N_("UUID"), 36, 0, FDISK_COLFL_DETAIL
},
2277 { FDISK_COL_NAME
, N_("Name"), 0.2, TT_FL_TRUNC
, FDISK_COLFL_DETAIL
},
2278 { FDISK_COL_ATTR
, N_("Attrs"), 0, 0, FDISK_COLFL_DETAIL
}
2282 * allocates GPT in-memory stuff
2284 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
)
2286 struct fdisk_label
*lb
;
2287 struct fdisk_gpt_label
*gpt
;
2291 gpt
= calloc(1, sizeof(*gpt
));
2295 /* initialize generic part of the driver */
2296 lb
= (struct fdisk_label
*) gpt
;
2298 lb
->id
= FDISK_DISKLABEL_GPT
;
2299 lb
->op
= &gpt_operations
;
2300 lb
->parttypes
= gpt_parttypes
;
2301 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);
2303 lb
->columns
= gpt_columns
;
2304 lb
->ncolumns
= ARRAY_SIZE(gpt_columns
);