Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2264)

Side by Side Diff: cgpt/cgpt_common.c

Issue 5115002: Fixing the bug of CGPT when primary entry table is invalid. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vboot_reference.git
Patch Set: Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | cgpt/cmd_add.c » ('j') | cgpt/cmd_add.c » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be 2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. 3 * found in the LICENSE file.
4 * 4 *
5 * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c 5 * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c
6 * files for more details. 6 * files for more details.
7 */ 7 */
8 8
9 #include "cgpt.h" 9 #include "cgpt.h"
10 10
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 } 473 }
474 474
475 GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index) { 475 GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index) {
476 uint8_t *entries; 476 uint8_t *entries;
477 uint32_t stride = GetSizeOfEntries(gpt); 477 uint32_t stride = GetSizeOfEntries(gpt);
478 require(stride); 478 require(stride);
479 require(entry_index < GetNumberOfEntries(gpt)); 479 require(entry_index < GetNumberOfEntries(gpt));
480 480
481 if (secondary == PRIMARY) { 481 if (secondary == PRIMARY) {
482 entries = gpt->primary_entries; 482 entries = gpt->primary_entries;
483 } else { 483 } else if (secondary == SECONDARY) {
484 entries = gpt->secondary_entries; 484 entries = gpt->secondary_entries;
485 } else { /* ANY_VALID */
486 require(ANY_VALID);
Bill Richardson 2010/11/17 18:06:24 'require' is just 'assert' that doesn't compile aw
Louis 2010/11/18 01:51:29 This is a typo. Should be "require(secondary==ANY_
487 if (gpt->valid_entries & MASK_PRIMARY) {
488 entries = gpt->primary_entries;
489 } else {
490 require(gpt->valid_entries & MASK_SECONDARY);
491 entries = gpt->secondary_entries;
492 }
485 } 493 }
486 494
487 return (GptEntry*)(&entries[stride * entry_index]); 495 return (GptEntry*)(&entries[stride * entry_index]);
488 } 496 }
489 497
490 void SetPriority(GptData *gpt, int secondary, uint32_t entry_index, 498 void SetPriority(GptData *gpt, int secondary, uint32_t entry_index,
491 int priority) { 499 int priority) {
492 GptEntry *entry; 500 GptEntry *entry;
493 entry = GetEntry(gpt, secondary, entry_index); 501 entry = GetEntry(gpt, secondary, entry_index);
494 require(priority >= 0 && priority <= CGPT_ATTRIBUTE_MAX_PRIORITY); 502 require(priority >= 0 && priority <= CGPT_ATTRIBUTE_MAX_PRIORITY);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 primary_header->entries_crc32 = 571 primary_header->entries_crc32 =
564 Crc32(gpt->primary_entries, TOTAL_ENTRIES_SIZE); 572 Crc32(gpt->primary_entries, TOTAL_ENTRIES_SIZE);
565 } 573 }
566 if (gpt->modified & GPT_MODIFIED_ENTRIES2) { 574 if (gpt->modified & GPT_MODIFIED_ENTRIES2) {
567 secondary_header->entries_crc32 = 575 secondary_header->entries_crc32 =
568 Crc32(gpt->secondary_entries, TOTAL_ENTRIES_SIZE); 576 Crc32(gpt->secondary_entries, TOTAL_ENTRIES_SIZE);
569 } 577 }
570 if (gpt->modified & GPT_MODIFIED_HEADER1) { 578 if (gpt->modified & GPT_MODIFIED_HEADER1) {
571 primary_header->header_crc32 = 0; 579 primary_header->header_crc32 = 0;
572 primary_header->header_crc32 = Crc32( 580 primary_header->header_crc32 = Crc32(
573 (const uint8_t *)primary_header, primary_header->size); 581 (const uint8_t *)primary_header, sizeof(GptHeader));
574 } 582 }
575 if (gpt->modified & GPT_MODIFIED_HEADER2) { 583 if (gpt->modified & GPT_MODIFIED_HEADER2) {
576 secondary_header->header_crc32 = 0; 584 secondary_header->header_crc32 = 0;
577 secondary_header->header_crc32 = Crc32( 585 secondary_header->header_crc32 = Crc32(
578 (const uint8_t *)secondary_header, secondary_header->size); 586 (const uint8_t *)secondary_header, sizeof(GptHeader));
579 } 587 }
580 } 588 }
581 /* Two headers are NOT bitwise identical. For example, my_lba pointers to header 589 /* Two headers are NOT bitwise identical. For example, my_lba pointers to header
582 * itself so that my_lba in primary and secondary is definitely different. 590 * itself so that my_lba in primary and secondary is definitely different.
583 * Only the following fields should be identical. 591 * Only the following fields should be identical.
584 * 592 *
585 * first_usable_lba 593 * first_usable_lba
586 * last_usable_lba 594 * last_usable_lba
587 * number_of_entries 595 * number_of_entries
588 * size_of_entry 596 * size_of_entry
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 663
656 primary_header = (GptHeader*)gpt->primary_header; 664 primary_header = (GptHeader*)gpt->primary_header;
657 secondary_header = (GptHeader*)gpt->secondary_header; 665 secondary_header = (GptHeader*)gpt->secondary_header;
658 666
659 if (valid_headers == MASK_BOTH) { 667 if (valid_headers == MASK_BOTH) {
660 if (!IsSynonymous(primary_header, secondary_header)) { 668 if (!IsSynonymous(primary_header, secondary_header)) {
661 CopySynonymousParts(secondary_header, primary_header); 669 CopySynonymousParts(secondary_header, primary_header);
662 return GPT_MODIFIED_HEADER2; 670 return GPT_MODIFIED_HEADER2;
663 } 671 }
664 } else if (valid_headers == MASK_PRIMARY) { 672 } else if (valid_headers == MASK_PRIMARY) {
665 memcpy(secondary_header, primary_header, primary_header->size); 673 memcpy(secondary_header, primary_header, sizeof(GptHeader));
666 secondary_header->my_lba = gpt->drive_sectors - 1; /* the last sector */ 674 secondary_header->my_lba = gpt->drive_sectors - 1; /* the last sector */
667 secondary_header->alternate_lba = primary_header->my_lba; 675 secondary_header->alternate_lba = primary_header->my_lba;
668 secondary_header->entries_lba = secondary_header->my_lba - 676 secondary_header->entries_lba = secondary_header->my_lba -
669 GPT_ENTRIES_SECTORS; 677 GPT_ENTRIES_SECTORS;
670 return GPT_MODIFIED_HEADER2; 678 return GPT_MODIFIED_HEADER2;
671 } else if (valid_headers == MASK_SECONDARY) { 679 } else if (valid_headers == MASK_SECONDARY) {
672 memcpy(primary_header, secondary_header, secondary_header->size); 680 memcpy(primary_header, secondary_header, sizeof(GptHeader));
673 primary_header->my_lba = GPT_PMBR_SECTOR; /* the second sector on drive */ 681 primary_header->my_lba = GPT_PMBR_SECTOR; /* the second sector on drive */
674 primary_header->alternate_lba = secondary_header->my_lba; 682 primary_header->alternate_lba = secondary_header->my_lba;
675 primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTOR; 683 primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTOR;
676 return GPT_MODIFIED_HEADER1; 684 return GPT_MODIFIED_HEADER1;
677 } 685 }
678 686
679 return 0; 687 return 0;
680 } 688 }
681 689
682 690
683 int IsZero(const Guid *gp) { 691 int IsZero(const Guid *gp) {
684 return (0 == memcmp(gp, &guid_unused, sizeof(Guid))); 692 return (0 == memcmp(gp, &guid_unused, sizeof(Guid)));
685 } 693 }
686 694
687 void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen) { 695 void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen) {
688 char buf[GUID_STRLEN]; 696 char buf[GUID_STRLEN];
689 if (IsZero(&pmbr->boot_guid)) { 697 if (IsZero(&pmbr->boot_guid)) {
690 require(snprintf(str, buflen, "PMBR") < buflen); 698 require(snprintf(str, buflen, "PMBR") < buflen);
691 } else { 699 } else {
692 GuidToStr(&pmbr->boot_guid, buf, sizeof(buf)); 700 GuidToStr(&pmbr->boot_guid, buf, sizeof(buf));
693 require(snprintf(str, buflen, "PMBR (Boot GUID: %s)", buf) < buflen); 701 require(snprintf(str, buflen, "PMBR (Boot GUID: %s)", buf) < buflen);
694 } 702 }
695 } 703 }
696
OLDNEW
« no previous file with comments | « no previous file | cgpt/cmd_add.c » ('j') | cgpt/cmd_add.c » ('J')

Powered by Google App Engine
This is Rietveld 408576698