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.
15 #include <sys/utsname.h>
16 #include <sys/types.h>
36 * @short_description: specific functionality
39 #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */
40 #define GPT_HEADER_REVISION_V1_02 0x00010200
41 #define GPT_HEADER_REVISION_V1_00 0x00010000
42 #define GPT_HEADER_REVISION_V0_99 0x00009900
43 #define GPT_HEADER_MINSZ 92 /* bytes */
45 #define GPT_PMBR_LBA 0
46 #define GPT_MBR_PROTECTIVE 1
47 #define GPT_MBR_HYBRID 2
49 #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001ULL
51 #define EFI_PMBR_OSTYPE 0xEE
52 #define MSDOS_MBR_SIGNATURE 0xAA55
53 #define GPT_PART_NAME_LEN (72 / sizeof(uint16_t))
54 #define GPT_NPARTITIONS ((size_t) FDISK_GPT_NPARTITIONS_DEFAULT)
56 /* Globally unique identifier */
60 uint16_t time_hi_and_version
;
62 uint8_t clock_seq_low
;
67 /* only checking that the GUID is 0 is enough to verify an empty partition. */
68 #define GPT_UNUSED_ENTRY_GUID \
69 ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
70 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
72 /* Linux native partition type */
73 #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
81 GPT_ATTRBIT_NOBLOCK
= 1,
82 GPT_ATTRBIT_LEGACY
= 2,
84 /* GUID specific (range 48..64)*/
85 GPT_ATTRBIT_GUID_FIRST
= 48,
86 GPT_ATTRBIT_GUID_COUNT
= 16
89 #define GPT_ATTRSTR_REQ "RequiredPartition"
90 #define GPT_ATTRSTR_REQ_TYPO "RequiredPartiton"
91 #define GPT_ATTRSTR_NOBLOCK "NoBlockIOProtocol"
92 #define GPT_ATTRSTR_LEGACY "LegacyBIOSBootable"
94 /* The GPT Partition entry array contains an array of GPT entries. */
96 struct gpt_guid type
; /* purpose and type of the partition */
97 struct gpt_guid partition_guid
;
101 uint16_t name
[GPT_PART_NAME_LEN
];
102 } __attribute__ ((packed
));
106 uint64_t signature
; /* header identification */
107 uint32_t revision
; /* header version */
108 uint32_t size
; /* in bytes */
109 uint32_t crc32
; /* header CRC checksum */
110 uint32_t reserved1
; /* must be 0 */
111 uint64_t my_lba
; /* LBA of block that contains this struct (LBA 1) */
112 uint64_t alternative_lba
; /* backup GPT header */
113 uint64_t first_usable_lba
; /* first usable logical block for partitions */
114 uint64_t last_usable_lba
; /* last usable logical block for partitions */
115 struct gpt_guid disk_guid
; /* unique disk identifier */
116 uint64_t partition_entry_lba
; /* LBA of start of partition entries array */
117 uint32_t npartition_entries
; /* total partition entries - normally 128 */
118 uint32_t sizeof_partition_entry
; /* bytes for each GUID pt */
119 uint32_t partition_entry_array_crc32
; /* partition CRC checksum */
120 uint8_t reserved2
[512 - 92]; /* must all be 0 */
121 } __attribute__ ((packed
));
124 uint8_t boot_indicator
; /* unused by EFI, set to 0x80 for bootable */
125 uint8_t start_head
; /* unused by EFI, pt start in CHS */
126 uint8_t start_sector
; /* unused by EFI, pt start in CHS */
128 uint8_t os_type
; /* EFI and legacy non-EFI OS types */
129 uint8_t end_head
; /* unused by EFI, pt end in CHS */
130 uint8_t end_sector
; /* unused by EFI, pt end in CHS */
131 uint8_t end_track
; /* unused by EFI, pt end in CHS */
132 uint32_t starting_lba
; /* used by EFI - start addr of the on disk pt */
133 uint32_t size_in_lba
; /* used by EFI - size of pt in LBA */
134 } __attribute__ ((packed
));
136 /* Protected MBR and legacy MBR share same structure */
137 struct gpt_legacy_mbr
{
138 uint8_t boot_code
[440];
139 uint32_t unique_mbr_signature
;
141 struct gpt_record partition_record
[4];
143 } __attribute__ ((packed
));
147 * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
149 #define DEF_GUID(_u, _n) \
155 static const struct fdisk_parttype gpt_parttypes
[] =
157 #include "pt-gpt-partnames.h"
160 static const struct fdisk_shortcut gpt_parttype_cuts
[] =
162 { .shortcut
= "L", .alias
= "linux", .data
= "0FC63DAF-8483-4772-8E79-3D69D8477DE4" }, /* Linux */
163 { .shortcut
= "S", .alias
= "swap", .data
= "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" }, /* Swap */
164 { .shortcut
= "H", .alias
= "home", .data
= "933AC7E1-2EB4-4F13-B844-0E14E2AEF915" }, /* Home */
165 { .shortcut
= "U", .alias
= "uefi", .data
= "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" }, /* UEFI system */
166 { .shortcut
= "R", .alias
= "raid", .data
= "A19D880F-05FC-4D3B-A006-743F0F84911E" }, /* Linux RAID */
167 { .shortcut
= "V", .alias
= "lvm", .data
= "E6D6D379-F507-44C2-A23C-238F2A3DF928" }, /* LVM */
168 { .shortcut
= "X", .alias
= "xbootldr", .data
= "BC13C2FF-59E6-4262-A352-B275FD6F7172" }, /* Linux extended boot */
171 #define alignment_required(_x) ((_x)->grain != (_x)->sector_size)
173 /* gpt_entry macros */
174 #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start)
175 #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end)
178 * in-memory fdisk GPT stuff
180 struct fdisk_gpt_label
{
181 struct fdisk_label head
; /* generic part */
183 /* gpt specific part */
184 struct gpt_header
*pheader
; /* primary header */
185 struct gpt_header
*bheader
; /* backup header */
187 unsigned char *ents
; /* entries (partitions) */
189 unsigned int no_relocate
:1, /* do not fix backup location */
193 static void gpt_deinit(struct fdisk_label
*lb
);
195 static inline struct fdisk_gpt_label
*self_label(struct fdisk_context
*cxt
)
197 return (struct fdisk_gpt_label
*) cxt
->label
;
201 * Returns the partition length, or 0 if end is before beginning.
203 static uint64_t gpt_partition_size(const struct gpt_entry
*e
)
205 uint64_t start
= gpt_partition_start(e
);
206 uint64_t end
= gpt_partition_end(e
);
208 return start
> end
? 0 : end
- start
+ 1ULL;
211 /* prints UUID in the real byte order! */
212 static void gpt_debug_uuid(const char *mesg
, struct gpt_guid
*guid
)
214 const unsigned char *uuid
= (unsigned char *) guid
;
216 fprintf(stderr
, "%s: "
217 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
219 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
223 uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14],uuid
[15]);
227 * UUID is traditionally 16 byte big-endian array, except Intel EFI
228 * specification where the UUID is a structure of little-endian fields.
230 static void swap_efi_guid(struct gpt_guid
*uid
)
232 uid
->time_low
= swab32(uid
->time_low
);
233 uid
->time_mid
= swab16(uid
->time_mid
);
234 uid
->time_hi_and_version
= swab16(uid
->time_hi_and_version
);
237 static int string_to_guid(const char *in
, struct gpt_guid
*guid
)
239 if (uuid_parse(in
, (unsigned char *) guid
)) { /* BE */
240 DBG(GPT
, ul_debug("failed to parse GUID: %s", in
));
243 swap_efi_guid(guid
); /* LE */
247 static char *guid_to_string(const struct gpt_guid
*guid
, char *out
)
249 struct gpt_guid u
= *guid
; /* LE */
251 swap_efi_guid(&u
); /* BE */
252 uuid_unparse_upper((unsigned char *) &u
, out
);
257 static struct fdisk_parttype
*gpt_partition_parttype(
258 struct fdisk_context
*cxt
,
259 const struct gpt_entry
*e
)
261 struct fdisk_parttype
*t
;
262 char str
[UUID_STR_LEN
];
263 struct gpt_guid guid
= e
->type
;
265 guid_to_string(&guid
, str
);
266 t
= fdisk_label_get_parttype_from_string(cxt
->label
, str
);
267 return t
? : fdisk_new_unknown_parttype(0, str
);
270 static void gpt_entry_set_type(struct gpt_entry
*e
, struct gpt_guid
*uuid
)
273 DBG(GPT
, gpt_debug_uuid("new type", uuid
));
276 static int gpt_entry_set_name(struct gpt_entry
*e
, char *str
)
278 uint16_t name
[GPT_PART_NAME_LEN
] = { 0 };
280 uint8_t *in
= (uint8_t *) str
;
282 for (i
= 0; *in
&& i
< GPT_PART_NAME_LEN
; in
++) {
286 } else if ((*in
& 0xE0) == 0xC0) {
288 name
[i
] = (uint16_t)(*in
& 0x1F) << (mblen
*6);
289 } else if ((*in
& 0xF0) == 0xE0) {
291 name
[i
] = (uint16_t)(*in
& 0x0F) << (mblen
*6);
293 /* broken UTF-8 or code point greater than U+FFFF */
297 /* incomplete UTF-8 sequence */
298 if ((*in
& 0xC0) != 0x80)
301 name
[i
] |= (uint16_t)(*in
& 0x3F) << (--mblen
*6);
303 /* check for code points reserved for surrogate pairs*/
304 if ((name
[i
] & 0xF800) == 0xD800)
311 for (i
= 0; i
< GPT_PART_NAME_LEN
; i
++)
312 e
->name
[i
] = cpu_to_le16(name
[i
]);
314 return (int)((char *) in
- str
);
317 static int gpt_entry_set_uuid(struct gpt_entry
*e
, char *str
)
319 struct gpt_guid uuid
;
322 rc
= string_to_guid(str
, &uuid
);
326 e
->partition_guid
= uuid
;
330 static inline int gpt_entry_is_used(const struct gpt_entry
*e
)
332 return memcmp(&e
->type
, &GPT_UNUSED_ENTRY_GUID
,
333 sizeof(struct gpt_guid
)) != 0;
337 static const char *gpt_get_header_revstr(struct gpt_header
*header
)
342 switch (le32_to_cpu(header
->revision
)) {
343 case GPT_HEADER_REVISION_V1_02
:
345 case GPT_HEADER_REVISION_V1_00
:
347 case GPT_HEADER_REVISION_V0_99
:
357 static inline unsigned char *gpt_get_entry_ptr(struct fdisk_gpt_label
*gpt
, size_t i
)
359 return gpt
->ents
+ le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
) * i
;
362 static inline struct gpt_entry
*gpt_get_entry(struct fdisk_gpt_label
*gpt
, size_t i
)
364 return (struct gpt_entry
*) gpt_get_entry_ptr(gpt
, i
);
367 static inline struct gpt_entry
*gpt_zeroize_entry(struct fdisk_gpt_label
*gpt
, size_t i
)
369 return (struct gpt_entry
*) memset(gpt_get_entry_ptr(gpt
, i
),
370 0, le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
));
373 /* Use to access array of entries, for() loops, etc. But don't use when
374 * you directly do something with GPT header, then use uint32_t.
376 static inline size_t gpt_get_nentries(struct fdisk_gpt_label
*gpt
)
378 return (size_t) le32_to_cpu(gpt
->pheader
->npartition_entries
);
381 /* calculate size of entries array in bytes for specified number of entries */
382 static inline int gpt_calculate_sizeof_entries(
383 struct gpt_header
*hdr
,
384 uint32_t nents
, size_t *sz
)
386 uint32_t esz
= hdr
? le32_to_cpu(hdr
->sizeof_partition_entry
) :
387 sizeof(struct gpt_entry
);
389 if (nents
== 0 || esz
== 0 || SIZE_MAX
/esz
< nents
) {
390 DBG(GPT
, ul_debug("entries array size check failed"));
394 *sz
= (size_t) nents
* esz
;
398 /* calculate size of entries array in sectors for specified number of entries */
399 static inline int gpt_calculate_sectorsof_entries(
400 struct gpt_header
*hdr
,
401 uint32_t nents
, uint64_t *sz
,
402 struct fdisk_context
*cxt
)
405 int rc
= gpt_calculate_sizeof_entries(hdr
, nents
, &esz
); /* in bytes */
408 *sz
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
412 /* calculate alternative (backup) entries array offset from primary header */
413 static inline int gpt_calculate_alternative_entries_lba(
414 struct gpt_header
*hdr
,
417 struct fdisk_context
*cxt
)
420 int rc
= gpt_calculate_sectorsof_entries(hdr
, nents
, &esects
, cxt
);
424 if (cxt
->total_sectors
< 1ULL + esects
)
427 *sz
= cxt
->total_sectors
- 1ULL - esects
;
431 static inline int gpt_calculate_last_lba(
432 struct gpt_header
*hdr
,
435 struct fdisk_context
*cxt
)
438 int rc
= gpt_calculate_sectorsof_entries(hdr
, nents
, &esects
, cxt
);
442 if (cxt
->total_sectors
< 2ULL + esects
)
445 *sz
= cxt
->total_sectors
- 2ULL - esects
;
449 static inline int gpt_calculate_first_lba(
450 struct gpt_header
*hdr
,
453 struct fdisk_context
*cxt
)
456 int rc
= gpt_calculate_sectorsof_entries(hdr
, nents
, &esects
, cxt
);
463 /* the current size of entries array in bytes */
464 static inline int gpt_sizeof_entries(struct gpt_header
*hdr
, size_t *sz
)
466 return gpt_calculate_sizeof_entries(hdr
, le32_to_cpu(hdr
->npartition_entries
), sz
);
469 static char *gpt_get_header_id(struct gpt_header
*header
)
471 char str
[UUID_STR_LEN
];
472 struct gpt_guid guid
= header
->disk_guid
;
474 guid_to_string(&guid
, str
);
480 * Builds a clean new valid protective MBR - will wipe out any existing data.
481 * Returns 0 on success, otherwise < 0 on error.
483 static int gpt_mknew_pmbr(struct fdisk_context
*cxt
)
485 struct gpt_legacy_mbr
*pmbr
= NULL
;
488 if (!cxt
|| !cxt
->firstsector
)
491 if (fdisk_has_protected_bootbits(cxt
))
492 rc
= fdisk_init_firstsector_buffer(cxt
, 0, MBR_PT_BOOTBITS_SIZE
);
494 rc
= fdisk_init_firstsector_buffer(cxt
, 0, 0);
498 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
499 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
501 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
502 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
503 pmbr
->partition_record
[0].start_sector
= 2;
504 pmbr
->partition_record
[0].end_head
= 0xFF;
505 pmbr
->partition_record
[0].end_sector
= 0xFF;
506 pmbr
->partition_record
[0].end_track
= 0xFF;
507 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
508 pmbr
->partition_record
[0].size_in_lba
=
509 cpu_to_le32((uint32_t) min( cxt
->total_sectors
- 1ULL, 0xFFFFFFFFULL
) );
515 /* Move backup header to the end of the device */
516 static int gpt_fix_alternative_lba(struct fdisk_context
*cxt
, struct fdisk_gpt_label
*gpt
)
518 struct gpt_header
*p
, *b
;
519 uint64_t x
= 0, orig
;
526 p
= gpt
->pheader
; /* primary */
527 b
= gpt
->bheader
; /* backup */
529 nents
= le32_to_cpu(p
->npartition_entries
);
530 orig
= le64_to_cpu(p
->alternative_lba
);
532 /* reference from primary to backup */
533 p
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1ULL);
535 /* reference from backup to primary */
536 b
->alternative_lba
= p
->my_lba
;
537 b
->my_lba
= p
->alternative_lba
;
539 /* fix backup partitions array address */
540 rc
= gpt_calculate_alternative_entries_lba(p
, nents
, &x
, cxt
);
544 b
->partition_entry_lba
= cpu_to_le64(x
);
546 /* update last usable LBA */
547 rc
= gpt_calculate_last_lba(p
, nents
, &x
, cxt
);
551 p
->last_usable_lba
= cpu_to_le64(x
);
552 b
->last_usable_lba
= cpu_to_le64(x
);
554 DBG(GPT
, ul_debug("Alternative-LBA updated from %"PRIu64
" to %"PRIu64
,
555 orig
, le64_to_cpu(p
->alternative_lba
)));
558 DBG(GPT
, ul_debug("failed to fix alternative-LBA [rc=%d]", rc
));
562 static uint64_t gpt_calculate_minimal_size(struct fdisk_context
*cxt
, struct fdisk_gpt_label
*gpt
)
565 uint64_t x
= 0, total
= 0;
566 struct gpt_header
*hdr
;
570 assert(gpt
->pheader
);
575 /* LBA behind the last partition */
576 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
577 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
579 if (gpt_entry_is_used(e
)) {
580 uint64_t end
= gpt_partition_end(e
);
587 /* the current last LBA usable for partitions */
588 gpt_calculate_last_lba(hdr
, le32_to_cpu(hdr
->npartition_entries
), &x
, cxt
);
590 /* size of all stuff at the end of the device */
591 total
+= cxt
->total_sectors
- x
;
593 DBG(GPT
, ul_debug("minimal device is %"PRIu64
, total
));
597 static int gpt_possible_minimize(struct fdisk_context
*cxt
, struct fdisk_gpt_label
*gpt
)
599 struct gpt_header
*hdr
= gpt
->pheader
;
600 uint64_t total
= gpt_calculate_minimal_size(cxt
, gpt
);
602 return le64_to_cpu(hdr
->alternative_lba
) > (total
- 1ULL);
605 /* move backup header behind the last partition */
606 static int gpt_minimize_alternative_lba(struct fdisk_context
*cxt
, struct fdisk_gpt_label
*gpt
)
608 uint64_t total
= gpt_calculate_minimal_size(cxt
, gpt
);
609 uint64_t orig
= cxt
->total_sectors
;
612 /* Let's temporary change size of the device to recalculate backup header */
613 cxt
->total_sectors
= total
;
614 rc
= gpt_fix_alternative_lba(cxt
, gpt
);
618 cxt
->total_sectors
= orig
;
619 fdisk_label_set_changed(cxt
->label
, 1);
623 /* some universal differences between the headers */
624 static void gpt_mknew_header_common(struct fdisk_context
*cxt
,
625 struct gpt_header
*header
, uint64_t lba
)
630 header
->my_lba
= cpu_to_le64(lba
);
632 if (lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
) {
634 header
->alternative_lba
= cpu_to_le64(cxt
->total_sectors
- 1ULL);
635 header
->partition_entry_lba
= cpu_to_le64(2ULL);
640 gpt_calculate_alternative_entries_lba(header
,
641 le32_to_cpu(header
->npartition_entries
), &x
, cxt
);
643 header
->alternative_lba
= cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA
);
644 header
->partition_entry_lba
= cpu_to_le64(x
);
649 * Builds a new GPT header (at sector lba) from a backup header2.
650 * If building a primary header, then backup is the secondary, and vice versa.
652 * Always pass a new (zeroized) header to build upon as we don't
653 * explicitly zero-set some values such as CRCs and reserved.
655 * Returns 0 on success, otherwise < 0 on error.
657 static int gpt_mknew_header_from_bkp(struct fdisk_context
*cxt
,
658 struct gpt_header
*header
,
660 struct gpt_header
*header2
)
662 if (!cxt
|| !header
|| !header2
)
665 header
->signature
= header2
->signature
;
666 header
->revision
= header2
->revision
;
667 header
->size
= header2
->size
;
668 header
->npartition_entries
= header2
->npartition_entries
;
669 header
->sizeof_partition_entry
= header2
->sizeof_partition_entry
;
670 header
->first_usable_lba
= header2
->first_usable_lba
;
671 header
->last_usable_lba
= header2
->last_usable_lba
;
673 memcpy(&header
->disk_guid
,
674 &header2
->disk_guid
, sizeof(header2
->disk_guid
));
675 gpt_mknew_header_common(cxt
, header
, lba
);
680 static struct gpt_header
*gpt_copy_header(struct fdisk_context
*cxt
,
681 struct gpt_header
*src
)
683 struct gpt_header
*res
;
688 assert(cxt
->sector_size
>= sizeof(struct gpt_header
));
690 res
= calloc(1, cxt
->sector_size
);
692 fdisk_warn(cxt
, _("failed to allocate GPT header"));
696 res
->my_lba
= src
->alternative_lba
;
697 res
->alternative_lba
= src
->my_lba
;
699 res
->signature
= src
->signature
;
700 res
->revision
= src
->revision
;
701 res
->size
= src
->size
;
702 res
->npartition_entries
= src
->npartition_entries
;
703 res
->sizeof_partition_entry
= src
->sizeof_partition_entry
;
704 res
->first_usable_lba
= src
->first_usable_lba
;
705 res
->last_usable_lba
= src
->last_usable_lba
;
707 memcpy(&res
->disk_guid
, &src
->disk_guid
, sizeof(src
->disk_guid
));
710 if (res
->my_lba
== GPT_PRIMARY_PARTITION_TABLE_LBA
)
711 res
->partition_entry_lba
= cpu_to_le64(2ULL);
713 uint64_t esz
= (uint64_t) le32_to_cpu(src
->npartition_entries
) * sizeof(struct gpt_entry
);
714 uint64_t esects
= (esz
+ cxt
->sector_size
- 1) / cxt
->sector_size
;
716 res
->partition_entry_lba
= cpu_to_le64(cxt
->total_sectors
- 1ULL - esects
);
722 static int get_script_u64(struct fdisk_context
*cxt
, uint64_t *num
, const char *name
)
734 str
= fdisk_script_get_header(cxt
->script
, name
);
738 rc
= ul_parse_size(str
, (uintmax_t *) num
, &pwr
);
742 *num
/= cxt
->sector_size
;
746 static int count_first_last_lba(struct fdisk_context
*cxt
,
747 uint64_t *first
, uint64_t *last
,
751 uint64_t flba
= 0, llba
= 0;
752 uint64_t nents
= GPT_NPARTITIONS
;
760 /* Get the table length from the script, if given */
762 rc
= get_script_u64(cxt
, &nents
, "table-length");
764 nents
= GPT_NPARTITIONS
; /* undefined by script */
769 /* The table length was not changed by the script, compute it. */
771 /* If the device is not large enough reduce this number of
772 * partitions and try to recalculate it again, until we get
773 * something useful or return error.
775 for (; nents
> 0; nents
--) {
776 rc
= gpt_calculate_last_lba(NULL
, nents
, &llba
, cxt
);
778 rc
= gpt_calculate_first_lba(NULL
, nents
, &flba
, cxt
);
793 rc
= get_script_u64(cxt
, first
, "first-lba");
797 DBG(GPT
, ul_debug("FirstLBA: script=%"PRIu64
", uefi=%"PRIu64
", topology=%ju.",
798 *first
, flba
, (uintmax_t)cxt
->first_lba
));
800 if (rc
== 0 && (*first
< flba
|| *first
> llba
)) {
801 fdisk_warnx(cxt
, _("First LBA specified by script is out of range."));
805 rc
= get_script_u64(cxt
, last
, "last-lba");
809 DBG(GPT
, ul_debug("LastLBA: script=%"PRIu64
", uefi=%"PRIu64
", topology=%ju.",
810 *last
, llba
, (uintmax_t)cxt
->last_lba
));
812 if (rc
== 0 && (*last
> llba
|| *last
< flba
)) {
813 fdisk_warnx(cxt
, _("Last LBA specified by script is out of range."));
821 /* default by topology */
823 *first
= flba
< cxt
->first_lba
&&
824 cxt
->first_lba
< *last
? cxt
->first_lba
: flba
;
829 * Builds a clean new GPT header (currently under revision 1.0).
831 * Always pass a new (zeroized) header to build upon as we don't
832 * explicitly zero-set some values such as CRCs and reserved.
834 * Returns 0 on success, otherwise < 0 on error.
836 static int gpt_mknew_header(struct fdisk_context
*cxt
,
837 struct gpt_header
*header
, uint64_t lba
)
839 uint64_t first
, last
;
846 header
->signature
= cpu_to_le64(GPT_HEADER_SIGNATURE
);
847 header
->revision
= cpu_to_le32(GPT_HEADER_REVISION_V1_00
);
849 /* According to EFI standard it's valid to count all the first
850 * sector into header size, but some tools may have a problem
851 * to accept it, so use the header without the zeroed area.
852 * This does not have any impact to CRC, etc. --kzak Jan-2015
854 header
->size
= cpu_to_le32(sizeof(struct gpt_header
)
855 - sizeof(header
->reserved2
));
857 /* Set {First,Last}LBA and number of the partitions
858 * (default is GPT_NPARTITIONS) */
859 rc
= count_first_last_lba(cxt
, &first
, &last
, &nents
);
863 header
->npartition_entries
= cpu_to_le32(nents
);
864 header
->sizeof_partition_entry
= cpu_to_le32(sizeof(struct gpt_entry
));
866 header
->first_usable_lba
= cpu_to_le64(first
);
867 header
->last_usable_lba
= cpu_to_le64(last
);
869 gpt_mknew_header_common(cxt
, header
, lba
);
872 const char *id
= fdisk_script_get_header(cxt
->script
, "label-id");
873 struct gpt_guid guid
= header
->disk_guid
;
874 if (id
&& string_to_guid(id
, &guid
) == 0)
876 header
->disk_guid
= guid
;
880 struct gpt_guid guid
;
882 uuid_generate_random((unsigned char *) &guid
);
883 swap_efi_guid(&guid
);
884 header
->disk_guid
= guid
;
890 * Checks if there is a valid protective MBR partition table.
891 * Returns 0 if it is invalid or failure. Otherwise, return
892 * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depending on the detection.
894 static int valid_pmbr(struct fdisk_context
*cxt
)
896 int i
, part
= 0, ret
= 0; /* invalid by default */
897 struct gpt_legacy_mbr
*pmbr
= NULL
;
899 if (!cxt
->firstsector
)
902 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
904 if (le16_to_cpu(pmbr
->signature
) != MSDOS_MBR_SIGNATURE
)
907 /* seems like a valid MBR was found, check DOS primary partitions */
908 for (i
= 0; i
< 4; i
++) {
909 if (pmbr
->partition_record
[i
].os_type
== EFI_PMBR_OSTYPE
) {
911 * Ok, we at least know that there's a protective MBR,
912 * now check if there are other partition types for
916 ret
= GPT_MBR_PROTECTIVE
;
921 if (ret
!= GPT_MBR_PROTECTIVE
)
925 for (i
= 0 ; i
< 4; i
++) {
926 if ((pmbr
->partition_record
[i
].os_type
!= EFI_PMBR_OSTYPE
) &&
927 (pmbr
->partition_record
[i
].os_type
!= 0x00)) {
928 ret
= GPT_MBR_HYBRID
;
933 /* LBA of the GPT partition header */
934 if (pmbr
->partition_record
[part
].starting_lba
!=
935 cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA
))
939 * Protective MBRs take up the lesser of the whole disk
940 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
941 * Some partitioning programs, nonetheless, choose to set
942 * the size to the maximum 32-bit limitation, disregarding
945 * Hybrid MBRs do not necessarily comply with this.
947 * Consider a bad value here to be a warning to support dd-ing
948 * an image from a smaller disk to a bigger disk.
950 if (ret
== GPT_MBR_PROTECTIVE
) {
951 uint64_t sz_lba
= (uint64_t) le32_to_cpu(pmbr
->partition_record
[part
].size_in_lba
);
952 if (sz_lba
!= cxt
->total_sectors
- 1ULL && sz_lba
!= 0xFFFFFFFFULL
) {
954 fdisk_warnx(cxt
, _("GPT PMBR size mismatch (%"PRIu64
" != %"PRIu64
") "
955 "will be corrected by write."),
956 sz_lba
, cxt
->total_sectors
- (uint64_t) 1);
958 /* Note that gpt_write_pmbr() overwrites PMBR, but we want to keep it valid already
959 * in memory too to disable warnings when valid_pmbr() called next time */
960 pmbr
->partition_record
[part
].size_in_lba
=
961 cpu_to_le32((uint32_t) min( cxt
->total_sectors
- 1ULL, 0xFFFFFFFFULL
) );
962 fdisk_label_set_changed(cxt
->label
, 1);
966 DBG(GPT
, ul_debug("PMBR type: %s",
967 ret
== GPT_MBR_PROTECTIVE
? "protective" :
968 ret
== GPT_MBR_HYBRID
? "hybrid" : "???" ));
972 static uint64_t last_lba(struct fdisk_context
*cxt
)
975 uint64_t sectors
= 0;
977 memset(&s
, 0, sizeof(s
));
978 if (fstat(cxt
->dev_fd
, &s
) == -1) {
979 fdisk_warn(cxt
, _("gpt: stat() failed"));
983 if (S_ISBLK(s
.st_mode
))
984 sectors
= cxt
->total_sectors
- 1ULL;
985 else if (S_ISREG(s
.st_mode
))
986 sectors
= ((uint64_t) s
.st_size
/
987 (uint64_t) cxt
->sector_size
) - 1ULL;
989 fdisk_warnx(cxt
, _("gpt: cannot handle files with mode %o"), s
.st_mode
);
991 DBG(GPT
, ul_debug("last LBA: %"PRIu64
"", sectors
));
995 static ssize_t
read_lba(struct fdisk_context
*cxt
, uint64_t lba
,
996 void *buffer
, const size_t bytes
)
998 off_t offset
= lba
* cxt
->sector_size
;
1000 if (lseek(cxt
->dev_fd
, offset
, SEEK_SET
) == (off_t
) -1)
1002 return (size_t)read(cxt
->dev_fd
, buffer
, bytes
) != bytes
;
1006 /* Returns the GPT entry array */
1007 static unsigned char *gpt_read_entries(struct fdisk_context
*cxt
,
1008 struct gpt_header
*header
)
1013 unsigned char *ret
= NULL
;
1019 if (gpt_sizeof_entries(header
, &sz
))
1022 ret
= calloc(1, sz
);
1026 offset
= (off_t
) le64_to_cpu(header
->partition_entry_lba
) *
1029 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
1032 ssz
= read(cxt
->dev_fd
, ret
, sz
);
1033 if (ssz
< 0 || (size_t) ssz
!= sz
)
1043 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
,
1044 size_t ex_off
, size_t ex_len
)
1046 return (ul_crc32_exclude_offset(~0L, buf
, len
, ex_off
, ex_len
, 0) ^ ~0L);
1049 static inline uint32_t gpt_header_count_crc32(struct gpt_header
*header
)
1051 return count_crc32((unsigned char *) header
, /* buffer */
1052 le32_to_cpu(header
->size
), /* size of buffer */
1053 offsetof(struct gpt_header
, crc32
), /* exclude */
1054 sizeof(header
->crc32
)); /* size of excluded area */
1057 static inline uint32_t gpt_entryarr_count_crc32(struct gpt_header
*header
, unsigned char *ents
)
1061 if (gpt_sizeof_entries(header
, &arysz
))
1064 return count_crc32(ents
, arysz
, 0, 0);
1069 * Recompute header and partition array 32bit CRC checksums.
1070 * This function does not fail - if there's corruption, then it
1071 * will be reported when checksumming it again (ie: probing or verify).
1073 static void gpt_recompute_crc(struct gpt_header
*header
, unsigned char *ents
)
1078 header
->partition_entry_array_crc32
=
1079 cpu_to_le32( gpt_entryarr_count_crc32(header
, ents
) );
1081 header
->crc32
= cpu_to_le32( gpt_header_count_crc32(header
) );
1085 * Compute the 32bit CRC checksum of the partition table header.
1086 * Returns 1 if it is valid, otherwise 0.
1088 static int gpt_check_header_crc(struct gpt_header
*header
, unsigned char *ents
)
1090 uint32_t orgcrc
= le32_to_cpu(header
->crc32
),
1091 crc
= gpt_header_count_crc32(header
);
1097 * If we have checksum mismatch it may be due to stale data, like a
1098 * partition being added or deleted. Recompute the CRC again and make
1099 * sure this is not the case.
1102 gpt_recompute_crc(header
, ents
);
1103 return gpt_header_count_crc32(header
) == orgcrc
;
1110 * It initializes the partition entry array.
1111 * Returns 1 if the checksum is valid, otherwise 0.
1113 static int gpt_check_entryarr_crc(struct gpt_header
*header
, unsigned char *ents
)
1115 if (!header
|| !ents
)
1118 return gpt_entryarr_count_crc32(header
, ents
) ==
1119 le32_to_cpu(header
->partition_entry_array_crc32
);
1122 static int gpt_check_lba_sanity(struct fdisk_context
*cxt
, struct gpt_header
*header
)
1125 uint64_t lu
, fu
, lastlba
= last_lba(cxt
);
1127 fu
= le64_to_cpu(header
->first_usable_lba
);
1128 lu
= le64_to_cpu(header
->last_usable_lba
);
1130 /* check if first and last usable LBA make sense */
1132 DBG(GPT
, ul_debug("error: header last LBA is before first LBA"));
1136 /* check if first and last usable LBAs with the disk's last LBA */
1137 if (fu
> lastlba
|| lu
> lastlba
) {
1138 DBG(GPT
, ul_debug("error: header LBAs are after the disk's last LBA (%ju..%ju)",
1139 (uintmax_t) fu
, (uintmax_t) lu
));
1143 /* the header has to be outside usable range */
1144 if (fu
< GPT_PRIMARY_PARTITION_TABLE_LBA
&&
1145 GPT_PRIMARY_PARTITION_TABLE_LBA
< lu
) {
1146 DBG(GPT
, ul_debug("error: header outside of usable range"));
1155 /* Check if there is a valid header signature */
1156 static int gpt_check_signature(struct gpt_header
*header
)
1158 return header
->signature
== cpu_to_le64(GPT_HEADER_SIGNATURE
);
1162 * Return the specified GPT Header, or NULL upon failure/invalid.
1163 * Note that all tests must pass to ensure a valid header,
1164 * we do not rely on only testing the signature for a valid probe.
1166 static struct gpt_header
*gpt_read_header(struct fdisk_context
*cxt
,
1168 unsigned char **_ents
)
1170 struct gpt_header
*header
= NULL
;
1171 unsigned char *ents
= NULL
;
1177 /* always allocate all sector, the area after GPT header
1178 * has to be fill by zeros */
1179 assert(cxt
->sector_size
>= sizeof(struct gpt_header
));
1181 header
= calloc(1, cxt
->sector_size
);
1185 /* read and verify header */
1186 if (read_lba(cxt
, lba
, header
, cxt
->sector_size
) != 0)
1189 if (!gpt_check_signature(header
))
1192 /* make sure header size is between 92 and sector size bytes */
1193 hsz
= le32_to_cpu(header
->size
);
1194 if (hsz
< GPT_HEADER_MINSZ
|| hsz
> cxt
->sector_size
)
1197 if (!gpt_check_header_crc(header
, NULL
))
1200 /* read and verify entries */
1201 ents
= gpt_read_entries(cxt
, header
);
1205 if (!gpt_check_entryarr_crc(header
, ents
))
1208 if (!gpt_check_lba_sanity(cxt
, header
))
1211 /* valid header must be at MyLBA */
1212 if (le64_to_cpu(header
->my_lba
) != lba
)
1220 DBG(GPT
, ul_debug("found valid header on LBA %"PRIu64
"", lba
));
1226 DBG(GPT
, ul_debug("read header on LBA %"PRIu64
" failed", lba
));
1231 static int gpt_locate_disklabel(struct fdisk_context
*cxt
, int n
,
1232 const char **name
, uint64_t *offset
, size_t *size
)
1234 struct fdisk_gpt_label
*gpt
;
1249 *name
= _("GPT Header");
1250 *offset
= (uint64_t) GPT_PRIMARY_PARTITION_TABLE_LBA
* cxt
->sector_size
;
1251 *size
= sizeof(struct gpt_header
);
1254 *name
= _("GPT Entries");
1255 gpt
= self_label(cxt
);
1256 *offset
= (uint64_t) le64_to_cpu(gpt
->pheader
->partition_entry_lba
) *
1258 return gpt_sizeof_entries(gpt
->pheader
, size
);
1260 *name
= _("GPT Backup Entries");
1261 gpt
= self_label(cxt
);
1262 *offset
= (uint64_t) le64_to_cpu(gpt
->bheader
->partition_entry_lba
) *
1264 return gpt_sizeof_entries(gpt
->bheader
, size
);
1266 *name
= _("GPT Backup Header");
1267 gpt
= self_label(cxt
);
1268 *offset
= (uint64_t) le64_to_cpu(gpt
->pheader
->alternative_lba
) * cxt
->sector_size
;
1269 *size
= sizeof(struct gpt_header
);
1272 return 1; /* no more chunks */
1278 static int gpt_get_disklabel_item(struct fdisk_context
*cxt
, struct fdisk_labelitem
*item
)
1280 struct gpt_header
*h
;
1286 assert(fdisk_is_label(cxt
, GPT
));
1288 h
= self_label(cxt
)->pheader
;
1291 case GPT_LABELITEM_ID
:
1292 item
->name
= _("Disk identifier");
1294 item
->data
.str
= gpt_get_header_id(h
);
1295 if (!item
->data
.str
)
1298 case GPT_LABELITEM_FIRSTLBA
:
1299 item
->name
= _("First usable LBA");
1301 item
->data
.num64
= le64_to_cpu(h
->first_usable_lba
);
1303 case GPT_LABELITEM_LASTLBA
:
1304 item
->name
= _("Last usable LBA");
1306 item
->data
.num64
= le64_to_cpu(h
->last_usable_lba
);
1308 case GPT_LABELITEM_ALTLBA
:
1309 /* TRANSLATORS: The LBA (Logical Block Address) of the backup GPT header. */
1310 item
->name
= _("Alternative LBA");
1312 item
->data
.num64
= le64_to_cpu(h
->alternative_lba
);
1314 case GPT_LABELITEM_ENTRIESLBA
:
1315 /* TRANSLATORS: The start of the array of partition entries. */
1316 item
->name
= _("Partition entries starting LBA");
1318 item
->data
.num64
= le64_to_cpu(h
->partition_entry_lba
);
1320 case GPT_LABELITEM_ENTRIESLASTLBA
:
1321 /* TRANSLATORS: The end of the array of partition entries. */
1322 item
->name
= _("Partition entries ending LBA");
1324 gpt_calculate_sectorsof_entries(h
,
1325 le32_to_cpu(h
->npartition_entries
), &x
, cxt
);
1326 item
->data
.num64
= le64_to_cpu(h
->partition_entry_lba
) + x
- 1;
1328 case GPT_LABELITEM_ENTRIESALLOC
:
1329 item
->name
= _("Allocated partition entries");
1331 item
->data
.num64
= le32_to_cpu(h
->npartition_entries
);
1334 if (item
->id
< __FDISK_NLABELITEMS
)
1335 rc
= 1; /* unsupported generic item */
1337 rc
= 2; /* out of range */
1345 * Returns the number of partitions that are in use.
1347 static size_t partitions_in_use(struct fdisk_gpt_label
*gpt
)
1352 assert(gpt
->pheader
);
1355 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
1356 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
1358 if (gpt_entry_is_used(e
))
1366 * Check if a partition is too big for the disk (sectors).
1367 * Returns the faulting partition number, otherwise 0.
1369 static uint32_t check_too_big_partitions(struct fdisk_gpt_label
*gpt
, uint64_t sectors
)
1374 assert(gpt
->pheader
);
1377 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
1378 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
1380 if (!gpt_entry_is_used(e
))
1382 if (gpt_partition_end(e
) >= sectors
)
1390 * Check if a partition ends before it begins
1391 * Returns the faulting partition number, otherwise 0.
1393 static uint32_t check_start_after_end_partitions(struct fdisk_gpt_label
*gpt
)
1398 assert(gpt
->pheader
);
1401 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
1402 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
1404 if (!gpt_entry_is_used(e
))
1406 if (gpt_partition_start(e
) > gpt_partition_end(e
))
1414 * Check if partition e1 overlaps with partition e2.
1416 static inline int partition_overlap(struct gpt_entry
*e1
, struct gpt_entry
*e2
)
1418 uint64_t start1
= gpt_partition_start(e1
);
1419 uint64_t end1
= gpt_partition_end(e1
);
1420 uint64_t start2
= gpt_partition_start(e2
);
1421 uint64_t end2
= gpt_partition_end(e2
);
1423 return (start1
&& start2
&& (start1
<= end2
) != (end1
< start2
));
1427 * Find any partitions that overlap.
1429 static uint32_t check_overlap_partitions(struct fdisk_gpt_label
*gpt
)
1434 assert(gpt
->pheader
);
1437 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++)
1438 for (j
= 0; j
< i
; j
++) {
1439 struct gpt_entry
*ei
= gpt_get_entry(gpt
, i
);
1440 struct gpt_entry
*ej
= gpt_get_entry(gpt
, j
);
1442 if (!gpt_entry_is_used(ei
) || !gpt_entry_is_used(ej
))
1444 if (partition_overlap(ei
, ej
)) {
1445 DBG(GPT
, ul_debug("partitions overlap detected [%zu vs. %zu]", i
, j
));
1454 * Find the first available block after the starting point; returns 0 if
1455 * there are no available blocks left, or error. From gdisk.
1457 static uint64_t find_first_available(struct fdisk_gpt_label
*gpt
, uint64_t start
)
1459 int first_moved
= 0;
1464 assert(gpt
->pheader
);
1467 fu
= le64_to_cpu(gpt
->pheader
->first_usable_lba
);
1468 lu
= le64_to_cpu(gpt
->pheader
->last_usable_lba
);
1471 * Begin from the specified starting point or from the first usable
1472 * LBA, whichever is greater...
1474 first
= start
< fu
? fu
: start
;
1477 * Now search through all partitions; if first is within an
1478 * existing partition, move it to the next sector after that
1479 * partition and repeat. If first was moved, set firstMoved
1480 * flag; repeat until firstMoved is not set, so as to catch
1481 * cases where partitions are out of sequential order....
1487 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
1488 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
1490 if (!gpt_entry_is_used(e
))
1492 if (first
< gpt_partition_start(e
))
1494 if (first
<= gpt_partition_end(e
)) {
1495 first
= gpt_partition_end(e
) + 1;
1499 } while (first_moved
== 1);
1508 /* Returns last available sector in the free space pointed to by start. From gdisk. */
1509 static uint64_t find_last_free(struct fdisk_gpt_label
*gpt
, uint64_t start
)
1512 uint64_t nearest_start
;
1515 assert(gpt
->pheader
);
1518 nearest_start
= le64_to_cpu(gpt
->pheader
->last_usable_lba
);
1520 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
1521 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
1522 uint64_t ps
= gpt_partition_start(e
);
1524 if (nearest_start
> ps
&& ps
> start
)
1525 nearest_start
= ps
- 1ULL;
1528 return nearest_start
;
1531 /* Returns the last free sector on the disk. From gdisk. */
1532 static uint64_t find_last_free_sector(struct fdisk_gpt_label
*gpt
)
1538 assert(gpt
->pheader
);
1541 /* start by assuming the last usable LBA is available */
1542 last
= le64_to_cpu(gpt
->pheader
->last_usable_lba
);
1547 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
1548 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
1550 if (last
>= gpt_partition_start(e
) &&
1551 last
<= gpt_partition_end(e
)) {
1552 last
= gpt_partition_start(e
) - 1ULL;
1556 } while (last_moved
== 1);
1562 * Finds the first available sector in the largest block of unallocated
1563 * space on the disk. Returns 0 if there are no available blocks left.
1566 static uint64_t find_first_in_largest(struct fdisk_gpt_label
*gpt
)
1568 uint64_t start
= 0, first_sect
, last_sect
;
1569 uint64_t segment_size
, selected_size
= 0, selected_segment
= 0;
1572 assert(gpt
->pheader
);
1576 first_sect
= find_first_available(gpt
, start
);
1577 if (first_sect
!= 0) {
1578 last_sect
= find_last_free(gpt
, first_sect
);
1579 segment_size
= last_sect
- first_sect
+ 1ULL;
1581 if (segment_size
> selected_size
) {
1582 selected_size
= segment_size
;
1583 selected_segment
= first_sect
;
1585 start
= last_sect
+ 1ULL;
1587 } while (first_sect
!= 0);
1589 return selected_segment
;
1593 * Find the total number of free sectors, the number of segments in which
1594 * they reside, and the size of the largest of those segments. From gdisk.
1596 static uint64_t get_free_sectors(struct fdisk_context
*cxt
,
1597 struct fdisk_gpt_label
*gpt
,
1598 uint32_t *nsegments
,
1599 uint64_t *largest_segment
)
1602 uint64_t first_sect
, last_sect
;
1603 uint64_t largest_seg
= 0, segment_sz
;
1604 uint64_t totfound
= 0, start
= 0; /* starting point for each search */
1606 if (!cxt
->total_sectors
)
1610 assert(gpt
->pheader
);
1614 first_sect
= find_first_available(gpt
, start
);
1616 last_sect
= find_last_free(gpt
, first_sect
);
1617 segment_sz
= last_sect
- first_sect
+ 1;
1619 if (segment_sz
> largest_seg
)
1620 largest_seg
= segment_sz
;
1621 totfound
+= segment_sz
;
1623 start
= last_sect
+ 1ULL;
1625 } while (first_sect
);
1630 if (largest_segment
)
1631 *largest_segment
= largest_seg
;
1636 static int gpt_probe_label(struct fdisk_context
*cxt
)
1639 struct fdisk_gpt_label
*gpt
;
1643 assert(fdisk_is_label(cxt
, GPT
));
1645 gpt
= self_label(cxt
);
1647 /* TODO: it would be nice to support scenario when GPT headers are OK,
1648 * but PMBR is corrupt */
1649 mbr_type
= valid_pmbr(cxt
);
1653 /* primary header */
1654 gpt
->pheader
= gpt_read_header(cxt
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
1658 /* primary OK, try backup from alternative LBA */
1659 gpt
->bheader
= gpt_read_header(cxt
,
1660 le64_to_cpu(gpt
->pheader
->alternative_lba
),
1663 /* primary corrupted -- try last LBA */
1664 gpt
->bheader
= gpt_read_header(cxt
, last_lba(cxt
), &gpt
->ents
);
1666 if (!gpt
->pheader
&& !gpt
->bheader
)
1669 /* primary OK, backup corrupted -- recovery */
1670 if (gpt
->pheader
&& !gpt
->bheader
) {
1671 fdisk_warnx(cxt
, _("The backup GPT table is corrupt, but the "
1672 "primary appears OK, so that will be used."));
1673 gpt
->bheader
= gpt_copy_header(cxt
, gpt
->pheader
);
1676 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1677 fdisk_label_set_changed(cxt
->label
, 1);
1679 /* primary corrupted, backup OK -- recovery */
1680 } else if (!gpt
->pheader
&& gpt
->bheader
) {
1681 fdisk_warnx(cxt
, _("The primary GPT table is corrupt, but the "
1682 "backup appears OK, so that will be used."));
1683 gpt
->pheader
= gpt_copy_header(cxt
, gpt
->bheader
);
1686 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1687 fdisk_label_set_changed(cxt
->label
, 1);
1690 /* The headers make be correct, but Backup do not have to be on the end
1691 * of the device (due to device resize, etc.). Let's fix this issue. */
1692 if (gpt
->minimize
== 0 &&
1693 (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
||
1694 le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1ULL)) {
1696 if (gpt
->no_relocate
|| fdisk_is_readonly(cxt
))
1697 fdisk_warnx(cxt
, _("The backup GPT table is not on the end of the device."));
1700 fdisk_warnx(cxt
, _("The backup GPT table is not on the end of the device. "
1701 "This problem will be corrected by write."));
1703 if (gpt_fix_alternative_lba(cxt
, gpt
) != 0)
1704 fdisk_warnx(cxt
, _("Failed to recalculate backup GPT table location"));
1705 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
1706 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
1707 fdisk_label_set_changed(cxt
->label
, 1);
1711 if (gpt
->minimize
&& gpt_possible_minimize(cxt
, gpt
))
1712 fdisk_label_set_changed(cxt
->label
, 1);
1714 cxt
->label
->nparts_max
= gpt_get_nentries(gpt
);
1715 cxt
->label
->nparts_cur
= partitions_in_use(gpt
);
1718 DBG(GPT
, ul_debug("probe failed"));
1719 gpt_deinit(cxt
->label
);
1723 static char *encode_to_utf8(unsigned char *src
, size_t count
)
1725 unsigned char *dest
;
1726 size_t len
= (count
* 3 / 2) + 1;
1728 dest
= calloc(1, len
);
1732 ul_encode_to_utf8(UL_ENCODE_UTF16LE
, dest
, len
, src
, count
);
1733 return (char *) dest
;
1736 static int gpt_entry_attrs_to_string(struct gpt_entry
*e
, char **res
)
1738 unsigned int n
, count
= 0;
1749 return 0; /* no attributes at all */
1751 bits
= (char *) &attrs
;
1753 /* Note that sizeof() is correct here, we need separators between
1754 * the strings so also count \0 is correct */
1755 *res
= calloc(1, sizeof(GPT_ATTRSTR_NOBLOCK
) +
1756 sizeof(GPT_ATTRSTR_REQ
) +
1757 sizeof(GPT_ATTRSTR_LEGACY
) +
1758 sizeof("GUID:") + (GPT_ATTRBIT_GUID_COUNT
* 3));
1763 if (isset(bits
, GPT_ATTRBIT_REQ
)) {
1764 memcpy(p
, GPT_ATTRSTR_REQ
, (l
= sizeof(GPT_ATTRSTR_REQ
)));
1767 if (isset(bits
, GPT_ATTRBIT_NOBLOCK
)) {
1770 memcpy(p
, GPT_ATTRSTR_NOBLOCK
, (l
= sizeof(GPT_ATTRSTR_NOBLOCK
)));
1773 if (isset(bits
, GPT_ATTRBIT_LEGACY
)) {
1776 memcpy(p
, GPT_ATTRSTR_LEGACY
, (l
= sizeof(GPT_ATTRSTR_LEGACY
)));
1780 for (n
= GPT_ATTRBIT_GUID_FIRST
;
1781 n
< GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
; n
++) {
1783 if (!isset(bits
, n
))
1788 p
+= sprintf(p
, "GUID:%u", n
);
1790 p
+= sprintf(p
, ",%u", n
);
1797 static int gpt_entry_attrs_from_string(
1798 struct fdisk_context
*cxt
,
1799 struct gpt_entry
*e
,
1802 const char *p
= str
;
1809 DBG(GPT
, ul_debug("parsing string attributes '%s'", p
));
1811 bits
= (char *) &attrs
;
1816 while (isblank(*p
)) p
++;
1820 DBG(GPT
, ul_debug(" item '%s'", p
));
1822 if (strncmp(p
, GPT_ATTRSTR_REQ
,
1823 sizeof(GPT_ATTRSTR_REQ
) - 1) == 0) {
1824 bit
= GPT_ATTRBIT_REQ
;
1825 p
+= sizeof(GPT_ATTRSTR_REQ
) - 1;
1826 } else if (strncmp(p
, GPT_ATTRSTR_REQ_TYPO
,
1827 sizeof(GPT_ATTRSTR_REQ_TYPO
) - 1) == 0) {
1828 bit
= GPT_ATTRBIT_REQ
;
1829 p
+= sizeof(GPT_ATTRSTR_REQ_TYPO
) - 1;
1830 } else if (strncmp(p
, GPT_ATTRSTR_LEGACY
,
1831 sizeof(GPT_ATTRSTR_LEGACY
) - 1) == 0) {
1832 bit
= GPT_ATTRBIT_LEGACY
;
1833 p
+= sizeof(GPT_ATTRSTR_LEGACY
) - 1;
1834 } else if (strncmp(p
, GPT_ATTRSTR_NOBLOCK
,
1835 sizeof(GPT_ATTRSTR_NOBLOCK
) - 1) == 0) {
1836 bit
= GPT_ATTRBIT_NOBLOCK
;
1837 p
+= sizeof(GPT_ATTRSTR_NOBLOCK
) - 1;
1839 /* GUID:<bit> as well as <bit> */
1840 } else if (isdigit((unsigned char) *p
)
1841 || (strncmp(p
, "GUID:", 5) == 0
1842 && isdigit((unsigned char) *(p
+ 5)))) {
1849 bit
= strtol(p
, &end
, 0);
1850 if (errno
|| !end
|| end
== str
1851 || bit
< GPT_ATTRBIT_GUID_FIRST
1852 || bit
>= GPT_ATTRBIT_GUID_FIRST
+ GPT_ATTRBIT_GUID_COUNT
)
1859 fdisk_warnx(cxt
, _("unsupported GPT attribute bit '%s'"), p
);
1863 if (*p
&& *p
!= ',' && !isblank(*p
)) {
1864 fdisk_warnx(cxt
, _("failed to parse GPT attribute string '%s'"), str
);
1870 while (isblank(*p
)) p
++;
1879 static int gpt_get_partition(struct fdisk_context
*cxt
, size_t n
,
1880 struct fdisk_partition
*pa
)
1882 struct fdisk_gpt_label
*gpt
;
1883 struct gpt_entry
*e
;
1884 char u_str
[UUID_STR_LEN
];
1886 struct gpt_guid guid
;
1890 assert(fdisk_is_label(cxt
, GPT
));
1892 gpt
= self_label(cxt
);
1894 if (n
>= gpt_get_nentries(gpt
))
1897 gpt
= self_label(cxt
);
1898 e
= gpt_get_entry(gpt
, n
);
1900 pa
->used
= gpt_entry_is_used(e
) || gpt_partition_start(e
);
1904 pa
->start
= gpt_partition_start(e
);
1905 pa
->size
= gpt_partition_size(e
);
1906 pa
->type
= gpt_partition_parttype(cxt
, e
);
1908 guid
= e
->partition_guid
;
1909 if (guid_to_string(&guid
, u_str
)) {
1910 pa
->uuid
= strdup(u_str
);
1918 rc
= gpt_entry_attrs_to_string(e
, &pa
->attrs
);
1922 pa
->name
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1925 fdisk_reset_partition(pa
);
1930 static int gpt_set_partition(struct fdisk_context
*cxt
, size_t n
,
1931 struct fdisk_partition
*pa
)
1933 struct fdisk_gpt_label
*gpt
;
1934 struct gpt_entry
*e
;
1936 uint64_t start
, end
;
1940 assert(fdisk_is_label(cxt
, GPT
));
1942 gpt
= self_label(cxt
);
1944 if (n
>= gpt_get_nentries(gpt
))
1947 FDISK_INIT_UNDEF(start
);
1948 FDISK_INIT_UNDEF(end
);
1950 gpt
= self_label(cxt
);
1951 e
= gpt_get_entry(gpt
, n
);
1954 char new_u
[UUID_STR_LEN
], old_u
[UUID_STR_LEN
];
1955 struct gpt_guid guid
;
1957 guid
= e
->partition_guid
;
1958 guid_to_string(&guid
, old_u
);
1959 rc
= gpt_entry_set_uuid(e
, pa
->uuid
);
1962 guid
= e
->partition_guid
;
1963 guid_to_string(&guid
, new_u
);
1964 fdisk_info(cxt
, _("Partition UUID changed from %s to %s."),
1970 char *old
= encode_to_utf8((unsigned char *)e
->name
, sizeof(e
->name
));
1971 len
= gpt_entry_set_name(e
, pa
->name
);
1973 fdisk_warn(cxt
, _("Failed to translate partition name, name not changed."));
1975 fdisk_info(cxt
, _("Partition name changed from '%s' to '%.*s'."),
1976 old
, len
, pa
->name
);
1980 if (pa
->type
&& pa
->type
->typestr
) {
1981 struct gpt_guid
typeid;
1983 rc
= string_to_guid(pa
->type
->typestr
, &typeid);
1986 gpt_entry_set_type(e
, &typeid);
1989 rc
= gpt_entry_attrs_from_string(cxt
, e
, pa
->attrs
);
1994 if (fdisk_partition_has_start(pa
))
1996 if (fdisk_partition_has_size(pa
) || fdisk_partition_has_start(pa
)) {
1997 uint64_t xstart
= fdisk_partition_has_start(pa
) ? pa
->start
: gpt_partition_start(e
);
1998 uint64_t xsize
= fdisk_partition_has_size(pa
) ? pa
->size
: gpt_partition_size(e
);
1999 end
= xstart
+ xsize
- 1ULL;
2002 if (!FDISK_IS_UNDEF(start
)) {
2003 if (start
< le64_to_cpu(gpt
->pheader
->first_usable_lba
)) {
2004 fdisk_warnx(cxt
, _("The start of the partition understeps FirstUsableLBA."));
2007 e
->lba_start
= cpu_to_le64(start
);
2009 if (!FDISK_IS_UNDEF(end
)) {
2010 if (end
> le64_to_cpu(gpt
->pheader
->last_usable_lba
)) {
2011 fdisk_warnx(cxt
, _("The end of the partition oversteps LastUsableLBA."));
2014 e
->lba_end
= cpu_to_le64(end
);
2016 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2017 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2019 fdisk_label_set_changed(cxt
->label
, 1);
2023 static int gpt_read(struct fdisk_context
*cxt
, off_t offset
, void *buf
, size_t count
)
2025 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
2028 if (read_all(cxt
->dev_fd
, buf
, count
))
2031 DBG(GPT
, ul_debug(" read OK [offset=%zu, size=%zu]",
2032 (size_t) offset
, count
));
2036 static int gpt_write(struct fdisk_context
*cxt
, off_t offset
, void *buf
, size_t count
)
2038 if (offset
!= lseek(cxt
->dev_fd
, offset
, SEEK_SET
))
2041 if (write_all(cxt
->dev_fd
, buf
, count
))
2044 if (fsync(cxt
->dev_fd
) != 0)
2047 DBG(GPT
, ul_debug(" write OK [offset=%zu, size=%zu]",
2048 (size_t) offset
, count
));
2054 * Returns 0 on success, or corresponding error otherwise.
2056 static int gpt_write_partitions(struct fdisk_context
*cxt
,
2057 struct gpt_header
*header
, unsigned char *ents
)
2062 rc
= gpt_sizeof_entries(header
, &esz
);
2066 return gpt_write(cxt
,
2067 (off_t
) le64_to_cpu(header
->partition_entry_lba
) * cxt
->sector_size
,
2072 * Write a GPT header to a specified LBA.
2074 * We read all sector, so we have to write all sector back
2075 * to the device -- never ever rely on sizeof(struct gpt_header)!
2077 * Returns 0 on success, or corresponding error otherwise.
2079 static int gpt_write_header(struct fdisk_context
*cxt
,
2080 struct gpt_header
*header
, uint64_t lba
)
2082 return gpt_write(cxt
, lba
* cxt
->sector_size
, header
, cxt
->sector_size
);
2086 * Write the protective MBR.
2087 * Returns 0 on success, or corresponding error otherwise.
2089 static int gpt_write_pmbr(struct fdisk_context
*cxt
)
2091 struct gpt_legacy_mbr
*pmbr
;
2092 struct gpt_legacy_mbr
*current
;
2096 assert(cxt
->firstsector
);
2098 DBG(GPT
, ul_debug("(over)writing PMBR"));
2099 pmbr
= (struct gpt_legacy_mbr
*) cxt
->firstsector
;
2101 /* zero out the legacy partitions */
2102 memset(pmbr
->partition_record
, 0, sizeof(pmbr
->partition_record
));
2104 pmbr
->signature
= cpu_to_le16(MSDOS_MBR_SIGNATURE
);
2105 pmbr
->partition_record
[0].os_type
= EFI_PMBR_OSTYPE
;
2106 pmbr
->partition_record
[0].start_sector
= 2;
2107 pmbr
->partition_record
[0].end_head
= 0xFF;
2108 pmbr
->partition_record
[0].end_sector
= 0xFF;
2109 pmbr
->partition_record
[0].end_track
= 0xFF;
2110 pmbr
->partition_record
[0].starting_lba
= cpu_to_le32(1);
2113 * Set size_in_lba to the size of the disk minus one. If the size of the disk
2114 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
2116 if (cxt
->total_sectors
- 1ULL > 0xFFFFFFFFULL
)
2117 pmbr
->partition_record
[0].size_in_lba
= cpu_to_le32(0xFFFFFFFF);
2119 pmbr
->partition_record
[0].size_in_lba
=
2120 cpu_to_le32((uint32_t) (cxt
->total_sectors
- 1ULL));
2122 /* Read the current PMBR and compare it with the new, don't write if
2124 current
= malloc(sizeof(*current
));
2128 rc
= gpt_read(cxt
, GPT_PMBR_LBA
* cxt
->sector_size
,
2129 current
, sizeof(*current
));
2131 rc
= memcmp(pmbr
, current
, sizeof(*current
));
2136 DBG(GPT
, ul_debug("Same MBR on disk => don't write it"));
2141 /* pMBR covers the first sector (LBA) of the disk */
2142 return gpt_write(cxt
, GPT_PMBR_LBA
* cxt
->sector_size
,
2143 pmbr
, cxt
->sector_size
);
2147 * Writes in-memory GPT and pMBR data to disk.
2148 * Returns 0 if successful write, otherwise, a corresponding error.
2149 * Any indication of error will abort the operation.
2151 static int gpt_write_disklabel(struct fdisk_context
*cxt
)
2153 struct fdisk_gpt_label
*gpt
;
2158 assert(fdisk_is_label(cxt
, GPT
));
2160 DBG(GPT
, ul_debug("writing..."));
2162 gpt
= self_label(cxt
);
2163 mbr_type
= valid_pmbr(cxt
);
2165 /* check that disk is big enough to handle the backup header */
2166 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) > cxt
->total_sectors
)
2169 /* check that the backup header is properly placed */
2170 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) < cxt
->total_sectors
- 1ULL)
2173 if (check_overlap_partitions(gpt
))
2177 gpt_minimize_alternative_lba(cxt
, gpt
);
2179 /* recompute CRCs for both headers */
2180 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2181 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2184 * UEFI requires writing in this specific order:
2185 * 1) backup partition tables
2186 * 2) backup GPT header
2187 * 3) primary partition tables
2188 * 4) primary GPT header
2191 * If any write fails, we abort the rest.
2193 if (gpt_write_partitions(cxt
, gpt
->bheader
, gpt
->ents
) != 0)
2195 if (gpt_write_header(cxt
, gpt
->bheader
,
2196 le64_to_cpu(gpt
->pheader
->alternative_lba
)) != 0)
2198 if (gpt_write_partitions(cxt
, gpt
->pheader
, gpt
->ents
) != 0)
2200 if (gpt_write_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
) != 0)
2203 if (mbr_type
== GPT_MBR_HYBRID
)
2204 fdisk_warnx(cxt
, _("The device contains hybrid MBR -- writing GPT only."));
2205 else if (gpt_write_pmbr(cxt
) != 0)
2208 DBG(GPT
, ul_debug("...write success"));
2211 DBG(GPT
, ul_debug("...write failed: incorrect input"));
2215 DBG(GPT
, ul_debug("...write failed: %m"));
2220 * Verify data integrity and report any found problems for:
2221 * - primary and backup header validations
2222 * - partition validations
2224 static int gpt_verify_disklabel(struct fdisk_context
*cxt
)
2228 struct fdisk_gpt_label
*gpt
;
2232 assert(fdisk_is_label(cxt
, GPT
));
2234 gpt
= self_label(cxt
);
2238 if (!gpt
->bheader
) {
2240 fdisk_warnx(cxt
, _("Disk does not contain a valid backup header."));
2243 if (!gpt_check_header_crc(gpt
->pheader
, gpt
->ents
)) {
2245 fdisk_warnx(cxt
, _("Invalid primary header CRC checksum."));
2247 if (gpt
->bheader
&& !gpt_check_header_crc(gpt
->bheader
, gpt
->ents
)) {
2249 fdisk_warnx(cxt
, _("Invalid backup header CRC checksum."));
2252 if (!gpt_check_entryarr_crc(gpt
->pheader
, gpt
->ents
)) {
2254 fdisk_warnx(cxt
, _("Invalid partition entry checksum."));
2257 if (!gpt_check_lba_sanity(cxt
, gpt
->pheader
)) {
2259 fdisk_warnx(cxt
, _("Invalid primary header LBA sanity checks."));
2261 if (gpt
->bheader
&& !gpt_check_lba_sanity(cxt
, gpt
->bheader
)) {
2263 fdisk_warnx(cxt
, _("Invalid backup header LBA sanity checks."));
2266 if (le64_to_cpu(gpt
->pheader
->my_lba
) != GPT_PRIMARY_PARTITION_TABLE_LBA
) {
2268 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at primary header."));
2270 if (gpt
->bheader
&& le64_to_cpu(gpt
->bheader
->my_lba
) != last_lba(cxt
)) {
2272 fdisk_warnx(cxt
, _("MyLBA mismatch with real position at backup header."));
2275 if (le64_to_cpu(gpt
->pheader
->alternative_lba
) >= cxt
->total_sectors
) {
2277 fdisk_warnx(cxt
, _("Disk is too small to hold all data."));
2281 * if the GPT is the primary table, check the alternateLBA
2282 * to see if it is a valid GPT
2284 if (gpt
->bheader
&& (le64_to_cpu(gpt
->pheader
->my_lba
) !=
2285 le64_to_cpu(gpt
->bheader
->alternative_lba
))) {
2287 fdisk_warnx(cxt
, _("Primary and backup header mismatch."));
2290 ptnum
= check_overlap_partitions(gpt
);
2293 fdisk_warnx(cxt
, _("Partition %u overlaps with partition %u."),
2297 ptnum
= check_too_big_partitions(gpt
, cxt
->total_sectors
);
2300 fdisk_warnx(cxt
, _("Partition %u is too big for the disk."),
2304 ptnum
= check_start_after_end_partitions(gpt
);
2307 fdisk_warnx(cxt
, _("Partition %u ends before it starts."),
2311 if (!nerror
) { /* yay :-) */
2312 uint32_t nsegments
= 0;
2313 uint64_t free_sectors
= 0, largest_segment
= 0;
2316 fdisk_info(cxt
, _("No errors detected."));
2317 fdisk_info(cxt
, _("Header version: %s"), gpt_get_header_revstr(gpt
->pheader
));
2318 fdisk_info(cxt
, _("Using %zu out of %zu partitions."),
2319 partitions_in_use(gpt
),
2320 gpt_get_nentries(gpt
));
2322 free_sectors
= get_free_sectors(cxt
, gpt
, &nsegments
, &largest_segment
);
2323 if (largest_segment
)
2324 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
| SIZE_SUFFIX_3LETTER
,
2325 largest_segment
* cxt
->sector_size
);
2328 P_("A total of %ju free sectors is available in %u segment.",
2329 "A total of %ju free sectors is available in %u segments "
2330 "(the largest is %s).", nsegments
),
2331 free_sectors
, nsegments
, strsz
? : "0 B");
2336 P_("%d error detected.", "%d errors detected.", nerror
),
2342 /* Delete a single GPT partition, specified by partnum. */
2343 static int gpt_delete_partition(struct fdisk_context
*cxt
,
2346 struct fdisk_gpt_label
*gpt
;
2350 assert(fdisk_is_label(cxt
, GPT
));
2352 gpt
= self_label(cxt
);
2354 if (partnum
>= cxt
->label
->nparts_max
)
2357 if (!gpt_entry_is_used(gpt_get_entry(gpt
, partnum
)))
2360 /* hasta la vista, baby! */
2361 gpt_zeroize_entry(gpt
, partnum
);
2363 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2364 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2365 cxt
->label
->nparts_cur
--;
2366 fdisk_label_set_changed(cxt
->label
, 1);
2372 /* Performs logical checks to add a new partition entry */
2373 static int gpt_add_partition(
2374 struct fdisk_context
*cxt
,
2375 struct fdisk_partition
*pa
,
2378 uint64_t user_f
, user_l
; /* user input ranges for first and last sectors */
2379 uint64_t disk_f
, disk_l
; /* first and last available sector ranges on device*/
2380 uint64_t dflt_f
, dflt_l
, max_l
; /* largest segment (default) */
2381 struct gpt_guid
typeid;
2382 struct fdisk_gpt_label
*gpt
;
2383 struct gpt_header
*pheader
;
2384 struct gpt_entry
*e
;
2385 struct fdisk_ask
*ask
= NULL
;
2391 assert(fdisk_is_label(cxt
, GPT
));
2393 gpt
= self_label(cxt
);
2396 assert(gpt
->pheader
);
2399 pheader
= gpt
->pheader
;
2401 rc
= fdisk_partition_next_partno(pa
, cxt
, &partnum
);
2403 DBG(GPT
, ul_debug("failed to get next partno"));
2407 assert(partnum
< gpt_get_nentries(gpt
));
2409 if (gpt_entry_is_used(gpt_get_entry(gpt
, partnum
))) {
2410 fdisk_warnx(cxt
, _("Partition %zu is already defined. "
2411 "Delete it before re-adding it."), partnum
+1);
2414 if (gpt_get_nentries(gpt
) == partitions_in_use(gpt
)) {
2415 fdisk_warnx(cxt
, _("All partitions are already in use."));
2418 if (!get_free_sectors(cxt
, gpt
, NULL
, NULL
)) {
2419 fdisk_warnx(cxt
, _("No free sectors available."));
2423 rc
= string_to_guid(pa
&& pa
->type
&& pa
->type
->typestr
?
2425 GPT_DEFAULT_ENTRY_TYPE
, &typeid);
2429 disk_f
= find_first_available(gpt
, le64_to_cpu(pheader
->first_usable_lba
));
2430 e
= gpt_get_entry(gpt
, 0);
2432 /* if first sector no explicitly defined then ignore small gaps before
2433 * the first partition */
2434 if ((!pa
|| !fdisk_partition_has_start(pa
))
2435 && gpt_entry_is_used(e
)
2436 && disk_f
< gpt_partition_start(e
)) {
2440 DBG(GPT
, ul_debug("testing first sector %"PRIu64
"", disk_f
));
2441 disk_f
= find_first_available(gpt
, disk_f
);
2444 x
= find_last_free(gpt
, disk_f
);
2445 if (x
- disk_f
>= cxt
->grain
/ cxt
->sector_size
)
2447 DBG(GPT
, ul_debug("first sector %"PRIu64
" addresses to small space, continue...", disk_f
));
2452 disk_f
= find_first_available(gpt
, le64_to_cpu(pheader
->first_usable_lba
));
2456 disk_l
= find_last_free_sector(gpt
);
2458 /* the default is the largest free space */
2459 dflt_f
= find_first_in_largest(gpt
);
2460 dflt_l
= find_last_free(gpt
, dflt_f
);
2462 /* don't offer too small free space by default, this is possible to
2463 * bypass by sfdisk script */
2464 if ((!pa
|| !fdisk_partition_has_start(pa
))
2465 && dflt_l
- dflt_f
+ 1 < cxt
->grain
/ cxt
->sector_size
) {
2466 fdisk_warnx(cxt
, _("No enough free sectors available."));
2470 /* align the default in range <dflt_f,dflt_l>*/
2471 dflt_f
= fdisk_align_lba_in_range(cxt
, dflt_f
, dflt_f
, dflt_l
);
2474 if (pa
&& pa
->start_follow_default
) {
2477 } else if (pa
&& fdisk_partition_has_start(pa
)) {
2478 DBG(GPT
, ul_debug("first sector defined: %ju", (uintmax_t)pa
->start
));
2479 if (pa
->start
!= find_first_available(gpt
, pa
->start
)) {
2480 fdisk_warnx(cxt
, _("Sector %ju already used."), (uintmax_t)pa
->start
);
2488 ask
= fdisk_new_ask();
2490 fdisk_reset_ask(ask
);
2495 fdisk_ask_set_query(ask
, _("First sector"));
2496 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_NUMBER
);
2497 fdisk_ask_number_set_low(ask
, disk_f
); /* minimal */
2498 fdisk_ask_number_set_default(ask
, dflt_f
); /* default */
2499 fdisk_ask_number_set_high(ask
, disk_l
); /* maximal */
2501 rc
= fdisk_do_ask(cxt
, ask
);
2505 user_f
= fdisk_ask_number_get_result(ask
);
2506 if (user_f
!= find_first_available(gpt
, user_f
)) {
2507 fdisk_warnx(cxt
, _("Sector %ju already used."), user_f
);
2516 dflt_l
= max_l
= find_last_free(gpt
, user_f
);
2518 /* Make sure the last partition has aligned size by default because
2519 * range specified by LastUsableLBA may be unaligned on disks where
2520 * logical sector != physical (512/4K) because backup header size is
2521 * calculated from logical sectors. */
2522 if (max_l
== le64_to_cpu(gpt
->pheader
->last_usable_lba
))
2523 dflt_l
= fdisk_align_lba_in_range(cxt
, max_l
, user_f
, max_l
) - 1;
2525 if (pa
&& pa
->end_follow_default
) {
2528 } else if (pa
&& fdisk_partition_has_size(pa
)) {
2529 user_l
= user_f
+ pa
->size
- 1;
2530 DBG(GPT
, ul_debug("size defined: %ju, end: %"PRIu64
2531 "(last possible: %"PRIu64
", optimal: %"PRIu64
")",
2532 (uintmax_t)pa
->size
, user_l
, max_l
, dflt_l
));
2534 if (user_l
!= dflt_l
2535 && !pa
->size_explicit
2536 && alignment_required(cxt
)
2537 && user_l
- user_f
> (cxt
->grain
/ fdisk_get_sector_size(cxt
))) {
2539 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
);
2540 if (user_l
> user_f
)
2546 ask
= fdisk_new_ask();
2548 fdisk_reset_ask(ask
);
2552 fdisk_ask_set_query(ask
, _("Last sector, +/-sectors or +/-size{K,M,G,T,P}"));
2553 fdisk_ask_set_type(ask
, FDISK_ASKTYPE_OFFSET
);
2554 fdisk_ask_number_set_low(ask
, user_f
); /* minimal */
2555 fdisk_ask_number_set_default(ask
, dflt_l
); /* default */
2556 fdisk_ask_number_set_high(ask
, max_l
); /* maximal */
2557 fdisk_ask_number_set_base(ask
, user_f
); /* base for relative input */
2558 fdisk_ask_number_set_unit(ask
, cxt
->sector_size
);
2559 fdisk_ask_number_set_wrap_negative(ask
, 1); /* wrap negative around high */
2561 rc
= fdisk_do_ask(cxt
, ask
);
2565 user_l
= fdisk_ask_number_get_result(ask
);
2566 if (fdisk_ask_number_is_relative(ask
)) {
2567 user_l
= fdisk_align_lba_in_range(cxt
, user_l
, user_f
, dflt_l
);
2568 if (user_l
> user_f
)
2572 if (user_l
>= user_f
&& user_l
<= disk_l
)
2575 fdisk_warnx(cxt
, _("Value out of range."));
2580 if (user_f
> user_l
|| partnum
>= cxt
->label
->nparts_max
) {
2581 fdisk_warnx(cxt
, _("Could not create partition %zu"), partnum
+ 1);
2586 /* Be paranoid and check against on-disk setting rather than against libfdisk cxt */
2587 if (user_l
> le64_to_cpu(pheader
->last_usable_lba
)) {
2588 fdisk_warnx(cxt
, _("The last usable GPT sector is %ju, but %ju is requested."),
2589 le64_to_cpu(pheader
->last_usable_lba
), user_l
);
2594 if (user_f
< le64_to_cpu(pheader
->first_usable_lba
)) {
2595 fdisk_warnx(cxt
, _("The first usable GPT sector is %ju, but %ju is requested."),
2596 le64_to_cpu(pheader
->first_usable_lba
), user_f
);
2601 assert(!FDISK_IS_UNDEF(user_l
));
2602 assert(!FDISK_IS_UNDEF(user_f
));
2603 assert(partnum
< gpt_get_nentries(gpt
));
2605 e
= gpt_get_entry(gpt
, partnum
);
2606 e
->lba_end
= cpu_to_le64(user_l
);
2607 e
->lba_start
= cpu_to_le64(user_f
);
2609 gpt_entry_set_type(e
, &typeid);
2611 if (pa
&& pa
->uuid
) {
2612 /* Sometimes it's necessary to create a copy of the PT and
2613 * reuse already defined UUID
2615 rc
= gpt_entry_set_uuid(e
, pa
->uuid
);
2619 /* Any time a new partition entry is created a new GUID must be
2620 * generated for that partition, and every partition is guaranteed
2621 * to have a unique GUID.
2623 struct gpt_guid guid
;
2625 uuid_generate_random((unsigned char *) &guid
);
2626 swap_efi_guid(&guid
);
2627 e
->partition_guid
= guid
;
2630 if (pa
&& pa
->name
&& *pa
->name
)
2631 gpt_entry_set_name(e
, pa
->name
);
2632 if (pa
&& pa
->attrs
)
2633 gpt_entry_attrs_from_string(cxt
, e
, pa
->attrs
);
2635 DBG(GPT
, ul_debug("new partition: partno=%zu, start=%"PRIu64
", end=%"PRIu64
", size=%"PRIu64
"",
2637 gpt_partition_start(e
),
2638 gpt_partition_end(e
),
2639 gpt_partition_size(e
)));
2641 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2642 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2646 struct fdisk_parttype
*t
;
2648 cxt
->label
->nparts_cur
++;
2649 fdisk_label_set_changed(cxt
->label
, 1);
2651 t
= gpt_partition_parttype(cxt
, e
);
2652 fdisk_info_new_partition(cxt
, partnum
+ 1, user_f
, user_l
, t
);
2653 fdisk_unref_parttype(t
);
2660 fdisk_unref_ask(ask
);
2665 * Create a new GPT disklabel - destroys any previous data.
2667 static int gpt_create_disklabel(struct fdisk_context
*cxt
)
2671 char str
[UUID_STR_LEN
];
2672 struct fdisk_gpt_label
*gpt
;
2673 struct gpt_guid guid
;
2677 assert(fdisk_is_label(cxt
, GPT
));
2679 gpt
= self_label(cxt
);
2681 /* label private stuff has to be empty, see gpt_deinit() */
2682 assert(gpt
->pheader
== NULL
);
2683 assert(gpt
->bheader
== NULL
);
2686 * When no header, entries or pmbr is set, we're probably
2687 * dealing with a new, empty disk - so always allocate memory
2688 * to deal with the data structures whatever the case is.
2690 rc
= gpt_mknew_pmbr(cxt
);
2694 assert(cxt
->sector_size
>= sizeof(struct gpt_header
));
2697 gpt
->pheader
= calloc(1, cxt
->sector_size
);
2698 if (!gpt
->pheader
) {
2702 rc
= gpt_mknew_header(cxt
, gpt
->pheader
, GPT_PRIMARY_PARTITION_TABLE_LBA
);
2706 /* backup ("copy" primary) */
2707 gpt
->bheader
= calloc(1, cxt
->sector_size
);
2708 if (!gpt
->bheader
) {
2712 rc
= gpt_mknew_header_from_bkp(cxt
, gpt
->bheader
,
2713 last_lba(cxt
), gpt
->pheader
);
2717 rc
= gpt_sizeof_entries(gpt
->pheader
, &esz
);
2720 gpt
->ents
= calloc(1, esz
);
2725 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2726 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2728 cxt
->label
->nparts_max
= gpt_get_nentries(gpt
);
2729 cxt
->label
->nparts_cur
= 0;
2731 guid
= gpt
->pheader
->disk_guid
;
2732 guid_to_string(&guid
, str
);
2733 fdisk_label_set_changed(cxt
->label
, 1);
2734 fdisk_info(cxt
, _("Created a new GPT disklabel (GUID: %s)."), str
);
2736 if (gpt_get_nentries(gpt
) < GPT_NPARTITIONS
)
2737 fdisk_info(cxt
, _("The maximal number of partitions is %zu (default is %zu)."),
2738 gpt_get_nentries(gpt
), GPT_NPARTITIONS
);
2743 static int gpt_set_disklabel_id(struct fdisk_context
*cxt
, const char *str
)
2745 struct fdisk_gpt_label
*gpt
;
2746 struct gpt_guid uuid
;
2752 assert(fdisk_is_label(cxt
, GPT
));
2754 gpt
= self_label(cxt
);
2758 if (fdisk_ask_string(cxt
,
2759 _("Enter new disk UUID (in 8-4-4-4-12 format)"), &buf
))
2761 rc
= string_to_guid(buf
, &uuid
);
2764 rc
= string_to_guid(str
, &uuid
);
2767 fdisk_warnx(cxt
, _("Failed to parse your UUID."));
2771 old
= gpt_get_header_id(gpt
->pheader
);
2773 gpt
->pheader
->disk_guid
= uuid
;
2774 gpt
->bheader
->disk_guid
= uuid
;
2776 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2777 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2779 new = gpt_get_header_id(gpt
->pheader
);
2781 fdisk_info(cxt
, _("Disk identifier changed from %s to %s."), old
, new);
2785 fdisk_label_set_changed(cxt
->label
, 1);
2789 static int gpt_check_table_overlap(struct fdisk_context
*cxt
,
2790 uint64_t first_usable
,
2791 uint64_t last_usable
)
2793 struct fdisk_gpt_label
*gpt
= self_label(cxt
);
2797 /* First check if there's enough room for the table. last_lba may have wrapped */
2798 if (first_usable
> cxt
->total_sectors
|| /* far too little space */
2799 last_usable
> cxt
->total_sectors
|| /* wrapped */
2800 first_usable
> last_usable
) { /* too little space */
2801 fdisk_warnx(cxt
, _("Not enough space for new partition table!"));
2805 /* check that all partitions fit in the remaining space */
2806 for (i
= 0; i
< gpt_get_nentries(gpt
); i
++) {
2807 struct gpt_entry
*e
= gpt_get_entry(gpt
, i
);
2809 if (!gpt_entry_is_used(e
))
2811 if (gpt_partition_start(e
) < first_usable
) {
2812 fdisk_warnx(cxt
, _("Partition #%zu out of range (minimal start is %"PRIu64
" sectors)"),
2813 i
+ 1, first_usable
);
2816 if (gpt_partition_end(e
) > last_usable
) {
2817 fdisk_warnx(cxt
, _("Partition #%zu out of range (maximal end is %"PRIu64
" sectors)"),
2818 i
+ 1, last_usable
- (uint64_t) 1);
2826 * fdisk_gpt_set_npartitions:
2828 * @nents: number of wanted entries
2830 * Enlarge GPT entries array if possible. The function check if an existing
2831 * partition does not overlap the entries array area. If yes, then it report
2832 * warning and returns -EINVAL.
2834 * Returns: 0 on success, < 0 on error.
2837 int fdisk_gpt_set_npartitions(struct fdisk_context
*cxt
, uint32_t nents
)
2839 struct fdisk_gpt_label
*gpt
;
2840 size_t new_size
= 0;
2842 uint64_t first_usable
= 0ULL, last_usable
= 0ULL;
2848 if (!fdisk_is_label(cxt
, GPT
))
2851 gpt
= self_label(cxt
);
2853 old_nents
= le32_to_cpu(gpt
->pheader
->npartition_entries
);
2854 if (old_nents
== nents
)
2855 return 0; /* do nothing, say nothing */
2857 /* calculate the size (bytes) of the entries array */
2858 rc
= gpt_calculate_sizeof_entries(gpt
->pheader
, nents
, &new_size
);
2860 uint32_t entry_size
= le32_to_cpu(gpt
->pheader
->sizeof_partition_entry
);
2862 if (entry_size
== 0)
2863 fdisk_warnx(cxt
, _("The partition entry size is zero."));
2865 fdisk_warnx(cxt
, _("The number of the partition has to be smaller than %zu."),
2866 (size_t) UINT32_MAX
/ entry_size
);
2870 rc
= gpt_calculate_first_lba(gpt
->pheader
, nents
, &first_usable
, cxt
);
2872 rc
= gpt_calculate_last_lba(gpt
->pheader
, nents
, &last_usable
, cxt
);
2876 /* if expanding the table, first check that everything fits,
2877 * then allocate more memory and zero. */
2878 if (nents
> old_nents
) {
2879 unsigned char *ents
;
2880 size_t old_size
= 0;
2882 rc
= gpt_calculate_sizeof_entries(gpt
->pheader
, old_nents
, &old_size
);
2884 rc
= gpt_check_table_overlap(cxt
, first_usable
, last_usable
);
2887 ents
= realloc(gpt
->ents
, new_size
);
2889 fdisk_warnx(cxt
, _("Cannot allocate memory!"));
2892 memset(ents
+ old_size
, 0, new_size
- old_size
);
2896 /* everything's ok, apply the new size */
2897 gpt
->pheader
->npartition_entries
= cpu_to_le32(nents
);
2898 gpt
->bheader
->npartition_entries
= cpu_to_le32(nents
);
2900 /* usable LBA addresses will have changed */
2901 fdisk_set_first_lba(cxt
, first_usable
);
2902 fdisk_set_last_lba(cxt
, last_usable
);
2903 gpt
->pheader
->first_usable_lba
= cpu_to_le64(first_usable
);
2904 gpt
->bheader
->first_usable_lba
= cpu_to_le64(first_usable
);
2905 gpt
->pheader
->last_usable_lba
= cpu_to_le64(last_usable
);
2906 gpt
->bheader
->last_usable_lba
= cpu_to_le64(last_usable
);
2908 /* The backup header must be recalculated */
2909 gpt_mknew_header_common(cxt
, gpt
->bheader
, le64_to_cpu(gpt
->pheader
->alternative_lba
));
2911 /* CRCs will have changed */
2912 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
2913 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
2915 /* update library info */
2916 cxt
->label
->nparts_max
= gpt_get_nentries(gpt
);
2918 fdisk_info(cxt
, _("Partition table length changed from %"PRIu32
" to %"PRIu32
"."),
2921 fdisk_label_set_changed(cxt
->label
, 1);
2925 static int gpt_part_is_used(struct fdisk_context
*cxt
, size_t i
)
2927 struct fdisk_gpt_label
*gpt
;
2928 struct gpt_entry
*e
;
2932 assert(fdisk_is_label(cxt
, GPT
));
2934 gpt
= self_label(cxt
);
2936 if (i
>= gpt_get_nentries(gpt
))
2939 e
= gpt_get_entry(gpt
, i
);
2941 return gpt_entry_is_used(e
) || gpt_partition_start(e
);
2945 * fdisk_gpt_is_hybrid:
2948 * The regular GPT contains PMBR (dummy protective MBR) where the protective
2949 * MBR does not address any partitions.
2951 * Hybrid GPT contains regular MBR where this partition table addresses the
2952 * same partitions as GPT. It's recommended to not use hybrid GPT due to MBR
2955 * The libfdisk does not provide functionality to sync GPT and MBR, you have to
2956 * directly access and modify (P)MBR (see fdisk_new_nested_context()).
2958 * Returns: 1 if partition table detected as hybrid otherwise return 0
2960 int fdisk_gpt_is_hybrid(struct fdisk_context
*cxt
)
2963 return valid_pmbr(cxt
) == GPT_MBR_HYBRID
;
2967 * fdisk_gpt_get_partition_attrs:
2969 * @partnum: partition number
2970 * @attrs: GPT partition attributes
2972 * Sets @attrs for the given partition
2974 * Returns: 0 on success, <0 on error.
2976 int fdisk_gpt_get_partition_attrs(
2977 struct fdisk_context
*cxt
,
2981 struct fdisk_gpt_label
*gpt
;
2986 if (!fdisk_is_label(cxt
, GPT
))
2989 gpt
= self_label(cxt
);
2991 if (partnum
>= gpt_get_nentries(gpt
))
2994 *attrs
= le64_to_cpu(gpt_get_entry(gpt
, partnum
)->attrs
);
2999 * fdisk_gpt_set_partition_attrs:
3001 * @partnum: partition number
3002 * @attrs: GPT partition attributes
3004 * Sets the GPT partition attributes field to @attrs.
3006 * Returns: 0 on success, <0 on error.
3008 int fdisk_gpt_set_partition_attrs(
3009 struct fdisk_context
*cxt
,
3013 struct fdisk_gpt_label
*gpt
;
3018 if (!fdisk_is_label(cxt
, GPT
))
3021 DBG(GPT
, ul_debug("entry attributes change requested partno=%zu", partnum
));
3022 gpt
= self_label(cxt
);
3024 if (partnum
>= gpt_get_nentries(gpt
))
3027 gpt_get_entry(gpt
, partnum
)->attrs
= cpu_to_le64(attrs
);
3028 fdisk_info(cxt
, _("The attributes on partition %zu changed to 0x%016" PRIx64
"."),
3029 partnum
+ 1, attrs
);
3031 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
3032 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
3033 fdisk_label_set_changed(cxt
->label
, 1);
3037 static int gpt_toggle_partition_flag(
3038 struct fdisk_context
*cxt
,
3042 struct fdisk_gpt_label
*gpt
;
3043 struct gpt_entry
*e
;
3047 const char *name
= NULL
;
3052 assert(fdisk_is_label(cxt
, GPT
));
3054 DBG(GPT
, ul_debug("entry attribute change requested partno=%zu", i
));
3055 gpt
= self_label(cxt
);
3057 if (i
>= gpt_get_nentries(gpt
))
3060 e
= gpt_get_entry(gpt
, i
);
3062 bits
= (char *) &attrs
;
3065 case GPT_FLAG_REQUIRED
:
3066 bit
= GPT_ATTRBIT_REQ
;
3067 name
= GPT_ATTRSTR_REQ
;
3069 case GPT_FLAG_NOBLOCK
:
3070 bit
= GPT_ATTRBIT_NOBLOCK
;
3071 name
= GPT_ATTRSTR_NOBLOCK
;
3073 case GPT_FLAG_LEGACYBOOT
:
3074 bit
= GPT_ATTRBIT_LEGACY
;
3075 name
= GPT_ATTRSTR_LEGACY
;
3077 case GPT_FLAG_GUIDSPECIFIC
:
3078 rc
= fdisk_ask_number(cxt
, 48, 48, 63, _("Enter GUID specific bit"), &tmp
);
3084 /* already specified PT_FLAG_GUIDSPECIFIC bit */
3085 if (flag
>= 48 && flag
<= 63) {
3087 flag
= GPT_FLAG_GUIDSPECIFIC
;
3093 fdisk_warnx(cxt
, _("failed to toggle unsupported bit %lu"), flag
);
3097 if (!isset(bits
, bit
))
3104 if (flag
== GPT_FLAG_GUIDSPECIFIC
)
3105 fdisk_info(cxt
, isset(bits
, bit
) ?
3106 _("The GUID specific bit %d on partition %zu is enabled now.") :
3107 _("The GUID specific bit %d on partition %zu is disabled now."),
3110 fdisk_info(cxt
, isset(bits
, bit
) ?
3111 _("The %s flag on partition %zu is enabled now.") :
3112 _("The %s flag on partition %zu is disabled now."),
3115 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
3116 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
3117 fdisk_label_set_changed(cxt
->label
, 1);
3121 static int gpt_entry_cmp_start(const void *a
, const void *b
)
3123 const struct gpt_entry
*ae
= (const struct gpt_entry
*) a
,
3124 *be
= (const struct gpt_entry
*) b
;
3125 int au
= gpt_entry_is_used(ae
),
3126 bu
= gpt_entry_is_used(be
);
3135 return cmp_numbers(gpt_partition_start(ae
), gpt_partition_start(be
));
3138 /* sort partition by start sector */
3139 static int gpt_reorder(struct fdisk_context
*cxt
)
3141 struct fdisk_gpt_label
*gpt
;
3142 size_t i
, nparts
, mess
;
3146 assert(fdisk_is_label(cxt
, GPT
));
3148 gpt
= self_label(cxt
);
3149 nparts
= gpt_get_nentries(gpt
);
3151 for (i
= 0, mess
= 0; mess
== 0 && i
+ 1 < nparts
; i
++)
3152 mess
= gpt_entry_cmp_start(
3153 (const void *) gpt_get_entry(gpt
, i
),
3154 (const void *) gpt_get_entry(gpt
, i
+ 1)) > 0;
3159 qsort(gpt
->ents
, nparts
, sizeof(struct gpt_entry
),
3160 gpt_entry_cmp_start
);
3162 gpt_recompute_crc(gpt
->pheader
, gpt
->ents
);
3163 gpt_recompute_crc(gpt
->bheader
, gpt
->ents
);
3164 fdisk_label_set_changed(cxt
->label
, 1);
3169 static int gpt_reset_alignment(struct fdisk_context
*cxt
)
3171 struct fdisk_gpt_label
*gpt
;
3172 struct gpt_header
*h
;
3176 assert(fdisk_is_label(cxt
, GPT
));
3178 gpt
= self_label(cxt
);
3179 h
= gpt
? gpt
->pheader
: NULL
;
3182 /* always follow existing table */
3183 cxt
->first_lba
= le64_to_cpu(h
->first_usable_lba
);
3184 cxt
->last_lba
= le64_to_cpu(h
->last_usable_lba
);
3186 /* estimate ranges for GPT */
3187 uint64_t first
, last
;
3190 rc
= count_first_last_lba(cxt
, &first
, &last
, NULL
);
3193 if (cxt
->first_lba
< first
)
3194 cxt
->first_lba
= first
;
3195 if (cxt
->last_lba
> last
)
3196 cxt
->last_lba
= last
;
3202 * Deinitialize fdisk-specific variables
3204 static void gpt_deinit(struct fdisk_label
*lb
)
3206 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
3216 gpt
->pheader
= NULL
;
3217 gpt
->bheader
= NULL
;
3220 static const struct fdisk_label_operations gpt_operations
=
3222 .probe
= gpt_probe_label
,
3223 .write
= gpt_write_disklabel
,
3224 .verify
= gpt_verify_disklabel
,
3225 .create
= gpt_create_disklabel
,
3226 .locate
= gpt_locate_disklabel
,
3227 .get_item
= gpt_get_disklabel_item
,
3228 .set_id
= gpt_set_disklabel_id
,
3230 .get_part
= gpt_get_partition
,
3231 .set_part
= gpt_set_partition
,
3232 .add_part
= gpt_add_partition
,
3233 .del_part
= gpt_delete_partition
,
3234 .reorder
= gpt_reorder
,
3236 .part_is_used
= gpt_part_is_used
,
3237 .part_toggle_flag
= gpt_toggle_partition_flag
,
3239 .deinit
= gpt_deinit
,
3241 .reset_alignment
= gpt_reset_alignment
3244 static const struct fdisk_field gpt_fields
[] =
3247 { FDISK_FIELD_DEVICE
, N_("Device"), 10, 0 },
3248 { FDISK_FIELD_START
, N_("Start"), 5, FDISK_FIELDFL_NUMBER
},
3249 { FDISK_FIELD_END
, N_("End"), 5, FDISK_FIELDFL_NUMBER
},
3250 { FDISK_FIELD_SECTORS
, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER
},
3251 { FDISK_FIELD_SIZE
, N_("Size"), 5, FDISK_FIELDFL_NUMBER
| FDISK_FIELDFL_EYECANDY
},
3252 { FDISK_FIELD_TYPE
, N_("Type"), 0.1, FDISK_FIELDFL_EYECANDY
},
3254 { FDISK_FIELD_TYPEID
, N_("Type-UUID"), 36, FDISK_FIELDFL_DETAIL
},
3255 { FDISK_FIELD_UUID
, N_("UUID"), 36, FDISK_FIELDFL_DETAIL
},
3256 { FDISK_FIELD_NAME
, N_("Name"), 0.2, FDISK_FIELDFL_DETAIL
},
3257 { FDISK_FIELD_ATTR
, N_("Attrs"), 0, FDISK_FIELDFL_DETAIL
}
3261 * allocates GPT in-memory stuff
3263 struct fdisk_label
*fdisk_new_gpt_label(struct fdisk_context
*cxt
__attribute__ ((__unused__
)))
3265 struct fdisk_label
*lb
;
3266 struct fdisk_gpt_label
*gpt
;
3268 gpt
= calloc(1, sizeof(*gpt
));
3272 /* initialize generic part of the driver */
3273 lb
= (struct fdisk_label
*) gpt
;
3275 lb
->id
= FDISK_DISKLABEL_GPT
;
3276 lb
->op
= &gpt_operations
;
3278 lb
->parttypes
= gpt_parttypes
;
3279 lb
->nparttypes
= ARRAY_SIZE(gpt_parttypes
);
3280 lb
->parttype_cuts
= gpt_parttype_cuts
;
3281 lb
->nparttype_cuts
= ARRAY_SIZE(gpt_parttype_cuts
);
3283 lb
->fields
= gpt_fields
;
3284 lb
->nfields
= ARRAY_SIZE(gpt_fields
);
3286 /* return calloc() result to keep static anaylizers happy */
3287 return (struct fdisk_label
*) gpt
;
3291 * fdisk_gpt_disable_relocation
3295 * Disable automatic backup header relocation to the end of the device. The
3296 * header position is recalculated during libfdisk probing stage by
3297 * fdisk_assign_device() and later written by fdisk_write_disklabel(), so you
3298 * need to call it before fdisk_assign_device().
3302 void fdisk_gpt_disable_relocation(struct fdisk_label
*lb
, int disable
)
3304 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
3307 gpt
->no_relocate
= disable
? 1 : 0;
3311 * fdisk_gpt_enable_minimize
3315 * Force libfdisk to write backup header to behind last partition. The
3316 * header position is recalculated on fdisk_write_disklabel().
3320 void fdisk_gpt_enable_minimize(struct fdisk_label
*lb
, int enable
)
3322 struct fdisk_gpt_label
*gpt
= (struct fdisk_gpt_label
*) lb
;
3325 gpt
->minimize
= enable
? 1 : 0;
3329 static int test_getattr(struct fdisk_test
*ts
__attribute__((unused
)),
3330 int argc
, char *argv
[])
3335 const char *disk
= argv
[1];
3336 size_t part
= strtoul(argv
[2], NULL
, 0) - 1;
3337 struct fdisk_context
*cxt
;
3338 uint64_t atters
= 0;
3340 cxt
= fdisk_new_context();
3341 fdisk_assign_device(cxt
, disk
, 1);
3343 if (!fdisk_is_label(cxt
, GPT
))
3344 return EXIT_FAILURE
;
3346 if (fdisk_gpt_get_partition_attrs(cxt
, part
, &atters
))
3347 return EXIT_FAILURE
;
3349 printf("%s: 0x%016" PRIx64
"\n", argv
[2], atters
);
3351 fdisk_unref_context(cxt
);
3355 static int test_setattr(struct fdisk_test
*ts
__attribute__((unused
)),
3356 int argc
, char *argv
[])
3361 const char *disk
= argv
[1];
3362 size_t part
= strtoul(argv
[2], NULL
, 0) - 1;
3363 uint64_t atters
= strtoull(argv
[3], NULL
, 0);
3364 struct fdisk_context
*cxt
;
3366 cxt
= fdisk_new_context();
3367 fdisk_assign_device(cxt
, disk
, 0);
3369 if (!fdisk_is_label(cxt
, GPT
))
3370 return EXIT_FAILURE
;
3372 if (fdisk_gpt_set_partition_attrs(cxt
, part
, atters
))
3373 return EXIT_FAILURE
;
3375 if (fdisk_write_disklabel(cxt
))
3376 return EXIT_FAILURE
;
3378 fdisk_unref_context(cxt
);
3382 int main(int argc
, char *argv
[])
3384 struct fdisk_test tss
[] = {
3385 { "--getattr", test_getattr
, "<disk> <partition> print attributes" },
3386 { "--setattr", test_setattr
, "<disk> <partition> <value> set attributes" },
3390 return fdisk_run_test(tss
, argc
, argv
);