Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
| 6 | #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
| 7 | |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 9 | #include <features.h> |
| 10 | #include <stdint.h> |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 13 | #include "endian.h" |
| 14 | #include "gpt.h" |
| 15 | #include "cgptlib.h" |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 16 | #include "mtdlib.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 17 | |
| 18 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 19 | struct legacy_partition { |
| 20 | uint8_t status; |
| 21 | uint8_t f_head; |
| 22 | uint8_t f_sect; |
| 23 | uint8_t f_cyl; |
| 24 | uint8_t type; |
| 25 | uint8_t l_head; |
| 26 | uint8_t l_sect; |
| 27 | uint8_t l_cyl; |
| 28 | uint32_t f_lba; |
| 29 | uint32_t num_sect; |
| 30 | } __attribute__((packed)); |
| 31 | |
| 32 | |
| 33 | // syslinux uses this format: |
| 34 | struct pmbr { |
| 35 | uint8_t bootcode[424]; |
| 36 | Guid boot_guid; |
| 37 | uint32_t disk_id; |
| 38 | uint8_t magic[2]; // 0x1d, 0x9a |
| 39 | struct legacy_partition part[4]; |
| 40 | uint8_t sig[2]; // 0x55, 0xaa |
| 41 | } __attribute__((packed)); |
| 42 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 43 | void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 44 | |
| 45 | // Handle to the drive storing the GPT. |
| 46 | struct drive { |
| 47 | int fd; /* file descriptor */ |
| 48 | uint64_t size; /* total size (in bytes) */ |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 49 | int is_mtd; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 50 | GptData gpt; |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 51 | MtdData mtd; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 52 | struct pmbr pmbr; |
| 53 | }; |
| 54 | |
| 55 | |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 56 | /* mode should be O_RDONLY or O_RDWR */ |
| 57 | int DriveOpen(const char *drive_path, struct drive *drive, int mode); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 58 | int DriveClose(struct drive *drive, int update_as_needed); |
| 59 | int CheckValid(const struct drive *drive); |
| 60 | |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 61 | /* Loads sectors from 'drive'. |
| 62 | * *buf is pointed to an allocated memory when returned, and should be |
| 63 | * freed. |
| 64 | * |
| 65 | * drive -- open drive. |
| 66 | * buf -- pointer to buffer pointer |
| 67 | * sector -- offset of starting sector (in sectors) |
| 68 | * sector_bytes -- bytes per sector |
| 69 | * sector_count -- number of sectors to load |
| 70 | * |
| 71 | * Returns CGPT_OK for successful. Aborts if any error occurs. |
| 72 | */ |
| 73 | int Load(struct drive *drive, uint8_t **buf, |
| 74 | const uint64_t sector, |
| 75 | const uint64_t sector_bytes, |
| 76 | const uint64_t sector_count); |
| 77 | |
| 78 | /* Saves sectors to 'drive'. |
| 79 | * |
| 80 | * drive -- open drive |
| 81 | * buf -- pointer to buffer |
| 82 | * sector -- starting sector offset |
| 83 | * sector_bytes -- bytes per sector |
| 84 | * sector_count -- number of sector to save |
| 85 | * |
| 86 | * Returns CGPT_OK for successful, CGPT_FAILED for failed. |
| 87 | */ |
| 88 | int Save(struct drive *drive, const uint8_t *buf, |
| 89 | const uint64_t sector, |
| 90 | const uint64_t sector_bytes, |
| 91 | const uint64_t sector_count); |
| 92 | |
| 93 | |
| 94 | /* GUID conversion functions. Accepted format: |
| 95 | * |
| 96 | * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" |
| 97 | * |
| 98 | * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing |
| 99 | * '\0'). |
| 100 | */ |
| 101 | #define GUID_STRLEN 37 |
| 102 | int StrToGuid(const char *str, Guid *guid); |
| 103 | void GuidToStr(const Guid *guid, char *str, unsigned int buflen); |
| 104 | int GuidEqual(const Guid *guid1, const Guid *guid2); |
| 105 | int IsZero(const Guid *guid); |
| 106 | |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 107 | /* Constant global type values to compare against */ |
Gabe Black | 93cf15e | 2011-07-07 16:00:00 -0700 | [diff] [blame] | 108 | extern const Guid guid_chromeos_firmware; |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 109 | extern const Guid guid_chromeos_kernel; |
| 110 | extern const Guid guid_chromeos_rootfs; |
| 111 | extern const Guid guid_linux_data; |
| 112 | extern const Guid guid_chromeos_reserved; |
| 113 | extern const Guid guid_efi; |
| 114 | extern const Guid guid_unused; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 115 | |
| 116 | int ReadPMBR(struct drive *drive); |
| 117 | int WritePMBR(struct drive *drive); |
| 118 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 119 | /* Convert possibly unterminated UTF16 string to UTF8. |
| 120 | * Caller must prepare enough space for UTF8, which could be up to |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 121 | * twice the byte length of UTF16 string plus the terminating '\0'. |
| 122 | * |
| 123 | * Return: CGPT_OK --- all character are converted successfully. |
| 124 | * CGPT_FAILED --- convert error, i.e. output buffer is too short. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 125 | */ |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 126 | int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput, |
| 127 | uint8_t *utf8, unsigned int maxoutput); |
| 128 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 129 | /* Convert null-terminated UTF8 string to UTF16. |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 130 | * Caller must prepare enough space for UTF16, which is the byte length of UTF8 |
| 131 | * plus the terminating 0x0000. |
| 132 | * |
| 133 | * Return: CGPT_OK --- all character are converted successfully. |
| 134 | * CGPT_FAILED --- convert error, i.e. output buffer is too short. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 135 | */ |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 136 | int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 137 | |
| 138 | /* Helper functions for supported GPT types. */ |
| 139 | int ResolveType(const Guid *type, char *buf); |
| 140 | int SupportedType(const char *name, Guid *type); |
| 141 | void PrintTypes(void); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 142 | void EntryDetails(GptEntry *entry, uint32_t index, int raw); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 143 | |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 144 | uint32_t GetNumberOfEntries(const struct drive *drive); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 145 | GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index); |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 146 | void SetPriority(struct drive *drive, int secondary, uint32_t entry_index, |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 147 | int priority); |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 148 | int GetPriority(struct drive *drive, int secondary, uint32_t entry_index); |
| 149 | void SetTries(struct drive *drive, int secondary, uint32_t entry_index, |
| 150 | int tries); |
| 151 | int GetTries(struct drive *drive, int secondary, uint32_t entry_index); |
| 152 | void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index, |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 153 | int success); |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 154 | int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index); |
| 155 | |
| 156 | void SetRaw(struct drive *drive, int secondary, uint32_t entry_index, |
| 157 | uint32_t raw); |
| 158 | |
| 159 | void UpdateAllEntries(struct drive *drive); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 160 | |
| 161 | uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers); |
| 162 | uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries); |
| 163 | void UpdateCrc(GptData *gpt); |
| 164 | int IsSynonymous(const GptHeader* a, const GptHeader* b); |
| 165 | |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 166 | int IsUnused(struct drive *drive, int secondary, uint32_t index); |
| 167 | int IsKernel(struct drive *drive, int secondary, uint32_t index); |
| 168 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 169 | // For usage and error messages. |
| 170 | extern const char* progname; |
| 171 | extern const char* command; |
| 172 | void Error(const char *format, ...); |
| 173 | |
Jay Srinivasan | 5fac757 | 2012-02-23 10:59:01 -0800 | [diff] [blame] | 174 | // The code paths that require uuid_generate are not used currently in |
| 175 | // libcgpt-cc.a so using this method would create an unnecessary dependency |
| 176 | // on libuuid which then requires us to build it for 32-bit for the static |
| 177 | // post-installer. So, we just expose this function pointer which should be |
| 178 | // set to uuid_generate in case of the cgpt binary and can be null or some |
| 179 | // no-op method in case of ilbcgpt-cc.a. |
| 180 | extern void (*uuid_generator)(uint8_t* buffer); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 181 | |
| 182 | // Command functions. |
| 183 | int cmd_show(int argc, char *argv[]); |
| 184 | int cmd_repair(int argc, char *argv[]); |
| 185 | int cmd_create(int argc, char *argv[]); |
| 186 | int cmd_add(int argc, char *argv[]); |
| 187 | int cmd_boot(int argc, char *argv[]); |
Bill Richardson | 4a20931 | 2010-07-02 11:34:38 -0700 | [diff] [blame] | 188 | int cmd_find(int argc, char *argv[]); |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 189 | int cmd_prioritize(int argc, char *argv[]); |
Stefan Reinauer | b7b865c | 2012-08-23 15:06:25 -0700 | [diff] [blame] | 190 | int cmd_legacy(int argc, char *argv[]); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 191 | |
| 192 | #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) |
| 193 | const char *GptError(int errnum); |
| 194 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 195 | // Size in chars of the GPT Entry's PartitionName field |
| 196 | #define GPT_PARTNAME_LEN 72 |
| 197 | |
| 198 | /* The standard "assert" macro goes away when NDEBUG is defined. This doesn't. |
| 199 | */ |
| 200 | #define require(A) do { \ |
| 201 | if (!(A)) { \ |
| 202 | fprintf(stderr, "condition (%s) failed at %s:%d\n", \ |
| 203 | #A, __FILE__, __LINE__); \ |
| 204 | exit(1); } \ |
| 205 | } while (0) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 206 | |
| 207 | #endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |