add.c revision 1.32 1 /*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
29 #endif
30
31 #include <sys/cdefs.h>
32 #ifdef __FBSDID
33 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: add.c,v 1.32 2015/12/01 16:32:19 christos Exp $");
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/stat.h>
42
43 #include <err.h>
44 #include <stddef.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include "map.h"
51 #include "gpt.h"
52 #include "gpt_private.h"
53
54 static gpt_uuid_t type;
55 static off_t alignment, block, sectors, size;
56 static unsigned int entry;
57 static uint8_t *name;
58 static int cmd_add(gpt_t, int, char *[]);
59
60 static const char *addhelp[] = {
61 "[-a alignment] [-b blocknr] [-i index] [-l label]",
62 "[-s size] [-t type]",
63 };
64
65 struct gpt_cmd c_add = {
66 "add",
67 cmd_add,
68 addhelp, __arraycount(addhelp),
69 0,
70 };
71
72 #define usage() gpt_usage(NULL, &c_add)
73
74 static int
75 add(gpt_t gpt)
76 {
77 map_t map;
78 struct gpt_hdr *hdr;
79 struct gpt_ent *ent, e;
80 unsigned int i;
81 off_t alignsecs;
82
83 if ((hdr = gpt_hdr(gpt)) == NULL)
84 return -1;
85
86 ent = NULL;
87
88 if (entry > le32toh(hdr->hdr_entries)) {
89 gpt_warnx(gpt, "index %u out of range (%u max)",
90 entry, le32toh(hdr->hdr_entries));
91 return -1;
92 }
93
94 if (entry > 0) {
95 i = entry - 1;
96 ent = gpt_ent_primary(gpt, i);
97 if (!gpt_uuid_is_nil(ent->ent_type)) {
98 gpt_warnx(gpt, "Entry at index %u is not free", entry);
99 return -1;
100 }
101 } else {
102 /* Find empty slot in GPT table. */
103 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
104 ent = gpt_ent_primary(gpt, i);
105 if (gpt_uuid_is_nil(ent->ent_type))
106 break;
107 }
108 if (i == le32toh(hdr->hdr_entries)) {
109 gpt_warnx(gpt, "No available table entries");
110 return -1;
111 }
112 }
113
114 if (alignment > 0) {
115 alignsecs = alignment / gpt->secsz;
116 map = map_alloc(gpt, block, sectors, alignsecs);
117 if (map == NULL) {
118 gpt_warnx(gpt, "Not enough space available on "
119 "device for an aligned partition");
120 return -1;
121 }
122 } else {
123 map = map_alloc(gpt, block, sectors, 0);
124 if (map == NULL) {
125 gpt_warnx(gpt, "Not enough space available on device");
126 return -1;
127 }
128 }
129
130 memset(&e, 0, sizeof(e));
131 gpt_uuid_copy(e.ent_type, type);
132 e.ent_lba_start = htole64(map->map_start);
133 e.ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
134 if (name != NULL)
135 utf8_to_utf16(name, e.ent_name, 36);
136
137 memcpy(ent, &e, sizeof(e));
138 gpt_write_primary(gpt);
139
140 ent = gpt_ent_backup(gpt, i);
141 memcpy(ent, &e, sizeof(e));
142 gpt_write_backup(gpt);
143
144 gpt_msg(gpt, "Partition %d added: %s %" PRIu64 " %" PRIu64 "\n", i + 1,
145 type, map->map_start, map->map_size);
146 return 0;
147 }
148
149 static int
150 cmd_add(gpt_t gpt, int argc, char *argv[])
151 {
152 char *p;
153 int ch;
154 int64_t human_num;
155
156 while ((ch = getopt(argc, argv, "a:b:i:l:s:t:")) != -1) {
157 switch(ch) {
158 case 'a':
159 if (alignment > 0)
160 return usage();
161 if (dehumanize_number(optarg, &human_num) < 0)
162 return usage();
163 alignment = human_num;
164 if (alignment < 1)
165 return usage();
166 break;
167 case 'b':
168 if (block > 0)
169 return usage();
170 if (dehumanize_number(optarg, &human_num) < 0)
171 return usage();
172 block = human_num;
173 if (block < 1)
174 return usage();
175 break;
176 case 'i':
177 if (entry > 0)
178 usage();
179 entry = strtoul(optarg, &p, 10);
180 if (*p != 0 || entry < 1)
181 return usage();
182 break;
183 case 'l':
184 if (name != NULL)
185 return usage();
186 name = (uint8_t *)strdup(optarg);
187 break;
188 case 's':
189 if (sectors > 0 || size > 0)
190 return usage();
191 sectors = strtoll(optarg, &p, 10);
192 if (sectors < 1)
193 return usage();
194 if (*p == '\0')
195 break;
196 if (*p == 's' || *p == 'S') {
197 if (*(p + 1) == '\0')
198 break;
199 else
200 return usage();
201 }
202 if (*p == 'b' || *p == 'B') {
203 if (*(p + 1) == '\0') {
204 size = sectors;
205 sectors = 0;
206 break;
207 } else
208 return usage();
209 }
210 if (dehumanize_number(optarg, &human_num) < 0)
211 return usage();
212 size = human_num;
213 sectors = 0;
214 break;
215 case 't':
216 if (!gpt_uuid_is_nil(type))
217 return usage();
218 if (gpt_uuid_parse(optarg, type) != 0)
219 return usage();
220 break;
221 default:
222 return usage();
223 }
224 }
225
226 if (argc == optind)
227 return usage();
228
229 /* Create NetBSD FFS partitions by default. */
230 if (gpt_uuid_is_nil(type)) {
231 gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
232 }
233
234 if (optind != argc)
235 return usage();
236
237 if ((sectors = gpt_check(gpt, alignment, size)) == -1)
238 return -1;
239
240 return add(gpt);
241 }
242