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_hfs = GPT_ENT_TYPE_APPLE_HFS;
135 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
136 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
137 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
138 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
139 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
140 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
141 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
142 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
143 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
144 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
147 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
148 enum gpt_elt elt, struct gpt_hdr *hdr)
151 struct g_provider *pp;
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,
165 bcopy(buf, hdr, sizeof(*hdr));
166 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0)
169 table->state[elt] = GPT_STATE_CORRUPT;
170 sz = le32toh(hdr->hdr_size);
171 if (sz < 92 || sz > pp->sectorsize)
173 crc = le32toh(hdr->hdr_crc_self);
174 hdr->hdr_crc_self = 0;
175 if (crc32(hdr, sz) != crc)
178 hdr->hdr_crc_self = crc;
180 table->state[elt] = GPT_STATE_INVALID;
181 hdr->hdr_revision = le32toh(hdr->hdr_revision);
182 if (hdr->hdr_revision < 0x00010000)
184 hdr->hdr_lba_self = le64toh(hdr->hdr_lba_self);
185 if (hdr->hdr_lba_self != table->lba[elt])
187 hdr->hdr_lba_alt = le64toh(hdr->hdr_lba_alt);
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)
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)
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)
203 hdr->hdr_lba_table = le64toh(hdr->hdr_lba_table);
204 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
206 if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
207 hdr->hdr_lba_table <= hdr->hdr_lba_end)
209 lba = hdr->hdr_lba_table +
210 (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
214 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
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);
223 static struct gpt_ent *
224 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
225 enum gpt_elt elt, struct gpt_hdr *hdr)
227 struct g_provider *pp;
228 struct gpt_ent *ent, *tbl;
230 unsigned int idx, sectors, tblsz;
234 table->lba[elt] = hdr->hdr_lba_table;
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);
244 table->state[elt] = GPT_STATE_CORRUPT;
245 if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
250 table->state[elt] = GPT_STATE_OK;
251 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
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));
271 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
274 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
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);
286 gpt_parse_type(const char *type, struct uuid *uuid)
292 if (type[0] == '!') {
293 error = parse_uuid(type + 1, &tmp);
296 if (EQUUID(&tmp, &gpt_uuid_unused))
301 alias = g_part_alias_name(G_PART_ALIAS_EFI);
302 if (!strcasecmp(type, alias)) {
303 *uuid = gpt_uuid_efi;
306 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
307 if (!strcasecmp(type, alias)) {
308 *uuid = gpt_uuid_freebsd;
311 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT);
312 if (!strcasecmp(type, alias)) {
313 *uuid = gpt_uuid_freebsd_boot;
316 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
317 if (!strcasecmp(type, alias)) {
318 *uuid = gpt_uuid_freebsd_swap;
321 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
322 if (!strcasecmp(type, alias)) {
323 *uuid = gpt_uuid_freebsd_ufs;
326 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
327 if (!strcasecmp(type, alias)) {
328 *uuid = gpt_uuid_freebsd_vinum;
331 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
332 if (!strcasecmp(type, alias)) {
333 *uuid = gpt_uuid_freebsd_zfs;
336 alias = g_part_alias_name(G_PART_ALIAS_MBR);
337 if (!strcasecmp(type, alias)) {
338 *uuid = gpt_uuid_mbr;
341 alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
342 if (!strcasecmp(type, alias)) {
343 *uuid = gpt_uuid_apple_hfs;
350 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
351 struct g_part_parms *gpp)
353 struct g_part_gpt_entry *entry;
356 entry = (struct g_part_gpt_entry *)baseentry;
357 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
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));
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));
374 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
376 struct g_part_gpt_table *table;
380 table = (struct g_part_gpt_table *)basetable;
381 bzero(table->mbr, codesz);
382 codesz = MIN(codesz, gpp->gpp_codesize);
384 bcopy(gpp->gpp_codeptr, table->mbr, codesz);
389 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
391 struct g_provider *pp;
392 struct g_part_gpt_table *table;
396 /* We don't nest, which means that our depth should be 0. */
397 if (basetable->gpt_depth != 0)
400 table = (struct g_part_gpt_table *)basetable;
401 pp = gpp->gpp_provider;
402 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
403 pp->sectorsize - 1) / pp->sectorsize;
404 if (pp->sectorsize < MBRSIZE ||
405 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
409 last = (pp->mediasize / pp->sectorsize) - 1;
411 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
412 table->mbr[DOSPARTOFF + 1] = 0x01; /* shd */
413 table->mbr[DOSPARTOFF + 2] = 0x01; /* ssect */
414 table->mbr[DOSPARTOFF + 3] = 0x00; /* scyl */
415 table->mbr[DOSPARTOFF + 4] = 0xee; /* typ */
416 table->mbr[DOSPARTOFF + 5] = 0xff; /* ehd */
417 table->mbr[DOSPARTOFF + 6] = 0xff; /* esect */
418 table->mbr[DOSPARTOFF + 7] = 0xff; /* ecyl */
419 le32enc(table->mbr + DOSPARTOFF + 8, 1); /* start */
420 le32enc(table->mbr + DOSPARTOFF + 12, MIN(last, 0xffffffffLL));
422 table->lba[GPT_ELT_PRIHDR] = 1;
423 table->lba[GPT_ELT_PRITBL] = 2;
424 table->lba[GPT_ELT_SECHDR] = last;
425 table->lba[GPT_ELT_SECTBL] = last - tblsz;
427 bcopy(GPT_HDR_SIG, table->hdr.hdr_sig, sizeof(table->hdr.hdr_sig));
428 table->hdr.hdr_revision = GPT_HDR_REVISION;
429 table->hdr.hdr_size = offsetof(struct gpt_hdr, padding);
430 table->hdr.hdr_lba_start = 2 + tblsz;
431 table->hdr.hdr_lba_end = last - tblsz - 1;
432 kern_uuidgen(&table->hdr.hdr_uuid, 1);
433 table->hdr.hdr_entries = basetable->gpt_entries;
434 table->hdr.hdr_entsz = sizeof(struct gpt_ent);
436 basetable->gpt_first = table->hdr.hdr_lba_start;
437 basetable->gpt_last = table->hdr.hdr_lba_end;
442 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
446 * Wipe the first 2 sectors as well as the last to clear the
449 basetable->gpt_smhead |= 3;
450 basetable->gpt_smtail |= 1;
455 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
456 struct sbuf *sb, const char *indent)
458 struct g_part_gpt_entry *entry;
460 entry = (struct g_part_gpt_entry *)baseentry;
461 if (indent == NULL) {
462 /* conftxt: libdisk compatibility */
463 sbuf_printf(sb, " xs GPT xt ");
464 sbuf_printf_uuid(sb, &entry->ent.ent_type);
465 } else if (entry != NULL) {
466 /* confxml: partition entry information */
467 sbuf_printf(sb, "%s<label>", indent);
468 g_gpt_printf_utf16(sb, entry->ent.ent_name,
469 sizeof(entry->ent.ent_name) >> 1);
470 sbuf_printf(sb, "</label>\n");
471 sbuf_printf(sb, "%s<rawtype>", indent);
472 sbuf_printf_uuid(sb, &entry->ent.ent_type);
473 sbuf_printf(sb, "</rawtype>\n");
475 /* confxml: scheme information */
480 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
482 struct g_part_gpt_entry *entry;
484 entry = (struct g_part_gpt_entry *)baseentry;
485 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
486 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0);
490 g_part_gpt_modify(struct g_part_table *basetable,
491 struct g_part_entry *baseentry, struct g_part_parms *gpp)
493 struct g_part_gpt_entry *entry;
496 entry = (struct g_part_gpt_entry *)baseentry;
497 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
498 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
502 if (gpp->gpp_parms & G_PART_PARM_LABEL)
503 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
504 sizeof(entry->ent.ent_name));
509 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
510 char *buf, size_t bufsz)
512 struct g_part_gpt_entry *entry;
515 entry = (struct g_part_gpt_entry *)baseentry;
516 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
517 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
522 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
524 struct g_provider *pp;
528 /* We don't nest, which means that our depth should be 0. */
529 if (table->gpt_depth != 0)
535 * Sanity-check the provider. Since the first sector on the provider
536 * must be a PMBR and a PMBR is 512 bytes large, the sector size
537 * must be at least 512 bytes. Also, since the theoretical minimum
538 * number of sectors needed by GPT is 6, any medium that has less
539 * than 6 sectors is never going to be able to hold a GPT. The
540 * number 6 comes from:
541 * 1 sector for the PMBR
542 * 2 sectors for the GPT headers (each 1 sector)
543 * 2 sectors for the GPT tables (each 1 sector)
544 * 1 sector for an actual partition
545 * It's better to catch this pathological case early than behaving
546 * pathologically later on...
548 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
551 /* Check that there's a MBR. */
552 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
555 res = le16dec(buf + DOSMAGICOFFSET);
560 /* Check that there's a primary header. */
561 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
564 res = memcmp(buf, GPT_HDR_SIG, 8);
567 return (G_PART_PROBE_PRI_HIGH);
569 /* No primary? Check that there's a secondary. */
570 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
574 res = memcmp(buf, GPT_HDR_SIG, 8);
576 return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO);
580 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
582 struct gpt_hdr prihdr, sechdr;
583 struct gpt_ent *tbl, *pritbl, *sectbl;
584 struct g_provider *pp;
585 struct g_part_gpt_table *table;
586 struct g_part_gpt_entry *entry;
590 table = (struct g_part_gpt_table *)basetable;
594 buf = g_read_data(cp, 0, pp->sectorsize, &error);
597 bcopy(buf, table->mbr, MBRSIZE);
600 /* Read the primary header and table. */
601 gpt_read_hdr(table, cp, GPT_ELT_PRIHDR, &prihdr);
602 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
603 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, &prihdr);
605 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
609 /* Read the secondary header and table. */
610 gpt_read_hdr(table, cp, GPT_ELT_SECHDR, &sechdr);
611 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
612 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, &sechdr);
614 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
618 /* Fail if we haven't got any good tables at all. */
619 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
620 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
621 printf("GEOM: %s: corrupt or invalid GPT detected.\n",
623 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
629 * If both headers are good but they disagree with each other,
630 * then invalidate one. We prefer to keep the primary header,
631 * unless the primary table is corrupt.
633 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
634 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
635 !gpt_matched_hdrs(&prihdr, &sechdr)) {
636 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
637 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
638 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
640 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
641 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
645 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
646 printf("GEOM: %s: the primary GPT table is corrupt or "
647 "invalid.\n", pp->name);
648 printf("GEOM: %s: using the secondary instead -- recovery "
649 "strongly advised.\n", pp->name);
655 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
656 printf("GEOM: %s: the secondary GPT table is corrupt "
657 "or invalid.\n", pp->name);
658 printf("GEOM: %s: using the primary only -- recovery "
659 "suggested.\n", pp->name);
667 basetable->gpt_first = table->hdr.hdr_lba_start;
668 basetable->gpt_last = table->hdr.hdr_lba_end;
669 basetable->gpt_entries = table->hdr.hdr_entries;
671 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
672 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
674 entry = (struct g_part_gpt_entry *)g_part_new_entry(basetable,
675 index+1, tbl[index].ent_lba_start, tbl[index].ent_lba_end);
676 entry->ent = tbl[index];
684 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
685 char *buf, size_t bufsz)
687 struct g_part_gpt_entry *entry;
690 entry = (struct g_part_gpt_entry *)baseentry;
691 type = &entry->ent.ent_type;
692 if (EQUUID(type, &gpt_uuid_efi))
693 return (g_part_alias_name(G_PART_ALIAS_EFI));
694 if (EQUUID(type, &gpt_uuid_freebsd))
695 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
696 if (EQUUID(type, &gpt_uuid_freebsd_boot))
697 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT));
698 if (EQUUID(type, &gpt_uuid_freebsd_swap))
699 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
700 if (EQUUID(type, &gpt_uuid_freebsd_ufs))
701 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
702 if (EQUUID(type, &gpt_uuid_freebsd_vinum))
703 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
704 if (EQUUID(type, &gpt_uuid_freebsd_zfs))
705 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
706 if (EQUUID(type, &gpt_uuid_mbr))
707 return (g_part_alias_name(G_PART_ALIAS_MBR));
709 snprintf_uuid(buf + 1, bufsz - 1, type);
714 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
716 unsigned char *buf, *bp;
717 struct g_provider *pp;
718 struct g_part_entry *baseentry;
719 struct g_part_gpt_entry *entry;
720 struct g_part_gpt_table *table;
726 table = (struct g_part_gpt_table *)basetable;
727 tlbsz = (table->hdr.hdr_entries * table->hdr.hdr_entsz +
728 pp->sectorsize - 1) / pp->sectorsize;
731 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
732 bcopy(table->mbr, buf, MBRSIZE);
733 error = g_write_data(cp, 0, buf, pp->sectorsize);
738 /* Allocate space for the header and entries. */
739 buf = g_malloc((tlbsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
741 memcpy(buf, table->hdr.hdr_sig, sizeof(table->hdr.hdr_sig));
742 le32enc(buf + 8, table->hdr.hdr_revision);
743 le32enc(buf + 12, table->hdr.hdr_size);
744 le64enc(buf + 40, table->hdr.hdr_lba_start);
745 le64enc(buf + 48, table->hdr.hdr_lba_end);
746 le_uuid_enc(buf + 56, &table->hdr.hdr_uuid);
747 le32enc(buf + 80, table->hdr.hdr_entries);
748 le32enc(buf + 84, table->hdr.hdr_entsz);
750 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
751 if (baseentry->gpe_deleted)
753 entry = (struct g_part_gpt_entry *)baseentry;
754 index = baseentry->gpe_index - 1;
755 bp = buf + pp->sectorsize + table->hdr.hdr_entsz * index;
756 le_uuid_enc(bp, &entry->ent.ent_type);
757 le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
758 le64enc(bp + 32, entry->ent.ent_lba_start);
759 le64enc(bp + 40, entry->ent.ent_lba_end);
760 le64enc(bp + 48, entry->ent.ent_attr);
761 memcpy(bp + 56, entry->ent.ent_name,
762 sizeof(entry->ent.ent_name));
765 crc = crc32(buf + pp->sectorsize,
766 table->hdr.hdr_entries * table->hdr.hdr_entsz);
767 le32enc(buf + 88, crc);
769 /* Write primary meta-data. */
770 le32enc(buf + 16, 0); /* hdr_crc_self. */
771 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */
772 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */
773 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */
774 crc = crc32(buf, table->hdr.hdr_size);
775 le32enc(buf + 16, crc);
777 error = g_write_data(cp, table->lba[GPT_ELT_PRITBL] * pp->sectorsize,
778 buf + pp->sectorsize, tlbsz * pp->sectorsize);
781 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
782 buf, pp->sectorsize);
786 /* Write secondary meta-data. */
787 le32enc(buf + 16, 0); /* hdr_crc_self. */
788 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */
789 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */
790 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */
791 crc = crc32(buf, table->hdr.hdr_size);
792 le32enc(buf + 16, crc);
794 error = g_write_data(cp, table->lba[GPT_ELT_SECTBL] * pp->sectorsize,
795 buf + pp->sectorsize, tlbsz * pp->sectorsize);
798 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
799 buf, pp->sectorsize);
807 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
813 bo = LITTLE_ENDIAN; /* GPT is little-endian */
814 while (len > 0 && *str != 0) {
815 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
817 if ((ch & 0xf800) == 0xd800) {
819 c = (bo == BIG_ENDIAN) ? be16toh(*str)
824 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
825 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
829 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
830 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
832 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
835 /* Write the Unicode character in UTF-8 */
837 sbuf_printf(sb, "%c", ch);
839 sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6),
841 else if (ch < 0x10000)
842 sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12),
843 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
844 else if (ch < 0x200000)
845 sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18),
846 0x80 | ((ch >> 12) & 0x3f),
847 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
852 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
854 size_t s16idx, s8idx;
856 unsigned int c, utfbytes;
861 bzero(s16, s16len << 1);
862 while (s8[s8idx] != 0 && s16idx < s16len) {
864 if ((c & 0xc0) != 0x80) {
865 /* Initial characters. */
867 /* Incomplete encoding of previous char. */
868 s16[s16idx++] = htole16(0xfffd);
870 if ((c & 0xf8) == 0xf0) {
873 } else if ((c & 0xf0) == 0xe0) {
876 } else if ((c & 0xe0) == 0xc0) {
884 /* Followup characters. */
886 utfchar = (utfchar << 6) + (c & 0x3f);
888 } else if (utfbytes == 0)
892 * Write the complete Unicode character as UTF-16 when we
893 * have all the UTF-8 charactars collected.
897 * If we need to write 2 UTF-16 characters, but
898 * we only have room for 1, then we truncate the
899 * string by writing a 0 instead.
901 if (utfchar >= 0x10000 && s16idx < s16len - 1) {
903 htole16(0xd800 | ((utfchar >> 10) - 0x40));
905 htole16(0xdc00 | (utfchar & 0x3ff));
907 s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
912 * If our input string was truncated, append an invalid encoding
913 * character to the output string.
915 if (utfbytes != 0 && s16idx < s16len)
916 s16[s16idx++] = htole16(0xfffd);