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 <asm/system.h>
103 #include <asm/byteorder.h>
107 #if CONFIG_BLK_DEV_MD
108 extern void md_autodetect_dev(kdev_t dev);
111 /* Handle printing of 64-bit values */
112 /* Borrowed from /usr/include/inttypes.h */
113 # if BITS_PER_LONG == 64
114 # define __PRI64_PREFIX "l"
116 # define __PRI64_PREFIX "ll"
118 # define PRIx64 __PRI64_PREFIX "x"
123 #define Dprintk(x...) printk(KERN_DEBUG x)
125 #define Dprintk(x...)
128 /* This allows a kernel command line option 'gpt' to override
129 * the test for invalid PMBR. Not __initdata because reloading
130 * the partition tables happens after init too.
132 static int force_gpt;
134 force_gpt_fn(char *str)
139 __setup("gpt", force_gpt_fn);
143 * There are multiple 16-bit CRC polynomials in common use, but this is
144 * *the* standard CRC-32 polynomial, first popularized by Ethernet.
145 * x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0
147 #define CRCPOLY_LE 0xedb88320
148 /* How many bits at a time to use. Requires a table of 4<<CRC_xx_BITS bytes. */
149 /* For less performance-sensitive, use 4 */
150 #define CRC_LE_BITS 8
151 static u32 *crc32table_le;
154 * crc32init_le() - allocate and initialize LE table data
156 * crc is the crc of the byte i; other entries are filled in based on the
157 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
160 static int __init crc32init_le(void)
166 kmalloc((1 << CRC_LE_BITS) * sizeof(u32), GFP_KERNEL);
169 crc32table_le[0] = 0;
171 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
172 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
173 for (j = 0; j < 1 << CRC_LE_BITS; j += 2 * i)
174 crc32table_le[i + j] = crc ^ crc32table_le[j];
180 * crc32cleanup_le(): free LE table data
182 static void __exit crc32cleanup_le(void)
184 if (crc32table_le) kfree(crc32table_le);
185 crc32table_le = NULL;
188 __initcall(crc32init_le);
189 __exitcall(crc32cleanup_le);
192 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
193 * @crc - seed value for computation. ~0 for Ethernet, sometimes 0 for
194 * other uses, or the previous crc32 value if computing incrementally.
195 * @p - pointer to buffer over which CRC is run
196 * @len - length of buffer @p
199 static u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
202 crc = (crc >> 8) ^ crc32table_le[(crc ^ *p++) & 255];
209 * efi_crc32() - EFI version of crc32 function
210 * @buf: buffer to calculate crc32 of
211 * @len - length of buf
213 * Description: Returns EFI-style CRC32 value for @buf
215 * This function uses the little endian Ethernet polynomial
216 * but seeds the function with ~0, and xor's with ~0 at the end.
217 * Note, the EFI Specification, v1.02, has a reference to
218 * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
221 efi_crc32(const void *buf, unsigned long len)
223 return (crc32_le(~0L, buf, len) ^ ~0L);
227 * is_pmbr_valid(): test Protective MBR for validity
228 * @mbr: pointer to a legacy mbr structure
230 * Description: Returns 1 if PMBR is valid, 0 otherwise.
231 * Validity depends on two things:
232 * 1) MSDOS signature is in the last two bytes of the MBR
233 * 2) One partition of type 0xEE is found
236 is_pmbr_valid(legacy_mbr *mbr)
238 int i, found = 0, signature = 0;
241 signature = (le16_to_cpu(mbr->signature) == MSDOS_MBR_SIGNATURE);
242 for (i = 0; signature && i < 4; i++) {
243 if (mbr->partition_record[i].sys_ind ==
244 EFI_PMBR_OSTYPE_EFI_GPT) {
249 return (signature && found);
253 * last_lba(): return number of last logical block of device
254 * @hd: gendisk with partition list
255 * @bdev: block device
257 * Description: Returns last LBA value on success, 0 on error.
258 * This is stored (by sd and ide-geometry) in
259 * the part[0] entry for this disk, and is the number of
260 * physical sectors available on the disk.
263 last_lba(struct gendisk *hd, struct block_device *bdev)
265 if (!hd || !hd->part || !bdev)
267 return hd->part[MINOR(to_kdev_t(bdev->bd_dev))].nr_sects - 1;
271 * read_lba(): Read bytes from disk, starting at given LBA
278 * Description: Reads @count bytes from @bdev into @buffer.
279 * Returns number of bytes read on success, 0 on error.
282 read_lba(struct gendisk *hd, struct block_device *bdev, u64 lba,
283 u8 * buffer, size_t count)
286 size_t totalreadcount = 0, bytesread = 0;
287 unsigned long blocksize;
290 unsigned char *data = NULL;
292 if (!hd || !bdev || !buffer || !count)
295 blocksize = get_hardsect_size(to_kdev_t(bdev->bd_dev));
299 for (i = 0; count > 0; i++) {
300 data = read_dev_sector(bdev, lba, §);
302 return totalreadcount;
305 PAGE_CACHE_SIZE - (data -
306 (unsigned char *) page_address(sect.v));
307 bytesread = min(bytesread, count);
308 memcpy(buffer, data, bytesread);
309 put_dev_sector(sect);
312 totalreadcount += bytesread;
314 lba += (bytesread / blocksize);
316 return totalreadcount;
321 * alloc_read_gpt_entries(): reads partition entries from disk
326 * Description: Returns ptes on success, NULL on error.
327 * Allocates space for PTEs based on information found in @gpt.
328 * Notes: remember to free pte when you're done!
331 alloc_read_gpt_entries(struct gendisk *hd,
332 struct block_device *bdev, gpt_header *gpt)
336 if (!hd || !bdev || !gpt)
339 count = le32_to_cpu(gpt->num_partition_entries) *
340 le32_to_cpu(gpt->sizeof_partition_entry);
343 pte = kmalloc(count, GFP_KERNEL);
346 memset(pte, 0, count);
348 if (read_lba(hd, bdev, le64_to_cpu(gpt->partition_entry_lba),
359 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
362 * @lba is the Logical Block Address of the partition table
364 * Description: returns GPT header on success, NULL on error. Allocates
365 * and fills a GPT header starting at @ from @bdev.
366 * Note: remember to free gpt when finished with it.
369 alloc_read_gpt_header(struct gendisk *hd, struct block_device *bdev, u64 lba)
375 gpt = kmalloc(sizeof (gpt_header), GFP_KERNEL);
378 memset(gpt, 0, sizeof (gpt_header));
380 if (read_lba(hd, bdev, lba, (u8 *) gpt,
381 sizeof (gpt_header)) < sizeof (gpt_header)) {
391 * is_gpt_valid() - tests one GPT header and PTEs for validity
394 * @lba is the logical block address of the GPT header to test
395 * @gpt is a GPT header ptr, filled on return.
396 * @ptes is a PTEs ptr, filled on return.
398 * Description: returns 1 if valid, 0 on error.
399 * If valid, returns pointers to newly allocated GPT header and PTEs.
402 is_gpt_valid(struct gendisk *hd, struct block_device *bdev, u64 lba,
403 gpt_header **gpt, gpt_entry **ptes)
407 if (!hd || !bdev || !gpt || !ptes)
409 if (!(*gpt = alloc_read_gpt_header(hd, bdev, lba)))
412 /* Check the GUID Partition Table signature */
413 if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
414 Dprintk("GUID Partition Table Header signature is wrong: %"
415 PRIx64 " != %" PRIx64 "\n", le64_to_cpu((*gpt)->signature),
416 GPT_HEADER_SIGNATURE);
422 /* Check the GUID Partition Table CRC */
423 origcrc = le32_to_cpu((*gpt)->header_crc32);
424 (*gpt)->header_crc32 = 0;
425 crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
427 if (crc != origcrc) {
429 ("GUID Partition Table Header CRC is wrong: %x != %x\n",
435 (*gpt)->header_crc32 = cpu_to_le32(origcrc);
437 /* Check that the my_lba entry points to the LBA that contains
438 * the GUID Partition Table */
439 if (le64_to_cpu((*gpt)->my_lba) != lba) {
440 Dprintk("GPT my_lba incorrect: %" PRIx64 " != %" PRIx64 "\n",
441 le64_to_cpu((*gpt)->my_lba), lba);
447 if (!(*ptes = alloc_read_gpt_entries(hd, bdev, *gpt))) {
453 /* Check the GUID Partition Entry Array CRC */
454 crc = efi_crc32((const unsigned char *) (*ptes),
455 le32_to_cpu((*gpt)->num_partition_entries) *
456 le32_to_cpu((*gpt)->sizeof_partition_entry));
458 if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
459 Dprintk("GUID Partitition Entry Array CRC check failed.\n");
467 /* We're done, all's well */
472 * compare_gpts() - Search disk for valid GPT headers and PTEs
473 * @pgpt is the primary GPT header
474 * @agpt is the alternate GPT header
475 * @lastlba is the last LBA number
476 * Description: Returns nothing. Sanity checks pgpt and agpt fields
477 * and prints warnings on discrepancies.
481 compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
486 if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
488 "GPT:Primary header LBA != Alt. header alternate_lba\n");
489 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
490 le64_to_cpu(pgpt->my_lba),
491 le64_to_cpu(agpt->alternate_lba));
494 if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
496 "GPT:Primary header alternate_lba != Alt. header my_lba\n");
497 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
498 le64_to_cpu(pgpt->alternate_lba),
499 le64_to_cpu(agpt->my_lba));
502 if (le64_to_cpu(pgpt->first_usable_lba) !=
503 le64_to_cpu(agpt->first_usable_lba)) {
504 printk(KERN_WARNING "GPT:first_usable_lbas don't match.\n");
505 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
506 le64_to_cpu(pgpt->first_usable_lba),
507 le64_to_cpu(agpt->first_usable_lba));
510 if (le64_to_cpu(pgpt->last_usable_lba) !=
511 le64_to_cpu(agpt->last_usable_lba)) {
512 printk(KERN_WARNING "GPT:last_usable_lbas don't match.\n");
513 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
514 le64_to_cpu(pgpt->last_usable_lba),
515 le64_to_cpu(agpt->last_usable_lba));
518 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
519 printk(KERN_WARNING "GPT:disk_guids don't match.\n");
522 if (le32_to_cpu(pgpt->num_partition_entries) !=
523 le32_to_cpu(agpt->num_partition_entries)) {
524 printk(KERN_WARNING "GPT:num_partition_entries don't match: "
526 le32_to_cpu(pgpt->num_partition_entries),
527 le32_to_cpu(agpt->num_partition_entries));
530 if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
531 le32_to_cpu(agpt->sizeof_partition_entry)) {
533 "GPT:sizeof_partition_entry values don't match: "
535 le32_to_cpu(pgpt->sizeof_partition_entry),
536 le32_to_cpu(agpt->sizeof_partition_entry));
539 if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
540 le32_to_cpu(agpt->partition_entry_array_crc32)) {
542 "GPT:partition_entry_array_crc32 values don't match: "
544 le32_to_cpu(pgpt->partition_entry_array_crc32),
545 le32_to_cpu(agpt->partition_entry_array_crc32));
548 if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
550 "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
551 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
552 le64_to_cpu(pgpt->alternate_lba), lastlba);
556 if (le64_to_cpu(agpt->my_lba) != lastlba) {
558 "GPT:Alternate GPT header not at the end of the disk.\n");
559 printk(KERN_WARNING "GPT:%" PRIx64 " != %" PRIx64 "\n",
560 le64_to_cpu(agpt->my_lba), lastlba);
566 "GPT: Use GNU Parted to correct GPT errors.\n");
571 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
574 * @gpt is a GPT header ptr, filled on return.
575 * @ptes is a PTEs ptr, filled on return.
576 * Description: Returns 1 if valid, 0 on error.
577 * If valid, returns pointers to newly allocated GPT header and PTEs.
578 * Validity depends on finding either the Primary GPT header and PTEs valid,
579 * or the Alternate GPT header and PTEs valid, and the PMBR valid.
582 find_valid_gpt(struct gendisk *hd, struct block_device *bdev,
583 gpt_header **gpt, gpt_entry **ptes)
585 int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
586 gpt_header *pgpt = NULL, *agpt = NULL;
587 gpt_entry *pptes = NULL, *aptes = NULL;
588 legacy_mbr *legacymbr = NULL;
590 if (!hd || !bdev || !gpt || !ptes)
593 lastlba = last_lba(hd, bdev);
594 good_pgpt = is_gpt_valid(hd, bdev, GPT_PRIMARY_PARTITION_TABLE_LBA,
597 good_agpt = is_gpt_valid(hd, bdev,
598 le64_to_cpu(pgpt->alternate_lba),
601 good_agpt = is_gpt_valid(hd, bdev, lastlba,
606 good_agpt = is_gpt_valid(hd, bdev, lastlba,
610 /* The obviously unsuccessful case */
611 if (!good_pgpt && !good_agpt) {
615 /* This will be added to the EFI Spec. per Intel after v1.02. */
616 legacymbr = kmalloc(sizeof (*legacymbr), GFP_KERNEL);
618 memset(legacymbr, 0, sizeof (*legacymbr));
619 read_lba(hd, bdev, 0, (u8 *) legacymbr,
620 sizeof (*legacymbr));
621 good_pmbr = is_pmbr_valid(legacymbr);
626 /* Failure due to bad PMBR */
627 if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {
629 " Warning: Disk has a valid GPT signature "
630 "but invalid PMBR.\n");
632 " Assuming this disk is *not* a GPT disk anymore.\n");
634 " Use gpt kernel option to override. "
635 "Use GNU Parted to correct disk.\n");
639 /* Would fail due to bad PMBR, but force GPT anyhow */
640 if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {
642 " Warning: Disk has a valid GPT signature but "
645 " Use GNU Parted to correct disk.\n");
647 " gpt option taken, disk treated as GPT.\n");
650 compare_gpts(pgpt, agpt, lastlba);
653 if (good_pgpt && (good_pmbr || force_gpt)) {
656 if (agpt) { kfree(agpt); agpt = NULL; }
657 if (aptes) { kfree(aptes); aptes = NULL; }
660 "Alternate GPT is invalid, "
661 "using primary GPT.\n");
665 else if (good_agpt && (good_pmbr || force_gpt)) {
668 if (pgpt) { kfree(pgpt); pgpt = NULL; }
669 if (pptes) { kfree(pptes); pptes = NULL; }
671 "Primary GPT is invalid, using alternate GPT.\n");
676 if (pgpt) { kfree(pgpt); pgpt=NULL; }
677 if (agpt) { kfree(agpt); agpt=NULL; }
678 if (pptes) { kfree(pptes); pptes=NULL; }
679 if (aptes) { kfree(aptes); aptes=NULL; }
686 * add_gpt_partitions(struct gendisk *hd, struct block_device *bdev,
690 * Description: Create devices for each entry in the GUID Partition Table
693 * We do not create a Linux partition for GPT, but
694 * only for the actual data partitions.
696 * -1 if unable to read the partition table
697 * 0 if this isn't our partition table
702 add_gpt_partitions(struct gendisk *hd, struct block_device *bdev, int nextminor)
704 gpt_header *gpt = NULL;
705 gpt_entry *ptes = NULL;
712 if (!find_valid_gpt(hd, bdev, &gpt, &ptes) || !gpt || !ptes) {
724 Dprintk("GUID Partition Table is valid! Yea!\n");
726 max_p = (1 << hd->minor_shift) - 1;
727 for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < max_p; i++) {
728 if (!efi_guidcmp(ptes[i].partition_type_guid, NULL_GUID))
731 add_gd_partition(hd, nextminor+i,
732 le64_to_cpu(ptes[i].starting_lba),
733 (le64_to_cpu(ptes[i].ending_lba) -
734 le64_to_cpu(ptes[i].starting_lba) +
737 /* If there's this is a RAID volume, tell md */
738 #if CONFIG_BLK_DEV_MD
739 if (!efi_guidcmp(ptes[i].partition_type_guid,
740 PARTITION_LINUX_RAID_GUID)) {
741 md_autodetect_dev(MKDEV
742 (MAJOR(to_kdev_t(bdev->bd_dev)),
756 * efi_partition(): EFI GPT partition handling entry function
759 * @first_sector: unused
760 * @first_part_minor: minor number assigned to first GPT partition found
762 * Description: called from check.c, if the disk contains GPT
763 * partitions, sets up partition entries in the kernel.
765 * If the first block on the disk is a legacy MBR,
766 * it will get handled by msdos_partition().
767 * If it's a Protective MBR, we'll handle it here.
769 * set_blocksize() calls are necessary to be able to read
770 * a disk with an odd number of 512-byte sectors, as the
771 * default BLOCK_SIZE of 1024 bytes won't let that last
772 * sector be read otherwise.
775 * -1 if unable to read the partition table
776 * 0 if this isn't our partitoin table
780 efi_partition(struct gendisk *hd, struct block_device *bdev,
781 unsigned long first_sector, int first_part_minor)
784 kdev_t dev = to_kdev_t(bdev->bd_dev);
785 int hardblocksize = get_hardsect_size(dev);
786 int orig_blksize_size = BLOCK_SIZE;
789 /* Need to change the block size that the block layer uses */
790 if (blksize_size[MAJOR(dev)]) {
791 orig_blksize_size = blksize_size[MAJOR(dev)][MINOR(dev)];
794 if (orig_blksize_size != hardblocksize)
795 set_blocksize(dev, hardblocksize);
797 rc = add_gpt_partitions(hd, bdev, first_part_minor);
800 if (orig_blksize_size != hardblocksize)
801 set_blocksize(dev, orig_blksize_size);