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