Joel Kitching | 9adf2aa | 2019-08-20 17:43:50 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2012 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 | */ |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 5 | |
Joel Kitching | 9adf2aa | 2019-08-20 17:43:50 +0800 | [diff] [blame] | 6 | #ifndef VBOOT_REFERENCE_CGPT_H_ |
| 7 | #define VBOOT_REFERENCE_CGPT_H_ |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 8 | |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 9 | #include <fcntl.h> |
Idwer Vollering | b384db3 | 2021-05-10 21:15:45 +0200 | [diff] [blame^] | 10 | #if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 11 | #include <features.h> |
David Riley | 05987b1 | 2015-02-05 19:22:49 -0800 | [diff] [blame] | 12 | #endif |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 13 | #include <stdint.h> |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 16 | #include "cgpt_endian.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 17 | #include "cgptlib.h" |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 18 | #include "gpt.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 19 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 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 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 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 { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 47 | uint64_t size; /* total size (in bytes) */ |
| 48 | GptData gpt; |
| 49 | struct pmbr pmbr; |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 50 | int fd; /* file descriptor */ |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Nam T. Nguyen | 6ee52d9 | 2014-10-24 13:20:39 -0700 | [diff] [blame] | 53 | // Opens a block device or file, loads raw GPT data from it. |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 54 | // 'mode' should be O_RDONLY or O_RDWR. |
| 55 | // If 'drive_size' is 0, both the partitions and GPT structs reside on the same |
| 56 | // 'drive_path'. |
| 57 | // Otherwise, 'drive_size' is taken as the size of the device that all |
| 58 | // partitions will reside on, and 'drive_path' is where we store GPT structs. |
Nam T. Nguyen | 6ee52d9 | 2014-10-24 13:20:39 -0700 | [diff] [blame] | 59 | // |
| 60 | // Returns CGPT_FAILED if any error happens. |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 61 | // Returns CGPT_OK if success and information are stored in 'drive'. */ |
| 62 | int DriveOpen(const char *drive_path, struct drive *drive, int mode, |
| 63 | uint64_t drive_size); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 64 | int DriveClose(struct drive *drive, int update_as_needed); |
| 65 | int CheckValid(const struct drive *drive); |
| 66 | |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 67 | /* Loads sectors from 'drive'. |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 68 | * |
| 69 | * drive -- open drive. |
Julius Werner | 8e8f4b9 | 2019-10-28 16:26:18 -0700 | [diff] [blame] | 70 | * buf -- pointer to buffer of at least (sector_bytes * sector_count) size |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 71 | * sector -- offset of starting sector (in sectors) |
| 72 | * sector_bytes -- bytes per sector |
| 73 | * sector_count -- number of sectors to load |
| 74 | * |
| 75 | * Returns CGPT_OK for successful. Aborts if any error occurs. |
| 76 | */ |
Julius Werner | 8e8f4b9 | 2019-10-28 16:26:18 -0700 | [diff] [blame] | 77 | int Load(struct drive *drive, uint8_t *buf, |
Albert Chaulk | 534723a | 2013-03-20 14:46:50 -0700 | [diff] [blame] | 78 | const uint64_t sector, |
| 79 | const uint64_t sector_bytes, |
| 80 | const uint64_t sector_count); |
| 81 | |
| 82 | /* Saves sectors to 'drive'. |
| 83 | * |
| 84 | * drive -- open drive |
| 85 | * buf -- pointer to buffer |
| 86 | * sector -- starting sector offset |
| 87 | * sector_bytes -- bytes per sector |
| 88 | * sector_count -- number of sector to save |
| 89 | * |
| 90 | * Returns CGPT_OK for successful, CGPT_FAILED for failed. |
| 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 | |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 98 | /* Constant global type values to compare against */ |
Gabe Black | 93cf15e | 2011-07-07 16:00:00 -0700 | [diff] [blame] | 99 | extern const Guid guid_chromeos_firmware; |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 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; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 106 | |
| 107 | int ReadPMBR(struct drive *drive); |
| 108 | int WritePMBR(struct drive *drive); |
| 109 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 110 | /* Convert possibly unterminated UTF16 string to UTF8. |
| 111 | * 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] | 112 | * twice the byte length of UTF16 string plus the terminating '\0'. |
| 113 | * |
| 114 | * Return: CGPT_OK --- all character are converted successfully. |
| 115 | * CGPT_FAILED --- convert error, i.e. output buffer is too short. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 116 | */ |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 117 | int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput, |
| 118 | uint8_t *utf8, unsigned int maxoutput); |
| 119 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 120 | /* Convert null-terminated UTF8 string to UTF16. |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 121 | * Caller must prepare enough space for UTF16, which is the byte length of UTF8 |
| 122 | * plus the terminating 0x0000. |
| 123 | * |
| 124 | * Return: CGPT_OK --- all character are converted successfully. |
| 125 | * CGPT_FAILED --- convert error, i.e. output buffer is too short. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 126 | */ |
Louis Yung-Chieh Lo | 500b3c2 | 2010-11-22 18:19:11 +0800 | [diff] [blame] | 127 | int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 128 | |
| 129 | /* Helper functions for supported GPT types. */ |
| 130 | int ResolveType(const Guid *type, char *buf); |
| 131 | int SupportedType(const char *name, Guid *type); |
| 132 | void PrintTypes(void); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 133 | void EntryDetails(GptEntry *entry, uint32_t index, int raw); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 134 | |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 135 | uint32_t GetNumberOfEntries(const struct drive *drive); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 136 | GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index); |
Albert Chaulk | b334e65 | 2013-03-28 15:25:33 -0700 | [diff] [blame] | 137 | |
Ben Chan | a4a8c02 | 2018-01-31 11:39:14 -0800 | [diff] [blame] | 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); |
Mike Frysinger | 6c18af5 | 2016-09-07 16:45:48 -0400 | [diff] [blame] | 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); |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 144 | void SetPriority(struct drive *drive, int secondary, uint32_t entry_index, |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 145 | int priority); |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 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, |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 151 | int success); |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 152 | int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index); |
| 153 | |
| 154 | void SetRaw(struct drive *drive, int secondary, uint32_t entry_index, |
| 155 | uint32_t raw); |
| 156 | |
| 157 | void UpdateAllEntries(struct drive *drive); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 158 | |
| 159 | uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers); |
| 160 | uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries); |
| 161 | void UpdateCrc(GptData *gpt); |
| 162 | int IsSynonymous(const GptHeader* a, const GptHeader* b); |
| 163 | |
Albert Chaulk | fa6b35c | 2013-03-26 13:43:02 -0700 | [diff] [blame] | 164 | int IsUnused(struct drive *drive, int secondary, uint32_t index); |
| 165 | int IsKernel(struct drive *drive, int secondary, uint32_t index); |
| 166 | |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 167 | // Optional. Applications that need this must provide an implementation. |
| 168 | // |
| 169 | // Explanation: |
| 170 | // Some external utilities need to manipulate the GPT, but don't create new |
| 171 | // partitions from scratch. The cgpt executable uses libuuid to provide this |
| 172 | // functionality, but we don't want to have to build or install a separate |
| 173 | // instance of that library just for the 32-bit static post-install tool, |
| 174 | // which doesn't need this function. |
| 175 | int GenerateGuid(Guid *newguid); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 176 | |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 177 | // For usage and error messages. |
| 178 | void Error(const char *format, ...); |
Nam T. Nguyen | 6ee52d9 | 2014-10-24 13:20:39 -0700 | [diff] [blame] | 179 | void Warning(const char *format, ...); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 180 | |
| 181 | // Command functions. |
Mike Frysinger | c60eb7e | 2016-09-07 20:23:46 -0400 | [diff] [blame] | 182 | int check_int_parse(char option, const char *buf); |
| 183 | int check_int_limit(char option, int val, int low, int high); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 184 | int cmd_show(int argc, char *argv[]); |
| 185 | int cmd_repair(int argc, char *argv[]); |
| 186 | int cmd_create(int argc, char *argv[]); |
| 187 | int cmd_add(int argc, char *argv[]); |
| 188 | int cmd_boot(int argc, char *argv[]); |
Bill Richardson | 4a20931 | 2010-07-02 11:34:38 -0700 | [diff] [blame] | 189 | int cmd_find(int argc, char *argv[]); |
Matt Delco | 0c274a9 | 2018-08-02 11:50:06 -0700 | [diff] [blame] | 190 | int cmd_edit(int argc, char *argv[]); |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 191 | int cmd_prioritize(int argc, char *argv[]); |
Stefan Reinauer | b7b865c | 2012-08-23 15:06:25 -0700 | [diff] [blame] | 192 | int cmd_legacy(int argc, char *argv[]); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 193 | |
| 194 | #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) |
| 195 | const char *GptError(int errnum); |
| 196 | |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 197 | // Size in chars of the GPT Entry's PartitionName field |
| 198 | #define GPT_PARTNAME_LEN 72 |
| 199 | |
| 200 | /* The standard "assert" macro goes away when NDEBUG is defined. This doesn't. |
| 201 | */ |
| 202 | #define require(A) do { \ |
| 203 | if (!(A)) { \ |
| 204 | fprintf(stderr, "condition (%s) failed at %s:%d\n", \ |
| 205 | #A, __FILE__, __LINE__); \ |
| 206 | exit(1); } \ |
| 207 | } while (0) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 208 | |
Joel Kitching | 9adf2aa | 2019-08-20 17:43:50 +0800 | [diff] [blame] | 209 | #endif /* VBOOT_REFERENCE_CGPT_H_ */ |