migrate.c revision 1.28 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 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
29 #endif
30
31 #include <sys/cdefs.h>
32 #ifdef __FBSDID
33 __FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $");
34 #endif
35 #ifdef __RCSID
36 __RCSID("$NetBSD: migrate.c,v 1.28 2015/12/05 18:46:08 christos Exp $");
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #define FSTYPENAMES
42 #define MBRPTYPENAMES
43 #ifdef HAVE_NBTOOL_CONFIG_H
44 #include <nbinclude/sys/bootblock.h>
45 #include <nbinclude/sys/disklabel.h>
46 #else
47 #include <sys/bootblock.h>
48 #include <sys/disklabel.h>
49 #endif
50
51 #include <err.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include "map.h"
59 #include "gpt.h"
60 #include "gpt_private.h"
61
62 /*
63 * Allow compilation on platforms that do not have a BSD label.
64 * The values are valid for amd64, i386 and ia64 disklabels.
65 * XXX: use disklabel_params from disklabel.c
66 */
67 #ifndef LABELOFFSET
68 #define LABELOFFSET 0
69 #endif
70 #ifndef LABELSECTOR
71 #define LABELSECTOR 1
72 #endif
73 #ifndef RAW_PART
74 #define RAW_PART 3
75 #endif
76
77 /* FreeBSD filesystem types that don't match corresponding NetBSD types */
78 #define FREEBSD_FS_VINUM 14
79 #define FREEBSD_FS_ZFS 27
80
81 static int cmd_migrate(gpt_t, int, char *[]);
82
83 static const char *migratehelp[] = {
84 "[-fs] [-p partitions]",
85 };
86
87 struct gpt_cmd c_migrate = {
88 "migrate",
89 cmd_migrate,
90 migratehelp, __arraycount(migratehelp),
91 0,
92 };
93
94 #define usage() gpt_usage(NULL, &c_migrate)
95
96 static const char *
97 fstypename(u_int t)
98 {
99 static char buf[64];
100 if (t >= __arraycount(fstypenames)) {
101 snprintf(buf, sizeof(buf), "*%u*", t);
102 return buf;
103 }
104 return fstypenames[t];
105 }
106
107 static const char *
108 mbrptypename(u_int t)
109 {
110 static char buf[64];
111 size_t i;
112
113 for (i = 0; i < __arraycount(mbr_ptypes); i++)
114 if ((u_int)mbr_ptypes[i].id == t)
115 return mbr_ptypes[i].name;
116
117 snprintf(buf, sizeof(buf), "*%u*", t);
118 return buf;
119 }
120
121 static struct gpt_ent *
122 migrate_disklabel(gpt_t gpt, off_t start, struct gpt_ent *ent)
123 {
124 char *buf;
125 struct disklabel *dl;
126 off_t ofs, rawofs;
127 int i;
128
129 buf = gpt_read(gpt, start + LABELSECTOR, 1);
130 if (buf == NULL) {
131 gpt_warn(gpt, "Error reading label");
132 return NULL;
133 }
134 dl = (void*)(buf + LABELOFFSET);
135
136 if (le32toh(dl->d_magic) != DISKMAGIC ||
137 le32toh(dl->d_magic2) != DISKMAGIC) {
138 gpt_warnx(gpt, "FreeBSD slice without disklabel");
139 free(buf);
140 return (ent);
141 }
142
143 rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
144 le32toh(dl->d_secsize);
145 for (i = 0; i < le16toh(dl->d_npartitions); i++) {
146 if (dl->d_partitions[i].p_fstype == FS_UNUSED)
147 continue;
148 ofs = le32toh(dl->d_partitions[i].p_offset) *
149 le32toh(dl->d_secsize);
150 if (ofs < rawofs)
151 rawofs = 0;
152 }
153 rawofs /= gpt->secsz;
154
155 for (i = 0; i < le16toh(dl->d_npartitions); i++) {
156 switch (dl->d_partitions[i].p_fstype) {
157 case FS_UNUSED:
158 continue;
159 case FS_SWAP: {
160 gpt_uuid_create(GPT_TYPE_FREEBSD_SWAP, ent->ent_type,
161 ent->ent_name, sizeof(ent->ent_name));
162 break;
163 }
164 case FS_BSDFFS: {
165 gpt_uuid_create(GPT_TYPE_FREEBSD_UFS, ent->ent_type,
166 ent->ent_name, sizeof(ent->ent_name));
167 break;
168 }
169 case FREEBSD_FS_VINUM: {
170 gpt_uuid_create(GPT_TYPE_FREEBSD_VINUM, ent->ent_type,
171 ent->ent_name, sizeof(ent->ent_name));
172 break;
173 }
174 case FREEBSD_FS_ZFS: {
175 gpt_uuid_create(GPT_TYPE_FREEBSD_ZFS, ent->ent_type,
176 ent->ent_name, sizeof(ent->ent_name));
177 break;
178 }
179 default:
180 gpt_warnx(gpt, "Unknown FreeBSD partition (%d)",
181 dl->d_partitions[i].p_fstype);
182 continue;
183 }
184
185 ofs = (le32toh(dl->d_partitions[i].p_offset) *
186 le32toh(dl->d_secsize)) / gpt->secsz;
187 ofs = (ofs > 0) ? ofs - rawofs : 0;
188 ent->ent_lba_start = htole64((uint64_t)(start + ofs));
189 ent->ent_lba_end = htole64((uint64_t)(start + ofs +
190 (off_t)le32toh((uint64_t)dl->d_partitions[i].p_size)
191 - 1LL));
192 ent++;
193 }
194
195 free(buf);
196 return ent;
197 }
198
199 static struct gpt_ent*
200 migrate_netbsd_disklabel(gpt_t gpt, off_t start, struct gpt_ent *ent)
201 {
202 char *buf;
203 struct disklabel *dl;
204 off_t ofs, rawofs;
205 unsigned int i;
206 gpt_type_t type;
207
208 buf = gpt_read(gpt, start + LABELSECTOR, 1);
209 if (buf == NULL) {
210 gpt_warn(gpt, "Error reading label");
211 return NULL;
212 }
213 dl = (void*)(buf + LABELOFFSET);
214
215 if (le32toh(dl->d_magic) != DISKMAGIC ||
216 le32toh(dl->d_magic2) != DISKMAGIC) {
217 gpt_warnx(gpt, "NetBSD slice without disklabel");
218 free(buf);
219 return ent;
220 }
221
222 rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
223 le32toh(dl->d_secsize);
224 for (i = 0; i < le16toh(dl->d_npartitions); i++) {
225 if (dl->d_partitions[i].p_fstype == FS_UNUSED)
226 continue;
227 ofs = le32toh(dl->d_partitions[i].p_offset) *
228 le32toh(dl->d_secsize);
229 if (ofs < rawofs)
230 rawofs = 0;
231 }
232 if (gpt->verbose > 1)
233 gpt_msg(gpt, "rawofs=%ju", (uintmax_t)rawofs);
234 rawofs /= gpt->secsz;
235
236 for (i = 0; i < le16toh(dl->d_npartitions); i++) {
237 if (gpt->verbose > 1)
238 gpt_msg(gpt, "Disklabel partition %u type %s", i,
239 fstypename(dl->d_partitions[i].p_fstype));
240
241 switch (dl->d_partitions[i].p_fstype) {
242 case FS_UNUSED:
243 continue;
244 case FS_HFS:
245 type = GPT_TYPE_APPLE_HFS;
246 break;
247 case FS_EX2FS:
248 type = GPT_TYPE_LINUX_DATA;
249 break;
250 case FS_SWAP:
251 type = GPT_TYPE_NETBSD_SWAP;
252 break;
253 case FS_BSDFFS:
254 type = GPT_TYPE_NETBSD_FFS;
255 break;
256 case FS_BSDLFS:
257 type = GPT_TYPE_NETBSD_LFS;
258 break;
259 case FS_RAID:
260 type = GPT_TYPE_NETBSD_RAIDFRAME;
261 break;
262 case FS_CCD:
263 type = GPT_TYPE_NETBSD_CCD;
264 break;
265 case FS_CGD:
266 type = GPT_TYPE_NETBSD_CGD;
267 break;
268 default:
269 gpt_warnx(gpt, "Partition %u unknown type %s, "
270 "using \"Microsoft Basic Data\"", i,
271 fstypename(dl->d_partitions[i].p_fstype));
272 type = GPT_TYPE_MS_BASIC_DATA;
273 break;
274 }
275
276 gpt_uuid_create(type, ent->ent_type,
277 ent->ent_name, sizeof(ent->ent_name));
278
279 ofs = (le32toh(dl->d_partitions[i].p_offset) *
280 le32toh(dl->d_secsize)) / gpt->secsz;
281 ofs = (ofs > 0) ? ofs - rawofs : 0;
282 ent->ent_lba_start = htole64((uint64_t)ofs);
283 ent->ent_lba_end = htole64((uint64_t)(ofs +
284 (off_t)le32toh((uint64_t)dl->d_partitions[i].p_size)
285 - 1LL));
286 ent++;
287 }
288
289 free(buf);
290 return ent;
291 }
292
293 static int
294 migrate(gpt_t gpt, u_int parts, int force, int slice)
295 {
296 off_t last = gpt_last(gpt);
297 map_t map;
298 struct gpt_ent *ent;
299 struct mbr *mbr;
300 uint32_t start, size;
301 unsigned int i;
302
303 map = map_find(gpt, MAP_TYPE_MBR);
304 if (map == NULL || map->map_start != 0) {
305 gpt_warnx(gpt, "No MBR in disk to convert");
306 return -1;
307 }
308
309 mbr = map->map_data;
310
311 if (gpt_create(gpt, last, parts, 0) == -1)
312 return -1;
313
314 ent = gpt->tbl->map_data;
315
316 /* Mirror partitions. */
317 for (i = 0; i < 4; i++) {
318 start = le16toh(mbr->mbr_part[i].part_start_hi);
319 start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
320 size = le16toh(mbr->mbr_part[i].part_size_hi);
321 size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);
322
323 if (gpt->verbose > 1)
324 gpt_msg(gpt, "MBR partition %u type %s", i,
325 mbrptypename(mbr->mbr_part[i].part_typ));
326 switch (mbr->mbr_part[i].part_typ) {
327 case MBR_PTYPE_UNUSED:
328 continue;
329 case MBR_PTYPE_386BSD: { /* FreeBSD */
330 if (slice) {
331 gpt_uuid_create(GPT_TYPE_FREEBSD,
332 ent->ent_type, ent->ent_name,
333 sizeof(ent->ent_name));
334 ent->ent_lba_start = htole64((uint64_t)start);
335 ent->ent_lba_end = htole64(
336 (uint64_t)(start + size - 1LL));
337 ent++;
338 } else
339 ent = migrate_disklabel(gpt, start, ent);
340 break;
341 }
342 case MBR_PTYPE_NETBSD:
343 ent = migrate_netbsd_disklabel(gpt, start, ent);
344 break;
345 case MBR_PTYPE_EFI: {
346 gpt_uuid_create(GPT_TYPE_EFI,
347 ent->ent_type, ent->ent_name,
348 sizeof(ent->ent_name));
349 ent->ent_lba_start = htole64((uint64_t)start);
350 ent->ent_lba_end = htole64(
351 (uint64_t)(start + size - 1LL));
352 ent++;
353 break;
354 }
355 default:
356 if (!force) {
357 gpt_warnx(gpt, "unknown partition type (%d)",
358 mbr->mbr_part[i].part_typ);
359 return -1;
360 }
361 break;
362 }
363 }
364
365 if (gpt_write_primary(gpt) == -1)
366 return -1;
367
368 if (gpt_write_backup(gpt) == -1)
369 return -1;
370
371 /*
372 * Turn the MBR into a Protective MBR.
373 */
374 memset(mbr->mbr_part, 0, sizeof(mbr->mbr_part));
375 gpt_create_pmbr_part(mbr->mbr_part, last);
376 if (gpt_write(gpt, map) == -1) {
377 gpt_warn(gpt, "Cant write PMBR");
378 return -1;
379 }
380 return 0;
381 }
382
383 static int
384 cmd_migrate(gpt_t gpt, int argc, char *argv[])
385 {
386 int ch;
387 int force = 0;
388 int slice = 0;
389 u_int parts = 128;
390
391 /* Get the migrate options */
392 while ((ch = getopt(argc, argv, "fp:s")) != -1) {
393 switch(ch) {
394 case 'f':
395 force = 1;
396 break;
397 case 'p':
398 if (gpt_uint_get(&parts) == -1)
399 return usage();
400 break;
401 case 's':
402 slice = 1;
403 break;
404 default:
405 return usage();
406 }
407 }
408
409 if (argc != optind)
410 return usage();
411
412 return migrate(gpt, parts, force, slice);
413 }
414