2 * Copyright (c) 2002 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
29 #include <sys/types.h>
30 #include <sys/disklabel.h>
51 "usage: %s [-ks] device\n", getprogname());
55 static struct gpt_ent*
56 migrate_disklabel(int fd, off_t start, struct gpt_ent *ent)
62 buf = gpt_read(fd, start + LABELSECTOR, 1);
63 dl = (void*)(buf + LABELOFFSET);
65 if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC) {
66 warnx("%s: warning: FreeBSD slice without disklabel",
71 for (i = 0; i < dl->d_npartitions; i++) {
72 switch (dl->d_partitions[i].p_fstype) {
74 uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
76 unicode16(ent->ent_name,
77 L"FreeBSD swap partition", 36);
81 uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
83 unicode16(ent->ent_name,
84 L"FreeBSD UFS partition", 36);
88 uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
89 ent->ent_type = vinum;
90 unicode16(ent->ent_name,
91 L"FreeBSD vinum partition", 36);
98 ent->ent_lba_start = dl->d_partitions[i].p_offset;
99 ent->ent_lba_end = ent->ent_lba_start +
100 dl->d_partitions[i].p_size - 1LL;
117 uint32_t start, size;
120 last = mediasz / secsz - 1LL;
122 map = map_find(MAP_TYPE_MBR);
123 if (map == NULL || map_find(MAP_TYPE_MBR_PART) == NULL) {
124 warnx("%s: error: no partitions to convert",
131 if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
132 map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
133 warnx("%s: error: device already contains a GPT", device_name);
137 /* Get the amount of free space after the MBR */
138 blocks = map_unused(1LL, 0LL);
140 warnx("%s: error: no room for the GPT header", device_name);
144 /* Don't create more than parts entries. */
145 if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
146 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
147 if ((parts * sizeof(struct gpt_ent)) % secsz)
149 blocks++; /* Don't forget the header itself */
152 /* Never cross the median of the device. */
153 if ((blocks + 1LL) > ((last + 1LL) >> 1))
154 blocks = ((last + 1LL) >> 1) - 1LL;
157 * Get the amount of free space at the end of the device and
158 * calculate the size for the GPT structures.
161 if (map->map_type != MAP_TYPE_UNUSED) {
162 warnx("%s: error: no room for the backup header", device_name);
166 if (map->map_size < blocks)
167 blocks = map->map_size;
169 warnx("%s: error: no room for the GPT table", device_name);
173 blocks--; /* Number of blocks in the GPT table. */
174 gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
175 tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
176 calloc(blocks, secsz));
177 if (gpt == NULL || tbl == NULL)
180 lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
182 tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz));
185 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
186 hdr->hdr_revision = GPT_HDR_REVISION;
188 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
189 * contains padding we must not include in the size.
191 hdr->hdr_size = offsetof(struct gpt_hdr, padding);
192 hdr->hdr_lba_self = gpt->map_start;
193 hdr->hdr_lba_alt = tpg->map_start;
194 hdr->hdr_lba_start = tbl->map_start + blocks;
195 hdr->hdr_lba_end = lbt->map_start - 1LL;
196 uuidgen(&hdr->hdr_uuid, 1);
197 hdr->hdr_lba_table = tbl->map_start;
198 hdr->hdr_entries = (blocks * secsz) / sizeof(struct gpt_ent);
199 if (hdr->hdr_entries > parts)
200 hdr->hdr_entries = parts;
201 hdr->hdr_entsz = sizeof(struct gpt_ent);
204 for (i = 0; i < hdr->hdr_entries; i++)
205 uuidgen(&ent[i].ent_uuid, 1);
207 /* Mirror partitions. */
208 for (i = 0; i < 4; i++) {
209 start = mbr->mbr_part[i].part_start_hi;
210 start = (start << 16) + mbr->mbr_part[i].part_start_lo;
211 size = mbr->mbr_part[i].part_size_hi;
212 size = (size << 16) + mbr->mbr_part[i].part_size_lo;
214 switch (mbr->mbr_part[i].part_typ) {
215 case 165: { /* FreeBSD */
217 uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
218 ent->ent_type = freebsd;
219 ent->ent_lba_start = start;
220 ent->ent_lba_end = start + size - 1LL;
221 unicode16(ent->ent_name,
222 L"FreeBSD disklabel partition", 36);
225 ent = migrate_disklabel(fd, start, ent);
228 case 239: { /* EFI */
229 uuid_t efi_slice = GPT_ENT_TYPE_EFI;
230 ent->ent_type = efi_slice;
231 ent->ent_lba_start = start;
232 ent->ent_lba_end = start + size - 1LL;
233 unicode16(ent->ent_name, L"EFI system partition", 36);
243 hdr->hdr_crc_table = crc32(ent, hdr->hdr_entries * hdr->hdr_entsz);
244 hdr->hdr_crc_self = crc32(hdr, hdr->hdr_size);
252 memcpy(tpg->map_data, gpt->map_data, secsz);
254 hdr->hdr_lba_self = tpg->map_start;
255 hdr->hdr_lba_alt = gpt->map_start;
256 hdr->hdr_lba_table = lbt->map_start;
257 hdr->hdr_crc_self = 0; /* Don't ever forget this! */
258 hdr->hdr_crc_self = crc32(hdr, hdr->hdr_size);
264 map = map_find(MAP_TYPE_MBR);
267 * Turn the MBR into a Protective MBR.
269 bzero(mbr->mbr_part, sizeof(mbr->mbr_part));
270 mbr->mbr_part[0].part_shd = 0xff;
271 mbr->mbr_part[0].part_ssect = 0xff;
272 mbr->mbr_part[0].part_scyl = 0xff;
273 mbr->mbr_part[0].part_typ = 0xee;
274 mbr->mbr_part[0].part_ehd = 0xff;
275 mbr->mbr_part[0].part_esect = 0xff;
276 mbr->mbr_part[0].part_ecyl = 0xff;
277 mbr->mbr_part[0].part_start_lo = 1;
278 if (mediasz > 0xffffffff) {
279 mbr->mbr_part[0].part_size_lo = 0xffff;
280 mbr->mbr_part[0].part_size_hi = 0xffff;
282 mbr->mbr_part[0].part_size_lo = mediasz & 0xffff;
283 mbr->mbr_part[0].part_size_hi = mediasz >> 16;
290 cmd_migrate(int argc, char *argv[])
294 /* Get the migrate options */
295 while ((ch = getopt(argc, argv, "ks")) != -1) {
311 while (optind < argc) {
312 fd = gpt_open(argv[optind++]);
314 warn("unable to open device '%s'", device_name);