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>
35 * @short_description: specific functionality
38 #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */
39 #define GPT_HEADER_REVISION_V1_02 0x00010200
40 #define GPT_HEADER_REVISION_V1_00 0x00010000
41 #define GPT_HEADER_REVISION_V0_99 0x00009900
42 #define GPT_HEADER_MINSZ 92 /* bytes */
44 #define GPT_PMBR_LBA 0
45 #define GPT_MBR_PROTECTIVE 1
46 #define GPT_MBR_HYBRID 2
48 #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001ULL
50 #define EFI_PMBR_OSTYPE 0xEE
51 #define MSDOS_MBR_SIGNATURE 0xAA55
52 #define GPT_PART_NAME_LEN (72 / sizeof(uint16_t))
53 #define GPT_NPARTITIONS FDISK_GPT_NPARTITIONS_DEFAULT
55 /* Globally unique identifier */
59 uint16_t time_hi_and_version
;
61 uint8_t clock_seq_low
;
66 /* only checking that the GUID is 0 is enough to verify an empty partition. */
67 #define GPT_UNUSED_ENTRY_GUID \
68 ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
69 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
71 /* Linux native partition type */
72 #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
80 GPT_ATTRBIT_NOBLOCK
= 1,
81 GPT_ATTRBIT_LEGACY
= 2,
83 /* GUID specific (range 48..64)*/
84 GPT_ATTRBIT_GUID_FIRST
= 48,
85 GPT_ATTRBIT_GUID_COUNT
= 16
88 #define GPT_ATTRSTR_REQ "RequiredPartition"
89 #define GPT_ATTRSTR_REQ_TYPO "RequiredPartiton"
90 #define GPT_ATTRSTR_NOBLOCK "NoBlockIOProtocol"
91 #define GPT_ATTRSTR_LEGACY "LegacyBIOSBootable"
93 /* The GPT Partition entry array contains an array of GPT entries. */
95 struct gpt_guid type
; /* purpose and type of the partition */
96 struct gpt_guid partition_guid
;
100 uint16_t name
[GPT_PART_NAME_LEN
];
101 } __attribute__ ((packed
));
105 uint64_t signature
; /* header identification */
106 uint32_t revision
; /* header version */
107 uint32_t size
; /* in bytes */
108 uint32_t crc32
; /* header CRC checksum */
109 uint32_t reserved1
; /* must be 0 */
110 uint64_t my_lba
; /* LBA of block that contains this struct (LBA 1) */
111 uint64_t alternative_lba
; /* backup GPT header */
112 uint64_t first_usable_lba
; /* first usable logical block for partitions */
113 uint64_t last_usable_lba
; /* last usable logical block for partitions */
114 struct gpt_guid disk_guid
; /* unique disk identifier */
115 uint64_t partition_entry_lba
; /* LBA of start of partition entries array */
116 uint32_t npartition_entries
; /* total partition entries - normally 128 */
117 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
118 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
119 uint8_t reserved2
[512 - 92]; /* must all be 0 */
120 } __attribute__ ((packed
));
123 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
124 uint8_t start_head
; /* unused by EFI, pt start in CHS */
125 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
127 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
128 uint8_t end_head
; /* unused by EFI, pt end in CHS */
129 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
130 uint8_t end_track
; /* unused by EFI, pt end in CHS */
131 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
132 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
133 } __attribute__ ((packed
));
135 /* Protected MBR and legacy MBR share same structure */
136 struct gpt_legacy_mbr
{
137 uint8_t boot_code
[440];
138 uint32_t unique_mbr_signature
;
140 struct gpt_record partition_record
[4];
142 } __attribute__ ((packed
));
146 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
148 #define DEF_GUID(_u, _n) \
154 /* Probably the most complete list of the GUIDs are at:
155 * https://wikipedia.org/wiki/GUID_Partition_Table
157 static struct fdisk_parttype gpt_parttypes
[] =
160 DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")),
162 DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")),
163 DEF_GUID("D3BFE2DE-3DAF-11DF-BA40-E3A556D89593", N_("Intel Fast Flash")),
165 /* Hah!IdontneedEFI */
166 DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot")),
169 DEF_GUID("F4019732-066E-4E12-8273-346C5641494F", N_("Sony boot partition")),
170 DEF_GUID("BFBFAFE7-A34F-448A-9A5B-6213EB736C22", N_("Lenovo boot partition")),
172 /* PowerPC reference platform boot partition */
173 DEF_GUID("9E1A2D38-C612-4316-AA26-8B49521E5A8B", N_("PowerPC PReP boot")),
175 /* Open Network Install Environment */
176 DEF_GUID("7412F7D5-A156-4B13-81DC-867174929325", N_("ONIE boot")),
177 DEF_GUID("D4E6E2CD-4469-46F3-B5CB-1BFF57AFC149", N_("ONIE config")),
180 DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
181 DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
182 DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")),
183 DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")),
184 DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery environment")),
185 DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")),
186 DEF_GUID("E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D", N_("Microsoft Storage Spaces")),
189 DEF_GUID("75894C1E-3AEB-11D3-B7C1-7B03A0000000", N_("HP-UX data")),
190 DEF_GUID("E2A1E728-32E3-11D6-A682-7B03A0000000", N_("HP-UX service")),
192 /* Linux (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec) */
193 DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")),
194 DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")),
195 DEF_GUID("3B8F8425-20E0-4F3B-907F-1A25A76F98E8", N_("Linux server data")),
196 DEF_GUID("44479540-F297-41B2-9AF7-D131D5F0458A", N_("Linux root (x86)")),
197 DEF_GUID("69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", N_("Linux root (ARM)")),
198 DEF_GUID("4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", N_("Linux root (x86-64)")),
199 DEF_GUID("B921B045-1DF0-41C3-AF44-4C6F280D3FAE", N_("Linux root (ARM-64)")),
200 DEF_GUID("993D8D3D-F80E-4225-855A-9DAF8ED7EA97", N_("Linux root (IA-64)")),
201 DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")),
202 DEF_GUID("933AC7E1-2EB4-4F13-B844-0E14E2AEF915", N_("Linux home")),
203 DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")),
204 DEF_GUID("BC13C2FF-59E6-4262-A352-B275FD6F7172", N_("Linux extended boot")),
205 DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")),
206 /* ... too crazy, ignore for now:
207 DEF_GUID("7FFEC5C9-2D00-49B7-8941-3EA10A5586B7", N_("Linux plain dm-crypt")),
208 DEF_GUID("CA7D7CCB-63ED-4C53-861C-1742536059CC", N_("Linux LUKS")),
212 DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")),
213 DEF_GUID("83BD6B9D-7F41-11DC-BE0B-001560B84F0F", N_("FreeBSD boot")),
214 DEF_GUID("516E7CB5-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD swap")),
215 DEF_GUID("516E7CB6-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD UFS")),
216 DEF_GUID("516E7CBA-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD ZFS")),
217 DEF_GUID("516E7CB8-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD Vinum")),
220 DEF_GUID("48465300-0000-11AA-AA11-00306543ECAC", N_("Apple HFS/HFS+")),
221 DEF_GUID("55465300-0000-11AA-AA11-00306543ECAC", N_("Apple UFS")),
222 DEF_GUID("52414944-0000-11AA-AA11-00306543ECAC", N_("Apple RAID")),
223 DEF_GUID("52414944-5F4F-11AA-AA11-00306543ECAC", N_("Apple RAID offline")),
224 DEF_GUID("426F6F74-0000-11AA-AA11-00306543ECAC", N_("Apple boot")),
225 DEF_GUID("4C616265-6C00-11AA-AA11-00306543ECAC", N_("Apple label")),
226 DEF_GUID("5265636F-7665-11AA-AA11-00306543ECAC", N_("Apple TV recovery")),
227 DEF_GUID("53746F72-6167-11AA-AA11-00306543ECAC", N_("Apple Core storage")),
230 DEF_GUID("6A82CB45-1DD2-11B2-99A6-080020736631", N_("Solaris boot")),
231 DEF_GUID("6A85CF4D-1DD2-11B2-99A6-080020736631", N_("Solaris root")),
232 /* same as Apple ZFS */
233 DEF_GUID("6A898CC3-1DD2-11B2-99A6-080020736631", N_("Solaris /usr & Apple ZFS")),
234 DEF_GUID("6A87C46F-1DD2-11B2-99A6-080020736631", N_("Solaris swap")),
235 DEF_GUID("6A8B642B-1DD2-11B2-99A6-080020736631", N_("Solaris backup")),
236 DEF_GUID("6A8EF2E9-1DD2-11B2-99A6-080020736631", N_("Solaris /var")),
237 DEF_GUID("6A90BA39-1DD2-11B2-99A6-080020736631", N_("Solaris /home")),
238 DEF_GUID("6A9283A5-1DD2-11B2-99A6-080020736631", N_("Solaris alternate sector")),
239 DEF_GUID("6A945A3B-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 1")),
240 DEF_GUID("6A9630D1-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 2")),
241 DEF_GUID("6A980767-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 3")),
242 DEF_GUID("6A96237F-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 4")),
243 DEF_GUID("6A8D2AC7-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 5")),
246 DEF_GUID("49F48D32-B10E-11DC-B99B-0019D1879648", N_("NetBSD swap")),
247 DEF_GUID("49F48D5A-B10E-11DC-B99B-0019D1879648", N_("NetBSD FFS")),
248 DEF_GUID("49F48D82-B10E-11DC-B99B-0019D1879648", N_("NetBSD LFS")),
249 DEF_GUID("2DB519C4-B10E-11DC-B99B-0019D1879648", N_("NetBSD concatenated")),
250 DEF_GUID("2DB519EC-B10E-11DC-B99B-0019D1879648", N_("NetBSD encrypted")),
251 DEF_GUID("49F48DAA-B10E-11DC-B99B-0019D1879648", N_("NetBSD RAID")),
254 DEF_GUID("FE3A2A5D-4F32-41A7-B725-ACCC3285A309", N_("ChromeOS kernel")),
255 DEF_GUID("3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", N_("ChromeOS root fs")),
256 DEF_GUID("2E0A753D-9E48-43B0-8337-B15192CB1B5E", N_("ChromeOS reserved")),
259 DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")),
260 DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")),
261 DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")),
262 DEF_GUID("0394EF8B-237E-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
263 DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")),
264 DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")),
267 DEF_GUID("45B0969E-9B03-4F30-B4C6-B4B80CEFF106", N_("Ceph Journal")),
268 DEF_GUID("45B0969E-9B03-4F30-B4C6-5EC00CEFF106", N_("Ceph Encrypted Journal")),
269 DEF_GUID("4FBD7E29-9D25-41B8-AFD0-062C0CEFF05D", N_("Ceph OSD")),
270 DEF_GUID("4FBD7E29-9D25-41B8-AFD0-5EC00CEFF05D", N_("Ceph crypt OSD")),
271 DEF_GUID("89C57F98-2FE5-4DC0-89C1-F3AD0CEFF2BE", N_("Ceph disk in creation")),
272 DEF_GUID("89C57F98-2FE5-4DC0-89C1-5EC00CEFF2BE", N_("Ceph crypt disk in creation")),
275 DEF_GUID("824CC7A0-36A8-11E3-890A-952519AD3F61", N_("OpenBSD data")),
278 DEF_GUID("CEF5A9AD-73BC-4601-89F3-CDEEEEE321A1", N_("QNX6 file system")),
281 DEF_GUID("C91818F9-8025-47AF-89D2-F030D7000C2C", N_("Plan 9 partition"))
284 /* gpt_entry macros */
285 #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start)
286 #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end)
289 * in-memory fdisk GPT stuff
291 struct fdisk_gpt_label
{
292 struct fdisk_label head
; /* generic part */
294 /* gpt specific part */
295 struct gpt_header
*pheader
; /* primary header */
296 struct gpt_header
*bheader
; /* backup header */
297 struct gpt_entry
*ents
; /* entries (partitions) */
300 static void gpt_deinit(struct fdisk_label
*lb
);
302 static inline struct fdisk_gpt_label
*self_label(struct fdisk_context
*cxt
)
304 return (struct fdisk_gpt_label
*) cxt
->label
;
308 * Returns the partition length, or 0 if end is before beginning.
310 static uint64_t gpt_partition_size(const struct gpt_entry
*e
)
312 uint64_t start
= gpt_partition_start(e
);
313 uint64_t end
= gpt_partition_end(e
);
315 return start
> end
? 0 : end
- start
+ 1ULL;
318 /* prints UUID in the real byte order! */
319 static void gpt_debug_uuid(const char *mesg
, struct gpt_guid
*guid
)
321 const unsigned char *uuid
= (unsigned char *) guid
;
323 fprintf(stderr
, "%s: "
324 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
326 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
330 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
334 * UUID is traditionally 16 byte big-endian array, except Intel EFI
335 * specification where the UUID is a structure of little-endian fields.
337 static void swap_efi_guid(struct gpt_guid
*uid
)
339 uid
->time_low
= swab32(uid
->time_low
);
340 uid
->time_mid
= swab16(uid
->time_mid
);
341 uid
->time_hi_and_version
= swab16(uid
->time_hi_and_version
);
344 static int string_to_guid(const char *in
, struct gpt_guid
*guid
)
346 if (uuid_parse(in
, (unsigned char *) guid
)) { /* BE */
347 DBG(LABEL
, ul_debug("GPT: failed to parse GUID: %s", in
));
350 swap_efi_guid(guid
); /* LE */
354 static char *guid_to_string(const struct gpt_guid
*guid
, char *out
)
356 struct gpt_guid u
= *guid
; /* LE */
358 swap_efi_guid(&u
); /* BE */
359 uuid_unparse_upper((unsigned char *) &u
, out
);
364 static struct fdisk_parttype
*gpt_partition_parttype(
365 struct fdisk_context
*cxt
,
366 const struct gpt_entry
*e
)
368 struct fdisk_parttype
*t
;
371 guid_to_string(&e
->type
, str
);
372 t
= fdisk_label_get_parttype_from_string(cxt
->label
, str
);
373 return t
? : fdisk_new_unknown_parttype(0, str
);
376 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
379 DBG(LABEL
, gpt_debug_uuid("new type", &(e
->type
)));
382 static void gpt_entry_set_name(struct gpt_entry
*e
, char *str
)
384 char name
[GPT_PART_NAME_LEN
] = { 0 };
385 size_t i
, sz
= strlen(str
);
388 if (sz
> GPT_PART_NAME_LEN
)
389 sz
= GPT_PART_NAME_LEN
;
390 memcpy(name
, str
, sz
);
393 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
394 e
->name
[i
] = cpu_to_le16((uint16_t) name
[i
]);
397 static int gpt_entry_set_uuid(struct gpt_entry
*e
, char *str
)
399 struct gpt_guid uuid
;
402 rc
= string_to_guid(str
, &uuid
);
406 e
->partition_guid
= uuid
;
411 static const char *gpt_get_header_revstr(struct gpt_header
*header
)
416 switch (le32_to_cpu(header
->revision
)) {
417 case GPT_HEADER_REVISION_V1_02
:
419 case GPT_HEADER_REVISION_V1_00
:
421 case GPT_HEADER_REVISION_V0_99
:
431 static inline int partition_unused(const struct gpt_entry
*e
)
433 return !memcmp(&e
->type
, &GPT_UNUSED_ENTRY_GUID
,
434 sizeof(struct gpt_guid
));
437 static char *gpt_get_header_id(struct gpt_header
*header
)
441 guid_to_string(&header
->disk_guid
, str
);
448 * Builds a clean new valid protective MBR - will wipe out any existing data.
449 * Returns 0 on success, otherwise < 0 on error.
451 static int gpt_mknew_pmbr(struct fdisk_context
*cxt
)
453 struct gpt_legacy_mbr
*pmbr
= NULL
;
456 if (!cxt
|| !cxt
->firstsector
)
459 if (fdisk_has_protected_bootbits(cxt
))
460 rc
= fdisk_init_firstsector_buffer(cxt
, 0, MBR_PT_BOOTBITS_SIZE
);
462 rc
= fdisk_init_firstsector_buffer(cxt
, 0, 0);
466 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
468 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
469 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
470 pmbr
->partition_record
[0].start_sector
= 1;
471 pmbr
->partition_record
[0].end_head
= 0xFE;
472 pmbr
->partition_record
[0].end_sector
= 0xFF;
473 pmbr
->partition_record
[0].end_track
= 0xFF;
474 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
475 pmbr
->partition_record
[0].size_in_lba
=
476 cpu_to_le32((uint32_t) min( cxt
->total_sectors
- 1ULL, 0xFFFFFFFFULL
) );
481 /* some universal differences between the headers */
482 static void gpt_mknew_header_common(struct fdisk_context
*cxt
,
483 struct gpt_header
*header
, uint64_t lba
)
488 header
->my_lba
= cpu_to_le64(lba
);
490 if (lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
) { /* primary */
491 header
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1ULL);
492 header
->partition_entry_lba
= cpu_to_le64(2ULL);
493 } else { /* backup */
494 uint64_t esz
= le32_to_cpu(header
->npartition_entries
) * sizeof(struct gpt_entry
);
495 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
497 header
->alternative_lba
= cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA
);
498 header
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1ULL - esects
);
503 * Builds a new GPT header (at sector lba) from a backup header2.
504 * If building a primary header, then backup is the secondary, and vice versa.
506 * Always pass a new (zeroized) header to build upon as we don't
507 * explicitly zero-set some values such as CRCs and reserved.
509 * Returns 0 on success, otherwise < 0 on error.
511 static int gpt_mknew_header_from_bkp(struct fdisk_context
*cxt
,
512 struct gpt_header
*header
,
514 struct gpt_header
*header2
)
516 if (!cxt
|| !header
|| !header2
)
519 header
->signature
= header2
->signature
;
520 header
->revision
= header2
->revision
;
521 header
->size
= header2
->size
;
522 header
->npartition_entries
= header2
->npartition_entries
;
523 header
->sizeof_partition_entry
= header2
->sizeof_partition_entry
;
524 header
->first_usable_lba
= header2
->first_usable_lba
;
525 header
->last_usable_lba
= header2
->last_usable_lba
;
527 memcpy(&header
->disk_guid
,
528 &header2
->disk_guid
, sizeof(header2
->disk_guid
));
529 gpt_mknew_header_common(cxt
, header
, lba
);
534 static struct gpt_header
*gpt_copy_header(struct fdisk_context
*cxt
,
535 struct gpt_header
*src
)
537 struct gpt_header
*res
;
542 assert(cxt
->sector_size
>= sizeof(struct gpt_header
));
544 res
= calloc(1, cxt
->sector_size
);
546 fdisk_warn(cxt
, _("failed to allocate GPT header"));
550 res
->my_lba
= src
->alternative_lba
;
551 res
->alternative_lba
= src
->my_lba
;
553 res
->signature
= src
->signature
;
554 res
->revision
= src
->revision
;
555 res
->size
= src
->size
;
556 res
->npartition_entries
= src
->npartition_entries
;
557 res
->sizeof_partition_entry
= src
->sizeof_partition_entry
;
558 res
->first_usable_lba
= src
->first_usable_lba
;
559 res
->last_usable_lba
= src
->last_usable_lba
;
561 memcpy(&res
->disk_guid
, &src
->disk_guid
, sizeof(src
->disk_guid
));
564 if (res
->my_lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
)
565 res
->partition_entry_lba
= cpu_to_le64(2ULL);
567 uint64_t esz
= le32_to_cpu(src
->npartition_entries
) * sizeof(struct gpt_entry
);
568 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
570 res
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1ULL - esects
);
576 static int get_script_u64(struct fdisk_context
*cxt
, uint64_t *num
, const char *name
)
588 str
= fdisk_script_get_header(cxt
->script
, name
);
592 rc
= parse_size(str
, (uintmax_t *) num
, &pwr
);
596 *num
/= cxt
->sector_size
;
600 static int count_first_last_lba(struct fdisk_context
*cxt
,
601 uint64_t *first
, uint64_t *last
)
615 esz
= sizeof(struct gpt_entry
) * GPT_NPARTITIONS
/ cxt
->sector_size
;
616 llba
= cxt
->total_sectors
- 2ULL - esz
;
621 rc
= get_script_u64(cxt
, first
, "first-lba");
625 DBG(LABEL
, ul_debug("FirstLBA: script=%"PRIu64
", uefi=%"PRIu64
", topology=%ju.",
626 *first
, flba
, (uintmax_t)cxt
->first_lba
));
628 if (rc
== 0 && (*first
< flba
|| *first
> llba
)) {
629 fdisk_warnx(cxt
, _("First LBA specified by script is out of range."));
633 rc
= get_script_u64(cxt
, last
, "last-lba");
637 DBG(LABEL
, ul_debug("LastLBA: script=%"PRIu64
", uefi=%"PRIu64
", topology=%ju.",
638 *last
, llba
, (uintmax_t)cxt
->last_lba
));
640 if (rc
== 0 && (*last
> llba
|| *last
< flba
)) {
641 fdisk_warnx(cxt
, _("Last LBA specified by script is out of range."));
649 /* default by topology */
651 *first
= flba
< cxt
->first_lba
&&
652 cxt
->first_lba
< *last
? cxt
->first_lba
: flba
;
657 * Builds a clean new GPT header (currently under revision 1.0).
659 * Always pass a new (zeroized) header to build upon as we don't
660 * explicitly zero-set some values such as CRCs and reserved.
662 * Returns 0 on success, otherwise < 0 on error.
664 static int gpt_mknew_header(struct fdisk_context
*cxt
,
665 struct gpt_header
*header
, uint64_t lba
)
667 uint64_t first
, last
;
673 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
674 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
676 /* According to EFI standard it's valid to count all the first
677 * sector into header size, but some tools may have a problem
678 * to accept it, so use the header without the zeroed area.
679 * This does not have any impact to CRC, etc. --kzak Jan-2015
681 header
->size
= cpu_to_le32(sizeof(struct gpt_header
)
682 - sizeof(header
->reserved2
));
685 * 128 partitions are the default. It can go beyond that, but
686 * we're creating a de facto header here, so no funny business.
688 header
->npartition_entries
= cpu_to_le32(GPT_NPARTITIONS
);
689 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
691 rc
= count_first_last_lba(cxt
, &first
, &last
);
695 header
->first_usable_lba
= cpu_to_le64(first
);
696 header
->last_usable_lba
= cpu_to_le64(last
);
698 gpt_mknew_header_common(cxt
, header
, lba
);
701 const char *id
= fdisk_script_get_header(cxt
->script
, "label-id");
702 if (id
&& string_to_guid(id
, &header
->disk_guid
) == 0)
707 uuid_generate_random((unsigned char *) &header
->disk_guid
);
708 swap_efi_guid(&header
->disk_guid
);
714 * Checks if there is a valid protective MBR partition table.
715 * Returns 0 if it is invalid or failure. Otherwise, return
716 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depending on the detection.
718 static int valid_pmbr(struct fdisk_context
*cxt
)
720 int i
, part
= 0, ret
= 0; /* invalid by default */
721 struct gpt_legacy_mbr
*pmbr
= NULL
;
723 if (!cxt
->firstsector
)
726 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
728 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
731 /* seems like a valid MBR was found, check DOS primary partitions */
732 for (i
= 0; i
< 4; i
++) {
733 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
735 * Ok, we at least know that there's a protective MBR,
736 * now check if there are other partition types for
740 ret
= GPT_MBR_PROTECTIVE
;
745 if (ret
!= GPT_MBR_PROTECTIVE
)
748 /* LBA of the GPT partition header */
749 if (pmbr
->partition_record
[part
].starting_lba
!=
750 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
753 for (i
= 0 ; i
< 4; i
++) {
754 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
755 (pmbr
->partition_record
[i
].os_type
!= 0x00))
756 ret
= GPT_MBR_HYBRID
;
760 * Protective MBRs take up the lesser of the whole disk
761 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
762 * Some partitioning programs, nonetheless, choose to set
763 * the size to the maximum 32-bit limitation, disregarding
766 * Hybrid MBRs do not necessarily comply with this.
768 * Consider a bad value here to be a warning to support dd-ing
769 * an image from a smaller disk to a bigger disk.
771 if (ret
== GPT_MBR_PROTECTIVE
) {
772 uint64_t sz_lba
= (uint64_t) le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
773 if (sz_lba
!= cxt
->total_sectors
- 1ULL && sz_lba
!= 0xFFFFFFFFULL
) {
774 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%"PRIu64
" != %"PRIu64
") "
775 "will be corrected by w(rite)."),
776 sz_lba
, cxt
->total_sectors
- 1ULL);
777 fdisk_label_set_changed(cxt
->label
, 1);
784 static uint64_t last_lba(struct fdisk_context
*cxt
)
787 uint64_t sectors
= 0;
789 memset(&s
, 0, sizeof(s
));
790 if (fstat(cxt
->dev_fd
, &s
) == -1) {
791 fdisk_warn(cxt
, _("gpt: stat() failed"));
795 if (S_ISBLK(s
.st_mode
))
796 sectors
= cxt
->total_sectors
- 1ULL;
797 else if (S_ISREG(s
.st_mode
))
798 sectors
= ((uint64_t) s
.st_size
/
799 (uint64_t) cxt
->sector_size
) - 1ULL;
801 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
803 DBG(LABEL
, ul_debug("GPT last LBA: %"PRIu64
"", sectors
));
807 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
808 void *buffer
, const size_t bytes
)
810 off_t offset
= lba
* cxt
->sector_size
;
812 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
814 return (size_t)read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
818 /* Returns the GPT entry array */
819 static struct gpt_entry
*gpt_read_entries(struct fdisk_context
*cxt
,
820 struct gpt_header
*header
)
823 struct gpt_entry
*ret
= NULL
;
829 sz
= le32_to_cpu(header
->npartition_entries
) *
830 le32_to_cpu(header
->sizeof_partition_entry
);
835 offset
= le64_to_cpu(header
->partition_entry_lba
) *
838 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
840 if (sz
!= read(cxt
->dev_fd
, ret
, sz
))
850 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
,
851 size_t ex_off
, size_t ex_len
)
853 return (ul_crc32_exclude_offset(~0L, buf
, len
, ex_off
, ex_len
) ^ ~0L);
856 static inline uint32_t gpt_header_count_crc32(struct gpt_header
*header
)
858 return count_crc32((unsigned char *) header
, /* buffer */
859 le32_to_cpu(header
->size
), /* size of buffer */
860 offsetof(struct gpt_header
, crc32
), /* exclude */
861 sizeof(header
->crc32
)); /* size of excluded area */
864 static inline uint32_t gpt_entryarr_count_crc32(struct gpt_header
*header
, struct gpt_entry
*ents
)
868 arysz
= le32_to_cpu(header
->npartition_entries
) *
869 le32_to_cpu(header
->sizeof_partition_entry
);
871 return count_crc32((unsigned char *) ents
, arysz
, 0, 0);
876 * Recompute header and partition array 32bit CRC checksums.
877 * This function does not fail - if there's corruption, then it
878 * will be reported when checksumming it again (ie: probing or verify).
880 static void gpt_recompute_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
885 header
->partition_entry_array_crc32
=
886 cpu_to_le32( gpt_entryarr_count_crc32(header
, ents
) );
888 header
->crc32
= cpu_to_le32( gpt_header_count_crc32(header
) );
892 * Compute the 32bit CRC checksum of the partition table header.
893 * Returns 1 if it is valid, otherwise 0.
895 static int gpt_check_header_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
897 uint32_t orgcrc
= le32_to_cpu(header
->crc32
),
898 crc
= gpt_header_count_crc32(header
);
904 * If we have checksum mismatch it may be due to stale data, like a
905 * partition being added or deleted. Recompute the CRC again and make
906 * sure this is not the case.
909 gpt_recompute_crc(header
, ents
);
910 return gpt_header_count_crc32(header
) == orgcrc
;
917 * It initializes the partition entry array.
918 * Returns 1 if the checksum is valid, otherwise 0.
920 static int gpt_check_entryarr_crc(struct gpt_header
*header
,
921 struct gpt_entry
*ents
)
923 if (!header
|| !ents
)
926 return gpt_entryarr_count_crc32(header
, ents
) ==
927 le32_to_cpu(header
->partition_entry_array_crc32
);
930 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
933 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
935 fu
= le64_to_cpu(header
->first_usable_lba
);
936 lu
= le64_to_cpu(header
->last_usable_lba
);
938 /* check if first and last usable LBA make sense */
940 DBG(LABEL
, ul_debug("error: header last LBA is before first LBA"));
944 /* check if first and last usable LBAs with the disk's last LBA */
945 if (fu
> lastlba
|| lu
> lastlba
) {
946 DBG(LABEL
, ul_debug("error: header LBAs are after the disk's last LBA"));
950 /* the header has to be outside usable range */
951 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
952 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
953 DBG(LABEL
, ul_debug("error: header outside of usable range"));
962 /* Check if there is a valid header signature */
963 static int gpt_check_signature(struct gpt_header
*header
)
965 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
969 * Return the specified GPT Header, or NULL upon failure/invalid.
970 * Note that all tests must pass to ensure a valid header,
971 * we do not rely on only testing the signature for a valid probe.
973 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
975 struct gpt_entry
**_ents
)
977 struct gpt_header
*header
= NULL
;
978 struct gpt_entry
*ents
= NULL
;
984 /* always allocate all sector, the area after GPT header
985 * has to be fill by zeros */
986 assert(cxt
->sector_size
>= sizeof(struct gpt_header
));
988 header
= calloc(1, cxt
->sector_size
);
992 /* read and verify header */
993 if (read_lba(cxt
, lba
, header
, cxt
->sector_size
) != 0)
996 if (!gpt_check_signature(header
))
999 /* make sure header size is between 92 and sector size bytes */
1000 hsz
= le32_to_cpu(header
->size
);
1001 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
1004 if (!gpt_check_header_crc(header
, NULL
))
1007 /* read and verify entries */
1008 ents
= gpt_read_entries(cxt
, header
);
1012 if (!gpt_check_entryarr_crc(header
, ents
))
1015 if (!gpt_check_lba_sanity(cxt
, header
))
1018 /* valid header must be at MyLBA */
1019 if (le64_to_cpu(header
->my_lba
) != lba
)
1028 DBG(LABEL
, ul_debug("found valid GPT Header on LBA %"PRIu64
"", lba
));
1034 DBG(LABEL
, ul_debug("read GPT Header on LBA %"PRIu64
" failed", lba
));
1039 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
1040 const char **name
, uint64_t *offset
, size_t *size
)
1042 struct fdisk_gpt_label
*gpt
;
1057 *name
= _("GPT Header");
1058 *offset
= (uint64_t) GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
1059 *size
= sizeof(struct gpt_header
);
1062 *name
= _("GPT Entries");
1063 gpt
= self_label(cxt
);
1064 *offset
= le64_to_cpu(gpt
->pheader
->partition_entry_lba
) * cxt
->sector_size
;
1065 *size
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
1066 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
1069 return 1; /* no more chunks */
1075 static int gpt_get_disklabel_item(struct fdisk_context
*cxt
, struct fdisk_labelitem
*item
)
1077 struct gpt_header
*h
;
1082 assert(fdisk_is_label(cxt
, GPT
));
1084 h
= self_label(cxt
)->pheader
;
1087 case GPT_LABELITEM_ID
:
1088 item
->name
= _("Disk identifier");
1090 item
->data
.str
= gpt_get_header_id(h
);
1091 if (!item
->data
.str
)
1094 case GPT_LABELITEM_FIRSTLBA
:
1095 item
->name
= _("First LBA");
1097 item
->data
.num64
= le64_to_cpu(h
->first_usable_lba
);
1099 case GPT_LABELITEM_LASTLBA
:
1100 item
->name
= _("Last LBA");
1102 item
->data
.num64
= le64_to_cpu(h
->last_usable_lba
);
1104 case GPT_LABELITEM_ALTLBA
:
1105 /* TRANSLATORS: The LBA (Logical Block Address) of the backup GPT header. */
1106 item
->name
= _("Alternative LBA");
1108 item
->data
.num64
= le64_to_cpu(h
->alternative_lba
);
1110 case GPT_LABELITEM_ENTRIESLBA
:
1111 /* TRANSLATORS: The start of the array of partition entries. */
1112 item
->name
= _("Partition entries LBA");
1114 item
->data
.num64
= le64_to_cpu(h
->partition_entry_lba
);
1116 case GPT_LABELITEM_ENTRIESALLOC
:
1117 item
->name
= _("Allocated partition entries");
1119 item
->data
.num64
= le32_to_cpu(h
->npartition_entries
);
1122 if (item
->id
< __FDISK_NLABELITEMS
)
1123 rc
= 1; /* unsupported generic item */
1125 rc
= 2; /* out of range */
1133 * Returns the number of partitions that are in use.
1135 static unsigned partitions_in_use(struct gpt_header
*header
,
1136 struct gpt_entry
*ents
)
1138 uint32_t i
, used
= 0;
1140 if (!header
|| ! ents
)
1143 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
1144 if (!partition_unused(&ents
[i
]))
1151 * Check if a partition is too big for the disk (sectors).
1152 * Returns the faulting partition number, otherwise 0.
1154 static uint32_t check_too_big_partitions(struct gpt_header
*header
,
1155 struct gpt_entry
*ents
, uint64_t sectors
)
1159 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1160 if (partition_unused(&ents
[i
]))
1162 if (gpt_partition_end(&ents
[i
]) >= sectors
)
1170 * Check if a partition ends before it begins
1171 * Returns the faulting partition number, otherwise 0.
1173 static uint32_t check_start_after_end_partitions(struct gpt_header
*header
,
1174 struct gpt_entry
*ents
)
1178 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1179 if (partition_unused(&ents
[i
]))
1181 if (gpt_partition_start(&ents
[i
]) > gpt_partition_end(&ents
[i
]))
1189 * Check if partition e1 overlaps with partition e2.
1191 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
1193 uint64_t start1
= gpt_partition_start(e1
);
1194 uint64_t end1
= gpt_partition_end(e1
);
1195 uint64_t start2
= gpt_partition_start(e2
);
1196 uint64_t end2
= gpt_partition_end(e2
);
1198 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
1202 * Find any partitions that overlap.
1204 static uint32_t check_overlap_partitions(struct gpt_header
*header
,
1205 struct gpt_entry
*ents
)
1209 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
1210 for (j
= 0; j
< i
; j
++) {
1211 if (partition_unused(&ents
[i
]) ||
1212 partition_unused(&ents
[j
]))
1214 if (partition_overlap(&ents
[i
], &ents
[j
])) {
1215 DBG(LABEL
, ul_debug("GPT partitions overlap detected [%u vs. %u]", i
, j
));
1224 * Find the first available block after the starting point; returns 0 if
1225 * there are no available blocks left, or error. From gdisk.
1227 static uint64_t find_first_available(struct gpt_header
*header
,
1228 struct gpt_entry
*ents
, uint64_t start
)
1231 uint32_t i
, first_moved
= 0;
1235 if (!header
|| !ents
)
1238 fu
= le64_to_cpu(header
->first_usable_lba
);
1239 lu
= le64_to_cpu(header
->last_usable_lba
);
1242 * Begin from the specified starting point or from the first usable
1243 * LBA, whichever is greater...
1245 first
= start
< fu
? fu
: start
;
1248 * Now search through all partitions; if first is within an
1249 * existing partition, move it to the next sector after that
1250 * partition and repeat. If first was moved, set firstMoved
1251 * flag; repeat until firstMoved is not set, so as to catch
1252 * cases where partitions are out of sequential order....
1256 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1257 if (partition_unused(&ents
[i
]))
1259 if (first
< gpt_partition_start(&ents
[i
]))
1261 if (first
<= gpt_partition_end(&ents
[i
])) {
1262 first
= gpt_partition_end(&ents
[i
]) + 1;
1266 } while (first_moved
== 1);
1275 /* Returns last available sector in the free space pointed to by start. From gdisk. */
1276 static uint64_t find_last_free(struct gpt_header
*header
,
1277 struct gpt_entry
*ents
, uint64_t start
)
1280 uint64_t nearest_start
;
1282 if (!header
|| !ents
)
1285 nearest_start
= le64_to_cpu(header
->last_usable_lba
);
1287 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1288 uint64_t ps
= gpt_partition_start(&ents
[i
]);
1290 if (nearest_start
> ps
&& ps
> start
)
1291 nearest_start
= ps
- 1ULL;
1294 return nearest_start
;
1297 /* Returns the last free sector on the disk. From gdisk. */
1298 static uint64_t find_last_free_sector(struct gpt_header
*header
,
1299 struct gpt_entry
*ents
)
1301 uint32_t i
, last_moved
;
1304 if (!header
|| !ents
)
1307 /* start by assuming the last usable LBA is available */
1308 last
= le64_to_cpu(header
->last_usable_lba
);
1311 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1312 if ((last
>= gpt_partition_start(&ents
[i
])) &&
1313 (last
<= gpt_partition_end(&ents
[i
]))) {
1314 last
= gpt_partition_start(&ents
[i
]) - 1ULL;
1318 } while (last_moved
== 1);
1324 * Finds the first available sector in the largest block of unallocated
1325 * space on the disk. Returns 0 if there are no available blocks left.
1328 static uint64_t find_first_in_largest(struct gpt_header
*header
,
1329 struct gpt_entry
*ents
)
1331 uint64_t start
= 0, first_sect
, last_sect
;
1332 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1334 if (!header
|| !ents
)
1338 first_sect
= find_first_available(header
, ents
, start
);
1339 if (first_sect
!= 0) {
1340 last_sect
= find_last_free(header
, ents
, first_sect
);
1341 segment_size
= last_sect
- first_sect
+ 1ULL;
1343 if (segment_size
> selected_size
) {
1344 selected_size
= segment_size
;
1345 selected_segment
= first_sect
;
1347 start
= last_sect
+ 1ULL;
1349 } while (first_sect
!= 0);
1352 return selected_segment
;
1356 * Find the total number of free sectors, the number of segments in which
1357 * they reside, and the size of the largest of those segments. From gdisk.
1359 static uint64_t get_free_sectors(struct fdisk_context
*cxt
, struct gpt_header
*header
,
1360 struct gpt_entry
*ents
, uint32_t *nsegments
,
1361 uint64_t *largest_segment
)
1364 uint64_t first_sect
, last_sect
;
1365 uint64_t largest_seg
= 0, segment_sz
;
1366 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1368 if (!cxt
->total_sectors
)
1372 first_sect
= find_first_available(header
, ents
, start
);
1374 last_sect
= find_last_free(header
, ents
, first_sect
);
1375 segment_sz
= last_sect
- first_sect
+ 1;
1377 if (segment_sz
> largest_seg
)
1378 largest_seg
= segment_sz
;
1379 totfound
+= segment_sz
;
1381 start
= last_sect
+ 1ULL;
1383 } while (first_sect
);
1388 if (largest_segment
)
1389 *largest_segment
= largest_seg
;
1394 static int gpt_probe_label(struct fdisk_context
*cxt
)
1397 struct fdisk_gpt_label
*gpt
;
1401 assert(fdisk_is_label(cxt
, GPT
));
1403 gpt
= self_label(cxt
);
1405 /* TODO: it would be nice to support scenario when GPT headers are OK,
1406 * but PMBR is corrupt */
1407 mbr_type
= valid_pmbr(cxt
);
1411 DBG(LABEL
, ul_debug("found a %s MBR", mbr_type
== GPT_MBR_PROTECTIVE
?
1412 "protective" : "hybrid"));
1414 /* primary header */
1415 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1419 /* primary OK, try backup from alternative LBA */
1420 gpt
->bheader
= gpt_read_header(cxt
,
1421 le64_to_cpu(gpt
->pheader
->alternative_lba
),
1424 /* primary corrupted -- try last LBA */
1425 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), &gpt
->ents
);
1427 if (!gpt
->pheader
&& !gpt
->bheader
)
1430 /* primary OK, backup corrupted -- recovery */
1431 if (gpt
->pheader
&& !gpt
->bheader
) {
1432 fdisk_warnx(cxt
, _("The backup GPT table is corrupt, but the "
1433 "primary appears OK, so that will be used."));
1434 gpt
->bheader
= gpt_copy_header(cxt
, gpt
->pheader
);
1437 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1439 /* primary corrupted, backup OK -- recovery */
1440 } else if (!gpt
->pheader
&& gpt
->bheader
) {
1441 fdisk_warnx(cxt
, _("The primary GPT table is corrupt, but the "
1442 "backup appears OK, so that will be used."));
1443 gpt
->pheader
= gpt_copy_header(cxt
, gpt
->bheader
);
1446 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1449 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1450 cxt
->label
->nparts_cur
= partitions_in_use(gpt
->pheader
, gpt
->ents
);
1453 DBG(LABEL
, ul_debug("GPT probe failed"));
1454 gpt_deinit(cxt
->label
);
1459 * Stolen from libblkid - can be removed once partition semantics
1460 * are added to the fdisk API.
1462 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1466 size_t i
, j
, len
= count
;
1468 dest
= calloc(1, count
);
1472 for (j
= i
= 0; i
+ 2 <= count
; i
+= 2) {
1473 /* always little endian */
1474 c
= (src
[i
+1] << 8) | src
[i
];
1478 } else if (c
< 0x80) {
1481 dest
[j
++] = (uint8_t) c
;
1482 } else if (c
< 0x800) {
1485 dest
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
1486 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1490 dest
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
1491 dest
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
1492 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1500 static int gpt_entry_attrs_to_string(struct gpt_entry
*e
, char **res
)
1502 unsigned int n
, count
= 0;
1513 return 0; /* no attributes at all */
1515 bits
= (char *) &attrs
;
1517 /* Note that sizeof() is correct here, we need separators between
1518 * the strings so also count \0 is correct */
1519 *res
= calloc(1, sizeof(GPT_ATTRSTR_NOBLOCK
) +
1520 sizeof(GPT_ATTRSTR_REQ
) +
1521 sizeof(GPT_ATTRSTR_LEGACY
) +
1522 sizeof("GUID:") + (GPT_ATTRBIT_GUID_COUNT
* 3));
1527 if (isset(bits
, GPT_ATTRBIT_REQ
)) {
1528 memcpy(p
, GPT_ATTRSTR_REQ
, (l
= sizeof(GPT_ATTRSTR_REQ
)));
1531 if (isset(bits
, GPT_ATTRBIT_NOBLOCK
)) {
1534 memcpy(p
, GPT_ATTRSTR_NOBLOCK
, (l
= sizeof(GPT_ATTRSTR_NOBLOCK
)));
1537 if (isset(bits
, GPT_ATTRBIT_LEGACY
)) {
1540 memcpy(p
, GPT_ATTRSTR_LEGACY
, (l
= sizeof(GPT_ATTRSTR_LEGACY
)));
1544 for (n
= GPT_ATTRBIT_GUID_FIRST
;
1545 n
< GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
; n
++) {
1547 if (!isset(bits
, n
))
1552 p
+= sprintf(p
, "GUID:%u", n
);
1554 p
+= sprintf(p
, ",%u", n
);
1561 static int gpt_entry_attrs_from_string(
1562 struct fdisk_context
*cxt
,
1563 struct gpt_entry
*e
,
1566 const char *p
= str
;
1573 DBG(LABEL
, ul_debug("GPT: parsing string attributes '%s'", p
));
1575 bits
= (char *) &attrs
;
1580 while (isblank(*p
)) p
++;
1584 DBG(LABEL
, ul_debug(" parsing item '%s'", p
));
1586 if (strncmp(p
, GPT_ATTRSTR_REQ
,
1587 sizeof(GPT_ATTRSTR_REQ
) - 1) == 0) {
1588 bit
= GPT_ATTRBIT_REQ
;
1589 p
+= sizeof(GPT_ATTRSTR_REQ
) - 1;
1590 } else if (strncmp(p
, GPT_ATTRSTR_REQ_TYPO
,
1591 sizeof(GPT_ATTRSTR_REQ_TYPO
) - 1) == 0) {
1592 bit
= GPT_ATTRBIT_REQ
;
1593 p
+= sizeof(GPT_ATTRSTR_REQ_TYPO
) - 1;
1594 } else if (strncmp(p
, GPT_ATTRSTR_LEGACY
,
1595 sizeof(GPT_ATTRSTR_LEGACY
) - 1) == 0) {
1596 bit
= GPT_ATTRBIT_LEGACY
;
1597 p
+= sizeof(GPT_ATTRSTR_LEGACY
) - 1;
1598 } else if (strncmp(p
, GPT_ATTRSTR_NOBLOCK
,
1599 sizeof(GPT_ATTRSTR_NOBLOCK
) - 1) == 0) {
1600 bit
= GPT_ATTRBIT_NOBLOCK
;
1601 p
+= sizeof(GPT_ATTRSTR_NOBLOCK
) - 1;
1603 /* GUID:<bit> as well as <bit> */
1604 } else if (isdigit((unsigned char) *p
)
1605 || (strncmp(p
, "GUID:", 5) == 0
1606 && isdigit((unsigned char) *(p
+ 5)))) {
1613 bit
= strtol(p
, &end
, 0);
1614 if (errno
|| !end
|| end
== str
1615 || bit
< GPT_ATTRBIT_GUID_FIRST
1616 || bit
>= GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
)
1623 fdisk_warnx(cxt
, _("unsupported GPT attribute bit '%s'"), p
);
1627 if (*p
&& *p
!= ',' && !isblank(*p
)) {
1628 fdisk_warnx(cxt
, _("failed to parse GPT attribute string '%s'"), str
);
1634 while (isblank(*p
)) p
++;
1643 static int gpt_get_partition(struct fdisk_context
*cxt
, size_t n
,
1644 struct fdisk_partition
*pa
)
1646 struct fdisk_gpt_label
*gpt
;
1647 struct gpt_entry
*e
;
1653 assert(fdisk_is_label(cxt
, GPT
));
1655 gpt
= self_label(cxt
);
1657 if ((uint32_t) n
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1660 gpt
= self_label(cxt
);
1663 pa
->used
= !partition_unused(e
) || gpt_partition_start(e
);
1667 pa
->start
= gpt_partition_start(e
);
1668 pa
->size
= gpt_partition_size(e
);
1669 pa
->type
= gpt_partition_parttype(cxt
, e
);
1671 if (guid_to_string(&e
->partition_guid
, u_str
)) {
1672 pa
->uuid
= strdup(u_str
);
1680 rc
= gpt_entry_attrs_to_string(e
, &pa
->attrs
);
1684 pa
->name
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1687 fdisk_reset_partition(pa
);
1692 static int gpt_set_partition(struct fdisk_context
*cxt
, size_t n
,
1693 struct fdisk_partition
*pa
)
1695 struct fdisk_gpt_label
*gpt
;
1696 struct gpt_entry
*e
;
1698 uint64_t start
, end
;
1702 assert(fdisk_is_label(cxt
, GPT
));
1704 gpt
= self_label(cxt
);
1706 if ((uint32_t) n
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1709 FDISK_INIT_UNDEF(start
);
1710 FDISK_INIT_UNDEF(end
);
1712 gpt
= self_label(cxt
);
1716 char new_u
[37], old_u
[37];
1718 guid_to_string(&e
->partition_guid
, old_u
);
1719 rc
= gpt_entry_set_uuid(e
, pa
->uuid
);
1722 guid_to_string(&e
->partition_guid
, new_u
);
1723 fdisk_info(cxt
, _("Partition UUID changed from %s to %s."),
1728 char *old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1729 gpt_entry_set_name(e
, pa
->name
);
1731 fdisk_info(cxt
, _("Partition name changed from '%s' to '%.*s'."),
1732 old
, (int) GPT_PART_NAME_LEN
, pa
->name
);
1736 if (pa
->type
&& pa
->type
->typestr
) {
1737 struct gpt_guid
typeid;
1739 rc
= string_to_guid(pa
->type
->typestr
, &typeid);
1742 gpt_entry_set_type(e
, &typeid);
1745 rc
= gpt_entry_attrs_from_string(cxt
, e
, pa
->attrs
);
1750 if (fdisk_partition_has_start(pa
))
1752 if (fdisk_partition_has_size(pa
) || fdisk_partition_has_start(pa
)) {
1753 uint64_t xstart
= fdisk_partition_has_start(pa
) ? pa
->start
: gpt_partition_start(e
);
1754 uint64_t xsize
= fdisk_partition_has_size(pa
) ? pa
->size
: gpt_partition_size(e
);
1755 end
= xstart
+ xsize
- 1ULL;
1758 if (!FDISK_IS_UNDEF(start
)) {
1759 if (start
< le64_to_cpu(gpt
->pheader
->first_usable_lba
)) {
1760 fdisk_warnx(cxt
, _("The start of the partition understeps FirstUsableLBA."));
1763 e
->lba_start
= cpu_to_le64(start
);
1765 if (!FDISK_IS_UNDEF(end
)) {
1766 if (end
> le64_to_cpu(gpt
->pheader
->last_usable_lba
)) {
1767 fdisk_warnx(cxt
, _("The end of the partition oversteps LastUsableLBA."));
1770 e
->lba_end
= cpu_to_le64(end
);
1772 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1773 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1775 fdisk_label_set_changed(cxt
->label
, 1);
1783 * Returns 0 on success, or corresponding error otherwise.
1785 static int gpt_write_partitions(struct fdisk_context
*cxt
,
1786 struct gpt_header
*header
, struct gpt_entry
*ents
)
1788 off_t offset
= le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
;
1789 uint32_t nparts
= le32_to_cpu(header
->npartition_entries
);
1790 uint32_t totwrite
= nparts
* le32_to_cpu(header
->sizeof_partition_entry
);
1793 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1796 rc
= write(cxt
->dev_fd
, ents
, totwrite
);
1797 if (rc
> 0 && totwrite
== (uint32_t) rc
)
1804 * Write a GPT header to a specified LBA.
1806 * We read all sector, so we have to write all sector back
1807 * to the device -- never ever rely on sizeof(struct gpt_header)!
1809 * Returns 0 on success, or corresponding error otherwise.
1811 static int gpt_write_header(struct fdisk_context
*cxt
,
1812 struct gpt_header
*header
, uint64_t lba
)
1814 off_t offset
= lba
* cxt
->sector_size
;
1816 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1818 if (cxt
->sector_size
==
1819 (size_t) write(cxt
->dev_fd
, header
, cxt
->sector_size
))
1826 * Write the protective MBR.
1827 * Returns 0 on success, or corresponding error otherwise.
1829 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
1832 struct gpt_legacy_mbr
*pmbr
= NULL
;
1835 assert(cxt
->firstsector
);
1837 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
1839 /* zero out the legacy partitions */
1840 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
1842 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
1843 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
1844 pmbr
->partition_record
[0].start_sector
= 1;
1845 pmbr
->partition_record
[0].end_head
= 0xFE;
1846 pmbr
->partition_record
[0].end_sector
= 0xFF;
1847 pmbr
->partition_record
[0].end_track
= 0xFF;
1848 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
1851 * Set size_in_lba to the size of the disk minus one. If the size of the disk
1852 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
1854 if (cxt
->total_sectors
- 1ULL > 0xFFFFFFFFULL
)
1855 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
1857 pmbr
->partition_record
[0].size_in_lba
=
1858 cpu_to_le32((uint32_t) (cxt
->total_sectors
- 1ULL));
1860 offset
= GPT_PMBR_LBA
* cxt
->sector_size
;
1861 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1864 /* pMBR covers the first sector (LBA) of the disk */
1865 if (write_all(cxt
->dev_fd
, pmbr
, cxt
->sector_size
))
1873 * Writes in-memory GPT and pMBR data to disk.
1874 * Returns 0 if successful write, otherwise, a corresponding error.
1875 * Any indication of error will abort the operation.
1877 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
1879 struct fdisk_gpt_label
*gpt
;
1884 assert(fdisk_is_label(cxt
, GPT
));
1886 gpt
= self_label(cxt
);
1887 mbr_type
= valid_pmbr(cxt
);
1889 /* check that disk is big enough to handle the backup header */
1890 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
1893 /* check that the backup header is properly placed */
1894 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1ULL)
1895 /* TODO: correct this (with user authorization) and write */
1898 if (check_overlap_partitions(gpt
->pheader
, gpt
->ents
))
1901 /* recompute CRCs for both headers */
1902 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1903 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1906 * UEFI requires writing in this specific order:
1907 * 1) backup partition tables
1908 * 2) backup GPT header
1909 * 3) primary partition tables
1910 * 4) primary GPT header
1913 * If any write fails, we abort the rest.
1915 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
1917 if (gpt_write_header(cxt
, gpt
->bheader
,
1918 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
1920 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
1922 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
1925 if (mbr_type
== GPT_MBR_HYBRID
)
1926 fdisk_warnx(cxt
, _("The device contains hybrid MBR -- writing GPT only. "
1927 "You have to sync the MBR manually."));
1928 else if (gpt_write_pmbr(cxt
) != 0)
1931 DBG(LABEL
, ul_debug("GPT write success"));
1934 DBG(LABEL
, ul_debug("GPT write failed: incorrect input"));
1938 DBG(LABEL
, ul_debug("GPT write failed: %m"));
1943 * Verify data integrity and report any found problems for:
1944 * - primary and backup header validations
1945 * - partition validations
1947 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
1951 struct fdisk_gpt_label
*gpt
;
1955 assert(fdisk_is_label(cxt
, GPT
));
1957 gpt
= self_label(cxt
);
1961 if (!gpt
->bheader
) {
1963 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
1966 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
1968 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
1970 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
1972 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
1975 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
1977 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
1980 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
1982 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
1984 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
1986 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
1989 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
1991 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
1993 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
1995 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
1998 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
2000 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
2004 * if the GPT is the primary table, check the alternateLBA
2005 * to see if it is a valid GPT
2007 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
2008 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
2010 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
2013 ptnum
= check_overlap_partitions(gpt
->pheader
, gpt
->ents
);
2016 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
2020 ptnum
= check_too_big_partitions(gpt
->pheader
, gpt
->ents
, cxt
->total_sectors
);
2023 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
2027 ptnum
= check_start_after_end_partitions(gpt
->pheader
, gpt
->ents
);
2030 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
2034 if (!nerror
) { /* yay :-) */
2035 uint32_t nsegments
= 0;
2036 uint64_t free_sectors
= 0, largest_segment
= 0;
2039 fdisk_info(cxt
, _("No errors detected."));
2040 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
2041 fdisk_info(cxt
, _("Using %u out of %d partitions."),
2042 partitions_in_use(gpt
->pheader
, gpt
->ents
),
2043 le32_to_cpu(gpt
->pheader
->npartition_entries
));
2045 free_sectors
= get_free_sectors(cxt
, gpt
->pheader
, gpt
->ents
,
2046 &nsegments
, &largest_segment
);
2047 if (largest_segment
)
2048 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
| SIZE_SUFFIX_3LETTER
,
2049 largest_segment
* cxt
->sector_size
);
2052 P_("A total of %ju free sectors is available in %u segment.",
2053 "A total of %ju free sectors is available in %u segments "
2054 "(the largest is %s).", nsegments
),
2055 free_sectors
, nsegments
, strsz
);
2060 P_("%d error detected.", "%d errors detected.", nerror
),
2066 /* Delete a single GPT partition, specified by partnum. */
2067 static int gpt_delete_partition(struct fdisk_context
*cxt
,
2070 struct fdisk_gpt_label
*gpt
;
2074 assert(fdisk_is_label(cxt
, GPT
));
2076 gpt
= self_label(cxt
);
2078 if (partnum
>= cxt
->label
->nparts_max
2079 || partition_unused(&gpt
->ents
[partnum
]))
2082 /* hasta la vista, baby! */
2083 memset(&gpt
->ents
[partnum
], 0, sizeof(struct gpt_entry
));
2084 if (!partition_unused(&gpt
->ents
[partnum
]))
2087 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2088 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2089 cxt
->label
->nparts_cur
--;
2090 fdisk_label_set_changed(cxt
->label
, 1);
2097 /* Performs logical checks to add a new partition entry */
2098 static int gpt_add_partition(
2099 struct fdisk_context
*cxt
,
2100 struct fdisk_partition
*pa
,
2103 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
2104 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
2105 uint64_t dflt_f
, dflt_l
; /* largest segment (default) */
2106 struct gpt_guid
typeid;
2107 struct fdisk_gpt_label
*gpt
;
2108 struct gpt_header
*pheader
;
2109 struct gpt_entry
*e
, *ents
;
2110 struct fdisk_ask
*ask
= NULL
;
2116 assert(fdisk_is_label(cxt
, GPT
));
2118 gpt
= self_label(cxt
);
2119 pheader
= gpt
->pheader
;
2122 rc
= fdisk_partition_next_partno(pa
, cxt
, &partnum
);
2124 DBG(LABEL
, ul_debug("GPT failed to get next partno"));
2127 if (!partition_unused(&ents
[partnum
])) {
2128 fdisk_warnx(cxt
, _("Partition %zu is already defined. "
2129 "Delete it before re-adding it."), partnum
+1);
2132 if (le32_to_cpu(pheader
->npartition_entries
) ==
2133 partitions_in_use(pheader
, ents
)) {
2134 fdisk_warnx(cxt
, _("All partitions are already in use."));
2137 if (!get_free_sectors(cxt
, pheader
, ents
, NULL
, NULL
)) {
2138 fdisk_warnx(cxt
, _("No free sectors available."));
2142 rc
= string_to_guid(pa
&& pa
->type
&& pa
->type
->typestr
?
2144 GPT_DEFAULT_ENTRY_TYPE
, &typeid);
2148 disk_f
= find_first_available(pheader
, ents
, le64_to_cpu(pheader
->first_usable_lba
));
2150 /* if first sector no explicitly defined then ignore small gaps before
2151 * the first partition */
2152 if ((!pa
|| !fdisk_partition_has_start(pa
))
2153 && !partition_unused(&ents
[0])
2154 && disk_f
< gpt_partition_start(&ents
[0])) {
2158 DBG(LABEL
, ul_debug("testing first sector %"PRIu64
"", disk_f
));
2159 disk_f
= find_first_available(pheader
, ents
, disk_f
);
2162 x
= find_last_free(pheader
, ents
, disk_f
);
2163 if (x
- disk_f
>= cxt
->grain
/ cxt
->sector_size
)
2165 DBG(LABEL
, ul_debug("first sector %"PRIu64
" addresses to small space, continue...", disk_f
));
2170 disk_f
= find_first_available(pheader
, ents
, le64_to_cpu(pheader
->first_usable_lba
));
2173 disk_l
= find_last_free_sector(pheader
, ents
);
2175 /* the default is the largest free space */
2176 dflt_f
= find_first_in_largest(pheader
, ents
);
2177 dflt_l
= find_last_free(pheader
, ents
, dflt_f
);
2179 /* align the default in range <dflt_f,dflt_l>*/
2180 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
2183 if (pa
&& pa
->start_follow_default
) {
2186 } else if (pa
&& fdisk_partition_has_start(pa
)) {
2187 DBG(LABEL
, ul_debug("first sector defined: %ju", (uintmax_t)pa
->start
));
2188 if (pa
->start
!= find_first_available(pheader
, ents
, pa
->start
)) {
2189 fdisk_warnx(cxt
, _("Sector %ju already used."), (uintmax_t)pa
->start
);
2197 ask
= fdisk_new_ask();
2199 fdisk_reset_ask(ask
);
2202 fdisk_ask_set_query(ask
, _("First sector"));
2203 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
2204 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
2205 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
2206 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
2208 rc
= fdisk_do_ask(cxt
, ask
);
2212 user_f
= fdisk_ask_number_get_result(ask
);
2213 if (user_f
!= find_first_available(pheader
, ents
, user_f
)) {
2214 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
2223 dflt_l
= find_last_free(pheader
, ents
, user_f
);
2225 if (pa
&& pa
->end_follow_default
) {
2228 } else if (pa
&& fdisk_partition_has_size(pa
)) {
2229 user_l
= user_f
+ pa
->size
- 1;
2230 DBG(LABEL
, ul_debug("size defined: %ju, end: %"PRIu64
" (last possible: %"PRIu64
")",
2231 (uintmax_t)pa
->size
, user_l
, dflt_l
));
2232 if (user_l
!= dflt_l
&& !pa
->size_explicit
2233 && user_l
- user_f
> (cxt
->grain
/ fdisk_get_sector_size(cxt
))) {
2234 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
);
2235 if (user_l
> user_f
)
2241 ask
= fdisk_new_ask();
2243 fdisk_reset_ask(ask
);
2247 fdisk_ask_set_query(ask
, _("Last sector, +sectors or +size{K,M,G,T,P}"));
2248 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
2249 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
2250 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
2251 fdisk_ask_number_set_high(ask
, dflt_l
); /* maximal */
2252 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
2253 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
2255 rc
= fdisk_do_ask(cxt
, ask
);
2259 user_l
= fdisk_ask_number_get_result(ask
);
2260 if (fdisk_ask_number_is_relative(ask
)) {
2261 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
);
2262 if (user_l
> user_f
)
2266 if (user_l
>= user_f
&& user_l
<= disk_l
)
2269 fdisk_warnx(cxt
, _("Value out of range."));
2274 if (user_f
> user_l
|| partnum
>= cxt
->label
->nparts_max
) {
2275 fdisk_warnx(cxt
, _("Could not create partition %zu"), partnum
+ 1);
2280 /* Be paranoid and check against on-disk setting rather than against libfdisk cxt */
2281 if (user_l
> le64_to_cpu(pheader
->last_usable_lba
)) {
2282 fdisk_warnx(cxt
, _("The last usable GPT sector is %ju, but %ju is requested."),
2283 le64_to_cpu(pheader
->last_usable_lba
), user_l
);
2288 if (user_f
< le64_to_cpu(pheader
->first_usable_lba
)) {
2289 fdisk_warnx(cxt
, _("The first usable GPT sector is %ju, but %ju is requested."),
2290 le64_to_cpu(pheader
->first_usable_lba
), user_f
);
2295 assert(!FDISK_IS_UNDEF(user_l
));
2296 assert(!FDISK_IS_UNDEF(user_f
));
2299 e
->lba_end
= cpu_to_le64(user_l
);
2300 e
->lba_start
= cpu_to_le64(user_f
);
2302 gpt_entry_set_type(e
, &typeid);
2304 if (pa
&& pa
->uuid
) {
2305 /* Sometimes it's necessary to create a copy of the PT and
2306 * reuse already defined UUID
2308 rc
= gpt_entry_set_uuid(e
, pa
->uuid
);
2312 /* Any time a new partition entry is created a new GUID must be
2313 * generated for that partition, and every partition is guaranteed
2314 * to have a unique GUID.
2316 uuid_generate_random((unsigned char *) &e
->partition_guid
);
2317 swap_efi_guid(&e
->partition_guid
);
2320 if (pa
&& pa
->name
&& *pa
->name
)
2321 gpt_entry_set_name(e
, pa
->name
);
2322 if (pa
&& pa
->attrs
)
2323 gpt_entry_attrs_from_string(cxt
, e
, pa
->attrs
);
2325 DBG(LABEL
, ul_debug("GPT new partition: partno=%zu, start=%"PRIu64
", end=%"PRIu64
", size=%"PRIu64
"",
2327 gpt_partition_start(e
),
2328 gpt_partition_end(e
),
2329 gpt_partition_size(e
)));
2331 gpt_recompute_crc(gpt
->pheader
, ents
);
2332 gpt_recompute_crc(gpt
->bheader
, ents
);
2336 struct fdisk_parttype
*t
;
2338 cxt
->label
->nparts_cur
++;
2339 fdisk_label_set_changed(cxt
->label
, 1);
2341 t
= gpt_partition_parttype(cxt
, &ents
[partnum
]);
2342 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
2343 fdisk_unref_parttype(t
);
2350 fdisk_unref_ask(ask
);
2355 * Create a new GPT disklabel - destroys any previous data.
2357 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
2362 struct fdisk_gpt_label
*gpt
;
2366 assert(fdisk_is_label(cxt
, GPT
));
2368 gpt
= self_label(cxt
);
2370 /* label private stuff has to be empty, see gpt_deinit() */
2371 assert(gpt
->pheader
== NULL
);
2372 assert(gpt
->bheader
== NULL
);
2375 * When no header, entries or pmbr is set, we're probably
2376 * dealing with a new, empty disk - so always allocate memory
2377 * to deal with the data structures whatever the case is.
2379 rc
= gpt_mknew_pmbr(cxt
);
2383 assert(cxt
->sector_size
>= sizeof(struct gpt_header
));
2386 gpt
->pheader
= calloc(1, cxt
->sector_size
);
2387 if (!gpt
->pheader
) {
2391 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
2395 /* backup ("copy" primary) */
2396 gpt
->bheader
= calloc(1, cxt
->sector_size
);
2397 if (!gpt
->bheader
) {
2401 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
2402 last_lba(cxt
), gpt
->pheader
);
2406 esz
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
2407 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
2408 gpt
->ents
= calloc(1, esz
);
2413 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2414 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2416 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2417 cxt
->label
->nparts_cur
= 0;
2419 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
2420 fdisk_label_set_changed(cxt
->label
, 1);
2421 fdisk_info(cxt
, _("Created a new GPT disklabel (GUID: %s)."), str
);
2426 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
)
2428 struct fdisk_gpt_label
*gpt
;
2429 struct gpt_guid uuid
;
2430 char *str
, *old
, *new;
2435 assert(fdisk_is_label(cxt
, GPT
));
2437 gpt
= self_label(cxt
);
2438 if (fdisk_ask_string(cxt
,
2439 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str
))
2442 rc
= string_to_guid(str
, &uuid
);
2446 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2450 old
= gpt_get_header_id(gpt
->pheader
);
2452 gpt
->pheader
->disk_guid
= uuid
;
2453 gpt
->bheader
->disk_guid
= uuid
;
2455 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2456 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2458 new = gpt_get_header_id(gpt
->pheader
);
2460 fdisk_info(cxt
, _("Disk identifier changed from %s to %s."), old
, new);
2464 fdisk_label_set_changed(cxt
->label
, 1);
2468 static int gpt_check_table_overlap(struct fdisk_context
*cxt
,
2469 uint64_t first_usable
,
2470 uint64_t last_usable
)
2472 struct fdisk_gpt_label
*gpt
= self_label(cxt
);
2476 /* First check if there's enough room for the table. last_lba may have wrapped */
2477 if (first_usable
> cxt
->total_sectors
|| /* far too little space */
2478 last_usable
> cxt
->total_sectors
|| /* wrapped */
2479 first_usable
> last_usable
) { /* too little space */
2480 fdisk_warnx(cxt
, _("Not enough space for new partition table!"));
2484 /* check that all partitions fit in the remaining space */
2485 for (i
= 0; i
< le32_to_cpu(gpt
->pheader
->npartition_entries
); i
++) {
2486 if (partition_unused(&gpt
->ents
[i
]))
2488 if (gpt_partition_start(&gpt
->ents
[i
]) < first_usable
) {
2489 fdisk_warnx(cxt
, _("Partition #%u out of range (minimal start is %"PRIu64
" sectors)"),
2490 i
+ 1, first_usable
);
2493 if (gpt_partition_end(&gpt
->ents
[i
]) > last_usable
) {
2494 fdisk_warnx(cxt
, _("Partition #%u out of range (maximal end is %"PRIu64
" sectors)"),
2495 i
+ 1, last_usable
- 1ULL);
2503 * fdisk_gpt_set_npartitions:
2505 * @entries: new size
2507 * Elarge GPT entries array if possible. The function check if an existing
2508 * partition does not overlap the entries array area. If yes, then it report
2509 * warning and returns -EINVAL.
2511 * Returns: 0 on success, < 0 on error.
2514 int fdisk_gpt_set_npartitions(struct fdisk_context
*cxt
, uint32_t entries
)
2516 struct fdisk_gpt_label
*gpt
;
2517 size_t old_size
, new_size
;
2519 struct gpt_entry
*ents
;
2520 uint64_t first_usable
, last_usable
;
2525 assert(fdisk_is_label(cxt
, GPT
));
2527 gpt
= self_label(cxt
);
2529 old
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2531 return 0; /* do nothing, say nothing */
2533 /* calculate the size (bytes) of the entries array */
2534 new_size
= entries
* le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
2535 old_size
= old
* le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
2537 /* calculate new range of usable LBAs */
2538 first_usable
= (uint64_t) (new_size
/ cxt
->sector_size
) + 2ULL;
2539 last_usable
= cxt
->total_sectors
- 2ULL - (uint64_t) (new_size
/ cxt
->sector_size
);
2541 /* if expanding the table, first check that everything fits,
2542 * then allocate more memory and zero. */
2543 if (entries
> old
) {
2544 rc
= gpt_check_table_overlap(cxt
, first_usable
, last_usable
);
2547 ents
= realloc(gpt
->ents
, new_size
);
2549 fdisk_warnx(cxt
, _("Cannot allocate memory!"));
2552 memset(ents
+ old
, 0, new_size
- old_size
);
2556 /* everything's ok, apply the new size */
2557 gpt
->pheader
->npartition_entries
= cpu_to_le32(entries
);
2558 gpt
->bheader
->npartition_entries
= cpu_to_le32(entries
);
2560 /* usable LBA addresses will have changed */
2561 fdisk_set_first_lba(cxt
, first_usable
);
2562 fdisk_set_last_lba(cxt
, last_usable
);
2563 gpt
->pheader
->first_usable_lba
= cpu_to_le64(first_usable
);
2564 gpt
->bheader
->first_usable_lba
= cpu_to_le64(first_usable
);
2565 gpt
->pheader
->last_usable_lba
= cpu_to_le64(last_usable
);
2566 gpt
->bheader
->last_usable_lba
= cpu_to_le64(last_usable
);
2569 /* The backup header must be recalculated */
2570 gpt_mknew_header_common(cxt
, gpt
->bheader
, le64_to_cpu(gpt
->pheader
->alternative_lba
));
2572 /* CRCs will have changed */
2573 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2574 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2576 fdisk_info(cxt
, _("Partition table length changed from %"PRIu32
" to %"PRIu64
"."), old
, entries
);
2578 fdisk_label_set_changed(cxt
->label
, 1);
2582 static int gpt_part_is_used(struct fdisk_context
*cxt
, size_t i
)
2584 struct fdisk_gpt_label
*gpt
;
2585 struct gpt_entry
*e
;
2589 assert(fdisk_is_label(cxt
, GPT
));
2591 gpt
= self_label(cxt
);
2593 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2597 return !partition_unused(e
) || gpt_partition_start(e
);
2601 * fdisk_gpt_is_hybrid:
2604 * The regular GPT contains PMBR (dummy protective MBR) where the protective
2605 * MBR does not address any partitions.
2607 * Hybrid GPT contains regular MBR where this partition table addresses the
2608 * same partitions as GPT. It's recommended to not use hybrid GPT due to MBR
2611 * The libfdisk does not provide functionality to sync GPT and MBR, you have to
2612 * directly access and modify (P)MBR (see fdisk_new_nested_context()).
2614 * Returns: 1 if partition table detected as hybrid otherwise return 0
2616 int fdisk_gpt_is_hybrid(struct fdisk_context
*cxt
)
2619 return valid_pmbr(cxt
) == GPT_MBR_HYBRID
;
2623 * fdisk_gpt_get_partition_attrs:
2625 * @partnum: partition number
2626 * @attrs: GPT partition attributes
2628 * Sets @attrs for the given partition
2630 * Returns: 0 on success, <0 on error.
2632 int fdisk_gpt_get_partition_attrs(
2633 struct fdisk_context
*cxt
,
2637 struct fdisk_gpt_label
*gpt
;
2641 assert(fdisk_is_label(cxt
, GPT
));
2643 gpt
= self_label(cxt
);
2645 if ((uint32_t) partnum
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2648 *attrs
= le64_to_cpu(gpt
->ents
[partnum
].attrs
);
2653 * fdisk_gpt_set_partition_attrs:
2655 * @partnum: partition number
2656 * @attrs: GPT partition attributes
2658 * Sets the GPT partition attributes field to @attrs.
2660 * Returns: 0 on success, <0 on error.
2662 int fdisk_gpt_set_partition_attrs(
2663 struct fdisk_context
*cxt
,
2667 struct fdisk_gpt_label
*gpt
;
2671 assert(fdisk_is_label(cxt
, GPT
));
2673 DBG(LABEL
, ul_debug("GPT entry attributes change requested partno=%zu", partnum
));
2674 gpt
= self_label(cxt
);
2676 if ((uint32_t) partnum
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2679 gpt
->ents
[partnum
].attrs
= cpu_to_le64(attrs
);
2680 fdisk_info(cxt
, _("The attributes on partition %zu changed to 0x%016" PRIx64
"."),
2681 partnum
+ 1, attrs
);
2683 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2684 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2685 fdisk_label_set_changed(cxt
->label
, 1);
2689 static int gpt_toggle_partition_flag(
2690 struct fdisk_context
*cxt
,
2694 struct fdisk_gpt_label
*gpt
;
2698 const char *name
= NULL
;
2703 assert(fdisk_is_label(cxt
, GPT
));
2705 DBG(LABEL
, ul_debug("GPT entry attribute change requested partno=%zu", i
));
2706 gpt
= self_label(cxt
);
2708 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2711 attrs
= gpt
->ents
[i
].attrs
;
2712 bits
= (char *) &attrs
;
2715 case GPT_FLAG_REQUIRED
:
2716 bit
= GPT_ATTRBIT_REQ
;
2717 name
= GPT_ATTRSTR_REQ
;
2719 case GPT_FLAG_NOBLOCK
:
2720 bit
= GPT_ATTRBIT_NOBLOCK
;
2721 name
= GPT_ATTRSTR_NOBLOCK
;
2723 case GPT_FLAG_LEGACYBOOT
:
2724 bit
= GPT_ATTRBIT_LEGACY
;
2725 name
= GPT_ATTRSTR_LEGACY
;
2727 case GPT_FLAG_GUIDSPECIFIC
:
2728 rc
= fdisk_ask_number(cxt
, 48, 48, 63, _("Enter GUID specific bit"), &tmp
);
2734 /* already specified PT_FLAG_GUIDSPECIFIC bit */
2735 if (flag
>= 48 && flag
<= 63) {
2737 flag
= GPT_FLAG_GUIDSPECIFIC
;
2743 fdisk_warnx(cxt
, _("failed to toggle unsupported bit %lu"), flag
);
2747 if (!isset(bits
, bit
))
2752 gpt
->ents
[i
].attrs
= attrs
;
2754 if (flag
== GPT_FLAG_GUIDSPECIFIC
)
2755 fdisk_info(cxt
, isset(bits
, bit
) ?
2756 _("The GUID specific bit %d on partition %zu is enabled now.") :
2757 _("The GUID specific bit %d on partition %zu is disabled now."),
2760 fdisk_info(cxt
, isset(bits
, bit
) ?
2761 _("The %s flag on partition %zu is enabled now.") :
2762 _("The %s flag on partition %zu is disabled now."),
2765 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2766 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2767 fdisk_label_set_changed(cxt
->label
, 1);
2771 static int gpt_entry_cmp_start(const void *a
, const void *b
)
2773 struct gpt_entry
*ae
= (struct gpt_entry
*) a
,
2774 *be
= (struct gpt_entry
*) b
;
2775 int au
= partition_unused(ae
),
2776 bu
= partition_unused(be
);
2785 return cmp_numbers(gpt_partition_start(ae
), gpt_partition_start(be
));
2788 /* sort partition by start sector */
2789 static int gpt_reorder(struct fdisk_context
*cxt
)
2791 struct fdisk_gpt_label
*gpt
;
2792 size_t i
, nparts
, mess
;
2796 assert(fdisk_is_label(cxt
, GPT
));
2798 gpt
= self_label(cxt
);
2799 nparts
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2801 for (i
= 0, mess
= 0; mess
== 0 && i
+ 1 < nparts
; i
++)
2802 mess
= gpt_entry_cmp_start(
2803 (const void *) &gpt
->ents
[i
],
2804 (const void *) &gpt
->ents
[i
+ 1]) > 0;
2807 fdisk_info(cxt
, _("Nothing to do. Ordering is correct already."));
2811 qsort(gpt
->ents
, nparts
, sizeof(struct gpt_entry
),
2812 gpt_entry_cmp_start
);
2814 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2815 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2816 fdisk_label_set_changed(cxt
->label
, 1);
2821 static int gpt_reset_alignment(struct fdisk_context
*cxt
)
2823 struct fdisk_gpt_label
*gpt
;
2824 struct gpt_header
*h
;
2828 assert(fdisk_is_label(cxt
, GPT
));
2830 gpt
= self_label(cxt
);
2831 h
= gpt
? gpt
->pheader
: NULL
;
2834 /* always follow existing table */
2835 cxt
->first_lba
= le64_to_cpu(h
->first_usable_lba
);
2836 cxt
->last_lba
= le64_to_cpu(h
->last_usable_lba
);
2838 /* estimate ranges for GPT */
2839 uint64_t first
, last
;
2841 count_first_last_lba(cxt
, &first
, &last
);
2843 if (cxt
->first_lba
< first
)
2844 cxt
->first_lba
= first
;
2845 if (cxt
->last_lba
> last
)
2846 cxt
->last_lba
= last
;
2852 * Deinitialize fdisk-specific variables
2854 static void gpt_deinit(struct fdisk_label
*lb
)
2856 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
2866 gpt
->pheader
= NULL
;
2867 gpt
->bheader
= NULL
;
2870 static const struct fdisk_label_operations gpt_operations
=
2872 .probe
= gpt_probe_label
,
2873 .write
= gpt_write_disklabel
,
2874 .verify
= gpt_verify_disklabel
,
2875 .create
= gpt_create_disklabel
,
2876 .locate
= gpt_locate_disklabel
,
2877 .get_item
= gpt_get_disklabel_item
,
2878 .set_id
= gpt_set_disklabel_id
,
2880 .get_part
= gpt_get_partition
,
2881 .set_part
= gpt_set_partition
,
2882 .add_part
= gpt_add_partition
,
2883 .del_part
= gpt_delete_partition
,
2884 .reorder
= gpt_reorder
,
2886 .part_is_used
= gpt_part_is_used
,
2887 .part_toggle_flag
= gpt_toggle_partition_flag
,
2889 .deinit
= gpt_deinit
,
2891 .reset_alignment
= gpt_reset_alignment
2894 static const struct fdisk_field gpt_fields
[] =
2897 { FDISK_FIELD_DEVICE
, N_("Device"), 10, 0 },
2898 { FDISK_FIELD_START
, N_("Start"), 5, FDISK_FIELDFL_NUMBER
},
2899 { FDISK_FIELD_END
, N_("End"), 5, FDISK_FIELDFL_NUMBER
},
2900 { FDISK_FIELD_SECTORS
, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER
},
2901 { FDISK_FIELD_SIZE
, N_("Size"), 5, FDISK_FIELDFL_NUMBER
| FDISK_FIELDFL_EYECANDY
},
2902 { FDISK_FIELD_TYPE
, N_("Type"), 0.1, FDISK_FIELDFL_EYECANDY
},
2904 { FDISK_FIELD_TYPEID
, N_("Type-UUID"), 36, FDISK_FIELDFL_DETAIL
},
2905 { FDISK_FIELD_UUID
, N_("UUID"), 36, FDISK_FIELDFL_DETAIL
},
2906 { FDISK_FIELD_NAME
, N_("Name"), 0.2, FDISK_FIELDFL_DETAIL
},
2907 { FDISK_FIELD_ATTR
, N_("Attrs"), 0, FDISK_FIELDFL_DETAIL
}
2911 * allocates GPT in-memory stuff
2913 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
)
2915 struct fdisk_label
*lb
;
2916 struct fdisk_gpt_label
*gpt
;
2920 gpt
= calloc(1, sizeof(*gpt
));
2924 /* initialize generic part of the driver */
2925 lb
= (struct fdisk_label
*) gpt
;
2927 lb
->id
= FDISK_DISKLABEL_GPT
;
2928 lb
->op
= &gpt_operations
;
2929 lb
->parttypes
= gpt_parttypes
;
2930 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);
2932 lb
->fields
= gpt_fields
;
2933 lb
->nfields
= ARRAY_SIZE(gpt_fields
);
2939 static int test_getattr(struct fdisk_test
*ts
, int argc
, char *argv
[])
2941 const char *disk
= argv
[1];
2942 size_t part
= strtoul(argv
[2], NULL
, 0) - 1;
2943 struct fdisk_context
*cxt
;
2944 uint64_t atters
= 0;
2946 cxt
= fdisk_new_context();
2947 fdisk_assign_device(cxt
, disk
, 1);
2949 if (!fdisk_is_label(cxt
, GPT
))
2950 return EXIT_FAILURE
;
2952 if (fdisk_gpt_get_partition_attrs(cxt
, part
, &atters
))
2953 return EXIT_FAILURE
;
2955 printf("%s: 0x%016" PRIx64
"\n", argv
[2], atters
);
2957 fdisk_unref_context(cxt
);
2961 static int test_setattr(struct fdisk_test
*ts
, int argc
, char *argv
[])
2963 const char *disk
= argv
[1];
2964 size_t part
= strtoul(argv
[2], NULL
, 0) - 1;
2965 uint64_t atters
= strtoull(argv
[3], NULL
, 0);
2966 struct fdisk_context
*cxt
;
2968 cxt
= fdisk_new_context();
2969 fdisk_assign_device(cxt
, disk
, 0);
2971 if (!fdisk_is_label(cxt
, GPT
))
2972 return EXIT_FAILURE
;
2974 if (fdisk_gpt_set_partition_attrs(cxt
, part
, atters
))
2975 return EXIT_FAILURE
;
2977 if (fdisk_write_disklabel(cxt
))
2978 return EXIT_FAILURE
;
2980 fdisk_unref_context(cxt
);
2984 int main(int argc
, char *argv
[])
2986 struct fdisk_test tss
[] = {
2987 { "--getattr", test_getattr
, "<disk> <partition> print attributes" },
2988 { "--setattr", test_setattr
, "<disk> <partition> <value> set attributes" },
2992 return fdisk_run_test(tss
, argc
, argv
);