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