1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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/param.h> 30 #include <sys/bio.h> 31 #include <sys/diskmbr.h> 32 #include <sys/gsb_crc32.h> 33 #include <sys/endian.h> 34 #include <sys/gpt.h> 35 #include <sys/kernel.h> 36 #include <sys/kobj.h> 37 #include <sys/limits.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/queue.h> 42 #include <sys/sbuf.h> 43 #include <sys/systm.h> 44 #include <sys/sysctl.h> 45 #include <sys/uuid.h> 46 #include <geom/geom.h> 47 #include <geom/geom_int.h> 48 #include <geom/part/g_part.h> 49 50 #include "g_part_if.h" 51 52 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support"); 53 54 SYSCTL_DECL(_kern_geom_part); 55 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt, 56 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 57 "GEOM_PART_GPT GUID Partition Table"); 58 59 static u_int allow_nesting = 0; 60 SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting, 61 CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes"); 62 63 CTASSERT(offsetof(struct gpt_hdr, padding) == 92); 64 CTASSERT(sizeof(struct gpt_ent) == 128); 65 66 extern u_int geom_part_check_integrity; 67 68 #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 69 70 #define MBRSIZE 512 71 72 enum gpt_elt { 73 GPT_ELT_PRIHDR, 74 GPT_ELT_PRITBL, 75 GPT_ELT_SECHDR, 76 GPT_ELT_SECTBL, 77 GPT_ELT_COUNT 78 }; 79 80 enum gpt_state { 81 GPT_STATE_UNKNOWN, /* Not determined. */ 82 GPT_STATE_MISSING, /* No signature found. */ 83 GPT_STATE_CORRUPT, /* Checksum mismatch. */ 84 GPT_STATE_INVALID, /* Nonconformant/invalid. */ 85 GPT_STATE_UNSUPPORTED, /* Not supported. */ 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 struct g_part_parms *); 107 108 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 109 struct g_part_parms *); 110 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 111 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 112 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 113 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 114 struct sbuf *, const char *); 115 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 116 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 117 struct g_part_parms *); 118 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 119 char *, size_t); 120 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 121 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 122 static int g_part_gpt_setunset(struct g_part_table *table, 123 struct g_part_entry *baseentry, const char *attrib, unsigned int set); 124 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 125 char *, size_t); 126 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 127 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 128 struct g_part_parms *); 129 static int g_part_gpt_recover(struct g_part_table *); 130 131 static kobj_method_t g_part_gpt_methods[] = { 132 KOBJMETHOD(g_part_add, g_part_gpt_add), 133 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 134 KOBJMETHOD(g_part_create, g_part_gpt_create), 135 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 136 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 137 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 138 KOBJMETHOD(g_part_modify, g_part_gpt_modify), 139 KOBJMETHOD(g_part_resize, g_part_gpt_resize), 140 KOBJMETHOD(g_part_name, g_part_gpt_name), 141 KOBJMETHOD(g_part_probe, g_part_gpt_probe), 142 KOBJMETHOD(g_part_read, g_part_gpt_read), 143 KOBJMETHOD(g_part_recover, g_part_gpt_recover), 144 KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 145 KOBJMETHOD(g_part_type, g_part_gpt_type), 146 KOBJMETHOD(g_part_write, g_part_gpt_write), 147 { 0, 0 } 148 }; 149 150 #define MAXENTSIZE 1024 151 152 static struct g_part_scheme g_part_gpt_scheme = { 153 "GPT", 154 g_part_gpt_methods, 155 sizeof(struct g_part_gpt_table), 156 .gps_entrysz = sizeof(struct g_part_gpt_entry), 157 .gps_minent = 1, 158 .gps_defent = 128, 159 .gps_maxent = 4096, 160 .gps_bootcodesz = MBRSIZE, 161 }; 162 G_PART_SCHEME_DECLARE(g_part_gpt); 163 MODULE_VERSION(geom_part_gpt, 0); 164 165 static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS; 166 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 167 static struct uuid gpt_uuid_apple_core_storage = 168 GPT_ENT_TYPE_APPLE_CORE_STORAGE; 169 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 170 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 171 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 172 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 173 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 174 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 175 static struct uuid gpt_uuid_apple_zfs = GPT_ENT_TYPE_APPLE_ZFS; 176 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 177 static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE; 178 static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; 179 static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED; 180 static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT; 181 static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD; 182 static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER; 183 static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2; 184 static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32; 185 static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64; 186 static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY; 187 static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP; 188 static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1; 189 static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM; 190 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 191 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 192 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 193 static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 194 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 195 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 196 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 197 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 198 static struct uuid gpt_uuid_hifive_fsbl = GPT_ENT_TYPE_HIFIVE_FSBL; 199 static struct uuid gpt_uuid_hifive_bbl = GPT_ENT_TYPE_HIFIVE_BBL; 200 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 201 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 202 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 203 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 204 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 205 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 206 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 207 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 208 static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY; 209 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 210 static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES; 211 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 212 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 213 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 214 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 215 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 216 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 217 static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA; 218 static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT; 219 static struct uuid gpt_uuid_solaris_boot = GPT_ENT_TYPE_SOLARIS_BOOT; 220 static struct uuid gpt_uuid_solaris_root = GPT_ENT_TYPE_SOLARIS_ROOT; 221 static struct uuid gpt_uuid_solaris_swap = GPT_ENT_TYPE_SOLARIS_SWAP; 222 static struct uuid gpt_uuid_solaris_backup = GPT_ENT_TYPE_SOLARIS_BACKUP; 223 static struct uuid gpt_uuid_solaris_var = GPT_ENT_TYPE_SOLARIS_VAR; 224 static struct uuid gpt_uuid_solaris_home = GPT_ENT_TYPE_SOLARIS_HOME; 225 static struct uuid gpt_uuid_solaris_altsec = GPT_ENT_TYPE_SOLARIS_ALTSEC; 226 static struct uuid gpt_uuid_solaris_reserved = GPT_ENT_TYPE_SOLARIS_RESERVED; 227 static struct uuid gpt_uuid_u_boot_env = GPT_ENT_TYPE_U_BOOT_ENV; 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_u_boot_env, G_PART_ALIAS_U_BOOT_ENV, 0 }, 301 { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 302 { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 303 { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 304 { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 }, 305 { NULL, 0, 0 } 306 }; 307 308 static int 309 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 310 quad_t end) 311 { 312 313 if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 314 return (EINVAL); 315 316 mbr += DOSPARTOFF + idx * DOSPARTSIZE; 317 mbr[0] = 0; 318 if (start == 1) { 319 /* 320 * Treat the PMBR partition specially to maximize 321 * interoperability with BIOSes. 322 */ 323 mbr[1] = mbr[3] = 0; 324 mbr[2] = 2; 325 } else 326 mbr[1] = mbr[2] = mbr[3] = 0xff; 327 mbr[4] = typ; 328 mbr[5] = mbr[6] = mbr[7] = 0xff; 329 le32enc(mbr + 8, (uint32_t)start); 330 le32enc(mbr + 12, (uint32_t)(end - start + 1)); 331 return (0); 332 } 333 334 static int 335 gpt_map_type(struct uuid *t) 336 { 337 struct g_part_uuid_alias *uap; 338 339 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 340 if (EQUUID(t, uap->uuid)) 341 return (uap->mbrtype); 342 } 343 return (0); 344 } 345 346 static void 347 gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 348 { 349 350 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 351 gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 352 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 353 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 354 } 355 356 /* 357 * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 358 * whole disk anymore. Rather, it covers the GPT table and the EFI 359 * system partition only. This way the HFS+ partition and any FAT 360 * partitions can be added to the MBR without creating an overlap. 361 */ 362 static int 363 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 364 { 365 uint8_t *p; 366 367 p = table->mbr + DOSPARTOFF; 368 if (p[4] != 0xee || le32dec(p + 8) != 1) 369 return (0); 370 371 p += DOSPARTSIZE; 372 if (p[4] != 0xaf) 373 return (0); 374 375 printf("GEOM: %s: enabling Boot Camp\n", provname); 376 return (1); 377 } 378 379 static void 380 gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 381 { 382 struct g_part_entry *baseentry; 383 struct g_part_gpt_entry *entry; 384 struct g_part_gpt_table *table; 385 int bootable, error, index, slices, typ; 386 387 table = (struct g_part_gpt_table *)basetable; 388 389 bootable = -1; 390 for (index = 0; index < NDOSPART; index++) { 391 if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 392 bootable = index; 393 } 394 395 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 396 slices = 0; 397 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 398 if (baseentry->gpe_deleted) 399 continue; 400 index = baseentry->gpe_index - 1; 401 if (index >= NDOSPART) 402 continue; 403 404 entry = (struct g_part_gpt_entry *)baseentry; 405 406 switch (index) { 407 case 0: /* This must be the EFI system partition. */ 408 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 409 goto disable; 410 error = gpt_write_mbr_entry(table->mbr, index, 0xee, 411 1ull, entry->ent.ent_lba_end); 412 break; 413 case 1: /* This must be the HFS+ partition. */ 414 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 415 goto disable; 416 error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 417 entry->ent.ent_lba_start, entry->ent.ent_lba_end); 418 break; 419 default: 420 typ = gpt_map_type(&entry->ent.ent_type); 421 error = gpt_write_mbr_entry(table->mbr, index, typ, 422 entry->ent.ent_lba_start, entry->ent.ent_lba_end); 423 break; 424 } 425 if (error) 426 continue; 427 428 if (index == bootable) 429 table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 430 slices |= 1 << index; 431 } 432 if ((slices & 3) == 3) 433 return; 434 435 disable: 436 table->bootcamp = 0; 437 gpt_create_pmbr(table, pp); 438 } 439 440 static struct gpt_hdr * 441 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 442 enum gpt_elt elt) 443 { 444 struct gpt_hdr *buf, *hdr; 445 struct g_provider *pp; 446 quad_t lba, last; 447 int error; 448 uint32_t crc, sz; 449 450 pp = cp->provider; 451 last = (pp->mediasize / pp->sectorsize) - 1; 452 table->state[elt] = GPT_STATE_MISSING; 453 /* 454 * If the primary header is valid look for secondary 455 * header in AlternateLBA, otherwise in the last medium's LBA. 456 */ 457 if (elt == GPT_ELT_SECHDR) { 458 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 459 table->lba[elt] = last; 460 } else 461 table->lba[elt] = 1; 462 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 463 &error); 464 if (buf == NULL) 465 return (NULL); 466 hdr = NULL; 467 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 468 goto fail; 469 470 table->state[elt] = GPT_STATE_CORRUPT; 471 sz = le32toh(buf->hdr_size); 472 if (sz < 92 || sz > pp->sectorsize) 473 goto fail; 474 475 hdr = g_malloc(sz, M_WAITOK | M_ZERO); 476 bcopy(buf, hdr, sz); 477 hdr->hdr_size = sz; 478 479 crc = le32toh(buf->hdr_crc_self); 480 buf->hdr_crc_self = 0; 481 if (crc32(buf, sz) != crc) 482 goto fail; 483 hdr->hdr_crc_self = crc; 484 485 table->state[elt] = GPT_STATE_INVALID; 486 hdr->hdr_revision = le32toh(buf->hdr_revision); 487 if (hdr->hdr_revision < GPT_HDR_REVISION) 488 goto fail; 489 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 490 if (hdr->hdr_lba_self != table->lba[elt]) 491 goto fail; 492 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 493 if (hdr->hdr_lba_alt == hdr->hdr_lba_self) 494 goto fail; 495 if (hdr->hdr_lba_alt > last && geom_part_check_integrity) 496 goto fail; 497 498 /* Check the managed area. */ 499 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 500 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 501 goto fail; 502 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 503 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 504 goto fail; 505 506 /* Check the table location and size of the table. */ 507 hdr->hdr_entries = le32toh(buf->hdr_entries); 508 hdr->hdr_entsz = le32toh(buf->hdr_entsz); 509 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 510 (hdr->hdr_entsz & 7) != 0) 511 goto fail; 512 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 513 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 514 goto fail; 515 if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 516 hdr->hdr_lba_table <= hdr->hdr_lba_end) 517 goto fail; 518 lba = hdr->hdr_lba_table + 519 howmany((uint64_t)hdr->hdr_entries * hdr->hdr_entsz, 520 pp->sectorsize) - 1; 521 if (lba >= last) 522 goto fail; 523 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 524 goto fail; 525 526 table->state[elt] = GPT_STATE_OK; 527 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 528 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 529 530 /* save LBA for secondary header */ 531 if (elt == GPT_ELT_PRIHDR) 532 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 533 534 g_free(buf); 535 return (hdr); 536 537 fail: 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, gpp); 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 g_free(prihdr); 993 g_free(pritbl); 994 g_free(sechdr); 995 g_free(sectbl); 996 return (EINVAL); 997 } 998 999 /* 1000 * If both headers are good but they disagree with each other, 1001 * then invalidate one. We prefer to keep the primary header, 1002 * unless the primary table is corrupt. 1003 */ 1004 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 1005 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 1006 !gpt_matched_hdrs(prihdr, sechdr)) { 1007 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 1008 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 1009 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 1010 g_free(sechdr); 1011 sechdr = NULL; 1012 } else { 1013 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 1014 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 1015 g_free(prihdr); 1016 prihdr = NULL; 1017 } 1018 } 1019 1020 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 1021 printf("GEOM: %s: the primary GPT table is corrupt or " 1022 "invalid.\n", pp->name); 1023 printf("GEOM: %s: using the secondary instead -- recovery " 1024 "strongly advised.\n", pp->name); 1025 table->hdr = sechdr; 1026 basetable->gpt_corrupt = 1; 1027 g_free(prihdr); 1028 tbl = sectbl; 1029 g_free(pritbl); 1030 } else { 1031 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 1032 printf("GEOM: %s: the secondary GPT table is corrupt " 1033 "or invalid.\n", pp->name); 1034 printf("GEOM: %s: using the primary only -- recovery " 1035 "suggested.\n", pp->name); 1036 basetable->gpt_corrupt = 1; 1037 } else if (table->lba[GPT_ELT_SECHDR] != last) { 1038 printf( "GEOM: %s: the secondary GPT header is not in " 1039 "the last LBA.\n", pp->name); 1040 basetable->gpt_corrupt = 1; 1041 } 1042 table->hdr = prihdr; 1043 g_free(sechdr); 1044 tbl = pritbl; 1045 g_free(sectbl); 1046 } 1047 1048 /* 1049 * The reserved area preceeds the valid area for partitions. Warn when 1050 * the lba_start doesn't meet the standard's minimum size for the gpt 1051 * entry array. UEFI 2.10 section 5.3 specifies that the LBA must be 32 1052 * (for 512 byte sectors) or 6 (4k sectors) or larger. This is different 1053 * than the number of valid entries in the GPT entry array, which can be 1054 * smaller. 1055 */ 1056 if (table->hdr->hdr_lba_start < GPT_MIN_RESERVED / pp->sectorsize + 2) { 1057 printf("GEOM: warning: %s lba_start %llu < required min %d\n", 1058 pp->name, (unsigned long long)table->hdr->hdr_lba_start, 1059 GPT_MIN_RESERVED / pp->sectorsize + 2); 1060 } 1061 1062 basetable->gpt_first = table->hdr->hdr_lba_start; 1063 basetable->gpt_last = table->hdr->hdr_lba_end; 1064 basetable->gpt_entries = table->hdr->hdr_entries; 1065 1066 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 1067 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 1068 continue; 1069 entry = (struct g_part_gpt_entry *)g_part_new_entry( 1070 basetable, index + 1, tbl[index].ent_lba_start, 1071 tbl[index].ent_lba_end); 1072 entry->ent = tbl[index]; 1073 } 1074 1075 g_free(tbl); 1076 1077 /* 1078 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 1079 * if (and only if) any FAT32 or FAT16 partitions have been 1080 * created. This happens irrespective of whether Boot Camp is 1081 * used/enabled, though it's generally understood to be done 1082 * to support legacy Windows under Boot Camp. We refer to this 1083 * mirroring simply as Boot Camp. We try to detect Boot Camp 1084 * so that we can update the MBR if and when GPT changes have 1085 * been made. Note that we do not enable Boot Camp if not 1086 * previously enabled because we can't assume that we're on a 1087 * Mac alongside Mac OS X. 1088 */ 1089 table->bootcamp = gpt_is_bootcamp(table, pp->name); 1090 1091 return (0); 1092 } 1093 1094 static int 1095 g_part_gpt_recover(struct g_part_table *basetable) 1096 { 1097 struct g_part_gpt_table *table; 1098 struct g_provider *pp; 1099 1100 table = (struct g_part_gpt_table *)basetable; 1101 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 1102 gpt_create_pmbr(table, pp); 1103 g_gpt_set_defaults(basetable, pp, NULL); 1104 basetable->gpt_corrupt = 0; 1105 return (0); 1106 } 1107 1108 static int 1109 g_part_gpt_setunset(struct g_part_table *basetable, 1110 struct g_part_entry *baseentry, const char *attrib, unsigned int set) 1111 { 1112 struct g_part_gpt_entry *entry; 1113 struct g_part_gpt_table *table; 1114 struct g_provider *pp; 1115 uint8_t *p; 1116 uint64_t attr; 1117 int i; 1118 1119 table = (struct g_part_gpt_table *)basetable; 1120 entry = (struct g_part_gpt_entry *)baseentry; 1121 1122 if (strcasecmp(attrib, "active") == 0) { 1123 if (table->bootcamp) { 1124 /* The active flag must be set on a valid entry. */ 1125 if (entry == NULL) 1126 return (ENXIO); 1127 if (baseentry->gpe_index > NDOSPART) 1128 return (EINVAL); 1129 for (i = 0; i < NDOSPART; i++) { 1130 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 1131 p[0] = (i == baseentry->gpe_index - 1) 1132 ? ((set) ? 0x80 : 0) : 0; 1133 } 1134 } else { 1135 /* The PMBR is marked as active without an entry. */ 1136 if (entry != NULL) 1137 return (ENXIO); 1138 for (i = 0; i < NDOSPART; i++) { 1139 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 1140 p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 1141 } 1142 } 1143 return (0); 1144 } else if (strcasecmp(attrib, "lenovofix") == 0) { 1145 /* 1146 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR. 1147 * This workaround allows Lenovo X220, T420, T520, etc to boot 1148 * from GPT Partitions in BIOS mode. 1149 */ 1150 1151 if (entry != NULL) 1152 return (ENXIO); 1153 1154 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 1155 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 1156 gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1, 1157 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 1158 return (0); 1159 } 1160 1161 if (entry == NULL) 1162 return (ENODEV); 1163 1164 attr = 0; 1165 if (strcasecmp(attrib, "bootme") == 0) { 1166 attr |= GPT_ENT_ATTR_BOOTME; 1167 } else if (strcasecmp(attrib, "bootonce") == 0) { 1168 attr |= GPT_ENT_ATTR_BOOTONCE; 1169 if (set) 1170 attr |= GPT_ENT_ATTR_BOOTME; 1171 } else if (strcasecmp(attrib, "bootfailed") == 0) { 1172 /* 1173 * It should only be possible to unset BOOTFAILED, but it might 1174 * be useful for test purposes to also be able to set it. 1175 */ 1176 attr |= GPT_ENT_ATTR_BOOTFAILED; 1177 } 1178 if (attr == 0) 1179 return (EINVAL); 1180 1181 if (set) 1182 attr = entry->ent.ent_attr | attr; 1183 else 1184 attr = entry->ent.ent_attr & ~attr; 1185 if (attr != entry->ent.ent_attr) { 1186 entry->ent.ent_attr = attr; 1187 if (!baseentry->gpe_created) 1188 baseentry->gpe_modified = 1; 1189 } 1190 return (0); 1191 } 1192 1193 static const char * 1194 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 1195 char *buf, size_t bufsz) 1196 { 1197 struct g_part_gpt_entry *entry; 1198 struct uuid *type; 1199 struct g_part_uuid_alias *uap; 1200 1201 entry = (struct g_part_gpt_entry *)baseentry; 1202 type = &entry->ent.ent_type; 1203 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1204 if (EQUUID(type, uap->uuid)) 1205 return (g_part_alias_name(uap->alias)); 1206 buf[0] = '!'; 1207 snprintf_uuid(buf + 1, bufsz - 1, type); 1208 1209 return (buf); 1210 } 1211 1212 static int 1213 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 1214 { 1215 unsigned char *buf, *bp; 1216 struct g_provider *pp; 1217 struct g_part_entry *baseentry; 1218 struct g_part_gpt_entry *entry; 1219 struct g_part_gpt_table *table; 1220 size_t tblsz; 1221 uint32_t crc; 1222 int error, index; 1223 1224 pp = cp->provider; 1225 table = (struct g_part_gpt_table *)basetable; 1226 tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz, 1227 pp->sectorsize); 1228 1229 /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1230 if (table->bootcamp) 1231 gpt_update_bootcamp(basetable, pp); 1232 1233 /* Write the PMBR */ 1234 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 1235 bcopy(table->mbr, buf, MBRSIZE); 1236 error = g_write_data(cp, 0, buf, pp->sectorsize); 1237 g_free(buf); 1238 if (error) 1239 return (error); 1240 1241 /* Allocate space for the header and entries. */ 1242 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 1243 1244 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 1245 le32enc(buf + 8, table->hdr->hdr_revision); 1246 le32enc(buf + 12, table->hdr->hdr_size); 1247 le64enc(buf + 40, table->hdr->hdr_lba_start); 1248 le64enc(buf + 48, table->hdr->hdr_lba_end); 1249 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 1250 le32enc(buf + 80, table->hdr->hdr_entries); 1251 le32enc(buf + 84, table->hdr->hdr_entsz); 1252 1253 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1254 if (baseentry->gpe_deleted) 1255 continue; 1256 entry = (struct g_part_gpt_entry *)baseentry; 1257 index = baseentry->gpe_index - 1; 1258 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 1259 le_uuid_enc(bp, &entry->ent.ent_type); 1260 le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 1261 le64enc(bp + 32, entry->ent.ent_lba_start); 1262 le64enc(bp + 40, entry->ent.ent_lba_end); 1263 le64enc(bp + 48, entry->ent.ent_attr); 1264 memcpy(bp + 56, entry->ent.ent_name, 1265 sizeof(entry->ent.ent_name)); 1266 } 1267 1268 crc = crc32(buf + pp->sectorsize, 1269 table->hdr->hdr_entries * table->hdr->hdr_entsz); 1270 le32enc(buf + 88, crc); 1271 1272 /* Write primary meta-data. */ 1273 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1274 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 1275 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 1276 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 1277 crc = crc32(buf, table->hdr->hdr_size); 1278 le32enc(buf + 16, crc); 1279 1280 for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) { 1281 error = g_write_data(cp, 1282 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1283 buf + (index + 1) * pp->sectorsize, 1284 (tblsz - index > maxphys / pp->sectorsize) ? maxphys : 1285 (tblsz - index) * pp->sectorsize); 1286 if (error) 1287 goto out; 1288 } 1289 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 1290 buf, pp->sectorsize); 1291 if (error) 1292 goto out; 1293 1294 /* Write secondary meta-data. */ 1295 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1296 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 1297 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 1298 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 1299 crc = crc32(buf, table->hdr->hdr_size); 1300 le32enc(buf + 16, crc); 1301 1302 for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) { 1303 error = g_write_data(cp, 1304 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1305 buf + (index + 1) * pp->sectorsize, 1306 (tblsz - index > maxphys / pp->sectorsize) ? maxphys : 1307 (tblsz - index) * pp->sectorsize); 1308 if (error) 1309 goto out; 1310 } 1311 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 1312 buf, pp->sectorsize); 1313 1314 out: 1315 g_free(buf); 1316 return (error); 1317 } 1318 1319 static void 1320 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp, 1321 struct g_part_parms *gpp) 1322 { 1323 struct g_part_entry *baseentry; 1324 struct g_part_gpt_entry *entry; 1325 struct g_part_gpt_table *table; 1326 quad_t start, end, min, max; 1327 quad_t lba, last; 1328 size_t spb, tblsz; 1329 1330 table = (struct g_part_gpt_table *)basetable; 1331 last = pp->mediasize / pp->sectorsize - 1; 1332 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 1333 pp->sectorsize); 1334 1335 table->lba[GPT_ELT_PRIHDR] = 1; 1336 table->lba[GPT_ELT_PRITBL] = 2; 1337 table->lba[GPT_ELT_SECHDR] = last; 1338 table->lba[GPT_ELT_SECTBL] = last - tblsz; 1339 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 1340 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 1341 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 1342 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 1343 1344 max = start = 2 + tblsz; 1345 min = end = last - tblsz - 1; 1346 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1347 if (baseentry->gpe_deleted) 1348 continue; 1349 entry = (struct g_part_gpt_entry *)baseentry; 1350 if (entry->ent.ent_lba_start < min) 1351 min = entry->ent.ent_lba_start; 1352 if (entry->ent.ent_lba_end > max) 1353 max = entry->ent.ent_lba_end; 1354 } 1355 /* 1356 * Don't force alignment of any kind whatsoever on resize, restore or 1357 * recover. resize doesn't go through this path, recover has a NULL gpp 1358 * and restore has flags == restore (maybe with an appended 'C' to 1359 * commit the operation). For these operations, we have to trust the 1360 * user knows what they are doing. 1361 * 1362 * Otherwise it some flavor of creation of a new partition, so we align 1363 * to a 4k offset on the drive, to make 512e/4kn drives more performant 1364 * by default. 1365 */ 1366 if (gpp == NULL || 1367 (gpp->gpp_parms & G_PART_PARM_FLAGS) == 0 || 1368 strstr(gpp->gpp_flags, "restore") == NULL) { 1369 spb = 4096 / pp->sectorsize; 1370 if (spb > 1) { 1371 lba = start + ((start % spb) ? spb - start % spb : 0); 1372 if (lba <= min) 1373 start = lba; 1374 lba = end - (end + 1) % spb; 1375 if (max <= lba) 1376 end = lba; 1377 } 1378 } 1379 table->hdr->hdr_lba_start = start; 1380 table->hdr->hdr_lba_end = end; 1381 1382 basetable->gpt_first = start; 1383 basetable->gpt_last = end; 1384 } 1385 1386 static void 1387 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 1388 { 1389 u_int bo; 1390 uint32_t ch; 1391 uint16_t c; 1392 1393 bo = LITTLE_ENDIAN; /* GPT is little-endian */ 1394 while (len > 0 && *str != 0) { 1395 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 1396 str++, len--; 1397 if ((ch & 0xf800) == 0xd800) { 1398 if (len > 0) { 1399 c = (bo == BIG_ENDIAN) ? be16toh(*str) 1400 : le16toh(*str); 1401 str++, len--; 1402 } else 1403 c = 0xfffd; 1404 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 1405 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 1406 ch += 0x10000; 1407 } else 1408 ch = 0xfffd; 1409 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 1410 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 1411 continue; 1412 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 1413 continue; 1414 1415 /* Write the Unicode character in UTF-8 */ 1416 if (ch < 0x80) 1417 g_conf_printf_escaped(sb, "%c", ch); 1418 else if (ch < 0x800) 1419 g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6), 1420 0x80 | (ch & 0x3f)); 1421 else if (ch < 0x10000) 1422 g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12), 1423 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1424 else if (ch < 0x200000) 1425 g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 | 1426 (ch >> 18), 0x80 | ((ch >> 12) & 0x3f), 1427 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1428 } 1429 } 1430 1431 static void 1432 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1433 { 1434 size_t s16idx, s8idx; 1435 uint32_t utfchar; 1436 unsigned int c, utfbytes; 1437 1438 s8idx = s16idx = 0; 1439 utfchar = 0; 1440 utfbytes = 0; 1441 bzero(s16, s16len << 1); 1442 while (s8[s8idx] != 0 && s16idx < s16len) { 1443 c = s8[s8idx++]; 1444 if ((c & 0xc0) != 0x80) { 1445 /* Initial characters. */ 1446 if (utfbytes != 0) { 1447 /* Incomplete encoding of previous char. */ 1448 s16[s16idx++] = htole16(0xfffd); 1449 } 1450 if ((c & 0xf8) == 0xf0) { 1451 utfchar = c & 0x07; 1452 utfbytes = 3; 1453 } else if ((c & 0xf0) == 0xe0) { 1454 utfchar = c & 0x0f; 1455 utfbytes = 2; 1456 } else if ((c & 0xe0) == 0xc0) { 1457 utfchar = c & 0x1f; 1458 utfbytes = 1; 1459 } else { 1460 utfchar = c & 0x7f; 1461 utfbytes = 0; 1462 } 1463 } else { 1464 /* Followup characters. */ 1465 if (utfbytes > 0) { 1466 utfchar = (utfchar << 6) + (c & 0x3f); 1467 utfbytes--; 1468 } else if (utfbytes == 0) 1469 utfbytes = ~0; 1470 } 1471 /* 1472 * Write the complete Unicode character as UTF-16 when we 1473 * have all the UTF-8 charactars collected. 1474 */ 1475 if (utfbytes == 0) { 1476 /* 1477 * If we need to write 2 UTF-16 characters, but 1478 * we only have room for 1, then we truncate the 1479 * string by writing a 0 instead. 1480 */ 1481 if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1482 s16[s16idx++] = 1483 htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1484 s16[s16idx++] = 1485 htole16(0xdc00 | (utfchar & 0x3ff)); 1486 } else 1487 s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1488 htole16(utfchar); 1489 } 1490 } 1491 /* 1492 * If our input string was truncated, append an invalid encoding 1493 * character to the output string. 1494 */ 1495 if (utfbytes != 0 && s16idx < s16len) 1496 s16[s16idx++] = htole16(0xfffd); 1497 } 1498