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"
73 GPT_ATTRBIT_NOBLOCK
= 1,
74 GPT_ATTRBIT_LEGACY
= 2,
76 /* GUID specific (range 48..64)*/
77 GPT_ATTRBIT_GUID_FIRST
= 48,
78 GPT_ATTRBIT_GUID_COUNT
= 16
81 #define GPT_ATTRSTR_REQ "RequiredPartiton"
82 #define GPT_ATTRSTR_NOBLOCK "NoBlockIOProtocol"
83 #define GPT_ATTRSTR_LEGACY "LegacyBIOSBootable"
85 /* The GPT Partition entry array contains an array of GPT entries. */
87 struct gpt_guid type
; /* purpose and type of the partition */
88 struct gpt_guid partition_guid
;
92 uint16_t name
[GPT_PART_NAME_LEN
];
93 } __attribute__ ((packed
));
97 uint64_t signature
; /* header identification */
98 uint32_t revision
; /* header version */
99 uint32_t size
; /* in bytes */
100 uint32_t crc32
; /* header CRC checksum */
101 uint32_t reserved1
; /* must be 0 */
102 uint64_t my_lba
; /* LBA that contains this struct (LBA 1) */
103 uint64_t alternative_lba
; /* backup GPT header */
104 uint64_t first_usable_lba
; /* first usable logical block for partitions */
105 uint64_t last_usable_lba
; /* last usable logical block for partitions */
106 struct gpt_guid disk_guid
; /* unique disk identifier */
107 uint64_t partition_entry_lba
; /* stat LBA of the partition entry array */
108 uint32_t npartition_entries
; /* total partition entries - normally 128 */
109 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
110 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
111 uint8_t reserved2
[512 - 92]; /* must be 0 */
112 } __attribute__ ((packed
));
115 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
116 uint8_t start_head
; /* unused by EFI, pt start in CHS */
117 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
119 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
120 uint8_t end_head
; /* unused by EFI, pt end in CHS */
121 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
122 uint8_t end_track
; /* unused by EFI, pt end in CHS */
123 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
124 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
125 } __attribute__ ((packed
));
127 /* Protected MBR and legacy MBR share same structure */
128 struct gpt_legacy_mbr
{
129 uint8_t boot_code
[440];
130 uint32_t unique_mbr_signature
;
132 struct gpt_record partition_record
[4];
134 } __attribute__ ((packed
));
138 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
140 #define DEF_GUID(_u, _n) \
146 static struct fdisk_parttype gpt_parttypes
[] =
149 DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")),
151 DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")),
152 DEF_GUID("D3BFE2DE-3DAF-11DF-BA40-E3A556D89593", N_("Intel Fast Flash")),
154 /* Hah!IdontneedEFI */
155 DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot")),
158 DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
159 DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
160 DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")),
161 DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")),
162 DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery environment")),
163 DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")),
164 DEF_GUID("E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D", N_("Microsoft Storage Spaces")),
167 DEF_GUID("75894C1E-3AEB-11D3-B7C1-7B03A0000000", N_("HP-UX data")),
168 DEF_GUID("E2A1E728-32E3-11D6-A682-7B03A0000000", N_("HP-UX service")),
170 /* Linux (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec) */
171 DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")),
172 DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")),
173 DEF_GUID("3B8F8425-20E0-4F3B-907F-1A25A76F98E8", N_("Linux server data")),
174 DEF_GUID("44479540-F297-41B2-9AF7-D131D5F0458A", N_("Linux root (x86)")),
175 DEF_GUID("4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", N_("Linux root (x86-64)")),
176 DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")),
177 DEF_GUID("933AC7E1-2EB4-4F13-B844-0E14E2AEF915", N_("Linux home")),
178 DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")),
179 DEF_GUID("BC13C2FF-59E6-4262-A352-B275FD6F7172", N_("Linux extended boot")),
180 DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")),
183 DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")),
184 DEF_GUID("83BD6B9D-7F41-11DC-BE0B-001560B84F0F", N_("FreeBSD boot")),
185 DEF_GUID("516E7CB5-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD swap")),
186 DEF_GUID("516E7CB6-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD UFS")),
187 DEF_GUID("516E7CBA-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD ZFS")),
188 DEF_GUID("516E7CB8-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD Vinum")),
191 DEF_GUID("48465300-0000-11AA-AA11-00306543ECAC", N_("Apple HFS/HFS+")),
192 DEF_GUID("55465300-0000-11AA-AA11-00306543ECAC", N_("Apple UFS")),
193 DEF_GUID("52414944-0000-11AA-AA11-00306543ECAC", N_("Apple RAID")),
194 DEF_GUID("52414944-5F4F-11AA-AA11-00306543ECAC", N_("Apple RAID offline")),
195 DEF_GUID("426F6F74-0000-11AA-AA11-00306543ECAC", N_("Apple boot")),
196 DEF_GUID("4C616265-6C00-11AA-AA11-00306543ECAC", N_("Apple label")),
197 DEF_GUID("5265636F-7665-11AA-AA11-00306543ECAC", N_("Apple TV recovery")),
198 DEF_GUID("53746F72-6167-11AA-AA11-00306543ECAC", N_("Apple Core storage")),
201 DEF_GUID("6A82CB45-1DD2-11B2-99A6-080020736631", N_("Solaris boot")),
202 DEF_GUID("6A85CF4D-1DD2-11B2-99A6-080020736631", N_("Solaris root")),
203 /* same as Apple ZFS */
204 DEF_GUID("6A898CC3-1DD2-11B2-99A6-080020736631", N_("Solaris /usr & Apple ZFS")),
205 DEF_GUID("6A87C46F-1DD2-11B2-99A6-080020736631", N_("Solaris swap")),
206 DEF_GUID("6A8B642B-1DD2-11B2-99A6-080020736631", N_("Solaris backup")),
207 DEF_GUID("6A8EF2E9-1DD2-11B2-99A6-080020736631", N_("Solaris /var")),
208 DEF_GUID("6A90BA39-1DD2-11B2-99A6-080020736631", N_("Solaris /home")),
209 DEF_GUID("6A9283A5-1DD2-11B2-99A6-080020736631", N_("Solaris alternate sector")),
210 DEF_GUID("6A945A3B-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 1")),
211 DEF_GUID("6A9630D1-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 2")),
212 DEF_GUID("6A980767-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 3")),
213 DEF_GUID("6A96237F-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 4")),
214 DEF_GUID("6A8D2AC7-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 5")),
217 DEF_GUID("49F48D32-B10E-11DC-B99B-0019D1879648", N_("NetBSD swap")),
218 DEF_GUID("49F48D5A-B10E-11DC-B99B-0019D1879648", N_("NetBSD FFS")),
219 DEF_GUID("49F48D82-B10E-11DC-B99B-0019D1879648", N_("NetBSD LFS")),
220 DEF_GUID("2DB519C4-B10E-11DC-B99B-0019D1879648", N_("NetBSD concatenated")),
221 DEF_GUID("2DB519EC-B10E-11DC-B99B-0019D1879648", N_("NetBSD encrypted")),
222 DEF_GUID("49F48DAA-B10E-11DC-B99B-0019D1879648", N_("NetBSD RAID")),
225 DEF_GUID("FE3A2A5D-4F32-41A7-B725-ACCC3285A309", N_("ChromeOS kernel")),
226 DEF_GUID("3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", N_("ChromeOS root fs")),
227 DEF_GUID("2E0A753D-9E48-43B0-8337-B15192CB1B5E", N_("ChromeOS reserved")),
230 DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")),
231 DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")),
232 DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")),
233 DEF_GUID("0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
234 DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")),
235 DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")),
238 /* gpt_entry macros */
239 #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start)
240 #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end)
243 * in-memory fdisk GPT stuff
245 struct fdisk_gpt_label
{
246 struct fdisk_label head
; /* generic part */
248 /* gpt specific part */
249 struct gpt_header
*pheader
; /* primary header */
250 struct gpt_header
*bheader
; /* backup header */
251 struct gpt_entry
*ents
; /* entries (partitions) */
254 static void gpt_deinit(struct fdisk_label
*lb
);
256 static inline struct fdisk_gpt_label
*self_label(struct fdisk_context
*cxt
)
258 return (struct fdisk_gpt_label
*) cxt
->label
;
262 * Returns the partition length, or 0 if end is before beginning.
264 static uint64_t gpt_partition_size(const struct gpt_entry
*e
)
266 uint64_t start
= gpt_partition_start(e
);
267 uint64_t end
= gpt_partition_end(e
);
269 return start
> end
? 0 : end
- start
+ 1ULL;
272 /* prints UUID in the real byte order! */
273 static void gpt_debug_uuid(const char *mesg
, struct gpt_guid
*guid
)
275 const unsigned char *uuid
= (unsigned char *) guid
;
277 fprintf(stderr
, "%s: "
278 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
280 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
284 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
288 * UUID is traditionally 16 byte big-endian array, except Intel EFI
289 * specification where the UUID is a structure of little-endian fields.
291 static void swap_efi_guid(struct gpt_guid
*uid
)
293 uid
->time_low
= swab32(uid
->time_low
);
294 uid
->time_mid
= swab16(uid
->time_mid
);
295 uid
->time_hi_and_version
= swab16(uid
->time_hi_and_version
);
298 static int string_to_guid(const char *in
, struct gpt_guid
*guid
)
300 if (uuid_parse(in
, (unsigned char *) guid
)) /* BE */
302 swap_efi_guid(guid
); /* LE */
306 static char *guid_to_string(const struct gpt_guid
*guid
, char *out
)
308 struct gpt_guid u
= *guid
; /* LE */
310 swap_efi_guid(&u
); /* BE */
311 uuid_unparse_upper((unsigned char *) &u
, out
);
316 static struct fdisk_parttype
*gpt_partition_parttype(
317 struct fdisk_context
*cxt
,
318 const struct gpt_entry
*e
)
320 struct fdisk_parttype
*t
;
323 guid_to_string(&e
->type
, str
);
324 t
= fdisk_label_get_parttype_from_string(cxt
->label
, str
);
325 return t
? : fdisk_new_unknown_parttype(0, str
);
330 static const char *gpt_get_header_revstr(struct gpt_header
*header
)
335 switch (header
->revision
) {
336 case GPT_HEADER_REVISION_V1_02
:
338 case GPT_HEADER_REVISION_V1_00
:
340 case GPT_HEADER_REVISION_V0_99
:
350 static inline int partition_unused(const struct gpt_entry
*e
)
352 return !memcmp(&e
->type
, &GPT_UNUSED_ENTRY_GUID
,
353 sizeof(struct gpt_guid
));
357 * Builds a clean new valid protective MBR - will wipe out any existing data.
358 * Returns 0 on success, otherwise < 0 on error.
360 static int gpt_mknew_pmbr(struct fdisk_context
*cxt
)
362 struct gpt_legacy_mbr
*pmbr
= NULL
;
365 if (!cxt
|| !cxt
->firstsector
)
368 rc
= fdisk_init_firstsector_buffer(cxt
);
372 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
374 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
375 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
376 pmbr
->partition_record
[0].start_sector
= 1;
377 pmbr
->partition_record
[0].end_head
= 0xFE;
378 pmbr
->partition_record
[0].end_sector
= 0xFF;
379 pmbr
->partition_record
[0].end_track
= 0xFF;
380 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
381 pmbr
->partition_record
[0].size_in_lba
=
382 cpu_to_le32(min((uint32_t) cxt
->total_sectors
- 1, 0xFFFFFFFF));
387 /* some universal differences between the headers */
388 static void gpt_mknew_header_common(struct fdisk_context
*cxt
,
389 struct gpt_header
*header
, uint64_t lba
)
394 header
->my_lba
= cpu_to_le64(lba
);
396 if (lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
) { /* primary */
397 header
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1);
398 header
->partition_entry_lba
= cpu_to_le64(2);
399 } else { /* backup */
400 uint64_t esz
= le32_to_cpu(header
->npartition_entries
) * sizeof(struct gpt_entry
);
401 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
403 header
->alternative_lba
= cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA
);
404 header
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
409 * Builds a new GPT header (at sector lba) from a backup header2.
410 * If building a primary header, then backup is the secondary, and vice versa.
412 * Always pass a new (zeroized) header to build upon as we don't
413 * explicitly zero-set some values such as CRCs and reserved.
415 * Returns 0 on success, otherwise < 0 on error.
417 static int gpt_mknew_header_from_bkp(struct fdisk_context
*cxt
,
418 struct gpt_header
*header
,
420 struct gpt_header
*header2
)
422 if (!cxt
|| !header
|| !header2
)
425 header
->signature
= header2
->signature
;
426 header
->revision
= header2
->revision
;
427 header
->size
= header2
->size
;
428 header
->npartition_entries
= header2
->npartition_entries
;
429 header
->sizeof_partition_entry
= header2
->sizeof_partition_entry
;
430 header
->first_usable_lba
= header2
->first_usable_lba
;
431 header
->last_usable_lba
= header2
->last_usable_lba
;
433 memcpy(&header
->disk_guid
,
434 &header2
->disk_guid
, sizeof(header2
->disk_guid
));
435 gpt_mknew_header_common(cxt
, header
, lba
);
440 static struct gpt_header
*gpt_copy_header(struct fdisk_context
*cxt
,
441 struct gpt_header
*src
)
443 struct gpt_header
*res
;
448 res
= calloc(1, sizeof(*res
));
450 fdisk_warn(cxt
, _("failed to allocate GPT header"));
454 res
->my_lba
= src
->alternative_lba
;
455 res
->alternative_lba
= src
->my_lba
;
457 res
->signature
= src
->signature
;
458 res
->revision
= src
->revision
;
459 res
->size
= src
->size
;
460 res
->npartition_entries
= src
->npartition_entries
;
461 res
->sizeof_partition_entry
= src
->sizeof_partition_entry
;
462 res
->first_usable_lba
= src
->first_usable_lba
;
463 res
->last_usable_lba
= src
->last_usable_lba
;
465 memcpy(&res
->disk_guid
, &src
->disk_guid
, sizeof(src
->disk_guid
));
468 if (res
->my_lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
)
469 res
->partition_entry_lba
= cpu_to_le64(2);
471 uint64_t esz
= le32_to_cpu(src
->npartition_entries
) * sizeof(struct gpt_entry
);
472 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
474 res
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
480 static void count_first_last_lba(struct fdisk_context
*cxt
,
481 uint64_t *first
, uint64_t *last
)
487 esz
= sizeof(struct gpt_entry
) * GPT_NPARTITIONS
/ cxt
->sector_size
;
488 *last
= cxt
->total_sectors
- 2 - esz
;
491 if (*first
< cxt
->first_lba
&& cxt
->first_lba
< *last
)
492 /* Align according to topology */
493 *first
= cxt
->first_lba
;
497 * Builds a clean new GPT header (currently under revision 1.0).
499 * Always pass a new (zeroized) header to build upon as we don't
500 * explicitly zero-set some values such as CRCs and reserved.
502 * Returns 0 on success, otherwise < 0 on error.
504 static int gpt_mknew_header(struct fdisk_context
*cxt
,
505 struct gpt_header
*header
, uint64_t lba
)
507 uint64_t first
, last
;
513 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
514 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
515 header
->size
= cpu_to_le32(sizeof(struct gpt_header
));
518 * 128 partitions are the default. It can go beyond that, but
519 * we're creating a de facto header here, so no funny business.
521 header
->npartition_entries
= cpu_to_le32(GPT_NPARTITIONS
);
522 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
524 count_first_last_lba(cxt
, &first
, &last
);
525 header
->first_usable_lba
= cpu_to_le64(first
);
526 header
->last_usable_lba
= cpu_to_le64(last
);
528 gpt_mknew_header_common(cxt
, header
, lba
);
531 const char *id
= fdisk_script_get_header(cxt
->script
, "label-id");
532 if (id
&& string_to_guid(id
, &header
->disk_guid
) == 0)
537 uuid_generate_random((unsigned char *) &header
->disk_guid
);
538 swap_efi_guid(&header
->disk_guid
);
544 * Checks if there is a valid protective MBR partition table.
545 * Returns 0 if it is invalid or failure. Otherwise, return
546 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection.
548 static int valid_pmbr(struct fdisk_context
*cxt
)
550 int i
, part
= 0, ret
= 0; /* invalid by default */
551 struct gpt_legacy_mbr
*pmbr
= NULL
;
554 if (!cxt
->firstsector
)
557 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
559 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
562 /* LBA of the GPT partition header */
563 if (pmbr
->partition_record
[0].starting_lba
!=
564 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
567 /* seems like a valid MBR was found, check DOS primary partitions */
568 for (i
= 0; i
< 4; i
++) {
569 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
571 * Ok, we at least know that there's a protective MBR,
572 * now check if there are other partition types for
576 ret
= GPT_MBR_PROTECTIVE
;
581 if (ret
!= GPT_MBR_PROTECTIVE
)
584 for (i
= 0 ; i
< 4; i
++) {
585 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
586 (pmbr
->partition_record
[i
].os_type
!= 0x00))
587 ret
= GPT_MBR_HYBRID
;
591 * Protective MBRs take up the lesser of the whole disk
592 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
593 * Some partitioning programs, nonetheless, choose to set
594 * the size to the maximum 32-bit limitation, disregarding
597 * Hybrid MBRs do not necessarily comply with this.
599 * Consider a bad value here to be a warning to support dd-ing
600 * an image from a smaller disk to a bigger disk.
602 if (ret
== GPT_MBR_PROTECTIVE
) {
603 sz_lba
= le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
604 if (sz_lba
!= (uint32_t) cxt
->total_sectors
- 1 && sz_lba
!= 0xFFFFFFFF) {
605 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%u != %u) "
606 "will be corrected by w(rite)."),
608 (uint32_t) cxt
->total_sectors
- 1);
609 fdisk_label_set_changed(cxt
->label
, 1);
616 static uint64_t last_lba(struct fdisk_context
*cxt
)
619 uint64_t sectors
= 0;
621 memset(&s
, 0, sizeof(s
));
622 if (fstat(cxt
->dev_fd
, &s
) == -1) {
623 fdisk_warn(cxt
, _("gpt: stat() failed"));
627 if (S_ISBLK(s
.st_mode
))
628 sectors
= cxt
->total_sectors
- 1;
629 else if (S_ISREG(s
.st_mode
))
630 sectors
= ((uint64_t) s
.st_size
/
631 (uint64_t) cxt
->sector_size
) - 1ULL;
633 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
635 DBG(LABEL
, ul_debug("GPT last LBA: %ju", sectors
));
639 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
640 void *buffer
, const size_t bytes
)
642 off_t offset
= lba
* cxt
->sector_size
;
644 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
646 return read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
650 /* Returns the GPT entry array */
651 static struct gpt_entry
*gpt_read_entries(struct fdisk_context
*cxt
,
652 struct gpt_header
*header
)
655 struct gpt_entry
*ret
= NULL
;
661 sz
= le32_to_cpu(header
->npartition_entries
) *
662 le32_to_cpu(header
->sizeof_partition_entry
);
667 offset
= le64_to_cpu(header
->partition_entry_lba
) *
670 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
672 if (sz
!= read(cxt
->dev_fd
, ret
, sz
))
682 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
)
684 return (crc32(~0L, buf
, len
) ^ ~0L);
688 * Recompute header and partition array 32bit CRC checksums.
689 * This function does not fail - if there's corruption, then it
690 * will be reported when checksuming it again (ie: probing or verify).
692 static void gpt_recompute_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
702 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
703 header
->crc32
= cpu_to_le32(crc
);
705 /* partition entry array CRC */
706 header
->partition_entry_array_crc32
= 0;
707 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
708 le32_to_cpu(header
->sizeof_partition_entry
);
710 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
711 header
->partition_entry_array_crc32
= cpu_to_le32(crc
);
715 * Compute the 32bit CRC checksum of the partition table header.
716 * Returns 1 if it is valid, otherwise 0.
718 static int gpt_check_header_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
720 uint32_t crc
, orgcrc
= le32_to_cpu(header
->crc32
);
723 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
724 header
->crc32
= cpu_to_le32(orgcrc
);
726 if (crc
== le32_to_cpu(header
->crc32
))
730 * If we have checksum mismatch it may be due to stale data,
731 * like a partition being added or deleted. Recompute the CRC again
732 * and make sure this is not the case.
735 gpt_recompute_crc(header
, ents
);
736 orgcrc
= le32_to_cpu(header
->crc32
);
738 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
739 header
->crc32
= cpu_to_le32(orgcrc
);
741 return crc
== le32_to_cpu(header
->crc32
);
748 * It initializes the partition entry array.
749 * Returns 1 if the checksum is valid, otherwise 0.
751 static int gpt_check_entryarr_crc(struct gpt_header
*header
,
752 struct gpt_entry
*ents
)
758 if (!header
|| !ents
)
761 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
762 le32_to_cpu(header
->sizeof_partition_entry
);
767 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
768 ret
= (crc
== le32_to_cpu(header
->partition_entry_array_crc32
));
773 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
776 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
778 fu
= le64_to_cpu(header
->first_usable_lba
);
779 lu
= le64_to_cpu(header
->last_usable_lba
);
781 /* check if first and last usable LBA make sense */
783 DBG(LABEL
, ul_debug("error: header last LBA is before first LBA"));
787 /* check if first and last usable LBAs with the disk's last LBA */
788 if (fu
> lastlba
|| lu
> lastlba
) {
789 DBG(LABEL
, ul_debug("error: header LBAs are after the disk's last LBA"));
793 /* the header has to be outside usable range */
794 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
795 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
796 DBG(LABEL
, ul_debug("error: header outside of usable range"));
805 /* Check if there is a valid header signature */
806 static int gpt_check_signature(struct gpt_header
*header
)
808 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
812 * Return the specified GPT Header, or NULL upon failure/invalid.
813 * Note that all tests must pass to ensure a valid header,
814 * we do not rely on only testing the signature for a valid probe.
816 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
818 struct gpt_entry
**_ents
)
820 struct gpt_header
*header
= NULL
;
821 struct gpt_entry
*ents
= NULL
;
827 header
= calloc(1, sizeof(*header
));
831 /* read and verify header */
832 if (read_lba(cxt
, lba
, header
, sizeof(struct gpt_header
)) != 0)
835 if (!gpt_check_signature(header
))
838 if (!gpt_check_header_crc(header
, NULL
))
841 /* read and verify entries */
842 ents
= gpt_read_entries(cxt
, header
);
846 if (!gpt_check_entryarr_crc(header
, ents
))
849 if (!gpt_check_lba_sanity(cxt
, header
))
852 /* valid header must be at MyLBA */
853 if (le64_to_cpu(header
->my_lba
) != lba
)
856 /* make sure header size is between 92 and sector size bytes */
857 hsz
= le32_to_cpu(header
->size
);
858 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
866 DBG(LABEL
, ul_debug("found valid GPT Header on LBA %ju", lba
));
872 DBG(LABEL
, ul_debug("read GPT Header on LBA %ju failed", lba
));
877 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
878 const char **name
, off_t
*offset
, size_t *size
)
880 struct fdisk_gpt_label
*gpt
;
895 *name
= _("GPT Header");
896 *offset
= GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
897 *size
= sizeof(struct gpt_header
);
900 *name
= _("GPT Entries");
901 gpt
= self_label(cxt
);
902 *offset
= le64_to_cpu(gpt
->pheader
->partition_entry_lba
) * cxt
->sector_size
;
903 *size
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
904 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
907 return 1; /* no more chunks */
916 * Returns the number of partitions that are in use.
918 static unsigned partitions_in_use(struct gpt_header
*header
, struct gpt_entry
*e
)
920 uint32_t i
, used
= 0;
925 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
926 if (!partition_unused(&e
[i
]))
933 * Check if a partition is too big for the disk (sectors).
934 * Returns the faulting partition number, otherwise 0.
936 static uint32_t partition_check_too_big(struct gpt_header
*header
,
937 struct gpt_entry
*e
, uint64_t sectors
)
941 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
942 if (partition_unused(&e
[i
]))
944 if (gpt_partition_end(&e
[i
]) >= sectors
)
952 * Check if a partition ends before it begins
953 * Returns the faulting partition number, otherwise 0.
955 static uint32_t partition_start_after_end(struct gpt_header
*header
, struct gpt_entry
*e
)
959 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
960 if (partition_unused(&e
[i
]))
962 if (gpt_partition_start(&e
[i
]) > gpt_partition_end(&e
[i
]))
970 * Check if partition e1 overlaps with partition e2.
972 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
974 uint64_t start1
= gpt_partition_start(e1
);
975 uint64_t end1
= gpt_partition_end(e1
);
976 uint64_t start2
= gpt_partition_start(e2
);
977 uint64_t end2
= gpt_partition_end(e2
);
979 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
983 * Find any partitions that overlap.
985 static uint32_t partition_check_overlaps(struct gpt_header
*header
, struct gpt_entry
*e
)
989 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
990 for (j
= 0; j
< i
; j
++) {
991 if (partition_unused(&e
[i
]) ||
992 partition_unused(&e
[j
]))
994 if (partition_overlap(&e
[i
], &e
[j
])) {
995 DBG(LABEL
, ul_debug("GPT partitions overlap detected [%u vs. %u]", i
, j
));
1004 * Find the first available block after the starting point; returns 0 if
1005 * there are no available blocks left, or error. From gdisk.
1007 static uint64_t find_first_available(struct gpt_header
*header
,
1008 struct gpt_entry
*e
, uint64_t start
)
1011 uint32_t i
, first_moved
= 0;
1018 fu
= le64_to_cpu(header
->first_usable_lba
);
1019 lu
= le64_to_cpu(header
->last_usable_lba
);
1022 * Begin from the specified starting point or from the first usable
1023 * LBA, whichever is greater...
1025 first
= start
< fu
? fu
: start
;
1028 * Now search through all partitions; if first is within an
1029 * existing partition, move it to the next sector after that
1030 * partition and repeat. If first was moved, set firstMoved
1031 * flag; repeat until firstMoved is not set, so as to catch
1032 * cases where partitions are out of sequential order....
1036 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1037 if (partition_unused(&e
[i
]))
1039 if (first
< gpt_partition_start(&e
[i
]))
1041 if (first
<= gpt_partition_end(&e
[i
])) {
1042 first
= gpt_partition_end(&e
[i
]) + 1;
1046 } while (first_moved
== 1);
1055 /* Returns last available sector in the free space pointed to by start. From gdisk. */
1056 static uint64_t find_last_free(struct gpt_header
*header
,
1057 struct gpt_entry
*e
, uint64_t start
)
1060 uint64_t nearest_start
;
1065 nearest_start
= le64_to_cpu(header
->last_usable_lba
);
1067 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1068 uint64_t ps
= gpt_partition_start(&e
[i
]);
1070 if (nearest_start
> ps
&& ps
> start
)
1071 nearest_start
= ps
- 1;
1074 return nearest_start
;
1077 /* Returns the last free sector on the disk. From gdisk. */
1078 static uint64_t find_last_free_sector(struct gpt_header
*header
,
1079 struct gpt_entry
*e
)
1081 uint32_t i
, last_moved
;
1087 /* start by assuming the last usable LBA is available */
1088 last
= le64_to_cpu(header
->last_usable_lba
);
1091 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1092 if ((last
>= gpt_partition_start(&e
[i
])) &&
1093 (last
<= gpt_partition_end(&e
[i
]))) {
1094 last
= gpt_partition_start(&e
[i
]) - 1;
1098 } while (last_moved
== 1);
1104 * Finds the first available sector in the largest block of unallocated
1105 * space on the disk. Returns 0 if there are no available blocks left.
1108 static uint64_t find_first_in_largest(struct gpt_header
*header
, struct gpt_entry
*e
)
1110 uint64_t start
= 0, first_sect
, last_sect
;
1111 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1117 first_sect
= find_first_available(header
, e
, start
);
1118 if (first_sect
!= 0) {
1119 last_sect
= find_last_free(header
, e
, first_sect
);
1120 segment_size
= last_sect
- first_sect
+ 1;
1122 if (segment_size
> selected_size
) {
1123 selected_size
= segment_size
;
1124 selected_segment
= first_sect
;
1126 start
= last_sect
+ 1;
1128 } while (first_sect
!= 0);
1131 return selected_segment
;
1135 * Find the total number of free sectors, the number of segments in which
1136 * they reside, and the size of the largest of those segments. From gdisk.
1138 static uint64_t get_free_sectors(struct fdisk_context
*cxt
, struct gpt_header
*header
,
1139 struct gpt_entry
*e
, uint32_t *nsegments
,
1140 uint64_t *largest_segment
)
1143 uint64_t first_sect
, last_sect
;
1144 uint64_t largest_seg
= 0, segment_sz
;
1145 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1147 if (!cxt
->total_sectors
)
1151 first_sect
= find_first_available(header
, e
, start
);
1153 last_sect
= find_last_free(header
, e
, first_sect
);
1154 segment_sz
= last_sect
- first_sect
+ 1;
1156 if (segment_sz
> largest_seg
)
1157 largest_seg
= segment_sz
;
1158 totfound
+= segment_sz
;
1160 start
= last_sect
+ 1;
1162 } while (first_sect
);
1167 if (largest_segment
)
1168 *largest_segment
= largest_seg
;
1173 static int gpt_probe_label(struct fdisk_context
*cxt
)
1176 struct fdisk_gpt_label
*gpt
;
1180 assert(fdisk_is_label(cxt
, GPT
));
1182 gpt
= self_label(cxt
);
1184 /* TODO: it would be nice to support scenario when GPT headers are OK,
1185 * but PMBR is corrupt */
1186 mbr_type
= valid_pmbr(cxt
);
1190 DBG(LABEL
, ul_debug("found a %s MBR", mbr_type
== GPT_MBR_PROTECTIVE
?
1191 "protective" : "hybrid"));
1193 /* primary header */
1194 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1198 /* primary OK, try backup from alternative LBA */
1199 gpt
->bheader
= gpt_read_header(cxt
,
1200 le64_to_cpu(gpt
->pheader
->alternative_lba
),
1203 /* primary corrupted -- try last LBA */
1204 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), &gpt
->ents
);
1206 if (!gpt
->pheader
&& !gpt
->bheader
)
1209 /* primary OK, backup corrupted -- recovery */
1210 if (gpt
->pheader
&& !gpt
->bheader
) {
1211 fdisk_warnx(cxt
, _("The backup GPT table is corrupt, but the "
1212 "primary appears OK, so that will be used."));
1213 gpt
->bheader
= gpt_copy_header(cxt
, gpt
->pheader
);
1216 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1218 /* primary corrupted, backup OK -- recovery */
1219 } else if (!gpt
->pheader
&& gpt
->bheader
) {
1220 fdisk_warnx(cxt
, _("The primary GPT table is corrupt, but the "
1221 "backup appears OK, so that will be used."));
1222 gpt
->pheader
= gpt_copy_header(cxt
, gpt
->bheader
);
1225 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1228 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1229 cxt
->label
->nparts_cur
= partitions_in_use(gpt
->pheader
, gpt
->ents
);
1232 DBG(LABEL
, ul_debug("GPT probe failed"));
1233 gpt_deinit(cxt
->label
);
1238 * Stolen from libblkid - can be removed once partition semantics
1239 * are added to the fdisk API.
1241 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1245 size_t i
, j
, len
= count
;
1247 dest
= calloc(1, count
);
1251 for (j
= i
= 0; i
+ 2 <= count
; i
+= 2) {
1252 /* always little endian */
1253 c
= (src
[i
+1] << 8) | src
[i
];
1257 } else if (c
< 0x80) {
1260 dest
[j
++] = (uint8_t) c
;
1261 } else if (c
< 0x800) {
1264 dest
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
1265 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1269 dest
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
1270 dest
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
1271 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1279 static int gpt_entry_attrs_to_string(struct gpt_entry
*e
, char **res
)
1281 unsigned int n
, count
= 0;
1290 attrs
= le64_to_cpu(e
->attrs
);
1292 return 0; /* no attributes at all */
1294 bits
= (char *) &attrs
;
1296 /* Note that sizeof() is correct here, we need separators between
1297 * the strings so also count \0 is correct */
1298 *res
= calloc(1, sizeof(GPT_ATTRSTR_NOBLOCK
) +
1299 sizeof(GPT_ATTRSTR_REQ
) +
1300 sizeof(GPT_ATTRSTR_LEGACY
) +
1301 sizeof("GUID:") + (GPT_ATTRBIT_GUID_COUNT
* 3));
1306 if (isset(bits
, GPT_ATTRBIT_REQ
)) {
1307 memcpy(p
, GPT_ATTRSTR_REQ
, (l
= sizeof(GPT_ATTRSTR_REQ
)));
1310 if (isset(bits
, GPT_ATTRBIT_NOBLOCK
)) {
1313 memcpy(p
, GPT_ATTRSTR_NOBLOCK
, (l
= sizeof(GPT_ATTRSTR_NOBLOCK
)));
1316 if (isset(bits
, GPT_ATTRBIT_LEGACY
)) {
1319 memcpy(p
, GPT_ATTRSTR_LEGACY
, (l
= sizeof(GPT_ATTRSTR_LEGACY
)));
1323 for (n
= GPT_ATTRBIT_GUID_FIRST
;
1324 n
< GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
; n
++) {
1326 if (!isset(bits
, n
))
1331 p
+= sprintf(p
, "GUID:%u", n
);
1333 p
+= sprintf(p
, ",%u", n
);
1340 static int gpt_get_partition(struct fdisk_context
*cxt
, size_t n
,
1341 struct fdisk_partition
*pa
)
1343 struct fdisk_gpt_label
*gpt
;
1344 struct gpt_entry
*e
;
1350 assert(fdisk_is_label(cxt
, GPT
));
1352 gpt
= self_label(cxt
);
1354 if ((uint32_t) n
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1357 gpt
= self_label(cxt
);
1360 pa
->used
= !partition_unused(e
) || gpt_partition_start(e
);
1364 pa
->start
= gpt_partition_start(e
);
1365 pa
->end
= gpt_partition_end(e
);
1366 pa
->size
= gpt_partition_size(e
);
1367 pa
->type
= gpt_partition_parttype(cxt
, e
);
1369 if (guid_to_string(&e
->partition_guid
, u_str
)) {
1370 pa
->uuid
= strdup(u_str
);
1378 rc
= gpt_entry_attrs_to_string(e
, &pa
->attrs
);
1382 pa
->name
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1385 fdisk_reset_partition(pa
);
1391 * List label partitions.
1393 static int gpt_list_disklabel(struct fdisk_context
*cxt
)
1397 assert(fdisk_is_label(cxt
, GPT
));
1399 if (fdisk_is_details(cxt
)) {
1400 struct gpt_header
*h
= self_label(cxt
)->pheader
;
1402 fdisk_info(cxt
, _("First LBA: %ju"), h
->first_usable_lba
);
1403 fdisk_info(cxt
, _("Last LBA: %ju"), h
->last_usable_lba
);
1404 fdisk_info(cxt
, _("Alternative LBA: %ju"), h
->alternative_lba
);
1405 fdisk_info(cxt
, _("Partitions entries LBA: %ju"), h
->partition_entry_lba
);
1406 fdisk_info(cxt
, _("Allocated partition entries: %u"), h
->npartition_entries
);
1414 * Returns 0 on success, or corresponding error otherwise.
1416 static int gpt_write_partitions(struct fdisk_context
*cxt
,
1417 struct gpt_header
*header
, struct gpt_entry
*ents
)
1419 off_t offset
= le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
;
1420 uint32_t nparts
= le32_to_cpu(header
->npartition_entries
);
1421 uint32_t totwrite
= nparts
* le32_to_cpu(header
->sizeof_partition_entry
);
1424 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1427 rc
= write(cxt
->dev_fd
, ents
, totwrite
);
1428 if (rc
> 0 && totwrite
== (uint32_t) rc
)
1435 * Write a GPT header to a specified LBA
1436 * Returns 0 on success, or corresponding error otherwise.
1438 static int gpt_write_header(struct fdisk_context
*cxt
,
1439 struct gpt_header
*header
, uint64_t lba
)
1441 off_t offset
= lba
* cxt
->sector_size
;
1443 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1445 if (cxt
->sector_size
==
1446 (size_t) write(cxt
->dev_fd
, header
, cxt
->sector_size
))
1453 * Write the protective MBR.
1454 * Returns 0 on success, or corresponding error otherwise.
1456 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
1459 struct gpt_legacy_mbr
*pmbr
= NULL
;
1462 assert(cxt
->firstsector
);
1464 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
1466 /* zero out the legacy partitions */
1467 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
1469 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
1470 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
1471 pmbr
->partition_record
[0].start_sector
= 1;
1472 pmbr
->partition_record
[0].end_head
= 0xFE;
1473 pmbr
->partition_record
[0].end_sector
= 0xFF;
1474 pmbr
->partition_record
[0].end_track
= 0xFF;
1475 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
1478 * Set size_in_lba to the size of the disk minus one. If the size of the disk
1479 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
1481 if (cxt
->total_sectors
- 1 > 0xFFFFFFFFULL
)
1482 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
1484 pmbr
->partition_record
[0].size_in_lba
=
1485 cpu_to_le32(cxt
->total_sectors
- 1UL);
1487 offset
= GPT_PMBR_LBA
* cxt
->sector_size
;
1488 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1491 /* pMBR covers the first sector (LBA) of the disk */
1492 if (write_all(cxt
->dev_fd
, pmbr
, cxt
->sector_size
))
1500 * Writes in-memory GPT and pMBR data to disk.
1501 * Returns 0 if successful write, otherwise, a corresponding error.
1502 * Any indication of error will abort the operation.
1504 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
1506 struct fdisk_gpt_label
*gpt
;
1511 assert(fdisk_is_label(cxt
, GPT
));
1513 gpt
= self_label(cxt
);
1514 mbr_type
= valid_pmbr(cxt
);
1516 /* check that disk is big enough to handle the backup header */
1517 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
1520 /* check that the backup header is properly placed */
1521 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1)
1522 /* TODO: correct this (with user authorization) and write */
1525 if (partition_check_overlaps(gpt
->pheader
, gpt
->ents
))
1528 /* recompute CRCs for both headers */
1529 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1530 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1533 * UEFI requires writing in this specific order:
1534 * 1) backup partition tables
1535 * 2) backup GPT header
1536 * 3) primary partition tables
1537 * 4) primary GPT header
1540 * If any write fails, we abort the rest.
1542 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
1544 if (gpt_write_header(cxt
, gpt
->bheader
,
1545 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
1547 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
1549 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
1552 if (mbr_type
== GPT_MBR_HYBRID
)
1553 fdisk_warnx(cxt
, _("The device contains hybrid MBR -- writing GPT only. "
1554 "You have to sync the MBR manually."));
1555 else if (gpt_write_pmbr(cxt
) != 0)
1558 DBG(LABEL
, ul_debug("GPT write success"));
1561 DBG(LABEL
, ul_debug("GPT write failed: incorrect input"));
1565 DBG(LABEL
, ul_debug("GPT write failed: %m"));
1570 * Verify data integrity and report any found problems for:
1571 * - primary and backup header validations
1572 * - paritition validations
1574 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
1578 struct fdisk_gpt_label
*gpt
;
1582 assert(fdisk_is_label(cxt
, GPT
));
1584 gpt
= self_label(cxt
);
1586 if (!gpt
|| !gpt
->bheader
) {
1588 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
1591 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
1593 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
1595 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
1597 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
1600 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
1602 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
1605 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
1607 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
1609 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
1611 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
1614 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
1616 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
1618 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
1620 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
1623 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
1625 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
1629 * if the GPT is the primary table, check the alternateLBA
1630 * to see if it is a valid GPT
1632 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
1633 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
1635 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
1638 ptnum
= partition_check_overlaps(gpt
->pheader
, gpt
->ents
);
1641 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
1645 ptnum
= partition_check_too_big(gpt
->pheader
, gpt
->ents
, cxt
->total_sectors
);
1648 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
1652 ptnum
= partition_start_after_end(gpt
->pheader
, gpt
->ents
);
1655 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
1659 if (!nerror
) { /* yay :-) */
1660 uint32_t nsegments
= 0;
1661 uint64_t free_sectors
= 0, largest_segment
= 0;
1664 fdisk_info(cxt
, _("No errors detected."));
1665 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
1666 fdisk_info(cxt
, _("Using %u out of %d partitions."),
1667 partitions_in_use(gpt
->pheader
, gpt
->ents
),
1668 le32_to_cpu(gpt
->pheader
->npartition_entries
));
1670 free_sectors
= get_free_sectors(cxt
, gpt
->pheader
, gpt
->ents
,
1671 &nsegments
, &largest_segment
);
1672 if (largest_segment
)
1673 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
| SIZE_SUFFIX_3LETTER
,
1674 largest_segment
* cxt
->sector_size
);
1677 P_("A total of %ju free sectors is available in %u segment.",
1678 "A total of %ju free sectors is available in %u segments "
1679 "(the largest is %s).", nsegments
),
1680 free_sectors
, nsegments
, strsz
);
1685 P_("%d error detected.", "%d errors detected.", nerror
),
1691 /* Delete a single GPT partition, specified by partnum. */
1692 static int gpt_delete_partition(struct fdisk_context
*cxt
,
1695 struct fdisk_gpt_label
*gpt
;
1699 assert(fdisk_is_label(cxt
, GPT
));
1701 gpt
= self_label(cxt
);
1703 if (partnum
>= cxt
->label
->nparts_max
1704 || partition_unused(&gpt
->ents
[partnum
]))
1707 /* hasta la vista, baby! */
1708 memset(&gpt
->ents
[partnum
], 0, sizeof(struct gpt_entry
));
1709 if (!partition_unused(&gpt
->ents
[partnum
]))
1712 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1713 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1714 cxt
->label
->nparts_cur
--;
1715 fdisk_label_set_changed(cxt
->label
, 1);
1721 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
1724 DBG(LABEL
, gpt_debug_uuid("new type", &(e
->type
)));
1727 static void gpt_entry_set_name(struct gpt_entry
*e
, char *str
)
1729 char name
[GPT_PART_NAME_LEN
] = { 0 };
1730 size_t i
, sz
= strlen(str
);
1733 if (sz
> GPT_PART_NAME_LEN
)
1734 sz
= GPT_PART_NAME_LEN
;
1735 memcpy(name
, str
, sz
);
1738 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
1739 e
->name
[i
] = cpu_to_le16((uint16_t) name
[i
]);
1742 static int gpt_entry_set_uuid(struct gpt_entry
*e
, char *str
)
1744 struct gpt_guid uuid
;
1747 rc
= string_to_guid(str
, &uuid
);
1751 e
->partition_guid
= uuid
;
1755 /* Performs logical checks to add a new partition entry */
1756 static int gpt_add_partition(
1757 struct fdisk_context
*cxt
,
1758 struct fdisk_partition
*pa
)
1760 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
1761 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
1762 uint64_t dflt_f
, dflt_l
; /* largest segment (default) */
1763 struct gpt_guid
typeid;
1764 struct fdisk_gpt_label
*gpt
;
1765 struct gpt_header
*pheader
;
1766 struct gpt_entry
*e
, *ents
;
1767 struct fdisk_ask
*ask
= NULL
;
1773 assert(fdisk_is_label(cxt
, GPT
));
1775 gpt
= self_label(cxt
);
1776 pheader
= gpt
->pheader
;
1779 rc
= fdisk_partition_next_partno(pa
, cxt
, &partnum
);
1781 DBG(LABEL
, ul_debug("GPT failed to get next partno"));
1784 if (!partition_unused(&ents
[partnum
])) {
1785 fdisk_warnx(cxt
, _("Partition %zu is already defined. "
1786 "Delete it before re-adding it."), partnum
+1);
1789 if (le32_to_cpu(pheader
->npartition_entries
) ==
1790 partitions_in_use(pheader
, ents
)) {
1791 fdisk_warnx(cxt
, _("All partitions are already in use."));
1794 if (!get_free_sectors(cxt
, pheader
, ents
, NULL
, NULL
)) {
1795 fdisk_warnx(cxt
, _("No free sectors available."));
1799 string_to_guid(pa
&& pa
->type
&& pa
->type
->typestr
?
1801 GPT_DEFAULT_ENTRY_TYPE
, &typeid);
1803 disk_f
= find_first_available(pheader
, ents
, 0);
1804 disk_l
= find_last_free_sector(pheader
, ents
);
1806 /* the default is the largest free space */
1807 dflt_f
= find_first_in_largest(pheader
, ents
);
1808 dflt_l
= find_last_free(pheader
, ents
, dflt_f
);
1810 /* align the default in range <dflt_f,dflt_l>*/
1811 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
1814 if (pa
&& pa
->start
) {
1815 DBG(LABEL
, ul_debug("first sector defined: %ju", pa
->start
));
1816 if (pa
->start
!= find_first_available(pheader
, ents
, pa
->start
)) {
1817 fdisk_warnx(cxt
, _("Sector %ju already used."), pa
->start
);
1821 } else if (pa
&& pa
->start_follow_default
) {
1827 ask
= fdisk_new_ask();
1829 fdisk_reset_ask(ask
);
1832 fdisk_ask_set_query(ask
, _("First sector"));
1833 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
1834 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
1835 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
1836 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
1838 rc
= fdisk_do_ask(cxt
, ask
);
1842 user_f
= fdisk_ask_number_get_result(ask
);
1843 if (user_f
!= find_first_available(pheader
, ents
, user_f
)) {
1844 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
1853 dflt_l
= find_last_free(pheader
, ents
, user_f
);
1855 if (pa
&& pa
->size
) {
1856 user_l
= user_f
+ pa
->size
- 1;
1857 DBG(LABEL
, ul_debug("size defined: %ju, end: %ju (last possible: %ju)",
1858 pa
->size
, user_l
, dflt_l
));
1859 if (user_l
!= dflt_l
)
1860 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1862 } else if (pa
&& pa
->end_follow_default
) {
1867 ask
= fdisk_new_ask();
1869 fdisk_reset_ask(ask
);
1871 fdisk_ask_set_query(ask
, _("Last sector, +sectors or +size{K,M,G,T,P}"));
1872 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
1873 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
1874 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
1875 fdisk_ask_number_set_high(ask
, dflt_l
); /* maximal */
1876 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
1877 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
1879 rc
= fdisk_do_ask(cxt
, ask
);
1883 user_l
= fdisk_ask_number_get_result(ask
);
1884 if (fdisk_ask_number_is_relative(ask
)) {
1885 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1887 /* no space for anything useful, use all space
1888 if (user_l + (cxt->grain / cxt->sector_size) > dflt_l)
1891 } if (user_l
> user_f
&& user_l
<= disk_l
)
1897 if (user_f
> user_l
|| partnum
>= cxt
->label
->nparts_max
) {
1898 fdisk_warnx(cxt
, _("Could not create partition %zu"), partnum
+ 1);
1904 e
->lba_end
= cpu_to_le64(user_l
);
1905 e
->lba_start
= cpu_to_le64(user_f
);
1907 gpt_entry_set_type(e
, &typeid);
1909 if (pa
&& pa
->uuid
) {
1910 /* Sometimes it's necessary to create a copy of the PT and
1911 * reuse already defined UUID
1913 rc
= gpt_entry_set_uuid(e
, pa
->uuid
);
1917 /* Any time a new partition entry is created a new GUID must be
1918 * generated for that partition, and every partition is guaranteed
1919 * to have a unique GUID.
1921 uuid_generate_random((unsigned char *) &e
->partition_guid
);
1922 swap_efi_guid(&e
->partition_guid
);
1925 if (pa
&& pa
->name
&& *pa
->name
)
1926 gpt_entry_set_name(e
, pa
->name
);
1928 DBG(LABEL
, ul_debug("GPT new partition: partno=%zu, start=%ju, end=%ju, size=%ju",
1930 gpt_partition_start(e
),
1931 gpt_partition_end(e
),
1932 gpt_partition_size(e
)));
1934 gpt_recompute_crc(gpt
->pheader
, ents
);
1935 gpt_recompute_crc(gpt
->bheader
, ents
);
1939 struct fdisk_parttype
*t
;
1941 cxt
->label
->nparts_cur
++;
1942 fdisk_label_set_changed(cxt
->label
, 1);
1944 t
= gpt_partition_parttype(cxt
, &ents
[partnum
]);
1945 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
1946 fdisk_free_parttype(t
);
1951 fdisk_free_ask(ask
);
1956 * Create a new GPT disklabel - destroys any previous data.
1958 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
1963 struct fdisk_gpt_label
*gpt
;
1967 assert(fdisk_is_label(cxt
, GPT
));
1969 gpt
= self_label(cxt
);
1971 /* label private stuff has to be empty, see gpt_deinit() */
1972 assert(gpt
->pheader
== NULL
);
1973 assert(gpt
->bheader
== NULL
);
1976 * When no header, entries or pmbr is set, we're probably
1977 * dealing with a new, empty disk - so always allocate memory
1978 * to deal with the data structures whatever the case is.
1980 rc
= gpt_mknew_pmbr(cxt
);
1985 gpt
->pheader
= calloc(1, sizeof(*gpt
->pheader
));
1986 if (!gpt
->pheader
) {
1990 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
1994 /* backup ("copy" primary) */
1995 gpt
->bheader
= calloc(1, sizeof(*gpt
->bheader
));
1996 if (!gpt
->bheader
) {
2000 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
2001 last_lba(cxt
), gpt
->pheader
);
2005 esz
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
2006 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
2007 gpt
->ents
= calloc(1, esz
);
2012 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2013 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2015 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2016 cxt
->label
->nparts_cur
= 0;
2018 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
2019 fdisk_label_set_changed(cxt
->label
, 1);
2020 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2021 _("Created a new GPT disklabel (GUID: %s)."), str
);
2026 static int gpt_get_disklabel_id(struct fdisk_context
*cxt
, char **id
)
2028 struct fdisk_gpt_label
*gpt
;
2034 assert(fdisk_is_label(cxt
, GPT
));
2036 gpt
= self_label(cxt
);
2037 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
2045 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
)
2047 struct fdisk_gpt_label
*gpt
;
2048 struct gpt_guid uuid
;
2049 char *str
, *old
, *new;
2054 assert(fdisk_is_label(cxt
, GPT
));
2056 gpt
= self_label(cxt
);
2057 if (fdisk_ask_string(cxt
,
2058 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str
))
2061 rc
= string_to_guid(str
, &uuid
);
2065 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2069 gpt_get_disklabel_id(cxt
, &old
);
2071 gpt
->pheader
->disk_guid
= uuid
;
2072 gpt
->bheader
->disk_guid
= uuid
;
2074 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2075 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2077 gpt_get_disklabel_id(cxt
, &new);
2079 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2080 _("Disk identifier changed from %s to %s."), old
, new);
2084 fdisk_label_set_changed(cxt
->label
, 1);
2088 static int gpt_set_partition_type(
2089 struct fdisk_context
*cxt
,
2091 struct fdisk_parttype
*t
)
2093 struct gpt_guid uuid
;
2094 struct fdisk_gpt_label
*gpt
;
2098 assert(fdisk_is_label(cxt
, GPT
));
2100 gpt
= self_label(cxt
);
2101 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
)
2102 || !t
|| !t
->typestr
|| string_to_guid(t
->typestr
, &uuid
) != 0)
2105 gpt_entry_set_type(&gpt
->ents
[i
], &uuid
);
2106 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2107 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2109 fdisk_label_set_changed(cxt
->label
, 1);
2113 static int gpt_part_is_used(struct fdisk_context
*cxt
, size_t i
)
2115 struct fdisk_gpt_label
*gpt
;
2116 struct gpt_entry
*e
;
2120 assert(fdisk_is_label(cxt
, GPT
));
2122 gpt
= self_label(cxt
);
2124 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2128 return !partition_unused(e
) || gpt_partition_start(e
);
2131 int fdisk_gpt_partition_set_uuid(struct fdisk_context
*cxt
, size_t i
)
2133 struct fdisk_gpt_label
*gpt
;
2134 struct gpt_entry
*e
;
2135 char *str
, new_u
[37], old_u
[37];
2140 assert(fdisk_is_label(cxt
, GPT
));
2142 DBG(LABEL
, ul_debug("UUID change requested partno=%zu", i
));
2144 gpt
= self_label(cxt
);
2146 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2149 if (fdisk_ask_string(cxt
,
2150 _("New UUID (in 8-4-4-4-12 format)"), &str
))
2154 guid_to_string(&e
->partition_guid
, old_u
);
2156 rc
= gpt_entry_set_uuid(e
, str
);
2161 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2165 guid_to_string(&e
->partition_guid
, new_u
);
2167 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2168 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2169 fdisk_label_set_changed(cxt
->label
, 1);
2171 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2172 _("Partition UUID changed from %s to %s."),
2177 int fdisk_gpt_partition_set_name(struct fdisk_context
*cxt
, size_t i
)
2179 struct fdisk_gpt_label
*gpt
;
2180 struct gpt_entry
*e
;
2185 assert(fdisk_is_label(cxt
, GPT
));
2187 DBG(LABEL
, ul_debug("NAME change requested partno=%zu", i
));
2189 gpt
= self_label(cxt
);
2191 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2194 if (fdisk_ask_string(cxt
, _("New name"), &str
))
2198 old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
2200 gpt_entry_set_name(e
, str
);
2202 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2203 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2205 fdisk_label_set_changed(cxt
->label
, 1);
2207 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2208 _("Partition name changed from '%s' to '%.*s'."),
2209 old
, (int) GPT_PART_NAME_LEN
, str
);
2216 int fdisk_gpt_is_hybrid(struct fdisk_context
*cxt
)
2219 return valid_pmbr(cxt
) == GPT_MBR_HYBRID
;
2222 static int gpt_toggle_partition_flag(
2223 struct fdisk_context
*cxt
,
2227 struct fdisk_gpt_label
*gpt
;
2228 uint64_t attrs
, tmp
;
2230 const char *name
= NULL
;
2235 assert(fdisk_is_label(cxt
, GPT
));
2237 DBG(LABEL
, ul_debug("GPT entry attribute change requested partno=%zu", i
));
2238 gpt
= self_label(cxt
);
2240 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2243 attrs
= le64_to_cpu(gpt
->ents
[i
].attrs
);
2244 bits
= (char *) &attrs
;
2247 case GPT_FLAG_REQUIRED
:
2248 bit
= GPT_ATTRBIT_REQ
;
2249 name
= GPT_ATTRSTR_REQ
;
2251 case GPT_FLAG_NOBLOCK
:
2252 bit
= GPT_ATTRBIT_NOBLOCK
;
2253 name
= GPT_ATTRSTR_NOBLOCK
;
2255 case GPT_FLAG_LEGACYBOOT
:
2256 bit
= GPT_ATTRBIT_LEGACY
;
2257 name
= GPT_ATTRSTR_LEGACY
;
2259 case GPT_FLAG_GUIDSPECIFIC
:
2260 rc
= fdisk_ask_number(cxt
, 48, 48, 63, _("Enter GUID specific bit"), &tmp
);
2266 /* already specified PT_FLAG_GUIDSPECIFIC bit */
2267 if (flag
>= 48 && flag
<= 63) {
2269 flag
= GPT_FLAG_GUIDSPECIFIC
;
2275 fdisk_warnx(cxt
, _("failed to toggle unsupported bit %lu"), flag
);
2279 if (!isset(bits
, bit
))
2284 gpt
->ents
[i
].attrs
= cpu_to_le64(attrs
);
2286 if (flag
== GPT_FLAG_GUIDSPECIFIC
)
2287 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2289 _("The GUID specific bit %d on partition %zu is enabled now.") :
2290 _("The GUID specific bit %d on partition %zu is disabled now."),
2293 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2295 _("The %s flag on partition %zu is enabled now.") :
2296 _("The %s flag on partition %zu is disabled now."),
2299 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2300 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2301 fdisk_label_set_changed(cxt
->label
, 1);
2305 static int gpt_entry_cmp_start(const void *a
, const void *b
)
2307 struct gpt_entry
*ae
= (struct gpt_entry
*) a
,
2308 *be
= (struct gpt_entry
*) b
;
2309 int au
= partition_unused(ae
),
2310 bu
= partition_unused(be
);
2319 return gpt_partition_start(ae
) - gpt_partition_start(be
);
2322 /* sort partition by start sector */
2323 static int gpt_reorder(struct fdisk_context
*cxt
)
2325 struct fdisk_gpt_label
*gpt
;
2330 assert(fdisk_is_label(cxt
, GPT
));
2332 gpt
= self_label(cxt
);
2333 nparts
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2335 qsort(gpt
->ents
, nparts
, sizeof(struct gpt_entry
),
2336 gpt_entry_cmp_start
);
2338 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2339 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2340 fdisk_label_set_changed(cxt
->label
, 1);
2342 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
, _("Done."));
2346 static int gpt_reset_alignment(struct fdisk_context
*cxt
)
2348 struct fdisk_gpt_label
*gpt
;
2349 struct gpt_header
*h
;
2353 assert(fdisk_is_label(cxt
, GPT
));
2355 gpt
= self_label(cxt
);
2356 h
= gpt
? gpt
->pheader
: NULL
;
2359 /* always follow existing table */
2360 cxt
->first_lba
= h
->first_usable_lba
;
2361 cxt
->last_lba
= h
->last_usable_lba
;
2363 /* estimate ranges for GPT */
2364 uint64_t first
, last
;
2366 count_first_last_lba(cxt
, &first
, &last
);
2368 if (cxt
->first_lba
< first
)
2369 cxt
->first_lba
= first
;
2370 if (cxt
->last_lba
> last
)
2371 cxt
->last_lba
= last
;
2377 * Deinitialize fdisk-specific variables
2379 static void gpt_deinit(struct fdisk_label
*lb
)
2381 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
2391 gpt
->pheader
= NULL
;
2392 gpt
->bheader
= NULL
;
2395 static const struct fdisk_label_operations gpt_operations
=
2397 .probe
= gpt_probe_label
,
2398 .write
= gpt_write_disklabel
,
2399 .verify
= gpt_verify_disklabel
,
2400 .create
= gpt_create_disklabel
,
2401 .list
= gpt_list_disklabel
,
2402 .locate
= gpt_locate_disklabel
,
2403 .reorder
= gpt_reorder
,
2404 .get_id
= gpt_get_disklabel_id
,
2405 .set_id
= gpt_set_disklabel_id
,
2407 .get_part
= gpt_get_partition
,
2408 .add_part
= gpt_add_partition
,
2410 .part_delete
= gpt_delete_partition
,
2412 .part_is_used
= gpt_part_is_used
,
2413 .part_set_type
= gpt_set_partition_type
,
2414 .part_toggle_flag
= gpt_toggle_partition_flag
,
2416 .deinit
= gpt_deinit
,
2418 .reset_alignment
= gpt_reset_alignment
2421 static const struct fdisk_field gpt_fields
[] =
2424 { FDISK_FIELD_DEVICE
, N_("Device"), 10, 0 },
2425 { FDISK_FIELD_START
, N_("Start"), 5, FDISK_FIELDFL_NUMBER
},
2426 { FDISK_FIELD_END
, N_("End"), 5, FDISK_FIELDFL_NUMBER
},
2427 { FDISK_FIELD_SECTORS
, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER
},
2428 { FDISK_FIELD_CYLINDERS
,N_("Cylinders"), 5, FDISK_FIELDFL_NUMBER
},
2429 { FDISK_FIELD_SIZE
, N_("Size"), 5, FDISK_FIELDFL_NUMBER
| FDISK_FIELDFL_EYECANDY
},
2430 { FDISK_FIELD_TYPE
, N_("Type"), 0.1, FDISK_FIELDFL_EYECANDY
},
2432 { FDISK_FIELD_TYPEID
, N_("Type-UUID"), 36, FDISK_FIELDFL_DETAIL
},
2433 { FDISK_FIELD_UUID
, N_("UUID"), 36, FDISK_FIELDFL_DETAIL
},
2434 { FDISK_FIELD_NAME
, N_("Name"), 0.2, FDISK_FIELDFL_DETAIL
},
2435 { FDISK_FIELD_ATTR
, N_("Attrs"), 0, FDISK_FIELDFL_DETAIL
}
2439 * allocates GPT in-memory stuff
2441 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
)
2443 struct fdisk_label
*lb
;
2444 struct fdisk_gpt_label
*gpt
;
2448 gpt
= calloc(1, sizeof(*gpt
));
2452 /* initialize generic part of the driver */
2453 lb
= (struct fdisk_label
*) gpt
;
2455 lb
->id
= FDISK_DISKLABEL_GPT
;
2456 lb
->op
= &gpt_operations
;
2457 lb
->parttypes
= gpt_parttypes
;
2458 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);
2460 lb
->fields
= gpt_fields
;
2461 lb
->nfields
= ARRAY_SIZE(gpt_fields
);