11d3aed33SMarcel Moolenaar /*- 23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33728855aSPedro F. Giffuni * 4369fe59dSMarcel Moolenaar * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar 51d3aed33SMarcel Moolenaar * All rights reserved. 61d3aed33SMarcel Moolenaar * 71d3aed33SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 81d3aed33SMarcel Moolenaar * modification, are permitted provided that the following conditions 91d3aed33SMarcel Moolenaar * are met: 101d3aed33SMarcel Moolenaar * 111d3aed33SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 121d3aed33SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 131d3aed33SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 141d3aed33SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 151d3aed33SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 161d3aed33SMarcel Moolenaar * 171d3aed33SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 181d3aed33SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 191d3aed33SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 201d3aed33SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 211d3aed33SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 221d3aed33SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231d3aed33SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241d3aed33SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251d3aed33SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 261d3aed33SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271d3aed33SMarcel Moolenaar */ 281d3aed33SMarcel Moolenaar 291d3aed33SMarcel Moolenaar #include <sys/cdefs.h> 301d3aed33SMarcel Moolenaar __FBSDID("$FreeBSD$"); 311d3aed33SMarcel Moolenaar 321d3aed33SMarcel Moolenaar #include <sys/param.h> 331d3aed33SMarcel Moolenaar #include <sys/bio.h> 341d3aed33SMarcel Moolenaar #include <sys/diskmbr.h> 351d3aed33SMarcel Moolenaar #include <sys/endian.h> 361d3aed33SMarcel Moolenaar #include <sys/gpt.h> 371d3aed33SMarcel Moolenaar #include <sys/kernel.h> 381d3aed33SMarcel Moolenaar #include <sys/kobj.h> 391d3aed33SMarcel Moolenaar #include <sys/limits.h> 401d3aed33SMarcel Moolenaar #include <sys/lock.h> 411d3aed33SMarcel Moolenaar #include <sys/malloc.h> 421d3aed33SMarcel Moolenaar #include <sys/mutex.h> 431d3aed33SMarcel Moolenaar #include <sys/queue.h> 441d3aed33SMarcel Moolenaar #include <sys/sbuf.h> 451d3aed33SMarcel Moolenaar #include <sys/systm.h> 46cb08c2ccSAlexander Leidinger #include <sys/sysctl.h> 471d3aed33SMarcel Moolenaar #include <sys/uuid.h> 481d3aed33SMarcel Moolenaar #include <geom/geom.h> 4939dcac84SAndrey V. Elsukov #include <geom/geom_int.h> 501d3aed33SMarcel Moolenaar #include <geom/part/g_part.h> 511d3aed33SMarcel Moolenaar 521d3aed33SMarcel Moolenaar #include "g_part_if.h" 531d3aed33SMarcel Moolenaar 54cb08c2ccSAlexander Leidinger FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support"); 55cb08c2ccSAlexander Leidinger 561d3aed33SMarcel Moolenaar CTASSERT(offsetof(struct gpt_hdr, padding) == 92); 571d3aed33SMarcel Moolenaar CTASSERT(sizeof(struct gpt_ent) == 128); 581d3aed33SMarcel Moolenaar 591d3aed33SMarcel Moolenaar #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 601d3aed33SMarcel Moolenaar 614d32fcb4SMarcel Moolenaar #define MBRSIZE 512 624d32fcb4SMarcel Moolenaar 631d3aed33SMarcel Moolenaar enum gpt_elt { 641d3aed33SMarcel Moolenaar GPT_ELT_PRIHDR, 651d3aed33SMarcel Moolenaar GPT_ELT_PRITBL, 661d3aed33SMarcel Moolenaar GPT_ELT_SECHDR, 671d3aed33SMarcel Moolenaar GPT_ELT_SECTBL, 681d3aed33SMarcel Moolenaar GPT_ELT_COUNT 691d3aed33SMarcel Moolenaar }; 701d3aed33SMarcel Moolenaar 711d3aed33SMarcel Moolenaar enum gpt_state { 721d3aed33SMarcel Moolenaar GPT_STATE_UNKNOWN, /* Not determined. */ 731d3aed33SMarcel Moolenaar GPT_STATE_MISSING, /* No signature found. */ 741d3aed33SMarcel Moolenaar GPT_STATE_CORRUPT, /* Checksum mismatch. */ 751d3aed33SMarcel Moolenaar GPT_STATE_INVALID, /* Nonconformant/invalid. */ 761d3aed33SMarcel Moolenaar GPT_STATE_OK /* Perfectly fine. */ 771d3aed33SMarcel Moolenaar }; 781d3aed33SMarcel Moolenaar 791d3aed33SMarcel Moolenaar struct g_part_gpt_table { 801d3aed33SMarcel Moolenaar struct g_part_table base; 814d32fcb4SMarcel Moolenaar u_char mbr[MBRSIZE]; 821c2dee3cSRobert Noland struct gpt_hdr *hdr; 831d3aed33SMarcel Moolenaar quad_t lba[GPT_ELT_COUNT]; 841d3aed33SMarcel Moolenaar enum gpt_state state[GPT_ELT_COUNT]; 85369fe59dSMarcel Moolenaar int bootcamp; 861d3aed33SMarcel Moolenaar }; 871d3aed33SMarcel Moolenaar 881d3aed33SMarcel Moolenaar struct g_part_gpt_entry { 891d3aed33SMarcel Moolenaar struct g_part_entry base; 901d3aed33SMarcel Moolenaar struct gpt_ent ent; 911d3aed33SMarcel Moolenaar }; 921d3aed33SMarcel Moolenaar 93d3532631SMarcel Moolenaar static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t); 94d3532631SMarcel Moolenaar static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t); 952b9be055SAndrey V. Elsukov static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *); 96d3532631SMarcel Moolenaar 971d3aed33SMarcel Moolenaar static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 981d3aed33SMarcel Moolenaar struct g_part_parms *); 994d32fcb4SMarcel Moolenaar static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 1001d3aed33SMarcel Moolenaar static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 1011d3aed33SMarcel Moolenaar static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 10251f53a08SWarner Losh static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 1035db67052SMarcel Moolenaar struct sbuf *, const char *); 1041d3aed33SMarcel Moolenaar static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 1051d3aed33SMarcel Moolenaar static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 1061d3aed33SMarcel Moolenaar struct g_part_parms *); 10751f53a08SWarner Losh static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 1081d3aed33SMarcel Moolenaar char *, size_t); 1091d3aed33SMarcel Moolenaar static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 1101d3aed33SMarcel Moolenaar static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 111cec283baSPawel Jakub Dawidek static int g_part_gpt_setunset(struct g_part_table *table, 112cec283baSPawel Jakub Dawidek struct g_part_entry *baseentry, const char *attrib, unsigned int set); 1131d3aed33SMarcel Moolenaar static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 1141d3aed33SMarcel Moolenaar char *, size_t); 1151d3aed33SMarcel Moolenaar static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 1163f71c319SMarcel Moolenaar static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 1173f71c319SMarcel Moolenaar struct g_part_parms *); 118e7926a37SAndrey V. Elsukov static int g_part_gpt_recover(struct g_part_table *); 1191d3aed33SMarcel Moolenaar 1201d3aed33SMarcel Moolenaar static kobj_method_t g_part_gpt_methods[] = { 1211d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_add, g_part_gpt_add), 1224d32fcb4SMarcel Moolenaar KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 1231d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_create, g_part_gpt_create), 1241d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 1255db67052SMarcel Moolenaar KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 1261d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 1271d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_modify, g_part_gpt_modify), 1283f71c319SMarcel Moolenaar KOBJMETHOD(g_part_resize, g_part_gpt_resize), 1291d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_name, g_part_gpt_name), 1301d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_probe, g_part_gpt_probe), 1311d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_read, g_part_gpt_read), 132e7926a37SAndrey V. Elsukov KOBJMETHOD(g_part_recover, g_part_gpt_recover), 133cec283baSPawel Jakub Dawidek KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 1341d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_type, g_part_gpt_type), 1351d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_write, g_part_gpt_write), 1361d3aed33SMarcel Moolenaar { 0, 0 } 1371d3aed33SMarcel Moolenaar }; 1381d3aed33SMarcel Moolenaar 1391d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_gpt_scheme = { 1401d3aed33SMarcel Moolenaar "GPT", 1411d3aed33SMarcel Moolenaar g_part_gpt_methods, 1421d3aed33SMarcel Moolenaar sizeof(struct g_part_gpt_table), 1431d3aed33SMarcel Moolenaar .gps_entrysz = sizeof(struct g_part_gpt_entry), 1441d3aed33SMarcel Moolenaar .gps_minent = 128, 145799eac8cSAndrey V. Elsukov .gps_maxent = 4096, 1464d32fcb4SMarcel Moolenaar .gps_bootcodesz = MBRSIZE, 1471d3aed33SMarcel Moolenaar }; 1484ffca444SMarcel Moolenaar G_PART_SCHEME_DECLARE(g_part_gpt); 149*74d6c131SKyle Evans MODULE_VERSION(geom_part_gpt, 0); 1501d3aed33SMarcel Moolenaar 151b42712a8SConrad Meyer static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS; 152f1317430SRui Paulo static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 1534fb4ebe0SAndrey V. Elsukov static struct uuid gpt_uuid_apple_core_storage = 1544fb4ebe0SAndrey V. Elsukov GPT_ENT_TYPE_APPLE_CORE_STORAGE; 15587662ab3SRui Paulo static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 156f1317430SRui Paulo static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 157f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 158f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 159f1317430SRui Paulo static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 160f1317430SRui Paulo static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 16188007f61SAndrey V. Elsukov static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 1629c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE; 1639c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; 1649c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED; 1659c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT; 1669c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD; 1679c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER; 1689c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2; 1699c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32; 1709c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64; 1719c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY; 1729c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP; 1739c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1; 1749c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM; 1751d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 1761d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 177f352a0d4SJohn Baldwin static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 178f24a8224SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 1791d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 1801d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 1811d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 182a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 183f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 184f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 185f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 1861d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 1879c0c355fSAllan Jude static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 18833f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 18933f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 19033f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 1919c0c355fSAllan Jude static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY; 1929c0c355fSAllan Jude static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 1939c0c355fSAllan Jude static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES; 19433f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 19533f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 19633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 19733f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 19833f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 19933f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 2009c0c355fSAllan Jude static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA; 2011ee0f089SNathan Whitehorn static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT; 2029c0c355fSAllan Jude static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 2039c0c355fSAllan Jude static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 2049c0c355fSAllan Jude static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 2059c0c355fSAllan Jude static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 2069c0c355fSAllan Jude static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR; 2071d3aed33SMarcel Moolenaar 208f1317430SRui Paulo static struct g_part_uuid_alias { 209f1317430SRui Paulo struct uuid *uuid; 210f1317430SRui Paulo int alias; 211369fe59dSMarcel Moolenaar int mbrtype; 212f1317430SRui Paulo } gpt_uuid_alias_match[] = { 213b42712a8SConrad Meyer { &gpt_uuid_apple_apfs, G_PART_ALIAS_APPLE_APFS, 0 }, 214369fe59dSMarcel Moolenaar { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 2154fb4ebe0SAndrey V. Elsukov { &gpt_uuid_apple_core_storage, G_PART_ALIAS_APPLE_CORE_STORAGE, 0 }, 216369fe59dSMarcel Moolenaar { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 217369fe59dSMarcel Moolenaar { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 218369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 219369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 220369fe59dSMarcel Moolenaar { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 221369fe59dSMarcel Moolenaar { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 222369fe59dSMarcel Moolenaar { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 2239c0c355fSAllan Jude { &gpt_uuid_chromeos_firmware, G_PART_ALIAS_CHROMEOS_FIRMWARE, 0 }, 2249c0c355fSAllan Jude { &gpt_uuid_chromeos_kernel, G_PART_ALIAS_CHROMEOS_KERNEL, 0 }, 2259c0c355fSAllan Jude { &gpt_uuid_chromeos_reserved, G_PART_ALIAS_CHROMEOS_RESERVED, 0 }, 2269c0c355fSAllan Jude { &gpt_uuid_chromeos_root, G_PART_ALIAS_CHROMEOS_ROOT, 0 }, 2279c0c355fSAllan Jude { &gpt_uuid_dfbsd_ccd, G_PART_ALIAS_DFBSD_CCD, 0 }, 2289c0c355fSAllan Jude { &gpt_uuid_dfbsd_hammer, G_PART_ALIAS_DFBSD_HAMMER, 0 }, 2299c0c355fSAllan Jude { &gpt_uuid_dfbsd_hammer2, G_PART_ALIAS_DFBSD_HAMMER2, 0 }, 2309c0c355fSAllan Jude { &gpt_uuid_dfbsd_label32, G_PART_ALIAS_DFBSD, 0xa5 }, 2319c0c355fSAllan Jude { &gpt_uuid_dfbsd_label64, G_PART_ALIAS_DFBSD64, 0xa5 }, 2329c0c355fSAllan Jude { &gpt_uuid_dfbsd_legacy, G_PART_ALIAS_DFBSD_LEGACY, 0 }, 2339c0c355fSAllan Jude { &gpt_uuid_dfbsd_swap, G_PART_ALIAS_DFBSD_SWAP, 0 }, 2349c0c355fSAllan Jude { &gpt_uuid_dfbsd_ufs1, G_PART_ALIAS_DFBSD_UFS, 0 }, 2359c0c355fSAllan Jude { &gpt_uuid_dfbsd_vinum, G_PART_ALIAS_DFBSD_VINUM, 0 }, 236369fe59dSMarcel Moolenaar { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 237369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 238369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 239f24a8224SMarcel Moolenaar { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 }, 240369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 241369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 242369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 243369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 244369fe59dSMarcel Moolenaar { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 245369fe59dSMarcel Moolenaar { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 246369fe59dSMarcel Moolenaar { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 247369fe59dSMarcel Moolenaar { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 248369fe59dSMarcel Moolenaar { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 249369fe59dSMarcel Moolenaar { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 250369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 251369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 2529c0c355fSAllan Jude { &gpt_uuid_ms_recovery, G_PART_ALIAS_MS_RECOVERY, 0 }, 253369fe59dSMarcel Moolenaar { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 2549c0c355fSAllan Jude { &gpt_uuid_ms_spaces, G_PART_ALIAS_MS_SPACES, 0 }, 255369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 256369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 257369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 258369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 259369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 260369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 2619c0c355fSAllan Jude { &gpt_uuid_openbsd_data, G_PART_ALIAS_OPENBSD_DATA, 0 }, 2621ee0f089SNathan Whitehorn { &gpt_uuid_prep_boot, G_PART_ALIAS_PREP_BOOT, 0x41 }, 2639c0c355fSAllan Jude { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 2649c0c355fSAllan Jude { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 2659c0c355fSAllan Jude { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 2669c0c355fSAllan Jude { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 }, 267369fe59dSMarcel Moolenaar { NULL, 0, 0 } 268f1317430SRui Paulo }; 269f1317430SRui Paulo 270369fe59dSMarcel Moolenaar static int 271369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 272369fe59dSMarcel Moolenaar quad_t end) 273369fe59dSMarcel Moolenaar { 274369fe59dSMarcel Moolenaar 275369fe59dSMarcel Moolenaar if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 276369fe59dSMarcel Moolenaar return (EINVAL); 277369fe59dSMarcel Moolenaar 278369fe59dSMarcel Moolenaar mbr += DOSPARTOFF + idx * DOSPARTSIZE; 279369fe59dSMarcel Moolenaar mbr[0] = 0; 280369fe59dSMarcel Moolenaar if (start == 1) { 281369fe59dSMarcel Moolenaar /* 282369fe59dSMarcel Moolenaar * Treat the PMBR partition specially to maximize 283369fe59dSMarcel Moolenaar * interoperability with BIOSes. 284369fe59dSMarcel Moolenaar */ 285369fe59dSMarcel Moolenaar mbr[1] = mbr[3] = 0; 286369fe59dSMarcel Moolenaar mbr[2] = 2; 287369fe59dSMarcel Moolenaar } else 288369fe59dSMarcel Moolenaar mbr[1] = mbr[2] = mbr[3] = 0xff; 289369fe59dSMarcel Moolenaar mbr[4] = typ; 290369fe59dSMarcel Moolenaar mbr[5] = mbr[6] = mbr[7] = 0xff; 291369fe59dSMarcel Moolenaar le32enc(mbr + 8, (uint32_t)start); 292369fe59dSMarcel Moolenaar le32enc(mbr + 12, (uint32_t)(end - start + 1)); 293369fe59dSMarcel Moolenaar return (0); 294369fe59dSMarcel Moolenaar } 295369fe59dSMarcel Moolenaar 296369fe59dSMarcel Moolenaar static int 297369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t) 298369fe59dSMarcel Moolenaar { 299369fe59dSMarcel Moolenaar struct g_part_uuid_alias *uap; 300369fe59dSMarcel Moolenaar 301369fe59dSMarcel Moolenaar for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 302369fe59dSMarcel Moolenaar if (EQUUID(t, uap->uuid)) 303369fe59dSMarcel Moolenaar return (uap->mbrtype); 304369fe59dSMarcel Moolenaar } 305369fe59dSMarcel Moolenaar return (0); 306369fe59dSMarcel Moolenaar } 307369fe59dSMarcel Moolenaar 3083bd22a9cSMarcel Moolenaar static void 3093bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 3103bd22a9cSMarcel Moolenaar { 3113bd22a9cSMarcel Moolenaar 3123bd22a9cSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 3133bd22a9cSMarcel Moolenaar gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 3143bd22a9cSMarcel Moolenaar MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 3153bd22a9cSMarcel Moolenaar le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 3163bd22a9cSMarcel Moolenaar } 3173bd22a9cSMarcel Moolenaar 318369fe59dSMarcel Moolenaar /* 319369fe59dSMarcel Moolenaar * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 320369fe59dSMarcel Moolenaar * whole disk anymore. Rather, it covers the GPT table and the EFI 321369fe59dSMarcel Moolenaar * system partition only. This way the HFS+ partition and any FAT 322369fe59dSMarcel Moolenaar * partitions can be added to the MBR without creating an overlap. 323369fe59dSMarcel Moolenaar */ 324369fe59dSMarcel Moolenaar static int 325369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 326369fe59dSMarcel Moolenaar { 327369fe59dSMarcel Moolenaar uint8_t *p; 328369fe59dSMarcel Moolenaar 329369fe59dSMarcel Moolenaar p = table->mbr + DOSPARTOFF; 330369fe59dSMarcel Moolenaar if (p[4] != 0xee || le32dec(p + 8) != 1) 331369fe59dSMarcel Moolenaar return (0); 332369fe59dSMarcel Moolenaar 333369fe59dSMarcel Moolenaar p += DOSPARTSIZE; 334369fe59dSMarcel Moolenaar if (p[4] != 0xaf) 335369fe59dSMarcel Moolenaar return (0); 336369fe59dSMarcel Moolenaar 337369fe59dSMarcel Moolenaar printf("GEOM: %s: enabling Boot Camp\n", provname); 338369fe59dSMarcel Moolenaar return (1); 339369fe59dSMarcel Moolenaar } 340369fe59dSMarcel Moolenaar 341369fe59dSMarcel Moolenaar static void 3423bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 343369fe59dSMarcel Moolenaar { 344369fe59dSMarcel Moolenaar struct g_part_entry *baseentry; 345369fe59dSMarcel Moolenaar struct g_part_gpt_entry *entry; 346369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 347369fe59dSMarcel Moolenaar int bootable, error, index, slices, typ; 348369fe59dSMarcel Moolenaar 349369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 350369fe59dSMarcel Moolenaar 351369fe59dSMarcel Moolenaar bootable = -1; 352369fe59dSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 353369fe59dSMarcel Moolenaar if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 354369fe59dSMarcel Moolenaar bootable = index; 355369fe59dSMarcel Moolenaar } 356369fe59dSMarcel Moolenaar 357369fe59dSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 358369fe59dSMarcel Moolenaar slices = 0; 359369fe59dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 360369fe59dSMarcel Moolenaar if (baseentry->gpe_deleted) 361369fe59dSMarcel Moolenaar continue; 362369fe59dSMarcel Moolenaar index = baseentry->gpe_index - 1; 363369fe59dSMarcel Moolenaar if (index >= NDOSPART) 364369fe59dSMarcel Moolenaar continue; 365369fe59dSMarcel Moolenaar 366369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 367369fe59dSMarcel Moolenaar 368369fe59dSMarcel Moolenaar switch (index) { 369369fe59dSMarcel Moolenaar case 0: /* This must be the EFI system partition. */ 370369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 371369fe59dSMarcel Moolenaar goto disable; 372369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xee, 373369fe59dSMarcel Moolenaar 1ull, entry->ent.ent_lba_end); 374369fe59dSMarcel Moolenaar break; 375369fe59dSMarcel Moolenaar case 1: /* This must be the HFS+ partition. */ 376369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 377369fe59dSMarcel Moolenaar goto disable; 378369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 379369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 380369fe59dSMarcel Moolenaar break; 381369fe59dSMarcel Moolenaar default: 382369fe59dSMarcel Moolenaar typ = gpt_map_type(&entry->ent.ent_type); 383369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, typ, 384369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 385369fe59dSMarcel Moolenaar break; 386369fe59dSMarcel Moolenaar } 387369fe59dSMarcel Moolenaar if (error) 388369fe59dSMarcel Moolenaar continue; 389369fe59dSMarcel Moolenaar 390369fe59dSMarcel Moolenaar if (index == bootable) 391369fe59dSMarcel Moolenaar table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 392369fe59dSMarcel Moolenaar slices |= 1 << index; 393369fe59dSMarcel Moolenaar } 394369fe59dSMarcel Moolenaar if ((slices & 3) == 3) 395369fe59dSMarcel Moolenaar return; 396369fe59dSMarcel Moolenaar 397369fe59dSMarcel Moolenaar disable: 398369fe59dSMarcel Moolenaar table->bootcamp = 0; 3993bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 400369fe59dSMarcel Moolenaar } 401369fe59dSMarcel Moolenaar 4021c2dee3cSRobert Noland static struct gpt_hdr * 4031d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 4041c2dee3cSRobert Noland enum gpt_elt elt) 4051d3aed33SMarcel Moolenaar { 4061c2dee3cSRobert Noland struct gpt_hdr *buf, *hdr; 4071d3aed33SMarcel Moolenaar struct g_provider *pp; 4081d3aed33SMarcel Moolenaar quad_t lba, last; 4091d3aed33SMarcel Moolenaar int error; 4101d3aed33SMarcel Moolenaar uint32_t crc, sz; 4111d3aed33SMarcel Moolenaar 4121d3aed33SMarcel Moolenaar pp = cp->provider; 4131d3aed33SMarcel Moolenaar last = (pp->mediasize / pp->sectorsize) - 1; 4141d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 415e7926a37SAndrey V. Elsukov /* 416e7926a37SAndrey V. Elsukov * If the primary header is valid look for secondary 417e7926a37SAndrey V. Elsukov * header in AlternateLBA, otherwise in the last medium's LBA. 418e7926a37SAndrey V. Elsukov */ 419e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_SECHDR) { 420e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 421e7926a37SAndrey V. Elsukov table->lba[elt] = last; 422e7926a37SAndrey V. Elsukov } else 423e7926a37SAndrey V. Elsukov table->lba[elt] = 1; 4241d3aed33SMarcel Moolenaar buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 4251d3aed33SMarcel Moolenaar &error); 4261d3aed33SMarcel Moolenaar if (buf == NULL) 4271c2dee3cSRobert Noland return (NULL); 4281c2dee3cSRobert Noland hdr = NULL; 4291c2dee3cSRobert Noland if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 4301c2dee3cSRobert Noland goto fail; 4311d3aed33SMarcel Moolenaar 4321d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 4331c2dee3cSRobert Noland sz = le32toh(buf->hdr_size); 4341d3aed33SMarcel Moolenaar if (sz < 92 || sz > pp->sectorsize) 4351c2dee3cSRobert Noland goto fail; 4361c2dee3cSRobert Noland 4371c2dee3cSRobert Noland hdr = g_malloc(sz, M_WAITOK | M_ZERO); 4381c2dee3cSRobert Noland bcopy(buf, hdr, sz); 4391d3aed33SMarcel Moolenaar hdr->hdr_size = sz; 4401c2dee3cSRobert Noland 4411c2dee3cSRobert Noland crc = le32toh(buf->hdr_crc_self); 4421c2dee3cSRobert Noland buf->hdr_crc_self = 0; 4431c2dee3cSRobert Noland if (crc32(buf, sz) != crc) 4441c2dee3cSRobert Noland goto fail; 4451d3aed33SMarcel Moolenaar hdr->hdr_crc_self = crc; 4461d3aed33SMarcel Moolenaar 4471d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_INVALID; 4481c2dee3cSRobert Noland hdr->hdr_revision = le32toh(buf->hdr_revision); 449e7926a37SAndrey V. Elsukov if (hdr->hdr_revision < GPT_HDR_REVISION) 4501c2dee3cSRobert Noland goto fail; 4511c2dee3cSRobert Noland hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 4521d3aed33SMarcel Moolenaar if (hdr->hdr_lba_self != table->lba[elt]) 4531c2dee3cSRobert Noland goto fail; 4541c2dee3cSRobert Noland hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 455e7926a37SAndrey V. Elsukov if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 456e7926a37SAndrey V. Elsukov hdr->hdr_lba_alt > last) 457e7926a37SAndrey V. Elsukov goto fail; 4581d3aed33SMarcel Moolenaar 4591d3aed33SMarcel Moolenaar /* Check the managed area. */ 4601c2dee3cSRobert Noland hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 4611d3aed33SMarcel Moolenaar if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 4621c2dee3cSRobert Noland goto fail; 4631c2dee3cSRobert Noland hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 4641d3aed33SMarcel Moolenaar if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 4651c2dee3cSRobert Noland goto fail; 4661d3aed33SMarcel Moolenaar 4671d3aed33SMarcel Moolenaar /* Check the table location and size of the table. */ 4681c2dee3cSRobert Noland hdr->hdr_entries = le32toh(buf->hdr_entries); 4691c2dee3cSRobert Noland hdr->hdr_entsz = le32toh(buf->hdr_entsz); 4701d3aed33SMarcel Moolenaar if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 4711d3aed33SMarcel Moolenaar (hdr->hdr_entsz & 7) != 0) 4721c2dee3cSRobert Noland goto fail; 4731c2dee3cSRobert Noland hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 4741d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 4751c2dee3cSRobert Noland goto fail; 4761d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 4771d3aed33SMarcel Moolenaar hdr->hdr_lba_table <= hdr->hdr_lba_end) 4781c2dee3cSRobert Noland goto fail; 4791d3aed33SMarcel Moolenaar lba = hdr->hdr_lba_table + 48055e0987aSPedro F. Giffuni howmany(hdr->hdr_entries * hdr->hdr_entsz, pp->sectorsize) - 1; 4811d3aed33SMarcel Moolenaar if (lba >= last) 4821c2dee3cSRobert Noland goto fail; 4831d3aed33SMarcel Moolenaar if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 4841c2dee3cSRobert Noland goto fail; 4851d3aed33SMarcel Moolenaar 4861d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4871c2dee3cSRobert Noland le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 4881c2dee3cSRobert Noland hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 4891c2dee3cSRobert Noland 490e7926a37SAndrey V. Elsukov /* save LBA for secondary header */ 491e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_PRIHDR) 492e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 493e7926a37SAndrey V. Elsukov 4941c2dee3cSRobert Noland g_free(buf); 4951c2dee3cSRobert Noland return (hdr); 4961c2dee3cSRobert Noland 4971c2dee3cSRobert Noland fail: 4981c2dee3cSRobert Noland if (hdr != NULL) 4991c2dee3cSRobert Noland g_free(hdr); 5001c2dee3cSRobert Noland g_free(buf); 5011c2dee3cSRobert Noland return (NULL); 5021d3aed33SMarcel Moolenaar } 5031d3aed33SMarcel Moolenaar 5041d3aed33SMarcel Moolenaar static struct gpt_ent * 5051d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 5061d3aed33SMarcel Moolenaar enum gpt_elt elt, struct gpt_hdr *hdr) 5071d3aed33SMarcel Moolenaar { 5081d3aed33SMarcel Moolenaar struct g_provider *pp; 5091d3aed33SMarcel Moolenaar struct gpt_ent *ent, *tbl; 5101d3aed33SMarcel Moolenaar char *buf, *p; 511799eac8cSAndrey V. Elsukov unsigned int idx, sectors, tblsz, size; 5121d3aed33SMarcel Moolenaar int error; 5131d3aed33SMarcel Moolenaar 5141c2dee3cSRobert Noland if (hdr == NULL) 5151c2dee3cSRobert Noland return (NULL); 5161c2dee3cSRobert Noland 5171d3aed33SMarcel Moolenaar pp = cp->provider; 5181d3aed33SMarcel Moolenaar table->lba[elt] = hdr->hdr_lba_table; 5191d3aed33SMarcel Moolenaar 5201d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 5211d3aed33SMarcel Moolenaar tblsz = hdr->hdr_entries * hdr->hdr_entsz; 52255e0987aSPedro F. Giffuni sectors = howmany(tblsz, pp->sectorsize); 523799eac8cSAndrey V. Elsukov buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 524799eac8cSAndrey V. Elsukov for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 525799eac8cSAndrey V. Elsukov size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 526799eac8cSAndrey V. Elsukov (sectors - idx) * pp->sectorsize; 527799eac8cSAndrey V. Elsukov p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 528799eac8cSAndrey V. Elsukov size, &error); 529799eac8cSAndrey V. Elsukov if (p == NULL) { 530799eac8cSAndrey V. Elsukov g_free(buf); 5311d3aed33SMarcel Moolenaar return (NULL); 532799eac8cSAndrey V. Elsukov } 533799eac8cSAndrey V. Elsukov bcopy(p, buf + idx * pp->sectorsize, size); 534799eac8cSAndrey V. Elsukov g_free(p); 535799eac8cSAndrey V. Elsukov } 5361d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 5371d3aed33SMarcel Moolenaar if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 5381d3aed33SMarcel Moolenaar g_free(buf); 5391d3aed33SMarcel Moolenaar return (NULL); 5401d3aed33SMarcel Moolenaar } 5411d3aed33SMarcel Moolenaar 5421d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 5431d3aed33SMarcel Moolenaar tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 5441d3aed33SMarcel Moolenaar M_WAITOK | M_ZERO); 5451d3aed33SMarcel Moolenaar 5461d3aed33SMarcel Moolenaar for (idx = 0, ent = tbl, p = buf; 5471d3aed33SMarcel Moolenaar idx < hdr->hdr_entries; 5481d3aed33SMarcel Moolenaar idx++, ent++, p += hdr->hdr_entsz) { 5491d3aed33SMarcel Moolenaar le_uuid_dec(p, &ent->ent_type); 5501d3aed33SMarcel Moolenaar le_uuid_dec(p + 16, &ent->ent_uuid); 5511d3aed33SMarcel Moolenaar ent->ent_lba_start = le64dec(p + 32); 5521d3aed33SMarcel Moolenaar ent->ent_lba_end = le64dec(p + 40); 5531d3aed33SMarcel Moolenaar ent->ent_attr = le64dec(p + 48); 554d3532631SMarcel Moolenaar /* Keep UTF-16 in little-endian. */ 555d3532631SMarcel Moolenaar bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 5561d3aed33SMarcel Moolenaar } 5571d3aed33SMarcel Moolenaar 5581d3aed33SMarcel Moolenaar g_free(buf); 5591d3aed33SMarcel Moolenaar return (tbl); 5601d3aed33SMarcel Moolenaar } 5611d3aed33SMarcel Moolenaar 5621d3aed33SMarcel Moolenaar static int 5631d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 5641d3aed33SMarcel Moolenaar { 5651d3aed33SMarcel Moolenaar 5661c2dee3cSRobert Noland if (pri == NULL || sec == NULL) 5671c2dee3cSRobert Noland return (0); 5681c2dee3cSRobert Noland 5691d3aed33SMarcel Moolenaar if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 5701d3aed33SMarcel Moolenaar return (0); 5711d3aed33SMarcel Moolenaar return ((pri->hdr_revision == sec->hdr_revision && 5721d3aed33SMarcel Moolenaar pri->hdr_size == sec->hdr_size && 5731d3aed33SMarcel Moolenaar pri->hdr_lba_start == sec->hdr_lba_start && 5741d3aed33SMarcel Moolenaar pri->hdr_lba_end == sec->hdr_lba_end && 5751d3aed33SMarcel Moolenaar pri->hdr_entries == sec->hdr_entries && 5761d3aed33SMarcel Moolenaar pri->hdr_entsz == sec->hdr_entsz && 5771d3aed33SMarcel Moolenaar pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 5781d3aed33SMarcel Moolenaar } 5791d3aed33SMarcel Moolenaar 5801d3aed33SMarcel Moolenaar static int 5811d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid) 5821d3aed33SMarcel Moolenaar { 5831d3aed33SMarcel Moolenaar struct uuid tmp; 584d287f590SMarcel Moolenaar const char *alias; 5851d3aed33SMarcel Moolenaar int error; 586f1317430SRui Paulo struct g_part_uuid_alias *uap; 5871d3aed33SMarcel Moolenaar 588d287f590SMarcel Moolenaar if (type[0] == '!') { 589d287f590SMarcel Moolenaar error = parse_uuid(type + 1, &tmp); 5901d3aed33SMarcel Moolenaar if (error) 5911d3aed33SMarcel Moolenaar return (error); 5921d3aed33SMarcel Moolenaar if (EQUUID(&tmp, &gpt_uuid_unused)) 5931d3aed33SMarcel Moolenaar return (EINVAL); 5941d3aed33SMarcel Moolenaar *uuid = tmp; 5951d3aed33SMarcel Moolenaar return (0); 5961d3aed33SMarcel Moolenaar } 597f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 598f1317430SRui Paulo alias = g_part_alias_name(uap->alias); 599d287f590SMarcel Moolenaar if (!strcasecmp(type, alias)) { 600665bb830SMarcel Moolenaar *uuid = *uap->uuid; 6011d3aed33SMarcel Moolenaar return (0); 6021d3aed33SMarcel Moolenaar } 603d7255ff4SRui Paulo } 604d287f590SMarcel Moolenaar return (EINVAL); 605d287f590SMarcel Moolenaar } 6061d3aed33SMarcel Moolenaar 6071d3aed33SMarcel Moolenaar static int 6081d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 6091d3aed33SMarcel Moolenaar struct g_part_parms *gpp) 6101d3aed33SMarcel Moolenaar { 6111d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 6121d3aed33SMarcel Moolenaar int error; 6131d3aed33SMarcel Moolenaar 6141d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 6151d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 6161d3aed33SMarcel Moolenaar if (error) 6171d3aed33SMarcel Moolenaar return (error); 6181d3aed33SMarcel Moolenaar kern_uuidgen(&entry->ent.ent_uuid, 1); 6191d3aed33SMarcel Moolenaar entry->ent.ent_lba_start = baseentry->gpe_start; 6201d3aed33SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 6211d3aed33SMarcel Moolenaar if (baseentry->gpe_deleted) { 6221d3aed33SMarcel Moolenaar entry->ent.ent_attr = 0; 6231d3aed33SMarcel Moolenaar bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 6241d3aed33SMarcel Moolenaar } 625d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 626d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 627ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 628ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 6291d3aed33SMarcel Moolenaar return (0); 6301d3aed33SMarcel Moolenaar } 6311d3aed33SMarcel Moolenaar 6321d3aed33SMarcel Moolenaar static int 6334d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 6344d32fcb4SMarcel Moolenaar { 6354d32fcb4SMarcel Moolenaar struct g_part_gpt_table *table; 63666477112SMarcel Moolenaar size_t codesz; 6374d32fcb4SMarcel Moolenaar 63866477112SMarcel Moolenaar codesz = DOSPARTOFF; 6394d32fcb4SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 64066477112SMarcel Moolenaar bzero(table->mbr, codesz); 64166477112SMarcel Moolenaar codesz = MIN(codesz, gpp->gpp_codesize); 64266477112SMarcel Moolenaar if (codesz > 0) 64366477112SMarcel Moolenaar bcopy(gpp->gpp_codeptr, table->mbr, codesz); 6444d32fcb4SMarcel Moolenaar return (0); 6454d32fcb4SMarcel Moolenaar } 6464d32fcb4SMarcel Moolenaar 6474d32fcb4SMarcel Moolenaar static int 6481d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 6491d3aed33SMarcel Moolenaar { 6501d3aed33SMarcel Moolenaar struct g_provider *pp; 6511d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 6521d3aed33SMarcel Moolenaar size_t tblsz; 6531d3aed33SMarcel Moolenaar 6542a1c00ffSMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 6552a1c00ffSMarcel Moolenaar if (basetable->gpt_depth != 0) 6562a1c00ffSMarcel Moolenaar return (ENXIO); 6572a1c00ffSMarcel Moolenaar 6581d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 6591d3aed33SMarcel Moolenaar pp = gpp->gpp_provider; 66055e0987aSPedro F. Giffuni tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 66155e0987aSPedro F. Giffuni pp->sectorsize); 6624d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || 6631d3aed33SMarcel Moolenaar pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 6641d3aed33SMarcel Moolenaar pp->sectorsize) 6651d3aed33SMarcel Moolenaar return (ENOSPC); 6661d3aed33SMarcel Moolenaar 6673bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 6683bd22a9cSMarcel Moolenaar 669a59a1310SRobert Noland /* Allocate space for the header */ 670a59a1310SRobert Noland table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 671a59a1310SRobert Noland 6721c2dee3cSRobert Noland bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 6731c2dee3cSRobert Noland table->hdr->hdr_revision = GPT_HDR_REVISION; 6741c2dee3cSRobert Noland table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 6751c2dee3cSRobert Noland kern_uuidgen(&table->hdr->hdr_uuid, 1); 6761c2dee3cSRobert Noland table->hdr->hdr_entries = basetable->gpt_entries; 6771c2dee3cSRobert Noland table->hdr->hdr_entsz = sizeof(struct gpt_ent); 6781d3aed33SMarcel Moolenaar 6792b9be055SAndrey V. Elsukov g_gpt_set_defaults(basetable, pp); 6801d3aed33SMarcel Moolenaar return (0); 6811d3aed33SMarcel Moolenaar } 6821d3aed33SMarcel Moolenaar 6831d3aed33SMarcel Moolenaar static int 6841d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 6851d3aed33SMarcel Moolenaar { 68636066952SMarius Strobl struct g_part_gpt_table *table; 687e7926a37SAndrey V. Elsukov struct g_provider *pp; 68836066952SMarius Strobl 68936066952SMarius Strobl table = (struct g_part_gpt_table *)basetable; 690e7926a37SAndrey V. Elsukov pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 69136066952SMarius Strobl g_free(table->hdr); 69236066952SMarius Strobl table->hdr = NULL; 6931d3aed33SMarcel Moolenaar 6941d3aed33SMarcel Moolenaar /* 6959ef60043SAndrey V. Elsukov * Wipe the first 2 sectors and last one to clear the partitioning. 6969ef60043SAndrey V. Elsukov * Wipe sectors only if they have valid metadata. 6971d3aed33SMarcel Moolenaar */ 6989ef60043SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) 6991d3aed33SMarcel Moolenaar basetable->gpt_smhead |= 3; 700e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 701e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 7021d3aed33SMarcel Moolenaar basetable->gpt_smtail |= 1; 7031d3aed33SMarcel Moolenaar return (0); 7041d3aed33SMarcel Moolenaar } 7051d3aed33SMarcel Moolenaar 70651f53a08SWarner Losh static void 7075db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 7085db67052SMarcel Moolenaar struct sbuf *sb, const char *indent) 7095db67052SMarcel Moolenaar { 7105db67052SMarcel Moolenaar struct g_part_gpt_entry *entry; 7115db67052SMarcel Moolenaar 7125db67052SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7130c132595SMarcel Moolenaar if (indent == NULL) { 7140c132595SMarcel Moolenaar /* conftxt: libdisk compatibility */ 7155db67052SMarcel Moolenaar sbuf_printf(sb, " xs GPT xt "); 7165db67052SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 7170c132595SMarcel Moolenaar } else if (entry != NULL) { 7180c132595SMarcel Moolenaar /* confxml: partition entry information */ 719d3532631SMarcel Moolenaar sbuf_printf(sb, "%s<label>", indent); 720d3532631SMarcel Moolenaar g_gpt_printf_utf16(sb, entry->ent.ent_name, 721d3532631SMarcel Moolenaar sizeof(entry->ent.ent_name) >> 1); 722d3532631SMarcel Moolenaar sbuf_printf(sb, "</label>\n"); 723cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 724cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 725cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 726cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 727cec283baSPawel Jakub Dawidek indent); 728cec283baSPawel Jakub Dawidek } 729cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 730cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 731cec283baSPawel Jakub Dawidek indent); 732cec283baSPawel Jakub Dawidek } 7330c132595SMarcel Moolenaar sbuf_printf(sb, "%s<rawtype>", indent); 7340c132595SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 7350c132595SMarcel Moolenaar sbuf_printf(sb, "</rawtype>\n"); 736cd18ad83SMarcel Moolenaar sbuf_printf(sb, "%s<rawuuid>", indent); 737cd18ad83SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 738cd18ad83SMarcel Moolenaar sbuf_printf(sb, "</rawuuid>\n"); 739ab4effdcSWarner Losh sbuf_printf(sb, "%s<efimedia>", indent); 740ab4effdcSWarner Losh sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index); 741ab4effdcSWarner Losh sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 742ab4effdcSWarner Losh sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start, 743a905e396SWarner Losh (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1)); 744ab4effdcSWarner Losh sbuf_printf(sb, "</efimedia>\n"); 7450c132595SMarcel Moolenaar } else { 7460c132595SMarcel Moolenaar /* confxml: scheme information */ 7470c132595SMarcel Moolenaar } 7485db67052SMarcel Moolenaar } 7495db67052SMarcel Moolenaar 7505db67052SMarcel Moolenaar static int 7511d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 7521d3aed33SMarcel Moolenaar { 7531d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7541d3aed33SMarcel Moolenaar 7551d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7561d3aed33SMarcel Moolenaar return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 7574042ab48SAndrey V. Elsukov EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) || 7584042ab48SAndrey V. Elsukov EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0); 7591d3aed33SMarcel Moolenaar } 7601d3aed33SMarcel Moolenaar 7611d3aed33SMarcel Moolenaar static int 7621d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable, 7631d3aed33SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7641d3aed33SMarcel Moolenaar { 7651d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7661d3aed33SMarcel Moolenaar int error; 7671d3aed33SMarcel Moolenaar 7681d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 769d287f590SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_TYPE) { 7701d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 7711d3aed33SMarcel Moolenaar if (error) 7721d3aed33SMarcel Moolenaar return (error); 773d287f590SMarcel Moolenaar } 774d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 775d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 776ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 777ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 7781d3aed33SMarcel Moolenaar return (0); 7791d3aed33SMarcel Moolenaar } 7801d3aed33SMarcel Moolenaar 7813f71c319SMarcel Moolenaar static int 7823f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable, 7833f71c319SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7843f71c319SMarcel Moolenaar { 7853f71c319SMarcel Moolenaar struct g_part_gpt_entry *entry; 7863f71c319SMarcel Moolenaar 787884c8e4fSAndrey V. Elsukov if (baseentry == NULL) 788e7d0c7e4SAndrey V. Elsukov return (g_part_gpt_recover(basetable)); 789884c8e4fSAndrey V. Elsukov 790884c8e4fSAndrey V. Elsukov entry = (struct g_part_gpt_entry *)baseentry; 7913f71c319SMarcel Moolenaar baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 7923f71c319SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 7933f71c319SMarcel Moolenaar 7943f71c319SMarcel Moolenaar return (0); 7953f71c319SMarcel Moolenaar } 7963f71c319SMarcel Moolenaar 79751f53a08SWarner Losh static const char * 7981d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 7991d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 8001d3aed33SMarcel Moolenaar { 8011d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8021d3aed33SMarcel Moolenaar char c; 8031d3aed33SMarcel Moolenaar 8041d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 8051d3aed33SMarcel Moolenaar c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 8061d3aed33SMarcel Moolenaar snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 8071d3aed33SMarcel Moolenaar return (buf); 8081d3aed33SMarcel Moolenaar } 8091d3aed33SMarcel Moolenaar 8101d3aed33SMarcel Moolenaar static int 8111d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 8121d3aed33SMarcel Moolenaar { 8131d3aed33SMarcel Moolenaar struct g_provider *pp; 8143e5a0a6bSMarcel Moolenaar u_char *buf; 8153e5a0a6bSMarcel Moolenaar int error, index, pri, res; 8161d3aed33SMarcel Moolenaar 8171d3aed33SMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 8181d3aed33SMarcel Moolenaar if (table->gpt_depth != 0) 8191d3aed33SMarcel Moolenaar return (ENXIO); 8201d3aed33SMarcel Moolenaar 8211d3aed33SMarcel Moolenaar pp = cp->provider; 8221d3aed33SMarcel Moolenaar 8231d3aed33SMarcel Moolenaar /* 8241d3aed33SMarcel Moolenaar * Sanity-check the provider. Since the first sector on the provider 8251d3aed33SMarcel Moolenaar * must be a PMBR and a PMBR is 512 bytes large, the sector size 8261d3aed33SMarcel Moolenaar * must be at least 512 bytes. Also, since the theoretical minimum 8271d3aed33SMarcel Moolenaar * number of sectors needed by GPT is 6, any medium that has less 8281d3aed33SMarcel Moolenaar * than 6 sectors is never going to be able to hold a GPT. The 8291d3aed33SMarcel Moolenaar * number 6 comes from: 8301d3aed33SMarcel Moolenaar * 1 sector for the PMBR 8311d3aed33SMarcel Moolenaar * 2 sectors for the GPT headers (each 1 sector) 8321d3aed33SMarcel Moolenaar * 2 sectors for the GPT tables (each 1 sector) 8331d3aed33SMarcel Moolenaar * 1 sector for an actual partition 8341d3aed33SMarcel Moolenaar * It's better to catch this pathological case early than behaving 8351d3aed33SMarcel Moolenaar * pathologically later on... 8361d3aed33SMarcel Moolenaar */ 8374d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 8381d3aed33SMarcel Moolenaar return (ENOSPC); 8391d3aed33SMarcel Moolenaar 8403e5a0a6bSMarcel Moolenaar /* 8413e5a0a6bSMarcel Moolenaar * Check that there's a MBR or a PMBR. If it's a PMBR, we return 8423e5a0a6bSMarcel Moolenaar * as the highest priority on a match, otherwise we assume some 8433e5a0a6bSMarcel Moolenaar * GPT-unaware tool has destroyed the GPT by recreating a MBR and 8443e5a0a6bSMarcel Moolenaar * we really want the MBR scheme to take precedence. 8453e5a0a6bSMarcel Moolenaar */ 8461d3aed33SMarcel Moolenaar buf = g_read_data(cp, 0L, pp->sectorsize, &error); 8471d3aed33SMarcel Moolenaar if (buf == NULL) 8481d3aed33SMarcel Moolenaar return (error); 8491d3aed33SMarcel Moolenaar res = le16dec(buf + DOSMAGICOFFSET); 8503e5a0a6bSMarcel Moolenaar pri = G_PART_PROBE_PRI_LOW; 851af90a872SAndrey V. Elsukov if (res == DOSMAGIC) { 8523e5a0a6bSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 8533e5a0a6bSMarcel Moolenaar if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee) 8543e5a0a6bSMarcel Moolenaar pri = G_PART_PROBE_PRI_HIGH; 8553e5a0a6bSMarcel Moolenaar } 8561d3aed33SMarcel Moolenaar g_free(buf); 8571d3aed33SMarcel Moolenaar 8581d3aed33SMarcel Moolenaar /* Check that there's a primary header. */ 8591d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 8601d3aed33SMarcel Moolenaar if (buf == NULL) 8611d3aed33SMarcel Moolenaar return (error); 8621d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8631d3aed33SMarcel Moolenaar g_free(buf); 8641d3aed33SMarcel Moolenaar if (res == 0) 8653e5a0a6bSMarcel Moolenaar return (pri); 866af90a872SAndrey V. Elsukov } else 867af90a872SAndrey V. Elsukov g_free(buf); 8681d3aed33SMarcel Moolenaar 8691d3aed33SMarcel Moolenaar /* No primary? Check that there's a secondary. */ 8701d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 8711d3aed33SMarcel Moolenaar &error); 8721d3aed33SMarcel Moolenaar if (buf == NULL) 8731d3aed33SMarcel Moolenaar return (error); 8741d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8751d3aed33SMarcel Moolenaar g_free(buf); 8763e5a0a6bSMarcel Moolenaar return ((res == 0) ? pri : ENXIO); 8771d3aed33SMarcel Moolenaar } 8781d3aed33SMarcel Moolenaar 8791d3aed33SMarcel Moolenaar static int 8801d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 8811d3aed33SMarcel Moolenaar { 8821c2dee3cSRobert Noland struct gpt_hdr *prihdr, *sechdr; 8831d3aed33SMarcel Moolenaar struct gpt_ent *tbl, *pritbl, *sectbl; 8841d3aed33SMarcel Moolenaar struct g_provider *pp; 8851d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 8861d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8874d32fcb4SMarcel Moolenaar u_char *buf; 888e7926a37SAndrey V. Elsukov uint64_t last; 8894d32fcb4SMarcel Moolenaar int error, index; 8901d3aed33SMarcel Moolenaar 8911d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 8921d3aed33SMarcel Moolenaar pp = cp->provider; 893e7926a37SAndrey V. Elsukov last = (pp->mediasize / pp->sectorsize) - 1; 8941d3aed33SMarcel Moolenaar 8954d32fcb4SMarcel Moolenaar /* Read the PMBR */ 8964d32fcb4SMarcel Moolenaar buf = g_read_data(cp, 0, pp->sectorsize, &error); 8974d32fcb4SMarcel Moolenaar if (buf == NULL) 8984d32fcb4SMarcel Moolenaar return (error); 8994d32fcb4SMarcel Moolenaar bcopy(buf, table->mbr, MBRSIZE); 9004d32fcb4SMarcel Moolenaar g_free(buf); 9014d32fcb4SMarcel Moolenaar 9021d3aed33SMarcel Moolenaar /* Read the primary header and table. */ 9031c2dee3cSRobert Noland prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 9041d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 9051c2dee3cSRobert Noland pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 9061d3aed33SMarcel Moolenaar } else { 9071d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 9081d3aed33SMarcel Moolenaar pritbl = NULL; 9091d3aed33SMarcel Moolenaar } 9101d3aed33SMarcel Moolenaar 9111d3aed33SMarcel Moolenaar /* Read the secondary header and table. */ 9121c2dee3cSRobert Noland sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 9131d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 9141c2dee3cSRobert Noland sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 9151d3aed33SMarcel Moolenaar } else { 9161d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 9171d3aed33SMarcel Moolenaar sectbl = NULL; 9181d3aed33SMarcel Moolenaar } 9191d3aed33SMarcel Moolenaar 9201d3aed33SMarcel Moolenaar /* Fail if we haven't got any good tables at all. */ 9211d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 9221d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 9231d3aed33SMarcel Moolenaar printf("GEOM: %s: corrupt or invalid GPT detected.\n", 9241d3aed33SMarcel Moolenaar pp->name); 9251d3aed33SMarcel Moolenaar printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 9261d3aed33SMarcel Moolenaar pp->name); 927ee4d316fSConrad Meyer if (prihdr != NULL) 928ee4d316fSConrad Meyer g_free(prihdr); 929ee4d316fSConrad Meyer if (pritbl != NULL) 930ee4d316fSConrad Meyer g_free(pritbl); 931ee4d316fSConrad Meyer if (sechdr != NULL) 932ee4d316fSConrad Meyer g_free(sechdr); 933ee4d316fSConrad Meyer if (sectbl != NULL) 934ee4d316fSConrad Meyer g_free(sectbl); 9351d3aed33SMarcel Moolenaar return (EINVAL); 9361d3aed33SMarcel Moolenaar } 9371d3aed33SMarcel Moolenaar 9381d3aed33SMarcel Moolenaar /* 9391d3aed33SMarcel Moolenaar * If both headers are good but they disagree with each other, 9401d3aed33SMarcel Moolenaar * then invalidate one. We prefer to keep the primary header, 9411d3aed33SMarcel Moolenaar * unless the primary table is corrupt. 9421d3aed33SMarcel Moolenaar */ 9431d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 9441d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 9451c2dee3cSRobert Noland !gpt_matched_hdrs(prihdr, sechdr)) { 946dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 9471d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 948dd0db05dSMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 9491c2dee3cSRobert Noland g_free(sechdr); 9501c2dee3cSRobert Noland sechdr = NULL; 951dd0db05dSMarcel Moolenaar } else { 9521d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 953dd0db05dSMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 9541c2dee3cSRobert Noland g_free(prihdr); 9551c2dee3cSRobert Noland prihdr = NULL; 956dd0db05dSMarcel Moolenaar } 9571d3aed33SMarcel Moolenaar } 9581d3aed33SMarcel Moolenaar 959dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 9601d3aed33SMarcel Moolenaar printf("GEOM: %s: the primary GPT table is corrupt or " 9611d3aed33SMarcel Moolenaar "invalid.\n", pp->name); 9621d3aed33SMarcel Moolenaar printf("GEOM: %s: using the secondary instead -- recovery " 9631d3aed33SMarcel Moolenaar "strongly advised.\n", pp->name); 9641d3aed33SMarcel Moolenaar table->hdr = sechdr; 965e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9661c2dee3cSRobert Noland if (prihdr != NULL) 9671c2dee3cSRobert Noland g_free(prihdr); 9681d3aed33SMarcel Moolenaar tbl = sectbl; 9691d3aed33SMarcel Moolenaar if (pritbl != NULL) 9701d3aed33SMarcel Moolenaar g_free(pritbl); 9711d3aed33SMarcel Moolenaar } else { 972dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 9731d3aed33SMarcel Moolenaar printf("GEOM: %s: the secondary GPT table is corrupt " 9741d3aed33SMarcel Moolenaar "or invalid.\n", pp->name); 9751d3aed33SMarcel Moolenaar printf("GEOM: %s: using the primary only -- recovery " 9761d3aed33SMarcel Moolenaar "suggested.\n", pp->name); 977e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 978e7926a37SAndrey V. Elsukov } else if (table->lba[GPT_ELT_SECHDR] != last) { 979e7926a37SAndrey V. Elsukov printf( "GEOM: %s: the secondary GPT header is not in " 980e7926a37SAndrey V. Elsukov "the last LBA.\n", pp->name); 981e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9821d3aed33SMarcel Moolenaar } 9831d3aed33SMarcel Moolenaar table->hdr = prihdr; 9841c2dee3cSRobert Noland if (sechdr != NULL) 9851c2dee3cSRobert Noland g_free(sechdr); 9861d3aed33SMarcel Moolenaar tbl = pritbl; 9871d3aed33SMarcel Moolenaar if (sectbl != NULL) 9881d3aed33SMarcel Moolenaar g_free(sectbl); 9891d3aed33SMarcel Moolenaar } 9901d3aed33SMarcel Moolenaar 9911c2dee3cSRobert Noland basetable->gpt_first = table->hdr->hdr_lba_start; 9921c2dee3cSRobert Noland basetable->gpt_last = table->hdr->hdr_lba_end; 993b74dd6c7SAndrey V. Elsukov basetable->gpt_entries = (table->hdr->hdr_lba_start - 2) * 994b74dd6c7SAndrey V. Elsukov pp->sectorsize / table->hdr->hdr_entsz; 9951d3aed33SMarcel Moolenaar 996b74dd6c7SAndrey V. Elsukov for (index = table->hdr->hdr_entries - 1; index >= 0; index--) { 9971d3aed33SMarcel Moolenaar if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 9981d3aed33SMarcel Moolenaar continue; 999e7926a37SAndrey V. Elsukov entry = (struct g_part_gpt_entry *)g_part_new_entry( 1000e7926a37SAndrey V. Elsukov basetable, index + 1, tbl[index].ent_lba_start, 1001e7926a37SAndrey V. Elsukov tbl[index].ent_lba_end); 10021d3aed33SMarcel Moolenaar entry->ent = tbl[index]; 10031d3aed33SMarcel Moolenaar } 10041d3aed33SMarcel Moolenaar 10051d3aed33SMarcel Moolenaar g_free(tbl); 1006369fe59dSMarcel Moolenaar 1007369fe59dSMarcel Moolenaar /* 1008369fe59dSMarcel Moolenaar * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 1009369fe59dSMarcel Moolenaar * if (and only if) any FAT32 or FAT16 partitions have been 1010369fe59dSMarcel Moolenaar * created. This happens irrespective of whether Boot Camp is 1011369fe59dSMarcel Moolenaar * used/enabled, though it's generally understood to be done 1012369fe59dSMarcel Moolenaar * to support legacy Windows under Boot Camp. We refer to this 1013369fe59dSMarcel Moolenaar * mirroring simply as Boot Camp. We try to detect Boot Camp 1014369fe59dSMarcel Moolenaar * so that we can update the MBR if and when GPT changes have 1015369fe59dSMarcel Moolenaar * been made. Note that we do not enable Boot Camp if not 1016369fe59dSMarcel Moolenaar * previously enabled because we can't assume that we're on a 1017369fe59dSMarcel Moolenaar * Mac alongside Mac OS X. 1018369fe59dSMarcel Moolenaar */ 1019369fe59dSMarcel Moolenaar table->bootcamp = gpt_is_bootcamp(table, pp->name); 1020369fe59dSMarcel Moolenaar 10211d3aed33SMarcel Moolenaar return (0); 10221d3aed33SMarcel Moolenaar } 10231d3aed33SMarcel Moolenaar 1024cec283baSPawel Jakub Dawidek static int 1025e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable) 1026e7926a37SAndrey V. Elsukov { 10273bd22a9cSMarcel Moolenaar struct g_part_gpt_table *table; 10283bd22a9cSMarcel Moolenaar struct g_provider *pp; 1029e7926a37SAndrey V. Elsukov 10303bd22a9cSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 10313bd22a9cSMarcel Moolenaar pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 10323bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 10333bd22a9cSMarcel Moolenaar g_gpt_set_defaults(basetable, pp); 1034e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 0; 1035e7926a37SAndrey V. Elsukov return (0); 1036e7926a37SAndrey V. Elsukov } 1037e7926a37SAndrey V. Elsukov 1038e7926a37SAndrey V. Elsukov static int 1039369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable, 1040369fe59dSMarcel Moolenaar struct g_part_entry *baseentry, const char *attrib, unsigned int set) 1041cec283baSPawel Jakub Dawidek { 1042cec283baSPawel Jakub Dawidek struct g_part_gpt_entry *entry; 1043369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 1044ce808c7aSAllan Jude struct g_provider *pp; 10453bd22a9cSMarcel Moolenaar uint8_t *p; 1046369fe59dSMarcel Moolenaar uint64_t attr; 1047369fe59dSMarcel Moolenaar int i; 1048cec283baSPawel Jakub Dawidek 1049369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 1050369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 1051369fe59dSMarcel Moolenaar 1052369fe59dSMarcel Moolenaar if (strcasecmp(attrib, "active") == 0) { 10533bd22a9cSMarcel Moolenaar if (table->bootcamp) { 10543bd22a9cSMarcel Moolenaar /* The active flag must be set on a valid entry. */ 10553bd22a9cSMarcel Moolenaar if (entry == NULL) 10563bd22a9cSMarcel Moolenaar return (ENXIO); 10573bd22a9cSMarcel Moolenaar if (baseentry->gpe_index > NDOSPART) 1058369fe59dSMarcel Moolenaar return (EINVAL); 1059369fe59dSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 10603bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 10613bd22a9cSMarcel Moolenaar p[0] = (i == baseentry->gpe_index - 1) 10623bd22a9cSMarcel Moolenaar ? ((set) ? 0x80 : 0) : 0; 10633bd22a9cSMarcel Moolenaar } 10643bd22a9cSMarcel Moolenaar } else { 10653bd22a9cSMarcel Moolenaar /* The PMBR is marked as active without an entry. */ 10663bd22a9cSMarcel Moolenaar if (entry != NULL) 10673bd22a9cSMarcel Moolenaar return (ENXIO); 10683bd22a9cSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 10693bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 10703bd22a9cSMarcel Moolenaar p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 10713bd22a9cSMarcel Moolenaar } 1072369fe59dSMarcel Moolenaar } 1073369fe59dSMarcel Moolenaar return (0); 1074ce808c7aSAllan Jude } else if (strcasecmp(attrib, "lenovofix") == 0) { 1075ce808c7aSAllan Jude /* 1076ce808c7aSAllan Jude * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR. 1077ce808c7aSAllan Jude * This workaround allows Lenovo X220, T420, T520, etc to boot 1078ce808c7aSAllan Jude * from GPT Partitions in BIOS mode. 1079ce808c7aSAllan Jude */ 1080ce808c7aSAllan Jude 1081ce808c7aSAllan Jude if (entry != NULL) 1082ce808c7aSAllan Jude return (ENXIO); 1083ce808c7aSAllan Jude 1084ce808c7aSAllan Jude pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 1085ce808c7aSAllan Jude bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 1086ce808c7aSAllan Jude gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1, 1087ce808c7aSAllan Jude MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 1088ce808c7aSAllan Jude return (0); 1089369fe59dSMarcel Moolenaar } 1090369fe59dSMarcel Moolenaar 10913bd22a9cSMarcel Moolenaar if (entry == NULL) 10923bd22a9cSMarcel Moolenaar return (ENODEV); 10933bd22a9cSMarcel Moolenaar 1094369fe59dSMarcel Moolenaar attr = 0; 1095cec283baSPawel Jakub Dawidek if (strcasecmp(attrib, "bootme") == 0) { 1096369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1097cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootonce") == 0) { 1098369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTONCE; 1099cec283baSPawel Jakub Dawidek if (set) 1100369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1101cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootfailed") == 0) { 1102cec283baSPawel Jakub Dawidek /* 1103cec283baSPawel Jakub Dawidek * It should only be possible to unset BOOTFAILED, but it might 1104cec283baSPawel Jakub Dawidek * be useful for test purposes to also be able to set it. 1105cec283baSPawel Jakub Dawidek */ 1106369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTFAILED; 1107cec283baSPawel Jakub Dawidek } 1108369fe59dSMarcel Moolenaar if (attr == 0) 1109cec283baSPawel Jakub Dawidek return (EINVAL); 1110cec283baSPawel Jakub Dawidek 1111369fe59dSMarcel Moolenaar if (set) 1112369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr | attr; 1113369fe59dSMarcel Moolenaar else 1114369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr & ~attr; 1115369fe59dSMarcel Moolenaar if (attr != entry->ent.ent_attr) { 1116369fe59dSMarcel Moolenaar entry->ent.ent_attr = attr; 1117369fe59dSMarcel Moolenaar if (!baseentry->gpe_created) 1118369fe59dSMarcel Moolenaar baseentry->gpe_modified = 1; 1119cec283baSPawel Jakub Dawidek } 1120cec283baSPawel Jakub Dawidek return (0); 1121cec283baSPawel Jakub Dawidek } 1122cec283baSPawel Jakub Dawidek 11231d3aed33SMarcel Moolenaar static const char * 11241d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 11251d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 11261d3aed33SMarcel Moolenaar { 11271d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 11281d3aed33SMarcel Moolenaar struct uuid *type; 1129f1317430SRui Paulo struct g_part_uuid_alias *uap; 11301d3aed33SMarcel Moolenaar 11311d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 11321d3aed33SMarcel Moolenaar type = &entry->ent.ent_type; 1133f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1134f1317430SRui Paulo if (EQUUID(type, uap->uuid)) 1135f1317430SRui Paulo return (g_part_alias_name(uap->alias)); 1136cf231470SMarcel Moolenaar buf[0] = '!'; 1137cf231470SMarcel Moolenaar snprintf_uuid(buf + 1, bufsz - 1, type); 1138f1317430SRui Paulo 11391d3aed33SMarcel Moolenaar return (buf); 11401d3aed33SMarcel Moolenaar } 11411d3aed33SMarcel Moolenaar 11421d3aed33SMarcel Moolenaar static int 11431d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 11441d3aed33SMarcel Moolenaar { 11451d3aed33SMarcel Moolenaar unsigned char *buf, *bp; 11461d3aed33SMarcel Moolenaar struct g_provider *pp; 11471d3aed33SMarcel Moolenaar struct g_part_entry *baseentry; 11481d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 11491d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 1150e7926a37SAndrey V. Elsukov size_t tblsz; 11511d3aed33SMarcel Moolenaar uint32_t crc; 11521d3aed33SMarcel Moolenaar int error, index; 11531d3aed33SMarcel Moolenaar 11541d3aed33SMarcel Moolenaar pp = cp->provider; 11551d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 115655e0987aSPedro F. Giffuni tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz, 115755e0987aSPedro F. Giffuni pp->sectorsize); 11581d3aed33SMarcel Moolenaar 1159369fe59dSMarcel Moolenaar /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1160369fe59dSMarcel Moolenaar if (table->bootcamp) 11613bd22a9cSMarcel Moolenaar gpt_update_bootcamp(basetable, pp); 1162d4746e10SAndrey V. Elsukov 11634d32fcb4SMarcel Moolenaar /* Write the PMBR */ 11641d3aed33SMarcel Moolenaar buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 11654d32fcb4SMarcel Moolenaar bcopy(table->mbr, buf, MBRSIZE); 11661d3aed33SMarcel Moolenaar error = g_write_data(cp, 0, buf, pp->sectorsize); 11671d3aed33SMarcel Moolenaar g_free(buf); 11681d3aed33SMarcel Moolenaar if (error) 11691d3aed33SMarcel Moolenaar return (error); 11701d3aed33SMarcel Moolenaar 11711d3aed33SMarcel Moolenaar /* Allocate space for the header and entries. */ 1172e7926a37SAndrey V. Elsukov buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 11731d3aed33SMarcel Moolenaar 11741c2dee3cSRobert Noland memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 11751c2dee3cSRobert Noland le32enc(buf + 8, table->hdr->hdr_revision); 11761c2dee3cSRobert Noland le32enc(buf + 12, table->hdr->hdr_size); 11771c2dee3cSRobert Noland le64enc(buf + 40, table->hdr->hdr_lba_start); 11781c2dee3cSRobert Noland le64enc(buf + 48, table->hdr->hdr_lba_end); 11791c2dee3cSRobert Noland le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 11801c2dee3cSRobert Noland le32enc(buf + 80, table->hdr->hdr_entries); 11811c2dee3cSRobert Noland le32enc(buf + 84, table->hdr->hdr_entsz); 11821d3aed33SMarcel Moolenaar 11831d3aed33SMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1184d287f590SMarcel Moolenaar if (baseentry->gpe_deleted) 1185d287f590SMarcel Moolenaar continue; 11861d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 11871d3aed33SMarcel Moolenaar index = baseentry->gpe_index - 1; 11881c2dee3cSRobert Noland bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 11891d3aed33SMarcel Moolenaar le_uuid_enc(bp, &entry->ent.ent_type); 11901d3aed33SMarcel Moolenaar le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 11911d3aed33SMarcel Moolenaar le64enc(bp + 32, entry->ent.ent_lba_start); 11921d3aed33SMarcel Moolenaar le64enc(bp + 40, entry->ent.ent_lba_end); 11931d3aed33SMarcel Moolenaar le64enc(bp + 48, entry->ent.ent_attr); 11941d3aed33SMarcel Moolenaar memcpy(bp + 56, entry->ent.ent_name, 11951d3aed33SMarcel Moolenaar sizeof(entry->ent.ent_name)); 11961d3aed33SMarcel Moolenaar } 11971d3aed33SMarcel Moolenaar 11981d3aed33SMarcel Moolenaar crc = crc32(buf + pp->sectorsize, 11991c2dee3cSRobert Noland table->hdr->hdr_entries * table->hdr->hdr_entsz); 12001d3aed33SMarcel Moolenaar le32enc(buf + 88, crc); 12011d3aed33SMarcel Moolenaar 12021d3aed33SMarcel Moolenaar /* Write primary meta-data. */ 12031d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 12041d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 12051d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 12061d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 12071c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 12081d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 12091d3aed33SMarcel Moolenaar 1210799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1211799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1212799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1213799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1214799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1215799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 12161d3aed33SMarcel Moolenaar if (error) 12171d3aed33SMarcel Moolenaar goto out; 1218799eac8cSAndrey V. Elsukov } 12191d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 12201d3aed33SMarcel Moolenaar buf, pp->sectorsize); 12211d3aed33SMarcel Moolenaar if (error) 12221d3aed33SMarcel Moolenaar goto out; 12231d3aed33SMarcel Moolenaar 12241d3aed33SMarcel Moolenaar /* Write secondary meta-data. */ 12251d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 12261d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 12271d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 12281d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 12291c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 12301d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 12311d3aed33SMarcel Moolenaar 1232799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1233799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1234799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1235799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1236799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1237799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 12381d3aed33SMarcel Moolenaar if (error) 12391d3aed33SMarcel Moolenaar goto out; 1240799eac8cSAndrey V. Elsukov } 12411d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 12421d3aed33SMarcel Moolenaar buf, pp->sectorsize); 12431d3aed33SMarcel Moolenaar 12441d3aed33SMarcel Moolenaar out: 12451d3aed33SMarcel Moolenaar g_free(buf); 12461d3aed33SMarcel Moolenaar return (error); 12471d3aed33SMarcel Moolenaar } 12481d3aed33SMarcel Moolenaar 12491d3aed33SMarcel Moolenaar static void 12502b9be055SAndrey V. Elsukov g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 12512b9be055SAndrey V. Elsukov { 12524787115dSMarcel Moolenaar struct g_part_entry *baseentry; 12534787115dSMarcel Moolenaar struct g_part_gpt_entry *entry; 12542b9be055SAndrey V. Elsukov struct g_part_gpt_table *table; 12554787115dSMarcel Moolenaar quad_t start, end, min, max; 12564787115dSMarcel Moolenaar quad_t lba, last; 12574787115dSMarcel Moolenaar size_t spb, tblsz; 12582b9be055SAndrey V. Elsukov 12592b9be055SAndrey V. Elsukov table = (struct g_part_gpt_table *)basetable; 12602b9be055SAndrey V. Elsukov last = pp->mediasize / pp->sectorsize - 1; 126155e0987aSPedro F. Giffuni tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 126255e0987aSPedro F. Giffuni pp->sectorsize); 12632b9be055SAndrey V. Elsukov 12642b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRIHDR] = 1; 12652b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRITBL] = 2; 12662b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = last; 12672b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECTBL] = last - tblsz; 12682b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 12692b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 12702b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 12712b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 12722b9be055SAndrey V. Elsukov 12734787115dSMarcel Moolenaar max = start = 2 + tblsz; 12744787115dSMarcel Moolenaar min = end = last - tblsz - 1; 12754787115dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 12764787115dSMarcel Moolenaar if (baseentry->gpe_deleted) 12774787115dSMarcel Moolenaar continue; 12784787115dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 12794787115dSMarcel Moolenaar if (entry->ent.ent_lba_start < min) 12804787115dSMarcel Moolenaar min = entry->ent.ent_lba_start; 12814787115dSMarcel Moolenaar if (entry->ent.ent_lba_end > max) 12824787115dSMarcel Moolenaar max = entry->ent.ent_lba_end; 12834787115dSMarcel Moolenaar } 12844787115dSMarcel Moolenaar spb = 4096 / pp->sectorsize; 12854787115dSMarcel Moolenaar if (spb > 1) { 12864787115dSMarcel Moolenaar lba = start + ((start % spb) ? spb - start % spb : 0); 12874787115dSMarcel Moolenaar if (lba <= min) 12884787115dSMarcel Moolenaar start = lba; 12894787115dSMarcel Moolenaar lba = end - (end + 1) % spb; 12904787115dSMarcel Moolenaar if (max <= lba) 12914787115dSMarcel Moolenaar end = lba; 12924787115dSMarcel Moolenaar } 12934787115dSMarcel Moolenaar table->hdr->hdr_lba_start = start; 12944787115dSMarcel Moolenaar table->hdr->hdr_lba_end = end; 12952b9be055SAndrey V. Elsukov 12964787115dSMarcel Moolenaar basetable->gpt_first = start; 12974787115dSMarcel Moolenaar basetable->gpt_last = end; 12982b9be055SAndrey V. Elsukov } 12992b9be055SAndrey V. Elsukov 13002b9be055SAndrey V. Elsukov static void 1301d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 13021d3aed33SMarcel Moolenaar { 13031d3aed33SMarcel Moolenaar u_int bo; 13041d3aed33SMarcel Moolenaar uint32_t ch; 13051d3aed33SMarcel Moolenaar uint16_t c; 13061d3aed33SMarcel Moolenaar 1307d3532631SMarcel Moolenaar bo = LITTLE_ENDIAN; /* GPT is little-endian */ 13081d3aed33SMarcel Moolenaar while (len > 0 && *str != 0) { 13091d3aed33SMarcel Moolenaar ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 13101d3aed33SMarcel Moolenaar str++, len--; 13111d3aed33SMarcel Moolenaar if ((ch & 0xf800) == 0xd800) { 13121d3aed33SMarcel Moolenaar if (len > 0) { 13131d3aed33SMarcel Moolenaar c = (bo == BIG_ENDIAN) ? be16toh(*str) 13141d3aed33SMarcel Moolenaar : le16toh(*str); 13151d3aed33SMarcel Moolenaar str++, len--; 13161d3aed33SMarcel Moolenaar } else 13171d3aed33SMarcel Moolenaar c = 0xfffd; 13181d3aed33SMarcel Moolenaar if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 13191d3aed33SMarcel Moolenaar ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 13201d3aed33SMarcel Moolenaar ch += 0x10000; 13211d3aed33SMarcel Moolenaar } else 13221d3aed33SMarcel Moolenaar ch = 0xfffd; 13231d3aed33SMarcel Moolenaar } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 13241d3aed33SMarcel Moolenaar bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 13251d3aed33SMarcel Moolenaar continue; 13261d3aed33SMarcel Moolenaar } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 13271d3aed33SMarcel Moolenaar continue; 13281d3aed33SMarcel Moolenaar 1329d3532631SMarcel Moolenaar /* Write the Unicode character in UTF-8 */ 13301d3aed33SMarcel Moolenaar if (ch < 0x80) 133139dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c", ch); 13321d3aed33SMarcel Moolenaar else if (ch < 0x800) 133339dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6), 13341d3aed33SMarcel Moolenaar 0x80 | (ch & 0x3f)); 13351d3aed33SMarcel Moolenaar else if (ch < 0x10000) 133639dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12), 13371d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 13381d3aed33SMarcel Moolenaar else if (ch < 0x200000) 133939dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 | 134039dcac84SAndrey V. Elsukov (ch >> 18), 0x80 | ((ch >> 12) & 0x3f), 13411d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 13421d3aed33SMarcel Moolenaar } 13431d3aed33SMarcel Moolenaar } 1344d3532631SMarcel Moolenaar 1345d3532631SMarcel Moolenaar static void 1346d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1347d3532631SMarcel Moolenaar { 1348d3532631SMarcel Moolenaar size_t s16idx, s8idx; 1349d3532631SMarcel Moolenaar uint32_t utfchar; 1350d3532631SMarcel Moolenaar unsigned int c, utfbytes; 1351d3532631SMarcel Moolenaar 1352d3532631SMarcel Moolenaar s8idx = s16idx = 0; 1353d3532631SMarcel Moolenaar utfchar = 0; 1354d3532631SMarcel Moolenaar utfbytes = 0; 1355d3532631SMarcel Moolenaar bzero(s16, s16len << 1); 1356d3532631SMarcel Moolenaar while (s8[s8idx] != 0 && s16idx < s16len) { 1357d3532631SMarcel Moolenaar c = s8[s8idx++]; 1358d3532631SMarcel Moolenaar if ((c & 0xc0) != 0x80) { 1359d3532631SMarcel Moolenaar /* Initial characters. */ 1360d3532631SMarcel Moolenaar if (utfbytes != 0) { 1361d3532631SMarcel Moolenaar /* Incomplete encoding of previous char. */ 1362d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1363d3532631SMarcel Moolenaar } 1364d3532631SMarcel Moolenaar if ((c & 0xf8) == 0xf0) { 1365d3532631SMarcel Moolenaar utfchar = c & 0x07; 1366d3532631SMarcel Moolenaar utfbytes = 3; 1367d3532631SMarcel Moolenaar } else if ((c & 0xf0) == 0xe0) { 1368d3532631SMarcel Moolenaar utfchar = c & 0x0f; 1369d3532631SMarcel Moolenaar utfbytes = 2; 1370d3532631SMarcel Moolenaar } else if ((c & 0xe0) == 0xc0) { 1371d3532631SMarcel Moolenaar utfchar = c & 0x1f; 1372d3532631SMarcel Moolenaar utfbytes = 1; 1373d3532631SMarcel Moolenaar } else { 1374d3532631SMarcel Moolenaar utfchar = c & 0x7f; 1375d3532631SMarcel Moolenaar utfbytes = 0; 1376d3532631SMarcel Moolenaar } 1377d3532631SMarcel Moolenaar } else { 1378d3532631SMarcel Moolenaar /* Followup characters. */ 1379d3532631SMarcel Moolenaar if (utfbytes > 0) { 1380d3532631SMarcel Moolenaar utfchar = (utfchar << 6) + (c & 0x3f); 1381d3532631SMarcel Moolenaar utfbytes--; 1382d3532631SMarcel Moolenaar } else if (utfbytes == 0) 1383d3532631SMarcel Moolenaar utfbytes = ~0; 1384d3532631SMarcel Moolenaar } 1385d3532631SMarcel Moolenaar /* 1386d3532631SMarcel Moolenaar * Write the complete Unicode character as UTF-16 when we 1387d3532631SMarcel Moolenaar * have all the UTF-8 charactars collected. 1388d3532631SMarcel Moolenaar */ 1389d3532631SMarcel Moolenaar if (utfbytes == 0) { 1390d3532631SMarcel Moolenaar /* 1391d3532631SMarcel Moolenaar * If we need to write 2 UTF-16 characters, but 1392d3532631SMarcel Moolenaar * we only have room for 1, then we truncate the 1393d3532631SMarcel Moolenaar * string by writing a 0 instead. 1394d3532631SMarcel Moolenaar */ 1395d3532631SMarcel Moolenaar if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1396d3532631SMarcel Moolenaar s16[s16idx++] = 1397d3532631SMarcel Moolenaar htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1398d3532631SMarcel Moolenaar s16[s16idx++] = 1399d3532631SMarcel Moolenaar htole16(0xdc00 | (utfchar & 0x3ff)); 1400d3532631SMarcel Moolenaar } else 1401d3532631SMarcel Moolenaar s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1402d3532631SMarcel Moolenaar htole16(utfchar); 1403d3532631SMarcel Moolenaar } 1404d3532631SMarcel Moolenaar } 1405d3532631SMarcel Moolenaar /* 1406d3532631SMarcel Moolenaar * If our input string was truncated, append an invalid encoding 1407d3532631SMarcel Moolenaar * character to the output string. 1408d3532631SMarcel Moolenaar */ 1409d3532631SMarcel Moolenaar if (utfbytes != 0 && s16idx < s16len) 1410d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1411d3532631SMarcel Moolenaar } 1412