2 * Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org>
5 * GUID Partition Table (GPT) support. Based on UEFI Specs 2.3.1
6 * Chapter 5: GUID Partition Table (GPT) Disk Layout (Jun 27th, 2012).
7 * Some ideas and inspiration from GNU parted and gptfdisk.
14 #include <sys/utsname.h>
15 #include <sys/types.h>
31 #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */
32 #define GPT_HEADER_REVISION_V1_02 0x00010200
33 #define GPT_HEADER_REVISION_V1_00 0x00010000
34 #define GPT_HEADER_REVISION_V0_99 0x00009900
35 #define GPT_HEADER_MINSZ 92 /* bytes */
37 #define GPT_PMBR_LBA 0
38 #define GPT_MBR_PROTECTIVE 1
39 #define GPT_MBR_HYBRID 2
41 #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001
43 #define EFI_PMBR_OSTYPE 0xEE
44 #define MSDOS_MBR_SIGNATURE 0xAA55
45 #define GPT_PART_NAME_LEN (72 / sizeof(uint16_t))
46 #define GPT_NPARTITIONS 128
48 /* Globally unique identifier */
52 uint16_t time_hi_and_version
;
54 uint8_t clock_seq_low
;
59 /* only checking that the GUID is 0 is enough to verify an empty partition. */
60 #define GPT_UNUSED_ENTRY_GUID \
61 ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
62 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
64 /* Linux native partition type */
65 #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
73 GPT_ATTRBIT_NOBLOCK
= 1,
74 GPT_ATTRBIT_LEGACY
= 2,
76 /* GUID specific (range 48..64)*/
77 GPT_ATTRBIT_GUID_FIRST
= 48,
78 GPT_ATTRBIT_GUID_COUNT
= 16
81 #define GPT_ATTRSTR_REQ "RequiredPartiton"
82 #define GPT_ATTRSTR_NOBLOCK "NoBlockIOProtocol"
83 #define GPT_ATTRSTR_LEGACY "LegacyBIOSBootable"
85 /* The GPT Partition entry array contains an array of GPT entries. */
87 struct gpt_guid type
; /* purpose and type of the partition */
88 struct gpt_guid partition_guid
;
92 uint16_t name
[GPT_PART_NAME_LEN
];
93 } __attribute__ ((packed
));
97 uint64_t signature
; /* header identification */
98 uint32_t revision
; /* header version */
99 uint32_t size
; /* in bytes */
100 uint32_t crc32
; /* header CRC checksum */
101 uint32_t reserved1
; /* must be 0 */
102 uint64_t my_lba
; /* LBA that contains this struct (LBA 1) */
103 uint64_t alternative_lba
; /* backup GPT header */
104 uint64_t first_usable_lba
; /* first usable logical block for partitions */
105 uint64_t last_usable_lba
; /* last usable logical block for partitions */
106 struct gpt_guid disk_guid
; /* unique disk identifier */
107 uint64_t partition_entry_lba
; /* stat LBA of the partition entry array */
108 uint32_t npartition_entries
; /* total partition entries - normally 128 */
109 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
110 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
111 uint8_t reserved2
[512 - 92]; /* must be 0 */
112 } __attribute__ ((packed
));
115 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
116 uint8_t start_head
; /* unused by EFI, pt start in CHS */
117 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
119 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
120 uint8_t end_head
; /* unused by EFI, pt end in CHS */
121 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
122 uint8_t end_track
; /* unused by EFI, pt end in CHS */
123 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
124 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
125 } __attribute__ ((packed
));
127 /* Protected MBR and legacy MBR share same structure */
128 struct gpt_legacy_mbr
{
129 uint8_t boot_code
[440];
130 uint32_t unique_mbr_signature
;
132 struct gpt_record partition_record
[4];
134 } __attribute__ ((packed
));
138 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
140 #define DEF_GUID(_u, _n) \
146 static struct fdisk_parttype gpt_parttypes
[] =
149 DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")),
151 DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")),
152 DEF_GUID("D3BFE2DE-3DAF-11DF-BA40-E3A556D89593", N_("Intel Fast Flash")),
154 /* Hah!IdontneedEFI */
155 DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot")),
158 DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")),
159 DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")),
160 DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")),
161 DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")),
162 DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery environment")),
163 DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")),
166 DEF_GUID("75894C1E-3AEB-11D3-B7C1-7B03A0000000", N_("HP-UX data")),
167 DEF_GUID("E2A1E728-32E3-11D6-A682-7B03A0000000", N_("HP-UX service")),
169 /* Linux (http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec) */
170 DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")),
171 DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")),
172 DEF_GUID("3B8F8425-20E0-4F3B-907F-1A25A76F98E8", N_("Linux server data")),
173 DEF_GUID("44479540-F297-41B2-9AF7-D131D5F0458A", N_("Linux root (x86)")),
174 DEF_GUID("4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", N_("Linux root (x86-64)")),
175 DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")),
176 DEF_GUID("933AC7E1-2EB4-4F13-B844-0E14E2AEF915", N_("Linux home")),
177 DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")),
178 DEF_GUID("BC13C2FF-59E6-4262-A352-B275FD6F7172", N_("Linux extended boot")),
179 DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")),
182 DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")),
183 DEF_GUID("83BD6B9D-7F41-11DC-BE0B-001560B84F0F", N_("FreeBSD boot")),
184 DEF_GUID("516E7CB5-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD swap")),
185 DEF_GUID("516E7CB6-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD UFS")),
186 DEF_GUID("516E7CBA-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD ZFS")),
187 DEF_GUID("516E7CB8-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD Vinum")),
190 DEF_GUID("48465300-0000-11AA-AA11-00306543ECAC", N_("Apple HFS/HFS+")),
191 DEF_GUID("55465300-0000-11AA-AA11-00306543ECAC", N_("Apple UFS")),
192 DEF_GUID("52414944-0000-11AA-AA11-00306543ECAC", N_("Apple RAID")),
193 DEF_GUID("52414944-5F4F-11AA-AA11-00306543ECAC", N_("Apple RAID offline")),
194 DEF_GUID("426F6F74-0000-11AA-AA11-00306543ECAC", N_("Apple boot")),
195 DEF_GUID("4C616265-6C00-11AA-AA11-00306543ECAC", N_("Apple label")),
196 DEF_GUID("5265636F-7665-11AA-AA11-00306543ECAC", N_("Apple TV recovery")),
197 DEF_GUID("53746F72-6167-11AA-AA11-00306543ECAC", N_("Apple Core storage")),
200 DEF_GUID("6A82CB45-1DD2-11B2-99A6-080020736631", N_("Solaris boot")),
201 DEF_GUID("6A85CF4D-1DD2-11B2-99A6-080020736631", N_("Solaris root")),
202 /* same as Apple ZFS */
203 DEF_GUID("6A898CC3-1DD2-11B2-99A6-080020736631", N_("Solaris /usr & Apple ZFS")),
204 DEF_GUID("6A87C46F-1DD2-11B2-99A6-080020736631", N_("Solaris swap")),
205 DEF_GUID("6A8B642B-1DD2-11B2-99A6-080020736631", N_("Solaris backup")),
206 DEF_GUID("6A8EF2E9-1DD2-11B2-99A6-080020736631", N_("Solaris /var")),
207 DEF_GUID("6A90BA39-1DD2-11B2-99A6-080020736631", N_("Solaris /home")),
208 DEF_GUID("6A9283A5-1DD2-11B2-99A6-080020736631", N_("Solaris alternate sector")),
209 DEF_GUID("6A945A3B-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 1")),
210 DEF_GUID("6A9630D1-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 2")),
211 DEF_GUID("6A980767-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 3")),
212 DEF_GUID("6A96237F-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 4")),
213 DEF_GUID("6A8D2AC7-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 5")),
216 DEF_GUID("49F48D32-B10E-11DC-B99B-0019D1879648", N_("NetBSD swap")),
217 DEF_GUID("49F48D5A-B10E-11DC-B99B-0019D1879648", N_("NetBSD FFS")),
218 DEF_GUID("49F48D82-B10E-11DC-B99B-0019D1879648", N_("NetBSD LFS")),
219 DEF_GUID("2DB519C4-B10E-11DC-B99B-0019D1879648", N_("NetBSD concatenated")),
220 DEF_GUID("2DB519EC-B10E-11DC-B99B-0019D1879648", N_("NetBSD encrypted")),
221 DEF_GUID("49F48DAA-B10E-11DC-B99B-0019D1879648", N_("NetBSD RAID")),
224 DEF_GUID("FE3A2A5D-4F32-41A7-B725-ACCC3285A309", N_("ChromeOS kernel")),
225 DEF_GUID("3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", N_("ChromeOS root fs")),
226 DEF_GUID("2E0A753D-9E48-43B0-8337-B15192CB1B5E", N_("ChromeOS reserved")),
229 DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")),
230 DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")),
231 DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")),
232 DEF_GUID("0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")),
233 DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")),
234 DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")),
237 /* gpt_entry macros */
238 #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start)
239 #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end)
242 * in-memory fdisk GPT stuff
244 struct fdisk_gpt_label
{
245 struct fdisk_label head
; /* generic part */
247 /* gpt specific part */
248 struct gpt_header
*pheader
; /* primary header */
249 struct gpt_header
*bheader
; /* backup header */
250 struct gpt_entry
*ents
; /* entries (partitions) */
253 static void gpt_deinit(struct fdisk_label
*lb
);
255 static inline struct fdisk_gpt_label
*self_label(struct fdisk_context
*cxt
)
257 return (struct fdisk_gpt_label
*) cxt
->label
;
261 * Returns the partition length, or 0 if end is before beginning.
263 static uint64_t gpt_partition_size(const struct gpt_entry
*e
)
265 uint64_t start
= gpt_partition_start(e
);
266 uint64_t end
= gpt_partition_end(e
);
268 return start
> end
? 0 : end
- start
+ 1ULL;
271 /* prints UUID in the real byte order! */
272 static void gpt_debug_uuid(const char *mesg
, struct gpt_guid
*guid
)
274 const unsigned char *uuid
= (unsigned char *) guid
;
276 fprintf(stderr
, "%s: "
277 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
279 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
283 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
287 * UUID is traditionally 16 byte big-endian array, except Intel EFI
288 * specification where the UUID is a structure of little-endian fields.
290 static void swap_efi_guid(struct gpt_guid
*uid
)
292 uid
->time_low
= swab32(uid
->time_low
);
293 uid
->time_mid
= swab16(uid
->time_mid
);
294 uid
->time_hi_and_version
= swab16(uid
->time_hi_and_version
);
297 static int string_to_guid(const char *in
, struct gpt_guid
*guid
)
299 if (uuid_parse(in
, (unsigned char *) guid
)) /* BE */
301 swap_efi_guid(guid
); /* LE */
305 static char *guid_to_string(const struct gpt_guid
*guid
, char *out
)
307 struct gpt_guid u
= *guid
; /* LE */
309 swap_efi_guid(&u
); /* BE */
310 uuid_unparse_upper((unsigned char *) &u
, out
);
315 static struct fdisk_parttype
*gpt_partition_parttype(
316 struct fdisk_context
*cxt
,
317 const struct gpt_entry
*e
)
319 struct fdisk_parttype
*t
;
322 guid_to_string(&e
->type
, str
);
323 t
= fdisk_get_parttype_from_string(cxt
, str
);
324 return t
? : fdisk_new_unknown_parttype(0, str
);
329 static const char *gpt_get_header_revstr(struct gpt_header
*header
)
334 switch (header
->revision
) {
335 case GPT_HEADER_REVISION_V1_02
:
337 case GPT_HEADER_REVISION_V1_00
:
339 case GPT_HEADER_REVISION_V0_99
:
349 static inline int partition_unused(const struct gpt_entry
*e
)
351 return !memcmp(&e
->type
, &GPT_UNUSED_ENTRY_GUID
,
352 sizeof(struct gpt_guid
));
356 * Builds a clean new valid protective MBR - will wipe out any existing data.
357 * Returns 0 on success, otherwise < 0 on error.
359 static int gpt_mknew_pmbr(struct fdisk_context
*cxt
)
361 struct gpt_legacy_mbr
*pmbr
= NULL
;
363 if (!cxt
|| !cxt
->firstsector
)
366 fdisk_zeroize_firstsector(cxt
);
368 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
370 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
371 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
372 pmbr
->partition_record
[0].start_sector
= 1;
373 pmbr
->partition_record
[0].end_head
= 0xFE;
374 pmbr
->partition_record
[0].end_sector
= 0xFF;
375 pmbr
->partition_record
[0].end_track
= 0xFF;
376 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
377 pmbr
->partition_record
[0].size_in_lba
=
378 cpu_to_le32(min((uint32_t) cxt
->total_sectors
- 1, 0xFFFFFFFF));
383 /* some universal differences between the headers */
384 static void gpt_mknew_header_common(struct fdisk_context
*cxt
,
385 struct gpt_header
*header
, uint64_t lba
)
390 header
->my_lba
= cpu_to_le64(lba
);
392 if (lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
) { /* primary */
393 header
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1);
394 header
->partition_entry_lba
= cpu_to_le64(2);
395 } else { /* backup */
396 uint64_t esz
= le32_to_cpu(header
->npartition_entries
) * sizeof(struct gpt_entry
);
397 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
399 header
->alternative_lba
= cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA
);
400 header
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
405 * Builds a new GPT header (at sector lba) from a backup header2.
406 * If building a primary header, then backup is the secondary, and vice versa.
408 * Always pass a new (zeroized) header to build upon as we don't
409 * explicitly zero-set some values such as CRCs and reserved.
411 * Returns 0 on success, otherwise < 0 on error.
413 static int gpt_mknew_header_from_bkp(struct fdisk_context
*cxt
,
414 struct gpt_header
*header
,
416 struct gpt_header
*header2
)
418 if (!cxt
|| !header
|| !header2
)
421 header
->signature
= header2
->signature
;
422 header
->revision
= header2
->revision
;
423 header
->size
= header2
->size
;
424 header
->npartition_entries
= header2
->npartition_entries
;
425 header
->sizeof_partition_entry
= header2
->sizeof_partition_entry
;
426 header
->first_usable_lba
= header2
->first_usable_lba
;
427 header
->last_usable_lba
= header2
->last_usable_lba
;
429 memcpy(&header
->disk_guid
,
430 &header2
->disk_guid
, sizeof(header2
->disk_guid
));
431 gpt_mknew_header_common(cxt
, header
, lba
);
436 static struct gpt_header
*gpt_copy_header(struct fdisk_context
*cxt
,
437 struct gpt_header
*src
)
439 struct gpt_header
*res
;
444 res
= calloc(1, sizeof(*res
));
446 fdisk_warn(cxt
, _("failed to allocate GPT header"));
450 res
->my_lba
= src
->alternative_lba
;
451 res
->alternative_lba
= src
->my_lba
;
453 res
->signature
= src
->signature
;
454 res
->revision
= src
->revision
;
455 res
->size
= src
->size
;
456 res
->npartition_entries
= src
->npartition_entries
;
457 res
->sizeof_partition_entry
= src
->sizeof_partition_entry
;
458 res
->first_usable_lba
= src
->first_usable_lba
;
459 res
->last_usable_lba
= src
->last_usable_lba
;
461 memcpy(&res
->disk_guid
, &src
->disk_guid
, sizeof(src
->disk_guid
));
464 if (res
->my_lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
)
465 res
->partition_entry_lba
= cpu_to_le64(2);
467 uint64_t esz
= le32_to_cpu(src
->npartition_entries
) * sizeof(struct gpt_entry
);
468 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
470 res
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1 - esects
);
476 static void count_first_last_lba(struct fdisk_context
*cxt
,
477 uint64_t *first
, uint64_t *last
)
483 esz
= sizeof(struct gpt_entry
) * GPT_NPARTITIONS
/ cxt
->sector_size
;
484 *last
= cxt
->total_sectors
- 2 - esz
;
487 if (*first
< cxt
->first_lba
&& cxt
->first_lba
< *last
)
488 /* Align according to topology */
489 *first
= cxt
->first_lba
;
493 * Builds a clean new GPT header (currently under revision 1.0).
495 * Always pass a new (zeroized) header to build upon as we don't
496 * explicitly zero-set some values such as CRCs and reserved.
498 * Returns 0 on success, otherwise < 0 on error.
500 static int gpt_mknew_header(struct fdisk_context
*cxt
,
501 struct gpt_header
*header
, uint64_t lba
)
503 uint64_t first
, last
;
508 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
509 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
510 header
->size
= cpu_to_le32(sizeof(struct gpt_header
));
513 * 128 partitions is the default. It can go behond this, however,
514 * we're creating a de facto header here, so no funny business.
516 header
->npartition_entries
= cpu_to_le32(GPT_NPARTITIONS
);
517 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
519 count_first_last_lba(cxt
, &first
, &last
);
520 header
->first_usable_lba
= cpu_to_le64(first
);
521 header
->last_usable_lba
= cpu_to_le64(last
);
523 gpt_mknew_header_common(cxt
, header
, lba
);
524 uuid_generate_random((unsigned char *) &header
->disk_guid
);
525 swap_efi_guid(&header
->disk_guid
);
531 * Checks if there is a valid protective MBR partition table.
532 * Returns 0 if it is invalid or failure. Otherwise, return
533 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection.
535 static int valid_pmbr(struct fdisk_context
*cxt
)
537 int i
, part
= 0, ret
= 0; /* invalid by default */
538 struct gpt_legacy_mbr
*pmbr
= NULL
;
541 if (!cxt
->firstsector
)
544 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
546 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
549 /* LBA of the GPT partition header */
550 if (pmbr
->partition_record
[0].starting_lba
!=
551 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
554 /* seems like a valid MBR was found, check DOS primary partitions */
555 for (i
= 0; i
< 4; i
++) {
556 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
558 * Ok, we at least know that there's a protective MBR,
559 * now check if there are other partition types for
563 ret
= GPT_MBR_PROTECTIVE
;
568 if (ret
!= GPT_MBR_PROTECTIVE
)
571 for (i
= 0 ; i
< 4; i
++) {
572 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
573 (pmbr
->partition_record
[i
].os_type
!= 0x00))
574 ret
= GPT_MBR_HYBRID
;
578 * Protective MBRs take up the lesser of the whole disk
579 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
580 * Some partitioning programs, nonetheless, choose to set
581 * the size to the maximum 32-bit limitation, disregarding
584 * Hybrid MBRs do not necessarily comply with this.
586 * Consider a bad value here to be a warning to support dd-ing
587 * an image from a smaller disk to a bigger disk.
589 if (ret
== GPT_MBR_PROTECTIVE
) {
590 sz_lba
= le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
591 if (sz_lba
!= (uint32_t) cxt
->total_sectors
- 1 && sz_lba
!= 0xFFFFFFFF) {
592 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%u != %u) "
593 "will be corrected by w(rite)."),
595 (uint32_t) cxt
->total_sectors
- 1);
596 fdisk_label_set_changed(cxt
->label
, 1);
603 static uint64_t last_lba(struct fdisk_context
*cxt
)
607 memset(&s
, 0, sizeof(s
));
608 if (fstat(cxt
->dev_fd
, &s
) == -1) {
609 fdisk_warn(cxt
, _("gpt: stat() failed"));
613 if (S_ISBLK(s
.st_mode
))
614 return cxt
->total_sectors
- 1;
615 else if (S_ISREG(s
.st_mode
)) {
616 uint64_t sectors
= s
.st_size
>> cxt
->sector_size
;
617 return (sectors
/ cxt
->sector_size
) - 1ULL;
619 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
623 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
624 void *buffer
, const size_t bytes
)
626 off_t offset
= lba
* cxt
->sector_size
;
628 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
630 return read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
634 /* Returns the GPT entry array */
635 static struct gpt_entry
*gpt_read_entries(struct fdisk_context
*cxt
,
636 struct gpt_header
*header
)
639 struct gpt_entry
*ret
= NULL
;
645 sz
= le32_to_cpu(header
->npartition_entries
) *
646 le32_to_cpu(header
->sizeof_partition_entry
);
651 offset
= le64_to_cpu(header
->partition_entry_lba
) *
654 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
656 if (sz
!= read(cxt
->dev_fd
, ret
, sz
))
666 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
)
668 return (crc32(~0L, buf
, len
) ^ ~0L);
672 * Recompute header and partition array 32bit CRC checksums.
673 * This function does not fail - if there's corruption, then it
674 * will be reported when checksuming it again (ie: probing or verify).
676 static void gpt_recompute_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
686 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
687 header
->crc32
= cpu_to_le32(crc
);
689 /* partition entry array CRC */
690 header
->partition_entry_array_crc32
= 0;
691 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
692 le32_to_cpu(header
->sizeof_partition_entry
);
694 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
695 header
->partition_entry_array_crc32
= cpu_to_le32(crc
);
699 * Compute the 32bit CRC checksum of the partition table header.
700 * Returns 1 if it is valid, otherwise 0.
702 static int gpt_check_header_crc(struct gpt_header
*header
, struct gpt_entry
*ents
)
704 uint32_t crc
, orgcrc
= le32_to_cpu(header
->crc32
);
707 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
708 header
->crc32
= cpu_to_le32(orgcrc
);
710 if (crc
== le32_to_cpu(header
->crc32
))
714 * If we have checksum mismatch it may be due to stale data,
715 * like a partition being added or deleted. Recompute the CRC again
716 * and make sure this is not the case.
719 gpt_recompute_crc(header
, ents
);
720 orgcrc
= le32_to_cpu(header
->crc32
);
722 crc
= count_crc32((unsigned char *) header
, le32_to_cpu(header
->size
));
723 header
->crc32
= cpu_to_le32(orgcrc
);
725 return crc
== le32_to_cpu(header
->crc32
);
732 * It initializes the partition entry array.
733 * Returns 1 if the checksum is valid, otherwise 0.
735 static int gpt_check_entryarr_crc(struct gpt_header
*header
,
736 struct gpt_entry
*ents
)
742 if (!header
|| !ents
)
745 entry_sz
= le32_to_cpu(header
->npartition_entries
) *
746 le32_to_cpu(header
->sizeof_partition_entry
);
751 crc
= count_crc32((unsigned char *) ents
, entry_sz
);
752 ret
= (crc
== le32_to_cpu(header
->partition_entry_array_crc32
));
757 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
760 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
762 fu
= le64_to_cpu(header
->first_usable_lba
);
763 lu
= le64_to_cpu(header
->last_usable_lba
);
765 /* check if first and last usable LBA make sense */
767 DBG(LABEL
, ul_debug("error: header last LBA is before first LBA"));
771 /* check if first and last usable LBAs with the disk's last LBA */
772 if (fu
> lastlba
|| lu
> lastlba
) {
773 DBG(LABEL
, ul_debug("error: header LBAs are after the disk's last LBA"));
777 /* the header has to be outside usable range */
778 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
779 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
780 DBG(LABEL
, ul_debug("error: header outside of usable range"));
789 /* Check if there is a valid header signature */
790 static int gpt_check_signature(struct gpt_header
*header
)
792 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
796 * Return the specified GPT Header, or NULL upon failure/invalid.
797 * Note that all tests must pass to ensure a valid header,
798 * we do not rely on only testing the signature for a valid probe.
800 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
802 struct gpt_entry
**_ents
)
804 struct gpt_header
*header
= NULL
;
805 struct gpt_entry
*ents
= NULL
;
811 header
= calloc(1, sizeof(*header
));
815 /* read and verify header */
816 if (read_lba(cxt
, lba
, header
, sizeof(struct gpt_header
)) != 0)
819 if (!gpt_check_signature(header
))
822 if (!gpt_check_header_crc(header
, NULL
))
825 /* read and verify entries */
826 ents
= gpt_read_entries(cxt
, header
);
830 if (!gpt_check_entryarr_crc(header
, ents
))
833 if (!gpt_check_lba_sanity(cxt
, header
))
836 /* valid header must be at MyLBA */
837 if (le64_to_cpu(header
->my_lba
) != lba
)
840 /* make sure header size is between 92 and sector size bytes */
841 hsz
= le32_to_cpu(header
->size
);
842 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
850 DBG(LABEL
, ul_debug("found valid GPT Header on LBA %ju", lba
));
856 DBG(LABEL
, ul_debug("read GPT Header on LBA %ju failed", lba
));
861 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
862 const char **name
, off_t
*offset
, size_t *size
)
864 struct fdisk_gpt_label
*gpt
;
879 *name
= _("GPT Header");
880 *offset
= GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
881 *size
= sizeof(struct gpt_header
);
884 *name
= _("GPT Entries");
885 gpt
= self_label(cxt
);
886 *offset
= le64_to_cpu(gpt
->pheader
->partition_entry_lba
) * cxt
->sector_size
;
887 *size
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
888 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
891 return 1; /* no more chunks */
900 * Returns the number of partitions that are in use.
902 static unsigned partitions_in_use(struct gpt_header
*header
, struct gpt_entry
*e
)
904 uint32_t i
, used
= 0;
909 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
910 if (!partition_unused(&e
[i
]))
917 * Check if a partition is too big for the disk (sectors).
918 * Returns the faulting partition number, otherwise 0.
920 static uint32_t partition_check_too_big(struct gpt_header
*header
,
921 struct gpt_entry
*e
, uint64_t sectors
)
925 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
926 if (partition_unused(&e
[i
]))
928 if (gpt_partition_end(&e
[i
]) >= sectors
)
936 * Check if a partition ends before it begins
937 * Returns the faulting partition number, otherwise 0.
939 static uint32_t partition_start_after_end(struct gpt_header
*header
, struct gpt_entry
*e
)
943 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
944 if (partition_unused(&e
[i
]))
946 if (gpt_partition_start(&e
[i
]) > gpt_partition_end(&e
[i
]))
954 * Check if partition e1 overlaps with partition e2
956 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
958 uint64_t start1
= gpt_partition_start(e1
);
959 uint64_t end1
= gpt_partition_end(e1
);
960 uint64_t start2
= gpt_partition_start(e2
);
961 uint64_t end2
= gpt_partition_end(e2
);
963 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
967 * Find any paritions that overlap.
969 static uint32_t partition_check_overlaps(struct gpt_header
*header
, struct gpt_entry
*e
)
973 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++)
974 for (j
= 0; j
< i
; j
++) {
975 if (partition_unused(&e
[i
]) ||
976 partition_unused(&e
[j
]))
978 if (partition_overlap(&e
[i
], &e
[j
])) {
979 DBG(LABEL
, ul_debug("GPT partitions overlap detected [%u vs. %u]", i
, j
));
988 * Find the first available block after the starting point; returns 0 if
989 * there are no available blocks left, or error. From gdisk.
991 static uint64_t find_first_available(struct gpt_header
*header
,
992 struct gpt_entry
*e
, uint64_t start
)
995 uint32_t i
, first_moved
= 0;
1002 fu
= le64_to_cpu(header
->first_usable_lba
);
1003 lu
= le64_to_cpu(header
->last_usable_lba
);
1006 * Begin from the specified starting point or from the first usable
1007 * LBA, whichever is greater...
1009 first
= start
< fu
? fu
: start
;
1012 * Now search through all partitions; if first is within an
1013 * existing partition, move it to the next sector after that
1014 * partition and repeat. If first was moved, set firstMoved
1015 * flag; repeat until firstMoved is not set, so as to catch
1016 * cases where partitions are out of sequential order....
1020 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1021 if (partition_unused(&e
[i
]))
1023 if (first
< gpt_partition_start(&e
[i
]))
1025 if (first
<= gpt_partition_end(&e
[i
])) {
1026 first
= gpt_partition_end(&e
[i
]) + 1;
1030 } while (first_moved
== 1);
1039 /* Returns last available sector in the free space pointed to by start. From gdisk. */
1040 static uint64_t find_last_free(struct gpt_header
*header
,
1041 struct gpt_entry
*e
, uint64_t start
)
1044 uint64_t nearest_start
;
1049 nearest_start
= le64_to_cpu(header
->last_usable_lba
);
1051 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1052 uint64_t ps
= gpt_partition_start(&e
[i
]);
1054 if (nearest_start
> ps
&& ps
> start
)
1055 nearest_start
= ps
- 1;
1058 return nearest_start
;
1061 /* Returns the last free sector on the disk. From gdisk. */
1062 static uint64_t find_last_free_sector(struct gpt_header
*header
,
1063 struct gpt_entry
*e
)
1065 uint32_t i
, last_moved
;
1071 /* start by assuming the last usable LBA is available */
1072 last
= le64_to_cpu(header
->last_usable_lba
);
1075 for (i
= 0; i
< le32_to_cpu(header
->npartition_entries
); i
++) {
1076 if ((last
>= gpt_partition_start(&e
[i
])) &&
1077 (last
<= gpt_partition_end(&e
[i
]))) {
1078 last
= gpt_partition_start(&e
[i
]) - 1;
1082 } while (last_moved
== 1);
1088 * Finds the first available sector in the largest block of unallocated
1089 * space on the disk. Returns 0 if there are no available blocks left.
1092 static uint64_t find_first_in_largest(struct gpt_header
*header
, struct gpt_entry
*e
)
1094 uint64_t start
= 0, first_sect
, last_sect
;
1095 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1101 first_sect
= find_first_available(header
, e
, start
);
1102 if (first_sect
!= 0) {
1103 last_sect
= find_last_free(header
, e
, first_sect
);
1104 segment_size
= last_sect
- first_sect
+ 1;
1106 if (segment_size
> selected_size
) {
1107 selected_size
= segment_size
;
1108 selected_segment
= first_sect
;
1110 start
= last_sect
+ 1;
1112 } while (first_sect
!= 0);
1115 return selected_segment
;
1119 * Find the total number of free sectors, the number of segments in which
1120 * they reside, and the size of the largest of those segments. From gdisk.
1122 static uint64_t get_free_sectors(struct fdisk_context
*cxt
, struct gpt_header
*header
,
1123 struct gpt_entry
*e
, uint32_t *nsegments
,
1124 uint64_t *largest_segment
)
1127 uint64_t first_sect
, last_sect
;
1128 uint64_t largest_seg
= 0, segment_sz
;
1129 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1131 if (!cxt
->total_sectors
)
1135 first_sect
= find_first_available(header
, e
, start
);
1137 last_sect
= find_last_free(header
, e
, first_sect
);
1138 segment_sz
= last_sect
- first_sect
+ 1;
1140 if (segment_sz
> largest_seg
)
1141 largest_seg
= segment_sz
;
1142 totfound
+= segment_sz
;
1144 start
= last_sect
+ 1;
1146 } while (first_sect
);
1151 if (largest_segment
)
1152 *largest_segment
= largest_seg
;
1157 static int gpt_probe_label(struct fdisk_context
*cxt
)
1160 struct fdisk_gpt_label
*gpt
;
1164 assert(fdisk_is_disklabel(cxt
, GPT
));
1166 gpt
= self_label(cxt
);
1168 /* TODO: it would be nice to support scenario when GPT headers are OK,
1169 * but PMBR is corrupt */
1170 mbr_type
= valid_pmbr(cxt
);
1174 DBG(LABEL
, ul_debug("found a %s MBR", mbr_type
== GPT_MBR_PROTECTIVE
?
1175 "protective" : "hybrid"));
1177 /* primary header */
1178 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1182 /* primary OK, try backup from alternative LBA */
1183 gpt
->bheader
= gpt_read_header(cxt
,
1184 le64_to_cpu(gpt
->pheader
->alternative_lba
),
1187 /* primary corrupted -- try last LBA */
1188 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), &gpt
->ents
);
1190 if (!gpt
->pheader
&& !gpt
->bheader
)
1193 /* primary OK, backup corrupted -- recovery */
1194 if (gpt
->pheader
&& !gpt
->bheader
) {
1195 fdisk_warnx(cxt
, _("The backup GPT table is corrupt, but the "
1196 "primary appears OK, so that will be used."));
1197 gpt
->bheader
= gpt_copy_header(cxt
, gpt
->pheader
);
1200 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1202 /* primary corrupted, backup OK -- recovery */
1203 } else if (!gpt
->pheader
&& gpt
->bheader
) {
1204 fdisk_warnx(cxt
, _("The primary GPT table is corrupt, but the "
1205 "backup appears OK, so that will be used."));
1206 gpt
->pheader
= gpt_copy_header(cxt
, gpt
->bheader
);
1209 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1212 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1213 cxt
->label
->nparts_cur
= partitions_in_use(gpt
->pheader
, gpt
->ents
);
1216 DBG(LABEL
, ul_debug("GPT probe failed"));
1217 gpt_deinit(cxt
->label
);
1222 * Stolen from libblkid - can be removed once partition semantics
1223 * are added to the fdisk API.
1225 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1229 size_t i
, j
, len
= count
;
1231 dest
= calloc(1, count
);
1235 for (j
= i
= 0; i
+ 2 <= count
; i
+= 2) {
1236 /* always little endian */
1237 c
= (src
[i
+1] << 8) | src
[i
];
1241 } else if (c
< 0x80) {
1244 dest
[j
++] = (uint8_t) c
;
1245 } else if (c
< 0x800) {
1248 dest
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
1249 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1253 dest
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
1254 dest
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
1255 dest
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
1263 static int gpt_entry_attrs_to_string(struct gpt_entry
*e
, char **res
)
1265 unsigned int n
, count
= 0;
1274 attrs
= le64_to_cpu(e
->attrs
);
1276 return 0; /* no attributes at all */
1278 bits
= (char *) &attrs
;
1280 /* Note that sizeof() is correct here, we need separators between
1281 * the strings so also count \0 is correct */
1282 *res
= calloc(1, sizeof(GPT_ATTRSTR_NOBLOCK
) +
1283 sizeof(GPT_ATTRSTR_REQ
) +
1284 sizeof(GPT_ATTRSTR_LEGACY
) +
1285 sizeof("GUID:") + (GPT_ATTRBIT_GUID_COUNT
* 3));
1290 if (isset(bits
, GPT_ATTRBIT_REQ
)) {
1291 memcpy(p
, GPT_ATTRSTR_REQ
, (l
= sizeof(GPT_ATTRSTR_REQ
)));
1294 if (isset(bits
, GPT_ATTRBIT_NOBLOCK
)) {
1297 memcpy(p
, GPT_ATTRSTR_NOBLOCK
, (l
= sizeof(GPT_ATTRSTR_NOBLOCK
)));
1300 if (isset(bits
, GPT_ATTRBIT_LEGACY
)) {
1303 memcpy(p
, GPT_ATTRSTR_LEGACY
, (l
= sizeof(GPT_ATTRSTR_LEGACY
)));
1307 for (n
= GPT_ATTRBIT_GUID_FIRST
;
1308 n
< GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
; n
++) {
1310 if (!isset(bits
, n
))
1315 p
+= sprintf(p
, "GUID:%u", n
);
1317 p
+= sprintf(p
, ",%u", n
);
1324 static int gpt_get_partition(struct fdisk_context
*cxt
, size_t n
,
1325 struct fdisk_partition
*pa
)
1327 struct fdisk_gpt_label
*gpt
;
1328 struct gpt_entry
*e
;
1334 assert(fdisk_is_disklabel(cxt
, GPT
));
1336 gpt
= self_label(cxt
);
1338 if ((uint32_t) n
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
1341 gpt
= self_label(cxt
);
1344 pa
->used
= !partition_unused(e
) || gpt_partition_start(e
);
1348 pa
->start
= gpt_partition_start(e
);
1349 pa
->end
= gpt_partition_end(e
);
1350 pa
->size
= gpt_partition_size(e
);
1351 pa
->type
= gpt_partition_parttype(cxt
, e
);
1353 if (guid_to_string(&e
->partition_guid
, u_str
)) {
1354 pa
->uuid
= strdup(u_str
);
1362 rc
= gpt_entry_attrs_to_string(e
, &pa
->attrs
);
1366 pa
->name
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1369 fdisk_reset_partition(pa
);
1374 * List label partitions.
1376 static int gpt_list_disklabel(struct fdisk_context
*cxt
)
1380 assert(fdisk_is_disklabel(cxt
, GPT
));
1382 if (fdisk_context_display_details(cxt
)) {
1383 struct gpt_header
*h
= self_label(cxt
)->pheader
;
1385 fdisk_colon(cxt
, _("First LBA: %ju"), h
->first_usable_lba
);
1386 fdisk_colon(cxt
, _("Last LBA: %ju"), h
->last_usable_lba
);
1387 fdisk_colon(cxt
, _("Alternative LBA: %ju"), h
->alternative_lba
);
1388 fdisk_colon(cxt
, _("Partitions entries LBA: %ju"), h
->partition_entry_lba
);
1389 fdisk_colon(cxt
, _("Allocated partition entries: %u"), h
->npartition_entries
);
1397 * Returns 0 on success, or corresponding error otherwise.
1399 static int gpt_write_partitions(struct fdisk_context
*cxt
,
1400 struct gpt_header
*header
, struct gpt_entry
*ents
)
1402 off_t offset
= le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
;
1403 uint32_t nparts
= le32_to_cpu(header
->npartition_entries
);
1404 uint32_t totwrite
= nparts
* le32_to_cpu(header
->sizeof_partition_entry
);
1407 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1410 rc
= write(cxt
->dev_fd
, ents
, totwrite
);
1411 if (rc
> 0 && totwrite
== (uint32_t) rc
)
1418 * Write a GPT header to a specified LBA
1419 * Returns 0 on success, or corresponding error otherwise.
1421 static int gpt_write_header(struct fdisk_context
*cxt
,
1422 struct gpt_header
*header
, uint64_t lba
)
1424 off_t offset
= lba
* cxt
->sector_size
;
1426 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1428 if (cxt
->sector_size
==
1429 (size_t) write(cxt
->dev_fd
, header
, cxt
->sector_size
))
1436 * Write the protective MBR.
1437 * Returns 0 on success, or corresponding error otherwise.
1439 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
1442 struct gpt_legacy_mbr
*pmbr
= NULL
;
1445 assert(cxt
->firstsector
);
1447 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
1449 /* zero out the legacy partitions */
1450 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
1452 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
1453 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
1454 pmbr
->partition_record
[0].start_sector
= 1;
1455 pmbr
->partition_record
[0].end_head
= 0xFE;
1456 pmbr
->partition_record
[0].end_sector
= 0xFF;
1457 pmbr
->partition_record
[0].end_track
= 0xFF;
1458 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
1461 * Set size_in_lba to the size of the disk minus one. If the size of the disk
1462 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
1464 if (cxt
->total_sectors
- 1 > 0xFFFFFFFFULL
)
1465 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
1467 pmbr
->partition_record
[0].size_in_lba
=
1468 cpu_to_le32(cxt
->total_sectors
- 1UL);
1470 offset
= GPT_PMBR_LBA
* cxt
->sector_size
;
1471 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1474 /* pMBR covers the first sector (LBA) of the disk */
1475 if (write_all(cxt
->dev_fd
, pmbr
, cxt
->sector_size
))
1483 * Writes in-memory GPT and pMBR data to disk.
1484 * Returns 0 if successful write, otherwise, a corresponding error.
1485 * Any indication of error will abort the operation.
1487 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
1489 struct fdisk_gpt_label
*gpt
;
1494 assert(fdisk_is_disklabel(cxt
, GPT
));
1496 gpt
= self_label(cxt
);
1497 mbr_type
= valid_pmbr(cxt
);
1499 /* check that disk is big enough to handle the backup header */
1500 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
1503 /* check that the backup header is properly placed */
1504 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1)
1505 /* TODO: correct this (with user authorization) and write */
1508 if (partition_check_overlaps(gpt
->pheader
, gpt
->ents
))
1511 /* recompute CRCs for both headers */
1512 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1513 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1516 * UEFI requires writing in this specific order:
1517 * 1) backup partition tables
1518 * 2) backup GPT header
1519 * 3) primary partition tables
1520 * 4) primary GPT header
1523 * If any write fails, we abort the rest.
1525 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
1527 if (gpt_write_header(cxt
, gpt
->bheader
,
1528 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
1530 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
1532 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
1535 if (mbr_type
== GPT_MBR_HYBRID
)
1536 fdisk_warnx(cxt
, _("The device contains hybrid MBR -- writing GPT only. "
1537 "You have to sync the MBR manually."));
1538 else if (gpt_write_pmbr(cxt
) != 0)
1541 DBG(LABEL
, ul_debug("GPT write success"));
1544 DBG(LABEL
, ul_debug("GPT write failed: incorrect input"));
1548 DBG(LABEL
, ul_debug("GPT write failed: %m"));
1553 * Verify data integrity and report any found problems for:
1554 * - primary and backup header validations
1555 * - paritition validations
1557 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
1561 struct fdisk_gpt_label
*gpt
;
1565 assert(fdisk_is_disklabel(cxt
, GPT
));
1567 gpt
= self_label(cxt
);
1569 if (!gpt
|| !gpt
->bheader
) {
1571 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
1574 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
1576 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
1578 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
1580 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
1583 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
1585 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
1588 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
1590 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
1592 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
1594 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
1597 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
1599 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
1601 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
1603 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
1606 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
1608 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
1612 * if the GPT is the primary table, check the alternateLBA
1613 * to see if it is a valid GPT
1615 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
1616 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
1618 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
1621 ptnum
= partition_check_overlaps(gpt
->pheader
, gpt
->ents
);
1624 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
1628 ptnum
= partition_check_too_big(gpt
->pheader
, gpt
->ents
, cxt
->total_sectors
);
1631 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
1635 ptnum
= partition_start_after_end(gpt
->pheader
, gpt
->ents
);
1638 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
1642 if (!nerror
) { /* yay :-) */
1643 uint32_t nsegments
= 0;
1644 uint64_t free_sectors
= 0, largest_segment
= 0;
1647 fdisk_info(cxt
, _("No errors detected."));
1648 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
1649 fdisk_info(cxt
, _("Using %u out of %d partitions."),
1650 partitions_in_use(gpt
->pheader
, gpt
->ents
),
1651 le32_to_cpu(gpt
->pheader
->npartition_entries
));
1653 free_sectors
= get_free_sectors(cxt
, gpt
->pheader
, gpt
->ents
,
1654 &nsegments
, &largest_segment
);
1655 if (largest_segment
)
1656 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
| SIZE_SUFFIX_3LETTER
,
1657 largest_segment
* cxt
->sector_size
);
1660 P_("A total of %ju free sectors is available in %u segment.",
1661 "A total of %ju free sectors is available in %u segments "
1662 "(the largest is %s).", nsegments
),
1663 free_sectors
, nsegments
, strsz
);
1668 P_("%d error detected.", "%d errors detected.", nerror
),
1674 /* Delete a single GPT partition, specified by partnum. */
1675 static int gpt_delete_partition(struct fdisk_context
*cxt
,
1678 struct fdisk_gpt_label
*gpt
;
1682 assert(fdisk_is_disklabel(cxt
, GPT
));
1684 gpt
= self_label(cxt
);
1686 if (partnum
>= cxt
->label
->nparts_max
1687 || partition_unused(&gpt
->ents
[partnum
]))
1690 /* hasta la vista, baby! */
1691 memset(&gpt
->ents
[partnum
], 0, sizeof(struct gpt_entry
));
1692 if (!partition_unused(&gpt
->ents
[partnum
]))
1695 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1696 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1697 cxt
->label
->nparts_cur
--;
1698 fdisk_label_set_changed(cxt
->label
, 1);
1704 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
1707 DBG(LABEL
, gpt_debug_uuid("new type", &(e
->type
)));
1711 * Create a new GPT partition entry, specified by partnum, and with a range
1712 * of fsect to lsenct sectors, of type t.
1713 * Returns 0 on success, or negative upon failure.
1715 static int gpt_create_new_partition(struct fdisk_context
*cxt
,
1716 size_t partnum
, uint64_t fsect
, uint64_t lsect
,
1717 struct gpt_guid
*type
,
1718 struct gpt_entry
*entries
)
1720 struct gpt_entry
*e
= NULL
;
1721 struct fdisk_gpt_label
*gpt
;
1725 assert(fdisk_is_disklabel(cxt
, GPT
));
1727 DBG(LABEL
, ul_debug("GPT new partition: partno=%zu, start=%ju, end=%ju",
1728 partnum
, fsect
, lsect
));
1730 gpt
= self_label(cxt
);
1732 if (fsect
> lsect
|| partnum
>= cxt
->label
->nparts_max
)
1735 e
= calloc(1, sizeof(*e
));
1738 e
->lba_end
= cpu_to_le64(lsect
);
1739 e
->lba_start
= cpu_to_le64(fsect
);
1741 gpt_entry_set_type(e
, type
);
1744 * Any time a new partition entry is created a new GUID must be
1745 * generated for that partition, and every partition is guaranteed
1746 * to have a unique GUID.
1748 uuid_generate_random((unsigned char *) &e
->partition_guid
);
1749 swap_efi_guid(&e
->partition_guid
);
1751 memcpy(&entries
[partnum
], e
, sizeof(*e
));
1753 gpt_recompute_crc(gpt
->pheader
, entries
);
1754 gpt_recompute_crc(gpt
->bheader
, entries
);
1760 /* Performs logical checks to add a new partition entry */
1761 static int gpt_add_partition(
1762 struct fdisk_context
*cxt
,
1763 struct fdisk_partition
*pa
)
1765 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
1766 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
1767 uint64_t dflt_f
, dflt_l
; /* largest segment (default) */
1768 struct gpt_guid
typeid;
1769 struct fdisk_gpt_label
*gpt
;
1770 struct gpt_header
*pheader
;
1771 struct gpt_entry
*ents
;
1772 struct fdisk_ask
*ask
= NULL
;
1778 assert(fdisk_is_disklabel(cxt
, GPT
));
1780 gpt
= self_label(cxt
);
1781 pheader
= gpt
->pheader
;
1784 rc
= fdisk_partition_next_partno(pa
, cxt
, &partnum
);
1786 DBG(LABEL
, ul_debug("GPT failed to get next partno"));
1789 if (!partition_unused(&ents
[partnum
])) {
1790 fdisk_warnx(cxt
, _("Partition %zu is already defined. "
1791 "Delete it before re-adding it."), partnum
+1);
1794 if (le32_to_cpu(pheader
->npartition_entries
) ==
1795 partitions_in_use(pheader
, ents
)) {
1796 fdisk_warnx(cxt
, _("All partitions are already in use."));
1799 if (!get_free_sectors(cxt
, pheader
, ents
, NULL
, NULL
)) {
1800 fdisk_warnx(cxt
, _("No free sectors available."));
1804 string_to_guid(pa
&& pa
->type
&& pa
->type
->typestr
?
1806 GPT_DEFAULT_ENTRY_TYPE
, &typeid);
1808 disk_f
= find_first_available(pheader
, ents
, 0);
1809 disk_l
= find_last_free_sector(pheader
, ents
);
1811 /* the default is the largest free space */
1812 dflt_f
= find_first_in_largest(pheader
, ents
);
1813 dflt_l
= find_last_free(pheader
, ents
, dflt_f
);
1815 /* align the default in range <dflt_f,dflt_l>*/
1816 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
1819 if (pa
&& pa
->start
) {
1820 if (pa
->start
!= find_first_available(pheader
, ents
, pa
->start
)) {
1821 fdisk_warnx(cxt
, _("Sector %ju already used."), pa
->start
);
1825 } else if (pa
&& pa
->start_follow_default
) {
1831 ask
= fdisk_new_ask();
1833 fdisk_reset_ask(ask
);
1836 fdisk_ask_set_query(ask
, _("First sector"));
1837 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
1838 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
1839 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
1840 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
1842 rc
= fdisk_do_ask(cxt
, ask
);
1846 user_f
= fdisk_ask_number_get_result(ask
);
1847 if (user_f
!= find_first_available(pheader
, ents
, user_f
)) {
1848 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
1857 dflt_l
= find_last_free(pheader
, ents
, user_f
);
1859 if (pa
&& pa
->size
) {
1860 user_l
= user_f
+ pa
->size
;
1861 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1863 /* no space for anything useful, use all space
1864 if (user_l + (cxt->grain / cxt->sector_size) > dflt_l)
1868 } else if (pa
&& pa
->end_follow_default
) {
1873 ask
= fdisk_new_ask();
1875 fdisk_reset_ask(ask
);
1877 fdisk_ask_set_query(ask
, _("Last sector, +sectors or +size{K,M,G,T,P}"));
1878 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
1879 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
1880 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
1881 fdisk_ask_number_set_high(ask
, dflt_l
); /* maximal */
1882 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
1883 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
1885 rc
= fdisk_do_ask(cxt
, ask
);
1889 user_l
= fdisk_ask_number_get_result(ask
);
1890 if (fdisk_ask_number_is_relative(ask
)) {
1891 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
) - 1;
1893 /* no space for anything useful, use all space
1894 if (user_l + (cxt->grain / cxt->sector_size) > dflt_l)
1897 } if (user_l
> user_f
&& user_l
<= disk_l
)
1902 if ((rc
= gpt_create_new_partition(cxt
, partnum
,
1903 user_f
, user_l
, &typeid, ents
) != 0)) {
1904 fdisk_warnx(cxt
, _("Could not create partition %zu"), partnum
+ 1);
1907 struct fdisk_parttype
*t
;
1909 cxt
->label
->nparts_cur
++;
1910 fdisk_label_set_changed(cxt
->label
, 1);
1912 t
= gpt_partition_parttype(cxt
, &ents
[partnum
]);
1913 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
1914 fdisk_free_parttype(t
);
1919 fdisk_free_ask(ask
);
1924 * Create a new GPT disklabel - destroys any previous data.
1926 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
1931 struct fdisk_gpt_label
*gpt
;
1935 assert(fdisk_is_disklabel(cxt
, GPT
));
1937 gpt
= self_label(cxt
);
1939 /* label private stuff has to be empty, see gpt_deinit() */
1940 assert(gpt
->pheader
== NULL
);
1941 assert(gpt
->bheader
== NULL
);
1944 * When no header, entries or pmbr is set, we're probably
1945 * dealing with a new, empty disk - so always allocate memory
1946 * to deal with the data structures whatever the case is.
1948 rc
= gpt_mknew_pmbr(cxt
);
1953 gpt
->pheader
= calloc(1, sizeof(*gpt
->pheader
));
1954 if (!gpt
->pheader
) {
1958 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
1962 /* backup ("copy" primary) */
1963 gpt
->bheader
= calloc(1, sizeof(*gpt
->bheader
));
1964 if (!gpt
->bheader
) {
1968 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
1969 last_lba(cxt
), gpt
->pheader
);
1973 esz
= le32_to_cpu(gpt
->pheader
->npartition_entries
) *
1974 le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
1975 gpt
->ents
= calloc(1, esz
);
1980 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1981 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1983 cxt
->label
->nparts_max
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
1984 cxt
->label
->nparts_cur
= 0;
1986 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
1987 fdisk_label_set_changed(cxt
->label
, 1);
1988 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
1989 _("Created a new GPT disklabel (GUID: %s)."), str
);
1994 static int gpt_get_disklabel_id(struct fdisk_context
*cxt
, char **id
)
1996 struct fdisk_gpt_label
*gpt
;
2002 assert(fdisk_is_disklabel(cxt
, GPT
));
2004 gpt
= self_label(cxt
);
2005 guid_to_string(&gpt
->pheader
->disk_guid
, str
);
2013 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
)
2015 struct fdisk_gpt_label
*gpt
;
2016 struct gpt_guid uuid
;
2017 char *str
, *old
, *new;
2022 assert(fdisk_is_disklabel(cxt
, GPT
));
2024 gpt
= self_label(cxt
);
2025 if (fdisk_ask_string(cxt
,
2026 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str
))
2029 rc
= string_to_guid(str
, &uuid
);
2033 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2037 gpt_get_disklabel_id(cxt
, &old
);
2039 gpt
->pheader
->disk_guid
= uuid
;
2040 gpt
->bheader
->disk_guid
= uuid
;
2042 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2043 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2045 gpt_get_disklabel_id(cxt
, &new);
2047 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2048 _("Disk identifier changed from %s to %s."), old
, new);
2052 fdisk_label_set_changed(cxt
->label
, 1);
2056 static int gpt_set_partition_type(
2057 struct fdisk_context
*cxt
,
2059 struct fdisk_parttype
*t
)
2061 struct gpt_guid uuid
;
2062 struct fdisk_gpt_label
*gpt
;
2066 assert(fdisk_is_disklabel(cxt
, GPT
));
2068 gpt
= self_label(cxt
);
2069 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
)
2070 || !t
|| !t
->typestr
|| string_to_guid(t
->typestr
, &uuid
) != 0)
2073 gpt_entry_set_type(&gpt
->ents
[i
], &uuid
);
2074 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2075 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2077 fdisk_label_set_changed(cxt
->label
, 1);
2081 static int gpt_part_is_used(struct fdisk_context
*cxt
, size_t i
)
2083 struct fdisk_gpt_label
*gpt
;
2084 struct gpt_entry
*e
;
2088 assert(fdisk_is_disklabel(cxt
, GPT
));
2090 gpt
= self_label(cxt
);
2092 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2096 return !partition_unused(e
) || gpt_partition_start(e
);
2099 int fdisk_gpt_partition_set_uuid(struct fdisk_context
*cxt
, size_t i
)
2101 struct fdisk_gpt_label
*gpt
;
2102 struct gpt_entry
*e
;
2103 struct gpt_guid uuid
;
2104 char *str
, new_u
[37], old_u
[37];
2109 assert(fdisk_is_disklabel(cxt
, GPT
));
2111 DBG(LABEL
, ul_debug("UUID change requested partno=%zu", i
));
2113 gpt
= self_label(cxt
);
2115 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2118 if (fdisk_ask_string(cxt
,
2119 _("New UUID (in 8-4-4-4-12 format)"), &str
))
2122 rc
= string_to_guid(str
, &uuid
);
2126 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2132 guid_to_string(&e
->partition_guid
, old_u
);
2133 guid_to_string(&uuid
, new_u
);
2135 e
->partition_guid
= uuid
;
2136 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2137 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2138 fdisk_label_set_changed(cxt
->label
, 1);
2140 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2141 _("Partition UUID changed from %s to %s."),
2146 int fdisk_gpt_partition_set_name(struct fdisk_context
*cxt
, size_t i
)
2148 struct fdisk_gpt_label
*gpt
;
2149 struct gpt_entry
*e
;
2150 char *str
, *old
, name
[GPT_PART_NAME_LEN
] = { 0 };
2155 assert(fdisk_is_disklabel(cxt
, GPT
));
2157 DBG(LABEL
, ul_debug("NAME change requested partno=%zu", i
));
2159 gpt
= self_label(cxt
);
2161 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2164 if (fdisk_ask_string(cxt
, _("New name"), &str
))
2168 old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
2172 if (sz
> GPT_PART_NAME_LEN
)
2173 sz
= GPT_PART_NAME_LEN
;
2174 memcpy(name
, str
, sz
);
2177 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
2178 e
->name
[i
] = cpu_to_le16((uint16_t) name
[i
]);
2180 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2181 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2183 fdisk_label_set_changed(cxt
->label
, 1);
2185 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2186 _("Partition name changed from '%s' to '%.*s'."),
2187 old
, (int) GPT_PART_NAME_LEN
, str
);
2194 int fdisk_gpt_is_hybrid(struct fdisk_context
*cxt
)
2197 return valid_pmbr(cxt
) == GPT_MBR_HYBRID
;
2200 static int gpt_toggle_partition_flag(
2201 struct fdisk_context
*cxt
,
2205 struct fdisk_gpt_label
*gpt
;
2206 uint64_t attrs
, tmp
;
2208 const char *name
= NULL
;
2213 assert(fdisk_is_disklabel(cxt
, GPT
));
2215 DBG(LABEL
, ul_debug("GPT entry attribute change requested partno=%zu", i
));
2216 gpt
= self_label(cxt
);
2218 if ((uint32_t) i
>= le32_to_cpu(gpt
->pheader
->npartition_entries
))
2221 attrs
= le64_to_cpu(gpt
->ents
[i
].attrs
);
2222 bits
= (char *) &attrs
;
2225 case GPT_FLAG_REQUIRED
:
2226 bit
= GPT_ATTRBIT_REQ
;
2227 name
= GPT_ATTRSTR_REQ
;
2229 case GPT_FLAG_NOBLOCK
:
2230 bit
= GPT_ATTRBIT_NOBLOCK
;
2231 name
= GPT_ATTRSTR_NOBLOCK
;
2233 case GPT_FLAG_LEGACYBOOT
:
2234 bit
= GPT_ATTRBIT_LEGACY
;
2235 name
= GPT_ATTRSTR_LEGACY
;
2237 case GPT_FLAG_GUIDSPECIFIC
:
2238 rc
= fdisk_ask_number(cxt
, 48, 48, 63, _("Enter GUID specific bit"), &tmp
);
2248 if (!isset(bits
, bit
))
2253 gpt
->ents
[i
].attrs
= cpu_to_le64(attrs
);
2255 if (flag
== GPT_FLAG_GUIDSPECIFIC
)
2256 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2258 _("The GUID specific bit %d on partition %zu is enabled now.") :
2259 _("The GUID specific bit %d on partition %zu is disabled now."),
2262 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
2264 _("The %s flag on partition %zu is enabled now.") :
2265 _("The %s flag on partition %zu is disabled now."),
2268 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2269 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2270 fdisk_label_set_changed(cxt
->label
, 1);
2274 static int gpt_reset_alignment(struct fdisk_context
*cxt
)
2276 struct fdisk_gpt_label
*gpt
;
2277 struct gpt_header
*h
;
2281 assert(fdisk_is_disklabel(cxt
, GPT
));
2283 gpt
= self_label(cxt
);
2284 h
= gpt
? gpt
->pheader
: NULL
;
2287 /* always follow existing table */
2288 cxt
->first_lba
= h
->first_usable_lba
;
2289 cxt
->last_lba
= h
->last_usable_lba
;
2291 /* estimate ranges for GPT */
2292 uint64_t first
, last
;
2294 count_first_last_lba(cxt
, &first
, &last
);
2296 if (cxt
->first_lba
< first
)
2297 cxt
->first_lba
= first
;
2298 if (cxt
->last_lba
> last
)
2299 cxt
->last_lba
= last
;
2305 * Deinitialize fdisk-specific variables
2307 static void gpt_deinit(struct fdisk_label
*lb
)
2309 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
2319 gpt
->pheader
= NULL
;
2320 gpt
->bheader
= NULL
;
2323 static const struct fdisk_label_operations gpt_operations
=
2325 .probe
= gpt_probe_label
,
2326 .write
= gpt_write_disklabel
,
2327 .verify
= gpt_verify_disklabel
,
2328 .create
= gpt_create_disklabel
,
2329 .list
= gpt_list_disklabel
,
2330 .locate
= gpt_locate_disklabel
,
2331 .get_id
= gpt_get_disklabel_id
,
2332 .set_id
= gpt_set_disklabel_id
,
2334 .get_part
= gpt_get_partition
,
2335 .add_part
= gpt_add_partition
,
2337 .part_delete
= gpt_delete_partition
,
2339 .part_is_used
= gpt_part_is_used
,
2340 .part_set_type
= gpt_set_partition_type
,
2341 .part_toggle_flag
= gpt_toggle_partition_flag
,
2343 .deinit
= gpt_deinit
,
2345 .reset_alignment
= gpt_reset_alignment
2348 static const struct fdisk_column gpt_columns
[] =
2351 { FDISK_COL_DEVICE
, N_("Device"), 10, 0 },
2352 { FDISK_COL_START
, N_("Start"), 5, TT_FL_RIGHT
},
2353 { FDISK_COL_END
, N_("End"), 5, TT_FL_RIGHT
},
2354 { FDISK_COL_SECTORS
, N_("Sectors"), 5, TT_FL_RIGHT
},
2355 { FDISK_COL_CYLINDERS
, N_("Cylinders"), 5, TT_FL_RIGHT
},
2356 { FDISK_COL_SIZE
, N_("Size"), 5, TT_FL_RIGHT
, FDISK_COLFL_EYECANDY
},
2357 { FDISK_COL_TYPE
, N_("Type"), 0.1, TT_FL_TRUNC
, FDISK_COLFL_EYECANDY
},
2359 { FDISK_COL_TYPEID
, N_("Type-UUID"), 36, 0, FDISK_COLFL_DETAIL
},
2360 { FDISK_COL_UUID
, N_("UUID"), 36, 0, FDISK_COLFL_DETAIL
},
2361 { FDISK_COL_NAME
, N_("Name"), 0.2, TT_FL_TRUNC
, FDISK_COLFL_DETAIL
},
2362 { FDISK_COL_ATTR
, N_("Attrs"), 0, 0, FDISK_COLFL_DETAIL
}
2366 * allocates GPT in-memory stuff
2368 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
)
2370 struct fdisk_label
*lb
;
2371 struct fdisk_gpt_label
*gpt
;
2375 gpt
= calloc(1, sizeof(*gpt
));
2379 /* initialize generic part of the driver */
2380 lb
= (struct fdisk_label
*) gpt
;
2382 lb
->id
= FDISK_DISKLABEL_GPT
;
2383 lb
->op
= &gpt_operations
;
2384 lb
->parttypes
= gpt_parttypes
;
2385 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);
2387 lb
->columns
= gpt_columns
;
2388 lb
->ncolumns
= ARRAY_SIZE(gpt_columns
);