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