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