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