create.c revision 1.14 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 2002 Marcel Moolenaar
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that the following conditions
7 1.1 christos * are met:
8 1.1 christos *
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos *
15 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 christos */
26 1.1 christos
27 1.8 christos #if HAVE_NBTOOL_CONFIG_H
28 1.8 christos #include "nbtool_config.h"
29 1.8 christos #endif
30 1.8 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.2 christos #ifdef __FBSDID
33 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
34 1.2 christos #endif
35 1.2 christos #ifdef __RCSID
36 1.14 christos __RCSID("$NetBSD: create.c,v 1.14 2015/12/01 09:05:33 christos Exp $");
37 1.2 christos #endif
38 1.1 christos
39 1.1 christos #include <sys/types.h>
40 1.14 christos #include <sys/param.h>
41 1.14 christos #include <sys/stat.h>
42 1.7 jakllsch #include <sys/bootblock.h>
43 1.1 christos
44 1.1 christos #include <err.h>
45 1.1 christos #include <stddef.h>
46 1.1 christos #include <stdio.h>
47 1.1 christos #include <stdlib.h>
48 1.1 christos #include <string.h>
49 1.1 christos #include <unistd.h>
50 1.1 christos
51 1.1 christos #include "map.h"
52 1.1 christos #include "gpt.h"
53 1.14 christos #include "gpt_private.h"
54 1.1 christos
55 1.1 christos static int force;
56 1.14 christos static u_int parts;
57 1.1 christos static int primary_only;
58 1.1 christos
59 1.14 christos const char createmsg[] = "create [-fP] [-p <partitions>]";
60 1.3 riz
61 1.14 christos static int
62 1.1 christos usage_create(void)
63 1.1 christos {
64 1.1 christos
65 1.1 christos fprintf(stderr,
66 1.3 riz "usage: %s %s\n", getprogname(), createmsg);
67 1.14 christos return -1;
68 1.1 christos }
69 1.1 christos
70 1.14 christos static int
71 1.14 christos create(gpt_t gpt)
72 1.1 christos {
73 1.1 christos off_t blocks, last;
74 1.14 christos map_t map;
75 1.1 christos struct mbr *mbr;
76 1.1 christos struct gpt_hdr *hdr;
77 1.1 christos struct gpt_ent *ent;
78 1.1 christos unsigned int i;
79 1.14 christos void *p;
80 1.1 christos
81 1.14 christos last = gpt->mediasz / gpt->secsz - 1LL;
82 1.1 christos
83 1.14 christos if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) != NULL ||
84 1.14 christos map_find(gpt, MAP_TYPE_SEC_GPT_HDR) != NULL) {
85 1.14 christos gpt_warnx(gpt, "Device already contains a GPT");
86 1.14 christos return -1;
87 1.1 christos }
88 1.14 christos map = map_find(gpt, MAP_TYPE_MBR);
89 1.1 christos if (map != NULL) {
90 1.1 christos if (!force) {
91 1.14 christos gpt_warnx(gpt, "Device contains a MBR");
92 1.14 christos return -1;
93 1.1 christos }
94 1.1 christos /* Nuke the MBR in our internal map. */
95 1.1 christos map->map_type = MAP_TYPE_UNUSED;
96 1.1 christos }
97 1.1 christos
98 1.1 christos /*
99 1.1 christos * Create PMBR.
100 1.1 christos */
101 1.14 christos if (map_find(gpt, MAP_TYPE_PMBR) == NULL) {
102 1.14 christos if (map_free(gpt, 0LL, 1LL) == 0) {
103 1.14 christos gpt_warnx(gpt, "No room for the PMBR");
104 1.14 christos return -1;
105 1.14 christos }
106 1.14 christos mbr = gpt_read(gpt, 0LL, 1);
107 1.14 christos if (mbr == NULL) {
108 1.14 christos gpt_warnx(gpt, "Error reading MBR");
109 1.14 christos return -1;
110 1.1 christos }
111 1.8 christos memset(mbr, 0, sizeof(*mbr));
112 1.1 christos mbr->mbr_sig = htole16(MBR_SIG);
113 1.14 christos gpt_create_pmbr_part(mbr->mbr_part, last);
114 1.14 christos
115 1.14 christos map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr);
116 1.14 christos gpt_write(gpt, map);
117 1.1 christos }
118 1.1 christos
119 1.1 christos /* Get the amount of free space after the MBR */
120 1.14 christos blocks = map_free(gpt, 1LL, 0LL);
121 1.1 christos if (blocks == 0LL) {
122 1.14 christos gpt_warnx(gpt, "No room for the GPT header");
123 1.14 christos return -1;
124 1.1 christos }
125 1.1 christos
126 1.1 christos /* Don't create more than parts entries. */
127 1.14 christos if ((uint64_t)(blocks - 1) * gpt->secsz >
128 1.14 christos parts * sizeof(struct gpt_ent)) {
129 1.14 christos blocks = (parts * sizeof(struct gpt_ent)) / gpt->secsz;
130 1.14 christos if ((parts * sizeof(struct gpt_ent)) % gpt->secsz)
131 1.1 christos blocks++;
132 1.1 christos blocks++; /* Don't forget the header itself */
133 1.1 christos }
134 1.1 christos
135 1.1 christos /* Never cross the median of the device. */
136 1.1 christos if ((blocks + 1LL) > ((last + 1LL) >> 1))
137 1.1 christos blocks = ((last + 1LL) >> 1) - 1LL;
138 1.1 christos
139 1.1 christos /*
140 1.1 christos * Get the amount of free space at the end of the device and
141 1.1 christos * calculate the size for the GPT structures.
142 1.1 christos */
143 1.14 christos map = map_last(gpt);
144 1.1 christos if (map->map_type != MAP_TYPE_UNUSED) {
145 1.14 christos gpt_warnx(gpt, "No room for the backup header");
146 1.14 christos return -1;
147 1.1 christos }
148 1.1 christos
149 1.1 christos if (map->map_size < blocks)
150 1.1 christos blocks = map->map_size;
151 1.1 christos if (blocks == 1LL) {
152 1.14 christos gpt_warnx(gpt, "No room for the GPT table");
153 1.14 christos return -1;
154 1.14 christos }
155 1.14 christos
156 1.14 christos if ((p = calloc(1, gpt->secsz)) == NULL) {
157 1.14 christos gpt_warnx(gpt, "Can't allocate the GPT");
158 1.14 christos return -1;
159 1.14 christos }
160 1.14 christos if ((gpt->gpt = map_add(gpt, 1LL, 1LL,
161 1.14 christos MAP_TYPE_PRI_GPT_HDR, p)) == NULL) {
162 1.14 christos free(p);
163 1.14 christos gpt_warnx(gpt, "Can't add the GPT");
164 1.14 christos return -1;
165 1.1 christos }
166 1.1 christos
167 1.1 christos blocks--; /* Number of blocks in the GPT table. */
168 1.14 christos if ((p = calloc(blocks, gpt->secsz)) == NULL) {
169 1.14 christos gpt_warnx(gpt, "Can't allocate the GPT table");
170 1.14 christos return -1;
171 1.14 christos }
172 1.14 christos if ((gpt->tbl = map_add(gpt, 2LL, blocks,
173 1.14 christos MAP_TYPE_PRI_GPT_TBL, p)) == NULL) {
174 1.14 christos free(p);
175 1.14 christos gpt_warnx(gpt, "Can't add the GPT table");
176 1.14 christos return -1;
177 1.14 christos }
178 1.1 christos
179 1.14 christos hdr = gpt->gpt->map_data;
180 1.1 christos memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
181 1.1 christos hdr->hdr_revision = htole32(GPT_HDR_REVISION);
182 1.9 christos hdr->hdr_size = htole32(GPT_HDR_SIZE);
183 1.14 christos hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
184 1.1 christos hdr->hdr_lba_alt = htole64(last);
185 1.14 christos hdr->hdr_lba_start = htole64(gpt->tbl->map_start + blocks);
186 1.1 christos hdr->hdr_lba_end = htole64(last - blocks - 1LL);
187 1.12 christos gpt_uuid_generate(hdr->hdr_guid);
188 1.14 christos hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
189 1.14 christos hdr->hdr_entries = htole32((blocks * gpt->secsz) /
190 1.14 christos sizeof(struct gpt_ent));
191 1.1 christos if (le32toh(hdr->hdr_entries) > parts)
192 1.1 christos hdr->hdr_entries = htole32(parts);
193 1.1 christos hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
194 1.1 christos
195 1.14 christos ent = gpt->tbl->map_data;
196 1.1 christos for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
197 1.12 christos gpt_uuid_generate(ent[i].ent_guid);
198 1.1 christos }
199 1.1 christos
200 1.14 christos if (gpt_write_primary(gpt) == -1)
201 1.14 christos return -1;
202 1.1 christos
203 1.1 christos /*
204 1.1 christos * Create backup GPT if the user didn't suppress it.
205 1.1 christos */
206 1.1 christos if (!primary_only) {
207 1.14 christos // XXX: error checks
208 1.14 christos gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
209 1.14 christos calloc(1, gpt->secsz));
210 1.14 christos gpt->lbt = map_add(gpt, last - blocks, blocks,
211 1.14 christos MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data);
212 1.14 christos memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
213 1.14 christos hdr = gpt->tpg->map_data;
214 1.14 christos hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
215 1.14 christos hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
216 1.14 christos hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
217 1.14 christos if (gpt_write_backup(gpt) == -1)
218 1.14 christos return -1;
219 1.1 christos }
220 1.14 christos return 0;
221 1.1 christos }
222 1.1 christos
223 1.1 christos int
224 1.14 christos cmd_create(gpt_t gpt, int argc, char *argv[])
225 1.1 christos {
226 1.14 christos int ch;
227 1.1 christos
228 1.14 christos parts = 128;
229 1.14 christos
230 1.14 christos while ((ch = getopt(argc, argv, "fPp:")) != -1) {
231 1.1 christos switch(ch) {
232 1.1 christos case 'f':
233 1.1 christos force = 1;
234 1.1 christos break;
235 1.14 christos case 'P':
236 1.14 christos primary_only = 1;
237 1.14 christos break;
238 1.1 christos case 'p':
239 1.14 christos parts = atoi(optarg);
240 1.1 christos break;
241 1.1 christos default:
242 1.14 christos return usage_create();
243 1.1 christos }
244 1.1 christos }
245 1.1 christos
246 1.14 christos if (argc != optind)
247 1.14 christos return usage_create();
248 1.1 christos
249 1.14 christos return create(gpt);
250 1.1 christos }
251