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/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 }; 83 84 struct g_part_gpt_entry { 85 struct g_part_entry base; 86 struct gpt_ent ent; 87 }; 88 89 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t); 90 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t); 91 92 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 93 struct g_part_parms *); 94 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 95 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 96 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 97 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 98 struct sbuf *, const char *); 99 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 100 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 101 struct g_part_parms *); 102 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 103 char *, size_t); 104 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 105 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 106 static int g_part_gpt_setunset(struct g_part_table *table, 107 struct g_part_entry *baseentry, const char *attrib, unsigned int set); 108 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 109 char *, size_t); 110 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 111 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 112 struct g_part_parms *); 113 static int g_part_gpt_recover(struct g_part_table *); 114 115 static kobj_method_t g_part_gpt_methods[] = { 116 KOBJMETHOD(g_part_add, g_part_gpt_add), 117 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 118 KOBJMETHOD(g_part_create, g_part_gpt_create), 119 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 120 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 121 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 122 KOBJMETHOD(g_part_modify, g_part_gpt_modify), 123 KOBJMETHOD(g_part_resize, g_part_gpt_resize), 124 KOBJMETHOD(g_part_name, g_part_gpt_name), 125 KOBJMETHOD(g_part_probe, g_part_gpt_probe), 126 KOBJMETHOD(g_part_read, g_part_gpt_read), 127 KOBJMETHOD(g_part_recover, g_part_gpt_recover), 128 KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 129 KOBJMETHOD(g_part_type, g_part_gpt_type), 130 KOBJMETHOD(g_part_write, g_part_gpt_write), 131 { 0, 0 } 132 }; 133 134 static struct g_part_scheme g_part_gpt_scheme = { 135 "GPT", 136 g_part_gpt_methods, 137 sizeof(struct g_part_gpt_table), 138 .gps_entrysz = sizeof(struct g_part_gpt_entry), 139 .gps_minent = 128, 140 .gps_maxent = 4096, 141 .gps_bootcodesz = MBRSIZE, 142 }; 143 G_PART_SCHEME_DECLARE(g_part_gpt); 144 145 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 146 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 147 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 148 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 149 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 150 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 151 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 152 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 153 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 154 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 155 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 156 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 157 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 158 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 159 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 160 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 161 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 162 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 163 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 164 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 165 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 166 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 167 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 168 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 169 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 170 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 171 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 172 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 173 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 174 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 175 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 176 177 static struct g_part_uuid_alias { 178 struct uuid *uuid; 179 int alias; 180 } gpt_uuid_alias_match[] = { 181 { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT }, 182 { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS }, 183 { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL }, 184 { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID }, 185 { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE }, 186 { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY }, 187 { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS }, 188 { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT }, 189 { &gpt_uuid_efi, G_PART_ALIAS_EFI }, 190 { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD }, 191 { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT }, 192 { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP }, 193 { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS }, 194 { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM }, 195 { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS }, 196 { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA }, 197 { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM }, 198 { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID }, 199 { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP }, 200 { &gpt_uuid_mbr, G_PART_ALIAS_MBR }, 201 { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA }, 202 { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA }, 203 { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA }, 204 { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED }, 205 { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD }, 206 { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD }, 207 { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS }, 208 { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS }, 209 { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID }, 210 { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP }, 211 212 { NULL, 0 } 213 }; 214 215 static struct gpt_hdr * 216 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 217 enum gpt_elt elt) 218 { 219 struct gpt_hdr *buf, *hdr; 220 struct g_provider *pp; 221 quad_t lba, last; 222 int error; 223 uint32_t crc, sz; 224 225 pp = cp->provider; 226 last = (pp->mediasize / pp->sectorsize) - 1; 227 table->state[elt] = GPT_STATE_MISSING; 228 /* 229 * If the primary header is valid look for secondary 230 * header in AlternateLBA, otherwise in the last medium's LBA. 231 */ 232 if (elt == GPT_ELT_SECHDR) { 233 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 234 table->lba[elt] = last; 235 } else 236 table->lba[elt] = 1; 237 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 238 &error); 239 if (buf == NULL) 240 return (NULL); 241 hdr = NULL; 242 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 243 goto fail; 244 245 table->state[elt] = GPT_STATE_CORRUPT; 246 sz = le32toh(buf->hdr_size); 247 if (sz < 92 || sz > pp->sectorsize) 248 goto fail; 249 250 hdr = g_malloc(sz, M_WAITOK | M_ZERO); 251 bcopy(buf, hdr, sz); 252 hdr->hdr_size = sz; 253 254 crc = le32toh(buf->hdr_crc_self); 255 buf->hdr_crc_self = 0; 256 if (crc32(buf, sz) != crc) 257 goto fail; 258 hdr->hdr_crc_self = crc; 259 260 table->state[elt] = GPT_STATE_INVALID; 261 hdr->hdr_revision = le32toh(buf->hdr_revision); 262 if (hdr->hdr_revision < GPT_HDR_REVISION) 263 goto fail; 264 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 265 if (hdr->hdr_lba_self != table->lba[elt]) 266 goto fail; 267 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 268 if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 269 hdr->hdr_lba_alt > last) 270 goto fail; 271 272 /* Check the managed area. */ 273 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 274 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 275 goto fail; 276 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 277 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 278 goto fail; 279 280 /* Check the table location and size of the table. */ 281 hdr->hdr_entries = le32toh(buf->hdr_entries); 282 hdr->hdr_entsz = le32toh(buf->hdr_entsz); 283 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 284 (hdr->hdr_entsz & 7) != 0) 285 goto fail; 286 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 287 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 288 goto fail; 289 if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 290 hdr->hdr_lba_table <= hdr->hdr_lba_end) 291 goto fail; 292 lba = hdr->hdr_lba_table + 293 (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) / 294 pp->sectorsize - 1; 295 if (lba >= last) 296 goto fail; 297 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 298 goto fail; 299 300 table->state[elt] = GPT_STATE_OK; 301 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 302 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 303 304 /* save LBA for secondary header */ 305 if (elt == GPT_ELT_PRIHDR) 306 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 307 308 g_free(buf); 309 return (hdr); 310 311 fail: 312 if (hdr != NULL) 313 g_free(hdr); 314 g_free(buf); 315 return (NULL); 316 } 317 318 static struct gpt_ent * 319 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 320 enum gpt_elt elt, struct gpt_hdr *hdr) 321 { 322 struct g_provider *pp; 323 struct gpt_ent *ent, *tbl; 324 char *buf, *p; 325 unsigned int idx, sectors, tblsz, size; 326 int error; 327 328 if (hdr == NULL) 329 return (NULL); 330 331 pp = cp->provider; 332 table->lba[elt] = hdr->hdr_lba_table; 333 334 table->state[elt] = GPT_STATE_MISSING; 335 tblsz = hdr->hdr_entries * hdr->hdr_entsz; 336 sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize; 337 buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 338 for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 339 size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 340 (sectors - idx) * pp->sectorsize; 341 p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 342 size, &error); 343 if (p == NULL) { 344 g_free(buf); 345 return (NULL); 346 } 347 bcopy(p, buf + idx * pp->sectorsize, size); 348 g_free(p); 349 } 350 table->state[elt] = GPT_STATE_CORRUPT; 351 if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 352 g_free(buf); 353 return (NULL); 354 } 355 356 table->state[elt] = GPT_STATE_OK; 357 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 358 M_WAITOK | M_ZERO); 359 360 for (idx = 0, ent = tbl, p = buf; 361 idx < hdr->hdr_entries; 362 idx++, ent++, p += hdr->hdr_entsz) { 363 le_uuid_dec(p, &ent->ent_type); 364 le_uuid_dec(p + 16, &ent->ent_uuid); 365 ent->ent_lba_start = le64dec(p + 32); 366 ent->ent_lba_end = le64dec(p + 40); 367 ent->ent_attr = le64dec(p + 48); 368 /* Keep UTF-16 in little-endian. */ 369 bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 370 } 371 372 g_free(buf); 373 return (tbl); 374 } 375 376 static int 377 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 378 { 379 380 if (pri == NULL || sec == NULL) 381 return (0); 382 383 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 384 return (0); 385 return ((pri->hdr_revision == sec->hdr_revision && 386 pri->hdr_size == sec->hdr_size && 387 pri->hdr_lba_start == sec->hdr_lba_start && 388 pri->hdr_lba_end == sec->hdr_lba_end && 389 pri->hdr_entries == sec->hdr_entries && 390 pri->hdr_entsz == sec->hdr_entsz && 391 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 392 } 393 394 static int 395 gpt_parse_type(const char *type, struct uuid *uuid) 396 { 397 struct uuid tmp; 398 const char *alias; 399 int error; 400 struct g_part_uuid_alias *uap; 401 402 if (type[0] == '!') { 403 error = parse_uuid(type + 1, &tmp); 404 if (error) 405 return (error); 406 if (EQUUID(&tmp, &gpt_uuid_unused)) 407 return (EINVAL); 408 *uuid = tmp; 409 return (0); 410 } 411 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 412 alias = g_part_alias_name(uap->alias); 413 if (!strcasecmp(type, alias)) { 414 *uuid = *uap->uuid; 415 return (0); 416 } 417 } 418 return (EINVAL); 419 } 420 421 static int 422 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 423 struct g_part_parms *gpp) 424 { 425 struct g_part_gpt_entry *entry; 426 int error; 427 428 entry = (struct g_part_gpt_entry *)baseentry; 429 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 430 if (error) 431 return (error); 432 kern_uuidgen(&entry->ent.ent_uuid, 1); 433 entry->ent.ent_lba_start = baseentry->gpe_start; 434 entry->ent.ent_lba_end = baseentry->gpe_end; 435 if (baseentry->gpe_deleted) { 436 entry->ent.ent_attr = 0; 437 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 438 } 439 if (gpp->gpp_parms & G_PART_PARM_LABEL) 440 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 441 sizeof(entry->ent.ent_name) / 442 sizeof(entry->ent.ent_name[0])); 443 return (0); 444 } 445 446 static int 447 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 448 { 449 struct g_part_gpt_table *table; 450 size_t codesz; 451 452 codesz = DOSPARTOFF; 453 table = (struct g_part_gpt_table *)basetable; 454 bzero(table->mbr, codesz); 455 codesz = MIN(codesz, gpp->gpp_codesize); 456 if (codesz > 0) 457 bcopy(gpp->gpp_codeptr, table->mbr, codesz); 458 459 /* Mark the PMBR active since some BIOS require it */ 460 table->mbr[DOSPARTOFF] = 0x80; /* status */ 461 return (0); 462 } 463 464 static int 465 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 466 { 467 struct g_provider *pp; 468 struct g_part_gpt_table *table; 469 quad_t last; 470 size_t tblsz; 471 472 /* We don't nest, which means that our depth should be 0. */ 473 if (basetable->gpt_depth != 0) 474 return (ENXIO); 475 476 table = (struct g_part_gpt_table *)basetable; 477 pp = gpp->gpp_provider; 478 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 479 pp->sectorsize - 1) / pp->sectorsize; 480 if (pp->sectorsize < MBRSIZE || 481 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 482 pp->sectorsize) 483 return (ENOSPC); 484 485 last = (pp->mediasize / pp->sectorsize) - 1; 486 487 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 488 table->mbr[DOSPARTOFF + 1] = 0x01; /* shd */ 489 table->mbr[DOSPARTOFF + 2] = 0x01; /* ssect */ 490 table->mbr[DOSPARTOFF + 3] = 0x00; /* scyl */ 491 table->mbr[DOSPARTOFF + 4] = 0xee; /* typ */ 492 table->mbr[DOSPARTOFF + 5] = 0xff; /* ehd */ 493 table->mbr[DOSPARTOFF + 6] = 0xff; /* esect */ 494 table->mbr[DOSPARTOFF + 7] = 0xff; /* ecyl */ 495 le32enc(table->mbr + DOSPARTOFF + 8, 1); /* start */ 496 le32enc(table->mbr + DOSPARTOFF + 12, MIN(last, 0xffffffffLL)); 497 498 table->lba[GPT_ELT_PRIHDR] = 1; 499 table->lba[GPT_ELT_PRITBL] = 2; 500 table->lba[GPT_ELT_SECHDR] = last; 501 table->lba[GPT_ELT_SECTBL] = last - tblsz; 502 503 /* Allocate space for the header */ 504 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 505 506 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 507 table->hdr->hdr_revision = GPT_HDR_REVISION; 508 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 509 table->hdr->hdr_lba_start = 2 + tblsz; 510 table->hdr->hdr_lba_end = last - tblsz - 1; 511 kern_uuidgen(&table->hdr->hdr_uuid, 1); 512 table->hdr->hdr_entries = basetable->gpt_entries; 513 table->hdr->hdr_entsz = sizeof(struct gpt_ent); 514 515 basetable->gpt_first = table->hdr->hdr_lba_start; 516 basetable->gpt_last = table->hdr->hdr_lba_end; 517 return (0); 518 } 519 520 static int 521 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 522 { 523 struct g_part_gpt_table *table; 524 struct g_provider *pp; 525 526 table = (struct g_part_gpt_table *)basetable; 527 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 528 g_free(table->hdr); 529 table->hdr = NULL; 530 531 /* 532 * Wipe the first 2 sectors to clear the partitioning. Wipe the last 533 * sector only if it has valid secondary header. 534 */ 535 basetable->gpt_smhead |= 3; 536 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 537 table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 538 basetable->gpt_smtail |= 1; 539 return (0); 540 } 541 542 static void 543 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 544 struct sbuf *sb, const char *indent) 545 { 546 struct g_part_gpt_entry *entry; 547 548 entry = (struct g_part_gpt_entry *)baseentry; 549 if (indent == NULL) { 550 /* conftxt: libdisk compatibility */ 551 sbuf_printf(sb, " xs GPT xt "); 552 sbuf_printf_uuid(sb, &entry->ent.ent_type); 553 } else if (entry != NULL) { 554 /* confxml: partition entry information */ 555 sbuf_printf(sb, "%s<label>", indent); 556 g_gpt_printf_utf16(sb, entry->ent.ent_name, 557 sizeof(entry->ent.ent_name) >> 1); 558 sbuf_printf(sb, "</label>\n"); 559 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 560 sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 561 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 562 sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 563 indent); 564 } 565 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 566 sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 567 indent); 568 } 569 sbuf_printf(sb, "%s<rawtype>", indent); 570 sbuf_printf_uuid(sb, &entry->ent.ent_type); 571 sbuf_printf(sb, "</rawtype>\n"); 572 sbuf_printf(sb, "%s<rawuuid>", indent); 573 sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 574 sbuf_printf(sb, "</rawuuid>\n"); 575 } else { 576 /* confxml: scheme information */ 577 } 578 } 579 580 static int 581 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 582 { 583 struct g_part_gpt_entry *entry; 584 585 entry = (struct g_part_gpt_entry *)baseentry; 586 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 587 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0); 588 } 589 590 static int 591 g_part_gpt_modify(struct g_part_table *basetable, 592 struct g_part_entry *baseentry, struct g_part_parms *gpp) 593 { 594 struct g_part_gpt_entry *entry; 595 int error; 596 597 entry = (struct g_part_gpt_entry *)baseentry; 598 if (gpp->gpp_parms & G_PART_PARM_TYPE) { 599 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 600 if (error) 601 return (error); 602 } 603 if (gpp->gpp_parms & G_PART_PARM_LABEL) 604 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 605 sizeof(entry->ent.ent_name) / 606 sizeof(entry->ent.ent_name[0])); 607 return (0); 608 } 609 610 static int 611 g_part_gpt_resize(struct g_part_table *basetable, 612 struct g_part_entry *baseentry, struct g_part_parms *gpp) 613 { 614 struct g_part_gpt_entry *entry; 615 entry = (struct g_part_gpt_entry *)baseentry; 616 617 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 618 entry->ent.ent_lba_end = baseentry->gpe_end; 619 620 return (0); 621 } 622 623 static const char * 624 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 625 char *buf, size_t bufsz) 626 { 627 struct g_part_gpt_entry *entry; 628 char c; 629 630 entry = (struct g_part_gpt_entry *)baseentry; 631 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 632 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 633 return (buf); 634 } 635 636 static int 637 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 638 { 639 struct g_provider *pp; 640 char *buf; 641 int error, res; 642 643 /* We don't nest, which means that our depth should be 0. */ 644 if (table->gpt_depth != 0) 645 return (ENXIO); 646 647 pp = cp->provider; 648 649 /* 650 * Sanity-check the provider. Since the first sector on the provider 651 * must be a PMBR and a PMBR is 512 bytes large, the sector size 652 * must be at least 512 bytes. Also, since the theoretical minimum 653 * number of sectors needed by GPT is 6, any medium that has less 654 * than 6 sectors is never going to be able to hold a GPT. The 655 * number 6 comes from: 656 * 1 sector for the PMBR 657 * 2 sectors for the GPT headers (each 1 sector) 658 * 2 sectors for the GPT tables (each 1 sector) 659 * 1 sector for an actual partition 660 * It's better to catch this pathological case early than behaving 661 * pathologically later on... 662 */ 663 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 664 return (ENOSPC); 665 666 /* Check that there's a MBR. */ 667 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 668 if (buf == NULL) 669 return (error); 670 res = le16dec(buf + DOSMAGICOFFSET); 671 g_free(buf); 672 if (res != DOSMAGIC) 673 return (ENXIO); 674 675 /* Check that there's a primary header. */ 676 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 677 if (buf == NULL) 678 return (error); 679 res = memcmp(buf, GPT_HDR_SIG, 8); 680 g_free(buf); 681 if (res == 0) 682 return (G_PART_PROBE_PRI_HIGH); 683 684 /* No primary? Check that there's a secondary. */ 685 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 686 &error); 687 if (buf == NULL) 688 return (error); 689 res = memcmp(buf, GPT_HDR_SIG, 8); 690 g_free(buf); 691 return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO); 692 } 693 694 static int 695 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 696 { 697 struct gpt_hdr *prihdr, *sechdr; 698 struct gpt_ent *tbl, *pritbl, *sectbl; 699 struct g_provider *pp; 700 struct g_part_gpt_table *table; 701 struct g_part_gpt_entry *entry; 702 u_char *buf; 703 uint64_t last; 704 int error, index; 705 706 table = (struct g_part_gpt_table *)basetable; 707 pp = cp->provider; 708 last = (pp->mediasize / pp->sectorsize) - 1; 709 710 /* Read the PMBR */ 711 buf = g_read_data(cp, 0, pp->sectorsize, &error); 712 if (buf == NULL) 713 return (error); 714 bcopy(buf, table->mbr, MBRSIZE); 715 g_free(buf); 716 717 /* Read the primary header and table. */ 718 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 719 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 720 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 721 } else { 722 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 723 pritbl = NULL; 724 } 725 726 /* Read the secondary header and table. */ 727 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 728 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 729 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 730 } else { 731 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 732 sectbl = NULL; 733 } 734 735 /* Fail if we haven't got any good tables at all. */ 736 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 737 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 738 printf("GEOM: %s: corrupt or invalid GPT detected.\n", 739 pp->name); 740 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 741 pp->name); 742 return (EINVAL); 743 } 744 745 /* 746 * If both headers are good but they disagree with each other, 747 * then invalidate one. We prefer to keep the primary header, 748 * unless the primary table is corrupt. 749 */ 750 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 751 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 752 !gpt_matched_hdrs(prihdr, sechdr)) { 753 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 754 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 755 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 756 g_free(sechdr); 757 sechdr = NULL; 758 } else { 759 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 760 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 761 g_free(prihdr); 762 prihdr = NULL; 763 } 764 } 765 766 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 767 printf("GEOM: %s: the primary GPT table is corrupt or " 768 "invalid.\n", pp->name); 769 printf("GEOM: %s: using the secondary instead -- recovery " 770 "strongly advised.\n", pp->name); 771 table->hdr = sechdr; 772 basetable->gpt_corrupt = 1; 773 if (prihdr != NULL) 774 g_free(prihdr); 775 tbl = sectbl; 776 if (pritbl != NULL) 777 g_free(pritbl); 778 } else { 779 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 780 printf("GEOM: %s: the secondary GPT table is corrupt " 781 "or invalid.\n", pp->name); 782 printf("GEOM: %s: using the primary only -- recovery " 783 "suggested.\n", pp->name); 784 basetable->gpt_corrupt = 1; 785 } else if (table->lba[GPT_ELT_SECHDR] != last) { 786 printf( "GEOM: %s: the secondary GPT header is not in " 787 "the last LBA.\n", pp->name); 788 basetable->gpt_corrupt = 1; 789 } 790 table->hdr = prihdr; 791 if (sechdr != NULL) 792 g_free(sechdr); 793 tbl = pritbl; 794 if (sectbl != NULL) 795 g_free(sectbl); 796 } 797 798 basetable->gpt_first = table->hdr->hdr_lba_start; 799 basetable->gpt_last = table->hdr->hdr_lba_end; 800 basetable->gpt_entries = table->hdr->hdr_entries; 801 802 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 803 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 804 continue; 805 entry = (struct g_part_gpt_entry *)g_part_new_entry( 806 basetable, index + 1, tbl[index].ent_lba_start, 807 tbl[index].ent_lba_end); 808 entry->ent = tbl[index]; 809 } 810 811 g_free(tbl); 812 return (0); 813 } 814 815 static int 816 g_part_gpt_recover(struct g_part_table *basetable) 817 { 818 struct g_part_gpt_table *table; 819 struct g_provider *pp; 820 uint64_t last; 821 size_t tblsz; 822 823 table = (struct g_part_gpt_table *)basetable; 824 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 825 last = pp->mediasize / pp->sectorsize - 1; 826 tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz + 827 pp->sectorsize - 1) / pp->sectorsize; 828 829 table->lba[GPT_ELT_PRIHDR] = 1; 830 table->lba[GPT_ELT_PRITBL] = 2; 831 table->lba[GPT_ELT_SECHDR] = last; 832 table->lba[GPT_ELT_SECTBL] = last - tblsz; 833 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 834 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 835 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 836 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 837 table->hdr->hdr_lba_start = 2 + tblsz; 838 table->hdr->hdr_lba_end = last - tblsz - 1; 839 840 basetable->gpt_first = table->hdr->hdr_lba_start; 841 basetable->gpt_last = table->hdr->hdr_lba_end; 842 basetable->gpt_corrupt = 0; 843 844 return (0); 845 } 846 847 static int 848 g_part_gpt_setunset(struct g_part_table *table, struct g_part_entry *baseentry, 849 const char *attrib, unsigned int set) 850 { 851 struct g_part_entry *iter; 852 struct g_part_gpt_entry *entry; 853 int changed, bootme, bootonce, bootfailed; 854 855 bootme = bootonce = bootfailed = 0; 856 if (strcasecmp(attrib, "bootme") == 0) { 857 bootme = 1; 858 } else if (strcasecmp(attrib, "bootonce") == 0) { 859 /* BOOTME is set automatically with BOOTONCE, but not unset. */ 860 bootonce = 1; 861 if (set) 862 bootme = 1; 863 } else if (strcasecmp(attrib, "bootfailed") == 0) { 864 /* 865 * It should only be possible to unset BOOTFAILED, but it might 866 * be useful for test purposes to also be able to set it. 867 */ 868 bootfailed = 1; 869 } 870 if (!bootme && !bootonce && !bootfailed) 871 return (EINVAL); 872 873 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 874 if (iter->gpe_deleted) 875 continue; 876 if (iter != baseentry) 877 continue; 878 changed = 0; 879 entry = (struct g_part_gpt_entry *)iter; 880 if (set) { 881 if (bootme && 882 !(entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)) { 883 entry->ent.ent_attr |= GPT_ENT_ATTR_BOOTME; 884 changed = 1; 885 } 886 if (bootonce && 887 !(entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE)) { 888 entry->ent.ent_attr |= GPT_ENT_ATTR_BOOTONCE; 889 changed = 1; 890 } 891 if (bootfailed && 892 !(entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED)) { 893 entry->ent.ent_attr |= GPT_ENT_ATTR_BOOTFAILED; 894 changed = 1; 895 } 896 } else { 897 if (bootme && 898 (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)) { 899 entry->ent.ent_attr &= ~GPT_ENT_ATTR_BOOTME; 900 changed = 1; 901 } 902 if (bootonce && 903 (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE)) { 904 entry->ent.ent_attr &= ~GPT_ENT_ATTR_BOOTONCE; 905 changed = 1; 906 } 907 if (bootfailed && 908 (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED)) { 909 entry->ent.ent_attr &= ~GPT_ENT_ATTR_BOOTFAILED; 910 changed = 1; 911 } 912 } 913 if (changed && !iter->gpe_created) 914 iter->gpe_modified = 1; 915 } 916 return (0); 917 } 918 919 static const char * 920 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 921 char *buf, size_t bufsz) 922 { 923 struct g_part_gpt_entry *entry; 924 struct uuid *type; 925 struct g_part_uuid_alias *uap; 926 927 entry = (struct g_part_gpt_entry *)baseentry; 928 type = &entry->ent.ent_type; 929 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 930 if (EQUUID(type, uap->uuid)) 931 return (g_part_alias_name(uap->alias)); 932 buf[0] = '!'; 933 snprintf_uuid(buf + 1, bufsz - 1, type); 934 935 return (buf); 936 } 937 938 static int 939 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 940 { 941 unsigned char *buf, *bp; 942 struct g_provider *pp; 943 struct g_part_entry *baseentry; 944 struct g_part_gpt_entry *entry; 945 struct g_part_gpt_table *table; 946 size_t tblsz; 947 uint32_t crc; 948 int error, index; 949 950 pp = cp->provider; 951 table = (struct g_part_gpt_table *)basetable; 952 tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz + 953 pp->sectorsize - 1) / pp->sectorsize; 954 955 /* Write the PMBR */ 956 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 957 bcopy(table->mbr, buf, MBRSIZE); 958 error = g_write_data(cp, 0, buf, pp->sectorsize); 959 g_free(buf); 960 if (error) 961 return (error); 962 963 /* Allocate space for the header and entries. */ 964 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 965 966 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 967 le32enc(buf + 8, table->hdr->hdr_revision); 968 le32enc(buf + 12, table->hdr->hdr_size); 969 le64enc(buf + 40, table->hdr->hdr_lba_start); 970 le64enc(buf + 48, table->hdr->hdr_lba_end); 971 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 972 le32enc(buf + 80, table->hdr->hdr_entries); 973 le32enc(buf + 84, table->hdr->hdr_entsz); 974 975 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 976 if (baseentry->gpe_deleted) 977 continue; 978 entry = (struct g_part_gpt_entry *)baseentry; 979 index = baseentry->gpe_index - 1; 980 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 981 le_uuid_enc(bp, &entry->ent.ent_type); 982 le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 983 le64enc(bp + 32, entry->ent.ent_lba_start); 984 le64enc(bp + 40, entry->ent.ent_lba_end); 985 le64enc(bp + 48, entry->ent.ent_attr); 986 memcpy(bp + 56, entry->ent.ent_name, 987 sizeof(entry->ent.ent_name)); 988 } 989 990 crc = crc32(buf + pp->sectorsize, 991 table->hdr->hdr_entries * table->hdr->hdr_entsz); 992 le32enc(buf + 88, crc); 993 994 /* Write primary meta-data. */ 995 le32enc(buf + 16, 0); /* hdr_crc_self. */ 996 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 997 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 998 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 999 crc = crc32(buf, table->hdr->hdr_size); 1000 le32enc(buf + 16, crc); 1001 1002 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1003 error = g_write_data(cp, 1004 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1005 buf + (index + 1) * pp->sectorsize, 1006 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1007 (tblsz - index) * pp->sectorsize); 1008 if (error) 1009 goto out; 1010 } 1011 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 1012 buf, pp->sectorsize); 1013 if (error) 1014 goto out; 1015 1016 /* Write secondary meta-data. */ 1017 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1018 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 1019 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 1020 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 1021 crc = crc32(buf, table->hdr->hdr_size); 1022 le32enc(buf + 16, crc); 1023 1024 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1025 error = g_write_data(cp, 1026 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1027 buf + (index + 1) * pp->sectorsize, 1028 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1029 (tblsz - index) * pp->sectorsize); 1030 if (error) 1031 goto out; 1032 } 1033 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 1034 buf, pp->sectorsize); 1035 1036 out: 1037 g_free(buf); 1038 return (error); 1039 } 1040 1041 static void 1042 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 1043 { 1044 u_int bo; 1045 uint32_t ch; 1046 uint16_t c; 1047 1048 bo = LITTLE_ENDIAN; /* GPT is little-endian */ 1049 while (len > 0 && *str != 0) { 1050 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 1051 str++, len--; 1052 if ((ch & 0xf800) == 0xd800) { 1053 if (len > 0) { 1054 c = (bo == BIG_ENDIAN) ? be16toh(*str) 1055 : le16toh(*str); 1056 str++, len--; 1057 } else 1058 c = 0xfffd; 1059 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 1060 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 1061 ch += 0x10000; 1062 } else 1063 ch = 0xfffd; 1064 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 1065 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 1066 continue; 1067 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 1068 continue; 1069 1070 /* Write the Unicode character in UTF-8 */ 1071 if (ch < 0x80) 1072 sbuf_printf(sb, "%c", ch); 1073 else if (ch < 0x800) 1074 sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6), 1075 0x80 | (ch & 0x3f)); 1076 else if (ch < 0x10000) 1077 sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12), 1078 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1079 else if (ch < 0x200000) 1080 sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18), 1081 0x80 | ((ch >> 12) & 0x3f), 1082 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1083 } 1084 } 1085 1086 static void 1087 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1088 { 1089 size_t s16idx, s8idx; 1090 uint32_t utfchar; 1091 unsigned int c, utfbytes; 1092 1093 s8idx = s16idx = 0; 1094 utfchar = 0; 1095 utfbytes = 0; 1096 bzero(s16, s16len << 1); 1097 while (s8[s8idx] != 0 && s16idx < s16len) { 1098 c = s8[s8idx++]; 1099 if ((c & 0xc0) != 0x80) { 1100 /* Initial characters. */ 1101 if (utfbytes != 0) { 1102 /* Incomplete encoding of previous char. */ 1103 s16[s16idx++] = htole16(0xfffd); 1104 } 1105 if ((c & 0xf8) == 0xf0) { 1106 utfchar = c & 0x07; 1107 utfbytes = 3; 1108 } else if ((c & 0xf0) == 0xe0) { 1109 utfchar = c & 0x0f; 1110 utfbytes = 2; 1111 } else if ((c & 0xe0) == 0xc0) { 1112 utfchar = c & 0x1f; 1113 utfbytes = 1; 1114 } else { 1115 utfchar = c & 0x7f; 1116 utfbytes = 0; 1117 } 1118 } else { 1119 /* Followup characters. */ 1120 if (utfbytes > 0) { 1121 utfchar = (utfchar << 6) + (c & 0x3f); 1122 utfbytes--; 1123 } else if (utfbytes == 0) 1124 utfbytes = ~0; 1125 } 1126 /* 1127 * Write the complete Unicode character as UTF-16 when we 1128 * have all the UTF-8 charactars collected. 1129 */ 1130 if (utfbytes == 0) { 1131 /* 1132 * If we need to write 2 UTF-16 characters, but 1133 * we only have room for 1, then we truncate the 1134 * string by writing a 0 instead. 1135 */ 1136 if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1137 s16[s16idx++] = 1138 htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1139 s16[s16idx++] = 1140 htole16(0xdc00 | (utfchar & 0x3ff)); 1141 } else 1142 s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1143 htole16(utfchar); 1144 } 1145 } 1146 /* 1147 * If our input string was truncated, append an invalid encoding 1148 * character to the output string. 1149 */ 1150 if (utfbytes != 0 && s16idx < s16len) 1151 s16[s16idx++] = htole16(0xfffd); 1152 } 1153