OLD | NEW |
| (Empty) |
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 | |
3 * found in the LICENSE file. | |
4 * | |
5 * Header file for cgpt. | |
6 */ | |
7 #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ | |
8 #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ | |
9 | |
10 #include <getopt.h> | |
11 #include <stdint.h> | |
12 #include "cgptlib.h" | |
13 #include "gpt.h" | |
14 | |
15 enum { | |
16 CGPT_OK = 0, | |
17 CGPT_FAILED, /* generic error */ | |
18 }; | |
19 | |
20 #define NOT_INITED (-1) /* to indicated a signed integer is not initialed. */ | |
21 | |
22 #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) | |
23 | |
24 /* 'struct option' of getopt_long() is not enough for our usage. | |
25 * Therefore, we define the extra information to make option parsing | |
26 * more organizable. | |
27 * Note that please make sure every entry in struct option is mapped into an | |
28 * individual entry in this struct. */ | |
29 struct option_details { | |
30 char *comment; | |
31 | |
32 /* If has_arg is 'required_argument', 'validator' is called to check whether | |
33 * the 'argument' is valid or not. Once the argument is valid, the value is | |
34 * stored in 'parsed'. | |
35 * | |
36 * If has_arg is 'no_argument', 'validator' is called to load 'valid_range' | |
37 * into 'parsed' ('argument' is 0 in this case). Since getopt_long() only | |
38 * supports integer type for 'flag' and 'val', this can support for any type. | |
39 * | |
40 * If has_arg is 'optional_argument', like 'required_argument', 'validator' is | |
41 * called to check if 'argument' is valid or not. 'argument' indicates if | |
42 * argument is present or not. | |
43 * | |
44 * 'validator' returns CGPT_OK if argument is valid; otherwise CGPT_FAILED | |
45 * if invalid. */ | |
46 int (*validator)(const char *argument, void *valid_range, void *parsed); | |
47 void *valid_range; /* The structure passed to validator. */ | |
48 void *parsed; /* The structure passed to validator. */ | |
49 }; | |
50 | |
51 /* This is a special 'validator'. It assists those options without an argument, | |
52 * i.e. help, to indicate the option is present. */ | |
53 int AssignTrue(const char *argument, void *pointer, void *integer); | |
54 | |
55 /* Special validator. Copy string to 'parsed' with max 'valid_range' bytes. */ | |
56 int CopyString(const char *argument, void *max_len, void *dst); | |
57 | |
58 /* Validator function. Returns 1 if 'argument' is between 'max' and 'min' | |
59 * in 'valid_range'. */ | |
60 struct number_range { | |
61 int max; | |
62 int min; | |
63 }; | |
64 int InNumberRange(const char *argument, void *valid_range, void *parsed); | |
65 | |
66 void ShowOptions(const struct option *opts, | |
67 const struct option_details *details, | |
68 const int num); | |
69 | |
70 /* Handles all options from given argc and argv. This function supports both | |
71 * short and long options. | |
72 * | |
73 * Assumptions: | |
74 * 1. every short option has a corresponding long option and the short option | |
75 * is equal to 'val' of that long option. | |
76 * 2. every entry in 'options' has a corresponding entry in 'details'. | |
77 * One by one and in order. | |
78 * | |
79 * Returns CGPT_OK if given options in argv are good, otherwise CGPT_FAILED. | |
80 * Note that the global variable 'optind' points to next non-option after | |
81 * this function returns. | |
82 */ | |
83 int HandleOptions(const int argc, | |
84 char *const *argv, | |
85 const char *short_option, | |
86 const int option_count, | |
87 const struct option *options, | |
88 const struct option_details *details); | |
89 | |
90 struct drive; | |
91 int OpenDriveInLastArgument(const int argc, | |
92 char *const *argv, | |
93 struct drive *drive); | |
94 | |
95 /* GUID conversion functions. Accepted format: | |
96 * | |
97 * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" | |
98 * | |
99 * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing | |
100 * '\0'). | |
101 */ | |
102 #define GUID_STRLEN 37 | |
103 int StrToGuid(const char *str, Guid *guid); | |
104 void GuidToStr(const Guid *guid, char *str); | |
105 | |
106 /* Convert UTF16 string to UTF8. Rewritten from gpt utility. | |
107 * Caller must prepare enough space for UTF8. The rough estimation is: | |
108 * | |
109 * utf8 length = bytecount(utf16) * 1.5 | |
110 */ | |
111 void UTF16ToUTF8(const uint16_t *utf16, uint8_t *utf8); | |
112 /* Convert UTF8 string to UTF16. Rewritten from gpt utility. | |
113 * Caller must prepare enough space for UTF16. The conservative estimation is: | |
114 * | |
115 * utf16 bytecount = bytecount(utf8) / 3 * 4 | |
116 */ | |
117 void UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16); | |
118 | |
119 /* Helper functions for supported GPT types. */ | |
120 int ResolveType(const Guid *type, char *buf); | |
121 int SupportedType(const char *name, Guid *type); | |
122 void PrintTypes(void); | |
123 void EntryDetails(GptEntry *entry, int index, int raw); | |
124 | |
125 /* Describes the drive storing the GPT. */ | |
126 struct drive { | |
127 int inited; /* indicated if this structure is valid */ | |
128 int fd; /* file descriptor */ | |
129 uint64_t size; /* total size (in bytes) */ | |
130 GptData gpt; | |
131 }; | |
132 | |
133 extern const char* progname; | |
134 | |
135 /* Given a hard drive path, this function loads GPT sectors from that drive, | |
136 * and fills 'drive' structure. All memory allocated in drive_open() will be | |
137 * freed at drive_close(). | |
138 * | |
139 * If 'drive_path' starts with '/', it is treated as absolute path. | |
140 * If 'drive_path' starts with '.', it is treated as relative path. | |
141 * Otherwise, it will be prepended with '/dev/' to comply with gpt. | |
142 * | |
143 * Returns CGPT_FAILED if any error happens. | |
144 * Returns CGPT_OK if success and information are stored in 'drive'. | |
145 */ | |
146 int DriveOpen(const char *drive_path, struct drive *drive); | |
147 int DriveClose(struct drive *drive); | |
148 int CheckValid(const struct drive *drive); | |
149 | |
150 /* Function declarations for commands. | |
151 * The return value of these functions is passed to main()'s exit value. */ | |
152 int CgptAdm(int argc, char *argv[]); | |
153 int CgptAttribute(int argc, char *argv[]); | |
154 int CgptDev(int argc, char *argv[]); | |
155 int CgptRepair(int argc, char *argv[]); | |
156 int CgptShow(int argc, char *argv[]); | |
157 | |
158 #endif /* VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ */ | |
OLD | NEW |