| 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 #ifndef VBOOT_REFERENCE_CGPTLIB_H_ | 6 #ifndef VBOOT_REFERENCE_CGPTLIB_H_ |
| 7 #define VBOOT_REFERENCE_CGPTLIB_H_ | 7 #define VBOOT_REFERENCE_CGPTLIB_H_ |
| 8 | 8 |
| 9 #include "gpt.h" | |
| 10 #include <stdint.h> | 9 #include <stdint.h> |
| 11 | 10 |
| 12 enum { | 11 enum { |
| 13 GPT_SUCCESS = 0, | 12 GPT_SUCCESS = 0, |
| 14 GPT_ERROR_NO_VALID_KERNEL, | 13 GPT_ERROR_NO_VALID_KERNEL, |
| 15 GPT_ERROR_INVALID_HEADERS, | 14 GPT_ERROR_INVALID_HEADERS, |
| 16 GPT_ERROR_INVALID_ENTRIES, | 15 GPT_ERROR_INVALID_ENTRIES, |
| 17 GPT_ERROR_INVALID_SECTOR_SIZE, | 16 GPT_ERROR_INVALID_SECTOR_SIZE, |
| 18 GPT_ERROR_INVALID_SECTOR_NUMBER, | 17 GPT_ERROR_INVALID_SECTOR_NUMBER, |
| 19 GPT_ERROR_INVALID_UPDATE_TYPE, | 18 GPT_ERROR_INVALID_UPDATE_TYPE, |
| 20 }; | 19 }; |
| 21 | 20 |
| 22 const char *GptError(int errno); | |
| 23 | |
| 24 /* Bit masks for GptData.modified field. */ | 21 /* Bit masks for GptData.modified field. */ |
| 25 #define GPT_MODIFIED_HEADER1 0x01 | 22 #define GPT_MODIFIED_HEADER1 0x01 |
| 26 #define GPT_MODIFIED_HEADER2 0x02 | 23 #define GPT_MODIFIED_HEADER2 0x02 |
| 27 #define GPT_MODIFIED_ENTRIES1 0x04 | 24 #define GPT_MODIFIED_ENTRIES1 0x04 |
| 28 #define GPT_MODIFIED_ENTRIES2 0x08 | 25 #define GPT_MODIFIED_ENTRIES2 0x08 |
| 29 | 26 |
| 30 /* The 'update_type' of GptUpdateKernelEntry() | 27 /* The 'update_type' of GptUpdateKernelEntry() |
| 31 * We expose TRY and BAD only because those are what verified boot needs. | 28 * We expose TRY and BAD only because those are what verified boot needs. |
| 32 * For more precise control on GPT attribute bits, please refer to | 29 * For more precise control on GPT attribute bits, please refer to |
| 33 * gpt_internal.h */ | 30 * gpt_internal.h */ |
| 34 enum { | 31 enum { |
| 35 GPT_UPDATE_ENTRY_TRY = 1, | 32 GPT_UPDATE_ENTRY_TRY = 1, |
| 36 /* System will be trying to boot the currently selected kernel partition. | 33 /* System will be trying to boot the currently selected kernel partition. |
| 37 * Update its try count if necessary. */ | 34 * Update its try count if necessary. */ |
| 38 GPT_UPDATE_ENTRY_BAD = 2, | 35 GPT_UPDATE_ENTRY_BAD = 2, |
| 39 /* The currently selected kernel partition failed validation. Mark entry as | 36 /* The currently selected kernel partition failed validation. Mark entry as |
| 40 * invalid. */ | 37 * invalid. */ |
| 41 }; | 38 }; |
| 42 | 39 |
| 43 /* Defines ChromeOS-specific limitation on GPT */ | |
| 44 #define MIN_SIZE_OF_HEADER 92 | |
| 45 #define MAX_SIZE_OF_HEADER 512 | |
| 46 #define MIN_SIZE_OF_ENTRY 128 | |
| 47 #define MAX_SIZE_OF_ENTRY 512 | |
| 48 #define SIZE_OF_ENTRY_MULTIPLE 8 | |
| 49 #define MIN_NUMBER_OF_ENTRIES 32 | |
| 50 #define MAX_NUMBER_OF_ENTRIES 512 | |
| 51 #define TOTAL_ENTRIES_SIZE 16384 /* usual case is 128 bytes * 128 entries */ | |
| 52 | |
| 53 /* Defines GPT sizes */ | |
| 54 #define GPT_PMBR_SECTOR 1 /* size (in sectors) of PMBR */ | |
| 55 #define GPT_HEADER_SECTOR 1 | |
| 56 #define GPT_ENTRIES_SECTORS 32 /* assume sector size if 512 bytes, then | |
| 57 * (TOTAL_ENTRIES_SIZE / 512) = 32 */ | |
| 58 | |
| 59 /* alias name of index in internal array for primary and secondary header and | |
| 60 * entries. */ | |
| 61 enum { | |
| 62 PRIMARY = 0, | |
| 63 SECONDARY = 1, | |
| 64 MASK_NONE = 0, | |
| 65 MASK_PRIMARY = 1, | |
| 66 MASK_SECONDARY = 2, | |
| 67 MASK_BOTH = 3, | |
| 68 }; | |
| 69 | |
| 70 typedef struct { | 40 typedef struct { |
| 71 /* Fill in the following fields before calling GptInit() */ | 41 /* Fill in the following fields before calling GptInit() */ |
| 72 uint8_t *primary_header; /* GPT primary header, from sector 1 of disk | 42 uint8_t *primary_header; /* GPT primary header, from sector 1 of disk |
| 73 * (size: 512 bytes) */ | 43 * (size: 512 bytes) */ |
| 74 uint8_t *secondary_header; /* GPT secondary header, from last sector of | 44 uint8_t *secondary_header; /* GPT secondary header, from last sector of |
| 75 * disk (size: 512 bytes) */ | 45 * disk (size: 512 bytes) */ |
| 76 uint8_t *primary_entries; /* primary GPT table, follows primary header | 46 uint8_t *primary_entries; /* primary GPT table, follows primary header |
| 77 * (size: 16 KB) */ | 47 * (size: 16 KB) */ |
| 78 uint8_t *secondary_entries; /* secondary GPT table, precedes secondary | 48 uint8_t *secondary_entries; /* secondary GPT table, precedes secondary |
| 79 * header (size: 16 KB) */ | 49 * header (size: 16 KB) */ |
| 80 uint32_t sector_bytes; /* Size of a LBA sector, in bytes */ | 50 uint32_t sector_bytes; /* Size of a LBA sector, in bytes */ |
| 81 uint64_t drive_sectors; /* Size of drive in LBA sectors, in sectors */ | 51 uint64_t drive_sectors; /* Size of drive in LBA sectors, in sectors */ |
| 82 | 52 |
| 83 /* Outputs */ | 53 /* Outputs */ |
| 84 uint8_t modified; /* Which inputs have been modified? | 54 uint8_t modified; /* Which inputs have been modified? |
| 85 * 0x01 = header1 | 55 * 0x01 = header1 |
| 86 * 0x02 = header2 | 56 * 0x02 = header2 |
| 87 * 0x04 = table1 | 57 * 0x04 = table1 |
| 88 * 0x08 = table2 */ | 58 * 0x08 = table2 */ |
| 89 int current_kernel; /* the current chromeos kernel index in partition table. | 59 int current_kernel; /* the current chromeos kernel index in partition table. |
| 90 * -1 means not found on drive. */ | 60 * -1 means not found on drive. */ |
| 91 | 61 |
| 92 /* Internal variables */ | 62 /* Internal variables */ |
| 93 uint32_t valid_headers, valid_entries; | 63 uint32_t valid_headers, valid_entries; |
| 64 int current_priority; |
| 94 } GptData; | 65 } GptData; |
| 95 | 66 |
| 96 int GptInit(GptData *gpt); | 67 int GptInit(GptData* gpt); |
| 97 /* Initializes the GPT data structure's internal state. The following fields | 68 /* Initializes the GPT data structure's internal state. The following fields |
| 98 * must be filled before calling this function: | 69 * must be filled before calling this function: |
| 99 * | 70 * |
| 100 * primary_header | 71 * primary_header |
| 101 * secondary_header | 72 * secondary_header |
| 102 * primary_entries | 73 * primary_entries |
| 103 * secondary_entries | 74 * secondary_entries |
| 104 * sector_bytes | 75 * sector_bytes |
| 105 * drive_sectors | 76 * drive_sectors |
| 106 * | 77 * |
| 107 * On return the modified field may be set, if the GPT data has been modified | 78 * On return the modified field may be set, if the GPT data has been modified |
| 108 * and should be written to disk. | 79 * and should be written to disk. |
| 109 * | 80 * |
| 110 * Returns GPT_SUCCESS if successful, non-zero if error: | 81 * Returns GPT_SUCCESS if successful, non-zero if error: |
| 111 * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters | 82 * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters |
| 112 * recovery mode, | 83 * recovery mode, |
| 113 * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters | 84 * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters |
| 114 * recovery mode, | 85 * recovery mode, |
| 115 * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported, | 86 * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported, |
| 116 * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too | 87 * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too |
| 117 * small) */ | 88 * small) */ |
| 118 | 89 |
| 119 int GptNextKernelEntry(GptData *gpt, uint64_t *start_sector, uint64_t *size); | 90 int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size); |
| 120 /* Provides the location of the next kernel partition, in order of decreasing | 91 /* Provides the location of the next kernel partition, in order of decreasing |
| 121 * priority. On return the start_sector parameter contains the LBA sector | 92 * priority. On return the start_sector parameter contains the LBA sector |
| 122 * for the start of the kernel partition, and the size parameter contains the | 93 * for the start of the kernel partition, and the size parameter contains the |
| 123 * size of the kernel partition in LBA sectors. gpt.current_kernel contains | 94 * size of the kernel partition in LBA sectors. gpt.current_kernel contains |
| 124 * the partition index of the current chromeos kernel partition. | 95 * the partition index of the current chromeos kernel partition. |
| 125 * | 96 * |
| 126 * Returns GPT_SUCCESS if successful, else | 97 * Returns GPT_SUCCESS if successful, else |
| 127 * GPT_ERROR_NO_VALID_KERNEL, no avaliable kernel, enters recovery mode */ | 98 * GPT_ERROR_NO_VALID_KERNEL, no avaliable kernel, enters recovery mode */ |
| 128 | 99 |
| 129 int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type); | 100 int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type); |
| 130 /* Updates the kernel entry with the specified index, using the specified type | 101 /* Updates the kernel entry with the specified index, using the specified type |
| 131 * of update (GPT_UPDATE_ENTRY_*). | 102 * of update (GPT_UPDATE_ENTRY_*). |
| 132 * | 103 * |
| 133 * On return the modified field may be set, if the GPT data has been modified | 104 * On return the modified field may be set, if the GPT data has been modified |
| 134 * and should be written to disk. | 105 * and should be written to disk. |
| 135 * | 106 * |
| 136 * Returns GPT_SUCCESS if successful, else | 107 * Returns GPT_SUCCESS if successful, else |
| 137 * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given. | 108 * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given. |
| 138 */ | 109 */ |
| 139 | 110 |
| 140 #endif /* VBOOT_REFERENCE_CGPTLIB_H_ */ | 111 #endif /* VBOOT_REFERENCE_CGPTLIB_H_ */ |
| OLD | NEW |