2 * Copyright (c) 2002 Marcel Moolenaar
5 * 'init' directive added by Matthew Dillon.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $
29 * $DragonFly: src/sbin/gpt/create.c,v 1.1 2007/06/16 22:29:27 dillon Exp $
32 #include <sys/types.h>
47 static int primary_only;
52 fprintf(stderr, "usage: %s [-fp] device ...\n", getprogname());
59 fprintf(stderr, "usage: %s -f [-B] device ...\n", getprogname());
60 fprintf(stderr, "\tnote: -f is mandatory for this command\n");
77 last = mediasz / secsz - 1LL;
79 if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
80 map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
81 warnx("%s: error: device already contains a GPT", device_name);
84 map = map_find(MAP_TYPE_MBR);
87 warnx("%s: error: device contains a MBR", device_name);
91 /* Nuke the MBR in our internal map. */
92 map->map_type = MAP_TYPE_UNUSED;
98 if (map_find(MAP_TYPE_PMBR) == NULL) {
99 if (map_free(0LL, 1LL) == 0) {
100 warnx("%s: error: no room for the PMBR", device_name);
103 mbr = gpt_read(fd, 0LL, 1);
104 bzero(mbr, sizeof(*mbr));
105 mbr->mbr_sig = htole16(MBR_SIG);
106 mbr->mbr_part[0].part_shd = 0xff;
107 mbr->mbr_part[0].part_ssect = 0xff;
108 mbr->mbr_part[0].part_scyl = 0xff;
109 mbr->mbr_part[0].part_typ = 0xee;
110 mbr->mbr_part[0].part_ehd = 0xff;
111 mbr->mbr_part[0].part_esect = 0xff;
112 mbr->mbr_part[0].part_ecyl = 0xff;
113 mbr->mbr_part[0].part_start_lo = htole16(1);
114 if (last > 0xffffffff) {
115 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
116 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
118 mbr->mbr_part[0].part_size_lo = htole16(last);
119 mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
121 map = map_add(0LL, 1LL, MAP_TYPE_PMBR, mbr);
125 /* Get the amount of free space after the MBR */
126 blocks = map_free(1LL, 0LL);
128 warnx("%s: error: no room for the GPT header", device_name);
132 /* Don't create more than parts entries. */
133 if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
134 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
135 if ((parts * sizeof(struct gpt_ent)) % secsz)
137 blocks++; /* Don't forget the header itself */
140 /* Never cross the median of the device. */
141 if ((blocks + 1LL) > ((last + 1LL) >> 1))
142 blocks = ((last + 1LL) >> 1) - 1LL;
145 * Get the amount of free space at the end of the device and
146 * calculate the size for the GPT structures.
149 if (map->map_type != MAP_TYPE_UNUSED) {
150 warnx("%s: error: no room for the backup header", device_name);
154 if (map->map_size < blocks)
155 blocks = map->map_size;
157 warnx("%s: error: no room for the GPT table", device_name);
161 blocks--; /* Number of blocks in the GPT table. */
162 gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
163 tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
164 calloc(blocks, secsz));
165 if (gpt == NULL || tbl == NULL)
169 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
170 hdr->hdr_revision = htole32(GPT_HDR_REVISION);
172 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
173 * contains padding we must not include in the size.
175 hdr->hdr_size = htole32(offsetof(struct gpt_hdr, padding));
176 hdr->hdr_lba_self = htole64(gpt->map_start);
177 hdr->hdr_lba_alt = htole64(last);
178 hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
179 hdr->hdr_lba_end = htole64(last - blocks - 1LL);
180 uuid_create(&uuid, NULL);
181 le_uuid_enc(&hdr->hdr_uuid, &uuid);
182 hdr->hdr_lba_table = htole64(tbl->map_start);
183 hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
184 if (le32toh(hdr->hdr_entries) > parts)
185 hdr->hdr_entries = htole32(parts);
186 hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
189 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
190 uuid_create(&uuid, NULL);
191 le_uuid_enc(&ent[i].ent_uuid, &uuid);
194 hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
195 le32toh(hdr->hdr_entsz)));
196 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
202 * Create backup GPT if the user didn't suppress it.
205 tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
207 lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
209 memcpy(tpg->map_data, gpt->map_data, secsz);
211 hdr->hdr_lba_self = htole64(tpg->map_start);
212 hdr->hdr_lba_alt = htole64(gpt->map_start);
213 hdr->hdr_lba_table = htole64(lbt->map_start);
214 hdr->hdr_crc_self = 0; /* Don't ever forget this! */
215 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
223 dosys(const char *ctl, ...)
229 vasprintf(&scmd, ctl, va);
231 printf("%s\n", scmd);
232 if (system(scmd) != 0) {
233 fprintf(stderr, "Execution failed\n");
241 cmd_create(int argc, char *argv[])
245 while ((ch = getopt(argc, argv, "fp")) != -1) {
261 while (optind < argc) {
262 fd = gpt_open(argv[optind++]);
264 warn("unable to open device '%s'", device_name);
279 * Create a fresh gpt partition table and populate it with reasonable
283 cmd_init(int argc, char *argv[])
288 while ((ch = getopt(argc, argv, "fIB")) != -1) {
294 fprintf(stderr, "Maybe you were trying to supply "
295 "fdisk options. This is the gpt "
310 if (argc == optind) {
315 while (optind < argc) {
316 const char *path = argv[optind++];
321 * Destroy and [re]Create the gpt
325 warn("unable to open device '%s'", device_name);
333 warn("unable to open device '%s'", device_name);
343 if (strstr(device_name, "serno"))
344 asprintf(&slice0, "%s.s0", device_name);
346 asprintf(&slice0, "%ss0", device_name);
348 if (strstr(device_name, "serno"))
349 asprintf(&slice1, "%s.s1", device_name);
351 asprintf(&slice1, "%ss1", device_name);
357 dosys("disklabel -r -w %s %s auto",
358 (with_boot ? "-B" : ""),
364 dosys("newfs_msdos %s", slice0);
367 * If asked to setup an EFI boot, mount the dos partition
370 * We do not setup the dragonfly disklabel with -B
376 asprintf(&mtpt, "/tmp/gpt%08x%08x",
377 (int)random(), (int)random());
378 if (mkdir(mtpt, 0700) < 0) {
379 fprintf(stderr, "cannot mkdir %s\n", mtpt);
383 dosys("mount_msdos %s %s", slice0, mtpt);
384 dosys("mkdir -p %s/efi/boot", mtpt);
385 dosys("cpdup /boot/boot1.efi %s/efi/boot/bootx64.efi",
387 dosys("umount %s", mtpt);