partitions/efi: use lba-aware partition records
[deliverable/linux.git] / block / partitions / efi.c
CommitLineData
1da177e4
LT
1/************************************************************
2 * EFI GUID Partition Table handling
7d13af32
KZ
3 *
4 * http://www.uefi.org/specs/
5 * http://www.intel.com/technology/efi/
6 *
1da177e4
LT
7 * efi.[ch] by Matt Domsch <Matt_Domsch@dell.com>
8 * Copyright 2000,2001,2002,2004 Dell Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * TODO:
26 *
27 * Changelog:
28 * Mon Nov 09 2004 Matt Domsch <Matt_Domsch@dell.com>
29 * - test for valid PMBR and valid PGPT before ever reading
30 * AGPT, allow override with 'gpt' kernel command line option.
31 * - check for first/last_usable_lba outside of size of disk
32 *
33 * Tue Mar 26 2002 Matt Domsch <Matt_Domsch@dell.com>
34 * - Ported to 2.5.7-pre1 and 2.5.7-dj2
35 * - Applied patch to avoid fault in alternate header handling
36 * - cleaned up find_valid_gpt
37 * - On-disk structure and copy in memory is *always* LE now -
38 * swab fields as needed
39 * - remove print_gpt_header()
40 * - only use first max_p partition entries, to keep the kernel minor number
41 * and partition numbers tied.
42 *
43 * Mon Feb 04 2002 Matt Domsch <Matt_Domsch@dell.com>
44 * - Removed __PRIPTR_PREFIX - not being used
45 *
46 * Mon Jan 14 2002 Matt Domsch <Matt_Domsch@dell.com>
47 * - Ported to 2.5.2-pre11 + library crc32 patch Linus applied
48 *
49 * Thu Dec 6 2001 Matt Domsch <Matt_Domsch@dell.com>
50 * - Added compare_gpts().
51 * - moved le_efi_guid_to_cpus() back into this file. GPT is the only
52 * thing that keeps EFI GUIDs on disk.
53 * - Changed gpt structure names and members to be simpler and more Linux-like.
54 *
55 * Wed Oct 17 2001 Matt Domsch <Matt_Domsch@dell.com>
56 * - Removed CONFIG_DEVFS_VOLUMES_UUID code entirely per Martin Wilck
57 *
58 * Wed Oct 10 2001 Matt Domsch <Matt_Domsch@dell.com>
59 * - Changed function comments to DocBook style per Andreas Dilger suggestion.
60 *
61 * Mon Oct 08 2001 Matt Domsch <Matt_Domsch@dell.com>
62 * - Change read_lba() to use the page cache per Al Viro's work.
63 * - print u64s properly on all architectures
64 * - fixed debug_printk(), now Dprintk()
65 *
66 * Mon Oct 01 2001 Matt Domsch <Matt_Domsch@dell.com>
67 * - Style cleanups
68 * - made most functions static
69 * - Endianness addition
70 * - remove test for second alternate header, as it's not per spec,
71 * and is unnecessary. There's now a method to read/write the last
72 * sector of an odd-sized disk from user space. No tools have ever
73 * been released which used this code, so it's effectively dead.
74 * - Per Asit Mallick of Intel, added a test for a valid PMBR.
75 * - Added kernel command line option 'gpt' to override valid PMBR test.
76 *
77 * Wed Jun 6 2001 Martin Wilck <Martin.Wilck@Fujitsu-Siemens.com>
78 * - added devfs volume UUID support (/dev/volumes/uuids) for
79 * mounting file systems by the partition GUID.
80 *
81 * Tue Dec 5 2000 Matt Domsch <Matt_Domsch@dell.com>
82 * - Moved crc32() to linux/lib, added efi_crc32().
83 *
84 * Thu Nov 30 2000 Matt Domsch <Matt_Domsch@dell.com>
85 * - Replaced Intel's CRC32 function with an equivalent
86 * non-license-restricted version.
87 *
88 * Wed Oct 25 2000 Matt Domsch <Matt_Domsch@dell.com>
89 * - Fixed the last_lba() call to return the proper last block
90 *
91 * Thu Oct 12 2000 Matt Domsch <Matt_Domsch@dell.com>
92 * - Thanks to Andries Brouwer for his debugging assistance.
93 * - Code works, detects all the partitions.
94 *
95 ************************************************************/
1da177e4 96#include <linux/crc32.h>
eec7ecfe 97#include <linux/ctype.h>
7d13af32 98#include <linux/math64.h>
5a0e3ad6 99#include <linux/slab.h>
1da177e4
LT
100#include "check.h"
101#include "efi.h"
102
1da177e4
LT
103/* This allows a kernel command line option 'gpt' to override
104 * the test for invalid PMBR. Not __initdata because reloading
105 * the partition tables happens after init too.
106 */
107static int force_gpt;
108static int __init
109force_gpt_fn(char *str)
110{
111 force_gpt = 1;
112 return 1;
113}
114__setup("gpt", force_gpt_fn);
115
116
117/**
118 * efi_crc32() - EFI version of crc32 function
119 * @buf: buffer to calculate crc32 of
120 * @len - length of buf
121 *
122 * Description: Returns EFI-style CRC32 value for @buf
123 *
124 * This function uses the little endian Ethernet polynomial
125 * but seeds the function with ~0, and xor's with ~0 at the end.
126 * Note, the EFI Specification, v1.02, has a reference to
127 * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
128 */
129static inline u32
130efi_crc32(const void *buf, unsigned long len)
131{
132 return (crc32(~0L, buf, len) ^ ~0L);
133}
134
135/**
136 * last_lba(): return number of last logical block of device
137 * @bdev: block device
138 *
139 * Description: Returns last LBA value on success, 0 on error.
140 * This is stored (by sd and ide-geometry) in
141 * the part[0] entry for this disk, and is the number of
142 * physical sectors available on the disk.
143 */
1493bf21 144static u64 last_lba(struct block_device *bdev)
1da177e4
LT
145{
146 if (!bdev || !bdev->bd_inode)
147 return 0;
7d13af32
KZ
148 return div_u64(bdev->bd_inode->i_size,
149 bdev_logical_block_size(bdev)) - 1ULL;
1da177e4
LT
150}
151
c2ebdc24 152static inline int pmbr_part_valid(gpt_mbr_record *part)
1da177e4 153{
c2ebdc24
DB
154 if (part->os_type == EFI_PMBR_OSTYPE_EFI_GPT &&
155 le32_to_cpu(part->start_sector) == 1UL)
156 return 1;
1da177e4
LT
157 return 0;
158}
159
160/**
161 * is_pmbr_valid(): test Protective MBR for validity
162 * @mbr: pointer to a legacy mbr structure
1da177e4
LT
163 *
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
168 */
169static int
4c64c30a 170is_pmbr_valid(legacy_mbr *mbr)
1da177e4
LT
171{
172 int i;
173 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
174 return 0;
175 for (i = 0; i < 4; i++)
4c64c30a 176 if (pmbr_part_valid(&mbr->partition_record[i]))
1da177e4
LT
177 return 1;
178 return 0;
179}
180
181/**
182 * read_lba(): Read bytes from disk, starting at given LBA
1493bf21 183 * @state
1da177e4
LT
184 * @lba
185 * @buffer
186 * @size_t
187 *
1493bf21 188 * Description: Reads @count bytes from @state->bdev into @buffer.
1da177e4
LT
189 * Returns number of bytes read on success, 0 on error.
190 */
1493bf21
TH
191static size_t read_lba(struct parsed_partitions *state,
192 u64 lba, u8 *buffer, size_t count)
1da177e4
LT
193{
194 size_t totalreadcount = 0;
1493bf21 195 struct block_device *bdev = state->bdev;
7d13af32 196 sector_t n = lba * (bdev_logical_block_size(bdev) / 512);
1da177e4 197
1493bf21 198 if (!buffer || lba > last_lba(bdev))
1da177e4
LT
199 return 0;
200
201 while (count) {
202 int copied = 512;
203 Sector sect;
1493bf21 204 unsigned char *data = read_part_sector(state, n++, &sect);
1da177e4
LT
205 if (!data)
206 break;
207 if (copied > count)
208 copied = count;
209 memcpy(buffer, data, copied);
210 put_dev_sector(sect);
211 buffer += copied;
212 totalreadcount +=copied;
213 count -= copied;
214 }
215 return totalreadcount;
216}
217
218/**
219 * alloc_read_gpt_entries(): reads partition entries from disk
1493bf21 220 * @state
1da177e4
LT
221 * @gpt - GPT header
222 *
223 * Description: Returns ptes on success, NULL on error.
224 * Allocates space for PTEs based on information found in @gpt.
225 * Notes: remember to free pte when you're done!
226 */
1493bf21
TH
227static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
228 gpt_header *gpt)
1da177e4
LT
229{
230 size_t count;
231 gpt_entry *pte;
1493bf21
TH
232
233 if (!gpt)
1da177e4
LT
234 return NULL;
235
236 count = le32_to_cpu(gpt->num_partition_entries) *
237 le32_to_cpu(gpt->sizeof_partition_entry);
238 if (!count)
239 return NULL;
ea56505b 240 pte = kmalloc(count, GFP_KERNEL);
1da177e4
LT
241 if (!pte)
242 return NULL;
1da177e4 243
1493bf21 244 if (read_lba(state, le64_to_cpu(gpt->partition_entry_lba),
1da177e4
LT
245 (u8 *) pte,
246 count) < count) {
247 kfree(pte);
248 pte=NULL;
249 return NULL;
250 }
251 return pte;
252}
253
254/**
255 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
1493bf21 256 * @state
1da177e4
LT
257 * @lba is the Logical Block Address of the partition table
258 *
259 * Description: returns GPT header on success, NULL on error. Allocates
1493bf21 260 * and fills a GPT header starting at @ from @state->bdev.
1da177e4
LT
261 * Note: remember to free gpt when finished with it.
262 */
1493bf21
TH
263static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
264 u64 lba)
1da177e4
LT
265{
266 gpt_header *gpt;
1493bf21 267 unsigned ssz = bdev_logical_block_size(state->bdev);
1da177e4 268
ea56505b 269 gpt = kmalloc(ssz, GFP_KERNEL);
1da177e4
LT
270 if (!gpt)
271 return NULL;
1da177e4 272
1493bf21 273 if (read_lba(state, lba, (u8 *) gpt, ssz) < ssz) {
1da177e4
LT
274 kfree(gpt);
275 gpt=NULL;
276 return NULL;
277 }
278
279 return gpt;
280}
281
282/**
283 * is_gpt_valid() - tests one GPT header and PTEs for validity
1493bf21 284 * @state
1da177e4
LT
285 * @lba is the logical block address of the GPT header to test
286 * @gpt is a GPT header ptr, filled on return.
287 * @ptes is a PTEs ptr, filled on return.
288 *
289 * Description: returns 1 if valid, 0 on error.
290 * If valid, returns pointers to newly allocated GPT header and PTEs.
291 */
1493bf21
TH
292static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
293 gpt_header **gpt, gpt_entry **ptes)
1da177e4
LT
294{
295 u32 crc, origcrc;
296 u64 lastlba;
297
1493bf21 298 if (!ptes)
1da177e4 299 return 0;
1493bf21 300 if (!(*gpt = alloc_read_gpt_header(state, lba)))
1da177e4
LT
301 return 0;
302
303 /* Check the GUID Partition Table signature */
304 if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
d9916962
TG
305 pr_debug("GUID Partition Table Header signature is wrong:"
306 "%lld != %lld\n",
307 (unsigned long long)le64_to_cpu((*gpt)->signature),
308 (unsigned long long)GPT_HEADER_SIGNATURE);
1da177e4
LT
309 goto fail;
310 }
311
8b8a6e18 312 /* Check the GUID Partition Table header size is too big */
3eb8e74e
TW
313 if (le32_to_cpu((*gpt)->header_size) >
314 bdev_logical_block_size(state->bdev)) {
8b8a6e18 315 pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
3eb8e74e
TW
316 le32_to_cpu((*gpt)->header_size),
317 bdev_logical_block_size(state->bdev));
318 goto fail;
319 }
320
8b8a6e18
PJ
321 /* Check the GUID Partition Table header size is too small */
322 if (le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header)) {
323 pr_debug("GUID Partition Table Header size is too small: %u < %zu\n",
324 le32_to_cpu((*gpt)->header_size),
325 sizeof(gpt_header));
326 goto fail;
327 }
328
1da177e4
LT
329 /* Check the GUID Partition Table CRC */
330 origcrc = le32_to_cpu((*gpt)->header_crc32);
331 (*gpt)->header_crc32 = 0;
332 crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
333
334 if (crc != origcrc) {
d9916962
TG
335 pr_debug("GUID Partition Table Header CRC is wrong: %x != %x\n",
336 crc, origcrc);
1da177e4
LT
337 goto fail;
338 }
339 (*gpt)->header_crc32 = cpu_to_le32(origcrc);
340
341 /* Check that the my_lba entry points to the LBA that contains
342 * the GUID Partition Table */
343 if (le64_to_cpu((*gpt)->my_lba) != lba) {
d9916962
TG
344 pr_debug("GPT my_lba incorrect: %lld != %lld\n",
345 (unsigned long long)le64_to_cpu((*gpt)->my_lba),
346 (unsigned long long)lba);
1da177e4
LT
347 goto fail;
348 }
349
350 /* Check the first_usable_lba and last_usable_lba are
351 * within the disk.
352 */
1493bf21 353 lastlba = last_lba(state->bdev);
1da177e4 354 if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
d9916962
TG
355 pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
356 (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
357 (unsigned long long)lastlba);
1da177e4
LT
358 goto fail;
359 }
360 if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
d9916962
TG
361 pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
362 (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
363 (unsigned long long)lastlba);
1da177e4
LT
364 goto fail;
365 }
366
fa039d5f
TW
367 /* Check that sizeof_partition_entry has the correct value */
368 if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
369 pr_debug("GUID Partitition Entry Size check failed.\n");
370 goto fail;
371 }
372
1493bf21 373 if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
1da177e4
LT
374 goto fail;
375
376 /* Check the GUID Partition Entry Array CRC */
377 crc = efi_crc32((const unsigned char *) (*ptes),
378 le32_to_cpu((*gpt)->num_partition_entries) *
379 le32_to_cpu((*gpt)->sizeof_partition_entry));
380
381 if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
d9916962 382 pr_debug("GUID Partitition Entry Array CRC check failed.\n");
1da177e4
LT
383 goto fail_ptes;
384 }
385
386 /* We're done, all's well */
387 return 1;
388
389 fail_ptes:
390 kfree(*ptes);
391 *ptes = NULL;
392 fail:
393 kfree(*gpt);
394 *gpt = NULL;
395 return 0;
396}
397
398/**
399 * is_pte_valid() - tests one PTE for validity
400 * @pte is the pte to check
401 * @lastlba is last lba of the disk
402 *
403 * Description: returns 1 if valid, 0 on error.
404 */
405static inline int
406is_pte_valid(const gpt_entry *pte, const u64 lastlba)
407{
408 if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
409 le64_to_cpu(pte->starting_lba) > lastlba ||
410 le64_to_cpu(pte->ending_lba) > lastlba)
411 return 0;
412 return 1;
413}
414
415/**
416 * compare_gpts() - Search disk for valid GPT headers and PTEs
417 * @pgpt is the primary GPT header
418 * @agpt is the alternate GPT header
419 * @lastlba is the last LBA number
420 * Description: Returns nothing. Sanity checks pgpt and agpt fields
421 * and prints warnings on discrepancies.
422 *
423 */
424static void
425compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
426{
427 int error_found = 0;
428 if (!pgpt || !agpt)
429 return;
430 if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
431 printk(KERN_WARNING
432 "GPT:Primary header LBA != Alt. header alternate_lba\n");
433 printk(KERN_WARNING "GPT:%lld != %lld\n",
434 (unsigned long long)le64_to_cpu(pgpt->my_lba),
435 (unsigned long long)le64_to_cpu(agpt->alternate_lba));
436 error_found++;
437 }
438 if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
439 printk(KERN_WARNING
440 "GPT:Primary header alternate_lba != Alt. header my_lba\n");
441 printk(KERN_WARNING "GPT:%lld != %lld\n",
442 (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
443 (unsigned long long)le64_to_cpu(agpt->my_lba));
444 error_found++;
445 }
446 if (le64_to_cpu(pgpt->first_usable_lba) !=
447 le64_to_cpu(agpt->first_usable_lba)) {
448 printk(KERN_WARNING "GPT:first_usable_lbas don't match.\n");
449 printk(KERN_WARNING "GPT:%lld != %lld\n",
450 (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
451 (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
452 error_found++;
453 }
454 if (le64_to_cpu(pgpt->last_usable_lba) !=
455 le64_to_cpu(agpt->last_usable_lba)) {
456 printk(KERN_WARNING "GPT:last_usable_lbas don't match.\n");
457 printk(KERN_WARNING "GPT:%lld != %lld\n",
458 (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
459 (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
460 error_found++;
461 }
462 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
463 printk(KERN_WARNING "GPT:disk_guids don't match.\n");
464 error_found++;
465 }
466 if (le32_to_cpu(pgpt->num_partition_entries) !=
467 le32_to_cpu(agpt->num_partition_entries)) {
468 printk(KERN_WARNING "GPT:num_partition_entries don't match: "
469 "0x%x != 0x%x\n",
470 le32_to_cpu(pgpt->num_partition_entries),
471 le32_to_cpu(agpt->num_partition_entries));
472 error_found++;
473 }
474 if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
475 le32_to_cpu(agpt->sizeof_partition_entry)) {
476 printk(KERN_WARNING
477 "GPT:sizeof_partition_entry values don't match: "
478 "0x%x != 0x%x\n",
479 le32_to_cpu(pgpt->sizeof_partition_entry),
480 le32_to_cpu(agpt->sizeof_partition_entry));
481 error_found++;
482 }
483 if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
484 le32_to_cpu(agpt->partition_entry_array_crc32)) {
485 printk(KERN_WARNING
486 "GPT:partition_entry_array_crc32 values don't match: "
487 "0x%x != 0x%x\n",
488 le32_to_cpu(pgpt->partition_entry_array_crc32),
489 le32_to_cpu(agpt->partition_entry_array_crc32));
490 error_found++;
491 }
492 if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
493 printk(KERN_WARNING
494 "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
495 printk(KERN_WARNING "GPT:%lld != %lld\n",
496 (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
497 (unsigned long long)lastlba);
498 error_found++;
499 }
500
501 if (le64_to_cpu(agpt->my_lba) != lastlba) {
502 printk(KERN_WARNING
503 "GPT:Alternate GPT header not at the end of the disk.\n");
504 printk(KERN_WARNING "GPT:%lld != %lld\n",
505 (unsigned long long)le64_to_cpu(agpt->my_lba),
506 (unsigned long long)lastlba);
507 error_found++;
508 }
509
510 if (error_found)
511 printk(KERN_WARNING
512 "GPT: Use GNU Parted to correct GPT errors.\n");
513 return;
514}
515
516/**
517 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
1493bf21 518 * @state
1da177e4
LT
519 * @gpt is a GPT header ptr, filled on return.
520 * @ptes is a PTEs ptr, filled on return.
521 * Description: Returns 1 if valid, 0 on error.
522 * If valid, returns pointers to newly allocated GPT header and PTEs.
523 * Validity depends on PMBR being valid (or being overridden by the
524 * 'gpt' kernel command line option) and finding either the Primary
525 * GPT header and PTEs valid, or the Alternate GPT header and PTEs
526 * valid. If the Primary GPT header is not valid, the Alternate GPT header
527 * is not checked unless the 'gpt' kernel command line option is passed.
528 * This protects against devices which misreport their size, and forces
529 * the user to decide to use the Alternate GPT.
530 */
1493bf21
TH
531static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
532 gpt_entry **ptes)
1da177e4
LT
533{
534 int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
535 gpt_header *pgpt = NULL, *agpt = NULL;
536 gpt_entry *pptes = NULL, *aptes = NULL;
4c64c30a 537 legacy_mbr *legacymbr;
1da177e4 538 u64 lastlba;
1493bf21
TH
539
540 if (!ptes)
1da177e4
LT
541 return 0;
542
1493bf21 543 lastlba = last_lba(state->bdev);
1da177e4
LT
544 if (!force_gpt) {
545 /* This will be added to the EFI Spec. per Intel after v1.02. */
f8314dc6 546 legacymbr = kzalloc(sizeof (*legacymbr), GFP_KERNEL);
1da177e4 547 if (legacymbr) {
1493bf21
TH
548 read_lba(state, 0, (u8 *) legacymbr,
549 sizeof (*legacymbr));
4c64c30a 550 good_pmbr = is_pmbr_valid(legacymbr);
1da177e4 551 kfree(legacymbr);
1da177e4
LT
552 }
553 if (!good_pmbr)
554 goto fail;
555 }
556
1493bf21 557 good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
1da177e4
LT
558 &pgpt, &pptes);
559 if (good_pgpt)
1493bf21 560 good_agpt = is_gpt_valid(state,
1da177e4
LT
561 le64_to_cpu(pgpt->alternate_lba),
562 &agpt, &aptes);
563 if (!good_agpt && force_gpt)
1493bf21 564 good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
1da177e4
LT
565
566 /* The obviously unsuccessful case */
567 if (!good_pgpt && !good_agpt)
568 goto fail;
569
570 compare_gpts(pgpt, agpt, lastlba);
571
572 /* The good cases */
573 if (good_pgpt) {
574 *gpt = pgpt;
575 *ptes = pptes;
576 kfree(agpt);
577 kfree(aptes);
578 if (!good_agpt) {
579 printk(KERN_WARNING
580 "Alternate GPT is invalid, "
581 "using primary GPT.\n");
582 }
583 return 1;
584 }
585 else if (good_agpt) {
586 *gpt = agpt;
587 *ptes = aptes;
588 kfree(pgpt);
589 kfree(pptes);
590 printk(KERN_WARNING
591 "Primary GPT is invalid, using alternate GPT.\n");
592 return 1;
593 }
594
595 fail:
596 kfree(pgpt);
597 kfree(agpt);
598 kfree(pptes);
599 kfree(aptes);
600 *gpt = NULL;
601 *ptes = NULL;
602 return 0;
603}
604
605/**
1493bf21 606 * efi_partition(struct parsed_partitions *state)
1da177e4 607 * @state
1da177e4
LT
608 *
609 * Description: called from check.c, if the disk contains GPT
610 * partitions, sets up partition entries in the kernel.
611 *
612 * If the first block on the disk is a legacy MBR,
613 * it will get handled by msdos_partition().
614 * If it's a Protective MBR, we'll handle it here.
615 *
616 * We do not create a Linux partition for GPT, but
617 * only for the actual data partitions.
618 * Returns:
619 * -1 if unable to read the partition table
620 * 0 if this isn't our partition table
621 * 1 if successful
622 *
623 */
1493bf21 624int efi_partition(struct parsed_partitions *state)
1da177e4
LT
625{
626 gpt_header *gpt = NULL;
627 gpt_entry *ptes = NULL;
628 u32 i;
1493bf21 629 unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
1da177e4 630
1493bf21 631 if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
1da177e4
LT
632 kfree(gpt);
633 kfree(ptes);
634 return 0;
635 }
636
d9916962 637 pr_debug("GUID Partition Table is valid! Yea!\n");
1da177e4
LT
638
639 for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
eec7ecfe
WD
640 struct partition_meta_info *info;
641 unsigned label_count = 0;
642 unsigned label_max;
7d13af32
KZ
643 u64 start = le64_to_cpu(ptes[i].starting_lba);
644 u64 size = le64_to_cpu(ptes[i].ending_lba) -
645 le64_to_cpu(ptes[i].starting_lba) + 1ULL;
646
1493bf21 647 if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
1da177e4
LT
648 continue;
649
7d13af32 650 put_partition(state, i+1, start * ssz, size * ssz);
1da177e4
LT
651
652 /* If this is a RAID volume, tell md */
653 if (!efi_guidcmp(ptes[i].partition_type_guid,
654 PARTITION_LINUX_RAID_GUID))
cc910624 655 state->parts[i + 1].flags = ADDPART_FLAG_RAID;
eec7ecfe
WD
656
657 info = &state->parts[i + 1].info;
1ad7e899 658 efi_guid_unparse(&ptes[i].unique_partition_guid, info->uuid);
eec7ecfe
WD
659
660 /* Naively convert UTF16-LE to 7 bits. */
661 label_max = min(sizeof(info->volname) - 1,
662 sizeof(ptes[i].partition_name));
663 info->volname[label_max] = 0;
664 while (label_count < label_max) {
665 u8 c = ptes[i].partition_name[label_count] & 0xff;
666 if (c && !isprint(c))
667 c = '!';
668 info->volname[label_count] = c;
669 label_count++;
670 }
671 state->parts[i + 1].has_info = true;
1da177e4
LT
672 }
673 kfree(ptes);
674 kfree(gpt);
9c867fbe 675 strlcat(state->pp_buf, "\n", PAGE_SIZE);
1da177e4
LT
676 return 1;
677}
This page took 1.568084 seconds and 5 git commands to generate.