]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/geom/part/g_part_gpt.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / geom / part / g_part_gpt.c
1 /*-
2  * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/gpt.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/sbuf.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/uuid.h>
46 #include <geom/geom.h>
47 #include <geom/part/g_part.h>
48
49 #include "g_part_if.h"
50
51 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
52
53 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
54 CTASSERT(sizeof(struct gpt_ent) == 128);
55
56 #define EQUUID(a,b)     (memcmp(a, b, sizeof(struct uuid)) == 0)
57
58 #define MBRSIZE         512
59
60 enum gpt_elt {
61         GPT_ELT_PRIHDR,
62         GPT_ELT_PRITBL,
63         GPT_ELT_SECHDR,
64         GPT_ELT_SECTBL,
65         GPT_ELT_COUNT
66 };
67
68 enum gpt_state {
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. */
74 };
75
76 struct g_part_gpt_table {
77         struct g_part_table     base;
78         u_char                  mbr[MBRSIZE];
79         struct gpt_hdr          *hdr;
80         quad_t                  lba[GPT_ELT_COUNT];
81         enum gpt_state          state[GPT_ELT_COUNT];
82         int                     bootcamp;
83 };
84
85 struct g_part_gpt_entry {
86         struct g_part_entry     base;
87         struct gpt_ent          ent;
88 };
89
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 *);
93
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 *,
105     char *, size_t);
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 *,
111     char *, size_t);
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 *);
116
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),
133         { 0, 0 }
134 };
135
136 static struct g_part_scheme g_part_gpt_scheme = {
137         "GPT",
138         g_part_gpt_methods,
139         sizeof(struct g_part_gpt_table),
140         .gps_entrysz = sizeof(struct g_part_gpt_entry),
141         .gps_minent = 128,
142         .gps_maxent = 4096,
143         .gps_bootcodesz = MBRSIZE,
144 };
145 G_PART_SCHEME_DECLARE(g_part_gpt);
146
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;
178
179 static struct g_part_uuid_alias {
180         struct uuid *uuid;
181         int alias;
182         int mbrtype;
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 },
214         { NULL, 0, 0 }
215 };
216
217 static int
218 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
219     quad_t end)
220 {
221
222         if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
223                 return (EINVAL);
224
225         mbr += DOSPARTOFF + idx * DOSPARTSIZE;
226         mbr[0] = 0;
227         if (start == 1) {
228                 /*
229                  * Treat the PMBR partition specially to maximize
230                  * interoperability with BIOSes.
231                  */
232                 mbr[1] = mbr[3] = 0;
233                 mbr[2] = 2;
234         } else
235                 mbr[1] = mbr[2] = mbr[3] = 0xff;
236         mbr[4] = typ;
237         mbr[5] = mbr[6] = mbr[7] = 0xff;
238         le32enc(mbr + 8, (uint32_t)start);
239         le32enc(mbr + 12, (uint32_t)(end - start + 1));
240         return (0);
241 }
242
243 static int
244 gpt_map_type(struct uuid *t)
245 {
246         struct g_part_uuid_alias *uap;
247
248         for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
249                 if (EQUUID(t, uap->uuid))
250                         return (uap->mbrtype);
251         }
252         return (0);
253 }
254
255 /*
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.
260  */
261 static int
262 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
263 {
264         uint8_t *p;
265
266         p = table->mbr + DOSPARTOFF;
267         if (p[4] != 0xee || le32dec(p + 8) != 1)
268                 return (0);
269
270         p += DOSPARTSIZE;
271         if (p[4] != 0xaf)
272                 return (0);
273
274         printf("GEOM: %s: enabling Boot Camp\n", provname);
275         return (1);
276 }
277
278 static void
279 gpt_update_bootcamp(struct g_part_table *basetable)
280 {
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;
285
286         table = (struct g_part_gpt_table *)basetable;
287
288         bootable = -1;
289         for (index = 0; index < NDOSPART; index++) {
290                 if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
291                         bootable = index;
292         }
293
294         bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
295         slices = 0;
296         LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
297                 if (baseentry->gpe_deleted)
298                         continue;
299                 index = baseentry->gpe_index - 1;
300                 if (index >= NDOSPART)
301                         continue;
302
303                 entry = (struct g_part_gpt_entry *)baseentry;
304
305                 switch (index) {
306                 case 0: /* This must be the EFI system partition. */
307                         if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
308                                 goto disable;
309                         error = gpt_write_mbr_entry(table->mbr, index, 0xee,
310                             1ull, entry->ent.ent_lba_end);
311                         break;
312                 case 1: /* This must be the HFS+ partition. */
313                         if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
314                                 goto disable;
315                         error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
316                             entry->ent.ent_lba_start, entry->ent.ent_lba_end);
317                         break;
318                 default:
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);
322                         break;
323                 }
324                 if (error)
325                         continue;
326
327                 if (index == bootable)
328                         table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
329                 slices |= 1 << index;
330         }
331         if ((slices & 3) == 3)
332                 return;
333
334  disable:
335         table->bootcamp = 0;
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));
339 }
340
341 static struct gpt_hdr *
342 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
343     enum gpt_elt elt)
344 {
345         struct gpt_hdr *buf, *hdr;
346         struct g_provider *pp;
347         quad_t lba, last;
348         int error;
349         uint32_t crc, sz;
350
351         pp = cp->provider;
352         last = (pp->mediasize / pp->sectorsize) - 1;
353         table->state[elt] = GPT_STATE_MISSING;
354         /*
355          * If the primary header is valid look for secondary
356          * header in AlternateLBA, otherwise in the last medium's LBA.
357          */
358         if (elt == GPT_ELT_SECHDR) {
359                 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
360                         table->lba[elt] = last;
361         } else
362                 table->lba[elt] = 1;
363         buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
364             &error);
365         if (buf == NULL)
366                 return (NULL);
367         hdr = NULL;
368         if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
369                 goto fail;
370
371         table->state[elt] = GPT_STATE_CORRUPT;
372         sz = le32toh(buf->hdr_size);
373         if (sz < 92 || sz > pp->sectorsize)
374                 goto fail;
375
376         hdr = g_malloc(sz, M_WAITOK | M_ZERO);
377         bcopy(buf, hdr, sz);
378         hdr->hdr_size = sz;
379
380         crc = le32toh(buf->hdr_crc_self);
381         buf->hdr_crc_self = 0;
382         if (crc32(buf, sz) != crc)
383                 goto fail;
384         hdr->hdr_crc_self = crc;
385
386         table->state[elt] = GPT_STATE_INVALID;
387         hdr->hdr_revision = le32toh(buf->hdr_revision);
388         if (hdr->hdr_revision < GPT_HDR_REVISION)
389                 goto fail;
390         hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
391         if (hdr->hdr_lba_self != table->lba[elt])
392                 goto fail;
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)
396                 goto fail;
397
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)
401                 goto fail;
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)
404                 goto fail;
405
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)
411                 goto fail;
412         hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
413         if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
414                 goto fail;
415         if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
416             hdr->hdr_lba_table <= hdr->hdr_lba_end)
417                 goto fail;
418         lba = hdr->hdr_lba_table +
419             (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) /
420             pp->sectorsize - 1;
421         if (lba >= last)
422                 goto fail;
423         if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
424                 goto fail;
425
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);
429
430         /* save LBA for secondary header */
431         if (elt == GPT_ELT_PRIHDR)
432                 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
433
434         g_free(buf);
435         return (hdr);
436
437  fail:
438         if (hdr != NULL)
439                 g_free(hdr);
440         g_free(buf);
441         return (NULL);
442 }
443
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)
447 {
448         struct g_provider *pp;
449         struct gpt_ent *ent, *tbl;
450         char *buf, *p;
451         unsigned int idx, sectors, tblsz, size;
452         int error;
453
454         if (hdr == NULL)
455                 return (NULL);
456
457         pp = cp->provider;
458         table->lba[elt] = hdr->hdr_lba_table;
459
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,
468                     size, &error);
469                 if (p == NULL) {
470                         g_free(buf);
471                         return (NULL);
472                 }
473                 bcopy(p, buf + idx * pp->sectorsize, size);
474                 g_free(p);
475         }
476         table->state[elt] = GPT_STATE_CORRUPT;
477         if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
478                 g_free(buf);
479                 return (NULL);
480         }
481
482         table->state[elt] = GPT_STATE_OK;
483         tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
484             M_WAITOK | M_ZERO);
485
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));
496         }
497
498         g_free(buf);
499         return (tbl);
500 }
501
502 static int
503 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
504 {
505
506         if (pri == NULL || sec == NULL)
507                 return (0);
508
509         if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
510                 return (0);
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);
518 }
519
520 static int
521 gpt_parse_type(const char *type, struct uuid *uuid)
522 {
523         struct uuid tmp;
524         const char *alias;
525         int error;
526         struct g_part_uuid_alias *uap;
527
528         if (type[0] == '!') {
529                 error = parse_uuid(type + 1, &tmp);
530                 if (error)
531                         return (error);
532                 if (EQUUID(&tmp, &gpt_uuid_unused))
533                         return (EINVAL);
534                 *uuid = tmp;
535                 return (0);
536         }
537         for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
538                 alias = g_part_alias_name(uap->alias);
539                 if (!strcasecmp(type, alias)) {
540                         *uuid = *uap->uuid;
541                         return (0);
542                 }
543         }
544         return (EINVAL);
545 }
546
547 static int
548 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
549     struct g_part_parms *gpp)
550 {
551         struct g_part_gpt_entry *entry;
552         int error;
553
554         entry = (struct g_part_gpt_entry *)baseentry;
555         error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
556         if (error)
557                 return (error);
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));
564         }
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]));
569         return (0);
570 }
571
572 static int
573 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
574 {
575         struct g_part_gpt_table *table;
576         size_t codesz;
577
578         codesz = DOSPARTOFF;
579         table = (struct g_part_gpt_table *)basetable;
580         bzero(table->mbr, codesz);
581         codesz = MIN(codesz, gpp->gpp_codesize);
582         if (codesz > 0)
583                 bcopy(gpp->gpp_codeptr, table->mbr, codesz);
584
585         /* Mark the PMBR active since some BIOS require it. */
586         if (!table->bootcamp)
587                 table->mbr[DOSPARTOFF] = 0x80;          /* status */
588         return (0);
589 }
590
591 static int
592 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
593 {
594         struct g_provider *pp;
595         struct g_part_gpt_table *table;
596         quad_t last;
597         size_t tblsz;
598
599         /* We don't nest, which means that our depth should be 0. */
600         if (basetable->gpt_depth != 0)
601                 return (ENXIO);
602
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) *
609             pp->sectorsize)
610                 return (ENOSPC);
611
612         last = (pp->mediasize / pp->sectorsize) - 1;
613
614         le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
615         gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, MIN(last, UINT32_MAX));
616
617         /* Allocate space for the header */
618         table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
619
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);
626
627         g_gpt_set_defaults(basetable, pp);
628         return (0);
629 }
630
631 static int
632 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
633 {
634         struct g_part_gpt_table *table;
635         struct g_provider *pp;
636
637         table = (struct g_part_gpt_table *)basetable;
638         pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
639         g_free(table->hdr);
640         table->hdr = NULL;
641
642         /*
643          * Wipe the first 2 sectors to clear the partitioning. Wipe the last
644          * sector only if it has valid secondary header.
645          */
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;
650         return (0);
651 }
652
653 static void
654 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 
655     struct sbuf *sb, const char *indent)
656 {
657         struct g_part_gpt_entry *entry;
658  
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",
674                             indent);
675                 }
676                 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
677                         sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
678                             indent);
679                 }
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");
686         } else {
687                 /* confxml: scheme information */
688         }
689 }
690
691 static int
692 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)  
693 {
694         struct g_part_gpt_entry *entry;
695
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);
699 }
700
701 static int
702 g_part_gpt_modify(struct g_part_table *basetable,
703     struct g_part_entry *baseentry, struct g_part_parms *gpp)
704 {
705         struct g_part_gpt_entry *entry;
706         int error;
707
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);
711                 if (error)
712                         return (error);
713         }
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]));
718         return (0);
719 }
720
721 static int
722 g_part_gpt_resize(struct g_part_table *basetable,
723     struct g_part_entry *baseentry, struct g_part_parms *gpp)
724 {
725         struct g_part_gpt_entry *entry;
726         entry = (struct g_part_gpt_entry *)baseentry;
727
728         baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
729         entry->ent.ent_lba_end = baseentry->gpe_end;
730
731         return (0);
732 }
733
734 static const char *
735 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
736     char *buf, size_t bufsz)
737 {
738         struct g_part_gpt_entry *entry;
739         char c;
740
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);
744         return (buf);
745 }
746
747 static int
748 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
749 {
750         struct g_provider *pp;
751         char *buf;
752         int error, res;
753
754         /* We don't nest, which means that our depth should be 0. */
755         if (table->gpt_depth != 0)
756                 return (ENXIO);
757
758         pp = cp->provider;
759
760         /*
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...
773          */
774         if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
775                 return (ENOSPC);
776
777         /* Check that there's a MBR. */
778         buf = g_read_data(cp, 0L, pp->sectorsize, &error);
779         if (buf == NULL)
780                 return (error);
781         res = le16dec(buf + DOSMAGICOFFSET);
782         g_free(buf);
783         if (res != DOSMAGIC) 
784                 return (ENXIO);
785
786         /* Check that there's a primary header. */
787         buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
788         if (buf == NULL)
789                 return (error);
790         res = memcmp(buf, GPT_HDR_SIG, 8);
791         g_free(buf);
792         if (res == 0)
793                 return (G_PART_PROBE_PRI_HIGH);
794
795         /* No primary? Check that there's a secondary. */
796         buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
797             &error);
798         if (buf == NULL)
799                 return (error);
800         res = memcmp(buf, GPT_HDR_SIG, 8); 
801         g_free(buf);
802         return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO);
803 }
804
805 static int
806 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
807 {
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;
813         u_char *buf;
814         uint64_t last;
815         int error, index;
816
817         table = (struct g_part_gpt_table *)basetable;
818         pp = cp->provider;
819         last = (pp->mediasize / pp->sectorsize) - 1;
820
821         /* Read the PMBR */
822         buf = g_read_data(cp, 0, pp->sectorsize, &error);
823         if (buf == NULL)
824                 return (error);
825         bcopy(buf, table->mbr, MBRSIZE);
826         g_free(buf);
827
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);
832         } else {
833                 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
834                 pritbl = NULL;
835         }
836
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);
841         } else {
842                 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
843                 sectbl = NULL;
844         }
845
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",
850                     pp->name);
851                 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
852                     pp->name);
853                 return (EINVAL);
854         }
855
856         /*
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.
860          */
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;
867                         g_free(sechdr);
868                         sechdr = NULL;
869                 } else {
870                         table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
871                         table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
872                         g_free(prihdr);
873                         prihdr = NULL;
874                 }
875         }
876
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);
882                 table->hdr = sechdr;
883                 basetable->gpt_corrupt = 1;
884                 if (prihdr != NULL)
885                         g_free(prihdr);
886                 tbl = sectbl;
887                 if (pritbl != NULL)
888                         g_free(pritbl);
889         } else {
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;
900                 }
901                 table->hdr = prihdr;
902                 if (sechdr != NULL)
903                         g_free(sechdr);
904                 tbl = pritbl;
905                 if (sectbl != NULL)
906                         g_free(sectbl);
907         }
908
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;
912
913         for (index = basetable->gpt_entries - 1; index >= 0; index--) {
914                 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
915                         continue;
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];
920         }
921
922         g_free(tbl);
923
924         /*
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.
935          */
936         table->bootcamp = gpt_is_bootcamp(table, pp->name);
937
938         return (0);
939 }
940
941 static int
942 g_part_gpt_recover(struct g_part_table *basetable)
943 {
944
945         g_gpt_set_defaults(basetable,
946             LIST_FIRST(&basetable->gpt_gp->consumer)->provider);
947         basetable->gpt_corrupt = 0;
948         return (0);
949 }
950
951 static int
952 g_part_gpt_setunset(struct g_part_table *basetable,
953     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
954 {
955         struct g_part_gpt_entry *entry;
956         struct g_part_gpt_table *table;
957         uint64_t attr;
958         int i;
959
960         table = (struct g_part_gpt_table *)basetable;
961         entry = (struct g_part_gpt_entry *)baseentry;
962
963         if (strcasecmp(attrib, "active") == 0) {
964                 if (!table->bootcamp || baseentry->gpe_index > NDOSPART)
965                         return (EINVAL);
966                 for (i = 0; i < NDOSPART; i++) {
967                         table->mbr[DOSPARTOFF + i * DOSPARTSIZE] =
968                             (i == baseentry->gpe_index - 1) ? 0x80 : 0;
969                 }
970                 return (0);
971         }
972
973         attr = 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;
978                 if (set)
979                         attr |= GPT_ENT_ATTR_BOOTME;
980         } else if (strcasecmp(attrib, "bootfailed") == 0) {
981                 /*
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.
984                  */
985                 attr |= GPT_ENT_ATTR_BOOTFAILED;
986         }
987         if (attr == 0)
988                 return (EINVAL);
989
990         if (set)
991                 attr = entry->ent.ent_attr | attr;
992         else
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;
998         }
999         return (0);
1000 }
1001
1002 static const char *
1003 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 
1004     char *buf, size_t bufsz)
1005 {
1006         struct g_part_gpt_entry *entry;
1007         struct uuid *type;
1008         struct g_part_uuid_alias *uap;
1009  
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));
1015         buf[0] = '!';
1016         snprintf_uuid(buf + 1, bufsz - 1, type);
1017
1018         return (buf);
1019 }
1020
1021 static int
1022 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1023 {
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;
1029         size_t tblsz;
1030         uint32_t crc;
1031         int error, index;
1032
1033         pp = cp->provider;
1034         table = (struct g_part_gpt_table *)basetable;
1035         tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz +
1036             pp->sectorsize - 1) / pp->sectorsize;
1037
1038         /* Reconstruct the MBR from the GPT if under Boot Camp. */
1039         if (table->bootcamp)
1040                 gpt_update_bootcamp(basetable);
1041
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);
1046         g_free(buf);
1047         if (error)
1048                 return (error);
1049
1050         /* Allocate space for the header and entries. */
1051         buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1052
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);
1061
1062         LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1063                 if (baseentry->gpe_deleted)
1064                         continue;
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));
1075         }
1076
1077         crc = crc32(buf + pp->sectorsize,
1078             table->hdr->hdr_entries * table->hdr->hdr_entsz);
1079         le32enc(buf + 88, crc);
1080
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);
1088
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);
1095                 if (error)
1096                         goto out;
1097         }
1098         error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1099             buf, pp->sectorsize);
1100         if (error)
1101                 goto out;
1102
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);
1110
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);
1117                 if (error)
1118                         goto out;
1119         }
1120         error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1121             buf, pp->sectorsize);
1122
1123  out:
1124         g_free(buf);
1125         return (error);
1126 }
1127
1128 static void
1129 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
1130 {
1131         struct g_part_gpt_table *table;
1132         quad_t last;
1133         size_t tblsz;
1134
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;
1139
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;
1148
1149         table->hdr->hdr_lba_start = 2 + tblsz;
1150         table->hdr->hdr_lba_end = last - tblsz - 1;
1151
1152         basetable->gpt_first = table->hdr->hdr_lba_start;
1153         basetable->gpt_last = table->hdr->hdr_lba_end;
1154 }
1155
1156 static void
1157 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1158 {
1159         u_int bo;
1160         uint32_t ch;
1161         uint16_t c;
1162
1163         bo = LITTLE_ENDIAN;     /* GPT is little-endian */
1164         while (len > 0 && *str != 0) {
1165                 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1166                 str++, len--;
1167                 if ((ch & 0xf800) == 0xd800) {
1168                         if (len > 0) {
1169                                 c = (bo == BIG_ENDIAN) ? be16toh(*str)
1170                                     : le16toh(*str);
1171                                 str++, len--;
1172                         } else
1173                                 c = 0xfffd;
1174                         if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1175                                 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1176                                 ch += 0x10000;
1177                         } else
1178                                 ch = 0xfffd;
1179                 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1180                         bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1181                         continue;
1182                 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1183                         continue;
1184
1185                 /* Write the Unicode character in UTF-8 */
1186                 if (ch < 0x80)
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));
1198         }
1199 }
1200
1201 static void
1202 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1203 {
1204         size_t s16idx, s8idx;
1205         uint32_t utfchar;
1206         unsigned int c, utfbytes;
1207
1208         s8idx = s16idx = 0;
1209         utfchar = 0;
1210         utfbytes = 0;
1211         bzero(s16, s16len << 1);
1212         while (s8[s8idx] != 0 && s16idx < s16len) {
1213                 c = s8[s8idx++];
1214                 if ((c & 0xc0) != 0x80) {
1215                         /* Initial characters. */
1216                         if (utfbytes != 0) {
1217                                 /* Incomplete encoding of previous char. */
1218                                 s16[s16idx++] = htole16(0xfffd);
1219                         }
1220                         if ((c & 0xf8) == 0xf0) {
1221                                 utfchar = c & 0x07;
1222                                 utfbytes = 3;
1223                         } else if ((c & 0xf0) == 0xe0) {
1224                                 utfchar = c & 0x0f;
1225                                 utfbytes = 2;
1226                         } else if ((c & 0xe0) == 0xc0) {
1227                                 utfchar = c & 0x1f;
1228                                 utfbytes = 1;
1229                         } else {
1230                                 utfchar = c & 0x7f;
1231                                 utfbytes = 0;
1232                         }
1233                 } else {
1234                         /* Followup characters. */
1235                         if (utfbytes > 0) {
1236                                 utfchar = (utfchar << 6) + (c & 0x3f);
1237                                 utfbytes--;
1238                         } else if (utfbytes == 0)
1239                                 utfbytes = ~0;
1240                 }
1241                 /*
1242                  * Write the complete Unicode character as UTF-16 when we
1243                  * have all the UTF-8 charactars collected.
1244                  */
1245                 if (utfbytes == 0) {
1246                         /*
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.
1250                          */
1251                         if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1252                                 s16[s16idx++] =
1253                                     htole16(0xd800 | ((utfchar >> 10) - 0x40));
1254                                 s16[s16idx++] =
1255                                     htole16(0xdc00 | (utfchar & 0x3ff));
1256                         } else
1257                                 s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1258                                     htole16(utfchar);
1259                 }
1260         }
1261         /*
1262          * If our input string was truncated, append an invalid encoding
1263          * character to the output string.
1264          */
1265         if (utfbytes != 0 && s16idx < s16len)
1266                 s16[s16idx++] = htole16(0xfffd);
1267 }