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