]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/partitions/gpt.c
misc: Fix various typos
[thirdparty/util-linux.git] / libblkid / src / partitions / gpt.c
1 /*
2 * EFI GPT partition parsing code
3 *
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 *
9 * This code is not copy & past from any other implementation.
10 *
11 * For more information about GPT start your study at:
12 * http://en.wikipedia.org/wiki/GUID_Partition_Table
13 * http://technet.microsoft.com/en-us/library/cc739412(WS.10).aspx
14 */
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <stddef.h>
20 #include <limits.h>
21
22 #include "partitions.h"
23 #include "crc32.h"
24
25 #define GPT_PRIMARY_LBA 1
26
27 /* Signature - “EFI PART” */
28 #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
29 #define GPT_HEADER_SIGNATURE_STR "EFI PART"
30
31 /* basic types */
32 typedef uint16_t efi_char16_t;
33
34 /* UUID */
35 typedef struct {
36 uint32_t time_low;
37 uint16_t time_mid;
38 uint16_t time_hi_and_version;
39 uint8_t clock_seq_hi;
40 uint8_t clock_seq_low;
41 uint8_t node[6];
42 } efi_guid_t;
43
44
45 #define GPT_UNUSED_ENTRY_GUID \
46 ((efi_guid_t) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
47 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
48 struct gpt_header {
49 uint64_t signature; /* "EFI PART" */
50 uint32_t revision;
51 uint32_t header_size; /* usually 92 bytes */
52 uint32_t header_crc32; /* checksum of header with this
53 * field zeroed during calculation */
54 uint32_t reserved1;
55
56 uint64_t my_lba; /* location of this header copy */
57 uint64_t alternate_lba; /* location of the other header copy */
58 uint64_t first_usable_lba; /* first usable LBA for partitions */
59 uint64_t last_usable_lba; /* last usable LBA for partitions */
60
61 efi_guid_t disk_guid; /* disk UUID */
62
63 uint64_t partition_entries_lba; /* always 2 in primary header copy */
64 uint32_t num_partition_entries;
65 uint32_t sizeof_partition_entry;
66 uint32_t partition_entry_array_crc32;
67
68 /*
69 * The rest of the block is reserved by UEFI and must be zero. EFI
70 * standard handles this by:
71 *
72 * uint8_t reserved2[ BLKSSZGET - 92 ];
73 *
74 * This definition is useless in practice. It is necessary to read
75 * whole block from the device rather than sizeof(struct gpt_header)
76 * only.
77 */
78 } __attribute__ ((packed));
79
80 /*** not used
81 struct gpt_entry_attributes {
82 uint64_t required_to_function:1;
83 uint64_t reserved:47;
84 uint64_t type_guid_specific:16;
85 } __attribute__ ((packed));
86 ***/
87
88 struct gpt_entry {
89 efi_guid_t partition_type_guid; /* type UUID */
90 efi_guid_t unique_partition_guid; /* partition UUID */
91 uint64_t starting_lba;
92 uint64_t ending_lba;
93
94 /*struct gpt_entry_attributes attributes;*/
95
96 uint64_t attributes;
97
98 efi_char16_t partition_name[72 / sizeof(efi_char16_t)]; /* UTF-16LE string*/
99 } __attribute__ ((packed));
100
101
102 /*
103 * EFI uses crc32 with ~0 seed and xor's with ~0 at the end.
104 */
105 static inline uint32_t count_crc32(const unsigned char *buf, size_t len,
106 size_t exclude_off, size_t exclude_len)
107 {
108 return (crc32_exclude_offset(~0L, buf, len, exclude_off, exclude_len) ^ ~0L);
109 }
110
111 static inline unsigned char *get_lba_buffer(blkid_probe pr,
112 uint64_t lba, size_t bytes)
113 {
114 return blkid_probe_get_buffer(pr,
115 blkid_probe_get_sectorsize(pr) * lba, bytes);
116 }
117
118 static inline int guidcmp(efi_guid_t left, efi_guid_t right)
119 {
120 return memcmp(&left, &right, sizeof (efi_guid_t));
121 }
122
123 /*
124 * UUID is traditionally 16 byte big-endian array, except Intel EFI
125 * specification where the UUID is a structure of little-endian fields.
126 */
127 static void swap_efi_guid(efi_guid_t *uid)
128 {
129 uid->time_low = swab32(uid->time_low);
130 uid->time_mid = swab16(uid->time_mid);
131 uid->time_hi_and_version = swab16(uid->time_hi_and_version);
132 }
133
134 static int last_lba(blkid_probe pr, uint64_t *lba)
135 {
136 uint64_t sz = blkid_probe_get_size(pr);
137 unsigned int ssz = blkid_probe_get_sectorsize(pr);
138
139 if (sz < ssz)
140 return -1;
141
142 *lba = (sz / ssz) - 1ULL;
143 return 0;
144 }
145
146 /*
147 * Protective (legacy) MBR.
148 *
149 * This MBR contains standard DOS partition table with a single partition, type
150 * of 0xEE. The partition usually encompassing the entire GPT drive - or 2TiB
151 * for large disks.
152 *
153 * Note that Apple uses GPT/MBR hybrid disks, where the DOS partition table is
154 * synchronized with GPT. This synchronization has many restriction of course
155 * (due DOS PT limitations).
156 *
157 * Note that the PMBR detection is optional (enabled by default) and could be
158 * disabled by BLKID_PARTS_FOPCE_GPT flag (see also blkid_partitions_set_flags()).
159 */
160 static int is_pmbr_valid(blkid_probe pr, int *has)
161 {
162 int flags = blkid_partitions_get_flags(pr);
163 unsigned char *data;
164 struct dos_partition *p;
165 int i;
166
167 if (has)
168 *has = 0;
169 if (flags & BLKID_PARTS_FORCE_GPT)
170 goto ok; /* skip PMBR check */
171
172 data = blkid_probe_get_sector(pr, 0);
173 if (!data) {
174 if (errno)
175 return -errno;
176 goto failed;
177 }
178
179 if (!mbr_is_valid_magic(data))
180 goto failed;
181
182 for (i = 0, p = mbr_get_partition(data, 0); i < 4; i++, p++) {
183 if (p->sys_ind == MBR_GPT_PARTITION)
184 goto ok;
185 }
186 failed:
187 return 0;
188 ok:
189 if (has)
190 *has = 1;
191 return 1;
192 }
193
194 /*
195 * Reads GPT header to @hdr and returns a pointer to @hdr or NULL in case of
196 * error. The function also returns GPT entries in @ents.
197 *
198 * Note, this function does not allocate any memory. The GPT header has fixed
199 * size so we use stack, and @ents returns memory from libblkid buffer (so the
200 * next blkid_probe_get_buffer() will overwrite this buffer).
201 *
202 * This function checks validity of header and entries array. A corrupted
203 * header is not returned.
204 */
205 static struct gpt_header *get_gpt_header(
206 blkid_probe pr, struct gpt_header *hdr,
207 struct gpt_entry **ents, uint64_t lba,
208 uint64_t lastlba)
209 {
210 struct gpt_header *h;
211 uint32_t crc;
212 uint64_t lu, fu;
213 size_t esz;
214 uint32_t hsz, ssz;
215
216 ssz = blkid_probe_get_sectorsize(pr);
217
218 /* whole sector is allocated for GPT header */
219 h = (struct gpt_header *) get_lba_buffer(pr, lba, ssz);
220 if (!h)
221 return NULL;
222
223 if (le64_to_cpu(h->signature) != GPT_HEADER_SIGNATURE)
224 return NULL;
225
226 hsz = le32_to_cpu(h->header_size);
227
228 /* EFI: The HeaderSize must be greater than 92 and must be less
229 * than or equal to the logical block size.
230 */
231 if (hsz > ssz || hsz < sizeof(*h))
232 return NULL;
233
234 /* Header has to be verified when header_crc32 is zero */
235 crc = count_crc32((unsigned char *) h, hsz,
236 offsetof(struct gpt_header, header_crc32),
237 sizeof(h->header_crc32));
238
239 if (crc != le32_to_cpu(h->header_crc32)) {
240 DBG(LOWPROBE, ul_debug("GPT header corrupted"));
241 return NULL;
242 }
243
244 /* Valid header has to be at MyLBA */
245 if (le64_to_cpu(h->my_lba) != lba) {
246 DBG(LOWPROBE, ul_debug(
247 "GPT->MyLBA mismatch with real position"));
248 return NULL;
249 }
250
251 fu = le64_to_cpu(h->first_usable_lba);
252 lu = le64_to_cpu(h->last_usable_lba);
253
254 /* Check if First and Last usable LBA makes sense */
255 if (lu < fu || fu > lastlba || lu > lastlba) {
256 DBG(LOWPROBE, ul_debug(
257 "GPT->{First,Last}UsableLBA out of range"));
258 return NULL;
259 }
260
261 /* The header has to be outside usable range */
262 if (fu < lba && lba < lu) {
263 DBG(LOWPROBE, ul_debug("GPT header is inside usable area"));
264 return NULL;
265 }
266
267 if (le32_to_cpu(h->num_partition_entries) == 0 ||
268 le32_to_cpu(h->sizeof_partition_entry) == 0 ||
269 ULONG_MAX / le32_to_cpu(h->num_partition_entries) < le32_to_cpu(h->sizeof_partition_entry)) {
270 DBG(LOWPROBE, ul_debug("GPT entries undefined"));
271 return NULL;
272 }
273
274 /* Size of blocks with GPT entries */
275 esz = le32_to_cpu(h->num_partition_entries) *
276 le32_to_cpu(h->sizeof_partition_entry);
277
278 /* The header seems valid, save it
279 * (we don't care about zeros in hdr->reserved2 area) */
280 memcpy(hdr, h, sizeof(*h));
281 h = hdr;
282
283 /* Read GPT entries */
284 *ents = (struct gpt_entry *) get_lba_buffer(pr,
285 le64_to_cpu(h->partition_entries_lba), esz);
286 if (!*ents) {
287 DBG(LOWPROBE, ul_debug("GPT entries unreadable"));
288 return NULL;
289 }
290
291 /* Validate entries */
292 crc = count_crc32((unsigned char *) *ents, esz, 0, 0);
293 if (crc != le32_to_cpu(h->partition_entry_array_crc32)) {
294 DBG(LOWPROBE, ul_debug("GPT entries corrupted"));
295 return NULL;
296 }
297
298 return h;
299 }
300
301 static int probe_gpt_pt(blkid_probe pr,
302 const struct blkid_idmag *mag __attribute__((__unused__)))
303 {
304 uint64_t lastlba = 0, lba;
305 struct gpt_header hdr, *h;
306 struct gpt_entry *e;
307 blkid_parttable tab = NULL;
308 blkid_partlist ls;
309 uint64_t fu, lu;
310 uint32_t ssf, i;
311 efi_guid_t guid;
312 int ret;
313
314 if (last_lba(pr, &lastlba))
315 goto nothing;
316
317 ret = is_pmbr_valid(pr, NULL);
318 if (ret < 0)
319 return ret;
320 else if (ret == 0)
321 goto nothing;
322
323 errno = 0;
324 h = get_gpt_header(pr, &hdr, &e, (lba = GPT_PRIMARY_LBA), lastlba);
325 if (!h && !errno)
326 h = get_gpt_header(pr, &hdr, &e, (lba = lastlba), lastlba);
327
328 if (!h) {
329 if (errno)
330 return -errno;
331 goto nothing;
332 }
333
334 blkid_probe_use_wiper(pr, lba * blkid_probe_get_size(pr), 8);
335
336 if (blkid_probe_set_magic(pr, blkid_probe_get_sectorsize(pr) * lba,
337 sizeof(GPT_HEADER_SIGNATURE_STR) - 1,
338 (unsigned char *) GPT_HEADER_SIGNATURE_STR))
339 goto err;
340
341 guid = h->disk_guid;
342 swap_efi_guid(&guid);
343
344 if (blkid_partitions_need_typeonly(pr)) {
345 /* Non-binary interface -- caller does not ask for details
346 * about partitions, just set generic variables only. */
347 blkid_partitions_set_ptuuid(pr, (unsigned char *) &guid);
348 return BLKID_PROBE_OK;
349 }
350
351 ls = blkid_probe_get_partlist(pr);
352 if (!ls)
353 goto nothing;
354
355 tab = blkid_partlist_new_parttable(ls, "gpt",
356 blkid_probe_get_sectorsize(pr) * lba);
357 if (!tab)
358 goto err;
359
360 blkid_parttable_set_uuid(tab, (const unsigned char *) &guid);
361
362 ssf = blkid_probe_get_sectorsize(pr) / 512;
363
364 fu = le64_to_cpu(h->first_usable_lba);
365 lu = le64_to_cpu(h->last_usable_lba);
366
367 for (i = 0; i < le32_to_cpu(h->num_partition_entries); i++, e++) {
368
369 blkid_partition par;
370 uint64_t start = le64_to_cpu(e->starting_lba);
371 uint64_t size = le64_to_cpu(e->ending_lba) -
372 le64_to_cpu(e->starting_lba) + 1ULL;
373
374 /* 00000000-0000-0000-0000-000000000000 entry */
375 if (!guidcmp(e->partition_type_guid, GPT_UNUSED_ENTRY_GUID)) {
376 blkid_partlist_increment_partno(ls);
377 continue;
378 }
379 /* the partition has to inside usable range */
380 if (start < fu || start + size - 1 > lu) {
381 DBG(LOWPROBE, ul_debug(
382 "GPT entry[%d] overflows usable area - ignore",
383 i));
384 blkid_partlist_increment_partno(ls);
385 continue;
386 }
387
388 par = blkid_partlist_add_partition(ls, tab,
389 start * ssf, size * ssf);
390 if (!par)
391 goto err;
392
393 blkid_partition_set_utf8name(par,
394 (unsigned char *) e->partition_name,
395 sizeof(e->partition_name), BLKID_ENC_UTF16LE);
396
397 guid = e->unique_partition_guid;
398 swap_efi_guid(&guid);
399 blkid_partition_set_uuid(par, (const unsigned char *) &guid);
400
401 guid = e->partition_type_guid;
402 swap_efi_guid(&guid);
403 blkid_partition_set_type_uuid(par, (const unsigned char *) &guid);
404
405 blkid_partition_set_flags(par, le64_to_cpu(e->attributes));
406 }
407
408 return BLKID_PROBE_OK;
409
410 nothing:
411 return BLKID_PROBE_NONE;
412
413 err:
414 return -ENOMEM;
415 }
416
417
418 const struct blkid_idinfo gpt_pt_idinfo =
419 {
420 .name = "gpt",
421 .probefunc = probe_gpt_pt,
422 .minsz = 1024 * 1440 + 1, /* ignore floppies */
423
424 /*
425 * It would be possible to check for DOS signature (0xAA55), but
426 * unfortunately almost all EFI GPT implementations allow to optionally
427 * skip the legacy MBR. We follows this behavior and MBR is optional.
428 * See is_valid_pmbr().
429 *
430 * It means we have to always call probe_gpt_pt().
431 */
432 .magics = BLKID_NONE_MAGIC
433 };
434
435
436
437 /* probe for *alone* protective MBR */
438 static int probe_pmbr_pt(blkid_probe pr,
439 const struct blkid_idmag *mag __attribute__((__unused__)))
440 {
441 int has = 0;
442 struct gpt_entry *e;
443 uint64_t lastlba = 0;
444 struct gpt_header hdr;
445
446 if (last_lba(pr, &lastlba))
447 goto nothing;
448
449 is_pmbr_valid(pr, &has);
450 if (!has)
451 goto nothing;
452
453 if (!get_gpt_header(pr, &hdr, &e, GPT_PRIMARY_LBA, lastlba) &&
454 !get_gpt_header(pr, &hdr, &e, lastlba, lastlba))
455 return 0;
456 nothing:
457 return 1;
458 }
459
460 const struct blkid_idinfo pmbr_pt_idinfo =
461 {
462 .name = "PMBR",
463 .probefunc = probe_pmbr_pt,
464 .magics =
465 {
466 { .magic = "\x55\xAA", .len = 2, .sboff = 510 },
467 { NULL }
468 }
469 };
470