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