2 * Copyright (c) 2002, 2005-2007, 2011 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>
44 #include <sys/sysctl.h>
46 #include <geom/geom.h>
47 #include <geom/part/g_part.h>
49 #include "g_part_if.h"
51 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
53 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
54 CTASSERT(sizeof(struct gpt_ent) == 128);
56 #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0)
69 GPT_STATE_UNKNOWN, /* Not determined. */
70 GPT_STATE_MISSING, /* No signature found. */
71 GPT_STATE_CORRUPT, /* Checksum mismatch. */
72 GPT_STATE_INVALID, /* Nonconformant/invalid. */
73 GPT_STATE_OK /* Perfectly fine. */
76 struct g_part_gpt_table {
77 struct g_part_table base;
80 quad_t lba[GPT_ELT_COUNT];
81 enum gpt_state state[GPT_ELT_COUNT];
85 struct g_part_gpt_entry {
86 struct g_part_entry base;
90 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
91 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
92 static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *);
94 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
95 struct g_part_parms *);
96 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
97 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
98 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
99 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
100 struct sbuf *, const char *);
101 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
102 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
103 struct g_part_parms *);
104 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
106 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
107 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
108 static int g_part_gpt_setunset(struct g_part_table *table,
109 struct g_part_entry *baseentry, const char *attrib, unsigned int set);
110 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
112 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
113 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
114 struct g_part_parms *);
115 static int g_part_gpt_recover(struct g_part_table *);
117 static kobj_method_t g_part_gpt_methods[] = {
118 KOBJMETHOD(g_part_add, g_part_gpt_add),
119 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode),
120 KOBJMETHOD(g_part_create, g_part_gpt_create),
121 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy),
122 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf),
123 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto),
124 KOBJMETHOD(g_part_modify, g_part_gpt_modify),
125 KOBJMETHOD(g_part_resize, g_part_gpt_resize),
126 KOBJMETHOD(g_part_name, g_part_gpt_name),
127 KOBJMETHOD(g_part_probe, g_part_gpt_probe),
128 KOBJMETHOD(g_part_read, g_part_gpt_read),
129 KOBJMETHOD(g_part_recover, g_part_gpt_recover),
130 KOBJMETHOD(g_part_setunset, g_part_gpt_setunset),
131 KOBJMETHOD(g_part_type, g_part_gpt_type),
132 KOBJMETHOD(g_part_write, g_part_gpt_write),
136 static struct g_part_scheme g_part_gpt_scheme = {
139 sizeof(struct g_part_gpt_table),
140 .gps_entrysz = sizeof(struct g_part_gpt_entry),
143 .gps_bootcodesz = MBRSIZE,
145 G_PART_SCHEME_DECLARE(g_part_gpt);
147 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
148 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
149 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
150 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
151 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
152 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
153 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
154 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
155 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
156 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
157 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
158 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
159 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
160 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
161 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
162 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
163 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
164 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
165 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
166 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
167 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
168 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
169 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
170 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
171 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
172 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
173 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
174 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
175 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
176 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
177 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
179 static struct g_part_uuid_alias {
183 } gpt_uuid_alias_match[] = {
184 { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab },
185 { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf },
186 { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 },
187 { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 },
188 { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
189 { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 },
190 { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 },
191 { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 },
192 { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee },
193 { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 },
194 { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 },
195 { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 },
196 { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 },
197 { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 },
198 { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 },
199 { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b },
200 { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 },
201 { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 },
202 { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 },
203 { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 },
204 { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b },
205 { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 },
206 { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 },
207 { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 },
208 { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 },
209 { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 },
210 { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 },
211 { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 },
212 { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 },
213 { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 },
218 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
222 if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
225 mbr += DOSPARTOFF + idx * DOSPARTSIZE;
229 * Treat the PMBR partition specially to maximize
230 * interoperability with BIOSes.
235 mbr[1] = mbr[2] = mbr[3] = 0xff;
237 mbr[5] = mbr[6] = mbr[7] = 0xff;
238 le32enc(mbr + 8, (uint32_t)start);
239 le32enc(mbr + 12, (uint32_t)(end - start + 1));
244 gpt_map_type(struct uuid *t)
246 struct g_part_uuid_alias *uap;
248 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
249 if (EQUUID(t, uap->uuid))
250 return (uap->mbrtype);
256 * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
257 * whole disk anymore. Rather, it covers the GPT table and the EFI
258 * system partition only. This way the HFS+ partition and any FAT
259 * partitions can be added to the MBR without creating an overlap.
262 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
266 p = table->mbr + DOSPARTOFF;
267 if (p[4] != 0xee || le32dec(p + 8) != 1)
274 printf("GEOM: %s: enabling Boot Camp\n", provname);
279 gpt_update_bootcamp(struct g_part_table *basetable)
281 struct g_part_entry *baseentry;
282 struct g_part_gpt_entry *entry;
283 struct g_part_gpt_table *table;
284 int bootable, error, index, slices, typ;
286 table = (struct g_part_gpt_table *)basetable;
289 for (index = 0; index < NDOSPART; index++) {
290 if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
294 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
296 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
297 if (baseentry->gpe_deleted)
299 index = baseentry->gpe_index - 1;
300 if (index >= NDOSPART)
303 entry = (struct g_part_gpt_entry *)baseentry;
306 case 0: /* This must be the EFI system partition. */
307 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
309 error = gpt_write_mbr_entry(table->mbr, index, 0xee,
310 1ull, entry->ent.ent_lba_end);
312 case 1: /* This must be the HFS+ partition. */
313 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
315 error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
316 entry->ent.ent_lba_start, entry->ent.ent_lba_end);
319 typ = gpt_map_type(&entry->ent.ent_type);
320 error = gpt_write_mbr_entry(table->mbr, index, typ,
321 entry->ent.ent_lba_start, entry->ent.ent_lba_end);
327 if (index == bootable)
328 table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
329 slices |= 1 << index;
331 if ((slices & 3) == 3)
336 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
337 gpt_write_mbr_entry(table->mbr, 0, 0xee, 1ull,
338 MIN(table->lba[GPT_ELT_SECHDR], UINT32_MAX));
341 static struct gpt_hdr *
342 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
345 struct gpt_hdr *buf, *hdr;
346 struct g_provider *pp;
352 last = (pp->mediasize / pp->sectorsize) - 1;
353 table->state[elt] = GPT_STATE_MISSING;
355 * If the primary header is valid look for secondary
356 * header in AlternateLBA, otherwise in the last medium's LBA.
358 if (elt == GPT_ELT_SECHDR) {
359 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
360 table->lba[elt] = last;
363 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
368 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
371 table->state[elt] = GPT_STATE_CORRUPT;
372 sz = le32toh(buf->hdr_size);
373 if (sz < 92 || sz > pp->sectorsize)
376 hdr = g_malloc(sz, M_WAITOK | M_ZERO);
380 crc = le32toh(buf->hdr_crc_self);
381 buf->hdr_crc_self = 0;
382 if (crc32(buf, sz) != crc)
384 hdr->hdr_crc_self = crc;
386 table->state[elt] = GPT_STATE_INVALID;
387 hdr->hdr_revision = le32toh(buf->hdr_revision);
388 if (hdr->hdr_revision < GPT_HDR_REVISION)
390 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
391 if (hdr->hdr_lba_self != table->lba[elt])
393 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
394 if (hdr->hdr_lba_alt == hdr->hdr_lba_self ||
395 hdr->hdr_lba_alt > last)
398 /* Check the managed area. */
399 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
400 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
402 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
403 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
406 /* Check the table location and size of the table. */
407 hdr->hdr_entries = le32toh(buf->hdr_entries);
408 hdr->hdr_entsz = le32toh(buf->hdr_entsz);
409 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
410 (hdr->hdr_entsz & 7) != 0)
412 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
413 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
415 if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
416 hdr->hdr_lba_table <= hdr->hdr_lba_end)
418 lba = hdr->hdr_lba_table +
419 (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
423 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
426 table->state[elt] = GPT_STATE_OK;
427 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
428 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
430 /* save LBA for secondary header */
431 if (elt == GPT_ELT_PRIHDR)
432 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
444 static struct gpt_ent *
445 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
446 enum gpt_elt elt, struct gpt_hdr *hdr)
448 struct g_provider *pp;
449 struct gpt_ent *ent, *tbl;
451 unsigned int idx, sectors, tblsz, size;
458 table->lba[elt] = hdr->hdr_lba_table;
460 table->state[elt] = GPT_STATE_MISSING;
461 tblsz = hdr->hdr_entries * hdr->hdr_entsz;
462 sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize;
463 buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
464 for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) {
465 size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS:
466 (sectors - idx) * pp->sectorsize;
467 p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
473 bcopy(p, buf + idx * pp->sectorsize, size);
476 table->state[elt] = GPT_STATE_CORRUPT;
477 if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
482 table->state[elt] = GPT_STATE_OK;
483 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
486 for (idx = 0, ent = tbl, p = buf;
487 idx < hdr->hdr_entries;
488 idx++, ent++, p += hdr->hdr_entsz) {
489 le_uuid_dec(p, &ent->ent_type);
490 le_uuid_dec(p + 16, &ent->ent_uuid);
491 ent->ent_lba_start = le64dec(p + 32);
492 ent->ent_lba_end = le64dec(p + 40);
493 ent->ent_attr = le64dec(p + 48);
494 /* Keep UTF-16 in little-endian. */
495 bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
503 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
506 if (pri == NULL || sec == NULL)
509 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
511 return ((pri->hdr_revision == sec->hdr_revision &&
512 pri->hdr_size == sec->hdr_size &&
513 pri->hdr_lba_start == sec->hdr_lba_start &&
514 pri->hdr_lba_end == sec->hdr_lba_end &&
515 pri->hdr_entries == sec->hdr_entries &&
516 pri->hdr_entsz == sec->hdr_entsz &&
517 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
521 gpt_parse_type(const char *type, struct uuid *uuid)
526 struct g_part_uuid_alias *uap;
528 if (type[0] == '!') {
529 error = parse_uuid(type + 1, &tmp);
532 if (EQUUID(&tmp, &gpt_uuid_unused))
537 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
538 alias = g_part_alias_name(uap->alias);
539 if (!strcasecmp(type, alias)) {
548 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
549 struct g_part_parms *gpp)
551 struct g_part_gpt_entry *entry;
554 entry = (struct g_part_gpt_entry *)baseentry;
555 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
558 kern_uuidgen(&entry->ent.ent_uuid, 1);
559 entry->ent.ent_lba_start = baseentry->gpe_start;
560 entry->ent.ent_lba_end = baseentry->gpe_end;
561 if (baseentry->gpe_deleted) {
562 entry->ent.ent_attr = 0;
563 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
565 if (gpp->gpp_parms & G_PART_PARM_LABEL)
566 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
567 sizeof(entry->ent.ent_name) /
568 sizeof(entry->ent.ent_name[0]));
573 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
575 struct g_part_gpt_table *table;
579 table = (struct g_part_gpt_table *)basetable;
580 bzero(table->mbr, codesz);
581 codesz = MIN(codesz, gpp->gpp_codesize);
583 bcopy(gpp->gpp_codeptr, table->mbr, codesz);
585 /* Mark the PMBR active since some BIOS require it. */
586 if (!table->bootcamp)
587 table->mbr[DOSPARTOFF] = 0x80; /* status */
592 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
594 struct g_provider *pp;
595 struct g_part_gpt_table *table;
599 /* We don't nest, which means that our depth should be 0. */
600 if (basetable->gpt_depth != 0)
603 table = (struct g_part_gpt_table *)basetable;
604 pp = gpp->gpp_provider;
605 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
606 pp->sectorsize - 1) / pp->sectorsize;
607 if (pp->sectorsize < MBRSIZE ||
608 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
612 last = (pp->mediasize / pp->sectorsize) - 1;
614 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
615 gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, MIN(last, UINT32_MAX));
617 /* Allocate space for the header */
618 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
620 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
621 table->hdr->hdr_revision = GPT_HDR_REVISION;
622 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
623 kern_uuidgen(&table->hdr->hdr_uuid, 1);
624 table->hdr->hdr_entries = basetable->gpt_entries;
625 table->hdr->hdr_entsz = sizeof(struct gpt_ent);
627 g_gpt_set_defaults(basetable, pp);
632 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
634 struct g_part_gpt_table *table;
635 struct g_provider *pp;
637 table = (struct g_part_gpt_table *)basetable;
638 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
643 * Wipe the first 2 sectors to clear the partitioning. Wipe the last
644 * sector only if it has valid secondary header.
646 basetable->gpt_smhead |= 3;
647 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
648 table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
649 basetable->gpt_smtail |= 1;
654 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
655 struct sbuf *sb, const char *indent)
657 struct g_part_gpt_entry *entry;
659 entry = (struct g_part_gpt_entry *)baseentry;
660 if (indent == NULL) {
661 /* conftxt: libdisk compatibility */
662 sbuf_printf(sb, " xs GPT xt ");
663 sbuf_printf_uuid(sb, &entry->ent.ent_type);
664 } else if (entry != NULL) {
665 /* confxml: partition entry information */
666 sbuf_printf(sb, "%s<label>", indent);
667 g_gpt_printf_utf16(sb, entry->ent.ent_name,
668 sizeof(entry->ent.ent_name) >> 1);
669 sbuf_printf(sb, "</label>\n");
670 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
671 sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
672 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
673 sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
676 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
677 sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
680 sbuf_printf(sb, "%s<rawtype>", indent);
681 sbuf_printf_uuid(sb, &entry->ent.ent_type);
682 sbuf_printf(sb, "</rawtype>\n");
683 sbuf_printf(sb, "%s<rawuuid>", indent);
684 sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
685 sbuf_printf(sb, "</rawuuid>\n");
687 /* confxml: scheme information */
692 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
694 struct g_part_gpt_entry *entry;
696 entry = (struct g_part_gpt_entry *)baseentry;
697 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
698 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0);
702 g_part_gpt_modify(struct g_part_table *basetable,
703 struct g_part_entry *baseentry, struct g_part_parms *gpp)
705 struct g_part_gpt_entry *entry;
708 entry = (struct g_part_gpt_entry *)baseentry;
709 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
710 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
714 if (gpp->gpp_parms & G_PART_PARM_LABEL)
715 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
716 sizeof(entry->ent.ent_name) /
717 sizeof(entry->ent.ent_name[0]));
722 g_part_gpt_resize(struct g_part_table *basetable,
723 struct g_part_entry *baseentry, struct g_part_parms *gpp)
725 struct g_part_gpt_entry *entry;
726 entry = (struct g_part_gpt_entry *)baseentry;
728 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
729 entry->ent.ent_lba_end = baseentry->gpe_end;
735 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
736 char *buf, size_t bufsz)
738 struct g_part_gpt_entry *entry;
741 entry = (struct g_part_gpt_entry *)baseentry;
742 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
743 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
748 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
750 struct g_provider *pp;
754 /* We don't nest, which means that our depth should be 0. */
755 if (table->gpt_depth != 0)
761 * Sanity-check the provider. Since the first sector on the provider
762 * must be a PMBR and a PMBR is 512 bytes large, the sector size
763 * must be at least 512 bytes. Also, since the theoretical minimum
764 * number of sectors needed by GPT is 6, any medium that has less
765 * than 6 sectors is never going to be able to hold a GPT. The
766 * number 6 comes from:
767 * 1 sector for the PMBR
768 * 2 sectors for the GPT headers (each 1 sector)
769 * 2 sectors for the GPT tables (each 1 sector)
770 * 1 sector for an actual partition
771 * It's better to catch this pathological case early than behaving
772 * pathologically later on...
774 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
777 /* Check that there's a MBR. */
778 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
781 res = le16dec(buf + DOSMAGICOFFSET);
786 /* Check that there's a primary header. */
787 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
790 res = memcmp(buf, GPT_HDR_SIG, 8);
793 return (G_PART_PROBE_PRI_HIGH);
795 /* No primary? Check that there's a secondary. */
796 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
800 res = memcmp(buf, GPT_HDR_SIG, 8);
802 return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO);
806 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
808 struct gpt_hdr *prihdr, *sechdr;
809 struct gpt_ent *tbl, *pritbl, *sectbl;
810 struct g_provider *pp;
811 struct g_part_gpt_table *table;
812 struct g_part_gpt_entry *entry;
817 table = (struct g_part_gpt_table *)basetable;
819 last = (pp->mediasize / pp->sectorsize) - 1;
822 buf = g_read_data(cp, 0, pp->sectorsize, &error);
825 bcopy(buf, table->mbr, MBRSIZE);
828 /* Read the primary header and table. */
829 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
830 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
831 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
833 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
837 /* Read the secondary header and table. */
838 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
839 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
840 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
842 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
846 /* Fail if we haven't got any good tables at all. */
847 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
848 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
849 printf("GEOM: %s: corrupt or invalid GPT detected.\n",
851 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
857 * If both headers are good but they disagree with each other,
858 * then invalidate one. We prefer to keep the primary header,
859 * unless the primary table is corrupt.
861 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
862 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
863 !gpt_matched_hdrs(prihdr, sechdr)) {
864 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
865 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
866 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
870 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
871 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
877 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
878 printf("GEOM: %s: the primary GPT table is corrupt or "
879 "invalid.\n", pp->name);
880 printf("GEOM: %s: using the secondary instead -- recovery "
881 "strongly advised.\n", pp->name);
883 basetable->gpt_corrupt = 1;
890 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
891 printf("GEOM: %s: the secondary GPT table is corrupt "
892 "or invalid.\n", pp->name);
893 printf("GEOM: %s: using the primary only -- recovery "
894 "suggested.\n", pp->name);
895 basetable->gpt_corrupt = 1;
896 } else if (table->lba[GPT_ELT_SECHDR] != last) {
897 printf( "GEOM: %s: the secondary GPT header is not in "
898 "the last LBA.\n", pp->name);
899 basetable->gpt_corrupt = 1;
909 basetable->gpt_first = table->hdr->hdr_lba_start;
910 basetable->gpt_last = table->hdr->hdr_lba_end;
911 basetable->gpt_entries = table->hdr->hdr_entries;
913 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
914 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
916 entry = (struct g_part_gpt_entry *)g_part_new_entry(
917 basetable, index + 1, tbl[index].ent_lba_start,
918 tbl[index].ent_lba_end);
919 entry->ent = tbl[index];
925 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
926 * if (and only if) any FAT32 or FAT16 partitions have been
927 * created. This happens irrespective of whether Boot Camp is
928 * used/enabled, though it's generally understood to be done
929 * to support legacy Windows under Boot Camp. We refer to this
930 * mirroring simply as Boot Camp. We try to detect Boot Camp
931 * so that we can update the MBR if and when GPT changes have
932 * been made. Note that we do not enable Boot Camp if not
933 * previously enabled because we can't assume that we're on a
934 * Mac alongside Mac OS X.
936 table->bootcamp = gpt_is_bootcamp(table, pp->name);
942 g_part_gpt_recover(struct g_part_table *basetable)
945 g_gpt_set_defaults(basetable,
946 LIST_FIRST(&basetable->gpt_gp->consumer)->provider);
947 basetable->gpt_corrupt = 0;
952 g_part_gpt_setunset(struct g_part_table *basetable,
953 struct g_part_entry *baseentry, const char *attrib, unsigned int set)
955 struct g_part_gpt_entry *entry;
956 struct g_part_gpt_table *table;
960 table = (struct g_part_gpt_table *)basetable;
961 entry = (struct g_part_gpt_entry *)baseentry;
963 if (strcasecmp(attrib, "active") == 0) {
964 if (!table->bootcamp || baseentry->gpe_index > NDOSPART)
966 for (i = 0; i < NDOSPART; i++) {
967 table->mbr[DOSPARTOFF + i * DOSPARTSIZE] =
968 (i == baseentry->gpe_index - 1) ? 0x80 : 0;
974 if (strcasecmp(attrib, "bootme") == 0) {
975 attr |= GPT_ENT_ATTR_BOOTME;
976 } else if (strcasecmp(attrib, "bootonce") == 0) {
977 attr |= GPT_ENT_ATTR_BOOTONCE;
979 attr |= GPT_ENT_ATTR_BOOTME;
980 } else if (strcasecmp(attrib, "bootfailed") == 0) {
982 * It should only be possible to unset BOOTFAILED, but it might
983 * be useful for test purposes to also be able to set it.
985 attr |= GPT_ENT_ATTR_BOOTFAILED;
991 attr = entry->ent.ent_attr | attr;
993 attr = entry->ent.ent_attr & ~attr;
994 if (attr != entry->ent.ent_attr) {
995 entry->ent.ent_attr = attr;
996 if (!baseentry->gpe_created)
997 baseentry->gpe_modified = 1;
1003 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1004 char *buf, size_t bufsz)
1006 struct g_part_gpt_entry *entry;
1008 struct g_part_uuid_alias *uap;
1010 entry = (struct g_part_gpt_entry *)baseentry;
1011 type = &entry->ent.ent_type;
1012 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1013 if (EQUUID(type, uap->uuid))
1014 return (g_part_alias_name(uap->alias));
1016 snprintf_uuid(buf + 1, bufsz - 1, type);
1022 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1024 unsigned char *buf, *bp;
1025 struct g_provider *pp;
1026 struct g_part_entry *baseentry;
1027 struct g_part_gpt_entry *entry;
1028 struct g_part_gpt_table *table;
1034 table = (struct g_part_gpt_table *)basetable;
1035 tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
1036 pp->sectorsize - 1) / pp->sectorsize;
1038 /* Reconstruct the MBR from the GPT if under Boot Camp. */
1039 if (table->bootcamp)
1040 gpt_update_bootcamp(basetable);
1042 /* Write the PMBR */
1043 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1044 bcopy(table->mbr, buf, MBRSIZE);
1045 error = g_write_data(cp, 0, buf, pp->sectorsize);
1050 /* Allocate space for the header and entries. */
1051 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1053 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
1054 le32enc(buf + 8, table->hdr->hdr_revision);
1055 le32enc(buf + 12, table->hdr->hdr_size);
1056 le64enc(buf + 40, table->hdr->hdr_lba_start);
1057 le64enc(buf + 48, table->hdr->hdr_lba_end);
1058 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
1059 le32enc(buf + 80, table->hdr->hdr_entries);
1060 le32enc(buf + 84, table->hdr->hdr_entsz);
1062 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1063 if (baseentry->gpe_deleted)
1065 entry = (struct g_part_gpt_entry *)baseentry;
1066 index = baseentry->gpe_index - 1;
1067 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
1068 le_uuid_enc(bp, &entry->ent.ent_type);
1069 le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
1070 le64enc(bp + 32, entry->ent.ent_lba_start);
1071 le64enc(bp + 40, entry->ent.ent_lba_end);
1072 le64enc(bp + 48, entry->ent.ent_attr);
1073 memcpy(bp + 56, entry->ent.ent_name,
1074 sizeof(entry->ent.ent_name));
1077 crc = crc32(buf + pp->sectorsize,
1078 table->hdr->hdr_entries * table->hdr->hdr_entsz);
1079 le32enc(buf + 88, crc);
1081 /* Write primary meta-data. */
1082 le32enc(buf + 16, 0); /* hdr_crc_self. */
1083 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */
1084 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */
1085 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */
1086 crc = crc32(buf, table->hdr->hdr_size);
1087 le32enc(buf + 16, crc);
1089 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
1090 error = g_write_data(cp,
1091 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1092 buf + (index + 1) * pp->sectorsize,
1093 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
1094 (tblsz - index) * pp->sectorsize);
1098 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1099 buf, pp->sectorsize);
1103 /* Write secondary meta-data. */
1104 le32enc(buf + 16, 0); /* hdr_crc_self. */
1105 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */
1106 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */
1107 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */
1108 crc = crc32(buf, table->hdr->hdr_size);
1109 le32enc(buf + 16, crc);
1111 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
1112 error = g_write_data(cp,
1113 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1114 buf + (index + 1) * pp->sectorsize,
1115 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
1116 (tblsz - index) * pp->sectorsize);
1120 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1121 buf, pp->sectorsize);
1129 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
1131 struct g_part_gpt_table *table;
1135 table = (struct g_part_gpt_table *)basetable;
1136 last = pp->mediasize / pp->sectorsize - 1;
1137 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) +
1138 pp->sectorsize - 1) / pp->sectorsize;
1140 table->lba[GPT_ELT_PRIHDR] = 1;
1141 table->lba[GPT_ELT_PRITBL] = 2;
1142 table->lba[GPT_ELT_SECHDR] = last;
1143 table->lba[GPT_ELT_SECTBL] = last - tblsz;
1144 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
1145 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
1146 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
1147 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
1149 table->hdr->hdr_lba_start = 2 + tblsz;
1150 table->hdr->hdr_lba_end = last - tblsz - 1;
1152 basetable->gpt_first = table->hdr->hdr_lba_start;
1153 basetable->gpt_last = table->hdr->hdr_lba_end;
1157 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1163 bo = LITTLE_ENDIAN; /* GPT is little-endian */
1164 while (len > 0 && *str != 0) {
1165 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1167 if ((ch & 0xf800) == 0xd800) {
1169 c = (bo == BIG_ENDIAN) ? be16toh(*str)
1174 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1175 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1179 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1180 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1182 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1185 /* Write the Unicode character in UTF-8 */
1187 sbuf_printf(sb, "%c", ch);
1188 else if (ch < 0x800)
1189 sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6),
1190 0x80 | (ch & 0x3f));
1191 else if (ch < 0x10000)
1192 sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12),
1193 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1194 else if (ch < 0x200000)
1195 sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18),
1196 0x80 | ((ch >> 12) & 0x3f),
1197 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1202 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1204 size_t s16idx, s8idx;
1206 unsigned int c, utfbytes;
1211 bzero(s16, s16len << 1);
1212 while (s8[s8idx] != 0 && s16idx < s16len) {
1214 if ((c & 0xc0) != 0x80) {
1215 /* Initial characters. */
1216 if (utfbytes != 0) {
1217 /* Incomplete encoding of previous char. */
1218 s16[s16idx++] = htole16(0xfffd);
1220 if ((c & 0xf8) == 0xf0) {
1223 } else if ((c & 0xf0) == 0xe0) {
1226 } else if ((c & 0xe0) == 0xc0) {
1234 /* Followup characters. */
1236 utfchar = (utfchar << 6) + (c & 0x3f);
1238 } else if (utfbytes == 0)
1242 * Write the complete Unicode character as UTF-16 when we
1243 * have all the UTF-8 charactars collected.
1245 if (utfbytes == 0) {
1247 * If we need to write 2 UTF-16 characters, but
1248 * we only have room for 1, then we truncate the
1249 * string by writing a 0 instead.
1251 if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1253 htole16(0xd800 | ((utfchar >> 10) - 0x40));
1255 htole16(0xdc00 | (utfchar & 0x3ff));
1257 s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1262 * If our input string was truncated, append an invalid encoding
1263 * character to the output string.
1265 if (utfbytes != 0 && s16idx < s16len)
1266 s16[s16idx++] = htole16(0xfffd);