11d3aed33SMarcel Moolenaar /*- 2369fe59dSMarcel Moolenaar * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar 31d3aed33SMarcel Moolenaar * All rights reserved. 41d3aed33SMarcel Moolenaar * 51d3aed33SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 61d3aed33SMarcel Moolenaar * modification, are permitted provided that the following conditions 71d3aed33SMarcel Moolenaar * are met: 81d3aed33SMarcel Moolenaar * 91d3aed33SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 101d3aed33SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 111d3aed33SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 121d3aed33SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 131d3aed33SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 141d3aed33SMarcel Moolenaar * 151d3aed33SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 161d3aed33SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 171d3aed33SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 181d3aed33SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 191d3aed33SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 201d3aed33SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 211d3aed33SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 221d3aed33SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 231d3aed33SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 241d3aed33SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 251d3aed33SMarcel Moolenaar */ 261d3aed33SMarcel Moolenaar 271d3aed33SMarcel Moolenaar #include <sys/cdefs.h> 281d3aed33SMarcel Moolenaar __FBSDID("$FreeBSD$"); 291d3aed33SMarcel Moolenaar 301d3aed33SMarcel Moolenaar #include <sys/param.h> 311d3aed33SMarcel Moolenaar #include <sys/bio.h> 321d3aed33SMarcel Moolenaar #include <sys/diskmbr.h> 331d3aed33SMarcel Moolenaar #include <sys/endian.h> 341d3aed33SMarcel Moolenaar #include <sys/gpt.h> 351d3aed33SMarcel Moolenaar #include <sys/kernel.h> 361d3aed33SMarcel Moolenaar #include <sys/kobj.h> 371d3aed33SMarcel Moolenaar #include <sys/limits.h> 381d3aed33SMarcel Moolenaar #include <sys/lock.h> 391d3aed33SMarcel Moolenaar #include <sys/malloc.h> 401d3aed33SMarcel Moolenaar #include <sys/mutex.h> 411d3aed33SMarcel Moolenaar #include <sys/queue.h> 421d3aed33SMarcel Moolenaar #include <sys/sbuf.h> 431d3aed33SMarcel Moolenaar #include <sys/systm.h> 44cb08c2ccSAlexander Leidinger #include <sys/sysctl.h> 451d3aed33SMarcel Moolenaar #include <sys/uuid.h> 461d3aed33SMarcel Moolenaar #include <geom/geom.h> 471d3aed33SMarcel Moolenaar #include <geom/part/g_part.h> 481d3aed33SMarcel Moolenaar 491d3aed33SMarcel Moolenaar #include "g_part_if.h" 501d3aed33SMarcel Moolenaar 51cb08c2ccSAlexander Leidinger FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support"); 52cb08c2ccSAlexander Leidinger 531d3aed33SMarcel Moolenaar CTASSERT(offsetof(struct gpt_hdr, padding) == 92); 541d3aed33SMarcel Moolenaar CTASSERT(sizeof(struct gpt_ent) == 128); 551d3aed33SMarcel Moolenaar 561d3aed33SMarcel Moolenaar #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 571d3aed33SMarcel Moolenaar 584d32fcb4SMarcel Moolenaar #define MBRSIZE 512 594d32fcb4SMarcel Moolenaar 601d3aed33SMarcel Moolenaar enum gpt_elt { 611d3aed33SMarcel Moolenaar GPT_ELT_PRIHDR, 621d3aed33SMarcel Moolenaar GPT_ELT_PRITBL, 631d3aed33SMarcel Moolenaar GPT_ELT_SECHDR, 641d3aed33SMarcel Moolenaar GPT_ELT_SECTBL, 651d3aed33SMarcel Moolenaar GPT_ELT_COUNT 661d3aed33SMarcel Moolenaar }; 671d3aed33SMarcel Moolenaar 681d3aed33SMarcel Moolenaar enum gpt_state { 691d3aed33SMarcel Moolenaar GPT_STATE_UNKNOWN, /* Not determined. */ 701d3aed33SMarcel Moolenaar GPT_STATE_MISSING, /* No signature found. */ 711d3aed33SMarcel Moolenaar GPT_STATE_CORRUPT, /* Checksum mismatch. */ 721d3aed33SMarcel Moolenaar GPT_STATE_INVALID, /* Nonconformant/invalid. */ 731d3aed33SMarcel Moolenaar GPT_STATE_OK /* Perfectly fine. */ 741d3aed33SMarcel Moolenaar }; 751d3aed33SMarcel Moolenaar 761d3aed33SMarcel Moolenaar struct g_part_gpt_table { 771d3aed33SMarcel Moolenaar struct g_part_table base; 784d32fcb4SMarcel Moolenaar u_char mbr[MBRSIZE]; 791c2dee3cSRobert Noland struct gpt_hdr *hdr; 801d3aed33SMarcel Moolenaar quad_t lba[GPT_ELT_COUNT]; 811d3aed33SMarcel Moolenaar enum gpt_state state[GPT_ELT_COUNT]; 82369fe59dSMarcel Moolenaar int bootcamp; 831d3aed33SMarcel Moolenaar }; 841d3aed33SMarcel Moolenaar 851d3aed33SMarcel Moolenaar struct g_part_gpt_entry { 861d3aed33SMarcel Moolenaar struct g_part_entry base; 871d3aed33SMarcel Moolenaar struct gpt_ent ent; 881d3aed33SMarcel Moolenaar }; 891d3aed33SMarcel Moolenaar 90d3532631SMarcel Moolenaar static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t); 91d3532631SMarcel Moolenaar static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t); 922b9be055SAndrey V. Elsukov static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *); 93d3532631SMarcel Moolenaar 941d3aed33SMarcel Moolenaar static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 951d3aed33SMarcel Moolenaar struct g_part_parms *); 964d32fcb4SMarcel Moolenaar static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 971d3aed33SMarcel Moolenaar static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 981d3aed33SMarcel Moolenaar static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 9951f53a08SWarner Losh static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 1005db67052SMarcel Moolenaar struct sbuf *, const char *); 1011d3aed33SMarcel Moolenaar static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 1021d3aed33SMarcel Moolenaar static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 1031d3aed33SMarcel Moolenaar struct g_part_parms *); 10451f53a08SWarner Losh static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 1051d3aed33SMarcel Moolenaar char *, size_t); 1061d3aed33SMarcel Moolenaar static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 1071d3aed33SMarcel Moolenaar static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 108cec283baSPawel Jakub Dawidek static int g_part_gpt_setunset(struct g_part_table *table, 109cec283baSPawel Jakub Dawidek struct g_part_entry *baseentry, const char *attrib, unsigned int set); 1101d3aed33SMarcel Moolenaar static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 1111d3aed33SMarcel Moolenaar char *, size_t); 1121d3aed33SMarcel Moolenaar static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 1133f71c319SMarcel Moolenaar static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 1143f71c319SMarcel Moolenaar struct g_part_parms *); 115e7926a37SAndrey V. Elsukov static int g_part_gpt_recover(struct g_part_table *); 1161d3aed33SMarcel Moolenaar 1171d3aed33SMarcel Moolenaar static kobj_method_t g_part_gpt_methods[] = { 1181d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_add, g_part_gpt_add), 1194d32fcb4SMarcel Moolenaar KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 1201d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_create, g_part_gpt_create), 1211d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 1225db67052SMarcel Moolenaar KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 1231d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 1241d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_modify, g_part_gpt_modify), 1253f71c319SMarcel Moolenaar KOBJMETHOD(g_part_resize, g_part_gpt_resize), 1261d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_name, g_part_gpt_name), 1271d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_probe, g_part_gpt_probe), 1281d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_read, g_part_gpt_read), 129e7926a37SAndrey V. Elsukov KOBJMETHOD(g_part_recover, g_part_gpt_recover), 130cec283baSPawel Jakub Dawidek KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 1311d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_type, g_part_gpt_type), 1321d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_write, g_part_gpt_write), 1331d3aed33SMarcel Moolenaar { 0, 0 } 1341d3aed33SMarcel Moolenaar }; 1351d3aed33SMarcel Moolenaar 1361d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_gpt_scheme = { 1371d3aed33SMarcel Moolenaar "GPT", 1381d3aed33SMarcel Moolenaar g_part_gpt_methods, 1391d3aed33SMarcel Moolenaar sizeof(struct g_part_gpt_table), 1401d3aed33SMarcel Moolenaar .gps_entrysz = sizeof(struct g_part_gpt_entry), 1411d3aed33SMarcel Moolenaar .gps_minent = 128, 142799eac8cSAndrey V. Elsukov .gps_maxent = 4096, 1434d32fcb4SMarcel Moolenaar .gps_bootcodesz = MBRSIZE, 1441d3aed33SMarcel Moolenaar }; 1454ffca444SMarcel Moolenaar G_PART_SCHEME_DECLARE(g_part_gpt); 1461d3aed33SMarcel Moolenaar 147f1317430SRui Paulo static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 14887662ab3SRui Paulo static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 149f1317430SRui Paulo static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 150f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 151f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 152f1317430SRui Paulo static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 153f1317430SRui Paulo static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 15488007f61SAndrey V. Elsukov static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 1551d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 1561d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 157f352a0d4SJohn Baldwin static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 1581d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 1591d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 1601d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 161a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 162f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 163f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 164f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 1651d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 166*b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 167*b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 168*b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 16933f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 17033f7a412SRui Paulo static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 17133f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 17233f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 17333f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 17433f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 17533f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 17633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 17733f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 17833f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 1791d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 1801d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 1811d3aed33SMarcel Moolenaar 182f1317430SRui Paulo static struct g_part_uuid_alias { 183f1317430SRui Paulo struct uuid *uuid; 184f1317430SRui Paulo int alias; 185369fe59dSMarcel Moolenaar int mbrtype; 186f1317430SRui Paulo } gpt_uuid_alias_match[] = { 187369fe59dSMarcel Moolenaar { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 188369fe59dSMarcel Moolenaar { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 189369fe59dSMarcel Moolenaar { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 190369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 191369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 192369fe59dSMarcel Moolenaar { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 193369fe59dSMarcel Moolenaar { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 194369fe59dSMarcel Moolenaar { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 195369fe59dSMarcel Moolenaar { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 196369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 197369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 198369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 199369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 200369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 201369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 202369fe59dSMarcel Moolenaar { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 203369fe59dSMarcel Moolenaar { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 204369fe59dSMarcel Moolenaar { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 205369fe59dSMarcel Moolenaar { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 206*b20e4de3SDmitry Morozovsky { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 207*b20e4de3SDmitry Morozovsky { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 208*b20e4de3SDmitry Morozovsky { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 209369fe59dSMarcel Moolenaar { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 210369fe59dSMarcel Moolenaar { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 211369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 212369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 213369fe59dSMarcel Moolenaar { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 214369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 215369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 216369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 217369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 218369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 219369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 220369fe59dSMarcel Moolenaar { NULL, 0, 0 } 221f1317430SRui Paulo }; 222f1317430SRui Paulo 223369fe59dSMarcel Moolenaar static int 224369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 225369fe59dSMarcel Moolenaar quad_t end) 226369fe59dSMarcel Moolenaar { 227369fe59dSMarcel Moolenaar 228369fe59dSMarcel Moolenaar if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 229369fe59dSMarcel Moolenaar return (EINVAL); 230369fe59dSMarcel Moolenaar 231369fe59dSMarcel Moolenaar mbr += DOSPARTOFF + idx * DOSPARTSIZE; 232369fe59dSMarcel Moolenaar mbr[0] = 0; 233369fe59dSMarcel Moolenaar if (start == 1) { 234369fe59dSMarcel Moolenaar /* 235369fe59dSMarcel Moolenaar * Treat the PMBR partition specially to maximize 236369fe59dSMarcel Moolenaar * interoperability with BIOSes. 237369fe59dSMarcel Moolenaar */ 238369fe59dSMarcel Moolenaar mbr[1] = mbr[3] = 0; 239369fe59dSMarcel Moolenaar mbr[2] = 2; 240369fe59dSMarcel Moolenaar } else 241369fe59dSMarcel Moolenaar mbr[1] = mbr[2] = mbr[3] = 0xff; 242369fe59dSMarcel Moolenaar mbr[4] = typ; 243369fe59dSMarcel Moolenaar mbr[5] = mbr[6] = mbr[7] = 0xff; 244369fe59dSMarcel Moolenaar le32enc(mbr + 8, (uint32_t)start); 245369fe59dSMarcel Moolenaar le32enc(mbr + 12, (uint32_t)(end - start + 1)); 246369fe59dSMarcel Moolenaar return (0); 247369fe59dSMarcel Moolenaar } 248369fe59dSMarcel Moolenaar 249369fe59dSMarcel Moolenaar static int 250369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t) 251369fe59dSMarcel Moolenaar { 252369fe59dSMarcel Moolenaar struct g_part_uuid_alias *uap; 253369fe59dSMarcel Moolenaar 254369fe59dSMarcel Moolenaar for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 255369fe59dSMarcel Moolenaar if (EQUUID(t, uap->uuid)) 256369fe59dSMarcel Moolenaar return (uap->mbrtype); 257369fe59dSMarcel Moolenaar } 258369fe59dSMarcel Moolenaar return (0); 259369fe59dSMarcel Moolenaar } 260369fe59dSMarcel Moolenaar 261369fe59dSMarcel Moolenaar /* 262369fe59dSMarcel Moolenaar * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 263369fe59dSMarcel Moolenaar * whole disk anymore. Rather, it covers the GPT table and the EFI 264369fe59dSMarcel Moolenaar * system partition only. This way the HFS+ partition and any FAT 265369fe59dSMarcel Moolenaar * partitions can be added to the MBR without creating an overlap. 266369fe59dSMarcel Moolenaar */ 267369fe59dSMarcel Moolenaar static int 268369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 269369fe59dSMarcel Moolenaar { 270369fe59dSMarcel Moolenaar uint8_t *p; 271369fe59dSMarcel Moolenaar 272369fe59dSMarcel Moolenaar p = table->mbr + DOSPARTOFF; 273369fe59dSMarcel Moolenaar if (p[4] != 0xee || le32dec(p + 8) != 1) 274369fe59dSMarcel Moolenaar return (0); 275369fe59dSMarcel Moolenaar 276369fe59dSMarcel Moolenaar p += DOSPARTSIZE; 277369fe59dSMarcel Moolenaar if (p[4] != 0xaf) 278369fe59dSMarcel Moolenaar return (0); 279369fe59dSMarcel Moolenaar 280369fe59dSMarcel Moolenaar printf("GEOM: %s: enabling Boot Camp\n", provname); 281369fe59dSMarcel Moolenaar return (1); 282369fe59dSMarcel Moolenaar } 283369fe59dSMarcel Moolenaar 284369fe59dSMarcel Moolenaar static void 285369fe59dSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable) 286369fe59dSMarcel Moolenaar { 287369fe59dSMarcel Moolenaar struct g_part_entry *baseentry; 288369fe59dSMarcel Moolenaar struct g_part_gpt_entry *entry; 289369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 290369fe59dSMarcel Moolenaar int bootable, error, index, slices, typ; 291369fe59dSMarcel Moolenaar 292369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 293369fe59dSMarcel Moolenaar 294369fe59dSMarcel Moolenaar bootable = -1; 295369fe59dSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 296369fe59dSMarcel Moolenaar if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 297369fe59dSMarcel Moolenaar bootable = index; 298369fe59dSMarcel Moolenaar } 299369fe59dSMarcel Moolenaar 300369fe59dSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 301369fe59dSMarcel Moolenaar slices = 0; 302369fe59dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 303369fe59dSMarcel Moolenaar if (baseentry->gpe_deleted) 304369fe59dSMarcel Moolenaar continue; 305369fe59dSMarcel Moolenaar index = baseentry->gpe_index - 1; 306369fe59dSMarcel Moolenaar if (index >= NDOSPART) 307369fe59dSMarcel Moolenaar continue; 308369fe59dSMarcel Moolenaar 309369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 310369fe59dSMarcel Moolenaar 311369fe59dSMarcel Moolenaar switch (index) { 312369fe59dSMarcel Moolenaar case 0: /* This must be the EFI system partition. */ 313369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 314369fe59dSMarcel Moolenaar goto disable; 315369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xee, 316369fe59dSMarcel Moolenaar 1ull, entry->ent.ent_lba_end); 317369fe59dSMarcel Moolenaar break; 318369fe59dSMarcel Moolenaar case 1: /* This must be the HFS+ partition. */ 319369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 320369fe59dSMarcel Moolenaar goto disable; 321369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 322369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 323369fe59dSMarcel Moolenaar break; 324369fe59dSMarcel Moolenaar default: 325369fe59dSMarcel Moolenaar typ = gpt_map_type(&entry->ent.ent_type); 326369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, typ, 327369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 328369fe59dSMarcel Moolenaar break; 329369fe59dSMarcel Moolenaar } 330369fe59dSMarcel Moolenaar if (error) 331369fe59dSMarcel Moolenaar continue; 332369fe59dSMarcel Moolenaar 333369fe59dSMarcel Moolenaar if (index == bootable) 334369fe59dSMarcel Moolenaar table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 335369fe59dSMarcel Moolenaar slices |= 1 << index; 336369fe59dSMarcel Moolenaar } 337369fe59dSMarcel Moolenaar if ((slices & 3) == 3) 338369fe59dSMarcel Moolenaar return; 339369fe59dSMarcel Moolenaar 340369fe59dSMarcel Moolenaar disable: 341369fe59dSMarcel Moolenaar table->bootcamp = 0; 342369fe59dSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 343369fe59dSMarcel Moolenaar gpt_write_mbr_entry(table->mbr, 0, 0xee, 1ull, 344369fe59dSMarcel Moolenaar MIN(table->lba[GPT_ELT_SECHDR], UINT32_MAX)); 345369fe59dSMarcel Moolenaar } 346369fe59dSMarcel Moolenaar 3471c2dee3cSRobert Noland static struct gpt_hdr * 3481d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 3491c2dee3cSRobert Noland enum gpt_elt elt) 3501d3aed33SMarcel Moolenaar { 3511c2dee3cSRobert Noland struct gpt_hdr *buf, *hdr; 3521d3aed33SMarcel Moolenaar struct g_provider *pp; 3531d3aed33SMarcel Moolenaar quad_t lba, last; 3541d3aed33SMarcel Moolenaar int error; 3551d3aed33SMarcel Moolenaar uint32_t crc, sz; 3561d3aed33SMarcel Moolenaar 3571d3aed33SMarcel Moolenaar pp = cp->provider; 3581d3aed33SMarcel Moolenaar last = (pp->mediasize / pp->sectorsize) - 1; 3591d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 360e7926a37SAndrey V. Elsukov /* 361e7926a37SAndrey V. Elsukov * If the primary header is valid look for secondary 362e7926a37SAndrey V. Elsukov * header in AlternateLBA, otherwise in the last medium's LBA. 363e7926a37SAndrey V. Elsukov */ 364e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_SECHDR) { 365e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 366e7926a37SAndrey V. Elsukov table->lba[elt] = last; 367e7926a37SAndrey V. Elsukov } else 368e7926a37SAndrey V. Elsukov table->lba[elt] = 1; 3691d3aed33SMarcel Moolenaar buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 3701d3aed33SMarcel Moolenaar &error); 3711d3aed33SMarcel Moolenaar if (buf == NULL) 3721c2dee3cSRobert Noland return (NULL); 3731c2dee3cSRobert Noland hdr = NULL; 3741c2dee3cSRobert Noland if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 3751c2dee3cSRobert Noland goto fail; 3761d3aed33SMarcel Moolenaar 3771d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 3781c2dee3cSRobert Noland sz = le32toh(buf->hdr_size); 3791d3aed33SMarcel Moolenaar if (sz < 92 || sz > pp->sectorsize) 3801c2dee3cSRobert Noland goto fail; 3811c2dee3cSRobert Noland 3821c2dee3cSRobert Noland hdr = g_malloc(sz, M_WAITOK | M_ZERO); 3831c2dee3cSRobert Noland bcopy(buf, hdr, sz); 3841d3aed33SMarcel Moolenaar hdr->hdr_size = sz; 3851c2dee3cSRobert Noland 3861c2dee3cSRobert Noland crc = le32toh(buf->hdr_crc_self); 3871c2dee3cSRobert Noland buf->hdr_crc_self = 0; 3881c2dee3cSRobert Noland if (crc32(buf, sz) != crc) 3891c2dee3cSRobert Noland goto fail; 3901d3aed33SMarcel Moolenaar hdr->hdr_crc_self = crc; 3911d3aed33SMarcel Moolenaar 3921d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_INVALID; 3931c2dee3cSRobert Noland hdr->hdr_revision = le32toh(buf->hdr_revision); 394e7926a37SAndrey V. Elsukov if (hdr->hdr_revision < GPT_HDR_REVISION) 3951c2dee3cSRobert Noland goto fail; 3961c2dee3cSRobert Noland hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 3971d3aed33SMarcel Moolenaar if (hdr->hdr_lba_self != table->lba[elt]) 3981c2dee3cSRobert Noland goto fail; 3991c2dee3cSRobert Noland hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 400e7926a37SAndrey V. Elsukov if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 401e7926a37SAndrey V. Elsukov hdr->hdr_lba_alt > last) 402e7926a37SAndrey V. Elsukov goto fail; 4031d3aed33SMarcel Moolenaar 4041d3aed33SMarcel Moolenaar /* Check the managed area. */ 4051c2dee3cSRobert Noland hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 4061d3aed33SMarcel Moolenaar if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 4071c2dee3cSRobert Noland goto fail; 4081c2dee3cSRobert Noland hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 4091d3aed33SMarcel Moolenaar if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 4101c2dee3cSRobert Noland goto fail; 4111d3aed33SMarcel Moolenaar 4121d3aed33SMarcel Moolenaar /* Check the table location and size of the table. */ 4131c2dee3cSRobert Noland hdr->hdr_entries = le32toh(buf->hdr_entries); 4141c2dee3cSRobert Noland hdr->hdr_entsz = le32toh(buf->hdr_entsz); 4151d3aed33SMarcel Moolenaar if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 4161d3aed33SMarcel Moolenaar (hdr->hdr_entsz & 7) != 0) 4171c2dee3cSRobert Noland goto fail; 4181c2dee3cSRobert Noland hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 4191d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 4201c2dee3cSRobert Noland goto fail; 4211d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 4221d3aed33SMarcel Moolenaar hdr->hdr_lba_table <= hdr->hdr_lba_end) 4231c2dee3cSRobert Noland goto fail; 4241d3aed33SMarcel Moolenaar lba = hdr->hdr_lba_table + 4251d3aed33SMarcel Moolenaar (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) / 4261d3aed33SMarcel Moolenaar pp->sectorsize - 1; 4271d3aed33SMarcel Moolenaar if (lba >= last) 4281c2dee3cSRobert Noland goto fail; 4291d3aed33SMarcel Moolenaar if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 4301c2dee3cSRobert Noland goto fail; 4311d3aed33SMarcel Moolenaar 4321d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4331c2dee3cSRobert Noland le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 4341c2dee3cSRobert Noland hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 4351c2dee3cSRobert Noland 436e7926a37SAndrey V. Elsukov /* save LBA for secondary header */ 437e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_PRIHDR) 438e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 439e7926a37SAndrey V. Elsukov 4401c2dee3cSRobert Noland g_free(buf); 4411c2dee3cSRobert Noland return (hdr); 4421c2dee3cSRobert Noland 4431c2dee3cSRobert Noland fail: 4441c2dee3cSRobert Noland if (hdr != NULL) 4451c2dee3cSRobert Noland g_free(hdr); 4461c2dee3cSRobert Noland g_free(buf); 4471c2dee3cSRobert Noland return (NULL); 4481d3aed33SMarcel Moolenaar } 4491d3aed33SMarcel Moolenaar 4501d3aed33SMarcel Moolenaar static struct gpt_ent * 4511d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 4521d3aed33SMarcel Moolenaar enum gpt_elt elt, struct gpt_hdr *hdr) 4531d3aed33SMarcel Moolenaar { 4541d3aed33SMarcel Moolenaar struct g_provider *pp; 4551d3aed33SMarcel Moolenaar struct gpt_ent *ent, *tbl; 4561d3aed33SMarcel Moolenaar char *buf, *p; 457799eac8cSAndrey V. Elsukov unsigned int idx, sectors, tblsz, size; 4581d3aed33SMarcel Moolenaar int error; 4591d3aed33SMarcel Moolenaar 4601c2dee3cSRobert Noland if (hdr == NULL) 4611c2dee3cSRobert Noland return (NULL); 4621c2dee3cSRobert Noland 4631d3aed33SMarcel Moolenaar pp = cp->provider; 4641d3aed33SMarcel Moolenaar table->lba[elt] = hdr->hdr_lba_table; 4651d3aed33SMarcel Moolenaar 4661d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 4671d3aed33SMarcel Moolenaar tblsz = hdr->hdr_entries * hdr->hdr_entsz; 4681d3aed33SMarcel Moolenaar sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize; 469799eac8cSAndrey V. Elsukov buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 470799eac8cSAndrey V. Elsukov for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 471799eac8cSAndrey V. Elsukov size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 472799eac8cSAndrey V. Elsukov (sectors - idx) * pp->sectorsize; 473799eac8cSAndrey V. Elsukov p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 474799eac8cSAndrey V. Elsukov size, &error); 475799eac8cSAndrey V. Elsukov if (p == NULL) { 476799eac8cSAndrey V. Elsukov g_free(buf); 4771d3aed33SMarcel Moolenaar return (NULL); 478799eac8cSAndrey V. Elsukov } 479799eac8cSAndrey V. Elsukov bcopy(p, buf + idx * pp->sectorsize, size); 480799eac8cSAndrey V. Elsukov g_free(p); 481799eac8cSAndrey V. Elsukov } 4821d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 4831d3aed33SMarcel Moolenaar if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 4841d3aed33SMarcel Moolenaar g_free(buf); 4851d3aed33SMarcel Moolenaar return (NULL); 4861d3aed33SMarcel Moolenaar } 4871d3aed33SMarcel Moolenaar 4881d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4891d3aed33SMarcel Moolenaar tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 4901d3aed33SMarcel Moolenaar M_WAITOK | M_ZERO); 4911d3aed33SMarcel Moolenaar 4921d3aed33SMarcel Moolenaar for (idx = 0, ent = tbl, p = buf; 4931d3aed33SMarcel Moolenaar idx < hdr->hdr_entries; 4941d3aed33SMarcel Moolenaar idx++, ent++, p += hdr->hdr_entsz) { 4951d3aed33SMarcel Moolenaar le_uuid_dec(p, &ent->ent_type); 4961d3aed33SMarcel Moolenaar le_uuid_dec(p + 16, &ent->ent_uuid); 4971d3aed33SMarcel Moolenaar ent->ent_lba_start = le64dec(p + 32); 4981d3aed33SMarcel Moolenaar ent->ent_lba_end = le64dec(p + 40); 4991d3aed33SMarcel Moolenaar ent->ent_attr = le64dec(p + 48); 500d3532631SMarcel Moolenaar /* Keep UTF-16 in little-endian. */ 501d3532631SMarcel Moolenaar bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 5021d3aed33SMarcel Moolenaar } 5031d3aed33SMarcel Moolenaar 5041d3aed33SMarcel Moolenaar g_free(buf); 5051d3aed33SMarcel Moolenaar return (tbl); 5061d3aed33SMarcel Moolenaar } 5071d3aed33SMarcel Moolenaar 5081d3aed33SMarcel Moolenaar static int 5091d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 5101d3aed33SMarcel Moolenaar { 5111d3aed33SMarcel Moolenaar 5121c2dee3cSRobert Noland if (pri == NULL || sec == NULL) 5131c2dee3cSRobert Noland return (0); 5141c2dee3cSRobert Noland 5151d3aed33SMarcel Moolenaar if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 5161d3aed33SMarcel Moolenaar return (0); 5171d3aed33SMarcel Moolenaar return ((pri->hdr_revision == sec->hdr_revision && 5181d3aed33SMarcel Moolenaar pri->hdr_size == sec->hdr_size && 5191d3aed33SMarcel Moolenaar pri->hdr_lba_start == sec->hdr_lba_start && 5201d3aed33SMarcel Moolenaar pri->hdr_lba_end == sec->hdr_lba_end && 5211d3aed33SMarcel Moolenaar pri->hdr_entries == sec->hdr_entries && 5221d3aed33SMarcel Moolenaar pri->hdr_entsz == sec->hdr_entsz && 5231d3aed33SMarcel Moolenaar pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 5241d3aed33SMarcel Moolenaar } 5251d3aed33SMarcel Moolenaar 5261d3aed33SMarcel Moolenaar static int 5271d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid) 5281d3aed33SMarcel Moolenaar { 5291d3aed33SMarcel Moolenaar struct uuid tmp; 530d287f590SMarcel Moolenaar const char *alias; 5311d3aed33SMarcel Moolenaar int error; 532f1317430SRui Paulo struct g_part_uuid_alias *uap; 5331d3aed33SMarcel Moolenaar 534d287f590SMarcel Moolenaar if (type[0] == '!') { 535d287f590SMarcel Moolenaar error = parse_uuid(type + 1, &tmp); 5361d3aed33SMarcel Moolenaar if (error) 5371d3aed33SMarcel Moolenaar return (error); 5381d3aed33SMarcel Moolenaar if (EQUUID(&tmp, &gpt_uuid_unused)) 5391d3aed33SMarcel Moolenaar return (EINVAL); 5401d3aed33SMarcel Moolenaar *uuid = tmp; 5411d3aed33SMarcel Moolenaar return (0); 5421d3aed33SMarcel Moolenaar } 543f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 544f1317430SRui Paulo alias = g_part_alias_name(uap->alias); 545d287f590SMarcel Moolenaar if (!strcasecmp(type, alias)) { 546665bb830SMarcel Moolenaar *uuid = *uap->uuid; 5471d3aed33SMarcel Moolenaar return (0); 5481d3aed33SMarcel Moolenaar } 549d7255ff4SRui Paulo } 550d287f590SMarcel Moolenaar return (EINVAL); 551d287f590SMarcel Moolenaar } 5521d3aed33SMarcel Moolenaar 5531d3aed33SMarcel Moolenaar static int 5541d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 5551d3aed33SMarcel Moolenaar struct g_part_parms *gpp) 5561d3aed33SMarcel Moolenaar { 5571d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 5581d3aed33SMarcel Moolenaar int error; 5591d3aed33SMarcel Moolenaar 5601d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 5611d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 5621d3aed33SMarcel Moolenaar if (error) 5631d3aed33SMarcel Moolenaar return (error); 5641d3aed33SMarcel Moolenaar kern_uuidgen(&entry->ent.ent_uuid, 1); 5651d3aed33SMarcel Moolenaar entry->ent.ent_lba_start = baseentry->gpe_start; 5661d3aed33SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 5671d3aed33SMarcel Moolenaar if (baseentry->gpe_deleted) { 5681d3aed33SMarcel Moolenaar entry->ent.ent_attr = 0; 5691d3aed33SMarcel Moolenaar bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 5701d3aed33SMarcel Moolenaar } 571d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 572d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 573ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 574ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 5751d3aed33SMarcel Moolenaar return (0); 5761d3aed33SMarcel Moolenaar } 5771d3aed33SMarcel Moolenaar 5781d3aed33SMarcel Moolenaar static int 5794d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 5804d32fcb4SMarcel Moolenaar { 5814d32fcb4SMarcel Moolenaar struct g_part_gpt_table *table; 58266477112SMarcel Moolenaar size_t codesz; 5834d32fcb4SMarcel Moolenaar 58466477112SMarcel Moolenaar codesz = DOSPARTOFF; 5854d32fcb4SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 58666477112SMarcel Moolenaar bzero(table->mbr, codesz); 58766477112SMarcel Moolenaar codesz = MIN(codesz, gpp->gpp_codesize); 58866477112SMarcel Moolenaar if (codesz > 0) 58966477112SMarcel Moolenaar bcopy(gpp->gpp_codeptr, table->mbr, codesz); 590e80d42ddSRobert Noland 591369fe59dSMarcel Moolenaar /* Mark the PMBR active since some BIOS require it. */ 592369fe59dSMarcel Moolenaar if (!table->bootcamp) 593e80d42ddSRobert Noland table->mbr[DOSPARTOFF] = 0x80; /* status */ 5944d32fcb4SMarcel Moolenaar return (0); 5954d32fcb4SMarcel Moolenaar } 5964d32fcb4SMarcel Moolenaar 5974d32fcb4SMarcel Moolenaar static int 5981d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 5991d3aed33SMarcel Moolenaar { 6001d3aed33SMarcel Moolenaar struct g_provider *pp; 6011d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 6021d3aed33SMarcel Moolenaar quad_t last; 6031d3aed33SMarcel Moolenaar size_t tblsz; 6041d3aed33SMarcel Moolenaar 6052a1c00ffSMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 6062a1c00ffSMarcel Moolenaar if (basetable->gpt_depth != 0) 6072a1c00ffSMarcel Moolenaar return (ENXIO); 6082a1c00ffSMarcel Moolenaar 6091d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 6101d3aed33SMarcel Moolenaar pp = gpp->gpp_provider; 6111d3aed33SMarcel Moolenaar tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 6121d3aed33SMarcel Moolenaar pp->sectorsize - 1) / pp->sectorsize; 6134d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || 6141d3aed33SMarcel Moolenaar pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 6151d3aed33SMarcel Moolenaar pp->sectorsize) 6161d3aed33SMarcel Moolenaar return (ENOSPC); 6171d3aed33SMarcel Moolenaar 6181d3aed33SMarcel Moolenaar last = (pp->mediasize / pp->sectorsize) - 1; 6191d3aed33SMarcel Moolenaar 6204d32fcb4SMarcel Moolenaar le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 621369fe59dSMarcel Moolenaar gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, MIN(last, UINT32_MAX)); 6221d3aed33SMarcel Moolenaar 623a59a1310SRobert Noland /* Allocate space for the header */ 624a59a1310SRobert Noland table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 625a59a1310SRobert Noland 6261c2dee3cSRobert Noland bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 6271c2dee3cSRobert Noland table->hdr->hdr_revision = GPT_HDR_REVISION; 6281c2dee3cSRobert Noland table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 6291c2dee3cSRobert Noland kern_uuidgen(&table->hdr->hdr_uuid, 1); 6301c2dee3cSRobert Noland table->hdr->hdr_entries = basetable->gpt_entries; 6311c2dee3cSRobert Noland table->hdr->hdr_entsz = sizeof(struct gpt_ent); 6321d3aed33SMarcel Moolenaar 6332b9be055SAndrey V. Elsukov g_gpt_set_defaults(basetable, pp); 6341d3aed33SMarcel Moolenaar return (0); 6351d3aed33SMarcel Moolenaar } 6361d3aed33SMarcel Moolenaar 6371d3aed33SMarcel Moolenaar static int 6381d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 6391d3aed33SMarcel Moolenaar { 64036066952SMarius Strobl struct g_part_gpt_table *table; 641e7926a37SAndrey V. Elsukov struct g_provider *pp; 64236066952SMarius Strobl 64336066952SMarius Strobl table = (struct g_part_gpt_table *)basetable; 644e7926a37SAndrey V. Elsukov pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 64536066952SMarius Strobl g_free(table->hdr); 64636066952SMarius Strobl table->hdr = NULL; 6471d3aed33SMarcel Moolenaar 6481d3aed33SMarcel Moolenaar /* 649e7926a37SAndrey V. Elsukov * Wipe the first 2 sectors to clear the partitioning. Wipe the last 650e7926a37SAndrey V. Elsukov * sector only if it has valid secondary header. 6511d3aed33SMarcel Moolenaar */ 6521d3aed33SMarcel Moolenaar basetable->gpt_smhead |= 3; 653e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 654e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 6551d3aed33SMarcel Moolenaar basetable->gpt_smtail |= 1; 6561d3aed33SMarcel Moolenaar return (0); 6571d3aed33SMarcel Moolenaar } 6581d3aed33SMarcel Moolenaar 65951f53a08SWarner Losh static void 6605db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 6615db67052SMarcel Moolenaar struct sbuf *sb, const char *indent) 6625db67052SMarcel Moolenaar { 6635db67052SMarcel Moolenaar struct g_part_gpt_entry *entry; 6645db67052SMarcel Moolenaar 6655db67052SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 6660c132595SMarcel Moolenaar if (indent == NULL) { 6670c132595SMarcel Moolenaar /* conftxt: libdisk compatibility */ 6685db67052SMarcel Moolenaar sbuf_printf(sb, " xs GPT xt "); 6695db67052SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 6700c132595SMarcel Moolenaar } else if (entry != NULL) { 6710c132595SMarcel Moolenaar /* confxml: partition entry information */ 672d3532631SMarcel Moolenaar sbuf_printf(sb, "%s<label>", indent); 673d3532631SMarcel Moolenaar g_gpt_printf_utf16(sb, entry->ent.ent_name, 674d3532631SMarcel Moolenaar sizeof(entry->ent.ent_name) >> 1); 675d3532631SMarcel Moolenaar sbuf_printf(sb, "</label>\n"); 676cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 677cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 678cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 679cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 680cec283baSPawel Jakub Dawidek indent); 681cec283baSPawel Jakub Dawidek } 682cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 683cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 684cec283baSPawel Jakub Dawidek indent); 685cec283baSPawel Jakub Dawidek } 6860c132595SMarcel Moolenaar sbuf_printf(sb, "%s<rawtype>", indent); 6870c132595SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 6880c132595SMarcel Moolenaar sbuf_printf(sb, "</rawtype>\n"); 689cd18ad83SMarcel Moolenaar sbuf_printf(sb, "%s<rawuuid>", indent); 690cd18ad83SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 691cd18ad83SMarcel Moolenaar sbuf_printf(sb, "</rawuuid>\n"); 6920c132595SMarcel Moolenaar } else { 6930c132595SMarcel Moolenaar /* confxml: scheme information */ 6940c132595SMarcel Moolenaar } 6955db67052SMarcel Moolenaar } 6965db67052SMarcel Moolenaar 6975db67052SMarcel Moolenaar static int 6981d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 6991d3aed33SMarcel Moolenaar { 7001d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7011d3aed33SMarcel Moolenaar 7021d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7031d3aed33SMarcel Moolenaar return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 7041d3aed33SMarcel Moolenaar EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0); 7051d3aed33SMarcel Moolenaar } 7061d3aed33SMarcel Moolenaar 7071d3aed33SMarcel Moolenaar static int 7081d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable, 7091d3aed33SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7101d3aed33SMarcel Moolenaar { 7111d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7121d3aed33SMarcel Moolenaar int error; 7131d3aed33SMarcel Moolenaar 7141d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 715d287f590SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_TYPE) { 7161d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 7171d3aed33SMarcel Moolenaar if (error) 7181d3aed33SMarcel Moolenaar return (error); 719d287f590SMarcel Moolenaar } 720d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 721d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 722ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 723ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 7241d3aed33SMarcel Moolenaar return (0); 7251d3aed33SMarcel Moolenaar } 7261d3aed33SMarcel Moolenaar 7273f71c319SMarcel Moolenaar static int 7283f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable, 7293f71c319SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7303f71c319SMarcel Moolenaar { 7313f71c319SMarcel Moolenaar struct g_part_gpt_entry *entry; 7323f71c319SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7333f71c319SMarcel Moolenaar 7343f71c319SMarcel Moolenaar baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 7353f71c319SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 7363f71c319SMarcel Moolenaar 7373f71c319SMarcel Moolenaar return (0); 7383f71c319SMarcel Moolenaar } 7393f71c319SMarcel Moolenaar 74051f53a08SWarner Losh static const char * 7411d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 7421d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 7431d3aed33SMarcel Moolenaar { 7441d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7451d3aed33SMarcel Moolenaar char c; 7461d3aed33SMarcel Moolenaar 7471d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7481d3aed33SMarcel Moolenaar c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 7491d3aed33SMarcel Moolenaar snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 7501d3aed33SMarcel Moolenaar return (buf); 7511d3aed33SMarcel Moolenaar } 7521d3aed33SMarcel Moolenaar 7531d3aed33SMarcel Moolenaar static int 7541d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 7551d3aed33SMarcel Moolenaar { 7561d3aed33SMarcel Moolenaar struct g_provider *pp; 7571d3aed33SMarcel Moolenaar char *buf; 7581d3aed33SMarcel Moolenaar int error, res; 7591d3aed33SMarcel Moolenaar 7601d3aed33SMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 7611d3aed33SMarcel Moolenaar if (table->gpt_depth != 0) 7621d3aed33SMarcel Moolenaar return (ENXIO); 7631d3aed33SMarcel Moolenaar 7641d3aed33SMarcel Moolenaar pp = cp->provider; 7651d3aed33SMarcel Moolenaar 7661d3aed33SMarcel Moolenaar /* 7671d3aed33SMarcel Moolenaar * Sanity-check the provider. Since the first sector on the provider 7681d3aed33SMarcel Moolenaar * must be a PMBR and a PMBR is 512 bytes large, the sector size 7691d3aed33SMarcel Moolenaar * must be at least 512 bytes. Also, since the theoretical minimum 7701d3aed33SMarcel Moolenaar * number of sectors needed by GPT is 6, any medium that has less 7711d3aed33SMarcel Moolenaar * than 6 sectors is never going to be able to hold a GPT. The 7721d3aed33SMarcel Moolenaar * number 6 comes from: 7731d3aed33SMarcel Moolenaar * 1 sector for the PMBR 7741d3aed33SMarcel Moolenaar * 2 sectors for the GPT headers (each 1 sector) 7751d3aed33SMarcel Moolenaar * 2 sectors for the GPT tables (each 1 sector) 7761d3aed33SMarcel Moolenaar * 1 sector for an actual partition 7771d3aed33SMarcel Moolenaar * It's better to catch this pathological case early than behaving 7781d3aed33SMarcel Moolenaar * pathologically later on... 7791d3aed33SMarcel Moolenaar */ 7804d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 7811d3aed33SMarcel Moolenaar return (ENOSPC); 7821d3aed33SMarcel Moolenaar 7831d3aed33SMarcel Moolenaar /* Check that there's a MBR. */ 7841d3aed33SMarcel Moolenaar buf = g_read_data(cp, 0L, pp->sectorsize, &error); 7851d3aed33SMarcel Moolenaar if (buf == NULL) 7861d3aed33SMarcel Moolenaar return (error); 7871d3aed33SMarcel Moolenaar res = le16dec(buf + DOSMAGICOFFSET); 7881d3aed33SMarcel Moolenaar g_free(buf); 7891d3aed33SMarcel Moolenaar if (res != DOSMAGIC) 7901d3aed33SMarcel Moolenaar return (ENXIO); 7911d3aed33SMarcel Moolenaar 7921d3aed33SMarcel Moolenaar /* Check that there's a primary header. */ 7931d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 7941d3aed33SMarcel Moolenaar if (buf == NULL) 7951d3aed33SMarcel Moolenaar return (error); 7961d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 7971d3aed33SMarcel Moolenaar g_free(buf); 7981d3aed33SMarcel Moolenaar if (res == 0) 7991d3aed33SMarcel Moolenaar return (G_PART_PROBE_PRI_HIGH); 8001d3aed33SMarcel Moolenaar 8011d3aed33SMarcel Moolenaar /* No primary? Check that there's a secondary. */ 8021d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 8031d3aed33SMarcel Moolenaar &error); 8041d3aed33SMarcel Moolenaar if (buf == NULL) 8051d3aed33SMarcel Moolenaar return (error); 8061d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8071d3aed33SMarcel Moolenaar g_free(buf); 8081d3aed33SMarcel Moolenaar return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO); 8091d3aed33SMarcel Moolenaar } 8101d3aed33SMarcel Moolenaar 8111d3aed33SMarcel Moolenaar static int 8121d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 8131d3aed33SMarcel Moolenaar { 8141c2dee3cSRobert Noland struct gpt_hdr *prihdr, *sechdr; 8151d3aed33SMarcel Moolenaar struct gpt_ent *tbl, *pritbl, *sectbl; 8161d3aed33SMarcel Moolenaar struct g_provider *pp; 8171d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 8181d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8194d32fcb4SMarcel Moolenaar u_char *buf; 820e7926a37SAndrey V. Elsukov uint64_t last; 8214d32fcb4SMarcel Moolenaar int error, index; 8221d3aed33SMarcel Moolenaar 8231d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 8241d3aed33SMarcel Moolenaar pp = cp->provider; 825e7926a37SAndrey V. Elsukov last = (pp->mediasize / pp->sectorsize) - 1; 8261d3aed33SMarcel Moolenaar 8274d32fcb4SMarcel Moolenaar /* Read the PMBR */ 8284d32fcb4SMarcel Moolenaar buf = g_read_data(cp, 0, pp->sectorsize, &error); 8294d32fcb4SMarcel Moolenaar if (buf == NULL) 8304d32fcb4SMarcel Moolenaar return (error); 8314d32fcb4SMarcel Moolenaar bcopy(buf, table->mbr, MBRSIZE); 8324d32fcb4SMarcel Moolenaar g_free(buf); 8334d32fcb4SMarcel Moolenaar 8341d3aed33SMarcel Moolenaar /* Read the primary header and table. */ 8351c2dee3cSRobert Noland prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 8361d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 8371c2dee3cSRobert Noland pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 8381d3aed33SMarcel Moolenaar } else { 8391d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 8401d3aed33SMarcel Moolenaar pritbl = NULL; 8411d3aed33SMarcel Moolenaar } 8421d3aed33SMarcel Moolenaar 8431d3aed33SMarcel Moolenaar /* Read the secondary header and table. */ 8441c2dee3cSRobert Noland sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 8451d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 8461c2dee3cSRobert Noland sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 8471d3aed33SMarcel Moolenaar } else { 8481d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 8491d3aed33SMarcel Moolenaar sectbl = NULL; 8501d3aed33SMarcel Moolenaar } 8511d3aed33SMarcel Moolenaar 8521d3aed33SMarcel Moolenaar /* Fail if we haven't got any good tables at all. */ 8531d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 8541d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 8551d3aed33SMarcel Moolenaar printf("GEOM: %s: corrupt or invalid GPT detected.\n", 8561d3aed33SMarcel Moolenaar pp->name); 8571d3aed33SMarcel Moolenaar printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 8581d3aed33SMarcel Moolenaar pp->name); 8591d3aed33SMarcel Moolenaar return (EINVAL); 8601d3aed33SMarcel Moolenaar } 8611d3aed33SMarcel Moolenaar 8621d3aed33SMarcel Moolenaar /* 8631d3aed33SMarcel Moolenaar * If both headers are good but they disagree with each other, 8641d3aed33SMarcel Moolenaar * then invalidate one. We prefer to keep the primary header, 8651d3aed33SMarcel Moolenaar * unless the primary table is corrupt. 8661d3aed33SMarcel Moolenaar */ 8671d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 8681d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 8691c2dee3cSRobert Noland !gpt_matched_hdrs(prihdr, sechdr)) { 870dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 8711d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 872dd0db05dSMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 8731c2dee3cSRobert Noland g_free(sechdr); 8741c2dee3cSRobert Noland sechdr = NULL; 875dd0db05dSMarcel Moolenaar } else { 8761d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 877dd0db05dSMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 8781c2dee3cSRobert Noland g_free(prihdr); 8791c2dee3cSRobert Noland prihdr = NULL; 880dd0db05dSMarcel Moolenaar } 8811d3aed33SMarcel Moolenaar } 8821d3aed33SMarcel Moolenaar 883dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 8841d3aed33SMarcel Moolenaar printf("GEOM: %s: the primary GPT table is corrupt or " 8851d3aed33SMarcel Moolenaar "invalid.\n", pp->name); 8861d3aed33SMarcel Moolenaar printf("GEOM: %s: using the secondary instead -- recovery " 8871d3aed33SMarcel Moolenaar "strongly advised.\n", pp->name); 8881d3aed33SMarcel Moolenaar table->hdr = sechdr; 889e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 8901c2dee3cSRobert Noland if (prihdr != NULL) 8911c2dee3cSRobert Noland g_free(prihdr); 8921d3aed33SMarcel Moolenaar tbl = sectbl; 8931d3aed33SMarcel Moolenaar if (pritbl != NULL) 8941d3aed33SMarcel Moolenaar g_free(pritbl); 8951d3aed33SMarcel Moolenaar } else { 896dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 8971d3aed33SMarcel Moolenaar printf("GEOM: %s: the secondary GPT table is corrupt " 8981d3aed33SMarcel Moolenaar "or invalid.\n", pp->name); 8991d3aed33SMarcel Moolenaar printf("GEOM: %s: using the primary only -- recovery " 9001d3aed33SMarcel Moolenaar "suggested.\n", pp->name); 901e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 902e7926a37SAndrey V. Elsukov } else if (table->lba[GPT_ELT_SECHDR] != last) { 903e7926a37SAndrey V. Elsukov printf( "GEOM: %s: the secondary GPT header is not in " 904e7926a37SAndrey V. Elsukov "the last LBA.\n", pp->name); 905e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9061d3aed33SMarcel Moolenaar } 9071d3aed33SMarcel Moolenaar table->hdr = prihdr; 9081c2dee3cSRobert Noland if (sechdr != NULL) 9091c2dee3cSRobert Noland g_free(sechdr); 9101d3aed33SMarcel Moolenaar tbl = pritbl; 9111d3aed33SMarcel Moolenaar if (sectbl != NULL) 9121d3aed33SMarcel Moolenaar g_free(sectbl); 9131d3aed33SMarcel Moolenaar } 9141d3aed33SMarcel Moolenaar 9151c2dee3cSRobert Noland basetable->gpt_first = table->hdr->hdr_lba_start; 9161c2dee3cSRobert Noland basetable->gpt_last = table->hdr->hdr_lba_end; 9171c2dee3cSRobert Noland basetable->gpt_entries = table->hdr->hdr_entries; 9181d3aed33SMarcel Moolenaar 9191d3aed33SMarcel Moolenaar for (index = basetable->gpt_entries - 1; index >= 0; index--) { 9201d3aed33SMarcel Moolenaar if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 9211d3aed33SMarcel Moolenaar continue; 922e7926a37SAndrey V. Elsukov entry = (struct g_part_gpt_entry *)g_part_new_entry( 923e7926a37SAndrey V. Elsukov basetable, index + 1, tbl[index].ent_lba_start, 924e7926a37SAndrey V. Elsukov tbl[index].ent_lba_end); 9251d3aed33SMarcel Moolenaar entry->ent = tbl[index]; 9261d3aed33SMarcel Moolenaar } 9271d3aed33SMarcel Moolenaar 9281d3aed33SMarcel Moolenaar g_free(tbl); 929369fe59dSMarcel Moolenaar 930369fe59dSMarcel Moolenaar /* 931369fe59dSMarcel Moolenaar * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 932369fe59dSMarcel Moolenaar * if (and only if) any FAT32 or FAT16 partitions have been 933369fe59dSMarcel Moolenaar * created. This happens irrespective of whether Boot Camp is 934369fe59dSMarcel Moolenaar * used/enabled, though it's generally understood to be done 935369fe59dSMarcel Moolenaar * to support legacy Windows under Boot Camp. We refer to this 936369fe59dSMarcel Moolenaar * mirroring simply as Boot Camp. We try to detect Boot Camp 937369fe59dSMarcel Moolenaar * so that we can update the MBR if and when GPT changes have 938369fe59dSMarcel Moolenaar * been made. Note that we do not enable Boot Camp if not 939369fe59dSMarcel Moolenaar * previously enabled because we can't assume that we're on a 940369fe59dSMarcel Moolenaar * Mac alongside Mac OS X. 941369fe59dSMarcel Moolenaar */ 942369fe59dSMarcel Moolenaar table->bootcamp = gpt_is_bootcamp(table, pp->name); 943369fe59dSMarcel Moolenaar 9441d3aed33SMarcel Moolenaar return (0); 9451d3aed33SMarcel Moolenaar } 9461d3aed33SMarcel Moolenaar 947cec283baSPawel Jakub Dawidek static int 948e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable) 949e7926a37SAndrey V. Elsukov { 950e7926a37SAndrey V. Elsukov 9512b9be055SAndrey V. Elsukov g_gpt_set_defaults(basetable, 9522b9be055SAndrey V. Elsukov LIST_FIRST(&basetable->gpt_gp->consumer)->provider); 953e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 0; 954e7926a37SAndrey V. Elsukov return (0); 955e7926a37SAndrey V. Elsukov } 956e7926a37SAndrey V. Elsukov 957e7926a37SAndrey V. Elsukov static int 958369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable, 959369fe59dSMarcel Moolenaar struct g_part_entry *baseentry, const char *attrib, unsigned int set) 960cec283baSPawel Jakub Dawidek { 961cec283baSPawel Jakub Dawidek struct g_part_gpt_entry *entry; 962369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 963369fe59dSMarcel Moolenaar uint64_t attr; 964369fe59dSMarcel Moolenaar int i; 965cec283baSPawel Jakub Dawidek 966369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 967369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 968369fe59dSMarcel Moolenaar 969369fe59dSMarcel Moolenaar if (strcasecmp(attrib, "active") == 0) { 970369fe59dSMarcel Moolenaar if (!table->bootcamp || baseentry->gpe_index > NDOSPART) 971369fe59dSMarcel Moolenaar return (EINVAL); 972369fe59dSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 973369fe59dSMarcel Moolenaar table->mbr[DOSPARTOFF + i * DOSPARTSIZE] = 974369fe59dSMarcel Moolenaar (i == baseentry->gpe_index - 1) ? 0x80 : 0; 975369fe59dSMarcel Moolenaar } 976369fe59dSMarcel Moolenaar return (0); 977369fe59dSMarcel Moolenaar } 978369fe59dSMarcel Moolenaar 979369fe59dSMarcel Moolenaar attr = 0; 980cec283baSPawel Jakub Dawidek if (strcasecmp(attrib, "bootme") == 0) { 981369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 982cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootonce") == 0) { 983369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTONCE; 984cec283baSPawel Jakub Dawidek if (set) 985369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 986cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootfailed") == 0) { 987cec283baSPawel Jakub Dawidek /* 988cec283baSPawel Jakub Dawidek * It should only be possible to unset BOOTFAILED, but it might 989cec283baSPawel Jakub Dawidek * be useful for test purposes to also be able to set it. 990cec283baSPawel Jakub Dawidek */ 991369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTFAILED; 992cec283baSPawel Jakub Dawidek } 993369fe59dSMarcel Moolenaar if (attr == 0) 994cec283baSPawel Jakub Dawidek return (EINVAL); 995cec283baSPawel Jakub Dawidek 996369fe59dSMarcel Moolenaar if (set) 997369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr | attr; 998369fe59dSMarcel Moolenaar else 999369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr & ~attr; 1000369fe59dSMarcel Moolenaar if (attr != entry->ent.ent_attr) { 1001369fe59dSMarcel Moolenaar entry->ent.ent_attr = attr; 1002369fe59dSMarcel Moolenaar if (!baseentry->gpe_created) 1003369fe59dSMarcel Moolenaar baseentry->gpe_modified = 1; 1004cec283baSPawel Jakub Dawidek } 1005cec283baSPawel Jakub Dawidek return (0); 1006cec283baSPawel Jakub Dawidek } 1007cec283baSPawel Jakub Dawidek 10081d3aed33SMarcel Moolenaar static const char * 10091d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 10101d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 10111d3aed33SMarcel Moolenaar { 10121d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 10131d3aed33SMarcel Moolenaar struct uuid *type; 1014f1317430SRui Paulo struct g_part_uuid_alias *uap; 10151d3aed33SMarcel Moolenaar 10161d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 10171d3aed33SMarcel Moolenaar type = &entry->ent.ent_type; 1018f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1019f1317430SRui Paulo if (EQUUID(type, uap->uuid)) 1020f1317430SRui Paulo return (g_part_alias_name(uap->alias)); 1021cf231470SMarcel Moolenaar buf[0] = '!'; 1022cf231470SMarcel Moolenaar snprintf_uuid(buf + 1, bufsz - 1, type); 1023f1317430SRui Paulo 10241d3aed33SMarcel Moolenaar return (buf); 10251d3aed33SMarcel Moolenaar } 10261d3aed33SMarcel Moolenaar 10271d3aed33SMarcel Moolenaar static int 10281d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 10291d3aed33SMarcel Moolenaar { 10301d3aed33SMarcel Moolenaar unsigned char *buf, *bp; 10311d3aed33SMarcel Moolenaar struct g_provider *pp; 10321d3aed33SMarcel Moolenaar struct g_part_entry *baseentry; 10331d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 10341d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 1035e7926a37SAndrey V. Elsukov size_t tblsz; 10361d3aed33SMarcel Moolenaar uint32_t crc; 10371d3aed33SMarcel Moolenaar int error, index; 10381d3aed33SMarcel Moolenaar 10391d3aed33SMarcel Moolenaar pp = cp->provider; 10401d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 1041e7926a37SAndrey V. Elsukov tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz + 10421d3aed33SMarcel Moolenaar pp->sectorsize - 1) / pp->sectorsize; 10431d3aed33SMarcel Moolenaar 1044369fe59dSMarcel Moolenaar /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1045369fe59dSMarcel Moolenaar if (table->bootcamp) 1046369fe59dSMarcel Moolenaar gpt_update_bootcamp(basetable); 1047369fe59dSMarcel Moolenaar 10484d32fcb4SMarcel Moolenaar /* Write the PMBR */ 10491d3aed33SMarcel Moolenaar buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 10504d32fcb4SMarcel Moolenaar bcopy(table->mbr, buf, MBRSIZE); 10511d3aed33SMarcel Moolenaar error = g_write_data(cp, 0, buf, pp->sectorsize); 10521d3aed33SMarcel Moolenaar g_free(buf); 10531d3aed33SMarcel Moolenaar if (error) 10541d3aed33SMarcel Moolenaar return (error); 10551d3aed33SMarcel Moolenaar 10561d3aed33SMarcel Moolenaar /* Allocate space for the header and entries. */ 1057e7926a37SAndrey V. Elsukov buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 10581d3aed33SMarcel Moolenaar 10591c2dee3cSRobert Noland memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 10601c2dee3cSRobert Noland le32enc(buf + 8, table->hdr->hdr_revision); 10611c2dee3cSRobert Noland le32enc(buf + 12, table->hdr->hdr_size); 10621c2dee3cSRobert Noland le64enc(buf + 40, table->hdr->hdr_lba_start); 10631c2dee3cSRobert Noland le64enc(buf + 48, table->hdr->hdr_lba_end); 10641c2dee3cSRobert Noland le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 10651c2dee3cSRobert Noland le32enc(buf + 80, table->hdr->hdr_entries); 10661c2dee3cSRobert Noland le32enc(buf + 84, table->hdr->hdr_entsz); 10671d3aed33SMarcel Moolenaar 10681d3aed33SMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1069d287f590SMarcel Moolenaar if (baseentry->gpe_deleted) 1070d287f590SMarcel Moolenaar continue; 10711d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 10721d3aed33SMarcel Moolenaar index = baseentry->gpe_index - 1; 10731c2dee3cSRobert Noland bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 10741d3aed33SMarcel Moolenaar le_uuid_enc(bp, &entry->ent.ent_type); 10751d3aed33SMarcel Moolenaar le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 10761d3aed33SMarcel Moolenaar le64enc(bp + 32, entry->ent.ent_lba_start); 10771d3aed33SMarcel Moolenaar le64enc(bp + 40, entry->ent.ent_lba_end); 10781d3aed33SMarcel Moolenaar le64enc(bp + 48, entry->ent.ent_attr); 10791d3aed33SMarcel Moolenaar memcpy(bp + 56, entry->ent.ent_name, 10801d3aed33SMarcel Moolenaar sizeof(entry->ent.ent_name)); 10811d3aed33SMarcel Moolenaar } 10821d3aed33SMarcel Moolenaar 10831d3aed33SMarcel Moolenaar crc = crc32(buf + pp->sectorsize, 10841c2dee3cSRobert Noland table->hdr->hdr_entries * table->hdr->hdr_entsz); 10851d3aed33SMarcel Moolenaar le32enc(buf + 88, crc); 10861d3aed33SMarcel Moolenaar 10871d3aed33SMarcel Moolenaar /* Write primary meta-data. */ 10881d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 10891d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 10901d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 10911d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 10921c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 10931d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 10941d3aed33SMarcel Moolenaar 1095799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1096799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1097799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1098799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1099799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1100799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 11011d3aed33SMarcel Moolenaar if (error) 11021d3aed33SMarcel Moolenaar goto out; 1103799eac8cSAndrey V. Elsukov } 11041d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 11051d3aed33SMarcel Moolenaar buf, pp->sectorsize); 11061d3aed33SMarcel Moolenaar if (error) 11071d3aed33SMarcel Moolenaar goto out; 11081d3aed33SMarcel Moolenaar 11091d3aed33SMarcel Moolenaar /* Write secondary meta-data. */ 11101d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 11111d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 11121d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 11131d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 11141c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 11151d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 11161d3aed33SMarcel Moolenaar 1117799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1118799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1119799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1120799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1121799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1122799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 11231d3aed33SMarcel Moolenaar if (error) 11241d3aed33SMarcel Moolenaar goto out; 1125799eac8cSAndrey V. Elsukov } 11261d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 11271d3aed33SMarcel Moolenaar buf, pp->sectorsize); 11281d3aed33SMarcel Moolenaar 11291d3aed33SMarcel Moolenaar out: 11301d3aed33SMarcel Moolenaar g_free(buf); 11311d3aed33SMarcel Moolenaar return (error); 11321d3aed33SMarcel Moolenaar } 11331d3aed33SMarcel Moolenaar 11341d3aed33SMarcel Moolenaar static void 11352b9be055SAndrey V. Elsukov g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 11362b9be055SAndrey V. Elsukov { 11372b9be055SAndrey V. Elsukov struct g_part_gpt_table *table; 11382b9be055SAndrey V. Elsukov quad_t last; 11392b9be055SAndrey V. Elsukov size_t tblsz; 11402b9be055SAndrey V. Elsukov 11412b9be055SAndrey V. Elsukov table = (struct g_part_gpt_table *)basetable; 11422b9be055SAndrey V. Elsukov last = pp->mediasize / pp->sectorsize - 1; 11432b9be055SAndrey V. Elsukov tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 11442b9be055SAndrey V. Elsukov pp->sectorsize - 1) / pp->sectorsize; 11452b9be055SAndrey V. Elsukov 11462b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRIHDR] = 1; 11472b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRITBL] = 2; 11482b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = last; 11492b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECTBL] = last - tblsz; 11502b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 11512b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 11522b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 11532b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 11542b9be055SAndrey V. Elsukov 11552b9be055SAndrey V. Elsukov table->hdr->hdr_lba_start = 2 + tblsz; 11562b9be055SAndrey V. Elsukov table->hdr->hdr_lba_end = last - tblsz - 1; 11572b9be055SAndrey V. Elsukov 11582b9be055SAndrey V. Elsukov basetable->gpt_first = table->hdr->hdr_lba_start; 11592b9be055SAndrey V. Elsukov basetable->gpt_last = table->hdr->hdr_lba_end; 11602b9be055SAndrey V. Elsukov } 11612b9be055SAndrey V. Elsukov 11622b9be055SAndrey V. Elsukov static void 1163d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 11641d3aed33SMarcel Moolenaar { 11651d3aed33SMarcel Moolenaar u_int bo; 11661d3aed33SMarcel Moolenaar uint32_t ch; 11671d3aed33SMarcel Moolenaar uint16_t c; 11681d3aed33SMarcel Moolenaar 1169d3532631SMarcel Moolenaar bo = LITTLE_ENDIAN; /* GPT is little-endian */ 11701d3aed33SMarcel Moolenaar while (len > 0 && *str != 0) { 11711d3aed33SMarcel Moolenaar ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 11721d3aed33SMarcel Moolenaar str++, len--; 11731d3aed33SMarcel Moolenaar if ((ch & 0xf800) == 0xd800) { 11741d3aed33SMarcel Moolenaar if (len > 0) { 11751d3aed33SMarcel Moolenaar c = (bo == BIG_ENDIAN) ? be16toh(*str) 11761d3aed33SMarcel Moolenaar : le16toh(*str); 11771d3aed33SMarcel Moolenaar str++, len--; 11781d3aed33SMarcel Moolenaar } else 11791d3aed33SMarcel Moolenaar c = 0xfffd; 11801d3aed33SMarcel Moolenaar if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 11811d3aed33SMarcel Moolenaar ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 11821d3aed33SMarcel Moolenaar ch += 0x10000; 11831d3aed33SMarcel Moolenaar } else 11841d3aed33SMarcel Moolenaar ch = 0xfffd; 11851d3aed33SMarcel Moolenaar } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 11861d3aed33SMarcel Moolenaar bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 11871d3aed33SMarcel Moolenaar continue; 11881d3aed33SMarcel Moolenaar } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 11891d3aed33SMarcel Moolenaar continue; 11901d3aed33SMarcel Moolenaar 1191d3532631SMarcel Moolenaar /* Write the Unicode character in UTF-8 */ 11921d3aed33SMarcel Moolenaar if (ch < 0x80) 11931d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c", ch); 11941d3aed33SMarcel Moolenaar else if (ch < 0x800) 11951d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6), 11961d3aed33SMarcel Moolenaar 0x80 | (ch & 0x3f)); 11971d3aed33SMarcel Moolenaar else if (ch < 0x10000) 11981d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12), 11991d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 12001d3aed33SMarcel Moolenaar else if (ch < 0x200000) 12011d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18), 12021d3aed33SMarcel Moolenaar 0x80 | ((ch >> 12) & 0x3f), 12031d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 12041d3aed33SMarcel Moolenaar } 12051d3aed33SMarcel Moolenaar } 1206d3532631SMarcel Moolenaar 1207d3532631SMarcel Moolenaar static void 1208d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1209d3532631SMarcel Moolenaar { 1210d3532631SMarcel Moolenaar size_t s16idx, s8idx; 1211d3532631SMarcel Moolenaar uint32_t utfchar; 1212d3532631SMarcel Moolenaar unsigned int c, utfbytes; 1213d3532631SMarcel Moolenaar 1214d3532631SMarcel Moolenaar s8idx = s16idx = 0; 1215d3532631SMarcel Moolenaar utfchar = 0; 1216d3532631SMarcel Moolenaar utfbytes = 0; 1217d3532631SMarcel Moolenaar bzero(s16, s16len << 1); 1218d3532631SMarcel Moolenaar while (s8[s8idx] != 0 && s16idx < s16len) { 1219d3532631SMarcel Moolenaar c = s8[s8idx++]; 1220d3532631SMarcel Moolenaar if ((c & 0xc0) != 0x80) { 1221d3532631SMarcel Moolenaar /* Initial characters. */ 1222d3532631SMarcel Moolenaar if (utfbytes != 0) { 1223d3532631SMarcel Moolenaar /* Incomplete encoding of previous char. */ 1224d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1225d3532631SMarcel Moolenaar } 1226d3532631SMarcel Moolenaar if ((c & 0xf8) == 0xf0) { 1227d3532631SMarcel Moolenaar utfchar = c & 0x07; 1228d3532631SMarcel Moolenaar utfbytes = 3; 1229d3532631SMarcel Moolenaar } else if ((c & 0xf0) == 0xe0) { 1230d3532631SMarcel Moolenaar utfchar = c & 0x0f; 1231d3532631SMarcel Moolenaar utfbytes = 2; 1232d3532631SMarcel Moolenaar } else if ((c & 0xe0) == 0xc0) { 1233d3532631SMarcel Moolenaar utfchar = c & 0x1f; 1234d3532631SMarcel Moolenaar utfbytes = 1; 1235d3532631SMarcel Moolenaar } else { 1236d3532631SMarcel Moolenaar utfchar = c & 0x7f; 1237d3532631SMarcel Moolenaar utfbytes = 0; 1238d3532631SMarcel Moolenaar } 1239d3532631SMarcel Moolenaar } else { 1240d3532631SMarcel Moolenaar /* Followup characters. */ 1241d3532631SMarcel Moolenaar if (utfbytes > 0) { 1242d3532631SMarcel Moolenaar utfchar = (utfchar << 6) + (c & 0x3f); 1243d3532631SMarcel Moolenaar utfbytes--; 1244d3532631SMarcel Moolenaar } else if (utfbytes == 0) 1245d3532631SMarcel Moolenaar utfbytes = ~0; 1246d3532631SMarcel Moolenaar } 1247d3532631SMarcel Moolenaar /* 1248d3532631SMarcel Moolenaar * Write the complete Unicode character as UTF-16 when we 1249d3532631SMarcel Moolenaar * have all the UTF-8 charactars collected. 1250d3532631SMarcel Moolenaar */ 1251d3532631SMarcel Moolenaar if (utfbytes == 0) { 1252d3532631SMarcel Moolenaar /* 1253d3532631SMarcel Moolenaar * If we need to write 2 UTF-16 characters, but 1254d3532631SMarcel Moolenaar * we only have room for 1, then we truncate the 1255d3532631SMarcel Moolenaar * string by writing a 0 instead. 1256d3532631SMarcel Moolenaar */ 1257d3532631SMarcel Moolenaar if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1258d3532631SMarcel Moolenaar s16[s16idx++] = 1259d3532631SMarcel Moolenaar htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1260d3532631SMarcel Moolenaar s16[s16idx++] = 1261d3532631SMarcel Moolenaar htole16(0xdc00 | (utfchar & 0x3ff)); 1262d3532631SMarcel Moolenaar } else 1263d3532631SMarcel Moolenaar s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1264d3532631SMarcel Moolenaar htole16(utfchar); 1265d3532631SMarcel Moolenaar } 1266d3532631SMarcel Moolenaar } 1267d3532631SMarcel Moolenaar /* 1268d3532631SMarcel Moolenaar * If our input string was truncated, append an invalid encoding 1269d3532631SMarcel Moolenaar * character to the output string. 1270d3532631SMarcel Moolenaar */ 1271d3532631SMarcel Moolenaar if (utfbytes != 0 && s16idx < s16len) 1272d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1273d3532631SMarcel Moolenaar } 1274