xref: /freebsd/sys/geom/part/g_part_gpt.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
1 /*-
2  * Copyright (c) 2002, 2005, 2006, 2007 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/uuid.h>
45 #include <geom/geom.h>
46 #include <geom/part/g_part.h>
47 
48 #include "g_part_if.h"
49 
50 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
51 CTASSERT(sizeof(struct gpt_ent) == 128);
52 
53 #define	EQUUID(a,b)	(memcmp(a, b, sizeof(struct uuid)) == 0)
54 
55 #define	MBRSIZE		512
56 
57 enum gpt_elt {
58 	GPT_ELT_PRIHDR,
59 	GPT_ELT_PRITBL,
60 	GPT_ELT_SECHDR,
61 	GPT_ELT_SECTBL,
62 	GPT_ELT_COUNT
63 };
64 
65 enum gpt_state {
66 	GPT_STATE_UNKNOWN,	/* Not determined. */
67 	GPT_STATE_MISSING,	/* No signature found. */
68 	GPT_STATE_CORRUPT,	/* Checksum mismatch. */
69 	GPT_STATE_INVALID,	/* Nonconformant/invalid. */
70 	GPT_STATE_OK		/* Perfectly fine. */
71 };
72 
73 struct g_part_gpt_table {
74 	struct g_part_table	base;
75 	u_char			mbr[MBRSIZE];
76 	struct gpt_hdr		*hdr;
77 	quad_t			lba[GPT_ELT_COUNT];
78 	enum gpt_state		state[GPT_ELT_COUNT];
79 };
80 
81 struct g_part_gpt_entry {
82 	struct g_part_entry	base;
83 	struct gpt_ent		ent;
84 };
85 
86 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
87 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
88 
89 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
90     struct g_part_parms *);
91 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
92 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
93 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
94 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
95     struct sbuf *, const char *);
96 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
97 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
98     struct g_part_parms *);
99 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
100     char *, size_t);
101 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
102 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
103 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
104     char *, size_t);
105 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
106 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
107     struct g_part_parms *);
108 
109 static kobj_method_t g_part_gpt_methods[] = {
110 	KOBJMETHOD(g_part_add,		g_part_gpt_add),
111 	KOBJMETHOD(g_part_bootcode,	g_part_gpt_bootcode),
112 	KOBJMETHOD(g_part_create,	g_part_gpt_create),
113 	KOBJMETHOD(g_part_destroy,	g_part_gpt_destroy),
114 	KOBJMETHOD(g_part_dumpconf,	g_part_gpt_dumpconf),
115 	KOBJMETHOD(g_part_dumpto,	g_part_gpt_dumpto),
116 	KOBJMETHOD(g_part_modify,	g_part_gpt_modify),
117 	KOBJMETHOD(g_part_resize,	g_part_gpt_resize),
118 	KOBJMETHOD(g_part_name,		g_part_gpt_name),
119 	KOBJMETHOD(g_part_probe,	g_part_gpt_probe),
120 	KOBJMETHOD(g_part_read,		g_part_gpt_read),
121 	KOBJMETHOD(g_part_type,		g_part_gpt_type),
122 	KOBJMETHOD(g_part_write,	g_part_gpt_write),
123 	{ 0, 0 }
124 };
125 
126 static struct g_part_scheme g_part_gpt_scheme = {
127 	"GPT",
128 	g_part_gpt_methods,
129 	sizeof(struct g_part_gpt_table),
130 	.gps_entrysz = sizeof(struct g_part_gpt_entry),
131 	.gps_minent = 128,
132 	.gps_maxent = INT_MAX,
133 	.gps_bootcodesz = MBRSIZE,
134 };
135 G_PART_SCHEME_DECLARE(g_part_gpt);
136 
137 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
138 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
139 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
140 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
141 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
142 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
143 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
144 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
145 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
146 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
147 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
148 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
149 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
150 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
151 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
152 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
153 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
154 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
155 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
156 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
157 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
158 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
159 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
160 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
161 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
162 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
163 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
164 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
165 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
166 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
167 
168 static struct g_part_uuid_alias {
169 	struct uuid *uuid;
170 	int alias;
171 } gpt_uuid_alias_match[] = {
172 	{ &gpt_uuid_apple_boot,		G_PART_ALIAS_APPLE_BOOT },
173 	{ &gpt_uuid_apple_hfs,		G_PART_ALIAS_APPLE_HFS },
174 	{ &gpt_uuid_apple_label,	G_PART_ALIAS_APPLE_LABEL },
175 	{ &gpt_uuid_apple_raid,		G_PART_ALIAS_APPLE_RAID },
176 	{ &gpt_uuid_apple_raid_offline,	G_PART_ALIAS_APPLE_RAID_OFFLINE },
177 	{ &gpt_uuid_apple_tv_recovery,	G_PART_ALIAS_APPLE_TV_RECOVERY },
178 	{ &gpt_uuid_apple_ufs,		G_PART_ALIAS_APPLE_UFS },
179 	{ &gpt_uuid_efi, 		G_PART_ALIAS_EFI },
180 	{ &gpt_uuid_freebsd,		G_PART_ALIAS_FREEBSD },
181 	{ &gpt_uuid_freebsd_boot, 	G_PART_ALIAS_FREEBSD_BOOT },
182 	{ &gpt_uuid_freebsd_swap,	G_PART_ALIAS_FREEBSD_SWAP },
183 	{ &gpt_uuid_freebsd_ufs,	G_PART_ALIAS_FREEBSD_UFS },
184 	{ &gpt_uuid_freebsd_vinum,	G_PART_ALIAS_FREEBSD_VINUM },
185 	{ &gpt_uuid_freebsd_zfs,	G_PART_ALIAS_FREEBSD_ZFS },
186 	{ &gpt_uuid_linux_data,		G_PART_ALIAS_LINUX_DATA },
187 	{ &gpt_uuid_linux_lvm,		G_PART_ALIAS_LINUX_LVM },
188 	{ &gpt_uuid_linux_raid,		G_PART_ALIAS_LINUX_RAID },
189 	{ &gpt_uuid_linux_swap,		G_PART_ALIAS_LINUX_SWAP },
190 	{ &gpt_uuid_mbr,		G_PART_ALIAS_MBR },
191 	{ &gpt_uuid_ms_basic_data,	G_PART_ALIAS_MS_BASIC_DATA },
192 	{ &gpt_uuid_ms_ldm_data,	G_PART_ALIAS_MS_LDM_DATA },
193 	{ &gpt_uuid_ms_ldm_metadata,	G_PART_ALIAS_MS_LDM_METADATA },
194 	{ &gpt_uuid_ms_reserved,	G_PART_ALIAS_MS_RESERVED },
195 	{ &gpt_uuid_netbsd_ccd,		G_PART_ALIAS_NETBSD_CCD },
196 	{ &gpt_uuid_netbsd_cgd,		G_PART_ALIAS_NETBSD_CGD },
197 	{ &gpt_uuid_netbsd_ffs,		G_PART_ALIAS_NETBSD_FFS },
198 	{ &gpt_uuid_netbsd_lfs,		G_PART_ALIAS_NETBSD_LFS },
199 	{ &gpt_uuid_netbsd_raid,	G_PART_ALIAS_NETBSD_RAID },
200 	{ &gpt_uuid_netbsd_swap,	G_PART_ALIAS_NETBSD_SWAP },
201 
202 	{ NULL, 0 }
203 };
204 
205 static struct gpt_hdr *
206 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
207     enum gpt_elt elt)
208 {
209 	struct gpt_hdr *buf, *hdr;
210 	struct g_provider *pp;
211 	quad_t lba, last;
212 	int error;
213 	uint32_t crc, sz;
214 
215 	pp = cp->provider;
216 	last = (pp->mediasize / pp->sectorsize) - 1;
217 	table->lba[elt] = (elt == GPT_ELT_PRIHDR) ? 1 : last;
218 	table->state[elt] = GPT_STATE_MISSING;
219 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
220 	    &error);
221 	if (buf == NULL)
222 		return (NULL);
223 	hdr = NULL;
224 	if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
225 		goto fail;
226 
227 	table->state[elt] = GPT_STATE_CORRUPT;
228 	sz = le32toh(buf->hdr_size);
229 	if (sz < 92 || sz > pp->sectorsize)
230 		goto fail;
231 
232 	hdr = g_malloc(sz, M_WAITOK | M_ZERO);
233 	bcopy(buf, hdr, sz);
234 	hdr->hdr_size = sz;
235 
236 	crc = le32toh(buf->hdr_crc_self);
237 	buf->hdr_crc_self = 0;
238 	if (crc32(buf, sz) != crc)
239 		goto fail;
240 	hdr->hdr_crc_self = crc;
241 
242 	table->state[elt] = GPT_STATE_INVALID;
243 	hdr->hdr_revision = le32toh(buf->hdr_revision);
244 	if (hdr->hdr_revision < 0x00010000)
245 		goto fail;
246 	hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
247 	if (hdr->hdr_lba_self != table->lba[elt])
248 		goto fail;
249 	hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
250 
251 	/* Check the managed area. */
252 	hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
253 	if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
254 		goto fail;
255 	hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
256 	if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
257 		goto fail;
258 
259 	/* Check the table location and size of the table. */
260 	hdr->hdr_entries = le32toh(buf->hdr_entries);
261 	hdr->hdr_entsz = le32toh(buf->hdr_entsz);
262 	if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
263 	    (hdr->hdr_entsz & 7) != 0)
264 		goto fail;
265 	hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
266 	if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
267 		goto fail;
268 	if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
269 	    hdr->hdr_lba_table <= hdr->hdr_lba_end)
270 		goto fail;
271 	lba = hdr->hdr_lba_table +
272 	    (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
273 	    pp->sectorsize - 1;
274 	if (lba >= last)
275 		goto fail;
276 	if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
277 		goto fail;
278 
279 	table->state[elt] = GPT_STATE_OK;
280 	le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
281 	hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
282 
283 	g_free(buf);
284 	return (hdr);
285 
286  fail:
287 	if (hdr != NULL)
288 		g_free(hdr);
289 	g_free(buf);
290 	return (NULL);
291 }
292 
293 static struct gpt_ent *
294 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
295     enum gpt_elt elt, struct gpt_hdr *hdr)
296 {
297 	struct g_provider *pp;
298 	struct gpt_ent *ent, *tbl;
299 	char *buf, *p;
300 	unsigned int idx, sectors, tblsz;
301 	int error;
302 
303 	if (hdr == NULL)
304 		return (NULL);
305 
306 	pp = cp->provider;
307 	table->lba[elt] = hdr->hdr_lba_table;
308 
309 	table->state[elt] = GPT_STATE_MISSING;
310 	tblsz = hdr->hdr_entries * hdr->hdr_entsz;
311 	sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize;
312 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize,
313 	    sectors * pp->sectorsize, &error);
314 	if (buf == NULL)
315 		return (NULL);
316 
317 	table->state[elt] = GPT_STATE_CORRUPT;
318 	if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
319 		g_free(buf);
320 		return (NULL);
321 	}
322 
323 	table->state[elt] = GPT_STATE_OK;
324 	tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
325 	    M_WAITOK | M_ZERO);
326 
327 	for (idx = 0, ent = tbl, p = buf;
328 	     idx < hdr->hdr_entries;
329 	     idx++, ent++, p += hdr->hdr_entsz) {
330 		le_uuid_dec(p, &ent->ent_type);
331 		le_uuid_dec(p + 16, &ent->ent_uuid);
332 		ent->ent_lba_start = le64dec(p + 32);
333 		ent->ent_lba_end = le64dec(p + 40);
334 		ent->ent_attr = le64dec(p + 48);
335 		/* Keep UTF-16 in little-endian. */
336 		bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
337 	}
338 
339 	g_free(buf);
340 	return (tbl);
341 }
342 
343 static int
344 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
345 {
346 
347 	if (pri == NULL || sec == NULL)
348 		return (0);
349 
350 	if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
351 		return (0);
352 	return ((pri->hdr_revision == sec->hdr_revision &&
353 	    pri->hdr_size == sec->hdr_size &&
354 	    pri->hdr_lba_start == sec->hdr_lba_start &&
355 	    pri->hdr_lba_end == sec->hdr_lba_end &&
356 	    pri->hdr_entries == sec->hdr_entries &&
357 	    pri->hdr_entsz == sec->hdr_entsz &&
358 	    pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
359 }
360 
361 static int
362 gpt_parse_type(const char *type, struct uuid *uuid)
363 {
364 	struct uuid tmp;
365 	const char *alias;
366 	int error;
367 	struct g_part_uuid_alias *uap;
368 
369 	if (type[0] == '!') {
370 		error = parse_uuid(type + 1, &tmp);
371 		if (error)
372 			return (error);
373 		if (EQUUID(&tmp, &gpt_uuid_unused))
374 			return (EINVAL);
375 		*uuid = tmp;
376 		return (0);
377 	}
378 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
379 		alias = g_part_alias_name(uap->alias);
380 		if (!strcasecmp(type, alias)) {
381 			*uuid = *uap->uuid;
382 			return (0);
383 		}
384 	}
385 	return (EINVAL);
386 }
387 
388 static int
389 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
390     struct g_part_parms *gpp)
391 {
392 	struct g_part_gpt_entry *entry;
393 	int error;
394 
395 	entry = (struct g_part_gpt_entry *)baseentry;
396 	error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
397 	if (error)
398 		return (error);
399 	kern_uuidgen(&entry->ent.ent_uuid, 1);
400 	entry->ent.ent_lba_start = baseentry->gpe_start;
401 	entry->ent.ent_lba_end = baseentry->gpe_end;
402 	if (baseentry->gpe_deleted) {
403 		entry->ent.ent_attr = 0;
404 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
405 	}
406 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
407 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
408 		    sizeof(entry->ent.ent_name));
409 	return (0);
410 }
411 
412 static int
413 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
414 {
415 	struct g_part_gpt_table *table;
416 	size_t codesz;
417 
418 	codesz = DOSPARTOFF;
419 	table = (struct g_part_gpt_table *)basetable;
420 	bzero(table->mbr, codesz);
421 	codesz = MIN(codesz, gpp->gpp_codesize);
422 	if (codesz > 0)
423 		bcopy(gpp->gpp_codeptr, table->mbr, codesz);
424 
425 	/* Mark the PMBR active since some BIOS require it */
426 	table->mbr[DOSPARTOFF] = 0x80;		/* status */
427 	return (0);
428 }
429 
430 static int
431 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
432 {
433 	struct g_provider *pp;
434 	struct g_part_gpt_table *table;
435 	quad_t last;
436 	size_t tblsz;
437 
438 	/* We don't nest, which means that our depth should be 0. */
439 	if (basetable->gpt_depth != 0)
440 		return (ENXIO);
441 
442 	table = (struct g_part_gpt_table *)basetable;
443 	pp = gpp->gpp_provider;
444 	tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
445 	    pp->sectorsize - 1) / pp->sectorsize;
446 	if (pp->sectorsize < MBRSIZE ||
447 	    pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
448 	    pp->sectorsize)
449 		return (ENOSPC);
450 
451 	last = (pp->mediasize / pp->sectorsize) - 1;
452 
453 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
454 	table->mbr[DOSPARTOFF + 1] = 0x01;		/* shd */
455 	table->mbr[DOSPARTOFF + 2] = 0x01;		/* ssect */
456 	table->mbr[DOSPARTOFF + 3] = 0x00;		/* scyl */
457 	table->mbr[DOSPARTOFF + 4] = 0xee;		/* typ */
458 	table->mbr[DOSPARTOFF + 5] = 0xff;		/* ehd */
459 	table->mbr[DOSPARTOFF + 6] = 0xff;		/* esect */
460 	table->mbr[DOSPARTOFF + 7] = 0xff;		/* ecyl */
461 	le32enc(table->mbr + DOSPARTOFF + 8, 1);	/* start */
462 	le32enc(table->mbr + DOSPARTOFF + 12, MIN(last, 0xffffffffLL));
463 
464 	table->lba[GPT_ELT_PRIHDR] = 1;
465 	table->lba[GPT_ELT_PRITBL] = 2;
466 	table->lba[GPT_ELT_SECHDR] = last;
467 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
468 
469 	/* Allocate space for the header */
470 	table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
471 
472 	bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
473 	table->hdr->hdr_revision = GPT_HDR_REVISION;
474 	table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
475 	table->hdr->hdr_lba_start = 2 + tblsz;
476 	table->hdr->hdr_lba_end = last - tblsz - 1;
477 	kern_uuidgen(&table->hdr->hdr_uuid, 1);
478 	table->hdr->hdr_entries = basetable->gpt_entries;
479 	table->hdr->hdr_entsz = sizeof(struct gpt_ent);
480 
481 	basetable->gpt_first = table->hdr->hdr_lba_start;
482 	basetable->gpt_last = table->hdr->hdr_lba_end;
483 	return (0);
484 }
485 
486 static int
487 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
488 {
489 	struct g_part_gpt_table *table;
490 
491 	table = (struct g_part_gpt_table *)basetable;
492 	if (table->hdr != NULL)
493 		g_free(table->hdr);
494 	table->hdr = NULL;
495 
496 	/*
497 	 * Wipe the first 2 sectors as well as the last to clear the
498 	 * partitioning.
499 	 */
500 	basetable->gpt_smhead |= 3;
501 	basetable->gpt_smtail |= 1;
502 	return (0);
503 }
504 
505 static void
506 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
507     struct sbuf *sb, const char *indent)
508 {
509 	struct g_part_gpt_entry *entry;
510 
511 	entry = (struct g_part_gpt_entry *)baseentry;
512 	if (indent == NULL) {
513 		/* conftxt: libdisk compatibility */
514 		sbuf_printf(sb, " xs GPT xt ");
515 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
516 	} else if (entry != NULL) {
517 		/* confxml: partition entry information */
518 		sbuf_printf(sb, "%s<label>", indent);
519 		g_gpt_printf_utf16(sb, entry->ent.ent_name,
520 		    sizeof(entry->ent.ent_name) >> 1);
521 		sbuf_printf(sb, "</label>\n");
522 		sbuf_printf(sb, "%s<rawtype>", indent);
523 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
524 		sbuf_printf(sb, "</rawtype>\n");
525 		sbuf_printf(sb, "%s<rawuuid>", indent);
526 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
527 		sbuf_printf(sb, "</rawuuid>\n");
528 	} else {
529 		/* confxml: scheme information */
530 	}
531 }
532 
533 static int
534 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
535 {
536 	struct g_part_gpt_entry *entry;
537 
538 	entry = (struct g_part_gpt_entry *)baseentry;
539 	return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
540 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0);
541 }
542 
543 static int
544 g_part_gpt_modify(struct g_part_table *basetable,
545     struct g_part_entry *baseentry, struct g_part_parms *gpp)
546 {
547 	struct g_part_gpt_entry *entry;
548 	int error;
549 
550 	entry = (struct g_part_gpt_entry *)baseentry;
551 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
552 		error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
553 		if (error)
554 			return (error);
555 	}
556 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
557 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
558 		    sizeof(entry->ent.ent_name));
559 	return (0);
560 }
561 
562 static int
563 g_part_gpt_resize(struct g_part_table *basetable,
564     struct g_part_entry *baseentry, struct g_part_parms *gpp)
565 {
566 	struct g_part_gpt_entry *entry;
567 	entry = (struct g_part_gpt_entry *)baseentry;
568 
569 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
570 	entry->ent.ent_lba_end = baseentry->gpe_end;
571 
572 	return (0);
573 }
574 
575 static const char *
576 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
577     char *buf, size_t bufsz)
578 {
579 	struct g_part_gpt_entry *entry;
580 	char c;
581 
582 	entry = (struct g_part_gpt_entry *)baseentry;
583 	c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
584 	snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
585 	return (buf);
586 }
587 
588 static int
589 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
590 {
591 	struct g_provider *pp;
592 	char *buf;
593 	int error, res;
594 
595 	/* We don't nest, which means that our depth should be 0. */
596 	if (table->gpt_depth != 0)
597 		return (ENXIO);
598 
599 	pp = cp->provider;
600 
601 	/*
602 	 * Sanity-check the provider. Since the first sector on the provider
603 	 * must be a PMBR and a PMBR is 512 bytes large, the sector size
604 	 * must be at least 512 bytes.  Also, since the theoretical minimum
605 	 * number of sectors needed by GPT is 6, any medium that has less
606 	 * than 6 sectors is never going to be able to hold a GPT. The
607 	 * number 6 comes from:
608 	 *	1 sector for the PMBR
609 	 *	2 sectors for the GPT headers (each 1 sector)
610 	 *	2 sectors for the GPT tables (each 1 sector)
611 	 *	1 sector for an actual partition
612 	 * It's better to catch this pathological case early than behaving
613 	 * pathologically later on...
614 	 */
615 	if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
616 		return (ENOSPC);
617 
618 	/* Check that there's a MBR. */
619 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
620 	if (buf == NULL)
621 		return (error);
622 	res = le16dec(buf + DOSMAGICOFFSET);
623 	g_free(buf);
624 	if (res != DOSMAGIC)
625 		return (ENXIO);
626 
627 	/* Check that there's a primary header. */
628 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
629 	if (buf == NULL)
630 		return (error);
631 	res = memcmp(buf, GPT_HDR_SIG, 8);
632 	g_free(buf);
633 	if (res == 0)
634 		return (G_PART_PROBE_PRI_HIGH);
635 
636 	/* No primary? Check that there's a secondary. */
637 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
638 	    &error);
639 	if (buf == NULL)
640 		return (error);
641 	res = memcmp(buf, GPT_HDR_SIG, 8);
642 	g_free(buf);
643 	return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO);
644 }
645 
646 static int
647 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
648 {
649 	struct gpt_hdr *prihdr, *sechdr;
650 	struct gpt_ent *tbl, *pritbl, *sectbl;
651 	struct g_provider *pp;
652 	struct g_part_gpt_table *table;
653 	struct g_part_gpt_entry *entry;
654 	u_char *buf;
655 	int error, index;
656 
657 	table = (struct g_part_gpt_table *)basetable;
658 	pp = cp->provider;
659 
660 	/* Read the PMBR */
661 	buf = g_read_data(cp, 0, pp->sectorsize, &error);
662 	if (buf == NULL)
663 		return (error);
664 	bcopy(buf, table->mbr, MBRSIZE);
665 	g_free(buf);
666 
667 	/* Read the primary header and table. */
668 	prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
669 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
670 		pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
671 	} else {
672 		table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
673 		pritbl = NULL;
674 	}
675 
676 	/* Read the secondary header and table. */
677 	sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
678 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
679 		sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
680 	} else {
681 		table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
682 		sectbl = NULL;
683 	}
684 
685 	/* Fail if we haven't got any good tables at all. */
686 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
687 	    table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
688 		printf("GEOM: %s: corrupt or invalid GPT detected.\n",
689 		    pp->name);
690 		printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
691 		    pp->name);
692 		return (EINVAL);
693 	}
694 
695 	/*
696 	 * If both headers are good but they disagree with each other,
697 	 * then invalidate one. We prefer to keep the primary header,
698 	 * unless the primary table is corrupt.
699 	 */
700 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
701 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
702 	    !gpt_matched_hdrs(prihdr, sechdr)) {
703 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
704 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
705 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
706 			g_free(sechdr);
707 			sechdr = NULL;
708 		} else {
709 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
710 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
711 			g_free(prihdr);
712 			prihdr = NULL;
713 		}
714 	}
715 
716 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
717 		printf("GEOM: %s: the primary GPT table is corrupt or "
718 		    "invalid.\n", pp->name);
719 		printf("GEOM: %s: using the secondary instead -- recovery "
720 		    "strongly advised.\n", pp->name);
721 		table->hdr = sechdr;
722 		if (prihdr != NULL)
723 			g_free(prihdr);
724 		tbl = sectbl;
725 		if (pritbl != NULL)
726 			g_free(pritbl);
727 	} else {
728 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
729 			printf("GEOM: %s: the secondary GPT table is corrupt "
730 			    "or invalid.\n", pp->name);
731 			printf("GEOM: %s: using the primary only -- recovery "
732 			    "suggested.\n", pp->name);
733 		}
734 		table->hdr = prihdr;
735 		if (sechdr != NULL)
736 			g_free(sechdr);
737 		tbl = pritbl;
738 		if (sectbl != NULL)
739 			g_free(sectbl);
740 	}
741 
742 	basetable->gpt_first = table->hdr->hdr_lba_start;
743 	basetable->gpt_last = table->hdr->hdr_lba_end;
744 	basetable->gpt_entries = table->hdr->hdr_entries;
745 
746 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
747 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
748 			continue;
749 		entry = (struct g_part_gpt_entry *)g_part_new_entry(basetable,
750 		    index+1, tbl[index].ent_lba_start, tbl[index].ent_lba_end);
751 		entry->ent = tbl[index];
752 	}
753 
754 	g_free(tbl);
755 	return (0);
756 }
757 
758 static const char *
759 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
760     char *buf, size_t bufsz)
761 {
762 	struct g_part_gpt_entry *entry;
763 	struct uuid *type;
764 	struct g_part_uuid_alias *uap;
765 
766 	entry = (struct g_part_gpt_entry *)baseentry;
767 	type = &entry->ent.ent_type;
768 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
769 		if (EQUUID(type, uap->uuid))
770 			return (g_part_alias_name(uap->alias));
771 	buf[0] = '!';
772 	snprintf_uuid(buf + 1, bufsz - 1, type);
773 
774 	return (buf);
775 }
776 
777 static int
778 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
779 {
780 	unsigned char *buf, *bp;
781 	struct g_provider *pp;
782 	struct g_part_entry *baseentry;
783 	struct g_part_gpt_entry *entry;
784 	struct g_part_gpt_table *table;
785 	size_t tlbsz;
786 	uint32_t crc;
787 	int error, index;
788 
789 	pp = cp->provider;
790 	table = (struct g_part_gpt_table *)basetable;
791 	tlbsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
792 	    pp->sectorsize - 1) / pp->sectorsize;
793 
794 	/* Write the PMBR */
795 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
796 	bcopy(table->mbr, buf, MBRSIZE);
797 	error = g_write_data(cp, 0, buf, pp->sectorsize);
798 	g_free(buf);
799 	if (error)
800 		return (error);
801 
802 	/* Allocate space for the header and entries. */
803 	buf = g_malloc((tlbsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
804 
805 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
806 	le32enc(buf + 8, table->hdr->hdr_revision);
807 	le32enc(buf + 12, table->hdr->hdr_size);
808 	le64enc(buf + 40, table->hdr->hdr_lba_start);
809 	le64enc(buf + 48, table->hdr->hdr_lba_end);
810 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
811 	le32enc(buf + 80, table->hdr->hdr_entries);
812 	le32enc(buf + 84, table->hdr->hdr_entsz);
813 
814 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
815 		if (baseentry->gpe_deleted)
816 			continue;
817 		entry = (struct g_part_gpt_entry *)baseentry;
818 		index = baseentry->gpe_index - 1;
819 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
820 		le_uuid_enc(bp, &entry->ent.ent_type);
821 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
822 		le64enc(bp + 32, entry->ent.ent_lba_start);
823 		le64enc(bp + 40, entry->ent.ent_lba_end);
824 		le64enc(bp + 48, entry->ent.ent_attr);
825 		memcpy(bp + 56, entry->ent.ent_name,
826 		    sizeof(entry->ent.ent_name));
827 	}
828 
829 	crc = crc32(buf + pp->sectorsize,
830 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
831 	le32enc(buf + 88, crc);
832 
833 	/* Write primary meta-data. */
834 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
835 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
836 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
837 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
838 	crc = crc32(buf, table->hdr->hdr_size);
839 	le32enc(buf + 16, crc);
840 
841 	error = g_write_data(cp, table->lba[GPT_ELT_PRITBL] * pp->sectorsize,
842 	    buf + pp->sectorsize, tlbsz * pp->sectorsize);
843 	if (error)
844 		goto out;
845 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
846 	    buf, pp->sectorsize);
847 	if (error)
848 		goto out;
849 
850 	/* Write secondary meta-data. */
851 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
852 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
853 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
854 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
855 	crc = crc32(buf, table->hdr->hdr_size);
856 	le32enc(buf + 16, crc);
857 
858 	error = g_write_data(cp, table->lba[GPT_ELT_SECTBL] * pp->sectorsize,
859 	    buf + pp->sectorsize, tlbsz * pp->sectorsize);
860 	if (error)
861 		goto out;
862 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
863 	    buf, pp->sectorsize);
864 
865  out:
866 	g_free(buf);
867 	return (error);
868 }
869 
870 static void
871 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
872 {
873 	u_int bo;
874 	uint32_t ch;
875 	uint16_t c;
876 
877 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
878 	while (len > 0 && *str != 0) {
879 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
880 		str++, len--;
881 		if ((ch & 0xf800) == 0xd800) {
882 			if (len > 0) {
883 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
884 				    : le16toh(*str);
885 				str++, len--;
886 			} else
887 				c = 0xfffd;
888 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
889 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
890 				ch += 0x10000;
891 			} else
892 				ch = 0xfffd;
893 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
894 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
895 			continue;
896 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
897 			continue;
898 
899 		/* Write the Unicode character in UTF-8 */
900 		if (ch < 0x80)
901 			sbuf_printf(sb, "%c", ch);
902 		else if (ch < 0x800)
903 			sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6),
904 			    0x80 | (ch & 0x3f));
905 		else if (ch < 0x10000)
906 			sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12),
907 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
908 		else if (ch < 0x200000)
909 			sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18),
910 			    0x80 | ((ch >> 12) & 0x3f),
911 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
912 	}
913 }
914 
915 static void
916 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
917 {
918 	size_t s16idx, s8idx;
919 	uint32_t utfchar;
920 	unsigned int c, utfbytes;
921 
922 	s8idx = s16idx = 0;
923 	utfchar = 0;
924 	utfbytes = 0;
925 	bzero(s16, s16len << 1);
926 	while (s8[s8idx] != 0 && s16idx < s16len) {
927 		c = s8[s8idx++];
928 		if ((c & 0xc0) != 0x80) {
929 			/* Initial characters. */
930 			if (utfbytes != 0) {
931 				/* Incomplete encoding of previous char. */
932 				s16[s16idx++] = htole16(0xfffd);
933 			}
934 			if ((c & 0xf8) == 0xf0) {
935 				utfchar = c & 0x07;
936 				utfbytes = 3;
937 			} else if ((c & 0xf0) == 0xe0) {
938 				utfchar = c & 0x0f;
939 				utfbytes = 2;
940 			} else if ((c & 0xe0) == 0xc0) {
941 				utfchar = c & 0x1f;
942 				utfbytes = 1;
943 			} else {
944 				utfchar = c & 0x7f;
945 				utfbytes = 0;
946 			}
947 		} else {
948 			/* Followup characters. */
949 			if (utfbytes > 0) {
950 				utfchar = (utfchar << 6) + (c & 0x3f);
951 				utfbytes--;
952 			} else if (utfbytes == 0)
953 				utfbytes = ~0;
954 		}
955 		/*
956 		 * Write the complete Unicode character as UTF-16 when we
957 		 * have all the UTF-8 charactars collected.
958 		 */
959 		if (utfbytes == 0) {
960 			/*
961 			 * If we need to write 2 UTF-16 characters, but
962 			 * we only have room for 1, then we truncate the
963 			 * string by writing a 0 instead.
964 			 */
965 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
966 				s16[s16idx++] =
967 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
968 				s16[s16idx++] =
969 				    htole16(0xdc00 | (utfchar & 0x3ff));
970 			} else
971 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
972 				    htole16(utfchar);
973 		}
974 	}
975 	/*
976 	 * If our input string was truncated, append an invalid encoding
977 	 * character to the output string.
978 	 */
979 	if (utfbytes != 0 && s16idx < s16len)
980 		s16[s16idx++] = htole16(0xfffd);
981 }
982