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