| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef VBOOT_REFERENCE_CGPT_H_ |
| 7 |
#define VBOOT_REFERENCE_CGPT_H_ |
| 8 |
|
| 9 |
#include <fcntl.h> |
| 10 |
#if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__) |
| 11 |
#include <features.h> |
| 12 |
#endif |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include "cgpt_endian.h" |
| 17 |
#include "cgptlib.h" |
| 18 |
#include "gpt.h" |
| 19 |
|
| 20 |
struct legacy_partition { |
| 21 |
uint8_t status; |
| 22 |
uint8_t f_head; |
| 23 |
uint8_t f_sect; |
| 24 |
uint8_t f_cyl; |
| 25 |
uint8_t type; |
| 26 |
uint8_t l_head; |
| 27 |
uint8_t l_sect; |
| 28 |
uint8_t l_cyl; |
| 29 |
uint32_t f_lba; |
| 30 |
uint32_t num_sect; |
| 31 |
} __attribute__((packed)); |
| 32 |
|
| 33 |
|
| 34 |
struct pmbr { |
| 35 |
uint8_t bootcode[424]; |
| 36 |
Guid boot_guid; |
| 37 |
uint32_t disk_id; |
| 38 |
uint8_t magic[2]; |
| 39 |
struct legacy_partition part[4]; |
| 40 |
uint8_t sig[2]; |
| 41 |
} __attribute__((packed)); |
| 42 |
|
| 43 |
void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen); |
| 44 |
|
| 45 |
|
| 46 |
struct drive { |
| 47 |
uint64_t size; |
| 48 |
GptData gpt; |
| 49 |
struct pmbr pmbr; |
| 50 |
int fd; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int DriveOpen(const char *drive_path, struct drive *drive, int mode, |
| 63 |
uint64_t drive_size); |
| 64 |
int DriveClose(struct drive *drive, int update_as_needed); |
| 65 |
int CheckValid(const struct drive *drive); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
int Load(struct drive *drive, uint8_t *buf, |
| 78 |
const uint64_t sector, |
| 79 |
const uint64_t sector_bytes, |
| 80 |
const uint64_t sector_count); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
int Save(struct drive *drive, const uint8_t *buf, |
| 93 |
const uint64_t sector, |
| 94 |
const uint64_t sector_bytes, |
| 95 |
const uint64_t sector_count); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
extern const Guid guid_chromeos_firmware; |
| 100 |
extern const Guid guid_chromeos_kernel; |
| 101 |
extern const Guid guid_chromeos_rootfs; |
| 102 |
extern const Guid guid_linux_data; |
| 103 |
extern const Guid guid_chromeos_reserved; |
| 104 |
extern const Guid guid_efi; |
| 105 |
extern const Guid guid_unused; |
| 106 |
|
| 107 |
int ReadPMBR(struct drive *drive); |
| 108 |
int WritePMBR(struct drive *drive); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput, |
| 118 |
uint8_t *utf8, unsigned int maxoutput); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput); |
| 128 |
|
| 129 |
|
| 130 |
int ResolveType(const Guid *type, char *buf); |
| 131 |
int SupportedType(const char *name, Guid *type); |
| 132 |
void PrintTypes(void); |
| 133 |
void EntryDetails(GptEntry *entry, uint32_t index, int raw); |
| 134 |
|
| 135 |
uint32_t GetNumberOfEntries(const struct drive *drive); |
| 136 |
GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index); |
| 137 |
|
| 138 |
void SetRequired(struct drive *drive, int secondary, uint32_t entry_index, |
| 139 |
int required); |
| 140 |
int GetRequired(struct drive *drive, int secondary, uint32_t entry_index); |
| 141 |
void SetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index, |
| 142 |
int legacy_boot); |
| 143 |
int GetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index); |
| 144 |
void SetPriority(struct drive *drive, int secondary, uint32_t entry_index, |
| 145 |
int priority); |
| 146 |
int GetPriority(struct drive *drive, int secondary, uint32_t entry_index); |
| 147 |
void SetTries(struct drive *drive, int secondary, uint32_t entry_index, |
| 148 |
int tries); |
| 149 |
int GetTries(struct drive *drive, int secondary, uint32_t entry_index); |
| 150 |
void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index, |
| 151 |
int success); |
| 152 |
int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index); |
| 153 |
void SetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index, |
| 154 |
int error_counter); |
| 155 |
int GetErrorCounter(struct drive *drive, int secondary, uint32_t entry_index); |
| 156 |
|
| 157 |
void SetRaw(struct drive *drive, int secondary, uint32_t entry_index, |
| 158 |
uint32_t raw); |
| 159 |
|
| 160 |
void UpdateAllEntries(struct drive *drive); |
| 161 |
|
| 162 |
uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers); |
| 163 |
uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries); |
| 164 |
void UpdateCrc(GptData *gpt); |
| 165 |
int IsSynonymous(const GptHeader* a, const GptHeader* b); |
| 166 |
|
| 167 |
int IsUnused(struct drive *drive, int secondary, uint32_t index); |
| 168 |
int IsKernel(struct drive *drive, int secondary, uint32_t index); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
int GenerateGuid(Guid *newguid); |
| 179 |
|
| 180 |
|
| 181 |
void Error(const char *format, ...); |
| 182 |
void Warning(const char *format, ...); |
| 183 |
|
| 184 |
|
| 185 |
int check_int_parse(char option, const char *buf); |
| 186 |
int check_int_limit(char option, int val, int low, int high); |
| 187 |
int cmd_show(int argc, char *argv[]); |
| 188 |
int cmd_repair(int argc, char *argv[]); |
| 189 |
int cmd_create(int argc, char *argv[]); |
| 190 |
int cmd_add(int argc, char *argv[]); |
| 191 |
int cmd_boot(int argc, char *argv[]); |
| 192 |
int cmd_find(int argc, char *argv[]); |
| 193 |
int cmd_edit(int argc, char *argv[]); |
| 194 |
int cmd_prioritize(int argc, char *argv[]); |
| 195 |
int cmd_legacy(int argc, char *argv[]); |
| 196 |
|
| 197 |
#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) |
| 198 |
const char *GptError(int errnum); |
| 199 |
|
| 200 |
|
| 201 |
#define GPT_PARTNAME_LEN 72 |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
#define require(A) do { \ |
| 206 |
if (!(A)) { \ |
| 207 |
fprintf(stderr, "condition (%s) failed at %s:%d\n", \ |
| 208 |
#A, __FILE__, __LINE__); \ |
| 209 |
exit(1); } \ |
| 210 |
} while (0) |
| 211 |
|
| 212 |
#endif |