create.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/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
30 #endif
31 #ifdef __RCSID
32 __RCSID("$NetBSD: create.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
44 #include "map.h"
45 #include "gpt.h"
46
47 static int force;
48 static int primary_only;
49
50 static void
51 usage_create(void)
52 {
53
54 fprintf(stderr,
55 "usage: %s [-fp] device ...\n", getprogname());
56 exit(1);
57 }
58
59 static void
60 create(int fd)
61 {
62 uuid_t uuid;
63 off_t blocks, last;
64 map_t *gpt, *tpg;
65 map_t *tbl, *lbt;
66 map_t *map;
67 struct mbr *mbr;
68 struct gpt_hdr *hdr;
69 struct gpt_ent *ent;
70 unsigned int i;
71
72 last = mediasz / secsz - 1LL;
73
74 if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
75 map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
76 warnx("%s: error: device already contains a GPT", device_name);
77 return;
78 }
79 map = map_find(MAP_TYPE_MBR);
80 if (map != NULL) {
81 if (!force) {
82 warnx("%s: error: device contains a MBR", device_name);
83 return;
84 }
85
86 /* Nuke the MBR in our internal map. */
87 map->map_type = MAP_TYPE_UNUSED;
88 }
89
90 /*
91 * Create PMBR.
92 */
93 if (map_find(MAP_TYPE_PMBR) == NULL) {
94 if (map_free(0LL, 1LL) == 0) {
95 warnx("%s: error: no room for the PMBR", device_name);
96 return;
97 }
98 mbr = gpt_read(fd, 0LL, 1);
99 bzero(mbr, sizeof(*mbr));
100 mbr->mbr_sig = htole16(MBR_SIG);
101 mbr->mbr_part[0].part_shd = 0xff;
102 mbr->mbr_part[0].part_ssect = 0xff;
103 mbr->mbr_part[0].part_scyl = 0xff;
104 mbr->mbr_part[0].part_typ = 0xee;
105 mbr->mbr_part[0].part_ehd = 0xff;
106 mbr->mbr_part[0].part_esect = 0xff;
107 mbr->mbr_part[0].part_ecyl = 0xff;
108 mbr->mbr_part[0].part_start_lo = htole16(1);
109 if (last > 0xffffffff) {
110 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
111 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
112 } else {
113 mbr->mbr_part[0].part_size_lo = htole16(last);
114 mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
115 }
116 map = map_add(0LL, 1LL, MAP_TYPE_PMBR, mbr);
117 gpt_write(fd, map);
118 }
119
120 /* Get the amount of free space after the MBR */
121 blocks = map_free(1LL, 0LL);
122 if (blocks == 0LL) {
123 warnx("%s: error: no room for the GPT header", device_name);
124 return;
125 }
126
127 /* Don't create more than parts entries. */
128 if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
129 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
130 if ((parts * sizeof(struct gpt_ent)) % secsz)
131 blocks++;
132 blocks++; /* Don't forget the header itself */
133 }
134
135 /* Never cross the median of the device. */
136 if ((blocks + 1LL) > ((last + 1LL) >> 1))
137 blocks = ((last + 1LL) >> 1) - 1LL;
138
139 /*
140 * Get the amount of free space at the end of the device and
141 * calculate the size for the GPT structures.
142 */
143 map = map_last();
144 if (map->map_type != MAP_TYPE_UNUSED) {
145 warnx("%s: error: no room for the backup header", device_name);
146 return;
147 }
148
149 if (map->map_size < blocks)
150 blocks = map->map_size;
151 if (blocks == 1LL) {
152 warnx("%s: error: no room for the GPT table", device_name);
153 return;
154 }
155
156 blocks--; /* Number of blocks in the GPT table. */
157 gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
158 tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
159 calloc(blocks, secsz));
160 if (gpt == NULL || tbl == NULL)
161 return;
162
163 hdr = gpt->map_data;
164 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
165 hdr->hdr_revision = htole32(GPT_HDR_REVISION);
166 hdr->hdr_size = htole32(GPT_SIZE);
167 hdr->hdr_lba_self = htole64(gpt->map_start);
168 hdr->hdr_lba_alt = htole64(last);
169 hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
170 hdr->hdr_lba_end = htole64(last - blocks - 1LL);
171 uuid_create(&uuid, NULL);
172 le_uuid_enc((uuid_t *)&hdr->hdr_uuid, &uuid);
173 hdr->hdr_lba_table = htole64(tbl->map_start);
174 hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
175 if (le32toh(hdr->hdr_entries) > parts)
176 hdr->hdr_entries = htole32(parts);
177 hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
178
179 ent = tbl->map_data;
180 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
181 uuid_create(&uuid, NULL);
182 le_uuid_enc((uuid_t *)&ent[i].ent_uuid, &uuid);
183 }
184
185 hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
186 le32toh(hdr->hdr_entsz)));
187 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
188
189 gpt_write(fd, gpt);
190 gpt_write(fd, tbl);
191
192 /*
193 * Create backup GPT if the user didn't suppress it.
194 */
195 if (!primary_only) {
196 tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
197 calloc(1, secsz));
198 lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
199 tbl->map_data);
200 memcpy(tpg->map_data, gpt->map_data, secsz);
201 hdr = tpg->map_data;
202 hdr->hdr_lba_self = htole64(tpg->map_start);
203 hdr->hdr_lba_alt = htole64(gpt->map_start);
204 hdr->hdr_lba_table = htole64(lbt->map_start);
205 hdr->hdr_crc_self = 0; /* Don't ever forget this! */
206 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
207 gpt_write(fd, lbt);
208 gpt_write(fd, tpg);
209 }
210 }
211
212 int
213 cmd_create(int argc, char *argv[])
214 {
215 int ch, fd;
216
217 while ((ch = getopt(argc, argv, "fp")) != -1) {
218 switch(ch) {
219 case 'f':
220 force = 1;
221 break;
222 case 'p':
223 primary_only = 1;
224 break;
225 default:
226 usage_create();
227 }
228 }
229
230 if (argc == optind)
231 usage_create();
232
233 while (optind < argc) {
234 fd = gpt_open(argv[optind++]);
235 if (fd == -1) {
236 warn("unable to open device '%s'", device_name);
237 continue;
238 }
239
240 create(fd);
241
242 gpt_close(fd);
243 }
244
245 return (0);
246 }
247