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>
21 #include <libsmartcols.h>
32 #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */
33 #define GPT_HEADER_REVISION_V1_02 0x00010200
34 #define GPT_HEADER_REVISION_V1_00 0x00010000
35 #define GPT_HEADER_REVISION_V0_99 0x00009900
36 #define GPT_HEADER_MINSZ 92 /* bytes */
38 #define GPT_PMBR_LBA 0
39 #define GPT_MBR_PROTECTIVE 1
40 #define GPT_MBR_HYBRID 2
42 #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001
44 #define EFI_PMBR_OSTYPE 0xEE
45 #define MSDOS_MBR_SIGNATURE 0xAA55
46 #define GPT_PART_NAME_LEN (72 / sizeof(uint16_t))
47 #define GPT_NPARTITIONS 128
49 /* Globally unique identifier */
53 uint16_t time_hi_and_version
;
55 uint8_t clock_seq_low
;
60 /* only checking that the GUID is 0 is enough to verify an empty partition. */
61 #define GPT_UNUSED_ENTRY_GUID \
62 ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
63 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
65 /* Linux native partition type */
66 #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
74 GPT_ATTRBIT_NOBLOCK
= 1,
75 GPT_ATTRBIT_LEGACY
= 2,
77 /* GUID specific (range 48..64)*/
78 GPT_ATTRBIT_GUID_FIRST
= 48,
79 GPT_ATTRBIT_GUID_COUNT
= 16
82 #define GPT_ATTRSTR_REQ "RequiredPartiton"
83 #define GPT_ATTRSTR_NOBLOCK "NoBlockIOProtocol"
84 #define GPT_ATTRSTR_LEGACY "LegacyBIOSBootable"
86 /* The GPT Partition entry array contains an array of GPT entries. */
88 struct gpt_guid type
; /* purpose and type of the partition */
89 struct gpt_guid partition_guid
;
93 uint16_t name
[GPT_PART_NAME_LEN
];
94 } __attribute__ ((packed
));
98 uint64_t signature
; /* header identification */
99 uint32_t revision
; /* header version */
100 uint32_t size
; /* in bytes */
101 uint32_t crc32
; /* header CRC checksum */
102 uint32_t reserved1
; /* must be 0 */
103 uint64_t my_lba
; /* LBA that contains this struct (LBA 1) */
104 uint64_t alternative_lba
; /* backup GPT header */
105 uint64_t first_usable_lba
; /* first usable logical block for partitions */
106 uint64_t last_usable_lba
; /* last usable logical block for partitions */
107 struct gpt_guid disk_guid
; /* unique disk identifier */
108 uint64_t partition_entry_lba
; /* stat LBA of the partition entry array */
109 uint32_t npartition_entries
; /* total partition entries - normally 128 */
110 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
111 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
112 uint8_t reserved2
[512 - 92]; /* must be 0 */
113 } __attribute__ ((packed
));
116 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
117 uint8_t start_head
; /* unused by EFI, pt start in CHS */
118 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
120 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
121 uint8_t end_head
; /* unused by EFI, pt end in CHS */
122 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
123 uint8_t end_track
; /* unused by EFI, pt end in CHS */
124 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
125 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
126 } __attribute__ ((packed
));
128 /* Protected MBR and legacy MBR share same structure */
129 struct gpt_legacy_mbr
{
130 uint8_t boot_code
[440];
131 uint32_t unique_mbr_signature
;
133 struct gpt_record partition_record
[4];
135 } __attribute__ ((packed
));
139 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
141 #define DEF_GUID(_u, _n) \
147 static struct fdisk_parttype gpt_parttypes
[] =
150 DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")),
152 DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")),
153 DEF_GUID("D3BFE2DE-3DAF-11DF-BA40-E3A556D89593", N_("Intel Fast Flash")),
155 /* Hah!IdontneedEFI */
156 DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot")),
159 DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
160 DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
161 DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")),
162 DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")),
163 DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery environment")),
164 DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")),
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_get_parttype_from_string(cxt
, 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
;
512 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
513 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
514 header
->size
= cpu_to_le32(sizeof(struct gpt_header
));
517 * 128 partitions are the default. It can go beyond that, but
518 * we're creating a de facto header here, so no funny business.
520 header
->npartition_entries
= cpu_to_le32(GPT_NPARTITIONS
);
521 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
523 count_first_last_lba(cxt
, &first
, &last
);
524 header
->first_usable_lba
= cpu_to_le64(first
);
525 header
->last_usable_lba
= cpu_to_le64(last
);
527 gpt_mknew_header_common(cxt
, header
, lba
);
528 uuid_generate_random((unsigned char *) &header
->disk_guid
);
529 swap_efi_guid(&header
->disk_guid
);
535 * Checks if there is a valid protective MBR partition table.
536 * Returns 0 if it is invalid or failure. Otherwise, return
537 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection.
539 static int valid_pmbr(struct fdisk_context
*cxt
)
541 int i
, part
= 0, ret
= 0; /* invalid by default */
542 struct gpt_legacy_mbr
*pmbr
= NULL
;
545 if (!cxt
->firstsector
)
548 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
550 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
553 /* LBA of the GPT partition header */
554 if (pmbr
->partition_record
[0].starting_lba
!=
555 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
558 /* seems like a valid MBR was found, check DOS primary partitions */
559 for (i
= 0; i
< 4; i
++) {
560 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
562 * Ok, we at least know that there's a protective MBR,
563 * now check if there are other partition types for
567 ret
= GPT_MBR_PROTECTIVE
;
572 if (ret
!= GPT_MBR_PROTECTIVE
)
575 for (i
= 0 ; i
< 4; i
++) {
576 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
577 (pmbr
->partition_record
[i
].os_type
!= 0x00))
578 ret
= GPT_MBR_HYBRID
;
582 * Protective MBRs take up the lesser of the whole disk
583 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
584 * Some partitioning programs, nonetheless, choose to set
585 * the size to the maximum 32-bit limitation, disregarding
588 * Hybrid MBRs do not necessarily comply with this.
590 * Consider a bad value here to be a warning to support dd-ing
591 * an image from a smaller disk to a bigger disk.
593 if (ret
== GPT_MBR_PROTECTIVE
) {
594 sz_lba
= le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
595 if (sz_lba
!= (uint32_t) cxt
->total_sectors
- 1 && sz_lba
!= 0xFFFFFFFF) {
596 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%u != %u) "
597 "will be corrected by w(rite)."),
599 (uint32_t) cxt
->total_sectors
- 1);
600 fdisk_label_set_changed(cxt
->label
, 1);
607 static uint64_t last_lba(struct fdisk_context
*cxt
)
610 uint64_t sectors
= 0;
612 memset(&s
, 0, sizeof(s
));
613 if (fstat(cxt
->dev_fd
, &s
) == -1) {
614 fdisk_warn(cxt
, _("gpt: stat() failed"));
618 if (S_ISBLK(s
.st_mode
))
619 sectors
= cxt
->total_sectors
- 1;
620 else if (S_ISREG(s
.st_mode
))
621 sectors
= ((uint64_t) s
.st_size
/
622 (uint64_t) cxt
->sector_size
) - 1ULL;
624 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
626 DBG(LABEL
, ul_debug("GPT last LBA: %ju", sectors
));
630 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
631 void *buffer
, const size_t bytes
)
633 off_t offset
= lba
* cxt
->sector_size
;
635 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
637 return read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
641 /* Returns the GPT entry array */
642 static struct gpt_entry
*gpt_read_entries(struct fdisk_context
*cxt
,
643 struct gpt_header
*header
)
646 struct gpt_entry
*ret
= NULL
;
652 sz
= le32_to_cpu(header
->npartition_entries
) *
653 le32_to_cpu(header
->sizeof_partition_entry
);
658 offset
= le64_to_cpu(header
->partition_entry_lba
) *
661 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
663 if (sz
!= read(cxt
->dev_fd
, ret
, sz
))
673 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
)
675 return (crc32(~0L, buf
, len
) ^ ~0L);
679 * Recompute header and partition array 32bit CRC checksums.
680 * This function does not fail - if there's corruption, then it
681 * will be reported when checksuming it again (ie: probing or verify).
683 static void gpt_recompute_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
693 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
694 header
->crc32
= cpu_to_le32(crc
);
696 /* partition entry array CRC */
697 header
->partition_entry_array_crc32
= 0;
698 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
699 le32_to_cpu(header
->sizeof_partition_entry
);
701 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
702 header
->partition_entry_array_crc32
= cpu_to_le32(crc
);
706 * Compute the 32bit CRC checksum of the partition table header.
707 * Returns 1 if it is valid, otherwise 0.
709 static int gpt_check_header_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
711 uint32_t crc
, orgcrc
= le32_to_cpu(header
->crc32
);
714 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
715 header
->crc32
= cpu_to_le32(orgcrc
);
717 if (crc
== le32_to_cpu(header
->crc32
))
721 * If we have checksum mismatch it may be due to stale data,
722 * like a partition being added or deleted. Recompute the CRC again
723 * and make sure this is not the case.
726 gpt_recompute_crc(header
, ents
);
727 orgcrc
= le32_to_cpu(header
->crc32
);
729 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
730 header
->crc32
= cpu_to_le32(orgcrc
);
732 return crc
== le32_to_cpu(header
->crc32
);
739 * It initializes the partition entry array.
740 * Returns 1 if the checksum is valid, otherwise 0.
742 static int gpt_check_entryarr_crc(struct gpt_header
*header
,
743 struct gpt_entry
*ents
)
749 if (!header
|| !ents
)
752 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
753 le32_to_cpu(header
->sizeof_partition_entry
);
758 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
759 ret
= (crc
== le32_to_cpu(header
->partition_entry_array_crc32
));
764 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
767 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
769 fu
= le64_to_cpu(header
->first_usable_lba
);
770 lu
= le64_to_cpu(header
->last_usable_lba
);
772 /* check if first and last usable LBA make sense */
774 DBG(LABEL
, ul_debug("error: header last LBA is before first LBA"));
778 /* check if first and last usable LBAs with the disk's last LBA */
779 if (fu
> lastlba
|| lu
> lastlba
) {
780 DBG(LABEL
, ul_debug("error: header LBAs are after the disk's last LBA"));
784 /* the header has to be outside usable range */
785 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
786 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
787 DBG(LABEL
, ul_debug("error: header outside of usable range"));
796 /* Check if there is a valid header signature */
797 static int gpt_check_signature(struct gpt_header
*header
)
799 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
803 * Return the specified GPT Header, or NULL upon failure/invalid.
804 * Note that all tests must pass to ensure a valid header,
805 * we do not rely on only testing the signature for a valid probe.
807 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
809 struct gpt_entry
**_ents
)
811 struct gpt_header
*header
= NULL
;
812 struct gpt_entry
*ents
= NULL
;
818 header
= calloc(1, sizeof(*header
));
822 /* read and verify header */
823 if (read_lba(cxt
, lba
, header
, sizeof(struct gpt_header
)) != 0)
826 if (!gpt_check_signature(header
))
829 if (!gpt_check_header_crc(header
, NULL
))
832 /* read and verify entries */
833 ents
= gpt_read_entries(cxt
, header
);
837 if (!gpt_check_entryarr_crc(header
, ents
))
840 if (!gpt_check_lba_sanity(cxt
, header
))
843 /* valid header must be at MyLBA */
844 if (le64_to_cpu(header
->my_lba
) != lba
)
847 /* make sure header size is between 92 and sector size bytes */
848 hsz
= le32_to_cpu(header
->size
);
849 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
857 DBG(LABEL
, ul_debug("found valid GPT Header on LBA %ju", lba
));
863 DBG(LABEL
, ul_debug("read GPT Header on LBA %ju failed", lba
));
868 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
869 const char **name
, off_t
*offset
, size_t *size
)
871 struct fdisk_gpt_label
*gpt
;
886 *name
= _("GPT Header");
887 *offset
= GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
888 *size
= sizeof(struct gpt_header
);
891 *name
= _("GPT Entries");
892 gpt
= self_label(cxt
);
893 *offset
= le64_to_cpu(gpt
->pheader
->partition_entry_lba
) * cxt
->sector_size
;
894 *size
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
895 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
898 return 1; /* no more chunks */
907 * Returns the number of partitions that are in use.
909 static unsigned partitions_in_use(struct gpt_header
*header
, struct gpt_entry
*e
)
911 uint32_t i
, used
= 0;
916 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
917 if (!partition_unused(&e
[i
]))
924 * Check if a partition is too big for the disk (sectors).
925 * Returns the faulting partition number, otherwise 0.
927 static uint32_t partition_check_too_big(struct gpt_header
*header
,
928 struct gpt_entry
*e
, uint64_t sectors
)
932 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
933 if (partition_unused(&e
[i
]))
935 if (gpt_partition_end(&e
[i
]) >= sectors
)
943 * Check if a partition ends before it begins
944 * Returns the faulting partition number, otherwise 0.
946 static uint32_t partition_start_after_end(struct gpt_header
*header
, struct gpt_entry
*e
)
950 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
951 if (partition_unused(&e
[i
]))
953 if (gpt_partition_start(&e
[i
]) > gpt_partition_end(&e
[i
]))
961 * Check if partition e1 overlaps with partition e2.
963 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
965 uint64_t start1
= gpt_partition_start(e1
);
966 uint64_t end1
= gpt_partition_end(e1
);
967 uint64_t start2
= gpt_partition_start(e2
);
968 uint64_t end2
= gpt_partition_end(e2
);
970 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
974 * Find any partitions that overlap.
976 static uint32_t partition_check_overlaps(struct gpt_header
*header
, struct gpt_entry
*e
)
980 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
981 for (j
= 0; j
< i
; j
++) {
982 if (partition_unused(&e
[i
]) ||
983 partition_unused(&e
[j
]))
985 if (partition_overlap(&e
[i
], &e
[j
])) {
986 DBG(LABEL
, ul_debug("GPT partitions overlap detected [%u vs. %u]", i
, j
));
995 * Find the first available block after the starting point; returns 0 if
996 * there are no available blocks left, or error. From gdisk.
998 static uint64_t find_first_available(struct gpt_header
*header
,
999 struct gpt_entry
*e
, uint64_t start
)
1002 uint32_t i
, first_moved
= 0;
1009 fu
= le64_to_cpu(header
->first_usable_lba
);
1010 lu
= le64_to_cpu(header
->last_usable_lba
);
1013 * Begin from the specified starting point or from the first usable
1014 * LBA, whichever is greater...
1016 first
= start
< fu
? fu
: start
;
1019 * Now search through all partitions; if first is within an
1020 * existing partition, move it to the next sector after that
1021 * partition and repeat. If first was moved, set firstMoved
1022 * flag; repeat until firstMoved is not set, so as to catch
1023 * cases where partitions are out of sequential order....
1027 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1028 if (partition_unused(&e
[i
]))
1030 if (first
< gpt_partition_start(&e
[i
]))
1032 if (first
<= gpt_partition_end(&e
[i
])) {
1033 first
= gpt_partition_end(&e
[i
]) + 1;
1037 } while (first_moved
== 1);
1046 /* Returns last available sector in the free space pointed to by start. From gdisk. */
1047 static uint64_t find_last_free(struct gpt_header
*header
,
1048 struct gpt_entry
*e
, uint64_t start
)
1051 uint64_t nearest_start
;
1056 nearest_start
= le64_to_cpu(header
->last_usable_lba
);
1058 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1059 uint64_t ps
= gpt_partition_start(&e
[i
]);
1061 if (nearest_start
> ps
&& ps
> start
)
1062 nearest_start
= ps
- 1;
1065 return nearest_start
;
1068 /* Returns the last free sector on the disk. From gdisk. */
1069 static uint64_t find_last_free_sector(struct gpt_header
*header
,
1070 struct gpt_entry
*e
)
1072 uint32_t i
, last_moved
;
1078 /* start by assuming the last usable LBA is available */
1079 last
= le64_to_cpu(header
->last_usable_lba
);
1082 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1083 if ((last
>= gpt_partition_start(&e
[i
])) &&
1084 (last
<= gpt_partition_end(&e
[i
]))) {
1085 last
= gpt_partition_start(&e
[i
]) - 1;
1089 } while (last_moved
== 1);
1095 * Finds the first available sector in the largest block of unallocated
1096 * space on the disk. Returns 0 if there are no available blocks left.
1099 static uint64_t find_first_in_largest(struct gpt_header
*header
, struct gpt_entry
*e
)
1101 uint64_t start
= 0, first_sect
, last_sect
;
1102 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1108 first_sect
= find_first_available(header
, e
, start
);
1109 if (first_sect
!= 0) {
1110 last_sect
= find_last_free(header
, e
, first_sect
);
1111 segment_size
= last_sect
- first_sect
+ 1;
1113 if (segment_size
> selected_size
) {
1114 selected_size
= segment_size
;
1115 selected_segment
= first_sect
;
1117 start
= last_sect
+ 1;
1119 } while (first_sect
!= 0);
1122 return selected_segment
;
1126 * Find the total number of free sectors, the number of segments in which
1127 * they reside, and the size of the largest of those segments. From gdisk.
1129 static uint64_t get_free_sectors(struct fdisk_context
*cxt
, struct gpt_header
*header
,
1130 struct gpt_entry
*e
, uint32_t *nsegments
,
1131 uint64_t *largest_segment
)
1134 uint64_t first_sect
, last_sect
;
1135 uint64_t largest_seg
= 0, segment_sz
;
1136 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1138 if (!cxt
->total_sectors
)
1142 first_sect
= find_first_available(header
, e
, start
);
1144 last_sect
= find_last_free(header
, e
, first_sect
);
1145 segment_sz
= last_sect
- first_sect
+ 1;
1147 if (segment_sz
> largest_seg
)
1148 largest_seg
= segment_sz
;
1149 totfound
+= segment_sz
;
1151 start
= last_sect
+ 1;
1153 } while (first_sect
);
1158 if (largest_segment
)
1159 *largest_segment
= largest_seg
;
1164 static int gpt_probe_label(struct fdisk_context
*cxt
)
1167 struct fdisk_gpt_label
*gpt
;
1171 assert(fdisk_is_disklabel(cxt
, GPT
));
1173 gpt
= self_label(cxt
);
1175 /* TODO: it would be nice to support scenario when GPT headers are OK,
1176 * but PMBR is corrupt */
1177 mbr_type
= valid_pmbr(cxt
);
1181 DBG(LABEL
, ul_debug("found a %s MBR", mbr_type
== GPT_MBR_PROTECTIVE
?
1182 "protective" : "hybrid"));
1184 /* primary header */
1185 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1189 /* primary OK, try backup from alternative LBA */
1190 gpt
->bheader
= gpt_read_header(cxt
,
1191 le64_to_cpu(gpt
->pheader
->alternative_lba
),
1194 /* primary corrupted -- try last LBA */
1195 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), &gpt
->ents
);
1197 if (!gpt
->pheader
&& !gpt
->bheader
)
1200 /* primary OK, backup corrupted -- recovery */
1201 if (gpt
->pheader
&& !gpt
->bheader
) {
1202 fdisk_warnx(cxt
, _("The backup GPT table is corrupt, but the "
1203 "primary appears OK, so that will be used."));
1204 gpt
->bheader
= gpt_copy_header(cxt
, gpt
->pheader
);
1207 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1209 /* primary corrupted, backup OK -- recovery */
1210 } else if (!gpt
->pheader
&& gpt
->bheader
) {
1211 fdisk_warnx(cxt
, _("The primary GPT table is corrupt, but the "
1212 "backup appears OK, so that will be used."));
1213 gpt
->pheader
= gpt_copy_header(cxt
, gpt
->bheader
);
1216 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1219 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1220 cxt
->label
->nparts_cur
= partitions_in_use(gpt
->pheader
, gpt
->ents
);
1223 DBG(LABEL
, ul_debug("GPT probe failed"));
1224 gpt_deinit(cxt
->label
);
1229 * Stolen from libblkid - can be removed once partition semantics
1230 * are added to the fdisk API.
1232 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1236 size_t i
, j
, len
= count
;
1238 dest
= calloc(1, count
);
1242 for (j
= i
= 0; i
+ 2 <= count
; i
+= 2) {
1243 /* always little endian */
1244 c
= (src
[i
+1] << 8) | src
[i
];
1248 } else if (c
< 0x80) {
1251 dest
[j
++] = (uint8_t) c
;
1252 } else if (c
< 0x800) {
1255 dest
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
1256 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1260 dest
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
1261 dest
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
1262 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1270 static int gpt_entry_attrs_to_string(struct gpt_entry
*e
, char **res
)
1272 unsigned int n
, count
= 0;
1281 attrs
= le64_to_cpu(e
->attrs
);
1283 return 0; /* no attributes at all */
1285 bits
= (char *) &attrs
;
1287 /* Note that sizeof() is correct here, we need separators between
1288 * the strings so also count \0 is correct */
1289 *res
= calloc(1, sizeof(GPT_ATTRSTR_NOBLOCK
) +
1290 sizeof(GPT_ATTRSTR_REQ
) +
1291 sizeof(GPT_ATTRSTR_LEGACY
) +
1292 sizeof("GUID:") + (GPT_ATTRBIT_GUID_COUNT
* 3));
1297 if (isset(bits
, GPT_ATTRBIT_REQ
)) {
1298 memcpy(p
, GPT_ATTRSTR_REQ
, (l
= sizeof(GPT_ATTRSTR_REQ
)));
1301 if (isset(bits
, GPT_ATTRBIT_NOBLOCK
)) {
1304 memcpy(p
, GPT_ATTRSTR_NOBLOCK
, (l
= sizeof(GPT_ATTRSTR_NOBLOCK
)));
1307 if (isset(bits
, GPT_ATTRBIT_LEGACY
)) {
1310 memcpy(p
, GPT_ATTRSTR_LEGACY
, (l
= sizeof(GPT_ATTRSTR_LEGACY
)));
1314 for (n
= GPT_ATTRBIT_GUID_FIRST
;
1315 n
< GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
; n
++) {
1317 if (!isset(bits
, n
))
1322 p
+= sprintf(p
, "GUID:%u", n
);
1324 p
+= sprintf(p
, ",%u", n
);
1331 static int gpt_get_partition(struct fdisk_context
*cxt
, size_t n
,
1332 struct fdisk_partition
*pa
)
1334 struct fdisk_gpt_label
*gpt
;
1335 struct gpt_entry
*e
;
1341 assert(fdisk_is_disklabel(cxt
, GPT
));
1343 gpt
= self_label(cxt
);
1345 if ((uint32_t) n
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1348 gpt
= self_label(cxt
);
1351 pa
->used
= !partition_unused(e
) || gpt_partition_start(e
);
1355 pa
->start
= gpt_partition_start(e
);
1356 pa
->end
= gpt_partition_end(e
);
1357 pa
->size
= gpt_partition_size(e
);
1358 pa
->type
= gpt_partition_parttype(cxt
, e
);
1360 if (guid_to_string(&e
->partition_guid
, u_str
)) {
1361 pa
->uuid
= strdup(u_str
);
1369 rc
= gpt_entry_attrs_to_string(e
, &pa
->attrs
);
1373 pa
->name
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1376 fdisk_reset_partition(pa
);
1382 * List label partitions.
1384 static int gpt_list_disklabel(struct fdisk_context
*cxt
)
1388 assert(fdisk_is_disklabel(cxt
, GPT
));
1390 if (fdisk_context_display_details(cxt
)) {
1391 struct gpt_header
*h
= self_label(cxt
)->pheader
;
1393 fdisk_info(cxt
, _("First LBA: %ju"), h
->first_usable_lba
);
1394 fdisk_info(cxt
, _("Last LBA: %ju"), h
->last_usable_lba
);
1395 fdisk_info(cxt
, _("Alternative LBA: %ju"), h
->alternative_lba
);
1396 fdisk_info(cxt
, _("Partitions entries LBA: %ju"), h
->partition_entry_lba
);
1397 fdisk_info(cxt
, _("Allocated partition entries: %u"), h
->npartition_entries
);
1405 * Returns 0 on success, or corresponding error otherwise.
1407 static int gpt_write_partitions(struct fdisk_context
*cxt
,
1408 struct gpt_header
*header
, struct gpt_entry
*ents
)
1410 off_t offset
= le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
;
1411 uint32_t nparts
= le32_to_cpu(header
->npartition_entries
);
1412 uint32_t totwrite
= nparts
* le32_to_cpu(header
->sizeof_partition_entry
);
1415 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1418 rc
= write(cxt
->dev_fd
, ents
, totwrite
);
1419 if (rc
> 0 && totwrite
== (uint32_t) rc
)
1426 * Write a GPT header to a specified LBA
1427 * Returns 0 on success, or corresponding error otherwise.
1429 static int gpt_write_header(struct fdisk_context
*cxt
,
1430 struct gpt_header
*header
, uint64_t lba
)
1432 off_t offset
= lba
* cxt
->sector_size
;
1434 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1436 if (cxt
->sector_size
==
1437 (size_t) write(cxt
->dev_fd
, header
, cxt
->sector_size
))
1444 * Write the protective MBR.
1445 * Returns 0 on success, or corresponding error otherwise.
1447 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
1450 struct gpt_legacy_mbr
*pmbr
= NULL
;
1453 assert(cxt
->firstsector
);
1455 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
1457 /* zero out the legacy partitions */
1458 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
1460 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
1461 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
1462 pmbr
->partition_record
[0].start_sector
= 1;
1463 pmbr
->partition_record
[0].end_head
= 0xFE;
1464 pmbr
->partition_record
[0].end_sector
= 0xFF;
1465 pmbr
->partition_record
[0].end_track
= 0xFF;
1466 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
1469 * Set size_in_lba to the size of the disk minus one. If the size of the disk
1470 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
1472 if (cxt
->total_sectors
- 1 > 0xFFFFFFFFULL
)
1473 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
1475 pmbr
->partition_record
[0].size_in_lba
=
1476 cpu_to_le32(cxt
->total_sectors
- 1UL);
1478 offset
= GPT_PMBR_LBA
* cxt
->sector_size
;
1479 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1482 /* pMBR covers the first sector (LBA) of the disk */
1483 if (write_all(cxt
->dev_fd
, pmbr
, cxt
->sector_size
))
1491 * Writes in-memory GPT and pMBR data to disk.
1492 * Returns 0 if successful write, otherwise, a corresponding error.
1493 * Any indication of error will abort the operation.
1495 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
1497 struct fdisk_gpt_label
*gpt
;
1502 assert(fdisk_is_disklabel(cxt
, GPT
));
1504 gpt
= self_label(cxt
);
1505 mbr_type
= valid_pmbr(cxt
);
1507 /* check that disk is big enough to handle the backup header */
1508 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
1511 /* check that the backup header is properly placed */
1512 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1)
1513 /* TODO: correct this (with user authorization) and write */
1516 if (partition_check_overlaps(gpt
->pheader
, gpt
->ents
))
1519 /* recompute CRCs for both headers */
1520 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1521 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1524 * UEFI requires writing in this specific order:
1525 * 1) backup partition tables
1526 * 2) backup GPT header
1527 * 3) primary partition tables
1528 * 4) primary GPT header
1531 * If any write fails, we abort the rest.
1533 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
1535 if (gpt_write_header(cxt
, gpt
->bheader
,
1536 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
1538 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
1540 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
1543 if (mbr_type
== GPT_MBR_HYBRID
)
1544 fdisk_warnx(cxt
, _("The device contains hybrid MBR -- writing GPT only. "
1545 "You have to sync the MBR manually."));
1546 else if (gpt_write_pmbr(cxt
) != 0)
1549 DBG(LABEL
, ul_debug("GPT write success"));
1552 DBG(LABEL
, ul_debug("GPT write failed: incorrect input"));
1556 DBG(LABEL
, ul_debug("GPT write failed: %m"));
1561 * Verify data integrity and report any found problems for:
1562 * - primary and backup header validations
1563 * - paritition validations
1565 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
1569 struct fdisk_gpt_label
*gpt
;
1573 assert(fdisk_is_disklabel(cxt
, GPT
));
1575 gpt
= self_label(cxt
);
1577 if (!gpt
|| !gpt
->bheader
) {
1579 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
1582 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
1584 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
1586 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
1588 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
1591 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
1593 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
1596 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
1598 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
1600 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
1602 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
1605 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
1607 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
1609 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
1611 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
1614 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
1616 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
1620 * if the GPT is the primary table, check the alternateLBA
1621 * to see if it is a valid GPT
1623 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
1624 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
1626 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
1629 ptnum
= partition_check_overlaps(gpt
->pheader
, gpt
->ents
);
1632 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
1636 ptnum
= partition_check_too_big(gpt
->pheader
, gpt
->ents
, cxt
->total_sectors
);
1639 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
1643 ptnum
= partition_start_after_end(gpt
->pheader
, gpt
->ents
);
1646 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
1650 if (!nerror
) { /* yay :-) */
1651 uint32_t nsegments
= 0;
1652 uint64_t free_sectors
= 0, largest_segment
= 0;
1655 fdisk_info(cxt
, _("No errors detected."));
1656 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
1657 fdisk_info(cxt
, _("Using %u out of %d partitions."),
1658 partitions_in_use(gpt
->pheader
, gpt
->ents
),
1659 le32_to_cpu(gpt
->pheader
->npartition_entries
));
1661 free_sectors
= get_free_sectors(cxt
, gpt
->pheader
, gpt
->ents
,
1662 &nsegments
, &largest_segment
);
1663 if (largest_segment
)
1664 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
| SIZE_SUFFIX_3LETTER
,
1665 largest_segment
* cxt
->sector_size
);
1668 P_("A total of %ju free sectors is available in %u segment.",
1669 "A total of %ju free sectors is available in %u segments "
1670 "(the largest is %s).", nsegments
),
1671 free_sectors
, nsegments
, strsz
);
1676 P_("%d error detected.", "%d errors detected.", nerror
),
1682 /* Delete a single GPT partition, specified by partnum. */
1683 static int gpt_delete_partition(struct fdisk_context
*cxt
,
1686 struct fdisk_gpt_label
*gpt
;
1690 assert(fdisk_is_disklabel(cxt
, GPT
));
1692 gpt
= self_label(cxt
);
1694 if (partnum
>= cxt
->label
->nparts_max
1695 || partition_unused(&gpt
->ents
[partnum
]))
1698 /* hasta la vista, baby! */
1699 memset(&gpt
->ents
[partnum
], 0, sizeof(struct gpt_entry
));
1700 if (!partition_unused(&gpt
->ents
[partnum
]))
1703 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1704 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1705 cxt
->label
->nparts_cur
--;
1706 fdisk_label_set_changed(cxt
->label
, 1);
1712 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
1715 DBG(LABEL
, gpt_debug_uuid("new type", &(e
->type
)));
1719 * Create a new GPT partition entry, specified by partnum, and with a range
1720 * of fsect to lsenct sectors, of type t.
1721 * Returns 0 on success, or negative upon failure.
1723 static int gpt_create_new_partition(struct fdisk_context
*cxt
,
1724 size_t partnum
, uint64_t fsect
, uint64_t lsect
,
1725 struct gpt_guid
*type
,
1726 struct gpt_entry
*entries
)
1728 struct gpt_entry
*e
= NULL
;
1729 struct fdisk_gpt_label
*gpt
;
1733 assert(fdisk_is_disklabel(cxt
, GPT
));
1735 DBG(LABEL
, ul_debug("GPT new partition: partno=%zu, start=%ju, end=%ju",
1736 partnum
, fsect
, lsect
));
1738 gpt
= self_label(cxt
);
1740 if (fsect
> lsect
|| partnum
>= cxt
->label
->nparts_max
)
1743 e
= calloc(1, sizeof(*e
));
1746 e
->lba_end
= cpu_to_le64(lsect
);
1747 e
->lba_start
= cpu_to_le64(fsect
);
1749 gpt_entry_set_type(e
, type
);
1752 * Any time a new partition entry is created a new GUID must be
1753 * generated for that partition, and every partition is guaranteed
1754 * to have a unique GUID.
1756 uuid_generate_random((unsigned char *) &e
->partition_guid
);
1757 swap_efi_guid(&e
->partition_guid
);
1759 memcpy(&entries
[partnum
], e
, sizeof(*e
));
1761 gpt_recompute_crc(gpt
->pheader
, entries
);
1762 gpt_recompute_crc(gpt
->bheader
, entries
);
1768 /* Performs logical checks to add a new partition entry */
1769 static int gpt_add_partition(
1770 struct fdisk_context
*cxt
,
1771 struct fdisk_partition
*pa
)
1773 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
1774 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
1775 uint64_t dflt_f
, dflt_l
; /* largest segment (default) */
1776 struct gpt_guid
typeid;
1777 struct fdisk_gpt_label
*gpt
;
1778 struct gpt_header
*pheader
;
1779 struct gpt_entry
*ents
;
1780 struct fdisk_ask
*ask
= NULL
;
1786 assert(fdisk_is_disklabel(cxt
, GPT
));
1788 gpt
= self_label(cxt
);
1789 pheader
= gpt
->pheader
;
1792 rc
= fdisk_partition_next_partno(pa
, cxt
, &partnum
);
1794 DBG(LABEL
, ul_debug("GPT failed to get next partno"));
1797 if (!partition_unused(&ents
[partnum
])) {
1798 fdisk_warnx(cxt
, _("Partition %zu is already defined. "
1799 "Delete it before re-adding it."), partnum
+1);
1802 if (le32_to_cpu(pheader
->npartition_entries
) ==
1803 partitions_in_use(pheader
, ents
)) {
1804 fdisk_warnx(cxt
, _("All partitions are already in use."));
1807 if (!get_free_sectors(cxt
, pheader
, ents
, NULL
, NULL
)) {
1808 fdisk_warnx(cxt
, _("No free sectors available."));
1812 string_to_guid(pa
&& pa
->type
&& pa
->type
->typestr
?
1814 GPT_DEFAULT_ENTRY_TYPE
, &typeid);
1816 disk_f
= find_first_available(pheader
, ents
, 0);
1817 disk_l
= find_last_free_sector(pheader
, ents
);
1819 /* the default is the largest free space */
1820 dflt_f
= find_first_in_largest(pheader
, ents
);
1821 dflt_l
= find_last_free(pheader
, ents
, dflt_f
);
1823 /* align the default in range <dflt_f,dflt_l>*/
1824 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
1827 if (pa
&& pa
->start
) {
1828 if (pa
->start
!= find_first_available(pheader
, ents
, pa
->start
)) {
1829 fdisk_warnx(cxt
, _("Sector %ju already used."), pa
->start
);
1833 } else if (pa
&& pa
->start_follow_default
) {
1839 ask
= fdisk_new_ask();
1841 fdisk_reset_ask(ask
);
1844 fdisk_ask_set_query(ask
, _("First sector"));
1845 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
1846 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
1847 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
1848 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
1850 rc
= fdisk_do_ask(cxt
, ask
);
1854 user_f
= fdisk_ask_number_get_result(ask
);
1855 if (user_f
!= find_first_available(pheader
, ents
, user_f
)) {
1856 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
1865 dflt_l
= find_last_free(pheader
, ents
, user_f
);
1867 if (pa
&& pa
->size
) {
1868 user_l
= user_f
+ pa
->size
;
1869 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1871 /* no space for anything useful, use all space
1872 if (user_l + (cxt->grain / cxt->sector_size) > dflt_l)
1876 } else if (pa
&& pa
->end_follow_default
) {
1881 ask
= fdisk_new_ask();
1883 fdisk_reset_ask(ask
);
1885 fdisk_ask_set_query(ask
, _("Last sector, +sectors or +size{K,M,G,T,P}"));
1886 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
1887 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
1888 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
1889 fdisk_ask_number_set_high(ask
, dflt_l
); /* maximal */
1890 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
1891 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
1893 rc
= fdisk_do_ask(cxt
, ask
);
1897 user_l
= fdisk_ask_number_get_result(ask
);
1898 if (fdisk_ask_number_is_relative(ask
)) {
1899 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1901 /* no space for anything useful, use all space
1902 if (user_l + (cxt->grain / cxt->sector_size) > dflt_l)
1905 } if (user_l
> user_f
&& user_l
<= disk_l
)
1910 if ((rc
= gpt_create_new_partition(cxt
, partnum
,
1911 user_f
, user_l
, &typeid, ents
) != 0)) {
1912 fdisk_warnx(cxt
, _("Could not create partition %zu"), partnum
+ 1);
1915 struct fdisk_parttype
*t
;
1917 cxt
->label
->nparts_cur
++;
1918 fdisk_label_set_changed(cxt
->label
, 1);
1920 t
= gpt_partition_parttype(cxt
, &ents
[partnum
]);
1921 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
1922 fdisk_free_parttype(t
);
1927 fdisk_free_ask(ask
);
1932 * Create a new GPT disklabel - destroys any previous data.
1934 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
1939 struct fdisk_gpt_label
*gpt
;
1943 assert(fdisk_is_disklabel(cxt
, GPT
));
1945 gpt
= self_label(cxt
);
1947 /* label private stuff has to be empty, see gpt_deinit() */
1948 assert(gpt
->pheader
== NULL
);
1949 assert(gpt
->bheader
== NULL
);
1952 * When no header, entries or pmbr is set, we're probably
1953 * dealing with a new, empty disk - so always allocate memory
1954 * to deal with the data structures whatever the case is.
1956 rc
= gpt_mknew_pmbr(cxt
);
1961 gpt
->pheader
= calloc(1, sizeof(*gpt
->pheader
));
1962 if (!gpt
->pheader
) {
1966 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
1970 /* backup ("copy" primary) */
1971 gpt
->bheader
= calloc(1, sizeof(*gpt
->bheader
));
1972 if (!gpt
->bheader
) {
1976 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
1977 last_lba(cxt
), gpt
->pheader
);
1981 esz
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
1982 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
1983 gpt
->ents
= calloc(1, esz
);
1988 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1989 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1991 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1992 cxt
->label
->nparts_cur
= 0;
1994 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
1995 fdisk_label_set_changed(cxt
->label
, 1);
1996 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
1997 _("Created a new GPT disklabel (GUID: %s)."), str
);
2002 static int gpt_get_disklabel_id(struct fdisk_context
*cxt
, char **id
)
2004 struct fdisk_gpt_label
*gpt
;
2010 assert(fdisk_is_disklabel(cxt
, GPT
));
2012 gpt
= self_label(cxt
);
2013 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
2021 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
)
2023 struct fdisk_gpt_label
*gpt
;
2024 struct gpt_guid uuid
;
2025 char *str
, *old
, *new;
2030 assert(fdisk_is_disklabel(cxt
, GPT
));
2032 gpt
= self_label(cxt
);
2033 if (fdisk_ask_string(cxt
,
2034 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str
))
2037 rc
= string_to_guid(str
, &uuid
);
2041 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2045 gpt_get_disklabel_id(cxt
, &old
);
2047 gpt
->pheader
->disk_guid
= uuid
;
2048 gpt
->bheader
->disk_guid
= uuid
;
2050 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2051 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2053 gpt_get_disklabel_id(cxt
, &new);
2055 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2056 _("Disk identifier changed from %s to %s."), old
, new);
2060 fdisk_label_set_changed(cxt
->label
, 1);
2064 static int gpt_set_partition_type(
2065 struct fdisk_context
*cxt
,
2067 struct fdisk_parttype
*t
)
2069 struct gpt_guid uuid
;
2070 struct fdisk_gpt_label
*gpt
;
2074 assert(fdisk_is_disklabel(cxt
, GPT
));
2076 gpt
= self_label(cxt
);
2077 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
)
2078 || !t
|| !t
->typestr
|| string_to_guid(t
->typestr
, &uuid
) != 0)
2081 gpt_entry_set_type(&gpt
->ents
[i
], &uuid
);
2082 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2083 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2085 fdisk_label_set_changed(cxt
->label
, 1);
2089 static int gpt_part_is_used(struct fdisk_context
*cxt
, size_t i
)
2091 struct fdisk_gpt_label
*gpt
;
2092 struct gpt_entry
*e
;
2096 assert(fdisk_is_disklabel(cxt
, GPT
));
2098 gpt
= self_label(cxt
);
2100 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2104 return !partition_unused(e
) || gpt_partition_start(e
);
2107 int fdisk_gpt_partition_set_uuid(struct fdisk_context
*cxt
, size_t i
)
2109 struct fdisk_gpt_label
*gpt
;
2110 struct gpt_entry
*e
;
2111 struct gpt_guid uuid
;
2112 char *str
, new_u
[37], old_u
[37];
2117 assert(fdisk_is_disklabel(cxt
, GPT
));
2119 DBG(LABEL
, ul_debug("UUID change requested partno=%zu", i
));
2121 gpt
= self_label(cxt
);
2123 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2126 if (fdisk_ask_string(cxt
,
2127 _("New UUID (in 8-4-4-4-12 format)"), &str
))
2130 rc
= string_to_guid(str
, &uuid
);
2134 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2140 guid_to_string(&e
->partition_guid
, old_u
);
2141 guid_to_string(&uuid
, new_u
);
2143 e
->partition_guid
= uuid
;
2144 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2145 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2146 fdisk_label_set_changed(cxt
->label
, 1);
2148 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2149 _("Partition UUID changed from %s to %s."),
2154 int fdisk_gpt_partition_set_name(struct fdisk_context
*cxt
, size_t i
)
2156 struct fdisk_gpt_label
*gpt
;
2157 struct gpt_entry
*e
;
2158 char *str
, *old
, name
[GPT_PART_NAME_LEN
] = { 0 };
2163 assert(fdisk_is_disklabel(cxt
, GPT
));
2165 DBG(LABEL
, ul_debug("NAME change requested partno=%zu", i
));
2167 gpt
= self_label(cxt
);
2169 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2172 if (fdisk_ask_string(cxt
, _("New name"), &str
))
2176 old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
2180 if (sz
> GPT_PART_NAME_LEN
)
2181 sz
= GPT_PART_NAME_LEN
;
2182 memcpy(name
, str
, sz
);
2185 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
2186 e
->name
[i
] = cpu_to_le16((uint16_t) name
[i
]);
2188 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2189 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2191 fdisk_label_set_changed(cxt
->label
, 1);
2193 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2194 _("Partition name changed from '%s' to '%.*s'."),
2195 old
, (int) GPT_PART_NAME_LEN
, str
);
2202 int fdisk_gpt_is_hybrid(struct fdisk_context
*cxt
)
2205 return valid_pmbr(cxt
) == GPT_MBR_HYBRID
;
2208 static int gpt_toggle_partition_flag(
2209 struct fdisk_context
*cxt
,
2213 struct fdisk_gpt_label
*gpt
;
2214 uint64_t attrs
, tmp
;
2216 const char *name
= NULL
;
2221 assert(fdisk_is_disklabel(cxt
, GPT
));
2223 DBG(LABEL
, ul_debug("GPT entry attribute change requested partno=%zu", i
));
2224 gpt
= self_label(cxt
);
2226 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2229 attrs
= le64_to_cpu(gpt
->ents
[i
].attrs
);
2230 bits
= (char *) &attrs
;
2233 case GPT_FLAG_REQUIRED
:
2234 bit
= GPT_ATTRBIT_REQ
;
2235 name
= GPT_ATTRSTR_REQ
;
2237 case GPT_FLAG_NOBLOCK
:
2238 bit
= GPT_ATTRBIT_NOBLOCK
;
2239 name
= GPT_ATTRSTR_NOBLOCK
;
2241 case GPT_FLAG_LEGACYBOOT
:
2242 bit
= GPT_ATTRBIT_LEGACY
;
2243 name
= GPT_ATTRSTR_LEGACY
;
2245 case GPT_FLAG_GUIDSPECIFIC
:
2246 rc
= fdisk_ask_number(cxt
, 48, 48, 63, _("Enter GUID specific bit"), &tmp
);
2256 if (!isset(bits
, bit
))
2261 gpt
->ents
[i
].attrs
= cpu_to_le64(attrs
);
2263 if (flag
== GPT_FLAG_GUIDSPECIFIC
)
2264 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2266 _("The GUID specific bit %d on partition %zu is enabled now.") :
2267 _("The GUID specific bit %d on partition %zu is disabled now."),
2270 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2272 _("The %s flag on partition %zu is enabled now.") :
2273 _("The %s flag on partition %zu is disabled now."),
2276 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2277 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2278 fdisk_label_set_changed(cxt
->label
, 1);
2282 static int gpt_entry_cmp_start(const void *a
, const void *b
)
2284 struct gpt_entry
*ae
= (struct gpt_entry
*) a
,
2285 *be
= (struct gpt_entry
*) b
;
2286 int au
= partition_unused(ae
),
2287 bu
= partition_unused(be
);
2296 return gpt_partition_start(ae
) - gpt_partition_start(be
);
2299 /* sort partition by start sector */
2300 static int gpt_reorder(struct fdisk_context
*cxt
)
2302 struct fdisk_gpt_label
*gpt
;
2307 assert(fdisk_is_disklabel(cxt
, GPT
));
2309 gpt
= self_label(cxt
);
2310 nparts
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2312 qsort(gpt
->ents
, nparts
, sizeof(struct gpt_entry
),
2313 gpt_entry_cmp_start
);
2315 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2316 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2317 fdisk_label_set_changed(cxt
->label
, 1);
2319 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
, _("Done."));
2323 static int gpt_reset_alignment(struct fdisk_context
*cxt
)
2325 struct fdisk_gpt_label
*gpt
;
2326 struct gpt_header
*h
;
2330 assert(fdisk_is_disklabel(cxt
, GPT
));
2332 gpt
= self_label(cxt
);
2333 h
= gpt
? gpt
->pheader
: NULL
;
2336 /* always follow existing table */
2337 cxt
->first_lba
= h
->first_usable_lba
;
2338 cxt
->last_lba
= h
->last_usable_lba
;
2340 /* estimate ranges for GPT */
2341 uint64_t first
, last
;
2343 count_first_last_lba(cxt
, &first
, &last
);
2345 if (cxt
->first_lba
< first
)
2346 cxt
->first_lba
= first
;
2347 if (cxt
->last_lba
> last
)
2348 cxt
->last_lba
= last
;
2354 * Deinitialize fdisk-specific variables
2356 static void gpt_deinit(struct fdisk_label
*lb
)
2358 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
2368 gpt
->pheader
= NULL
;
2369 gpt
->bheader
= NULL
;
2372 static const struct fdisk_label_operations gpt_operations
=
2374 .probe
= gpt_probe_label
,
2375 .write
= gpt_write_disklabel
,
2376 .verify
= gpt_verify_disklabel
,
2377 .create
= gpt_create_disklabel
,
2378 .list
= gpt_list_disklabel
,
2379 .locate
= gpt_locate_disklabel
,
2380 .reorder
= gpt_reorder
,
2381 .get_id
= gpt_get_disklabel_id
,
2382 .set_id
= gpt_set_disklabel_id
,
2384 .get_part
= gpt_get_partition
,
2385 .add_part
= gpt_add_partition
,
2387 .part_delete
= gpt_delete_partition
,
2389 .part_is_used
= gpt_part_is_used
,
2390 .part_set_type
= gpt_set_partition_type
,
2391 .part_toggle_flag
= gpt_toggle_partition_flag
,
2393 .deinit
= gpt_deinit
,
2395 .reset_alignment
= gpt_reset_alignment
2398 static const struct fdisk_column gpt_columns
[] =
2401 { FDISK_COL_DEVICE
, N_("Device"), 10, 0 },
2402 { FDISK_COL_START
, N_("Start"), 5, SCOLS_FL_RIGHT
},
2403 { FDISK_COL_END
, N_("End"), 5, SCOLS_FL_RIGHT
},
2404 { FDISK_COL_SECTORS
, N_("Sectors"), 5, SCOLS_FL_RIGHT
},
2405 { FDISK_COL_CYLINDERS
, N_("Cylinders"), 5, SCOLS_FL_RIGHT
},
2406 { FDISK_COL_SIZE
, N_("Size"), 5, SCOLS_FL_RIGHT
, FDISK_COLFL_EYECANDY
},
2407 { FDISK_COL_TYPE
, N_("Type"), 0.1, SCOLS_FL_TRUNC
, FDISK_COLFL_EYECANDY
},
2409 { FDISK_COL_TYPEID
, N_("Type-UUID"), 36, 0, FDISK_COLFL_DETAIL
},
2410 { FDISK_COL_UUID
, N_("UUID"), 36, 0, FDISK_COLFL_DETAIL
},
2411 { FDISK_COL_NAME
, N_("Name"), 0.2, SCOLS_FL_TRUNC
, FDISK_COLFL_DETAIL
},
2412 { FDISK_COL_ATTR
, N_("Attrs"), 0, 0, FDISK_COLFL_DETAIL
}
2416 * allocates GPT in-memory stuff
2418 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
)
2420 struct fdisk_label
*lb
;
2421 struct fdisk_gpt_label
*gpt
;
2425 gpt
= calloc(1, sizeof(*gpt
));
2429 /* initialize generic part of the driver */
2430 lb
= (struct fdisk_label
*) gpt
;
2432 lb
->id
= FDISK_DISKLABEL_GPT
;
2433 lb
->op
= &gpt_operations
;
2434 lb
->parttypes
= gpt_parttypes
;
2435 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);
2437 lb
->columns
= gpt_columns
;
2438 lb
->ncolumns
= ARRAY_SIZE(gpt_columns
);