blob: a48ab3362d4d97176c005d26968b7c2faeee0146 [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
11static void Usage(void)
12{
13 printf("\nUsage: %s add [OPTIONS] DRIVE\n\n"
14 "Add, edit, or remove a partition entry.\n\n"
15 "Options:\n"
16 " -i NUM Specify partition (default is next available)\n"
17 " -b NUM Beginning sector\n"
18 " -s NUM Size in sectors\n"
19 " -t GUID Partition Type GUID\n"
20 " -u GUID Partition Unique ID\n"
21 " -l LABEL Label\n"
22 " -S NUM set Successful flag (0|1)\n"
23 " -T NUM set Tries flag (0-15)\n"
24 " -P NUM set Priority flag (0-15)\n"
25 " -A NUM set raw 64-bit attribute value\n"
26 "\n"
27 "Use the -i option to modify an existing partition.\n"
28 "The -b, -s, and -t options must be given for new partitions.\n"
29 "\n", progname);
30 PrintTypes();
31}
32
33int cmd_add(int argc, char *argv[]) {
Jay Srinivasana0581432012-01-26 21:50:05 -080034
35 CgptAddParams params;
36 memset(&params, 0, sizeof(params));
Bill Richardsonf1372d92010-06-11 09:15:55 -070037
Bill Richardsonf1372d92010-06-11 09:15:55 -070038 int c;
39 int errorcnt = 0;
40 char *e = 0;
41
42 opterr = 0; // quiet, you
43 while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:")) != -1)
44 {
45 switch (c)
46 {
47 case 'i':
Jay Srinivasana0581432012-01-26 21:50:05 -080048 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070049 if (!*optarg || (e && *e))
50 {
51 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
52 errorcnt++;
53 }
54 break;
55 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080056 params.set_begin = 1;
57 params.begin = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070058 if (!*optarg || (e && *e))
59 {
60 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
61 errorcnt++;
62 }
63 break;
64 case 's':
Jay Srinivasana0581432012-01-26 21:50:05 -080065 params.set_size = 1;
66 params.size = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070067 if (!*optarg || (e && *e))
68 {
69 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
70 errorcnt++;
71 }
72 break;
73 case 't':
Jay Srinivasana0581432012-01-26 21:50:05 -080074 params.set_type = 1;
75 if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
76 CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070077 Error("invalid argument to -%c: %s\n", c, optarg);
78 errorcnt++;
79 }
80 break;
81 case 'u':
Jay Srinivasana0581432012-01-26 21:50:05 -080082 params.set_unique = 1;
83 if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070084 Error("invalid argument to -%c: %s\n", c, optarg);
85 errorcnt++;
86 }
87 break;
88 case 'l':
Jay Srinivasana0581432012-01-26 21:50:05 -080089 params.label = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -070090 break;
91 case 'S':
Jay Srinivasana0581432012-01-26 21:50:05 -080092 params.set_successful = 1;
93 params.successful = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070094 if (!*optarg || (e && *e))
95 {
96 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
97 errorcnt++;
98 }
Jay Srinivasana0581432012-01-26 21:50:05 -080099 if (params.successful < 0 || params.successful > 1) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700100 Error("value for -%c must be between 0 and 1", c);
101 errorcnt++;
102 }
103 break;
104 case 'T':
Jay Srinivasana0581432012-01-26 21:50:05 -0800105 params.set_tries = 1;
106 params.tries = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700107 if (!*optarg || (e && *e))
108 {
109 fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
110 progname, c, optarg);
111 errorcnt++;
112 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800113 if (params.tries < 0 || params.tries > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700114 Error("value for -%c must be between 0 and 15", c);
115 errorcnt++;
116 }
117 break;
118 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -0800119 params.set_priority = 1;
120 params.priority = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700121 if (!*optarg || (e && *e))
122 {
123 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
124 errorcnt++;
125 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800126 if (params.priority < 0 || params.priority > 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 'A':
Jay Srinivasana0581432012-01-26 21:50:05 -0800132 params.set_raw = 1;
133 params.raw_value = strtoull(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 }
139 break;
140
141 case 'h':
142 Usage();
143 return CGPT_OK;
144 case '?':
145 Error("unrecognized option: -%c\n", optopt);
146 errorcnt++;
147 break;
148 case ':':
149 Error("missing argument to -%c\n", optopt);
150 errorcnt++;
151 break;
152 default:
153 errorcnt++;
154 break;
155 }
156 }
157 if (errorcnt)
158 {
159 Usage();
160 return CGPT_FAILED;
161 }
162
Jay Srinivasan250549d2012-02-16 17:40:45 -0800163 if (optind >= argc)
Jay Srinivasana0581432012-01-26 21:50:05 -0800164 {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700165 Error("missing drive argument\n");
166 return CGPT_FAILED;
167 }
168
Jay Srinivasan250549d2012-02-16 17:40:45 -0800169 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -0700170
Bill Richardson3f806a22013-03-20 15:02:34 -0700171 return CgptAdd(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700172}