xref: /freebsd/sys/geom/part/g_part_gpt.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2002, 2005-2007, 2011 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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/gpt.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/sbuf.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/uuid.h>
46 #include <geom/geom.h>
47 #include <geom/geom_int.h>
48 #include <geom/part/g_part.h>
49 
50 #include "g_part_if.h"
51 
52 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
53 
54 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
55 CTASSERT(sizeof(struct gpt_ent) == 128);
56 
57 #define	EQUUID(a,b)	(memcmp(a, b, sizeof(struct uuid)) == 0)
58 
59 #define	MBRSIZE		512
60 
61 enum gpt_elt {
62 	GPT_ELT_PRIHDR,
63 	GPT_ELT_PRITBL,
64 	GPT_ELT_SECHDR,
65 	GPT_ELT_SECTBL,
66 	GPT_ELT_COUNT
67 };
68 
69 enum gpt_state {
70 	GPT_STATE_UNKNOWN,	/* Not determined. */
71 	GPT_STATE_MISSING,	/* No signature found. */
72 	GPT_STATE_CORRUPT,	/* Checksum mismatch. */
73 	GPT_STATE_INVALID,	/* Nonconformant/invalid. */
74 	GPT_STATE_OK		/* Perfectly fine. */
75 };
76 
77 struct g_part_gpt_table {
78 	struct g_part_table	base;
79 	u_char			mbr[MBRSIZE];
80 	struct gpt_hdr		*hdr;
81 	quad_t			lba[GPT_ELT_COUNT];
82 	enum gpt_state		state[GPT_ELT_COUNT];
83 	int			bootcamp;
84 };
85 
86 struct g_part_gpt_entry {
87 	struct g_part_entry	base;
88 	struct gpt_ent		ent;
89 };
90 
91 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
92 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
93 static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *);
94 
95 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
96     struct g_part_parms *);
97 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
98 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
99 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
100 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
101     struct sbuf *, const char *);
102 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
103 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
104     struct g_part_parms *);
105 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
106     char *, size_t);
107 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
108 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
109 static int g_part_gpt_setunset(struct g_part_table *table,
110     struct g_part_entry *baseentry, const char *attrib, unsigned int set);
111 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
112     char *, size_t);
113 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
114 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
115     struct g_part_parms *);
116 static int g_part_gpt_recover(struct g_part_table *);
117 
118 static kobj_method_t g_part_gpt_methods[] = {
119 	KOBJMETHOD(g_part_add,		g_part_gpt_add),
120 	KOBJMETHOD(g_part_bootcode,	g_part_gpt_bootcode),
121 	KOBJMETHOD(g_part_create,	g_part_gpt_create),
122 	KOBJMETHOD(g_part_destroy,	g_part_gpt_destroy),
123 	KOBJMETHOD(g_part_dumpconf,	g_part_gpt_dumpconf),
124 	KOBJMETHOD(g_part_dumpto,	g_part_gpt_dumpto),
125 	KOBJMETHOD(g_part_modify,	g_part_gpt_modify),
126 	KOBJMETHOD(g_part_resize,	g_part_gpt_resize),
127 	KOBJMETHOD(g_part_name,		g_part_gpt_name),
128 	KOBJMETHOD(g_part_probe,	g_part_gpt_probe),
129 	KOBJMETHOD(g_part_read,		g_part_gpt_read),
130 	KOBJMETHOD(g_part_recover,	g_part_gpt_recover),
131 	KOBJMETHOD(g_part_setunset,	g_part_gpt_setunset),
132 	KOBJMETHOD(g_part_type,		g_part_gpt_type),
133 	KOBJMETHOD(g_part_write,	g_part_gpt_write),
134 	{ 0, 0 }
135 };
136 
137 static struct g_part_scheme g_part_gpt_scheme = {
138 	"GPT",
139 	g_part_gpt_methods,
140 	sizeof(struct g_part_gpt_table),
141 	.gps_entrysz = sizeof(struct g_part_gpt_entry),
142 	.gps_minent = 128,
143 	.gps_maxent = 4096,
144 	.gps_bootcodesz = MBRSIZE,
145 };
146 G_PART_SCHEME_DECLARE(g_part_gpt);
147 
148 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
149 static struct uuid gpt_uuid_apple_core_storage =
150     GPT_ENT_TYPE_APPLE_CORE_STORAGE;
151 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
152 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
153 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
154 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
155 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
156 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
157 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
158 static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
159 static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
160 static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
161 static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT;
162 static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD;
163 static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER;
164 static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2;
165 static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32;
166 static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64;
167 static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY;
168 static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP;
169 static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1;
170 static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM;
171 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
172 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
173 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
174 static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
175 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
176 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
177 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
178 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
179 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
180 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
181 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
182 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
183 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
184 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
185 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
186 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
187 static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY;
188 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
189 static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES;
190 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
191 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
192 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
193 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
194 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
195 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
196 static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA;
197 static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT;
198 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
199 static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
200 static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
201 static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
202 static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR;
203 
204 static struct g_part_uuid_alias {
205 	struct uuid *uuid;
206 	int alias;
207 	int mbrtype;
208 } gpt_uuid_alias_match[] = {
209 	{ &gpt_uuid_apple_boot,		G_PART_ALIAS_APPLE_BOOT,	 0xab },
210 	{ &gpt_uuid_apple_core_storage,	G_PART_ALIAS_APPLE_CORE_STORAGE, 0 },
211 	{ &gpt_uuid_apple_hfs,		G_PART_ALIAS_APPLE_HFS,		 0xaf },
212 	{ &gpt_uuid_apple_label,	G_PART_ALIAS_APPLE_LABEL,	 0 },
213 	{ &gpt_uuid_apple_raid,		G_PART_ALIAS_APPLE_RAID,	 0 },
214 	{ &gpt_uuid_apple_raid_offline,	G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
215 	{ &gpt_uuid_apple_tv_recovery,	G_PART_ALIAS_APPLE_TV_RECOVERY,	 0 },
216 	{ &gpt_uuid_apple_ufs,		G_PART_ALIAS_APPLE_UFS,		 0 },
217 	{ &gpt_uuid_bios_boot,		G_PART_ALIAS_BIOS_BOOT,		 0 },
218 	{ &gpt_uuid_chromeos_firmware,	G_PART_ALIAS_CHROMEOS_FIRMWARE,	 0 },
219 	{ &gpt_uuid_chromeos_kernel,	G_PART_ALIAS_CHROMEOS_KERNEL,	 0 },
220 	{ &gpt_uuid_chromeos_reserved,	G_PART_ALIAS_CHROMEOS_RESERVED,	 0 },
221 	{ &gpt_uuid_chromeos_root,	G_PART_ALIAS_CHROMEOS_ROOT,	 0 },
222 	{ &gpt_uuid_dfbsd_ccd,		G_PART_ALIAS_DFBSD_CCD,		 0 },
223 	{ &gpt_uuid_dfbsd_hammer,	G_PART_ALIAS_DFBSD_HAMMER,	 0 },
224 	{ &gpt_uuid_dfbsd_hammer2,	G_PART_ALIAS_DFBSD_HAMMER2,	 0 },
225 	{ &gpt_uuid_dfbsd_label32,	G_PART_ALIAS_DFBSD,		 0xa5 },
226 	{ &gpt_uuid_dfbsd_label64,	G_PART_ALIAS_DFBSD64,		 0xa5 },
227 	{ &gpt_uuid_dfbsd_legacy,	G_PART_ALIAS_DFBSD_LEGACY,	 0 },
228 	{ &gpt_uuid_dfbsd_swap,		G_PART_ALIAS_DFBSD_SWAP,	 0 },
229 	{ &gpt_uuid_dfbsd_ufs1,		G_PART_ALIAS_DFBSD_UFS,		 0 },
230 	{ &gpt_uuid_dfbsd_vinum,	G_PART_ALIAS_DFBSD_VINUM,	 0 },
231 	{ &gpt_uuid_efi, 		G_PART_ALIAS_EFI,		 0xee },
232 	{ &gpt_uuid_freebsd,		G_PART_ALIAS_FREEBSD,		 0xa5 },
233 	{ &gpt_uuid_freebsd_boot, 	G_PART_ALIAS_FREEBSD_BOOT,	 0 },
234 	{ &gpt_uuid_freebsd_nandfs, 	G_PART_ALIAS_FREEBSD_NANDFS,	 0 },
235 	{ &gpt_uuid_freebsd_swap,	G_PART_ALIAS_FREEBSD_SWAP,	 0 },
236 	{ &gpt_uuid_freebsd_ufs,	G_PART_ALIAS_FREEBSD_UFS,	 0 },
237 	{ &gpt_uuid_freebsd_vinum,	G_PART_ALIAS_FREEBSD_VINUM,	 0 },
238 	{ &gpt_uuid_freebsd_zfs,	G_PART_ALIAS_FREEBSD_ZFS,	 0 },
239 	{ &gpt_uuid_linux_data,		G_PART_ALIAS_LINUX_DATA,	 0x0b },
240 	{ &gpt_uuid_linux_lvm,		G_PART_ALIAS_LINUX_LVM,		 0 },
241 	{ &gpt_uuid_linux_raid,		G_PART_ALIAS_LINUX_RAID,	 0 },
242 	{ &gpt_uuid_linux_swap,		G_PART_ALIAS_LINUX_SWAP,	 0 },
243 	{ &gpt_uuid_mbr,		G_PART_ALIAS_MBR,		 0 },
244 	{ &gpt_uuid_ms_basic_data,	G_PART_ALIAS_MS_BASIC_DATA,	 0x0b },
245 	{ &gpt_uuid_ms_ldm_data,	G_PART_ALIAS_MS_LDM_DATA,	 0 },
246 	{ &gpt_uuid_ms_ldm_metadata,	G_PART_ALIAS_MS_LDM_METADATA,	 0 },
247 	{ &gpt_uuid_ms_recovery,	G_PART_ALIAS_MS_RECOVERY,	 0 },
248 	{ &gpt_uuid_ms_reserved,	G_PART_ALIAS_MS_RESERVED,	 0 },
249 	{ &gpt_uuid_ms_spaces,		G_PART_ALIAS_MS_SPACES,		 0 },
250 	{ &gpt_uuid_netbsd_ccd,		G_PART_ALIAS_NETBSD_CCD,	 0 },
251 	{ &gpt_uuid_netbsd_cgd,		G_PART_ALIAS_NETBSD_CGD,	 0 },
252 	{ &gpt_uuid_netbsd_ffs,		G_PART_ALIAS_NETBSD_FFS,	 0 },
253 	{ &gpt_uuid_netbsd_lfs,		G_PART_ALIAS_NETBSD_LFS,	 0 },
254 	{ &gpt_uuid_netbsd_raid,	G_PART_ALIAS_NETBSD_RAID,	 0 },
255 	{ &gpt_uuid_netbsd_swap,	G_PART_ALIAS_NETBSD_SWAP,	 0 },
256 	{ &gpt_uuid_openbsd_data,	G_PART_ALIAS_OPENBSD_DATA,	 0 },
257 	{ &gpt_uuid_prep_boot,		G_PART_ALIAS_PREP_BOOT,		 0x41 },
258 	{ &gpt_uuid_vmfs,		G_PART_ALIAS_VMFS,		 0 },
259 	{ &gpt_uuid_vmkdiag,		G_PART_ALIAS_VMKDIAG,		 0 },
260 	{ &gpt_uuid_vmreserved,		G_PART_ALIAS_VMRESERVED,	 0 },
261 	{ &gpt_uuid_vmvsanhdr,		G_PART_ALIAS_VMVSANHDR,		 0 },
262 	{ NULL, 0, 0 }
263 };
264 
265 static int
266 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
267     quad_t end)
268 {
269 
270 	if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
271 		return (EINVAL);
272 
273 	mbr += DOSPARTOFF + idx * DOSPARTSIZE;
274 	mbr[0] = 0;
275 	if (start == 1) {
276 		/*
277 		 * Treat the PMBR partition specially to maximize
278 		 * interoperability with BIOSes.
279 		 */
280 		mbr[1] = mbr[3] = 0;
281 		mbr[2] = 2;
282 	} else
283 		mbr[1] = mbr[2] = mbr[3] = 0xff;
284 	mbr[4] = typ;
285 	mbr[5] = mbr[6] = mbr[7] = 0xff;
286 	le32enc(mbr + 8, (uint32_t)start);
287 	le32enc(mbr + 12, (uint32_t)(end - start + 1));
288 	return (0);
289 }
290 
291 static int
292 gpt_map_type(struct uuid *t)
293 {
294 	struct g_part_uuid_alias *uap;
295 
296 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
297 		if (EQUUID(t, uap->uuid))
298 			return (uap->mbrtype);
299 	}
300 	return (0);
301 }
302 
303 static void
304 gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
305 {
306 
307 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
308 	gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
309 	    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
310 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
311 }
312 
313 /*
314  * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
315  * whole disk anymore. Rather, it covers the GPT table and the EFI
316  * system partition only. This way the HFS+ partition and any FAT
317  * partitions can be added to the MBR without creating an overlap.
318  */
319 static int
320 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
321 {
322 	uint8_t *p;
323 
324 	p = table->mbr + DOSPARTOFF;
325 	if (p[4] != 0xee || le32dec(p + 8) != 1)
326 		return (0);
327 
328 	p += DOSPARTSIZE;
329 	if (p[4] != 0xaf)
330 		return (0);
331 
332 	printf("GEOM: %s: enabling Boot Camp\n", provname);
333 	return (1);
334 }
335 
336 static void
337 gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
338 {
339 	struct g_part_entry *baseentry;
340 	struct g_part_gpt_entry *entry;
341 	struct g_part_gpt_table *table;
342 	int bootable, error, index, slices, typ;
343 
344 	table = (struct g_part_gpt_table *)basetable;
345 
346 	bootable = -1;
347 	for (index = 0; index < NDOSPART; index++) {
348 		if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
349 			bootable = index;
350 	}
351 
352 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
353 	slices = 0;
354 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
355 		if (baseentry->gpe_deleted)
356 			continue;
357 		index = baseentry->gpe_index - 1;
358 		if (index >= NDOSPART)
359 			continue;
360 
361 		entry = (struct g_part_gpt_entry *)baseentry;
362 
363 		switch (index) {
364 		case 0:	/* This must be the EFI system partition. */
365 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
366 				goto disable;
367 			error = gpt_write_mbr_entry(table->mbr, index, 0xee,
368 			    1ull, entry->ent.ent_lba_end);
369 			break;
370 		case 1:	/* This must be the HFS+ partition. */
371 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
372 				goto disable;
373 			error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
374 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
375 			break;
376 		default:
377 			typ = gpt_map_type(&entry->ent.ent_type);
378 			error = gpt_write_mbr_entry(table->mbr, index, typ,
379 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
380 			break;
381 		}
382 		if (error)
383 			continue;
384 
385 		if (index == bootable)
386 			table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
387 		slices |= 1 << index;
388 	}
389 	if ((slices & 3) == 3)
390 		return;
391 
392  disable:
393 	table->bootcamp = 0;
394 	gpt_create_pmbr(table, pp);
395 }
396 
397 static struct gpt_hdr *
398 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
399     enum gpt_elt elt)
400 {
401 	struct gpt_hdr *buf, *hdr;
402 	struct g_provider *pp;
403 	quad_t lba, last;
404 	int error;
405 	uint32_t crc, sz;
406 
407 	pp = cp->provider;
408 	last = (pp->mediasize / pp->sectorsize) - 1;
409 	table->state[elt] = GPT_STATE_MISSING;
410 	/*
411 	 * If the primary header is valid look for secondary
412 	 * header in AlternateLBA, otherwise in the last medium's LBA.
413 	 */
414 	if (elt == GPT_ELT_SECHDR) {
415 		if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
416 			table->lba[elt] = last;
417 	} else
418 		table->lba[elt] = 1;
419 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
420 	    &error);
421 	if (buf == NULL)
422 		return (NULL);
423 	hdr = NULL;
424 	if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
425 		goto fail;
426 
427 	table->state[elt] = GPT_STATE_CORRUPT;
428 	sz = le32toh(buf->hdr_size);
429 	if (sz < 92 || sz > pp->sectorsize)
430 		goto fail;
431 
432 	hdr = g_malloc(sz, M_WAITOK | M_ZERO);
433 	bcopy(buf, hdr, sz);
434 	hdr->hdr_size = sz;
435 
436 	crc = le32toh(buf->hdr_crc_self);
437 	buf->hdr_crc_self = 0;
438 	if (crc32(buf, sz) != crc)
439 		goto fail;
440 	hdr->hdr_crc_self = crc;
441 
442 	table->state[elt] = GPT_STATE_INVALID;
443 	hdr->hdr_revision = le32toh(buf->hdr_revision);
444 	if (hdr->hdr_revision < GPT_HDR_REVISION)
445 		goto fail;
446 	hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
447 	if (hdr->hdr_lba_self != table->lba[elt])
448 		goto fail;
449 	hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
450 	if (hdr->hdr_lba_alt == hdr->hdr_lba_self ||
451 	    hdr->hdr_lba_alt > last)
452 		goto fail;
453 
454 	/* Check the managed area. */
455 	hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
456 	if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
457 		goto fail;
458 	hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
459 	if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
460 		goto fail;
461 
462 	/* Check the table location and size of the table. */
463 	hdr->hdr_entries = le32toh(buf->hdr_entries);
464 	hdr->hdr_entsz = le32toh(buf->hdr_entsz);
465 	if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
466 	    (hdr->hdr_entsz & 7) != 0)
467 		goto fail;
468 	hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
469 	if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
470 		goto fail;
471 	if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
472 	    hdr->hdr_lba_table <= hdr->hdr_lba_end)
473 		goto fail;
474 	lba = hdr->hdr_lba_table +
475 	    (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
476 	    pp->sectorsize - 1;
477 	if (lba >= last)
478 		goto fail;
479 	if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
480 		goto fail;
481 
482 	table->state[elt] = GPT_STATE_OK;
483 	le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
484 	hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
485 
486 	/* save LBA for secondary header */
487 	if (elt == GPT_ELT_PRIHDR)
488 		table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
489 
490 	g_free(buf);
491 	return (hdr);
492 
493  fail:
494 	if (hdr != NULL)
495 		g_free(hdr);
496 	g_free(buf);
497 	return (NULL);
498 }
499 
500 static struct gpt_ent *
501 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
502     enum gpt_elt elt, struct gpt_hdr *hdr)
503 {
504 	struct g_provider *pp;
505 	struct gpt_ent *ent, *tbl;
506 	char *buf, *p;
507 	unsigned int idx, sectors, tblsz, size;
508 	int error;
509 
510 	if (hdr == NULL)
511 		return (NULL);
512 
513 	pp = cp->provider;
514 	table->lba[elt] = hdr->hdr_lba_table;
515 
516 	table->state[elt] = GPT_STATE_MISSING;
517 	tblsz = hdr->hdr_entries * hdr->hdr_entsz;
518 	sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize;
519 	buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
520 	for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) {
521 		size = (sectors - idx > MAXPHYS / pp->sectorsize) ?  MAXPHYS:
522 		    (sectors - idx) * pp->sectorsize;
523 		p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
524 		    size, &error);
525 		if (p == NULL) {
526 			g_free(buf);
527 			return (NULL);
528 		}
529 		bcopy(p, buf + idx * pp->sectorsize, size);
530 		g_free(p);
531 	}
532 	table->state[elt] = GPT_STATE_CORRUPT;
533 	if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
534 		g_free(buf);
535 		return (NULL);
536 	}
537 
538 	table->state[elt] = GPT_STATE_OK;
539 	tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
540 	    M_WAITOK | M_ZERO);
541 
542 	for (idx = 0, ent = tbl, p = buf;
543 	     idx < hdr->hdr_entries;
544 	     idx++, ent++, p += hdr->hdr_entsz) {
545 		le_uuid_dec(p, &ent->ent_type);
546 		le_uuid_dec(p + 16, &ent->ent_uuid);
547 		ent->ent_lba_start = le64dec(p + 32);
548 		ent->ent_lba_end = le64dec(p + 40);
549 		ent->ent_attr = le64dec(p + 48);
550 		/* Keep UTF-16 in little-endian. */
551 		bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
552 	}
553 
554 	g_free(buf);
555 	return (tbl);
556 }
557 
558 static int
559 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
560 {
561 
562 	if (pri == NULL || sec == NULL)
563 		return (0);
564 
565 	if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
566 		return (0);
567 	return ((pri->hdr_revision == sec->hdr_revision &&
568 	    pri->hdr_size == sec->hdr_size &&
569 	    pri->hdr_lba_start == sec->hdr_lba_start &&
570 	    pri->hdr_lba_end == sec->hdr_lba_end &&
571 	    pri->hdr_entries == sec->hdr_entries &&
572 	    pri->hdr_entsz == sec->hdr_entsz &&
573 	    pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
574 }
575 
576 static int
577 gpt_parse_type(const char *type, struct uuid *uuid)
578 {
579 	struct uuid tmp;
580 	const char *alias;
581 	int error;
582 	struct g_part_uuid_alias *uap;
583 
584 	if (type[0] == '!') {
585 		error = parse_uuid(type + 1, &tmp);
586 		if (error)
587 			return (error);
588 		if (EQUUID(&tmp, &gpt_uuid_unused))
589 			return (EINVAL);
590 		*uuid = tmp;
591 		return (0);
592 	}
593 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
594 		alias = g_part_alias_name(uap->alias);
595 		if (!strcasecmp(type, alias)) {
596 			*uuid = *uap->uuid;
597 			return (0);
598 		}
599 	}
600 	return (EINVAL);
601 }
602 
603 static int
604 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
605     struct g_part_parms *gpp)
606 {
607 	struct g_part_gpt_entry *entry;
608 	int error;
609 
610 	entry = (struct g_part_gpt_entry *)baseentry;
611 	error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
612 	if (error)
613 		return (error);
614 	kern_uuidgen(&entry->ent.ent_uuid, 1);
615 	entry->ent.ent_lba_start = baseentry->gpe_start;
616 	entry->ent.ent_lba_end = baseentry->gpe_end;
617 	if (baseentry->gpe_deleted) {
618 		entry->ent.ent_attr = 0;
619 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
620 	}
621 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
622 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
623 		    sizeof(entry->ent.ent_name) /
624 		    sizeof(entry->ent.ent_name[0]));
625 	return (0);
626 }
627 
628 static int
629 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
630 {
631 	struct g_part_gpt_table *table;
632 	size_t codesz;
633 
634 	codesz = DOSPARTOFF;
635 	table = (struct g_part_gpt_table *)basetable;
636 	bzero(table->mbr, codesz);
637 	codesz = MIN(codesz, gpp->gpp_codesize);
638 	if (codesz > 0)
639 		bcopy(gpp->gpp_codeptr, table->mbr, codesz);
640 	return (0);
641 }
642 
643 static int
644 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
645 {
646 	struct g_provider *pp;
647 	struct g_part_gpt_table *table;
648 	size_t tblsz;
649 
650 	/* We don't nest, which means that our depth should be 0. */
651 	if (basetable->gpt_depth != 0)
652 		return (ENXIO);
653 
654 	table = (struct g_part_gpt_table *)basetable;
655 	pp = gpp->gpp_provider;
656 	tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
657 	    pp->sectorsize - 1) / pp->sectorsize;
658 	if (pp->sectorsize < MBRSIZE ||
659 	    pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
660 	    pp->sectorsize)
661 		return (ENOSPC);
662 
663 	gpt_create_pmbr(table, pp);
664 
665 	/* Allocate space for the header */
666 	table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
667 
668 	bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
669 	table->hdr->hdr_revision = GPT_HDR_REVISION;
670 	table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
671 	kern_uuidgen(&table->hdr->hdr_uuid, 1);
672 	table->hdr->hdr_entries = basetable->gpt_entries;
673 	table->hdr->hdr_entsz = sizeof(struct gpt_ent);
674 
675 	g_gpt_set_defaults(basetable, pp);
676 	return (0);
677 }
678 
679 static int
680 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
681 {
682 	struct g_part_gpt_table *table;
683 	struct g_provider *pp;
684 
685 	table = (struct g_part_gpt_table *)basetable;
686 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
687 	g_free(table->hdr);
688 	table->hdr = NULL;
689 
690 	/*
691 	 * Wipe the first 2 sectors to clear the partitioning. Wipe the last
692 	 * sector only if it has valid secondary header.
693 	 */
694 	basetable->gpt_smhead |= 3;
695 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
696 	    table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
697 		basetable->gpt_smtail |= 1;
698 	return (0);
699 }
700 
701 static void
702 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
703     struct sbuf *sb, const char *indent)
704 {
705 	struct g_part_gpt_entry *entry;
706 
707 	entry = (struct g_part_gpt_entry *)baseentry;
708 	if (indent == NULL) {
709 		/* conftxt: libdisk compatibility */
710 		sbuf_printf(sb, " xs GPT xt ");
711 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
712 	} else if (entry != NULL) {
713 		/* confxml: partition entry information */
714 		sbuf_printf(sb, "%s<label>", indent);
715 		g_gpt_printf_utf16(sb, entry->ent.ent_name,
716 		    sizeof(entry->ent.ent_name) >> 1);
717 		sbuf_printf(sb, "</label>\n");
718 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
719 			sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
720 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
721 			sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
722 			    indent);
723 		}
724 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
725 			sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
726 			    indent);
727 		}
728 		sbuf_printf(sb, "%s<rawtype>", indent);
729 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
730 		sbuf_printf(sb, "</rawtype>\n");
731 		sbuf_printf(sb, "%s<rawuuid>", indent);
732 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
733 		sbuf_printf(sb, "</rawuuid>\n");
734 	} else {
735 		/* confxml: scheme information */
736 	}
737 }
738 
739 static int
740 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
741 {
742 	struct g_part_gpt_entry *entry;
743 
744 	entry = (struct g_part_gpt_entry *)baseentry;
745 	return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
746 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
747 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
748 }
749 
750 static int
751 g_part_gpt_modify(struct g_part_table *basetable,
752     struct g_part_entry *baseentry, struct g_part_parms *gpp)
753 {
754 	struct g_part_gpt_entry *entry;
755 	int error;
756 
757 	entry = (struct g_part_gpt_entry *)baseentry;
758 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
759 		error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
760 		if (error)
761 			return (error);
762 	}
763 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
764 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
765 		    sizeof(entry->ent.ent_name) /
766 		    sizeof(entry->ent.ent_name[0]));
767 	return (0);
768 }
769 
770 static int
771 g_part_gpt_resize(struct g_part_table *basetable,
772     struct g_part_entry *baseentry, struct g_part_parms *gpp)
773 {
774 	struct g_part_gpt_entry *entry;
775 
776 	if (baseentry == NULL)
777 		return (g_part_gpt_recover(basetable));
778 
779 	entry = (struct g_part_gpt_entry *)baseentry;
780 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
781 	entry->ent.ent_lba_end = baseentry->gpe_end;
782 
783 	return (0);
784 }
785 
786 static const char *
787 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
788     char *buf, size_t bufsz)
789 {
790 	struct g_part_gpt_entry *entry;
791 	char c;
792 
793 	entry = (struct g_part_gpt_entry *)baseentry;
794 	c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
795 	snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
796 	return (buf);
797 }
798 
799 static int
800 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
801 {
802 	struct g_provider *pp;
803 	u_char *buf;
804 	int error, index, pri, res;
805 
806 	/* We don't nest, which means that our depth should be 0. */
807 	if (table->gpt_depth != 0)
808 		return (ENXIO);
809 
810 	pp = cp->provider;
811 
812 	/*
813 	 * Sanity-check the provider. Since the first sector on the provider
814 	 * must be a PMBR and a PMBR is 512 bytes large, the sector size
815 	 * must be at least 512 bytes.  Also, since the theoretical minimum
816 	 * number of sectors needed by GPT is 6, any medium that has less
817 	 * than 6 sectors is never going to be able to hold a GPT. The
818 	 * number 6 comes from:
819 	 *	1 sector for the PMBR
820 	 *	2 sectors for the GPT headers (each 1 sector)
821 	 *	2 sectors for the GPT tables (each 1 sector)
822 	 *	1 sector for an actual partition
823 	 * It's better to catch this pathological case early than behaving
824 	 * pathologically later on...
825 	 */
826 	if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
827 		return (ENOSPC);
828 
829 	/*
830 	 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
831 	 * as the highest priority on a match, otherwise we assume some
832 	 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
833 	 * we really want the MBR scheme to take precedence.
834 	 */
835 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
836 	if (buf == NULL)
837 		return (error);
838 	res = le16dec(buf + DOSMAGICOFFSET);
839 	pri = G_PART_PROBE_PRI_LOW;
840 	if (res == DOSMAGIC) {
841 		for (index = 0; index < NDOSPART; index++) {
842 			if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
843 				pri = G_PART_PROBE_PRI_HIGH;
844 		}
845 		g_free(buf);
846 
847 		/* Check that there's a primary header. */
848 		buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
849 		if (buf == NULL)
850 			return (error);
851 		res = memcmp(buf, GPT_HDR_SIG, 8);
852 		g_free(buf);
853 		if (res == 0)
854 			return (pri);
855 	} else
856 		g_free(buf);
857 
858 	/* No primary? Check that there's a secondary. */
859 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
860 	    &error);
861 	if (buf == NULL)
862 		return (error);
863 	res = memcmp(buf, GPT_HDR_SIG, 8);
864 	g_free(buf);
865 	return ((res == 0) ? pri : ENXIO);
866 }
867 
868 static int
869 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
870 {
871 	struct gpt_hdr *prihdr, *sechdr;
872 	struct gpt_ent *tbl, *pritbl, *sectbl;
873 	struct g_provider *pp;
874 	struct g_part_gpt_table *table;
875 	struct g_part_gpt_entry *entry;
876 	u_char *buf;
877 	uint64_t last;
878 	int error, index;
879 
880 	table = (struct g_part_gpt_table *)basetable;
881 	pp = cp->provider;
882 	last = (pp->mediasize / pp->sectorsize) - 1;
883 
884 	/* Read the PMBR */
885 	buf = g_read_data(cp, 0, pp->sectorsize, &error);
886 	if (buf == NULL)
887 		return (error);
888 	bcopy(buf, table->mbr, MBRSIZE);
889 	g_free(buf);
890 
891 	/* Read the primary header and table. */
892 	prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
893 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
894 		pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
895 	} else {
896 		table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
897 		pritbl = NULL;
898 	}
899 
900 	/* Read the secondary header and table. */
901 	sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
902 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
903 		sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
904 	} else {
905 		table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
906 		sectbl = NULL;
907 	}
908 
909 	/* Fail if we haven't got any good tables at all. */
910 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
911 	    table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
912 		printf("GEOM: %s: corrupt or invalid GPT detected.\n",
913 		    pp->name);
914 		printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
915 		    pp->name);
916 		return (EINVAL);
917 	}
918 
919 	/*
920 	 * If both headers are good but they disagree with each other,
921 	 * then invalidate one. We prefer to keep the primary header,
922 	 * unless the primary table is corrupt.
923 	 */
924 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
925 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
926 	    !gpt_matched_hdrs(prihdr, sechdr)) {
927 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
928 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
929 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
930 			g_free(sechdr);
931 			sechdr = NULL;
932 		} else {
933 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
934 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
935 			g_free(prihdr);
936 			prihdr = NULL;
937 		}
938 	}
939 
940 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
941 		printf("GEOM: %s: the primary GPT table is corrupt or "
942 		    "invalid.\n", pp->name);
943 		printf("GEOM: %s: using the secondary instead -- recovery "
944 		    "strongly advised.\n", pp->name);
945 		table->hdr = sechdr;
946 		basetable->gpt_corrupt = 1;
947 		if (prihdr != NULL)
948 			g_free(prihdr);
949 		tbl = sectbl;
950 		if (pritbl != NULL)
951 			g_free(pritbl);
952 	} else {
953 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
954 			printf("GEOM: %s: the secondary GPT table is corrupt "
955 			    "or invalid.\n", pp->name);
956 			printf("GEOM: %s: using the primary only -- recovery "
957 			    "suggested.\n", pp->name);
958 			basetable->gpt_corrupt = 1;
959 		} else if (table->lba[GPT_ELT_SECHDR] != last) {
960 			printf( "GEOM: %s: the secondary GPT header is not in "
961 			    "the last LBA.\n", pp->name);
962 			basetable->gpt_corrupt = 1;
963 		}
964 		table->hdr = prihdr;
965 		if (sechdr != NULL)
966 			g_free(sechdr);
967 		tbl = pritbl;
968 		if (sectbl != NULL)
969 			g_free(sectbl);
970 	}
971 
972 	basetable->gpt_first = table->hdr->hdr_lba_start;
973 	basetable->gpt_last = table->hdr->hdr_lba_end;
974 	basetable->gpt_entries = (table->hdr->hdr_lba_start - 2) *
975 	    pp->sectorsize / table->hdr->hdr_entsz;
976 
977 	for (index = table->hdr->hdr_entries - 1; index >= 0; index--) {
978 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
979 			continue;
980 		entry = (struct g_part_gpt_entry *)g_part_new_entry(
981 		    basetable, index + 1, tbl[index].ent_lba_start,
982 		    tbl[index].ent_lba_end);
983 		entry->ent = tbl[index];
984 	}
985 
986 	g_free(tbl);
987 
988 	/*
989 	 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
990 	 * if (and only if) any FAT32 or FAT16 partitions have been
991 	 * created. This happens irrespective of whether Boot Camp is
992 	 * used/enabled, though it's generally understood to be done
993 	 * to support legacy Windows under Boot Camp. We refer to this
994 	 * mirroring simply as Boot Camp. We try to detect Boot Camp
995 	 * so that we can update the MBR if and when GPT changes have
996 	 * been made. Note that we do not enable Boot Camp if not
997 	 * previously enabled because we can't assume that we're on a
998 	 * Mac alongside Mac OS X.
999 	 */
1000 	table->bootcamp = gpt_is_bootcamp(table, pp->name);
1001 
1002 	return (0);
1003 }
1004 
1005 static int
1006 g_part_gpt_recover(struct g_part_table *basetable)
1007 {
1008 	struct g_part_gpt_table *table;
1009 	struct g_provider *pp;
1010 
1011 	table = (struct g_part_gpt_table *)basetable;
1012 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1013 	gpt_create_pmbr(table, pp);
1014 	g_gpt_set_defaults(basetable, pp);
1015 	basetable->gpt_corrupt = 0;
1016 	return (0);
1017 }
1018 
1019 static int
1020 g_part_gpt_setunset(struct g_part_table *basetable,
1021     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1022 {
1023 	struct g_part_gpt_entry *entry;
1024 	struct g_part_gpt_table *table;
1025 	struct g_provider *pp;
1026 	uint8_t *p;
1027 	uint64_t attr;
1028 	int i;
1029 
1030 	table = (struct g_part_gpt_table *)basetable;
1031 	entry = (struct g_part_gpt_entry *)baseentry;
1032 
1033 	if (strcasecmp(attrib, "active") == 0) {
1034 		if (table->bootcamp) {
1035 			/* The active flag must be set on a valid entry. */
1036 			if (entry == NULL)
1037 				return (ENXIO);
1038 			if (baseentry->gpe_index > NDOSPART)
1039 				return (EINVAL);
1040 			for (i = 0; i < NDOSPART; i++) {
1041 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1042 				p[0] = (i == baseentry->gpe_index - 1)
1043 				    ? ((set) ? 0x80 : 0) : 0;
1044 			}
1045 		} else {
1046 			/* The PMBR is marked as active without an entry. */
1047 			if (entry != NULL)
1048 				return (ENXIO);
1049 			for (i = 0; i < NDOSPART; i++) {
1050 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1051 				p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
1052 			}
1053 		}
1054 		return (0);
1055 	} else if (strcasecmp(attrib, "lenovofix") == 0) {
1056 		/*
1057 		 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1058 		 * This workaround allows Lenovo X220, T420, T520, etc to boot
1059 		 * from GPT Partitions in BIOS mode.
1060 		 */
1061 
1062 		if (entry != NULL)
1063 			return (ENXIO);
1064 
1065 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1066 		bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1067 		gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1068 		    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1069 		return (0);
1070 	}
1071 
1072 	if (entry == NULL)
1073 		return (ENODEV);
1074 
1075 	attr = 0;
1076 	if (strcasecmp(attrib, "bootme") == 0) {
1077 		attr |= GPT_ENT_ATTR_BOOTME;
1078 	} else if (strcasecmp(attrib, "bootonce") == 0) {
1079 		attr |= GPT_ENT_ATTR_BOOTONCE;
1080 		if (set)
1081 			attr |= GPT_ENT_ATTR_BOOTME;
1082 	} else if (strcasecmp(attrib, "bootfailed") == 0) {
1083 		/*
1084 		 * It should only be possible to unset BOOTFAILED, but it might
1085 		 * be useful for test purposes to also be able to set it.
1086 		 */
1087 		attr |= GPT_ENT_ATTR_BOOTFAILED;
1088 	}
1089 	if (attr == 0)
1090 		return (EINVAL);
1091 
1092 	if (set)
1093 		attr = entry->ent.ent_attr | attr;
1094 	else
1095 		attr = entry->ent.ent_attr & ~attr;
1096 	if (attr != entry->ent.ent_attr) {
1097 		entry->ent.ent_attr = attr;
1098 		if (!baseentry->gpe_created)
1099 			baseentry->gpe_modified = 1;
1100 	}
1101 	return (0);
1102 }
1103 
1104 static const char *
1105 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1106     char *buf, size_t bufsz)
1107 {
1108 	struct g_part_gpt_entry *entry;
1109 	struct uuid *type;
1110 	struct g_part_uuid_alias *uap;
1111 
1112 	entry = (struct g_part_gpt_entry *)baseentry;
1113 	type = &entry->ent.ent_type;
1114 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1115 		if (EQUUID(type, uap->uuid))
1116 			return (g_part_alias_name(uap->alias));
1117 	buf[0] = '!';
1118 	snprintf_uuid(buf + 1, bufsz - 1, type);
1119 
1120 	return (buf);
1121 }
1122 
1123 static int
1124 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1125 {
1126 	unsigned char *buf, *bp;
1127 	struct g_provider *pp;
1128 	struct g_part_entry *baseentry;
1129 	struct g_part_gpt_entry *entry;
1130 	struct g_part_gpt_table *table;
1131 	size_t tblsz;
1132 	uint32_t crc;
1133 	int error, index;
1134 
1135 	pp = cp->provider;
1136 	table = (struct g_part_gpt_table *)basetable;
1137 	tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
1138 	    pp->sectorsize - 1) / pp->sectorsize;
1139 
1140 	/* Reconstruct the MBR from the GPT if under Boot Camp. */
1141 	if (table->bootcamp)
1142 		gpt_update_bootcamp(basetable, pp);
1143 
1144 	/* Write the PMBR */
1145 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1146 	bcopy(table->mbr, buf, MBRSIZE);
1147 	error = g_write_data(cp, 0, buf, pp->sectorsize);
1148 	g_free(buf);
1149 	if (error)
1150 		return (error);
1151 
1152 	/* Allocate space for the header and entries. */
1153 	buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1154 
1155 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
1156 	le32enc(buf + 8, table->hdr->hdr_revision);
1157 	le32enc(buf + 12, table->hdr->hdr_size);
1158 	le64enc(buf + 40, table->hdr->hdr_lba_start);
1159 	le64enc(buf + 48, table->hdr->hdr_lba_end);
1160 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
1161 	le32enc(buf + 80, table->hdr->hdr_entries);
1162 	le32enc(buf + 84, table->hdr->hdr_entsz);
1163 
1164 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1165 		if (baseentry->gpe_deleted)
1166 			continue;
1167 		entry = (struct g_part_gpt_entry *)baseentry;
1168 		index = baseentry->gpe_index - 1;
1169 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
1170 		le_uuid_enc(bp, &entry->ent.ent_type);
1171 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
1172 		le64enc(bp + 32, entry->ent.ent_lba_start);
1173 		le64enc(bp + 40, entry->ent.ent_lba_end);
1174 		le64enc(bp + 48, entry->ent.ent_attr);
1175 		memcpy(bp + 56, entry->ent.ent_name,
1176 		    sizeof(entry->ent.ent_name));
1177 	}
1178 
1179 	crc = crc32(buf + pp->sectorsize,
1180 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
1181 	le32enc(buf + 88, crc);
1182 
1183 	/* Write primary meta-data. */
1184 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
1185 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
1186 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
1187 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
1188 	crc = crc32(buf, table->hdr->hdr_size);
1189 	le32enc(buf + 16, crc);
1190 
1191 	for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
1192 		error = g_write_data(cp,
1193 		    (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1194 		    buf + (index + 1) * pp->sectorsize,
1195 		    (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
1196 		    (tblsz - index) * pp->sectorsize);
1197 		if (error)
1198 			goto out;
1199 	}
1200 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1201 	    buf, pp->sectorsize);
1202 	if (error)
1203 		goto out;
1204 
1205 	/* Write secondary meta-data. */
1206 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
1207 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
1208 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
1209 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
1210 	crc = crc32(buf, table->hdr->hdr_size);
1211 	le32enc(buf + 16, crc);
1212 
1213 	for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
1214 		error = g_write_data(cp,
1215 		    (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1216 		    buf + (index + 1) * pp->sectorsize,
1217 		    (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
1218 		    (tblsz - index) * pp->sectorsize);
1219 		if (error)
1220 			goto out;
1221 	}
1222 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1223 	    buf, pp->sectorsize);
1224 
1225  out:
1226 	g_free(buf);
1227 	return (error);
1228 }
1229 
1230 static void
1231 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
1232 {
1233 	struct g_part_entry *baseentry;
1234 	struct g_part_gpt_entry *entry;
1235 	struct g_part_gpt_table *table;
1236 	quad_t start, end, min, max;
1237 	quad_t lba, last;
1238 	size_t spb, tblsz;
1239 
1240 	table = (struct g_part_gpt_table *)basetable;
1241 	last = pp->mediasize / pp->sectorsize - 1;
1242 	tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
1243 	    pp->sectorsize - 1) / pp->sectorsize;
1244 
1245 	table->lba[GPT_ELT_PRIHDR] = 1;
1246 	table->lba[GPT_ELT_PRITBL] = 2;
1247 	table->lba[GPT_ELT_SECHDR] = last;
1248 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
1249 	table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
1250 	table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
1251 	table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
1252 	table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
1253 
1254 	max = start = 2 + tblsz;
1255 	min = end = last - tblsz - 1;
1256 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1257 		if (baseentry->gpe_deleted)
1258 			continue;
1259 		entry = (struct g_part_gpt_entry *)baseentry;
1260 		if (entry->ent.ent_lba_start < min)
1261 			min = entry->ent.ent_lba_start;
1262 		if (entry->ent.ent_lba_end > max)
1263 			max = entry->ent.ent_lba_end;
1264 	}
1265 	spb = 4096 / pp->sectorsize;
1266 	if (spb > 1) {
1267 		lba = start + ((start % spb) ? spb - start % spb : 0);
1268 		if (lba <= min)
1269 			start = lba;
1270 		lba = end - (end + 1) % spb;
1271 		if (max <= lba)
1272 			end = lba;
1273 	}
1274 	table->hdr->hdr_lba_start = start;
1275 	table->hdr->hdr_lba_end = end;
1276 
1277 	basetable->gpt_first = start;
1278 	basetable->gpt_last = end;
1279 }
1280 
1281 static void
1282 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1283 {
1284 	u_int bo;
1285 	uint32_t ch;
1286 	uint16_t c;
1287 
1288 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
1289 	while (len > 0 && *str != 0) {
1290 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1291 		str++, len--;
1292 		if ((ch & 0xf800) == 0xd800) {
1293 			if (len > 0) {
1294 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
1295 				    : le16toh(*str);
1296 				str++, len--;
1297 			} else
1298 				c = 0xfffd;
1299 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1300 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1301 				ch += 0x10000;
1302 			} else
1303 				ch = 0xfffd;
1304 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1305 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1306 			continue;
1307 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1308 			continue;
1309 
1310 		/* Write the Unicode character in UTF-8 */
1311 		if (ch < 0x80)
1312 			g_conf_printf_escaped(sb, "%c", ch);
1313 		else if (ch < 0x800)
1314 			g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
1315 			    0x80 | (ch & 0x3f));
1316 		else if (ch < 0x10000)
1317 			g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
1318 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1319 		else if (ch < 0x200000)
1320 			g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
1321 			    (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
1322 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1323 	}
1324 }
1325 
1326 static void
1327 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1328 {
1329 	size_t s16idx, s8idx;
1330 	uint32_t utfchar;
1331 	unsigned int c, utfbytes;
1332 
1333 	s8idx = s16idx = 0;
1334 	utfchar = 0;
1335 	utfbytes = 0;
1336 	bzero(s16, s16len << 1);
1337 	while (s8[s8idx] != 0 && s16idx < s16len) {
1338 		c = s8[s8idx++];
1339 		if ((c & 0xc0) != 0x80) {
1340 			/* Initial characters. */
1341 			if (utfbytes != 0) {
1342 				/* Incomplete encoding of previous char. */
1343 				s16[s16idx++] = htole16(0xfffd);
1344 			}
1345 			if ((c & 0xf8) == 0xf0) {
1346 				utfchar = c & 0x07;
1347 				utfbytes = 3;
1348 			} else if ((c & 0xf0) == 0xe0) {
1349 				utfchar = c & 0x0f;
1350 				utfbytes = 2;
1351 			} else if ((c & 0xe0) == 0xc0) {
1352 				utfchar = c & 0x1f;
1353 				utfbytes = 1;
1354 			} else {
1355 				utfchar = c & 0x7f;
1356 				utfbytes = 0;
1357 			}
1358 		} else {
1359 			/* Followup characters. */
1360 			if (utfbytes > 0) {
1361 				utfchar = (utfchar << 6) + (c & 0x3f);
1362 				utfbytes--;
1363 			} else if (utfbytes == 0)
1364 				utfbytes = ~0;
1365 		}
1366 		/*
1367 		 * Write the complete Unicode character as UTF-16 when we
1368 		 * have all the UTF-8 charactars collected.
1369 		 */
1370 		if (utfbytes == 0) {
1371 			/*
1372 			 * If we need to write 2 UTF-16 characters, but
1373 			 * we only have room for 1, then we truncate the
1374 			 * string by writing a 0 instead.
1375 			 */
1376 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1377 				s16[s16idx++] =
1378 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
1379 				s16[s16idx++] =
1380 				    htole16(0xdc00 | (utfchar & 0x3ff));
1381 			} else
1382 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1383 				    htole16(utfchar);
1384 		}
1385 	}
1386 	/*
1387 	 * If our input string was truncated, append an invalid encoding
1388 	 * character to the output string.
1389 	 */
1390 	if (utfbytes != 0 && s16idx < s16len)
1391 		s16[s16idx++] = htole16(0xfffd);
1392 }
1393