blob: 7288d683e6fafb8885449945eecdcacd359b85b0 [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>
20
21#include "partitions.h"
22#include "crc32.h"
23#include "dos.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 */
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 */
159static int is_pmbr_valid(blkid_probe pr)
160{
161 int flags = blkid_partitions_get_flags(pr);
162 unsigned char *data;
163 struct dos_partition *p;
164 int i;
165
166 if (flags & BLKID_PARTS_FORCE_GPT)
167 goto ok; /* skip PMBR check */
168
169 data = blkid_probe_get_sector(pr, 0);
170 if (!data)
171 goto failed;
172
173 if (!is_valid_mbr_signature(data))
174 goto failed;
175
176 p = (struct dos_partition *) (data + BLKID_MSDOS_PT_OFFSET);
177
178 for (i = 0; i < 4; i++, p++) {
179 if (p->sys_type == BLKID_GPT_PARTITION)
180 goto ok;
181 }
182failed:
183 return 0;
184ok:
185 return 1;
186}
187
188/*
189 * Reads GPT header to @hdr and returns a pointer to @hdr or NULL in case of
190 * error. The function also returns GPT entries in @ents.
191 *
192 * Note, this function does not allocate any memory. The GPT header has fixed
193 * size so we use stack, and @ents returns memory from libblkid buffer (so the
194 * next blkid_probe_get_buffer() will overwrite this buffer).
195 *
196 * This function checks validity of header and entries array. A corrupted
197 * header is not returned.
198 */
199static struct gpt_header *get_gpt_header(
200 blkid_probe pr, struct gpt_header *hdr,
201 struct gpt_entry **ents, uint64_t lba,
202 uint64_t lastlba)
203{
204 struct gpt_header *h;
205 uint32_t crc, orgcrc;
206 uint64_t lu, fu;
207 size_t esz;
208 uint32_t hsz, ssz;
209
210 ssz = blkid_probe_get_sectorsize(pr);
211
212 /* whole sector is allocated for GPT header */
213 h = (struct gpt_header *) get_lba_buffer(pr, lba, ssz);
214 if (!h)
215 return NULL;
216
217 if (le64_to_cpu(h->signature) != GPT_HEADER_SIGNATURE)
218 return NULL;
219
220 hsz = le32_to_cpu(h->header_size);
221
222 /* EFI: The HeaderSize must be greater than 92 and must be less
223 * than or equal to the logical block size.
224 */
225 if (hsz > ssz || hsz < sizeof(*h))
226 return NULL;
227
228 /* Header has to be verified when header_crc32 is zero */
229 orgcrc = h->header_crc32;
230 h->header_crc32 = 0;
231 crc = count_crc32((unsigned char *) h, hsz);
232 h->header_crc32 = orgcrc;
233
234 if (crc != le32_to_cpu(orgcrc)) {
235 DBG(DEBUG_LOWPROBE, printf("GPT header corrupted\n"));
236 return NULL;
237 }
238
239 /* Valid header has to be at MyLBA */
240 if (le64_to_cpu(h->my_lba) != lba) {
241 DBG(DEBUG_LOWPROBE, printf(
242 "GPT->MyLBA mismatch with real position\n"));
243 return NULL;
244 }
245
246 fu = le64_to_cpu(h->first_usable_lba);
247 lu = le64_to_cpu(h->last_usable_lba);
248
249 /* Check if First and Last usable LBA makes sense */
250 if (lu < fu || fu > lastlba || lu > lastlba) {
251 DBG(DEBUG_LOWPROBE, printf(
252 "GPT->{First,Last}UsableLBA out of range\n"));
253 return NULL;
254 }
255
256 /* The header has to be outside usable range */
257 if (fu < lba && lba < lu) {
258 DBG(DEBUG_LOWPROBE, printf("GPT header is inside usable area\n"));
259 return NULL;
260 }
261
262 /* Size of blocks with GPT entries */
263 esz = le32_to_cpu(h->num_partition_entries) *
264 le32_to_cpu(h->sizeof_partition_entry);
265 if (!esz) {
266 DBG(DEBUG_LOWPROBE, printf("GPT entries undefined\n"));
267 return NULL;
268 }
269
270 /* The header seems valid, save it
271 * (we don't care about zeros in hdr->reserved2 area) */
272 memcpy(hdr, h, sizeof(*h));
273 h = hdr;
274
275 /* Read GPT entries */
276 *ents = (struct gpt_entry *) get_lba_buffer(pr,
277 le64_to_cpu(h->partition_entries_lba), esz);
278 if (!*ents) {
279 DBG(DEBUG_LOWPROBE, printf("GPT entries unreadable\n"));
280 return NULL;
281 }
282
283 /* Validate entries */
284 crc = count_crc32((unsigned char *) *ents, esz);
285 if (crc != le32_to_cpu(h->partition_entry_array_crc32)) {
286 DBG(DEBUG_LOWPROBE, printf("GPT entries corrupted\n"));
287 return NULL;
288 }
289
290 return h;
291}
292
293static int probe_gpt_pt(blkid_probe pr,
294 const struct blkid_idmag *mag __attribute__((__unused__)))
295{
296 uint64_t lastlba = 0, lba;
297 struct gpt_header hdr, *h;
298 struct gpt_entry *e;
299 blkid_parttable tab = NULL;
300 blkid_partlist ls;
301 uint64_t fu, lu;
302 uint32_t ssf, i;
303 efi_guid_t guid;
304
305 if (last_lba(pr, &lastlba))
306 goto nothing;
307
308 if (!is_pmbr_valid(pr))
309 goto nothing;
310
311 h = get_gpt_header(pr, &hdr, &e, (lba = GPT_PRIMARY_LBA), lastlba);
312 if (!h)
313 h = get_gpt_header(pr, &hdr, &e, (lba = lastlba), lastlba);
314
315 if (!h)
316 goto nothing;
317
318 blkid_probe_use_wiper(pr, lba * blkid_probe_get_size(pr), 8);
319
320 if (blkid_probe_set_magic(pr, lba << 9,
321 sizeof(GPT_HEADER_SIGNATURE_STR) - 1,
322 (unsigned char *) GPT_HEADER_SIGNATURE_STR))
323 goto err;
324
325 if (blkid_partitions_need_typeonly(pr))
326 /* caller does not ask for details about partitions */
327 return 0;
328
329 ls = blkid_probe_get_partlist(pr);
330 if (!ls)
331 goto err;
332
333 tab = blkid_partlist_new_parttable(ls, "gpt", lba << 9);
334 if (!tab)
335 goto err;
336
337 guid = h->disk_guid;
338 swap_efi_guid(&guid);
339 blkid_parttable_set_id(tab, (const unsigned char *) &guid);
340
341 ssf = blkid_probe_get_sectorsize(pr) / 512;
342
343 fu = le64_to_cpu(h->first_usable_lba);
344 lu = le64_to_cpu(h->last_usable_lba);
345
346 for (i = 0; i < le32_to_cpu(h->num_partition_entries); i++, e++) {
347
348 blkid_partition par;
349 uint64_t start = le64_to_cpu(e->starting_lba);
350 uint64_t size = le64_to_cpu(e->ending_lba) -
351 le64_to_cpu(e->starting_lba) + 1ULL;
352
353 /* 00000000-0000-0000-0000-000000000000 entry */
354 if (!guidcmp(e->partition_type_guid, GPT_UNUSED_ENTRY_GUID)) {
355 blkid_partlist_increment_partno(ls);
356 continue;
357 }
358 /* the partition has to inside usable range */
359 if (start < fu || start + size - 1 > lu) {
360 DBG(DEBUG_LOWPROBE, printf(
361 "GPT entry[%d] overflows usable area - ignore\n",
362 i));
363 blkid_partlist_increment_partno(ls);
364 continue;
365 }
366
367 par = blkid_partlist_add_partition(ls, tab,
368 start * ssf, size * ssf);
369 if (!par)
370 goto err;
371
372 blkid_partition_set_utf8name(par,
373 (unsigned char *) e->partition_name,
374 sizeof(e->partition_name), BLKID_ENC_UTF16LE);
375
376 guid = e->unique_partition_guid;
377 swap_efi_guid(&guid);
378 blkid_partition_set_uuid(par, (const unsigned char *) &guid);
379
380 guid = e->partition_type_guid;
381 swap_efi_guid(&guid);
382 blkid_partition_set_type_uuid(par, (const unsigned char *) &guid);
383
384 blkid_partition_set_flags(par, e->attributes);
385 }
386
387 return 0;
388
389nothing:
390 return 1;
391err:
392 return -1;
393}
394
395
396const struct blkid_idinfo gpt_pt_idinfo =
397{
398 .name = "gpt",
399 .probefunc = probe_gpt_pt,
400 .minsz = 1024 * 1440 + 1, /* ignore floppies */
401
402 /*
403 * It would be possible to check for DOS signature (0xAA55), but
404 * unfortunately almost all EFI GPT implemenations allow to optionaly
405 * skip the legacy MBR. We follows this behavior and MBR is optional.
406 * See is_valid_pmbr().
407 *
408 * It means we have to always call probe_gpt_pt().
409 */
410 .magics = BLKID_NONE_MAGIC
411};
412