blob: 665577fa42a5c1aaaa418307681850b15bd3e0bf [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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>
bigbiff7b4c7a62015-01-01 19:44:14 -050020#include <limits.h>
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050021
22#include "partitions.h"
23#include "crc32.h"
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050024
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 */
32typedef uint16_t efi_char16_t;
33
34/* UUID */
35typedef 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 }})
48struct 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; /* lirst 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
81struct 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
88struct 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 */
105static inline uint32_t count_crc32(const unsigned char *buf, size_t len)
106{
107 return (crc32(~0L, buf, len) ^ ~0L);
108}
109
110static inline unsigned char *get_lba_buffer(blkid_probe pr,
111 uint64_t lba, size_t bytes)
112{
113 return blkid_probe_get_buffer(pr,
114 blkid_probe_get_sectorsize(pr) * lba, bytes);
115}
116
117static inline int guidcmp(efi_guid_t left, efi_guid_t right)
118{
119 return memcmp(&left, &right, sizeof (efi_guid_t));
120}
121
122/*
123 * UUID is traditionally 16 byte big-endian array, except Intel EFI
124 * specification where the UUID is a structure of little-endian fields.
125 */
126static void swap_efi_guid(efi_guid_t *uid)
127{
128 uid->time_low = swab32(uid->time_low);
129 uid->time_mid = swab16(uid->time_mid);
130 uid->time_hi_and_version = swab16(uid->time_hi_and_version);
131}
132
133static int last_lba(blkid_probe pr, uint64_t *lba)
134{
135 blkid_loff_t sz = blkid_probe_get_size(pr);
136 unsigned int ssz = blkid_probe_get_sectorsize(pr);
137
138 if (sz < ssz)
139 return -1;
140
141 *lba = (sz / ssz) - 1ULL;
142 return 0;
143}
144
145/*
146 * Protective (legacy) MBR.
147 *
148 * This MBR contains standard DOS partition table with a single partition, type
149 * of 0xEE. The partition usually encompassing the entire GPT drive - or 2TiB
150 * for large disks.
151 *
152 * Note that Apple uses GPT/MBR hybrid disks, where the DOS partition table is
153 * synchronized with GPT. This synchronization has many restriction of course
154 * (due DOS PT limitations).
155 *
156 * Note that the PMBR detection is optional (enabled by default) and could be
157 * disabled by BLKID_PARTS_FOPCE_GPT flag (see also blkid_paertitions_set_flags()).
158 */
bigbiff7b4c7a62015-01-01 19:44:14 -0500159static int is_pmbr_valid(blkid_probe pr, int *has)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500160{
161 int flags = blkid_partitions_get_flags(pr);
162 unsigned char *data;
163 struct dos_partition *p;
164 int i;
165
bigbiff7b4c7a62015-01-01 19:44:14 -0500166 if (has)
167 *has = 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500168 if (flags & BLKID_PARTS_FORCE_GPT)
169 goto ok; /* skip PMBR check */
170
171 data = blkid_probe_get_sector(pr, 0);
bigbiff7b4c7a62015-01-01 19:44:14 -0500172 if (!data) {
173 if (errno)
174 return -errno;
175 goto failed;
176 }
177
178 if (!mbr_is_valid_magic(data))
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500179 goto failed;
180
bigbiff7b4c7a62015-01-01 19:44:14 -0500181 for (i = 0, p = mbr_get_partition(data, 0); i < 4; i++, p++) {
182 if (p->sys_ind == MBR_GPT_PARTITION)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500183 goto ok;
184 }
185failed:
186 return 0;
187ok:
bigbiff7b4c7a62015-01-01 19:44:14 -0500188 if (has)
189 *has = 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500190 return 1;
191}
192
193/*
194 * Reads GPT header to @hdr and returns a pointer to @hdr or NULL in case of
195 * error. The function also returns GPT entries in @ents.
196 *
197 * Note, this function does not allocate any memory. The GPT header has fixed
198 * size so we use stack, and @ents returns memory from libblkid buffer (so the
199 * next blkid_probe_get_buffer() will overwrite this buffer).
200 *
201 * This function checks validity of header and entries array. A corrupted
202 * header is not returned.
203 */
204static struct gpt_header *get_gpt_header(
205 blkid_probe pr, struct gpt_header *hdr,
206 struct gpt_entry **ents, uint64_t lba,
207 uint64_t lastlba)
208{
209 struct gpt_header *h;
210 uint32_t crc, orgcrc;
211 uint64_t lu, fu;
212 size_t esz;
213 uint32_t hsz, ssz;
214
215 ssz = blkid_probe_get_sectorsize(pr);
216
217 /* whole sector is allocated for GPT header */
218 h = (struct gpt_header *) get_lba_buffer(pr, lba, ssz);
219 if (!h)
220 return NULL;
221
222 if (le64_to_cpu(h->signature) != GPT_HEADER_SIGNATURE)
223 return NULL;
224
225 hsz = le32_to_cpu(h->header_size);
226
227 /* EFI: The HeaderSize must be greater than 92 and must be less
228 * than or equal to the logical block size.
229 */
230 if (hsz > ssz || hsz < sizeof(*h))
231 return NULL;
232
233 /* Header has to be verified when header_crc32 is zero */
234 orgcrc = h->header_crc32;
235 h->header_crc32 = 0;
236 crc = count_crc32((unsigned char *) h, hsz);
237 h->header_crc32 = orgcrc;
238
239 if (crc != le32_to_cpu(orgcrc)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500240 DBG(LOWPROBE, ul_debug("GPT header corrupted"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500241 return NULL;
242 }
243
244 /* Valid header has to be at MyLBA */
245 if (le64_to_cpu(h->my_lba) != lba) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500246 DBG(LOWPROBE, ul_debug(
247 "GPT->MyLBA mismatch with real position"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500248 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) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500256 DBG(LOWPROBE, ul_debug(
257 "GPT->{First,Last}UsableLBA out of range"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500258 return NULL;
259 }
260
261 /* The header has to be outside usable range */
262 if (fu < lba && lba < lu) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500263 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"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500271 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);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500277
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) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500287 DBG(LOWPROBE, ul_debug("GPT entries unreadable"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500288 return NULL;
289 }
290
291 /* Validate entries */
292 crc = count_crc32((unsigned char *) *ents, esz);
293 if (crc != le32_to_cpu(h->partition_entry_array_crc32)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500294 DBG(LOWPROBE, ul_debug("GPT entries corrupted"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500295 return NULL;
296 }
297
298 return h;
299}
300
301static 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;
bigbiff7b4c7a62015-01-01 19:44:14 -0500312 int ret;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500313
314 if (last_lba(pr, &lastlba))
315 goto nothing;
316
bigbiff7b4c7a62015-01-01 19:44:14 -0500317 ret = is_pmbr_valid(pr, NULL);
318 if (ret < 0)
319 return ret;
320 else if (ret == 0)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500321 goto nothing;
322
bigbiff7b4c7a62015-01-01 19:44:14 -0500323 errno = 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500324 h = get_gpt_header(pr, &hdr, &e, (lba = GPT_PRIMARY_LBA), lastlba);
bigbiff7b4c7a62015-01-01 19:44:14 -0500325 if (!h && !errno)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500326 h = get_gpt_header(pr, &hdr, &e, (lba = lastlba), lastlba);
327
bigbiff7b4c7a62015-01-01 19:44:14 -0500328 if (!h) {
329 if (errno)
330 return -errno;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500331 goto nothing;
bigbiff7b4c7a62015-01-01 19:44:14 -0500332 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500333
334 blkid_probe_use_wiper(pr, lba * blkid_probe_get_size(pr), 8);
335
bigbiff7b4c7a62015-01-01 19:44:14 -0500336 if (blkid_probe_set_magic(pr, blkid_probe_get_sectorsize(pr) * lba,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500337 sizeof(GPT_HEADER_SIGNATURE_STR) - 1,
338 (unsigned char *) GPT_HEADER_SIGNATURE_STR))
339 goto err;
340
bigbiff7b4c7a62015-01-01 19:44:14 -0500341 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 varibles only. */
347 blkid_partitions_set_ptuuid(pr, (unsigned char *) &guid);
348 return BLKID_PROBE_OK;
349 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500350
351 ls = blkid_probe_get_partlist(pr);
352 if (!ls)
bigbiff7b4c7a62015-01-01 19:44:14 -0500353 goto nothing;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500354
bigbiff7b4c7a62015-01-01 19:44:14 -0500355 tab = blkid_partlist_new_parttable(ls, "gpt",
356 blkid_probe_get_sectorsize(pr) * lba);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500357 if (!tab)
358 goto err;
359
bigbiff7b4c7a62015-01-01 19:44:14 -0500360 blkid_parttable_set_uuid(tab, (const unsigned char *) &guid);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500361
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) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500381 DBG(LOWPROBE, ul_debug(
382 "GPT entry[%d] overflows usable area - ignore",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500383 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
bigbiff7b4c7a62015-01-01 19:44:14 -0500405 blkid_partition_set_flags(par, le64_to_cpu(e->attributes));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500406 }
407
bigbiff7b4c7a62015-01-01 19:44:14 -0500408 return BLKID_PROBE_OK;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500409
410nothing:
bigbiff7b4c7a62015-01-01 19:44:14 -0500411 return BLKID_PROBE_NONE;
412
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500413err:
bigbiff7b4c7a62015-01-01 19:44:14 -0500414 return -ENOMEM;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500415}
416
417
418const 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 implemenations allow to optionaly
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
bigbiff7b4c7a62015-01-01 19:44:14 -0500435
436
437/* probe for *alone* protective MBR */
438static 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;
456nothing:
457 return 1;
458}
459
460const 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