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