g_part_gpt.c revision 179751
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: head/sys/geom/part/g_part_gpt.c 179751 2008-06-12 05:28:47Z marcel $"); 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 50CTASSERT(offsetof(struct gpt_hdr, padding) == 92); 51CTASSERT(sizeof(struct gpt_ent) == 128); 52 53#define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 54 55#define MBRSIZE 512 56 57enum gpt_elt { 58 GPT_ELT_PRIHDR, 59 GPT_ELT_PRITBL, 60 GPT_ELT_SECHDR, 61 GPT_ELT_SECTBL, 62 GPT_ELT_COUNT 63}; 64 65enum 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 73struct 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 81struct g_part_gpt_entry { 82 struct g_part_entry base; 83 struct gpt_ent ent; 84}; 85 86static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 87 struct g_part_parms *); 88static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 89static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 90static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 91static int g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 92 struct sbuf *, const char *); 93static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 94static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 95 struct g_part_parms *); 96static char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 97 char *, size_t); 98static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 99static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 100static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 101 char *, size_t); 102static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 103 104static kobj_method_t g_part_gpt_methods[] = { 105 KOBJMETHOD(g_part_add, g_part_gpt_add), 106 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 107 KOBJMETHOD(g_part_create, g_part_gpt_create), 108 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 109 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 110 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 111 KOBJMETHOD(g_part_modify, g_part_gpt_modify), 112 KOBJMETHOD(g_part_name, g_part_gpt_name), 113 KOBJMETHOD(g_part_probe, g_part_gpt_probe), 114 KOBJMETHOD(g_part_read, g_part_gpt_read), 115 KOBJMETHOD(g_part_type, g_part_gpt_type), 116 KOBJMETHOD(g_part_write, g_part_gpt_write), 117 { 0, 0 } 118}; 119 120static struct g_part_scheme g_part_gpt_scheme = { 121 "GPT", 122 g_part_gpt_methods, 123 sizeof(struct g_part_gpt_table), 124 .gps_entrysz = sizeof(struct g_part_gpt_entry), 125 .gps_minent = 128, 126 .gps_maxent = INT_MAX, 127 .gps_bootcodesz = MBRSIZE, 128}; 129G_PART_SCHEME_DECLARE(g_part_gpt); 130 131static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 132static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 133static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 134static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 135static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 136static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 137static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 138static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 139static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 140static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 141 142static void 143gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 144 enum gpt_elt elt, struct gpt_hdr *hdr) 145{ 146 struct uuid uuid; 147 struct g_provider *pp; 148 char *buf; 149 quad_t lba, last; 150 int error; 151 uint32_t crc, sz; 152 153 pp = cp->provider; 154 last = (pp->mediasize / pp->sectorsize) - 1; 155 table->lba[elt] = (elt == GPT_ELT_PRIHDR) ? 1 : last; 156 table->state[elt] = GPT_STATE_MISSING; 157 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 158 &error); 159 if (buf == NULL) 160 return; 161 bcopy(buf, hdr, sizeof(*hdr)); 162 if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0) 163 return; 164 165 table->state[elt] = GPT_STATE_CORRUPT; 166 sz = le32toh(hdr->hdr_size); 167 if (sz < 92 || sz > pp->sectorsize) 168 return; 169 crc = le32toh(hdr->hdr_crc_self); 170 hdr->hdr_crc_self = 0; 171 if (crc32(hdr, sz) != crc) 172 return; 173 hdr->hdr_size = sz; 174 hdr->hdr_crc_self = crc; 175 176 table->state[elt] = GPT_STATE_INVALID; 177 hdr->hdr_revision = le32toh(hdr->hdr_revision); 178 if (hdr->hdr_revision < 0x00010000) 179 return; 180 hdr->hdr_lba_self = le64toh(hdr->hdr_lba_self); 181 if (hdr->hdr_lba_self != table->lba[elt]) 182 return; 183 hdr->hdr_lba_alt = le64toh(hdr->hdr_lba_alt); 184 185 /* Check the managed area. */ 186 hdr->hdr_lba_start = le64toh(hdr->hdr_lba_start); 187 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 188 return; 189 hdr->hdr_lba_end = le64toh(hdr->hdr_lba_end); 190 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 191 return; 192 193 /* Check the table location and size of the table. */ 194 hdr->hdr_entries = le32toh(hdr->hdr_entries); 195 hdr->hdr_entsz = le32toh(hdr->hdr_entsz); 196 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 197 (hdr->hdr_entsz & 7) != 0) 198 return; 199 hdr->hdr_lba_table = le64toh(hdr->hdr_lba_table); 200 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 201 return; 202 if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 203 hdr->hdr_lba_table <= hdr->hdr_lba_end) 204 return; 205 lba = hdr->hdr_lba_table + 206 (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) / 207 pp->sectorsize - 1; 208 if (lba >= last) 209 return; 210 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 211 return; 212 213 table->state[elt] = GPT_STATE_OK; 214 le_uuid_dec(&hdr->hdr_uuid, &uuid); 215 hdr->hdr_uuid = uuid; 216 hdr->hdr_crc_table = le32toh(hdr->hdr_crc_table); 217} 218 219static struct gpt_ent * 220gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 221 enum gpt_elt elt, struct gpt_hdr *hdr) 222{ 223 struct g_provider *pp; 224 struct gpt_ent *ent, *tbl; 225 char *buf, *p; 226 unsigned int idx, sectors, tblsz; 227 int error; 228 uint16_t ch; 229 230 pp = cp->provider; 231 table->lba[elt] = hdr->hdr_lba_table; 232 233 table->state[elt] = GPT_STATE_MISSING; 234 tblsz = hdr->hdr_entries * hdr->hdr_entsz; 235 sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize; 236 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, 237 sectors * pp->sectorsize, &error); 238 if (buf == NULL) 239 return (NULL); 240 241 table->state[elt] = GPT_STATE_CORRUPT; 242 if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 243 g_free(buf); 244 return (NULL); 245 } 246 247 table->state[elt] = GPT_STATE_OK; 248 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 249 M_WAITOK | M_ZERO); 250 251 for (idx = 0, ent = tbl, p = buf; 252 idx < hdr->hdr_entries; 253 idx++, ent++, p += hdr->hdr_entsz) { 254 le_uuid_dec(p, &ent->ent_type); 255 le_uuid_dec(p + 16, &ent->ent_uuid); 256 ent->ent_lba_start = le64dec(p + 32); 257 ent->ent_lba_end = le64dec(p + 40); 258 ent->ent_attr = le64dec(p + 48); 259 for (ch = 0; ch < sizeof(ent->ent_name)/2; ch++) 260 ent->ent_name[ch] = le16dec(p + 56 + ch * 2); 261 } 262 263 g_free(buf); 264 return (tbl); 265} 266 267static int 268gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 269{ 270 271 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 272 return (0); 273 return ((pri->hdr_revision == sec->hdr_revision && 274 pri->hdr_size == sec->hdr_size && 275 pri->hdr_lba_start == sec->hdr_lba_start && 276 pri->hdr_lba_end == sec->hdr_lba_end && 277 pri->hdr_entries == sec->hdr_entries && 278 pri->hdr_entsz == sec->hdr_entsz && 279 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 280} 281 282static int 283gpt_parse_type(const char *type, struct uuid *uuid) 284{ 285 struct uuid tmp; 286 const char *alias; 287 int error; 288 289 if (type[0] == '!') { 290 error = parse_uuid(type + 1, &tmp); 291 if (error) 292 return (error); 293 if (EQUUID(&tmp, &gpt_uuid_unused)) 294 return (EINVAL); 295 *uuid = tmp; 296 return (0); 297 } 298 alias = g_part_alias_name(G_PART_ALIAS_EFI); 299 if (!strcasecmp(type, alias)) { 300 *uuid = gpt_uuid_efi; 301 return (0); 302 } 303 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD); 304 if (!strcasecmp(type, alias)) { 305 *uuid = gpt_uuid_freebsd; 306 return (0); 307 } 308 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT); 309 if (!strcasecmp(type, alias)) { 310 *uuid = gpt_uuid_freebsd_boot; 311 return (0); 312 } 313 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); 314 if (!strcasecmp(type, alias)) { 315 *uuid = gpt_uuid_freebsd_swap; 316 return (0); 317 } 318 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); 319 if (!strcasecmp(type, alias)) { 320 *uuid = gpt_uuid_freebsd_ufs; 321 return (0); 322 } 323 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); 324 if (!strcasecmp(type, alias)) { 325 *uuid = gpt_uuid_freebsd_vinum; 326 return (0); 327 } 328 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS); 329 if (!strcasecmp(type, alias)) { 330 *uuid = gpt_uuid_freebsd_zfs; 331 return (0); 332 } 333 alias = g_part_alias_name(G_PART_ALIAS_MBR); 334 if (!strcasecmp(type, alias)) { 335 *uuid = gpt_uuid_mbr; 336 return (0); 337 } 338 return (EINVAL); 339} 340 341static int 342g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 343 struct g_part_parms *gpp) 344{ 345 struct g_part_gpt_entry *entry; 346 int error; 347 348 entry = (struct g_part_gpt_entry *)baseentry; 349 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 350 if (error) 351 return (error); 352 kern_uuidgen(&entry->ent.ent_uuid, 1); 353 entry->ent.ent_lba_start = baseentry->gpe_start; 354 entry->ent.ent_lba_end = baseentry->gpe_end; 355 if (baseentry->gpe_deleted) { 356 entry->ent.ent_attr = 0; 357 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 358 } 359 /* XXX label */ 360 return (0); 361} 362 363static int 364g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 365{ 366 struct g_part_gpt_table *table; 367 368 table = (struct g_part_gpt_table *)basetable; 369 bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF); 370 return (0); 371} 372 373static int 374g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 375{ 376 struct g_provider *pp; 377 struct g_part_gpt_table *table; 378 quad_t last; 379 size_t tblsz; 380 381 table = (struct g_part_gpt_table *)basetable; 382 pp = gpp->gpp_provider; 383 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 384 pp->sectorsize - 1) / pp->sectorsize; 385 if (pp->sectorsize < MBRSIZE || 386 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 387 pp->sectorsize) 388 return (ENOSPC); 389 390 last = (pp->mediasize / pp->sectorsize) - 1; 391 392 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 393 table->mbr[DOSPARTOFF + 1] = 0xff; /* shd */ 394 table->mbr[DOSPARTOFF + 2] = 0xff; /* ssect */ 395 table->mbr[DOSPARTOFF + 3] = 0xff; /* scyl */ 396 table->mbr[DOSPARTOFF + 4] = 0xee; /* typ */ 397 table->mbr[DOSPARTOFF + 5] = 0xff; /* ehd */ 398 table->mbr[DOSPARTOFF + 6] = 0xff; /* esect */ 399 table->mbr[DOSPARTOFF + 7] = 0xff; /* ecyl */ 400 le32enc(table->mbr + DOSPARTOFF + 8, 1); /* start */ 401 le32enc(table->mbr + DOSPARTOFF + 12, MIN(last, 0xffffffffLL)); 402 403 table->lba[GPT_ELT_PRIHDR] = 1; 404 table->lba[GPT_ELT_PRITBL] = 2; 405 table->lba[GPT_ELT_SECHDR] = last; 406 table->lba[GPT_ELT_SECTBL] = last - tblsz; 407 408 bcopy(GPT_HDR_SIG, table->hdr.hdr_sig, sizeof(table->hdr.hdr_sig)); 409 table->hdr.hdr_revision = GPT_HDR_REVISION; 410 table->hdr.hdr_size = offsetof(struct gpt_hdr, padding); 411 table->hdr.hdr_lba_start = 2 + tblsz; 412 table->hdr.hdr_lba_end = last - tblsz - 1; 413 kern_uuidgen(&table->hdr.hdr_uuid, 1); 414 table->hdr.hdr_entries = basetable->gpt_entries; 415 table->hdr.hdr_entsz = sizeof(struct gpt_ent); 416 417 basetable->gpt_first = table->hdr.hdr_lba_start; 418 basetable->gpt_last = table->hdr.hdr_lba_end; 419 return (0); 420} 421 422static int 423g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 424{ 425 426 /* 427 * Wipe the first 2 sectors as well as the last to clear the 428 * partitioning. 429 */ 430 basetable->gpt_smhead |= 3; 431 basetable->gpt_smtail |= 1; 432 return (0); 433} 434 435static int 436g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 437 struct sbuf *sb, const char *indent) 438{ 439 struct g_part_gpt_entry *entry; 440 441 entry = (struct g_part_gpt_entry *)baseentry; 442 if (indent == NULL) { 443 /* conftxt: libdisk compatibility */ 444 sbuf_printf(sb, " xs GPT xt "); 445 sbuf_printf_uuid(sb, &entry->ent.ent_type); 446 } else if (entry != NULL) { 447 /* confxml: partition entry information */ 448 // sbuf_printf(sb, "%s<label>%s</label>\n", indent, NULL); 449 sbuf_printf(sb, "%s<rawtype>", indent); 450 sbuf_printf_uuid(sb, &entry->ent.ent_type); 451 sbuf_printf(sb, "</rawtype>\n"); 452 } else { 453 /* confxml: scheme information */ 454 } 455 return (0); 456} 457 458static int 459g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 460{ 461 struct g_part_gpt_entry *entry; 462 463 entry = (struct g_part_gpt_entry *)baseentry; 464 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 465 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0); 466} 467 468static int 469g_part_gpt_modify(struct g_part_table *basetable, 470 struct g_part_entry *baseentry, struct g_part_parms *gpp) 471{ 472 struct g_part_gpt_entry *entry; 473 int error; 474 475 entry = (struct g_part_gpt_entry *)baseentry; 476 if (gpp->gpp_parms & G_PART_PARM_TYPE) { 477 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 478 if (error) 479 return (error); 480 } 481 /* XXX label */ 482 return (0); 483} 484 485static char * 486g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 487 char *buf, size_t bufsz) 488{ 489 struct g_part_gpt_entry *entry; 490 char c; 491 492 entry = (struct g_part_gpt_entry *)baseentry; 493 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 494 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 495 return (buf); 496} 497 498static int 499g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 500{ 501 struct g_provider *pp; 502 char *buf; 503 int error, res; 504 505 /* We don't nest, which means that our depth should be 0. */ 506 if (table->gpt_depth != 0) 507 return (ENXIO); 508 509 pp = cp->provider; 510 511 /* 512 * Sanity-check the provider. Since the first sector on the provider 513 * must be a PMBR and a PMBR is 512 bytes large, the sector size 514 * must be at least 512 bytes. Also, since the theoretical minimum 515 * number of sectors needed by GPT is 6, any medium that has less 516 * than 6 sectors is never going to be able to hold a GPT. The 517 * number 6 comes from: 518 * 1 sector for the PMBR 519 * 2 sectors for the GPT headers (each 1 sector) 520 * 2 sectors for the GPT tables (each 1 sector) 521 * 1 sector for an actual partition 522 * It's better to catch this pathological case early than behaving 523 * pathologically later on... 524 */ 525 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 526 return (ENOSPC); 527 528 /* Check that there's a MBR. */ 529 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 530 if (buf == NULL) 531 return (error); 532 res = le16dec(buf + DOSMAGICOFFSET); 533 g_free(buf); 534 if (res != DOSMAGIC) 535 return (ENXIO); 536 537 /* Check that there's a primary header. */ 538 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 539 if (buf == NULL) 540 return (error); 541 res = memcmp(buf, GPT_HDR_SIG, 8); 542 g_free(buf); 543 if (res == 0) 544 return (G_PART_PROBE_PRI_HIGH); 545 546 /* No primary? Check that there's a secondary. */ 547 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 548 &error); 549 if (buf == NULL) 550 return (error); 551 res = memcmp(buf, GPT_HDR_SIG, 8); 552 g_free(buf); 553 return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO); 554} 555 556static int 557g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 558{ 559 struct gpt_hdr prihdr, sechdr; 560 struct gpt_ent *tbl, *pritbl, *sectbl; 561 struct g_provider *pp; 562 struct g_part_gpt_table *table; 563 struct g_part_gpt_entry *entry; 564 u_char *buf; 565 int error, index; 566 567 table = (struct g_part_gpt_table *)basetable; 568 pp = cp->provider; 569 570 /* Read the PMBR */ 571 buf = g_read_data(cp, 0, pp->sectorsize, &error); 572 if (buf == NULL) 573 return (error); 574 bcopy(buf, table->mbr, MBRSIZE); 575 g_free(buf); 576 577 /* Read the primary header and table. */ 578 gpt_read_hdr(table, cp, GPT_ELT_PRIHDR, &prihdr); 579 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 580 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, &prihdr); 581 } else { 582 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 583 pritbl = NULL; 584 } 585 586 /* Read the secondary header and table. */ 587 gpt_read_hdr(table, cp, GPT_ELT_SECHDR, &sechdr); 588 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 589 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, &sechdr); 590 } else { 591 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 592 sectbl = NULL; 593 } 594 595 /* Fail if we haven't got any good tables at all. */ 596 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 597 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 598 printf("GEOM: %s: corrupt or invalid GPT detected.\n", 599 pp->name); 600 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 601 pp->name); 602 return (EINVAL); 603 } 604 605 /* 606 * If both headers are good but they disagree with each other, 607 * then invalidate one. We prefer to keep the primary header, 608 * unless the primary table is corrupt. 609 */ 610 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 611 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 612 !gpt_matched_hdrs(&prihdr, &sechdr)) { 613 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) 614 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 615 else 616 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 617 } 618 619 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) { 620 printf("GEOM: %s: the primary GPT table is corrupt or " 621 "invalid.\n", pp->name); 622 printf("GEOM: %s: using the secondary instead -- recovery " 623 "strongly advised.\n", pp->name); 624 table->hdr = sechdr; 625 tbl = sectbl; 626 if (pritbl != NULL) 627 g_free(pritbl); 628 } else { 629 if (table->state[GPT_ELT_SECHDR] != GPT_STATE_OK) { 630 printf("GEOM: %s: the secondary GPT table is corrupt " 631 "or invalid.\n", pp->name); 632 printf("GEOM: %s: using the primary only -- recovery " 633 "suggested.\n", pp->name); 634 } 635 table->hdr = prihdr; 636 tbl = pritbl; 637 if (sectbl != NULL) 638 g_free(sectbl); 639 } 640 641 basetable->gpt_first = table->hdr.hdr_lba_start; 642 basetable->gpt_last = table->hdr.hdr_lba_end; 643 basetable->gpt_entries = table->hdr.hdr_entries; 644 645 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 646 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 647 continue; 648 entry = (struct g_part_gpt_entry *)g_part_new_entry(basetable, 649 index+1, tbl[index].ent_lba_start, tbl[index].ent_lba_end); 650 entry->ent = tbl[index]; 651 } 652 653 g_free(tbl); 654 return (0); 655} 656 657static const char * 658g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 659 char *buf, size_t bufsz) 660{ 661 struct g_part_gpt_entry *entry; 662 struct uuid *type; 663 664 entry = (struct g_part_gpt_entry *)baseentry; 665 type = &entry->ent.ent_type; 666 if (EQUUID(type, &gpt_uuid_efi)) 667 return (g_part_alias_name(G_PART_ALIAS_EFI)); 668 if (EQUUID(type, &gpt_uuid_freebsd)) 669 return (g_part_alias_name(G_PART_ALIAS_FREEBSD)); 670 if (EQUUID(type, &gpt_uuid_freebsd_boot)) 671 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_BOOT)); 672 if (EQUUID(type, &gpt_uuid_freebsd_swap)) 673 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); 674 if (EQUUID(type, &gpt_uuid_freebsd_ufs)) 675 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); 676 if (EQUUID(type, &gpt_uuid_freebsd_vinum)) 677 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); 678 if (EQUUID(type, &gpt_uuid_freebsd_zfs)) 679 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS)); 680 if (EQUUID(type, &gpt_uuid_mbr)) 681 return (g_part_alias_name(G_PART_ALIAS_MBR)); 682 buf[0] = '!'; 683 snprintf_uuid(buf + 1, bufsz - 1, type); 684 return (buf); 685} 686 687static int 688g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 689{ 690 unsigned char *buf, *bp; 691 struct g_provider *pp; 692 struct g_part_entry *baseentry; 693 struct g_part_gpt_entry *entry; 694 struct g_part_gpt_table *table; 695 size_t tlbsz; 696 uint32_t crc; 697 int error, index; 698 699 pp = cp->provider; 700 table = (struct g_part_gpt_table *)basetable; 701 tlbsz = (table->hdr.hdr_entries * table->hdr.hdr_entsz + 702 pp->sectorsize - 1) / pp->sectorsize; 703 704 /* Write the PMBR */ 705 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 706 bcopy(table->mbr, buf, MBRSIZE); 707 error = g_write_data(cp, 0, buf, pp->sectorsize); 708 g_free(buf); 709 if (error) 710 return (error); 711 712 /* Allocate space for the header and entries. */ 713 buf = g_malloc((tlbsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 714 715 memcpy(buf, table->hdr.hdr_sig, sizeof(table->hdr.hdr_sig)); 716 le32enc(buf + 8, table->hdr.hdr_revision); 717 le32enc(buf + 12, table->hdr.hdr_size); 718 le64enc(buf + 40, table->hdr.hdr_lba_start); 719 le64enc(buf + 48, table->hdr.hdr_lba_end); 720 le_uuid_enc(buf + 56, &table->hdr.hdr_uuid); 721 le32enc(buf + 80, table->hdr.hdr_entries); 722 le32enc(buf + 84, table->hdr.hdr_entsz); 723 724 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 725 if (baseentry->gpe_deleted) 726 continue; 727 entry = (struct g_part_gpt_entry *)baseentry; 728 index = baseentry->gpe_index - 1; 729 bp = buf + pp->sectorsize + table->hdr.hdr_entsz * index; 730 le_uuid_enc(bp, &entry->ent.ent_type); 731 le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 732 le64enc(bp + 32, entry->ent.ent_lba_start); 733 le64enc(bp + 40, entry->ent.ent_lba_end); 734 le64enc(bp + 48, entry->ent.ent_attr); 735 memcpy(bp + 56, entry->ent.ent_name, 736 sizeof(entry->ent.ent_name)); 737 } 738 739 crc = crc32(buf + pp->sectorsize, 740 table->hdr.hdr_entries * table->hdr.hdr_entsz); 741 le32enc(buf + 88, crc); 742 743 /* Write primary meta-data. */ 744 le32enc(buf + 16, 0); /* hdr_crc_self. */ 745 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 746 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 747 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 748 crc = crc32(buf, table->hdr.hdr_size); 749 le32enc(buf + 16, crc); 750 751 error = g_write_data(cp, table->lba[GPT_ELT_PRITBL] * pp->sectorsize, 752 buf + pp->sectorsize, tlbsz * pp->sectorsize); 753 if (error) 754 goto out; 755 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 756 buf, pp->sectorsize); 757 if (error) 758 goto out; 759 760 /* Write secondary meta-data. */ 761 le32enc(buf + 16, 0); /* hdr_crc_self. */ 762 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 763 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 764 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 765 crc = crc32(buf, table->hdr.hdr_size); 766 le32enc(buf + 16, crc); 767 768 error = g_write_data(cp, table->lba[GPT_ELT_SECTBL] * pp->sectorsize, 769 buf + pp->sectorsize, tlbsz * pp->sectorsize); 770 if (error) 771 goto out; 772 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 773 buf, pp->sectorsize); 774 775 out: 776 g_free(buf); 777 return (error); 778} 779 780#if 0 781static void 782g_gpt_to_utf8(struct sbuf *sb, uint16_t *str, size_t len) 783{ 784 u_int bo; 785 uint32_t ch; 786 uint16_t c; 787 788 bo = BYTE_ORDER; 789 while (len > 0 && *str != 0) { 790 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 791 str++, len--; 792 if ((ch & 0xf800) == 0xd800) { 793 if (len > 0) { 794 c = (bo == BIG_ENDIAN) ? be16toh(*str) 795 : le16toh(*str); 796 str++, len--; 797 } else 798 c = 0xfffd; 799 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 800 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 801 ch += 0x10000; 802 } else 803 ch = 0xfffd; 804 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 805 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 806 continue; 807 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 808 continue; 809 810 if (ch < 0x80) 811 sbuf_printf(sb, "%c", ch); 812 else if (ch < 0x800) 813 sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6), 814 0x80 | (ch & 0x3f)); 815 else if (ch < 0x10000) 816 sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12), 817 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 818 else if (ch < 0x200000) 819 sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18), 820 0x80 | ((ch >> 12) & 0x3f), 821 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 822 } 823} 824#endif 825