1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 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 "sysincludes.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-57 -- (reserved) 26 * 56 -- success 27 * 55-52 -- tries 28 * 51-48 -- priority 29 * 47-2 -- UEFI: reserved for future use 30 * 1 -- UEFI: partition is not mapped 31 * 0 -- UEFI: partition is required 32 */ 33 #define CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET (56 - 48) 34 #define CGPT_ATTRIBUTE_MAX_SUCCESSFUL (1ULL) 35 #define CGPT_ATTRIBUTE_SUCCESSFUL_MASK (CGPT_ATTRIBUTE_MAX_SUCCESSFUL << \ 36 CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET) 37 38 #define CGPT_ATTRIBUTE_TRIES_OFFSET (52 - 48) 39 #define CGPT_ATTRIBUTE_MAX_TRIES (15ULL) 40 #define CGPT_ATTRIBUTE_TRIES_MASK (CGPT_ATTRIBUTE_MAX_TRIES << \ 41 CGPT_ATTRIBUTE_TRIES_OFFSET) 42 43 #define CGPT_ATTRIBUTE_PRIORITY_OFFSET (48 - 48) 44 #define CGPT_ATTRIBUTE_MAX_PRIORITY (15ULL) 45 #define CGPT_ATTRIBUTE_PRIORITY_MASK (CGPT_ATTRIBUTE_MAX_PRIORITY << \ 46 CGPT_ATTRIBUTE_PRIORITY_OFFSET) 47 48 /* Defines ChromeOS-specific limitation on GPT */ 49 #define MIN_SIZE_OF_HEADER 92 50 #define MAX_SIZE_OF_HEADER 512 51 #define MIN_SIZE_OF_ENTRY 128 52 #define MAX_SIZE_OF_ENTRY 512 53 #define SIZE_OF_ENTRY_MULTIPLE 8 54 #define MIN_NUMBER_OF_ENTRIES 16 55 #define MAX_NUMBER_OF_ENTRIES 128 56 57 /* Defines GPT sizes */ 58 #define GPT_PMBR_SECTORS 1 /* size (in sectors) of PMBR */ 59 #define GPT_HEADER_SECTORS 1 60 61 /* 62 * Alias name of index in internal array for primary and secondary header and 63 * entries. 64 */ 65 enum { 66 /* constants for index */ 67 PRIMARY = 0, 68 SECONDARY = 1, 69 ANY_VALID = 9999, /* accept any between primary and secondary */ 70 71 /* constants for bit mask */ 72 MASK_NONE = 0, 73 MASK_PRIMARY = 1, 74 MASK_SECONDARY = 2, 75 MASK_BOTH = 3, 76 }; 77 78 /** 79 * Verify GptData parameters are sane. 80 */ 81 int CheckParameters(GptData* gpt); 82 83 /** 84 * Check header fields. 85 * 86 * Returns 0 if header is valid, 1 if invalid. 87 */ 88 int CheckHeader(GptHeader *h, int is_secondary, 89 uint64_t streaming_drive_sectors, 90 uint64_t gpt_drive_sectors, uint32_t flags); 91 92 /** 93 * Calculate and return the header CRC. 94 */ 95 uint32_t HeaderCrc(GptHeader *h); 96 97 /** 98 * Check entries. 99 * 100 * Returns 0 if entries are valid, 1 if invalid. 101 */ 102 int CheckEntries(GptEntry *entries, GptHeader *h); 103 104 /** 105 * Return 0 if the GptHeaders are the same for all fields which don't differ 106 * between the primary and secondary headers - that is, all fields other than: 107 * 108 * my_lba 109 * alternate_lba 110 * entries_lba 111 */ 112 int HeaderFieldsSame(GptHeader *h1, GptHeader *h2); 113 114 /** 115 * Check GptData, headers, entries. 116 * 117 * If successful, sets gpt->valid_headers and gpt->valid_entries and returns 118 * GPT_SUCCESS. 119 * 120 * On error, returns a GPT_ERROR_* return code. 121 */ 122 int GptSanityCheck(GptData *gpt); 123 124 /** 125 * Repair GPT data by copying from one set of valid headers/entries to the 126 * other. Assumes GptSanityCheck() has been run to determine which headers 127 * and/or entries are already valid. 128 */ 129 void GptRepair(GptData *gpt); 130 131 /** 132 * Called when the primary entries are modified and the CRCs need to be 133 * recalculated and propagated to the secondary entries 134 */ 135 void GptModified(GptData *gpt); 136 137 /* Getters and setters for partition attribute fields. */ 138 139 int GetEntrySuccessful(const GptEntry *e); 140 int GetEntryPriority(const GptEntry *e); 141 int GetEntryTries(const GptEntry *e); 142 void SetEntrySuccessful(GptEntry *e, int successful); 143 void SetEntryPriority(GptEntry *e, int priority); 144 void SetEntryTries(GptEntry *e, int tries); 145 146 /** 147 * Return 1 if the entry is a Chrome OS kernel partition, else 0. 148 */ 149 int IsKernelEntry(const GptEntry *e); 150 151 /** 152 * Copy the current kernel partition's UniquePartitionGuid to the dest. 153 */ 154 void GetCurrentKernelUniqueGuid(GptData *gpt, void *dest); 155 156 /** 157 * Return a pointer to text describing the passed in error. 158 */ 159 const char *GptErrorText(int error_code); 160 161 /** 162 * Return number of 512-byte sectors required to store the entries table. 163 */ 164 size_t CalculateEntriesSectors(GptHeader* h); 165 166 #endif /* VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_ */ 167