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