blob: 62b15996df2a3ae4e4e6b96c9a603a9a4ea88088 [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
39 int gpt_retval;
40 GptEntry *entry;
Bill Richardsonc4e92af2010-10-12 07:33:15 -070041 uint32_t index;
42
Bill Richardsonf1372d92010-06-11 09:15:55 -070043 int c;
44 int errorcnt = 0;
45 char *e = 0;
46
47 opterr = 0; // quiet, you
48 while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:A:")) != -1)
49 {
50 switch (c)
51 {
52 case 'i':
Jay Srinivasana0581432012-01-26 21:50:05 -080053 params.partition = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070054 if (!*optarg || (e && *e))
55 {
56 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
57 errorcnt++;
58 }
59 break;
60 case 'b':
Jay Srinivasana0581432012-01-26 21:50:05 -080061 params.set_begin = 1;
62 params.begin = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070063 if (!*optarg || (e && *e))
64 {
65 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
66 errorcnt++;
67 }
68 break;
69 case 's':
Jay Srinivasana0581432012-01-26 21:50:05 -080070 params.set_size = 1;
71 params.size = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070072 if (!*optarg || (e && *e))
73 {
74 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
75 errorcnt++;
76 }
77 break;
78 case 't':
Jay Srinivasana0581432012-01-26 21:50:05 -080079 params.set_type = 1;
80 if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
81 CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070082 Error("invalid argument to -%c: %s\n", c, optarg);
83 errorcnt++;
84 }
85 break;
86 case 'u':
Jay Srinivasana0581432012-01-26 21:50:05 -080087 params.set_unique = 1;
88 if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
Bill Richardsonf1372d92010-06-11 09:15:55 -070089 Error("invalid argument to -%c: %s\n", c, optarg);
90 errorcnt++;
91 }
92 break;
93 case 'l':
Jay Srinivasana0581432012-01-26 21:50:05 -080094 params.label = optarg;
Bill Richardsonf1372d92010-06-11 09:15:55 -070095 break;
96 case 'S':
Jay Srinivasana0581432012-01-26 21:50:05 -080097 params.set_successful = 1;
98 params.successful = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -070099 if (!*optarg || (e && *e))
100 {
101 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
102 errorcnt++;
103 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800104 if (params.successful < 0 || params.successful > 1) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700105 Error("value for -%c must be between 0 and 1", c);
106 errorcnt++;
107 }
108 break;
109 case 'T':
Jay Srinivasana0581432012-01-26 21:50:05 -0800110 params.set_tries = 1;
111 params.tries = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700112 if (!*optarg || (e && *e))
113 {
114 fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
115 progname, c, optarg);
116 errorcnt++;
117 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800118 if (params.tries < 0 || params.tries > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700119 Error("value for -%c must be between 0 and 15", c);
120 errorcnt++;
121 }
122 break;
123 case 'P':
Jay Srinivasana0581432012-01-26 21:50:05 -0800124 params.set_priority = 1;
125 params.priority = (uint32_t)strtoul(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700126 if (!*optarg || (e && *e))
127 {
128 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
129 errorcnt++;
130 }
Jay Srinivasana0581432012-01-26 21:50:05 -0800131 if (params.priority < 0 || params.priority > 15) {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700132 Error("value for -%c must be between 0 and 15", c);
133 errorcnt++;
134 }
135 break;
136 case 'A':
Jay Srinivasana0581432012-01-26 21:50:05 -0800137 params.set_raw = 1;
138 params.raw_value = strtoull(optarg, &e, 0);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700139 if (!*optarg || (e && *e))
140 {
141 Error("invalid argument to -%c: \"%s\"\n", c, optarg);
142 errorcnt++;
143 }
144 break;
145
146 case 'h':
147 Usage();
148 return CGPT_OK;
149 case '?':
150 Error("unrecognized option: -%c\n", optopt);
151 errorcnt++;
152 break;
153 case ':':
154 Error("missing argument to -%c\n", optopt);
155 errorcnt++;
156 break;
157 default:
158 errorcnt++;
159 break;
160 }
161 }
162 if (errorcnt)
163 {
164 Usage();
165 return CGPT_FAILED;
166 }
167
Jay Srinivasan250549d2012-02-16 17:40:45 -0800168 if (optind >= argc)
Jay Srinivasana0581432012-01-26 21:50:05 -0800169 {
Bill Richardsonf1372d92010-06-11 09:15:55 -0700170 Error("missing drive argument\n");
171 return CGPT_FAILED;
172 }
173
Jay Srinivasan250549d2012-02-16 17:40:45 -0800174 params.drive_name = argv[optind];
Bill Richardsonf1372d92010-06-11 09:15:55 -0700175
Jay Srinivasana0581432012-01-26 21:50:05 -0800176 return cgpt_add(&params);
Bill Richardsonf1372d92010-06-11 09:15:55 -0700177}