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