1 /* Copyright 2013 The ChromiumOS Authors 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #ifndef VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_ 7 #define VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_ 8 9 #include "2sysincludes.h" 10 #include "cgptlib.h" 11 #include "gpt.h" 12 13 /* 14 * If gpt->current_kernel is this value, means either: 15 * 1. an initial value before scanning GPT entries, 16 * 2. after scanning, no any valid kernel is found. 17 */ 18 #define CGPT_KERNEL_ENTRY_NOT_FOUND (-1) 19 20 /* 21 * Bit definitions and masks for GPT attributes. 22 * 23 * 63-61 -- (reserved) 24 * 60 -- read-only 25 * 59-58 -- (reserved) 26 * 57 -- error counter 27 * 56 -- success 28 * 55-52 -- tries 29 * 51-48 -- priority 30 * 47-3 -- UEFI: reserved for future use 31 * 2 -- UEFI: Legacy BIOS bootable 32 * 1 -- UEFI: partition is not mapped 33 * 0 -- UEFI: partition is required 34 */ 35 #define CGPT_ATTRIBUTE_ERROR_COUNTER_OFFSET (57 - 48) 36 #define CGPT_ATTRIBUTE_MAX_ERROR_COUNTER (1ULL) 37 #define CGPT_ATTRIBUTE_ERROR_COUNTER_MASK (CGPT_ATTRIBUTE_MAX_ERROR_COUNTER << \ 38 CGPT_ATTRIBUTE_ERROR_COUNTER_OFFSET) 39 40 #define CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET (56 - 48) 41 #define CGPT_ATTRIBUTE_MAX_SUCCESSFUL (1ULL) 42 #define CGPT_ATTRIBUTE_SUCCESSFUL_MASK (CGPT_ATTRIBUTE_MAX_SUCCESSFUL << \ 43 CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET) 44 45 #define CGPT_ATTRIBUTE_TRIES_OFFSET (52 - 48) 46 #define CGPT_ATTRIBUTE_MAX_TRIES (15ULL) 47 #define CGPT_ATTRIBUTE_TRIES_MASK (CGPT_ATTRIBUTE_MAX_TRIES << \ 48 CGPT_ATTRIBUTE_TRIES_OFFSET) 49 50 #define CGPT_ATTRIBUTE_PRIORITY_OFFSET (48 - 48) 51 #define CGPT_ATTRIBUTE_MAX_PRIORITY (15ULL) 52 #define CGPT_ATTRIBUTE_PRIORITY_MASK (CGPT_ATTRIBUTE_MAX_PRIORITY << \ 53 CGPT_ATTRIBUTE_PRIORITY_OFFSET) 54 55 #define CGPT_ATTRIBUTE_REQUIRED_OFFSET (0) 56 #define CGPT_ATTRIBUTE_MAX_REQUIRED (1ULL) 57 #define CGPT_ATTRIBUTE_LEGACY_BOOT_OFFSET (2) 58 #define CGPT_ATTRIBUTE_MAX_LEGACY_BOOT (1ULL) 59 60 /* Defines ChromeOS-specific limitation on GPT */ 61 #define MIN_SIZE_OF_HEADER 92 62 #define MAX_SIZE_OF_HEADER 512 63 #define MIN_SIZE_OF_ENTRY 128 64 #define MAX_SIZE_OF_ENTRY 512 65 #define SIZE_OF_ENTRY_MULTIPLE 8 66 #define MIN_NUMBER_OF_ENTRIES 16 67 #define MAX_NUMBER_OF_ENTRIES 128 68 69 /* All GptData.(primary|secondary)_entries must be allocated to this size! */ 70 #define GPT_ENTRIES_ALLOC_SIZE (MAX_NUMBER_OF_ENTRIES * sizeof(GptEntry)) 71 72 /* Defines GPT sizes */ 73 #define GPT_PMBR_SECTORS 1 /* size (in sectors) of PMBR */ 74 #define GPT_HEADER_SECTORS 1 75 76 /* 77 * Alias name of index in internal array for primary and secondary header and 78 * entries. 79 */ 80 enum { 81 /* constants for index */ 82 PRIMARY = 0, 83 SECONDARY = 1, 84 ANY_VALID = 9999, /* accept any between primary and secondary */ 85 86 /* constants for bit mask */ 87 MASK_NONE = 0, 88 MASK_PRIMARY = 1, 89 MASK_SECONDARY = 2, 90 MASK_BOTH = 3, 91 }; 92 93 /** 94 * Verify GptData parameters are valid. 95 */ 96 int CheckParameters(GptData* gpt); 97 98 /** 99 * Check header fields. 100 * 101 * Returns 0 if header is valid, 1 if invalid. 102 */ 103 int CheckHeader(GptHeader *h, int is_secondary, 104 uint64_t streaming_drive_sectors, 105 uint64_t gpt_drive_sectors, uint32_t flags, 106 uint32_t sector_bytes); 107 108 /** 109 * Calculate and return the header CRC. 110 */ 111 uint32_t HeaderCrc(GptHeader *h); 112 113 /** 114 * Check entries. 115 * 116 * Returns 0 if entries are valid, 1 if invalid. 117 */ 118 int CheckEntries(GptEntry *entries, GptHeader *h); 119 120 /** 121 * Return 0 if the GptHeaders are the same for all fields which don't differ 122 * between the primary and secondary headers - that is, all fields other than: 123 * 124 * my_lba 125 * alternate_lba 126 * entries_lba 127 */ 128 int HeaderFieldsSame(GptHeader *h1, GptHeader *h2); 129 130 /** 131 * Check GptData, headers, entries. 132 * 133 * If successful, sets gpt->valid_headers and gpt->valid_entries and returns 134 * GPT_SUCCESS. 135 * 136 * On error, returns a GPT_ERROR_* return code. 137 */ 138 int GptValidityCheck(GptData *gpt); 139 140 /** 141 * Repair GPT data by copying from one set of valid headers/entries to the 142 * other. Assumes GptValidityCheck() has been run to determine which headers 143 * and/or entries are already valid. 144 * 145 * The caller must make sure that even if one of the entries table is invalid 146 * then corresponding buffer is allocated and big enough to accommodate entries 147 * from the other (good) table. 148 */ 149 void GptRepair(GptData *gpt); 150 151 /** 152 * Called when the primary entries are modified and the CRCs need to be 153 * recalculated and propagated to the secondary entries 154 */ 155 void GptModified(GptData *gpt); 156 157 /** 158 * Return 1 if the entry is a Chrome OS kernel partition, else 0. 159 */ 160 int IsKernelEntry(const GptEntry *e); 161 162 /** 163 * Copy the current kernel partition's UniquePartitionGuid to the dest. 164 */ 165 void GetCurrentKernelUniqueGuid(GptData *gpt, void *dest); 166 167 /** 168 * Return a pointer to text describing the passed in error. 169 */ 170 const char *GptErrorText(int error_code); 171 172 /** 173 * Return number of sectors required to store the entries table. Where 174 * a sector has size sector_bytes. 175 */ 176 size_t CalculateEntriesSectors(GptHeader* h, uint32_t sector_bytes); 177 178 #endif /* VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_ */ 179