Commit e1f6b0a02d3bf45737fcb2addf4125cebe0cbcf9
Committed by
Lukasz Majewski
1 parent
f597fc3d4c
Exists in
v2017.01-smarct4x
and in
37 other branches
disk: part_efi: move code to static functions
Signed-off-by: Steve Rae <srae@broadcom.com> Tested-by: Lukasz Majewski <l.majewski@samsung.com> [Test HW: Exynos4412 - Trats2]
Showing 1 changed file with 102 additions and 73 deletions Side-by-side Diff
disk/part_efi.c
... | ... | @@ -69,6 +69,105 @@ |
69 | 69 | sizeof(efi_guid_t)); |
70 | 70 | } |
71 | 71 | |
72 | +static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba, | |
73 | + lbaint_t lastlba) | |
74 | +{ | |
75 | + uint32_t crc32_backup = 0; | |
76 | + uint32_t calc_crc32; | |
77 | + | |
78 | + /* Check the GPT header signature */ | |
79 | + if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) { | |
80 | + printf("%s signature is wrong: 0x%llX != 0x%llX\n", | |
81 | + "GUID Partition Table Header", | |
82 | + le64_to_cpu(gpt_h->signature), | |
83 | + GPT_HEADER_SIGNATURE); | |
84 | + return -1; | |
85 | + } | |
86 | + | |
87 | + /* Check the GUID Partition Table CRC */ | |
88 | + memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup)); | |
89 | + memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32)); | |
90 | + | |
91 | + calc_crc32 = efi_crc32((const unsigned char *)gpt_h, | |
92 | + le32_to_cpu(gpt_h->header_size)); | |
93 | + | |
94 | + memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup)); | |
95 | + | |
96 | + if (calc_crc32 != le32_to_cpu(crc32_backup)) { | |
97 | + printf("%s CRC is wrong: 0x%x != 0x%x\n", | |
98 | + "GUID Partition Table Header", | |
99 | + le32_to_cpu(crc32_backup), calc_crc32); | |
100 | + return -1; | |
101 | + } | |
102 | + | |
103 | + /* | |
104 | + * Check that the my_lba entry points to the LBA that contains the GPT | |
105 | + */ | |
106 | + if (le64_to_cpu(gpt_h->my_lba) != lba) { | |
107 | + printf("GPT: my_lba incorrect: %llX != " LBAF "\n", | |
108 | + le64_to_cpu(gpt_h->my_lba), | |
109 | + lba); | |
110 | + return -1; | |
111 | + } | |
112 | + | |
113 | + /* | |
114 | + * Check that the first_usable_lba and that the last_usable_lba are | |
115 | + * within the disk. | |
116 | + */ | |
117 | + if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) { | |
118 | + printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n", | |
119 | + le64_to_cpu(gpt_h->first_usable_lba), lastlba); | |
120 | + return -1; | |
121 | + } | |
122 | + if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) { | |
123 | + printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n", | |
124 | + le64_to_cpu(gpt_h->last_usable_lba), lastlba); | |
125 | + return -1; | |
126 | + } | |
127 | + | |
128 | + debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: " | |
129 | + LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba), | |
130 | + le64_to_cpu(gpt_h->last_usable_lba), lastlba); | |
131 | + | |
132 | + return 0; | |
133 | +} | |
134 | + | |
135 | +static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e) | |
136 | +{ | |
137 | + uint32_t calc_crc32; | |
138 | + | |
139 | + /* Check the GUID Partition Table Entry Array CRC */ | |
140 | + calc_crc32 = efi_crc32((const unsigned char *)gpt_e, | |
141 | + le32_to_cpu(gpt_h->num_partition_entries) * | |
142 | + le32_to_cpu(gpt_h->sizeof_partition_entry)); | |
143 | + | |
144 | + if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) { | |
145 | + printf("%s: 0x%x != 0x%x\n", | |
146 | + "GUID Partition Table Entry Array CRC is wrong", | |
147 | + le32_to_cpu(gpt_h->partition_entry_array_crc32), | |
148 | + calc_crc32); | |
149 | + return -1; | |
150 | + } | |
151 | + | |
152 | + return 0; | |
153 | +} | |
154 | + | |
155 | +static void prepare_backup_gpt_header(gpt_header *gpt_h) | |
156 | +{ | |
157 | + uint32_t calc_crc32; | |
158 | + uint64_t val; | |
159 | + | |
160 | + /* recalculate the values for the Backup GPT Header */ | |
161 | + val = le64_to_cpu(gpt_h->my_lba); | |
162 | + gpt_h->my_lba = gpt_h->alternate_lba; | |
163 | + gpt_h->alternate_lba = cpu_to_le64(val); | |
164 | + gpt_h->header_crc32 = 0; | |
165 | + | |
166 | + calc_crc32 = efi_crc32((const unsigned char *)gpt_h, | |
167 | + le32_to_cpu(gpt_h->header_size)); | |
168 | + gpt_h->header_crc32 = cpu_to_le32(calc_crc32); | |
169 | +} | |
170 | + | |
72 | 171 | #ifdef CONFIG_EFI_PARTITION |
73 | 172 | /* |
74 | 173 | * Public Functions (include/part.h) |
... | ... | @@ -259,7 +358,6 @@ |
259 | 358 | const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries |
260 | 359 | * sizeof(gpt_entry)), dev_desc); |
261 | 360 | u32 calc_crc32; |
262 | - u64 val; | |
263 | 361 | |
264 | 362 | debug("max lba: %x\n", (u32) dev_desc->lba); |
265 | 363 | /* Setup the Protective MBR */ |
266 | 364 | |
... | ... | @@ -284,16 +382,8 @@ |
284 | 382 | != pte_blk_cnt) |
285 | 383 | goto err; |
286 | 384 | |
287 | - /* recalculate the values for the Backup GPT Header */ | |
288 | - val = le64_to_cpu(gpt_h->my_lba); | |
289 | - gpt_h->my_lba = gpt_h->alternate_lba; | |
290 | - gpt_h->alternate_lba = cpu_to_le64(val); | |
291 | - gpt_h->header_crc32 = 0; | |
385 | + prepare_backup_gpt_header(gpt_h); | |
292 | 386 | |
293 | - calc_crc32 = efi_crc32((const unsigned char *)gpt_h, | |
294 | - le32_to_cpu(gpt_h->header_size)); | |
295 | - gpt_h->header_crc32 = cpu_to_le32(calc_crc32); | |
296 | - | |
297 | 387 | if (dev_desc->block_write(dev_desc->dev, |
298 | 388 | (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba) |
299 | 389 | + 1, |
... | ... | @@ -511,10 +601,6 @@ |
511 | 601 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
512 | 602 | gpt_header *pgpt_head, gpt_entry **pgpt_pte) |
513 | 603 | { |
514 | - u32 crc32_backup = 0; | |
515 | - u32 calc_crc32; | |
516 | - u64 lastlba; | |
517 | - | |
518 | 604 | if (!dev_desc || !pgpt_head) { |
519 | 605 | printf("%s: Invalid Argument(s)\n", __func__); |
520 | 606 | return 0; |
521 | 607 | |
522 | 608 | |
... | ... | @@ -527,56 +613,9 @@ |
527 | 613 | return 0; |
528 | 614 | } |
529 | 615 | |
530 | - /* Check the GPT header signature */ | |
531 | - if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) { | |
532 | - printf("GUID Partition Table Header signature is wrong:" | |
533 | - "0x%llX != 0x%llX\n", | |
534 | - le64_to_cpu(pgpt_head->signature), | |
535 | - GPT_HEADER_SIGNATURE); | |
616 | + if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba)) | |
536 | 617 | return 0; |
537 | - } | |
538 | 618 | |
539 | - /* Check the GUID Partition Table CRC */ | |
540 | - memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup)); | |
541 | - memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32)); | |
542 | - | |
543 | - calc_crc32 = efi_crc32((const unsigned char *)pgpt_head, | |
544 | - le32_to_cpu(pgpt_head->header_size)); | |
545 | - | |
546 | - memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup)); | |
547 | - | |
548 | - if (calc_crc32 != le32_to_cpu(crc32_backup)) { | |
549 | - printf("GUID Partition Table Header CRC is wrong:" | |
550 | - "0x%x != 0x%x\n", | |
551 | - le32_to_cpu(crc32_backup), calc_crc32); | |
552 | - return 0; | |
553 | - } | |
554 | - | |
555 | - /* Check that the my_lba entry points to the LBA that contains the GPT */ | |
556 | - if (le64_to_cpu(pgpt_head->my_lba) != lba) { | |
557 | - printf("GPT: my_lba incorrect: %llX != %" PRIX64 "\n", | |
558 | - le64_to_cpu(pgpt_head->my_lba), | |
559 | - lba); | |
560 | - return 0; | |
561 | - } | |
562 | - | |
563 | - /* Check the first_usable_lba and last_usable_lba are within the disk. */ | |
564 | - lastlba = (u64)dev_desc->lba; | |
565 | - if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) { | |
566 | - printf("GPT: first_usable_lba incorrect: %llX > %" PRIX64 "\n", | |
567 | - le64_to_cpu(pgpt_head->first_usable_lba), lastlba); | |
568 | - return 0; | |
569 | - } | |
570 | - if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) { | |
571 | - printf("GPT: last_usable_lba incorrect: %llX > %" PRIX64 "\n", | |
572 | - le64_to_cpu(pgpt_head->last_usable_lba), lastlba); | |
573 | - return 0; | |
574 | - } | |
575 | - | |
576 | - debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %" | |
577 | - PRIX64 "\n", le64_to_cpu(pgpt_head->first_usable_lba), | |
578 | - le64_to_cpu(pgpt_head->last_usable_lba), lastlba); | |
579 | - | |
580 | 619 | /* Read and allocate Partition Table Entries */ |
581 | 620 | *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); |
582 | 621 | if (*pgpt_pte == NULL) { |
... | ... | @@ -584,17 +623,7 @@ |
584 | 623 | return 0; |
585 | 624 | } |
586 | 625 | |
587 | - /* Check the GUID Partition Table Entry Array CRC */ | |
588 | - calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte, | |
589 | - le32_to_cpu(pgpt_head->num_partition_entries) * | |
590 | - le32_to_cpu(pgpt_head->sizeof_partition_entry)); | |
591 | - | |
592 | - if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) { | |
593 | - printf("GUID Partition Table Entry Array CRC is wrong:" | |
594 | - "0x%x != 0x%x\n", | |
595 | - le32_to_cpu(pgpt_head->partition_entry_array_crc32), | |
596 | - calc_crc32); | |
597 | - | |
626 | + if (validate_gpt_entries(pgpt_head, *pgpt_pte)) { | |
598 | 627 | free(*pgpt_pte); |
599 | 628 | return 0; |
600 | 629 | } |