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