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