xref: /freebsd/sys/geom/part/g_part_gpt.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
739     struct sbuf *sb, const char *indent)
740 {
741 	struct g_part_gpt_entry *entry;
742 
743 	entry = (struct g_part_gpt_entry *)baseentry;
744 	if (indent == NULL) {
745 		/* conftxt: libdisk compatibility */
746 		sbuf_cat(sb, " xs GPT xt ");
747 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
748 	} else if (entry != NULL) {
749 		/* confxml: partition entry information */
750 		sbuf_printf(sb, "%s<label>", indent);
751 		g_gpt_printf_utf16(sb, entry->ent.ent_name,
752 		    sizeof(entry->ent.ent_name) >> 1);
753 		sbuf_cat(sb, "</label>\n");
754 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
755 			sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
756 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
757 			sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
758 			    indent);
759 		}
760 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
761 			sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
762 			    indent);
763 		}
764 		sbuf_printf(sb, "%s<rawtype>", indent);
765 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
766 		sbuf_cat(sb, "</rawtype>\n");
767 		sbuf_printf(sb, "%s<rawuuid>", indent);
768 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
769 		sbuf_cat(sb, "</rawuuid>\n");
770 		sbuf_printf(sb, "%s<efimedia>", indent);
771 		sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index);
772 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
773 		sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start,
774 		    (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
775 		sbuf_cat(sb, "</efimedia>\n");
776 	} else {
777 		/* confxml: scheme information */
778 	}
779 }
780 
781 static int
782 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
783 {
784 	struct g_part_gpt_entry *entry;
785 
786 	entry = (struct g_part_gpt_entry *)baseentry;
787 	return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
788 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
789 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
790 }
791 
792 static int
793 g_part_gpt_modify(struct g_part_table *basetable,
794     struct g_part_entry *baseentry, struct g_part_parms *gpp)
795 {
796 	struct g_part_gpt_entry *entry;
797 	int error;
798 
799 	entry = (struct g_part_gpt_entry *)baseentry;
800 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
801 		error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
802 		if (error)
803 			return (error);
804 	}
805 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
806 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
807 		    sizeof(entry->ent.ent_name) /
808 		    sizeof(entry->ent.ent_name[0]));
809 	return (0);
810 }
811 
812 static int
813 g_part_gpt_resize(struct g_part_table *basetable,
814     struct g_part_entry *baseentry, struct g_part_parms *gpp)
815 {
816 	struct g_part_gpt_entry *entry;
817 
818 	if (baseentry == NULL)
819 		return (g_part_gpt_recover(basetable));
820 
821 	entry = (struct g_part_gpt_entry *)baseentry;
822 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
823 	entry->ent.ent_lba_end = baseentry->gpe_end;
824 
825 	return (0);
826 }
827 
828 static const char *
829 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
830     char *buf, size_t bufsz)
831 {
832 	struct g_part_gpt_entry *entry;
833 	char c;
834 
835 	entry = (struct g_part_gpt_entry *)baseentry;
836 	c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
837 	snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
838 	return (buf);
839 }
840 
841 static int
842 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
843 {
844 	struct g_provider *pp;
845 	u_char *buf;
846 	int error, index, pri, res;
847 
848 	/* Our depth should be 0 unless nesting was explicitly enabled. */
849 	if (!allow_nesting && table->gpt_depth != 0)
850 		return (ENXIO);
851 
852 	pp = cp->provider;
853 
854 	/*
855 	 * Sanity-check the provider. Since the first sector on the provider
856 	 * must be a PMBR and a PMBR is 512 bytes large, the sector size
857 	 * must be at least 512 bytes.  Also, since the theoretical minimum
858 	 * number of sectors needed by GPT is 6, any medium that has less
859 	 * than 6 sectors is never going to be able to hold a GPT. The
860 	 * number 6 comes from:
861 	 *	1 sector for the PMBR
862 	 *	2 sectors for the GPT headers (each 1 sector)
863 	 *	2 sectors for the GPT tables (each 1 sector)
864 	 *	1 sector for an actual partition
865 	 * It's better to catch this pathological case early than behaving
866 	 * pathologically later on...
867 	 */
868 	if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
869 		return (ENOSPC);
870 
871 	/*
872 	 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
873 	 * as the highest priority on a match, otherwise we assume some
874 	 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
875 	 * we really want the MBR scheme to take precedence.
876 	 */
877 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
878 	if (buf == NULL)
879 		return (error);
880 	res = le16dec(buf + DOSMAGICOFFSET);
881 	pri = G_PART_PROBE_PRI_LOW;
882 	if (res == DOSMAGIC) {
883 		for (index = 0; index < NDOSPART; index++) {
884 			if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
885 				pri = G_PART_PROBE_PRI_HIGH;
886 		}
887 		g_free(buf);
888 
889 		/* Check that there's a primary header. */
890 		buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
891 		if (buf == NULL)
892 			return (error);
893 		res = memcmp(buf, GPT_HDR_SIG, 8);
894 		g_free(buf);
895 		if (res == 0)
896 			return (pri);
897 	} else
898 		g_free(buf);
899 
900 	/* No primary? Check that there's a secondary. */
901 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
902 	    &error);
903 	if (buf == NULL)
904 		return (error);
905 	res = memcmp(buf, GPT_HDR_SIG, 8);
906 	g_free(buf);
907 	return ((res == 0) ? pri : ENXIO);
908 }
909 
910 static int
911 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
912 {
913 	struct gpt_hdr *prihdr, *sechdr;
914 	struct gpt_ent *tbl, *pritbl, *sectbl;
915 	struct g_provider *pp;
916 	struct g_part_gpt_table *table;
917 	struct g_part_gpt_entry *entry;
918 	u_char *buf;
919 	uint64_t last;
920 	int error, index;
921 
922 	table = (struct g_part_gpt_table *)basetable;
923 	pp = cp->provider;
924 	last = (pp->mediasize / pp->sectorsize) - 1;
925 
926 	/* Read the PMBR */
927 	buf = g_read_data(cp, 0, pp->sectorsize, &error);
928 	if (buf == NULL)
929 		return (error);
930 	bcopy(buf, table->mbr, MBRSIZE);
931 	g_free(buf);
932 
933 	/* Read the primary header and table. */
934 	prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
935 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
936 		pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
937 	} else {
938 		table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
939 		pritbl = NULL;
940 	}
941 
942 	/* Read the secondary header and table. */
943 	sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
944 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
945 		sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
946 	} else {
947 		table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
948 		sectbl = NULL;
949 	}
950 
951 	/* Fail if we haven't got any good tables at all. */
952 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
953 	    table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
954 		printf("GEOM: %s: corrupt or invalid GPT detected.\n",
955 		    pp->name);
956 		printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
957 		    pp->name);
958 		if (prihdr != NULL)
959 			g_free(prihdr);
960 		if (pritbl != NULL)
961 			g_free(pritbl);
962 		if (sechdr != NULL)
963 			g_free(sechdr);
964 		if (sectbl != NULL)
965 			g_free(sectbl);
966 		return (EINVAL);
967 	}
968 
969 	/*
970 	 * If both headers are good but they disagree with each other,
971 	 * then invalidate one. We prefer to keep the primary header,
972 	 * unless the primary table is corrupt.
973 	 */
974 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
975 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
976 	    !gpt_matched_hdrs(prihdr, sechdr)) {
977 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
978 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
979 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
980 			g_free(sechdr);
981 			sechdr = NULL;
982 		} else {
983 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
984 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
985 			g_free(prihdr);
986 			prihdr = NULL;
987 		}
988 	}
989 
990 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
991 		printf("GEOM: %s: the primary GPT table is corrupt or "
992 		    "invalid.\n", pp->name);
993 		printf("GEOM: %s: using the secondary instead -- recovery "
994 		    "strongly advised.\n", pp->name);
995 		table->hdr = sechdr;
996 		basetable->gpt_corrupt = 1;
997 		if (prihdr != NULL)
998 			g_free(prihdr);
999 		tbl = sectbl;
1000 		if (pritbl != NULL)
1001 			g_free(pritbl);
1002 	} else {
1003 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
1004 			printf("GEOM: %s: the secondary GPT table is corrupt "
1005 			    "or invalid.\n", pp->name);
1006 			printf("GEOM: %s: using the primary only -- recovery "
1007 			    "suggested.\n", pp->name);
1008 			basetable->gpt_corrupt = 1;
1009 		} else if (table->lba[GPT_ELT_SECHDR] != last) {
1010 			printf( "GEOM: %s: the secondary GPT header is not in "
1011 			    "the last LBA.\n", pp->name);
1012 			basetable->gpt_corrupt = 1;
1013 		}
1014 		table->hdr = prihdr;
1015 		if (sechdr != NULL)
1016 			g_free(sechdr);
1017 		tbl = pritbl;
1018 		if (sectbl != NULL)
1019 			g_free(sectbl);
1020 	}
1021 
1022 	basetable->gpt_first = table->hdr->hdr_lba_start;
1023 	basetable->gpt_last = table->hdr->hdr_lba_end;
1024 	basetable->gpt_entries = table->hdr->hdr_entries;
1025 
1026 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
1027 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
1028 			continue;
1029 		entry = (struct g_part_gpt_entry *)g_part_new_entry(
1030 		    basetable, index + 1, tbl[index].ent_lba_start,
1031 		    tbl[index].ent_lba_end);
1032 		entry->ent = tbl[index];
1033 	}
1034 
1035 	g_free(tbl);
1036 
1037 	/*
1038 	 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1039 	 * if (and only if) any FAT32 or FAT16 partitions have been
1040 	 * created. This happens irrespective of whether Boot Camp is
1041 	 * used/enabled, though it's generally understood to be done
1042 	 * to support legacy Windows under Boot Camp. We refer to this
1043 	 * mirroring simply as Boot Camp. We try to detect Boot Camp
1044 	 * so that we can update the MBR if and when GPT changes have
1045 	 * been made. Note that we do not enable Boot Camp if not
1046 	 * previously enabled because we can't assume that we're on a
1047 	 * Mac alongside Mac OS X.
1048 	 */
1049 	table->bootcamp = gpt_is_bootcamp(table, pp->name);
1050 
1051 	return (0);
1052 }
1053 
1054 static int
1055 g_part_gpt_recover(struct g_part_table *basetable)
1056 {
1057 	struct g_part_gpt_table *table;
1058 	struct g_provider *pp;
1059 
1060 	table = (struct g_part_gpt_table *)basetable;
1061 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1062 	gpt_create_pmbr(table, pp);
1063 	g_gpt_set_defaults(basetable, pp);
1064 	basetable->gpt_corrupt = 0;
1065 	return (0);
1066 }
1067 
1068 static int
1069 g_part_gpt_setunset(struct g_part_table *basetable,
1070     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1071 {
1072 	struct g_part_gpt_entry *entry;
1073 	struct g_part_gpt_table *table;
1074 	struct g_provider *pp;
1075 	uint8_t *p;
1076 	uint64_t attr;
1077 	int i;
1078 
1079 	table = (struct g_part_gpt_table *)basetable;
1080 	entry = (struct g_part_gpt_entry *)baseentry;
1081 
1082 	if (strcasecmp(attrib, "active") == 0) {
1083 		if (table->bootcamp) {
1084 			/* The active flag must be set on a valid entry. */
1085 			if (entry == NULL)
1086 				return (ENXIO);
1087 			if (baseentry->gpe_index > NDOSPART)
1088 				return (EINVAL);
1089 			for (i = 0; i < NDOSPART; i++) {
1090 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1091 				p[0] = (i == baseentry->gpe_index - 1)
1092 				    ? ((set) ? 0x80 : 0) : 0;
1093 			}
1094 		} else {
1095 			/* The PMBR is marked as active without an entry. */
1096 			if (entry != NULL)
1097 				return (ENXIO);
1098 			for (i = 0; i < NDOSPART; i++) {
1099 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1100 				p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
1101 			}
1102 		}
1103 		return (0);
1104 	} else if (strcasecmp(attrib, "lenovofix") == 0) {
1105 		/*
1106 		 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1107 		 * This workaround allows Lenovo X220, T420, T520, etc to boot
1108 		 * from GPT Partitions in BIOS mode.
1109 		 */
1110 
1111 		if (entry != NULL)
1112 			return (ENXIO);
1113 
1114 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1115 		bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1116 		gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1117 		    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1118 		return (0);
1119 	}
1120 
1121 	if (entry == NULL)
1122 		return (ENODEV);
1123 
1124 	attr = 0;
1125 	if (strcasecmp(attrib, "bootme") == 0) {
1126 		attr |= GPT_ENT_ATTR_BOOTME;
1127 	} else if (strcasecmp(attrib, "bootonce") == 0) {
1128 		attr |= GPT_ENT_ATTR_BOOTONCE;
1129 		if (set)
1130 			attr |= GPT_ENT_ATTR_BOOTME;
1131 	} else if (strcasecmp(attrib, "bootfailed") == 0) {
1132 		/*
1133 		 * It should only be possible to unset BOOTFAILED, but it might
1134 		 * be useful for test purposes to also be able to set it.
1135 		 */
1136 		attr |= GPT_ENT_ATTR_BOOTFAILED;
1137 	}
1138 	if (attr == 0)
1139 		return (EINVAL);
1140 
1141 	if (set)
1142 		attr = entry->ent.ent_attr | attr;
1143 	else
1144 		attr = entry->ent.ent_attr & ~attr;
1145 	if (attr != entry->ent.ent_attr) {
1146 		entry->ent.ent_attr = attr;
1147 		if (!baseentry->gpe_created)
1148 			baseentry->gpe_modified = 1;
1149 	}
1150 	return (0);
1151 }
1152 
1153 static const char *
1154 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1155     char *buf, size_t bufsz)
1156 {
1157 	struct g_part_gpt_entry *entry;
1158 	struct uuid *type;
1159 	struct g_part_uuid_alias *uap;
1160 
1161 	entry = (struct g_part_gpt_entry *)baseentry;
1162 	type = &entry->ent.ent_type;
1163 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1164 		if (EQUUID(type, uap->uuid))
1165 			return (g_part_alias_name(uap->alias));
1166 	buf[0] = '!';
1167 	snprintf_uuid(buf + 1, bufsz - 1, type);
1168 
1169 	return (buf);
1170 }
1171 
1172 static int
1173 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1174 {
1175 	unsigned char *buf, *bp;
1176 	struct g_provider *pp;
1177 	struct g_part_entry *baseentry;
1178 	struct g_part_gpt_entry *entry;
1179 	struct g_part_gpt_table *table;
1180 	size_t tblsz;
1181 	uint32_t crc;
1182 	int error, index;
1183 
1184 	pp = cp->provider;
1185 	table = (struct g_part_gpt_table *)basetable;
1186 	tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
1187 	    pp->sectorsize);
1188 
1189 	/* Reconstruct the MBR from the GPT if under Boot Camp. */
1190 	if (table->bootcamp)
1191 		gpt_update_bootcamp(basetable, pp);
1192 
1193 	/* Write the PMBR */
1194 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1195 	bcopy(table->mbr, buf, MBRSIZE);
1196 	error = g_write_data(cp, 0, buf, pp->sectorsize);
1197 	g_free(buf);
1198 	if (error)
1199 		return (error);
1200 
1201 	/* Allocate space for the header and entries. */
1202 	buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1203 
1204 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
1205 	le32enc(buf + 8, table->hdr->hdr_revision);
1206 	le32enc(buf + 12, table->hdr->hdr_size);
1207 	le64enc(buf + 40, table->hdr->hdr_lba_start);
1208 	le64enc(buf + 48, table->hdr->hdr_lba_end);
1209 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
1210 	le32enc(buf + 80, table->hdr->hdr_entries);
1211 	le32enc(buf + 84, table->hdr->hdr_entsz);
1212 
1213 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1214 		if (baseentry->gpe_deleted)
1215 			continue;
1216 		entry = (struct g_part_gpt_entry *)baseentry;
1217 		index = baseentry->gpe_index - 1;
1218 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
1219 		le_uuid_enc(bp, &entry->ent.ent_type);
1220 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
1221 		le64enc(bp + 32, entry->ent.ent_lba_start);
1222 		le64enc(bp + 40, entry->ent.ent_lba_end);
1223 		le64enc(bp + 48, entry->ent.ent_attr);
1224 		memcpy(bp + 56, entry->ent.ent_name,
1225 		    sizeof(entry->ent.ent_name));
1226 	}
1227 
1228 	crc = crc32(buf + pp->sectorsize,
1229 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
1230 	le32enc(buf + 88, crc);
1231 
1232 	/* Write primary meta-data. */
1233 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
1234 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
1235 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
1236 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
1237 	crc = crc32(buf, table->hdr->hdr_size);
1238 	le32enc(buf + 16, crc);
1239 
1240 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1241 		error = g_write_data(cp,
1242 		    (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1243 		    buf + (index + 1) * pp->sectorsize,
1244 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1245 		    (tblsz - index) * pp->sectorsize);
1246 		if (error)
1247 			goto out;
1248 	}
1249 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1250 	    buf, pp->sectorsize);
1251 	if (error)
1252 		goto out;
1253 
1254 	/* Write secondary meta-data. */
1255 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
1256 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
1257 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
1258 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
1259 	crc = crc32(buf, table->hdr->hdr_size);
1260 	le32enc(buf + 16, crc);
1261 
1262 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1263 		error = g_write_data(cp,
1264 		    (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1265 		    buf + (index + 1) * pp->sectorsize,
1266 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1267 		    (tblsz - index) * pp->sectorsize);
1268 		if (error)
1269 			goto out;
1270 	}
1271 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1272 	    buf, pp->sectorsize);
1273 
1274  out:
1275 	g_free(buf);
1276 	return (error);
1277 }
1278 
1279 static void
1280 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
1281 {
1282 	struct g_part_entry *baseentry;
1283 	struct g_part_gpt_entry *entry;
1284 	struct g_part_gpt_table *table;
1285 	quad_t start, end, min, max;
1286 	quad_t lba, last;
1287 	size_t spb, tblsz;
1288 
1289 	table = (struct g_part_gpt_table *)basetable;
1290 	last = pp->mediasize / pp->sectorsize - 1;
1291 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
1292 	    pp->sectorsize);
1293 
1294 	table->lba[GPT_ELT_PRIHDR] = 1;
1295 	table->lba[GPT_ELT_PRITBL] = 2;
1296 	table->lba[GPT_ELT_SECHDR] = last;
1297 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
1298 	table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
1299 	table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
1300 	table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
1301 	table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
1302 
1303 	max = start = 2 + tblsz;
1304 	min = end = last - tblsz - 1;
1305 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1306 		if (baseentry->gpe_deleted)
1307 			continue;
1308 		entry = (struct g_part_gpt_entry *)baseentry;
1309 		if (entry->ent.ent_lba_start < min)
1310 			min = entry->ent.ent_lba_start;
1311 		if (entry->ent.ent_lba_end > max)
1312 			max = entry->ent.ent_lba_end;
1313 	}
1314 	spb = 4096 / pp->sectorsize;
1315 	if (spb > 1) {
1316 		lba = start + ((start % spb) ? spb - start % spb : 0);
1317 		if (lba <= min)
1318 			start = lba;
1319 		lba = end - (end + 1) % spb;
1320 		if (max <= lba)
1321 			end = lba;
1322 	}
1323 	table->hdr->hdr_lba_start = start;
1324 	table->hdr->hdr_lba_end = end;
1325 
1326 	basetable->gpt_first = start;
1327 	basetable->gpt_last = end;
1328 }
1329 
1330 static void
1331 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1332 {
1333 	u_int bo;
1334 	uint32_t ch;
1335 	uint16_t c;
1336 
1337 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
1338 	while (len > 0 && *str != 0) {
1339 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1340 		str++, len--;
1341 		if ((ch & 0xf800) == 0xd800) {
1342 			if (len > 0) {
1343 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
1344 				    : le16toh(*str);
1345 				str++, len--;
1346 			} else
1347 				c = 0xfffd;
1348 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1349 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1350 				ch += 0x10000;
1351 			} else
1352 				ch = 0xfffd;
1353 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1354 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1355 			continue;
1356 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1357 			continue;
1358 
1359 		/* Write the Unicode character in UTF-8 */
1360 		if (ch < 0x80)
1361 			g_conf_printf_escaped(sb, "%c", ch);
1362 		else if (ch < 0x800)
1363 			g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
1364 			    0x80 | (ch & 0x3f));
1365 		else if (ch < 0x10000)
1366 			g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
1367 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1368 		else if (ch < 0x200000)
1369 			g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
1370 			    (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
1371 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1372 	}
1373 }
1374 
1375 static void
1376 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1377 {
1378 	size_t s16idx, s8idx;
1379 	uint32_t utfchar;
1380 	unsigned int c, utfbytes;
1381 
1382 	s8idx = s16idx = 0;
1383 	utfchar = 0;
1384 	utfbytes = 0;
1385 	bzero(s16, s16len << 1);
1386 	while (s8[s8idx] != 0 && s16idx < s16len) {
1387 		c = s8[s8idx++];
1388 		if ((c & 0xc0) != 0x80) {
1389 			/* Initial characters. */
1390 			if (utfbytes != 0) {
1391 				/* Incomplete encoding of previous char. */
1392 				s16[s16idx++] = htole16(0xfffd);
1393 			}
1394 			if ((c & 0xf8) == 0xf0) {
1395 				utfchar = c & 0x07;
1396 				utfbytes = 3;
1397 			} else if ((c & 0xf0) == 0xe0) {
1398 				utfchar = c & 0x0f;
1399 				utfbytes = 2;
1400 			} else if ((c & 0xe0) == 0xc0) {
1401 				utfchar = c & 0x1f;
1402 				utfbytes = 1;
1403 			} else {
1404 				utfchar = c & 0x7f;
1405 				utfbytes = 0;
1406 			}
1407 		} else {
1408 			/* Followup characters. */
1409 			if (utfbytes > 0) {
1410 				utfchar = (utfchar << 6) + (c & 0x3f);
1411 				utfbytes--;
1412 			} else if (utfbytes == 0)
1413 				utfbytes = ~0;
1414 		}
1415 		/*
1416 		 * Write the complete Unicode character as UTF-16 when we
1417 		 * have all the UTF-8 charactars collected.
1418 		 */
1419 		if (utfbytes == 0) {
1420 			/*
1421 			 * If we need to write 2 UTF-16 characters, but
1422 			 * we only have room for 1, then we truncate the
1423 			 * string by writing a 0 instead.
1424 			 */
1425 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1426 				s16[s16idx++] =
1427 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
1428 				s16[s16idx++] =
1429 				    htole16(0xdc00 | (utfchar & 0x3ff));
1430 			} else
1431 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1432 				    htole16(utfchar);
1433 		}
1434 	}
1435 	/*
1436 	 * If our input string was truncated, append an invalid encoding
1437 	 * character to the output string.
1438 	 */
1439 	if (utfbytes != 0 && s16idx < s16len)
1440 		s16[s16idx++] = htole16(0xfffd);
1441 }
1442