add.c revision 1.2 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.2 2006/10/15 22:36:29 christos 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 if (gpt == NULL) {
74 warnx("%s: error: no primary GPT header; run create or recover",
75 device_name);
76 return;
77 }
78
79 tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
80 if (tpg == NULL) {
81 warnx("%s: error: no secondary GPT header; run recover",
82 device_name);
83 return;
84 }
85
86 tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
87 lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
88 if (tbl == NULL || lbt == NULL) {
89 warnx("%s: error: run recover -- trust me", device_name);
90 return;
91 }
92
93 hdr = gpt->map_data;
94 if (entry > le32toh(hdr->hdr_entries)) {
95 warnx("%s: error: index %u out of range (%u max)", device_name,
96 entry, le32toh(hdr->hdr_entries));
97 return;
98 }
99
100 if (entry > 0) {
101 i = entry - 1;
102 ent = (void*)((char*)tbl->map_data + i *
103 le32toh(hdr->hdr_entsz));
104 if (!uuid_is_nil((uuid_t *)&ent->ent_type, NULL)) {
105 warnx("%s: error: entry at index %u is not free",
106 device_name, entry);
107 return;
108 }
109 } else {
110 /* Find empty slot in GPT table. */
111 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
112 ent = (void*)((char*)tbl->map_data + i *
113 le32toh(hdr->hdr_entsz));
114 if (uuid_is_nil((uuid_t *)&ent->ent_type, NULL))
115 break;
116 }
117 if (i == le32toh(hdr->hdr_entries)) {
118 warnx("%s: error: no available table entries",
119 device_name);
120 return;
121 }
122 }
123
124 map = map_alloc(block, size);
125 if (map == NULL) {
126 warnx("%s: error: no space available on device", device_name);
127 return;
128 }
129
130 le_uuid_enc((uuid_t *)&ent->ent_type, &type);
131 ent->ent_lba_start = htole64(map->map_start);
132 ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
133
134 hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
135 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
136 hdr->hdr_crc_self = 0;
137 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
138
139 gpt_write(fd, gpt);
140 gpt_write(fd, tbl);
141
142 hdr = tpg->map_data;
143 ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
144
145 le_uuid_enc(&ent->ent_type, &type);
146 ent->ent_lba_start = htole64(map->map_start);
147 ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
148
149 hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
150 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
151 hdr->hdr_crc_self = 0;
152 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
153
154 gpt_write(fd, lbt);
155 gpt_write(fd, tpg);
156
157 #ifdef __FreeBSD__
158 printf("%sp%u added\n", device_name, i + 1);
159 #endif
160 #ifdef __NetBSD__
161 printf("Partition added, use:\n");
162 printf("\tdkctl %s addwedge dk<N> %" PRIu64 " %" PRIu64 " <type>\n",
163 device_name, map->map_start, map->map_size);
164 printf("to create a wedge for it\n");
165 #endif
166 }
167
168 int
169 cmd_add(int argc, char *argv[])
170 {
171 char *p;
172 int ch, fd;
173
174 /* Get the migrate options */
175 while ((ch = getopt(argc, argv, "b:i:s:t:")) != -1) {
176 switch(ch) {
177 case 'b':
178 if (block > 0)
179 usage_add();
180 block = strtol(optarg, &p, 10);
181 if (*p != 0 || block < 1)
182 usage_add();
183 break;
184 case 'i':
185 if (entry > 0)
186 usage_add();
187 entry = strtol(optarg, &p, 10);
188 if (*p != 0 || entry < 1)
189 usage_add();
190 break;
191 case 's':
192 if (size > 0)
193 usage_add();
194 size = strtol(optarg, &p, 10);
195 if (*p != 0 || size < 1)
196 usage_add();
197 break;
198 case 't':
199 if (!uuid_is_nil(&type, NULL))
200 usage_add();
201 if (parse_uuid(optarg, &type) != 0)
202 usage_add();
203 break;
204 default:
205 usage_add();
206 }
207 }
208
209 if (argc == optind)
210 usage_add();
211
212 /* Create UFS partitions by default. */
213 if (uuid_is_nil(&type, NULL)) {
214 uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
215 type = ufs;
216 }
217
218 while (optind < argc) {
219 fd = gpt_open(argv[optind++]);
220 if (fd == -1) {
221 warn("unable to open device '%s'", device_name);
222 continue;
223 }
224
225 add(fd);
226
227 gpt_close(fd);
228 }
229
230 return (0);
231 }
232