blob: 0d89065b930da20761c4b3fe8a7e76265fa6d6e5 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardsonf1372d92010-06-11 09:15:55 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bill Richardsonf1372d92010-06-11 09:15:55 -07005#include <getopt.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07006#include <string.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07007
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include "cgpt.h"
9#include "vboot_host.h"
Bill Richardsonf1372d92010-06-11 09:15:55 -070010
Bill Richardson4cb54972014-06-20 14:33:00 -070011extern const char* progname;
12
Bill Richardsonf1372d92010-06-11 09:15:55 -070013static 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"
Nam T. Nguyenab899592014-11-13 19:30:46 -080018 " -D NUM Size (in bytes) of the disk where partitions reside\n"
19 " default 0, meaning partitions and GPT structs are\n"
20 " both on DRIVE\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070021 " -i NUM Specify partition (default is next available)\n"
22 " -b NUM Beginning sector\n"
23 " -s NUM Size in sectors\n"
24 " -t GUID Partition Type GUID\n"
25 " -u GUID Partition Unique ID\n"
26 " -l LABEL Label\n"
27 " -S NUM set Successful flag (0|1)\n"
28 " -T NUM set Tries flag (0-15)\n"
29 " -P NUM set Priority flag (0-15)\n"
30 " -A NUM set raw 64-bit attribute value\n"
31 "\n"
32 "Use the -i option to modify an existing partition.\n"
33 "The -b, -s, and -t options must be given for new partitions.\n"
34 "\n", progname);
35 PrintTypes();
36}
37
38int cmd_add(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080039
40 CgptAddParams params;
41 memset(&params, 0, sizeof(params));
Bill Richardsonf1372d92010-06-11 09:15:55 -070042
Bill Richardsonf1372d92010-06-11 09:15:55 -070043 int c;
44 int errorcnt = 0;
45 char *e = 0;
46
47 opterr = 0; // quiet, you
Nam T. Nguyenab899592014-11-13 19:30:46 -080048 while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:D:")) != -1)
Bill Richardsonf1372d92010-06-11 09:15:55 -070049 {
50 switch (c)
51 {
Nam T. Nguyenab899592014-11-13 19:30:46 -080052 case 'D':
53 params.drive_size = strtoull(optarg, &e, 0);
54 if (!*optarg || (e && *e))
55 {
56 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
57 errorcnt++;
58 }
59 break;
Bill Richardsonf1372d92010-06-11 09:15:55 -070060 case 'i':
Jay Srinivasana0581432012-01-26 21:50:05 -080061 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070062 if (!*optarg || (e && *e))
63 {
64 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
65 errorcnt++;
66 }
67 break;
68 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080069 params.set_begin = 1;
70 params.begin = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070071 if (!*optarg || (e && *e))
72 {
73 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
74 errorcnt++;
75 }
76 break;
77 case 's':
Jay Srinivasana0581432012-01-26 21:50:05 -080078 params.set_size = 1;
79 params.size = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070080 if (!*optarg || (e && *e))
81 {
82 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
83 errorcnt++;
84 }
85 break;
86 case 't':
Jay Srinivasana0581432012-01-26 21:50:05 -080087 params.set_type = 1;
88 if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
89 CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070090 Error("invalid argument to -%c: %s\n", c, optarg);
91 errorcnt++;
92 }
93 break;
94 case 'u':
Jay Srinivasana0581432012-01-26 21:50:05 -080095 params.set_unique = 1;
96 if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070097 Error("invalid argument to -%c: %s\n", c, optarg);
98 errorcnt++;
99 }
100 break;
101 case 'l':
Jay Srinivasana0581432012-01-26 21:50:05 -0800102 params.label = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -0700103 break;
104 case 'S':
Jay Srinivasana0581432012-01-26 21:50:05 -0800105 params.set_successful = 1;
106 params.successful = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700107 if (!*optarg || (e && *e))
108 {
109 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
110 errorcnt++;
111 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800112 if (params.successful < 0 || params.successful > 1) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700113 Error("value for -%c must be between 0 and 1", c);
114 errorcnt++;
115 }
116 break;
117 case 'T':
Jay Srinivasana0581432012-01-26 21:50:05 -0800118 params.set_tries = 1;
119 params.tries = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700120 if (!*optarg || (e && *e))
121 {
122 fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
123 progname, c, optarg);
124 errorcnt++;
125 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800126 if (params.tries < 0 || params.tries > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700127 Error("value for -%c must be between 0 and 15", c);
128 errorcnt++;
129 }
130 break;
131 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -0800132 params.set_priority = 1;
133 params.priority = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700134 if (!*optarg || (e && *e))
135 {
136 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
137 errorcnt++;
138 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800139 if (params.priority < 0 || params.priority > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700140 Error("value for -%c must be between 0 and 15", c);
141 errorcnt++;
142 }
143 break;
144 case 'A':
Jay Srinivasana0581432012-01-26 21:50:05 -0800145 params.set_raw = 1;
146 params.raw_value = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700147 if (!*optarg || (e && *e))
148 {
149 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
150 errorcnt++;
151 }
152 break;
153
154 case 'h':
155 Usage();
156 return CGPT_OK;
157 case '?':
158 Error("unrecognized option: -%c\n", optopt);
159 errorcnt++;
160 break;
161 case ':':
162 Error("missing argument to -%c\n", optopt);
163 errorcnt++;
164 break;
165 default:
166 errorcnt++;
167 break;
168 }
169 }
170 if (errorcnt)
171 {
172 Usage();
173 return CGPT_FAILED;
174 }
175
Jay Srinivasan250549d2012-02-16 17:40:45 -0800176 if (optind >= argc)
Jay Srinivasana0581432012-01-26 21:50:05 -0800177 {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700178 Error("missing drive argument\n");
179 return CGPT_FAILED;
180 }
181
Jay Srinivasan250549d2012-02-16 17:40:45 -0800182 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -0700183
Bill Richardson3f806a22013-03-20 15:02:34 -0700184 return CgptAdd(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700185}