Home | History | Annotate | Line # | Download | only in gpt
migrate.c revision 1.9
      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.9 2013/10/19 08:13:21 jnemeth Exp $");
     33 #endif
     34 
     35 #include <sys/types.h>
     36 #include <sys/param.h>
     37 #include <sys/bootblock.h>
     38 #include <sys/disklabel.h>
     39 
     40 #include <err.h>
     41 #include <stddef.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 
     47 #include "map.h"
     48 #include "gpt.h"
     49 
     50 /*
     51  * Allow compilation on platforms that do not have a BSD label.
     52  * The values are valid for amd64, i386 and ia64 disklabels.
     53  */
     54 #ifndef LABELOFFSET
     55 #define	LABELOFFSET	0
     56 #endif
     57 #ifndef LABELSECTOR
     58 #define	LABELSECTOR	1
     59 #endif
     60 
     61 static int force;
     62 static int slice;
     63 
     64 const char migratemsg[] = "migrate [-fs] device ...";
     65 
     66 __dead static void
     67 usage_migrate(void)
     68 {
     69 
     70 	fprintf(stderr,
     71 	    "usage: %s %s\n", getprogname(), migratemsg);
     72 	exit(1);
     73 }
     74 
     75 static struct gpt_ent*
     76 migrate_disklabel(int fd, off_t start, struct gpt_ent *ent)
     77 {
     78 	char *buf;
     79 	struct disklabel *dl;
     80 	off_t ofs, rawofs;
     81 	int i;
     82 
     83 	buf = gpt_read(fd, start + LABELSECTOR, 1);
     84 	dl = (void*)(buf + LABELOFFSET);
     85 
     86 	if (le32toh(dl->d_magic) != DISKMAGIC ||
     87 	    le32toh(dl->d_magic2) != DISKMAGIC) {
     88 		warnx("%s: warning: FreeBSD slice without disklabel",
     89 		    device_name);
     90 		return (ent);
     91 	}
     92 
     93 	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
     94 	    le32toh(dl->d_secsize);
     95 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
     96 		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
     97 			continue;
     98 		ofs = le32toh(dl->d_partitions[i].p_offset) *
     99 		    le32toh(dl->d_secsize);
    100 		if (ofs < rawofs)
    101 			rawofs = 0;
    102 	}
    103 	rawofs /= secsz;
    104 
    105 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    106 		switch (dl->d_partitions[i].p_fstype) {
    107 		case FS_UNUSED:
    108 			continue;
    109 		case FS_SWAP: {
    110 			static const uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
    111 			le_uuid_enc(ent->ent_type, &swap);
    112 			utf8_to_utf16((const uint8_t *)"FreeBSD swap partition",
    113 			    ent->ent_name, 36);
    114 			break;
    115 		}
    116 		case FS_BSDFFS: {
    117 			static const uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
    118 			le_uuid_enc(ent->ent_type, &ufs);
    119 			utf8_to_utf16((const uint8_t *)"FreeBSD UFS partition",
    120 			    ent->ent_name, 36);
    121 			break;
    122 		}
    123 		case 14: {	/* Vinum */
    124 			static const uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
    125 			le_uuid_enc(ent->ent_type, &vinum);
    126 			utf8_to_utf16((const uint8_t *)"FreeBSD vinum partition",
    127 			    ent->ent_name, 36);
    128 			break;
    129 		}
    130 		case 27: {	/* ZFS */
    131 			static const uuid_t zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
    132 			le_uuid_enc(ent->ent_type, &zfs);
    133 			utf8_to_utf16((const uint8_t *)"FreeBSD ZFS partition",
    134 			    ent->ent_name, 36);
    135 			break;
    136 		}
    137 		default:
    138 			warnx("%s: warning: unknown FreeBSD partition (%d)",
    139 			    device_name, dl->d_partitions[i].p_fstype);
    140 			continue;
    141 		}
    142 
    143 		ofs = (le32toh(dl->d_partitions[i].p_offset) *
    144 		    le32toh(dl->d_secsize)) / secsz;
    145 		ofs = (ofs > 0) ? ofs - rawofs : 0;
    146 		ent->ent_lba_start = htole64(start + ofs);
    147 		ent->ent_lba_end = htole64(start + ofs +
    148 		    le32toh(dl->d_partitions[i].p_size) - 1LL);
    149 		ent++;
    150 	}
    151 
    152 	return (ent);
    153 }
    154 
    155 static struct gpt_ent*
    156 migrate_netbsd_disklabel(int fd, off_t start, struct gpt_ent *ent)
    157 {
    158 	char *buf;
    159 	struct disklabel *dl;
    160 	off_t ofs, rawofs;
    161 	int i;
    162 
    163 	buf = gpt_read(fd, start + LABELSECTOR, 1);
    164 	dl = (void*)(buf + LABELOFFSET);
    165 
    166 	if (le32toh(dl->d_magic) != DISKMAGIC ||
    167 	    le32toh(dl->d_magic2) != DISKMAGIC) {
    168 		warnx("%s: warning: NetBSD slice without disklabel",
    169 		    device_name);
    170 		return (ent);
    171 	}
    172 
    173 	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
    174 	    le32toh(dl->d_secsize);
    175 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    176 		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
    177 			continue;
    178 		ofs = le32toh(dl->d_partitions[i].p_offset) *
    179 		    le32toh(dl->d_secsize);
    180 		if (ofs < rawofs)
    181 			rawofs = 0;
    182 	}
    183 	rawofs /= secsz;
    184 
    185 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    186 		switch (dl->d_partitions[i].p_fstype) {
    187 		case FS_UNUSED:
    188 			continue;
    189 		case FS_SWAP: {
    190 			static const uuid_t swap = GPT_ENT_TYPE_NETBSD_SWAP;
    191 			le_uuid_enc(ent->ent_type, &swap);
    192 			utf8_to_utf16((const uint8_t *)"NetBSD swap partition",
    193 			    ent->ent_name, 36);
    194 			break;
    195 		}
    196 		case FS_BSDFFS: {
    197 			static const uuid_t ufs = GPT_ENT_TYPE_NETBSD_FFS;
    198 			le_uuid_enc(ent->ent_type, &ufs);
    199 			utf8_to_utf16((const uint8_t *)"NetBSD FFS partition",
    200 			    ent->ent_name, 36);
    201 			break;
    202 		}
    203 		case FS_BSDLFS: {
    204 			static const uuid_t zfs = GPT_ENT_TYPE_NETBSD_LFS;
    205 			le_uuid_enc(ent->ent_type, &zfs);
    206 			utf8_to_utf16((const uint8_t *)"NetBSD LFS partition",
    207 			    ent->ent_name, 36);
    208 			break;
    209 		}
    210 		case FS_RAID: {
    211 			static const uuid_t zfs = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
    212 			le_uuid_enc(ent->ent_type, &zfs);
    213 			utf8_to_utf16((const uint8_t *)"NetBSD RAIDframe partition",
    214 			    ent->ent_name, 36);
    215 			break;
    216 		}
    217 		case FS_CCD: {
    218 			static const uuid_t zfs = GPT_ENT_TYPE_NETBSD_CCD;
    219 			le_uuid_enc(ent->ent_type, &zfs);
    220 			utf8_to_utf16((const uint8_t *)"NetBSD CCD partition",
    221 			    ent->ent_name, 36);
    222 			break;
    223 		}
    224 		case FS_CGD: {
    225 			static const uuid_t zfs = GPT_ENT_TYPE_NETBSD_CGD;
    226 			le_uuid_enc(ent->ent_type, &zfs);
    227 			utf8_to_utf16((const uint8_t *)"NetBSD CGD partition",
    228 			    ent->ent_name, 36);
    229 			break;
    230 		}
    231 		default:
    232 			warnx("%s: warning: unknown NetBSD partition (%d)",
    233 			    device_name, dl->d_partitions[i].p_fstype);
    234 			continue;
    235 		}
    236 
    237 		ofs = (le32toh(dl->d_partitions[i].p_offset) *
    238 		    le32toh(dl->d_secsize)) / secsz;
    239 		ofs = (ofs > 0) ? ofs - rawofs : 0;
    240 		ent->ent_lba_start = htole64(start + ofs);
    241 		ent->ent_lba_end = htole64(start + ofs +
    242 		    le32toh(dl->d_partitions[i].p_size) - 1LL);
    243 		ent++;
    244 	}
    245 
    246 	return (ent);
    247 }
    248 
    249 static void
    250 migrate(int fd)
    251 {
    252 	uuid_t uuid;
    253 	off_t blocks, last;
    254 	map_t *gpt, *tpg;
    255 	map_t *tbl, *lbt;
    256 	map_t *map;
    257 	struct gpt_hdr *hdr;
    258 	struct gpt_ent *ent;
    259 	struct mbr *mbr;
    260 	uint32_t start, size;
    261 	unsigned int i;
    262 
    263 	last = mediasz / secsz - 1LL;
    264 
    265 	map = map_find(MAP_TYPE_MBR);
    266 	if (map == NULL || map->map_start != 0) {
    267 		warnx("%s: error: no partitions to convert", device_name);
    268 		return;
    269 	}
    270 
    271 	mbr = map->map_data;
    272 
    273 	if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
    274 	    map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
    275 		warnx("%s: error: device already contains a GPT", device_name);
    276 		return;
    277 	}
    278 
    279 	/* Get the amount of free space after the MBR */
    280 	blocks = map_free(1LL, 0LL);
    281 	if (blocks == 0LL) {
    282 		warnx("%s: error: no room for the GPT header", device_name);
    283 		return;
    284 	}
    285 
    286 	/* Don't create more than parts entries. */
    287 	if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
    288 		blocks = (parts * sizeof(struct gpt_ent)) / secsz;
    289 		if ((parts * sizeof(struct gpt_ent)) % secsz)
    290 			blocks++;
    291 		blocks++;		/* Don't forget the header itself */
    292 	}
    293 
    294 	/* Never cross the median of the device. */
    295 	if ((blocks + 1LL) > ((last + 1LL) >> 1))
    296 		blocks = ((last + 1LL) >> 1) - 1LL;
    297 
    298 	/*
    299 	 * Get the amount of free space at the end of the device and
    300 	 * calculate the size for the GPT structures.
    301 	 */
    302 	map = map_last();
    303 	if (map->map_type != MAP_TYPE_UNUSED) {
    304 		warnx("%s: error: no room for the backup header", device_name);
    305 		return;
    306 	}
    307 
    308 	if (map->map_size < blocks)
    309 		blocks = map->map_size;
    310 	if (blocks == 1LL) {
    311 		warnx("%s: error: no room for the GPT table", device_name);
    312 		return;
    313 	}
    314 
    315 	blocks--;		/* Number of blocks in the GPT table. */
    316 	gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
    317 	tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
    318 	    calloc(blocks, secsz));
    319 	if (gpt == NULL || tbl == NULL)
    320 		return;
    321 
    322 	lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
    323 	    tbl->map_data);
    324 	tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz));
    325 
    326 	hdr = gpt->map_data;
    327 	memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
    328 	hdr->hdr_revision = htole32(GPT_HDR_REVISION);
    329 	/*
    330 	 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
    331 	 * contains padding we must not include in the size.
    332 	 */
    333 	hdr->hdr_size = htole32(GPT_SIZE);
    334 	hdr->hdr_lba_self = htole64(gpt->map_start);
    335 	hdr->hdr_lba_alt = htole64(tpg->map_start);
    336 	hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
    337 	hdr->hdr_lba_end = htole64(lbt->map_start - 1LL);
    338 	uuid_create(&uuid, NULL);
    339 	le_uuid_enc(hdr->hdr_uuid, &uuid);
    340 	hdr->hdr_lba_table = htole64(tbl->map_start);
    341 	hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
    342 	if (le32toh(hdr->hdr_entries) > parts)
    343 		hdr->hdr_entries = htole32(parts);
    344 	hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
    345 
    346 	ent = tbl->map_data;
    347 	for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    348 		uuid_create(&uuid, NULL);
    349 		le_uuid_enc(ent[i].ent_uuid, &uuid);
    350 	}
    351 
    352 	/* Mirror partitions. */
    353 	for (i = 0; i < 4; i++) {
    354 		start = le16toh(mbr->mbr_part[i].part_start_hi);
    355 		start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
    356 		size = le16toh(mbr->mbr_part[i].part_size_hi);
    357 		size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);
    358 
    359 		switch (mbr->mbr_part[i].part_typ) {
    360 		case MBR_PTYPE_UNUSED:
    361 			continue;
    362 		case MBR_PTYPE_386BSD: {	/* FreeBSD */
    363 			if (slice) {
    364 				static const uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
    365 				le_uuid_enc(ent->ent_type, &freebsd);
    366 				ent->ent_lba_start = htole64((uint64_t)start);
    367 				ent->ent_lba_end = htole64(start + size - 1LL);
    368 				utf8_to_utf16((const uint8_t *)"FreeBSD disklabel partition",
    369 				    ent->ent_name, 36);
    370 				ent++;
    371 			} else
    372 				ent = migrate_disklabel(fd, start, ent);
    373 			break;
    374 		}
    375 		case MBR_PTYPE_NETBSD:
    376 			ent = migrate_netbsd_disklabel(fd, start, ent);
    377 			break;
    378 		case MBR_PTYPE_EFI: {
    379 			static const uuid_t efi_slice = GPT_ENT_TYPE_EFI;
    380 			le_uuid_enc(ent->ent_type, &efi_slice);
    381 			ent->ent_lba_start = htole64((uint64_t)start);
    382 			ent->ent_lba_end = htole64(start + size - 1LL);
    383 			utf8_to_utf16((const uint8_t *)"EFI system partition",
    384 			    ent->ent_name, 36);
    385 			ent++;
    386 			break;
    387 		}
    388 		default:
    389 			if (!force) {
    390 				warnx("%s: error: unknown partition type (%d)",
    391 				    device_name, mbr->mbr_part[i].part_typ);
    392 				return;
    393 			}
    394 		}
    395 	}
    396 	ent = tbl->map_data;
    397 
    398 	hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
    399 	    le32toh(hdr->hdr_entsz)));
    400 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    401 
    402 	gpt_write(fd, gpt);
    403 	gpt_write(fd, tbl);
    404 
    405 	/*
    406 	 * Create backup GPT.
    407 	 */
    408 	memcpy(tpg->map_data, gpt->map_data, secsz);
    409 	hdr = tpg->map_data;
    410 	hdr->hdr_lba_self = htole64(tpg->map_start);
    411 	hdr->hdr_lba_alt = htole64(gpt->map_start);
    412 	hdr->hdr_lba_table = htole64(lbt->map_start);
    413 	hdr->hdr_crc_self = 0;			/* Don't ever forget this! */
    414 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    415 
    416 	gpt_write(fd, lbt);
    417 	gpt_write(fd, tpg);
    418 
    419 	map = map_find(MAP_TYPE_MBR);
    420 	mbr = map->map_data;
    421 	/*
    422 	 * Turn the MBR into a Protective MBR.
    423 	 */
    424 	bzero(mbr->mbr_part, sizeof(mbr->mbr_part));
    425 	mbr->mbr_part[0].part_shd = 0xff;
    426 	mbr->mbr_part[0].part_ssect = 0xff;
    427 	mbr->mbr_part[0].part_scyl = 0xff;
    428 	mbr->mbr_part[0].part_typ = 0xee;
    429 	mbr->mbr_part[0].part_ehd = 0xff;
    430 	mbr->mbr_part[0].part_esect = 0xff;
    431 	mbr->mbr_part[0].part_ecyl = 0xff;
    432 	mbr->mbr_part[0].part_start_lo = htole16(1);
    433 	if (last > 0xffffffff) {
    434 		mbr->mbr_part[0].part_size_lo = htole16(0xffff);
    435 		mbr->mbr_part[0].part_size_hi = htole16(0xffff);
    436 	} else {
    437 		mbr->mbr_part[0].part_size_lo = htole16(last);
    438 		mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
    439 	}
    440 	gpt_write(fd, map);
    441 }
    442 
    443 int
    444 cmd_migrate(int argc, char *argv[])
    445 {
    446 	int ch, fd;
    447 
    448 	/* Get the migrate options */
    449 	while ((ch = getopt(argc, argv, "fs")) != -1) {
    450 		switch(ch) {
    451 		case 'f':
    452 			force = 1;
    453 			break;
    454 		case 's':
    455 			slice = 1;
    456 			break;
    457 		default:
    458 			usage_migrate();
    459 		}
    460 	}
    461 
    462 	if (argc == optind)
    463 		usage_migrate();
    464 
    465 	while (optind < argc) {
    466 		fd = gpt_open(argv[optind++]);
    467 		if (fd == -1) {
    468 			warn("unable to open device '%s'", device_name);
    469 			continue;
    470 		}
    471 
    472 		migrate(fd);
    473 
    474 		gpt_close(fd);
    475 	}
    476 
    477 	return (0);
    478 }
    479