1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96#include <linux/crc32.h>
97#include <linux/ctype.h>
98#include <linux/math64.h>
99#include <linux/slab.h>
100#include "check.h"
101#include "efi.h"
102
103
104
105
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
119
120
121
122
123
124
125
126
127
128
129static inline u32
130efi_crc32(const void *buf, unsigned long len)
131{
132 return (crc32(~0L, buf, len) ^ ~0L);
133}
134
135
136
137
138
139
140
141
142
143
144static u64 last_lba(struct block_device *bdev)
145{
146 if (!bdev || !bdev->bd_inode)
147 return 0;
148 return div_u64(bdev->bd_inode->i_size,
149 bdev_logical_block_size(bdev)) - 1ULL;
150}
151
152static inline int
153pmbr_part_valid(struct partition *part)
154{
155 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
156 le32_to_cpu(part->start_sect) == 1UL)
157 return 1;
158 return 0;
159}
160
161
162
163
164
165
166
167
168
169
170static int
171is_pmbr_valid(legacy_mbr *mbr)
172{
173 int i;
174 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
175 return 0;
176 for (i = 0; i < 4; i++)
177 if (pmbr_part_valid(&mbr->partition_record[i]))
178 return 1;
179 return 0;
180}
181
182
183
184
185
186
187
188
189
190
191
192static size_t read_lba(struct parsed_partitions *state,
193 u64 lba, u8 *buffer, size_t count)
194{
195 size_t totalreadcount = 0;
196 struct block_device *bdev = state->bdev;
197 sector_t n = lba * (bdev_logical_block_size(bdev) / 512);
198
199 if (!buffer || lba > last_lba(bdev))
200 return 0;
201
202 while (count) {
203 int copied = 512;
204 Sector sect;
205 unsigned char *data = read_part_sector(state, n++, §);
206 if (!data)
207 break;
208 if (copied > count)
209 copied = count;
210 memcpy(buffer, data, copied);
211 put_dev_sector(sect);
212 buffer += copied;
213 totalreadcount +=copied;
214 count -= copied;
215 }
216 return totalreadcount;
217}
218
219
220
221
222
223
224
225
226
227
228static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
229 gpt_header *gpt)
230{
231 size_t count;
232 gpt_entry *pte;
233
234 if (!gpt)
235 return NULL;
236
237 count = le32_to_cpu(gpt->num_partition_entries) *
238 le32_to_cpu(gpt->sizeof_partition_entry);
239 if (!count)
240 return NULL;
241 pte = kmalloc(count, GFP_KERNEL);
242 if (!pte)
243 return NULL;
244
245 if (read_lba(state, le64_to_cpu(gpt->partition_entry_lba),
246 (u8 *) pte,
247 count) < count) {
248 kfree(pte);
249 pte=NULL;
250 return NULL;
251 }
252 return pte;
253}
254
255
256
257
258
259
260
261
262
263
264static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
265 u64 lba)
266{
267 gpt_header *gpt;
268 unsigned ssz = bdev_logical_block_size(state->bdev);
269
270 gpt = kmalloc(ssz, GFP_KERNEL);
271 if (!gpt)
272 return NULL;
273
274 if (read_lba(state, lba, (u8 *) gpt, ssz) < ssz) {
275 kfree(gpt);
276 gpt=NULL;
277 return NULL;
278 }
279
280 return gpt;
281}
282
283
284
285
286
287
288
289
290
291
292
293static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
294 gpt_header **gpt, gpt_entry **ptes)
295{
296 u32 crc, origcrc;
297 u64 lastlba;
298
299 if (!ptes)
300 return 0;
301 if (!(*gpt = alloc_read_gpt_header(state, lba)))
302 return 0;
303
304
305 if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
306 pr_debug("GUID Partition Table Header signature is wrong:"
307 "%lld != %lld\n",
308 (unsigned long long)le64_to_cpu((*gpt)->signature),
309 (unsigned long long)GPT_HEADER_SIGNATURE);
310 goto fail;
311 }
312
313
314 if (le32_to_cpu((*gpt)->header_size) >
315 bdev_logical_block_size(state->bdev)) {
316 pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
317 le32_to_cpu((*gpt)->header_size),
318 bdev_logical_block_size(state->bdev));
319 goto fail;
320 }
321
322
323 if (le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header)) {
324 pr_debug("GUID Partition Table Header size is too small: %u < %zu\n",
325 le32_to_cpu((*gpt)->header_size),
326 sizeof(gpt_header));
327 goto fail;
328 }
329
330
331 origcrc = le32_to_cpu((*gpt)->header_crc32);
332 (*gpt)->header_crc32 = 0;
333 crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
334
335 if (crc != origcrc) {
336 pr_debug("GUID Partition Table Header CRC is wrong: %x != %x\n",
337 crc, origcrc);
338 goto fail;
339 }
340 (*gpt)->header_crc32 = cpu_to_le32(origcrc);
341
342
343
344 if (le64_to_cpu((*gpt)->my_lba) != lba) {
345 pr_debug("GPT my_lba incorrect: %lld != %lld\n",
346 (unsigned long long)le64_to_cpu((*gpt)->my_lba),
347 (unsigned long long)lba);
348 goto fail;
349 }
350
351
352
353
354 lastlba = last_lba(state->bdev);
355 if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
356 pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
357 (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
358 (unsigned long long)lastlba);
359 goto fail;
360 }
361 if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
362 pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
363 (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
364 (unsigned long long)lastlba);
365 goto fail;
366 }
367
368
369 if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
370 pr_debug("GUID Partitition Entry Size check failed.\n");
371 goto fail;
372 }
373
374 if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
375 goto fail;
376
377
378 crc = efi_crc32((const unsigned char *) (*ptes),
379 le32_to_cpu((*gpt)->num_partition_entries) *
380 le32_to_cpu((*gpt)->sizeof_partition_entry));
381
382 if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
383 pr_debug("GUID Partitition Entry Array CRC check failed.\n");
384 goto fail_ptes;
385 }
386
387
388 return 1;
389
390 fail_ptes:
391 kfree(*ptes);
392 *ptes = NULL;
393 fail:
394 kfree(*gpt);
395 *gpt = NULL;
396 return 0;
397}
398
399
400
401
402
403
404
405
406static inline int
407is_pte_valid(const gpt_entry *pte, const u64 lastlba)
408{
409 if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
410 le64_to_cpu(pte->starting_lba) > lastlba ||
411 le64_to_cpu(pte->ending_lba) > lastlba)
412 return 0;
413 return 1;
414}
415
416
417
418
419
420
421
422
423
424
425static void
426compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
427{
428 int error_found = 0;
429 if (!pgpt || !agpt)
430 return;
431 if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
432 printk(KERN_WARNING
433 "GPT:Primary header LBA != Alt. header alternate_lba\n");
434 printk(KERN_WARNING "GPT:%lld != %lld\n",
435 (unsigned long long)le64_to_cpu(pgpt->my_lba),
436 (unsigned long long)le64_to_cpu(agpt->alternate_lba));
437 error_found++;
438 }
439 if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
440 printk(KERN_WARNING
441 "GPT:Primary header alternate_lba != Alt. header my_lba\n");
442 printk(KERN_WARNING "GPT:%lld != %lld\n",
443 (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
444 (unsigned long long)le64_to_cpu(agpt->my_lba));
445 error_found++;
446 }
447 if (le64_to_cpu(pgpt->first_usable_lba) !=
448 le64_to_cpu(agpt->first_usable_lba)) {
449 printk(KERN_WARNING "GPT:first_usable_lbas don't match.\n");
450 printk(KERN_WARNING "GPT:%lld != %lld\n",
451 (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
452 (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
453 error_found++;
454 }
455 if (le64_to_cpu(pgpt->last_usable_lba) !=
456 le64_to_cpu(agpt->last_usable_lba)) {
457 printk(KERN_WARNING "GPT:last_usable_lbas don't match.\n");
458 printk(KERN_WARNING "GPT:%lld != %lld\n",
459 (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
460 (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
461 error_found++;
462 }
463 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
464 printk(KERN_WARNING "GPT:disk_guids don't match.\n");
465 error_found++;
466 }
467 if (le32_to_cpu(pgpt->num_partition_entries) !=
468 le32_to_cpu(agpt->num_partition_entries)) {
469 printk(KERN_WARNING "GPT:num_partition_entries don't match: "
470 "0x%x != 0x%x\n",
471 le32_to_cpu(pgpt->num_partition_entries),
472 le32_to_cpu(agpt->num_partition_entries));
473 error_found++;
474 }
475 if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
476 le32_to_cpu(agpt->sizeof_partition_entry)) {
477 printk(KERN_WARNING
478 "GPT:sizeof_partition_entry values don't match: "
479 "0x%x != 0x%x\n",
480 le32_to_cpu(pgpt->sizeof_partition_entry),
481 le32_to_cpu(agpt->sizeof_partition_entry));
482 error_found++;
483 }
484 if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
485 le32_to_cpu(agpt->partition_entry_array_crc32)) {
486 printk(KERN_WARNING
487 "GPT:partition_entry_array_crc32 values don't match: "
488 "0x%x != 0x%x\n",
489 le32_to_cpu(pgpt->partition_entry_array_crc32),
490 le32_to_cpu(agpt->partition_entry_array_crc32));
491 error_found++;
492 }
493 if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
494 printk(KERN_WARNING
495 "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
496 printk(KERN_WARNING "GPT:%lld != %lld\n",
497 (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
498 (unsigned long long)lastlba);
499 error_found++;
500 }
501
502 if (le64_to_cpu(agpt->my_lba) != lastlba) {
503 printk(KERN_WARNING
504 "GPT:Alternate GPT header not at the end of the disk.\n");
505 printk(KERN_WARNING "GPT:%lld != %lld\n",
506 (unsigned long long)le64_to_cpu(agpt->my_lba),
507 (unsigned long long)lastlba);
508 error_found++;
509 }
510
511 if (error_found)
512 printk(KERN_WARNING
513 "GPT: Use GNU Parted to correct GPT errors.\n");
514 return;
515}
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
533 gpt_entry **ptes)
534{
535 int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
536 gpt_header *pgpt = NULL, *agpt = NULL;
537 gpt_entry *pptes = NULL, *aptes = NULL;
538 legacy_mbr *legacymbr;
539 u64 lastlba;
540
541 if (!ptes)
542 return 0;
543
544 lastlba = last_lba(state->bdev);
545 if (!force_gpt) {
546
547 legacymbr = kzalloc(sizeof (*legacymbr), GFP_KERNEL);
548 if (legacymbr) {
549 read_lba(state, 0, (u8 *) legacymbr,
550 sizeof (*legacymbr));
551 good_pmbr = is_pmbr_valid(legacymbr);
552 kfree(legacymbr);
553 }
554 if (!good_pmbr)
555 goto fail;
556 }
557
558 good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
559 &pgpt, &pptes);
560 if (good_pgpt)
561 good_agpt = is_gpt_valid(state,
562 le64_to_cpu(pgpt->alternate_lba),
563 &agpt, &aptes);
564 if (!good_agpt && force_gpt)
565 good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
566
567
568 if (!good_pgpt && !good_agpt)
569 goto fail;
570
571 compare_gpts(pgpt, agpt, lastlba);
572
573
574 if (good_pgpt) {
575 *gpt = pgpt;
576 *ptes = pptes;
577 kfree(agpt);
578 kfree(aptes);
579 if (!good_agpt) {
580 printk(KERN_WARNING
581 "Alternate GPT is invalid, "
582 "using primary GPT.\n");
583 }
584 return 1;
585 }
586 else if (good_agpt) {
587 *gpt = agpt;
588 *ptes = aptes;
589 kfree(pgpt);
590 kfree(pptes);
591 printk(KERN_WARNING
592 "Primary GPT is invalid, using alternate GPT.\n");
593 return 1;
594 }
595
596 fail:
597 kfree(pgpt);
598 kfree(agpt);
599 kfree(pptes);
600 kfree(aptes);
601 *gpt = NULL;
602 *ptes = NULL;
603 return 0;
604}
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625int efi_partition(struct parsed_partitions *state)
626{
627 gpt_header *gpt = NULL;
628 gpt_entry *ptes = NULL;
629 u32 i;
630 unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
631
632 if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
633 kfree(gpt);
634 kfree(ptes);
635 return 0;
636 }
637
638 pr_debug("GUID Partition Table is valid! Yea!\n");
639
640 for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
641 struct partition_meta_info *info;
642 unsigned label_count = 0;
643 unsigned label_max;
644 u64 start = le64_to_cpu(ptes[i].starting_lba);
645 u64 size = le64_to_cpu(ptes[i].ending_lba) -
646 le64_to_cpu(ptes[i].starting_lba) + 1ULL;
647
648 if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
649 continue;
650
651 put_partition(state, i+1, start * ssz, size * ssz);
652
653
654 if (!efi_guidcmp(ptes[i].partition_type_guid,
655 PARTITION_LINUX_RAID_GUID))
656 state->parts[i + 1].flags = ADDPART_FLAG_RAID;
657
658 info = &state->parts[i + 1].info;
659 efi_guid_unparse(&ptes[i].unique_partition_guid, info->uuid);
660
661
662 label_max = min(sizeof(info->volname) - 1,
663 sizeof(ptes[i].partition_name));
664 info->volname[label_max] = 0;
665 while (label_count < label_max) {
666 u8 c = ptes[i].partition_name[label_count] & 0xff;
667 if (c && !isprint(c))
668 c = '!';
669 info->volname[label_count] = c;
670 label_count++;
671 }
672 state->parts[i + 1].has_info = true;
673 }
674 kfree(ptes);
675 kfree(gpt);
676 strlcat(state->pp_buf, "\n", PAGE_SIZE);
677 return 1;
678}
679