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: src/sbin/gpt/migrate.c,v 1.16.2.1.6.1 2010/02/10 00:26:20 kensmith Exp $");
29
30#include <sys/types.h>
31#include <sys/disklabel.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
40#include "map.h"
41#include "gpt.h"
42
43/*
44 * Allow compilation on platforms that do not have a BSD label.
45 * The values are valid for amd64, i386 and ia64 disklabels.
46 */
47#ifndef LABELOFFSET
48#define	LABELOFFSET	0
49#endif
50#ifndef LABELSECTOR
51#define	LABELSECTOR	1
52#endif
53
54static int force;
55static int slice;
56
57static void
58usage_migrate(void)
59{
60
61	fprintf(stderr,
62	    "usage: %s [-fs] device ...\n", getprogname());
63	exit(1);
64}
65
66static struct gpt_ent*
67migrate_disklabel(int fd, off_t start, struct gpt_ent *ent)
68{
69	char *buf;
70	struct disklabel *dl;
71	off_t ofs, rawofs;
72	int i;
73
74	buf = gpt_read(fd, start + LABELSECTOR, 1);
75	dl = (void*)(buf + LABELOFFSET);
76
77	if (le32toh(dl->d_magic) != DISKMAGIC ||
78	    le32toh(dl->d_magic2) != DISKMAGIC) {
79		warnx("%s: warning: FreeBSD slice without disklabel",
80		    device_name);
81		return (ent);
82	}
83
84	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
85	    le32toh(dl->d_secsize);
86	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
87		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
88			continue;
89		ofs = le32toh(dl->d_partitions[i].p_offset) *
90		    le32toh(dl->d_secsize);
91		if (ofs < rawofs)
92			rawofs = 0;
93	}
94	rawofs /= secsz;
95
96	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
97		switch (dl->d_partitions[i].p_fstype) {
98		case FS_UNUSED:
99			continue;
100		case FS_SWAP: {
101			uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
102			le_uuid_enc(&ent->ent_type, &swap);
103			utf8_to_utf16("FreeBSD swap partition",
104			    ent->ent_name, 36);
105			break;
106		}
107		case FS_BSDFFS: {
108			uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
109			le_uuid_enc(&ent->ent_type, &ufs);
110			utf8_to_utf16("FreeBSD UFS partition",
111			    ent->ent_name, 36);
112			break;
113		}
114		case FS_VINUM: {
115			uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
116			le_uuid_enc(&ent->ent_type, &vinum);
117			utf8_to_utf16("FreeBSD vinum partition",
118			    ent->ent_name, 36);
119			break;
120		}
121		case FS_ZFS: {
122			uuid_t zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
123			le_uuid_enc(&ent->ent_type, &zfs);
124			utf8_to_utf16("FreeBSD ZFS partition",
125			    ent->ent_name, 36);
126			break;
127		}
128		default:
129			warnx("%s: warning: unknown FreeBSD partition (%d)",
130			    device_name, dl->d_partitions[i].p_fstype);
131			continue;
132		}
133
134		ofs = (le32toh(dl->d_partitions[i].p_offset) *
135		    le32toh(dl->d_secsize)) / secsz;
136		ofs = (ofs > 0) ? ofs - rawofs : 0;
137		ent->ent_lba_start = htole64(start + ofs);
138		ent->ent_lba_end = htole64(start + ofs +
139		    le32toh(dl->d_partitions[i].p_size) - 1LL);
140		ent++;
141	}
142
143	return (ent);
144}
145
146static void
147migrate(int fd)
148{
149	uuid_t uuid;
150	off_t blocks, last;
151	map_t *gpt, *tpg;
152	map_t *tbl, *lbt;
153	map_t *map;
154	struct gpt_hdr *hdr;
155	struct gpt_ent *ent;
156	struct mbr *mbr;
157	uint32_t start, size;
158	unsigned int i;
159
160	last = mediasz / secsz - 1LL;
161
162	map = map_find(MAP_TYPE_MBR);
163	if (map == NULL || map->map_start != 0) {
164		warnx("%s: error: no partitions to convert", device_name);
165		return;
166	}
167
168	mbr = map->map_data;
169
170	if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
171	    map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
172		warnx("%s: error: device already contains a GPT", device_name);
173		return;
174	}
175
176	/* Get the amount of free space after the MBR */
177	blocks = map_free(1LL, 0LL);
178	if (blocks == 0LL) {
179		warnx("%s: error: no room for the GPT header", device_name);
180		return;
181	}
182
183	/* Don't create more than parts entries. */
184	if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
185		blocks = (parts * sizeof(struct gpt_ent)) / secsz;
186		if ((parts * sizeof(struct gpt_ent)) % secsz)
187			blocks++;
188		blocks++;		/* Don't forget the header itself */
189	}
190
191	/* Never cross the median of the device. */
192	if ((blocks + 1LL) > ((last + 1LL) >> 1))
193		blocks = ((last + 1LL) >> 1) - 1LL;
194
195	/*
196	 * Get the amount of free space at the end of the device and
197	 * calculate the size for the GPT structures.
198	 */
199	map = map_last();
200	if (map->map_type != MAP_TYPE_UNUSED) {
201		warnx("%s: error: no room for the backup header", device_name);
202		return;
203	}
204
205	if (map->map_size < blocks)
206		blocks = map->map_size;
207	if (blocks == 1LL) {
208		warnx("%s: error: no room for the GPT table", device_name);
209		return;
210	}
211
212	blocks--;		/* Number of blocks in the GPT table. */
213	gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
214	tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
215	    calloc(blocks, secsz));
216	if (gpt == NULL || tbl == NULL)
217		return;
218
219	lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
220	    tbl->map_data);
221	tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz));
222
223	hdr = gpt->map_data;
224	memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
225	hdr->hdr_revision = htole32(GPT_HDR_REVISION);
226	/*
227	 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
228	 * contains padding we must not include in the size.
229	 */
230	hdr->hdr_size = htole32(offsetof(struct gpt_hdr, padding));
231	hdr->hdr_lba_self = htole64(gpt->map_start);
232	hdr->hdr_lba_alt = htole64(tpg->map_start);
233	hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
234	hdr->hdr_lba_end = htole64(lbt->map_start - 1LL);
235	uuid_create(&uuid, NULL);
236	le_uuid_enc(&hdr->hdr_uuid, &uuid);
237	hdr->hdr_lba_table = htole64(tbl->map_start);
238	hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
239	if (le32toh(hdr->hdr_entries) > parts)
240		hdr->hdr_entries = htole32(parts);
241	hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
242
243	ent = tbl->map_data;
244	for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
245		uuid_create(&uuid, NULL);
246		le_uuid_enc(&ent[i].ent_uuid, &uuid);
247	}
248
249	/* Mirror partitions. */
250	for (i = 0; i < 4; i++) {
251		start = le16toh(mbr->mbr_part[i].part_start_hi);
252		start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
253		size = le16toh(mbr->mbr_part[i].part_size_hi);
254		size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);
255
256		switch (mbr->mbr_part[i].part_typ) {
257		case 0:
258			continue;
259		case 165: {	/* FreeBSD */
260			if (slice) {
261				uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
262				le_uuid_enc(&ent->ent_type, &freebsd);
263				ent->ent_lba_start = htole64((uint64_t)start);
264				ent->ent_lba_end = htole64(start + size - 1LL);
265				utf8_to_utf16("FreeBSD disklabel partition",
266				    ent->ent_name, 36);
267				ent++;
268			} else
269				ent = migrate_disklabel(fd, start, ent);
270			break;
271		}
272		case 239: {	/* EFI */
273			uuid_t efi_slice = GPT_ENT_TYPE_EFI;
274			le_uuid_enc(&ent->ent_type, &efi_slice);
275			ent->ent_lba_start = htole64((uint64_t)start);
276			ent->ent_lba_end = htole64(start + size - 1LL);
277			utf8_to_utf16("EFI system partition",
278			    ent->ent_name, 36);
279			ent++;
280			break;
281		}
282		default:
283			if (!force) {
284				warnx("%s: error: unknown partition type (%d)",
285				    device_name, mbr->mbr_part[i].part_typ);
286				return;
287			}
288		}
289	}
290	ent = tbl->map_data;
291
292	hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
293	    le32toh(hdr->hdr_entsz)));
294	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
295
296	gpt_write(fd, gpt);
297	gpt_write(fd, tbl);
298
299	/*
300	 * Create backup GPT.
301	 */
302	memcpy(tpg->map_data, gpt->map_data, secsz);
303	hdr = tpg->map_data;
304	hdr->hdr_lba_self = htole64(tpg->map_start);
305	hdr->hdr_lba_alt = htole64(gpt->map_start);
306	hdr->hdr_lba_table = htole64(lbt->map_start);
307	hdr->hdr_crc_self = 0;			/* Don't ever forget this! */
308	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
309
310	gpt_write(fd, lbt);
311	gpt_write(fd, tpg);
312
313	map = map_find(MAP_TYPE_MBR);
314	mbr = map->map_data;
315	/*
316	 * Turn the MBR into a Protective MBR.
317	 */
318	bzero(mbr->mbr_part, sizeof(mbr->mbr_part));
319	mbr->mbr_part[0].part_shd = 0xff;
320	mbr->mbr_part[0].part_ssect = 0xff;
321	mbr->mbr_part[0].part_scyl = 0xff;
322	mbr->mbr_part[0].part_typ = 0xee;
323	mbr->mbr_part[0].part_ehd = 0xff;
324	mbr->mbr_part[0].part_esect = 0xff;
325	mbr->mbr_part[0].part_ecyl = 0xff;
326	mbr->mbr_part[0].part_start_lo = htole16(1);
327	if (last > 0xffffffff) {
328		mbr->mbr_part[0].part_size_lo = htole16(0xffff);
329		mbr->mbr_part[0].part_size_hi = htole16(0xffff);
330	} else {
331		mbr->mbr_part[0].part_size_lo = htole16(last);
332		mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
333	}
334	gpt_write(fd, map);
335}
336
337int
338cmd_migrate(int argc, char *argv[])
339{
340	int ch, fd;
341
342	/* Get the migrate options */
343	while ((ch = getopt(argc, argv, "fs")) != -1) {
344		switch(ch) {
345		case 'f':
346			force = 1;
347			break;
348		case 's':
349			slice = 1;
350			break;
351		default:
352			usage_migrate();
353		}
354	}
355
356	if (argc == optind)
357		usage_migrate();
358
359	while (optind < argc) {
360		fd = gpt_open(argv[optind++]);
361		if (fd == -1) {
362			warn("unable to open device '%s'", device_name);
363			return (1);
364		}
365
366		migrate(fd);
367
368		gpt_close(fd);
369	}
370
371	return (0);
372}
373