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