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