OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ | 5 #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
6 #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ | 6 #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
7 | 7 |
8 #define _GNU_SOURCE | 8 #define _GNU_SOURCE |
9 #define _FILE_OFFSET_BITS 64 | 9 #define _FILE_OFFSET_BITS 64 |
10 #include <features.h> | 10 #include <features.h> |
11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 #include <stdio.h> |
| 13 #include <stdlib.h> |
12 #include "endian.h" | 14 #include "endian.h" |
13 #include "gpt.h" | 15 #include "gpt.h" |
14 #include "cgptlib.h" | 16 #include "cgptlib.h" |
15 | 17 |
16 | 18 |
17 // Just for clarity | 19 // Just for clarity |
18 enum { | 20 enum { |
19 CGPT_OK = 0, | 21 CGPT_OK = 0, |
20 CGPT_FAILED, | 22 CGPT_FAILED, |
21 }; | 23 }; |
(...skipping 16 matching lines...) Expand all Loading... |
38 // syslinux uses this format: | 40 // syslinux uses this format: |
39 struct pmbr { | 41 struct pmbr { |
40 uint8_t bootcode[424]; | 42 uint8_t bootcode[424]; |
41 Guid boot_guid; | 43 Guid boot_guid; |
42 uint32_t disk_id; | 44 uint32_t disk_id; |
43 uint8_t magic[2]; // 0x1d, 0x9a | 45 uint8_t magic[2]; // 0x1d, 0x9a |
44 struct legacy_partition part[4]; | 46 struct legacy_partition part[4]; |
45 uint8_t sig[2]; // 0x55, 0xaa | 47 uint8_t sig[2]; // 0x55, 0xaa |
46 } __attribute__((packed)); | 48 } __attribute__((packed)); |
47 | 49 |
48 void PMBRToStr(struct pmbr *pmbr, char *str); | 50 void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen); |
49 | 51 |
50 // Handle to the drive storing the GPT. | 52 // Handle to the drive storing the GPT. |
51 struct drive { | 53 struct drive { |
52 int fd; /* file descriptor */ | 54 int fd; /* file descriptor */ |
53 uint64_t size; /* total size (in bytes) */ | 55 uint64_t size; /* total size (in bytes) */ |
54 GptData gpt; | 56 GptData gpt; |
55 struct pmbr pmbr; | 57 struct pmbr pmbr; |
56 }; | 58 }; |
57 | 59 |
58 | 60 |
59 int DriveOpen(const char *drive_path, struct drive *drive); | 61 int DriveOpen(const char *drive_path, struct drive *drive); |
60 int DriveClose(struct drive *drive, int update_as_needed); | 62 int DriveClose(struct drive *drive, int update_as_needed); |
61 int CheckValid(const struct drive *drive); | 63 int CheckValid(const struct drive *drive); |
62 | 64 |
63 /* GUID conversion functions. Accepted format: | 65 /* GUID conversion functions. Accepted format: |
64 * | 66 * |
65 * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" | 67 * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" |
66 * | 68 * |
67 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing | 69 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing |
68 * '\0'). | 70 * '\0'). |
69 */ | 71 */ |
70 #define GUID_STRLEN 37 | 72 #define GUID_STRLEN 37 |
71 int StrToGuid(const char *str, Guid *guid); | 73 int StrToGuid(const char *str, Guid *guid); |
72 void GuidToStr(const Guid *guid, char *str); | 74 void GuidToStr(const Guid *guid, char *str, unsigned int buflen); |
73 int IsZero(const Guid *guid); | 75 int IsZero(const Guid *guid); |
74 | 76 |
75 | 77 |
76 int ReadPMBR(struct drive *drive); | 78 int ReadPMBR(struct drive *drive); |
77 int WritePMBR(struct drive *drive); | 79 int WritePMBR(struct drive *drive); |
78 | 80 |
79 | 81 |
80 /* Convert UTF16 string to UTF8. Rewritten from gpt utility. | 82 /* Convert possibly unterminated UTF16 string to UTF8. |
81 * Caller must prepare enough space for UTF8. The rough estimation is: | 83 * Caller must prepare enough space for UTF8, which could be up to |
82 * | 84 * twice the number of UTF16 chars plus the terminating '\0'. |
83 * utf8 length = bytecount(utf16) * 1.5 | |
84 */ | 85 */ |
85 void UTF16ToUTF8(const uint16_t *utf16, uint8_t *utf8); | 86 void UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput, |
86 /* Convert UTF8 string to UTF16. Rewritten from gpt utility. | 87 uint8_t *utf8, unsigned int maxoutput); |
87 * Caller must prepare enough space for UTF16. The conservative estimation is: | 88 /* Convert null-terminated UTF8 string to UTF16. |
88 * | 89 * Caller must prepare enough space for UTF16, including a terminating 0x0000 |
89 * utf16 bytecount = bytecount(utf8) / 3 * 4 | |
90 */ | 90 */ |
91 void UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16); | 91 void UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput); |
92 | 92 |
93 /* Helper functions for supported GPT types. */ | 93 /* Helper functions for supported GPT types. */ |
94 int ResolveType(const Guid *type, char *buf); | 94 int ResolveType(const Guid *type, char *buf); |
95 int SupportedType(const char *name, Guid *type); | 95 int SupportedType(const char *name, Guid *type); |
96 void PrintTypes(void); | 96 void PrintTypes(void); |
97 void EntryDetails(GptEntry *entry, int index, int raw); | 97 void EntryDetails(GptEntry *entry, uint32_t index, int raw); |
98 | 98 |
99 uint32_t GetNumberOfEntries(const GptData *gpt); | 99 uint32_t GetNumberOfEntries(const GptData *gpt); |
100 GptEntry *GetEntry(GptData *gpt, int secondary, int entry_index); | 100 GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index); |
101 void SetPriority(GptData *gpt, int secondary, int entry_index, int priority); | 101 void SetPriority(GptData *gpt, int secondary, uint32_t entry_index, |
102 int GetPriority(GptData *gpt, int secondary, int entry_index); | 102 int priority); |
103 void SetTries(GptData *gpt, int secondary, int entry_index, int tries); | 103 int GetPriority(GptData *gpt, int secondary, uint32_t entry_index); |
104 int GetTries(GptData *gpt, int secondary, int entry_index); | 104 void SetTries(GptData *gpt, int secondary, uint32_t entry_index, int tries); |
105 void SetSuccessful(GptData *gpt, int secondary, int entry_index, int success); | 105 int GetTries(GptData *gpt, int secondary, uint32_t entry_index); |
106 int GetSuccessful(GptData *gpt, int secondary, int entry_index); | 106 void SetSuccessful(GptData *gpt, int secondary, uint32_t entry_index, |
| 107 int success); |
| 108 int GetSuccessful(GptData *gpt, int secondary, uint32_t entry_index); |
107 | 109 |
108 uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers); | 110 uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers); |
109 uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries); | 111 uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries); |
110 void UpdateCrc(GptData *gpt); | 112 void UpdateCrc(GptData *gpt); |
111 int IsSynonymous(const GptHeader* a, const GptHeader* b); | 113 int IsSynonymous(const GptHeader* a, const GptHeader* b); |
112 | 114 |
113 // For usage and error messages. | 115 // For usage and error messages. |
114 extern const char* progname; | 116 extern const char* progname; |
115 extern const char* command; | 117 extern const char* command; |
116 void Error(const char *format, ...); | 118 void Error(const char *format, ...); |
117 | 119 |
118 | 120 |
119 // Command functions. | 121 // Command functions. |
120 int cmd_show(int argc, char *argv[]); | 122 int cmd_show(int argc, char *argv[]); |
121 int cmd_repair(int argc, char *argv[]); | 123 int cmd_repair(int argc, char *argv[]); |
122 int cmd_create(int argc, char *argv[]); | 124 int cmd_create(int argc, char *argv[]); |
123 int cmd_add(int argc, char *argv[]); | 125 int cmd_add(int argc, char *argv[]); |
124 int cmd_boot(int argc, char *argv[]); | 126 int cmd_boot(int argc, char *argv[]); |
125 int cmd_find(int argc, char *argv[]); | 127 int cmd_find(int argc, char *argv[]); |
126 | 128 |
127 #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) | 129 #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) |
128 const char *GptError(int errnum); | 130 const char *GptError(int errnum); |
129 | 131 |
| 132 // Size in chars of the GPT Entry's PartitionName field |
| 133 #define GPT_PARTNAME_LEN 72 |
| 134 |
| 135 /* The standard "assert" macro goes away when NDEBUG is defined. This doesn't. |
| 136 */ |
| 137 #define require(A) do { \ |
| 138 if (!(A)) { \ |
| 139 fprintf(stderr, "condition (%s) failed at %s:%d\n", \ |
| 140 #A, __FILE__, __LINE__); \ |
| 141 exit(1); } \ |
| 142 } while (0) |
130 | 143 |
131 #endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ | 144 #endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
OLD | NEW |