1 /************************************************************
2 * EFI GUID Partition Table handling
3 * Per Intel EFI Specification v1.02
4 * http://developer.intel.com/technology/efi/efi.htm
5 * efi.[ch] by Matt Domsch <Matt_Domsch@dell.com>
6 * Copyright 2000,2001,2002 Dell Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Tue Mar 26 2002 Matt Domsch <Matt_Domsch@dell.com>
27 * - Ported to 2.5.7-pre1 and 2.5.7-dj2
28 * - Applied patch to avoid fault in alternate header handling
29 * - cleaned up find_valid_gpt
30 * - On-disk structure and copy in memory is *always* LE now -
31 * swab fields as needed
32 * - remove print_gpt_header()
33 * - only use first max_p partition entries, to keep the kernel minor number
34 * and partition numbers tied.
36 * Mon Feb 04 2002 Matt Domsch <Matt_Domsch@dell.com>
37 * - Removed __PRIPTR_PREFIX - not being used
39 * Mon Jan 14 2002 Matt Domsch <Matt_Domsch@dell.com>
40 * - Ported to 2.5.2-pre11 + library crc32 patch Linus applied
42 * Thu Dec 6 2001 Matt Domsch <Matt_Domsch@dell.com>
43 * - Added compare_gpts().
44 * - moved le_efi_guid_to_cpus() back into this file. GPT is the only
45 * thing that keeps EFI GUIDs on disk.
46 * - Changed gpt structure names and members to be simpler and more Linux-like.
48 * Wed Oct 17 2001 Matt Domsch <Matt_Domsch@dell.com>
49 * - Removed CONFIG_DEVFS_VOLUMES_UUID code entirely per Martin Wilck
51 * Wed Oct 10 2001 Matt Domsch <Matt_Domsch@dell.com>
52 * - Changed function comments to DocBook style per Andreas Dilger suggestion.
54 * Mon Oct 08 2001 Matt Domsch <Matt_Domsch@dell.com>
55 * - Change read_lba() to use the page cache per Al Viro's work.
56 * - print u64s properly on all architectures
57 * - fixed debug_printk(), now Dprintk()
59 * Mon Oct 01 2001 Matt Domsch <Matt_Domsch@dell.com>
61 * - made most functions static
62 * - Endianness addition
63 * - remove test for second alternate header, as it's not per spec,
64 * and is unnecessary. There's now a method to read/write the last
65 * sector of an odd-sized disk from user space. No tools have ever
66 * been released which used this code, so it's effectively dead.
67 * - Per Asit Mallick of Intel, added a test for a valid PMBR.
68 * - Added kernel command line option 'gpt' to override valid PMBR test.
70 * Wed Jun 6 2001 Martin Wilck <Martin.Wilck@Fujitsu-Siemens.com>
71 * - added devfs volume UUID support (/dev/volumes/uuids) for
72 * mounting file systems by the partition GUID.
74 * Tue Dec 5 2000 Matt Domsch <Matt_Domsch@dell.com>
75 * - Moved crc32() to linux/lib, added efi_crc32().
77 * Thu Nov 30 2000 Matt Domsch <Matt_Domsch@dell.com>
78 * - Replaced Intel's CRC32 function with an equivalent
79 * non-license-restricted version.
81 * Wed Oct 25 2000 Matt Domsch <Matt_Domsch@dell.com>
82 * - Fixed the last_lba() call to return the proper last block
84 * Thu Oct 12 2000 Matt Domsch <Matt_Domsch@dell.com>
85 * - Thanks to Andries Brouwer for his debugging assistance.
86 * - Code works, detects all the partitions.
88 ************************************************************/
89 #include <linux/config.h>
90 #include <linux/crc32.h>
96 #define Dprintk(x...) printk(KERN_DEBUG x)
101 /* This allows a kernel command line option 'gpt' to override
102 * the test for invalid PMBR. Not __initdata because reloading
103 * the partition tables happens after init too.
105 static int force_gpt
;
107 force_gpt_fn(char *str
)
112 __setup("gpt", force_gpt_fn
);
116 * efi_crc32() - EFI version of crc32 function
117 * @buf: buffer to calculate crc32 of
118 * @len - length of buf
120 * Description: Returns EFI-style CRC32 value for @buf
122 * This function uses the little endian Ethernet polynomial
123 * but seeds the function with ~0, and xor's with ~0 at the end.
124 * Note, the EFI Specification, v1.02, has a reference to
125 * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
128 efi_crc32(const void *buf
, unsigned long len
)
130 return (crc32(~0L, buf
, len
) ^ ~0L);
134 * is_pmbr_valid(): test Protective MBR for validity
135 * @mbr: pointer to a legacy mbr structure
137 * Description: Returns 1 if PMBR is valid, 0 otherwise.
138 * Validity depends on two things:
139 * 1) MSDOS signature is in the last two bytes of the MBR
140 * 2) One partition of type 0xEE is found
143 is_pmbr_valid(legacy_mbr
*mbr
)
145 int i
, found
= 0, signature
= 0;
148 signature
= (le16_to_cpu(mbr
->signature
) == MSDOS_MBR_SIGNATURE
);
149 for (i
= 0; signature
&& i
< 4; i
++) {
150 if (mbr
->partition_record
[i
].sys_ind
==
151 EFI_PMBR_OSTYPE_EFI_GPT
) {
156 return (signature
&& found
);
160 * last_lba(): return number of last logical block of device
161 * @bdev: block device
163 * Description: Returns last LBA value on success, 0 on error.
164 * This is stored (by sd and ide-geometry) in
165 * the part[0] entry for this disk, and is the number of
166 * physical sectors available on the disk.
169 last_lba(struct block_device
*bdev
)
171 return (bdev
->bd_inode
->i_size
>> 9) - 1;
175 * read_lba(): Read bytes from disk, starting at given LBA
181 * Description: Reads @count bytes from @bdev into @buffer.
182 * Returns number of bytes read on success, 0 on error.
185 read_lba(struct block_device
*bdev
, u64 lba
, u8
* buffer
, size_t count
)
187 size_t totalreadcount
= 0;
189 if (!bdev
|| !buffer
)
195 unsigned char *data
= read_dev_sector(bdev
, lba
++, §
);
200 memcpy(buffer
, data
, copied
);
201 put_dev_sector(sect
);
203 totalreadcount
+=copied
;
206 return totalreadcount
;
211 * alloc_read_gpt_entries(): reads partition entries from disk
215 * Description: Returns ptes on success, NULL on error.
216 * Allocates space for PTEs based on information found in @gpt.
217 * Notes: remember to free pte when you're done!
220 alloc_read_gpt_entries(struct block_device
*bdev
, gpt_header
*gpt
)
227 count
= le32_to_cpu(gpt
->num_partition_entries
) *
228 le32_to_cpu(gpt
->sizeof_partition_entry
);
231 pte
= kmalloc(count
, GFP_KERNEL
);
234 memset(pte
, 0, count
);
236 if (read_lba(bdev
, le64_to_cpu(gpt
->partition_entry_lba
),
247 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
249 * @lba is the Logical Block Address of the partition table
251 * Description: returns GPT header on success, NULL on error. Allocates
252 * and fills a GPT header starting at @ from @bdev.
253 * Note: remember to free gpt when finished with it.
256 alloc_read_gpt_header(struct block_device
*bdev
, u64 lba
)
262 gpt
= kmalloc(sizeof (gpt_header
), GFP_KERNEL
);
265 memset(gpt
, 0, sizeof (gpt_header
));
267 if (read_lba(bdev
, lba
, (u8
*) gpt
,
268 sizeof (gpt_header
)) < sizeof (gpt_header
)) {
278 * is_gpt_valid() - tests one GPT header and PTEs for validity
280 * @lba is the logical block address of the GPT header to test
281 * @gpt is a GPT header ptr, filled on return.
282 * @ptes is a PTEs ptr, filled on return.
284 * Description: returns 1 if valid, 0 on error.
285 * If valid, returns pointers to newly allocated GPT header and PTEs.
288 is_gpt_valid(struct block_device
*bdev
, u64 lba
,
289 gpt_header
**gpt
, gpt_entry
**ptes
)
293 if (!bdev
|| !gpt
|| !ptes
)
295 if (!(*gpt
= alloc_read_gpt_header(bdev
, lba
)))
298 /* Check the GUID Partition Table signature */
299 if (le64_to_cpu((*gpt
)->signature
) != GPT_HEADER_SIGNATURE
) {
300 Dprintk("GUID Partition Table Header signature is wrong:"
302 (unsigned long long)le64_to_cpu((*gpt
)->signature
),
303 (unsigned long long)GPT_HEADER_SIGNATURE
);
309 /* Check the GUID Partition Table CRC */
310 origcrc
= le32_to_cpu((*gpt
)->header_crc32
);
311 (*gpt
)->header_crc32
= 0;
312 crc
= efi_crc32((const unsigned char *) (*gpt
), le32_to_cpu((*gpt
)->header_size
));
314 if (crc
!= origcrc
) {
316 ("GUID Partition Table Header CRC is wrong: %x != %x\n",
322 (*gpt
)->header_crc32
= cpu_to_le32(origcrc
);
324 /* Check that the my_lba entry points to the LBA that contains
325 * the GUID Partition Table */
326 if (le64_to_cpu((*gpt
)->my_lba
) != lba
) {
327 Dprintk("GPT my_lba incorrect: %lld != %lld\n",
328 (unsigned long long)le64_to_cpu((*gpt
)->my_lba
),
329 (unsigned long long)lba
);
335 if (!(*ptes
= alloc_read_gpt_entries(bdev
, *gpt
))) {
341 /* Check the GUID Partition Entry Array CRC */
342 crc
= efi_crc32((const unsigned char *) (*ptes
),
343 le32_to_cpu((*gpt
)->num_partition_entries
) *
344 le32_to_cpu((*gpt
)->sizeof_partition_entry
));
346 if (crc
!= le32_to_cpu((*gpt
)->partition_entry_array_crc32
)) {
347 Dprintk("GUID Partitition Entry Array CRC check failed.\n");
355 /* We're done, all's well */
360 * compare_gpts() - Search disk for valid GPT headers and PTEs
361 * @pgpt is the primary GPT header
362 * @agpt is the alternate GPT header
363 * @lastlba is the last LBA number
364 * Description: Returns nothing. Sanity checks pgpt and agpt fields
365 * and prints warnings on discrepancies.
369 compare_gpts(gpt_header
*pgpt
, gpt_header
*agpt
, u64 lastlba
)
374 if (le64_to_cpu(pgpt
->my_lba
) != le64_to_cpu(agpt
->alternate_lba
)) {
376 "GPT:Primary header LBA != Alt. header alternate_lba\n");
377 printk(KERN_WARNING
"GPT:%lld != %lld\n",
378 (unsigned long long)le64_to_cpu(pgpt
->my_lba
),
379 (unsigned long long)le64_to_cpu(agpt
->alternate_lba
));
382 if (le64_to_cpu(pgpt
->alternate_lba
) != le64_to_cpu(agpt
->my_lba
)) {
384 "GPT:Primary header alternate_lba != Alt. header my_lba\n");
385 printk(KERN_WARNING
"GPT:%lld != %lld\n",
386 (unsigned long long)le64_to_cpu(pgpt
->alternate_lba
),
387 (unsigned long long)le64_to_cpu(agpt
->my_lba
));
390 if (le64_to_cpu(pgpt
->first_usable_lba
) !=
391 le64_to_cpu(agpt
->first_usable_lba
)) {
392 printk(KERN_WARNING
"GPT:first_usable_lbas don't match.\n");
393 printk(KERN_WARNING
"GPT:%lld != %lld\n",
394 (unsigned long long)le64_to_cpu(pgpt
->first_usable_lba
),
395 (unsigned long long)le64_to_cpu(agpt
->first_usable_lba
));
398 if (le64_to_cpu(pgpt
->last_usable_lba
) !=
399 le64_to_cpu(agpt
->last_usable_lba
)) {
400 printk(KERN_WARNING
"GPT:last_usable_lbas don't match.\n");
401 printk(KERN_WARNING
"GPT:%lld != %lld\n",
402 (unsigned long long)le64_to_cpu(pgpt
->last_usable_lba
),
403 (unsigned long long)le64_to_cpu(agpt
->last_usable_lba
));
406 if (efi_guidcmp(pgpt
->disk_guid
, agpt
->disk_guid
)) {
407 printk(KERN_WARNING
"GPT:disk_guids don't match.\n");
410 if (le32_to_cpu(pgpt
->num_partition_entries
) !=
411 le32_to_cpu(agpt
->num_partition_entries
)) {
412 printk(KERN_WARNING
"GPT:num_partition_entries don't match: "
414 le32_to_cpu(pgpt
->num_partition_entries
),
415 le32_to_cpu(agpt
->num_partition_entries
));
418 if (le32_to_cpu(pgpt
->sizeof_partition_entry
) !=
419 le32_to_cpu(agpt
->sizeof_partition_entry
)) {
421 "GPT:sizeof_partition_entry values don't match: "
423 le32_to_cpu(pgpt
->sizeof_partition_entry
),
424 le32_to_cpu(agpt
->sizeof_partition_entry
));
427 if (le32_to_cpu(pgpt
->partition_entry_array_crc32
) !=
428 le32_to_cpu(agpt
->partition_entry_array_crc32
)) {
430 "GPT:partition_entry_array_crc32 values don't match: "
432 le32_to_cpu(pgpt
->partition_entry_array_crc32
),
433 le32_to_cpu(agpt
->partition_entry_array_crc32
));
436 if (le64_to_cpu(pgpt
->alternate_lba
) != lastlba
) {
438 "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
439 printk(KERN_WARNING
"GPT:%lld != %lld\n",
440 (unsigned long long)le64_to_cpu(pgpt
->alternate_lba
),
441 (unsigned long long)lastlba
);
445 if (le64_to_cpu(agpt
->my_lba
) != lastlba
) {
447 "GPT:Alternate GPT header not at the end of the disk.\n");
448 printk(KERN_WARNING
"GPT:%lld != %lld\n",
449 (unsigned long long)le64_to_cpu(agpt
->my_lba
),
450 (unsigned long long)lastlba
);
456 "GPT: Use GNU Parted to correct GPT errors.\n");
461 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
463 * @gpt is a GPT header ptr, filled on return.
464 * @ptes is a PTEs ptr, filled on return.
465 * Description: Returns 1 if valid, 0 on error.
466 * If valid, returns pointers to newly allocated GPT header and PTEs.
467 * Validity depends on finding either the Primary GPT header and PTEs valid,
468 * or the Alternate GPT header and PTEs valid, and the PMBR valid.
471 find_valid_gpt(struct block_device
*bdev
, gpt_header
**gpt
, gpt_entry
**ptes
)
473 int good_pgpt
= 0, good_agpt
= 0, good_pmbr
= 0;
474 gpt_header
*pgpt
= NULL
, *agpt
= NULL
;
475 gpt_entry
*pptes
= NULL
, *aptes
= NULL
;
476 legacy_mbr
*legacymbr
= NULL
;
478 if (!bdev
|| !gpt
|| !ptes
)
481 lastlba
= last_lba(bdev
);
482 good_pgpt
= is_gpt_valid(bdev
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
485 good_agpt
= is_gpt_valid(bdev
,
486 le64_to_cpu(pgpt
->alternate_lba
),
489 good_agpt
= is_gpt_valid(bdev
, lastlba
,
494 good_agpt
= is_gpt_valid(bdev
, lastlba
,
498 /* The obviously unsuccessful case */
499 if (!good_pgpt
&& !good_agpt
) {
503 /* This will be added to the EFI Spec. per Intel after v1.02. */
504 legacymbr
= kmalloc(sizeof (*legacymbr
), GFP_KERNEL
);
506 memset(legacymbr
, 0, sizeof (*legacymbr
));
507 read_lba(bdev
, 0, (u8
*) legacymbr
,
508 sizeof (*legacymbr
));
509 good_pmbr
= is_pmbr_valid(legacymbr
);
514 /* Failure due to bad PMBR */
515 if ((good_pgpt
|| good_agpt
) && !good_pmbr
&& !force_gpt
) {
517 " Warning: Disk has a valid GPT signature "
518 "but invalid PMBR.\n");
520 " Assuming this disk is *not* a GPT disk anymore.\n");
522 " Use gpt kernel option to override. "
523 "Use GNU Parted to correct disk.\n");
527 /* Would fail due to bad PMBR, but force GPT anyhow */
528 if ((good_pgpt
|| good_agpt
) && !good_pmbr
&& force_gpt
) {
530 " Warning: Disk has a valid GPT signature but "
533 " Use GNU Parted to correct disk.\n");
535 " gpt option taken, disk treated as GPT.\n");
538 compare_gpts(pgpt
, agpt
, lastlba
);
541 if (good_pgpt
&& (good_pmbr
|| force_gpt
)) {
544 if (agpt
) { kfree(agpt
); agpt
= NULL
; }
545 if (aptes
) { kfree(aptes
); aptes
= NULL
; }
548 "Alternate GPT is invalid, "
549 "using primary GPT.\n");
553 else if (good_agpt
&& (good_pmbr
|| force_gpt
)) {
556 if (pgpt
) { kfree(pgpt
); pgpt
= NULL
; }
557 if (pptes
) { kfree(pptes
); pptes
= NULL
; }
559 "Primary GPT is invalid, using alternate GPT.\n");
564 if (pgpt
) { kfree(pgpt
); pgpt
=NULL
; }
565 if (agpt
) { kfree(agpt
); agpt
=NULL
; }
566 if (pptes
) { kfree(pptes
); pptes
=NULL
; }
567 if (aptes
) { kfree(aptes
); aptes
=NULL
; }
574 * efi_partition(struct parsed_partitions *state, struct block_device *bdev)
578 * Description: called from check.c, if the disk contains GPT
579 * partitions, sets up partition entries in the kernel.
581 * If the first block on the disk is a legacy MBR,
582 * it will get handled by msdos_partition().
583 * If it's a Protective MBR, we'll handle it here.
585 * We do not create a Linux partition for GPT, but
586 * only for the actual data partitions.
588 * -1 if unable to read the partition table
589 * 0 if this isn't our partition table
594 efi_partition(struct parsed_partitions
*state
, struct block_device
*bdev
)
596 gpt_header
*gpt
= NULL
;
597 gpt_entry
*ptes
= NULL
;
600 if (!find_valid_gpt(bdev
, &gpt
, &ptes
) || !gpt
|| !ptes
) {
606 Dprintk("GUID Partition Table is valid! Yea!\n");
608 for (i
= 0; i
< le32_to_cpu(gpt
->num_partition_entries
) && i
< state
->limit
-1; i
++) {
609 if (!efi_guidcmp(ptes
[i
].partition_type_guid
, NULL_GUID
))
612 put_partition(state
, i
+1, le64_to_cpu(ptes
[i
].starting_lba
),
613 (le64_to_cpu(ptes
[i
].ending_lba
) -
614 le64_to_cpu(ptes
[i
].starting_lba
) +
617 /* If there's this is a RAID volume, tell md */
618 if (!efi_guidcmp(ptes
[i
].partition_type_guid
,
619 PARTITION_LINUX_RAID_GUID
))
620 state
->parts
[i
+1].flags
= 1;
629 * Overrides for Emacs so that we follow Linus's tabbing style.
630 * Emacs will notice this stuff at the end of the file and automatically
631 * adjust the settings for this buffer only. This must remain at the end
633 * ---------------------------------------------------------------------------
636 * c-brace-imaginary-offset: 0
638 * c-argdecl-indent: 4
640 * c-continued-statement-offset: 4
641 * c-continued-brace-offset: 0
642 * indent-tabs-mode: nil