2
* EFI GPT partition parsing code
4
* Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6
* This file may be redistributed under the terms of the
7
* GNU Lesser General Public License.
9
* This code is not copy & past from any other implementation.
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
21
#include "partitions.h"
25
#define GPT_PRIMARY_LBA 1
27
/* Signature - āEFI PARTā */
28
#define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
31
typedef uint16_t efi_char16_t;
37
uint16_t time_hi_and_version;
39
uint8_t clock_seq_low;
44
#define GPT_UNUSED_ENTRY_GUID \
45
((efi_guid_t) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
46
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
48
uint64_t signature; /* "EFI PART" */
50
uint32_t header_size; /* usualy 92 bytes */
51
uint32_t header_crc32; /* checksum of header with this
52
* field zeroed during calculation */
55
uint64_t my_lba; /* location of this header copy */
56
uint64_t alternate_lba; /* location of the other header copy */
57
uint64_t first_usable_lba; /* lirst usable LBA for partitions */
58
uint64_t last_usable_lba; /* last usable LBA for partitions */
60
efi_guid_t disk_guid; /* disk UUID */
62
uint64_t partition_entries_lba; /* always 2 in primary header copy */
63
uint32_t num_partition_entries;
64
uint32_t sizeof_partition_entry;
65
uint32_t partition_entry_array_crc32;
68
* The rest of the block is reserved by UEFI and must be zero. EFI
69
* standard handles this by:
71
* uint8_t reserved2[ BLKSSZGET - 92 ];
73
* This definition is useless in practice. It is necessary to read
74
* whole block from the device rather than sizeof(struct gpt_header)
77
} __attribute__ ((packed));
80
struct gpt_entry_attributes {
81
uint64_t required_to_function:1;
83
uint64_t type_guid_specific:16;
84
} __attribute__ ((packed));
88
efi_guid_t partition_type_guid; /* type UUID */
89
efi_guid_t unique_partition_guid; /* partition UUID */
90
uint64_t starting_lba;
93
/*struct gpt_entry_attributes attributes;*/
97
efi_char16_t partition_name[72 / sizeof(efi_char16_t)]; /* UTF-16LE string*/
98
} __attribute__ ((packed));
102
* EFI uses crc32 with ~0 seed and xor's with ~0 at the end.
104
static inline uint32_t count_crc32(const unsigned char *buf, size_t len)
106
return (crc32(~0L, buf, len) ^ ~0L);
109
static inline unsigned char *get_lba_buffer(blkid_probe pr,
110
uint64_t lba, size_t bytes)
112
return blkid_probe_get_buffer(pr,
113
blkid_probe_get_sectorsize(pr) * lba, bytes);
116
static inline int guidcmp(efi_guid_t left, efi_guid_t right)
118
return memcmp(&left, &right, sizeof (efi_guid_t));
122
* UUID is traditionaly 16 byte big-endian array, except Intel EFI
123
* specification where the UUID is a structure of little-endian fields.
125
static void swap_efi_guid(efi_guid_t *uid)
127
uid->time_low = swab32(uid->time_low);
128
uid->time_mid = swab16(uid->time_mid);
129
uid->time_hi_and_version = swab16(uid->time_hi_and_version);
132
static int last_lba(blkid_probe pr, uint64_t *lba)
134
blkid_loff_t sz = blkid_probe_get_size(pr);
135
if (sz < blkid_probe_get_sectorsize(pr))
138
*lba = (sz >> 9) - 1;
143
* Protective (legacy) MBR.
145
* This MBR contains standard DOS partition table with a single partition, type
146
* of 0xEE. The partition usually encompassing the entire GPT drive - or 2TiB
149
* Note that Apple uses GPT/MBR hybrid disks, where the DOS partition table is
150
* synchronized with GPT. This synchronization has many restriction of course
151
* (due DOS PT limitations).
153
* Note that the PMBR detection is optional (enabled by default) and could be
154
* disabled by BLKID_PARTS_FOPCE_GPT flag (see also blkid_paertitions_set_flags()).
156
static int is_pmbr_valid(blkid_probe pr)
158
int flags = blkid_partitions_get_flags(pr);
160
struct dos_partition *p;
163
if (flags & BLKID_PARTS_FORCE_GPT)
164
goto ok; /* skip PMBR check */
166
data = blkid_probe_get_sector(pr, 0);
170
if (!is_valid_mbr_signature(data))
173
p = (struct dos_partition *) (data + BLKID_MSDOS_PT_OFFSET);
175
for (i = 0; i < 4; i++, p++) {
176
if (p->sys_type == BLKID_GPT_PARTITION)
186
* Reads GPT header to @hdr and returns a pointer to @hdr or NULL in case of
187
* error. The function also returns GPT entries in @ents.
189
* Note, this function does not allocate any memory. The GPT header has fixed
190
* size so we use stack, and @ents returns memory from libblkid buffer (so the
191
* next blkid_probe_get_buffer() will overwrite this buffer).
193
* This function checks validity of header and entries array. A corrupted
194
* header is not returned.
196
static struct gpt_header *get_gpt_header(
197
blkid_probe pr, struct gpt_header *hdr,
198
struct gpt_entry **ents, uint64_t lba,
201
struct gpt_header *h;
202
uint32_t crc, orgcrc;
207
ssz = blkid_probe_get_sectorsize(pr);
209
/* whole sector is allocated for GPT header */
210
h = (struct gpt_header *) get_lba_buffer(pr, lba, ssz);
214
if (le64_to_cpu(h->signature) != GPT_HEADER_SIGNATURE)
217
hsz = le32_to_cpu(h->header_size);
219
/* EFI: The HeaderSize must be greater than 92 and must be less
220
* than or equal to the logical block size.
222
if (hsz > ssz || hsz < sizeof(*h))
225
/* Header has to be verified when header_crc32 is zero */
226
orgcrc = le32_to_cpu(h->header_crc32);
229
crc = count_crc32((unsigned char *) h, hsz);
231
DBG(DEBUG_LOWPROBE, printf("GPT header corrupted\n"));
234
h->header_crc32 = cpu_to_le32(orgcrc);
236
/* Valid header has to be at MyLBA */
237
if (le64_to_cpu(h->my_lba) != lba) {
238
DBG(DEBUG_LOWPROBE, printf(
239
"GPT->MyLBA mismatch with real position\n"));
243
fu = le64_to_cpu(h->first_usable_lba);
244
lu = le64_to_cpu(h->last_usable_lba);
246
/* Check if First and Last usable LBA makes sense */
247
if (lu < fu || fu > lastlba || lu > lastlba) {
248
DBG(DEBUG_LOWPROBE, printf(
249
"GPT->{First,Last}UsableLBA out of range\n"));
253
/* The header has to be outside usable range */
254
if (fu < lba && lba < lu) {
255
DBG(DEBUG_LOWPROBE, printf("GPT header is inside usable area\n"));
259
/* Size of blocks with GPT entries */
260
esz = le32_to_cpu(h->num_partition_entries) *
261
le32_to_cpu(h->sizeof_partition_entry);
263
DBG(DEBUG_LOWPROBE, printf("GPT entries undefined\n"));
267
/* The header seems valid, save it
268
* (we don't care about zeros in hdr->reserved2 area) */
269
memcpy(hdr, h, sizeof(*h));
272
/* Read GPT entries */
273
*ents = (struct gpt_entry *) get_lba_buffer(pr,
274
le64_to_cpu(h->partition_entries_lba), esz);
276
DBG(DEBUG_LOWPROBE, printf("GPT entries unreadable\n"));
280
/* Validate entries */
281
crc = count_crc32((unsigned char *) *ents, esz);
282
if (crc != le32_to_cpu(h->partition_entry_array_crc32)) {
283
DBG(DEBUG_LOWPROBE, printf("GPT entries corrupted\n"));
290
static int probe_gpt_pt(blkid_probe pr,
291
const struct blkid_idmag *mag __attribute__((__unused__)))
293
uint64_t lastlba = 0, lba;
294
struct gpt_header hdr, *h;
296
blkid_parttable tab = NULL;
302
if (last_lba(pr, &lastlba))
305
if (!is_pmbr_valid(pr))
308
h = get_gpt_header(pr, &hdr, &e, (lba = GPT_PRIMARY_LBA), lastlba);
310
h = get_gpt_header(pr, &hdr, &e, (lba = lastlba), lastlba);
315
blkid_probe_use_wiper(pr, lba * blkid_probe_get_size(pr), 8);
317
if (blkid_partitions_need_typeonly(pr))
318
/* caller does not ask for details about partitions */
321
ls = blkid_probe_get_partlist(pr);
325
tab = blkid_partlist_new_parttable(ls, "gpt", lba << 9);
329
ssf = blkid_probe_get_sectorsize(pr) / 512;
331
fu = le64_to_cpu(h->first_usable_lba);
332
lu = le64_to_cpu(h->last_usable_lba);
334
for (i = 0; i < le32_to_cpu(h->num_partition_entries); i++, e++) {
337
uint64_t start = le64_to_cpu(e->starting_lba);
338
uint64_t size = le64_to_cpu(e->ending_lba) -
339
le64_to_cpu(e->starting_lba) + 1ULL;
341
/* 00000000-0000-0000-0000-000000000000 entry */
342
if (!guidcmp(e->partition_type_guid, GPT_UNUSED_ENTRY_GUID)) {
343
blkid_partlist_increment_partno(ls);
346
/* the partition has to inside usable range */
347
if (start < fu || start + size - 1 > lu) {
348
DBG(DEBUG_LOWPROBE, printf(
349
"GPT entry[%d] overflows usable area - ignore\n",
351
blkid_partlist_increment_partno(ls);
355
par = blkid_partlist_add_partition(ls, tab,
356
start * ssf, size * ssf);
360
blkid_partition_set_utf8name(par,
361
(unsigned char *) e->partition_name,
362
sizeof(e->partition_name), BLKID_ENC_UTF16LE);
364
swap_efi_guid(&e->unique_partition_guid);
365
swap_efi_guid(&e->partition_type_guid);
367
blkid_partition_set_uuid(par,
368
(const unsigned char *) &e->unique_partition_guid);
370
blkid_partition_set_type_uuid(par,
371
(const unsigned char *) &e->partition_type_guid);
373
blkid_partition_set_flags(par, e->attributes);
385
const struct blkid_idinfo gpt_pt_idinfo =
388
.probefunc = probe_gpt_pt,
389
.minsz = 1024 * 1440 + 1, /* ignore floppies */
392
* It would be possible to check for DOS signature (0xAA55), but
393
* unfortunately almost all EFI GPT implemenations allow to optionaly
394
* skip the legacy MBR. We follows this behavior and MBR is optional.
395
* See is_valid_pmbr().
397
* It means we have to always call probe_gpt_pt().
399
.magics = BLKID_NONE_MAGIC