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
22 #include "partitions.h"
25 #define GPT_PRIMARY_LBA 1
27 /* Signature - “EFI PART” */
28 #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
29 #define GPT_HEADER_SIGNATURE_STR "EFI PART"
32 typedef uint16_t efi_char16_t
;
38 uint16_t time_hi_and_version
;
40 uint8_t clock_seq_low
;
45 #define GPT_UNUSED_ENTRY_GUID \
46 ((efi_guid_t) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
47 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
49 uint64_t signature
; /* "EFI PART" */
51 uint32_t header_size
; /* usually 92 bytes */
52 uint32_t header_crc32
; /* checksum of header with this
53 * field zeroed during calculation */
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 */
61 efi_guid_t disk_guid
; /* disk UUID */
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
;
69 * The rest of the block is reserved by UEFI and must be zero. EFI
70 * standard handles this by:
72 * uint8_t reserved2[ BLKSSZGET - 92 ];
74 * This definition is useless in practice. It is necessary to read
75 * whole block from the device rather than sizeof(struct gpt_header)
78 } __attribute__ ((packed
));
81 struct gpt_entry_attributes {
82 uint64_t required_to_function:1;
84 uint64_t type_guid_specific:16;
85 } __attribute__ ((packed));
89 efi_guid_t partition_type_guid
; /* type UUID */
90 efi_guid_t unique_partition_guid
; /* partition UUID */
91 uint64_t starting_lba
;
94 /*struct gpt_entry_attributes attributes;*/
98 efi_char16_t partition_name
[72 / sizeof(efi_char16_t
)]; /* UTF-16LE string*/
99 } __attribute__ ((packed
));
103 * EFI uses crc32 with ~0 seed and xor's with ~0 at the end.
105 static inline uint32_t count_crc32(const unsigned char *buf
, size_t len
,
106 size_t exclude_off
, size_t exclude_len
)
108 return (crc32_exclude_offset(~0L, buf
, len
, exclude_off
, exclude_len
) ^ ~0L);
111 static inline unsigned char *get_lba_buffer(blkid_probe pr
,
112 uint64_t lba
, size_t bytes
)
114 return blkid_probe_get_buffer(pr
,
115 blkid_probe_get_sectorsize(pr
) * lba
, bytes
);
118 static inline int guidcmp(efi_guid_t left
, efi_guid_t right
)
120 return memcmp(&left
, &right
, sizeof (efi_guid_t
));
124 * UUID is traditionally 16 byte big-endian array, except Intel EFI
125 * specification where the UUID is a structure of little-endian fields.
127 static void swap_efi_guid(efi_guid_t
*uid
)
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
);
134 static int last_lba(blkid_probe pr
, uint64_t *lba
)
136 uint64_t sz
= blkid_probe_get_size(pr
);
137 unsigned int ssz
= blkid_probe_get_sectorsize(pr
);
142 *lba
= (sz
/ ssz
) - 1ULL;
147 * Protective (legacy) MBR.
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
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).
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()).
160 static int is_pmbr_valid(blkid_probe pr
, int *has
)
162 int flags
= blkid_partitions_get_flags(pr
);
164 struct dos_partition
*p
;
169 if (flags
& BLKID_PARTS_FORCE_GPT
)
170 goto ok
; /* skip PMBR check */
172 data
= blkid_probe_get_sector(pr
, 0);
179 if (!mbr_is_valid_magic(data
))
182 for (i
= 0, p
= mbr_get_partition(data
, 0); i
< 4; i
++, p
++) {
183 if (p
->sys_ind
== MBR_GPT_PARTITION
)
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.
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).
202 * This function checks validity of header and entries array. A corrupted
203 * header is not returned.
205 static struct gpt_header
*get_gpt_header(
206 blkid_probe pr
, struct gpt_header
*hdr
,
207 struct gpt_entry
**ents
, uint64_t lba
,
210 struct gpt_header
*h
;
216 ssz
= blkid_probe_get_sectorsize(pr
);
218 /* whole sector is allocated for GPT header */
219 h
= (struct gpt_header
*) get_lba_buffer(pr
, lba
, ssz
);
223 if (le64_to_cpu(h
->signature
) != GPT_HEADER_SIGNATURE
)
226 hsz
= le32_to_cpu(h
->header_size
);
228 /* EFI: The HeaderSize must be greater than 92 and must be less
229 * than or equal to the logical block size.
231 if (hsz
> ssz
|| hsz
< sizeof(*h
))
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
));
239 if (crc
!= le32_to_cpu(h
->header_crc32
)) {
240 DBG(LOWPROBE
, ul_debug("GPT header corrupted"));
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"));
251 fu
= le64_to_cpu(h
->first_usable_lba
);
252 lu
= le64_to_cpu(h
->last_usable_lba
);
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"));
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"));
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"));
274 /* Size of blocks with GPT entries */
275 esz
= le32_to_cpu(h
->num_partition_entries
) *
276 le32_to_cpu(h
->sizeof_partition_entry
);
278 /* The header seems valid, save it
279 * (we don't care about zeros in hdr->reserved2 area) */
280 memcpy(hdr
, h
, sizeof(*h
));
283 /* Read GPT entries */
284 *ents
= (struct gpt_entry
*) get_lba_buffer(pr
,
285 le64_to_cpu(h
->partition_entries_lba
), esz
);
287 DBG(LOWPROBE
, ul_debug("GPT entries unreadable"));
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"));
301 static int probe_gpt_pt(blkid_probe pr
,
302 const struct blkid_idmag
*mag
__attribute__((__unused__
)))
304 uint64_t lastlba
= 0, lba
;
305 struct gpt_header hdr
, *h
;
307 blkid_parttable tab
= NULL
;
314 if (last_lba(pr
, &lastlba
))
317 ret
= is_pmbr_valid(pr
, NULL
);
324 h
= get_gpt_header(pr
, &hdr
, &e
, (lba
= GPT_PRIMARY_LBA
), lastlba
);
326 h
= get_gpt_header(pr
, &hdr
, &e
, (lba
= lastlba
), lastlba
);
334 blkid_probe_use_wiper(pr
, lba
* blkid_probe_get_size(pr
), 8);
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
))
342 swap_efi_guid(&guid
);
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
;
351 ls
= blkid_probe_get_partlist(pr
);
355 tab
= blkid_partlist_new_parttable(ls
, "gpt",
356 blkid_probe_get_sectorsize(pr
) * lba
);
360 blkid_parttable_set_uuid(tab
, (const unsigned char *) &guid
);
362 ssf
= blkid_probe_get_sectorsize(pr
) / 512;
364 fu
= le64_to_cpu(h
->first_usable_lba
);
365 lu
= le64_to_cpu(h
->last_usable_lba
);
367 for (i
= 0; i
< le32_to_cpu(h
->num_partition_entries
); i
++, e
++) {
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;
374 /* 00000000-0000-0000-0000-000000000000 entry */
375 if (!guidcmp(e
->partition_type_guid
, GPT_UNUSED_ENTRY_GUID
)) {
376 blkid_partlist_increment_partno(ls
);
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",
384 blkid_partlist_increment_partno(ls
);
388 par
= blkid_partlist_add_partition(ls
, tab
,
389 start
* ssf
, size
* ssf
);
393 blkid_partition_set_utf8name(par
,
394 (unsigned char *) e
->partition_name
,
395 sizeof(e
->partition_name
), BLKID_ENC_UTF16LE
);
397 guid
= e
->unique_partition_guid
;
398 swap_efi_guid(&guid
);
399 blkid_partition_set_uuid(par
, (const unsigned char *) &guid
);
401 guid
= e
->partition_type_guid
;
402 swap_efi_guid(&guid
);
403 blkid_partition_set_type_uuid(par
, (const unsigned char *) &guid
);
405 blkid_partition_set_flags(par
, le64_to_cpu(e
->attributes
));
408 return BLKID_PROBE_OK
;
411 return BLKID_PROBE_NONE
;
418 const struct blkid_idinfo gpt_pt_idinfo
=
421 .probefunc
= probe_gpt_pt
,
422 .minsz
= 1024 * 1440 + 1, /* ignore floppies */
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().
430 * It means we have to always call probe_gpt_pt().
432 .magics
= BLKID_NONE_MAGIC
437 /* probe for *alone* protective MBR */
438 static int probe_pmbr_pt(blkid_probe pr
,
439 const struct blkid_idmag
*mag
__attribute__((__unused__
)))
443 uint64_t lastlba
= 0;
444 struct gpt_header hdr
;
446 if (last_lba(pr
, &lastlba
))
449 is_pmbr_valid(pr
, &has
);
453 if (!get_gpt_header(pr
, &hdr
, &e
, GPT_PRIMARY_LBA
, lastlba
) &&
454 !get_gpt_header(pr
, &hdr
, &e
, lastlba
, lastlba
))
460 const struct blkid_idinfo pmbr_pt_idinfo
=
463 .probefunc
= probe_pmbr_pt
,
466 { .magic
= "\x55\xAA", .len
= 2, .sboff
= 510 },