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; 158f24a8224SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 1591d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 1601d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 1611d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 162a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 163f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 164f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 165f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 1661d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 167b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 168b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 169b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 17033f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 17133f7a412SRui Paulo static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 17233f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 17333f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 17433f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 17533f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 17633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 17733f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 17833f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 17933f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 1801d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 1811d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 1821d3aed33SMarcel Moolenaar 183f1317430SRui Paulo static struct g_part_uuid_alias { 184f1317430SRui Paulo struct uuid *uuid; 185f1317430SRui Paulo int alias; 186369fe59dSMarcel Moolenaar int mbrtype; 187f1317430SRui Paulo } gpt_uuid_alias_match[] = { 188369fe59dSMarcel Moolenaar { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 189369fe59dSMarcel Moolenaar { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 190369fe59dSMarcel Moolenaar { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 191369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 192369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 193369fe59dSMarcel Moolenaar { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 194369fe59dSMarcel Moolenaar { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 195369fe59dSMarcel Moolenaar { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 196369fe59dSMarcel Moolenaar { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 197369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 198369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 199f24a8224SMarcel Moolenaar { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 }, 200369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 201369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 202369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 203369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 204369fe59dSMarcel Moolenaar { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 205369fe59dSMarcel Moolenaar { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 206369fe59dSMarcel Moolenaar { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 207369fe59dSMarcel Moolenaar { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 208b20e4de3SDmitry Morozovsky { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 209b20e4de3SDmitry Morozovsky { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 210b20e4de3SDmitry Morozovsky { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 211369fe59dSMarcel Moolenaar { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 212369fe59dSMarcel Moolenaar { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 213369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 214369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 215369fe59dSMarcel Moolenaar { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 216369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 217369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 218369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 219369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 220369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 221369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 222369fe59dSMarcel Moolenaar { NULL, 0, 0 } 223f1317430SRui Paulo }; 224f1317430SRui Paulo 225369fe59dSMarcel Moolenaar static int 226369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 227369fe59dSMarcel Moolenaar quad_t end) 228369fe59dSMarcel Moolenaar { 229369fe59dSMarcel Moolenaar 230369fe59dSMarcel Moolenaar if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 231369fe59dSMarcel Moolenaar return (EINVAL); 232369fe59dSMarcel Moolenaar 233369fe59dSMarcel Moolenaar mbr += DOSPARTOFF + idx * DOSPARTSIZE; 234369fe59dSMarcel Moolenaar mbr[0] = 0; 235369fe59dSMarcel Moolenaar if (start == 1) { 236369fe59dSMarcel Moolenaar /* 237369fe59dSMarcel Moolenaar * Treat the PMBR partition specially to maximize 238369fe59dSMarcel Moolenaar * interoperability with BIOSes. 239369fe59dSMarcel Moolenaar */ 240369fe59dSMarcel Moolenaar mbr[1] = mbr[3] = 0; 241369fe59dSMarcel Moolenaar mbr[2] = 2; 242369fe59dSMarcel Moolenaar } else 243369fe59dSMarcel Moolenaar mbr[1] = mbr[2] = mbr[3] = 0xff; 244369fe59dSMarcel Moolenaar mbr[4] = typ; 245369fe59dSMarcel Moolenaar mbr[5] = mbr[6] = mbr[7] = 0xff; 246369fe59dSMarcel Moolenaar le32enc(mbr + 8, (uint32_t)start); 247369fe59dSMarcel Moolenaar le32enc(mbr + 12, (uint32_t)(end - start + 1)); 248369fe59dSMarcel Moolenaar return (0); 249369fe59dSMarcel Moolenaar } 250369fe59dSMarcel Moolenaar 251369fe59dSMarcel Moolenaar static int 252369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t) 253369fe59dSMarcel Moolenaar { 254369fe59dSMarcel Moolenaar struct g_part_uuid_alias *uap; 255369fe59dSMarcel Moolenaar 256369fe59dSMarcel Moolenaar for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 257369fe59dSMarcel Moolenaar if (EQUUID(t, uap->uuid)) 258369fe59dSMarcel Moolenaar return (uap->mbrtype); 259369fe59dSMarcel Moolenaar } 260369fe59dSMarcel Moolenaar return (0); 261369fe59dSMarcel Moolenaar } 262369fe59dSMarcel Moolenaar 263*3bd22a9cSMarcel Moolenaar static void 264*3bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 265*3bd22a9cSMarcel Moolenaar { 266*3bd22a9cSMarcel Moolenaar 267*3bd22a9cSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 268*3bd22a9cSMarcel Moolenaar gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 269*3bd22a9cSMarcel Moolenaar MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 270*3bd22a9cSMarcel Moolenaar le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 271*3bd22a9cSMarcel Moolenaar } 272*3bd22a9cSMarcel Moolenaar 273369fe59dSMarcel Moolenaar /* 274369fe59dSMarcel Moolenaar * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 275369fe59dSMarcel Moolenaar * whole disk anymore. Rather, it covers the GPT table and the EFI 276369fe59dSMarcel Moolenaar * system partition only. This way the HFS+ partition and any FAT 277369fe59dSMarcel Moolenaar * partitions can be added to the MBR without creating an overlap. 278369fe59dSMarcel Moolenaar */ 279369fe59dSMarcel Moolenaar static int 280369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 281369fe59dSMarcel Moolenaar { 282369fe59dSMarcel Moolenaar uint8_t *p; 283369fe59dSMarcel Moolenaar 284369fe59dSMarcel Moolenaar p = table->mbr + DOSPARTOFF; 285369fe59dSMarcel Moolenaar if (p[4] != 0xee || le32dec(p + 8) != 1) 286369fe59dSMarcel Moolenaar return (0); 287369fe59dSMarcel Moolenaar 288369fe59dSMarcel Moolenaar p += DOSPARTSIZE; 289369fe59dSMarcel Moolenaar if (p[4] != 0xaf) 290369fe59dSMarcel Moolenaar return (0); 291369fe59dSMarcel Moolenaar 292369fe59dSMarcel Moolenaar printf("GEOM: %s: enabling Boot Camp\n", provname); 293369fe59dSMarcel Moolenaar return (1); 294369fe59dSMarcel Moolenaar } 295369fe59dSMarcel Moolenaar 296369fe59dSMarcel Moolenaar static void 297*3bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 298369fe59dSMarcel Moolenaar { 299369fe59dSMarcel Moolenaar struct g_part_entry *baseentry; 300369fe59dSMarcel Moolenaar struct g_part_gpt_entry *entry; 301369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 302369fe59dSMarcel Moolenaar int bootable, error, index, slices, typ; 303369fe59dSMarcel Moolenaar 304369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 305369fe59dSMarcel Moolenaar 306369fe59dSMarcel Moolenaar bootable = -1; 307369fe59dSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 308369fe59dSMarcel Moolenaar if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 309369fe59dSMarcel Moolenaar bootable = index; 310369fe59dSMarcel Moolenaar } 311369fe59dSMarcel Moolenaar 312369fe59dSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 313369fe59dSMarcel Moolenaar slices = 0; 314369fe59dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 315369fe59dSMarcel Moolenaar if (baseentry->gpe_deleted) 316369fe59dSMarcel Moolenaar continue; 317369fe59dSMarcel Moolenaar index = baseentry->gpe_index - 1; 318369fe59dSMarcel Moolenaar if (index >= NDOSPART) 319369fe59dSMarcel Moolenaar continue; 320369fe59dSMarcel Moolenaar 321369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 322369fe59dSMarcel Moolenaar 323369fe59dSMarcel Moolenaar switch (index) { 324369fe59dSMarcel Moolenaar case 0: /* This must be the EFI system partition. */ 325369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 326369fe59dSMarcel Moolenaar goto disable; 327369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xee, 328369fe59dSMarcel Moolenaar 1ull, entry->ent.ent_lba_end); 329369fe59dSMarcel Moolenaar break; 330369fe59dSMarcel Moolenaar case 1: /* This must be the HFS+ partition. */ 331369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 332369fe59dSMarcel Moolenaar goto disable; 333369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 334369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 335369fe59dSMarcel Moolenaar break; 336369fe59dSMarcel Moolenaar default: 337369fe59dSMarcel Moolenaar typ = gpt_map_type(&entry->ent.ent_type); 338369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, typ, 339369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 340369fe59dSMarcel Moolenaar break; 341369fe59dSMarcel Moolenaar } 342369fe59dSMarcel Moolenaar if (error) 343369fe59dSMarcel Moolenaar continue; 344369fe59dSMarcel Moolenaar 345369fe59dSMarcel Moolenaar if (index == bootable) 346369fe59dSMarcel Moolenaar table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 347369fe59dSMarcel Moolenaar slices |= 1 << index; 348369fe59dSMarcel Moolenaar } 349369fe59dSMarcel Moolenaar if ((slices & 3) == 3) 350369fe59dSMarcel Moolenaar return; 351369fe59dSMarcel Moolenaar 352369fe59dSMarcel Moolenaar disable: 353369fe59dSMarcel Moolenaar table->bootcamp = 0; 354*3bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 355369fe59dSMarcel Moolenaar } 356369fe59dSMarcel Moolenaar 3571c2dee3cSRobert Noland static struct gpt_hdr * 3581d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 3591c2dee3cSRobert Noland enum gpt_elt elt) 3601d3aed33SMarcel Moolenaar { 3611c2dee3cSRobert Noland struct gpt_hdr *buf, *hdr; 3621d3aed33SMarcel Moolenaar struct g_provider *pp; 3631d3aed33SMarcel Moolenaar quad_t lba, last; 3641d3aed33SMarcel Moolenaar int error; 3651d3aed33SMarcel Moolenaar uint32_t crc, sz; 3661d3aed33SMarcel Moolenaar 3671d3aed33SMarcel Moolenaar pp = cp->provider; 3681d3aed33SMarcel Moolenaar last = (pp->mediasize / pp->sectorsize) - 1; 3691d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 370e7926a37SAndrey V. Elsukov /* 371e7926a37SAndrey V. Elsukov * If the primary header is valid look for secondary 372e7926a37SAndrey V. Elsukov * header in AlternateLBA, otherwise in the last medium's LBA. 373e7926a37SAndrey V. Elsukov */ 374e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_SECHDR) { 375e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 376e7926a37SAndrey V. Elsukov table->lba[elt] = last; 377e7926a37SAndrey V. Elsukov } else 378e7926a37SAndrey V. Elsukov table->lba[elt] = 1; 3791d3aed33SMarcel Moolenaar buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 3801d3aed33SMarcel Moolenaar &error); 3811d3aed33SMarcel Moolenaar if (buf == NULL) 3821c2dee3cSRobert Noland return (NULL); 3831c2dee3cSRobert Noland hdr = NULL; 3841c2dee3cSRobert Noland if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 3851c2dee3cSRobert Noland goto fail; 3861d3aed33SMarcel Moolenaar 3871d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 3881c2dee3cSRobert Noland sz = le32toh(buf->hdr_size); 3891d3aed33SMarcel Moolenaar if (sz < 92 || sz > pp->sectorsize) 3901c2dee3cSRobert Noland goto fail; 3911c2dee3cSRobert Noland 3921c2dee3cSRobert Noland hdr = g_malloc(sz, M_WAITOK | M_ZERO); 3931c2dee3cSRobert Noland bcopy(buf, hdr, sz); 3941d3aed33SMarcel Moolenaar hdr->hdr_size = sz; 3951c2dee3cSRobert Noland 3961c2dee3cSRobert Noland crc = le32toh(buf->hdr_crc_self); 3971c2dee3cSRobert Noland buf->hdr_crc_self = 0; 3981c2dee3cSRobert Noland if (crc32(buf, sz) != crc) 3991c2dee3cSRobert Noland goto fail; 4001d3aed33SMarcel Moolenaar hdr->hdr_crc_self = crc; 4011d3aed33SMarcel Moolenaar 4021d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_INVALID; 4031c2dee3cSRobert Noland hdr->hdr_revision = le32toh(buf->hdr_revision); 404e7926a37SAndrey V. Elsukov if (hdr->hdr_revision < GPT_HDR_REVISION) 4051c2dee3cSRobert Noland goto fail; 4061c2dee3cSRobert Noland hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 4071d3aed33SMarcel Moolenaar if (hdr->hdr_lba_self != table->lba[elt]) 4081c2dee3cSRobert Noland goto fail; 4091c2dee3cSRobert Noland hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 410e7926a37SAndrey V. Elsukov if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 411e7926a37SAndrey V. Elsukov hdr->hdr_lba_alt > last) 412e7926a37SAndrey V. Elsukov goto fail; 4131d3aed33SMarcel Moolenaar 4141d3aed33SMarcel Moolenaar /* Check the managed area. */ 4151c2dee3cSRobert Noland hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 4161d3aed33SMarcel Moolenaar if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 4171c2dee3cSRobert Noland goto fail; 4181c2dee3cSRobert Noland hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 4191d3aed33SMarcel Moolenaar if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 4201c2dee3cSRobert Noland goto fail; 4211d3aed33SMarcel Moolenaar 4221d3aed33SMarcel Moolenaar /* Check the table location and size of the table. */ 4231c2dee3cSRobert Noland hdr->hdr_entries = le32toh(buf->hdr_entries); 4241c2dee3cSRobert Noland hdr->hdr_entsz = le32toh(buf->hdr_entsz); 4251d3aed33SMarcel Moolenaar if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 4261d3aed33SMarcel Moolenaar (hdr->hdr_entsz & 7) != 0) 4271c2dee3cSRobert Noland goto fail; 4281c2dee3cSRobert Noland hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 4291d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 4301c2dee3cSRobert Noland goto fail; 4311d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 4321d3aed33SMarcel Moolenaar hdr->hdr_lba_table <= hdr->hdr_lba_end) 4331c2dee3cSRobert Noland goto fail; 4341d3aed33SMarcel Moolenaar lba = hdr->hdr_lba_table + 4351d3aed33SMarcel Moolenaar (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) / 4361d3aed33SMarcel Moolenaar pp->sectorsize - 1; 4371d3aed33SMarcel Moolenaar if (lba >= last) 4381c2dee3cSRobert Noland goto fail; 4391d3aed33SMarcel Moolenaar if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 4401c2dee3cSRobert Noland goto fail; 4411d3aed33SMarcel Moolenaar 4421d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4431c2dee3cSRobert Noland le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 4441c2dee3cSRobert Noland hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 4451c2dee3cSRobert Noland 446e7926a37SAndrey V. Elsukov /* save LBA for secondary header */ 447e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_PRIHDR) 448e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 449e7926a37SAndrey V. Elsukov 4501c2dee3cSRobert Noland g_free(buf); 4511c2dee3cSRobert Noland return (hdr); 4521c2dee3cSRobert Noland 4531c2dee3cSRobert Noland fail: 4541c2dee3cSRobert Noland if (hdr != NULL) 4551c2dee3cSRobert Noland g_free(hdr); 4561c2dee3cSRobert Noland g_free(buf); 4571c2dee3cSRobert Noland return (NULL); 4581d3aed33SMarcel Moolenaar } 4591d3aed33SMarcel Moolenaar 4601d3aed33SMarcel Moolenaar static struct gpt_ent * 4611d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 4621d3aed33SMarcel Moolenaar enum gpt_elt elt, struct gpt_hdr *hdr) 4631d3aed33SMarcel Moolenaar { 4641d3aed33SMarcel Moolenaar struct g_provider *pp; 4651d3aed33SMarcel Moolenaar struct gpt_ent *ent, *tbl; 4661d3aed33SMarcel Moolenaar char *buf, *p; 467799eac8cSAndrey V. Elsukov unsigned int idx, sectors, tblsz, size; 4681d3aed33SMarcel Moolenaar int error; 4691d3aed33SMarcel Moolenaar 4701c2dee3cSRobert Noland if (hdr == NULL) 4711c2dee3cSRobert Noland return (NULL); 4721c2dee3cSRobert Noland 4731d3aed33SMarcel Moolenaar pp = cp->provider; 4741d3aed33SMarcel Moolenaar table->lba[elt] = hdr->hdr_lba_table; 4751d3aed33SMarcel Moolenaar 4761d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 4771d3aed33SMarcel Moolenaar tblsz = hdr->hdr_entries * hdr->hdr_entsz; 4781d3aed33SMarcel Moolenaar sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize; 479799eac8cSAndrey V. Elsukov buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 480799eac8cSAndrey V. Elsukov for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 481799eac8cSAndrey V. Elsukov size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 482799eac8cSAndrey V. Elsukov (sectors - idx) * pp->sectorsize; 483799eac8cSAndrey V. Elsukov p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 484799eac8cSAndrey V. Elsukov size, &error); 485799eac8cSAndrey V. Elsukov if (p == NULL) { 486799eac8cSAndrey V. Elsukov g_free(buf); 4871d3aed33SMarcel Moolenaar return (NULL); 488799eac8cSAndrey V. Elsukov } 489799eac8cSAndrey V. Elsukov bcopy(p, buf + idx * pp->sectorsize, size); 490799eac8cSAndrey V. Elsukov g_free(p); 491799eac8cSAndrey V. Elsukov } 4921d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 4931d3aed33SMarcel Moolenaar if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 4941d3aed33SMarcel Moolenaar g_free(buf); 4951d3aed33SMarcel Moolenaar return (NULL); 4961d3aed33SMarcel Moolenaar } 4971d3aed33SMarcel Moolenaar 4981d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4991d3aed33SMarcel Moolenaar tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 5001d3aed33SMarcel Moolenaar M_WAITOK | M_ZERO); 5011d3aed33SMarcel Moolenaar 5021d3aed33SMarcel Moolenaar for (idx = 0, ent = tbl, p = buf; 5031d3aed33SMarcel Moolenaar idx < hdr->hdr_entries; 5041d3aed33SMarcel Moolenaar idx++, ent++, p += hdr->hdr_entsz) { 5051d3aed33SMarcel Moolenaar le_uuid_dec(p, &ent->ent_type); 5061d3aed33SMarcel Moolenaar le_uuid_dec(p + 16, &ent->ent_uuid); 5071d3aed33SMarcel Moolenaar ent->ent_lba_start = le64dec(p + 32); 5081d3aed33SMarcel Moolenaar ent->ent_lba_end = le64dec(p + 40); 5091d3aed33SMarcel Moolenaar ent->ent_attr = le64dec(p + 48); 510d3532631SMarcel Moolenaar /* Keep UTF-16 in little-endian. */ 511d3532631SMarcel Moolenaar bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 5121d3aed33SMarcel Moolenaar } 5131d3aed33SMarcel Moolenaar 5141d3aed33SMarcel Moolenaar g_free(buf); 5151d3aed33SMarcel Moolenaar return (tbl); 5161d3aed33SMarcel Moolenaar } 5171d3aed33SMarcel Moolenaar 5181d3aed33SMarcel Moolenaar static int 5191d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 5201d3aed33SMarcel Moolenaar { 5211d3aed33SMarcel Moolenaar 5221c2dee3cSRobert Noland if (pri == NULL || sec == NULL) 5231c2dee3cSRobert Noland return (0); 5241c2dee3cSRobert Noland 5251d3aed33SMarcel Moolenaar if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 5261d3aed33SMarcel Moolenaar return (0); 5271d3aed33SMarcel Moolenaar return ((pri->hdr_revision == sec->hdr_revision && 5281d3aed33SMarcel Moolenaar pri->hdr_size == sec->hdr_size && 5291d3aed33SMarcel Moolenaar pri->hdr_lba_start == sec->hdr_lba_start && 5301d3aed33SMarcel Moolenaar pri->hdr_lba_end == sec->hdr_lba_end && 5311d3aed33SMarcel Moolenaar pri->hdr_entries == sec->hdr_entries && 5321d3aed33SMarcel Moolenaar pri->hdr_entsz == sec->hdr_entsz && 5331d3aed33SMarcel Moolenaar pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 5341d3aed33SMarcel Moolenaar } 5351d3aed33SMarcel Moolenaar 5361d3aed33SMarcel Moolenaar static int 5371d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid) 5381d3aed33SMarcel Moolenaar { 5391d3aed33SMarcel Moolenaar struct uuid tmp; 540d287f590SMarcel Moolenaar const char *alias; 5411d3aed33SMarcel Moolenaar int error; 542f1317430SRui Paulo struct g_part_uuid_alias *uap; 5431d3aed33SMarcel Moolenaar 544d287f590SMarcel Moolenaar if (type[0] == '!') { 545d287f590SMarcel Moolenaar error = parse_uuid(type + 1, &tmp); 5461d3aed33SMarcel Moolenaar if (error) 5471d3aed33SMarcel Moolenaar return (error); 5481d3aed33SMarcel Moolenaar if (EQUUID(&tmp, &gpt_uuid_unused)) 5491d3aed33SMarcel Moolenaar return (EINVAL); 5501d3aed33SMarcel Moolenaar *uuid = tmp; 5511d3aed33SMarcel Moolenaar return (0); 5521d3aed33SMarcel Moolenaar } 553f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 554f1317430SRui Paulo alias = g_part_alias_name(uap->alias); 555d287f590SMarcel Moolenaar if (!strcasecmp(type, alias)) { 556665bb830SMarcel Moolenaar *uuid = *uap->uuid; 5571d3aed33SMarcel Moolenaar return (0); 5581d3aed33SMarcel Moolenaar } 559d7255ff4SRui Paulo } 560d287f590SMarcel Moolenaar return (EINVAL); 561d287f590SMarcel Moolenaar } 5621d3aed33SMarcel Moolenaar 5631d3aed33SMarcel Moolenaar static int 5641d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 5651d3aed33SMarcel Moolenaar struct g_part_parms *gpp) 5661d3aed33SMarcel Moolenaar { 5671d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 5681d3aed33SMarcel Moolenaar int error; 5691d3aed33SMarcel Moolenaar 5701d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 5711d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 5721d3aed33SMarcel Moolenaar if (error) 5731d3aed33SMarcel Moolenaar return (error); 5741d3aed33SMarcel Moolenaar kern_uuidgen(&entry->ent.ent_uuid, 1); 5751d3aed33SMarcel Moolenaar entry->ent.ent_lba_start = baseentry->gpe_start; 5761d3aed33SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 5771d3aed33SMarcel Moolenaar if (baseentry->gpe_deleted) { 5781d3aed33SMarcel Moolenaar entry->ent.ent_attr = 0; 5791d3aed33SMarcel Moolenaar bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 5801d3aed33SMarcel Moolenaar } 581d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 582d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 583ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 584ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 5851d3aed33SMarcel Moolenaar return (0); 5861d3aed33SMarcel Moolenaar } 5871d3aed33SMarcel Moolenaar 5881d3aed33SMarcel Moolenaar static int 5894d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 5904d32fcb4SMarcel Moolenaar { 5914d32fcb4SMarcel Moolenaar struct g_part_gpt_table *table; 59266477112SMarcel Moolenaar size_t codesz; 5934d32fcb4SMarcel Moolenaar 59466477112SMarcel Moolenaar codesz = DOSPARTOFF; 5954d32fcb4SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 59666477112SMarcel Moolenaar bzero(table->mbr, codesz); 59766477112SMarcel Moolenaar codesz = MIN(codesz, gpp->gpp_codesize); 59866477112SMarcel Moolenaar if (codesz > 0) 59966477112SMarcel Moolenaar bcopy(gpp->gpp_codeptr, table->mbr, codesz); 6004d32fcb4SMarcel Moolenaar return (0); 6014d32fcb4SMarcel Moolenaar } 6024d32fcb4SMarcel Moolenaar 6034d32fcb4SMarcel Moolenaar static int 6041d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 6051d3aed33SMarcel Moolenaar { 6061d3aed33SMarcel Moolenaar struct g_provider *pp; 6071d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 6081d3aed33SMarcel Moolenaar size_t tblsz; 6091d3aed33SMarcel Moolenaar 6102a1c00ffSMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 6112a1c00ffSMarcel Moolenaar if (basetable->gpt_depth != 0) 6122a1c00ffSMarcel Moolenaar return (ENXIO); 6132a1c00ffSMarcel Moolenaar 6141d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 6151d3aed33SMarcel Moolenaar pp = gpp->gpp_provider; 6161d3aed33SMarcel Moolenaar tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 6171d3aed33SMarcel Moolenaar pp->sectorsize - 1) / pp->sectorsize; 6184d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || 6191d3aed33SMarcel Moolenaar pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 6201d3aed33SMarcel Moolenaar pp->sectorsize) 6211d3aed33SMarcel Moolenaar return (ENOSPC); 6221d3aed33SMarcel Moolenaar 623*3bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 624*3bd22a9cSMarcel Moolenaar 625a59a1310SRobert Noland /* Allocate space for the header */ 626a59a1310SRobert Noland table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 627a59a1310SRobert Noland 6281c2dee3cSRobert Noland bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 6291c2dee3cSRobert Noland table->hdr->hdr_revision = GPT_HDR_REVISION; 6301c2dee3cSRobert Noland table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 6311c2dee3cSRobert Noland kern_uuidgen(&table->hdr->hdr_uuid, 1); 6321c2dee3cSRobert Noland table->hdr->hdr_entries = basetable->gpt_entries; 6331c2dee3cSRobert Noland table->hdr->hdr_entsz = sizeof(struct gpt_ent); 6341d3aed33SMarcel Moolenaar 6352b9be055SAndrey V. Elsukov g_gpt_set_defaults(basetable, pp); 6361d3aed33SMarcel Moolenaar return (0); 6371d3aed33SMarcel Moolenaar } 6381d3aed33SMarcel Moolenaar 6391d3aed33SMarcel Moolenaar static int 6401d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 6411d3aed33SMarcel Moolenaar { 64236066952SMarius Strobl struct g_part_gpt_table *table; 643e7926a37SAndrey V. Elsukov struct g_provider *pp; 64436066952SMarius Strobl 64536066952SMarius Strobl table = (struct g_part_gpt_table *)basetable; 646e7926a37SAndrey V. Elsukov pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 64736066952SMarius Strobl g_free(table->hdr); 64836066952SMarius Strobl table->hdr = NULL; 6491d3aed33SMarcel Moolenaar 6501d3aed33SMarcel Moolenaar /* 651e7926a37SAndrey V. Elsukov * Wipe the first 2 sectors to clear the partitioning. Wipe the last 652e7926a37SAndrey V. Elsukov * sector only if it has valid secondary header. 6531d3aed33SMarcel Moolenaar */ 6541d3aed33SMarcel Moolenaar basetable->gpt_smhead |= 3; 655e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 656e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 6571d3aed33SMarcel Moolenaar basetable->gpt_smtail |= 1; 6581d3aed33SMarcel Moolenaar return (0); 6591d3aed33SMarcel Moolenaar } 6601d3aed33SMarcel Moolenaar 66151f53a08SWarner Losh static void 6625db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 6635db67052SMarcel Moolenaar struct sbuf *sb, const char *indent) 6645db67052SMarcel Moolenaar { 6655db67052SMarcel Moolenaar struct g_part_gpt_entry *entry; 6665db67052SMarcel Moolenaar 6675db67052SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 6680c132595SMarcel Moolenaar if (indent == NULL) { 6690c132595SMarcel Moolenaar /* conftxt: libdisk compatibility */ 6705db67052SMarcel Moolenaar sbuf_printf(sb, " xs GPT xt "); 6715db67052SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 6720c132595SMarcel Moolenaar } else if (entry != NULL) { 6730c132595SMarcel Moolenaar /* confxml: partition entry information */ 674d3532631SMarcel Moolenaar sbuf_printf(sb, "%s<label>", indent); 675d3532631SMarcel Moolenaar g_gpt_printf_utf16(sb, entry->ent.ent_name, 676d3532631SMarcel Moolenaar sizeof(entry->ent.ent_name) >> 1); 677d3532631SMarcel Moolenaar sbuf_printf(sb, "</label>\n"); 678cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 679cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 680cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 681cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 682cec283baSPawel Jakub Dawidek indent); 683cec283baSPawel Jakub Dawidek } 684cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 685cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 686cec283baSPawel Jakub Dawidek indent); 687cec283baSPawel Jakub Dawidek } 6880c132595SMarcel Moolenaar sbuf_printf(sb, "%s<rawtype>", indent); 6890c132595SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 6900c132595SMarcel Moolenaar sbuf_printf(sb, "</rawtype>\n"); 691cd18ad83SMarcel Moolenaar sbuf_printf(sb, "%s<rawuuid>", indent); 692cd18ad83SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 693cd18ad83SMarcel Moolenaar sbuf_printf(sb, "</rawuuid>\n"); 6940c132595SMarcel Moolenaar } else { 6950c132595SMarcel Moolenaar /* confxml: scheme information */ 6960c132595SMarcel Moolenaar } 6975db67052SMarcel Moolenaar } 6985db67052SMarcel Moolenaar 6995db67052SMarcel Moolenaar static int 7001d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 7011d3aed33SMarcel Moolenaar { 7021d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7031d3aed33SMarcel Moolenaar 7041d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7051d3aed33SMarcel Moolenaar return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 7061d3aed33SMarcel Moolenaar EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0); 7071d3aed33SMarcel Moolenaar } 7081d3aed33SMarcel Moolenaar 7091d3aed33SMarcel Moolenaar static int 7101d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable, 7111d3aed33SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7121d3aed33SMarcel Moolenaar { 7131d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7141d3aed33SMarcel Moolenaar int error; 7151d3aed33SMarcel Moolenaar 7161d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 717d287f590SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_TYPE) { 7181d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 7191d3aed33SMarcel Moolenaar if (error) 7201d3aed33SMarcel Moolenaar return (error); 721d287f590SMarcel Moolenaar } 722d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 723d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 724ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 725ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 7261d3aed33SMarcel Moolenaar return (0); 7271d3aed33SMarcel Moolenaar } 7281d3aed33SMarcel Moolenaar 7293f71c319SMarcel Moolenaar static int 7303f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable, 7313f71c319SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7323f71c319SMarcel Moolenaar { 7333f71c319SMarcel Moolenaar struct g_part_gpt_entry *entry; 7343f71c319SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7353f71c319SMarcel Moolenaar 7363f71c319SMarcel Moolenaar baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 7373f71c319SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 7383f71c319SMarcel Moolenaar 7393f71c319SMarcel Moolenaar return (0); 7403f71c319SMarcel Moolenaar } 7413f71c319SMarcel Moolenaar 74251f53a08SWarner Losh static const char * 7431d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 7441d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 7451d3aed33SMarcel Moolenaar { 7461d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7471d3aed33SMarcel Moolenaar char c; 7481d3aed33SMarcel Moolenaar 7491d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7501d3aed33SMarcel Moolenaar c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 7511d3aed33SMarcel Moolenaar snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 7521d3aed33SMarcel Moolenaar return (buf); 7531d3aed33SMarcel Moolenaar } 7541d3aed33SMarcel Moolenaar 7551d3aed33SMarcel Moolenaar static int 7561d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 7571d3aed33SMarcel Moolenaar { 7581d3aed33SMarcel Moolenaar struct g_provider *pp; 7591d3aed33SMarcel Moolenaar char *buf; 7601d3aed33SMarcel Moolenaar int error, res; 7611d3aed33SMarcel Moolenaar 7621d3aed33SMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 7631d3aed33SMarcel Moolenaar if (table->gpt_depth != 0) 7641d3aed33SMarcel Moolenaar return (ENXIO); 7651d3aed33SMarcel Moolenaar 7661d3aed33SMarcel Moolenaar pp = cp->provider; 7671d3aed33SMarcel Moolenaar 7681d3aed33SMarcel Moolenaar /* 7691d3aed33SMarcel Moolenaar * Sanity-check the provider. Since the first sector on the provider 7701d3aed33SMarcel Moolenaar * must be a PMBR and a PMBR is 512 bytes large, the sector size 7711d3aed33SMarcel Moolenaar * must be at least 512 bytes. Also, since the theoretical minimum 7721d3aed33SMarcel Moolenaar * number of sectors needed by GPT is 6, any medium that has less 7731d3aed33SMarcel Moolenaar * than 6 sectors is never going to be able to hold a GPT. The 7741d3aed33SMarcel Moolenaar * number 6 comes from: 7751d3aed33SMarcel Moolenaar * 1 sector for the PMBR 7761d3aed33SMarcel Moolenaar * 2 sectors for the GPT headers (each 1 sector) 7771d3aed33SMarcel Moolenaar * 2 sectors for the GPT tables (each 1 sector) 7781d3aed33SMarcel Moolenaar * 1 sector for an actual partition 7791d3aed33SMarcel Moolenaar * It's better to catch this pathological case early than behaving 7801d3aed33SMarcel Moolenaar * pathologically later on... 7811d3aed33SMarcel Moolenaar */ 7824d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 7831d3aed33SMarcel Moolenaar return (ENOSPC); 7841d3aed33SMarcel Moolenaar 7851d3aed33SMarcel Moolenaar /* Check that there's a MBR. */ 7861d3aed33SMarcel Moolenaar buf = g_read_data(cp, 0L, pp->sectorsize, &error); 7871d3aed33SMarcel Moolenaar if (buf == NULL) 7881d3aed33SMarcel Moolenaar return (error); 7891d3aed33SMarcel Moolenaar res = le16dec(buf + DOSMAGICOFFSET); 7901d3aed33SMarcel Moolenaar g_free(buf); 7911d3aed33SMarcel Moolenaar if (res != DOSMAGIC) 7921d3aed33SMarcel Moolenaar return (ENXIO); 7931d3aed33SMarcel Moolenaar 7941d3aed33SMarcel Moolenaar /* Check that there's a primary header. */ 7951d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 7961d3aed33SMarcel Moolenaar if (buf == NULL) 7971d3aed33SMarcel Moolenaar return (error); 7981d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 7991d3aed33SMarcel Moolenaar g_free(buf); 8001d3aed33SMarcel Moolenaar if (res == 0) 8011d3aed33SMarcel Moolenaar return (G_PART_PROBE_PRI_HIGH); 8021d3aed33SMarcel Moolenaar 8031d3aed33SMarcel Moolenaar /* No primary? Check that there's a secondary. */ 8041d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 8051d3aed33SMarcel Moolenaar &error); 8061d3aed33SMarcel Moolenaar if (buf == NULL) 8071d3aed33SMarcel Moolenaar return (error); 8081d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8091d3aed33SMarcel Moolenaar g_free(buf); 8101d3aed33SMarcel Moolenaar return ((res == 0) ? G_PART_PROBE_PRI_HIGH : ENXIO); 8111d3aed33SMarcel Moolenaar } 8121d3aed33SMarcel Moolenaar 8131d3aed33SMarcel Moolenaar static int 8141d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 8151d3aed33SMarcel Moolenaar { 8161c2dee3cSRobert Noland struct gpt_hdr *prihdr, *sechdr; 8171d3aed33SMarcel Moolenaar struct gpt_ent *tbl, *pritbl, *sectbl; 8181d3aed33SMarcel Moolenaar struct g_provider *pp; 8191d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 8201d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8214d32fcb4SMarcel Moolenaar u_char *buf; 822e7926a37SAndrey V. Elsukov uint64_t last; 8234d32fcb4SMarcel Moolenaar int error, index; 8241d3aed33SMarcel Moolenaar 8251d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 8261d3aed33SMarcel Moolenaar pp = cp->provider; 827e7926a37SAndrey V. Elsukov last = (pp->mediasize / pp->sectorsize) - 1; 8281d3aed33SMarcel Moolenaar 8294d32fcb4SMarcel Moolenaar /* Read the PMBR */ 8304d32fcb4SMarcel Moolenaar buf = g_read_data(cp, 0, pp->sectorsize, &error); 8314d32fcb4SMarcel Moolenaar if (buf == NULL) 8324d32fcb4SMarcel Moolenaar return (error); 8334d32fcb4SMarcel Moolenaar bcopy(buf, table->mbr, MBRSIZE); 8344d32fcb4SMarcel Moolenaar g_free(buf); 8354d32fcb4SMarcel Moolenaar 8361d3aed33SMarcel Moolenaar /* Read the primary header and table. */ 8371c2dee3cSRobert Noland prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 8381d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 8391c2dee3cSRobert Noland pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 8401d3aed33SMarcel Moolenaar } else { 8411d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 8421d3aed33SMarcel Moolenaar pritbl = NULL; 8431d3aed33SMarcel Moolenaar } 8441d3aed33SMarcel Moolenaar 8451d3aed33SMarcel Moolenaar /* Read the secondary header and table. */ 8461c2dee3cSRobert Noland sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 8471d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 8481c2dee3cSRobert Noland sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 8491d3aed33SMarcel Moolenaar } else { 8501d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 8511d3aed33SMarcel Moolenaar sectbl = NULL; 8521d3aed33SMarcel Moolenaar } 8531d3aed33SMarcel Moolenaar 8541d3aed33SMarcel Moolenaar /* Fail if we haven't got any good tables at all. */ 8551d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 8561d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 8571d3aed33SMarcel Moolenaar printf("GEOM: %s: corrupt or invalid GPT detected.\n", 8581d3aed33SMarcel Moolenaar pp->name); 8591d3aed33SMarcel Moolenaar printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 8601d3aed33SMarcel Moolenaar pp->name); 8611d3aed33SMarcel Moolenaar return (EINVAL); 8621d3aed33SMarcel Moolenaar } 8631d3aed33SMarcel Moolenaar 8641d3aed33SMarcel Moolenaar /* 8651d3aed33SMarcel Moolenaar * If both headers are good but they disagree with each other, 8661d3aed33SMarcel Moolenaar * then invalidate one. We prefer to keep the primary header, 8671d3aed33SMarcel Moolenaar * unless the primary table is corrupt. 8681d3aed33SMarcel Moolenaar */ 8691d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 8701d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 8711c2dee3cSRobert Noland !gpt_matched_hdrs(prihdr, sechdr)) { 872dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 8731d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 874dd0db05dSMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 8751c2dee3cSRobert Noland g_free(sechdr); 8761c2dee3cSRobert Noland sechdr = NULL; 877dd0db05dSMarcel Moolenaar } else { 8781d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 879dd0db05dSMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 8801c2dee3cSRobert Noland g_free(prihdr); 8811c2dee3cSRobert Noland prihdr = NULL; 882dd0db05dSMarcel Moolenaar } 8831d3aed33SMarcel Moolenaar } 8841d3aed33SMarcel Moolenaar 885dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 8861d3aed33SMarcel Moolenaar printf("GEOM: %s: the primary GPT table is corrupt or " 8871d3aed33SMarcel Moolenaar "invalid.\n", pp->name); 8881d3aed33SMarcel Moolenaar printf("GEOM: %s: using the secondary instead -- recovery " 8891d3aed33SMarcel Moolenaar "strongly advised.\n", pp->name); 8901d3aed33SMarcel Moolenaar table->hdr = sechdr; 891e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 8921c2dee3cSRobert Noland if (prihdr != NULL) 8931c2dee3cSRobert Noland g_free(prihdr); 8941d3aed33SMarcel Moolenaar tbl = sectbl; 8951d3aed33SMarcel Moolenaar if (pritbl != NULL) 8961d3aed33SMarcel Moolenaar g_free(pritbl); 8971d3aed33SMarcel Moolenaar } else { 898dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 8991d3aed33SMarcel Moolenaar printf("GEOM: %s: the secondary GPT table is corrupt " 9001d3aed33SMarcel Moolenaar "or invalid.\n", pp->name); 9011d3aed33SMarcel Moolenaar printf("GEOM: %s: using the primary only -- recovery " 9021d3aed33SMarcel Moolenaar "suggested.\n", pp->name); 903e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 904e7926a37SAndrey V. Elsukov } else if (table->lba[GPT_ELT_SECHDR] != last) { 905e7926a37SAndrey V. Elsukov printf( "GEOM: %s: the secondary GPT header is not in " 906e7926a37SAndrey V. Elsukov "the last LBA.\n", pp->name); 907e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9081d3aed33SMarcel Moolenaar } 9091d3aed33SMarcel Moolenaar table->hdr = prihdr; 9101c2dee3cSRobert Noland if (sechdr != NULL) 9111c2dee3cSRobert Noland g_free(sechdr); 9121d3aed33SMarcel Moolenaar tbl = pritbl; 9131d3aed33SMarcel Moolenaar if (sectbl != NULL) 9141d3aed33SMarcel Moolenaar g_free(sectbl); 9151d3aed33SMarcel Moolenaar } 9161d3aed33SMarcel Moolenaar 9171c2dee3cSRobert Noland basetable->gpt_first = table->hdr->hdr_lba_start; 9181c2dee3cSRobert Noland basetable->gpt_last = table->hdr->hdr_lba_end; 9191c2dee3cSRobert Noland basetable->gpt_entries = table->hdr->hdr_entries; 9201d3aed33SMarcel Moolenaar 9211d3aed33SMarcel Moolenaar for (index = basetable->gpt_entries - 1; index >= 0; index--) { 9221d3aed33SMarcel Moolenaar if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 9231d3aed33SMarcel Moolenaar continue; 924e7926a37SAndrey V. Elsukov entry = (struct g_part_gpt_entry *)g_part_new_entry( 925e7926a37SAndrey V. Elsukov basetable, index + 1, tbl[index].ent_lba_start, 926e7926a37SAndrey V. Elsukov tbl[index].ent_lba_end); 9271d3aed33SMarcel Moolenaar entry->ent = tbl[index]; 9281d3aed33SMarcel Moolenaar } 9291d3aed33SMarcel Moolenaar 9301d3aed33SMarcel Moolenaar g_free(tbl); 931369fe59dSMarcel Moolenaar 932369fe59dSMarcel Moolenaar /* 933369fe59dSMarcel Moolenaar * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 934369fe59dSMarcel Moolenaar * if (and only if) any FAT32 or FAT16 partitions have been 935369fe59dSMarcel Moolenaar * created. This happens irrespective of whether Boot Camp is 936369fe59dSMarcel Moolenaar * used/enabled, though it's generally understood to be done 937369fe59dSMarcel Moolenaar * to support legacy Windows under Boot Camp. We refer to this 938369fe59dSMarcel Moolenaar * mirroring simply as Boot Camp. We try to detect Boot Camp 939369fe59dSMarcel Moolenaar * so that we can update the MBR if and when GPT changes have 940369fe59dSMarcel Moolenaar * been made. Note that we do not enable Boot Camp if not 941369fe59dSMarcel Moolenaar * previously enabled because we can't assume that we're on a 942369fe59dSMarcel Moolenaar * Mac alongside Mac OS X. 943369fe59dSMarcel Moolenaar */ 944369fe59dSMarcel Moolenaar table->bootcamp = gpt_is_bootcamp(table, pp->name); 945369fe59dSMarcel Moolenaar 9461d3aed33SMarcel Moolenaar return (0); 9471d3aed33SMarcel Moolenaar } 9481d3aed33SMarcel Moolenaar 949cec283baSPawel Jakub Dawidek static int 950e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable) 951e7926a37SAndrey V. Elsukov { 952*3bd22a9cSMarcel Moolenaar struct g_part_gpt_table *table; 953*3bd22a9cSMarcel Moolenaar struct g_provider *pp; 954e7926a37SAndrey V. Elsukov 955*3bd22a9cSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 956*3bd22a9cSMarcel Moolenaar pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 957*3bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 958*3bd22a9cSMarcel Moolenaar g_gpt_set_defaults(basetable, pp); 959e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 0; 960e7926a37SAndrey V. Elsukov return (0); 961e7926a37SAndrey V. Elsukov } 962e7926a37SAndrey V. Elsukov 963e7926a37SAndrey V. Elsukov static int 964369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable, 965369fe59dSMarcel Moolenaar struct g_part_entry *baseentry, const char *attrib, unsigned int set) 966cec283baSPawel Jakub Dawidek { 967cec283baSPawel Jakub Dawidek struct g_part_gpt_entry *entry; 968369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 969*3bd22a9cSMarcel Moolenaar uint8_t *p; 970369fe59dSMarcel Moolenaar uint64_t attr; 971369fe59dSMarcel Moolenaar int i; 972cec283baSPawel Jakub Dawidek 973369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 974369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 975369fe59dSMarcel Moolenaar 976369fe59dSMarcel Moolenaar if (strcasecmp(attrib, "active") == 0) { 977*3bd22a9cSMarcel Moolenaar if (table->bootcamp) { 978*3bd22a9cSMarcel Moolenaar /* The active flag must be set on a valid entry. */ 979*3bd22a9cSMarcel Moolenaar if (entry == NULL) 980*3bd22a9cSMarcel Moolenaar return (ENXIO); 981*3bd22a9cSMarcel Moolenaar if (baseentry->gpe_index > NDOSPART) 982369fe59dSMarcel Moolenaar return (EINVAL); 983369fe59dSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 984*3bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 985*3bd22a9cSMarcel Moolenaar p[0] = (i == baseentry->gpe_index - 1) 986*3bd22a9cSMarcel Moolenaar ? ((set) ? 0x80 : 0) : 0; 987*3bd22a9cSMarcel Moolenaar } 988*3bd22a9cSMarcel Moolenaar } else { 989*3bd22a9cSMarcel Moolenaar /* The PMBR is marked as active without an entry. */ 990*3bd22a9cSMarcel Moolenaar if (entry != NULL) 991*3bd22a9cSMarcel Moolenaar return (ENXIO); 992*3bd22a9cSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 993*3bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 994*3bd22a9cSMarcel Moolenaar p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 995*3bd22a9cSMarcel Moolenaar } 996369fe59dSMarcel Moolenaar } 997369fe59dSMarcel Moolenaar return (0); 998369fe59dSMarcel Moolenaar } 999369fe59dSMarcel Moolenaar 1000*3bd22a9cSMarcel Moolenaar if (entry == NULL) 1001*3bd22a9cSMarcel Moolenaar return (ENODEV); 1002*3bd22a9cSMarcel Moolenaar 1003369fe59dSMarcel Moolenaar attr = 0; 1004cec283baSPawel Jakub Dawidek if (strcasecmp(attrib, "bootme") == 0) { 1005369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1006cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootonce") == 0) { 1007369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTONCE; 1008cec283baSPawel Jakub Dawidek if (set) 1009369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1010cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootfailed") == 0) { 1011cec283baSPawel Jakub Dawidek /* 1012cec283baSPawel Jakub Dawidek * It should only be possible to unset BOOTFAILED, but it might 1013cec283baSPawel Jakub Dawidek * be useful for test purposes to also be able to set it. 1014cec283baSPawel Jakub Dawidek */ 1015369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTFAILED; 1016cec283baSPawel Jakub Dawidek } 1017369fe59dSMarcel Moolenaar if (attr == 0) 1018cec283baSPawel Jakub Dawidek return (EINVAL); 1019cec283baSPawel Jakub Dawidek 1020369fe59dSMarcel Moolenaar if (set) 1021369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr | attr; 1022369fe59dSMarcel Moolenaar else 1023369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr & ~attr; 1024369fe59dSMarcel Moolenaar if (attr != entry->ent.ent_attr) { 1025369fe59dSMarcel Moolenaar entry->ent.ent_attr = attr; 1026369fe59dSMarcel Moolenaar if (!baseentry->gpe_created) 1027369fe59dSMarcel Moolenaar baseentry->gpe_modified = 1; 1028cec283baSPawel Jakub Dawidek } 1029cec283baSPawel Jakub Dawidek return (0); 1030cec283baSPawel Jakub Dawidek } 1031cec283baSPawel Jakub Dawidek 10321d3aed33SMarcel Moolenaar static const char * 10331d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 10341d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 10351d3aed33SMarcel Moolenaar { 10361d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 10371d3aed33SMarcel Moolenaar struct uuid *type; 1038f1317430SRui Paulo struct g_part_uuid_alias *uap; 10391d3aed33SMarcel Moolenaar 10401d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 10411d3aed33SMarcel Moolenaar type = &entry->ent.ent_type; 1042f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1043f1317430SRui Paulo if (EQUUID(type, uap->uuid)) 1044f1317430SRui Paulo return (g_part_alias_name(uap->alias)); 1045cf231470SMarcel Moolenaar buf[0] = '!'; 1046cf231470SMarcel Moolenaar snprintf_uuid(buf + 1, bufsz - 1, type); 1047f1317430SRui Paulo 10481d3aed33SMarcel Moolenaar return (buf); 10491d3aed33SMarcel Moolenaar } 10501d3aed33SMarcel Moolenaar 10511d3aed33SMarcel Moolenaar static int 10521d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 10531d3aed33SMarcel Moolenaar { 10541d3aed33SMarcel Moolenaar unsigned char *buf, *bp; 10551d3aed33SMarcel Moolenaar struct g_provider *pp; 10561d3aed33SMarcel Moolenaar struct g_part_entry *baseentry; 10571d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 10581d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 1059e7926a37SAndrey V. Elsukov size_t tblsz; 10601d3aed33SMarcel Moolenaar uint32_t crc; 10611d3aed33SMarcel Moolenaar int error, index; 10621d3aed33SMarcel Moolenaar 10631d3aed33SMarcel Moolenaar pp = cp->provider; 10641d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 1065e7926a37SAndrey V. Elsukov tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz + 10661d3aed33SMarcel Moolenaar pp->sectorsize - 1) / pp->sectorsize; 10671d3aed33SMarcel Moolenaar 1068369fe59dSMarcel Moolenaar /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1069369fe59dSMarcel Moolenaar if (table->bootcamp) 1070*3bd22a9cSMarcel Moolenaar gpt_update_bootcamp(basetable, pp); 1071d4746e10SAndrey V. Elsukov 10724d32fcb4SMarcel Moolenaar /* Write the PMBR */ 10731d3aed33SMarcel Moolenaar buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 10744d32fcb4SMarcel Moolenaar bcopy(table->mbr, buf, MBRSIZE); 10751d3aed33SMarcel Moolenaar error = g_write_data(cp, 0, buf, pp->sectorsize); 10761d3aed33SMarcel Moolenaar g_free(buf); 10771d3aed33SMarcel Moolenaar if (error) 10781d3aed33SMarcel Moolenaar return (error); 10791d3aed33SMarcel Moolenaar 10801d3aed33SMarcel Moolenaar /* Allocate space for the header and entries. */ 1081e7926a37SAndrey V. Elsukov buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 10821d3aed33SMarcel Moolenaar 10831c2dee3cSRobert Noland memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 10841c2dee3cSRobert Noland le32enc(buf + 8, table->hdr->hdr_revision); 10851c2dee3cSRobert Noland le32enc(buf + 12, table->hdr->hdr_size); 10861c2dee3cSRobert Noland le64enc(buf + 40, table->hdr->hdr_lba_start); 10871c2dee3cSRobert Noland le64enc(buf + 48, table->hdr->hdr_lba_end); 10881c2dee3cSRobert Noland le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 10891c2dee3cSRobert Noland le32enc(buf + 80, table->hdr->hdr_entries); 10901c2dee3cSRobert Noland le32enc(buf + 84, table->hdr->hdr_entsz); 10911d3aed33SMarcel Moolenaar 10921d3aed33SMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1093d287f590SMarcel Moolenaar if (baseentry->gpe_deleted) 1094d287f590SMarcel Moolenaar continue; 10951d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 10961d3aed33SMarcel Moolenaar index = baseentry->gpe_index - 1; 10971c2dee3cSRobert Noland bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 10981d3aed33SMarcel Moolenaar le_uuid_enc(bp, &entry->ent.ent_type); 10991d3aed33SMarcel Moolenaar le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 11001d3aed33SMarcel Moolenaar le64enc(bp + 32, entry->ent.ent_lba_start); 11011d3aed33SMarcel Moolenaar le64enc(bp + 40, entry->ent.ent_lba_end); 11021d3aed33SMarcel Moolenaar le64enc(bp + 48, entry->ent.ent_attr); 11031d3aed33SMarcel Moolenaar memcpy(bp + 56, entry->ent.ent_name, 11041d3aed33SMarcel Moolenaar sizeof(entry->ent.ent_name)); 11051d3aed33SMarcel Moolenaar } 11061d3aed33SMarcel Moolenaar 11071d3aed33SMarcel Moolenaar crc = crc32(buf + pp->sectorsize, 11081c2dee3cSRobert Noland table->hdr->hdr_entries * table->hdr->hdr_entsz); 11091d3aed33SMarcel Moolenaar le32enc(buf + 88, crc); 11101d3aed33SMarcel Moolenaar 11111d3aed33SMarcel Moolenaar /* Write primary meta-data. */ 11121d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 11131d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 11141d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 11151d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 11161c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 11171d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 11181d3aed33SMarcel Moolenaar 1119799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1120799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1121799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1122799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1123799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1124799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 11251d3aed33SMarcel Moolenaar if (error) 11261d3aed33SMarcel Moolenaar goto out; 1127799eac8cSAndrey V. Elsukov } 11281d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 11291d3aed33SMarcel Moolenaar buf, pp->sectorsize); 11301d3aed33SMarcel Moolenaar if (error) 11311d3aed33SMarcel Moolenaar goto out; 11321d3aed33SMarcel Moolenaar 11331d3aed33SMarcel Moolenaar /* Write secondary meta-data. */ 11341d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 11351d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 11361d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 11371d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 11381c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 11391d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 11401d3aed33SMarcel Moolenaar 1141799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1142799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1143799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1144799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1145799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1146799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 11471d3aed33SMarcel Moolenaar if (error) 11481d3aed33SMarcel Moolenaar goto out; 1149799eac8cSAndrey V. Elsukov } 11501d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 11511d3aed33SMarcel Moolenaar buf, pp->sectorsize); 11521d3aed33SMarcel Moolenaar 11531d3aed33SMarcel Moolenaar out: 11541d3aed33SMarcel Moolenaar g_free(buf); 11551d3aed33SMarcel Moolenaar return (error); 11561d3aed33SMarcel Moolenaar } 11571d3aed33SMarcel Moolenaar 11581d3aed33SMarcel Moolenaar static void 11592b9be055SAndrey V. Elsukov g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 11602b9be055SAndrey V. Elsukov { 11612b9be055SAndrey V. Elsukov struct g_part_gpt_table *table; 11622b9be055SAndrey V. Elsukov quad_t last; 11632b9be055SAndrey V. Elsukov size_t tblsz; 11642b9be055SAndrey V. Elsukov 11652b9be055SAndrey V. Elsukov table = (struct g_part_gpt_table *)basetable; 11662b9be055SAndrey V. Elsukov last = pp->mediasize / pp->sectorsize - 1; 11672b9be055SAndrey V. Elsukov tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 11682b9be055SAndrey V. Elsukov pp->sectorsize - 1) / pp->sectorsize; 11692b9be055SAndrey V. Elsukov 11702b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRIHDR] = 1; 11712b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRITBL] = 2; 11722b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = last; 11732b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECTBL] = last - tblsz; 11742b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 11752b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 11762b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 11772b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 11782b9be055SAndrey V. Elsukov 11792b9be055SAndrey V. Elsukov table->hdr->hdr_lba_start = 2 + tblsz; 11802b9be055SAndrey V. Elsukov table->hdr->hdr_lba_end = last - tblsz - 1; 11812b9be055SAndrey V. Elsukov 11822b9be055SAndrey V. Elsukov basetable->gpt_first = table->hdr->hdr_lba_start; 11832b9be055SAndrey V. Elsukov basetable->gpt_last = table->hdr->hdr_lba_end; 11842b9be055SAndrey V. Elsukov } 11852b9be055SAndrey V. Elsukov 11862b9be055SAndrey V. Elsukov static void 1187d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 11881d3aed33SMarcel Moolenaar { 11891d3aed33SMarcel Moolenaar u_int bo; 11901d3aed33SMarcel Moolenaar uint32_t ch; 11911d3aed33SMarcel Moolenaar uint16_t c; 11921d3aed33SMarcel Moolenaar 1193d3532631SMarcel Moolenaar bo = LITTLE_ENDIAN; /* GPT is little-endian */ 11941d3aed33SMarcel Moolenaar while (len > 0 && *str != 0) { 11951d3aed33SMarcel Moolenaar ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 11961d3aed33SMarcel Moolenaar str++, len--; 11971d3aed33SMarcel Moolenaar if ((ch & 0xf800) == 0xd800) { 11981d3aed33SMarcel Moolenaar if (len > 0) { 11991d3aed33SMarcel Moolenaar c = (bo == BIG_ENDIAN) ? be16toh(*str) 12001d3aed33SMarcel Moolenaar : le16toh(*str); 12011d3aed33SMarcel Moolenaar str++, len--; 12021d3aed33SMarcel Moolenaar } else 12031d3aed33SMarcel Moolenaar c = 0xfffd; 12041d3aed33SMarcel Moolenaar if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 12051d3aed33SMarcel Moolenaar ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 12061d3aed33SMarcel Moolenaar ch += 0x10000; 12071d3aed33SMarcel Moolenaar } else 12081d3aed33SMarcel Moolenaar ch = 0xfffd; 12091d3aed33SMarcel Moolenaar } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 12101d3aed33SMarcel Moolenaar bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 12111d3aed33SMarcel Moolenaar continue; 12121d3aed33SMarcel Moolenaar } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 12131d3aed33SMarcel Moolenaar continue; 12141d3aed33SMarcel Moolenaar 1215d3532631SMarcel Moolenaar /* Write the Unicode character in UTF-8 */ 12161d3aed33SMarcel Moolenaar if (ch < 0x80) 12171d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c", ch); 12181d3aed33SMarcel Moolenaar else if (ch < 0x800) 12191d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6), 12201d3aed33SMarcel Moolenaar 0x80 | (ch & 0x3f)); 12211d3aed33SMarcel Moolenaar else if (ch < 0x10000) 12221d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12), 12231d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 12241d3aed33SMarcel Moolenaar else if (ch < 0x200000) 12251d3aed33SMarcel Moolenaar sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18), 12261d3aed33SMarcel Moolenaar 0x80 | ((ch >> 12) & 0x3f), 12271d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 12281d3aed33SMarcel Moolenaar } 12291d3aed33SMarcel Moolenaar } 1230d3532631SMarcel Moolenaar 1231d3532631SMarcel Moolenaar static void 1232d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1233d3532631SMarcel Moolenaar { 1234d3532631SMarcel Moolenaar size_t s16idx, s8idx; 1235d3532631SMarcel Moolenaar uint32_t utfchar; 1236d3532631SMarcel Moolenaar unsigned int c, utfbytes; 1237d3532631SMarcel Moolenaar 1238d3532631SMarcel Moolenaar s8idx = s16idx = 0; 1239d3532631SMarcel Moolenaar utfchar = 0; 1240d3532631SMarcel Moolenaar utfbytes = 0; 1241d3532631SMarcel Moolenaar bzero(s16, s16len << 1); 1242d3532631SMarcel Moolenaar while (s8[s8idx] != 0 && s16idx < s16len) { 1243d3532631SMarcel Moolenaar c = s8[s8idx++]; 1244d3532631SMarcel Moolenaar if ((c & 0xc0) != 0x80) { 1245d3532631SMarcel Moolenaar /* Initial characters. */ 1246d3532631SMarcel Moolenaar if (utfbytes != 0) { 1247d3532631SMarcel Moolenaar /* Incomplete encoding of previous char. */ 1248d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1249d3532631SMarcel Moolenaar } 1250d3532631SMarcel Moolenaar if ((c & 0xf8) == 0xf0) { 1251d3532631SMarcel Moolenaar utfchar = c & 0x07; 1252d3532631SMarcel Moolenaar utfbytes = 3; 1253d3532631SMarcel Moolenaar } else if ((c & 0xf0) == 0xe0) { 1254d3532631SMarcel Moolenaar utfchar = c & 0x0f; 1255d3532631SMarcel Moolenaar utfbytes = 2; 1256d3532631SMarcel Moolenaar } else if ((c & 0xe0) == 0xc0) { 1257d3532631SMarcel Moolenaar utfchar = c & 0x1f; 1258d3532631SMarcel Moolenaar utfbytes = 1; 1259d3532631SMarcel Moolenaar } else { 1260d3532631SMarcel Moolenaar utfchar = c & 0x7f; 1261d3532631SMarcel Moolenaar utfbytes = 0; 1262d3532631SMarcel Moolenaar } 1263d3532631SMarcel Moolenaar } else { 1264d3532631SMarcel Moolenaar /* Followup characters. */ 1265d3532631SMarcel Moolenaar if (utfbytes > 0) { 1266d3532631SMarcel Moolenaar utfchar = (utfchar << 6) + (c & 0x3f); 1267d3532631SMarcel Moolenaar utfbytes--; 1268d3532631SMarcel Moolenaar } else if (utfbytes == 0) 1269d3532631SMarcel Moolenaar utfbytes = ~0; 1270d3532631SMarcel Moolenaar } 1271d3532631SMarcel Moolenaar /* 1272d3532631SMarcel Moolenaar * Write the complete Unicode character as UTF-16 when we 1273d3532631SMarcel Moolenaar * have all the UTF-8 charactars collected. 1274d3532631SMarcel Moolenaar */ 1275d3532631SMarcel Moolenaar if (utfbytes == 0) { 1276d3532631SMarcel Moolenaar /* 1277d3532631SMarcel Moolenaar * If we need to write 2 UTF-16 characters, but 1278d3532631SMarcel Moolenaar * we only have room for 1, then we truncate the 1279d3532631SMarcel Moolenaar * string by writing a 0 instead. 1280d3532631SMarcel Moolenaar */ 1281d3532631SMarcel Moolenaar if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1282d3532631SMarcel Moolenaar s16[s16idx++] = 1283d3532631SMarcel Moolenaar htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1284d3532631SMarcel Moolenaar s16[s16idx++] = 1285d3532631SMarcel Moolenaar htole16(0xdc00 | (utfchar & 0x3ff)); 1286d3532631SMarcel Moolenaar } else 1287d3532631SMarcel Moolenaar s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1288d3532631SMarcel Moolenaar htole16(utfchar); 1289d3532631SMarcel Moolenaar } 1290d3532631SMarcel Moolenaar } 1291d3532631SMarcel Moolenaar /* 1292d3532631SMarcel Moolenaar * If our input string was truncated, append an invalid encoding 1293d3532631SMarcel Moolenaar * character to the output string. 1294d3532631SMarcel Moolenaar */ 1295d3532631SMarcel Moolenaar if (utfbytes != 0 && s16idx < s16len) 1296d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1297d3532631SMarcel Moolenaar } 1298