MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / partitions / efi.c
blob4cb29608753d78678d995376ff5649a50ef94122
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
23 * TODO:
25 * Changelog:
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>
60 * - Style cleanups
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>
91 #include "check.h"
92 #include "efi.h"
94 #undef EFI_DEBUG
95 #ifdef EFI_DEBUG
96 #define Dprintk(x...) printk(KERN_DEBUG x)
97 #else
98 #define Dprintk(x...)
99 #endif
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;
106 static int __init
107 force_gpt_fn(char *str)
109 force_gpt = 1;
110 return 1;
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).
127 static inline u32
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
142 static int
143 is_pmbr_valid(legacy_mbr *mbr)
145 int i, found = 0, signature = 0;
146 if (!mbr)
147 return 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) {
152 found = 1;
153 break;
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.
168 static u64
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
176 * @bdev
177 * @lba
178 * @buffer
179 * @size_t
181 * Description: Reads @count bytes from @bdev into @buffer.
182 * Returns number of bytes read on success, 0 on error.
184 static size_t
185 read_lba(struct block_device *bdev, u64 lba, u8 * buffer, size_t count)
187 size_t totalreadcount = 0;
189 if (!bdev || !buffer)
190 return 0;
192 while (count) {
193 int copied = 512;
194 Sector sect;
195 unsigned char *data = read_dev_sector(bdev, lba++, &sect);
196 if (!data)
197 break;
198 if (copied > count)
199 copied = count;
200 memcpy(buffer, data, copied);
201 put_dev_sector(sect);
202 buffer += copied;
203 totalreadcount +=copied;
204 count -= copied;
206 return totalreadcount;
211 * alloc_read_gpt_entries(): reads partition entries from disk
212 * @bdev
213 * @gpt - GPT header
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!
219 static gpt_entry *
220 alloc_read_gpt_entries(struct block_device *bdev, gpt_header *gpt)
222 size_t count;
223 gpt_entry *pte;
224 if (!bdev || !gpt)
225 return NULL;
227 count = le32_to_cpu(gpt->num_partition_entries) *
228 le32_to_cpu(gpt->sizeof_partition_entry);
229 if (!count)
230 return NULL;
231 pte = kmalloc(count, GFP_KERNEL);
232 if (!pte)
233 return NULL;
234 memset(pte, 0, count);
236 if (read_lba(bdev, le64_to_cpu(gpt->partition_entry_lba),
237 (u8 *) pte,
238 count) < count) {
239 kfree(pte);
240 pte=NULL;
241 return NULL;
243 return pte;
247 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
248 * @bdev
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.
255 static gpt_header *
256 alloc_read_gpt_header(struct block_device *bdev, u64 lba)
258 gpt_header *gpt;
259 if (!bdev)
260 return NULL;
262 gpt = kmalloc(sizeof (gpt_header), GFP_KERNEL);
263 if (!gpt)
264 return NULL;
265 memset(gpt, 0, sizeof (gpt_header));
267 if (read_lba(bdev, lba, (u8 *) gpt,
268 sizeof (gpt_header)) < sizeof (gpt_header)) {
269 kfree(gpt);
270 gpt=NULL;
271 return NULL;
274 return gpt;
278 * is_gpt_valid() - tests one GPT header and PTEs for validity
279 * @bdev
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.
287 static int
288 is_gpt_valid(struct block_device *bdev, u64 lba,
289 gpt_header **gpt, gpt_entry **ptes)
291 u32 crc, origcrc;
293 if (!bdev || !gpt || !ptes)
294 return 0;
295 if (!(*gpt = alloc_read_gpt_header(bdev, lba)))
296 return 0;
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:"
301 "%lld != %lld\n",
302 (unsigned long long)le64_to_cpu((*gpt)->signature),
303 (unsigned long long)GPT_HEADER_SIGNATURE);
304 kfree(*gpt);
305 *gpt = NULL;
306 return 0;
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) {
315 Dprintk
316 ("GUID Partition Table Header CRC is wrong: %x != %x\n",
317 crc, origcrc);
318 kfree(*gpt);
319 *gpt = NULL;
320 return 0;
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);
330 kfree(*gpt);
331 *gpt = NULL;
332 return 0;
335 if (!(*ptes = alloc_read_gpt_entries(bdev, *gpt))) {
336 kfree(*gpt);
337 *gpt = NULL;
338 return 0;
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");
348 kfree(*gpt);
349 *gpt = NULL;
350 kfree(*ptes);
351 *ptes = NULL;
352 return 0;
355 /* We're done, all's well */
356 return 1;
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.
368 static void
369 compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
371 int error_found = 0;
372 if (!pgpt || !agpt)
373 return;
374 if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
375 printk(KERN_WARNING
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));
380 error_found++;
382 if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
383 printk(KERN_WARNING
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));
388 error_found++;
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));
396 error_found++;
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));
404 error_found++;
406 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
407 printk(KERN_WARNING "GPT:disk_guids don't match.\n");
408 error_found++;
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: "
413 "0x%x != 0x%x\n",
414 le32_to_cpu(pgpt->num_partition_entries),
415 le32_to_cpu(agpt->num_partition_entries));
416 error_found++;
418 if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
419 le32_to_cpu(agpt->sizeof_partition_entry)) {
420 printk(KERN_WARNING
421 "GPT:sizeof_partition_entry values don't match: "
422 "0x%x != 0x%x\n",
423 le32_to_cpu(pgpt->sizeof_partition_entry),
424 le32_to_cpu(agpt->sizeof_partition_entry));
425 error_found++;
427 if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
428 le32_to_cpu(agpt->partition_entry_array_crc32)) {
429 printk(KERN_WARNING
430 "GPT:partition_entry_array_crc32 values don't match: "
431 "0x%x != 0x%x\n",
432 le32_to_cpu(pgpt->partition_entry_array_crc32),
433 le32_to_cpu(agpt->partition_entry_array_crc32));
434 error_found++;
436 if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
437 printk(KERN_WARNING
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);
442 error_found++;
445 if (le64_to_cpu(agpt->my_lba) != lastlba) {
446 printk(KERN_WARNING
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);
451 error_found++;
454 if (error_found)
455 printk(KERN_WARNING
456 "GPT: Use GNU Parted to correct GPT errors.\n");
457 return;
461 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
462 * @bdev
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.
470 static int
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;
477 u64 lastlba;
478 if (!bdev || !gpt || !ptes)
479 return 0;
481 lastlba = last_lba(bdev);
482 good_pgpt = is_gpt_valid(bdev, GPT_PRIMARY_PARTITION_TABLE_LBA,
483 &pgpt, &pptes);
484 if (good_pgpt) {
485 good_agpt = is_gpt_valid(bdev,
486 le64_to_cpu(pgpt->alternate_lba),
487 &agpt, &aptes);
488 if (!good_agpt) {
489 good_agpt = is_gpt_valid(bdev, lastlba,
490 &agpt, &aptes);
493 else {
494 good_agpt = is_gpt_valid(bdev, lastlba,
495 &agpt, &aptes);
498 /* The obviously unsuccessful case */
499 if (!good_pgpt && !good_agpt) {
500 goto fail;
503 /* This will be added to the EFI Spec. per Intel after v1.02. */
504 legacymbr = kmalloc(sizeof (*legacymbr), GFP_KERNEL);
505 if (legacymbr) {
506 memset(legacymbr, 0, sizeof (*legacymbr));
507 read_lba(bdev, 0, (u8 *) legacymbr,
508 sizeof (*legacymbr));
509 good_pmbr = is_pmbr_valid(legacymbr);
510 kfree(legacymbr);
511 legacymbr=NULL;
514 /* Failure due to bad PMBR */
515 if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {
516 printk(KERN_WARNING
517 " Warning: Disk has a valid GPT signature "
518 "but invalid PMBR.\n");
519 printk(KERN_WARNING
520 " Assuming this disk is *not* a GPT disk anymore.\n");
521 printk(KERN_WARNING
522 " Use gpt kernel option to override. "
523 "Use GNU Parted to correct disk.\n");
524 goto fail;
527 /* Would fail due to bad PMBR, but force GPT anyhow */
528 if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {
529 printk(KERN_WARNING
530 " Warning: Disk has a valid GPT signature but "
531 "invalid PMBR.\n");
532 printk(KERN_WARNING
533 " Use GNU Parted to correct disk.\n");
534 printk(KERN_WARNING
535 " gpt option taken, disk treated as GPT.\n");
538 compare_gpts(pgpt, agpt, lastlba);
540 /* The good cases */
541 if (good_pgpt && (good_pmbr || force_gpt)) {
542 *gpt = pgpt;
543 *ptes = pptes;
544 if (agpt) { kfree(agpt); agpt = NULL; }
545 if (aptes) { kfree(aptes); aptes = NULL; }
546 if (!good_agpt) {
547 printk(KERN_WARNING
548 "Alternate GPT is invalid, "
549 "using primary GPT.\n");
551 return 1;
553 else if (good_agpt && (good_pmbr || force_gpt)) {
554 *gpt = agpt;
555 *ptes = aptes;
556 if (pgpt) { kfree(pgpt); pgpt = NULL; }
557 if (pptes) { kfree(pptes); pptes = NULL; }
558 printk(KERN_WARNING
559 "Primary GPT is invalid, using alternate GPT.\n");
560 return 1;
563 fail:
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; }
568 *gpt = NULL;
569 *ptes = NULL;
570 return 0;
574 * efi_partition(struct parsed_partitions *state, struct block_device *bdev)
575 * @state
576 * @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.
587 * Returns:
588 * -1 if unable to read the partition table
589 * 0 if this isn't our partition table
590 * 1 if successful
594 efi_partition(struct parsed_partitions *state, struct block_device *bdev)
596 gpt_header *gpt = NULL;
597 gpt_entry *ptes = NULL;
598 u32 i;
600 if (!find_valid_gpt(bdev, &gpt, &ptes) || !gpt || !ptes) {
601 kfree(gpt);
602 kfree(ptes);
603 return 0;
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))
610 continue;
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) +
615 1));
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;
622 kfree(ptes);
623 kfree(gpt);
624 printk("\n");
625 return 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
632 * of the file.
633 * ---------------------------------------------------------------------------
634 * Local variables:
635 * c-indent-level: 4
636 * c-brace-imaginary-offset: 0
637 * c-brace-offset: -4
638 * c-argdecl-indent: 4
639 * c-label-offset: -4
640 * c-continued-statement-offset: 4
641 * c-continued-brace-offset: 0
642 * indent-tabs-mode: nil
643 * tab-width: 8
644 * End: