make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / fs / partitions / efi.c
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
7  *
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.
12  *
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.
17  *
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
21  *
22  *
23  * TODO:
24  *
25  * Changelog:
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.
37  *
38  * Mon  Feb 04 2002 Matt Domsch <Matt_Domsch@dell.com>
39  * - Removed __PRIPTR_PREFIX - not being used
40  *
41  * Mon  Jan 14 2002 Matt Domsch <Matt_Domsch@dell.com>
42  * - Ported to 2.5.2-pre11 + library crc32 patch Linus applied
43  *
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.
49  * 
50  * Wed Oct 17 2001 Matt Domsch <Matt_Domsch@dell.com>
51  * - Removed CONFIG_DEVFS_VOLUMES_UUID code entirely per Martin Wilck
52  *
53  * Wed Oct 10 2001 Matt Domsch <Matt_Domsch@dell.com>
54  * - Changed function comments to DocBook style per Andreas Dilger suggestion.
55  *
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()
60  *
61  * Mon Oct 01 2001 Matt Domsch <Matt_Domsch@dell.com>
62  * - Style cleanups
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.
71  *
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. 
75  *
76  * Tue Dec  5 2000 Matt Domsch <Matt_Domsch@dell.com>
77  * - Moved crc32() to linux/lib, added efi_crc32().
78  *
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.
82  *
83  * Wed Oct 25 2000 Matt Domsch <Matt_Domsch@dell.com>
84  * - Fixed the last_lba() call to return the proper last block
85  *
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.
89  *
90  ************************************************************/
91 #include <linux/config.h>
92 #include <linux/fs.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>
104 #include "check.h"
105 #include "efi.h"
106
107 #if CONFIG_BLK_DEV_MD
108 extern void md_autodetect_dev(kdev_t dev);
109 #endif
110
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"
115 # else
116 #  define __PRI64_PREFIX        "ll"
117 # endif
118 # define PRIx64         __PRI64_PREFIX "x"
119
120
121 #undef EFI_DEBUG
122 #ifdef EFI_DEBUG
123 #define Dprintk(x...) printk(KERN_DEBUG x)
124 #else
125 #define Dprintk(x...)
126 #endif
127
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.
131  */
132 static int force_gpt;
133 static int __init
134 force_gpt_fn(char *str)
135 {
136         force_gpt = 1;
137         return 1;
138 }
139 __setup("gpt", force_gpt_fn);
140
141
142 /*
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
146  */
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;
152
153 /**
154  * crc32init_le() - allocate and initialize LE table data
155  *
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].
158  *
159  */
160 static int __init crc32init_le(void)
161 {
162         unsigned i, j;
163         u32 crc = 1;
164
165         crc32table_le =
166             kmalloc((1 << CRC_LE_BITS) * sizeof(u32), GFP_KERNEL);
167         if (!crc32table_le)
168                 return 1;
169         crc32table_le[0] = 0;
170
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];
175         }
176         return 0;
177 }
178
179 /**
180  * crc32cleanup_le(): free LE table data
181  */
182 static void __exit crc32cleanup_le(void)
183 {
184         if (crc32table_le) kfree(crc32table_le);
185         crc32table_le = NULL;
186 }
187
188 __initcall(crc32init_le);
189 __exitcall(crc32cleanup_le);
190
191 /**
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
197  * 
198  */
199 static u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
200 {
201         while (len--) {
202                 crc = (crc >> 8) ^ crc32table_le[(crc ^ *p++) & 255];
203         }
204         return crc;
205 }
206
207
208 /**
209  * efi_crc32() - EFI version of crc32 function
210  * @buf: buffer to calculate crc32 of
211  * @len - length of buf
212  *
213  * Description: Returns EFI-style CRC32 value for @buf
214  * 
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).
219  */
220 static inline u32
221 efi_crc32(const void *buf, unsigned long len)
222 {
223         return (crc32_le(~0L, buf, len) ^ ~0L);
224 }
225
226 /**
227  * is_pmbr_valid(): test Protective MBR for validity
228  * @mbr: pointer to a legacy mbr structure
229  *
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
234  */
235 static int
236 is_pmbr_valid(legacy_mbr *mbr)
237 {
238         int i, found = 0, signature = 0;
239         if (!mbr)
240                 return 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) {
245                         found = 1;
246                         break;
247                 }
248         }
249         return (signature && found);
250 }
251
252 /**
253  * last_lba(): return number of last logical block of device
254  * @hd: gendisk with partition list
255  * @bdev: block device
256  * 
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.
261  */
262 static u64
263 last_lba(struct gendisk *hd, struct block_device *bdev)
264 {
265         if (!hd || !hd->part || !bdev)
266                 return 0;
267         return hd->part[MINOR(to_kdev_t(bdev->bd_dev))].nr_sects - 1;
268 }
269
270 /**
271  * read_lba(): Read bytes from disk, starting at given LBA
272  * @hd
273  * @bdev
274  * @lba
275  * @buffer
276  * @size_t
277  *
278  * Description:  Reads @count bytes from @bdev into @buffer.
279  * Returns number of bytes read on success, 0 on error.
280  */
281 static size_t
282 read_lba(struct gendisk *hd, struct block_device *bdev, u64 lba,
283          u8 * buffer, size_t count)
284 {
285
286         size_t totalreadcount = 0, bytesread = 0;
287         unsigned long blocksize;
288         int i;
289         Sector sect;
290         unsigned char *data = NULL;
291
292         if (!hd || !bdev || !buffer || !count)
293                 return 0;
294
295         blocksize = get_hardsect_size(to_kdev_t(bdev->bd_dev));
296         if (!blocksize)
297                 blocksize = 512;
298
299         for (i = 0; count > 0; i++) {
300                 data = read_dev_sector(bdev, lba, &sect);
301                 if (!data)
302                         return totalreadcount;
303
304                 bytesread =
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);
310
311                 buffer += bytesread;
312                 totalreadcount += bytesread;
313                 count -= bytesread;
314                 lba += (bytesread / blocksize);
315         }
316         return totalreadcount;
317 }
318
319
320 /**
321  * alloc_read_gpt_entries(): reads partition entries from disk
322  * @hd
323  * @bdev
324  * @gpt - GPT header
325  * 
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!
329  */
330 static gpt_entry *
331 alloc_read_gpt_entries(struct gendisk *hd,
332                        struct block_device *bdev, gpt_header *gpt)
333 {
334         size_t count;
335         gpt_entry *pte;
336         if (!hd || !bdev || !gpt)
337                 return NULL;
338
339         count = le32_to_cpu(gpt->num_partition_entries) *
340                 le32_to_cpu(gpt->sizeof_partition_entry);
341         if (!count)
342                 return NULL;
343         pte = kmalloc(count, GFP_KERNEL);
344         if (!pte)
345                 return NULL;
346         memset(pte, 0, count);
347
348         if (read_lba(hd, bdev, le64_to_cpu(gpt->partition_entry_lba),
349                      (u8 *) pte,
350                      count) < count) {
351                 kfree(pte);
352                 pte=NULL;
353                 return NULL;
354         }
355         return pte;
356 }
357
358 /**
359  * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
360  * @hd
361  * @bdev
362  * @lba is the Logical Block Address of the partition table
363  * 
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.
367  */
368 static gpt_header *
369 alloc_read_gpt_header(struct gendisk *hd, struct block_device *bdev, u64 lba)
370 {
371         gpt_header *gpt;
372         if (!hd || !bdev)
373                 return NULL;
374
375         gpt = kmalloc(sizeof (gpt_header), GFP_KERNEL);
376         if (!gpt)
377                 return NULL;
378         memset(gpt, 0, sizeof (gpt_header));
379
380         if (read_lba(hd, bdev, lba, (u8 *) gpt,
381                      sizeof (gpt_header)) < sizeof (gpt_header)) {
382                 kfree(gpt);
383                 gpt=NULL;
384                 return NULL;
385         }
386
387         return gpt;
388 }
389
390 /**
391  * is_gpt_valid() - tests one GPT header and PTEs for validity
392  * @hd
393  * @bdev
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.
397  *
398  * Description: returns 1 if valid,  0 on error.
399  * If valid, returns pointers to newly allocated GPT header and PTEs.
400  */
401 static int
402 is_gpt_valid(struct gendisk *hd, struct block_device *bdev, u64 lba,
403              gpt_header **gpt, gpt_entry **ptes)
404 {
405         u32 crc, origcrc;
406
407         if (!hd || !bdev || !gpt || !ptes)
408                 return 0;
409         if (!(*gpt = alloc_read_gpt_header(hd, bdev, lba)))
410                 return 0;
411
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);
417                 kfree(*gpt);
418                 *gpt = NULL;
419                 return 0;
420         }
421
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));
426
427         if (crc != origcrc) {
428                 Dprintk
429                     ("GUID Partition Table Header CRC is wrong: %x != %x\n",
430                      crc, origcrc);
431                 kfree(*gpt);
432                 *gpt = NULL;
433                 return 0;
434         }
435         (*gpt)->header_crc32 = cpu_to_le32(origcrc);
436
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);
442                 kfree(*gpt);
443                 *gpt = NULL;
444                 return 0;
445         }
446
447         if (!(*ptes = alloc_read_gpt_entries(hd, bdev, *gpt))) {
448                 kfree(*gpt);
449                 *gpt = NULL;
450                 return 0;
451         }
452
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));
457
458         if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
459                 Dprintk("GUID Partitition Entry Array CRC check failed.\n");
460                 kfree(*gpt);
461                 *gpt = NULL;
462                 kfree(*ptes);
463                 *ptes = NULL;
464                 return 0;
465         }
466
467         /* We're done, all's well */
468         return 1;
469 }
470
471 /**
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.
478  * 
479  */
480 static void
481 compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
482 {
483         int error_found = 0;
484         if (!pgpt || !agpt)
485                 return;
486         if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
487                 printk(KERN_WARNING
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));
492                 error_found++;
493         }
494         if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
495                 printk(KERN_WARNING
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));
500                 error_found++;
501         }
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));
508                 error_found++;
509         }
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));
516                 error_found++;
517         }
518         if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
519                 printk(KERN_WARNING "GPT:disk_guids don't match.\n");
520                 error_found++;
521         }
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: "
525                        "0x%x != 0x%x\n",
526                        le32_to_cpu(pgpt->num_partition_entries),
527                        le32_to_cpu(agpt->num_partition_entries));
528                 error_found++;
529         }
530         if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
531             le32_to_cpu(agpt->sizeof_partition_entry)) {
532                 printk(KERN_WARNING
533                        "GPT:sizeof_partition_entry values don't match: "
534                        "0x%x != 0x%x\n",
535                        le32_to_cpu(pgpt->sizeof_partition_entry),
536                        le32_to_cpu(agpt->sizeof_partition_entry));
537                 error_found++;
538         }
539         if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
540             le32_to_cpu(agpt->partition_entry_array_crc32)) {
541                 printk(KERN_WARNING
542                        "GPT:partition_entry_array_crc32 values don't match: "
543                        "0x%x != 0x%x\n",
544                        le32_to_cpu(pgpt->partition_entry_array_crc32),
545                        le32_to_cpu(agpt->partition_entry_array_crc32));
546                 error_found++;
547         }
548         if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
549                 printk(KERN_WARNING
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);
553                 error_found++;
554         }
555
556         if (le64_to_cpu(agpt->my_lba) != lastlba) {
557                 printk(KERN_WARNING
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);
561                 error_found++;
562         }
563
564         if (error_found)
565                 printk(KERN_WARNING
566                        "GPT: Use GNU Parted to correct GPT errors.\n");
567         return;
568 }
569
570 /**
571  * find_valid_gpt() - Search disk for valid GPT headers and PTEs
572  * @hd
573  * @bdev
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.
580  */
581 static int
582 find_valid_gpt(struct gendisk *hd, struct block_device *bdev,
583                gpt_header **gpt, gpt_entry **ptes)
584 {
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;
589         u64 lastlba;
590         if (!hd || !bdev || !gpt || !ptes)
591                 return 0;
592
593         lastlba = last_lba(hd, bdev);
594         good_pgpt = is_gpt_valid(hd, bdev, GPT_PRIMARY_PARTITION_TABLE_LBA,
595                                  &pgpt, &pptes);
596         if (good_pgpt) {
597                 good_agpt = is_gpt_valid(hd, bdev,
598                                          le64_to_cpu(pgpt->alternate_lba),
599                                          &agpt, &aptes);
600                 if (!good_agpt) {
601                         good_agpt = is_gpt_valid(hd, bdev, lastlba,
602                                                  &agpt, &aptes);
603                 }
604         }
605         else {
606                 good_agpt = is_gpt_valid(hd, bdev, lastlba,
607                                          &agpt, &aptes);
608         }
609
610         /* The obviously unsuccessful case */
611         if (!good_pgpt && !good_agpt) {
612                 goto fail;
613         }
614
615         /* This will be added to the EFI Spec. per Intel after v1.02. */
616         legacymbr = kmalloc(sizeof (*legacymbr), GFP_KERNEL);
617         if (legacymbr) {
618                 memset(legacymbr, 0, sizeof (*legacymbr));
619                 read_lba(hd, bdev, 0, (u8 *) legacymbr,
620                          sizeof (*legacymbr));
621                 good_pmbr = is_pmbr_valid(legacymbr);
622                 kfree(legacymbr);
623                 legacymbr=NULL;
624         }
625
626         /* Failure due to bad PMBR */
627         if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {
628                 printk(KERN_WARNING 
629                        "  Warning: Disk has a valid GPT signature "
630                        "but invalid PMBR.\n");
631                 printk(KERN_WARNING
632                        "  Assuming this disk is *not* a GPT disk anymore.\n");
633                 printk(KERN_WARNING
634                        "  Use gpt kernel option to override.  "
635                        "Use GNU Parted to correct disk.\n");
636                 goto fail;
637         }
638
639         /* Would fail due to bad PMBR, but force GPT anyhow */
640         if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {
641                 printk(KERN_WARNING
642                        "  Warning: Disk has a valid GPT signature but "
643                        "invalid PMBR.\n");
644                 printk(KERN_WARNING
645                        "  Use GNU Parted to correct disk.\n");
646                 printk(KERN_WARNING
647                        "  gpt option taken, disk treated as GPT.\n");
648         }
649
650         compare_gpts(pgpt, agpt, lastlba);
651
652         /* The good cases */
653         if (good_pgpt && (good_pmbr || force_gpt)) {
654                 *gpt  = pgpt;
655                 *ptes = pptes;
656                 if (agpt)  { kfree(agpt);   agpt = NULL; }
657                 if (aptes) { kfree(aptes); aptes = NULL; }
658                 if (!good_agpt) {
659                         printk(KERN_WARNING 
660                                "Alternate GPT is invalid, "
661                                "using primary GPT.\n");
662                 }
663                 return 1;
664         }
665         else if (good_agpt && (good_pmbr || force_gpt)) {
666                 *gpt  = agpt;
667                 *ptes = aptes;
668                 if (pgpt)  { kfree(pgpt);   pgpt = NULL; }
669                 if (pptes) { kfree(pptes); pptes = NULL; }
670                 printk(KERN_WARNING 
671                        "Primary GPT is invalid, using alternate GPT.\n");
672                 return 1;
673         }
674
675  fail:
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; }
680         *gpt = NULL;
681         *ptes = NULL;
682         return 0;
683 }
684
685 /**
686  * add_gpt_partitions(struct gendisk *hd, struct block_device *bdev,
687  * @hd
688  * @bdev
689  *
690  * Description: Create devices for each entry in the GUID Partition Table
691  * Entries.
692  *
693  * We do not create a Linux partition for GPT, but
694  * only for the actual data partitions.
695  * Returns:
696  * -1 if unable to read the partition table
697  *  0 if this isn't our partition table
698  *  1 if successful
699  *
700  */
701 static int
702 add_gpt_partitions(struct gendisk *hd, struct block_device *bdev, int nextminor)
703 {
704         gpt_header *gpt = NULL;
705         gpt_entry *ptes = NULL;
706         u32 i;
707         int max_p; 
708
709         if (!hd || !bdev)
710                 return -1;
711
712         if (!find_valid_gpt(hd, bdev, &gpt, &ptes) || !gpt || !ptes) {
713                 if (gpt) {
714                         kfree(gpt);
715                         gpt = NULL;
716                 }
717                 if (ptes) {
718                         kfree(ptes);
719                         ptes = NULL;
720                 }
721                 return 0;
722         }
723
724         Dprintk("GUID Partition Table is valid!  Yea!\n");
725
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))
729                         continue;
730
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) +
735                                   1));
736
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)),
743                                            nextminor+i));
744                 }
745 #endif
746         }
747         kfree(ptes);
748         ptes=NULL;
749         kfree(gpt);
750         gpt=NULL;
751         printk("\n");
752         return 1;
753 }
754
755 /**
756  * efi_partition(): EFI GPT partition handling entry function
757  * @hd
758  * @bdev
759  * @first_sector: unused
760  * @first_part_minor: minor number assigned to first GPT partition found
761  *
762  * Description: called from check.c, if the disk contains GPT
763  * partitions, sets up partition entries in the kernel.
764  *
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.
768  *
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.
773  *
774  * Returns:
775  * -1 if unable to read the partition table
776  *  0 if this isn't our partitoin table
777  *  1 if successful
778  */
779 int
780 efi_partition(struct gendisk *hd, struct block_device *bdev,
781               unsigned long first_sector, int first_part_minor)
782 {
783
784         kdev_t dev = to_kdev_t(bdev->bd_dev);
785         int hardblocksize = get_hardsect_size(dev);
786         int orig_blksize_size = BLOCK_SIZE;
787         int rc = 0;
788
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)];
792         }
793
794         if (orig_blksize_size != hardblocksize)
795                 set_blocksize(dev, hardblocksize);
796
797         rc = add_gpt_partitions(hd, bdev, first_part_minor);
798
799         /* change back */
800         if (orig_blksize_size != hardblocksize)
801                 set_blocksize(dev, orig_blksize_size);
802
803         return rc;
804 }