97 #include <linux/ctype.h>
99 #include <linux/slab.h>
107 static int force_gpt;
109 force_gpt_fn(
char *
str)
130 efi_crc32(
const void *
buf,
unsigned long len)
132 return (
crc32(~0L, buf, len) ^ ~0L);
148 return div_u64(bdev->
bd_inode->i_size,
149 bdev_logical_block_size(bdev)) - 1ULL;
171 is_pmbr_valid(legacy_mbr *mbr)
176 for (i = 0; i < 4; i++)
177 if (pmbr_part_valid(&mbr->partition_record[i]))
195 size_t totalreadcount = 0;
197 sector_t n = lba * (bdev_logical_block_size(bdev) / 512);
199 if (!buffer || lba > last_lba(bdev))
205 unsigned char *
data = read_part_sector(state, n++, §);
210 memcpy(buffer, data, copied);
211 put_dev_sector(sect);
213 totalreadcount +=copied;
216 return totalreadcount;
245 if (read_lba(state,
le64_to_cpu(gpt->partition_entry_lba),
268 unsigned ssz = bdev_logical_block_size(state->
bdev);
274 if (read_lba(state, lba, (
u8 *) gpt, ssz) < ssz) {
294 gpt_header **gpt, gpt_entry **ptes)
301 if (!(*gpt = alloc_read_gpt_header(state, lba)))
306 pr_debug(
"GUID Partition Table Header signature is wrong:"
308 (
unsigned long long)
le64_to_cpu((*gpt)->signature),
315 bdev_logical_block_size(state->
bdev)) {
316 pr_debug(
"GUID Partition Table Header size is wrong: %u > %u\n",
318 bdev_logical_block_size(state->
bdev));
324 (*gpt)->header_crc32 = 0;
325 crc = efi_crc32((
const unsigned char *) (*gpt),
le32_to_cpu((*gpt)->header_size));
327 if (crc != origcrc) {
328 pr_debug(
"GUID Partition Table Header CRC is wrong: %x != %x\n",
337 pr_debug(
"GPT my_lba incorrect: %lld != %lld\n",
339 (
unsigned long long)lba);
346 lastlba = last_lba(state->
bdev);
347 if (
le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
348 pr_debug(
"GPT: first_usable_lba incorrect: %lld > %lld\n",
349 (
unsigned long long)
le64_to_cpu((*gpt)->first_usable_lba),
350 (
unsigned long long)lastlba);
353 if (
le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
354 pr_debug(
"GPT: last_usable_lba incorrect: %lld > %lld\n",
355 (
unsigned long long)
le64_to_cpu((*gpt)->last_usable_lba),
356 (
unsigned long long)lastlba);
361 if (
le32_to_cpu((*gpt)->sizeof_partition_entry) !=
sizeof(gpt_entry)) {
362 pr_debug(
"GUID Partitition Entry Size check failed.\n");
366 if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
370 crc = efi_crc32((
const unsigned char *) (*ptes),
374 if (crc !=
le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
375 pr_debug(
"GUID Partitition Entry Array CRC check failed.\n");
399 is_pte_valid(
const gpt_entry *pte,
const u64 lastlba)
401 if ((!efi_guidcmp(pte->partition_type_guid,
NULL_GUID)) ||
418 compare_gpts(gpt_header *pgpt, gpt_header *agpt,
u64 lastlba)
425 "GPT:Primary header LBA != Alt. header alternate_lba\n");
428 (
unsigned long long)
le64_to_cpu(agpt->alternate_lba));
433 "GPT:Primary header alternate_lba != Alt. header my_lba\n");
435 (
unsigned long long)
le64_to_cpu(pgpt->alternate_lba),
443 (
unsigned long long)
le64_to_cpu(pgpt->first_usable_lba),
444 (
unsigned long long)
le64_to_cpu(agpt->first_usable_lba));
451 (
unsigned long long)
le64_to_cpu(pgpt->last_usable_lba),
452 (
unsigned long long)
le64_to_cpu(agpt->last_usable_lba));
455 if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
470 "GPT:sizeof_partition_entry values don't match: "
476 if (
le32_to_cpu(pgpt->partition_entry_array_crc32) !=
479 "GPT:partition_entry_array_crc32 values don't match: "
487 "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
489 (
unsigned long long)
le64_to_cpu(pgpt->alternate_lba),
490 (
unsigned long long)lastlba);
496 "GPT:Alternate GPT header not at the end of the disk.\n");
499 (
unsigned long long)lastlba);
505 "GPT: Use GNU Parted to correct GPT errors.\n");
527 int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
528 gpt_header *pgpt =
NULL, *agpt =
NULL;
529 gpt_entry *pptes =
NULL, *aptes =
NULL;
530 legacy_mbr *legacymbr;
536 lastlba = last_lba(state->
bdev);
539 legacymbr = kzalloc(
sizeof (*legacymbr),
GFP_KERNEL);
541 read_lba(state, 0, (
u8 *) legacymbr,
542 sizeof (*legacymbr));
543 good_pmbr = is_pmbr_valid(legacymbr);
553 good_agpt = is_gpt_valid(state,
556 if (!good_agpt && force_gpt)
557 good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
560 if (!good_pgpt && !good_agpt)
563 compare_gpts(pgpt, agpt, lastlba);
573 "Alternate GPT is invalid, "
574 "using primary GPT.\n");
578 else if (good_agpt) {
584 "Primary GPT is invalid, using alternate GPT.\n");
619 gpt_header *gpt =
NULL;
620 gpt_entry *ptes =
NULL;
622 unsigned ssz = bdev_logical_block_size(state->
bdev) / 512;
623 u8 unparsed_guid[37];
625 if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
631 pr_debug(
"GUID Partition Table is valid! Yea!\n");
633 for (i = 0; i <
le32_to_cpu(gpt->num_partition_entries) && i < state->
limit-1; i++) {
634 struct partition_meta_info *
info;
635 unsigned label_count = 0;
641 if (!is_pte_valid(&ptes[i], last_lba(state->
bdev)))
644 put_partition(state, i+1, start * ssz, size * ssz);
649 state->
parts[i + 1].flags = ADDPART_FLAG_RAID;
651 info = &state->
parts[i + 1].info;
656 part_pack_uuid(unparsed_guid, info->uuid);
659 label_max =
min(
sizeof(info->volname) - 1,
660 sizeof(ptes[i].partition_name));
661 info->volname[label_max] = 0;
662 while (label_count < label_max) {
663 u8 c = ptes[
i].partition_name[label_count] & 0xff;
666 info->volname[label_count] =
c;
669 state->
parts[i + 1].has_info =
true;