Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -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 | #include "cgpt.h" |
| 6 | |
| 7 | #include <getopt.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 8 | #include <string.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 9 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 10 | #include "cgpt_params.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 11 | |
| 12 | static void Usage(void) |
| 13 | { |
| 14 | printf("\nUsage: %s add [OPTIONS] DRIVE\n\n" |
| 15 | "Add, edit, or remove a partition entry.\n\n" |
| 16 | "Options:\n" |
| 17 | " -i NUM Specify partition (default is next available)\n" |
| 18 | " -b NUM Beginning sector\n" |
| 19 | " -s NUM Size in sectors\n" |
| 20 | " -t GUID Partition Type GUID\n" |
| 21 | " -u GUID Partition Unique ID\n" |
| 22 | " -l LABEL Label\n" |
| 23 | " -S NUM set Successful flag (0|1)\n" |
| 24 | " -T NUM set Tries flag (0-15)\n" |
| 25 | " -P NUM set Priority flag (0-15)\n" |
| 26 | " -A NUM set raw 64-bit attribute value\n" |
| 27 | "\n" |
| 28 | "Use the -i option to modify an existing partition.\n" |
| 29 | "The -b, -s, and -t options must be given for new partitions.\n" |
| 30 | "\n", progname); |
| 31 | PrintTypes(); |
| 32 | } |
| 33 | |
| 34 | int cmd_add(int argc, char *argv[]) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 35 | |
| 36 | CgptAddParams params; |
| 37 | memset(¶ms, 0, sizeof(params)); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 38 | |
| 39 | int gpt_retval; |
| 40 | GptEntry *entry; |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 41 | uint32_t index; |
| 42 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 43 | int c; |
| 44 | int errorcnt = 0; |
| 45 | char *e = 0; |
| 46 | |
| 47 | opterr = 0; // quiet, you |
| 48 | while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:")) != -1) |
| 49 | { |
| 50 | switch (c) |
| 51 | { |
| 52 | case 'i': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 53 | params.partition = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 54 | if (!*optarg || (e && *e)) |
| 55 | { |
| 56 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 57 | errorcnt++; |
| 58 | } |
| 59 | break; |
| 60 | case 'b': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 61 | params.set_begin = 1; |
| 62 | params.begin = strtoull(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 63 | if (!*optarg || (e && *e)) |
| 64 | { |
| 65 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 66 | errorcnt++; |
| 67 | } |
| 68 | break; |
| 69 | case 's': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 70 | params.set_size = 1; |
| 71 | params.size = strtoull(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 72 | if (!*optarg || (e && *e)) |
| 73 | { |
| 74 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 75 | errorcnt++; |
| 76 | } |
| 77 | break; |
| 78 | case 't': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 79 | params.set_type = 1; |
| 80 | if (CGPT_OK != SupportedType(optarg, ¶ms.type_guid) && |
| 81 | CGPT_OK != StrToGuid(optarg, ¶ms.type_guid)) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 82 | Error("invalid argument to -%c: %s\n", c, optarg); |
| 83 | errorcnt++; |
| 84 | } |
| 85 | break; |
| 86 | case 'u': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 87 | params.set_unique = 1; |
| 88 | if (CGPT_OK != StrToGuid(optarg, ¶ms.unique_guid)) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 89 | Error("invalid argument to -%c: %s\n", c, optarg); |
| 90 | errorcnt++; |
| 91 | } |
| 92 | break; |
| 93 | case 'l': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 94 | params.label = optarg; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 95 | break; |
| 96 | case 'S': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 97 | params.set_successful = 1; |
| 98 | params.successful = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 99 | if (!*optarg || (e && *e)) |
| 100 | { |
| 101 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 102 | errorcnt++; |
| 103 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 104 | if (params.successful < 0 || params.successful > 1) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 105 | Error("value for -%c must be between 0 and 1", c); |
| 106 | errorcnt++; |
| 107 | } |
| 108 | break; |
| 109 | case 'T': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 110 | params.set_tries = 1; |
| 111 | params.tries = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 112 | if (!*optarg || (e && *e)) |
| 113 | { |
| 114 | fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n", |
| 115 | progname, c, optarg); |
| 116 | errorcnt++; |
| 117 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 118 | if (params.tries < 0 || params.tries > 15) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 119 | Error("value for -%c must be between 0 and 15", c); |
| 120 | errorcnt++; |
| 121 | } |
| 122 | break; |
| 123 | case 'P': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 124 | params.set_priority = 1; |
| 125 | params.priority = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 126 | if (!*optarg || (e && *e)) |
| 127 | { |
| 128 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 129 | errorcnt++; |
| 130 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 131 | if (params.priority < 0 || params.priority > 15) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 132 | Error("value for -%c must be between 0 and 15", c); |
| 133 | errorcnt++; |
| 134 | } |
| 135 | break; |
| 136 | case 'A': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 137 | params.set_raw = 1; |
| 138 | params.raw_value = strtoull(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 139 | if (!*optarg || (e && *e)) |
| 140 | { |
| 141 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 142 | errorcnt++; |
| 143 | } |
| 144 | break; |
| 145 | |
| 146 | case 'h': |
| 147 | Usage(); |
| 148 | return CGPT_OK; |
| 149 | case '?': |
| 150 | Error("unrecognized option: -%c\n", optopt); |
| 151 | errorcnt++; |
| 152 | break; |
| 153 | case ':': |
| 154 | Error("missing argument to -%c\n", optopt); |
| 155 | errorcnt++; |
| 156 | break; |
| 157 | default: |
| 158 | errorcnt++; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | if (errorcnt) |
| 163 | { |
| 164 | Usage(); |
| 165 | return CGPT_FAILED; |
| 166 | } |
| 167 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame^] | 168 | if (optind >= argc) |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 169 | { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 170 | Error("missing drive argument\n"); |
| 171 | return CGPT_FAILED; |
| 172 | } |
| 173 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame^] | 174 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 175 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 176 | return cgpt_add(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 177 | } |