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