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 Computer Corporation
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 * Wed Mar 27 2002 Matt Domsch <Matt_Domsch@dell.com>
27 * - Ported to 2.5.7-pre1 and 2.4.18
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.
35 * - 2.4.18 patch needs own crc32() function - there's no official
36 * lib/crc32.c in 2.4.x.
38 * Mon Feb 04 2002 Matt Domsch <Matt_Domsch@dell.com>
39 * - Removed __PRIPTR_PREFIX - not being used
41 * Mon Jan 14 2002 Matt Domsch <Matt_Domsch@dell.com>
42 * - Ported to 2.5.2-pre11 + library crc32 patch Linus applied
44 * Thu Dec 6 2001 Matt Domsch <Matt_Domsch@dell.com>
45 * - Added compare_gpts().
46 * - moved le_efi_guid_to_cpus() back into this file. GPT is the only
47 * thing that keeps EFI GUIDs on disk.
48 * - Changed gpt structure names and members to be simpler and more Linux-like.
50 * Wed Oct 17 2001 Matt Domsch <Matt_Domsch@dell.com>
51 * - Removed CONFIG_DEVFS_VOLUMES_UUID code entirely per Martin Wilck
53 * Wed Oct 10 2001 Matt Domsch <Matt_Domsch@dell.com>
54 * - Changed function comments to DocBook style per Andreas Dilger suggestion.
56 * Mon Oct 08 2001 Matt Domsch <Matt_Domsch@dell.com>
57 * - Change read_lba() to use the page cache per Al Viro's work.
58 * - print u64s properly on all architectures
59 * - fixed debug_printk(), now Dprintk()
61 * Mon Oct 01 2001 Matt Domsch <Matt_Domsch@dell.com>
63 * - made most functions static
64 * - Endianness addition
65 * - remove test for second alternate header, as it's not per spec,
66 * and is unnecessary. There's now a method to read/write the last
67 * sector of an odd-sized disk from user space. No tools have ever
68 * been released which used this code, so it's effectively dead.
69 * - Per Asit Mallick of Intel, added a test for a valid PMBR.
70 * - Added kernel command line option 'gpt' to override valid PMBR test.
72 * Wed Jun 6 2001 Martin Wilck <Martin.Wilck@Fujitsu-Siemens.com>
73 * - added devfs volume UUID support (/dev/volumes/uuids) for
74 * mounting file systems by the partition GUID.
76 * Tue Dec 5 2000 Matt Domsch <Matt_Domsch@dell.com>
77 * - Moved crc32() to linux/lib, added efi_crc32().
79 * Thu Nov 30 2000 Matt Domsch <Matt_Domsch@dell.com>
80 * - Replaced Intel's CRC32 function with an equivalent
81 * non-license-restricted version.
83 * Wed Oct 25 2000 Matt Domsch <Matt_Domsch@dell.com>
84 * - Fixed the last_lba() call to return the proper last block
86 * Thu Oct 12 2000 Matt Domsch <Matt_Domsch@dell.com>
87 * - Thanks to Andries Brouwer for his debugging assistance.
88 * - Code works, detects all the partitions.
90 ************************************************************/
91 #include <linux/config.h>
93 #include <linux/genhd.h>
94 #include <linux/kernel.h>
95 #include <linux/major.h>
96 #include <linux/string.h>
97 #include <linux/blk.h>
98 #include <linux/blkpg.h>
99 #include <linux/slab.h>
100 #include <linux/smp_lock.h>
101 #include <linux/init.h>
102 #include <linux/crc32.h>
103 #include <asm/system.h>
104 #include <asm/byteorder.h>
108 #if CONFIG_BLK_DEV_MD
109 extern void md_autodetect_dev(kdev_t dev);
112 /* Handle printing of 64-bit values */
113 /* Borrowed from /usr/include/inttypes.h */
114 # if BITS_PER_LONG == 64
115 # define __PRI64_PREFIX "l"
117 # define __PRI64_PREFIX "ll"
119 # define PRIx64 __PRI64_PREFIX "x"
124 #define Dprintk(x...) printk(KERN_DEBUG x)
126 #define Dprintk(x...)
129 /* This allows a kernel command line option 'gpt' to override
130 * the test for invalid PMBR. Not __initdata because reloading
131 * the partition tables happens after init too.
133 static int force_gpt;
135 force_gpt_fn(char *str)
140 __setup("gpt", force_gpt_fn);
143 * efi_crc32() - EFI version of crc32 function
144 * @buf: buffer to calculate crc32 of
145 * @len - length of buf
147 * Description: Returns EFI-style CRC32 value for @buf
149 * This function uses the little endian Ethernet polynomial
150 * but seeds the function with ~0, and xor's with ~0 at the end.
151 * Note, the EFI Specification, v1.02, has a reference to
152 * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
155 efi_crc32(const void *buf, unsigned long len)
157 return (crc32(~0L, buf, len) ^ ~0L);
161 * is_pmbr_valid(): test Protective MBR for validity
162 * @mbr: pointer to a legacy mbr structure
164 * Description: Returns 1 if PMBR is valid, 0 otherwise.
165 * Validity depends on two things:
166 * 1) MSDOS signature is in the last two bytes of the MBR
167 * 2) One partition of type 0xEE is found
170 is_pmbr_valid(legacy_mbr *mbr)
172 int i, found = 0, signature = 0;
175 signature = (le16_to_cpu(mbr->signature) == MSDOS_MBR_SIGNATURE);
176 for (i = 0; signature && i < 4; i++) {
177 if (mbr->partition_record[i].sys_ind ==
178 EFI_PMBR_OSTYPE_EFI_GPT) {
183 return (signature && found);
187 * last_lba(): return number of last logical block of device
188 * @hd: gendisk with partition list
189 * @bdev: block device
191 * Description: Returns last LBA value on success, 0 on error.
192 * This is stored (by sd and ide-geometry) in
193 * the part[0] entry for this disk, and is the number of
194 * physical sectors available on the disk.
197 last_lba(struct gendisk *hd, struct block_device *bdev)
199 if (!hd || !hd->part || !bdev)
201 return hd->part[MINOR(to_kdev_t(bdev->bd_dev))].nr_sects - 1;
205 * read_lba(): Read bytes from disk, starting at given LBA
212 * Description: Reads @count bytes from @bdev into @buffer.
213 * Returns number of bytes read on success, 0 on error.
216 read_lba(struct gendisk *hd, struct block_device *bdev, u64 lba,
217 u8 * buffer, size_t count)
220 size_t totalreadcount = 0, bytesread = 0;
221 unsigned long blocksize;
224 unsigned char *data = NULL;
226 if (!hd || !bdev || !buffer || !count)
229 blocksize = get_hardsect_size(to_kdev_t(bdev->bd_dev));
233 for (i = 0; count > 0; i++) {
234 data = read_dev_sector(bdev, lba, §);
236 return totalreadcount;
239 PAGE_CACHE_SIZE - (data -
240 (unsigned char *) page_address(sect.v));
241 bytesread = min(bytesread, count);
242 memcpy(buffer, data, bytesread);
243 put_dev_sector(sect);
246 totalreadcount += bytesread;
248 lba += (bytesread / blocksize);
250 return totalreadcount;
255 * alloc_read_gpt_entries(): reads partition entries from disk
260 * Description: Returns ptes on success, NULL on error.
261 * Allocates space for PTEs based on information found in @gpt.
262 * Notes: remember to free pte when you're done!
265 alloc_read_gpt_entries(struct gendisk *hd,
266 struct block_device *bdev, gpt_header *gpt)
270 if (!hd || !bdev || !gpt)
273 count = le32_to_cpu(gpt->num_partition_entries) *
274 le32_to_cpu(gpt->sizeof_partition_entry);
277 pte = kmalloc(count, GFP_KERNEL);
280 memset(pte, 0, count);
282 if (read_lba(hd, bdev, le64_to_cpu(gpt->partition_entry_lba),
293 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
296 * @lba is the Logical Block Address of the partition table
298 * Description: returns GPT header on success, NULL on error. Allocates
299 * and fills a GPT header starting at @ from @bdev.
300 * Note: remember to free gpt when finished with it.
303 alloc_read_gpt_header(struct gendisk *hd, struct block_device *bdev, u64 lba)
309 gpt = kmalloc(sizeof (gpt_header), GFP_KERNEL);
312 memset(gpt, 0, sizeof (gpt_header));
314 if (read_lba(hd, bdev, lba, (u8 *) gpt,
315 sizeof (gpt_header)) < sizeof (gpt_header)) {
325 * is_gpt_valid() - tests one GPT header and PTEs for validity
328 * @lba is the logical block address of the GPT header to test
329 * @gpt is a GPT header ptr, filled on return.
330 * @ptes is a PTEs ptr, filled on return.
332 * Description: returns 1 if valid, 0 on error.
333 * If valid, returns pointers to newly allocated GPT header and PTEs.
336 is_gpt_valid(struct gendisk *hd, struct block_device *bdev, u64 lba,
337 gpt_header **gpt, gpt_entry **ptes)
341 if (!hd || !bdev || !gpt || !ptes)
343 if (!(*gpt = alloc_read_gpt_header(hd, bdev, lba)))
346 /* Check the GUID Partition Table signature */
347 if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
348 Dprintk("GUID Partition Table Header signature is wrong: %"
349 PRIx64 " != %" PRIx64 "\n", le64_to_cpu((*gpt)->signature),
350 GPT_HEADER_SIGNATURE);
356 /* Check the GUID Partition Table CRC */
357 origcrc = le32_to_cpu((*gpt)->header_crc32);
358 (*gpt)->header_crc32 = 0;
359 crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
361 if (crc != origcrc) {
363 ("GUID Partition Table Header CRC is wrong: %x != %x\n",
369 (*gpt)->header_crc32 = cpu_to_le32(origcrc);
371 /* Check that the my_lba entry points to the LBA that contains
372 * the GUID Partition Table */
373 if (le64_to_cpu((*gpt)->my_lba) != lba) {
374 Dprintk("GPT my_lba incorrect: %" PRIx64 " != %" PRIx64 "\n",
375 le64_to_cpu((*gpt)->my_lba), lba);
381 if (!(*ptes = alloc_read_gpt_entries(hd, bdev, *gpt))) {
387 /* Check the GUID Partition Entry Array CRC */
388 crc = efi_crc32((const unsigned char *) (*ptes),
389 le32_to_cpu((*gpt)->num_partition_entries) *
390 le32_to_cpu((*gpt)->sizeof_partition_entry));
392 if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
393 Dprintk("GUID Partitition Entry Array CRC check failed.\n");
401 /* We're done, all's well */
406 * compare_gpts() - Search disk for valid GPT headers and PTEs
407 * @pgpt is the primary GPT header
408 * @agpt is the alternate GPT header
409 * @lastlba is the last LBA number
410 * Description: Returns nothing. Sanity checks pgpt and agpt fields
411 * and prints warnings on discrepancies.
415 compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
420 if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
422 "GPT:Primary header LBA != Alt. header alternate_lba\n");
423 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
424 le64_to_cpu(pgpt->my_lba),
425 le64_to_cpu(agpt->alternate_lba));
428 if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
430 "GPT:Primary header alternate_lba != Alt. header my_lba\n");
431 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
432 le64_to_cpu(pgpt->alternate_lba),
433 le64_to_cpu(agpt->my_lba));
436 if (le64_to_cpu(pgpt->first_usable_lba) !=
437 le64_to_cpu(agpt->first_usable_lba)) {
438 printk(KERN_WARNING "GPT:first_usable_lbas don't match.\n");
439 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
440 le64_to_cpu(pgpt->first_usable_lba),
441 le64_to_cpu(agpt->first_usable_lba));
444 if (le64_to_cpu(pgpt->last_usable_lba) !=
445 le64_to_cpu(agpt->last_usable_lba)) {
446 printk(KERN_WARNING "GPT:last_usable_lbas don't match.\n");
447 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
448 le64_to_cpu(pgpt->last_usable_lba),
449 le64_to_cpu(agpt->last_usable_lba));
452 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
453 printk(KERN_WARNING "GPT:disk_guids don't match.\n");
456 if (le32_to_cpu(pgpt->num_partition_entries) !=
457 le32_to_cpu(agpt->num_partition_entries)) {
458 printk(KERN_WARNING "GPT:num_partition_entries don't match: "
460 le32_to_cpu(pgpt->num_partition_entries),
461 le32_to_cpu(agpt->num_partition_entries));
464 if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
465 le32_to_cpu(agpt->sizeof_partition_entry)) {
467 "GPT:sizeof_partition_entry values don't match: "
469 le32_to_cpu(pgpt->sizeof_partition_entry),
470 le32_to_cpu(agpt->sizeof_partition_entry));
473 if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
474 le32_to_cpu(agpt->partition_entry_array_crc32)) {
476 "GPT:partition_entry_array_crc32 values don't match: "
478 le32_to_cpu(pgpt->partition_entry_array_crc32),
479 le32_to_cpu(agpt->partition_entry_array_crc32));
482 if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
484 "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
485 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
486 le64_to_cpu(pgpt->alternate_lba), lastlba);
490 if (le64_to_cpu(agpt->my_lba) != lastlba) {
492 "GPT:Alternate GPT header not at the end of the disk.\n");
493 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
494 le64_to_cpu(agpt->my_lba), lastlba);
500 "GPT: Use GNU Parted to correct GPT errors.\n");
505 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
508 * @gpt is a GPT header ptr, filled on return.
509 * @ptes is a PTEs ptr, filled on return.
510 * Description: Returns 1 if valid, 0 on error.
511 * If valid, returns pointers to newly allocated GPT header and PTEs.
512 * Validity depends on finding either the Primary GPT header and PTEs valid,
513 * or the Alternate GPT header and PTEs valid, and the PMBR valid.
516 find_valid_gpt(struct gendisk *hd, struct block_device *bdev,
517 gpt_header **gpt, gpt_entry **ptes)
519 int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
520 gpt_header *pgpt = NULL, *agpt = NULL;
521 gpt_entry *pptes = NULL, *aptes = NULL;
522 legacy_mbr *legacymbr = NULL;
524 if (!hd || !bdev || !gpt || !ptes)
527 lastlba = last_lba(hd, bdev);
528 good_pgpt = is_gpt_valid(hd, bdev, GPT_PRIMARY_PARTITION_TABLE_LBA,
531 good_agpt = is_gpt_valid(hd, bdev,
532 le64_to_cpu(pgpt->alternate_lba),
535 good_agpt = is_gpt_valid(hd, bdev, lastlba,
540 good_agpt = is_gpt_valid(hd, bdev, lastlba,
544 /* The obviously unsuccessful case */
545 if (!good_pgpt && !good_agpt) {
549 /* This will be added to the EFI Spec. per Intel after v1.02. */
550 legacymbr = kmalloc(sizeof (*legacymbr), GFP_KERNEL);
552 memset(legacymbr, 0, sizeof (*legacymbr));
553 read_lba(hd, bdev, 0, (u8 *) legacymbr,
554 sizeof (*legacymbr));
555 good_pmbr = is_pmbr_valid(legacymbr);
560 /* Failure due to bad PMBR */
561 if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {
563 " Warning: Disk has a valid GPT signature "
564 "but invalid PMBR.\n");
566 " Assuming this disk is *not* a GPT disk anymore.\n");
568 " Use gpt kernel option to override. "
569 "Use GNU Parted to correct disk.\n");
573 /* Would fail due to bad PMBR, but force GPT anyhow */
574 if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {
576 " Warning: Disk has a valid GPT signature but "
579 " Use GNU Parted to correct disk.\n");
581 " gpt option taken, disk treated as GPT.\n");
584 compare_gpts(pgpt, agpt, lastlba);
587 if (good_pgpt && (good_pmbr || force_gpt)) {
590 if (agpt) { kfree(agpt); agpt = NULL; }
591 if (aptes) { kfree(aptes); aptes = NULL; }
594 "Alternate GPT is invalid, "
595 "using primary GPT.\n");
599 else if (good_agpt && (good_pmbr || force_gpt)) {
602 if (pgpt) { kfree(pgpt); pgpt = NULL; }
603 if (pptes) { kfree(pptes); pptes = NULL; }
605 "Primary GPT is invalid, using alternate GPT.\n");
610 if (pgpt) { kfree(pgpt); pgpt=NULL; }
611 if (agpt) { kfree(agpt); agpt=NULL; }
612 if (pptes) { kfree(pptes); pptes=NULL; }
613 if (aptes) { kfree(aptes); aptes=NULL; }
620 * add_gpt_partitions(struct gendisk *hd, struct block_device *bdev,
624 * Description: Create devices for each entry in the GUID Partition Table
627 * We do not create a Linux partition for GPT, but
628 * only for the actual data partitions.
630 * -1 if unable to read the partition table
631 * 0 if this isn't our partition table
636 add_gpt_partitions(struct gendisk *hd, struct block_device *bdev, int nextminor)
638 gpt_header *gpt = NULL;
639 gpt_entry *ptes = NULL;
646 if (!find_valid_gpt(hd, bdev, &gpt, &ptes) || !gpt || !ptes) {
658 Dprintk("GUID Partition Table is valid! Yea!\n");
660 max_p = (1 << hd->minor_shift) - 1;
661 for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < max_p; i++) {
662 if (!efi_guidcmp(ptes[i].partition_type_guid, NULL_GUID))
665 add_gd_partition(hd, nextminor+i,
666 le64_to_cpu(ptes[i].starting_lba),
667 (le64_to_cpu(ptes[i].ending_lba) -
668 le64_to_cpu(ptes[i].starting_lba) +
671 /* If there's this is a RAID volume, tell md */
672 #if CONFIG_BLK_DEV_MD
673 if (!efi_guidcmp(ptes[i].partition_type_guid,
674 PARTITION_LINUX_RAID_GUID)) {
675 md_autodetect_dev(MKDEV
676 (MAJOR(to_kdev_t(bdev->bd_dev)),
690 * efi_partition(): EFI GPT partition handling entry function
693 * @first_sector: unused
694 * @first_part_minor: minor number assigned to first GPT partition found
696 * Description: called from check.c, if the disk contains GPT
697 * partitions, sets up partition entries in the kernel.
699 * If the first block on the disk is a legacy MBR,
700 * it will get handled by msdos_partition().
701 * If it's a Protective MBR, we'll handle it here.
703 * set_blocksize() calls are necessary to be able to read
704 * a disk with an odd number of 512-byte sectors, as the
705 * default BLOCK_SIZE of 1024 bytes won't let that last
706 * sector be read otherwise.
709 * -1 if unable to read the partition table
710 * 0 if this isn't our partitoin table
714 efi_partition(struct gendisk *hd, struct block_device *bdev,
715 unsigned long first_sector, int first_part_minor)
718 kdev_t dev = to_kdev_t(bdev->bd_dev);
719 int hardblocksize = get_hardsect_size(dev);
720 int orig_blksize_size = BLOCK_SIZE;
723 /* Need to change the block size that the block layer uses */
724 if (blksize_size[MAJOR(dev)]) {
725 orig_blksize_size = blksize_size[MAJOR(dev)][MINOR(dev)];
728 if (orig_blksize_size != hardblocksize)
729 set_blocksize(dev, hardblocksize);
731 rc = add_gpt_partitions(hd, bdev, first_part_minor);
734 if (orig_blksize_size != hardblocksize)
735 set_blocksize(dev, orig_blksize_size);