Home | History | Annotate | Line # | Download | only in gpt
migrate.c revision 1.19
      1   1.1  christos /*-
      2   1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      3   1.1  christos  * All rights reserved.
      4   1.1  christos  *
      5   1.1  christos  * Redistribution and use in source and binary forms, with or without
      6   1.1  christos  * modification, are permitted provided that the following conditions
      7   1.1  christos  * are met:
      8   1.1  christos  *
      9   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  christos  *    documentation and/or other materials provided with the distribution.
     14   1.1  christos  *
     15   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25   1.1  christos  */
     26   1.1  christos 
     27  1.15  christos #if HAVE_NBTOOL_CONFIG_H
     28  1.15  christos #include "nbtool_config.h"
     29  1.15  christos #endif
     30  1.15  christos 
     31   1.1  christos #include <sys/cdefs.h>
     32   1.2  christos #ifdef __FBSDID
     33   1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $");
     34   1.2  christos #endif
     35   1.2  christos #ifdef __RCSID
     36  1.19  christos __RCSID("$NetBSD: migrate.c,v 1.19 2014/09/30 17:59:59 christos Exp $");
     37   1.2  christos #endif
     38   1.1  christos 
     39   1.1  christos #include <sys/types.h>
     40   1.3        he #include <sys/param.h>
     41  1.17  christos #ifdef HAVE_NBTOOL_CONFIG_H
     42  1.17  christos #include <nbinclude/sys/bootblock.h>
     43  1.17  christos #include <nbinclude/sys/disklabel.h>
     44  1.17  christos #else
     45   1.9   jnemeth #include <sys/bootblock.h>
     46   1.1  christos #include <sys/disklabel.h>
     47  1.17  christos #endif
     48   1.1  christos 
     49   1.1  christos #include <err.h>
     50   1.1  christos #include <stddef.h>
     51   1.1  christos #include <stdio.h>
     52   1.1  christos #include <stdlib.h>
     53   1.1  christos #include <string.h>
     54   1.1  christos #include <unistd.h>
     55   1.1  christos 
     56   1.1  christos #include "map.h"
     57   1.1  christos #include "gpt.h"
     58   1.1  christos 
     59   1.1  christos /*
     60   1.1  christos  * Allow compilation on platforms that do not have a BSD label.
     61   1.1  christos  * The values are valid for amd64, i386 and ia64 disklabels.
     62  1.15  christos  * XXX: use disklabel_params from disklabel.c
     63   1.1  christos  */
     64   1.1  christos #ifndef LABELOFFSET
     65   1.1  christos #define	LABELOFFSET	0
     66   1.1  christos #endif
     67   1.1  christos #ifndef LABELSECTOR
     68   1.1  christos #define	LABELSECTOR	1
     69   1.1  christos #endif
     70  1.15  christos #ifndef RAW_PART
     71  1.15  christos #define	RAW_PART	3
     72  1.15  christos #endif
     73   1.1  christos 
     74  1.10   jnemeth /* FreeBSD filesystem types that don't match corresponding NetBSD types */
     75  1.10   jnemeth #define	FREEBSD_FS_VINUM	14
     76  1.10   jnemeth #define	FREEBSD_FS_ZFS		27
     77  1.10   jnemeth 
     78   1.1  christos static int force;
     79   1.1  christos static int slice;
     80   1.1  christos 
     81   1.4       riz const char migratemsg[] = "migrate [-fs] device ...";
     82   1.4       riz 
     83   1.5     joerg __dead static void
     84   1.1  christos usage_migrate(void)
     85   1.1  christos {
     86   1.1  christos 
     87   1.1  christos 	fprintf(stderr,
     88   1.4       riz 	    "usage: %s %s\n", getprogname(), migratemsg);
     89   1.1  christos 	exit(1);
     90   1.1  christos }
     91   1.1  christos 
     92   1.1  christos static struct gpt_ent*
     93   1.1  christos migrate_disklabel(int fd, off_t start, struct gpt_ent *ent)
     94   1.1  christos {
     95   1.1  christos 	char *buf;
     96   1.1  christos 	struct disklabel *dl;
     97   1.1  christos 	off_t ofs, rawofs;
     98   1.1  christos 	int i;
     99   1.1  christos 
    100   1.1  christos 	buf = gpt_read(fd, start + LABELSECTOR, 1);
    101   1.1  christos 	dl = (void*)(buf + LABELOFFSET);
    102   1.1  christos 
    103   1.1  christos 	if (le32toh(dl->d_magic) != DISKMAGIC ||
    104   1.1  christos 	    le32toh(dl->d_magic2) != DISKMAGIC) {
    105   1.1  christos 		warnx("%s: warning: FreeBSD slice without disklabel",
    106   1.1  christos 		    device_name);
    107  1.12  christos 		free(buf);
    108   1.1  christos 		return (ent);
    109   1.1  christos 	}
    110   1.1  christos 
    111   1.1  christos 	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
    112   1.1  christos 	    le32toh(dl->d_secsize);
    113   1.1  christos 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    114   1.1  christos 		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
    115   1.1  christos 			continue;
    116   1.1  christos 		ofs = le32toh(dl->d_partitions[i].p_offset) *
    117   1.1  christos 		    le32toh(dl->d_secsize);
    118   1.1  christos 		if (ofs < rawofs)
    119   1.1  christos 			rawofs = 0;
    120   1.1  christos 	}
    121   1.1  christos 	rawofs /= secsz;
    122   1.1  christos 
    123   1.1  christos 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    124   1.1  christos 		switch (dl->d_partitions[i].p_fstype) {
    125   1.1  christos 		case FS_UNUSED:
    126   1.1  christos 			continue;
    127   1.1  christos 		case FS_SWAP: {
    128  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_SWAP, ent->ent_type,
    129  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    130   1.1  christos 			break;
    131   1.1  christos 		}
    132   1.1  christos 		case FS_BSDFFS: {
    133  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_UFS, ent->ent_type,
    134  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    135   1.1  christos 			break;
    136   1.1  christos 		}
    137  1.10   jnemeth 		case FREEBSD_FS_VINUM: {
    138  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_VINUM, ent->ent_type,
    139  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    140   1.1  christos 			break;
    141   1.1  christos 		}
    142  1.10   jnemeth 		case FREEBSD_FS_ZFS: {
    143  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_ZFS, ent->ent_type,
    144  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    145   1.8   jnemeth 			break;
    146   1.8   jnemeth 		}
    147   1.1  christos 		default:
    148   1.1  christos 			warnx("%s: warning: unknown FreeBSD partition (%d)",
    149   1.1  christos 			    device_name, dl->d_partitions[i].p_fstype);
    150   1.1  christos 			continue;
    151   1.1  christos 		}
    152   1.1  christos 
    153   1.1  christos 		ofs = (le32toh(dl->d_partitions[i].p_offset) *
    154   1.1  christos 		    le32toh(dl->d_secsize)) / secsz;
    155   1.1  christos 		ofs = (ofs > 0) ? ofs - rawofs : 0;
    156   1.1  christos 		ent->ent_lba_start = htole64(start + ofs);
    157   1.1  christos 		ent->ent_lba_end = htole64(start + ofs +
    158   1.1  christos 		    le32toh(dl->d_partitions[i].p_size) - 1LL);
    159   1.1  christos 		ent++;
    160   1.1  christos 	}
    161   1.1  christos 
    162  1.12  christos 	free(buf);
    163   1.1  christos 	return (ent);
    164   1.1  christos }
    165   1.1  christos 
    166   1.9   jnemeth static struct gpt_ent*
    167   1.9   jnemeth migrate_netbsd_disklabel(int fd, off_t start, struct gpt_ent *ent)
    168   1.9   jnemeth {
    169   1.9   jnemeth 	char *buf;
    170   1.9   jnemeth 	struct disklabel *dl;
    171   1.9   jnemeth 	off_t ofs, rawofs;
    172   1.9   jnemeth 	int i;
    173   1.9   jnemeth 
    174   1.9   jnemeth 	buf = gpt_read(fd, start + LABELSECTOR, 1);
    175   1.9   jnemeth 	dl = (void*)(buf + LABELOFFSET);
    176   1.9   jnemeth 
    177   1.9   jnemeth 	if (le32toh(dl->d_magic) != DISKMAGIC ||
    178   1.9   jnemeth 	    le32toh(dl->d_magic2) != DISKMAGIC) {
    179   1.9   jnemeth 		warnx("%s: warning: NetBSD slice without disklabel",
    180   1.9   jnemeth 		    device_name);
    181  1.12  christos 		free(buf);
    182   1.9   jnemeth 		return (ent);
    183   1.9   jnemeth 	}
    184   1.9   jnemeth 
    185   1.9   jnemeth 	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
    186   1.9   jnemeth 	    le32toh(dl->d_secsize);
    187   1.9   jnemeth 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    188   1.9   jnemeth 		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
    189   1.9   jnemeth 			continue;
    190   1.9   jnemeth 		ofs = le32toh(dl->d_partitions[i].p_offset) *
    191   1.9   jnemeth 		    le32toh(dl->d_secsize);
    192   1.9   jnemeth 		if (ofs < rawofs)
    193   1.9   jnemeth 			rawofs = 0;
    194   1.9   jnemeth 	}
    195   1.9   jnemeth 	rawofs /= secsz;
    196   1.9   jnemeth 
    197   1.9   jnemeth 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    198   1.9   jnemeth 		switch (dl->d_partitions[i].p_fstype) {
    199   1.9   jnemeth 		case FS_UNUSED:
    200   1.9   jnemeth 			continue;
    201   1.9   jnemeth 		case FS_SWAP: {
    202  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_SWAP, ent->ent_type,
    203  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    204   1.9   jnemeth 			break;
    205   1.9   jnemeth 		}
    206   1.9   jnemeth 		case FS_BSDFFS: {
    207  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_FFS, ent->ent_type,
    208  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    209   1.9   jnemeth 			break;
    210   1.9   jnemeth 		}
    211   1.9   jnemeth 		case FS_BSDLFS: {
    212  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_LFS, ent->ent_type,
    213  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    214   1.9   jnemeth 			break;
    215   1.9   jnemeth 		}
    216   1.9   jnemeth 		case FS_RAID: {
    217  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_RAIDFRAME, ent->ent_type,
    218  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    219   1.9   jnemeth 			break;
    220   1.9   jnemeth 		}
    221   1.9   jnemeth 		case FS_CCD: {
    222  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_CCD, ent->ent_type,
    223  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    224   1.9   jnemeth 			break;
    225   1.9   jnemeth 		}
    226   1.9   jnemeth 		case FS_CGD: {
    227  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_CGD, ent->ent_type,
    228  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    229   1.9   jnemeth 			break;
    230   1.9   jnemeth 		}
    231   1.9   jnemeth 		default:
    232   1.9   jnemeth 			warnx("%s: warning: unknown NetBSD partition (%d)",
    233   1.9   jnemeth 			    device_name, dl->d_partitions[i].p_fstype);
    234   1.9   jnemeth 			continue;
    235   1.9   jnemeth 		}
    236   1.9   jnemeth 
    237   1.9   jnemeth 		ofs = (le32toh(dl->d_partitions[i].p_offset) *
    238   1.9   jnemeth 		    le32toh(dl->d_secsize)) / secsz;
    239   1.9   jnemeth 		ofs = (ofs > 0) ? ofs - rawofs : 0;
    240  1.11   jnemeth 		ent->ent_lba_start = htole64(ofs);
    241  1.11   jnemeth 		ent->ent_lba_end = htole64(ofs +
    242   1.9   jnemeth 		    le32toh(dl->d_partitions[i].p_size) - 1LL);
    243   1.9   jnemeth 		ent++;
    244   1.9   jnemeth 	}
    245   1.9   jnemeth 
    246  1.12  christos 	free(buf);
    247   1.9   jnemeth 	return (ent);
    248   1.9   jnemeth }
    249   1.9   jnemeth 
    250   1.1  christos static void
    251   1.1  christos migrate(int fd)
    252   1.1  christos {
    253   1.1  christos 	off_t blocks, last;
    254   1.1  christos 	map_t *gpt, *tpg;
    255   1.1  christos 	map_t *tbl, *lbt;
    256   1.1  christos 	map_t *map;
    257   1.1  christos 	struct gpt_hdr *hdr;
    258   1.1  christos 	struct gpt_ent *ent;
    259   1.1  christos 	struct mbr *mbr;
    260   1.1  christos 	uint32_t start, size;
    261   1.1  christos 	unsigned int i;
    262   1.1  christos 
    263   1.1  christos 	last = mediasz / secsz - 1LL;
    264   1.1  christos 
    265   1.1  christos 	map = map_find(MAP_TYPE_MBR);
    266   1.1  christos 	if (map == NULL || map->map_start != 0) {
    267   1.1  christos 		warnx("%s: error: no partitions to convert", device_name);
    268   1.1  christos 		return;
    269   1.1  christos 	}
    270   1.1  christos 
    271   1.1  christos 	mbr = map->map_data;
    272   1.1  christos 
    273   1.1  christos 	if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
    274   1.1  christos 	    map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
    275   1.1  christos 		warnx("%s: error: device already contains a GPT", device_name);
    276   1.1  christos 		return;
    277   1.1  christos 	}
    278   1.1  christos 
    279   1.1  christos 	/* Get the amount of free space after the MBR */
    280   1.1  christos 	blocks = map_free(1LL, 0LL);
    281   1.1  christos 	if (blocks == 0LL) {
    282   1.1  christos 		warnx("%s: error: no room for the GPT header", device_name);
    283   1.1  christos 		return;
    284   1.1  christos 	}
    285   1.1  christos 
    286   1.1  christos 	/* Don't create more than parts entries. */
    287   1.1  christos 	if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
    288   1.1  christos 		blocks = (parts * sizeof(struct gpt_ent)) / secsz;
    289   1.1  christos 		if ((parts * sizeof(struct gpt_ent)) % secsz)
    290   1.1  christos 			blocks++;
    291   1.1  christos 		blocks++;		/* Don't forget the header itself */
    292   1.1  christos 	}
    293   1.1  christos 
    294   1.1  christos 	/* Never cross the median of the device. */
    295   1.1  christos 	if ((blocks + 1LL) > ((last + 1LL) >> 1))
    296   1.1  christos 		blocks = ((last + 1LL) >> 1) - 1LL;
    297   1.1  christos 
    298   1.1  christos 	/*
    299   1.1  christos 	 * Get the amount of free space at the end of the device and
    300   1.1  christos 	 * calculate the size for the GPT structures.
    301   1.1  christos 	 */
    302   1.1  christos 	map = map_last();
    303   1.1  christos 	if (map->map_type != MAP_TYPE_UNUSED) {
    304   1.1  christos 		warnx("%s: error: no room for the backup header", device_name);
    305   1.1  christos 		return;
    306   1.1  christos 	}
    307   1.1  christos 
    308   1.1  christos 	if (map->map_size < blocks)
    309   1.1  christos 		blocks = map->map_size;
    310   1.1  christos 	if (blocks == 1LL) {
    311   1.1  christos 		warnx("%s: error: no room for the GPT table", device_name);
    312   1.1  christos 		return;
    313   1.1  christos 	}
    314   1.1  christos 
    315   1.1  christos 	blocks--;		/* Number of blocks in the GPT table. */
    316   1.1  christos 	gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
    317   1.1  christos 	tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
    318   1.1  christos 	    calloc(blocks, secsz));
    319   1.1  christos 	if (gpt == NULL || tbl == NULL)
    320   1.1  christos 		return;
    321   1.1  christos 
    322   1.1  christos 	lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
    323   1.1  christos 	    tbl->map_data);
    324   1.1  christos 	tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz));
    325   1.1  christos 
    326   1.1  christos 	hdr = gpt->map_data;
    327   1.1  christos 	memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
    328   1.1  christos 	hdr->hdr_revision = htole32(GPT_HDR_REVISION);
    329   1.1  christos 	/*
    330   1.1  christos 	 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
    331   1.1  christos 	 * contains padding we must not include in the size.
    332   1.1  christos 	 */
    333  1.18  christos 	hdr->hdr_size = htole32(GPT_HDR_SIZE);
    334   1.1  christos 	hdr->hdr_lba_self = htole64(gpt->map_start);
    335   1.1  christos 	hdr->hdr_lba_alt = htole64(tpg->map_start);
    336   1.1  christos 	hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
    337   1.1  christos 	hdr->hdr_lba_end = htole64(lbt->map_start - 1LL);
    338  1.19  christos 	gpt_uuid_copy(hdr->hdr_guid, gpt_uuid_nil);
    339   1.1  christos 	hdr->hdr_lba_table = htole64(tbl->map_start);
    340   1.1  christos 	hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
    341   1.1  christos 	if (le32toh(hdr->hdr_entries) > parts)
    342   1.1  christos 		hdr->hdr_entries = htole32(parts);
    343   1.1  christos 	hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
    344   1.1  christos 
    345   1.1  christos 	ent = tbl->map_data;
    346   1.1  christos 	for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    347  1.19  christos 		gpt_uuid_copy(ent[i].ent_guid, gpt_uuid_nil);
    348   1.1  christos 	}
    349   1.1  christos 
    350   1.1  christos 	/* Mirror partitions. */
    351   1.1  christos 	for (i = 0; i < 4; i++) {
    352   1.1  christos 		start = le16toh(mbr->mbr_part[i].part_start_hi);
    353   1.1  christos 		start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
    354   1.1  christos 		size = le16toh(mbr->mbr_part[i].part_size_hi);
    355   1.1  christos 		size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);
    356   1.1  christos 
    357   1.1  christos 		switch (mbr->mbr_part[i].part_typ) {
    358   1.9   jnemeth 		case MBR_PTYPE_UNUSED:
    359   1.1  christos 			continue;
    360   1.9   jnemeth 		case MBR_PTYPE_386BSD: {	/* FreeBSD */
    361   1.1  christos 			if (slice) {
    362  1.19  christos 				gpt_uuid_create(GPT_TYPE_FREEBSD,
    363  1.19  christos 				    ent->ent_type, ent->ent_name,
    364  1.19  christos 				    sizeof(ent->ent_name));
    365   1.1  christos 				ent->ent_lba_start = htole64((uint64_t)start);
    366   1.1  christos 				ent->ent_lba_end = htole64(start + size - 1LL);
    367   1.1  christos 				ent++;
    368   1.1  christos 			} else
    369   1.1  christos 				ent = migrate_disklabel(fd, start, ent);
    370   1.1  christos 			break;
    371   1.1  christos 		}
    372   1.9   jnemeth 		case MBR_PTYPE_NETBSD:
    373   1.9   jnemeth 			ent = migrate_netbsd_disklabel(fd, start, ent);
    374   1.9   jnemeth 			break;
    375   1.9   jnemeth 		case MBR_PTYPE_EFI: {
    376  1.19  christos 			gpt_uuid_create(GPT_TYPE_EFI,
    377  1.19  christos 			    ent->ent_type, ent->ent_name,
    378  1.19  christos 			    sizeof(ent->ent_name));
    379   1.1  christos 			ent->ent_lba_start = htole64((uint64_t)start);
    380   1.1  christos 			ent->ent_lba_end = htole64(start + size - 1LL);
    381   1.1  christos 			ent++;
    382   1.1  christos 			break;
    383   1.1  christos 		}
    384   1.1  christos 		default:
    385   1.1  christos 			if (!force) {
    386   1.1  christos 				warnx("%s: error: unknown partition type (%d)",
    387   1.1  christos 				    device_name, mbr->mbr_part[i].part_typ);
    388   1.1  christos 				return;
    389   1.1  christos 			}
    390   1.1  christos 		}
    391   1.1  christos 	}
    392   1.1  christos 	ent = tbl->map_data;
    393   1.1  christos 
    394   1.1  christos 	hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
    395   1.1  christos 	    le32toh(hdr->hdr_entsz)));
    396   1.1  christos 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    397   1.1  christos 
    398   1.1  christos 	gpt_write(fd, gpt);
    399   1.1  christos 	gpt_write(fd, tbl);
    400   1.1  christos 
    401   1.1  christos 	/*
    402   1.1  christos 	 * Create backup GPT.
    403   1.1  christos 	 */
    404   1.1  christos 	memcpy(tpg->map_data, gpt->map_data, secsz);
    405   1.1  christos 	hdr = tpg->map_data;
    406   1.1  christos 	hdr->hdr_lba_self = htole64(tpg->map_start);
    407   1.1  christos 	hdr->hdr_lba_alt = htole64(gpt->map_start);
    408   1.1  christos 	hdr->hdr_lba_table = htole64(lbt->map_start);
    409   1.1  christos 	hdr->hdr_crc_self = 0;			/* Don't ever forget this! */
    410   1.1  christos 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    411   1.1  christos 
    412   1.1  christos 	gpt_write(fd, lbt);
    413   1.1  christos 	gpt_write(fd, tpg);
    414   1.1  christos 
    415   1.1  christos 	map = map_find(MAP_TYPE_MBR);
    416   1.1  christos 	mbr = map->map_data;
    417   1.1  christos 	/*
    418   1.1  christos 	 * Turn the MBR into a Protective MBR.
    419   1.1  christos 	 */
    420  1.16  christos 	memset(mbr->mbr_part, 0, sizeof(mbr->mbr_part));
    421  1.13  jakllsch 	mbr->mbr_part[0].part_shd = 0x00;
    422  1.13  jakllsch 	mbr->mbr_part[0].part_ssect = 0x02;
    423  1.13  jakllsch 	mbr->mbr_part[0].part_scyl = 0x00;
    424  1.14  jakllsch 	mbr->mbr_part[0].part_typ = MBR_PTYPE_PMBR;
    425  1.13  jakllsch 	mbr->mbr_part[0].part_ehd = 0xfe;
    426   1.1  christos 	mbr->mbr_part[0].part_esect = 0xff;
    427   1.1  christos 	mbr->mbr_part[0].part_ecyl = 0xff;
    428   1.1  christos 	mbr->mbr_part[0].part_start_lo = htole16(1);
    429   1.1  christos 	if (last > 0xffffffff) {
    430   1.1  christos 		mbr->mbr_part[0].part_size_lo = htole16(0xffff);
    431   1.1  christos 		mbr->mbr_part[0].part_size_hi = htole16(0xffff);
    432   1.1  christos 	} else {
    433   1.1  christos 		mbr->mbr_part[0].part_size_lo = htole16(last);
    434   1.1  christos 		mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
    435   1.1  christos 	}
    436   1.1  christos 	gpt_write(fd, map);
    437   1.1  christos }
    438   1.1  christos 
    439   1.1  christos int
    440   1.1  christos cmd_migrate(int argc, char *argv[])
    441   1.1  christos {
    442   1.1  christos 	int ch, fd;
    443   1.1  christos 
    444   1.1  christos 	/* Get the migrate options */
    445   1.1  christos 	while ((ch = getopt(argc, argv, "fs")) != -1) {
    446   1.1  christos 		switch(ch) {
    447   1.1  christos 		case 'f':
    448   1.1  christos 			force = 1;
    449   1.1  christos 			break;
    450   1.1  christos 		case 's':
    451   1.1  christos 			slice = 1;
    452   1.1  christos 			break;
    453   1.1  christos 		default:
    454   1.1  christos 			usage_migrate();
    455   1.1  christos 		}
    456   1.1  christos 	}
    457   1.1  christos 
    458   1.1  christos 	if (argc == optind)
    459   1.1  christos 		usage_migrate();
    460   1.1  christos 
    461   1.1  christos 	while (optind < argc) {
    462   1.1  christos 		fd = gpt_open(argv[optind++]);
    463   1.1  christos 		if (fd == -1) {
    464   1.1  christos 			warn("unable to open device '%s'", device_name);
    465   1.1  christos 			continue;
    466   1.1  christos 		}
    467   1.1  christos 
    468   1.1  christos 		migrate(fd);
    469   1.1  christos 
    470   1.1  christos 		gpt_close(fd);
    471   1.1  christos 	}
    472   1.1  christos 
    473   1.1  christos 	return (0);
    474   1.1  christos }
    475