2 * Copyright (c) 2002, 2005, 2006, 2007 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
43 #include <sys/systm.h>
45 #include <geom/geom.h>
46 #include <geom/part/g_part.h>
48 #include "g_part_if.h"
50 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
51 CTASSERT(sizeof(struct gpt_ent) == 128);
53 #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0)
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. */
73 struct g_part_gpt_table {
74 struct g_part_table base;
77 quad_t lba[GPT_ELT_COUNT];
78 enum gpt_state state[GPT_ELT_COUNT];
81 struct g_part_gpt_entry {
82 struct g_part_entry base;
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);
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 *,
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 *,
105 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
107 static 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),
123 static struct g_part_scheme g_part_gpt_scheme = {
126 sizeof(struct g_part_gpt_table),
127 .gps_entrysz = sizeof(struct g_part_gpt_entry),
129 .gps_maxent = INT_MAX,
130 .gps_bootcodesz = MBRSIZE,
132 G_PART_SCHEME_DECLARE(g_part_gpt);
134 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
135 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
136 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
137 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
138 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
139 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
140 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
141 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
142 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
143 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
144 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
145 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
146 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
147 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
148 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
149 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
150 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
151 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
152 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
153 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
155 static struct g_part_uuid_alias {
158 } gpt_uuid_alias_match[] = {
159 { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT },
160 { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS },
161 { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL },
162 { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID },
163 { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE },
164 { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY },
165 { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS },
166 { &gpt_uuid_efi, G_PART_ALIAS_EFI },
167 { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD },
168 { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT },
169 { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP },
170 { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS },
171 { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM },
172 { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS },
173 { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA },
174 { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM },
175 { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID },
176 { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP },
177 { &gpt_uuid_mbr, G_PART_ALIAS_MBR },
181 static struct gpt_hdr *
182 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
185 struct gpt_hdr *buf, *hdr;
186 struct g_provider *pp;
192 last = (pp->mediasize / pp->sectorsize) - 1;
193 table->lba[elt] = (elt == GPT_ELT_PRIHDR) ? 1 : last;
194 table->state[elt] = GPT_STATE_MISSING;
195 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
200 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
203 table->state[elt] = GPT_STATE_CORRUPT;
204 sz = le32toh(buf->hdr_size);
205 if (sz < 92 || sz > pp->sectorsize)
208 hdr = g_malloc(sz, M_WAITOK | M_ZERO);
212 crc = le32toh(buf->hdr_crc_self);
213 buf->hdr_crc_self = 0;
214 if (crc32(buf, sz) != crc)
216 hdr->hdr_crc_self = crc;
218 table->state[elt] = GPT_STATE_INVALID;
219 hdr->hdr_revision = le32toh(buf->hdr_revision);
220 if (hdr->hdr_revision < 0x00010000)
222 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
223 if (hdr->hdr_lba_self != table->lba[elt])
225 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
227 /* Check the managed area. */
228 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
229 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
231 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
232 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
235 /* Check the table location and size of the table. */
236 hdr->hdr_entries = le32toh(buf->hdr_entries);
237 hdr->hdr_entsz = le32toh(buf->hdr_entsz);
238 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
239 (hdr->hdr_entsz & 7) != 0)
241 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
242 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
244 if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
245 hdr->hdr_lba_table <= hdr->hdr_lba_end)
247 lba = hdr->hdr_lba_table +
248 (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
252 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
255 table->state[elt] = GPT_STATE_OK;
256 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
257 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
269 static struct gpt_ent *
270 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
271 enum gpt_elt elt, struct gpt_hdr *hdr)
273 struct g_provider *pp;
274 struct gpt_ent *ent, *tbl;
276 unsigned int idx, sectors, tblsz;
283 table->lba[elt] = hdr->hdr_lba_table;
285 table->state[elt] = GPT_STATE_MISSING;
286 tblsz = hdr->hdr_entries * hdr->hdr_entsz;
287 sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize;
288 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize,
289 sectors * pp->sectorsize, &error);
293 table->state[elt] = GPT_STATE_CORRUPT;
294 if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
299 table->state[elt] = GPT_STATE_OK;
300 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
303 for (idx = 0, ent = tbl, p = buf;
304 idx < hdr->hdr_entries;
305 idx++, ent++, p += hdr->hdr_entsz) {
306 le_uuid_dec(p, &ent->ent_type);
307 le_uuid_dec(p + 16, &ent->ent_uuid);
308 ent->ent_lba_start = le64dec(p + 32);
309 ent->ent_lba_end = le64dec(p + 40);
310 ent->ent_attr = le64dec(p + 48);
311 /* Keep UTF-16 in little-endian. */
312 bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
320 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
323 if (pri == NULL || sec == NULL)
326 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
328 return ((pri->hdr_revision == sec->hdr_revision &&
329 pri->hdr_size == sec->hdr_size &&
330 pri->hdr_lba_start == sec->hdr_lba_start &&
331 pri->hdr_lba_end == sec->hdr_lba_end &&
332 pri->hdr_entries == sec->hdr_entries &&
333 pri->hdr_entsz == sec->hdr_entsz &&
334 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
338 gpt_parse_type(const char *type, struct uuid *uuid)
343 struct g_part_uuid_alias *uap;
345 if (type[0] == '!') {
346 error = parse_uuid(type + 1, &tmp);
349 if (EQUUID(&tmp, &gpt_uuid_unused))
354 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
355 alias = g_part_alias_name(uap->alias);
356 if (!strcasecmp(type, alias)) {
365 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
366 struct g_part_parms *gpp)
368 struct g_part_gpt_entry *entry;
371 entry = (struct g_part_gpt_entry *)baseentry;
372 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
375 kern_uuidgen(&entry->ent.ent_uuid, 1);
376 entry->ent.ent_lba_start = baseentry->gpe_start;
377 entry->ent.ent_lba_end = baseentry->gpe_end;
378 if (baseentry->gpe_deleted) {
379 entry->ent.ent_attr = 0;
380 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
382 if (gpp->gpp_parms & G_PART_PARM_LABEL)
383 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
384 sizeof(entry->ent.ent_name));
389 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
391 struct g_part_gpt_table *table;
395 table = (struct g_part_gpt_table *)basetable;
396 bzero(table->mbr, codesz);
397 codesz = MIN(codesz, gpp->gpp_codesize);
399 bcopy(gpp->gpp_codeptr, table->mbr, codesz);
401 /* Mark the PMBR active since some BIOS require it */
402 table->mbr[DOSPARTOFF] = 0x80; /* status */
407 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
409 struct g_provider *pp;
410 struct g_part_gpt_table *table;
414 /* We don't nest, which means that our depth should be 0. */
415 if (basetable->gpt_depth != 0)
418 table = (struct g_part_gpt_table *)basetable;
419 pp = gpp->gpp_provider;
420 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
421 pp->sectorsize - 1) / pp->sectorsize;
422 if (pp->sectorsize < MBRSIZE ||
423 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
427 last = (pp->mediasize / pp->sectorsize) - 1;
429 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
430 table->mbr[DOSPARTOFF + 1] = 0x01; /* shd */
431 table->mbr[DOSPARTOFF + 2] = 0x01; /* ssect */
432 table->mbr[DOSPARTOFF + 3] = 0x00; /* scyl */
433 table->mbr[DOSPARTOFF + 4] = 0xee; /* typ */
434 table->mbr[DOSPARTOFF + 5] = 0xff; /* ehd */
435 table->mbr[DOSPARTOFF + 6] = 0xff; /* esect */
436 table->mbr[DOSPARTOFF + 7] = 0xff; /* ecyl */
437 le32enc(table->mbr + DOSPARTOFF + 8, 1); /* start */
438 le32enc(table->mbr + DOSPARTOFF + 12, MIN(last, 0xffffffffLL));
440 table->lba[GPT_ELT_PRIHDR] = 1;
441 table->lba[GPT_ELT_PRITBL] = 2;
442 table->lba[GPT_ELT_SECHDR] = last;
443 table->lba[GPT_ELT_SECTBL] = last - tblsz;
445 /* Allocate space for the header */
446 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
448 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
449 table->hdr->hdr_revision = GPT_HDR_REVISION;
450 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
451 table->hdr->hdr_lba_start = 2 + tblsz;
452 table->hdr->hdr_lba_end = last - tblsz - 1;
453 kern_uuidgen(&table->hdr->hdr_uuid, 1);
454 table->hdr->hdr_entries = basetable->gpt_entries;
455 table->hdr->hdr_entsz = sizeof(struct gpt_ent);
457 basetable->gpt_first = table->hdr->hdr_lba_start;
458 basetable->gpt_last = table->hdr->hdr_lba_end;
463 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
465 struct g_part_gpt_table *table;
467 table = (struct g_part_gpt_table *)basetable;
468 if (table->hdr != NULL)
473 * Wipe the first 2 sectors as well as the last to clear the
476 basetable->gpt_smhead |= 3;
477 basetable->gpt_smtail |= 1;
482 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
483 struct sbuf *sb, const char *indent)
485 struct g_part_gpt_entry *entry;
487 entry = (struct g_part_gpt_entry *)baseentry;
488 if (indent == NULL) {
489 /* conftxt: libdisk compatibility */
490 sbuf_printf(sb, " xs GPT xt ");
491 sbuf_printf_uuid(sb, &entry->ent.ent_type);
492 } else if (entry != NULL) {
493 /* confxml: partition entry information */
494 sbuf_printf(sb, "%s<label>", indent);
495 g_gpt_printf_utf16(sb, entry->ent.ent_name,
496 sizeof(entry->ent.ent_name) >> 1);
497 sbuf_printf(sb, "</label>\n");
498 sbuf_printf(sb, "%s<rawtype>", indent);
499 sbuf_printf_uuid(sb, &entry->ent.ent_type);
500 sbuf_printf(sb, "</rawtype>\n");
502 /* confxml: scheme information */
507 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
509 struct g_part_gpt_entry *entry;
511 entry = (struct g_part_gpt_entry *)baseentry;
512 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
513 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0);
517 g_part_gpt_modify(struct g_part_table *basetable,
518 struct g_part_entry *baseentry, struct g_part_parms *gpp)
520 struct g_part_gpt_entry *entry;
523 entry = (struct g_part_gpt_entry *)baseentry;
524 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
525 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
529 if (gpp->gpp_parms & G_PART_PARM_LABEL)
530 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
531 sizeof(entry->ent.ent_name));
536 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
537 char *buf, size_t bufsz)
539 struct g_part_gpt_entry *entry;
542 entry = (struct g_part_gpt_entry *)baseentry;
543 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
544 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
549 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
551 struct g_provider *pp;
555 /* We don't nest, which means that our depth should be 0. */
556 if (table->gpt_depth != 0)
562 * Sanity-check the provider. Since the first sector on the provider
563 * must be a PMBR and a PMBR is 512 bytes large, the sector size
564 * must be at least 512 bytes. Also, since the theoretical minimum
565 * number of sectors needed by GPT is 6, any medium that has less
566 * than 6 sectors is never going to be able to hold a GPT. The
567 * number 6 comes from:
568 * 1 sector for the PMBR
569 * 2 sectors for the GPT headers (each 1 sector)
570 * 2 sectors for the GPT tables (each 1 sector)
571 * 1 sector for an actual partition
572 * It's better to catch this pathological case early than behaving
573 * pathologically later on...
575 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
578 /* Check that there's a MBR. */
579 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
582 res = le16dec(buf + DOSMAGICOFFSET);
587 /* Check that there's a primary header. */
588 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
591 res = memcmp(buf, GPT_HDR_SIG, 8);
594 return (G_PART_PROBE_PRI_HIGH);
596 /* No primary? Check that there's a secondary. */
597 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
601 res = memcmp(buf, GPT_HDR_SIG, 8);
603 return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO);
607 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
609 struct gpt_hdr *prihdr, *sechdr;
610 struct gpt_ent *tbl, *pritbl, *sectbl;
611 struct g_provider *pp;
612 struct g_part_gpt_table *table;
613 struct g_part_gpt_entry *entry;
617 table = (struct g_part_gpt_table *)basetable;
621 buf = g_read_data(cp, 0, pp->sectorsize, &error);
624 bcopy(buf, table->mbr, MBRSIZE);
627 /* Read the primary header and table. */
628 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
629 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
630 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
632 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
636 /* Read the secondary header and table. */
637 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
638 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
639 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
641 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
645 /* Fail if we haven't got any good tables at all. */
646 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
647 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
648 printf("GEOM: %s: corrupt or invalid GPT detected.\n",
650 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
656 * If both headers are good but they disagree with each other,
657 * then invalidate one. We prefer to keep the primary header,
658 * unless the primary table is corrupt.
660 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
661 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
662 !gpt_matched_hdrs(prihdr, sechdr)) {
663 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
664 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
665 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
669 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
670 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
676 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
677 printf("GEOM: %s: the primary GPT table is corrupt or "
678 "invalid.\n", pp->name);
679 printf("GEOM: %s: using the secondary instead -- recovery "
680 "strongly advised.\n", pp->name);
688 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
689 printf("GEOM: %s: the secondary GPT table is corrupt "
690 "or invalid.\n", pp->name);
691 printf("GEOM: %s: using the primary only -- recovery "
692 "suggested.\n", pp->name);
702 basetable->gpt_first = table->hdr->hdr_lba_start;
703 basetable->gpt_last = table->hdr->hdr_lba_end;
704 basetable->gpt_entries = table->hdr->hdr_entries;
706 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
707 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
709 entry = (struct g_part_gpt_entry *)g_part_new_entry(basetable,
710 index+1, tbl[index].ent_lba_start, tbl[index].ent_lba_end);
711 entry->ent = tbl[index];
719 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
720 char *buf, size_t bufsz)
722 struct g_part_gpt_entry *entry;
724 struct g_part_uuid_alias *uap;
726 entry = (struct g_part_gpt_entry *)baseentry;
727 type = &entry->ent.ent_type;
728 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
729 if (EQUUID(type, uap->uuid))
730 return (g_part_alias_name(uap->alias));
732 snprintf_uuid(buf + 1, bufsz - 1, type);
738 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
740 unsigned char *buf, *bp;
741 struct g_provider *pp;
742 struct g_part_entry *baseentry;
743 struct g_part_gpt_entry *entry;
744 struct g_part_gpt_table *table;
750 table = (struct g_part_gpt_table *)basetable;
751 tlbsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
752 pp->sectorsize - 1) / pp->sectorsize;
755 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
756 bcopy(table->mbr, buf, MBRSIZE);
757 error = g_write_data(cp, 0, buf, pp->sectorsize);
762 /* Allocate space for the header and entries. */
763 buf = g_malloc((tlbsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
765 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
766 le32enc(buf + 8, table->hdr->hdr_revision);
767 le32enc(buf + 12, table->hdr->hdr_size);
768 le64enc(buf + 40, table->hdr->hdr_lba_start);
769 le64enc(buf + 48, table->hdr->hdr_lba_end);
770 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
771 le32enc(buf + 80, table->hdr->hdr_entries);
772 le32enc(buf + 84, table->hdr->hdr_entsz);
774 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
775 if (baseentry->gpe_deleted)
777 entry = (struct g_part_gpt_entry *)baseentry;
778 index = baseentry->gpe_index - 1;
779 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
780 le_uuid_enc(bp, &entry->ent.ent_type);
781 le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
782 le64enc(bp + 32, entry->ent.ent_lba_start);
783 le64enc(bp + 40, entry->ent.ent_lba_end);
784 le64enc(bp + 48, entry->ent.ent_attr);
785 memcpy(bp + 56, entry->ent.ent_name,
786 sizeof(entry->ent.ent_name));
789 crc = crc32(buf + pp->sectorsize,
790 table->hdr->hdr_entries * table->hdr->hdr_entsz);
791 le32enc(buf + 88, crc);
793 /* Write primary meta-data. */
794 le32enc(buf + 16, 0); /* hdr_crc_self. */
795 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */
796 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */
797 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */
798 crc = crc32(buf, table->hdr->hdr_size);
799 le32enc(buf + 16, crc);
801 error = g_write_data(cp, table->lba[GPT_ELT_PRITBL] * pp->sectorsize,
802 buf + pp->sectorsize, tlbsz * pp->sectorsize);
805 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
806 buf, pp->sectorsize);
810 /* Write secondary meta-data. */
811 le32enc(buf + 16, 0); /* hdr_crc_self. */
812 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */
813 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */
814 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */
815 crc = crc32(buf, table->hdr->hdr_size);
816 le32enc(buf + 16, crc);
818 error = g_write_data(cp, table->lba[GPT_ELT_SECTBL] * pp->sectorsize,
819 buf + pp->sectorsize, tlbsz * pp->sectorsize);
822 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
823 buf, pp->sectorsize);
831 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
837 bo = LITTLE_ENDIAN; /* GPT is little-endian */
838 while (len > 0 && *str != 0) {
839 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
841 if ((ch & 0xf800) == 0xd800) {
843 c = (bo == BIG_ENDIAN) ? be16toh(*str)
848 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
849 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
853 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
854 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
856 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
859 /* Write the Unicode character in UTF-8 */
861 sbuf_printf(sb, "%c", ch);
863 sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6),
865 else if (ch < 0x10000)
866 sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12),
867 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
868 else if (ch < 0x200000)
869 sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18),
870 0x80 | ((ch >> 12) & 0x3f),
871 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
876 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
878 size_t s16idx, s8idx;
880 unsigned int c, utfbytes;
885 bzero(s16, s16len << 1);
886 while (s8[s8idx] != 0 && s16idx < s16len) {
888 if ((c & 0xc0) != 0x80) {
889 /* Initial characters. */
891 /* Incomplete encoding of previous char. */
892 s16[s16idx++] = htole16(0xfffd);
894 if ((c & 0xf8) == 0xf0) {
897 } else if ((c & 0xf0) == 0xe0) {
900 } else if ((c & 0xe0) == 0xc0) {
908 /* Followup characters. */
910 utfchar = (utfchar << 6) + (c & 0x3f);
912 } else if (utfbytes == 0)
916 * Write the complete Unicode character as UTF-16 when we
917 * have all the UTF-8 charactars collected.
921 * If we need to write 2 UTF-16 characters, but
922 * we only have room for 1, then we truncate the
923 * string by writing a 0 instead.
925 if (utfchar >= 0x10000 && s16idx < s16len - 1) {
927 htole16(0xd800 | ((utfchar >> 10) - 0x40));
929 htole16(0xdc00 | (utfchar & 0x3ff));
931 s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
936 * If our input string was truncated, append an invalid encoding
937 * character to the output string.
939 if (utfbytes != 0 && s16idx < s16len)
940 s16[s16idx++] = htole16(0xfffd);