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