blob: 500633b040267a64592f636d3c8b0806b5f030fc [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
5#include "cgpt.h"
6
7#include <getopt.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07008#include <string.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -07009
Jay Srinivasana0581432012-01-26 21:50:05 -080010#include "cgpt_params.h"
Bill Richardsonf1372d92010-06-11 09:15:55 -070011
12static 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
34int cmd_add(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080035
36 CgptAddParams params;
37 memset(&params, 0, sizeof(params));
Bill Richardsonf1372d92010-06-11 09:15:55 -070038
Bill Richardsonf1372d92010-06-11 09:15:55 -070039 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 Srinivasana0581432012-01-26 21:50:05 -080049 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070050 if (!*optarg || (e && *e))
51 {
52 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
53 errorcnt++;
54 }
55 break;
56 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080057 params.set_begin = 1;
58 params.begin = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070059 if (!*optarg || (e && *e))
60 {
61 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
62 errorcnt++;
63 }
64 break;
65 case 's':
Jay Srinivasana0581432012-01-26 21:50:05 -080066 params.set_size = 1;
67 params.size = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070068 if (!*optarg || (e && *e))
69 {
70 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
71 errorcnt++;
72 }
73 break;
74 case 't':
Jay Srinivasana0581432012-01-26 21:50:05 -080075 params.set_type = 1;
76 if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
77 CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070078 Error("invalid argument to -%c: %s\n", c, optarg);
79 errorcnt++;
80 }
81 break;
82 case 'u':
Jay Srinivasana0581432012-01-26 21:50:05 -080083 params.set_unique = 1;
84 if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070085 Error("invalid argument to -%c: %s\n", c, optarg);
86 errorcnt++;
87 }
88 break;
89 case 'l':
Jay Srinivasana0581432012-01-26 21:50:05 -080090 params.label = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -070091 break;
92 case 'S':
Jay Srinivasana0581432012-01-26 21:50:05 -080093 params.set_successful = 1;
94 params.successful = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070095 if (!*optarg || (e && *e))
96 {
97 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
98 errorcnt++;
99 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800100 if (params.successful < 0 || params.successful > 1) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700101 Error("value for -%c must be between 0 and 1", c);
102 errorcnt++;
103 }
104 break;
105 case 'T':
Jay Srinivasana0581432012-01-26 21:50:05 -0800106 params.set_tries = 1;
107 params.tries = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700108 if (!*optarg || (e && *e))
109 {
110 fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
111 progname, c, optarg);
112 errorcnt++;
113 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800114 if (params.tries < 0 || params.tries > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700115 Error("value for -%c must be between 0 and 15", c);
116 errorcnt++;
117 }
118 break;
119 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -0800120 params.set_priority = 1;
121 params.priority = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700122 if (!*optarg || (e && *e))
123 {
124 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
125 errorcnt++;
126 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800127 if (params.priority < 0 || params.priority > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700128 Error("value for -%c must be between 0 and 15", c);
129 errorcnt++;
130 }
131 break;
132 case 'A':
Jay Srinivasana0581432012-01-26 21:50:05 -0800133 params.set_raw = 1;
134 params.raw_value = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700135 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 Srinivasan250549d2012-02-16 17:40:45 -0800164 if (optind >= argc)
Jay Srinivasana0581432012-01-26 21:50:05 -0800165 {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700166 Error("missing drive argument\n");
167 return CGPT_FAILED;
168 }
169
Jay Srinivasan250549d2012-02-16 17:40:45 -0800170 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -0700171
Bill Richardson3f806a22013-03-20 15:02:34 -0700172 return CgptAdd(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700173}