blob: 2bb5df43903558760f0e2218c4f49a31ad5872fc [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"
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
35int cmd_add(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080036
37 CgptAddParams params;
38 memset(&params, 0, sizeof(params));
Bill Richardsonf1372d92010-06-11 09:15:55 -070039
Bill Richardsonf1372d92010-06-11 09:15:55 -070040 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 Srinivasana0581432012-01-26 21:50:05 -080050 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070051 if (!*optarg || (e && *e))
52 {
53 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
54 errorcnt++;
55 }
56 break;
57 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080058 params.set_begin = 1;
59 params.begin = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070060 if (!*optarg || (e && *e))
61 {
62 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
63 errorcnt++;
64 }
65 break;
66 case 's':
Jay Srinivasana0581432012-01-26 21:50:05 -080067 params.set_size = 1;
68 params.size = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070069 if (!*optarg || (e && *e))
70 {
71 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
72 errorcnt++;
73 }
74 break;
75 case 't':
Jay Srinivasana0581432012-01-26 21:50:05 -080076 params.set_type = 1;
77 if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
78 CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070079 Error("invalid argument to -%c: %s\n", c, optarg);
80 errorcnt++;
81 }
82 break;
83 case 'u':
Jay Srinivasana0581432012-01-26 21:50:05 -080084 params.set_unique = 1;
85 if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070086 Error("invalid argument to -%c: %s\n", c, optarg);
87 errorcnt++;
88 }
89 break;
90 case 'l':
Jay Srinivasana0581432012-01-26 21:50:05 -080091 params.label = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -070092 break;
93 case 'S':
Jay Srinivasana0581432012-01-26 21:50:05 -080094 params.set_successful = 1;
95 params.successful = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070096 if (!*optarg || (e && *e))
97 {
98 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
99 errorcnt++;
100 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800101 if (params.successful < 0 || params.successful > 1) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700102 Error("value for -%c must be between 0 and 1", c);
103 errorcnt++;
104 }
105 break;
106 case 'T':
Jay Srinivasana0581432012-01-26 21:50:05 -0800107 params.set_tries = 1;
108 params.tries = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700109 if (!*optarg || (e && *e))
110 {
111 fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
112 progname, c, optarg);
113 errorcnt++;
114 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800115 if (params.tries < 0 || params.tries > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700116 Error("value for -%c must be between 0 and 15", c);
117 errorcnt++;
118 }
119 break;
120 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -0800121 params.set_priority = 1;
122 params.priority = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700123 if (!*optarg || (e && *e))
124 {
125 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
126 errorcnt++;
127 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800128 if (params.priority < 0 || params.priority > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700129 Error("value for -%c must be between 0 and 15", c);
130 errorcnt++;
131 }
132 break;
133 case 'A':
Jay Srinivasana0581432012-01-26 21:50:05 -0800134 params.set_raw = 1;
135 params.raw_value = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700136 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 Srinivasan250549d2012-02-16 17:40:45 -0800165 if (optind >= argc)
Jay Srinivasana0581432012-01-26 21:50:05 -0800166 {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700167 Error("missing drive argument\n");
168 return CGPT_FAILED;
169 }
170
Jay Srinivasan250549d2012-02-16 17:40:45 -0800171 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -0700172
Bill Richardson3f806a22013-03-20 15:02:34 -0700173 return CgptAdd(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700174}