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_GPT_MISC_H_ 7 #define VBOOT_REFERENCE_GPT_MISC_H_ 8 9 #include "gpt.h" 10 #include "vboot_api.h" 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif /* __cplusplus */ 15 16 enum { 17 GPT_SUCCESS = 0, 18 GPT_ERROR_NO_VALID_KERNEL, 19 GPT_ERROR_INVALID_HEADERS, 20 GPT_ERROR_INVALID_ENTRIES, 21 GPT_ERROR_INVALID_SECTOR_SIZE, 22 GPT_ERROR_INVALID_SECTOR_NUMBER, 23 GPT_ERROR_INVALID_UPDATE_TYPE, 24 GPT_ERROR_CRC_CORRUPTED, 25 GPT_ERROR_OUT_OF_REGION, 26 GPT_ERROR_START_LBA_OVERLAP, 27 GPT_ERROR_END_LBA_OVERLAP, 28 GPT_ERROR_DUP_GUID, 29 GPT_ERROR_INVALID_FLASH_GEOMETRY, 30 GPT_ERROR_NO_SUCH_ENTRY, 31 /* Number of errors */ 32 GPT_ERROR_COUNT 33 }; 34 35 /* Bit masks for GptData.modified field. */ 36 #define GPT_MODIFIED_HEADER1 0x01 37 #define GPT_MODIFIED_HEADER2 0x02 38 #define GPT_MODIFIED_ENTRIES1 0x04 39 #define GPT_MODIFIED_ENTRIES2 0x08 40 41 /* 42 * The 'update_type' of GptUpdateKernelEntry(). We expose TRY and BAD only 43 * because those are what verified boot needs. For more precise control on GPT 44 * attribute bits, please refer to gpt_internal.h. 45 */ 46 enum { 47 /* 48 * System will be trying to boot the currently selected kernel 49 * partition. Update its try count if necessary. 50 */ 51 GPT_UPDATE_ENTRY_TRY = 1, 52 /* 53 * The currently selected kernel partition failed validation. Mark 54 * entry as invalid. 55 */ 56 GPT_UPDATE_ENTRY_BAD = 2, 57 /* 58 * Used for fastboot mode. If kernel partition slot is marked active, 59 * its GPT entry is marked with S1,P2,T0. 60 */ 61 GPT_UPDATE_ENTRY_ACTIVE = 3, 62 /* 63 * Used for fastboot mode. If kernel partition slot is marked invalid, 64 * its GPT entry is marked with S0,P0,T0. 65 */ 66 GPT_UPDATE_ENTRY_INVALID = 4, 67 }; 68 69 /* If this bit is 1, the GPT is stored in another from the streaming data */ 70 #define GPT_FLAG_EXTERNAL 0x1 71 72 /* 73 * A note about stored_on_device and gpt_drive_sectors: 74 * 75 * This code is used by both the "cgpt" utility and depthcharge/vboot. ATM, 76 * depthcharge does not have logic to properly setup stored_on_device and 77 * gpt_drive_sectors, but it does do a memset(gpt, 0, sizeof(GptData)). And so, 78 * GPT_STORED_ON_DEVICE should be 0 to make stored_on_device compatible with 79 * present behavior. At the same time, in vb2api_load_kernel() and GptLoad(), 80 * we need to have simple shims to set gpt_drive_sectors to drive_sectors. 81 * 82 * TODO(namnguyen): Remove those shims when the firmware can set these fields. 83 */ 84 typedef struct { 85 /* Fill in the following fields before calling GptInit() */ 86 /* GPT primary header, from sector 1 of disk (size: 512 bytes) */ 87 uint8_t *primary_header; 88 /* GPT secondary header, from last sector of disk (size: 512 bytes) */ 89 uint8_t *secondary_header; 90 /* Primary GPT table, follows primary header */ 91 uint8_t *primary_entries; 92 /* Secondary GPT table, precedes secondary header */ 93 uint8_t *secondary_entries; 94 /* Size of a LBA sector, in bytes */ 95 uint32_t sector_bytes; 96 /* Size of drive (that the partitions are on) in LBA sectors */ 97 uint64_t streaming_drive_sectors; 98 /* Size of the device that holds the GPT structures, 512-byte sectors */ 99 uint64_t gpt_drive_sectors; 100 /* Flags */ 101 uint32_t flags; 102 103 /* Outputs */ 104 /* Which inputs have been modified? GPT_MODIFIED_* */ 105 uint8_t modified; 106 /* 107 * The current chromeos kernel index in partition table. -1 means not 108 * found on drive. Note that GPT partition numbers are traditionally 109 * 1-based, but we're using a zero-based index here. 110 */ 111 int current_kernel; 112 113 /* Internal variables */ 114 uint8_t valid_headers, valid_entries, ignored; 115 int current_priority; 116 } GptData; 117 118 /** 119 * Initializes the GPT data structure's internal state. 120 * 121 * The following fields must be filled before calling this function: 122 * 123 * primary_header 124 * secondary_header 125 * primary_entries 126 * secondary_entries 127 * sector_bytes 128 * drive_sectors 129 * stored_on_device 130 * gpt_device_sectors 131 * 132 * On return the modified field may be set, if the GPT data has been modified 133 * and should be written to disk. 134 * 135 * Returns GPT_SUCCESS if successful, non-zero if error: 136 * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters 137 * recovery mode, 138 * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters 139 * recovery mode, 140 * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported, 141 * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too 142 * small) */ 143 int GptInit(GptData *gpt); 144 145 /** 146 * Return the nth instance of parition entry matching the partition type guid 147 * from the gpt table. Instance value starts from 0. If the entry is not found, 148 * it returns NULL. 149 */ 150 GptEntry *GptFindNthEntry(GptData *gpt, const Guid *guid, unsigned int n); 151 152 /** 153 * Allocate and read GPT data from the drive. The sector_bytes and 154 * drive_sectors fields should be filled on input. The primary and secondary 155 * header and entries are filled on output. 156 * 157 * Returns 0 if successful, 1 if error. 158 */ 159 int AllocAndReadGptData(vb2ex_disk_handle_t disk_handle, GptData *gptdata); 160 161 /** 162 * Write any changes for the GPT data back to the drive, then free the buffers. 163 */ 164 int WriteAndFreeGptData(vb2ex_disk_handle_t disk_handle, GptData *gptdata); 165 166 /** 167 * Return 1 if the entry is unused, 0 if it is used. 168 */ 169 int IsUnusedEntry(const GptEntry *e); 170 171 /** 172 * Return size(in lba) of a partition represented by given GPT entry. 173 */ 174 uint64_t GptGetEntrySizeLba(const GptEntry *e); 175 176 /** 177 * Return size(in bytes) of a partition represented by given GPT entry. 178 */ 179 uint64_t GptGetEntrySizeBytes(const GptData *gpt, const GptEntry *e); 180 181 /** 182 * Updates the kernel entry with the specified index, using the specified type 183 * of update (GPT_UPDATE_ENTRY_*). 184 * 185 * On return the modified field may be set, if the GPT data has been modified 186 * and should be written to disk. 187 * 188 * Returns GPT_SUCCESS if successful, else 189 * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given. 190 */ 191 int GptUpdateKernelWithEntry(GptData *gpt, GptEntry *e, uint32_t update_type); 192 193 /** 194 * Updates the kernel entry identified by current_kernel field. If 195 * current_kernel is not set it returns an error. 196 * 197 * Returns GPT_SUCCESS if successful, else 198 * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given. 199 */ 200 int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type); 201 202 /* Getters and setters for partition attribute fields. */ 203 204 int GetEntryRequired(const GptEntry *e); 205 int GetEntryLegacyBoot(const GptEntry *e); 206 int GetEntrySuccessful(const GptEntry *e); 207 int GetEntryPriority(const GptEntry *e); 208 int GetEntryTries(const GptEntry *e); 209 int GetEntryErrorCounter(const GptEntry *e); 210 void SetEntryRequired(GptEntry *e, int required); 211 void SetEntryLegacyBoot(GptEntry *e, int legacy_boot); 212 void SetEntrySuccessful(GptEntry *e, int successful); 213 void SetEntryPriority(GptEntry *e, int priority); 214 void SetEntryTries(GptEntry *e, int tries); 215 void SetEntryErrorCounter(GptEntry *e, int error_counter); 216 217 #ifdef __cplusplus 218 } 219 #endif /* __cplusplus */ 220 221 #endif /* VBOOT_REFERENCE_GPT_MISC_H_ */ 222