]>
Commit | Line | Data |
---|---|---|
5dbff4c0 | 1 | /* |
766d5156 DB |
2 | * Copyright (C) 2007 Karel Zak <kzak@redhat.com> |
3 | * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org> | |
4 | * | |
5 | * GUID Partition Table (GPT) support. Based on UEFI Specs 2.3.1 | |
6 | * Chapter 5: GUID Partition Table (GPT) Disk Layout (Jun 27th, 2012). | |
7 | * Some ideas and inspiration from GNU parted and gptfdisk. | |
5dbff4c0 KZ |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA | |
5dbff4c0 KZ |
22 | */ |
23 | ||
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <stdlib.h> | |
27 | #include <inttypes.h> | |
28 | #include <sys/stat.h> | |
5dbff4c0 KZ |
29 | #include <sys/utsname.h> |
30 | #include <sys/types.h> | |
31 | #include <fcntl.h> | |
32 | #include <unistd.h> | |
33 | #include <errno.h> | |
766d5156 DB |
34 | #include <ctype.h> |
35 | #include <uuid.h> | |
5dbff4c0 | 36 | |
62d50bbe KZ |
37 | #include "fdiskP.h" |
38 | ||
766d5156 DB |
39 | #include "nls.h" |
40 | #include "xalloc.h" | |
766d5156 | 41 | #include "crc32.h" |
810f986b | 42 | #include "blkdev.h" |
9eca9d0d | 43 | #include "bitops.h" |
766d5156 | 44 | #include "strutils.h" |
19613111 | 45 | #include "all-io.h" |
766d5156 DB |
46 | |
47 | #define GPT_HEADER_SIGNATURE 0x5452415020494645LL /* EFI PART */ | |
48 | #define GPT_HEADER_REVISION_V1_02 0x00010200 | |
49 | #define GPT_HEADER_REVISION_V1_00 0x00010000 | |
50 | #define GPT_HEADER_REVISION_V0_99 0x00009900 | |
e9bf0935 | 51 | #define GPT_HEADER_MINSZ 92 /* bytes */ |
766d5156 DB |
52 | |
53 | #define GPT_PMBR_LBA 0 | |
54 | #define GPT_MBR_PROTECTIVE 1 | |
55 | #define GPT_MBR_HYBRID 2 | |
56 | ||
57 | #define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001 | |
58 | ||
59 | #define EFI_PMBR_OSTYPE 0xEE | |
60 | #define MSDOS_MBR_SIGNATURE 0xAA55 | |
3f731001 DB |
61 | #define GPT_PART_NAME_LEN 72 / sizeof(uint16_t) |
62 | #define GPT_NPARTITIONS 128 | |
766d5156 DB |
63 | |
64 | /* Globally unique identifier */ | |
65 | struct gpt_guid { | |
66 | uint32_t time_low; | |
67 | uint16_t time_mid; | |
68 | uint16_t time_hi_and_version; | |
69 | uint8_t clock_seq_hi; | |
70 | uint8_t clock_seq_low; | |
71 | uint8_t node[6]; | |
72 | }; | |
73 | ||
74 | ||
75 | /* only checking that the GUID is 0 is enough to verify an empty partition. */ | |
76 | #define GPT_UNUSED_ENTRY_GUID \ | |
77 | ((struct gpt_guid) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \ | |
78 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}) | |
79 | ||
80 | /* Linux native partition type */ | |
81 | #define GPT_DEFAULT_ENTRY_GUID \ | |
82 | ((struct gpt_guid) { 0x0FC63DAF, 0x8483, 0x4772, 0x8E, 0x79, \ | |
83 | { 0x3D, 0x69, 0xD8, 0x47, 0x7D, 0xE4 }}) | |
84 | ||
85 | ||
86 | /* | |
87 | * Attribute bits | |
88 | */ | |
89 | struct gpt_attr { | |
90 | uint64_t required_to_function:1; | |
91 | uint64_t no_blockio_protocol:1; | |
92 | uint64_t legacy_bios_bootable:1; | |
93 | uint64_t reserved:45; | |
94 | uint64_t guid_secific:16; | |
95 | } __attribute__ ((packed)); | |
96 | ||
97 | /* The GPT Partition entry array contains an array of GPT entries. */ | |
98 | struct gpt_entry { | |
3f731001 DB |
99 | struct gpt_guid partition_type_guid; /* purpose and type of the partition */ |
100 | struct gpt_guid unique_partition_guid; | |
766d5156 DB |
101 | uint64_t lba_start; |
102 | uint64_t lba_end; | |
103 | struct gpt_attr attr; | |
104 | uint16_t partition_name[GPT_PART_NAME_LEN]; | |
105 | } __attribute__ ((packed)); | |
106 | ||
107 | /* GPT header */ | |
108 | struct gpt_header { | |
109 | uint64_t signature; /* header identification */ | |
110 | uint32_t revision; /* header version */ | |
111 | uint32_t size; /* in bytes */ | |
112 | uint32_t crc32; /* header CRC checksum */ | |
113 | uint32_t reserved1; /* must be 0 */ | |
114 | uint64_t my_lba; /* LBA that contains this struct (LBA 1) */ | |
115 | uint64_t alternative_lba; /* backup GPT header */ | |
116 | uint64_t first_usable_lba; /* first usable logical block for partitions */ | |
117 | uint64_t last_usable_lba; /* last usable logical block for partitions */ | |
3f731001 | 118 | struct gpt_guid disk_guid; /* unique disk identifier */ |
766d5156 DB |
119 | uint64_t partition_entry_lba; /* stat LBA of the partition entry array */ |
120 | uint32_t npartition_entries; /* total partition entries - normally 128 */ | |
121 | uint32_t sizeof_partition_entry; /* bytes for each GUID pt */ | |
122 | uint32_t partition_entry_array_crc32; /* partition CRC checksum */ | |
123 | uint8_t reserved2[512 - 92]; /* must be 0 */ | |
124 | } __attribute__ ((packed)); | |
125 | ||
126 | struct gpt_record { | |
127 | uint8_t boot_indicator; /* unused by EFI, set to 0x80 for bootable */ | |
128 | uint8_t start_head; /* unused by EFI, pt start in CHS */ | |
129 | uint8_t start_sector; /* unused by EFI, pt start in CHS */ | |
130 | uint8_t start_track; | |
131 | uint8_t os_type; /* EFI and legacy non-EFI OS types */ | |
132 | uint8_t end_head; /* unused by EFI, pt end in CHS */ | |
133 | uint8_t end_sector; /* unused by EFI, pt end in CHS */ | |
134 | uint8_t end_track; /* unused by EFI, pt end in CHS */ | |
135 | uint32_t starting_lba; /* used by EFI - start addr of the on disk pt */ | |
136 | uint32_t size_in_lba; /* used by EFI - size of pt in LBA */ | |
137 | } __attribute__ ((packed)); | |
138 | ||
139 | /* Protected MBR and legacy MBR share same structure */ | |
140 | struct gpt_legacy_mbr { | |
141 | uint8_t boot_code[440]; | |
142 | uint32_t unique_mbr_signature; | |
143 | uint16_t unknown; | |
144 | struct gpt_record partition_record[4]; | |
145 | uint16_t signature; | |
146 | } __attribute__ ((packed)); | |
147 | ||
148 | /* | |
149 | * Here be dragons! | |
150 | * See: http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs | |
151 | */ | |
152 | #define DEF_GUID(_u, _n) \ | |
153 | { \ | |
154 | .typestr = (_u), \ | |
155 | .name = (_n), \ | |
156 | } | |
157 | ||
158 | static struct fdisk_parttype gpt_parttypes[] = | |
159 | { | |
160 | /* Generic OS */ | |
161 | DEF_GUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B", N_("EFI System")), | |
162 | ||
163 | DEF_GUID("024DEE41-33E7-11D3-9D69-0008C781F39F", N_("MBR partition scheme")), | |
164 | /* Hah!IdontneedEFI */ | |
165 | DEF_GUID("21686148-6449-6E6F-744E-656564454649", N_("BIOS boot partition")), | |
166 | ||
167 | /* Windows */ | |
168 | DEF_GUID("E3C9E316-0B5C-4DB8-817D-F92DF00215AE", N_("Microsoft reserved")), | |
169 | DEF_GUID("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", N_("Microsoft basic data")), | |
170 | DEF_GUID("5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", N_("Microsoft LDM metadata")), | |
171 | DEF_GUID("AF9B60A0-1431-4F62-BC68-3311714A69AD", N_("Microsoft LDM data")), | |
172 | DEF_GUID("DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", N_("Windows recovery evironmnet")), | |
173 | DEF_GUID("37AFFC90-EF7D-4E96-91C3-2D7AE055B174", N_("IBM General Parallel Fs")), | |
174 | ||
175 | /* HP-UX */ | |
176 | DEF_GUID("75894C1E-3AEB-11D3-B7C1-7B03A0000000", N_("HP-UX data partition")), | |
177 | DEF_GUID("E2A1E728-32E3-11D6-A682-7B03A0000000", N_("HP-UX service partition")), | |
178 | ||
179 | /* Linux */ | |
180 | DEF_GUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4", N_("Linux filesystem")), | |
181 | DEF_GUID("A19D880F-05FC-4D3B-A006-743F0F84911E", N_("Linux RAID")), | |
182 | DEF_GUID("0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", N_("Linux swap")), | |
183 | DEF_GUID("E6D6D379-F507-44C2-A23C-238F2A3DF928", N_("Linux LVM")), | |
184 | DEF_GUID("8DA63339-0007-60C0-C436-083AC8230908", N_("Linux reserved")), | |
185 | ||
e9bf0935 | 186 | /* FreeBSD */ |
766d5156 DB |
187 | DEF_GUID("516E7CB4-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD data")), |
188 | DEF_GUID("83BD6B9D-7F41-11DC-BE0B-001560B84F0F", N_("FreeBSD boot")), | |
189 | DEF_GUID("516E7CB5-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD swap")), | |
190 | DEF_GUID("516E7CB6-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD UFS")), | |
191 | DEF_GUID("516E7CBA-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD ZFS")), | |
192 | DEF_GUID("516E7CB8-6ECF-11D6-8FF8-00022D09712B", N_("FreeBSD Vinum")), | |
193 | ||
194 | /* Apple OSX */ | |
195 | DEF_GUID("48465300-0000-11AA-AA11-00306543ECAC", N_("Apple HFS/HFS+")), | |
196 | DEF_GUID("55465300-0000-11AA-AA11-00306543ECAC", N_("Apple UFS")), | |
197 | DEF_GUID("52414944-0000-11AA-AA11-00306543ECAC", N_("Apple RAID")), | |
198 | DEF_GUID("52414944-5F4F-11AA-AA11-00306543ECAC", N_("Apple RAID offline")), | |
199 | DEF_GUID("426F6F74-0000-11AA-AA11-00306543ECAC", N_("Apple boot")), | |
200 | DEF_GUID("4C616265-6C00-11AA-AA11-00306543ECAC", N_("Apple label")), | |
201 | DEF_GUID("5265636F-7665-11AA-AA11-00306543ECAC", N_("Apple TV recovery")), | |
202 | DEF_GUID("53746F72-6167-11AA-AA11-00306543ECAC", N_("Apple Core storage")), | |
203 | ||
204 | /* Solaris */ | |
205 | DEF_GUID("6A82CB45-1DD2-11B2-99A6-080020736631", N_("Solaris boot")), | |
206 | DEF_GUID("6A85CF4D-1DD2-11B2-99A6-080020736631", N_("Solaris root")), | |
207 | /* same as Apple ZFS */ | |
208 | DEF_GUID("6A898CC3-1DD2-11B2-99A6-080020736631", N_("Solaris /usr & Apple ZFS")), | |
209 | DEF_GUID("6A87C46F-1DD2-11B2-99A6-080020736631", N_("Solaris swap")), | |
210 | DEF_GUID("6A8B642B-1DD2-11B2-99A6-080020736631", N_("Solaris backup")), | |
211 | DEF_GUID("6A8EF2E9-1DD2-11B2-99A6-080020736631", N_("Solaris /var")), | |
212 | DEF_GUID("6A90BA39-1DD2-11B2-99A6-080020736631", N_("Solaris /home")), | |
213 | DEF_GUID("6A9283A5-1DD2-11B2-99A6-080020736631", N_("Solaris alternate sector")), | |
214 | DEF_GUID("6A945A3B-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 1")), | |
215 | DEF_GUID("6A9630D1-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 2")), | |
216 | DEF_GUID("6A980767-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 3")), | |
217 | DEF_GUID("6A96237F-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 4")), | |
218 | DEF_GUID("6A8D2AC7-1DD2-11B2-99A6-080020736631", N_("Solaris reserved 5")), | |
219 | ||
220 | /* NetBSD */ | |
221 | DEF_GUID("49F48D32-B10E-11DC-B99B-0019D1879648", N_("NetBSD swap")), | |
222 | DEF_GUID("49F48D5A-B10E-11DC-B99B-0019D1879648", N_("NetBSD FFS")), | |
223 | DEF_GUID("49F48D82-B10E-11DC-B99B-0019D1879648", N_("NetBSD LFS")), | |
224 | DEF_GUID("2DB519C4-B10E-11DC-B99B-0019D1879648", N_("NetBSD concatenated")), | |
225 | DEF_GUID("2DB519EC-B10E-11DC-B99B-0019D1879648", N_("NetBSD encrypted")), | |
226 | DEF_GUID("49F48DAA-B10E-11DC-B99B-0019D1879648", N_("NetBSD RAID")), | |
227 | ||
228 | /* ChromeOS */ | |
229 | DEF_GUID("FE3A2A5D-4F32-41A7-B725-ACCC3285A309", N_("ChromeOS kernel")), | |
230 | DEF_GUID("3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", N_("ChromeOS root fs")), | |
231 | DEF_GUID("2E0A753D-9E48-43B0-8337-B15192CB1B5E", N_("ChromeOS reserved")), | |
232 | ||
233 | /* MidnightBSD */ | |
234 | DEF_GUID("85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD data")), | |
235 | DEF_GUID("85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD boot")), | |
236 | DEF_GUID("85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD swap")), | |
237 | DEF_GUID("0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD UFS")), | |
238 | DEF_GUID("85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD ZFS")), | |
239 | DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")), | |
240 | }; | |
241 | ||
d71ef5a4 | 242 | /* gpt_entry macros */ |
874aa9c3 KZ |
243 | #define gpt_partition_start(_e) le64_to_cpu((_e)->lba_start) |
244 | #define gpt_partition_end(_e) le64_to_cpu((_e)->lba_end) | |
245 | ||
d71ef5a4 KZ |
246 | /* |
247 | * in-memory fdisk GPT stuff | |
248 | */ | |
249 | struct fdisk_gpt_label { | |
250 | struct fdisk_label head; /* generic part */ | |
251 | ||
252 | /* gpt specific part */ | |
253 | struct gpt_header *pheader; /* primary header */ | |
254 | struct gpt_header *bheader; /* backup header */ | |
255 | struct gpt_entry *ents; /* entries (partitions) */ | |
256 | }; | |
257 | ||
258 | static void gpt_deinit(struct fdisk_label *lb); | |
259 | ||
9ffeb235 | 260 | static inline struct fdisk_gpt_label *self_label(struct fdisk_context *cxt) |
d71ef5a4 | 261 | { |
d71ef5a4 KZ |
262 | return (struct fdisk_gpt_label *) cxt->label; |
263 | } | |
264 | ||
874aa9c3 KZ |
265 | /* |
266 | * Returns the partition length, or 0 if end is before beginning. | |
267 | */ | |
268 | static uint64_t gpt_partition_size(const struct gpt_entry *e) | |
269 | { | |
270 | uint64_t start = gpt_partition_start(e); | |
271 | uint64_t end = gpt_partition_end(e); | |
272 | ||
273 | return start > end ? 0 : end - start + 1ULL; | |
274 | } | |
275 | ||
766d5156 DB |
276 | /* |
277 | * UUID is traditionally 16 byte big-endian array, except Intel EFI | |
278 | * specification where the UUID is a structure of little-endian fields. | |
279 | */ | |
280 | static void swap_efi_guid(struct gpt_guid *uid) | |
281 | { | |
282 | uid->time_low = swab32(uid->time_low); | |
283 | uid->time_mid = swab16(uid->time_mid); | |
284 | uid->time_hi_and_version = swab16(uid->time_hi_and_version); | |
5dbff4c0 KZ |
285 | } |
286 | ||
766d5156 DB |
287 | static int string_to_uuid(const char *in, struct gpt_guid *uuid) |
288 | { | |
289 | if (uuid_parse(in, (unsigned char *) uuid)) | |
290 | return -1; | |
291 | ||
292 | swap_efi_guid(uuid); | |
293 | return 0; | |
294 | } | |
295 | ||
46667ba4 | 296 | static char *uuid_to_string(struct gpt_guid *uuid, char *out) |
766d5156 DB |
297 | { |
298 | uuid_unparse_upper((unsigned char *) uuid, out); | |
46667ba4 | 299 | return out; |
766d5156 DB |
300 | } |
301 | ||
302 | static const char *gpt_get_header_revstr(struct gpt_header *header) | |
303 | { | |
304 | if (!header) | |
305 | goto unknown; | |
306 | ||
307 | switch (header->revision) { | |
308 | case GPT_HEADER_REVISION_V1_02: | |
309 | return "1.2"; | |
310 | case GPT_HEADER_REVISION_V1_00: | |
311 | return "1.0"; | |
312 | case GPT_HEADER_REVISION_V0_99: | |
313 | return "0.99"; | |
314 | default: | |
315 | goto unknown; | |
316 | } | |
317 | ||
318 | unknown: | |
319 | return "unknown"; | |
320 | } | |
321 | ||
874aa9c3 | 322 | static inline int partition_unused(const struct gpt_entry *e) |
766d5156 | 323 | { |
874aa9c3 | 324 | return !memcmp(&e->partition_type_guid, &GPT_UNUSED_ENTRY_GUID, |
766d5156 DB |
325 | sizeof(struct gpt_guid)); |
326 | } | |
327 | ||
3f731001 DB |
328 | /* |
329 | * Builds a clean new valid protective MBR - will wipe out any existing data. | |
330 | * Returns 0 on success, otherwise < 0 on error. | |
331 | */ | |
332 | static int gpt_mknew_pmbr(struct fdisk_context *cxt) | |
333 | { | |
334 | struct gpt_legacy_mbr *pmbr = NULL; | |
335 | ||
336 | if (!cxt || !cxt->firstsector) | |
337 | return -ENOSYS; | |
338 | ||
339 | fdisk_zeroize_firstsector(cxt); | |
340 | ||
341 | pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; | |
342 | ||
343 | pmbr->signature = cpu_to_le16(MSDOS_MBR_SIGNATURE); | |
344 | pmbr->partition_record[0].os_type = EFI_PMBR_OSTYPE; | |
345 | pmbr->partition_record[0].start_sector = 1; | |
346 | pmbr->partition_record[0].end_head = 0xFE; | |
347 | pmbr->partition_record[0].end_sector = 0xFF; | |
348 | pmbr->partition_record[0].end_track = 0xFF; | |
349 | pmbr->partition_record[0].starting_lba = cpu_to_le32(1); | |
350 | pmbr->partition_record[0].size_in_lba = | |
351 | cpu_to_le32(min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF)); | |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
356 | /* some universal differences between the headers */ | |
357 | static void gpt_mknew_header_common(struct fdisk_context *cxt, | |
358 | struct gpt_header *header, uint64_t lba) | |
359 | { | |
360 | if (!cxt || !header) | |
361 | return; | |
362 | ||
363 | header->my_lba = cpu_to_le64(lba); | |
364 | ||
365 | if (lba == GPT_PRIMARY_PARTITION_TABLE_LBA) { /* primary */ | |
366 | header->alternative_lba = cpu_to_le64(cxt->total_sectors - 1); | |
367 | header->partition_entry_lba = cpu_to_le64(2); | |
368 | } else { /* backup */ | |
369 | uint64_t esz = le32_to_cpu(header->npartition_entries) * sizeof(struct gpt_entry); | |
370 | uint64_t esects = (esz + cxt->sector_size - 1) / cxt->sector_size; | |
371 | ||
372 | header->alternative_lba = cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA); | |
373 | header->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1 - esects); | |
374 | } | |
375 | } | |
376 | ||
377 | /* | |
378 | * Builds a new GPT header (at sector lba) from a backup header2. | |
379 | * If building a primary header, then backup is the secondary, and vice versa. | |
380 | * | |
381 | * Always pass a new (zeroized) header to build upon as we don't | |
382 | * explicitly zero-set some values such as CRCs and reserved. | |
383 | * | |
384 | * Returns 0 on success, otherwise < 0 on error. | |
385 | */ | |
386 | static int gpt_mknew_header_from_bkp(struct fdisk_context *cxt, | |
387 | struct gpt_header *header, | |
388 | uint64_t lba, | |
389 | struct gpt_header *header2) | |
390 | { | |
391 | if (!cxt || !header || !header2) | |
392 | return -ENOSYS; | |
393 | ||
394 | header->signature = header2->signature; | |
395 | header->revision = header2->revision; | |
396 | header->size = header2->size; | |
397 | header->npartition_entries = header2->npartition_entries; | |
398 | header->sizeof_partition_entry = header2->sizeof_partition_entry; | |
399 | header->first_usable_lba = header2->first_usable_lba; | |
400 | header->last_usable_lba = header2->last_usable_lba; | |
401 | ||
402 | memcpy(&header->disk_guid, | |
403 | &header2->disk_guid, sizeof(header2->disk_guid)); | |
404 | gpt_mknew_header_common(cxt, header, lba); | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | /* | |
410 | * Builds a clean new GPT header (currently under revision 1.0). | |
411 | * | |
412 | * Always pass a new (zeroized) header to build upon as we don't | |
413 | * explicitly zero-set some values such as CRCs and reserved. | |
414 | * | |
415 | * Returns 0 on success, otherwise < 0 on error. | |
416 | */ | |
417 | static int gpt_mknew_header(struct fdisk_context *cxt, | |
418 | struct gpt_header *header, uint64_t lba) | |
419 | { | |
b4184690 | 420 | uint64_t esz = 0, first, last; |
3f731001 DB |
421 | |
422 | if (!cxt || !header) | |
423 | return -ENOSYS; | |
424 | ||
425 | esz = sizeof(struct gpt_entry) * GPT_NPARTITIONS / cxt->sector_size; | |
426 | ||
427 | header->signature = cpu_to_le64(GPT_HEADER_SIGNATURE); | |
428 | header->revision = cpu_to_le32(GPT_HEADER_REVISION_V1_00); | |
429 | header->size = cpu_to_le32(sizeof(struct gpt_header)); | |
430 | ||
431 | /* | |
432 | * 128 partitions is the default. It can go behond this, however, | |
433 | * we're creating a de facto header here, so no funny business. | |
434 | */ | |
435 | header->npartition_entries = cpu_to_le32(GPT_NPARTITIONS); | |
436 | header->sizeof_partition_entry = cpu_to_le32(sizeof(struct gpt_entry)); | |
b4184690 KZ |
437 | |
438 | last = cxt->total_sectors - 2 - esz; | |
439 | first = esz + 2; | |
440 | ||
441 | if (first < cxt->first_lba && cxt->first_lba < last) | |
442 | /* Align according to topology */ | |
443 | first = cxt->first_lba; | |
444 | ||
445 | header->first_usable_lba = cpu_to_le64(first); | |
446 | header->last_usable_lba = cpu_to_le64(last); | |
3f731001 DB |
447 | |
448 | gpt_mknew_header_common(cxt, header, lba); | |
449 | uuid_generate_random((unsigned char *) &header->disk_guid); | |
450 | swap_efi_guid(&header->disk_guid); | |
451 | ||
452 | return 0; | |
453 | } | |
454 | ||
766d5156 DB |
455 | /* |
456 | * Checks if there is a valid protective MBR partition table. | |
457 | * Returns 0 if it is invalid or failure. Otherwise, return | |
458 | * GPT_MBR_PROTECTIVE or GPT_MBR_HYBRID, depeding on the detection. | |
459 | */ | |
460 | static int valid_pmbr(struct fdisk_context *cxt) | |
461 | { | |
462 | int i, ret = 0; /* invalid by default */ | |
463 | struct gpt_legacy_mbr *pmbr = NULL; | |
464 | ||
465 | if (!cxt->firstsector) | |
466 | goto done; | |
467 | ||
468 | pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; | |
469 | ||
470 | if (pmbr->signature != cpu_to_le64(MSDOS_MBR_SIGNATURE)) | |
471 | goto done; | |
472 | ||
473 | /* LBA of the GPT partition header */ | |
474 | if (pmbr->partition_record[0].starting_lba != | |
475 | cpu_to_le32(GPT_PRIMARY_PARTITION_TABLE_LBA)) | |
476 | goto done; | |
477 | ||
478 | /* seems like a valid MBR was found, check DOS primary partitions */ | |
479 | for (i = 0; i < 4; i++) | |
480 | if (pmbr->partition_record[i].os_type == EFI_PMBR_OSTYPE) { | |
481 | /* | |
482 | * Ok, we at least know that there's a protective MBR, | |
483 | * now check if there are other partition types for | |
484 | * hybrid MBR. | |
485 | */ | |
486 | ret = GPT_MBR_PROTECTIVE; | |
487 | goto check_hybrid; | |
488 | } | |
489 | ||
490 | check_hybrid: | |
491 | if (ret != GPT_MBR_PROTECTIVE) | |
492 | goto done; | |
493 | for (i = 0 ; i < 4; i++) | |
494 | if ((pmbr->partition_record[i].os_type != EFI_PMBR_OSTYPE) && | |
495 | (pmbr->partition_record[i].os_type != 0x00)) | |
496 | ret = GPT_MBR_HYBRID; | |
497 | ||
498 | /* | |
499 | * Protective MBRs take up the lesser of the whole disk | |
500 | * or 2 TiB (32bit LBA), ignoring the rest of the disk. | |
501 | * | |
502 | * Hybrid MBRs do not necessarily comply with this. | |
503 | */ | |
504 | if (ret == GPT_MBR_PROTECTIVE) | |
505 | if (pmbr->partition_record[0].size_in_lba != | |
506 | cpu_to_le32(min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF))) | |
507 | ret = 0; | |
508 | done: | |
509 | return ret; | |
510 | } | |
511 | ||
512 | static uint64_t last_lba(struct fdisk_context *cxt) | |
5dbff4c0 | 513 | { |
5dbff4c0 KZ |
514 | struct stat s; |
515 | ||
766d5156 DB |
516 | memset(&s, 0, sizeof(s)); |
517 | if (fstat(cxt->dev_fd, &s) == -1) { | |
960cf573 | 518 | fprintf(stderr, "last_lba() could not stat: %m\n"); |
5dbff4c0 KZ |
519 | return 0; |
520 | } | |
766d5156 | 521 | |
5dbff4c0 | 522 | if (S_ISBLK(s.st_mode)) |
766d5156 DB |
523 | return cxt->total_sectors - 1; |
524 | else if (S_ISREG(s.st_mode)) { | |
525 | uint64_t sectors = s.st_size >> cxt->sector_size; | |
526 | return (sectors / cxt->sector_size) - 1ULL; | |
527 | } else { | |
5dbff4c0 | 528 | fprintf(stderr, |
bba382aa | 529 | "last_lba(): I don't know how to handle files with mode %o\n", |
5dbff4c0 | 530 | s.st_mode); |
5dbff4c0 | 531 | } |
766d5156 | 532 | return 0; |
5dbff4c0 KZ |
533 | } |
534 | ||
766d5156 DB |
535 | static ssize_t read_lba(struct fdisk_context *cxt, uint64_t lba, |
536 | void *buffer, const size_t bytes) | |
5dbff4c0 | 537 | { |
766d5156 | 538 | off_t offset = lba * cxt->sector_size; |
5dbff4c0 | 539 | |
766d5156 DB |
540 | lseek(cxt->dev_fd, offset, SEEK_SET); |
541 | return read(cxt->dev_fd, buffer, bytes); | |
5dbff4c0 KZ |
542 | } |
543 | ||
766d5156 DB |
544 | |
545 | /* Returns the GPT entry array */ | |
be5f8061 | 546 | static struct gpt_entry *gpt_read_entries(struct fdisk_context *cxt, |
d71ef5a4 | 547 | struct gpt_header *header) |
5dbff4c0 | 548 | { |
d71ef5a4 KZ |
549 | ssize_t sz; |
550 | struct gpt_entry *ret = NULL; | |
551 | off_t offset; | |
552 | ||
553 | assert(cxt); | |
554 | assert(header); | |
555 | ||
556 | sz = le32_to_cpu(header->npartition_entries) * | |
557 | le32_to_cpu(header->sizeof_partition_entry); | |
558 | ||
46667ba4 | 559 | ret = calloc(1, sz); |
d71ef5a4 KZ |
560 | if (!ret) |
561 | return NULL; | |
562 | offset = le64_to_cpu(header->partition_entry_lba) * | |
766d5156 DB |
563 | cxt->sector_size; |
564 | ||
565 | if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) | |
d71ef5a4 | 566 | goto fail; |
766d5156 | 567 | if (sz != read(cxt->dev_fd, ret, sz)) |
d71ef5a4 | 568 | goto fail; |
766d5156 DB |
569 | |
570 | return ret; | |
d71ef5a4 KZ |
571 | |
572 | fail: | |
573 | free(ret); | |
574 | return NULL; | |
766d5156 DB |
575 | } |
576 | ||
577 | static inline uint32_t count_crc32(const unsigned char *buf, size_t len) | |
578 | { | |
579 | return (crc32(~0L, buf, len) ^ ~0L); | |
580 | } | |
581 | ||
582 | /* | |
583 | * Recompute header and partition array 32bit CRC checksums. | |
584 | * This function does not fail - if there's corruption, then it | |
585 | * will be reported when checksuming it again (ie: probing or verify). | |
586 | */ | |
d71ef5a4 | 587 | static void gpt_recompute_crc(struct gpt_header *header, struct gpt_entry *ents) |
766d5156 DB |
588 | { |
589 | uint32_t crc = 0; | |
590 | size_t entry_sz = 0; | |
591 | ||
592 | if (!header) | |
593 | return; | |
594 | ||
595 | /* header CRC */ | |
596 | header->crc32 = 0; | |
597 | crc = count_crc32((unsigned char *) header, le32_to_cpu(header->size)); | |
598 | header->crc32 = cpu_to_le32(crc); | |
599 | ||
600 | /* partition entry array CRC */ | |
601 | header->partition_entry_array_crc32 = 0; | |
602 | entry_sz = le32_to_cpu(header->npartition_entries) * | |
603 | le32_to_cpu(header->sizeof_partition_entry); | |
604 | ||
d71ef5a4 | 605 | crc = count_crc32((unsigned char *) ents, entry_sz); |
766d5156 DB |
606 | header->partition_entry_array_crc32 = cpu_to_le32(crc); |
607 | } | |
608 | ||
609 | /* | |
610 | * Compute the 32bit CRC checksum of the partition table header. | |
611 | * Returns 1 if it is valid, otherwise 0. | |
612 | */ | |
d71ef5a4 | 613 | static int gpt_check_header_crc(struct gpt_header *header, struct gpt_entry *ents) |
766d5156 DB |
614 | { |
615 | uint32_t crc, orgcrc = le32_to_cpu(header->crc32); | |
616 | ||
617 | header->crc32 = 0; | |
618 | crc = count_crc32((unsigned char *) header, le32_to_cpu(header->size)); | |
619 | header->crc32 = cpu_to_le32(orgcrc); | |
620 | ||
d71ef5a4 KZ |
621 | if (crc == le32_to_cpu(header->crc32)) |
622 | return 1; | |
623 | ||
766d5156 DB |
624 | /* |
625 | * If we have checksum mismatch it may be due to stale data, | |
626 | * like a partition being added or deleted. Recompute the CRC again | |
627 | * and make sure this is not the case. | |
628 | */ | |
d71ef5a4 | 629 | if (ents) { |
766d5156 DB |
630 | gpt_recompute_crc(header, ents); |
631 | orgcrc = le32_to_cpu(header->crc32); | |
632 | header->crc32 = 0; | |
633 | crc = count_crc32((unsigned char *) header, le32_to_cpu(header->size)); | |
634 | header->crc32 = cpu_to_le32(orgcrc); | |
635 | ||
636 | return crc == le32_to_cpu(header->crc32); | |
d71ef5a4 KZ |
637 | } |
638 | ||
639 | return 0; | |
766d5156 DB |
640 | } |
641 | ||
642 | /* | |
643 | * It initializes the partition entry array. | |
644 | * Returns 1 if the checksum is valid, otherwise 0. | |
645 | */ | |
d71ef5a4 KZ |
646 | static int gpt_check_entryarr_crc(struct gpt_header *header, |
647 | struct gpt_entry *ents) | |
766d5156 DB |
648 | { |
649 | int ret = 0; | |
650 | ssize_t entry_sz; | |
651 | uint32_t crc; | |
652 | ||
d71ef5a4 | 653 | if (!header || !ents) |
766d5156 DB |
654 | goto done; |
655 | ||
656 | entry_sz = le32_to_cpu(header->npartition_entries) * | |
657 | le32_to_cpu(header->sizeof_partition_entry); | |
658 | ||
659 | if (!entry_sz) | |
660 | goto done; | |
661 | ||
766d5156 DB |
662 | crc = count_crc32((unsigned char *) ents, entry_sz); |
663 | ret = (crc == le32_to_cpu(header->partition_entry_array_crc32)); | |
664 | done: | |
665 | return ret; | |
666 | } | |
667 | ||
668 | static int gpt_check_lba_sanity(struct fdisk_context *cxt, struct gpt_header *header) | |
669 | { | |
670 | int ret = 0; | |
671 | uint64_t lu, fu, lastlba = last_lba(cxt); | |
672 | ||
673 | fu = le64_to_cpu(header->first_usable_lba); | |
674 | lu = le64_to_cpu(header->last_usable_lba); | |
675 | ||
676 | /* check if first and last usable LBA make sense */ | |
677 | if (lu < fu) { | |
678 | DBG(LABEL, dbgprint("error: header last LBA is before first LBA")); | |
679 | goto done; | |
5dbff4c0 | 680 | } |
766d5156 DB |
681 | |
682 | /* check if first and last usable LBAs with the disk's last LBA */ | |
683 | if (fu > lastlba || lu > lastlba) { | |
684 | DBG(LABEL, dbgprint("error: header LBAs are after the disk's last LBA")); | |
685 | goto done; | |
686 | } | |
687 | ||
688 | /* the header has to be outside usable range */ | |
689 | if (fu < GPT_PRIMARY_PARTITION_TABLE_LBA && | |
690 | GPT_PRIMARY_PARTITION_TABLE_LBA < lu) { | |
691 | DBG(LABEL, dbgprint("error: header outside of usable range")); | |
692 | goto done; | |
693 | } | |
694 | ||
695 | ret = 1; /* sane */ | |
696 | done: | |
697 | return ret; | |
698 | } | |
699 | ||
700 | /* Check if there is a valid header signature */ | |
701 | static int gpt_check_signature(struct gpt_header *header) | |
702 | { | |
703 | return header->signature == cpu_to_le64(GPT_HEADER_SIGNATURE); | |
704 | } | |
705 | ||
706 | /* | |
707 | * Return the specified GPT Header, or NULL upon failure/invalid. | |
708 | * Note that all tests must pass to ensure a valid header, | |
709 | * we do not rely on only testing the signature for a valid probe. | |
710 | */ | |
d71ef5a4 KZ |
711 | static struct gpt_header *gpt_read_header(struct fdisk_context *cxt, |
712 | uint64_t lba, | |
713 | struct gpt_entry **_ents) | |
766d5156 DB |
714 | { |
715 | struct gpt_header *header = NULL; | |
d71ef5a4 | 716 | struct gpt_entry *ents = NULL; |
e9bf0935 | 717 | uint32_t hsz; |
766d5156 DB |
718 | |
719 | if (!cxt) | |
720 | return NULL; | |
721 | ||
46667ba4 KZ |
722 | header = calloc(1, sizeof(*header)); |
723 | if (!header) | |
724 | return NULL; | |
766d5156 | 725 | |
d71ef5a4 | 726 | /* read and verify header */ |
766d5156 DB |
727 | if (!read_lba(cxt, lba, header, sizeof(struct gpt_header))) |
728 | goto invalid; | |
729 | ||
730 | if (!gpt_check_signature(header)) | |
731 | goto invalid; | |
732 | ||
d71ef5a4 KZ |
733 | if (!gpt_check_header_crc(header, NULL)) |
734 | goto invalid; | |
735 | ||
736 | /* read and verify entries */ | |
737 | ents = gpt_read_entries(cxt, header); | |
738 | if (!ents) | |
739 | goto invalid; | |
740 | ||
741 | if (!gpt_check_entryarr_crc(header, ents)) | |
766d5156 DB |
742 | goto invalid; |
743 | ||
744 | if (!gpt_check_lba_sanity(cxt, header)) | |
745 | goto invalid; | |
746 | ||
747 | /* valid header must be at MyLBA */ | |
748 | if (le64_to_cpu(header->my_lba) != lba) | |
749 | goto invalid; | |
750 | ||
e9bf0935 DB |
751 | /* make sure header size is between 92 and sector size bytes */ |
752 | hsz = le32_to_cpu(header->size); | |
753 | if (hsz < GPT_HEADER_MINSZ || hsz > cxt->sector_size) | |
754 | goto invalid; | |
755 | ||
d71ef5a4 KZ |
756 | if (_ents) |
757 | *_ents = ents; | |
758 | else | |
759 | free(ents); | |
760 | ||
766d5156 DB |
761 | return header; |
762 | invalid: | |
763 | free(header); | |
d71ef5a4 | 764 | free(ents); |
766d5156 DB |
765 | return NULL; |
766 | } | |
767 | ||
766d5156 DB |
768 | /* |
769 | * Returns the number of partitions that are in use. | |
770 | */ | |
130820a8 | 771 | static unsigned partitions_in_use(struct gpt_header *header, struct gpt_entry *e) |
766d5156 DB |
772 | { |
773 | uint32_t i, used = 0; | |
774 | ||
775 | if (!header || ! e) | |
776 | return 0; | |
777 | ||
778 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) | |
874aa9c3 | 779 | if (!partition_unused(&e[i])) |
766d5156 DB |
780 | used++; |
781 | return used; | |
782 | } | |
783 | ||
766d5156 DB |
784 | |
785 | /* | |
786 | * Check if a partition is too big for the disk (sectors). | |
787 | * Returns the faulting partition number, otherwise 0. | |
788 | */ | |
130820a8 | 789 | static unsigned partition_check_too_big(struct gpt_header *header, |
766d5156 DB |
790 | struct gpt_entry *e, uint64_t sectors) |
791 | { | |
792 | uint32_t i; | |
793 | ||
794 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) { | |
874aa9c3 | 795 | if (partition_unused(&e[i])) |
766d5156 | 796 | continue; |
874aa9c3 | 797 | if (gpt_partition_end(&e[i]) >= sectors) |
766d5156 | 798 | return i + 1; |
5dbff4c0 | 799 | } |
766d5156 DB |
800 | |
801 | return 0; | |
5dbff4c0 KZ |
802 | } |
803 | ||
766d5156 DB |
804 | /* |
805 | * Check if a partition ends before it begins | |
806 | * Returns the faulting partition number, otherwise 0. | |
5dbff4c0 | 807 | */ |
130820a8 | 808 | static unsigned partition_start_after_end(struct gpt_header *header, struct gpt_entry *e) |
5dbff4c0 | 809 | { |
766d5156 | 810 | uint32_t i; |
5dbff4c0 | 811 | |
766d5156 | 812 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) { |
874aa9c3 | 813 | if (partition_unused(&e[i])) |
766d5156 | 814 | continue; |
874aa9c3 | 815 | if (gpt_partition_start(&e[i]) > gpt_partition_end(&e[i])) |
766d5156 | 816 | return i + 1; |
5dbff4c0 | 817 | } |
766d5156 DB |
818 | |
819 | return 0; | |
5dbff4c0 KZ |
820 | } |
821 | ||
766d5156 DB |
822 | /* |
823 | * Check if partition e1 overlaps with partition e2 | |
824 | */ | |
874aa9c3 | 825 | static inline int partition_overlap(struct gpt_entry *e1, struct gpt_entry *e2) |
5dbff4c0 | 826 | { |
874aa9c3 KZ |
827 | uint64_t start1 = gpt_partition_start(e1); |
828 | uint64_t end1 = gpt_partition_end(e1); | |
829 | uint64_t start2 = gpt_partition_start(e2); | |
830 | uint64_t end2 = gpt_partition_end(e2); | |
831 | ||
832 | return (start1 && start2 && (start1 <= end2) != (end1 < start2)); | |
766d5156 DB |
833 | } |
834 | ||
835 | /* | |
836 | * Find any paritions that overlap. | |
837 | */ | |
130820a8 | 838 | static unsigned partition_check_overlaps(struct gpt_header *header, struct gpt_entry *e) |
766d5156 DB |
839 | { |
840 | uint32_t i, j; | |
841 | ||
842 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) | |
843 | for (j = 0; j < i; j++) { | |
874aa9c3 KZ |
844 | if (partition_unused(&e[i]) || |
845 | partition_unused(&e[j])) | |
766d5156 | 846 | continue; |
874aa9c3 | 847 | if (partition_overlap(&e[i], &e[j])) |
766d5156 DB |
848 | /* two overlaping partitions is enough! */ |
849 | return i + 1; | |
850 | } | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | /* | |
856 | * Find the first available block after the starting point; returns 0 if | |
857 | * there are no available blocks left, or error. From gdisk. | |
858 | */ | |
859 | static uint64_t find_first_available(struct gpt_header *header, | |
860 | struct gpt_entry *e, uint64_t start) | |
861 | { | |
862 | uint64_t first; | |
863 | uint32_t i, first_moved = 0; | |
864 | ||
602ebe7d KZ |
865 | uint64_t fu, lu; |
866 | ||
766d5156 | 867 | if (!header || !e) |
5dbff4c0 | 868 | return 0; |
766d5156 | 869 | |
602ebe7d KZ |
870 | fu = le64_to_cpu(header->first_usable_lba); |
871 | lu = le64_to_cpu(header->last_usable_lba); | |
872 | ||
766d5156 DB |
873 | /* |
874 | * Begin from the specified starting point or from the first usable | |
875 | * LBA, whichever is greater... | |
876 | */ | |
602ebe7d | 877 | first = start < fu ? fu : start; |
766d5156 DB |
878 | |
879 | /* | |
880 | * Now search through all partitions; if first is within an | |
881 | * existing partition, move it to the next sector after that | |
882 | * partition and repeat. If first was moved, set firstMoved | |
883 | * flag; repeat until firstMoved is not set, so as to catch | |
884 | * cases where partitions are out of sequential order.... | |
885 | */ | |
886 | do { | |
887 | first_moved = 0; | |
888 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) { | |
874aa9c3 | 889 | if (partition_unused(&e[i])) |
766d5156 | 890 | continue; |
874aa9c3 | 891 | if (first < gpt_partition_start(&e[i])) |
766d5156 | 892 | continue; |
874aa9c3 KZ |
893 | if (first <= gpt_partition_end(&e[i])) { |
894 | first = gpt_partition_end(&e[i]) + 1; | |
766d5156 DB |
895 | first_moved = 1; |
896 | } | |
897 | } | |
898 | } while (first_moved == 1); | |
899 | ||
602ebe7d | 900 | if (first > lu) |
766d5156 DB |
901 | first = 0; |
902 | ||
903 | return first; | |
5dbff4c0 KZ |
904 | } |
905 | ||
766d5156 DB |
906 | |
907 | /* Returns last available sector in the free space pointed to by start. From gdisk. */ | |
908 | static uint64_t find_last_free(struct gpt_header *header, | |
909 | struct gpt_entry *e, uint64_t start) | |
5dbff4c0 | 910 | { |
766d5156 DB |
911 | uint32_t i; |
912 | uint64_t nearest_start; | |
913 | ||
914 | if (!header || !e) | |
915 | return 0; | |
916 | ||
602ebe7d KZ |
917 | nearest_start = le64_to_cpu(header->last_usable_lba); |
918 | ||
766d5156 | 919 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) { |
602ebe7d KZ |
920 | uint64_t ps = gpt_partition_start(&e[i]); |
921 | ||
922 | if (nearest_start > ps && ps > start) | |
923 | nearest_start = ps - 1; | |
5dbff4c0 | 924 | } |
766d5156 DB |
925 | |
926 | return nearest_start; | |
5dbff4c0 | 927 | } |
766d5156 DB |
928 | |
929 | /* Returns the last free sector on the disk. From gdisk. */ | |
930 | static uint64_t find_last_free_sector(struct gpt_header *header, | |
931 | struct gpt_entry *e) | |
932 | { | |
933 | uint32_t i, last_moved; | |
934 | uint64_t last = 0; | |
935 | ||
936 | if (!header || !e) | |
937 | goto done; | |
938 | ||
939 | /* start by assuming the last usable LBA is available */ | |
602ebe7d | 940 | last = le64_to_cpu(header->last_usable_lba); |
766d5156 DB |
941 | do { |
942 | last_moved = 0; | |
943 | for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) { | |
874aa9c3 KZ |
944 | if ((last >= gpt_partition_start(&e[i])) && |
945 | (last <= gpt_partition_end(&e[i]))) { | |
946 | last = gpt_partition_start(&e[i]) - 1; | |
766d5156 DB |
947 | last_moved = 1; |
948 | } | |
949 | } | |
950 | } while (last_moved == 1); | |
951 | done: | |
952 | return last; | |
953 | } | |
954 | ||
955 | /* | |
956 | * Finds the first available sector in the largest block of unallocated | |
957 | * space on the disk. Returns 0 if there are no available blocks left. | |
958 | * From gdisk. | |
959 | */ | |
960 | static uint64_t find_first_in_largest(struct gpt_header *header, struct gpt_entry *e) | |
961 | { | |
962 | uint64_t start = 0, first_sect, last_sect; | |
963 | uint64_t segment_size, selected_size = 0, selected_segment = 0; | |
964 | ||
965 | if (!header || !e) | |
966 | goto done; | |
967 | ||
968 | do { | |
969 | first_sect = find_first_available(header, e, start); | |
970 | if (first_sect != 0) { | |
971 | last_sect = find_last_free(header, e, first_sect); | |
972 | segment_size = last_sect - first_sect + 1; | |
973 | ||
974 | if (segment_size > selected_size) { | |
975 | selected_size = segment_size; | |
976 | selected_segment = first_sect; | |
977 | } | |
978 | start = last_sect + 1; | |
979 | } | |
980 | } while (first_sect != 0); | |
981 | ||
982 | done: | |
983 | return selected_segment; | |
984 | } | |
985 | ||
986 | /* | |
987 | * Find the total number of free sectors, the number of segments in which | |
988 | * they reside, and the size of the largest of those segments. From gdisk. | |
989 | */ | |
990 | static uint64_t get_free_sectors(struct fdisk_context *cxt, struct gpt_header *header, | |
991 | struct gpt_entry *e, uint32_t *nsegments, | |
992 | uint64_t *largest_segment) | |
993 | { | |
994 | uint32_t num = 0; | |
995 | uint64_t first_sect, last_sect; | |
996 | uint64_t largest_seg = 0, segment_sz; | |
997 | uint64_t totfound = 0, start = 0; /* starting point for each search */ | |
998 | ||
999 | if (!cxt->total_sectors) | |
1000 | goto done; | |
1001 | ||
1002 | do { | |
1003 | first_sect = find_first_available(header, e, start); | |
1004 | if (first_sect) { | |
1005 | last_sect = find_last_free(header, e, first_sect); | |
1006 | segment_sz = last_sect - first_sect + 1; | |
1007 | ||
1008 | if (segment_sz > largest_seg) | |
1009 | largest_seg = segment_sz; | |
1010 | totfound += segment_sz; | |
1011 | num++; | |
1012 | start = last_sect + 1; | |
1013 | } | |
1014 | } while (first_sect); | |
1015 | ||
1016 | done: | |
512a430f KZ |
1017 | if (nsegments) |
1018 | *nsegments = num; | |
1019 | if (largest_segment) | |
1020 | *largest_segment = largest_seg; | |
766d5156 DB |
1021 | |
1022 | return totfound; | |
1023 | } | |
1024 | ||
9ffeb235 | 1025 | static int gpt_probe_label(struct fdisk_context *cxt) |
766d5156 DB |
1026 | { |
1027 | int mbr_type; | |
9ffeb235 | 1028 | struct fdisk_gpt_label *gpt; |
766d5156 | 1029 | |
9ffeb235 KZ |
1030 | assert(cxt); |
1031 | assert(cxt->label); | |
1032 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1033 | ||
1034 | gpt = self_label(cxt); | |
766d5156 DB |
1035 | |
1036 | mbr_type = valid_pmbr(cxt); | |
1037 | if (!mbr_type) | |
1038 | goto failed; | |
1039 | ||
1040 | DBG(LABEL, dbgprint("found a %s MBR", mbr_type == GPT_MBR_PROTECTIVE ? | |
1041 | "protective" : "hybrid")); | |
1042 | ||
d71ef5a4 KZ |
1043 | /* primary header */ |
1044 | gpt->pheader = gpt_read_header(cxt, GPT_PRIMARY_PARTITION_TABLE_LBA, | |
1045 | &gpt->ents); | |
766d5156 DB |
1046 | |
1047 | /* | |
1048 | * TODO: If the primary GPT is corrupt, we must check the last LBA of the | |
1049 | * device to see if it has a valid GPT Header and point to a valid GPT | |
1050 | * Partition Entry Array. | |
1051 | * If it points to a valid GPT Partition Entry Array, then software should | |
1052 | * restore the primary GPT if allowed by platform policy settings. | |
1053 | * | |
1054 | * For now we just abort GPT probing! | |
1055 | */ | |
d71ef5a4 | 1056 | if (!gpt->pheader || !gpt->ents) |
766d5156 DB |
1057 | goto failed; |
1058 | ||
1059 | /* OK, probing passed, now initialize backup header and fdisk variables. */ | |
d71ef5a4 KZ |
1060 | gpt->bheader = gpt_read_header(cxt, last_lba(cxt), NULL); |
1061 | ||
9ffeb235 KZ |
1062 | cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); |
1063 | cxt->label->nparts_cur = partitions_in_use(gpt->pheader, gpt->ents); | |
9fcd49d5 | 1064 | |
766d5156 DB |
1065 | printf(_("\nWARNING: fdisk GPT support is currently new, and therefore " |
1066 | "in an experimental phase. Use at your own discretion.\n\n")); | |
1067 | ||
1068 | return 1; | |
1069 | failed: | |
4e0e8253 | 1070 | DBG(LABEL, dbgprint("GPT probe failed")); |
9ffeb235 | 1071 | gpt_deinit(cxt->label); |
766d5156 DB |
1072 | return 0; |
1073 | } | |
1074 | ||
1075 | /* | |
1076 | * Stolen from libblkid - can be removed once partition semantics | |
1077 | * are added to the fdisk API. | |
1078 | */ | |
1079 | static char *encode_to_utf8(unsigned char *src, size_t count) | |
1080 | { | |
1081 | uint16_t c; | |
1082 | char *dest = xmalloc(count * sizeof(char)); | |
1083 | size_t i, j, len = count; | |
3f731001 | 1084 | |
766d5156 DB |
1085 | memset(dest, 0, sizeof(char) * count); |
1086 | ||
1087 | for (j = i = 0; i + 2 <= count; i += 2) { | |
1088 | /* always little endian */ | |
1089 | c = (src[i+1] << 8) | src[i]; | |
1090 | if (c == 0) { | |
1091 | dest[j] = '\0'; | |
1092 | break; | |
1093 | } else if (c < 0x80) { | |
1094 | if (j+1 >= len) | |
1095 | break; | |
1096 | dest[j++] = (uint8_t) c; | |
1097 | } else if (c < 0x800) { | |
1098 | if (j+2 >= len) | |
1099 | break; | |
1100 | dest[j++] = (uint8_t) (0xc0 | (c >> 6)); | |
1101 | dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); | |
1102 | } else { | |
1103 | if (j+3 >= len) | |
1104 | break; | |
1105 | dest[j++] = (uint8_t) (0xe0 | (c >> 12)); | |
1106 | dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f)); | |
1107 | dest[j++] = (uint8_t) (0x80 | (c & 0x3f)); | |
1108 | } | |
1109 | } | |
1110 | dest[j] = '\0'; | |
1111 | ||
1112 | return dest; | |
1113 | } | |
1114 | ||
1115 | /* | |
1116 | * List label partitions. | |
1117 | * This function must currently exist to comply with standard fdisk | |
1118 | * requirements, but once partition semantics are added to the fdisk | |
1119 | * API it can be removed for custom implementation (see gpt_label struct). | |
1120 | */ | |
1121 | void gpt_list_table(struct fdisk_context *cxt, | |
1122 | int xtra __attribute__ ((__unused__))) | |
1123 | { | |
1124 | uint32_t i; | |
9ffeb235 KZ |
1125 | struct fdisk_gpt_label *gpt; |
1126 | uint64_t fu; | |
1127 | uint64_t lu; | |
1128 | ||
1129 | assert(cxt); | |
1130 | assert(cxt->label); | |
1131 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1132 | ||
1133 | gpt = self_label(cxt); | |
1134 | fu = le64_to_cpu(gpt->pheader->first_usable_lba); | |
1135 | lu = le64_to_cpu(gpt->pheader->last_usable_lba); | |
766d5156 DB |
1136 | |
1137 | printf("\n# Start End Size Type Name\n"); | |
1138 | ||
d71ef5a4 | 1139 | for (i = 0; i < le32_to_cpu(gpt->pheader->npartition_entries); i++) { |
766d5156 | 1140 | char *name = NULL, *sizestr = NULL; |
d71ef5a4 KZ |
1141 | uint64_t start = gpt_partition_start(&gpt->ents[i]); |
1142 | uint64_t size = gpt_partition_size(&gpt->ents[i]); | |
766d5156 DB |
1143 | struct fdisk_parttype *t; |
1144 | ||
46667ba4 | 1145 | if (partition_unused(&gpt->ents[i]) || start == 0) |
766d5156 DB |
1146 | continue; |
1147 | ||
1148 | /* the partition has to inside usable range */ | |
1149 | if (start < fu || start + size - 1 > lu) | |
1150 | continue; | |
1151 | ||
d71ef5a4 KZ |
1152 | name = encode_to_utf8((unsigned char *)gpt->ents[i].partition_name, |
1153 | sizeof(gpt->ents[i].partition_name)); | |
766d5156 DB |
1154 | if (!name) |
1155 | continue; | |
1156 | sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, | |
1157 | size * cxt->sector_size); | |
1158 | if (!sizestr) | |
1159 | continue; | |
1160 | ||
1161 | t = fdisk_get_partition_type(cxt, i); | |
1162 | ||
413df906 | 1163 | printf("%2d %12ju %12ju %6s %-15.15s %s\n", |
766d5156 | 1164 | i+1, |
413df906 | 1165 | start, |
d71ef5a4 | 1166 | gpt_partition_end(&gpt->ents[i]), |
766d5156 DB |
1167 | sizestr, |
1168 | t->name, | |
1169 | name); | |
1170 | ||
9475cc78 | 1171 | fdisk_warn_alignment(cxt, start, i); |
413df906 | 1172 | |
766d5156 DB |
1173 | free(name); |
1174 | free(sizestr); | |
1175 | fdisk_free_parttype(t); | |
1176 | } | |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * Write partitions. | |
1181 | * Returns 0 on success, or corresponding error otherwise. | |
1182 | */ | |
1183 | static int gpt_write_partitions(struct fdisk_context *cxt, | |
d71ef5a4 | 1184 | struct gpt_header *header, struct gpt_entry *ents) |
766d5156 DB |
1185 | { |
1186 | off_t offset = le64_to_cpu(header->partition_entry_lba) * cxt->sector_size; | |
1187 | uint32_t nparts = le32_to_cpu(header->npartition_entries); | |
1188 | uint32_t totwrite = nparts * le32_to_cpu(header->sizeof_partition_entry); | |
130820a8 | 1189 | ssize_t rc; |
766d5156 DB |
1190 | |
1191 | if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) | |
1192 | goto fail; | |
d71ef5a4 KZ |
1193 | |
1194 | rc = write(cxt->dev_fd, ents, totwrite); | |
130820a8 | 1195 | if (rc > 0 && totwrite == (uint32_t) rc) |
766d5156 DB |
1196 | return 0; |
1197 | fail: | |
1198 | return -errno; | |
1199 | } | |
1200 | ||
1201 | /* | |
1202 | * Write a GPT header to a specified LBA | |
1203 | * Returns 0 on success, or corresponding error otherwise. | |
1204 | */ | |
1205 | static int gpt_write_header(struct fdisk_context *cxt, | |
1206 | struct gpt_header *header, uint64_t lba) | |
1207 | { | |
1208 | off_t offset = lba * cxt->sector_size; | |
1209 | ||
1210 | if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) | |
1211 | goto fail; | |
1212 | if (cxt->sector_size == | |
1213 | (size_t) write(cxt->dev_fd, header, cxt->sector_size)) | |
1214 | return 0; | |
1215 | fail: | |
1216 | return -errno; | |
1217 | } | |
1218 | ||
1219 | /* | |
1220 | * Write the protective MBR. | |
1221 | * Returns 0 on success, or corresponding error otherwise. | |
1222 | */ | |
1223 | static int gpt_write_pmbr(struct fdisk_context *cxt) | |
1224 | { | |
1225 | off_t offset; | |
1226 | struct gpt_legacy_mbr *pmbr = NULL; | |
1227 | ||
9ffeb235 KZ |
1228 | assert(cxt); |
1229 | assert(cxt->firstsector); | |
766d5156 DB |
1230 | |
1231 | pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; | |
1232 | ||
1233 | /* zero out the legacy partitions */ | |
1234 | memset(pmbr->partition_record, 0, sizeof(pmbr->partition_record)); | |
1235 | ||
1236 | pmbr->signature = cpu_to_le16(MSDOS_MBR_SIGNATURE); | |
1237 | pmbr->partition_record[0].os_type = EFI_PMBR_OSTYPE; | |
1238 | pmbr->partition_record[0].start_sector = 1; | |
1239 | pmbr->partition_record[0].end_head = 0xFE; | |
1240 | pmbr->partition_record[0].end_sector = 0xFF; | |
1241 | pmbr->partition_record[0].end_track = 0xFF; | |
1242 | pmbr->partition_record[0].starting_lba = cpu_to_le32(1); | |
1243 | ||
1244 | /* | |
1245 | * Set size_in_lba to the size of the disk minus one. If the size of the disk | |
1246 | * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF. | |
1247 | */ | |
1248 | if (cxt->total_sectors - 1 > 0xFFFFFFFFULL) | |
1249 | pmbr->partition_record[0].size_in_lba = cpu_to_le32(0xFFFFFFFF); | |
1250 | else | |
1251 | pmbr->partition_record[0].size_in_lba = | |
1252 | cpu_to_le32(cxt->total_sectors - 1UL); | |
1253 | ||
1254 | offset = GPT_PMBR_LBA * cxt->sector_size; | |
1255 | if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) | |
1256 | goto fail; | |
1257 | ||
19613111 DB |
1258 | /* pMBR covers the first sector (LBA) of the disk */ |
1259 | if (write_all(cxt->dev_fd, pmbr, cxt->sector_size)) | |
1260 | goto fail; | |
1261 | return 0; | |
766d5156 DB |
1262 | fail: |
1263 | return -errno; | |
1264 | } | |
1265 | ||
1266 | /* | |
1267 | * Writes in-memory GPT and pMBR data to disk. | |
1268 | * Returns 0 if successful write, otherwise, a corresponding error. | |
1269 | * Any indication of error will abort the operation. | |
1270 | */ | |
9ffeb235 | 1271 | static int gpt_write_disklabel(struct fdisk_context *cxt) |
766d5156 | 1272 | { |
9ffeb235 | 1273 | struct fdisk_gpt_label *gpt; |
d71ef5a4 | 1274 | |
9ffeb235 KZ |
1275 | assert(cxt); |
1276 | assert(cxt->label); | |
1277 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1278 | ||
1279 | gpt = self_label(cxt); | |
766d5156 DB |
1280 | |
1281 | /* we do not want to mess up hybrid MBRs by creating a valid pmbr */ | |
1282 | if (valid_pmbr(cxt) == GPT_MBR_HYBRID) | |
1283 | goto err0; | |
1284 | ||
1285 | /* check that disk is big enough to handle the backup header */ | |
d71ef5a4 | 1286 | if (gpt->pheader->alternative_lba > cxt->total_sectors) |
766d5156 DB |
1287 | goto err0; |
1288 | ||
1289 | /* check that the backup header is properly placed */ | |
d71ef5a4 | 1290 | if (gpt->pheader->alternative_lba < cxt->total_sectors - 1) |
766d5156 DB |
1291 | /* TODO: correct this (with user authorization) and write */ |
1292 | goto err0; | |
1293 | ||
d71ef5a4 | 1294 | if (partition_check_overlaps(gpt->pheader, gpt->ents)) |
766d5156 DB |
1295 | goto err0; |
1296 | ||
1297 | /* recompute CRCs for both headers */ | |
d71ef5a4 KZ |
1298 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
1299 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
766d5156 DB |
1300 | |
1301 | /* | |
1302 | * UEFI requires writing in this specific order: | |
1303 | * 1) backup partition tables | |
1304 | * 2) backup GPT header | |
1305 | * 3) primary partition tables | |
1306 | * 4) primary GPT header | |
1307 | * 5) protective MBR | |
1308 | * | |
1309 | * If any write fails, we abort the rest. | |
1310 | */ | |
d71ef5a4 | 1311 | if (gpt_write_partitions(cxt, gpt->bheader, gpt->ents) != 0) |
766d5156 | 1312 | goto err1; |
d71ef5a4 | 1313 | if (gpt_write_header(cxt, gpt->bheader, gpt->pheader->alternative_lba) != 0) |
766d5156 | 1314 | goto err1; |
d71ef5a4 | 1315 | if (gpt_write_partitions(cxt, gpt->pheader, gpt->ents) != 0) |
766d5156 | 1316 | goto err1; |
d71ef5a4 | 1317 | if (gpt_write_header(cxt, gpt->pheader, GPT_PRIMARY_PARTITION_TABLE_LBA) != 0) |
766d5156 DB |
1318 | goto err1; |
1319 | if (gpt_write_pmbr(cxt) != 0) | |
1320 | goto err1; | |
1321 | ||
1322 | return 0; | |
1323 | err0: | |
1324 | return -EINVAL; | |
1325 | err1: | |
1326 | return -errno; | |
1327 | } | |
1328 | ||
1329 | /* | |
1330 | * Verify data integrity and report any found problems for: | |
1331 | * - primary and backup header validations | |
1332 | * - paritition validations | |
1333 | */ | |
9ffeb235 | 1334 | static int gpt_verify_disklabel(struct fdisk_context *cxt) |
766d5156 | 1335 | { |
130820a8 | 1336 | int nerror = 0, ptnum; |
9ffeb235 KZ |
1337 | struct fdisk_gpt_label *gpt; |
1338 | ||
1339 | assert(cxt); | |
1340 | assert(cxt->label); | |
1341 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1342 | ||
1343 | gpt = self_label(cxt); | |
766d5156 | 1344 | |
d71ef5a4 | 1345 | if (!gpt || !gpt->bheader) { |
766d5156 DB |
1346 | nerror++; |
1347 | printf(_("Disk does not contain a valid backup header.\n")); | |
1348 | } | |
1349 | ||
d71ef5a4 | 1350 | if (!gpt_check_header_crc(gpt->pheader, gpt->ents)) { |
766d5156 DB |
1351 | nerror++; |
1352 | printf(_("Invalid primary header CRC checksum.\n")); | |
1353 | } | |
d71ef5a4 | 1354 | if (gpt->bheader && !gpt_check_header_crc(gpt->bheader, gpt->ents)) { |
766d5156 DB |
1355 | nerror++; |
1356 | printf(_("Invalid backup header CRC checksum.\n")); | |
1357 | } | |
1358 | ||
d71ef5a4 | 1359 | if (!gpt_check_entryarr_crc(gpt->pheader, gpt->ents)) { |
766d5156 DB |
1360 | nerror++; |
1361 | printf(_("Invalid partition entry checksum.\n")); | |
1362 | } | |
1363 | ||
d71ef5a4 | 1364 | if (!gpt_check_lba_sanity(cxt, gpt->pheader)) { |
766d5156 DB |
1365 | nerror++; |
1366 | printf(_("Invalid primary header LBA sanity checks.\n")); | |
1367 | } | |
d71ef5a4 | 1368 | if (gpt->bheader && !gpt_check_lba_sanity(cxt, gpt->bheader)) { |
766d5156 DB |
1369 | nerror++; |
1370 | printf(_("Invalid backup header LBA sanity checks.\n")); | |
1371 | } | |
1372 | ||
d71ef5a4 | 1373 | if (le64_to_cpu(gpt->pheader->my_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA) { |
766d5156 DB |
1374 | nerror++; |
1375 | printf(_("MyLBA mismatch with real position at primary header.\n")); | |
1376 | } | |
d71ef5a4 | 1377 | if (gpt->bheader && le64_to_cpu(gpt->bheader->my_lba) != last_lba(cxt)) { |
766d5156 DB |
1378 | nerror++; |
1379 | printf(_("MyLBA mismatch with real position at backup header.\n")); | |
1380 | ||
1381 | } | |
1382 | ||
d71ef5a4 | 1383 | if (gpt->pheader->alternative_lba >= cxt->total_sectors) { |
766d5156 DB |
1384 | nerror++; |
1385 | printf(_("Disk is to small to hold all data.\n")); | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * if the GPT is the primary table, check the alternateLBA | |
1390 | * to see if it is a valid GPT | |
1391 | */ | |
d71ef5a4 | 1392 | if (gpt->bheader && (gpt->pheader->my_lba != gpt->bheader->alternative_lba)) { |
766d5156 DB |
1393 | nerror++; |
1394 | printf(_("Primary and backup header mismatch.\n")); | |
1395 | } | |
1396 | ||
d71ef5a4 | 1397 | ptnum = partition_check_overlaps(gpt->pheader, gpt->ents); |
766d5156 DB |
1398 | if (ptnum) { |
1399 | nerror++; | |
130820a8 KZ |
1400 | printf(_("Partition %d overlaps with partition %d.\n"), |
1401 | ptnum, ptnum + 1); | |
766d5156 DB |
1402 | } |
1403 | ||
d71ef5a4 | 1404 | ptnum = partition_check_too_big(gpt->pheader, gpt->ents, cxt->total_sectors); |
766d5156 DB |
1405 | if (ptnum) { |
1406 | nerror++; | |
130820a8 | 1407 | printf(_("Partition %u is too big for the disk.\n"), ptnum); |
766d5156 DB |
1408 | } |
1409 | ||
d71ef5a4 | 1410 | ptnum = partition_start_after_end(gpt->pheader, gpt->ents); |
766d5156 DB |
1411 | if (ptnum) { |
1412 | nerror++; | |
130820a8 | 1413 | printf(_("Partition %u ends before it starts.\n"), ptnum); |
766d5156 DB |
1414 | } |
1415 | ||
1416 | if (!nerror) { /* yay :-) */ | |
1417 | uint32_t nsegments = 0; | |
1418 | uint64_t free_sectors = 0, largest_segment = 0; | |
1419 | ||
1420 | printf(_("No errors detected\n")); | |
d71ef5a4 | 1421 | printf(_("Header version: %s\n"), gpt_get_header_revstr(gpt->pheader)); |
766d5156 | 1422 | printf(_("Using %d out of %d partitions\n"), |
d71ef5a4 KZ |
1423 | partitions_in_use(gpt->pheader, gpt->ents), |
1424 | le32_to_cpu(gpt->pheader->npartition_entries)); | |
766d5156 | 1425 | |
d71ef5a4 | 1426 | free_sectors = get_free_sectors(cxt, gpt->pheader, gpt->ents, |
766d5156 | 1427 | &nsegments, &largest_segment); |
130820a8 KZ |
1428 | printf(_("A total of %ju free sectors available in %u segment(s) " |
1429 | "(largest %ju).\n"), | |
766d5156 DB |
1430 | free_sectors, nsegments, largest_segment); |
1431 | } else | |
1432 | printf(_("Detected %d error(s).\n"), nerror); | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
1437 | /* Delete a single GPT partition, specified by partnum. */ | |
8a95621d | 1438 | static int gpt_delete_partition(struct fdisk_context *cxt, |
9ffeb235 | 1439 | size_t partnum) |
766d5156 | 1440 | { |
9ffeb235 | 1441 | struct fdisk_gpt_label *gpt; |
d71ef5a4 | 1442 | |
9ffeb235 KZ |
1443 | assert(cxt); |
1444 | assert(cxt->label); | |
1445 | assert(fdisk_is_disklabel(cxt, GPT)); | |
d71ef5a4 | 1446 | |
9ffeb235 KZ |
1447 | gpt = self_label(cxt); |
1448 | ||
1449 | if (partnum >= cxt->label->nparts_max | |
1450 | || partition_unused(&gpt->ents[partnum])) | |
1f5eb51b | 1451 | return -EINVAL; |
766d5156 DB |
1452 | |
1453 | /* hasta la vista, baby! */ | |
d71ef5a4 KZ |
1454 | memset(&gpt->ents[partnum], 0, sizeof(struct gpt_entry)); |
1455 | if (!partition_unused(&gpt->ents[partnum])) | |
1f5eb51b | 1456 | return -EINVAL; |
766d5156 | 1457 | else { |
d71ef5a4 KZ |
1458 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
1459 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
9ffeb235 KZ |
1460 | cxt->label->nparts_cur--; |
1461 | fdisk_label_set_changed(cxt->label, 1); | |
766d5156 | 1462 | } |
1f5eb51b DB |
1463 | |
1464 | return 0; | |
766d5156 DB |
1465 | } |
1466 | ||
1467 | static void gpt_entry_set_type(struct gpt_entry *e, struct gpt_guid *type) | |
1468 | { | |
1469 | size_t i; | |
1470 | ||
1471 | /* | |
1472 | * Copy corresponding partition type GUID. Only the first three blocks | |
1473 | * are endian-aware. | |
1474 | */ | |
1475 | e->partition_type_guid.time_low = cpu_to_le32(type->time_low); | |
1476 | e->partition_type_guid.time_mid = cpu_to_le16(type->time_mid); | |
1477 | e->partition_type_guid.time_hi_and_version = cpu_to_le16(type->time_hi_and_version); | |
1478 | e->partition_type_guid.clock_seq_hi = type->clock_seq_hi; | |
1479 | e->partition_type_guid.clock_seq_low = type->clock_seq_low; | |
1480 | for (i = 0; i < 6; i++) | |
1481 | e->partition_type_guid.node[i] = type->node[i]; | |
1482 | ||
1483 | DBG(LABEL, fprintf(stderr, "new type: %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n", | |
1484 | type->time_low, type->time_mid, type->time_hi_and_version, | |
1485 | type->clock_seq_hi, type->clock_seq_low, | |
1486 | type->node[0], type->node[1], type->node[2], | |
1487 | type->node[3], type->node[4], type->node[5])); | |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * Create a new GPT partition entry, specified by partnum, and with a range | |
1492 | * of fsect to lsenct sectors, of type t. | |
1493 | * Returns 0 on success, or negative upon failure. | |
1494 | */ | |
d71ef5a4 | 1495 | static int gpt_create_new_partition(struct fdisk_context *cxt, |
9ffeb235 | 1496 | size_t partnum, uint64_t fsect, uint64_t lsect, |
766d5156 DB |
1497 | struct gpt_guid *type, |
1498 | struct gpt_entry *entries) | |
1499 | { | |
1500 | struct gpt_entry *e = NULL; | |
d71ef5a4 | 1501 | struct fdisk_gpt_label *gpt; |
766d5156 | 1502 | |
9ffeb235 KZ |
1503 | assert(cxt); |
1504 | assert(cxt->label); | |
1505 | assert(fdisk_is_disklabel(cxt, GPT)); | |
766d5156 | 1506 | |
9ffeb235 KZ |
1507 | gpt = self_label(cxt); |
1508 | ||
1509 | if (fsect > lsect || partnum >= cxt->label->nparts_max) | |
1510 | return -EINVAL; | |
d71ef5a4 | 1511 | |
46667ba4 KZ |
1512 | e = calloc(1, sizeof(*e)); |
1513 | if (!e) | |
1514 | return -ENOMEM; | |
766d5156 DB |
1515 | e->lba_end = cpu_to_le64(lsect); |
1516 | e->lba_start = cpu_to_le64(fsect); | |
1517 | ||
1518 | gpt_entry_set_type(e, type); | |
1519 | ||
1520 | /* deal with partition name | |
1521 | for (i = 0; i < GPT_PART_NAME_LEN; i++) | |
1522 | e->partition_name[i] = | |
1523 | cpu_to_le16((uint16_t) gpt_sys_types[sys].name[i]); | |
1524 | */ | |
1525 | ||
1526 | /* | |
1527 | * Any time a new partition entry is created a new GUID must be | |
1528 | * generated for that partition, and every partition is guaranteed | |
1529 | * to have a unique GUID. | |
1530 | */ | |
1531 | uuid_generate_random((unsigned char *) &e->unique_partition_guid); | |
0b3e1d9b | 1532 | swap_efi_guid(&e->unique_partition_guid); |
766d5156 | 1533 | |
d71ef5a4 | 1534 | memcpy(&entries[partnum], e, sizeof(*e)); |
766d5156 | 1535 | |
d71ef5a4 KZ |
1536 | gpt_recompute_crc(gpt->pheader, entries); |
1537 | gpt_recompute_crc(gpt->bheader, entries); | |
766d5156 DB |
1538 | |
1539 | free(e); | |
1540 | return 0; | |
1541 | } | |
1542 | ||
1543 | /* Performs logical checks to add a new partition entry */ | |
8a95621d KZ |
1544 | static int gpt_add_partition( |
1545 | struct fdisk_context *cxt, | |
9ffeb235 | 1546 | size_t partnum, |
8a95621d | 1547 | struct fdisk_parttype *t) |
766d5156 | 1548 | { |
512a430f KZ |
1549 | uint64_t user_f, user_l; /* user input ranges for first and last sectors */ |
1550 | uint64_t disk_f, disk_l; /* first and last available sector ranges on device*/ | |
1551 | uint64_t dflt_f, dflt_l; /* largest segment (default) */ | |
766d5156 | 1552 | struct gpt_guid uuid = GPT_DEFAULT_ENTRY_GUID; |
9ffeb235 | 1553 | struct fdisk_gpt_label *gpt; |
d71ef5a4 KZ |
1554 | struct gpt_header *pheader; |
1555 | struct gpt_entry *ents; | |
4114da08 KZ |
1556 | struct fdisk_ask *ask = NULL; |
1557 | int rc; | |
766d5156 | 1558 | |
9ffeb235 KZ |
1559 | assert(cxt); |
1560 | assert(cxt->label); | |
1561 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1562 | ||
1563 | gpt = self_label(cxt); | |
1564 | ||
1565 | if (partnum >= cxt->label->nparts_max) | |
8254c3a5 | 1566 | return -EINVAL; |
d71ef5a4 KZ |
1567 | |
1568 | pheader = gpt->pheader; | |
1569 | ents = gpt->ents; | |
1570 | ||
874aa9c3 | 1571 | if (!partition_unused(&ents[partnum])) { |
251d9088 | 1572 | printf(_("Partition %zd is already defined. " |
766d5156 | 1573 | "Delete it before re-adding it.\n"), partnum +1); |
8254c3a5 | 1574 | return -EINVAL; |
766d5156 | 1575 | } |
d71ef5a4 KZ |
1576 | if (le32_to_cpu(pheader->npartition_entries) == |
1577 | partitions_in_use(pheader, ents)) { | |
766d5156 | 1578 | printf(_("All partitions are already in use.\n")); |
8254c3a5 | 1579 | return -EINVAL; |
766d5156 DB |
1580 | } |
1581 | ||
512a430f | 1582 | if (!get_free_sectors(cxt, pheader, ents, NULL, NULL)) { |
766d5156 | 1583 | printf(_("No free sectors available.\n")); |
8254c3a5 | 1584 | return -ENOSPC; |
766d5156 DB |
1585 | } |
1586 | ||
512a430f KZ |
1587 | disk_f = find_first_available(pheader, ents, 0); |
1588 | disk_l = find_last_free_sector(pheader, ents); | |
1589 | ||
1590 | /* the default is the largest free space */ | |
1591 | dflt_f = find_first_in_largest(pheader, ents); | |
1592 | dflt_l = find_last_free(pheader, ents, dflt_f); | |
1593 | ||
1594 | /* align the default in range <dflt_f,dflt_l>*/ | |
9475cc78 | 1595 | dflt_f = fdisk_align_lba_in_range(cxt, dflt_f, dflt_f, dflt_l); |
766d5156 DB |
1596 | |
1597 | if (t && t->typestr) | |
1598 | string_to_uuid(t->typestr, &uuid); | |
1599 | ||
1600 | /* get user input for first and last sectors of the new partition */ | |
766d5156 | 1601 | for (;;) { |
4114da08 KZ |
1602 | if (!ask) |
1603 | ask = fdisk_new_ask(); | |
1604 | else | |
1605 | fdisk_reset_ask(ask); | |
1606 | ||
1607 | /* First sector */ | |
1608 | fdisk_ask_set_query(ask, _("First sector")); | |
1609 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_NUMBER); | |
1610 | fdisk_ask_number_set_low(ask, disk_f); /* minimal */ | |
1611 | fdisk_ask_number_set_default(ask, dflt_f); /* default */ | |
1612 | fdisk_ask_number_set_high(ask, disk_l); /* maximal */ | |
1613 | ||
1614 | rc = fdisk_do_ask(cxt, ask); | |
1615 | if (rc) | |
1616 | goto done; | |
1617 | ||
1618 | user_f = fdisk_ask_number_get_result(ask); | |
e3443e8f KZ |
1619 | if (user_f != find_first_available(pheader, ents, user_f)) { |
1620 | printf(_("Sector %ju already used\n"), user_f); | |
512a430f | 1621 | continue; |
e3443e8f | 1622 | } |
512a430f | 1623 | |
4114da08 KZ |
1624 | fdisk_reset_ask(ask); |
1625 | ||
512a430f KZ |
1626 | /* Last sector */ |
1627 | dflt_l = find_last_free(pheader, ents, user_f); | |
512a430f | 1628 | |
4114da08 KZ |
1629 | fdisk_ask_set_query(ask, _("Last sector, +sectors or +size{K,M,G,T,P}")); |
1630 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET); | |
1631 | fdisk_ask_number_set_low(ask, user_f); /* minimal */ | |
1632 | fdisk_ask_number_set_default(ask, dflt_l); /* default */ | |
1633 | fdisk_ask_number_set_high(ask, dflt_l); /* maximal */ | |
1634 | fdisk_ask_number_set_base(ask, user_f); /* base for relative input */ | |
1635 | fdisk_ask_number_set_unit(ask, cxt->sector_size); | |
1636 | ||
1637 | rc = fdisk_do_ask(cxt, ask); | |
1638 | if (rc) | |
1639 | goto done; | |
1640 | ||
1641 | user_l = fdisk_ask_number_get_result(ask); | |
1642 | if (fdisk_ask_number_is_relative(ask)) | |
1643 | user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l) - 1; | |
512a430f KZ |
1644 | if (user_l > user_f && user_l <= disk_l) |
1645 | break; | |
766d5156 DB |
1646 | } |
1647 | ||
d71ef5a4 KZ |
1648 | if (gpt_create_new_partition(cxt, partnum, |
1649 | user_f, user_l, &uuid, ents) != 0) | |
251d9088 | 1650 | printf(_("Could not create partition %zd\n"), partnum + 1); |
9fcd49d5 | 1651 | else { |
251d9088 | 1652 | printf(_("Created partition %zd\n"), partnum + 1); |
9ffeb235 KZ |
1653 | cxt->label->nparts_cur++; |
1654 | fdisk_label_set_changed(cxt->label, 1); | |
9fcd49d5 | 1655 | } |
8254c3a5 | 1656 | |
4114da08 KZ |
1657 | rc = 0; |
1658 | done: | |
1659 | fdisk_free_ask(ask); | |
1660 | return rc; | |
766d5156 DB |
1661 | } |
1662 | ||
3f731001 DB |
1663 | /* |
1664 | * Create a new GPT disklabel - destroys any previous data. | |
1665 | */ | |
9ffeb235 | 1666 | static int gpt_create_disklabel(struct fdisk_context *cxt) |
3f731001 DB |
1667 | { |
1668 | int rc = 0; | |
46667ba4 | 1669 | ssize_t esz = 0; |
d71ef5a4 | 1670 | struct gpt_guid *uid; |
9ffeb235 KZ |
1671 | struct fdisk_gpt_label *gpt; |
1672 | ||
1673 | assert(cxt); | |
1674 | assert(cxt->label); | |
1675 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1676 | ||
1677 | gpt = self_label(cxt); | |
3f731001 | 1678 | |
d71ef5a4 | 1679 | /* label private stuff has to be empty, see gpt_deinit() */ |
d71ef5a4 KZ |
1680 | assert(gpt->pheader == NULL); |
1681 | assert(gpt->bheader == NULL); | |
4e0e8253 | 1682 | |
3f731001 | 1683 | /* |
3f731001 DB |
1684 | * When no header, entries or pmbr is set, we're probably |
1685 | * dealing with a new, empty disk - so always allocate memory | |
1686 | * to deal with the data structures whatever the case is. | |
1687 | */ | |
3f731001 DB |
1688 | rc = gpt_mknew_pmbr(cxt); |
1689 | if (rc < 0) | |
1690 | goto done; | |
1691 | ||
d71ef5a4 | 1692 | /* primary */ |
46667ba4 KZ |
1693 | gpt->pheader = calloc(1, sizeof(*gpt->pheader)); |
1694 | if (!gpt->pheader) { | |
1695 | rc = -ENOMEM; | |
1696 | goto done; | |
1697 | } | |
d71ef5a4 | 1698 | rc = gpt_mknew_header(cxt, gpt->pheader, GPT_PRIMARY_PARTITION_TABLE_LBA); |
3f731001 DB |
1699 | if (rc < 0) |
1700 | goto done; | |
1701 | ||
d71ef5a4 | 1702 | /* backup ("copy" primary) */ |
46667ba4 KZ |
1703 | gpt->bheader = calloc(1, sizeof(*gpt->bheader)); |
1704 | if (!gpt->bheader) { | |
1705 | rc = -ENOMEM; | |
1706 | goto done; | |
1707 | } | |
d71ef5a4 KZ |
1708 | rc = gpt_mknew_header_from_bkp(cxt, gpt->bheader, |
1709 | last_lba(cxt), gpt->pheader); | |
3f731001 DB |
1710 | if (rc < 0) |
1711 | goto done; | |
1712 | ||
46667ba4 KZ |
1713 | esz = le32_to_cpu(gpt->pheader->npartition_entries) * |
1714 | le32_to_cpu(gpt->pheader->sizeof_partition_entry); | |
1715 | gpt->ents = calloc(1, esz); | |
1716 | if (!gpt->ents) { | |
1717 | rc = -ENOMEM; | |
1718 | goto done; | |
1719 | } | |
d71ef5a4 KZ |
1720 | gpt_recompute_crc(gpt->pheader, gpt->ents); |
1721 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
3f731001 | 1722 | |
9ffeb235 KZ |
1723 | cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); |
1724 | cxt->label->nparts_cur = 0; | |
9fcd49d5 | 1725 | |
d71ef5a4 | 1726 | uid = &gpt->pheader->disk_guid; |
b546d442 KZ |
1727 | fprintf(stderr, ("Building a new GPT disklabel " |
1728 | "(GUID: %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X)\n"), | |
d71ef5a4 KZ |
1729 | uid->time_low, uid->time_mid, |
1730 | uid->time_hi_and_version, | |
1731 | uid->clock_seq_hi, | |
1732 | uid->clock_seq_low, | |
1733 | uid->node[0], uid->node[1], | |
1734 | uid->node[2], uid->node[3], | |
1735 | uid->node[4], uid->node[5]); | |
9ffeb235 | 1736 | fdisk_label_set_changed(cxt->label, 1); |
3f731001 DB |
1737 | done: |
1738 | return rc; | |
1739 | } | |
1740 | ||
8a95621d KZ |
1741 | static struct fdisk_parttype *gpt_get_partition_type( |
1742 | struct fdisk_context *cxt, | |
9ffeb235 | 1743 | size_t i) |
766d5156 DB |
1744 | { |
1745 | struct fdisk_parttype *t; | |
1746 | struct gpt_guid uuid; | |
1747 | char str[37]; | |
9ffeb235 | 1748 | struct fdisk_gpt_label *gpt; |
d71ef5a4 | 1749 | |
9ffeb235 KZ |
1750 | assert(cxt); |
1751 | assert(cxt->label); | |
1752 | assert(fdisk_is_disklabel(cxt, GPT)); | |
766d5156 | 1753 | |
9ffeb235 KZ |
1754 | gpt = self_label(cxt); |
1755 | ||
1756 | if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) | |
766d5156 DB |
1757 | return NULL; |
1758 | ||
d71ef5a4 | 1759 | uuid = gpt->ents[i].partition_type_guid; |
766d5156 DB |
1760 | swap_efi_guid(&uuid); |
1761 | ||
1762 | uuid_to_string(&uuid, str); | |
1763 | t = fdisk_get_parttype_from_string(cxt, str); | |
1764 | if (!t) | |
1765 | t = fdisk_new_unknown_parttype(0, str); | |
1766 | ||
1767 | return t; | |
1768 | } | |
1769 | ||
1770 | ||
8a95621d KZ |
1771 | static int gpt_set_partition_type( |
1772 | struct fdisk_context *cxt, | |
9ffeb235 | 1773 | size_t i, |
8a95621d | 1774 | struct fdisk_parttype *t) |
766d5156 DB |
1775 | { |
1776 | struct gpt_guid uuid; | |
9ffeb235 | 1777 | struct fdisk_gpt_label *gpt; |
d71ef5a4 | 1778 | |
9ffeb235 KZ |
1779 | assert(cxt); |
1780 | assert(cxt->label); | |
1781 | assert(fdisk_is_disklabel(cxt, GPT)); | |
766d5156 | 1782 | |
9ffeb235 KZ |
1783 | gpt = self_label(cxt); |
1784 | if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries) | |
d71ef5a4 | 1785 | || !t || !t->typestr || string_to_uuid(t->typestr, &uuid) != 0) |
766d5156 DB |
1786 | return -EINVAL; |
1787 | ||
d71ef5a4 KZ |
1788 | gpt_entry_set_type(&gpt->ents[i], &uuid); |
1789 | gpt_recompute_crc(gpt->pheader, gpt->ents); | |
1790 | gpt_recompute_crc(gpt->bheader, gpt->ents); | |
bddd84e7 | 1791 | |
9ffeb235 | 1792 | fdisk_label_set_changed(cxt->label, 1); |
766d5156 DB |
1793 | return 0; |
1794 | } | |
1795 | ||
47b8e7c0 KZ |
1796 | static int gpt_get_partition_status( |
1797 | struct fdisk_context *cxt, | |
9ffeb235 | 1798 | size_t i, |
47b8e7c0 KZ |
1799 | int *status) |
1800 | { | |
9ffeb235 | 1801 | struct fdisk_gpt_label *gpt; |
47b8e7c0 KZ |
1802 | struct gpt_entry *e; |
1803 | ||
9ffeb235 KZ |
1804 | assert(cxt); |
1805 | assert(cxt->label); | |
1806 | assert(fdisk_is_disklabel(cxt, GPT)); | |
1807 | ||
1808 | gpt = self_label(cxt); | |
1809 | ||
1810 | if (!status || (uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) | |
47b8e7c0 KZ |
1811 | return -EINVAL; |
1812 | ||
1813 | e = &gpt->ents[i]; | |
1814 | *status = FDISK_PARTSTAT_NONE; | |
1815 | ||
46667ba4 | 1816 | if (!partition_unused(e) || gpt_partition_start(e)) |
47b8e7c0 KZ |
1817 | *status = FDISK_PARTSTAT_USED; |
1818 | ||
1819 | return 0; | |
1820 | } | |
1821 | ||
1822 | ||
4e0e8253 KZ |
1823 | /* |
1824 | * Deinitialize fdisk-specific variables | |
1825 | */ | |
d71ef5a4 | 1826 | static void gpt_deinit(struct fdisk_label *lb) |
4e0e8253 | 1827 | { |
d71ef5a4 KZ |
1828 | struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; |
1829 | ||
1830 | if (!gpt) | |
1831 | return; | |
1832 | ||
1833 | free(gpt->ents); | |
1834 | free(gpt->pheader); | |
1835 | free(gpt->bheader); | |
1836 | ||
1837 | gpt->ents = NULL; | |
1838 | gpt->pheader = NULL; | |
1839 | gpt->bheader = NULL; | |
4e0e8253 KZ |
1840 | } |
1841 | ||
0c5d095e | 1842 | static const struct fdisk_label_operations gpt_operations = |
766d5156 | 1843 | { |
0c5d095e KZ |
1844 | .probe = gpt_probe_label, |
1845 | .write = gpt_write_disklabel, | |
1846 | .verify = gpt_verify_disklabel, | |
1847 | .create = gpt_create_disklabel, | |
1848 | .part_add = gpt_add_partition, | |
1849 | .part_delete = gpt_delete_partition, | |
1850 | .part_get_type = gpt_get_partition_type, | |
4e0e8253 KZ |
1851 | .part_set_type = gpt_set_partition_type, |
1852 | ||
47b8e7c0 KZ |
1853 | .part_get_status = gpt_get_partition_status, |
1854 | ||
4e0e8253 | 1855 | .deinit = gpt_deinit |
766d5156 | 1856 | }; |
0c5d095e KZ |
1857 | |
1858 | /* | |
1859 | * allocates GPT in-memory stuff | |
1860 | */ | |
1861 | struct fdisk_label *fdisk_new_gpt_label(struct fdisk_context *cxt) | |
1862 | { | |
1863 | struct fdisk_label *lb; | |
1864 | struct fdisk_gpt_label *gpt; | |
1865 | ||
1866 | assert(cxt); | |
1867 | ||
1868 | gpt = calloc(1, sizeof(*gpt)); | |
1869 | if (!gpt) | |
1870 | return NULL; | |
1871 | ||
1872 | /* initialize generic part of the driver */ | |
1873 | lb = (struct fdisk_label *) gpt; | |
1874 | lb->name = "gpt"; | |
53b422ab | 1875 | lb->id = FDISK_DISKLABEL_GPT; |
0c5d095e KZ |
1876 | lb->op = &gpt_operations; |
1877 | lb->parttypes = gpt_parttypes; | |
1878 | lb->nparttypes = ARRAY_SIZE(gpt_parttypes); | |
1879 | ||
1880 | return lb; | |
1881 | } |