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