]>
Commit | Line | Data |
---|---|---|
5dbff4c0 | 1 | /* |
766d5156 DB |
2 | * Copyright (C) 2007 Karel Zak <kzak@redhat.com> |
3 | * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org> | |
4 | * | |
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. | |
5dbff4c0 | 8 | */ |
5dbff4c0 KZ |
9 | #include <stdio.h> |
10 | #include <string.h> | |
11 | #include <stdlib.h> | |
12 | #include <inttypes.h> | |
13 | #include <sys/stat.h> | |
5dbff4c0 KZ |
14 | #include <sys/utsname.h> |
15 | #include <sys/types.h> | |
16 | #include <fcntl.h> | |
17 | #include <unistd.h> | |
18 | #include <errno.h> | |
766d5156 DB |
19 | #include <ctype.h> |
20 | #include <uuid.h> | |
5dbff4c0 | 21 | |
62d50bbe KZ |
22 | #include "fdiskP.h" |
23 | ||
766d5156 | 24 | #include "crc32.h" |
810f986b | 25 | #include "blkdev.h" |
9eca9d0d | 26 | #include "bitops.h" |
766d5156 | 27 | #include "strutils.h" |
19613111 | 28 | #include "all-io.h" |
3457d90e | 29 | #include "pt-mbr.h" |
766d5156 | 30 | |
0077e7cd KZ |
31 | /** |
32 | * SECTION: gpt | |
705854f3 KZ |
33 | * @title: UEFI GPT |
34 | * @short_description: specific functionality | |
0077e7cd KZ |
35 | */ |
36 | ||
766d5156 DB |
37 | #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */ |
38 | #define GPT_HEADER_REVISION_V1_02 0x00010200 | |
39 | #define GPT_HEADER_REVISION_V1_00 0x00010000 | |
40 | #define GPT_HEADER_REVISION_V0_99 0x00009900 | |
e9bf0935 | 41 | #define GPT_HEADER_MINSZ 92 /* bytes */ |
766d5156 DB |
42 | |
43 | #define GPT_PMBR_LBA 0 | |
44 | #define GPT_MBR_PROTECTIVE 1 | |
45 | #define GPT_MBR_HYBRID 2 | |
46 | ||
0a7cdf80 | 47 | #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001ULL |
766d5156 DB |
48 | |
49 | #define EFI_PMBR_OSTYPE 0xEE | |
50 | #define MSDOS_MBR_SIGNATURE 0xAA55 | |
e39966c6 | 51 | #define GPT_PART_NAME_LEN (72 / sizeof(uint16_t)) |
74760053 | 52 | #define GPT_NPARTITIONS FDISK_GPT_NPARTITIONS_DEFAULT |
766d5156 DB |
53 | |
54 | /* Globally unique identifier */ | |
55 | struct gpt_guid { | |
56 | uint32_t time_low; | |
57 | uint16_t time_mid; | |
58 | uint16_t time_hi_and_version; | |
59 | uint8_t clock_seq_hi; | |
60 | uint8_t clock_seq_low; | |
61 | uint8_t node[6]; | |
62 | }; | |
63 | ||
64 | ||
65 | /* only checking that the GUID is 0 is enough to verify an empty partition. */ | |
66 | #define GPT_UNUSED_ENTRY_GUID \ | |
67 | ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \ | |
68 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}) | |
69 | ||
70 | /* Linux native partition type */ | |
c0d14b09 | 71 | #define GPT_DEFAULT_ENTRY_TYPE "0FC63DAF-8483-4772-8E79-3D69D8477DE4" |
766d5156 DB |
72 | |
73 | /* | |
74 | * Attribute bits | |
75 | */ | |
01086b80 KZ |
76 | enum { |
77 | /* UEFI specific */ | |
78 | GPT_ATTRBIT_REQ = 0, | |
79 | GPT_ATTRBIT_NOBLOCK = 1, | |
80 | GPT_ATTRBIT_LEGACY = 2, | |
81 | ||
82 | /* GUID specific (range 48..64)*/ | |
83 | GPT_ATTRBIT_GUID_FIRST = 48, | |
84 | GPT_ATTRBIT_GUID_COUNT = 16 | |
85 | }; | |
c83f772e | 86 | |
d1b7bfe5 SR |
87 | #define GPT_ATTRSTR_REQ "RequiredPartition" |
88 | #define GPT_ATTRSTR_REQ_TYPO "RequiredPartiton" | |
01086b80 KZ |
89 | #define GPT_ATTRSTR_NOBLOCK "NoBlockIOProtocol" |
90 | #define GPT_ATTRSTR_LEGACY "LegacyBIOSBootable" | |
c83f772e | 91 | |
766d5156 DB |
92 | /* The GPT Partition entry array contains an array of GPT entries. */ |
93 | struct gpt_entry { | |
d45fa25d KZ |
94 | struct gpt_guid type; /* purpose and type of the partition */ |
95 | struct gpt_guid partition_guid; | |
766d5156 DB |
96 | uint64_t lba_start; |
97 | uint64_t lba_end; | |
01086b80 | 98 | uint64_t attrs; |
d45fa25d | 99 | uint16_t name[GPT_PART_NAME_LEN]; |
766d5156 DB |
100 | } __attribute__ ((packed)); |
101 | ||
102 | /* GPT header */ | |
103 | struct gpt_header { | |
104 | uint64_t signature; /* header identification */ | |
105 | uint32_t revision; /* header version */ | |
106 | uint32_t size; /* in bytes */ | |
107 | uint32_t crc32; /* header CRC checksum */ | |
108 | uint32_t reserved1; /* must be 0 */ | |
9ed38607 | 109 | uint64_t my_lba; /* LBA of block that contains this struct (LBA 1) */ |
766d5156 DB |
110 | uint64_t alternative_lba; /* backup GPT header */ |
111 | uint64_t first_usable_lba; /* first usable logical block for partitions */ | |
112 | uint64_t last_usable_lba; /* last usable logical block for partitions */ | |
3f731001 | 113 | struct gpt_guid disk_guid; /* unique disk identifier */ |
9ed38607 | 114 | uint64_t partition_entry_lba; /* LBA of start of partition entries array */ |
766d5156 DB |
115 | uint32_t npartition_entries; /* total partition entries - normally 128 */ |
116 | uint32_t sizeof_partition_entry; /* bytes for each GUID pt */ | |
117 | uint32_t partition_entry_array_crc32; /* partition CRC checksum */ | |
9ed38607 | 118 | uint8_t reserved2[512 - 92]; /* must all be 0 */ |
766d5156 DB |
119 | } __attribute__ ((packed)); |
120 | ||
121 | struct gpt_record { | |
122 | uint8_t boot_indicator; /* unused by EFI, set to 0x80 for bootable */ | |
123 | uint8_t start_head; /* unused by EFI, pt start in CHS */ | |
124 | uint8_t start_sector; /* unused by EFI, pt start in CHS */ | |
125 | uint8_t start_track; | |
126 | uint8_t os_type; /* EFI and legacy non-EFI OS types */ | |
127 | uint8_t end_head; /* unused by EFI, pt end in CHS */ | |
128 | uint8_t end_sector; /* unused by EFI, pt end in CHS */ | |
129 | uint8_t end_track; /* unused by EFI, pt end in CHS */ | |
130 | uint32_t starting_lba; /* used by EFI - start addr of the on disk pt */ | |
131 | uint32_t size_in_lba; /* used by EFI - size of pt in LBA */ | |
132 | } __attribute__ ((packed)); | |
133 | ||
134 | /* Protected MBR and legacy MBR share same structure */ | |
135 | struct gpt_legacy_mbr { | |
136 | uint8_t boot_code[440]; | |
137 | uint32_t unique_mbr_signature; | |
138 | uint16_t unknown; | |
139 | struct gpt_record partition_record[4]; | |
140 | uint16_t signature; | |
141 | } __attribute__ ((packed)); | |
142 | ||
143 | /* | |
144 | * Here be dragons! | |
145 | * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs | |
146 | */ | |
147 | #define DEF_GUID(_u, _n) \ | |
148 | { \ | |
149 | .typestr = (_u), \ | |
150 | .name = (_n), \ | |
151 | } | |
152 | ||
153 | static struct fdisk_parttype gpt_parttypes[] = | |
154 | { | |
96c2b09f | 155 | #include "pt-gpt-partnames.h" |
766d5156 DB |
156 | }; |
157 | ||
694a407d KZ |
158 | #define alignment_required(_x) ((_x)->grain != (_x)->sector_size) |
159 | ||
d71ef5a4 | 160 | /* gpt_entry macros */ |
874aa9c3 KZ |
161 | #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start) |
162 | #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end) | |
163 | ||
d71ef5a4 KZ |
164 | /* |
165 | * in-memory fdisk GPT stuff | |
166 | */ | |
167 | struct fdisk_gpt_label { | |
168 | struct fdisk_label head; /* generic part */ | |
169 | ||
170 | /* gpt specific part */ | |
171 | struct gpt_header *pheader; /* primary header */ | |
172 | struct gpt_header *bheader; /* backup header */ | |
b28df75e KZ |
173 | |
174 | unsigned char *ents; /* entries (partitions) */ | |
d71ef5a4 KZ |
175 | }; |
176 | ||
177 | static void gpt_deinit(struct fdisk_label *lb); | |
178 | ||
9ffeb235 | 179 | static inline struct fdisk_gpt_label *self_label(struct fdisk_context *cxt) |
d71ef5a4 | 180 | { |
d71ef5a4 KZ |
181 | return (struct fdisk_gpt_label *) cxt->label; |
182 | } | |
183 | ||
874aa9c3 KZ |
184 | /* |
185 | * Returns the partition length, or 0 if end is before beginning. | |
186 | */ | |
187 | static uint64_t gpt_partition_size(const struct gpt_entry *e) | |
188 | { | |
189 | uint64_t start = gpt_partition_start(e); | |
190 | uint64_t end = gpt_partition_end(e); | |
191 | ||
192 | return start > end ? 0 : end - start + 1ULL; | |
193 | } | |
194 | ||
c0d14b09 | 195 | /* prints UUID in the real byte order! */ |
88141067 | 196 | static void gpt_debug_uuid(const char *mesg, struct gpt_guid *guid) |
c0d14b09 KZ |
197 | { |
198 | const unsigned char *uuid = (unsigned char *) guid; | |
199 | ||
200 | fprintf(stderr, "%s: " | |
201 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", | |
202 | mesg, | |
203 | uuid[0], uuid[1], uuid[2], uuid[3], | |
204 | uuid[4], uuid[5], | |
205 | uuid[6], uuid[7], | |
206 | uuid[8], uuid[9], | |
207 | uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]); | |
208 | } | |
c0d14b09 | 209 | |
766d5156 DB |
210 | /* |
211 | * UUID is traditionally 16 byte big-endian array, except Intel EFI | |
212 | * specification where the UUID is a structure of little-endian fields. | |
213 | */ | |
214 | static void swap_efi_guid(struct gpt_guid *uid) | |
215 | { | |
216 | uid->time_low = swab32(uid->time_low); | |
217 | uid->time_mid = swab16(uid->time_mid); | |
218 | uid->time_hi_and_version = swab16(uid->time_hi_and_version); | |
5dbff4c0 KZ |
219 | } |
220 | ||
c0d14b09 | 221 | static int string_to_guid(const char *in, struct gpt_guid *guid) |
766d5156 | 222 | { |
4044d244 | 223 | if (uuid_parse(in, (unsigned char *) guid)) { /* BE */ |
36cd4b3c | 224 | DBG(GPT, ul_debug("failed to parse GUID: %s", in)); |
4044d244 KZ |
225 | return -EINVAL; |
226 | } | |
c0d14b09 | 227 | swap_efi_guid(guid); /* LE */ |
766d5156 DB |
228 | return 0; |
229 | } | |
230 | ||
7f539277 | 231 | static char *guid_to_string(const struct gpt_guid *guid, char *out) |
766d5156 | 232 | { |
c0d14b09 KZ |
233 | struct gpt_guid u = *guid; /* LE */ |
234 | ||
235 | swap_efi_guid(&u); /* BE */ | |
236 | uuid_unparse_upper((unsigned char *) &u, out); | |
237 | ||
46667ba4 | 238 | return out; |
766d5156 DB |
239 | } |
240 | ||
7f539277 KZ |
241 | static struct fdisk_parttype *gpt_partition_parttype( |
242 | struct fdisk_context *cxt, | |
243 | const struct gpt_entry *e) | |
244 | { | |
245 | struct fdisk_parttype *t; | |
b443c177 | 246 | char str[UUID_STR_LEN]; |
92e486f8 | 247 | struct gpt_guid guid = e->type; |
7f539277 | 248 | |
92e486f8 | 249 | guid_to_string(&guid, str); |
a745611d | 250 | t = fdisk_label_get_parttype_from_string(cxt->label, str); |
7f539277 KZ |
251 | return t ? : fdisk_new_unknown_parttype(0, str); |
252 | } | |
253 | ||
b0a484a8 KZ |
254 | static void gpt_entry_set_type(struct gpt_entry *e, struct gpt_guid *uuid) |
255 | { | |
256 | e->type = *uuid; | |
36cd4b3c | 257 | DBG(GPT, gpt_debug_uuid("new type", uuid)); |
b0a484a8 KZ |
258 | } |
259 | ||
5be3df4a | 260 | static int gpt_entry_set_name(struct gpt_entry *e, char *str) |
b0a484a8 | 261 | { |
5be3df4a VD |
262 | uint16_t name[GPT_PART_NAME_LEN] = { 0 }; |
263 | size_t i, mblen = 0; | |
264 | uint8_t *in = (uint8_t *) str; | |
265 | ||
266 | for (i = 0; *in && i < GPT_PART_NAME_LEN; in++) { | |
267 | if (!mblen) { | |
268 | if (!(*in & 0x80)) { | |
269 | name[i++] = *in; | |
270 | } else if ((*in & 0xE0) == 0xC0) { | |
271 | mblen = 1; | |
272 | name[i] = (uint16_t)(*in & 0x1F) << (mblen *6); | |
273 | } else if ((*in & 0xF0) == 0xE0) { | |
274 | mblen = 2; | |
275 | name[i] = (uint16_t)(*in & 0x0F) << (mblen *6); | |
276 | } else { | |
277 | /* broken UTF-8 or code point greater than U+FFFF */ | |
278 | return -EILSEQ; | |
279 | } | |
280 | } else { | |
281 | /* incomplete UTF-8 sequence */ | |
282 | if ((*in & 0xC0) != 0x80) | |
283 | return -EILSEQ; | |
284 | ||
285 | name[i] |= (uint16_t)(*in & 0x3F) << (--mblen *6); | |
286 | if (!mblen) { | |
287 | /* check for code points reserved for surrogate pairs*/ | |
288 | if ((name[i] & 0xF800) == 0xD800) | |
289 | return -EILSEQ; | |
290 | i++; | |
291 | } | |
292 | } | |
b0a484a8 KZ |
293 | } |
294 | ||
295 | for (i = 0; i < GPT_PART_NAME_LEN; i++) | |
5be3df4a VD |
296 | e->name[i] = cpu_to_le16(name[i]); |
297 | ||
c1165241 | 298 | return (int)((char *) in - str); |
b0a484a8 KZ |
299 | } |
300 | ||
301 | static int gpt_entry_set_uuid(struct gpt_entry *e, char *str) | |
302 | { | |
303 | struct gpt_guid uuid; | |
304 | int rc; | |
305 | ||
306 | rc = string_to_guid(str, &uuid); | |
307 | if (rc) | |
308 | return rc; | |
309 | ||
310 | e->partition_guid = uuid; | |
311 | return 0; | |
312 | } | |
7f539277 | 313 | |
02a376f2 KZ |
314 | static inline int gpt_entry_is_used(const struct gpt_entry *e) |
315 | { | |
316 | return memcmp(&e->type, &GPT_UNUSED_ENTRY_GUID, | |
317 | sizeof(struct gpt_guid)) != 0; | |
318 | } | |
319 | ||
7f539277 | 320 | |
766d5156 DB |
321 | static const char *gpt_get_header_revstr(struct gpt_header *header) |
322 | { | |
323 | if (!header) | |
324 | goto unknown; | |
325 | ||
43a2b094 | 326 | switch (le32_to_cpu(header->revision)) { |
766d5156 DB |
327 | case GPT_HEADER_REVISION_V1_02: |
328 | return "1.2"; | |
329 | case GPT_HEADER_REVISION_V1_00: | |
330 | return "1.0"; | |
331 | case GPT_HEADER_REVISION_V0_99: | |
332 | return "0.99"; | |
333 | default: | |
334 | goto unknown; | |
335 | } | |
336 | ||
337 | unknown: | |
338 | return "unknown"; | |
339 | } | |
340 | ||
b28df75e KZ |
341 | static inline unsigned char *gpt_get_entry_ptr(struct fdisk_gpt_label *gpt, size_t i) |
342 | { | |
343 | return gpt->ents + le32_to_cpu(gpt->pheader->sizeof_partition_entry) * i; | |
344 | } | |
345 | ||
346 | static inline struct gpt_entry *gpt_get_entry(struct fdisk_gpt_label *gpt, size_t i) | |
347 | { | |
348 | return (struct gpt_entry *) gpt_get_entry_ptr(gpt, i); | |
349 | } | |
350 | ||
351 | static inline struct gpt_entry *gpt_zeroize_entry(struct fdisk_gpt_label *gpt, size_t i) | |
352 | { | |
353 | return (struct gpt_entry *) memset(gpt_get_entry_ptr(gpt, i), | |
354 | 0, le32_to_cpu(gpt->pheader->sizeof_partition_entry)); | |
355 | } | |
356 | ||
b683c081 KZ |
357 | /* Use to access array of entries, for() loops, etc. But don't use when |
358 | * you directly do something with GPT header, then use uint32_t. | |
359 | */ | |
360 | static inline size_t gpt_get_nentries(struct fdisk_gpt_label *gpt) | |
361 | { | |
362 | return (size_t) le32_to_cpu(gpt->pheader->npartition_entries); | |
363 | } | |
364 | ||
b1bc5ae3 KZ |
365 | /* calculate size of entries array in bytes for specified nuber of entries */ |
366 | static inline int gpt_calculate_sizeof_entries( | |
367 | struct gpt_header *hdr, | |
368 | uint32_t nents, size_t *sz) | |
9e320545 | 369 | { |
b1bc5ae3 KZ |
370 | uint32_t esz = hdr ? le32_to_cpu(hdr->sizeof_partition_entry) : |
371 | sizeof(struct gpt_entry); | |
9e320545 KZ |
372 | |
373 | if (nents == 0 || esz == 0 || SIZE_MAX/esz < nents) { | |
36cd4b3c | 374 | DBG(GPT, ul_debug("entreis array size check failed")); |
9e320545 KZ |
375 | return -ERANGE; |
376 | } | |
377 | ||
2815af9e | 378 | *sz = (size_t) nents * esz; |
9e320545 KZ |
379 | return 0; |
380 | } | |
381 | ||
b1bc5ae3 KZ |
382 | /* calculate size of entries array in sectors for specified nuber of entries */ |
383 | static inline int gpt_calculate_sectorsof_entries( | |
384 | struct gpt_header *hdr, | |
385 | uint32_t nents, uint64_t *sz, | |
386 | struct fdisk_context *cxt) | |
9e320545 | 387 | { |
b1bc5ae3 KZ |
388 | size_t esz; |
389 | int rc = gpt_calculate_sizeof_entries(hdr, nents, &esz); /* in bytes */ | |
390 | ||
391 | if (rc == 0) | |
392 | *sz = (esz + cxt->sector_size - 1) / cxt->sector_size; | |
393 | return rc; | |
394 | } | |
395 | ||
396 | /* calculate alternative (backup) entries array offset from primary header */ | |
397 | static inline int gpt_calculate_alternative_entries_lba( | |
398 | struct gpt_header *hdr, | |
399 | uint32_t nents, | |
400 | uint64_t *sz, | |
401 | struct fdisk_context *cxt) | |
402 | { | |
403 | uint64_t esects; | |
404 | int rc = gpt_calculate_sectorsof_entries(hdr, nents, &esects, cxt); | |
405 | ||
406 | if (rc == 0) | |
407 | *sz = cxt->total_sectors - 1ULL - esects; | |
408 | return rc; | |
409 | } | |
410 | ||
411 | static inline int gpt_calculate_last_lba( | |
412 | struct gpt_header *hdr, | |
413 | uint32_t nents, | |
414 | uint64_t *sz, | |
415 | struct fdisk_context *cxt) | |
416 | { | |
417 | uint64_t esects; | |
418 | int rc = gpt_calculate_sectorsof_entries(hdr, nents, &esects, cxt); | |
419 | ||
420 | if (rc == 0) | |
421 | *sz = cxt->total_sectors - 2ULL - esects; | |
422 | return rc; | |
423 | } | |
424 | ||
425 | static inline int gpt_calculate_first_lba( | |
426 | struct gpt_header *hdr, | |
427 | uint32_t nents, | |
428 | uint64_t *sz, | |
429 | struct fdisk_context *cxt) | |
430 | { | |
431 | uint64_t esects; | |
432 | int rc = gpt_calculate_sectorsof_entries(hdr, nents, &esects, cxt); | |
433 | ||
434 | if (rc == 0) | |
435 | *sz = esects + 2ULL; | |
436 | return rc; | |
9e320545 KZ |
437 | } |
438 | ||
b1bc5ae3 KZ |
439 | /* the current size of entries array in bytes */ |
440 | static inline int gpt_sizeof_entries(struct gpt_header *hdr, size_t *sz) | |
441 | { | |
442 | return gpt_calculate_sizeof_entries(hdr, le32_to_cpu(hdr->npartition_entries), sz); | |
443 | } | |
766d5156 | 444 | |
5989556a KZ |
445 | static char *gpt_get_header_id(struct gpt_header *header) |
446 | { | |
b443c177 | 447 | char str[UUID_STR_LEN]; |
92e486f8 | 448 | struct gpt_guid guid = header->disk_guid; |
5989556a | 449 | |
92e486f8 | 450 | guid_to_string(&guid, str); |
5989556a KZ |
451 | |
452 | return strdup(str); | |
453 | } | |
454 | ||
3f731001 DB |
455 | /* |
456 | * Builds a clean new valid protective MBR - will wipe out any existing data. | |
457 | * Returns 0 on success, otherwise < 0 on error. | |
458 | */ | |
459 | static int gpt_mknew_pmbr(struct fdisk_context *cxt) | |
460 | { | |
461 | struct gpt_legacy_mbr *pmbr = NULL; | |
7c2cfb18 | 462 | int rc; |
3f731001 DB |
463 | |
464 | if (!cxt || !cxt->firstsector) | |
465 | return -ENOSYS; | |
466 | ||
3457d90e KZ |
467 | if (fdisk_has_protected_bootbits(cxt)) |
468 | rc = fdisk_init_firstsector_buffer(cxt, 0, MBR_PT_BOOTBITS_SIZE); | |
469 | else | |
470 | rc = fdisk_init_firstsector_buffer(cxt, 0, 0); | |
7c2cfb18 KZ |
471 | if (rc) |
472 | return rc; | |
3f731001 DB |
473 | |
474 | pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; | |
8ffa3b65 | 475 | memset(pmbr->partition_record, 0, sizeof(pmbr->partition_record)); |
3f731001 DB |
476 | |
477 | pmbr->signature = cpu_to_le16(MSDOS_MBR_SIGNATURE); | |
478 | pmbr->partition_record[0].os_type = EFI_PMBR_OSTYPE; | |
8ffa3b65 KZ |
479 | pmbr->partition_record[0].start_sector = 2; |
480 | pmbr->partition_record[0].end_head = 0xFF; | |
3f731001 DB |
481 | pmbr->partition_record[0].end_sector = 0xFF; |
482 | pmbr->partition_record[0].end_track = 0xFF; | |
483 | pmbr->partition_record[0].starting_lba = cpu_to_le32(1); | |
484 | pmbr->partition_record[0].size_in_lba = | |
0a7cdf80 | 485 | cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) ); |
3f731001 DB |
486 | |
487 | return 0; | |
488 | } | |
489 | ||
b1bc5ae3 | 490 | |
9a8a3b88 KZ |
491 | /* Move backup header to the end of the device */ |
492 | static void gpt_fix_alternative_lba(struct fdisk_context *cxt, struct fdisk_gpt_label *gpt) | |
493 | { | |
494 | struct gpt_header *p, *b; | |
b1bc5ae3 KZ |
495 | uint64_t x = 0; |
496 | size_t nents; | |
9a8a3b88 KZ |
497 | |
498 | if (!cxt) | |
499 | return; | |
500 | ||
501 | p = gpt->pheader; /* primary */ | |
502 | b = gpt->bheader; /* backup */ | |
503 | ||
b1bc5ae3 | 504 | nents = le32_to_cpu(p->npartition_entries); |
9a8a3b88 KZ |
505 | |
506 | /* reference from primary to backup */ | |
507 | p->alternative_lba = cpu_to_le64(cxt->total_sectors - 1ULL); | |
508 | ||
509 | /* reference from backup to primary */ | |
510 | b->alternative_lba = p->my_lba; | |
511 | b->my_lba = p->alternative_lba; | |
512 | ||
513 | /* fix backup partitions array address */ | |
b1bc5ae3 KZ |
514 | gpt_calculate_alternative_entries_lba(p, nents, &x, cxt); |
515 | b->partition_entry_lba = cpu_to_le64(x); | |
9a8a3b88 KZ |
516 | |
517 | /* update last usable LBA */ | |
b1bc5ae3 KZ |
518 | gpt_calculate_last_lba(p, nents, &x, cxt); |
519 | p->last_usable_lba = cpu_to_le64(x); | |
520 | b->last_usable_lba = cpu_to_le64(x); | |
9a8a3b88 | 521 | |
36cd4b3c | 522 | DBG(GPT, ul_debug("Alternative-LBA updated to: %"PRIu64, le64_to_cpu(p->alternative_lba))); |
9a8a3b88 KZ |
523 | } |
524 | ||
3f731001 DB |
525 | /* some universal differences between the headers */ |
526 | static void gpt_mknew_header_common(struct fdisk_context *cxt, | |
527 | struct gpt_header *header, uint64_t lba) | |
528 | { | |
529 | if (!cxt || !header) | |
530 | return; | |
531 | ||
532 | header->my_lba = cpu_to_le64(lba); | |
533 | ||
b1bc5ae3 KZ |
534 | if (lba == GPT_PRIMARY_PARTITION_TABLE_LBA) { |
535 | /* primary */ | |
0a7cdf80 KZ |
536 | header->alternative_lba = cpu_to_le64(cxt->total_sectors - 1ULL); |
537 | header->partition_entry_lba = cpu_to_le64(2ULL); | |
b1bc5ae3 KZ |
538 | |
539 | } else { | |
540 | /* backup */ | |
541 | uint64_t x = 0; | |
542 | gpt_calculate_alternative_entries_lba(header, | |
543 | le32_to_cpu(header->npartition_entries), &x, cxt); | |
3f731001 DB |
544 | |
545 | header->alternative_lba = cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA); | |
b1bc5ae3 | 546 | header->partition_entry_lba = cpu_to_le64(x); |
3f731001 DB |
547 | } |
548 | } | |
549 | ||
550 | /* | |
551 | * Builds a new GPT header (at sector lba) from a backup header2. | |
552 | * If building a primary header, then backup is the secondary, and vice versa. | |
553 | * | |
554 | * Always pass a new (zeroized) header to build upon as we don't | |
555 | * explicitly zero-set some values such as CRCs and reserved. | |
556 | * | |
557 | * Returns 0 on success, otherwise < 0 on error. | |
558 | */ | |
559 | static int gpt_mknew_header_from_bkp(struct fdisk_context *cxt, | |
560 | struct gpt_header *header, | |
561 | uint64_t lba, | |
562 | struct gpt_header *header2) | |
563 | { | |
564 | if (!cxt || !header || !header2) | |
565 | return -ENOSYS; | |
566 | ||
567 | header->signature = header2->signature; | |
568 | header->revision = header2->revision; | |
569 | header->size = header2->size; | |
570 | header->npartition_entries = header2->npartition_entries; | |
571 | header->sizeof_partition_entry = header2->sizeof_partition_entry; | |
572 | header->first_usable_lba = header2->first_usable_lba; | |
573 | header->last_usable_lba = header2->last_usable_lba; | |
574 | ||
575 | memcpy(&header->disk_guid, | |
576 | &header2->disk_guid, sizeof(header2->disk_guid)); | |
577 | gpt_mknew_header_common(cxt, header, lba); | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
45ddb828 KZ |
582 | static struct gpt_header *gpt_copy_header(struct fdisk_context *cxt, |
583 | struct gpt_header *src) | |
584 | { | |
585 | struct gpt_header *res; | |
586 | ||
587 | if (!cxt || !src) | |
588 | return NULL; | |
589 | ||
4bb82a45 KZ |
590 | assert(cxt->sector_size >= sizeof(struct gpt_header)); |
591 | ||
592 | res = calloc(1, cxt->sector_size); | |
45ddb828 KZ |
593 | if (!res) { |
594 | fdisk_warn(cxt, _("failed to allocate GPT header")); | |
595 | return NULL; | |
596 | } | |
597 | ||
598 | res->my_lba = src->alternative_lba; | |
599 | res->alternative_lba = src->my_lba; | |
600 | ||
601 | res->signature = src->signature; | |
602 | res->revision = src->revision; | |
603 | res->size = src->size; | |
604 | res->npartition_entries = src->npartition_entries; | |
605 | res->sizeof_partition_entry = src->sizeof_partition_entry; | |
606 | res->first_usable_lba = src->first_usable_lba; | |
607 | res->last_usable_lba = src->last_usable_lba; | |
608 | ||
609 | memcpy(&res->disk_guid, &src->disk_guid, sizeof(src->disk_guid)); | |
610 | ||
611 | ||
612 | if (res->my_lba == GPT_PRIMARY_PARTITION_TABLE_LBA) | |
0a7cdf80 | 613 | res->partition_entry_lba = cpu_to_le64(2ULL); |
45ddb828 | 614 | else { |
5eaeb585 | 615 | uint64_t esz = (uint64_t) le32_to_cpu(src->npartition_entries) * sizeof(struct gpt_entry); |
45ddb828 KZ |
616 | uint64_t esects = (esz + cxt->sector_size - 1) / cxt->sector_size; |
617 | ||
0a7cdf80 | 618 | res->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1ULL - esects); |
45ddb828 KZ |
619 | } |
620 | ||
621 | return res; | |
622 | } | |
623 | ||
b7c67e64 KZ |
624 | static int get_script_u64(struct fdisk_context *cxt, uint64_t *num, const char *name) |
625 | { | |
626 | const char *str; | |
627 | int pwr = 0, rc = 0; | |
628 | ||
629 | assert(cxt); | |
630 | ||
631 | *num = 0; | |
632 | ||
633 | if (!cxt->script) | |
634 | return 1; | |
635 | ||
636 | str = fdisk_script_get_header(cxt->script, name); | |
637 | if (!str) | |
638 | return 1; | |
639 | ||
640 | rc = parse_size(str, (uintmax_t *) num, &pwr); | |
641 | if (rc < 0) | |
642 | return rc; | |
643 | if (pwr) | |
644 | *num /= cxt->sector_size; | |
645 | return 0; | |
646 | } | |
647 | ||
648 | static int count_first_last_lba(struct fdisk_context *cxt, | |
1240f549 KZ |
649 | uint64_t *first, uint64_t *last) |
650 | { | |
b7c67e64 KZ |
651 | int rc = 0; |
652 | uint64_t flba, llba; | |
653 | ||
1240f549 | 654 | assert(cxt); |
b7c67e64 KZ |
655 | assert(first); |
656 | assert(last); | |
657 | ||
658 | *first = *last = 0; | |
1240f549 | 659 | |
b7c67e64 | 660 | /* UEFI default */ |
b1bc5ae3 KZ |
661 | gpt_calculate_last_lba(NULL, GPT_NPARTITIONS, &llba, cxt); |
662 | gpt_calculate_first_lba(NULL, GPT_NPARTITIONS, &flba, cxt); | |
1240f549 | 663 | |
b7c67e64 KZ |
664 | /* script default */ |
665 | if (cxt->script) { | |
666 | rc = get_script_u64(cxt, first, "first-lba"); | |
667 | if (rc < 0) | |
668 | return rc; | |
669 | ||
36cd4b3c | 670 | DBG(GPT, ul_debug("FirstLBA: script=%"PRIu64", uefi=%"PRIu64", topology=%ju.", |
fdbd7bb9 | 671 | *first, flba, (uintmax_t)cxt->first_lba)); |
b7c67e64 KZ |
672 | |
673 | if (rc == 0 && (*first < flba || *first > llba)) { | |
674 | fdisk_warnx(cxt, _("First LBA specified by script is out of range.")); | |
675 | return -ERANGE; | |
676 | } | |
677 | ||
678 | rc = get_script_u64(cxt, last, "last-lba"); | |
679 | if (rc < 0) | |
680 | return rc; | |
681 | ||
36cd4b3c | 682 | DBG(GPT, ul_debug("LastLBA: script=%"PRIu64", uefi=%"PRIu64", topology=%ju.", |
fdbd7bb9 | 683 | *last, llba, (uintmax_t)cxt->last_lba)); |
b7c67e64 KZ |
684 | |
685 | if (rc == 0 && (*last > llba || *last < flba)) { | |
686 | fdisk_warnx(cxt, _("Last LBA specified by script is out of range.")); | |
687 | return -ERANGE; | |
688 | } | |
689 | } | |
690 | ||
691 | if (!*last) | |
692 | *last = llba; | |
693 | ||
694 | /* default by topology */ | |
695 | if (!*first) | |
696 | *first = flba < cxt->first_lba && | |
697 | cxt->first_lba < *last ? cxt->first_lba : flba; | |
698 | return 0; | |
1240f549 KZ |
699 | } |
700 | ||
3f731001 DB |
701 | /* |
702 | * Builds a clean new GPT header (currently under revision 1.0). | |
703 | * | |
704 | * Always pass a new (zeroized) header to build upon as we don't | |
705 | * explicitly zero-set some values such as CRCs and reserved. | |
706 | * | |
707 | * Returns 0 on success, otherwise < 0 on error. | |
708 | */ | |
709 | static int gpt_mknew_header(struct fdisk_context *cxt, | |
710 | struct gpt_header *header, uint64_t lba) | |
711 | { | |
1240f549 | 712 | uint64_t first, last; |
b7c67e64 | 713 | int has_id = 0, rc; |
3f731001 DB |
714 | |
715 | if (!cxt || !header) | |
716 | return -ENOSYS; | |
717 | ||
3f731001 DB |
718 | header->signature = cpu_to_le64(GPT_HEADER_SIGNATURE); |
719 | header->revision = cpu_to_le32(GPT_HEADER_REVISION_V1_00); | |
4bb82a45 KZ |
720 | |
721 | /* According to EFI standard it's valid to count all the first | |
722 | * sector into header size, but some tools may have a problem | |
9e930041 | 723 | * to accept it, so use the header without the zeroed area. |
4bb82a45 KZ |
724 | * This does not have any impact to CRC, etc. --kzak Jan-2015 |
725 | */ | |
726 | header->size = cpu_to_le32(sizeof(struct gpt_header) | |
727 | - sizeof(header->reserved2)); | |
3f731001 DB |
728 | |
729 | /* | |
09af3db4 | 730 | * 128 partitions are the default. It can go beyond that, but |
3f731001 DB |
731 | * we're creating a de facto header here, so no funny business. |
732 | */ | |
733 | header->npartition_entries = cpu_to_le32(GPT_NPARTITIONS); | |
734 | header->sizeof_partition_entry = cpu_to_le32(sizeof(struct gpt_entry)); | |
b4184690 | 735 | |
b7c67e64 KZ |
736 | rc = count_first_last_lba(cxt, &first, &last); |
737 | if (rc) | |
738 | return rc; | |
739 | ||
b4184690 KZ |
740 | header->first_usable_lba = cpu_to_le64(first); |
741 | header->last_usable_lba = cpu_to_le64(last); | |
3f731001 DB |
742 | |
743 | gpt_mknew_header_common(cxt, header, lba); | |
3f731001 | 744 | |
4b43f7c9 KZ |
745 | if (cxt->script) { |
746 | const char *id = fdisk_script_get_header(cxt->script, "label-id"); | |
92e486f8 RM |
747 | struct gpt_guid guid = header->disk_guid; |
748 | if (id && string_to_guid(id, &guid) == 0) | |
4b43f7c9 | 749 | has_id = 1; |
92e486f8 | 750 | header->disk_guid = guid; |
4b43f7c9 KZ |
751 | } |
752 | ||
753 | if (!has_id) { | |
92e486f8 RM |
754 | struct gpt_guid guid; |
755 | ||
4b43f7c9 | 756 | uuid_generate_random((unsigned char *) &header->disk_guid); |
92e486f8 RM |
757 | guid = header->disk_guid; |
758 | swap_efi_guid(&guid); | |
4b43f7c9 | 759 | } |
3f731001 DB |
760 | return 0; |
761 | } | |
762 | ||
766d5156 DB |
763 | /* |
764 | * Checks if there is a valid protective MBR partition table. | |
765 | * Returns 0 if it is invalid or failure. Otherwise, return | |
9e930041 | 766 | * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depending on the detection. |
766d5156 DB |
767 | */ |
768 | static int valid_pmbr(struct fdisk_context *cxt) | |
769 | { | |
879fadf1 | 770 | int i, part = 0, ret = 0; /* invalid by default */ |
766d5156 DB |
771 | struct gpt_legacy_mbr *pmbr = NULL; |
772 | ||
773 | if (!cxt->firstsector) | |
774 | goto done; | |
775 | ||
776 | pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; | |
777 | ||
f67c524e | 778 | if (le16_to_cpu(pmbr->signature) != MSDOS_MBR_SIGNATURE) |
766d5156 DB |
779 | goto done; |
780 | ||
766d5156 | 781 | /* seems like a valid MBR was found, check DOS primary partitions */ |
f67c524e | 782 | for (i = 0; i < 4; i++) { |
766d5156 DB |
783 | if (pmbr->partition_record[i].os_type == EFI_PMBR_OSTYPE) { |
784 | /* | |
785 | * Ok, we at least know that there's a protective MBR, | |
786 | * now check if there are other partition types for | |
787 | * hybrid MBR. | |
788 | */ | |
879fadf1 | 789 | part = i; |
766d5156 | 790 | ret = GPT_MBR_PROTECTIVE; |
c6bf5c09 | 791 | break; |
766d5156 | 792 | } |
f67c524e | 793 | } |
ac920fed | 794 | |
766d5156 DB |
795 | if (ret != GPT_MBR_PROTECTIVE) |
796 | goto done; | |
c6bf5c09 | 797 | |
c6bf5c09 | 798 | |
f67c524e | 799 | for (i = 0 ; i < 4; i++) { |
766d5156 | 800 | if ((pmbr->partition_record[i].os_type != EFI_PMBR_OSTYPE) && |
1d844703 | 801 | (pmbr->partition_record[i].os_type != 0x00)) { |
766d5156 | 802 | ret = GPT_MBR_HYBRID; |
1d844703 KZ |
803 | goto done; |
804 | } | |
f67c524e | 805 | } |
766d5156 | 806 | |
1d844703 KZ |
807 | /* LBA of the GPT partition header */ |
808 | if (pmbr->partition_record[part].starting_lba != | |
809 | cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA)) | |
810 | goto done; | |
811 | ||
766d5156 DB |
812 | /* |
813 | * Protective MBRs take up the lesser of the whole disk | |
814 | * or 2 TiB (32bit LBA), ignoring the rest of the disk. | |
1fd10841 DB |
815 | * Some partitioning programs, nonetheless, choose to set |
816 | * the size to the maximum 32-bit limitation, disregarding | |
817 | * the disk size. | |
766d5156 DB |
818 | * |
819 | * Hybrid MBRs do not necessarily comply with this. | |
59db52ad KZ |
820 | * |
821 | * Consider a bad value here to be a warning to support dd-ing | |
822 | * an image from a smaller disk to a bigger disk. | |
766d5156 | 823 | */ |
f67c524e | 824 | if (ret == GPT_MBR_PROTECTIVE) { |
0a7cdf80 KZ |
825 | uint64_t sz_lba = (uint64_t) le32_to_cpu(pmbr->partition_record[part].size_in_lba); |
826 | if (sz_lba != cxt->total_sectors - 1ULL && sz_lba != 0xFFFFFFFFULL) { | |
9a8a3b88 | 827 | |
0a7cdf80 | 828 | fdisk_warnx(cxt, _("GPT PMBR size mismatch (%"PRIu64" != %"PRIu64") " |
9a8a3b88 | 829 | "will be corrected by write."), |
0a7cdf80 | 830 | sz_lba, cxt->total_sectors - 1ULL); |
9a8a3b88 | 831 | |
2bb3aa36 | 832 | /* Note that gpt_write_pmbr() overwrites PMBR, but we want to keep it valid already |
9a8a3b88 KZ |
833 | * in memory too to disable warnings when valid_pmbr() called next time */ |
834 | pmbr->partition_record[part].size_in_lba = | |
835 | cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) ); | |
1572fb3e | 836 | fdisk_label_set_changed(cxt->label, 1); |
59db52ad | 837 | } |
f67c524e | 838 | } |
766d5156 | 839 | done: |
36cd4b3c | 840 | DBG(GPT, ul_debug("PMBR type: %s", |
1d844703 KZ |
841 | ret == GPT_MBR_PROTECTIVE ? "protective" : |
842 | ret == GPT_MBR_HYBRID ? "hybrid" : "???" )); | |
766d5156 DB |
843 | return ret; |
844 | } | |
845 | ||
846 | static uint64_t last_lba(struct fdisk_context *cxt) | |
5dbff4c0 | 847 | { |
5dbff4c0 | 848 | struct stat s; |
cbebd20d | 849 | uint64_t sectors = 0; |
5dbff4c0 | 850 | |
766d5156 DB |
851 | memset(&s, 0, sizeof(s)); |
852 | if (fstat(cxt->dev_fd, &s) == -1) { | |
83df5feb | 853 | fdisk_warn(cxt, _("gpt: stat() failed")); |
5dbff4c0 KZ |
854 | return 0; |
855 | } | |
766d5156 | 856 | |
5dbff4c0 | 857 | if (S_ISBLK(s.st_mode)) |
0a7cdf80 | 858 | sectors = cxt->total_sectors - 1ULL; |
cbebd20d KZ |
859 | else if (S_ISREG(s.st_mode)) |
860 | sectors = ((uint64_t) s.st_size / | |
861 | (uint64_t) cxt->sector_size) - 1ULL; | |
862 | else | |
83df5feb | 863 | fdisk_warnx(cxt, _("gpt: cannot handle files with mode %o"), s.st_mode); |
cbebd20d | 864 | |
36cd4b3c | 865 | DBG(GPT, ul_debug("last LBA: %"PRIu64"", sectors)); |
cbebd20d | 866 | return sectors; |
5dbff4c0 KZ |
867 | } |
868 | ||
766d5156 DB |
869 | static ssize_t read_lba(struct fdisk_context *cxt, uint64_t lba, |
870 | void *buffer, const size_t bytes) | |
5dbff4c0 | 871 | { |
766d5156 | 872 | off_t offset = lba * cxt->sector_size; |
5dbff4c0 | 873 | |
bbe8e6a9 KZ |
874 | if (lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1) |
875 | return -1; | |
b9710f1f | 876 | return (size_t)read(cxt->dev_fd, buffer, bytes) != bytes; |
5dbff4c0 KZ |
877 | } |
878 | ||
766d5156 DB |
879 | |
880 | /* Returns the GPT entry array */ | |
b28df75e | 881 | static unsigned char *gpt_read_entries(struct fdisk_context *cxt, |
d71ef5a4 | 882 | struct gpt_header *header) |
5dbff4c0 | 883 | { |
b9144a43 | 884 | size_t sz = 0; |
9e320545 KZ |
885 | ssize_t ssz; |
886 | ||
b28df75e | 887 | unsigned char *ret = NULL; |
d71ef5a4 KZ |
888 | off_t offset; |
889 | ||
890 | assert(cxt); | |
891 | assert(header); | |
892 | ||
b1bc5ae3 | 893 | if (gpt_sizeof_entries(header, &sz)) |
f71b96bf | 894 | return NULL; |
f71b96bf | 895 | |
a8294f40 | 896 | if (sz > (size_t) SSIZE_MAX) { |
36cd4b3c | 897 | DBG(GPT, ul_debug("entries array too large to read()")); |
a8294f40 KZ |
898 | return NULL; |
899 | } | |
900 | ||
46667ba4 | 901 | ret = calloc(1, sz); |
d71ef5a4 KZ |
902 | if (!ret) |
903 | return NULL; | |
9e320545 | 904 | |
5eaeb585 | 905 | offset = (off_t) le64_to_cpu(header->partition_entry_lba) * |
766d5156 DB |
906 | cxt->sector_size; |
907 | ||
908 | if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) | |
d71ef5a4 | 909 | goto fail; |
9e320545 KZ |
910 | |
911 | ssz = read(cxt->dev_fd, ret, sz); | |
912 | if (ssz < 0 || (size_t) ssz != sz) | |
d71ef5a4 | 913 | goto fail; |
766d5156 DB |
914 | |
915 | return ret; | |
d71ef5a4 KZ |
916 | |
917 | fail: | |
918 | free(ret); | |
919 | return NULL; | |
766d5156 DB |
920 | } |
921 | ||
7020de0b KZ |
922 | static inline uint32_t count_crc32(const unsigned char *buf, size_t len, |
923 | size_t ex_off, size_t ex_len) | |
766d5156 | 924 | { |
7f0d4d56 | 925 | return (ul_crc32_exclude_offset(~0L, buf, len, ex_off, ex_len) ^ ~0L); |
766d5156 DB |
926 | } |
927 | ||
7020de0b KZ |
928 | static inline uint32_t gpt_header_count_crc32(struct gpt_header *header) |
929 | { | |
930 | return count_crc32((unsigned char *) header, /* buffer */ | |
931 | le32_to_cpu(header->size), /* size of buffer */ | |
932 | offsetof(struct gpt_header, crc32), /* exclude */ | |
933 | sizeof(header->crc32)); /* size of excluded area */ | |
934 | } | |
935 | ||
b28df75e | 936 | static inline uint32_t gpt_entryarr_count_crc32(struct gpt_header *header, unsigned char *ents) |
7020de0b KZ |
937 | { |
938 | size_t arysz = 0; | |
939 | ||
b1bc5ae3 | 940 | if (gpt_sizeof_entries(header, &arysz)) |
9e320545 | 941 | return 0; |
7020de0b | 942 | |
b28df75e | 943 | return count_crc32(ents, arysz, 0, 0); |
7020de0b KZ |
944 | } |
945 | ||
946 | ||
766d5156 DB |
947 | /* |
948 | * Recompute header and partition array 32bit CRC checksums. | |
949 | * This function does not fail - if there's corruption, then it | |
9e930041 | 950 | * will be reported when checksumming it again (ie: probing or verify). |
766d5156 | 951 | */ |
b28df75e | 952 | static void gpt_recompute_crc(struct gpt_header *header, unsigned char *ents) |
766d5156 | 953 | { |
766d5156 DB |
954 | if (!header) |
955 | return; | |
956 | ||
7020de0b KZ |
957 | header->partition_entry_array_crc32 = |
958 | cpu_to_le32( gpt_entryarr_count_crc32(header, ents) ); | |
766d5156 | 959 | |
7020de0b | 960 | header->crc32 = cpu_to_le32( gpt_header_count_crc32(header) ); |
766d5156 DB |
961 | } |
962 | ||
963 | /* | |
964 | * Compute the 32bit CRC checksum of the partition table header. | |
965 | * Returns 1 if it is valid, otherwise 0. | |
966 | */ | |
b28df75e | 967 | static int gpt_check_header_crc(struct gpt_header *header, unsigned char *ents) |
766d5156 | 968 | { |
7020de0b KZ |
969 | uint32_t orgcrc = le32_to_cpu(header->crc32), |
970 | crc = gpt_header_count_crc32(header); | |
766d5156 | 971 | |
7020de0b | 972 | if (crc == orgcrc) |
d71ef5a4 KZ |
973 | return 1; |
974 | ||
766d5156 | 975 | /* |
7020de0b KZ |
976 | * If we have checksum mismatch it may be due to stale data, like a |
977 | * partition being added or deleted. Recompute the CRC again and make | |
978 | * sure this is not the case. | |
766d5156 | 979 | */ |
d71ef5a4 | 980 | if (ents) { |
766d5156 | 981 | gpt_recompute_crc(header, ents); |
7020de0b | 982 | return gpt_header_count_crc32(header) == orgcrc; |
d71ef5a4 KZ |
983 | } |
984 | ||
985 | return 0; | |
766d5156 DB |
986 | } |
987 | ||
988 | /* | |
989 | * It initializes the partition entry array. | |
990 | * Returns 1 if the checksum is valid, otherwise 0. | |
991 | */ | |
b28df75e | 992 | static int gpt_check_entryarr_crc(struct gpt_header *header, unsigned char *ents) |
766d5156 | 993 | { |
d71ef5a4 | 994 | if (!header || !ents) |
7020de0b | 995 | return 0; |
766d5156 | 996 | |
7020de0b KZ |
997 | return gpt_entryarr_count_crc32(header, ents) == |
998 | le32_to_cpu(header->partition_entry_array_crc32); | |
766d5156 DB |
999 | } |
1000 | ||
1001 | static int gpt_check_lba_sanity(struct fdisk_context *cxt, struct gpt_header *header) | |
1002 | { | |
1003 | int ret = 0; | |
1004 | uint64_t lu, fu, lastlba = last_lba(cxt); | |
1005 | ||
1006 | fu = le64_to_cpu(header->first_usable_lba); | |
1007 | lu = le64_to_cpu(header->last_usable_lba); | |
1008 | ||
1009 | /* check if first and last usable LBA make sense */ | |
1010 | if (lu < fu) { | |
36cd4b3c | 1011 | DBG(GPT, ul_debug("error: header last LBA is before first LBA")); |
766d5156 | 1012 | goto done; |
5dbff4c0 | 1013 | } |
766d5156 DB |
1014 | |
1015 | /* check if first and last usable LBAs with the disk's last LBA */ | |
1016 | if (fu > lastlba || lu > lastlba) { | |
36cd4b3c | 1017 | DBG(GPT, ul_debug("error: header LBAs are after the disk's last LBA")); |
766d5156 DB |
1018 | goto done; |
1019 | } | |
1020 | ||
1021 | /* the header has to be outside usable range */ | |
1022 | if (fu < GPT_PRIMARY_PARTITION_TABLE_LBA && | |
1023 | GPT_PRIMARY_PARTITION_TABLE_LBA < lu) { | |
36cd4b3c | 1024 | DBG(GPT, ul_debug("error: header outside of usable range")); |
766d5156 DB |
1025 | goto done; |
1026 | } | |
1027 | ||
1028 | ret = 1; /* sane */ | |
1029 | done: | |
1030 | return ret; | |
1031 | } | |
1032 | ||
1033 | /* Check if there is a valid header signature */ | |
1034 | static int gpt_check_signature(struct gpt_header *header) | |
1035 | { | |
1036 | return header->signature == cpu_to_le64(GPT_HEADER_SIGNATURE); | |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * Return the specified GPT Header, or NULL upon failure/invalid. | |
1041 | * Note that all tests must pass to ensure a valid header, | |
1042 | * we do not rely on only testing the signature for a valid probe. | |
1043 | */ | |
d71ef5a4 KZ |
1044 | static struct gpt_header *gpt_read_header(struct fdisk_context *cxt, |
1045 | uint64_t lba, | |
b28df75e | 1046 | unsigned char **_ents) |
766d5156 DB |
1047 | { |
1048 | struct gpt_header *header = NULL; | |
b28df75e | 1049 | unsigned char *ents = NULL; |
e9bf0935 | 1050 | uint32_t hsz; |
766d5156 DB |
1051 | |
1052 | if (!cxt) | |
1053 | return NULL; | |
1054 | ||
4bb82a45 KZ |
1055 | /* always allocate all sector, the area after GPT header |
1056 | * has to be fill by zeros */ | |
1057 | assert(cxt->sector_size >= sizeof(struct gpt_header)); | |
1058 | ||
1059 | header = calloc(1, cxt->sector_size); | |
46667ba4 KZ |
1060 | if (!header) |
1061 | return NULL; | |
766d5156 | 1062 | |
d71ef5a4 | 1063 | /* read and verify header */ |
4bb82a45 | 1064 | if (read_lba(cxt, lba, header, cxt->sector_size) != 0) |
766d5156 DB |
1065 | goto invalid; |
1066 | ||
1067 | if (!gpt_check_signature(header)) | |
1068 | goto invalid; | |
1069 | ||
9c6f3de6 KZ |
1070 | /* make sure header size is between 92 and sector size bytes */ |
1071 | hsz = le32_to_cpu(header->size); | |
1072 | if (hsz < GPT_HEADER_MINSZ || hsz > cxt->sector_size) | |
1073 | goto invalid; | |
1074 | ||
d71ef5a4 KZ |
1075 | if (!gpt_check_header_crc(header, NULL)) |
1076 | goto invalid; | |
1077 | ||
1078 | /* read and verify entries */ | |
1079 | ents = gpt_read_entries(cxt, header); | |
1080 | if (!ents) | |
1081 | goto invalid; | |
1082 | ||
1083 | if (!gpt_check_entryarr_crc(header, ents)) | |
766d5156 DB |
1084 | goto invalid; |
1085 | ||
1086 | if (!gpt_check_lba_sanity(cxt, header)) | |
1087 | goto invalid; | |
1088 | ||
1089 | /* valid header must be at MyLBA */ | |
1090 | if (le64_to_cpu(header->my_lba) != lba) | |
1091 | goto invalid; | |
1092 | ||
d71ef5a4 KZ |
1093 | if (_ents) |
1094 | *_ents = ents; | |
1095 | else | |
1096 | free(ents); | |
1097 | ||
36cd4b3c | 1098 | DBG(GPT, ul_debug("found valid header on LBA %"PRIu64"", lba)); |
766d5156 DB |
1099 | return header; |
1100 | invalid: | |
1101 | free(header); | |
d71ef5a4 | 1102 | free(ents); |
45ddb828 | 1103 | |
36cd4b3c | 1104 | DBG(GPT, ul_debug("read header on LBA %"PRIu64" failed", lba)); |
766d5156 DB |
1105 | return NULL; |
1106 | } | |
1107 | ||
775001ad KZ |
1108 | |
1109 | static int gpt_locate_disklabel(struct fdisk_context *cxt, int n, | |
9bbcf43f | 1110 | const char **name, uint64_t *offset, size_t *size) |
775001ad KZ |
1111 | { |
1112 | struct fdisk_gpt_label *gpt; | |
1113 | ||
1114 | assert(cxt); | |
1115 | ||
1116 | *name = NULL; | |
1117 | *offset = 0; | |
1118 | *size = 0; | |
1119 | ||
1120 | switch (n) { | |
1121 | case 0: | |
1122 | *name = "PMBR"; | |
1123 | *offset = 0; | |
1124 | *size = 512; | |
1125 | break; | |
1126 | case 1: | |
1127 | *name = _("GPT Header"); | |
9bbcf43f | 1128 | *offset = (uint64_t) GPT_PRIMARY_PARTITION_TABLE_LBA * cxt->sector_size; |
775001ad KZ |
1129 | *size = sizeof(struct gpt_header); |
1130 | break; | |
1131 | case 2: | |
1132 | *name = _("GPT Entries"); | |
1133 | gpt = self_label(cxt); | |
5eaeb585 KZ |
1134 | *offset = (uint64_t) le64_to_cpu(gpt->pheader->partition_entry_lba) * |
1135 | cxt->sector_size; | |
b1bc5ae3 | 1136 | return gpt_sizeof_entries(gpt->pheader, size); |
775001ad KZ |
1137 | default: |
1138 | return 1; /* no more chunks */ | |
1139 | } | |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
5989556a KZ |
1144 | static int gpt_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labelitem *item) |
1145 | { | |
1146 | struct gpt_header *h; | |
1147 | int rc = 0; | |
1148 | ||
1149 | assert(cxt); | |
1150 | assert(cxt->label); | |
1151 | assert(fdisk_is_label(cxt, GPT)); | |
1152 | ||
1153 | h = self_label(cxt)->pheader; | |
775001ad | 1154 | |
5989556a KZ |
1155 | switch (item->id) { |
1156 | case GPT_LABELITEM_ID: | |
1157 | item->name = _("Disk identifier"); | |
1158 | item->type = 's'; | |
1159 | item->data.str = gpt_get_header_id(h); | |
1160 | if (!item->data.str) | |
1161 | rc = -ENOMEM; | |
1162 | break; | |
1163 | case GPT_LABELITEM_FIRSTLBA: | |
1164 | item->name = _("First LBA"); | |
1165 | item->type = 'j'; | |
1166 | item->data.num64 = le64_to_cpu(h->first_usable_lba); | |
1167 | break; | |
1168 | case GPT_LABELITEM_LASTLBA: | |
1169 | item->name = _("Last LBA"); | |
1170 | item->type = 'j'; | |
1171 | item->data.num64 = le64_to_cpu(h->last_usable_lba); | |
1172 | break; | |
1173 | case GPT_LABELITEM_ALTLBA: | |
1174 | /* TRANSLATORS: The LBA (Logical Block Address) of the backup GPT header. */ | |
1175 | item->name = _("Alternative LBA"); | |
1176 | item->type = 'j'; | |
1177 | item->data.num64 = le64_to_cpu(h->alternative_lba); | |
1178 | break; | |
1179 | case GPT_LABELITEM_ENTRIESLBA: | |
1180 | /* TRANSLATORS: The start of the array of partition entries. */ | |
1181 | item->name = _("Partition entries LBA"); | |
1182 | item->type = 'j'; | |
1183 | item->data.num64 = le64_to_cpu(h->partition_entry_lba); | |
1184 | break; | |
1185 | case GPT_LABELITEM_ENTRIESALLOC: | |
1186 | item->name = _("Allocated partition entries"); | |
1187 | item->type = 'j'; | |
1188 | item->data.num64 = le32_to_cpu(h->npartition_entries); | |
1189 | break; | |
1190 | default: | |
1191 | if (item->id < __FDISK_NLABELITEMS) | |
9e930041 | 1192 | rc = 1; /* unsupported generic item */ |
5989556a KZ |
1193 | else |
1194 | rc = 2; /* out of range */ | |
1195 | break; | |
1196 | } | |
1197 | ||
1198 | return rc; | |
1199 | } | |
775001ad | 1200 | |
766d5156 DB |
1201 | /* |
1202 | * Returns the number of partitions that are in use. | |
1203 | */ | |
b683c081 | 1204 | static size_t partitions_in_use(struct fdisk_gpt_label *gpt) |
766d5156 | 1205 | { |
b683c081 | 1206 | size_t i, used = 0; |
766d5156 | 1207 | |
b28df75e KZ |
1208 | assert(gpt); |
1209 | assert(gpt->pheader); | |
1210 | assert(gpt->ents); | |
1211 | ||
b683c081 | 1212 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e | 1213 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
766d5156 | 1214 | |
02a376f2 | 1215 | if (gpt_entry_is_used(e)) |
766d5156 | 1216 | used++; |
b28df75e | 1217 | } |
766d5156 DB |
1218 | return used; |
1219 | } | |
1220 | ||
766d5156 DB |
1221 | |
1222 | /* | |
1223 | * Check if a partition is too big for the disk (sectors). | |
1224 | * Returns the faulting partition number, otherwise 0. | |
1225 | */ | |
b28df75e | 1226 | static uint32_t check_too_big_partitions(struct fdisk_gpt_label *gpt, uint64_t sectors) |
766d5156 | 1227 | { |
b683c081 | 1228 | size_t i; |
766d5156 | 1229 | |
b28df75e KZ |
1230 | assert(gpt); |
1231 | assert(gpt->pheader); | |
1232 | assert(gpt->ents); | |
1233 | ||
b683c081 | 1234 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e KZ |
1235 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
1236 | ||
02a376f2 | 1237 | if (!gpt_entry_is_used(e)) |
766d5156 | 1238 | continue; |
b28df75e | 1239 | if (gpt_partition_end(e) >= sectors) |
766d5156 | 1240 | return i + 1; |
5dbff4c0 | 1241 | } |
766d5156 DB |
1242 | |
1243 | return 0; | |
5dbff4c0 KZ |
1244 | } |
1245 | ||
766d5156 DB |
1246 | /* |
1247 | * Check if a partition ends before it begins | |
1248 | * Returns the faulting partition number, otherwise 0. | |
5dbff4c0 | 1249 | */ |
b28df75e | 1250 | static uint32_t check_start_after_end_partitions(struct fdisk_gpt_label *gpt) |
5dbff4c0 | 1251 | { |
b683c081 | 1252 | size_t i; |
5dbff4c0 | 1253 | |
b28df75e KZ |
1254 | assert(gpt); |
1255 | assert(gpt->pheader); | |
1256 | assert(gpt->ents); | |
1257 | ||
b683c081 | 1258 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e KZ |
1259 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
1260 | ||
02a376f2 | 1261 | if (!gpt_entry_is_used(e)) |
766d5156 | 1262 | continue; |
b28df75e | 1263 | if (gpt_partition_start(e) > gpt_partition_end(e)) |
766d5156 | 1264 | return i + 1; |
5dbff4c0 | 1265 | } |
766d5156 DB |
1266 | |
1267 | return 0; | |
5dbff4c0 KZ |
1268 | } |
1269 | ||
766d5156 | 1270 | /* |
09af3db4 | 1271 | * Check if partition e1 overlaps with partition e2. |
766d5156 | 1272 | */ |
874aa9c3 | 1273 | static inline int partition_overlap(struct gpt_entry *e1, struct gpt_entry *e2) |
5dbff4c0 | 1274 | { |
874aa9c3 KZ |
1275 | uint64_t start1 = gpt_partition_start(e1); |
1276 | uint64_t end1 = gpt_partition_end(e1); | |
1277 | uint64_t start2 = gpt_partition_start(e2); | |
1278 | uint64_t end2 = gpt_partition_end(e2); | |
1279 | ||
1280 | return (start1 && start2 && (start1 <= end2) != (end1 < start2)); | |
766d5156 DB |
1281 | } |
1282 | ||
1283 | /* | |
09af3db4 | 1284 | * Find any partitions that overlap. |
766d5156 | 1285 | */ |
b28df75e | 1286 | static uint32_t check_overlap_partitions(struct fdisk_gpt_label *gpt) |
766d5156 | 1287 | { |
b683c081 | 1288 | size_t i, j; |
766d5156 | 1289 | |
b28df75e KZ |
1290 | assert(gpt); |
1291 | assert(gpt->pheader); | |
1292 | assert(gpt->ents); | |
1293 | ||
b683c081 | 1294 | for (i = 0; i < gpt_get_nentries(gpt); i++) |
766d5156 | 1295 | for (j = 0; j < i; j++) { |
b28df75e KZ |
1296 | struct gpt_entry *ei = gpt_get_entry(gpt, i); |
1297 | struct gpt_entry *ej = gpt_get_entry(gpt, j); | |
1298 | ||
02a376f2 | 1299 | if (!gpt_entry_is_used(ei) || !gpt_entry_is_used(ej)) |
766d5156 | 1300 | continue; |
b28df75e | 1301 | if (partition_overlap(ei, ej)) { |
36cd4b3c | 1302 | DBG(GPT, ul_debug("partitions overlap detected [%zu vs. %zu]", i, j)); |
766d5156 | 1303 | return i + 1; |
c15aec86 | 1304 | } |
766d5156 DB |
1305 | } |
1306 | ||
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | /* | |
1311 | * Find the first available block after the starting point; returns 0 if | |
1312 | * there are no available blocks left, or error. From gdisk. | |
1313 | */ | |
b28df75e | 1314 | static uint64_t find_first_available(struct fdisk_gpt_label *gpt, uint64_t start) |
766d5156 | 1315 | { |
b683c081 | 1316 | int first_moved = 0; |
766d5156 | 1317 | uint64_t first; |
602ebe7d KZ |
1318 | uint64_t fu, lu; |
1319 | ||
b28df75e KZ |
1320 | assert(gpt); |
1321 | assert(gpt->pheader); | |
1322 | assert(gpt->ents); | |
766d5156 | 1323 | |
b28df75e KZ |
1324 | fu = le64_to_cpu(gpt->pheader->first_usable_lba); |
1325 | lu = le64_to_cpu(gpt->pheader->last_usable_lba); | |
602ebe7d | 1326 | |
766d5156 DB |
1327 | /* |
1328 | * Begin from the specified starting point or from the first usable | |
1329 | * LBA, whichever is greater... | |
1330 | */ | |
602ebe7d | 1331 | first = start < fu ? fu : start; |
766d5156 DB |
1332 | |
1333 | /* | |
1334 | * Now search through all partitions; if first is within an | |
1335 | * existing partition, move it to the next sector after that | |
1336 | * partition and repeat. If first was moved, set firstMoved | |
1337 | * flag; repeat until firstMoved is not set, so as to catch | |
1338 | * cases where partitions are out of sequential order.... | |
1339 | */ | |
1340 | do { | |
b683c081 KZ |
1341 | size_t i; |
1342 | ||
766d5156 | 1343 | first_moved = 0; |
b683c081 | 1344 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e KZ |
1345 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
1346 | ||
02a376f2 | 1347 | if (!gpt_entry_is_used(e)) |
766d5156 | 1348 | continue; |
b28df75e | 1349 | if (first < gpt_partition_start(e)) |
766d5156 | 1350 | continue; |
b28df75e KZ |
1351 | if (first <= gpt_partition_end(e)) { |
1352 | first = gpt_partition_end(e) + 1; | |
766d5156 DB |
1353 | first_moved = 1; |
1354 | } | |
1355 | } | |
1356 | } while (first_moved == 1); | |
1357 | ||
602ebe7d | 1358 | if (first > lu) |
766d5156 DB |
1359 | first = 0; |
1360 | ||
1361 | return first; | |
5dbff4c0 KZ |
1362 | } |
1363 | ||
766d5156 DB |
1364 | |
1365 | /* Returns last available sector in the free space pointed to by start. From gdisk. */ | |
b28df75e | 1366 | static uint64_t find_last_free(struct fdisk_gpt_label *gpt, uint64_t start) |
5dbff4c0 | 1367 | { |
b683c081 | 1368 | size_t i; |
766d5156 DB |
1369 | uint64_t nearest_start; |
1370 | ||
b28df75e KZ |
1371 | assert(gpt); |
1372 | assert(gpt->pheader); | |
1373 | assert(gpt->ents); | |
766d5156 | 1374 | |
b28df75e | 1375 | nearest_start = le64_to_cpu(gpt->pheader->last_usable_lba); |
602ebe7d | 1376 | |
b683c081 | 1377 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e KZ |
1378 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
1379 | uint64_t ps = gpt_partition_start(e); | |
602ebe7d KZ |
1380 | |
1381 | if (nearest_start > ps && ps > start) | |
0a7cdf80 | 1382 | nearest_start = ps - 1ULL; |
5dbff4c0 | 1383 | } |
766d5156 DB |
1384 | |
1385 | return nearest_start; | |
5dbff4c0 | 1386 | } |
766d5156 DB |
1387 | |
1388 | /* Returns the last free sector on the disk. From gdisk. */ | |
b28df75e | 1389 | static uint64_t find_last_free_sector(struct fdisk_gpt_label *gpt) |
766d5156 | 1390 | { |
b683c081 | 1391 | int last_moved; |
766d5156 DB |
1392 | uint64_t last = 0; |
1393 | ||
b28df75e KZ |
1394 | assert(gpt); |
1395 | assert(gpt->pheader); | |
1396 | assert(gpt->ents); | |
766d5156 DB |
1397 | |
1398 | /* start by assuming the last usable LBA is available */ | |
b28df75e | 1399 | last = le64_to_cpu(gpt->pheader->last_usable_lba); |
766d5156 | 1400 | do { |
b683c081 KZ |
1401 | size_t i; |
1402 | ||
766d5156 | 1403 | last_moved = 0; |
b683c081 | 1404 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e KZ |
1405 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
1406 | ||
1407 | if (last >= gpt_partition_start(e) && | |
1408 | last <= gpt_partition_end(e)) { | |
1409 | last = gpt_partition_start(e) - 1ULL; | |
766d5156 DB |
1410 | last_moved = 1; |
1411 | } | |
1412 | } | |
1413 | } while (last_moved == 1); | |
b28df75e | 1414 | |
766d5156 DB |
1415 | return last; |
1416 | } | |
1417 | ||
1418 | /* | |
1419 | * Finds the first available sector in the largest block of unallocated | |
1420 | * space on the disk. Returns 0 if there are no available blocks left. | |
1421 | * From gdisk. | |
1422 | */ | |
b28df75e | 1423 | static uint64_t find_first_in_largest(struct fdisk_gpt_label *gpt) |
766d5156 DB |
1424 | { |
1425 | uint64_t start = 0, first_sect, last_sect; | |
1426 | uint64_t segment_size, selected_size = 0, selected_segment = 0; | |
1427 | ||
b28df75e KZ |
1428 | assert(gpt); |
1429 | assert(gpt->pheader); | |
1430 | assert(gpt->ents); | |
766d5156 DB |
1431 | |
1432 | do { | |
b28df75e | 1433 | first_sect = find_first_available(gpt, start); |
766d5156 | 1434 | if (first_sect != 0) { |
b28df75e | 1435 | last_sect = find_last_free(gpt, first_sect); |
0a7cdf80 | 1436 | segment_size = last_sect - first_sect + 1ULL; |
766d5156 DB |
1437 | |
1438 | if (segment_size > selected_size) { | |
1439 | selected_size = segment_size; | |
1440 | selected_segment = first_sect; | |
1441 | } | |
0a7cdf80 | 1442 | start = last_sect + 1ULL; |
766d5156 DB |
1443 | } |
1444 | } while (first_sect != 0); | |
1445 | ||
766d5156 DB |
1446 | return selected_segment; |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Find the total number of free sectors, the number of segments in which | |
1451 | * they reside, and the size of the largest of those segments. From gdisk. | |
1452 | */ | |
b28df75e KZ |
1453 | static uint64_t get_free_sectors(struct fdisk_context *cxt, |
1454 | struct fdisk_gpt_label *gpt, | |
1455 | uint32_t *nsegments, | |
766d5156 DB |
1456 | uint64_t *largest_segment) |
1457 | { | |
1458 | uint32_t num = 0; | |
1459 | uint64_t first_sect, last_sect; | |
1460 | uint64_t largest_seg = 0, segment_sz; | |
1461 | uint64_t totfound = 0, start = 0; /* starting point for each search */ | |
1462 | ||
1463 | if (!cxt->total_sectors) | |
1464 | goto done; | |
1465 | ||
b28df75e KZ |
1466 | assert(gpt); |
1467 | assert(gpt->pheader); | |
1468 | assert(gpt->ents); | |
1469 | ||
766d5156 | 1470 | do { |
b28df75e | 1471 | first_sect = find_first_available(gpt, start); |
766d5156 | 1472 | if (first_sect) { |
b28df75e | 1473 | last_sect = find_last_free(gpt, first_sect); |
766d5156 DB |
1474 | segment_sz = last_sect - first_sect + 1; |
1475 | ||
1476 | if (segment_sz > largest_seg) | |
1477 | largest_seg = segment_sz; | |
1478 | totfound += segment_sz; | |
1479 | num++; | |
0a7cdf80 | 1480 | start = last_sect + 1ULL; |
766d5156 DB |
1481 | } |
1482 | } while (first_sect); | |
1483 | ||
1484 | done: | |
512a430f KZ |
1485 | if (nsegments) |
1486 | *nsegments = num; | |
1487 | if (largest_segment) | |
1488 | *largest_segment = largest_seg; | |
766d5156 DB |
1489 | |
1490 | return totfound; | |
1491 | } | |
1492 | ||
9ffeb235 | 1493 | static int gpt_probe_label(struct fdisk_context *cxt) |
766d5156 DB |
1494 | { |
1495 | int mbr_type; | |
9ffeb235 | 1496 | struct fdisk_gpt_label *gpt; |
766d5156 | 1497 | |
9ffeb235 KZ |
1498 | assert(cxt); |
1499 | assert(cxt->label); | |
aa36c2cf | 1500 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 KZ |
1501 | |
1502 | gpt = self_label(cxt); | |
766d5156 | 1503 | |
d2d9efa1 KZ |
1504 | /* TODO: it would be nice to support scenario when GPT headers are OK, |
1505 | * but PMBR is corrupt */ | |
766d5156 DB |
1506 | mbr_type = valid_pmbr(cxt); |
1507 | if (!mbr_type) | |
1508 | goto failed; | |
1509 | ||
d71ef5a4 KZ |
1510 | /* primary header */ |
1511 | gpt->pheader = gpt_read_header(cxt, GPT_PRIMARY_PARTITION_TABLE_LBA, | |
1512 | &gpt->ents); | |
45ddb828 KZ |
1513 | |
1514 | if (gpt->pheader) | |
1515 | /* primary OK, try backup from alternative LBA */ | |
1516 | gpt->bheader = gpt_read_header(cxt, | |
1517 | le64_to_cpu(gpt->pheader->alternative_lba), | |
1518 | NULL); | |
1519 | else | |
1520 | /* primary corrupted -- try last LBA */ | |
1521 | gpt->bheader = gpt_read_header(cxt, last_lba(cxt), &gpt->ents); | |
766d5156 | 1522 | |
d2d9efa1 | 1523 | if (!gpt->pheader && !gpt->bheader) |
766d5156 DB |
1524 | goto failed; |
1525 | ||
d2d9efa1 KZ |
1526 | /* primary OK, backup corrupted -- recovery */ |
1527 | if (gpt->pheader && !gpt->bheader) { | |
1528 | fdisk_warnx(cxt, _("The backup GPT table is corrupt, but the " | |
1529 | "primary appears OK, so that will be used.")); | |
45ddb828 KZ |
1530 | gpt->bheader = gpt_copy_header(cxt, gpt->pheader); |
1531 | if (!gpt->bheader) | |
d2d9efa1 | 1532 | goto failed; |
d2d9efa1 | 1533 | gpt_recompute_crc(gpt->bheader, gpt->ents); |
9a8a3b88 | 1534 | fdisk_label_set_changed(cxt->label, 1); |
d2d9efa1 KZ |
1535 | |
1536 | /* primary corrupted, backup OK -- recovery */ | |
1537 | } else if (!gpt->pheader && gpt->bheader) { | |
1538 | fdisk_warnx(cxt, _("The primary GPT table is corrupt, but the " | |
1539 | "backup appears OK, so that will be used.")); | |
45ddb828 KZ |
1540 | gpt->pheader = gpt_copy_header(cxt, gpt->bheader); |
1541 | if (!gpt->pheader) | |
d2d9efa1 | 1542 | goto failed; |
d2d9efa1 | 1543 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
9a8a3b88 KZ |
1544 | fdisk_label_set_changed(cxt->label, 1); |
1545 | } | |
1546 | ||
1547 | /* The headers make be correct, but Backup do not have to be on the end | |
1548 | * of the device (due to device resize, etc.). Let's fix this issue. */ | |
1549 | if (le64_to_cpu(gpt->pheader->alternative_lba) > cxt->total_sectors || | |
1550 | le64_to_cpu(gpt->pheader->alternative_lba) < cxt->total_sectors - 1ULL) { | |
1551 | fdisk_warnx(cxt, _("The backup GPT table is not on the end of the device. " | |
1552 | "This problem will be corrected by write.")); | |
1553 | ||
1554 | gpt_fix_alternative_lba(cxt, gpt); | |
1555 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
1556 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
1557 | fdisk_label_set_changed(cxt->label, 1); | |
d2d9efa1 | 1558 | } |
d71ef5a4 | 1559 | |
b683c081 | 1560 | cxt->label->nparts_max = gpt_get_nentries(gpt); |
b28df75e | 1561 | cxt->label->nparts_cur = partitions_in_use(gpt); |
766d5156 DB |
1562 | return 1; |
1563 | failed: | |
36cd4b3c | 1564 | DBG(GPT, ul_debug("probe failed")); |
9ffeb235 | 1565 | gpt_deinit(cxt->label); |
766d5156 DB |
1566 | return 0; |
1567 | } | |
1568 | ||
1569 | /* | |
1570 | * Stolen from libblkid - can be removed once partition semantics | |
1571 | * are added to the fdisk API. | |
1572 | */ | |
1573 | static char *encode_to_utf8(unsigned char *src, size_t count) | |
1574 | { | |
1575 | uint16_t c; | |
d06f321d | 1576 | char *dest; |
f069f6fe VD |
1577 | size_t i, j; |
1578 | size_t len = count * 3 / 2; | |
3f731001 | 1579 | |
f069f6fe | 1580 | dest = calloc(1, len + 1); |
d06f321d KZ |
1581 | if (!dest) |
1582 | return NULL; | |
766d5156 DB |
1583 | |
1584 | for (j = i = 0; i + 2 <= count; i += 2) { | |
1585 | /* always little endian */ | |
1586 | c = (src[i+1] << 8) | src[i]; | |
1587 | if (c == 0) { | |
766d5156 DB |
1588 | break; |
1589 | } else if (c < 0x80) { | |
f069f6fe | 1590 | if (j+1 > len) |
766d5156 DB |
1591 | break; |
1592 | dest[j++] = (uint8_t) c; | |
1593 | } else if (c < 0x800) { | |
f069f6fe | 1594 | if (j+2 > len) |
766d5156 DB |
1595 | break; |
1596 | dest[j++] = (uint8_t) (0xc0 | (c >> 6)); | |
1597 | dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); | |
1598 | } else { | |
f069f6fe | 1599 | if (j+3 > len) |
766d5156 DB |
1600 | break; |
1601 | dest[j++] = (uint8_t) (0xe0 | (c >> 12)); | |
1602 | dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f)); | |
1603 | dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); | |
1604 | } | |
1605 | } | |
766d5156 DB |
1606 | |
1607 | return dest; | |
1608 | } | |
1609 | ||
01086b80 | 1610 | static int gpt_entry_attrs_to_string(struct gpt_entry *e, char **res) |
c83f772e | 1611 | { |
01086b80 KZ |
1612 | unsigned int n, count = 0; |
1613 | size_t l; | |
1614 | char *bits, *p; | |
1615 | uint64_t attrs; | |
1616 | ||
1617 | assert(e); | |
1618 | assert(res); | |
1619 | ||
1620 | *res = NULL; | |
159652d9 | 1621 | attrs = e->attrs; |
01086b80 KZ |
1622 | if (!attrs) |
1623 | return 0; /* no attributes at all */ | |
1624 | ||
1625 | bits = (char *) &attrs; | |
1626 | ||
1627 | /* Note that sizeof() is correct here, we need separators between | |
1628 | * the strings so also count \0 is correct */ | |
1629 | *res = calloc(1, sizeof(GPT_ATTRSTR_NOBLOCK) + | |
1630 | sizeof(GPT_ATTRSTR_REQ) + | |
1631 | sizeof(GPT_ATTRSTR_LEGACY) + | |
1632 | sizeof("GUID:") + (GPT_ATTRBIT_GUID_COUNT * 3)); | |
c83f772e | 1633 | if (!*res) |
01086b80 KZ |
1634 | return -errno; |
1635 | ||
1636 | p = *res; | |
1637 | if (isset(bits, GPT_ATTRBIT_REQ)) { | |
1638 | memcpy(p, GPT_ATTRSTR_REQ, (l = sizeof(GPT_ATTRSTR_REQ))); | |
1639 | p += l - 1; | |
1640 | } | |
1641 | if (isset(bits, GPT_ATTRBIT_NOBLOCK)) { | |
1642 | if (p > *res) | |
1643 | *p++ = ' '; | |
1644 | memcpy(p, GPT_ATTRSTR_NOBLOCK, (l = sizeof(GPT_ATTRSTR_NOBLOCK))); | |
1645 | p += l - 1; | |
1646 | } | |
1647 | if (isset(bits, GPT_ATTRBIT_LEGACY)) { | |
1648 | if (p > *res) | |
1649 | *p++ = ' '; | |
1650 | memcpy(p, GPT_ATTRSTR_LEGACY, (l = sizeof(GPT_ATTRSTR_LEGACY))); | |
1651 | p += l - 1; | |
1652 | } | |
1653 | ||
1654 | for (n = GPT_ATTRBIT_GUID_FIRST; | |
1655 | n < GPT_ATTRBIT_GUID_FIRST + GPT_ATTRBIT_GUID_COUNT; n++) { | |
c83f772e | 1656 | |
01086b80 | 1657 | if (!isset(bits, n)) |
c83f772e | 1658 | continue; |
01086b80 KZ |
1659 | if (!count) { |
1660 | if (p > *res) | |
1661 | *p++ = ' '; | |
1662 | p += sprintf(p, "GUID:%u", n); | |
1663 | } else | |
1664 | p += sprintf(p, ",%u", n); | |
c83f772e | 1665 | count++; |
c83f772e KZ |
1666 | } |
1667 | ||
01086b80 | 1668 | return 0; |
c83f772e KZ |
1669 | } |
1670 | ||
c77ba531 KZ |
1671 | static int gpt_entry_attrs_from_string( |
1672 | struct fdisk_context *cxt, | |
1673 | struct gpt_entry *e, | |
1674 | const char *str) | |
1675 | { | |
1676 | const char *p = str; | |
1677 | uint64_t attrs = 0; | |
1678 | char *bits; | |
1679 | ||
1680 | assert(e); | |
1681 | assert(p); | |
1682 | ||
36cd4b3c | 1683 | DBG(GPT, ul_debug("parsing string attributes '%s'", p)); |
c77ba531 KZ |
1684 | |
1685 | bits = (char *) &attrs; | |
1686 | ||
1687 | while (p && *p) { | |
1688 | int bit = -1; | |
1689 | ||
1690 | while (isblank(*p)) p++; | |
1691 | if (!*p) | |
1692 | break; | |
1693 | ||
36cd4b3c | 1694 | DBG(GPT, ul_debug(" item '%s'", p)); |
c77ba531 | 1695 | |
17d0902c | 1696 | if (strncmp(p, GPT_ATTRSTR_REQ, |
c77ba531 KZ |
1697 | sizeof(GPT_ATTRSTR_REQ) - 1) == 0) { |
1698 | bit = GPT_ATTRBIT_REQ; | |
1699 | p += sizeof(GPT_ATTRSTR_REQ) - 1; | |
d1b7bfe5 SR |
1700 | } else if (strncmp(p, GPT_ATTRSTR_REQ_TYPO, |
1701 | sizeof(GPT_ATTRSTR_REQ_TYPO) - 1) == 0) { | |
1702 | bit = GPT_ATTRBIT_REQ; | |
1703 | p += sizeof(GPT_ATTRSTR_REQ_TYPO) - 1; | |
c77ba531 KZ |
1704 | } else if (strncmp(p, GPT_ATTRSTR_LEGACY, |
1705 | sizeof(GPT_ATTRSTR_LEGACY) - 1) == 0) { | |
1706 | bit = GPT_ATTRBIT_LEGACY; | |
1707 | p += sizeof(GPT_ATTRSTR_LEGACY) - 1; | |
1708 | } else if (strncmp(p, GPT_ATTRSTR_NOBLOCK, | |
1709 | sizeof(GPT_ATTRSTR_NOBLOCK) - 1) == 0) { | |
1710 | bit = GPT_ATTRBIT_NOBLOCK; | |
1711 | p += sizeof(GPT_ATTRSTR_NOBLOCK) - 1; | |
17d0902c KZ |
1712 | |
1713 | /* GUID:<bit> as well as <bit> */ | |
bae57b5a | 1714 | } else if (isdigit((unsigned char) *p) |
17d0902c | 1715 | || (strncmp(p, "GUID:", 5) == 0 |
bae57b5a | 1716 | && isdigit((unsigned char) *(p + 5)))) { |
c77ba531 KZ |
1717 | char *end = NULL; |
1718 | ||
17d0902c KZ |
1719 | if (*p == 'G') |
1720 | p += 5; | |
1721 | ||
c77ba531 KZ |
1722 | errno = 0; |
1723 | bit = strtol(p, &end, 0); | |
1724 | if (errno || !end || end == str | |
1725 | || bit < GPT_ATTRBIT_GUID_FIRST | |
1726 | || bit >= GPT_ATTRBIT_GUID_FIRST + GPT_ATTRBIT_GUID_COUNT) | |
1727 | bit = -1; | |
1728 | else | |
1729 | p = end; | |
1730 | } | |
1731 | ||
1732 | if (bit < 0) { | |
54fefa07 | 1733 | fdisk_warnx(cxt, _("unsupported GPT attribute bit '%s'"), p); |
c77ba531 KZ |
1734 | return -EINVAL; |
1735 | } | |
1736 | ||
17d0902c KZ |
1737 | if (*p && *p != ',' && !isblank(*p)) { |
1738 | fdisk_warnx(cxt, _("failed to parse GPT attribute string '%s'"), str); | |
1739 | return -EINVAL; | |
1740 | } | |
1741 | ||
c77ba531 KZ |
1742 | setbit(bits, bit); |
1743 | ||
1744 | while (isblank(*p)) p++; | |
1745 | if (*p == ',') | |
1746 | p++; | |
1747 | } | |
1748 | ||
159652d9 | 1749 | e->attrs = attrs; |
c77ba531 KZ |
1750 | return 0; |
1751 | } | |
1752 | ||
8c0a7f91 KZ |
1753 | static int gpt_get_partition(struct fdisk_context *cxt, size_t n, |
1754 | struct fdisk_partition *pa) | |
766d5156 | 1755 | { |
9ffeb235 | 1756 | struct fdisk_gpt_label *gpt; |
6941952e | 1757 | struct gpt_entry *e; |
b443c177 | 1758 | char u_str[UUID_STR_LEN]; |
01086b80 | 1759 | int rc = 0; |
92e486f8 | 1760 | struct gpt_guid guid; |
9ffeb235 KZ |
1761 | |
1762 | assert(cxt); | |
1763 | assert(cxt->label); | |
aa36c2cf | 1764 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 KZ |
1765 | |
1766 | gpt = self_label(cxt); | |
766d5156 | 1767 | |
b683c081 | 1768 | if (n >= gpt_get_nentries(gpt)) |
6941952e | 1769 | return -EINVAL; |
3c5fb475 | 1770 | |
6941952e | 1771 | gpt = self_label(cxt); |
b28df75e | 1772 | e = gpt_get_entry(gpt, n); |
b1920e0b | 1773 | |
02a376f2 | 1774 | pa->used = gpt_entry_is_used(e) || gpt_partition_start(e); |
8c0a7f91 KZ |
1775 | if (!pa->used) |
1776 | return 0; | |
766d5156 | 1777 | |
8c0a7f91 | 1778 | pa->start = gpt_partition_start(e); |
77d6a70a | 1779 | pa->size = gpt_partition_size(e); |
7f539277 | 1780 | pa->type = gpt_partition_parttype(cxt, e); |
766d5156 | 1781 | |
92e486f8 RM |
1782 | guid = e->partition_guid; |
1783 | if (guid_to_string(&guid, u_str)) { | |
8c0a7f91 | 1784 | pa->uuid = strdup(u_str); |
01086b80 KZ |
1785 | if (!pa->uuid) { |
1786 | rc = -errno; | |
1787 | goto done; | |
1788 | } | |
8c0a7f91 KZ |
1789 | } else |
1790 | pa->uuid = NULL; | |
1791 | ||
01086b80 KZ |
1792 | rc = gpt_entry_attrs_to_string(e, &pa->attrs); |
1793 | if (rc) | |
1794 | goto done; | |
b1920e0b | 1795 | |
8c0a7f91 | 1796 | pa->name = encode_to_utf8((unsigned char *)e->name, sizeof(e->name)); |
8c0a7f91 | 1797 | return 0; |
01086b80 | 1798 | done: |
8c0a7f91 | 1799 | fdisk_reset_partition(pa); |
01086b80 | 1800 | return rc; |
6941952e | 1801 | } |
766d5156 | 1802 | |
9348ef25 | 1803 | |
b0a484a8 KZ |
1804 | static int gpt_set_partition(struct fdisk_context *cxt, size_t n, |
1805 | struct fdisk_partition *pa) | |
1806 | { | |
1807 | struct fdisk_gpt_label *gpt; | |
1808 | struct gpt_entry *e; | |
1809 | int rc = 0; | |
9146a008 | 1810 | uint64_t start, end; |
b0a484a8 KZ |
1811 | |
1812 | assert(cxt); | |
1813 | assert(cxt->label); | |
1814 | assert(fdisk_is_label(cxt, GPT)); | |
1815 | ||
1816 | gpt = self_label(cxt); | |
1817 | ||
b683c081 | 1818 | if (n >= gpt_get_nentries(gpt)) |
b0a484a8 KZ |
1819 | return -EINVAL; |
1820 | ||
9146a008 KZ |
1821 | FDISK_INIT_UNDEF(start); |
1822 | FDISK_INIT_UNDEF(end); | |
1823 | ||
b0a484a8 | 1824 | gpt = self_label(cxt); |
b28df75e | 1825 | e = gpt_get_entry(gpt, n); |
b0a484a8 KZ |
1826 | |
1827 | if (pa->uuid) { | |
b443c177 | 1828 | char new_u[UUID_STR_LEN], old_u[UUID_STR_LEN]; |
92e486f8 | 1829 | struct gpt_guid guid; |
6936c081 | 1830 | |
92e486f8 RM |
1831 | guid = e->partition_guid; |
1832 | guid_to_string(&guid, old_u); | |
b0a484a8 KZ |
1833 | rc = gpt_entry_set_uuid(e, pa->uuid); |
1834 | if (rc) | |
1835 | return rc; | |
92e486f8 RM |
1836 | guid = e->partition_guid; |
1837 | guid_to_string(&guid, new_u); | |
0477369a | 1838 | fdisk_info(cxt, _("Partition UUID changed from %s to %s."), |
6936c081 | 1839 | old_u, new_u); |
b0a484a8 KZ |
1840 | } |
1841 | ||
6936c081 | 1842 | if (pa->name) { |
c1165241 | 1843 | int len; |
6936c081 | 1844 | char *old = encode_to_utf8((unsigned char *)e->name, sizeof(e->name)); |
c1165241 VD |
1845 | len = gpt_entry_set_name(e, pa->name); |
1846 | if (len < 0) | |
760942e2 | 1847 | fdisk_warn(cxt, _("Failed to translate partition name, name not changed.")); |
c1165241 VD |
1848 | else |
1849 | fdisk_info(cxt, _("Partition name changed from '%s' to '%.*s'."), | |
1850 | old, len, pa->name); | |
6936c081 KZ |
1851 | free(old); |
1852 | } | |
1853 | ||
b0a484a8 KZ |
1854 | if (pa->type && pa->type->typestr) { |
1855 | struct gpt_guid typeid; | |
1856 | ||
a48c0985 KZ |
1857 | rc = string_to_guid(pa->type->typestr, &typeid); |
1858 | if (rc) | |
1859 | return rc; | |
b0a484a8 KZ |
1860 | gpt_entry_set_type(e, &typeid); |
1861 | } | |
c77ba531 KZ |
1862 | if (pa->attrs) { |
1863 | rc = gpt_entry_attrs_from_string(cxt, e, pa->attrs); | |
1864 | if (rc) | |
1865 | return rc; | |
1866 | } | |
b0a484a8 | 1867 | |
ecf40cda | 1868 | if (fdisk_partition_has_start(pa)) |
9146a008 | 1869 | start = pa->start; |
76785052 KZ |
1870 | if (fdisk_partition_has_size(pa) || fdisk_partition_has_start(pa)) { |
1871 | uint64_t xstart = fdisk_partition_has_start(pa) ? pa->start : gpt_partition_start(e); | |
1872 | uint64_t xsize = fdisk_partition_has_size(pa) ? pa->size : gpt_partition_size(e); | |
1873 | end = xstart + xsize - 1ULL; | |
1874 | } | |
9146a008 | 1875 | |
c949fa98 KZ |
1876 | if (!FDISK_IS_UNDEF(start)) { |
1877 | if (start < le64_to_cpu(gpt->pheader->first_usable_lba)) { | |
614ddddf | 1878 | fdisk_warnx(cxt, _("The start of the partition understeps FirstUsableLBA.")); |
c949fa98 KZ |
1879 | return -EINVAL; |
1880 | } | |
9146a008 | 1881 | e->lba_start = cpu_to_le64(start); |
c949fa98 KZ |
1882 | } |
1883 | if (!FDISK_IS_UNDEF(end)) { | |
1884 | if (end > le64_to_cpu(gpt->pheader->last_usable_lba)) { | |
614ddddf | 1885 | fdisk_warnx(cxt, _("The end of the partition oversteps LastUsableLBA.")); |
c949fa98 KZ |
1886 | return -EINVAL; |
1887 | } | |
9146a008 | 1888 | e->lba_end = cpu_to_le64(end); |
c949fa98 | 1889 | } |
b0a484a8 KZ |
1890 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
1891 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
1892 | ||
1893 | fdisk_label_set_changed(cxt->label, 1); | |
1894 | return rc; | |
1895 | } | |
1896 | ||
afb27a8e KZ |
1897 | static int gpt_write(struct fdisk_context *cxt, off_t offset, void *buf, size_t count) |
1898 | { | |
1899 | if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) | |
1900 | return -errno; | |
b0a484a8 | 1901 | |
afb27a8e KZ |
1902 | if (write_all(cxt->dev_fd, buf, count)) |
1903 | return -errno; | |
1904 | ||
1905 | fsync(cxt->dev_fd); | |
1906 | ||
36cd4b3c | 1907 | DBG(GPT, ul_debug(" write OK [offset=%zu, size=%zu]", |
afb27a8e KZ |
1908 | (size_t) offset, count)); |
1909 | return 0; | |
1910 | } | |
766d5156 DB |
1911 | |
1912 | /* | |
1913 | * Write partitions. | |
1914 | * Returns 0 on success, or corresponding error otherwise. | |
1915 | */ | |
1916 | static int gpt_write_partitions(struct fdisk_context *cxt, | |
b28df75e | 1917 | struct gpt_header *header, unsigned char *ents) |
766d5156 | 1918 | { |
afb27a8e | 1919 | size_t esz = 0; |
9e320545 KZ |
1920 | int rc; |
1921 | ||
afb27a8e | 1922 | rc = gpt_sizeof_entries(header, &esz); |
9e320545 KZ |
1923 | if (rc) |
1924 | return rc; | |
766d5156 | 1925 | |
afb27a8e KZ |
1926 | return gpt_write(cxt, |
1927 | (off_t) le64_to_cpu(header->partition_entry_lba) * cxt->sector_size, | |
1928 | ents, esz); | |
766d5156 DB |
1929 | } |
1930 | ||
1931 | /* | |
4bb82a45 KZ |
1932 | * Write a GPT header to a specified LBA. |
1933 | * | |
1934 | * We read all sector, so we have to write all sector back | |
1935 | * to the device -- never ever rely on sizeof(struct gpt_header)! | |
1936 | * | |
766d5156 DB |
1937 | * Returns 0 on success, or corresponding error otherwise. |
1938 | */ | |
1939 | static int gpt_write_header(struct fdisk_context *cxt, | |
1940 | struct gpt_header *header, uint64_t lba) | |
1941 | { | |
afb27a8e | 1942 | return gpt_write(cxt, lba * cxt->sector_size, header, cxt->sector_size); |
766d5156 DB |
1943 | } |
1944 | ||
1945 | /* | |
1946 | * Write the protective MBR. | |
1947 | * Returns 0 on success, or corresponding error otherwise. | |
1948 | */ | |
1949 | static int gpt_write_pmbr(struct fdisk_context *cxt) | |
1950 | { | |
1b504263 | 1951 | struct gpt_legacy_mbr *pmbr; |
766d5156 | 1952 | |
9ffeb235 KZ |
1953 | assert(cxt); |
1954 | assert(cxt->firstsector); | |
766d5156 | 1955 | |
36cd4b3c | 1956 | DBG(GPT, ul_debug("(over)writing PMBR")); |
766d5156 DB |
1957 | pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; |
1958 | ||
1959 | /* zero out the legacy partitions */ | |
1960 | memset(pmbr->partition_record, 0, sizeof(pmbr->partition_record)); | |
1961 | ||
1962 | pmbr->signature = cpu_to_le16(MSDOS_MBR_SIGNATURE); | |
1963 | pmbr->partition_record[0].os_type = EFI_PMBR_OSTYPE; | |
8ffa3b65 KZ |
1964 | pmbr->partition_record[0].start_sector = 2; |
1965 | pmbr->partition_record[0].end_head = 0xFF; | |
766d5156 DB |
1966 | pmbr->partition_record[0].end_sector = 0xFF; |
1967 | pmbr->partition_record[0].end_track = 0xFF; | |
1968 | pmbr->partition_record[0].starting_lba = cpu_to_le32(1); | |
1969 | ||
1970 | /* | |
1971 | * Set size_in_lba to the size of the disk minus one. If the size of the disk | |
1972 | * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF. | |
1973 | */ | |
0a7cdf80 | 1974 | if (cxt->total_sectors - 1ULL > 0xFFFFFFFFULL) |
766d5156 DB |
1975 | pmbr->partition_record[0].size_in_lba = cpu_to_le32(0xFFFFFFFF); |
1976 | else | |
1977 | pmbr->partition_record[0].size_in_lba = | |
0a7cdf80 | 1978 | cpu_to_le32((uint32_t) (cxt->total_sectors - 1ULL)); |
766d5156 | 1979 | |
19613111 | 1980 | /* pMBR covers the first sector (LBA) of the disk */ |
afb27a8e KZ |
1981 | return gpt_write(cxt, GPT_PMBR_LBA * cxt->sector_size, |
1982 | pmbr, cxt->sector_size); | |
766d5156 DB |
1983 | } |
1984 | ||
1985 | /* | |
1986 | * Writes in-memory GPT and pMBR data to disk. | |
1987 | * Returns 0 if successful write, otherwise, a corresponding error. | |
1988 | * Any indication of error will abort the operation. | |
1989 | */ | |
9ffeb235 | 1990 | static int gpt_write_disklabel(struct fdisk_context *cxt) |
766d5156 | 1991 | { |
9ffeb235 | 1992 | struct fdisk_gpt_label *gpt; |
433d05ff | 1993 | int mbr_type; |
d71ef5a4 | 1994 | |
9ffeb235 KZ |
1995 | assert(cxt); |
1996 | assert(cxt->label); | |
aa36c2cf | 1997 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 | 1998 | |
36cd4b3c KZ |
1999 | DBG(GPT, ul_debug("writting...")); |
2000 | ||
9ffeb235 | 2001 | gpt = self_label(cxt); |
433d05ff | 2002 | mbr_type = valid_pmbr(cxt); |
766d5156 DB |
2003 | |
2004 | /* check that disk is big enough to handle the backup header */ | |
c15aec86 | 2005 | if (le64_to_cpu(gpt->pheader->alternative_lba) > cxt->total_sectors) |
766d5156 DB |
2006 | goto err0; |
2007 | ||
2008 | /* check that the backup header is properly placed */ | |
0a7cdf80 | 2009 | if (le64_to_cpu(gpt->pheader->alternative_lba) < cxt->total_sectors - 1ULL) |
766d5156 DB |
2010 | goto err0; |
2011 | ||
b28df75e | 2012 | if (check_overlap_partitions(gpt)) |
766d5156 DB |
2013 | goto err0; |
2014 | ||
2015 | /* recompute CRCs for both headers */ | |
d71ef5a4 KZ |
2016 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
2017 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
766d5156 DB |
2018 | |
2019 | /* | |
2020 | * UEFI requires writing in this specific order: | |
2021 | * 1) backup partition tables | |
2022 | * 2) backup GPT header | |
2023 | * 3) primary partition tables | |
2024 | * 4) primary GPT header | |
2025 | * 5) protective MBR | |
2026 | * | |
2027 | * If any write fails, we abort the rest. | |
2028 | */ | |
d71ef5a4 | 2029 | if (gpt_write_partitions(cxt, gpt->bheader, gpt->ents) != 0) |
766d5156 | 2030 | goto err1; |
c15aec86 KZ |
2031 | if (gpt_write_header(cxt, gpt->bheader, |
2032 | le64_to_cpu(gpt->pheader->alternative_lba)) != 0) | |
766d5156 | 2033 | goto err1; |
d71ef5a4 | 2034 | if (gpt_write_partitions(cxt, gpt->pheader, gpt->ents) != 0) |
766d5156 | 2035 | goto err1; |
d71ef5a4 | 2036 | if (gpt_write_header(cxt, gpt->pheader, GPT_PRIMARY_PARTITION_TABLE_LBA) != 0) |
766d5156 | 2037 | goto err1; |
433d05ff KZ |
2038 | |
2039 | if (mbr_type == GPT_MBR_HYBRID) | |
1d844703 | 2040 | fdisk_warnx(cxt, _("The device contains hybrid MBR -- writing GPT only.")); |
433d05ff | 2041 | else if (gpt_write_pmbr(cxt) != 0) |
766d5156 DB |
2042 | goto err1; |
2043 | ||
36cd4b3c | 2044 | DBG(GPT, ul_debug("...write success")); |
766d5156 DB |
2045 | return 0; |
2046 | err0: | |
36cd4b3c | 2047 | DBG(GPT, ul_debug("...write failed: incorrect input")); |
c15aec86 | 2048 | errno = EINVAL; |
766d5156 DB |
2049 | return -EINVAL; |
2050 | err1: | |
36cd4b3c | 2051 | DBG(GPT, ul_debug("...write failed: %m")); |
766d5156 DB |
2052 | return -errno; |
2053 | } | |
2054 | ||
2055 | /* | |
2056 | * Verify data integrity and report any found problems for: | |
2057 | * - primary and backup header validations | |
9e930041 | 2058 | * - partition validations |
766d5156 | 2059 | */ |
9ffeb235 | 2060 | static int gpt_verify_disklabel(struct fdisk_context *cxt) |
766d5156 | 2061 | { |
83df5feb KZ |
2062 | int nerror = 0; |
2063 | unsigned int ptnum; | |
9ffeb235 KZ |
2064 | struct fdisk_gpt_label *gpt; |
2065 | ||
2066 | assert(cxt); | |
2067 | assert(cxt->label); | |
aa36c2cf | 2068 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 KZ |
2069 | |
2070 | gpt = self_label(cxt); | |
e820595b KZ |
2071 | if (!gpt) |
2072 | return -EINVAL; | |
766d5156 | 2073 | |
e820595b | 2074 | if (!gpt->bheader) { |
766d5156 | 2075 | nerror++; |
83df5feb | 2076 | fdisk_warnx(cxt, _("Disk does not contain a valid backup header.")); |
766d5156 DB |
2077 | } |
2078 | ||
d71ef5a4 | 2079 | if (!gpt_check_header_crc(gpt->pheader, gpt->ents)) { |
766d5156 | 2080 | nerror++; |
83df5feb | 2081 | fdisk_warnx(cxt, _("Invalid primary header CRC checksum.")); |
766d5156 | 2082 | } |
d71ef5a4 | 2083 | if (gpt->bheader && !gpt_check_header_crc(gpt->bheader, gpt->ents)) { |
766d5156 | 2084 | nerror++; |
83df5feb | 2085 | fdisk_warnx(cxt, _("Invalid backup header CRC checksum.")); |
766d5156 DB |
2086 | } |
2087 | ||
d71ef5a4 | 2088 | if (!gpt_check_entryarr_crc(gpt->pheader, gpt->ents)) { |
766d5156 | 2089 | nerror++; |
83df5feb | 2090 | fdisk_warnx(cxt, _("Invalid partition entry checksum.")); |
766d5156 DB |
2091 | } |
2092 | ||
d71ef5a4 | 2093 | if (!gpt_check_lba_sanity(cxt, gpt->pheader)) { |
766d5156 | 2094 | nerror++; |
83df5feb | 2095 | fdisk_warnx(cxt, _("Invalid primary header LBA sanity checks.")); |
766d5156 | 2096 | } |
d71ef5a4 | 2097 | if (gpt->bheader && !gpt_check_lba_sanity(cxt, gpt->bheader)) { |
766d5156 | 2098 | nerror++; |
83df5feb | 2099 | fdisk_warnx(cxt, _("Invalid backup header LBA sanity checks.")); |
766d5156 DB |
2100 | } |
2101 | ||
d71ef5a4 | 2102 | if (le64_to_cpu(gpt->pheader->my_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA) { |
766d5156 | 2103 | nerror++; |
83df5feb | 2104 | fdisk_warnx(cxt, _("MyLBA mismatch with real position at primary header.")); |
766d5156 | 2105 | } |
d71ef5a4 | 2106 | if (gpt->bheader && le64_to_cpu(gpt->bheader->my_lba) != last_lba(cxt)) { |
766d5156 | 2107 | nerror++; |
83df5feb | 2108 | fdisk_warnx(cxt, _("MyLBA mismatch with real position at backup header.")); |
766d5156 DB |
2109 | |
2110 | } | |
c15aec86 | 2111 | if (le64_to_cpu(gpt->pheader->alternative_lba) >= cxt->total_sectors) { |
766d5156 | 2112 | nerror++; |
a1e276ae | 2113 | fdisk_warnx(cxt, _("Disk is too small to hold all data.")); |
766d5156 DB |
2114 | } |
2115 | ||
2116 | /* | |
2117 | * if the GPT is the primary table, check the alternateLBA | |
2118 | * to see if it is a valid GPT | |
2119 | */ | |
c15aec86 KZ |
2120 | if (gpt->bheader && (le64_to_cpu(gpt->pheader->my_lba) != |
2121 | le64_to_cpu(gpt->bheader->alternative_lba))) { | |
766d5156 | 2122 | nerror++; |
83df5feb | 2123 | fdisk_warnx(cxt, _("Primary and backup header mismatch.")); |
766d5156 DB |
2124 | } |
2125 | ||
b28df75e | 2126 | ptnum = check_overlap_partitions(gpt); |
766d5156 DB |
2127 | if (ptnum) { |
2128 | nerror++; | |
83df5feb KZ |
2129 | fdisk_warnx(cxt, _("Partition %u overlaps with partition %u."), |
2130 | ptnum, ptnum+1); | |
766d5156 DB |
2131 | } |
2132 | ||
b28df75e | 2133 | ptnum = check_too_big_partitions(gpt, cxt->total_sectors); |
766d5156 DB |
2134 | if (ptnum) { |
2135 | nerror++; | |
83df5feb KZ |
2136 | fdisk_warnx(cxt, _("Partition %u is too big for the disk."), |
2137 | ptnum); | |
766d5156 DB |
2138 | } |
2139 | ||
b28df75e | 2140 | ptnum = check_start_after_end_partitions(gpt); |
766d5156 DB |
2141 | if (ptnum) { |
2142 | nerror++; | |
83df5feb KZ |
2143 | fdisk_warnx(cxt, _("Partition %u ends before it starts."), |
2144 | ptnum); | |
766d5156 DB |
2145 | } |
2146 | ||
2147 | if (!nerror) { /* yay :-) */ | |
2148 | uint32_t nsegments = 0; | |
2149 | uint64_t free_sectors = 0, largest_segment = 0; | |
6d0ed4cb | 2150 | char *strsz = NULL; |
766d5156 | 2151 | |
ac1a559a | 2152 | fdisk_info(cxt, _("No errors detected.")); |
83df5feb | 2153 | fdisk_info(cxt, _("Header version: %s"), gpt_get_header_revstr(gpt->pheader)); |
b683c081 | 2154 | fdisk_info(cxt, _("Using %zu out of %zu partitions."), |
b28df75e | 2155 | partitions_in_use(gpt), |
b683c081 | 2156 | gpt_get_nentries(gpt)); |
766d5156 | 2157 | |
b28df75e | 2158 | free_sectors = get_free_sectors(cxt, gpt, &nsegments, &largest_segment); |
6d0ed4cb KZ |
2159 | if (largest_segment) |
2160 | strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, | |
2161 | largest_segment * cxt->sector_size); | |
2162 | ||
4ae11fe8 | 2163 | fdisk_info(cxt, |
829f4206 KZ |
2164 | P_("A total of %ju free sectors is available in %u segment.", |
2165 | "A total of %ju free sectors is available in %u segments " | |
6d0ed4cb KZ |
2166 | "(the largest is %s).", nsegments), |
2167 | free_sectors, nsegments, strsz); | |
2168 | free(strsz); | |
2169 | ||
766d5156 | 2170 | } else |
a1e276ae | 2171 | fdisk_warnx(cxt, |
8e7f944d | 2172 | P_("%d error detected.", "%d errors detected.", nerror), |
a1e276ae | 2173 | nerror); |
766d5156 DB |
2174 | |
2175 | return 0; | |
2176 | } | |
2177 | ||
2178 | /* Delete a single GPT partition, specified by partnum. */ | |
8a95621d | 2179 | static int gpt_delete_partition(struct fdisk_context *cxt, |
9ffeb235 | 2180 | size_t partnum) |
766d5156 | 2181 | { |
9ffeb235 | 2182 | struct fdisk_gpt_label *gpt; |
d71ef5a4 | 2183 | |
9ffeb235 KZ |
2184 | assert(cxt); |
2185 | assert(cxt->label); | |
aa36c2cf | 2186 | assert(fdisk_is_label(cxt, GPT)); |
d71ef5a4 | 2187 | |
9ffeb235 KZ |
2188 | gpt = self_label(cxt); |
2189 | ||
b28df75e | 2190 | if (partnum >= cxt->label->nparts_max) |
1f5eb51b | 2191 | return -EINVAL; |
766d5156 | 2192 | |
02a376f2 | 2193 | if (!gpt_entry_is_used(gpt_get_entry(gpt, partnum))) |
1f5eb51b | 2194 | return -EINVAL; |
b28df75e KZ |
2195 | |
2196 | /* hasta la vista, baby! */ | |
2197 | gpt_zeroize_entry(gpt, partnum); | |
2198 | ||
2199 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
2200 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
2201 | cxt->label->nparts_cur--; | |
2202 | fdisk_label_set_changed(cxt->label, 1); | |
1f5eb51b DB |
2203 | |
2204 | return 0; | |
766d5156 DB |
2205 | } |
2206 | ||
080633e4 | 2207 | |
766d5156 | 2208 | /* Performs logical checks to add a new partition entry */ |
8a95621d KZ |
2209 | static int gpt_add_partition( |
2210 | struct fdisk_context *cxt, | |
c3bc7483 KZ |
2211 | struct fdisk_partition *pa, |
2212 | size_t *partno) | |
766d5156 | 2213 | { |
512a430f KZ |
2214 | uint64_t user_f, user_l; /* user input ranges for first and last sectors */ |
2215 | uint64_t disk_f, disk_l; /* first and last available sector ranges on device*/ | |
2216 | uint64_t dflt_f, dflt_l; /* largest segment (default) */ | |
c0d14b09 | 2217 | struct gpt_guid typeid; |
9ffeb235 | 2218 | struct fdisk_gpt_label *gpt; |
d71ef5a4 | 2219 | struct gpt_header *pheader; |
b28df75e | 2220 | struct gpt_entry *e; |
4114da08 | 2221 | struct fdisk_ask *ask = NULL; |
77d6a70a | 2222 | size_t partnum; |
4114da08 | 2223 | int rc; |
766d5156 | 2224 | |
9ffeb235 KZ |
2225 | assert(cxt); |
2226 | assert(cxt->label); | |
aa36c2cf | 2227 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 KZ |
2228 | |
2229 | gpt = self_label(cxt); | |
b28df75e KZ |
2230 | |
2231 | assert(gpt); | |
2232 | assert(gpt->pheader); | |
2233 | assert(gpt->ents); | |
2234 | ||
d71ef5a4 | 2235 | pheader = gpt->pheader; |
d71ef5a4 | 2236 | |
6c89f750 | 2237 | rc = fdisk_partition_next_partno(pa, cxt, &partnum); |
1240f549 | 2238 | if (rc) { |
36cd4b3c | 2239 | DBG(GPT, ul_debug("failed to get next partno")); |
77d6a70a | 2240 | return rc; |
1240f549 | 2241 | } |
b28df75e | 2242 | |
b683c081 | 2243 | assert(partnum < gpt_get_nentries(gpt)); |
b28df75e | 2244 | |
02a376f2 | 2245 | if (gpt_entry_is_used(gpt_get_entry(gpt, partnum))) { |
829f4206 | 2246 | fdisk_warnx(cxt, _("Partition %zu is already defined. " |
83217641 | 2247 | "Delete it before re-adding it."), partnum +1); |
77d6a70a | 2248 | return -ERANGE; |
766d5156 | 2249 | } |
b683c081 | 2250 | if (gpt_get_nentries(gpt) == partitions_in_use(gpt)) { |
83df5feb | 2251 | fdisk_warnx(cxt, _("All partitions are already in use.")); |
77d6a70a | 2252 | return -ENOSPC; |
766d5156 | 2253 | } |
b28df75e | 2254 | if (!get_free_sectors(cxt, gpt, NULL, NULL)) { |
83df5feb | 2255 | fdisk_warnx(cxt, _("No free sectors available.")); |
8254c3a5 | 2256 | return -ENOSPC; |
766d5156 DB |
2257 | } |
2258 | ||
4044d244 | 2259 | rc = string_to_guid(pa && pa->type && pa->type->typestr ? |
77d6a70a KZ |
2260 | pa->type->typestr: |
2261 | GPT_DEFAULT_ENTRY_TYPE, &typeid); | |
4044d244 KZ |
2262 | if (rc) |
2263 | return rc; | |
77d6a70a | 2264 | |
b28df75e KZ |
2265 | disk_f = find_first_available(gpt, le64_to_cpu(pheader->first_usable_lba)); |
2266 | e = gpt_get_entry(gpt, 0); | |
4a4616b2 KZ |
2267 | |
2268 | /* if first sector no explicitly defined then ignore small gaps before | |
2269 | * the first partition */ | |
2270 | if ((!pa || !fdisk_partition_has_start(pa)) | |
02a376f2 | 2271 | && gpt_entry_is_used(e) |
b28df75e | 2272 | && disk_f < gpt_partition_start(e)) { |
4a4616b2 KZ |
2273 | |
2274 | do { | |
2275 | uint64_t x; | |
36cd4b3c | 2276 | DBG(GPT, ul_debug("testing first sector %"PRIu64"", disk_f)); |
b28df75e | 2277 | disk_f = find_first_available(gpt, disk_f); |
4a4616b2 KZ |
2278 | if (!disk_f) |
2279 | break; | |
b28df75e | 2280 | x = find_last_free(gpt, disk_f); |
4a4616b2 KZ |
2281 | if (x - disk_f >= cxt->grain / cxt->sector_size) |
2282 | break; | |
36cd4b3c | 2283 | DBG(GPT, ul_debug("first sector %"PRIu64" addresses to small space, continue...", disk_f)); |
0a7cdf80 | 2284 | disk_f = x + 1ULL; |
4a4616b2 KZ |
2285 | } while(1); |
2286 | ||
2287 | if (disk_f == 0) | |
b28df75e | 2288 | disk_f = find_first_available(gpt, le64_to_cpu(pheader->first_usable_lba)); |
4a4616b2 KZ |
2289 | } |
2290 | ||
b28df75e KZ |
2291 | e = NULL; |
2292 | disk_l = find_last_free_sector(gpt); | |
512a430f KZ |
2293 | |
2294 | /* the default is the largest free space */ | |
b28df75e KZ |
2295 | dflt_f = find_first_in_largest(gpt); |
2296 | dflt_l = find_last_free(gpt, dflt_f); | |
512a430f KZ |
2297 | |
2298 | /* align the default in range <dflt_f,dflt_l>*/ | |
9475cc78 | 2299 | dflt_f = fdisk_align_lba_in_range(cxt, dflt_f, dflt_f, dflt_l); |
766d5156 | 2300 | |
77d6a70a | 2301 | /* first sector */ |
ecf40cda KZ |
2302 | if (pa && pa->start_follow_default) { |
2303 | user_f = dflt_f; | |
2304 | ||
2305 | } else if (pa && fdisk_partition_has_start(pa)) { | |
36cd4b3c | 2306 | DBG(GPT, ul_debug("first sector defined: %ju", (uintmax_t)pa->start)); |
b28df75e | 2307 | if (pa->start != find_first_available(gpt, pa->start)) { |
fdbd7bb9 | 2308 | fdisk_warnx(cxt, _("Sector %ju already used."), (uintmax_t)pa->start); |
77d6a70a | 2309 | return -ERANGE; |
e3443e8f | 2310 | } |
77d6a70a | 2311 | user_f = pa->start; |
77d6a70a KZ |
2312 | } else { |
2313 | /* ask by dialog */ | |
2314 | for (;;) { | |
2315 | if (!ask) | |
2316 | ask = fdisk_new_ask(); | |
2317 | else | |
2318 | fdisk_reset_ask(ask); | |
5cebb2ab VD |
2319 | if (!ask) |
2320 | return -ENOMEM; | |
77d6a70a KZ |
2321 | |
2322 | /* First sector */ | |
2323 | fdisk_ask_set_query(ask, _("First sector")); | |
2324 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_NUMBER); | |
2325 | fdisk_ask_number_set_low(ask, disk_f); /* minimal */ | |
2326 | fdisk_ask_number_set_default(ask, dflt_f); /* default */ | |
2327 | fdisk_ask_number_set_high(ask, disk_l); /* maximal */ | |
2328 | ||
2329 | rc = fdisk_do_ask(cxt, ask); | |
2330 | if (rc) | |
2331 | goto done; | |
2332 | ||
2333 | user_f = fdisk_ask_number_get_result(ask); | |
b28df75e | 2334 | if (user_f != find_first_available(gpt, user_f)) { |
77d6a70a KZ |
2335 | fdisk_warnx(cxt, _("Sector %ju already used."), user_f); |
2336 | continue; | |
2337 | } | |
512a430f | 2338 | break; |
77d6a70a KZ |
2339 | } |
2340 | } | |
2341 | ||
1240f549 | 2342 | |
77d6a70a | 2343 | /* Last sector */ |
b28df75e | 2344 | dflt_l = find_last_free(gpt, user_f); |
77d6a70a | 2345 | |
ecf40cda KZ |
2346 | if (pa && pa->end_follow_default) { |
2347 | user_l = dflt_l; | |
2348 | ||
2349 | } else if (pa && fdisk_partition_has_size(pa)) { | |
ee50336c | 2350 | user_l = user_f + pa->size - 1; |
36cd4b3c | 2351 | DBG(GPT, ul_debug("size defined: %ju, end: %"PRIu64" (last possible: %"PRIu64")", |
fdbd7bb9 | 2352 | (uintmax_t)pa->size, user_l, dflt_l)); |
694a407d KZ |
2353 | |
2354 | if (user_l != dflt_l | |
2355 | && !pa->size_explicit | |
2356 | && alignment_required(cxt) | |
d527d2dd | 2357 | && user_l - user_f > (cxt->grain / fdisk_get_sector_size(cxt))) { |
694a407d | 2358 | |
68fe4b28 KZ |
2359 | user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l); |
2360 | if (user_l > user_f) | |
0a7cdf80 | 2361 | user_l -= 1ULL; |
68fe4b28 | 2362 | } |
77d6a70a KZ |
2363 | } else { |
2364 | for (;;) { | |
2365 | if (!ask) | |
2366 | ask = fdisk_new_ask(); | |
2367 | else | |
2368 | fdisk_reset_ask(ask); | |
7c43fd23 KZ |
2369 | if (!ask) |
2370 | return -ENOMEM; | |
77d6a70a | 2371 | |
757cefbb | 2372 | fdisk_ask_set_query(ask, _("Last sector, +/-sectors or +/-size{K,M,G,T,P}")); |
77d6a70a KZ |
2373 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET); |
2374 | fdisk_ask_number_set_low(ask, user_f); /* minimal */ | |
2375 | fdisk_ask_number_set_default(ask, dflt_l); /* default */ | |
2376 | fdisk_ask_number_set_high(ask, dflt_l); /* maximal */ | |
2377 | fdisk_ask_number_set_base(ask, user_f); /* base for relative input */ | |
2378 | fdisk_ask_number_set_unit(ask, cxt->sector_size); | |
757cefbb | 2379 | fdisk_ask_number_set_wrap_negative(ask, 1); /* wrap negative around high */ |
77d6a70a KZ |
2380 | |
2381 | rc = fdisk_do_ask(cxt, ask); | |
2382 | if (rc) | |
2383 | goto done; | |
2384 | ||
2385 | user_l = fdisk_ask_number_get_result(ask); | |
1240f549 | 2386 | if (fdisk_ask_number_is_relative(ask)) { |
765004f3 KZ |
2387 | user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l); |
2388 | if (user_l > user_f) | |
0a7cdf80 | 2389 | user_l -= 1ULL; |
0c344037 | 2390 | } |
18b266ce | 2391 | |
765004f3 | 2392 | if (user_l >= user_f && user_l <= disk_l) |
77d6a70a | 2393 | break; |
8c73e509 KZ |
2394 | |
2395 | fdisk_warnx(cxt, _("Value out of range.")); | |
77d6a70a | 2396 | } |
766d5156 DB |
2397 | } |
2398 | ||
8d95e7e0 KZ |
2399 | |
2400 | if (user_f > user_l || partnum >= cxt->label->nparts_max) { | |
27aadd8b | 2401 | fdisk_warnx(cxt, _("Could not create partition %zu"), partnum + 1); |
8d95e7e0 | 2402 | rc = -EINVAL; |
1240f549 | 2403 | goto done; |
8d95e7e0 KZ |
2404 | } |
2405 | ||
3fd1f771 | 2406 | /* Be paranoid and check against on-disk setting rather than against libfdisk cxt */ |
9d9a1b87 KZ |
2407 | if (user_l > le64_to_cpu(pheader->last_usable_lba)) { |
2408 | fdisk_warnx(cxt, _("The last usable GPT sector is %ju, but %ju is requested."), | |
2409 | le64_to_cpu(pheader->last_usable_lba), user_l); | |
2410 | rc = -EINVAL; | |
2411 | goto done; | |
2412 | } | |
2413 | ||
2414 | if (user_f < le64_to_cpu(pheader->first_usable_lba)) { | |
2415 | fdisk_warnx(cxt, _("The first usable GPT sector is %ju, but %ju is requested."), | |
2416 | le64_to_cpu(pheader->first_usable_lba), user_f); | |
2417 | rc = -EINVAL; | |
2418 | goto done; | |
2419 | } | |
2420 | ||
ecf40cda KZ |
2421 | assert(!FDISK_IS_UNDEF(user_l)); |
2422 | assert(!FDISK_IS_UNDEF(user_f)); | |
b683c081 | 2423 | assert(partnum < gpt_get_nentries(gpt)); |
ecf40cda | 2424 | |
b28df75e | 2425 | e = gpt_get_entry(gpt, partnum); |
8d95e7e0 KZ |
2426 | e->lba_end = cpu_to_le64(user_l); |
2427 | e->lba_start = cpu_to_le64(user_f); | |
2428 | ||
2429 | gpt_entry_set_type(e, &typeid); | |
2430 | ||
2431 | if (pa && pa->uuid) { | |
2432 | /* Sometimes it's necessary to create a copy of the PT and | |
2433 | * reuse already defined UUID | |
2434 | */ | |
2435 | rc = gpt_entry_set_uuid(e, pa->uuid); | |
2436 | if (rc) | |
2437 | goto done; | |
1240f549 | 2438 | } else { |
8d95e7e0 KZ |
2439 | /* Any time a new partition entry is created a new GUID must be |
2440 | * generated for that partition, and every partition is guaranteed | |
2441 | * to have a unique GUID. | |
2442 | */ | |
92e486f8 RM |
2443 | struct gpt_guid guid; |
2444 | ||
8d95e7e0 | 2445 | uuid_generate_random((unsigned char *) &e->partition_guid); |
92e486f8 RM |
2446 | guid = e->partition_guid; |
2447 | swap_efi_guid(&guid); | |
8d95e7e0 KZ |
2448 | } |
2449 | ||
2450 | if (pa && pa->name && *pa->name) | |
2451 | gpt_entry_set_name(e, pa->name); | |
c77ba531 KZ |
2452 | if (pa && pa->attrs) |
2453 | gpt_entry_attrs_from_string(cxt, e, pa->attrs); | |
8d95e7e0 | 2454 | |
36cd4b3c | 2455 | DBG(GPT, ul_debug("new partition: partno=%zu, start=%"PRIu64", end=%"PRIu64", size=%"PRIu64"", |
ee50336c KZ |
2456 | partnum, |
2457 | gpt_partition_start(e), | |
2458 | gpt_partition_end(e), | |
2459 | gpt_partition_size(e))); | |
2460 | ||
b28df75e KZ |
2461 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
2462 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
8d95e7e0 KZ |
2463 | |
2464 | /* report result */ | |
2465 | { | |
a01b5b70 KZ |
2466 | struct fdisk_parttype *t; |
2467 | ||
9ffeb235 KZ |
2468 | cxt->label->nparts_cur++; |
2469 | fdisk_label_set_changed(cxt->label, 1); | |
a01b5b70 | 2470 | |
b28df75e | 2471 | t = gpt_partition_parttype(cxt, e); |
a01b5b70 | 2472 | fdisk_info_new_partition(cxt, partnum + 1, user_f, user_l, t); |
dfc6db2a | 2473 | fdisk_unref_parttype(t); |
9fcd49d5 | 2474 | } |
8254c3a5 | 2475 | |
4114da08 | 2476 | rc = 0; |
c3bc7483 KZ |
2477 | if (partno) |
2478 | *partno = partnum; | |
4114da08 | 2479 | done: |
a3d83488 | 2480 | fdisk_unref_ask(ask); |
4114da08 | 2481 | return rc; |
766d5156 DB |
2482 | } |
2483 | ||
3f731001 DB |
2484 | /* |
2485 | * Create a new GPT disklabel - destroys any previous data. | |
2486 | */ | |
9ffeb235 | 2487 | static int gpt_create_disklabel(struct fdisk_context *cxt) |
3f731001 DB |
2488 | { |
2489 | int rc = 0; | |
5eaeb585 | 2490 | size_t esz = 0; |
b443c177 | 2491 | char str[UUID_STR_LEN]; |
9ffeb235 | 2492 | struct fdisk_gpt_label *gpt; |
92e486f8 | 2493 | struct gpt_guid guid; |
9ffeb235 KZ |
2494 | |
2495 | assert(cxt); | |
2496 | assert(cxt->label); | |
aa36c2cf | 2497 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 KZ |
2498 | |
2499 | gpt = self_label(cxt); | |
3f731001 | 2500 | |
d71ef5a4 | 2501 | /* label private stuff has to be empty, see gpt_deinit() */ |
d71ef5a4 KZ |
2502 | assert(gpt->pheader == NULL); |
2503 | assert(gpt->bheader == NULL); | |
4e0e8253 | 2504 | |
3f731001 | 2505 | /* |
3f731001 DB |
2506 | * When no header, entries or pmbr is set, we're probably |
2507 | * dealing with a new, empty disk - so always allocate memory | |
2508 | * to deal with the data structures whatever the case is. | |
2509 | */ | |
3f731001 DB |
2510 | rc = gpt_mknew_pmbr(cxt); |
2511 | if (rc < 0) | |
2512 | goto done; | |
2513 | ||
4bb82a45 KZ |
2514 | assert(cxt->sector_size >= sizeof(struct gpt_header)); |
2515 | ||
d71ef5a4 | 2516 | /* primary */ |
4bb82a45 | 2517 | gpt->pheader = calloc(1, cxt->sector_size); |
46667ba4 KZ |
2518 | if (!gpt->pheader) { |
2519 | rc = -ENOMEM; | |
2520 | goto done; | |
2521 | } | |
d71ef5a4 | 2522 | rc = gpt_mknew_header(cxt, gpt->pheader, GPT_PRIMARY_PARTITION_TABLE_LBA); |
3f731001 DB |
2523 | if (rc < 0) |
2524 | goto done; | |
2525 | ||
d71ef5a4 | 2526 | /* backup ("copy" primary) */ |
4bb82a45 | 2527 | gpt->bheader = calloc(1, cxt->sector_size); |
46667ba4 KZ |
2528 | if (!gpt->bheader) { |
2529 | rc = -ENOMEM; | |
2530 | goto done; | |
2531 | } | |
d71ef5a4 KZ |
2532 | rc = gpt_mknew_header_from_bkp(cxt, gpt->bheader, |
2533 | last_lba(cxt), gpt->pheader); | |
3f731001 DB |
2534 | if (rc < 0) |
2535 | goto done; | |
2536 | ||
b1bc5ae3 | 2537 | rc = gpt_sizeof_entries(gpt->pheader, &esz); |
9e320545 KZ |
2538 | if (rc) |
2539 | goto done; | |
46667ba4 KZ |
2540 | gpt->ents = calloc(1, esz); |
2541 | if (!gpt->ents) { | |
2542 | rc = -ENOMEM; | |
2543 | goto done; | |
2544 | } | |
d71ef5a4 KZ |
2545 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
2546 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
3f731001 | 2547 | |
b683c081 | 2548 | cxt->label->nparts_max = gpt_get_nentries(gpt); |
9ffeb235 | 2549 | cxt->label->nparts_cur = 0; |
9fcd49d5 | 2550 | |
92e486f8 RM |
2551 | guid = gpt->pheader->disk_guid; |
2552 | guid_to_string(&guid, str); | |
9ffeb235 | 2553 | fdisk_label_set_changed(cxt->label, 1); |
0477369a | 2554 | fdisk_info(cxt, _("Created a new GPT disklabel (GUID: %s)."), str); |
3f731001 DB |
2555 | done: |
2556 | return rc; | |
2557 | } | |
2558 | ||
e5f31446 | 2559 | static int gpt_set_disklabel_id(struct fdisk_context *cxt, const char *str) |
35b1f0a4 KZ |
2560 | { |
2561 | struct fdisk_gpt_label *gpt; | |
2562 | struct gpt_guid uuid; | |
e5f31446 | 2563 | char *old, *new; |
35b1f0a4 KZ |
2564 | int rc; |
2565 | ||
2566 | assert(cxt); | |
2567 | assert(cxt->label); | |
aa36c2cf | 2568 | assert(fdisk_is_label(cxt, GPT)); |
35b1f0a4 KZ |
2569 | |
2570 | gpt = self_label(cxt); | |
e5f31446 | 2571 | if (!str) { |
3c3b7648 KZ |
2572 | char *buf = NULL; |
2573 | ||
e5f31446 | 2574 | if (fdisk_ask_string(cxt, |
3c3b7648 | 2575 | _("Enter new disk UUID (in 8-4-4-4-12 format)"), &buf)) |
e5f31446 | 2576 | return -EINVAL; |
3c3b7648 KZ |
2577 | rc = string_to_guid(buf, &uuid); |
2578 | free(buf); | |
e5f31446 KZ |
2579 | } else |
2580 | rc = string_to_guid(str, &uuid); | |
35b1f0a4 KZ |
2581 | |
2582 | if (rc) { | |
2583 | fdisk_warnx(cxt, _("Failed to parse your UUID.")); | |
2584 | return rc; | |
2585 | } | |
2586 | ||
5989556a | 2587 | old = gpt_get_header_id(gpt->pheader); |
35b1f0a4 KZ |
2588 | |
2589 | gpt->pheader->disk_guid = uuid; | |
2590 | gpt->bheader->disk_guid = uuid; | |
2591 | ||
2592 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
2593 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
2594 | ||
5989556a | 2595 | new = gpt_get_header_id(gpt->pheader); |
35b1f0a4 | 2596 | |
0477369a | 2597 | fdisk_info(cxt, _("Disk identifier changed from %s to %s."), old, new); |
35b1f0a4 KZ |
2598 | |
2599 | free(old); | |
2600 | free(new); | |
2601 | fdisk_label_set_changed(cxt->label, 1); | |
2602 | return 0; | |
2603 | } | |
2604 | ||
a18e726c SP |
2605 | static int gpt_check_table_overlap(struct fdisk_context *cxt, |
2606 | uint64_t first_usable, | |
2607 | uint64_t last_usable) | |
2608 | { | |
2609 | struct fdisk_gpt_label *gpt = self_label(cxt); | |
b683c081 | 2610 | size_t i; |
a18e726c SP |
2611 | int rc = 0; |
2612 | ||
2613 | /* First check if there's enough room for the table. last_lba may have wrapped */ | |
2614 | if (first_usable > cxt->total_sectors || /* far too little space */ | |
2615 | last_usable > cxt->total_sectors || /* wrapped */ | |
2616 | first_usable > last_usable) { /* too little space */ | |
2617 | fdisk_warnx(cxt, _("Not enough space for new partition table!")); | |
2618 | return -ENOSPC; | |
2619 | } | |
2620 | ||
2621 | /* check that all partitions fit in the remaining space */ | |
b683c081 | 2622 | for (i = 0; i < gpt_get_nentries(gpt); i++) { |
b28df75e KZ |
2623 | struct gpt_entry *e = gpt_get_entry(gpt, i); |
2624 | ||
02a376f2 | 2625 | if (!gpt_entry_is_used(e)) |
a18e726c | 2626 | continue; |
b28df75e | 2627 | if (gpt_partition_start(e) < first_usable) { |
b683c081 | 2628 | fdisk_warnx(cxt, _("Partition #%zu out of range (minimal start is %"PRIu64" sectors)"), |
a18e726c SP |
2629 | i + 1, first_usable); |
2630 | rc = -EINVAL; | |
2631 | } | |
b28df75e | 2632 | if (gpt_partition_end(e) > last_usable) { |
b683c081 | 2633 | fdisk_warnx(cxt, _("Partition #%zu out of range (maximal end is %"PRIu64" sectors)"), |
0a7cdf80 | 2634 | i + 1, last_usable - 1ULL); |
a18e726c SP |
2635 | rc = -EINVAL; |
2636 | } | |
2637 | } | |
2638 | return rc; | |
2639 | } | |
2640 | ||
f88eeb25 KZ |
2641 | /** |
2642 | * fdisk_gpt_set_npartitions: | |
2643 | * @cxt: context | |
b1bc5ae3 | 2644 | * @nents: number of wanted entries |
f88eeb25 KZ |
2645 | * |
2646 | * Elarge GPT entries array if possible. The function check if an existing | |
2647 | * partition does not overlap the entries array area. If yes, then it report | |
2648 | * warning and returns -EINVAL. | |
2649 | * | |
2650 | * Returns: 0 on success, < 0 on error. | |
81b176c4 | 2651 | * Since: 2.29 |
f88eeb25 | 2652 | */ |
b1bc5ae3 | 2653 | int fdisk_gpt_set_npartitions(struct fdisk_context *cxt, uint32_t nents) |
a18e726c SP |
2654 | { |
2655 | struct fdisk_gpt_label *gpt; | |
b1bc5ae3 KZ |
2656 | size_t new_size; |
2657 | uint32_t old_nents; | |
a18e726c SP |
2658 | uint64_t first_usable, last_usable; |
2659 | int rc; | |
2660 | ||
2661 | assert(cxt); | |
2662 | assert(cxt->label); | |
21f1206a KZ |
2663 | |
2664 | if (!fdisk_is_label(cxt, GPT)) | |
2665 | return -EINVAL; | |
a18e726c SP |
2666 | |
2667 | gpt = self_label(cxt); | |
2668 | ||
b1bc5ae3 KZ |
2669 | old_nents = le32_to_cpu(gpt->pheader->npartition_entries); |
2670 | if (old_nents == nents) | |
a67054f9 | 2671 | return 0; /* do nothing, say nothing */ |
a18e726c SP |
2672 | |
2673 | /* calculate the size (bytes) of the entries array */ | |
b1bc5ae3 | 2674 | rc = gpt_calculate_sizeof_entries(gpt->pheader, nents, &new_size); |
9e320545 | 2675 | if (rc) { |
845fd622 KZ |
2676 | uint32_t entry_size = le32_to_cpu(gpt->pheader->sizeof_partition_entry); |
2677 | ||
2678 | if (entry_size == 0) | |
2679 | fdisk_warnx(cxt, _("The partition entry size is zero.")); | |
2680 | else | |
2681 | fdisk_warnx(cxt, _("The number of the partition has to be smaller than %zu."), | |
2682 | UINT32_MAX / entry_size); | |
9e320545 | 2683 | return rc; |
f71b96bf KZ |
2684 | } |
2685 | ||
b1bc5ae3 KZ |
2686 | gpt_calculate_first_lba(gpt->pheader, nents, &first_usable, cxt); |
2687 | gpt_calculate_last_lba(gpt->pheader, nents, &last_usable, cxt); | |
a18e726c SP |
2688 | |
2689 | /* if expanding the table, first check that everything fits, | |
2690 | * then allocate more memory and zero. */ | |
b1bc5ae3 | 2691 | if (nents > old_nents) { |
b28df75e | 2692 | unsigned char *ents; |
b1bc5ae3 | 2693 | size_t old_size; |
b28df75e | 2694 | |
b1bc5ae3 KZ |
2695 | rc = gpt_calculate_sizeof_entries(gpt->pheader, old_nents, &old_size); |
2696 | if (!rc) | |
2697 | rc = gpt_check_table_overlap(cxt, first_usable, last_usable); | |
a18e726c SP |
2698 | if (rc) |
2699 | return rc; | |
2700 | ents = realloc(gpt->ents, new_size); | |
2701 | if (!ents) { | |
2702 | fdisk_warnx(cxt, _("Cannot allocate memory!")); | |
2703 | return -ENOMEM; | |
2704 | } | |
52f35f1e | 2705 | memset(ents + old_size, 0, new_size - old_size); |
a18e726c SP |
2706 | gpt->ents = ents; |
2707 | } | |
2708 | ||
2709 | /* everything's ok, apply the new size */ | |
b1bc5ae3 KZ |
2710 | gpt->pheader->npartition_entries = cpu_to_le32(nents); |
2711 | gpt->bheader->npartition_entries = cpu_to_le32(nents); | |
a18e726c SP |
2712 | |
2713 | /* usable LBA addresses will have changed */ | |
2714 | fdisk_set_first_lba(cxt, first_usable); | |
2715 | fdisk_set_last_lba(cxt, last_usable); | |
2716 | gpt->pheader->first_usable_lba = cpu_to_le64(first_usable); | |
2717 | gpt->bheader->first_usable_lba = cpu_to_le64(first_usable); | |
2718 | gpt->pheader->last_usable_lba = cpu_to_le64(last_usable); | |
2719 | gpt->bheader->last_usable_lba = cpu_to_le64(last_usable); | |
2720 | ||
a18e726c SP |
2721 | /* The backup header must be recalculated */ |
2722 | gpt_mknew_header_common(cxt, gpt->bheader, le64_to_cpu(gpt->pheader->alternative_lba)); | |
2723 | ||
2724 | /* CRCs will have changed */ | |
2725 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
2726 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
2727 | ||
bbb574a2 KZ |
2728 | /* update library info */ |
2729 | cxt->label->nparts_max = gpt_get_nentries(gpt); | |
2730 | ||
b1bc5ae3 KZ |
2731 | fdisk_info(cxt, _("Partition table length changed from %"PRIu32" to %"PRIu64"."), |
2732 | old_nents, nents); | |
a18e726c SP |
2733 | |
2734 | fdisk_label_set_changed(cxt->label, 1); | |
2735 | return 0; | |
2736 | } | |
2737 | ||
8c0a7f91 | 2738 | static int gpt_part_is_used(struct fdisk_context *cxt, size_t i) |
47b8e7c0 | 2739 | { |
9ffeb235 | 2740 | struct fdisk_gpt_label *gpt; |
47b8e7c0 KZ |
2741 | struct gpt_entry *e; |
2742 | ||
9ffeb235 KZ |
2743 | assert(cxt); |
2744 | assert(cxt->label); | |
aa36c2cf | 2745 | assert(fdisk_is_label(cxt, GPT)); |
9ffeb235 KZ |
2746 | |
2747 | gpt = self_label(cxt); | |
2748 | ||
b683c081 | 2749 | if (i >= gpt_get_nentries(gpt)) |
8c0a7f91 | 2750 | return 0; |
b28df75e KZ |
2751 | |
2752 | e = gpt_get_entry(gpt, i); | |
47b8e7c0 | 2753 | |
02a376f2 | 2754 | return gpt_entry_is_used(e) || gpt_partition_start(e); |
47b8e7c0 KZ |
2755 | } |
2756 | ||
0077e7cd KZ |
2757 | /** |
2758 | * fdisk_gpt_is_hybrid: | |
2759 | * @cxt: context | |
2760 | * | |
2761 | * The regular GPT contains PMBR (dummy protective MBR) where the protective | |
2762 | * MBR does not address any partitions. | |
2763 | * | |
2764 | * Hybrid GPT contains regular MBR where this partition table addresses the | |
2765 | * same partitions as GPT. It's recommended to not use hybrid GPT due to MBR | |
2766 | * limits. | |
2767 | * | |
2768 | * The libfdisk does not provide functionality to sync GPT and MBR, you have to | |
2769 | * directly access and modify (P)MBR (see fdisk_new_nested_context()). | |
2770 | * | |
2771 | * Returns: 1 if partition table detected as hybrid otherwise return 0 | |
2772 | */ | |
433d05ff KZ |
2773 | int fdisk_gpt_is_hybrid(struct fdisk_context *cxt) |
2774 | { | |
2775 | assert(cxt); | |
2776 | return valid_pmbr(cxt) == GPT_MBR_HYBRID; | |
2777 | } | |
2778 | ||
4a4a0927 MM |
2779 | /** |
2780 | * fdisk_gpt_get_partition_attrs: | |
2781 | * @cxt: context | |
2782 | * @partnum: partition number | |
2783 | * @attrs: GPT partition attributes | |
2784 | * | |
2785 | * Sets @attrs for the given partition | |
2786 | * | |
2787 | * Returns: 0 on success, <0 on error. | |
2788 | */ | |
2789 | int fdisk_gpt_get_partition_attrs( | |
2790 | struct fdisk_context *cxt, | |
2791 | size_t partnum, | |
2792 | uint64_t *attrs) | |
2793 | { | |
2794 | struct fdisk_gpt_label *gpt; | |
2795 | ||
2796 | assert(cxt); | |
2797 | assert(cxt->label); | |
21f1206a KZ |
2798 | |
2799 | if (!fdisk_is_label(cxt, GPT)) | |
2800 | return -EINVAL; | |
4a4a0927 MM |
2801 | |
2802 | gpt = self_label(cxt); | |
2803 | ||
b683c081 | 2804 | if (partnum >= gpt_get_nentries(gpt)) |
4a4a0927 MM |
2805 | return -EINVAL; |
2806 | ||
b28df75e | 2807 | *attrs = le64_to_cpu(gpt_get_entry(gpt, partnum)->attrs); |
4a4a0927 MM |
2808 | return 0; |
2809 | } | |
2810 | ||
2811 | /** | |
2812 | * fdisk_gpt_set_partition_attrs: | |
2813 | * @cxt: context | |
2814 | * @partnum: partition number | |
2815 | * @attrs: GPT partition attributes | |
2816 | * | |
2817 | * Sets the GPT partition attributes field to @attrs. | |
2818 | * | |
2819 | * Returns: 0 on success, <0 on error. | |
2820 | */ | |
2821 | int fdisk_gpt_set_partition_attrs( | |
2822 | struct fdisk_context *cxt, | |
2823 | size_t partnum, | |
2824 | uint64_t attrs) | |
2825 | { | |
2826 | struct fdisk_gpt_label *gpt; | |
2827 | ||
2828 | assert(cxt); | |
2829 | assert(cxt->label); | |
21f1206a KZ |
2830 | |
2831 | if (!fdisk_is_label(cxt, GPT)) | |
2832 | return -EINVAL; | |
4a4a0927 | 2833 | |
36cd4b3c | 2834 | DBG(GPT, ul_debug("entry attributes change requested partno=%zu", partnum)); |
4a4a0927 MM |
2835 | gpt = self_label(cxt); |
2836 | ||
b683c081 | 2837 | if (partnum >= gpt_get_nentries(gpt)) |
4a4a0927 MM |
2838 | return -EINVAL; |
2839 | ||
b28df75e | 2840 | gpt_get_entry(gpt, partnum)->attrs = cpu_to_le64(attrs); |
4a4a0927 MM |
2841 | fdisk_info(cxt, _("The attributes on partition %zu changed to 0x%016" PRIx64 "."), |
2842 | partnum + 1, attrs); | |
2843 | ||
2844 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
2845 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
2846 | fdisk_label_set_changed(cxt->label, 1); | |
2847 | return 0; | |
2848 | } | |
2849 | ||
c83f772e KZ |
2850 | static int gpt_toggle_partition_flag( |
2851 | struct fdisk_context *cxt, | |
2852 | size_t i, | |
2853 | unsigned long flag) | |
2854 | { | |
2855 | struct fdisk_gpt_label *gpt; | |
b28df75e | 2856 | struct gpt_entry *e; |
262c94c2 RM |
2857 | uint64_t attrs; |
2858 | uintmax_t tmp; | |
01086b80 KZ |
2859 | char *bits; |
2860 | const char *name = NULL; | |
2861 | int bit = -1, rc; | |
c83f772e KZ |
2862 | |
2863 | assert(cxt); | |
2864 | assert(cxt->label); | |
aa36c2cf | 2865 | assert(fdisk_is_label(cxt, GPT)); |
c83f772e | 2866 | |
36cd4b3c | 2867 | DBG(GPT, ul_debug("entry attribute change requested partno=%zu", i)); |
c83f772e KZ |
2868 | gpt = self_label(cxt); |
2869 | ||
b683c081 | 2870 | if (i >= gpt_get_nentries(gpt)) |
c83f772e KZ |
2871 | return -EINVAL; |
2872 | ||
b28df75e KZ |
2873 | e = gpt_get_entry(gpt, i); |
2874 | attrs = e->attrs; | |
01086b80 | 2875 | bits = (char *) &attrs; |
c83f772e KZ |
2876 | |
2877 | switch (flag) { | |
2878 | case GPT_FLAG_REQUIRED: | |
01086b80 KZ |
2879 | bit = GPT_ATTRBIT_REQ; |
2880 | name = GPT_ATTRSTR_REQ; | |
c83f772e KZ |
2881 | break; |
2882 | case GPT_FLAG_NOBLOCK: | |
01086b80 KZ |
2883 | bit = GPT_ATTRBIT_NOBLOCK; |
2884 | name = GPT_ATTRSTR_NOBLOCK; | |
c83f772e KZ |
2885 | break; |
2886 | case GPT_FLAG_LEGACYBOOT: | |
01086b80 KZ |
2887 | bit = GPT_ATTRBIT_LEGACY; |
2888 | name = GPT_ATTRSTR_LEGACY; | |
c83f772e KZ |
2889 | break; |
2890 | case GPT_FLAG_GUIDSPECIFIC: | |
01086b80 | 2891 | rc = fdisk_ask_number(cxt, 48, 48, 63, _("Enter GUID specific bit"), &tmp); |
c83f772e KZ |
2892 | if (rc) |
2893 | return rc; | |
01086b80 KZ |
2894 | bit = tmp; |
2895 | break; | |
773aae5c KZ |
2896 | default: |
2897 | /* already specified PT_FLAG_GUIDSPECIFIC bit */ | |
2898 | if (flag >= 48 && flag <= 63) { | |
2899 | bit = flag; | |
2900 | flag = GPT_FLAG_GUIDSPECIFIC; | |
2901 | } | |
2902 | break; | |
01086b80 | 2903 | } |
c83f772e | 2904 | |
773aae5c KZ |
2905 | if (bit < 0) { |
2906 | fdisk_warnx(cxt, _("failed to toggle unsupported bit %lu"), flag); | |
01086b80 | 2907 | return -EINVAL; |
773aae5c | 2908 | } |
01086b80 KZ |
2909 | |
2910 | if (!isset(bits, bit)) | |
2911 | setbit(bits, bit); | |
2912 | else | |
2913 | clrbit(bits, bit); | |
2914 | ||
b28df75e | 2915 | e->attrs = attrs; |
01086b80 KZ |
2916 | |
2917 | if (flag == GPT_FLAG_GUIDSPECIFIC) | |
0477369a | 2918 | fdisk_info(cxt, isset(bits, bit) ? |
01086b80 KZ |
2919 | _("The GUID specific bit %d on partition %zu is enabled now.") : |
2920 | _("The GUID specific bit %d on partition %zu is disabled now."), | |
c83f772e | 2921 | bit, i + 1); |
01086b80 | 2922 | else |
0477369a | 2923 | fdisk_info(cxt, isset(bits, bit) ? |
01086b80 KZ |
2924 | _("The %s flag on partition %zu is enabled now.") : |
2925 | _("The %s flag on partition %zu is disabled now."), | |
2926 | name, i + 1); | |
c83f772e KZ |
2927 | |
2928 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
2929 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
01086b80 | 2930 | fdisk_label_set_changed(cxt->label, 1); |
c83f772e KZ |
2931 | return 0; |
2932 | } | |
1054699c | 2933 | |
9348ef25 KZ |
2934 | static int gpt_entry_cmp_start(const void *a, const void *b) |
2935 | { | |
ec4b88b8 KZ |
2936 | const struct gpt_entry *ae = (const struct gpt_entry *) a, |
2937 | *be = (const struct gpt_entry *) b; | |
02a376f2 KZ |
2938 | int au = gpt_entry_is_used(ae), |
2939 | bu = gpt_entry_is_used(be); | |
9348ef25 | 2940 | |
02a376f2 | 2941 | if (!au && !bu) |
9348ef25 | 2942 | return 0; |
02a376f2 | 2943 | if (!au) |
9348ef25 | 2944 | return 1; |
02a376f2 | 2945 | if (!bu) |
9348ef25 KZ |
2946 | return -1; |
2947 | ||
19ff8ff7 | 2948 | return cmp_numbers(gpt_partition_start(ae), gpt_partition_start(be)); |
9348ef25 KZ |
2949 | } |
2950 | ||
2951 | /* sort partition by start sector */ | |
2952 | static int gpt_reorder(struct fdisk_context *cxt) | |
2953 | { | |
2954 | struct fdisk_gpt_label *gpt; | |
dd49c7d6 | 2955 | size_t i, nparts, mess; |
9348ef25 KZ |
2956 | |
2957 | assert(cxt); | |
2958 | assert(cxt->label); | |
aa36c2cf | 2959 | assert(fdisk_is_label(cxt, GPT)); |
9348ef25 KZ |
2960 | |
2961 | gpt = self_label(cxt); | |
b683c081 | 2962 | nparts = gpt_get_nentries(gpt); |
9348ef25 | 2963 | |
dd49c7d6 KZ |
2964 | for (i = 0, mess = 0; mess == 0 && i + 1 < nparts; i++) |
2965 | mess = gpt_entry_cmp_start( | |
b28df75e KZ |
2966 | (const void *) gpt_get_entry(gpt, i), |
2967 | (const void *) gpt_get_entry(gpt, i + 1)) > 0; | |
dd49c7d6 KZ |
2968 | |
2969 | if (!mess) { | |
2970 | fdisk_info(cxt, _("Nothing to do. Ordering is correct already.")); | |
2971 | return 1; | |
2972 | } | |
2973 | ||
9348ef25 KZ |
2974 | qsort(gpt->ents, nparts, sizeof(struct gpt_entry), |
2975 | gpt_entry_cmp_start); | |
2976 | ||
2977 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
2978 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
2979 | fdisk_label_set_changed(cxt->label, 1); | |
2980 | ||
9348ef25 KZ |
2981 | return 0; |
2982 | } | |
2983 | ||
1240f549 KZ |
2984 | static int gpt_reset_alignment(struct fdisk_context *cxt) |
2985 | { | |
2986 | struct fdisk_gpt_label *gpt; | |
2987 | struct gpt_header *h; | |
2988 | ||
2989 | assert(cxt); | |
2990 | assert(cxt->label); | |
aa36c2cf | 2991 | assert(fdisk_is_label(cxt, GPT)); |
1240f549 KZ |
2992 | |
2993 | gpt = self_label(cxt); | |
2994 | h = gpt ? gpt->pheader : NULL; | |
2995 | ||
2996 | if (h) { | |
2997 | /* always follow existing table */ | |
43a2b094 KZ |
2998 | cxt->first_lba = le64_to_cpu(h->first_usable_lba); |
2999 | cxt->last_lba = le64_to_cpu(h->last_usable_lba); | |
1240f549 KZ |
3000 | } else { |
3001 | /* estimate ranges for GPT */ | |
3002 | uint64_t first, last; | |
3003 | ||
3004 | count_first_last_lba(cxt, &first, &last); | |
3005 | ||
3006 | if (cxt->first_lba < first) | |
3007 | cxt->first_lba = first; | |
3008 | if (cxt->last_lba > last) | |
3009 | cxt->last_lba = last; | |
3010 | } | |
3011 | ||
3012 | return 0; | |
3013 | } | |
4e0e8253 KZ |
3014 | /* |
3015 | * Deinitialize fdisk-specific variables | |
3016 | */ | |
d71ef5a4 | 3017 | static void gpt_deinit(struct fdisk_label *lb) |
4e0e8253 | 3018 | { |
d71ef5a4 KZ |
3019 | struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; |
3020 | ||
3021 | if (!gpt) | |
3022 | return; | |
3023 | ||
3024 | free(gpt->ents); | |
3025 | free(gpt->pheader); | |
3026 | free(gpt->bheader); | |
3027 | ||
3028 | gpt->ents = NULL; | |
3029 | gpt->pheader = NULL; | |
3030 | gpt->bheader = NULL; | |
4e0e8253 KZ |
3031 | } |
3032 | ||
0c5d095e | 3033 | static const struct fdisk_label_operations gpt_operations = |
766d5156 | 3034 | { |
0c5d095e KZ |
3035 | .probe = gpt_probe_label, |
3036 | .write = gpt_write_disklabel, | |
3037 | .verify = gpt_verify_disklabel, | |
3038 | .create = gpt_create_disklabel, | |
775001ad | 3039 | .locate = gpt_locate_disklabel, |
5989556a | 3040 | .get_item = gpt_get_disklabel_item, |
35b1f0a4 | 3041 | .set_id = gpt_set_disklabel_id, |
21fe3dde | 3042 | |
8c0a7f91 | 3043 | .get_part = gpt_get_partition, |
b0a484a8 | 3044 | .set_part = gpt_set_partition, |
77d6a70a | 3045 | .add_part = gpt_add_partition, |
e11c6684 | 3046 | .del_part = gpt_delete_partition, |
5989556a | 3047 | .reorder = gpt_reorder, |
8c0a7f91 KZ |
3048 | |
3049 | .part_is_used = gpt_part_is_used, | |
c83f772e | 3050 | .part_toggle_flag = gpt_toggle_partition_flag, |
4e0e8253 | 3051 | |
1240f549 KZ |
3052 | .deinit = gpt_deinit, |
3053 | ||
3054 | .reset_alignment = gpt_reset_alignment | |
766d5156 | 3055 | }; |
0c5d095e | 3056 | |
bd85d11f | 3057 | static const struct fdisk_field gpt_fields[] = |
6941952e KZ |
3058 | { |
3059 | /* basic */ | |
bd85d11f KZ |
3060 | { FDISK_FIELD_DEVICE, N_("Device"), 10, 0 }, |
3061 | { FDISK_FIELD_START, N_("Start"), 5, FDISK_FIELDFL_NUMBER }, | |
3062 | { FDISK_FIELD_END, N_("End"), 5, FDISK_FIELDFL_NUMBER }, | |
3063 | { FDISK_FIELD_SECTORS, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER }, | |
bd85d11f KZ |
3064 | { FDISK_FIELD_SIZE, N_("Size"), 5, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_EYECANDY }, |
3065 | { FDISK_FIELD_TYPE, N_("Type"), 0.1, FDISK_FIELDFL_EYECANDY }, | |
6941952e | 3066 | /* expert */ |
bd85d11f KZ |
3067 | { FDISK_FIELD_TYPEID, N_("Type-UUID"), 36, FDISK_FIELDFL_DETAIL }, |
3068 | { FDISK_FIELD_UUID, N_("UUID"), 36, FDISK_FIELDFL_DETAIL }, | |
3069 | { FDISK_FIELD_NAME, N_("Name"), 0.2, FDISK_FIELDFL_DETAIL }, | |
3070 | { FDISK_FIELD_ATTR, N_("Attrs"), 0, FDISK_FIELDFL_DETAIL } | |
6941952e KZ |
3071 | }; |
3072 | ||
0c5d095e KZ |
3073 | /* |
3074 | * allocates GPT in-memory stuff | |
3075 | */ | |
01aec449 | 3076 | struct fdisk_label *fdisk_new_gpt_label(struct fdisk_context *cxt __attribute__ ((__unused__))) |
0c5d095e KZ |
3077 | { |
3078 | struct fdisk_label *lb; | |
3079 | struct fdisk_gpt_label *gpt; | |
3080 | ||
0c5d095e KZ |
3081 | gpt = calloc(1, sizeof(*gpt)); |
3082 | if (!gpt) | |
3083 | return NULL; | |
3084 | ||
3085 | /* initialize generic part of the driver */ | |
3086 | lb = (struct fdisk_label *) gpt; | |
3087 | lb->name = "gpt"; | |
53b422ab | 3088 | lb->id = FDISK_DISKLABEL_GPT; |
0c5d095e KZ |
3089 | lb->op = &gpt_operations; |
3090 | lb->parttypes = gpt_parttypes; | |
3091 | lb->nparttypes = ARRAY_SIZE(gpt_parttypes); | |
3092 | ||
bd85d11f KZ |
3093 | lb->fields = gpt_fields; |
3094 | lb->nfields = ARRAY_SIZE(gpt_fields); | |
6941952e | 3095 | |
0c5d095e KZ |
3096 | return lb; |
3097 | } | |
4a4a0927 MM |
3098 | |
3099 | #ifdef TEST_PROGRAM | |
5fde1d9f | 3100 | static int test_getattr(struct fdisk_test *ts, int argc, char *argv[]) |
4a4a0927 MM |
3101 | { |
3102 | const char *disk = argv[1]; | |
3103 | size_t part = strtoul(argv[2], NULL, 0) - 1; | |
3104 | struct fdisk_context *cxt; | |
3105 | uint64_t atters = 0; | |
3106 | ||
3107 | cxt = fdisk_new_context(); | |
3108 | fdisk_assign_device(cxt, disk, 1); | |
3109 | ||
3110 | if (!fdisk_is_label(cxt, GPT)) | |
3111 | return EXIT_FAILURE; | |
3112 | ||
3113 | if (fdisk_gpt_get_partition_attrs(cxt, part, &atters)) | |
3114 | return EXIT_FAILURE; | |
3115 | ||
3116 | printf("%s: 0x%016" PRIx64 "\n", argv[2], atters); | |
3117 | ||
3118 | fdisk_unref_context(cxt); | |
3119 | return 0; | |
3120 | } | |
3121 | ||
5fde1d9f | 3122 | static int test_setattr(struct fdisk_test *ts, int argc, char *argv[]) |
4a4a0927 MM |
3123 | { |
3124 | const char *disk = argv[1]; | |
3125 | size_t part = strtoul(argv[2], NULL, 0) - 1; | |
3126 | uint64_t atters = strtoull(argv[3], NULL, 0); | |
3127 | struct fdisk_context *cxt; | |
3128 | ||
3129 | cxt = fdisk_new_context(); | |
3130 | fdisk_assign_device(cxt, disk, 0); | |
3131 | ||
3132 | if (!fdisk_is_label(cxt, GPT)) | |
3133 | return EXIT_FAILURE; | |
3134 | ||
3135 | if (fdisk_gpt_set_partition_attrs(cxt, part, atters)) | |
3136 | return EXIT_FAILURE; | |
3137 | ||
3138 | if (fdisk_write_disklabel(cxt)) | |
3139 | return EXIT_FAILURE; | |
3140 | ||
3141 | fdisk_unref_context(cxt); | |
3142 | return 0; | |
3143 | } | |
3144 | ||
3145 | int main(int argc, char *argv[]) | |
3146 | { | |
3147 | struct fdisk_test tss[] = { | |
3148 | { "--getattr", test_getattr, "<disk> <partition> print attributes" }, | |
3149 | { "--setattr", test_setattr, "<disk> <partition> <value> set attributes" }, | |
3150 | { NULL } | |
3151 | }; | |
3152 | ||
3153 | return fdisk_run_test(tss, argc, argv); | |
3154 | } | |
3155 | ||
3156 | #endif |