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 * Show GPT details. | |
6 */ | |
7 #include <getopt.h> | |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
10 #include "cgpt.h" | |
11 #include "cgptlib_internal.h" | |
12 #include "cgpt_tofix.h" | |
13 #include "utility.h" | |
14 | |
15 /* Integers to store parsed argument. */ | |
16 static int help, number, verbose; | |
17 | |
18 /* The structure for getopt_long(). When you add/delete any line, please refine | |
19 * attribute_comments[] and third parameter of getopt_long() too. */ | |
20 static struct option show_options[] = { | |
21 {.name = "help", .has_arg = no_argument, .flag = 0, .val = 'h'}, | |
22 {.name = "number", .has_arg = no_argument, .flag = 0, .val = 'n'}, | |
23 {.name = "verbose", .has_arg = no_argument, .flag = 0, .val = 'v'}, | |
24 { /* last element, which should be zero. */ } | |
25 }; | |
26 | |
27 /* Extra information than struct option, please update this structure if you | |
28 * add/remove any line in attribute_options[]. */ | |
29 static struct option_details show_options_details[] = { | |
30 /* help */ | |
31 { .comment = "print this help", | |
32 .validator = AssignTrue, | |
33 .valid_range = 0, | |
34 .parsed = &help}, | |
35 /* number */ | |
36 { .comment = "print raw numbers (don't interpret)", | |
37 .validator = AssignTrue, | |
38 .valid_range = 0, | |
39 .parsed = &number}, | |
40 /* verbose */ | |
41 { .comment = "verbose print", | |
42 .validator = AssignTrue, | |
43 .valid_range = 0, | |
44 .parsed = &verbose}, | |
45 { /* last element, which should be zero. */ } | |
46 }; | |
47 | |
48 void ShowHelp() { | |
49 printf("\nUsage: %s show [OPTIONS] device_name\n\n", progname); | |
50 ShowOptions(show_options, show_options_details, ARRAY_COUNT(show_options)); | |
51 printf("\n"); | |
52 } | |
53 | |
54 /* Generate output like: | |
55 * | |
56 * [AB-CD-EF-01] for group = 1 | |
57 * [ABCD-EF01] for group = 3 (low byte first) | |
58 * | |
59 * Needs (size*3-1+3) bytes of space in 'buf' (included the tailing '\0'). | |
60 */ | |
61 #define BUFFER_SIZE(size) (size *3 - 1 + 3) | |
62 static short Uint8To2Chars(const uint8_t t) { | |
63 int h = t >> 4; | |
64 int l = t & 0xf; | |
65 h = (h >= 0xA) ? h - 0xA + 'A' : h + '0'; | |
66 l = (l >= 0xA) ? l - 0xA + 'A' : l + '0'; | |
67 return (h << 8) + l; | |
68 } | |
69 static void RawDump(const uint8_t *memory, const int size, | |
70 char *buf, int group) { | |
71 int i, outlen = 0; | |
72 buf[outlen++] = '['; | |
73 for (i = 0; i < size; ++i) { | |
74 short c2 = Uint8To2Chars(memory[i]); | |
75 buf[outlen++] = c2 >> 8; | |
76 buf[outlen++] = c2 & 0xff; | |
77 if (i != (size - 1) && ((i + 1) % group) == 0) | |
78 buf[outlen++] = '-'; | |
79 } | |
80 buf[outlen++] = ']'; | |
81 buf[outlen++] = '\0'; | |
82 } | |
83 | |
84 /* Outpur formatters */ | |
85 #define TITLE_FMT "%10s%10s%8s %s\n" | |
86 #define GPT_FMT "%10d%10d%8s %s\n" | |
87 #define GPT_MORE "%10s%10s%8s ", "", "", "" | |
88 #define PARTITION_FMT "%10d%10d%8d %s\n" | |
89 #define PARTITION_MORE "%10s%10s%8s %s%s\n", "", "", "" | |
90 | |
91 static void HeaderDetails(GptHeader *header, const char *indent, int raw) { | |
92 int i; | |
93 | |
94 printf("%sSig: ", indent); | |
95 if (raw == NOT_INITED) { | |
96 printf("["); | |
97 for (i = 0; i < sizeof(header->signature); ++i) | |
98 printf("%c", header->signature[i]); | |
99 printf("]"); | |
100 } else { | |
101 char buf[BUFFER_SIZE(sizeof(header->signature))]; | |
102 RawDump((uint8_t *)header->signature, sizeof(header->signature), buf, 1); | |
103 printf("%s", buf); | |
104 } | |
105 printf("\n"); | |
106 | |
107 printf("%sRev: 0x%08x\n", indent, header->revision); | |
108 printf("%sSize: %d\n", indent, header->size); | |
109 printf("%sHeader CRC: 0x%08x\n", indent, header->header_crc32); | |
110 printf("%sMy LBA: %lld\n", indent, (long long)header->my_lba); | |
111 printf("%sFirst LBA: %lld\n", indent, (long long)header->first_usable_lba); | |
112 printf("%sLast LBA: %lld\n", indent, (long long)header->last_usable_lba); | |
113 | |
114 { /* For disk guid */ | |
115 char buf[GUID_STRLEN]; | |
116 GuidToStr(&header->disk_uuid, buf); | |
117 printf("%sDisk UUID: %s\n", indent, buf); | |
118 } | |
119 | |
120 printf("%sEntries LBA: %lld\n", indent, (long long)header->entries_lba); | |
121 printf("%sNumber of entries: %d\n", indent, header->number_of_entries); | |
122 printf("%sSize of entry: %d\n", indent, header->size_of_entry); | |
123 printf("%sEntries CRC: 0x%08x\n", indent, header->entries_crc32); | |
124 } | |
125 | |
126 void EntryDetails(GptEntry *entry, int index, int raw) { | |
127 char contents[256]; | |
128 | |
129 if (raw == NOT_INITED) { | |
130 uint8_t label[sizeof(entry->name) * 3 / 2]; | |
131 char type[GUID_STRLEN], unique[GUID_STRLEN];; | |
132 | |
133 UTF16ToUTF8(entry->name, label); | |
134 snprintf(contents, sizeof(contents), "Label: \"%s\"", label); | |
135 printf(PARTITION_FMT, (int)entry->starting_lba, | |
136 (int)(entry->ending_lba - entry->starting_lba + 1), | |
137 index+1, contents); | |
138 if (CGPT_OK == ResolveType(&entry->type, type)) { | |
139 printf(PARTITION_MORE, "Type: ", type); | |
140 } else { | |
141 GuidToStr(&entry->type, type); | |
142 printf(PARTITION_MORE, "Type: ", type); | |
143 } | |
144 GuidToStr(&entry->unique, unique); | |
145 printf(PARTITION_MORE, "UUID: ", unique); | |
146 if (!Memcmp(&guid_chromeos_kernel, &entry->type, sizeof(Guid))) { | |
147 int tries = (entry->attributes & CGPT_ATTRIBUTE_TRIES_MASK) >> | |
148 CGPT_ATTRIBUTE_TRIES_OFFSET; | |
149 int successful = (entry->attributes & CGPT_ATTRIBUTE_SUCCESSFUL_MASK) >> | |
150 CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET; | |
151 int priority = (entry->attributes & CGPT_ATTRIBUTE_PRIORITY_MASK) >> | |
152 CGPT_ATTRIBUTE_PRIORITY_OFFSET; | |
153 snprintf(contents, sizeof(contents), | |
154 "priority=%d tries=%d successful=%d", | |
155 priority, tries, successful); | |
156 printf(PARTITION_MORE, "Attr: ", contents); | |
157 } | |
158 } else { | |
159 char label[BUFFER_SIZE(sizeof(entry->name))]; | |
160 char type[GUID_STRLEN], unique[GUID_STRLEN]; | |
161 | |
162 RawDump((void*)entry->name, sizeof(entry->name), label, 2); | |
163 snprintf(contents, sizeof(contents), "Label: %s", label); | |
164 printf(PARTITION_FMT, (int)entry->starting_lba, | |
165 (int)(entry->ending_lba - entry->starting_lba + 1), | |
166 index+1, contents); | |
167 GuidToStr(&entry->type, type); | |
168 printf(PARTITION_MORE, "Type: ", type); | |
169 GuidToStr(&entry->unique, unique); | |
170 printf(PARTITION_MORE, "UUID: ", unique); | |
171 snprintf(contents, sizeof(contents), "[%016lx]", entry->attributes); | |
172 printf(PARTITION_MORE, "Attr: ", contents); | |
173 } | |
174 } | |
175 | |
176 void EntriesDetails(GptData *gpt, const int secondary, int raw) { | |
177 int i; | |
178 | |
179 for (i = 0; i < GetNumberOfEntries(gpt); ++i) { | |
180 GptEntry *entry; | |
181 entry = GetEntry(gpt, secondary, i); | |
182 | |
183 if (!Memcmp(&guid_unused, &entry->type, sizeof(Guid))) continue; | |
184 | |
185 EntryDetails(entry, i, raw); | |
186 } | |
187 } | |
188 | |
189 /* Parses all options (and validates them), then opens the drive. | |
190 * Show GPT information in following order: | |
191 * | |
192 * Primary header sector | |
193 * details (if -v applied) | |
194 * | |
195 * Primary table sectors | |
196 * | |
197 * 1st partition | |
198 * details (if -v applied) | |
199 * : | |
200 * last partition | |
201 * details (if -v applied) | |
202 * | |
203 * Secondary table sectors | |
204 * | |
205 * Secondary header sector | |
206 * details (if -v applied) | |
207 */ | |
208 int CgptShow(int argc, char *argv[]) { | |
209 struct drive drive; | |
210 | |
211 /* I know this is NOT the perfect place to put code to make options[] and | |
212 * details[] are synced. But this is the best place we have right now since C | |
213 * preprocessor doesn't know sizeof() for #if directive. */ | |
214 assert(ARRAY_COUNT(show_options) == | |
215 ARRAY_COUNT(show_options_details)); | |
216 | |
217 help = number = NOT_INITED; | |
218 | |
219 if (CGPT_OK != HandleOptions(argc, argv, | |
220 "hnv", | |
221 ARRAY_COUNT(show_options), | |
222 show_options, | |
223 show_options_details)) | |
224 return CGPT_FAILED; | |
225 if (help != NOT_INITED) { | |
226 ShowHelp(); | |
227 return CGPT_FAILED; | |
228 } | |
229 | |
230 if (CGPT_OK != OpenDriveInLastArgument(argc, argv, &drive)) | |
231 return CGPT_FAILED; | |
232 | |
233 printf(TITLE_FMT, "start", "size", "part", "contents"); | |
234 printf(GPT_FMT, 0, GPT_PMBR_SECTOR, "", "PMBR"); | |
235 | |
236 if (drive.gpt.valid_headers & MASK_PRIMARY) { | |
237 printf(GPT_FMT, (int)GPT_PMBR_SECTOR, | |
238 (int)GPT_HEADER_SECTOR, "", "Pri GPT header"); | |
239 if (verbose) { | |
240 GptHeader *header; | |
241 char indent[64]; | |
242 | |
243 snprintf(indent, sizeof(indent), GPT_MORE); | |
244 header = (GptHeader*)drive.gpt.primary_header; | |
245 HeaderDetails(header, indent, number); | |
246 } | |
247 } else { | |
248 printf(GPT_FMT, (int)GPT_PMBR_SECTOR, | |
249 (int)GPT_HEADER_SECTOR, "INVALID", "Pri GPT header"); | |
250 } | |
251 | |
252 printf(GPT_FMT, (int)(GPT_PMBR_SECTOR + GPT_HEADER_SECTOR), | |
253 (int)GPT_ENTRIES_SECTORS, | |
254 drive.gpt.valid_entries & MASK_PRIMARY ? "" : "INVALID", | |
255 "Pri GPT table"); | |
256 | |
257 if (drive.gpt.valid_entries & MASK_PRIMARY) | |
258 EntriesDetails(&drive.gpt, PRIMARY, number); | |
259 | |
260 printf(GPT_FMT, (int)(drive.gpt.drive_sectors - GPT_HEADER_SECTOR - | |
261 GPT_ENTRIES_SECTORS), | |
262 (int)GPT_ENTRIES_SECTORS, | |
263 drive.gpt.valid_entries & MASK_SECONDARY ? "" : "INVALID", | |
264 "Sec GPT table"); | |
265 /* We show secondary table details if any of following is true. | |
266 * 1. only secondary is valid. | |
267 * 2. secondary is not identical to promary. | |
268 */ | |
269 if ((drive.gpt.valid_entries & MASK_SECONDARY) && | |
270 (!(drive.gpt.valid_entries & MASK_PRIMARY) || | |
271 Memcmp(drive.gpt.primary_entries, drive.gpt.secondary_entries, | |
272 TOTAL_ENTRIES_SIZE))) { | |
273 EntriesDetails(&drive.gpt, SECONDARY, number); | |
274 } | |
275 | |
276 if (drive.gpt.valid_headers & MASK_SECONDARY) | |
277 printf(GPT_FMT, (int)(drive.gpt.drive_sectors - GPT_HEADER_SECTOR), | |
278 (int)GPT_HEADER_SECTOR, "", "Sec GPT header"); | |
279 else | |
280 printf(GPT_FMT, (int)GPT_PMBR_SECTOR, | |
281 (int)GPT_HEADER_SECTOR, "INVALID", "Sec GPT header"); | |
282 /* We show secondary header if any of following is true: | |
283 * 1. only secondary is valid. | |
284 * 2. secondary is not synonymous to primary. | |
285 */ | |
286 if ((drive.gpt.valid_headers & MASK_SECONDARY) && | |
287 (!(drive.gpt.valid_headers & MASK_PRIMARY) || | |
288 !IsSynonymous((GptHeader*)drive.gpt.primary_header, | |
289 (GptHeader*)drive.gpt.secondary_header))) { | |
290 if (verbose) { | |
291 GptHeader *header; | |
292 char indent[64]; | |
293 | |
294 snprintf(indent, sizeof(indent), GPT_MORE); | |
295 header = (GptHeader*)drive.gpt.secondary_header; | |
296 HeaderDetails(header, indent, number); | |
297 } | |
298 } | |
299 | |
300 CheckValid(&drive); | |
301 DriveClose(&drive); | |
302 | |
303 return CGPT_OK; | |
304 } | |
OLD | NEW |