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 #include "cgpt.h" | 5 #include "cgpt.h" |
6 | 6 |
7 #include <getopt.h> | 7 #include <getopt.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 18 matching lines...) Expand all Loading... |
29 " -A NUM set raw 64-bit attribute value\n" | 29 " -A NUM set raw 64-bit attribute value\n" |
30 "\n" | 30 "\n" |
31 "Use the -i option to modify an existing partition.\n" | 31 "Use the -i option to modify an existing partition.\n" |
32 "The -b, -s, and -t options must be given for new partitions.\n" | 32 "The -b, -s, and -t options must be given for new partitions.\n" |
33 "\n", progname); | 33 "\n", progname); |
34 PrintTypes(); | 34 PrintTypes(); |
35 } | 35 } |
36 | 36 |
37 int cmd_add(int argc, char *argv[]) { | 37 int cmd_add(int argc, char *argv[]) { |
38 struct drive drive; | 38 struct drive drive; |
39 int partition = 0; | 39 uint32_t partition = 0; |
40 uint64_t begin = 0; | 40 uint64_t begin = 0; |
41 uint64_t size = 0; | 41 uint64_t size = 0; |
42 Guid type_guid; | 42 Guid type_guid; |
43 Guid unique_guid; | 43 Guid unique_guid; |
44 char *label = 0; | 44 char *label = 0; |
45 int successful = 0; | 45 int successful = 0; |
46 int tries = 0; | 46 int tries = 0; |
47 int priority = 0; | 47 int priority = 0; |
48 uint16_t raw_value = 0; | 48 uint16_t raw_value = 0; |
49 int set_begin = 0; | 49 int set_begin = 0; |
50 int set_size = 0; | 50 int set_size = 0; |
51 int set_type = 0; | 51 int set_type = 0; |
52 int set_unique = 0; | 52 int set_unique = 0; |
53 int set_successful = 0; | 53 int set_successful = 0; |
54 int set_tries = 0; | 54 int set_tries = 0; |
55 int set_priority = 0; | 55 int set_priority = 0; |
56 int set_raw = 0; | 56 int set_raw = 0; |
57 | 57 |
58 int gpt_retval; | 58 int gpt_retval; |
59 GptEntry *entry; | 59 GptEntry *entry; |
60 int index; | 60 uint32_t index; |
61 | 61 |
62 int c; | 62 int c; |
63 int errorcnt = 0; | 63 int errorcnt = 0; |
64 char *e = 0; | 64 char *e = 0; |
65 | 65 |
66 opterr = 0; // quiet, you | 66 opterr = 0; // quiet, you |
67 while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:")) != -1) | 67 while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:")) != -1) |
68 { | 68 { |
69 switch (c) | 69 switch (c) |
70 { | 70 { |
71 case 'i': | 71 case 'i': |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 191 |
192 if (CGPT_OK != DriveOpen(argv[optind], &drive)) | 192 if (CGPT_OK != DriveOpen(argv[optind], &drive)) |
193 return CGPT_FAILED; | 193 return CGPT_FAILED; |
194 | 194 |
195 if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) { | 195 if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) { |
196 Error("GptSanityCheck() returned %d: %s\n", | 196 Error("GptSanityCheck() returned %d: %s\n", |
197 gpt_retval, GptError(gpt_retval)); | 197 gpt_retval, GptError(gpt_retval)); |
198 return CGPT_FAILED; | 198 return CGPT_FAILED; |
199 } | 199 } |
200 | 200 |
201 int max_part = GetNumberOfEntries(&drive.gpt); | 201 uint32_t max_part = GetNumberOfEntries(&drive.gpt); |
202 if (partition) { | 202 if (partition) { |
203 if (partition > max_part) { | 203 if (partition > max_part) { |
204 Error("invalid partition number: %d\n", partition); | 204 Error("invalid partition number: %d\n", partition); |
205 goto bad; | 205 goto bad; |
206 } | 206 } |
207 index = partition - 1; | 207 index = partition - 1; |
208 entry = GetEntry(&drive.gpt, PRIMARY, index); | 208 entry = GetEntry(&drive.gpt, PRIMARY, index); |
209 } else { | 209 } else { |
210 // find next empty partition | 210 // find next empty partition |
211 for (index = 0; index < max_part; index++) { | 211 for (index = 0; index < max_part; index++) { |
(...skipping 25 matching lines...) Expand all Loading... |
237 | 237 |
238 if (set_begin) | 238 if (set_begin) |
239 entry->starting_lba = begin; | 239 entry->starting_lba = begin; |
240 if (set_size) | 240 if (set_size) |
241 entry->ending_lba = begin + size - 1; | 241 entry->ending_lba = begin + size - 1; |
242 if (set_type) | 242 if (set_type) |
243 memcpy(&entry->type, &type_guid, sizeof(Guid)); | 243 memcpy(&entry->type, &type_guid, sizeof(Guid)); |
244 if (set_unique) | 244 if (set_unique) |
245 memcpy(&entry->unique, &unique_guid, sizeof(Guid)); | 245 memcpy(&entry->unique, &unique_guid, sizeof(Guid)); |
246 if (label) { | 246 if (label) { |
247 uint16_t buf[128]; | 247 UTF8ToUTF16((uint8_t *)label, entry->name, |
248 UTF8ToUTF16((uint8_t *)label, buf); | 248 sizeof(entry->name) / sizeof(entry->name[0])); |
249 memcpy(entry->name, buf, sizeof(entry->name)); | |
250 } | 249 } |
251 if (set_raw) { | 250 if (set_raw) { |
252 entry->attrs.fields.gpt_att = raw_value; | 251 entry->attrs.fields.gpt_att = raw_value; |
253 } else { | 252 } else { |
254 if (set_successful) | 253 if (set_successful) |
255 SetSuccessful(&drive.gpt, PRIMARY, index, successful); | 254 SetSuccessful(&drive.gpt, PRIMARY, index, successful); |
256 if (set_tries) | 255 if (set_tries) |
257 SetTries(&drive.gpt, PRIMARY, index, tries); | 256 SetTries(&drive.gpt, PRIMARY, index, tries); |
258 if (set_priority) | 257 if (set_priority) |
259 SetPriority(&drive.gpt, PRIMARY, index, priority); | 258 SetPriority(&drive.gpt, PRIMARY, index, priority); |
260 } | 259 } |
261 | 260 |
262 RepairEntries(&drive.gpt, MASK_PRIMARY); | 261 RepairEntries(&drive.gpt, MASK_PRIMARY); |
263 RepairHeader(&drive.gpt, MASK_PRIMARY); | 262 RepairHeader(&drive.gpt, MASK_PRIMARY); |
264 | 263 |
265 drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | | 264 drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | |
266 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); | 265 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); |
267 UpdateCrc(&drive.gpt); | 266 UpdateCrc(&drive.gpt); |
268 | 267 |
269 | 268 |
270 // Write it all out | 269 // Write it all out |
271 return DriveClose(&drive, 1); | 270 return DriveClose(&drive, 1); |
272 | 271 |
273 bad: | 272 bad: |
274 (void) DriveClose(&drive, 0); | 273 (void) DriveClose(&drive, 0); |
275 return CGPT_FAILED; | 274 return CGPT_FAILED; |
276 } | 275 } |
OLD | NEW |