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#include <linux/kernel.h>
86#include <linux/crc32.h>
87#include <linux/ctype.h>
88#include <linux/math64.h>
89#include <linux/slab.h>
90#include "check.h"
91#include "efi.h"
92
93
94
95
96
97static int force_gpt;
98static int __init
99force_gpt_fn(char *str)
100{
101 force_gpt = 1;
102 return 1;
103}
104__setup("gpt", force_gpt_fn);
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119static inline u32
120efi_crc32(const void *buf, unsigned long len)
121{
122 return (crc32(~0L, buf, len) ^ ~0L);
123}
124
125
126
127
128
129
130
131
132
133
134static u64 last_lba(struct block_device *bdev)
135{
136 if (!bdev || !bdev->bd_inode)
137 return 0;
138 return div_u64(bdev->bd_inode->i_size,
139 bdev_logical_block_size(bdev)) - 1ULL;
140}
141
142static inline int pmbr_part_valid(gpt_mbr_record *part)
143{
144 if (part->os_type != EFI_PMBR_OSTYPE_EFI_GPT)
145 goto invalid;
146
147
148 if (le32_to_cpu(part->starting_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA)
149 goto invalid;
150
151 return GPT_MBR_PROTECTIVE;
152invalid:
153 return 0;
154}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174static int is_pmbr_valid(legacy_mbr *mbr, sector_t total_sectors)
175{
176 uint32_t sz = 0;
177 int i, part = 0, ret = 0;
178
179 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
180 goto done;
181
182 for (i = 0; i < 4; i++) {
183 ret = pmbr_part_valid(&mbr->partition_record[i]);
184 if (ret == GPT_MBR_PROTECTIVE) {
185 part = i;
186
187
188
189
190
191 goto check_hybrid;
192 }
193 }
194
195 if (ret != GPT_MBR_PROTECTIVE)
196 goto done;
197check_hybrid:
198 for (i = 0; i < 4; i++)
199 if ((mbr->partition_record[i].os_type !=
200 EFI_PMBR_OSTYPE_EFI_GPT) &&
201 (mbr->partition_record[i].os_type != 0x00))
202 ret = GPT_MBR_HYBRID;
203
204
205
206
207
208
209
210
211
212
213
214
215
216 if (ret == GPT_MBR_PROTECTIVE) {
217 sz = le32_to_cpu(mbr->partition_record[part].size_in_lba);
218 if (sz != (uint32_t) total_sectors - 1 && sz != 0xFFFFFFFF)
219 pr_debug("GPT: mbr size in lba (%u) different than whole disk (%u).\n",
220 sz, min_t(uint32_t,
221 total_sectors - 1, 0xFFFFFFFF));
222 }
223done:
224 return ret;
225}
226
227
228
229
230
231
232
233
234
235
236
237static size_t read_lba(struct parsed_partitions *state,
238 u64 lba, u8 *buffer, size_t count)
239{
240 size_t totalreadcount = 0;
241 struct block_device *bdev = state->bdev;
242 sector_t n = lba * (bdev_logical_block_size(bdev) / 512);
243
244 if (!buffer || lba > last_lba(bdev))
245 return 0;
246
247 while (count) {
248 int copied = 512;
249 Sector sect;
250 unsigned char *data = read_part_sector(state, n++, §);
251 if (!data)
252 break;
253 if (copied > count)
254 copied = count;
255 memcpy(buffer, data, copied);
256 put_dev_sector(sect);
257 buffer += copied;
258 totalreadcount +=copied;
259 count -= copied;
260 }
261 return totalreadcount;
262}
263
264
265
266
267
268
269
270
271
272
273static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
274 gpt_header *gpt)
275{
276 size_t count;
277 gpt_entry *pte;
278
279 if (!gpt)
280 return NULL;
281
282 count = (size_t)le32_to_cpu(gpt->num_partition_entries) *
283 le32_to_cpu(gpt->sizeof_partition_entry);
284 if (!count)
285 return NULL;
286 pte = kmalloc(count, GFP_KERNEL);
287 if (!pte)
288 return NULL;
289
290 if (read_lba(state, le64_to_cpu(gpt->partition_entry_lba),
291 (u8 *) pte, count) < count) {
292 kfree(pte);
293 pte=NULL;
294 return NULL;
295 }
296 return pte;
297}
298
299
300
301
302
303
304
305
306
307
308static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
309 u64 lba)
310{
311 gpt_header *gpt;
312 unsigned ssz = bdev_logical_block_size(state->bdev);
313
314 gpt = kmalloc(ssz, GFP_KERNEL);
315 if (!gpt)
316 return NULL;
317
318 if (read_lba(state, lba, (u8 *) gpt, ssz) < ssz) {
319 kfree(gpt);
320 gpt=NULL;
321 return NULL;
322 }
323
324 return gpt;
325}
326
327
328
329
330
331
332
333
334
335
336
337static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
338 gpt_header **gpt, gpt_entry **ptes)
339{
340 u32 crc, origcrc;
341 u64 lastlba, pt_size;
342
343 if (!ptes)
344 return 0;
345 if (!(*gpt = alloc_read_gpt_header(state, lba)))
346 return 0;
347
348
349 if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
350 pr_debug("GUID Partition Table Header signature is wrong:"
351 "%lld != %lld\n",
352 (unsigned long long)le64_to_cpu((*gpt)->signature),
353 (unsigned long long)GPT_HEADER_SIGNATURE);
354 goto fail;
355 }
356
357
358 if (le32_to_cpu((*gpt)->header_size) >
359 bdev_logical_block_size(state->bdev)) {
360 pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
361 le32_to_cpu((*gpt)->header_size),
362 bdev_logical_block_size(state->bdev));
363 goto fail;
364 }
365
366
367 if (le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header)) {
368 pr_debug("GUID Partition Table Header size is too small: %u < %zu\n",
369 le32_to_cpu((*gpt)->header_size),
370 sizeof(gpt_header));
371 goto fail;
372 }
373
374
375 origcrc = le32_to_cpu((*gpt)->header_crc32);
376 (*gpt)->header_crc32 = 0;
377 crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
378
379 if (crc != origcrc) {
380 pr_debug("GUID Partition Table Header CRC is wrong: %x != %x\n",
381 crc, origcrc);
382 goto fail;
383 }
384 (*gpt)->header_crc32 = cpu_to_le32(origcrc);
385
386
387
388 if (le64_to_cpu((*gpt)->my_lba) != lba) {
389 pr_debug("GPT my_lba incorrect: %lld != %lld\n",
390 (unsigned long long)le64_to_cpu((*gpt)->my_lba),
391 (unsigned long long)lba);
392 goto fail;
393 }
394
395
396
397
398 lastlba = last_lba(state->bdev);
399 if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
400 pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
401 (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
402 (unsigned long long)lastlba);
403 goto fail;
404 }
405 if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
406 pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
407 (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
408 (unsigned long long)lastlba);
409 goto fail;
410 }
411 if (le64_to_cpu((*gpt)->last_usable_lba) < le64_to_cpu((*gpt)->first_usable_lba)) {
412 pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
413 (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
414 (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba));
415 goto fail;
416 }
417
418 if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
419 pr_debug("GUID Partition Entry Size check failed.\n");
420 goto fail;
421 }
422
423
424 pt_size = (u64)le32_to_cpu((*gpt)->num_partition_entries) *
425 le32_to_cpu((*gpt)->sizeof_partition_entry);
426 if (pt_size > KMALLOC_MAX_SIZE) {
427 pr_debug("GUID Partition Table is too large: %llu > %lu bytes\n",
428 (unsigned long long)pt_size, KMALLOC_MAX_SIZE);
429 goto fail;
430 }
431
432 if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
433 goto fail;
434
435
436 crc = efi_crc32((const unsigned char *) (*ptes), pt_size);
437
438 if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
439 pr_debug("GUID Partition Entry Array CRC check failed.\n");
440 goto fail_ptes;
441 }
442
443
444 return 1;
445
446 fail_ptes:
447 kfree(*ptes);
448 *ptes = NULL;
449 fail:
450 kfree(*gpt);
451 *gpt = NULL;
452 return 0;
453}
454
455
456
457
458
459
460
461
462static inline int
463is_pte_valid(const gpt_entry *pte, const u64 lastlba)
464{
465 if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
466 le64_to_cpu(pte->starting_lba) > lastlba ||
467 le64_to_cpu(pte->ending_lba) > lastlba)
468 return 0;
469 return 1;
470}
471
472
473
474
475
476
477
478
479
480
481
482static void
483compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
484{
485 int error_found = 0;
486 if (!pgpt || !agpt)
487 return;
488 if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
489 pr_warn("GPT:Primary header LBA != Alt. header alternate_lba\n");
490 pr_warn("GPT:%lld != %lld\n",
491 (unsigned long long)le64_to_cpu(pgpt->my_lba),
492 (unsigned long long)le64_to_cpu(agpt->alternate_lba));
493 error_found++;
494 }
495 if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
496 pr_warn("GPT:Primary header alternate_lba != Alt. header my_lba\n");
497 pr_warn("GPT:%lld != %lld\n",
498 (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
499 (unsigned long long)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 pr_warn("GPT:first_usable_lbas don't match.\n");
505 pr_warn("GPT:%lld != %lld\n",
506 (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
507 (unsigned long long)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 pr_warn("GPT:last_usable_lbas don't match.\n");
513 pr_warn("GPT:%lld != %lld\n",
514 (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
515 (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
516 error_found++;
517 }
518 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
519 pr_warn("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 pr_warn("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 pr_warn("GPT:sizeof_partition_entry values don't match: "
533 "0x%x != 0x%x\n",
534 le32_to_cpu(pgpt->sizeof_partition_entry),
535 le32_to_cpu(agpt->sizeof_partition_entry));
536 error_found++;
537 }
538 if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
539 le32_to_cpu(agpt->partition_entry_array_crc32)) {
540 pr_warn("GPT:partition_entry_array_crc32 values don't match: "
541 "0x%x != 0x%x\n",
542 le32_to_cpu(pgpt->partition_entry_array_crc32),
543 le32_to_cpu(agpt->partition_entry_array_crc32));
544 error_found++;
545 }
546 if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
547 pr_warn("GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
548 pr_warn("GPT:%lld != %lld\n",
549 (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
550 (unsigned long long)lastlba);
551 error_found++;
552 }
553
554 if (le64_to_cpu(agpt->my_lba) != lastlba) {
555 pr_warn("GPT:Alternate GPT header not at the end of the disk.\n");
556 pr_warn("GPT:%lld != %lld\n",
557 (unsigned long long)le64_to_cpu(agpt->my_lba),
558 (unsigned long long)lastlba);
559 error_found++;
560 }
561
562 if (error_found)
563 pr_warn("GPT: Use GNU Parted to correct GPT errors.\n");
564 return;
565}
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
584 gpt_entry **ptes)
585{
586 int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
587 gpt_header *pgpt = NULL, *agpt = NULL;
588 gpt_entry *pptes = NULL, *aptes = NULL;
589 legacy_mbr *legacymbr;
590 sector_t total_sectors = i_size_read(state->bdev->bd_inode) >> 9;
591 u64 lastlba;
592
593 if (!ptes)
594 return 0;
595
596 lastlba = last_lba(state->bdev);
597 if (!force_gpt) {
598
599 legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
600 if (!legacymbr)
601 goto fail;
602
603 read_lba(state, 0, (u8 *)legacymbr, sizeof(*legacymbr));
604 good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
605 kfree(legacymbr);
606
607 if (!good_pmbr)
608 goto fail;
609
610 pr_debug("Device has a %s MBR\n",
611 good_pmbr == GPT_MBR_PROTECTIVE ?
612 "protective" : "hybrid");
613 }
614
615 good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
616 &pgpt, &pptes);
617 if (good_pgpt)
618 good_agpt = is_gpt_valid(state,
619 le64_to_cpu(pgpt->alternate_lba),
620 &agpt, &aptes);
621 if (!good_agpt && force_gpt)
622 good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
623
624
625 if (!good_pgpt && !good_agpt)
626 goto fail;
627
628 compare_gpts(pgpt, agpt, lastlba);
629
630
631 if (good_pgpt) {
632 *gpt = pgpt;
633 *ptes = pptes;
634 kfree(agpt);
635 kfree(aptes);
636 if (!good_agpt)
637 pr_warn("Alternate GPT is invalid, using primary GPT.\n");
638 return 1;
639 }
640 else if (good_agpt) {
641 *gpt = agpt;
642 *ptes = aptes;
643 kfree(pgpt);
644 kfree(pptes);
645 pr_warn("Primary GPT is invalid, using alternate GPT.\n");
646 return 1;
647 }
648
649 fail:
650 kfree(pgpt);
651 kfree(agpt);
652 kfree(pptes);
653 kfree(aptes);
654 *gpt = NULL;
655 *ptes = NULL;
656 return 0;
657}
658
659
660
661
662
663
664
665
666
667
668static void utf16_le_to_7bit(const __le16 *in, unsigned int size, u8 *out)
669{
670 unsigned int i = 0;
671
672 out[size] = 0;
673
674 while (i < size) {
675 u8 c = le16_to_cpu(in[i]) & 0xff;
676
677 if (c && !isprint(c))
678 c = '!';
679 out[i] = c;
680 i++;
681 }
682}
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703int efi_partition(struct parsed_partitions *state)
704{
705 gpt_header *gpt = NULL;
706 gpt_entry *ptes = NULL;
707 u32 i;
708 unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
709
710 if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
711 kfree(gpt);
712 kfree(ptes);
713 return 0;
714 }
715
716 pr_debug("GUID Partition Table is valid! Yea!\n");
717
718 for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
719 struct partition_meta_info *info;
720 unsigned label_max;
721 u64 start = le64_to_cpu(ptes[i].starting_lba);
722 u64 size = le64_to_cpu(ptes[i].ending_lba) -
723 le64_to_cpu(ptes[i].starting_lba) + 1ULL;
724
725 if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
726 continue;
727
728 put_partition(state, i+1, start * ssz, size * ssz);
729
730
731 if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID))
732 state->parts[i + 1].flags = ADDPART_FLAG_RAID;
733
734 info = &state->parts[i + 1].info;
735 efi_guid_to_str(&ptes[i].unique_partition_guid, info->uuid);
736
737
738 label_max = min(ARRAY_SIZE(info->volname) - 1,
739 ARRAY_SIZE(ptes[i].partition_name));
740 utf16_le_to_7bit(ptes[i].partition_name, label_max, info->volname);
741 state->parts[i + 1].has_info = true;
742 }
743 kfree(ptes);
744 kfree(gpt);
745 strlcat(state->pp_buf, "\n", PAGE_SIZE);
746 return 1;
747}
748