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 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 39 | int c; |
| 40 | int errorcnt = 0; |
| 41 | char *e = 0; |
| 42 | |
| 43 | opterr = 0; // quiet, you |
| 44 | while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:")) != -1) |
| 45 | { |
| 46 | switch (c) |
| 47 | { |
| 48 | case 'i': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 49 | params.partition = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 50 | if (!*optarg || (e && *e)) |
| 51 | { |
| 52 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 53 | errorcnt++; |
| 54 | } |
| 55 | break; |
| 56 | case 'b': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 57 | params.set_begin = 1; |
| 58 | params.begin = strtoull(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 59 | if (!*optarg || (e && *e)) |
| 60 | { |
| 61 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 62 | errorcnt++; |
| 63 | } |
| 64 | break; |
| 65 | case 's': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 66 | params.set_size = 1; |
| 67 | params.size = strtoull(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 68 | if (!*optarg || (e && *e)) |
| 69 | { |
| 70 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 71 | errorcnt++; |
| 72 | } |
| 73 | break; |
| 74 | case 't': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 75 | params.set_type = 1; |
| 76 | if (CGPT_OK != SupportedType(optarg, ¶ms.type_guid) && |
| 77 | CGPT_OK != StrToGuid(optarg, ¶ms.type_guid)) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 78 | Error("invalid argument to -%c: %s\n", c, optarg); |
| 79 | errorcnt++; |
| 80 | } |
| 81 | break; |
| 82 | case 'u': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 83 | params.set_unique = 1; |
| 84 | if (CGPT_OK != StrToGuid(optarg, ¶ms.unique_guid)) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 85 | Error("invalid argument to -%c: %s\n", c, optarg); |
| 86 | errorcnt++; |
| 87 | } |
| 88 | break; |
| 89 | case 'l': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 90 | params.label = optarg; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 91 | break; |
| 92 | case 'S': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 93 | params.set_successful = 1; |
| 94 | params.successful = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 95 | if (!*optarg || (e && *e)) |
| 96 | { |
| 97 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 98 | errorcnt++; |
| 99 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 100 | if (params.successful < 0 || params.successful > 1) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 101 | Error("value for -%c must be between 0 and 1", c); |
| 102 | errorcnt++; |
| 103 | } |
| 104 | break; |
| 105 | case 'T': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 106 | params.set_tries = 1; |
| 107 | params.tries = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 108 | if (!*optarg || (e && *e)) |
| 109 | { |
| 110 | fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n", |
| 111 | progname, c, optarg); |
| 112 | errorcnt++; |
| 113 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 114 | if (params.tries < 0 || params.tries > 15) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 115 | Error("value for -%c must be between 0 and 15", c); |
| 116 | errorcnt++; |
| 117 | } |
| 118 | break; |
| 119 | case 'P': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 120 | params.set_priority = 1; |
| 121 | params.priority = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 122 | if (!*optarg || (e && *e)) |
| 123 | { |
| 124 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 125 | errorcnt++; |
| 126 | } |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 127 | if (params.priority < 0 || params.priority > 15) { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 128 | Error("value for -%c must be between 0 and 15", c); |
| 129 | errorcnt++; |
| 130 | } |
| 131 | break; |
| 132 | case 'A': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 133 | params.set_raw = 1; |
| 134 | params.raw_value = strtoull(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 135 | if (!*optarg || (e && *e)) |
| 136 | { |
| 137 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 138 | errorcnt++; |
| 139 | } |
| 140 | break; |
| 141 | |
| 142 | case 'h': |
| 143 | Usage(); |
| 144 | return CGPT_OK; |
| 145 | case '?': |
| 146 | Error("unrecognized option: -%c\n", optopt); |
| 147 | errorcnt++; |
| 148 | break; |
| 149 | case ':': |
| 150 | Error("missing argument to -%c\n", optopt); |
| 151 | errorcnt++; |
| 152 | break; |
| 153 | default: |
| 154 | errorcnt++; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | if (errorcnt) |
| 159 | { |
| 160 | Usage(); |
| 161 | return CGPT_FAILED; |
| 162 | } |
| 163 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 164 | if (optind >= argc) |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 165 | { |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 166 | Error("missing drive argument\n"); |
| 167 | return CGPT_FAILED; |
| 168 | } |
| 169 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 170 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 171 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 172 | return CgptAdd(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 173 | } |