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