2 * Copyright (c) 2002 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $
27 * $DragonFly: src/sbin/gpt/migrate.c,v 1.1 2007/06/16 22:29:27 dillon Exp $
30 #include <sys/types.h>
31 #include <sys/disklabel.h>
44 * Allow compilation on platforms that do not have a BSD label.
45 * The values are valid for amd64, i386 and ia64 disklabels.
62 "usage: %s [-fs] device ...\n", getprogname());
66 static struct gpt_ent
*
67 migrate_disklabel(int fd
, off_t start
, struct gpt_ent
*ent
)
74 buf
= gpt_read(fd
, start
+ LABELSECTOR
, 1);
75 dl
= (void*)(buf
+ LABELOFFSET
);
77 if (le32toh(dl
->d_magic
) != DISKMAGIC
||
78 le32toh(dl
->d_magic2
) != DISKMAGIC
) {
79 warnx("%s: warning: FreeBSD slice without disklabel",
84 rawofs
= le32toh(dl
->d_partitions
[RAW_PART
].p_offset
) *
85 le32toh(dl
->d_secsize
);
86 for (i
= 0; i
< le16toh(dl
->d_npartitions
); i
++) {
87 if (dl
->d_partitions
[i
].p_fstype
== FS_UNUSED
)
89 ofs
= le32toh(dl
->d_partitions
[i
].p_offset
) *
90 le32toh(dl
->d_secsize
);
96 for (i
= 0; i
< le16toh(dl
->d_npartitions
); i
++) {
97 switch (dl
->d_partitions
[i
].p_fstype
) {
101 uuid_t swap
= GPT_ENT_TYPE_FREEBSD_SWAP
;
102 le_uuid_enc(&ent
->ent_type
, &swap
);
103 utf8_to_utf16("FreeBSD swap partition",
108 uuid_t ufs
= GPT_ENT_TYPE_FREEBSD_UFS
;
109 le_uuid_enc(&ent
->ent_type
, &ufs
);
110 utf8_to_utf16("FreeBSD UFS partition",
115 uuid_t vinum
= GPT_ENT_TYPE_FREEBSD_VINUM
;
116 le_uuid_enc(&ent
->ent_type
, &vinum
);
117 utf8_to_utf16("FreeBSD vinum partition",
122 warnx("%s: warning: unknown FreeBSD partition (%d)",
123 device_name
, dl
->d_partitions
[i
].p_fstype
);
127 ofs
= (le32toh(dl
->d_partitions
[i
].p_offset
) *
128 le32toh(dl
->d_secsize
)) / secsz
;
129 ofs
= (ofs
> 0) ? ofs
- rawofs
: 0;
130 ent
->ent_lba_start
= htole64(start
+ ofs
);
131 ent
->ent_lba_end
= htole64(start
+ ofs
+
132 le32toh(dl
->d_partitions
[i
].p_size
) - 1LL);
150 uint32_t start
, size
;
153 last
= mediasz
/ secsz
- 1LL;
155 map
= map_find(MAP_TYPE_MBR
);
156 if (map
== NULL
|| map
->map_start
!= 0) {
157 warnx("%s: error: no partitions to convert", device_name
);
163 if (map_find(MAP_TYPE_PRI_GPT_HDR
) != NULL
||
164 map_find(MAP_TYPE_SEC_GPT_HDR
) != NULL
) {
165 warnx("%s: error: device already contains a GPT", device_name
);
169 /* Get the amount of free space after the MBR */
170 blocks
= map_free(1LL, 0LL);
172 warnx("%s: error: no room for the GPT header", device_name
);
176 /* Don't create more than parts entries. */
177 if ((uint64_t)(blocks
- 1) * secsz
> parts
* sizeof(struct gpt_ent
)) {
178 blocks
= (parts
* sizeof(struct gpt_ent
)) / secsz
;
179 if ((parts
* sizeof(struct gpt_ent
)) % secsz
)
181 blocks
++; /* Don't forget the header itself */
184 /* Never cross the median of the device. */
185 if ((blocks
+ 1LL) > ((last
+ 1LL) >> 1))
186 blocks
= ((last
+ 1LL) >> 1) - 1LL;
189 * Get the amount of free space at the end of the device and
190 * calculate the size for the GPT structures.
193 if (map
->map_type
!= MAP_TYPE_UNUSED
) {
194 warnx("%s: error: no room for the backup header", device_name
);
198 if (map
->map_size
< blocks
)
199 blocks
= map
->map_size
;
201 warnx("%s: error: no room for the GPT table", device_name
);
205 blocks
--; /* Number of blocks in the GPT table. */
206 gpt
= map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR
, calloc(1, secsz
));
207 tbl
= map_add(2LL, blocks
, MAP_TYPE_PRI_GPT_TBL
,
208 calloc(blocks
, secsz
));
209 if (gpt
== NULL
|| tbl
== NULL
)
212 lbt
= map_add(last
- blocks
, blocks
, MAP_TYPE_SEC_GPT_TBL
,
214 tpg
= map_add(last
, 1LL, MAP_TYPE_SEC_GPT_HDR
, calloc(1, secsz
));
217 memcpy(hdr
->hdr_sig
, GPT_HDR_SIG
, sizeof(hdr
->hdr_sig
));
218 hdr
->hdr_revision
= htole32(GPT_HDR_REVISION
);
220 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
221 * contains padding we must not include in the size.
223 hdr
->hdr_size
= htole32(offsetof(struct gpt_hdr
, padding
));
224 hdr
->hdr_lba_self
= htole64(gpt
->map_start
);
225 hdr
->hdr_lba_alt
= htole64(tpg
->map_start
);
226 hdr
->hdr_lba_start
= htole64(tbl
->map_start
+ blocks
);
227 hdr
->hdr_lba_end
= htole64(lbt
->map_start
- 1LL);
228 uuid_create(&uuid
, NULL
);
229 le_uuid_enc(&hdr
->hdr_uuid
, &uuid
);
230 hdr
->hdr_lba_table
= htole64(tbl
->map_start
);
231 hdr
->hdr_entries
= htole32((blocks
* secsz
) / sizeof(struct gpt_ent
));
232 if (le32toh(hdr
->hdr_entries
) > parts
)
233 hdr
->hdr_entries
= htole32(parts
);
234 hdr
->hdr_entsz
= htole32(sizeof(struct gpt_ent
));
237 for (i
= 0; i
< le32toh(hdr
->hdr_entries
); i
++) {
238 uuid_create(&uuid
, NULL
);
239 le_uuid_enc(&ent
[i
].ent_uuid
, &uuid
);
242 /* Mirror partitions. */
243 for (i
= 0; i
< 4; i
++) {
244 start
= le16toh(mbr
->mbr_part
[i
].part_start_hi
);
245 start
= (start
<< 16) + le16toh(mbr
->mbr_part
[i
].part_start_lo
);
246 size
= le16toh(mbr
->mbr_part
[i
].part_size_hi
);
247 size
= (size
<< 16) + le16toh(mbr
->mbr_part
[i
].part_size_lo
);
249 switch (mbr
->mbr_part
[i
].part_typ
) {
252 case 165: { /* FreeBSD */
254 uuid_t freebsd
= GPT_ENT_TYPE_FREEBSD
;
255 le_uuid_enc(&ent
->ent_type
, &freebsd
);
256 ent
->ent_lba_start
= htole64((uint64_t)start
);
257 ent
->ent_lba_end
= htole64(start
+ size
- 1LL);
258 utf8_to_utf16("FreeBSD disklabel partition",
262 ent
= migrate_disklabel(fd
, start
, ent
);
265 case 239: { /* EFI */
266 uuid_t efi_slice
= GPT_ENT_TYPE_EFI
;
267 le_uuid_enc(&ent
->ent_type
, &efi_slice
);
268 ent
->ent_lba_start
= htole64((uint64_t)start
);
269 ent
->ent_lba_end
= htole64(start
+ size
- 1LL);
270 utf8_to_utf16("EFI system partition",
277 warnx("%s: error: unknown partition type (%d)",
278 device_name
, mbr
->mbr_part
[i
].part_typ
);
285 hdr
->hdr_crc_table
= htole32(crc32(ent
, le32toh(hdr
->hdr_entries
) *
286 le32toh(hdr
->hdr_entsz
)));
287 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
295 memcpy(tpg
->map_data
, gpt
->map_data
, secsz
);
297 hdr
->hdr_lba_self
= htole64(tpg
->map_start
);
298 hdr
->hdr_lba_alt
= htole64(gpt
->map_start
);
299 hdr
->hdr_lba_table
= htole64(lbt
->map_start
);
300 hdr
->hdr_crc_self
= 0; /* Don't ever forget this! */
301 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
306 map
= map_find(MAP_TYPE_MBR
);
309 * Turn the MBR into a Protective MBR.
311 bzero(mbr
->mbr_part
, sizeof(mbr
->mbr_part
));
312 mbr
->mbr_part
[0].part_shd
= 0xff;
313 mbr
->mbr_part
[0].part_ssect
= 0xff;
314 mbr
->mbr_part
[0].part_scyl
= 0xff;
315 mbr
->mbr_part
[0].part_typ
= 0xee;
316 mbr
->mbr_part
[0].part_ehd
= 0xff;
317 mbr
->mbr_part
[0].part_esect
= 0xff;
318 mbr
->mbr_part
[0].part_ecyl
= 0xff;
319 mbr
->mbr_part
[0].part_start_lo
= htole16(1);
320 if (last
> 0xffffffff) {
321 mbr
->mbr_part
[0].part_size_lo
= htole16(0xffff);
322 mbr
->mbr_part
[0].part_size_hi
= htole16(0xffff);
324 mbr
->mbr_part
[0].part_size_lo
= htole16(last
);
325 mbr
->mbr_part
[0].part_size_hi
= htole16(last
>> 16);
331 cmd_migrate(int argc
, char *argv
[])
335 /* Get the migrate options */
336 while ((ch
= getopt(argc
, argv
, "fs")) != -1) {
352 while (optind
< argc
) {
353 fd
= gpt_open(argv
[optind
++]);
355 warn("unable to open device '%s'", device_name
);