1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/bio.h> 34 #include <sys/diskmbr.h> 35 #include <sys/gsb_crc32.h> 36 #include <sys/endian.h> 37 #include <sys/gpt.h> 38 #include <sys/kernel.h> 39 #include <sys/kobj.h> 40 #include <sys/limits.h> 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/mutex.h> 44 #include <sys/queue.h> 45 #include <sys/sbuf.h> 46 #include <sys/systm.h> 47 #include <sys/sysctl.h> 48 #include <sys/uuid.h> 49 #include <geom/geom.h> 50 #include <geom/geom_int.h> 51 #include <geom/part/g_part.h> 52 53 #include "g_part_if.h" 54 55 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support"); 56 57 SYSCTL_DECL(_kern_geom_part); 58 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt, 59 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 60 "GEOM_PART_GPT GUID Partition Table"); 61 62 static u_int allow_nesting = 0; 63 SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting, 64 CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes"); 65 66 CTASSERT(offsetof(struct gpt_hdr, padding) == 92); 67 CTASSERT(sizeof(struct gpt_ent) == 128); 68 69 #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 70 71 #define MBRSIZE 512 72 73 enum gpt_elt { 74 GPT_ELT_PRIHDR, 75 GPT_ELT_PRITBL, 76 GPT_ELT_SECHDR, 77 GPT_ELT_SECTBL, 78 GPT_ELT_COUNT 79 }; 80 81 enum gpt_state { 82 GPT_STATE_UNKNOWN, /* Not determined. */ 83 GPT_STATE_MISSING, /* No signature found. */ 84 GPT_STATE_CORRUPT, /* Checksum mismatch. */ 85 GPT_STATE_INVALID, /* Nonconformant/invalid. */ 86 GPT_STATE_OK /* Perfectly fine. */ 87 }; 88 89 struct g_part_gpt_table { 90 struct g_part_table base; 91 u_char mbr[MBRSIZE]; 92 struct gpt_hdr *hdr; 93 quad_t lba[GPT_ELT_COUNT]; 94 enum gpt_state state[GPT_ELT_COUNT]; 95 int bootcamp; 96 }; 97 98 struct g_part_gpt_entry { 99 struct g_part_entry base; 100 struct gpt_ent ent; 101 }; 102 103 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t); 104 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t); 105 static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *); 106 107 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 108 struct g_part_parms *); 109 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 110 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 111 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 112 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 113 struct sbuf *, const char *); 114 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 115 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 116 struct g_part_parms *); 117 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 118 char *, size_t); 119 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 120 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 121 static int g_part_gpt_setunset(struct g_part_table *table, 122 struct g_part_entry *baseentry, const char *attrib, unsigned int set); 123 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 124 char *, size_t); 125 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 126 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 127 struct g_part_parms *); 128 static int g_part_gpt_recover(struct g_part_table *); 129 130 static kobj_method_t g_part_gpt_methods[] = { 131 KOBJMETHOD(g_part_add, g_part_gpt_add), 132 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 133 KOBJMETHOD(g_part_create, g_part_gpt_create), 134 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 135 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 136 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 137 KOBJMETHOD(g_part_modify, g_part_gpt_modify), 138 KOBJMETHOD(g_part_resize, g_part_gpt_resize), 139 KOBJMETHOD(g_part_name, g_part_gpt_name), 140 KOBJMETHOD(g_part_probe, g_part_gpt_probe), 141 KOBJMETHOD(g_part_read, g_part_gpt_read), 142 KOBJMETHOD(g_part_recover, g_part_gpt_recover), 143 KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 144 KOBJMETHOD(g_part_type, g_part_gpt_type), 145 KOBJMETHOD(g_part_write, g_part_gpt_write), 146 { 0, 0 } 147 }; 148 149 static struct g_part_scheme g_part_gpt_scheme = { 150 "GPT", 151 g_part_gpt_methods, 152 sizeof(struct g_part_gpt_table), 153 .gps_entrysz = sizeof(struct g_part_gpt_entry), 154 .gps_minent = 128, 155 .gps_maxent = 4096, 156 .gps_bootcodesz = MBRSIZE, 157 }; 158 G_PART_SCHEME_DECLARE(g_part_gpt); 159 MODULE_VERSION(geom_part_gpt, 0); 160 161 static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS; 162 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 163 static struct uuid gpt_uuid_apple_core_storage = 164 GPT_ENT_TYPE_APPLE_CORE_STORAGE; 165 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 166 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 167 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 168 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 169 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 170 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 171 static struct uuid gpt_uuid_apple_zfs = GPT_ENT_TYPE_APPLE_ZFS; 172 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 173 static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE; 174 static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; 175 static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED; 176 static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT; 177 static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD; 178 static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER; 179 static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2; 180 static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32; 181 static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64; 182 static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY; 183 static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP; 184 static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1; 185 static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM; 186 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 187 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 188 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 189 static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 190 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 191 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 192 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 193 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 194 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 195 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 196 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 197 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 198 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 199 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 200 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 201 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 202 static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY; 203 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 204 static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES; 205 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 206 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 207 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 208 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 209 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 210 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 211 static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA; 212 static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT; 213 static struct uuid gpt_uuid_solaris_boot = GPT_ENT_TYPE_SOLARIS_BOOT; 214 static struct uuid gpt_uuid_solaris_root = GPT_ENT_TYPE_SOLARIS_ROOT; 215 static struct uuid gpt_uuid_solaris_swap = GPT_ENT_TYPE_SOLARIS_SWAP; 216 static struct uuid gpt_uuid_solaris_backup = GPT_ENT_TYPE_SOLARIS_BACKUP; 217 static struct uuid gpt_uuid_solaris_var = GPT_ENT_TYPE_SOLARIS_VAR; 218 static struct uuid gpt_uuid_solaris_home = GPT_ENT_TYPE_SOLARIS_HOME; 219 static struct uuid gpt_uuid_solaris_altsec = GPT_ENT_TYPE_SOLARIS_ALTSEC; 220 static struct uuid gpt_uuid_solaris_reserved = GPT_ENT_TYPE_SOLARIS_RESERVED; 221 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 222 static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 223 static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 224 static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 225 static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR; 226 227 static struct g_part_uuid_alias { 228 struct uuid *uuid; 229 int alias; 230 int mbrtype; 231 } gpt_uuid_alias_match[] = { 232 { &gpt_uuid_apple_apfs, G_PART_ALIAS_APPLE_APFS, 0 }, 233 { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 234 { &gpt_uuid_apple_core_storage, G_PART_ALIAS_APPLE_CORE_STORAGE, 0 }, 235 { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 236 { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 237 { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 238 { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 239 { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 240 { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 241 { &gpt_uuid_apple_zfs, G_PART_ALIAS_APPLE_ZFS, 0 }, 242 { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 243 { &gpt_uuid_chromeos_firmware, G_PART_ALIAS_CHROMEOS_FIRMWARE, 0 }, 244 { &gpt_uuid_chromeos_kernel, G_PART_ALIAS_CHROMEOS_KERNEL, 0 }, 245 { &gpt_uuid_chromeos_reserved, G_PART_ALIAS_CHROMEOS_RESERVED, 0 }, 246 { &gpt_uuid_chromeos_root, G_PART_ALIAS_CHROMEOS_ROOT, 0 }, 247 { &gpt_uuid_dfbsd_ccd, G_PART_ALIAS_DFBSD_CCD, 0 }, 248 { &gpt_uuid_dfbsd_hammer, G_PART_ALIAS_DFBSD_HAMMER, 0 }, 249 { &gpt_uuid_dfbsd_hammer2, G_PART_ALIAS_DFBSD_HAMMER2, 0 }, 250 { &gpt_uuid_dfbsd_label32, G_PART_ALIAS_DFBSD, 0xa5 }, 251 { &gpt_uuid_dfbsd_label64, G_PART_ALIAS_DFBSD64, 0xa5 }, 252 { &gpt_uuid_dfbsd_legacy, G_PART_ALIAS_DFBSD_LEGACY, 0 }, 253 { &gpt_uuid_dfbsd_swap, G_PART_ALIAS_DFBSD_SWAP, 0 }, 254 { &gpt_uuid_dfbsd_ufs1, G_PART_ALIAS_DFBSD_UFS, 0 }, 255 { &gpt_uuid_dfbsd_vinum, G_PART_ALIAS_DFBSD_VINUM, 0 }, 256 { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 257 { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 258 { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 259 { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 }, 260 { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 261 { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 262 { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 263 { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 264 { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 265 { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 266 { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 267 { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 268 { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 269 { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 270 { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 271 { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 272 { &gpt_uuid_ms_recovery, G_PART_ALIAS_MS_RECOVERY, 0 }, 273 { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 274 { &gpt_uuid_ms_spaces, G_PART_ALIAS_MS_SPACES, 0 }, 275 { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 276 { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 277 { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 278 { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 279 { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 280 { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 281 { &gpt_uuid_openbsd_data, G_PART_ALIAS_OPENBSD_DATA, 0 }, 282 { &gpt_uuid_prep_boot, G_PART_ALIAS_PREP_BOOT, 0x41 }, 283 { &gpt_uuid_solaris_boot, G_PART_ALIAS_SOLARIS_BOOT, 0 }, 284 { &gpt_uuid_solaris_root, G_PART_ALIAS_SOLARIS_ROOT, 0 }, 285 { &gpt_uuid_solaris_swap, G_PART_ALIAS_SOLARIS_SWAP, 0 }, 286 { &gpt_uuid_solaris_backup, G_PART_ALIAS_SOLARIS_BACKUP, 0 }, 287 { &gpt_uuid_solaris_var, G_PART_ALIAS_SOLARIS_VAR, 0 }, 288 { &gpt_uuid_solaris_home, G_PART_ALIAS_SOLARIS_HOME, 0 }, 289 { &gpt_uuid_solaris_altsec, G_PART_ALIAS_SOLARIS_ALTSEC, 0 }, 290 { &gpt_uuid_solaris_reserved, G_PART_ALIAS_SOLARIS_RESERVED, 0 }, 291 { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 292 { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 293 { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 294 { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 }, 295 { NULL, 0, 0 } 296 }; 297 298 static int 299 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 300 quad_t end) 301 { 302 303 if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 304 return (EINVAL); 305 306 mbr += DOSPARTOFF + idx * DOSPARTSIZE; 307 mbr[0] = 0; 308 if (start == 1) { 309 /* 310 * Treat the PMBR partition specially to maximize 311 * interoperability with BIOSes. 312 */ 313 mbr[1] = mbr[3] = 0; 314 mbr[2] = 2; 315 } else 316 mbr[1] = mbr[2] = mbr[3] = 0xff; 317 mbr[4] = typ; 318 mbr[5] = mbr[6] = mbr[7] = 0xff; 319 le32enc(mbr + 8, (uint32_t)start); 320 le32enc(mbr + 12, (uint32_t)(end - start + 1)); 321 return (0); 322 } 323 324 static int 325 gpt_map_type(struct uuid *t) 326 { 327 struct g_part_uuid_alias *uap; 328 329 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 330 if (EQUUID(t, uap->uuid)) 331 return (uap->mbrtype); 332 } 333 return (0); 334 } 335 336 static void 337 gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 338 { 339 340 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 341 gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 342 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 343 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 344 } 345 346 /* 347 * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 348 * whole disk anymore. Rather, it covers the GPT table and the EFI 349 * system partition only. This way the HFS+ partition and any FAT 350 * partitions can be added to the MBR without creating an overlap. 351 */ 352 static int 353 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 354 { 355 uint8_t *p; 356 357 p = table->mbr + DOSPARTOFF; 358 if (p[4] != 0xee || le32dec(p + 8) != 1) 359 return (0); 360 361 p += DOSPARTSIZE; 362 if (p[4] != 0xaf) 363 return (0); 364 365 printf("GEOM: %s: enabling Boot Camp\n", provname); 366 return (1); 367 } 368 369 static void 370 gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 371 { 372 struct g_part_entry *baseentry; 373 struct g_part_gpt_entry *entry; 374 struct g_part_gpt_table *table; 375 int bootable, error, index, slices, typ; 376 377 table = (struct g_part_gpt_table *)basetable; 378 379 bootable = -1; 380 for (index = 0; index < NDOSPART; index++) { 381 if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 382 bootable = index; 383 } 384 385 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 386 slices = 0; 387 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 388 if (baseentry->gpe_deleted) 389 continue; 390 index = baseentry->gpe_index - 1; 391 if (index >= NDOSPART) 392 continue; 393 394 entry = (struct g_part_gpt_entry *)baseentry; 395 396 switch (index) { 397 case 0: /* This must be the EFI system partition. */ 398 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 399 goto disable; 400 error = gpt_write_mbr_entry(table->mbr, index, 0xee, 401 1ull, entry->ent.ent_lba_end); 402 break; 403 case 1: /* This must be the HFS+ partition. */ 404 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 405 goto disable; 406 error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 407 entry->ent.ent_lba_start, entry->ent.ent_lba_end); 408 break; 409 default: 410 typ = gpt_map_type(&entry->ent.ent_type); 411 error = gpt_write_mbr_entry(table->mbr, index, typ, 412 entry->ent.ent_lba_start, entry->ent.ent_lba_end); 413 break; 414 } 415 if (error) 416 continue; 417 418 if (index == bootable) 419 table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 420 slices |= 1 << index; 421 } 422 if ((slices & 3) == 3) 423 return; 424 425 disable: 426 table->bootcamp = 0; 427 gpt_create_pmbr(table, pp); 428 } 429 430 static struct gpt_hdr * 431 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 432 enum gpt_elt elt) 433 { 434 struct gpt_hdr *buf, *hdr; 435 struct g_provider *pp; 436 quad_t lba, last; 437 int error; 438 uint32_t crc, sz; 439 440 pp = cp->provider; 441 last = (pp->mediasize / pp->sectorsize) - 1; 442 table->state[elt] = GPT_STATE_MISSING; 443 /* 444 * If the primary header is valid look for secondary 445 * header in AlternateLBA, otherwise in the last medium's LBA. 446 */ 447 if (elt == GPT_ELT_SECHDR) { 448 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 449 table->lba[elt] = last; 450 } else 451 table->lba[elt] = 1; 452 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 453 &error); 454 if (buf == NULL) 455 return (NULL); 456 hdr = NULL; 457 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 458 goto fail; 459 460 table->state[elt] = GPT_STATE_CORRUPT; 461 sz = le32toh(buf->hdr_size); 462 if (sz < 92 || sz > pp->sectorsize) 463 goto fail; 464 465 hdr = g_malloc(sz, M_WAITOK | M_ZERO); 466 bcopy(buf, hdr, sz); 467 hdr->hdr_size = sz; 468 469 crc = le32toh(buf->hdr_crc_self); 470 buf->hdr_crc_self = 0; 471 if (crc32(buf, sz) != crc) 472 goto fail; 473 hdr->hdr_crc_self = crc; 474 475 table->state[elt] = GPT_STATE_INVALID; 476 hdr->hdr_revision = le32toh(buf->hdr_revision); 477 if (hdr->hdr_revision < GPT_HDR_REVISION) 478 goto fail; 479 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 480 if (hdr->hdr_lba_self != table->lba[elt]) 481 goto fail; 482 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 483 if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 484 hdr->hdr_lba_alt > last) 485 goto fail; 486 487 /* Check the managed area. */ 488 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 489 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 490 goto fail; 491 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 492 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 493 goto fail; 494 495 /* Check the table location and size of the table. */ 496 hdr->hdr_entries = le32toh(buf->hdr_entries); 497 hdr->hdr_entsz = le32toh(buf->hdr_entsz); 498 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 499 (hdr->hdr_entsz & 7) != 0) 500 goto fail; 501 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 502 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 503 goto fail; 504 if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 505 hdr->hdr_lba_table <= hdr->hdr_lba_end) 506 goto fail; 507 lba = hdr->hdr_lba_table + 508 howmany(hdr->hdr_entries * hdr->hdr_entsz, pp->sectorsize) - 1; 509 if (lba >= last) 510 goto fail; 511 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 512 goto fail; 513 514 table->state[elt] = GPT_STATE_OK; 515 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 516 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 517 518 /* save LBA for secondary header */ 519 if (elt == GPT_ELT_PRIHDR) 520 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 521 522 g_free(buf); 523 return (hdr); 524 525 fail: 526 if (hdr != NULL) 527 g_free(hdr); 528 g_free(buf); 529 return (NULL); 530 } 531 532 static struct gpt_ent * 533 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 534 enum gpt_elt elt, struct gpt_hdr *hdr) 535 { 536 struct g_provider *pp; 537 struct gpt_ent *ent, *tbl; 538 char *buf, *p; 539 unsigned int idx, sectors, tblsz, size; 540 int error; 541 542 if (hdr == NULL) 543 return (NULL); 544 545 pp = cp->provider; 546 table->lba[elt] = hdr->hdr_lba_table; 547 548 table->state[elt] = GPT_STATE_MISSING; 549 tblsz = hdr->hdr_entries * hdr->hdr_entsz; 550 sectors = howmany(tblsz, pp->sectorsize); 551 buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 552 for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 553 size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 554 (sectors - idx) * pp->sectorsize; 555 p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 556 size, &error); 557 if (p == NULL) { 558 g_free(buf); 559 return (NULL); 560 } 561 bcopy(p, buf + idx * pp->sectorsize, size); 562 g_free(p); 563 } 564 table->state[elt] = GPT_STATE_CORRUPT; 565 if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 566 g_free(buf); 567 return (NULL); 568 } 569 570 table->state[elt] = GPT_STATE_OK; 571 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 572 M_WAITOK | M_ZERO); 573 574 for (idx = 0, ent = tbl, p = buf; 575 idx < hdr->hdr_entries; 576 idx++, ent++, p += hdr->hdr_entsz) { 577 le_uuid_dec(p, &ent->ent_type); 578 le_uuid_dec(p + 16, &ent->ent_uuid); 579 ent->ent_lba_start = le64dec(p + 32); 580 ent->ent_lba_end = le64dec(p + 40); 581 ent->ent_attr = le64dec(p + 48); 582 /* Keep UTF-16 in little-endian. */ 583 bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 584 } 585 586 g_free(buf); 587 return (tbl); 588 } 589 590 static int 591 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 592 { 593 594 if (pri == NULL || sec == NULL) 595 return (0); 596 597 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 598 return (0); 599 return ((pri->hdr_revision == sec->hdr_revision && 600 pri->hdr_size == sec->hdr_size && 601 pri->hdr_lba_start == sec->hdr_lba_start && 602 pri->hdr_lba_end == sec->hdr_lba_end && 603 pri->hdr_entries == sec->hdr_entries && 604 pri->hdr_entsz == sec->hdr_entsz && 605 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 606 } 607 608 static int 609 gpt_parse_type(const char *type, struct uuid *uuid) 610 { 611 struct uuid tmp; 612 const char *alias; 613 int error; 614 struct g_part_uuid_alias *uap; 615 616 if (type[0] == '!') { 617 error = parse_uuid(type + 1, &tmp); 618 if (error) 619 return (error); 620 if (EQUUID(&tmp, &gpt_uuid_unused)) 621 return (EINVAL); 622 *uuid = tmp; 623 return (0); 624 } 625 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 626 alias = g_part_alias_name(uap->alias); 627 if (!strcasecmp(type, alias)) { 628 *uuid = *uap->uuid; 629 return (0); 630 } 631 } 632 return (EINVAL); 633 } 634 635 static int 636 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 637 struct g_part_parms *gpp) 638 { 639 struct g_part_gpt_entry *entry; 640 int error; 641 642 entry = (struct g_part_gpt_entry *)baseentry; 643 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 644 if (error) 645 return (error); 646 kern_uuidgen(&entry->ent.ent_uuid, 1); 647 entry->ent.ent_lba_start = baseentry->gpe_start; 648 entry->ent.ent_lba_end = baseentry->gpe_end; 649 if (baseentry->gpe_deleted) { 650 entry->ent.ent_attr = 0; 651 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 652 } 653 if (gpp->gpp_parms & G_PART_PARM_LABEL) 654 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 655 sizeof(entry->ent.ent_name) / 656 sizeof(entry->ent.ent_name[0])); 657 return (0); 658 } 659 660 static int 661 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 662 { 663 struct g_part_gpt_table *table; 664 size_t codesz; 665 666 codesz = DOSPARTOFF; 667 table = (struct g_part_gpt_table *)basetable; 668 bzero(table->mbr, codesz); 669 codesz = MIN(codesz, gpp->gpp_codesize); 670 if (codesz > 0) 671 bcopy(gpp->gpp_codeptr, table->mbr, codesz); 672 return (0); 673 } 674 675 static int 676 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 677 { 678 struct g_provider *pp; 679 struct g_part_gpt_table *table; 680 size_t tblsz; 681 682 /* Our depth should be 0 unless nesting was explicitly enabled. */ 683 if (!allow_nesting && basetable->gpt_depth != 0) 684 return (ENXIO); 685 686 table = (struct g_part_gpt_table *)basetable; 687 pp = gpp->gpp_provider; 688 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 689 pp->sectorsize); 690 if (pp->sectorsize < MBRSIZE || 691 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 692 pp->sectorsize) 693 return (ENOSPC); 694 695 gpt_create_pmbr(table, pp); 696 697 /* Allocate space for the header */ 698 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 699 700 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 701 table->hdr->hdr_revision = GPT_HDR_REVISION; 702 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 703 kern_uuidgen(&table->hdr->hdr_uuid, 1); 704 table->hdr->hdr_entries = basetable->gpt_entries; 705 table->hdr->hdr_entsz = sizeof(struct gpt_ent); 706 707 g_gpt_set_defaults(basetable, pp); 708 return (0); 709 } 710 711 static int 712 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 713 { 714 struct g_part_gpt_table *table; 715 struct g_provider *pp; 716 717 table = (struct g_part_gpt_table *)basetable; 718 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 719 g_free(table->hdr); 720 table->hdr = NULL; 721 722 /* 723 * Wipe the first 2 sectors and last one to clear the partitioning. 724 * Wipe sectors only if they have valid metadata. 725 */ 726 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) 727 basetable->gpt_smhead |= 3; 728 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 729 table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 730 basetable->gpt_smtail |= 1; 731 return (0); 732 } 733 734 static void 735 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 736 struct sbuf *sb, const char *indent) 737 { 738 struct g_part_gpt_entry *entry; 739 740 entry = (struct g_part_gpt_entry *)baseentry; 741 if (indent == NULL) { 742 /* conftxt: libdisk compatibility */ 743 sbuf_cat(sb, " xs GPT xt "); 744 sbuf_printf_uuid(sb, &entry->ent.ent_type); 745 } else if (entry != NULL) { 746 /* confxml: partition entry information */ 747 sbuf_printf(sb, "%s<label>", indent); 748 g_gpt_printf_utf16(sb, entry->ent.ent_name, 749 sizeof(entry->ent.ent_name) >> 1); 750 sbuf_cat(sb, "</label>\n"); 751 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 752 sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 753 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 754 sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 755 indent); 756 } 757 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 758 sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 759 indent); 760 } 761 sbuf_printf(sb, "%s<rawtype>", indent); 762 sbuf_printf_uuid(sb, &entry->ent.ent_type); 763 sbuf_cat(sb, "</rawtype>\n"); 764 sbuf_printf(sb, "%s<rawuuid>", indent); 765 sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 766 sbuf_cat(sb, "</rawuuid>\n"); 767 sbuf_printf(sb, "%s<efimedia>", indent); 768 sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index); 769 sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 770 sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start, 771 (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1)); 772 sbuf_cat(sb, "</efimedia>\n"); 773 } else { 774 /* confxml: scheme information */ 775 } 776 } 777 778 static int 779 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 780 { 781 struct g_part_gpt_entry *entry; 782 783 entry = (struct g_part_gpt_entry *)baseentry; 784 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 785 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) || 786 EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0); 787 } 788 789 static int 790 g_part_gpt_modify(struct g_part_table *basetable, 791 struct g_part_entry *baseentry, struct g_part_parms *gpp) 792 { 793 struct g_part_gpt_entry *entry; 794 int error; 795 796 entry = (struct g_part_gpt_entry *)baseentry; 797 if (gpp->gpp_parms & G_PART_PARM_TYPE) { 798 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 799 if (error) 800 return (error); 801 } 802 if (gpp->gpp_parms & G_PART_PARM_LABEL) 803 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 804 sizeof(entry->ent.ent_name) / 805 sizeof(entry->ent.ent_name[0])); 806 return (0); 807 } 808 809 static int 810 g_part_gpt_resize(struct g_part_table *basetable, 811 struct g_part_entry *baseentry, struct g_part_parms *gpp) 812 { 813 struct g_part_gpt_entry *entry; 814 815 if (baseentry == NULL) 816 return (g_part_gpt_recover(basetable)); 817 818 entry = (struct g_part_gpt_entry *)baseentry; 819 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 820 entry->ent.ent_lba_end = baseentry->gpe_end; 821 822 return (0); 823 } 824 825 static const char * 826 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 827 char *buf, size_t bufsz) 828 { 829 struct g_part_gpt_entry *entry; 830 char c; 831 832 entry = (struct g_part_gpt_entry *)baseentry; 833 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 834 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 835 return (buf); 836 } 837 838 static int 839 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 840 { 841 struct g_provider *pp; 842 u_char *buf; 843 int error, index, pri, res; 844 845 /* Our depth should be 0 unless nesting was explicitly enabled. */ 846 if (!allow_nesting && table->gpt_depth != 0) 847 return (ENXIO); 848 849 pp = cp->provider; 850 851 /* 852 * Sanity-check the provider. Since the first sector on the provider 853 * must be a PMBR and a PMBR is 512 bytes large, the sector size 854 * must be at least 512 bytes. Also, since the theoretical minimum 855 * number of sectors needed by GPT is 6, any medium that has less 856 * than 6 sectors is never going to be able to hold a GPT. The 857 * number 6 comes from: 858 * 1 sector for the PMBR 859 * 2 sectors for the GPT headers (each 1 sector) 860 * 2 sectors for the GPT tables (each 1 sector) 861 * 1 sector for an actual partition 862 * It's better to catch this pathological case early than behaving 863 * pathologically later on... 864 */ 865 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 866 return (ENOSPC); 867 868 /* 869 * Check that there's a MBR or a PMBR. If it's a PMBR, we return 870 * as the highest priority on a match, otherwise we assume some 871 * GPT-unaware tool has destroyed the GPT by recreating a MBR and 872 * we really want the MBR scheme to take precedence. 873 */ 874 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 875 if (buf == NULL) 876 return (error); 877 res = le16dec(buf + DOSMAGICOFFSET); 878 pri = G_PART_PROBE_PRI_LOW; 879 if (res == DOSMAGIC) { 880 for (index = 0; index < NDOSPART; index++) { 881 if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee) 882 pri = G_PART_PROBE_PRI_HIGH; 883 } 884 g_free(buf); 885 886 /* Check that there's a primary header. */ 887 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 888 if (buf == NULL) 889 return (error); 890 res = memcmp(buf, GPT_HDR_SIG, 8); 891 g_free(buf); 892 if (res == 0) 893 return (pri); 894 } else 895 g_free(buf); 896 897 /* No primary? Check that there's a secondary. */ 898 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 899 &error); 900 if (buf == NULL) 901 return (error); 902 res = memcmp(buf, GPT_HDR_SIG, 8); 903 g_free(buf); 904 return ((res == 0) ? pri : ENXIO); 905 } 906 907 static int 908 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 909 { 910 struct gpt_hdr *prihdr, *sechdr; 911 struct gpt_ent *tbl, *pritbl, *sectbl; 912 struct g_provider *pp; 913 struct g_part_gpt_table *table; 914 struct g_part_gpt_entry *entry; 915 u_char *buf; 916 uint64_t last; 917 int error, index; 918 919 table = (struct g_part_gpt_table *)basetable; 920 pp = cp->provider; 921 last = (pp->mediasize / pp->sectorsize) - 1; 922 923 /* Read the PMBR */ 924 buf = g_read_data(cp, 0, pp->sectorsize, &error); 925 if (buf == NULL) 926 return (error); 927 bcopy(buf, table->mbr, MBRSIZE); 928 g_free(buf); 929 930 /* Read the primary header and table. */ 931 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 932 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 933 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 934 } else { 935 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 936 pritbl = NULL; 937 } 938 939 /* Read the secondary header and table. */ 940 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 941 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 942 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 943 } else { 944 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 945 sectbl = NULL; 946 } 947 948 /* Fail if we haven't got any good tables at all. */ 949 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 950 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 951 printf("GEOM: %s: corrupt or invalid GPT detected.\n", 952 pp->name); 953 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 954 pp->name); 955 if (prihdr != NULL) 956 g_free(prihdr); 957 if (pritbl != NULL) 958 g_free(pritbl); 959 if (sechdr != NULL) 960 g_free(sechdr); 961 if (sectbl != NULL) 962 g_free(sectbl); 963 return (EINVAL); 964 } 965 966 /* 967 * If both headers are good but they disagree with each other, 968 * then invalidate one. We prefer to keep the primary header, 969 * unless the primary table is corrupt. 970 */ 971 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 972 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 973 !gpt_matched_hdrs(prihdr, sechdr)) { 974 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 975 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 976 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 977 g_free(sechdr); 978 sechdr = NULL; 979 } else { 980 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 981 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 982 g_free(prihdr); 983 prihdr = NULL; 984 } 985 } 986 987 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 988 printf("GEOM: %s: the primary GPT table is corrupt or " 989 "invalid.\n", pp->name); 990 printf("GEOM: %s: using the secondary instead -- recovery " 991 "strongly advised.\n", pp->name); 992 table->hdr = sechdr; 993 basetable->gpt_corrupt = 1; 994 if (prihdr != NULL) 995 g_free(prihdr); 996 tbl = sectbl; 997 if (pritbl != NULL) 998 g_free(pritbl); 999 } else { 1000 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 1001 printf("GEOM: %s: the secondary GPT table is corrupt " 1002 "or invalid.\n", pp->name); 1003 printf("GEOM: %s: using the primary only -- recovery " 1004 "suggested.\n", pp->name); 1005 basetable->gpt_corrupt = 1; 1006 } else if (table->lba[GPT_ELT_SECHDR] != last) { 1007 printf( "GEOM: %s: the secondary GPT header is not in " 1008 "the last LBA.\n", pp->name); 1009 basetable->gpt_corrupt = 1; 1010 } 1011 table->hdr = prihdr; 1012 if (sechdr != NULL) 1013 g_free(sechdr); 1014 tbl = pritbl; 1015 if (sectbl != NULL) 1016 g_free(sectbl); 1017 } 1018 1019 basetable->gpt_first = table->hdr->hdr_lba_start; 1020 basetable->gpt_last = table->hdr->hdr_lba_end; 1021 basetable->gpt_entries = table->hdr->hdr_entries; 1022 1023 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 1024 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 1025 continue; 1026 entry = (struct g_part_gpt_entry *)g_part_new_entry( 1027 basetable, index + 1, tbl[index].ent_lba_start, 1028 tbl[index].ent_lba_end); 1029 entry->ent = tbl[index]; 1030 } 1031 1032 g_free(tbl); 1033 1034 /* 1035 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 1036 * if (and only if) any FAT32 or FAT16 partitions have been 1037 * created. This happens irrespective of whether Boot Camp is 1038 * used/enabled, though it's generally understood to be done 1039 * to support legacy Windows under Boot Camp. We refer to this 1040 * mirroring simply as Boot Camp. We try to detect Boot Camp 1041 * so that we can update the MBR if and when GPT changes have 1042 * been made. Note that we do not enable Boot Camp if not 1043 * previously enabled because we can't assume that we're on a 1044 * Mac alongside Mac OS X. 1045 */ 1046 table->bootcamp = gpt_is_bootcamp(table, pp->name); 1047 1048 return (0); 1049 } 1050 1051 static int 1052 g_part_gpt_recover(struct g_part_table *basetable) 1053 { 1054 struct g_part_gpt_table *table; 1055 struct g_provider *pp; 1056 1057 table = (struct g_part_gpt_table *)basetable; 1058 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 1059 gpt_create_pmbr(table, pp); 1060 g_gpt_set_defaults(basetable, pp); 1061 basetable->gpt_corrupt = 0; 1062 return (0); 1063 } 1064 1065 static int 1066 g_part_gpt_setunset(struct g_part_table *basetable, 1067 struct g_part_entry *baseentry, const char *attrib, unsigned int set) 1068 { 1069 struct g_part_gpt_entry *entry; 1070 struct g_part_gpt_table *table; 1071 struct g_provider *pp; 1072 uint8_t *p; 1073 uint64_t attr; 1074 int i; 1075 1076 table = (struct g_part_gpt_table *)basetable; 1077 entry = (struct g_part_gpt_entry *)baseentry; 1078 1079 if (strcasecmp(attrib, "active") == 0) { 1080 if (table->bootcamp) { 1081 /* The active flag must be set on a valid entry. */ 1082 if (entry == NULL) 1083 return (ENXIO); 1084 if (baseentry->gpe_index > NDOSPART) 1085 return (EINVAL); 1086 for (i = 0; i < NDOSPART; i++) { 1087 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 1088 p[0] = (i == baseentry->gpe_index - 1) 1089 ? ((set) ? 0x80 : 0) : 0; 1090 } 1091 } else { 1092 /* The PMBR is marked as active without an entry. */ 1093 if (entry != NULL) 1094 return (ENXIO); 1095 for (i = 0; i < NDOSPART; i++) { 1096 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 1097 p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 1098 } 1099 } 1100 return (0); 1101 } else if (strcasecmp(attrib, "lenovofix") == 0) { 1102 /* 1103 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR. 1104 * This workaround allows Lenovo X220, T420, T520, etc to boot 1105 * from GPT Partitions in BIOS mode. 1106 */ 1107 1108 if (entry != NULL) 1109 return (ENXIO); 1110 1111 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 1112 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 1113 gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1, 1114 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 1115 return (0); 1116 } 1117 1118 if (entry == NULL) 1119 return (ENODEV); 1120 1121 attr = 0; 1122 if (strcasecmp(attrib, "bootme") == 0) { 1123 attr |= GPT_ENT_ATTR_BOOTME; 1124 } else if (strcasecmp(attrib, "bootonce") == 0) { 1125 attr |= GPT_ENT_ATTR_BOOTONCE; 1126 if (set) 1127 attr |= GPT_ENT_ATTR_BOOTME; 1128 } else if (strcasecmp(attrib, "bootfailed") == 0) { 1129 /* 1130 * It should only be possible to unset BOOTFAILED, but it might 1131 * be useful for test purposes to also be able to set it. 1132 */ 1133 attr |= GPT_ENT_ATTR_BOOTFAILED; 1134 } 1135 if (attr == 0) 1136 return (EINVAL); 1137 1138 if (set) 1139 attr = entry->ent.ent_attr | attr; 1140 else 1141 attr = entry->ent.ent_attr & ~attr; 1142 if (attr != entry->ent.ent_attr) { 1143 entry->ent.ent_attr = attr; 1144 if (!baseentry->gpe_created) 1145 baseentry->gpe_modified = 1; 1146 } 1147 return (0); 1148 } 1149 1150 static const char * 1151 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 1152 char *buf, size_t bufsz) 1153 { 1154 struct g_part_gpt_entry *entry; 1155 struct uuid *type; 1156 struct g_part_uuid_alias *uap; 1157 1158 entry = (struct g_part_gpt_entry *)baseentry; 1159 type = &entry->ent.ent_type; 1160 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1161 if (EQUUID(type, uap->uuid)) 1162 return (g_part_alias_name(uap->alias)); 1163 buf[0] = '!'; 1164 snprintf_uuid(buf + 1, bufsz - 1, type); 1165 1166 return (buf); 1167 } 1168 1169 static int 1170 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 1171 { 1172 unsigned char *buf, *bp; 1173 struct g_provider *pp; 1174 struct g_part_entry *baseentry; 1175 struct g_part_gpt_entry *entry; 1176 struct g_part_gpt_table *table; 1177 size_t tblsz; 1178 uint32_t crc; 1179 int error, index; 1180 1181 pp = cp->provider; 1182 table = (struct g_part_gpt_table *)basetable; 1183 tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz, 1184 pp->sectorsize); 1185 1186 /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1187 if (table->bootcamp) 1188 gpt_update_bootcamp(basetable, pp); 1189 1190 /* Write the PMBR */ 1191 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 1192 bcopy(table->mbr, buf, MBRSIZE); 1193 error = g_write_data(cp, 0, buf, pp->sectorsize); 1194 g_free(buf); 1195 if (error) 1196 return (error); 1197 1198 /* Allocate space for the header and entries. */ 1199 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 1200 1201 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 1202 le32enc(buf + 8, table->hdr->hdr_revision); 1203 le32enc(buf + 12, table->hdr->hdr_size); 1204 le64enc(buf + 40, table->hdr->hdr_lba_start); 1205 le64enc(buf + 48, table->hdr->hdr_lba_end); 1206 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 1207 le32enc(buf + 80, table->hdr->hdr_entries); 1208 le32enc(buf + 84, table->hdr->hdr_entsz); 1209 1210 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1211 if (baseentry->gpe_deleted) 1212 continue; 1213 entry = (struct g_part_gpt_entry *)baseentry; 1214 index = baseentry->gpe_index - 1; 1215 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 1216 le_uuid_enc(bp, &entry->ent.ent_type); 1217 le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 1218 le64enc(bp + 32, entry->ent.ent_lba_start); 1219 le64enc(bp + 40, entry->ent.ent_lba_end); 1220 le64enc(bp + 48, entry->ent.ent_attr); 1221 memcpy(bp + 56, entry->ent.ent_name, 1222 sizeof(entry->ent.ent_name)); 1223 } 1224 1225 crc = crc32(buf + pp->sectorsize, 1226 table->hdr->hdr_entries * table->hdr->hdr_entsz); 1227 le32enc(buf + 88, crc); 1228 1229 /* Write primary meta-data. */ 1230 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1231 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 1232 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 1233 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 1234 crc = crc32(buf, table->hdr->hdr_size); 1235 le32enc(buf + 16, crc); 1236 1237 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1238 error = g_write_data(cp, 1239 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1240 buf + (index + 1) * pp->sectorsize, 1241 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1242 (tblsz - index) * pp->sectorsize); 1243 if (error) 1244 goto out; 1245 } 1246 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 1247 buf, pp->sectorsize); 1248 if (error) 1249 goto out; 1250 1251 /* Write secondary meta-data. */ 1252 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1253 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 1254 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 1255 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 1256 crc = crc32(buf, table->hdr->hdr_size); 1257 le32enc(buf + 16, crc); 1258 1259 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1260 error = g_write_data(cp, 1261 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1262 buf + (index + 1) * pp->sectorsize, 1263 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1264 (tblsz - index) * pp->sectorsize); 1265 if (error) 1266 goto out; 1267 } 1268 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 1269 buf, pp->sectorsize); 1270 1271 out: 1272 g_free(buf); 1273 return (error); 1274 } 1275 1276 static void 1277 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 1278 { 1279 struct g_part_entry *baseentry; 1280 struct g_part_gpt_entry *entry; 1281 struct g_part_gpt_table *table; 1282 quad_t start, end, min, max; 1283 quad_t lba, last; 1284 size_t spb, tblsz; 1285 1286 table = (struct g_part_gpt_table *)basetable; 1287 last = pp->mediasize / pp->sectorsize - 1; 1288 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 1289 pp->sectorsize); 1290 1291 table->lba[GPT_ELT_PRIHDR] = 1; 1292 table->lba[GPT_ELT_PRITBL] = 2; 1293 table->lba[GPT_ELT_SECHDR] = last; 1294 table->lba[GPT_ELT_SECTBL] = last - tblsz; 1295 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 1296 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 1297 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 1298 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 1299 1300 max = start = 2 + tblsz; 1301 min = end = last - tblsz - 1; 1302 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1303 if (baseentry->gpe_deleted) 1304 continue; 1305 entry = (struct g_part_gpt_entry *)baseentry; 1306 if (entry->ent.ent_lba_start < min) 1307 min = entry->ent.ent_lba_start; 1308 if (entry->ent.ent_lba_end > max) 1309 max = entry->ent.ent_lba_end; 1310 } 1311 spb = 4096 / pp->sectorsize; 1312 if (spb > 1) { 1313 lba = start + ((start % spb) ? spb - start % spb : 0); 1314 if (lba <= min) 1315 start = lba; 1316 lba = end - (end + 1) % spb; 1317 if (max <= lba) 1318 end = lba; 1319 } 1320 table->hdr->hdr_lba_start = start; 1321 table->hdr->hdr_lba_end = end; 1322 1323 basetable->gpt_first = start; 1324 basetable->gpt_last = end; 1325 } 1326 1327 static void 1328 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 1329 { 1330 u_int bo; 1331 uint32_t ch; 1332 uint16_t c; 1333 1334 bo = LITTLE_ENDIAN; /* GPT is little-endian */ 1335 while (len > 0 && *str != 0) { 1336 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 1337 str++, len--; 1338 if ((ch & 0xf800) == 0xd800) { 1339 if (len > 0) { 1340 c = (bo == BIG_ENDIAN) ? be16toh(*str) 1341 : le16toh(*str); 1342 str++, len--; 1343 } else 1344 c = 0xfffd; 1345 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 1346 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 1347 ch += 0x10000; 1348 } else 1349 ch = 0xfffd; 1350 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 1351 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 1352 continue; 1353 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 1354 continue; 1355 1356 /* Write the Unicode character in UTF-8 */ 1357 if (ch < 0x80) 1358 g_conf_printf_escaped(sb, "%c", ch); 1359 else if (ch < 0x800) 1360 g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6), 1361 0x80 | (ch & 0x3f)); 1362 else if (ch < 0x10000) 1363 g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12), 1364 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1365 else if (ch < 0x200000) 1366 g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 | 1367 (ch >> 18), 0x80 | ((ch >> 12) & 0x3f), 1368 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1369 } 1370 } 1371 1372 static void 1373 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1374 { 1375 size_t s16idx, s8idx; 1376 uint32_t utfchar; 1377 unsigned int c, utfbytes; 1378 1379 s8idx = s16idx = 0; 1380 utfchar = 0; 1381 utfbytes = 0; 1382 bzero(s16, s16len << 1); 1383 while (s8[s8idx] != 0 && s16idx < s16len) { 1384 c = s8[s8idx++]; 1385 if ((c & 0xc0) != 0x80) { 1386 /* Initial characters. */ 1387 if (utfbytes != 0) { 1388 /* Incomplete encoding of previous char. */ 1389 s16[s16idx++] = htole16(0xfffd); 1390 } 1391 if ((c & 0xf8) == 0xf0) { 1392 utfchar = c & 0x07; 1393 utfbytes = 3; 1394 } else if ((c & 0xf0) == 0xe0) { 1395 utfchar = c & 0x0f; 1396 utfbytes = 2; 1397 } else if ((c & 0xe0) == 0xc0) { 1398 utfchar = c & 0x1f; 1399 utfbytes = 1; 1400 } else { 1401 utfchar = c & 0x7f; 1402 utfbytes = 0; 1403 } 1404 } else { 1405 /* Followup characters. */ 1406 if (utfbytes > 0) { 1407 utfchar = (utfchar << 6) + (c & 0x3f); 1408 utfbytes--; 1409 } else if (utfbytes == 0) 1410 utfbytes = ~0; 1411 } 1412 /* 1413 * Write the complete Unicode character as UTF-16 when we 1414 * have all the UTF-8 charactars collected. 1415 */ 1416 if (utfbytes == 0) { 1417 /* 1418 * If we need to write 2 UTF-16 characters, but 1419 * we only have room for 1, then we truncate the 1420 * string by writing a 0 instead. 1421 */ 1422 if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1423 s16[s16idx++] = 1424 htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1425 s16[s16idx++] = 1426 htole16(0xdc00 | (utfchar & 0x3ff)); 1427 } else 1428 s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1429 htole16(utfchar); 1430 } 1431 } 1432 /* 1433 * If our input string was truncated, append an invalid encoding 1434 * character to the output string. 1435 */ 1436 if (utfbytes != 0 && s16idx < s16len) 1437 s16[s16idx++] = htole16(0xfffd); 1438 } 1439