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