OLD | NEW |
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 | 5 |
6 #include "cgptlib.h" | 6 #include "cgptlib.h" |
7 #include <string.h> | 7 #include <string.h> |
8 #include "cgptlib_internal.h" | 8 #include "cgptlib_internal.h" |
9 #include "crc32.h" | 9 #include "crc32.h" |
10 #include "gpt.h" | 10 #include "gpt.h" |
11 #include "quick_sort.h" | 11 #include "quick_sort.h" |
12 #include "utility.h" | 12 #include "utility.h" |
13 | 13 |
14 /* Macro to invalidate a GPT header/entries */ | 14 /* Macro to invalidate a GPT header/entries */ |
15 #define INVALIDATE_HEADER(valid_headers, index) \ | 15 #define INVALIDATE_HEADER(valid_headers, index) \ |
16 do { \ | 16 do { \ |
| 17 debug("- INVALIDATE_HEADER() at %s():%d\n", __FUNCTION__, __LINE__); \ |
17 valid_headers &= ~(1<<index); \ | 18 valid_headers &= ~(1<<index); \ |
18 } while (0) | 19 } while (0) |
19 #define INVALIDATE_ENTRIES(valid_entries, index) \ | 20 #define INVALIDATE_ENTRIES(valid_entries, index) \ |
20 do { \ | 21 do { \ |
| 22 debug("- INVALIDATE_ENTRIES() at %s():%d\n", __FUNCTION__, __LINE__); \ |
21 valid_entries &= ~(1<<index); \ | 23 valid_entries &= ~(1<<index); \ |
22 } while (0) | 24 } while (0) |
23 | 25 |
| 26 const char *GptError(int errno) { |
| 27 const char *error_string[] = { |
| 28 /* GPT_SUCCESS */ "Success", |
| 29 /* GPT_ERROR_NO_VALID_KERNEL */ "No valid kernel entry", |
| 30 /* GPT_ERROR_INVALID_HEADERS */ "Invalid headers", |
| 31 /* GPT_ERROR_INVALID_ENTRIES */ "Invalid entries", |
| 32 /* GPT_ERROR_INVALID_SECTOR_SIZE */ "Invalid sector size", |
| 33 /* GPT_ERROR_INVALID_SECTOR_NUMBER */ "Invalid sector number", |
| 34 /* GPT_ERROR_INVALID_UPDATE_TYPE */ "Invalid update type", |
| 35 }; |
| 36 return error_string[errno]; |
| 37 } |
| 38 |
24 /* Checks if sector_bytes and drive_sectors are valid values. */ | 39 /* Checks if sector_bytes and drive_sectors are valid values. */ |
25 int CheckParameters(GptData *gpt) { | 40 int CheckParameters(GptData *gpt) { |
26 /* Currently, we only support 512-byte sector. In the future, we may support | 41 /* Currently, we only support 512-byte sector. In the future, we may support |
27 * larger sector. */ | 42 * larger sector. */ |
28 if (gpt->sector_bytes != 512) | 43 if (gpt->sector_bytes != 512) |
29 return GPT_ERROR_INVALID_SECTOR_SIZE; | 44 return GPT_ERROR_INVALID_SECTOR_SIZE; |
30 | 45 |
31 /* The sector number of a drive should be reasonable. If the given value is | 46 /* The sector number of a drive should be reasonable. If the given value is |
32 * too small to contain basic GPT structure (PMBR + Headers + Entries), | 47 * too small to contain basic GPT structure (PMBR + Headers + Entries), |
33 * the value is wrong. */ | 48 * the value is wrong. */ |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 entry->attributes |= (uint64_t)tries << CGPT_ATTRIBUTE_TRIES_OFFSET; | 614 entry->attributes |= (uint64_t)tries << CGPT_ATTRIBUTE_TRIES_OFFSET; |
600 } | 615 } |
601 | 616 |
602 int GetTries(GptData *gpt, int secondary, int entry_index) { | 617 int GetTries(GptData *gpt, int secondary, int entry_index) { |
603 GptEntry *entry; | 618 GptEntry *entry; |
604 entry = GetEntry(gpt, secondary, entry_index); | 619 entry = GetEntry(gpt, secondary, entry_index); |
605 return (entry->attributes & CGPT_ATTRIBUTE_TRIES_MASK) >> | 620 return (entry->attributes & CGPT_ATTRIBUTE_TRIES_MASK) >> |
606 CGPT_ATTRIBUTE_TRIES_OFFSET; | 621 CGPT_ATTRIBUTE_TRIES_OFFSET; |
607 } | 622 } |
608 | 623 |
609 void SetSuccess(GptData *gpt, int secondary, int entry_index, int success) { | 624 void SetSuccessful(GptData *gpt, int secondary, int entry_index, int success) { |
610 GptEntry *entry; | 625 GptEntry *entry; |
611 entry = GetEntry(gpt, secondary, entry_index); | 626 entry = GetEntry(gpt, secondary, entry_index); |
612 | 627 |
613 assert(success >= 0 && success <= CGPT_ATTRIBUTE_MAX_SUCCESS); | 628 assert(success >= 0 && success <= CGPT_ATTRIBUTE_MAX_SUCCESSFUL); |
614 entry->attributes &= ~CGPT_ATTRIBUTE_SUCCESS_MASK; | 629 entry->attributes &= ~CGPT_ATTRIBUTE_SUCCESSFUL_MASK; |
615 entry->attributes |= (uint64_t)success << CGPT_ATTRIBUTE_SUCCESS_OFFSET; | 630 entry->attributes |= (uint64_t)success << CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET; |
616 } | 631 } |
617 | 632 |
618 int GetSuccess(GptData *gpt, int secondary, int entry_index) { | 633 int GetSuccessful(GptData *gpt, int secondary, int entry_index) { |
619 GptEntry *entry; | 634 GptEntry *entry; |
620 entry = GetEntry(gpt, secondary, entry_index); | 635 entry = GetEntry(gpt, secondary, entry_index); |
621 return (entry->attributes & CGPT_ATTRIBUTE_SUCCESS_MASK) >> | 636 return (entry->attributes & CGPT_ATTRIBUTE_SUCCESSFUL_MASK) >> |
622 CGPT_ATTRIBUTE_SUCCESS_OFFSET; | 637 CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET; |
623 } | 638 } |
624 | 639 |
| 640 uint32_t GetNumberOfEntries(const GptData *gpt) { |
| 641 GptHeader *header; |
| 642 header = (GptHeader*)gpt->primary_header; |
| 643 return header->number_of_entries; |
| 644 } |
| 645 |
| 646 |
625 /* Compare two priority values. Actually it is a circular priority, which is: | 647 /* Compare two priority values. Actually it is a circular priority, which is: |
626 * 3 > 2 > 1 > 0, but 0 > 3. (-1 means very low, and anyone is higher than -1) | 648 * 3 > 2 > 1 > 0, but 0 > 3. (-1 means very low, and anyone is higher than -1) |
627 * | 649 * |
628 * Return 1 if 'a' has higher priority than 'b'. | 650 * Return 1 if 'a' has higher priority than 'b'. |
629 */ | 651 */ |
630 int IsHigherPriority(int a, int b) { | 652 int IsHigherPriority(int a, int b) { |
631 if ((a == 0) && (b == CGPT_ATTRIBUTE_MAX_PRIORITY)) | 653 if ((a == 0) && (b == CGPT_ATTRIBUTE_MAX_PRIORITY)) |
632 return 1; | 654 return 1; |
633 else if ((a == CGPT_ATTRIBUTE_MAX_PRIORITY) && (b == 0)) | 655 else if ((a == CGPT_ATTRIBUTE_MAX_PRIORITY) && (b == 0)) |
634 return 0; | 656 return 0; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 RepairEntries(gpt, MASK_PRIMARY); | 759 RepairEntries(gpt, MASK_PRIMARY); |
738 /* Actually two entries are dirty now. | 760 /* Actually two entries are dirty now. |
739 * Also two headers are dirty because entries_crc32 has been updated. */ | 761 * Also two headers are dirty because entries_crc32 has been updated. */ |
740 gpt->modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | | 762 gpt->modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | |
741 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); | 763 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); |
742 UpdateCrc(gpt); | 764 UpdateCrc(gpt); |
743 } | 765 } |
744 | 766 |
745 return GPT_SUCCESS; | 767 return GPT_SUCCESS; |
746 } | 768 } |
OLD | NEW |