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