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>
34 #include <sys/errno.h>
35 #include <sys/param.h>
36 #include <bus/cam/scsi/scsi_daio.h>
50 static int primary_only;
52 static void do_erase(int fd);
57 fprintf(stderr, "usage: %s [-fp] device ...\n", getprogname());
64 fprintf(stderr, "usage: %s -f [-B] [-E] device ...\n", getprogname());
65 fprintf(stderr, "\tnote: -f is mandatory for this command\n");
82 last = mediasz / secsz - 1LL;
84 if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
85 map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
86 warnx("%s: error: device already contains a GPT", device_name);
89 map = map_find(MAP_TYPE_MBR);
92 warnx("%s: error: device contains a MBR", device_name);
96 /* Nuke the MBR in our internal map. */
97 map->map_type = MAP_TYPE_UNUSED;
103 if (map_find(MAP_TYPE_PMBR) == NULL) {
104 if (map_free(0LL, 1LL) == 0) {
105 warnx("%s: error: no room for the PMBR", device_name);
108 mbr = gpt_read(fd, 0LL, 1);
109 bzero(mbr, sizeof(*mbr));
110 mbr->mbr_sig = htole16(MBR_SIG);
111 mbr->mbr_part[0].part_shd = 0xff;
112 mbr->mbr_part[0].part_ssect = 0xff;
113 mbr->mbr_part[0].part_scyl = 0xff;
114 mbr->mbr_part[0].part_typ = 0xee;
115 mbr->mbr_part[0].part_ehd = 0xff;
116 mbr->mbr_part[0].part_esect = 0xff;
117 mbr->mbr_part[0].part_ecyl = 0xff;
118 mbr->mbr_part[0].part_start_lo = htole16(1);
119 if (last > 0xffffffff) {
120 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
121 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
123 mbr->mbr_part[0].part_size_lo = htole16(last);
124 mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
126 map = map_add(0LL, 1LL, MAP_TYPE_PMBR, mbr);
130 /* Get the amount of free space after the MBR */
131 blocks = map_free(1LL, 0LL);
133 warnx("%s: error: no room for the GPT header", device_name);
137 /* Don't create more than parts entries. */
138 if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
139 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
140 if ((parts * sizeof(struct gpt_ent)) % secsz)
142 blocks++; /* Don't forget the header itself */
145 /* Never cross the median of the device. */
146 if ((blocks + 1LL) > ((last + 1LL) >> 1))
147 blocks = ((last + 1LL) >> 1) - 1LL;
150 * Get the amount of free space at the end of the device and
151 * calculate the size for the GPT structures.
154 if (map->map_type != MAP_TYPE_UNUSED) {
155 warnx("%s: error: no room for the backup header", device_name);
159 if (map->map_size < blocks)
160 blocks = map->map_size;
162 warnx("%s: error: no room for the GPT table", device_name);
166 blocks--; /* Number of blocks in the GPT table. */
167 gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
168 tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
169 calloc(blocks, secsz));
170 if (gpt == NULL || tbl == NULL)
174 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
175 hdr->hdr_revision = htole32(GPT_HDR_REVISION);
177 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
178 * contains padding we must not include in the size.
180 hdr->hdr_size = htole32(offsetof(struct gpt_hdr, padding));
181 hdr->hdr_lba_self = htole64(gpt->map_start);
182 hdr->hdr_lba_alt = htole64(last);
183 hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
184 hdr->hdr_lba_end = htole64(last - blocks - 1LL);
185 uuid_create(&uuid, NULL);
186 le_uuid_enc(&hdr->hdr_uuid, &uuid);
187 hdr->hdr_lba_table = htole64(tbl->map_start);
188 hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
189 if (le32toh(hdr->hdr_entries) > parts)
190 hdr->hdr_entries = htole32(parts);
191 hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
194 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
195 uuid_create(&uuid, NULL);
196 le_uuid_enc(&ent[i].ent_uuid, &uuid);
199 hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
200 le32toh(hdr->hdr_entsz)));
201 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
207 * Create backup GPT if the user didn't suppress it.
210 tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
212 lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
214 memcpy(tpg->map_data, gpt->map_data, secsz);
216 hdr->hdr_lba_self = htole64(tpg->map_start);
217 hdr->hdr_lba_alt = htole64(gpt->map_start);
218 hdr->hdr_lba_table = htole64(lbt->map_start);
219 hdr->hdr_crc_self = 0; /* Don't ever forget this! */
220 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
228 dosys(const char *ctl, ...)
234 vasprintf(&scmd, ctl, va);
236 printf("%s\n", scmd);
237 if (system(scmd) != 0) {
238 fprintf(stderr, "Execution failed\n");
246 cmd_create(int argc, char *argv[])
250 while ((ch = getopt(argc, argv, "fp")) != -1) {
266 while (optind < argc) {
267 fd = gpt_open(argv[optind++]);
269 warn("unable to open device '%s'", device_name);
284 * Create a fresh gpt partition table and populate it with reasonable
288 cmd_init(int argc, char *argv[])
294 while ((ch = getopt(argc, argv, "fBEI")) != -1) {
300 fprintf(stderr, "Maybe you were trying to supply "
301 "fdisk options. This is the gpt "
319 if (argc == optind) {
324 while (optind < argc) {
325 const char *path = argv[optind++];
330 * Destroy and [re]Create the gpt
334 warn("unable to open device '%s'", path);
343 warn("unable to reopen device '%s'", path);
353 warn("unable to open device '%s'", device_name);
364 if (strstr(device_name, "serno"))
365 asprintf(&slice0, "%s.s0", device_name);
367 asprintf(&slice0, "%ss0", device_name);
369 if (strstr(device_name, "serno"))
370 asprintf(&slice1, "%s.s1", device_name);
372 asprintf(&slice1, "%ss1", device_name);
377 dosys("disklabel -r -w %s %s auto",
378 (with_boot ? "-B" : ""),
385 dosys("newfs_msdos %s", slice0);
388 * If asked to setup an EFI boot, mount the dos partition
391 * We do not setup the dragonfly disklabel with -B
397 asprintf(&mtpt, "/tmp/gpt%08x%08x",
398 (int)random(), (int)random());
399 if (mkdir(mtpt, 0700) < 0) {
400 fprintf(stderr, "cannot mkdir %s\n", mtpt);
404 dosys("mount_msdos %s %s", slice0, mtpt);
405 dosys("mkdir -p %s/efi/boot", mtpt);
406 dosys("cpdup /boot/boot1.efi %s/efi/boot/bootx64.efi",
408 dosys("umount %s", mtpt);
426 if (ioctl(fd, DAIOCTRIM, ioarg) < 0) {
427 printf("Trim error %s\n", strerror(errno));
428 printf("Continuing\n");
430 printf("Trim completed ok\n");