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