Fix markup.
[netbsd-mini2440.git] / sbin / gpt / create.c
blob5a6d17a389431aa4b0712c81c793940b52563147
1 /*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
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
35 #include <sys/types.h>
37 #include <err.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "map.h"
45 #include "gpt.h"
47 static int force;
48 static int primary_only;
50 const char createmsg[] = "create [-fp] device ...";
52 static void
53 usage_create(void)
56 fprintf(stderr,
57 "usage: %s %s\n", getprogname(), createmsg);
58 exit(1);
61 static void
62 create(int fd)
64 uuid_t uuid;
65 off_t blocks, last;
66 map_t *gpt, *tpg;
67 map_t *tbl, *lbt;
68 map_t *map;
69 struct mbr *mbr;
70 struct gpt_hdr *hdr;
71 struct gpt_ent *ent;
72 unsigned int i;
74 last = mediasz / secsz - 1LL;
76 if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
77 map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
78 warnx("%s: error: device already contains a GPT", device_name);
79 return;
81 map = map_find(MAP_TYPE_MBR);
82 if (map != NULL) {
83 if (!force) {
84 warnx("%s: error: device contains a MBR", device_name);
85 return;
88 /* Nuke the MBR in our internal map. */
89 map->map_type = MAP_TYPE_UNUSED;
93 * Create PMBR.
95 if (map_find(MAP_TYPE_PMBR) == NULL) {
96 if (map_free(0LL, 1LL) == 0) {
97 warnx("%s: error: no room for the PMBR", device_name);
98 return;
100 mbr = gpt_read(fd, 0LL, 1);
101 bzero(mbr, sizeof(*mbr));
102 mbr->mbr_sig = htole16(MBR_SIG);
103 mbr->mbr_part[0].part_shd = 0xff;
104 mbr->mbr_part[0].part_ssect = 0xff;
105 mbr->mbr_part[0].part_scyl = 0xff;
106 mbr->mbr_part[0].part_typ = 0xee;
107 mbr->mbr_part[0].part_ehd = 0xff;
108 mbr->mbr_part[0].part_esect = 0xff;
109 mbr->mbr_part[0].part_ecyl = 0xff;
110 mbr->mbr_part[0].part_start_lo = htole16(1);
111 if (last > 0xffffffff) {
112 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
113 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
114 } else {
115 mbr->mbr_part[0].part_size_lo = htole16(last);
116 mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
118 map = map_add(0LL, 1LL, MAP_TYPE_PMBR, mbr);
119 gpt_write(fd, map);
122 /* Get the amount of free space after the MBR */
123 blocks = map_free(1LL, 0LL);
124 if (blocks == 0LL) {
125 warnx("%s: error: no room for the GPT header", device_name);
126 return;
129 /* Don't create more than parts entries. */
130 if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
131 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
132 if ((parts * sizeof(struct gpt_ent)) % secsz)
133 blocks++;
134 blocks++; /* Don't forget the header itself */
137 /* Never cross the median of the device. */
138 if ((blocks + 1LL) > ((last + 1LL) >> 1))
139 blocks = ((last + 1LL) >> 1) - 1LL;
142 * Get the amount of free space at the end of the device and
143 * calculate the size for the GPT structures.
145 map = map_last();
146 if (map->map_type != MAP_TYPE_UNUSED) {
147 warnx("%s: error: no room for the backup header", device_name);
148 return;
151 if (map->map_size < blocks)
152 blocks = map->map_size;
153 if (blocks == 1LL) {
154 warnx("%s: error: no room for the GPT table", device_name);
155 return;
158 blocks--; /* Number of blocks in the GPT table. */
159 gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
160 tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
161 calloc(blocks, secsz));
162 if (gpt == NULL || tbl == NULL)
163 return;
165 hdr = gpt->map_data;
166 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
167 hdr->hdr_revision = htole32(GPT_HDR_REVISION);
168 hdr->hdr_size = htole32(GPT_SIZE);
169 hdr->hdr_lba_self = htole64(gpt->map_start);
170 hdr->hdr_lba_alt = htole64(last);
171 hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
172 hdr->hdr_lba_end = htole64(last - blocks - 1LL);
173 uuid_create(&uuid, NULL);
174 le_uuid_enc((uuid_t *)&hdr->hdr_uuid, &uuid);
175 hdr->hdr_lba_table = htole64(tbl->map_start);
176 hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
177 if (le32toh(hdr->hdr_entries) > parts)
178 hdr->hdr_entries = htole32(parts);
179 hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
181 ent = tbl->map_data;
182 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
183 uuid_create(&uuid, NULL);
184 le_uuid_enc((uuid_t *)&ent[i].ent_uuid, &uuid);
187 hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
188 le32toh(hdr->hdr_entsz)));
189 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
191 gpt_write(fd, gpt);
192 gpt_write(fd, tbl);
195 * Create backup GPT if the user didn't suppress it.
197 if (!primary_only) {
198 tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
199 calloc(1, secsz));
200 lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
201 tbl->map_data);
202 memcpy(tpg->map_data, gpt->map_data, secsz);
203 hdr = tpg->map_data;
204 hdr->hdr_lba_self = htole64(tpg->map_start);
205 hdr->hdr_lba_alt = htole64(gpt->map_start);
206 hdr->hdr_lba_table = htole64(lbt->map_start);
207 hdr->hdr_crc_self = 0; /* Don't ever forget this! */
208 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
209 gpt_write(fd, lbt);
210 gpt_write(fd, tpg);
215 cmd_create(int argc, char *argv[])
217 int ch, fd;
219 while ((ch = getopt(argc, argv, "fp")) != -1) {
220 switch(ch) {
221 case 'f':
222 force = 1;
223 break;
224 case 'p':
225 primary_only = 1;
226 break;
227 default:
228 usage_create();
232 if (argc == optind)
233 usage_create();
235 while (optind < argc) {
236 fd = gpt_open(argv[optind++]);
237 if (fd == -1) {
238 warn("unable to open device '%s'", device_name);
239 continue;
242 create(fd);
244 gpt_close(fd);
247 return (0);