add.c revision 1.3 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 #include <sys/cdefs.h>
28 #ifdef __FBSDID
29 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
30 #endif
31 #ifdef __RCSID
32 __RCSID("$NetBSD: add.c,v 1.3 2006/10/17 21:14:03 he Exp $");
33 #endif
34
35 #include <sys/types.h>
36
37 #include <err.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <inttypes.h>
44
45 #include "map.h"
46 #include "gpt.h"
47
48 static uuid_t type;
49 static off_t block, size;
50 static unsigned int entry;
51
52 static void
53 usage_add(void)
54 {
55
56 fprintf(stderr,
57 "usage: %s [-b lba] [-i index] [-s lba] [-t uuid] device ...\n",
58 getprogname());
59 exit(1);
60 }
61
62 static void
63 add(int fd)
64 {
65 map_t *gpt, *tpg;
66 map_t *tbl, *lbt;
67 map_t *map;
68 struct gpt_hdr *hdr;
69 struct gpt_ent *ent;
70 unsigned int i;
71
72 gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
73 ent = NULL;
74 if (gpt == NULL) {
75 warnx("%s: error: no primary GPT header; run create or recover",
76 device_name);
77 return;
78 }
79
80 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
81 if (tpg == NULL) {
82 warnx("%s: error: no secondary GPT header; run recover",
83 device_name);
84 return;
85 }
86
87 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
88 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
89 if (tbl == NULL || lbt == NULL) {
90 warnx("%s: error: run recover -- trust me", device_name);
91 return;
92 }
93
94 hdr = gpt->map_data;
95 if (entry > le32toh(hdr->hdr_entries)) {
96 warnx("%s: error: index %u out of range (%u max)", device_name,
97 entry, le32toh(hdr->hdr_entries));
98 return;
99 }
100
101 if (entry > 0) {
102 i = entry - 1;
103 ent = (void*)((char*)tbl->map_data + i *
104 le32toh(hdr->hdr_entsz));
105 if (!uuid_is_nil((uuid_t *)&ent->ent_type, NULL)) {
106 warnx("%s: error: entry at index %u is not free",
107 device_name, entry);
108 return;
109 }
110 } else {
111 /* Find empty slot in GPT table. */
112 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
113 ent = (void*)((char*)tbl->map_data + i *
114 le32toh(hdr->hdr_entsz));
115 if (uuid_is_nil((uuid_t *)&ent->ent_type, NULL))
116 break;
117 }
118 if (i == le32toh(hdr->hdr_entries)) {
119 warnx("%s: error: no available table entries",
120 device_name);
121 return;
122 }
123 }
124
125 map = map_alloc(block, size);
126 if (map == NULL) {
127 warnx("%s: error: no space available on device", device_name);
128 return;
129 }
130
131 le_uuid_enc((uuid_t *)&ent->ent_type, &type);
132 ent->ent_lba_start = htole64(map->map_start);
133 ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
134
135 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
136 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
137 hdr->hdr_crc_self = 0;
138 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
139
140 gpt_write(fd, gpt);
141 gpt_write(fd, tbl);
142
143 hdr = tpg->map_data;
144 ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
145
146 le_uuid_enc(&ent->ent_type, &type);
147 ent->ent_lba_start = htole64(map->map_start);
148 ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
149
150 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
151 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
152 hdr->hdr_crc_self = 0;
153 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
154
155 gpt_write(fd, lbt);
156 gpt_write(fd, tpg);
157
158 #ifdef __FreeBSD__
159 printf("%sp%u added\n", device_name, i + 1);
160 #endif
161 #ifdef __NetBSD__
162 printf("Partition added, use:\n");
163 printf("\tdkctl %s addwedge dk<N> %" PRIu64 " %" PRIu64 " <type>\n",
164 device_name, map->map_start, map->map_size);
165 printf("to create a wedge for it\n");
166 #endif
167 }
168
169 int
170 cmd_add(int argc, char *argv[])
171 {
172 char *p;
173 int ch, fd;
174
175 /* Get the migrate options */
176 while ((ch = getopt(argc, argv, "b:i:s:t:")) != -1) {
177 switch(ch) {
178 case 'b':
179 if (block > 0)
180 usage_add();
181 block = strtol(optarg, &p, 10);
182 if (*p != 0 || block < 1)
183 usage_add();
184 break;
185 case 'i':
186 if (entry > 0)
187 usage_add();
188 entry = strtol(optarg, &p, 10);
189 if (*p != 0 || entry < 1)
190 usage_add();
191 break;
192 case 's':
193 if (size > 0)
194 usage_add();
195 size = strtol(optarg, &p, 10);
196 if (*p != 0 || size < 1)
197 usage_add();
198 break;
199 case 't':
200 if (!uuid_is_nil(&type, NULL))
201 usage_add();
202 if (parse_uuid(optarg, &type) != 0)
203 usage_add();
204 break;
205 default:
206 usage_add();
207 }
208 }
209
210 if (argc == optind)
211 usage_add();
212
213 /* Create UFS partitions by default. */
214 if (uuid_is_nil(&type, NULL)) {
215 uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
216 type = ufs;
217 }
218
219 while (optind < argc) {
220 fd = gpt_open(argv[optind++]);
221 if (fd == -1) {
222 warn("unable to open device '%s'", device_name);
223 continue;
224 }
225
226 add(fd);
227
228 gpt_close(fd);
229 }
230
231 return (0);
232 }
233