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); 1491d3aed33SMarcel Moolenaar 150b42712a8SConrad Meyer static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS; 151f1317430SRui Paulo static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 1524fb4ebe0SAndrey V. Elsukov static struct uuid gpt_uuid_apple_core_storage = 1534fb4ebe0SAndrey V. Elsukov GPT_ENT_TYPE_APPLE_CORE_STORAGE; 15487662ab3SRui Paulo static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 155f1317430SRui Paulo static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 156f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 157f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 158f1317430SRui Paulo static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 159f1317430SRui Paulo static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 16088007f61SAndrey V. Elsukov static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 1619c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE; 1629c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; 1639c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED; 1649c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT; 1659c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD; 1669c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER; 1679c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2; 1689c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32; 1699c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64; 1709c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY; 1719c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP; 1729c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1; 1739c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM; 1741d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 1751d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 176f352a0d4SJohn Baldwin static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 177f24a8224SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 1781d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 1791d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 1801d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 181a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 182f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 183f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 184f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 1851d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 1869c0c355fSAllan Jude static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 18733f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 18833f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 18933f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 1909c0c355fSAllan Jude static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY; 1919c0c355fSAllan Jude static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 1929c0c355fSAllan Jude static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES; 19333f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 19433f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 19533f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 19633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 19733f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 19833f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 1999c0c355fSAllan Jude static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA; 2001ee0f089SNathan Whitehorn static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT; 2019c0c355fSAllan Jude static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 2029c0c355fSAllan Jude static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 2039c0c355fSAllan Jude static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 2049c0c355fSAllan Jude static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 2059c0c355fSAllan Jude static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR; 2061d3aed33SMarcel Moolenaar 207f1317430SRui Paulo static struct g_part_uuid_alias { 208f1317430SRui Paulo struct uuid *uuid; 209f1317430SRui Paulo int alias; 210369fe59dSMarcel Moolenaar int mbrtype; 211f1317430SRui Paulo } gpt_uuid_alias_match[] = { 212b42712a8SConrad Meyer { &gpt_uuid_apple_apfs, G_PART_ALIAS_APPLE_APFS, 0 }, 213369fe59dSMarcel Moolenaar { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 2144fb4ebe0SAndrey V. Elsukov { &gpt_uuid_apple_core_storage, G_PART_ALIAS_APPLE_CORE_STORAGE, 0 }, 215369fe59dSMarcel Moolenaar { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 216369fe59dSMarcel Moolenaar { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 217369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 218369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 219369fe59dSMarcel Moolenaar { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 220369fe59dSMarcel Moolenaar { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 221369fe59dSMarcel Moolenaar { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 2229c0c355fSAllan Jude { &gpt_uuid_chromeos_firmware, G_PART_ALIAS_CHROMEOS_FIRMWARE, 0 }, 2239c0c355fSAllan Jude { &gpt_uuid_chromeos_kernel, G_PART_ALIAS_CHROMEOS_KERNEL, 0 }, 2249c0c355fSAllan Jude { &gpt_uuid_chromeos_reserved, G_PART_ALIAS_CHROMEOS_RESERVED, 0 }, 2259c0c355fSAllan Jude { &gpt_uuid_chromeos_root, G_PART_ALIAS_CHROMEOS_ROOT, 0 }, 2269c0c355fSAllan Jude { &gpt_uuid_dfbsd_ccd, G_PART_ALIAS_DFBSD_CCD, 0 }, 2279c0c355fSAllan Jude { &gpt_uuid_dfbsd_hammer, G_PART_ALIAS_DFBSD_HAMMER, 0 }, 2289c0c355fSAllan Jude { &gpt_uuid_dfbsd_hammer2, G_PART_ALIAS_DFBSD_HAMMER2, 0 }, 2299c0c355fSAllan Jude { &gpt_uuid_dfbsd_label32, G_PART_ALIAS_DFBSD, 0xa5 }, 2309c0c355fSAllan Jude { &gpt_uuid_dfbsd_label64, G_PART_ALIAS_DFBSD64, 0xa5 }, 2319c0c355fSAllan Jude { &gpt_uuid_dfbsd_legacy, G_PART_ALIAS_DFBSD_LEGACY, 0 }, 2329c0c355fSAllan Jude { &gpt_uuid_dfbsd_swap, G_PART_ALIAS_DFBSD_SWAP, 0 }, 2339c0c355fSAllan Jude { &gpt_uuid_dfbsd_ufs1, G_PART_ALIAS_DFBSD_UFS, 0 }, 2349c0c355fSAllan Jude { &gpt_uuid_dfbsd_vinum, G_PART_ALIAS_DFBSD_VINUM, 0 }, 235369fe59dSMarcel Moolenaar { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 236369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 237369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 238f24a8224SMarcel Moolenaar { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 }, 239369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 240369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 241369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 242369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 243369fe59dSMarcel Moolenaar { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 244369fe59dSMarcel Moolenaar { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 245369fe59dSMarcel Moolenaar { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 246369fe59dSMarcel Moolenaar { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 247369fe59dSMarcel Moolenaar { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 248369fe59dSMarcel Moolenaar { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 249369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 250369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 2519c0c355fSAllan Jude { &gpt_uuid_ms_recovery, G_PART_ALIAS_MS_RECOVERY, 0 }, 252369fe59dSMarcel Moolenaar { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 2539c0c355fSAllan Jude { &gpt_uuid_ms_spaces, G_PART_ALIAS_MS_SPACES, 0 }, 254369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 255369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 256369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 257369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 258369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 259369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 2609c0c355fSAllan Jude { &gpt_uuid_openbsd_data, G_PART_ALIAS_OPENBSD_DATA, 0 }, 2611ee0f089SNathan Whitehorn { &gpt_uuid_prep_boot, G_PART_ALIAS_PREP_BOOT, 0x41 }, 2629c0c355fSAllan Jude { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 2639c0c355fSAllan Jude { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 2649c0c355fSAllan Jude { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 2659c0c355fSAllan Jude { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 }, 266369fe59dSMarcel Moolenaar { NULL, 0, 0 } 267f1317430SRui Paulo }; 268f1317430SRui Paulo 269369fe59dSMarcel Moolenaar static int 270369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 271369fe59dSMarcel Moolenaar quad_t end) 272369fe59dSMarcel Moolenaar { 273369fe59dSMarcel Moolenaar 274369fe59dSMarcel Moolenaar if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 275369fe59dSMarcel Moolenaar return (EINVAL); 276369fe59dSMarcel Moolenaar 277369fe59dSMarcel Moolenaar mbr += DOSPARTOFF + idx * DOSPARTSIZE; 278369fe59dSMarcel Moolenaar mbr[0] = 0; 279369fe59dSMarcel Moolenaar if (start == 1) { 280369fe59dSMarcel Moolenaar /* 281369fe59dSMarcel Moolenaar * Treat the PMBR partition specially to maximize 282369fe59dSMarcel Moolenaar * interoperability with BIOSes. 283369fe59dSMarcel Moolenaar */ 284369fe59dSMarcel Moolenaar mbr[1] = mbr[3] = 0; 285369fe59dSMarcel Moolenaar mbr[2] = 2; 286369fe59dSMarcel Moolenaar } else 287369fe59dSMarcel Moolenaar mbr[1] = mbr[2] = mbr[3] = 0xff; 288369fe59dSMarcel Moolenaar mbr[4] = typ; 289369fe59dSMarcel Moolenaar mbr[5] = mbr[6] = mbr[7] = 0xff; 290369fe59dSMarcel Moolenaar le32enc(mbr + 8, (uint32_t)start); 291369fe59dSMarcel Moolenaar le32enc(mbr + 12, (uint32_t)(end - start + 1)); 292369fe59dSMarcel Moolenaar return (0); 293369fe59dSMarcel Moolenaar } 294369fe59dSMarcel Moolenaar 295369fe59dSMarcel Moolenaar static int 296369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t) 297369fe59dSMarcel Moolenaar { 298369fe59dSMarcel Moolenaar struct g_part_uuid_alias *uap; 299369fe59dSMarcel Moolenaar 300369fe59dSMarcel Moolenaar for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 301369fe59dSMarcel Moolenaar if (EQUUID(t, uap->uuid)) 302369fe59dSMarcel Moolenaar return (uap->mbrtype); 303369fe59dSMarcel Moolenaar } 304369fe59dSMarcel Moolenaar return (0); 305369fe59dSMarcel Moolenaar } 306369fe59dSMarcel Moolenaar 3073bd22a9cSMarcel Moolenaar static void 3083bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 3093bd22a9cSMarcel Moolenaar { 3103bd22a9cSMarcel Moolenaar 3113bd22a9cSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 3123bd22a9cSMarcel Moolenaar gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 3133bd22a9cSMarcel Moolenaar MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 3143bd22a9cSMarcel Moolenaar le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 3153bd22a9cSMarcel Moolenaar } 3163bd22a9cSMarcel Moolenaar 317369fe59dSMarcel Moolenaar /* 318369fe59dSMarcel Moolenaar * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 319369fe59dSMarcel Moolenaar * whole disk anymore. Rather, it covers the GPT table and the EFI 320369fe59dSMarcel Moolenaar * system partition only. This way the HFS+ partition and any FAT 321369fe59dSMarcel Moolenaar * partitions can be added to the MBR without creating an overlap. 322369fe59dSMarcel Moolenaar */ 323369fe59dSMarcel Moolenaar static int 324369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 325369fe59dSMarcel Moolenaar { 326369fe59dSMarcel Moolenaar uint8_t *p; 327369fe59dSMarcel Moolenaar 328369fe59dSMarcel Moolenaar p = table->mbr + DOSPARTOFF; 329369fe59dSMarcel Moolenaar if (p[4] != 0xee || le32dec(p + 8) != 1) 330369fe59dSMarcel Moolenaar return (0); 331369fe59dSMarcel Moolenaar 332369fe59dSMarcel Moolenaar p += DOSPARTSIZE; 333369fe59dSMarcel Moolenaar if (p[4] != 0xaf) 334369fe59dSMarcel Moolenaar return (0); 335369fe59dSMarcel Moolenaar 336369fe59dSMarcel Moolenaar printf("GEOM: %s: enabling Boot Camp\n", provname); 337369fe59dSMarcel Moolenaar return (1); 338369fe59dSMarcel Moolenaar } 339369fe59dSMarcel Moolenaar 340369fe59dSMarcel Moolenaar static void 3413bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 342369fe59dSMarcel Moolenaar { 343369fe59dSMarcel Moolenaar struct g_part_entry *baseentry; 344369fe59dSMarcel Moolenaar struct g_part_gpt_entry *entry; 345369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 346369fe59dSMarcel Moolenaar int bootable, error, index, slices, typ; 347369fe59dSMarcel Moolenaar 348369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 349369fe59dSMarcel Moolenaar 350369fe59dSMarcel Moolenaar bootable = -1; 351369fe59dSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 352369fe59dSMarcel Moolenaar if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 353369fe59dSMarcel Moolenaar bootable = index; 354369fe59dSMarcel Moolenaar } 355369fe59dSMarcel Moolenaar 356369fe59dSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 357369fe59dSMarcel Moolenaar slices = 0; 358369fe59dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 359369fe59dSMarcel Moolenaar if (baseentry->gpe_deleted) 360369fe59dSMarcel Moolenaar continue; 361369fe59dSMarcel Moolenaar index = baseentry->gpe_index - 1; 362369fe59dSMarcel Moolenaar if (index >= NDOSPART) 363369fe59dSMarcel Moolenaar continue; 364369fe59dSMarcel Moolenaar 365369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 366369fe59dSMarcel Moolenaar 367369fe59dSMarcel Moolenaar switch (index) { 368369fe59dSMarcel Moolenaar case 0: /* This must be the EFI system partition. */ 369369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 370369fe59dSMarcel Moolenaar goto disable; 371369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xee, 372369fe59dSMarcel Moolenaar 1ull, entry->ent.ent_lba_end); 373369fe59dSMarcel Moolenaar break; 374369fe59dSMarcel Moolenaar case 1: /* This must be the HFS+ partition. */ 375369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 376369fe59dSMarcel Moolenaar goto disable; 377369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 378369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 379369fe59dSMarcel Moolenaar break; 380369fe59dSMarcel Moolenaar default: 381369fe59dSMarcel Moolenaar typ = gpt_map_type(&entry->ent.ent_type); 382369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, typ, 383369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 384369fe59dSMarcel Moolenaar break; 385369fe59dSMarcel Moolenaar } 386369fe59dSMarcel Moolenaar if (error) 387369fe59dSMarcel Moolenaar continue; 388369fe59dSMarcel Moolenaar 389369fe59dSMarcel Moolenaar if (index == bootable) 390369fe59dSMarcel Moolenaar table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 391369fe59dSMarcel Moolenaar slices |= 1 << index; 392369fe59dSMarcel Moolenaar } 393369fe59dSMarcel Moolenaar if ((slices & 3) == 3) 394369fe59dSMarcel Moolenaar return; 395369fe59dSMarcel Moolenaar 396369fe59dSMarcel Moolenaar disable: 397369fe59dSMarcel Moolenaar table->bootcamp = 0; 3983bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 399369fe59dSMarcel Moolenaar } 400369fe59dSMarcel Moolenaar 4011c2dee3cSRobert Noland static struct gpt_hdr * 4021d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 4031c2dee3cSRobert Noland enum gpt_elt elt) 4041d3aed33SMarcel Moolenaar { 4051c2dee3cSRobert Noland struct gpt_hdr *buf, *hdr; 4061d3aed33SMarcel Moolenaar struct g_provider *pp; 4071d3aed33SMarcel Moolenaar quad_t lba, last; 4081d3aed33SMarcel Moolenaar int error; 4091d3aed33SMarcel Moolenaar uint32_t crc, sz; 4101d3aed33SMarcel Moolenaar 4111d3aed33SMarcel Moolenaar pp = cp->provider; 4121d3aed33SMarcel Moolenaar last = (pp->mediasize / pp->sectorsize) - 1; 4131d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 414e7926a37SAndrey V. Elsukov /* 415e7926a37SAndrey V. Elsukov * If the primary header is valid look for secondary 416e7926a37SAndrey V. Elsukov * header in AlternateLBA, otherwise in the last medium's LBA. 417e7926a37SAndrey V. Elsukov */ 418e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_SECHDR) { 419e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 420e7926a37SAndrey V. Elsukov table->lba[elt] = last; 421e7926a37SAndrey V. Elsukov } else 422e7926a37SAndrey V. Elsukov table->lba[elt] = 1; 4231d3aed33SMarcel Moolenaar buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 4241d3aed33SMarcel Moolenaar &error); 4251d3aed33SMarcel Moolenaar if (buf == NULL) 4261c2dee3cSRobert Noland return (NULL); 4271c2dee3cSRobert Noland hdr = NULL; 4281c2dee3cSRobert Noland if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 4291c2dee3cSRobert Noland goto fail; 4301d3aed33SMarcel Moolenaar 4311d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 4321c2dee3cSRobert Noland sz = le32toh(buf->hdr_size); 4331d3aed33SMarcel Moolenaar if (sz < 92 || sz > pp->sectorsize) 4341c2dee3cSRobert Noland goto fail; 4351c2dee3cSRobert Noland 4361c2dee3cSRobert Noland hdr = g_malloc(sz, M_WAITOK | M_ZERO); 4371c2dee3cSRobert Noland bcopy(buf, hdr, sz); 4381d3aed33SMarcel Moolenaar hdr->hdr_size = sz; 4391c2dee3cSRobert Noland 4401c2dee3cSRobert Noland crc = le32toh(buf->hdr_crc_self); 4411c2dee3cSRobert Noland buf->hdr_crc_self = 0; 4421c2dee3cSRobert Noland if (crc32(buf, sz) != crc) 4431c2dee3cSRobert Noland goto fail; 4441d3aed33SMarcel Moolenaar hdr->hdr_crc_self = crc; 4451d3aed33SMarcel Moolenaar 4461d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_INVALID; 4471c2dee3cSRobert Noland hdr->hdr_revision = le32toh(buf->hdr_revision); 448e7926a37SAndrey V. Elsukov if (hdr->hdr_revision < GPT_HDR_REVISION) 4491c2dee3cSRobert Noland goto fail; 4501c2dee3cSRobert Noland hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 4511d3aed33SMarcel Moolenaar if (hdr->hdr_lba_self != table->lba[elt]) 4521c2dee3cSRobert Noland goto fail; 4531c2dee3cSRobert Noland hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 454e7926a37SAndrey V. Elsukov if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 455e7926a37SAndrey V. Elsukov hdr->hdr_lba_alt > last) 456e7926a37SAndrey V. Elsukov goto fail; 4571d3aed33SMarcel Moolenaar 4581d3aed33SMarcel Moolenaar /* Check the managed area. */ 4591c2dee3cSRobert Noland hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 4601d3aed33SMarcel Moolenaar if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 4611c2dee3cSRobert Noland goto fail; 4621c2dee3cSRobert Noland hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 4631d3aed33SMarcel Moolenaar if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 4641c2dee3cSRobert Noland goto fail; 4651d3aed33SMarcel Moolenaar 4661d3aed33SMarcel Moolenaar /* Check the table location and size of the table. */ 4671c2dee3cSRobert Noland hdr->hdr_entries = le32toh(buf->hdr_entries); 4681c2dee3cSRobert Noland hdr->hdr_entsz = le32toh(buf->hdr_entsz); 4691d3aed33SMarcel Moolenaar if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 4701d3aed33SMarcel Moolenaar (hdr->hdr_entsz & 7) != 0) 4711c2dee3cSRobert Noland goto fail; 4721c2dee3cSRobert Noland hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 4731d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 4741c2dee3cSRobert Noland goto fail; 4751d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 4761d3aed33SMarcel Moolenaar hdr->hdr_lba_table <= hdr->hdr_lba_end) 4771c2dee3cSRobert Noland goto fail; 4781d3aed33SMarcel Moolenaar lba = hdr->hdr_lba_table + 47955e0987aSPedro F. Giffuni howmany(hdr->hdr_entries * hdr->hdr_entsz, pp->sectorsize) - 1; 4801d3aed33SMarcel Moolenaar if (lba >= last) 4811c2dee3cSRobert Noland goto fail; 4821d3aed33SMarcel Moolenaar if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 4831c2dee3cSRobert Noland goto fail; 4841d3aed33SMarcel Moolenaar 4851d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4861c2dee3cSRobert Noland le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 4871c2dee3cSRobert Noland hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 4881c2dee3cSRobert Noland 489e7926a37SAndrey V. Elsukov /* save LBA for secondary header */ 490e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_PRIHDR) 491e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 492e7926a37SAndrey V. Elsukov 4931c2dee3cSRobert Noland g_free(buf); 4941c2dee3cSRobert Noland return (hdr); 4951c2dee3cSRobert Noland 4961c2dee3cSRobert Noland fail: 4971c2dee3cSRobert Noland if (hdr != NULL) 4981c2dee3cSRobert Noland g_free(hdr); 4991c2dee3cSRobert Noland g_free(buf); 5001c2dee3cSRobert Noland return (NULL); 5011d3aed33SMarcel Moolenaar } 5021d3aed33SMarcel Moolenaar 5031d3aed33SMarcel Moolenaar static struct gpt_ent * 5041d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 5051d3aed33SMarcel Moolenaar enum gpt_elt elt, struct gpt_hdr *hdr) 5061d3aed33SMarcel Moolenaar { 5071d3aed33SMarcel Moolenaar struct g_provider *pp; 5081d3aed33SMarcel Moolenaar struct gpt_ent *ent, *tbl; 5091d3aed33SMarcel Moolenaar char *buf, *p; 510799eac8cSAndrey V. Elsukov unsigned int idx, sectors, tblsz, size; 5111d3aed33SMarcel Moolenaar int error; 5121d3aed33SMarcel Moolenaar 5131c2dee3cSRobert Noland if (hdr == NULL) 5141c2dee3cSRobert Noland return (NULL); 5151c2dee3cSRobert Noland 5161d3aed33SMarcel Moolenaar pp = cp->provider; 5171d3aed33SMarcel Moolenaar table->lba[elt] = hdr->hdr_lba_table; 5181d3aed33SMarcel Moolenaar 5191d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 5201d3aed33SMarcel Moolenaar tblsz = hdr->hdr_entries * hdr->hdr_entsz; 52155e0987aSPedro F. Giffuni sectors = howmany(tblsz, pp->sectorsize); 522799eac8cSAndrey V. Elsukov buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 523799eac8cSAndrey V. Elsukov for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 524799eac8cSAndrey V. Elsukov size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 525799eac8cSAndrey V. Elsukov (sectors - idx) * pp->sectorsize; 526799eac8cSAndrey V. Elsukov p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 527799eac8cSAndrey V. Elsukov size, &error); 528799eac8cSAndrey V. Elsukov if (p == NULL) { 529799eac8cSAndrey V. Elsukov g_free(buf); 5301d3aed33SMarcel Moolenaar return (NULL); 531799eac8cSAndrey V. Elsukov } 532799eac8cSAndrey V. Elsukov bcopy(p, buf + idx * pp->sectorsize, size); 533799eac8cSAndrey V. Elsukov g_free(p); 534799eac8cSAndrey V. Elsukov } 5351d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 5361d3aed33SMarcel Moolenaar if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 5371d3aed33SMarcel Moolenaar g_free(buf); 5381d3aed33SMarcel Moolenaar return (NULL); 5391d3aed33SMarcel Moolenaar } 5401d3aed33SMarcel Moolenaar 5411d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 5421d3aed33SMarcel Moolenaar tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 5431d3aed33SMarcel Moolenaar M_WAITOK | M_ZERO); 5441d3aed33SMarcel Moolenaar 5451d3aed33SMarcel Moolenaar for (idx = 0, ent = tbl, p = buf; 5461d3aed33SMarcel Moolenaar idx < hdr->hdr_entries; 5471d3aed33SMarcel Moolenaar idx++, ent++, p += hdr->hdr_entsz) { 5481d3aed33SMarcel Moolenaar le_uuid_dec(p, &ent->ent_type); 5491d3aed33SMarcel Moolenaar le_uuid_dec(p + 16, &ent->ent_uuid); 5501d3aed33SMarcel Moolenaar ent->ent_lba_start = le64dec(p + 32); 5511d3aed33SMarcel Moolenaar ent->ent_lba_end = le64dec(p + 40); 5521d3aed33SMarcel Moolenaar ent->ent_attr = le64dec(p + 48); 553d3532631SMarcel Moolenaar /* Keep UTF-16 in little-endian. */ 554d3532631SMarcel Moolenaar bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 5551d3aed33SMarcel Moolenaar } 5561d3aed33SMarcel Moolenaar 5571d3aed33SMarcel Moolenaar g_free(buf); 5581d3aed33SMarcel Moolenaar return (tbl); 5591d3aed33SMarcel Moolenaar } 5601d3aed33SMarcel Moolenaar 5611d3aed33SMarcel Moolenaar static int 5621d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 5631d3aed33SMarcel Moolenaar { 5641d3aed33SMarcel Moolenaar 5651c2dee3cSRobert Noland if (pri == NULL || sec == NULL) 5661c2dee3cSRobert Noland return (0); 5671c2dee3cSRobert Noland 5681d3aed33SMarcel Moolenaar if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 5691d3aed33SMarcel Moolenaar return (0); 5701d3aed33SMarcel Moolenaar return ((pri->hdr_revision == sec->hdr_revision && 5711d3aed33SMarcel Moolenaar pri->hdr_size == sec->hdr_size && 5721d3aed33SMarcel Moolenaar pri->hdr_lba_start == sec->hdr_lba_start && 5731d3aed33SMarcel Moolenaar pri->hdr_lba_end == sec->hdr_lba_end && 5741d3aed33SMarcel Moolenaar pri->hdr_entries == sec->hdr_entries && 5751d3aed33SMarcel Moolenaar pri->hdr_entsz == sec->hdr_entsz && 5761d3aed33SMarcel Moolenaar pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 5771d3aed33SMarcel Moolenaar } 5781d3aed33SMarcel Moolenaar 5791d3aed33SMarcel Moolenaar static int 5801d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid) 5811d3aed33SMarcel Moolenaar { 5821d3aed33SMarcel Moolenaar struct uuid tmp; 583d287f590SMarcel Moolenaar const char *alias; 5841d3aed33SMarcel Moolenaar int error; 585f1317430SRui Paulo struct g_part_uuid_alias *uap; 5861d3aed33SMarcel Moolenaar 587d287f590SMarcel Moolenaar if (type[0] == '!') { 588d287f590SMarcel Moolenaar error = parse_uuid(type + 1, &tmp); 5891d3aed33SMarcel Moolenaar if (error) 5901d3aed33SMarcel Moolenaar return (error); 5911d3aed33SMarcel Moolenaar if (EQUUID(&tmp, &gpt_uuid_unused)) 5921d3aed33SMarcel Moolenaar return (EINVAL); 5931d3aed33SMarcel Moolenaar *uuid = tmp; 5941d3aed33SMarcel Moolenaar return (0); 5951d3aed33SMarcel Moolenaar } 596f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 597f1317430SRui Paulo alias = g_part_alias_name(uap->alias); 598d287f590SMarcel Moolenaar if (!strcasecmp(type, alias)) { 599665bb830SMarcel Moolenaar *uuid = *uap->uuid; 6001d3aed33SMarcel Moolenaar return (0); 6011d3aed33SMarcel Moolenaar } 602d7255ff4SRui Paulo } 603d287f590SMarcel Moolenaar return (EINVAL); 604d287f590SMarcel Moolenaar } 6051d3aed33SMarcel Moolenaar 6061d3aed33SMarcel Moolenaar static int 6071d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 6081d3aed33SMarcel Moolenaar struct g_part_parms *gpp) 6091d3aed33SMarcel Moolenaar { 6101d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 6111d3aed33SMarcel Moolenaar int error; 6121d3aed33SMarcel Moolenaar 6131d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 6141d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 6151d3aed33SMarcel Moolenaar if (error) 6161d3aed33SMarcel Moolenaar return (error); 6171d3aed33SMarcel Moolenaar kern_uuidgen(&entry->ent.ent_uuid, 1); 6181d3aed33SMarcel Moolenaar entry->ent.ent_lba_start = baseentry->gpe_start; 6191d3aed33SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 6201d3aed33SMarcel Moolenaar if (baseentry->gpe_deleted) { 6211d3aed33SMarcel Moolenaar entry->ent.ent_attr = 0; 6221d3aed33SMarcel Moolenaar bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 6231d3aed33SMarcel Moolenaar } 624d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 625d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 626ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 627ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 6281d3aed33SMarcel Moolenaar return (0); 6291d3aed33SMarcel Moolenaar } 6301d3aed33SMarcel Moolenaar 6311d3aed33SMarcel Moolenaar static int 6324d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 6334d32fcb4SMarcel Moolenaar { 6344d32fcb4SMarcel Moolenaar struct g_part_gpt_table *table; 63566477112SMarcel Moolenaar size_t codesz; 6364d32fcb4SMarcel Moolenaar 63766477112SMarcel Moolenaar codesz = DOSPARTOFF; 6384d32fcb4SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 63966477112SMarcel Moolenaar bzero(table->mbr, codesz); 64066477112SMarcel Moolenaar codesz = MIN(codesz, gpp->gpp_codesize); 64166477112SMarcel Moolenaar if (codesz > 0) 64266477112SMarcel Moolenaar bcopy(gpp->gpp_codeptr, table->mbr, codesz); 6434d32fcb4SMarcel Moolenaar return (0); 6444d32fcb4SMarcel Moolenaar } 6454d32fcb4SMarcel Moolenaar 6464d32fcb4SMarcel Moolenaar static int 6471d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 6481d3aed33SMarcel Moolenaar { 6491d3aed33SMarcel Moolenaar struct g_provider *pp; 6501d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 6511d3aed33SMarcel Moolenaar size_t tblsz; 6521d3aed33SMarcel Moolenaar 6532a1c00ffSMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 6542a1c00ffSMarcel Moolenaar if (basetable->gpt_depth != 0) 6552a1c00ffSMarcel Moolenaar return (ENXIO); 6562a1c00ffSMarcel Moolenaar 6571d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 6581d3aed33SMarcel Moolenaar pp = gpp->gpp_provider; 65955e0987aSPedro F. Giffuni tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 66055e0987aSPedro F. Giffuni pp->sectorsize); 6614d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || 6621d3aed33SMarcel Moolenaar pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 6631d3aed33SMarcel Moolenaar pp->sectorsize) 6641d3aed33SMarcel Moolenaar return (ENOSPC); 6651d3aed33SMarcel Moolenaar 6663bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 6673bd22a9cSMarcel Moolenaar 668a59a1310SRobert Noland /* Allocate space for the header */ 669a59a1310SRobert Noland table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 670a59a1310SRobert Noland 6711c2dee3cSRobert Noland bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 6721c2dee3cSRobert Noland table->hdr->hdr_revision = GPT_HDR_REVISION; 6731c2dee3cSRobert Noland table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 6741c2dee3cSRobert Noland kern_uuidgen(&table->hdr->hdr_uuid, 1); 6751c2dee3cSRobert Noland table->hdr->hdr_entries = basetable->gpt_entries; 6761c2dee3cSRobert Noland table->hdr->hdr_entsz = sizeof(struct gpt_ent); 6771d3aed33SMarcel Moolenaar 6782b9be055SAndrey V. Elsukov g_gpt_set_defaults(basetable, pp); 6791d3aed33SMarcel Moolenaar return (0); 6801d3aed33SMarcel Moolenaar } 6811d3aed33SMarcel Moolenaar 6821d3aed33SMarcel Moolenaar static int 6831d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 6841d3aed33SMarcel Moolenaar { 68536066952SMarius Strobl struct g_part_gpt_table *table; 686e7926a37SAndrey V. Elsukov struct g_provider *pp; 68736066952SMarius Strobl 68836066952SMarius Strobl table = (struct g_part_gpt_table *)basetable; 689e7926a37SAndrey V. Elsukov pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 69036066952SMarius Strobl g_free(table->hdr); 69136066952SMarius Strobl table->hdr = NULL; 6921d3aed33SMarcel Moolenaar 6931d3aed33SMarcel Moolenaar /* 6949ef60043SAndrey V. Elsukov * Wipe the first 2 sectors and last one to clear the partitioning. 6959ef60043SAndrey V. Elsukov * Wipe sectors only if they have valid metadata. 6961d3aed33SMarcel Moolenaar */ 6979ef60043SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) 6981d3aed33SMarcel Moolenaar basetable->gpt_smhead |= 3; 699e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 700e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 7011d3aed33SMarcel Moolenaar basetable->gpt_smtail |= 1; 7021d3aed33SMarcel Moolenaar return (0); 7031d3aed33SMarcel Moolenaar } 7041d3aed33SMarcel Moolenaar 70551f53a08SWarner Losh static void 7065db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 7075db67052SMarcel Moolenaar struct sbuf *sb, const char *indent) 7085db67052SMarcel Moolenaar { 7095db67052SMarcel Moolenaar struct g_part_gpt_entry *entry; 7105db67052SMarcel Moolenaar 7115db67052SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7120c132595SMarcel Moolenaar if (indent == NULL) { 7130c132595SMarcel Moolenaar /* conftxt: libdisk compatibility */ 7145db67052SMarcel Moolenaar sbuf_printf(sb, " xs GPT xt "); 7155db67052SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 7160c132595SMarcel Moolenaar } else if (entry != NULL) { 7170c132595SMarcel Moolenaar /* confxml: partition entry information */ 718d3532631SMarcel Moolenaar sbuf_printf(sb, "%s<label>", indent); 719d3532631SMarcel Moolenaar g_gpt_printf_utf16(sb, entry->ent.ent_name, 720d3532631SMarcel Moolenaar sizeof(entry->ent.ent_name) >> 1); 721d3532631SMarcel Moolenaar sbuf_printf(sb, "</label>\n"); 722cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 723cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 724cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 725cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 726cec283baSPawel Jakub Dawidek indent); 727cec283baSPawel Jakub Dawidek } 728cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 729cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 730cec283baSPawel Jakub Dawidek indent); 731cec283baSPawel Jakub Dawidek } 7320c132595SMarcel Moolenaar sbuf_printf(sb, "%s<rawtype>", indent); 7330c132595SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 7340c132595SMarcel Moolenaar sbuf_printf(sb, "</rawtype>\n"); 735cd18ad83SMarcel Moolenaar sbuf_printf(sb, "%s<rawuuid>", indent); 736cd18ad83SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 737cd18ad83SMarcel Moolenaar sbuf_printf(sb, "</rawuuid>\n"); 738ab4effdcSWarner Losh sbuf_printf(sb, "%s<efimedia>", indent); 739ab4effdcSWarner Losh sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index); 740ab4effdcSWarner Losh sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 741ab4effdcSWarner Losh sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start, 742a905e396SWarner Losh (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1)); 743ab4effdcSWarner Losh sbuf_printf(sb, "</efimedia>\n"); 7440c132595SMarcel Moolenaar } else { 7450c132595SMarcel Moolenaar /* confxml: scheme information */ 7460c132595SMarcel Moolenaar } 7475db67052SMarcel Moolenaar } 7485db67052SMarcel Moolenaar 7495db67052SMarcel Moolenaar static int 7501d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 7511d3aed33SMarcel Moolenaar { 7521d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7531d3aed33SMarcel Moolenaar 7541d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7551d3aed33SMarcel Moolenaar return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 7564042ab48SAndrey V. Elsukov EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) || 7574042ab48SAndrey V. Elsukov EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0); 7581d3aed33SMarcel Moolenaar } 7591d3aed33SMarcel Moolenaar 7601d3aed33SMarcel Moolenaar static int 7611d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable, 7621d3aed33SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7631d3aed33SMarcel Moolenaar { 7641d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7651d3aed33SMarcel Moolenaar int error; 7661d3aed33SMarcel Moolenaar 7671d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 768d287f590SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_TYPE) { 7691d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 7701d3aed33SMarcel Moolenaar if (error) 7711d3aed33SMarcel Moolenaar return (error); 772d287f590SMarcel Moolenaar } 773d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 774d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 775ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 776ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 7771d3aed33SMarcel Moolenaar return (0); 7781d3aed33SMarcel Moolenaar } 7791d3aed33SMarcel Moolenaar 7803f71c319SMarcel Moolenaar static int 7813f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable, 7823f71c319SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7833f71c319SMarcel Moolenaar { 7843f71c319SMarcel Moolenaar struct g_part_gpt_entry *entry; 7853f71c319SMarcel Moolenaar 786884c8e4fSAndrey V. Elsukov if (baseentry == NULL) 787e7d0c7e4SAndrey V. Elsukov return (g_part_gpt_recover(basetable)); 788884c8e4fSAndrey V. Elsukov 789884c8e4fSAndrey V. Elsukov entry = (struct g_part_gpt_entry *)baseentry; 7903f71c319SMarcel Moolenaar baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 7913f71c319SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 7923f71c319SMarcel Moolenaar 7933f71c319SMarcel Moolenaar return (0); 7943f71c319SMarcel Moolenaar } 7953f71c319SMarcel Moolenaar 79651f53a08SWarner Losh static const char * 7971d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 7981d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 7991d3aed33SMarcel Moolenaar { 8001d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8011d3aed33SMarcel Moolenaar char c; 8021d3aed33SMarcel Moolenaar 8031d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 8041d3aed33SMarcel Moolenaar c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 8051d3aed33SMarcel Moolenaar snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 8061d3aed33SMarcel Moolenaar return (buf); 8071d3aed33SMarcel Moolenaar } 8081d3aed33SMarcel Moolenaar 8091d3aed33SMarcel Moolenaar static int 8101d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 8111d3aed33SMarcel Moolenaar { 8121d3aed33SMarcel Moolenaar struct g_provider *pp; 8133e5a0a6bSMarcel Moolenaar u_char *buf; 8143e5a0a6bSMarcel Moolenaar int error, index, pri, res; 8151d3aed33SMarcel Moolenaar 8161d3aed33SMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 8171d3aed33SMarcel Moolenaar if (table->gpt_depth != 0) 8181d3aed33SMarcel Moolenaar return (ENXIO); 8191d3aed33SMarcel Moolenaar 8201d3aed33SMarcel Moolenaar pp = cp->provider; 8211d3aed33SMarcel Moolenaar 8221d3aed33SMarcel Moolenaar /* 8231d3aed33SMarcel Moolenaar * Sanity-check the provider. Since the first sector on the provider 8241d3aed33SMarcel Moolenaar * must be a PMBR and a PMBR is 512 bytes large, the sector size 8251d3aed33SMarcel Moolenaar * must be at least 512 bytes. Also, since the theoretical minimum 8261d3aed33SMarcel Moolenaar * number of sectors needed by GPT is 6, any medium that has less 8271d3aed33SMarcel Moolenaar * than 6 sectors is never going to be able to hold a GPT. The 8281d3aed33SMarcel Moolenaar * number 6 comes from: 8291d3aed33SMarcel Moolenaar * 1 sector for the PMBR 8301d3aed33SMarcel Moolenaar * 2 sectors for the GPT headers (each 1 sector) 8311d3aed33SMarcel Moolenaar * 2 sectors for the GPT tables (each 1 sector) 8321d3aed33SMarcel Moolenaar * 1 sector for an actual partition 8331d3aed33SMarcel Moolenaar * It's better to catch this pathological case early than behaving 8341d3aed33SMarcel Moolenaar * pathologically later on... 8351d3aed33SMarcel Moolenaar */ 8364d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 8371d3aed33SMarcel Moolenaar return (ENOSPC); 8381d3aed33SMarcel Moolenaar 8393e5a0a6bSMarcel Moolenaar /* 8403e5a0a6bSMarcel Moolenaar * Check that there's a MBR or a PMBR. If it's a PMBR, we return 8413e5a0a6bSMarcel Moolenaar * as the highest priority on a match, otherwise we assume some 8423e5a0a6bSMarcel Moolenaar * GPT-unaware tool has destroyed the GPT by recreating a MBR and 8433e5a0a6bSMarcel Moolenaar * we really want the MBR scheme to take precedence. 8443e5a0a6bSMarcel Moolenaar */ 8451d3aed33SMarcel Moolenaar buf = g_read_data(cp, 0L, pp->sectorsize, &error); 8461d3aed33SMarcel Moolenaar if (buf == NULL) 8471d3aed33SMarcel Moolenaar return (error); 8481d3aed33SMarcel Moolenaar res = le16dec(buf + DOSMAGICOFFSET); 8493e5a0a6bSMarcel Moolenaar pri = G_PART_PROBE_PRI_LOW; 850af90a872SAndrey V. Elsukov if (res == DOSMAGIC) { 8513e5a0a6bSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 8523e5a0a6bSMarcel Moolenaar if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee) 8533e5a0a6bSMarcel Moolenaar pri = G_PART_PROBE_PRI_HIGH; 8543e5a0a6bSMarcel Moolenaar } 8551d3aed33SMarcel Moolenaar g_free(buf); 8561d3aed33SMarcel Moolenaar 8571d3aed33SMarcel Moolenaar /* Check that there's a primary header. */ 8581d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 8591d3aed33SMarcel Moolenaar if (buf == NULL) 8601d3aed33SMarcel Moolenaar return (error); 8611d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8621d3aed33SMarcel Moolenaar g_free(buf); 8631d3aed33SMarcel Moolenaar if (res == 0) 8643e5a0a6bSMarcel Moolenaar return (pri); 865af90a872SAndrey V. Elsukov } else 866af90a872SAndrey V. Elsukov g_free(buf); 8671d3aed33SMarcel Moolenaar 8681d3aed33SMarcel Moolenaar /* No primary? Check that there's a secondary. */ 8691d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 8701d3aed33SMarcel Moolenaar &error); 8711d3aed33SMarcel Moolenaar if (buf == NULL) 8721d3aed33SMarcel Moolenaar return (error); 8731d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8741d3aed33SMarcel Moolenaar g_free(buf); 8753e5a0a6bSMarcel Moolenaar return ((res == 0) ? pri : ENXIO); 8761d3aed33SMarcel Moolenaar } 8771d3aed33SMarcel Moolenaar 8781d3aed33SMarcel Moolenaar static int 8791d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 8801d3aed33SMarcel Moolenaar { 8811c2dee3cSRobert Noland struct gpt_hdr *prihdr, *sechdr; 8821d3aed33SMarcel Moolenaar struct gpt_ent *tbl, *pritbl, *sectbl; 8831d3aed33SMarcel Moolenaar struct g_provider *pp; 8841d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 8851d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8864d32fcb4SMarcel Moolenaar u_char *buf; 887e7926a37SAndrey V. Elsukov uint64_t last; 8884d32fcb4SMarcel Moolenaar int error, index; 8891d3aed33SMarcel Moolenaar 8901d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 8911d3aed33SMarcel Moolenaar pp = cp->provider; 892e7926a37SAndrey V. Elsukov last = (pp->mediasize / pp->sectorsize) - 1; 8931d3aed33SMarcel Moolenaar 8944d32fcb4SMarcel Moolenaar /* Read the PMBR */ 8954d32fcb4SMarcel Moolenaar buf = g_read_data(cp, 0, pp->sectorsize, &error); 8964d32fcb4SMarcel Moolenaar if (buf == NULL) 8974d32fcb4SMarcel Moolenaar return (error); 8984d32fcb4SMarcel Moolenaar bcopy(buf, table->mbr, MBRSIZE); 8994d32fcb4SMarcel Moolenaar g_free(buf); 9004d32fcb4SMarcel Moolenaar 9011d3aed33SMarcel Moolenaar /* Read the primary header and table. */ 9021c2dee3cSRobert Noland prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 9031d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 9041c2dee3cSRobert Noland pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 9051d3aed33SMarcel Moolenaar } else { 9061d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 9071d3aed33SMarcel Moolenaar pritbl = NULL; 9081d3aed33SMarcel Moolenaar } 9091d3aed33SMarcel Moolenaar 9101d3aed33SMarcel Moolenaar /* Read the secondary header and table. */ 9111c2dee3cSRobert Noland sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 9121d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 9131c2dee3cSRobert Noland sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 9141d3aed33SMarcel Moolenaar } else { 9151d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 9161d3aed33SMarcel Moolenaar sectbl = NULL; 9171d3aed33SMarcel Moolenaar } 9181d3aed33SMarcel Moolenaar 9191d3aed33SMarcel Moolenaar /* Fail if we haven't got any good tables at all. */ 9201d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 9211d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 9221d3aed33SMarcel Moolenaar printf("GEOM: %s: corrupt or invalid GPT detected.\n", 9231d3aed33SMarcel Moolenaar pp->name); 9241d3aed33SMarcel Moolenaar printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 9251d3aed33SMarcel Moolenaar pp->name); 926*ee4d316fSConrad Meyer if (prihdr != NULL) 927*ee4d316fSConrad Meyer g_free(prihdr); 928*ee4d316fSConrad Meyer if (pritbl != NULL) 929*ee4d316fSConrad Meyer g_free(pritbl); 930*ee4d316fSConrad Meyer if (sechdr != NULL) 931*ee4d316fSConrad Meyer g_free(sechdr); 932*ee4d316fSConrad Meyer if (sectbl != NULL) 933*ee4d316fSConrad Meyer g_free(sectbl); 9341d3aed33SMarcel Moolenaar return (EINVAL); 9351d3aed33SMarcel Moolenaar } 9361d3aed33SMarcel Moolenaar 9371d3aed33SMarcel Moolenaar /* 9381d3aed33SMarcel Moolenaar * If both headers are good but they disagree with each other, 9391d3aed33SMarcel Moolenaar * then invalidate one. We prefer to keep the primary header, 9401d3aed33SMarcel Moolenaar * unless the primary table is corrupt. 9411d3aed33SMarcel Moolenaar */ 9421d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 9431d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 9441c2dee3cSRobert Noland !gpt_matched_hdrs(prihdr, sechdr)) { 945dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 9461d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 947dd0db05dSMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 9481c2dee3cSRobert Noland g_free(sechdr); 9491c2dee3cSRobert Noland sechdr = NULL; 950dd0db05dSMarcel Moolenaar } else { 9511d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 952dd0db05dSMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 9531c2dee3cSRobert Noland g_free(prihdr); 9541c2dee3cSRobert Noland prihdr = NULL; 955dd0db05dSMarcel Moolenaar } 9561d3aed33SMarcel Moolenaar } 9571d3aed33SMarcel Moolenaar 958dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 9591d3aed33SMarcel Moolenaar printf("GEOM: %s: the primary GPT table is corrupt or " 9601d3aed33SMarcel Moolenaar "invalid.\n", pp->name); 9611d3aed33SMarcel Moolenaar printf("GEOM: %s: using the secondary instead -- recovery " 9621d3aed33SMarcel Moolenaar "strongly advised.\n", pp->name); 9631d3aed33SMarcel Moolenaar table->hdr = sechdr; 964e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9651c2dee3cSRobert Noland if (prihdr != NULL) 9661c2dee3cSRobert Noland g_free(prihdr); 9671d3aed33SMarcel Moolenaar tbl = sectbl; 9681d3aed33SMarcel Moolenaar if (pritbl != NULL) 9691d3aed33SMarcel Moolenaar g_free(pritbl); 9701d3aed33SMarcel Moolenaar } else { 971dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 9721d3aed33SMarcel Moolenaar printf("GEOM: %s: the secondary GPT table is corrupt " 9731d3aed33SMarcel Moolenaar "or invalid.\n", pp->name); 9741d3aed33SMarcel Moolenaar printf("GEOM: %s: using the primary only -- recovery " 9751d3aed33SMarcel Moolenaar "suggested.\n", pp->name); 976e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 977e7926a37SAndrey V. Elsukov } else if (table->lba[GPT_ELT_SECHDR] != last) { 978e7926a37SAndrey V. Elsukov printf( "GEOM: %s: the secondary GPT header is not in " 979e7926a37SAndrey V. Elsukov "the last LBA.\n", pp->name); 980e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9811d3aed33SMarcel Moolenaar } 9821d3aed33SMarcel Moolenaar table->hdr = prihdr; 9831c2dee3cSRobert Noland if (sechdr != NULL) 9841c2dee3cSRobert Noland g_free(sechdr); 9851d3aed33SMarcel Moolenaar tbl = pritbl; 9861d3aed33SMarcel Moolenaar if (sectbl != NULL) 9871d3aed33SMarcel Moolenaar g_free(sectbl); 9881d3aed33SMarcel Moolenaar } 9891d3aed33SMarcel Moolenaar 9901c2dee3cSRobert Noland basetable->gpt_first = table->hdr->hdr_lba_start; 9911c2dee3cSRobert Noland basetable->gpt_last = table->hdr->hdr_lba_end; 992b74dd6c7SAndrey V. Elsukov basetable->gpt_entries = (table->hdr->hdr_lba_start - 2) * 993b74dd6c7SAndrey V. Elsukov pp->sectorsize / table->hdr->hdr_entsz; 9941d3aed33SMarcel Moolenaar 995b74dd6c7SAndrey V. Elsukov for (index = table->hdr->hdr_entries - 1; index >= 0; index--) { 9961d3aed33SMarcel Moolenaar if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 9971d3aed33SMarcel Moolenaar continue; 998e7926a37SAndrey V. Elsukov entry = (struct g_part_gpt_entry *)g_part_new_entry( 999e7926a37SAndrey V. Elsukov basetable, index + 1, tbl[index].ent_lba_start, 1000e7926a37SAndrey V. Elsukov tbl[index].ent_lba_end); 10011d3aed33SMarcel Moolenaar entry->ent = tbl[index]; 10021d3aed33SMarcel Moolenaar } 10031d3aed33SMarcel Moolenaar 10041d3aed33SMarcel Moolenaar g_free(tbl); 1005369fe59dSMarcel Moolenaar 1006369fe59dSMarcel Moolenaar /* 1007369fe59dSMarcel Moolenaar * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 1008369fe59dSMarcel Moolenaar * if (and only if) any FAT32 or FAT16 partitions have been 1009369fe59dSMarcel Moolenaar * created. This happens irrespective of whether Boot Camp is 1010369fe59dSMarcel Moolenaar * used/enabled, though it's generally understood to be done 1011369fe59dSMarcel Moolenaar * to support legacy Windows under Boot Camp. We refer to this 1012369fe59dSMarcel Moolenaar * mirroring simply as Boot Camp. We try to detect Boot Camp 1013369fe59dSMarcel Moolenaar * so that we can update the MBR if and when GPT changes have 1014369fe59dSMarcel Moolenaar * been made. Note that we do not enable Boot Camp if not 1015369fe59dSMarcel Moolenaar * previously enabled because we can't assume that we're on a 1016369fe59dSMarcel Moolenaar * Mac alongside Mac OS X. 1017369fe59dSMarcel Moolenaar */ 1018369fe59dSMarcel Moolenaar table->bootcamp = gpt_is_bootcamp(table, pp->name); 1019369fe59dSMarcel Moolenaar 10201d3aed33SMarcel Moolenaar return (0); 10211d3aed33SMarcel Moolenaar } 10221d3aed33SMarcel Moolenaar 1023cec283baSPawel Jakub Dawidek static int 1024e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable) 1025e7926a37SAndrey V. Elsukov { 10263bd22a9cSMarcel Moolenaar struct g_part_gpt_table *table; 10273bd22a9cSMarcel Moolenaar struct g_provider *pp; 1028e7926a37SAndrey V. Elsukov 10293bd22a9cSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 10303bd22a9cSMarcel Moolenaar pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 10313bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 10323bd22a9cSMarcel Moolenaar g_gpt_set_defaults(basetable, pp); 1033e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 0; 1034e7926a37SAndrey V. Elsukov return (0); 1035e7926a37SAndrey V. Elsukov } 1036e7926a37SAndrey V. Elsukov 1037e7926a37SAndrey V. Elsukov static int 1038369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable, 1039369fe59dSMarcel Moolenaar struct g_part_entry *baseentry, const char *attrib, unsigned int set) 1040cec283baSPawel Jakub Dawidek { 1041cec283baSPawel Jakub Dawidek struct g_part_gpt_entry *entry; 1042369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 1043ce808c7aSAllan Jude struct g_provider *pp; 10443bd22a9cSMarcel Moolenaar uint8_t *p; 1045369fe59dSMarcel Moolenaar uint64_t attr; 1046369fe59dSMarcel Moolenaar int i; 1047cec283baSPawel Jakub Dawidek 1048369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 1049369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 1050369fe59dSMarcel Moolenaar 1051369fe59dSMarcel Moolenaar if (strcasecmp(attrib, "active") == 0) { 10523bd22a9cSMarcel Moolenaar if (table->bootcamp) { 10533bd22a9cSMarcel Moolenaar /* The active flag must be set on a valid entry. */ 10543bd22a9cSMarcel Moolenaar if (entry == NULL) 10553bd22a9cSMarcel Moolenaar return (ENXIO); 10563bd22a9cSMarcel Moolenaar if (baseentry->gpe_index > NDOSPART) 1057369fe59dSMarcel Moolenaar return (EINVAL); 1058369fe59dSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 10593bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 10603bd22a9cSMarcel Moolenaar p[0] = (i == baseentry->gpe_index - 1) 10613bd22a9cSMarcel Moolenaar ? ((set) ? 0x80 : 0) : 0; 10623bd22a9cSMarcel Moolenaar } 10633bd22a9cSMarcel Moolenaar } else { 10643bd22a9cSMarcel Moolenaar /* The PMBR is marked as active without an entry. */ 10653bd22a9cSMarcel Moolenaar if (entry != NULL) 10663bd22a9cSMarcel Moolenaar return (ENXIO); 10673bd22a9cSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 10683bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 10693bd22a9cSMarcel Moolenaar p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 10703bd22a9cSMarcel Moolenaar } 1071369fe59dSMarcel Moolenaar } 1072369fe59dSMarcel Moolenaar return (0); 1073ce808c7aSAllan Jude } else if (strcasecmp(attrib, "lenovofix") == 0) { 1074ce808c7aSAllan Jude /* 1075ce808c7aSAllan Jude * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR. 1076ce808c7aSAllan Jude * This workaround allows Lenovo X220, T420, T520, etc to boot 1077ce808c7aSAllan Jude * from GPT Partitions in BIOS mode. 1078ce808c7aSAllan Jude */ 1079ce808c7aSAllan Jude 1080ce808c7aSAllan Jude if (entry != NULL) 1081ce808c7aSAllan Jude return (ENXIO); 1082ce808c7aSAllan Jude 1083ce808c7aSAllan Jude pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 1084ce808c7aSAllan Jude bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 1085ce808c7aSAllan Jude gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1, 1086ce808c7aSAllan Jude MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 1087ce808c7aSAllan Jude return (0); 1088369fe59dSMarcel Moolenaar } 1089369fe59dSMarcel Moolenaar 10903bd22a9cSMarcel Moolenaar if (entry == NULL) 10913bd22a9cSMarcel Moolenaar return (ENODEV); 10923bd22a9cSMarcel Moolenaar 1093369fe59dSMarcel Moolenaar attr = 0; 1094cec283baSPawel Jakub Dawidek if (strcasecmp(attrib, "bootme") == 0) { 1095369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1096cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootonce") == 0) { 1097369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTONCE; 1098cec283baSPawel Jakub Dawidek if (set) 1099369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1100cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootfailed") == 0) { 1101cec283baSPawel Jakub Dawidek /* 1102cec283baSPawel Jakub Dawidek * It should only be possible to unset BOOTFAILED, but it might 1103cec283baSPawel Jakub Dawidek * be useful for test purposes to also be able to set it. 1104cec283baSPawel Jakub Dawidek */ 1105369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTFAILED; 1106cec283baSPawel Jakub Dawidek } 1107369fe59dSMarcel Moolenaar if (attr == 0) 1108cec283baSPawel Jakub Dawidek return (EINVAL); 1109cec283baSPawel Jakub Dawidek 1110369fe59dSMarcel Moolenaar if (set) 1111369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr | attr; 1112369fe59dSMarcel Moolenaar else 1113369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr & ~attr; 1114369fe59dSMarcel Moolenaar if (attr != entry->ent.ent_attr) { 1115369fe59dSMarcel Moolenaar entry->ent.ent_attr = attr; 1116369fe59dSMarcel Moolenaar if (!baseentry->gpe_created) 1117369fe59dSMarcel Moolenaar baseentry->gpe_modified = 1; 1118cec283baSPawel Jakub Dawidek } 1119cec283baSPawel Jakub Dawidek return (0); 1120cec283baSPawel Jakub Dawidek } 1121cec283baSPawel Jakub Dawidek 11221d3aed33SMarcel Moolenaar static const char * 11231d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 11241d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 11251d3aed33SMarcel Moolenaar { 11261d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 11271d3aed33SMarcel Moolenaar struct uuid *type; 1128f1317430SRui Paulo struct g_part_uuid_alias *uap; 11291d3aed33SMarcel Moolenaar 11301d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 11311d3aed33SMarcel Moolenaar type = &entry->ent.ent_type; 1132f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1133f1317430SRui Paulo if (EQUUID(type, uap->uuid)) 1134f1317430SRui Paulo return (g_part_alias_name(uap->alias)); 1135cf231470SMarcel Moolenaar buf[0] = '!'; 1136cf231470SMarcel Moolenaar snprintf_uuid(buf + 1, bufsz - 1, type); 1137f1317430SRui Paulo 11381d3aed33SMarcel Moolenaar return (buf); 11391d3aed33SMarcel Moolenaar } 11401d3aed33SMarcel Moolenaar 11411d3aed33SMarcel Moolenaar static int 11421d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 11431d3aed33SMarcel Moolenaar { 11441d3aed33SMarcel Moolenaar unsigned char *buf, *bp; 11451d3aed33SMarcel Moolenaar struct g_provider *pp; 11461d3aed33SMarcel Moolenaar struct g_part_entry *baseentry; 11471d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 11481d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 1149e7926a37SAndrey V. Elsukov size_t tblsz; 11501d3aed33SMarcel Moolenaar uint32_t crc; 11511d3aed33SMarcel Moolenaar int error, index; 11521d3aed33SMarcel Moolenaar 11531d3aed33SMarcel Moolenaar pp = cp->provider; 11541d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 115555e0987aSPedro F. Giffuni tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz, 115655e0987aSPedro F. Giffuni pp->sectorsize); 11571d3aed33SMarcel Moolenaar 1158369fe59dSMarcel Moolenaar /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1159369fe59dSMarcel Moolenaar if (table->bootcamp) 11603bd22a9cSMarcel Moolenaar gpt_update_bootcamp(basetable, pp); 1161d4746e10SAndrey V. Elsukov 11624d32fcb4SMarcel Moolenaar /* Write the PMBR */ 11631d3aed33SMarcel Moolenaar buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 11644d32fcb4SMarcel Moolenaar bcopy(table->mbr, buf, MBRSIZE); 11651d3aed33SMarcel Moolenaar error = g_write_data(cp, 0, buf, pp->sectorsize); 11661d3aed33SMarcel Moolenaar g_free(buf); 11671d3aed33SMarcel Moolenaar if (error) 11681d3aed33SMarcel Moolenaar return (error); 11691d3aed33SMarcel Moolenaar 11701d3aed33SMarcel Moolenaar /* Allocate space for the header and entries. */ 1171e7926a37SAndrey V. Elsukov buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 11721d3aed33SMarcel Moolenaar 11731c2dee3cSRobert Noland memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 11741c2dee3cSRobert Noland le32enc(buf + 8, table->hdr->hdr_revision); 11751c2dee3cSRobert Noland le32enc(buf + 12, table->hdr->hdr_size); 11761c2dee3cSRobert Noland le64enc(buf + 40, table->hdr->hdr_lba_start); 11771c2dee3cSRobert Noland le64enc(buf + 48, table->hdr->hdr_lba_end); 11781c2dee3cSRobert Noland le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 11791c2dee3cSRobert Noland le32enc(buf + 80, table->hdr->hdr_entries); 11801c2dee3cSRobert Noland le32enc(buf + 84, table->hdr->hdr_entsz); 11811d3aed33SMarcel Moolenaar 11821d3aed33SMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1183d287f590SMarcel Moolenaar if (baseentry->gpe_deleted) 1184d287f590SMarcel Moolenaar continue; 11851d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 11861d3aed33SMarcel Moolenaar index = baseentry->gpe_index - 1; 11871c2dee3cSRobert Noland bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 11881d3aed33SMarcel Moolenaar le_uuid_enc(bp, &entry->ent.ent_type); 11891d3aed33SMarcel Moolenaar le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 11901d3aed33SMarcel Moolenaar le64enc(bp + 32, entry->ent.ent_lba_start); 11911d3aed33SMarcel Moolenaar le64enc(bp + 40, entry->ent.ent_lba_end); 11921d3aed33SMarcel Moolenaar le64enc(bp + 48, entry->ent.ent_attr); 11931d3aed33SMarcel Moolenaar memcpy(bp + 56, entry->ent.ent_name, 11941d3aed33SMarcel Moolenaar sizeof(entry->ent.ent_name)); 11951d3aed33SMarcel Moolenaar } 11961d3aed33SMarcel Moolenaar 11971d3aed33SMarcel Moolenaar crc = crc32(buf + pp->sectorsize, 11981c2dee3cSRobert Noland table->hdr->hdr_entries * table->hdr->hdr_entsz); 11991d3aed33SMarcel Moolenaar le32enc(buf + 88, crc); 12001d3aed33SMarcel Moolenaar 12011d3aed33SMarcel Moolenaar /* Write primary meta-data. */ 12021d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 12031d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 12041d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 12051d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 12061c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 12071d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 12081d3aed33SMarcel Moolenaar 1209799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1210799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1211799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1212799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1213799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1214799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 12151d3aed33SMarcel Moolenaar if (error) 12161d3aed33SMarcel Moolenaar goto out; 1217799eac8cSAndrey V. Elsukov } 12181d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 12191d3aed33SMarcel Moolenaar buf, pp->sectorsize); 12201d3aed33SMarcel Moolenaar if (error) 12211d3aed33SMarcel Moolenaar goto out; 12221d3aed33SMarcel Moolenaar 12231d3aed33SMarcel Moolenaar /* Write secondary meta-data. */ 12241d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 12251d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 12261d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 12271d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 12281c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 12291d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 12301d3aed33SMarcel Moolenaar 1231799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1232799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1233799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1234799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1235799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1236799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 12371d3aed33SMarcel Moolenaar if (error) 12381d3aed33SMarcel Moolenaar goto out; 1239799eac8cSAndrey V. Elsukov } 12401d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 12411d3aed33SMarcel Moolenaar buf, pp->sectorsize); 12421d3aed33SMarcel Moolenaar 12431d3aed33SMarcel Moolenaar out: 12441d3aed33SMarcel Moolenaar g_free(buf); 12451d3aed33SMarcel Moolenaar return (error); 12461d3aed33SMarcel Moolenaar } 12471d3aed33SMarcel Moolenaar 12481d3aed33SMarcel Moolenaar static void 12492b9be055SAndrey V. Elsukov g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 12502b9be055SAndrey V. Elsukov { 12514787115dSMarcel Moolenaar struct g_part_entry *baseentry; 12524787115dSMarcel Moolenaar struct g_part_gpt_entry *entry; 12532b9be055SAndrey V. Elsukov struct g_part_gpt_table *table; 12544787115dSMarcel Moolenaar quad_t start, end, min, max; 12554787115dSMarcel Moolenaar quad_t lba, last; 12564787115dSMarcel Moolenaar size_t spb, tblsz; 12572b9be055SAndrey V. Elsukov 12582b9be055SAndrey V. Elsukov table = (struct g_part_gpt_table *)basetable; 12592b9be055SAndrey V. Elsukov last = pp->mediasize / pp->sectorsize - 1; 126055e0987aSPedro F. Giffuni tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent), 126155e0987aSPedro F. Giffuni pp->sectorsize); 12622b9be055SAndrey V. Elsukov 12632b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRIHDR] = 1; 12642b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRITBL] = 2; 12652b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = last; 12662b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECTBL] = last - tblsz; 12672b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 12682b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 12692b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 12702b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 12712b9be055SAndrey V. Elsukov 12724787115dSMarcel Moolenaar max = start = 2 + tblsz; 12734787115dSMarcel Moolenaar min = end = last - tblsz - 1; 12744787115dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 12754787115dSMarcel Moolenaar if (baseentry->gpe_deleted) 12764787115dSMarcel Moolenaar continue; 12774787115dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 12784787115dSMarcel Moolenaar if (entry->ent.ent_lba_start < min) 12794787115dSMarcel Moolenaar min = entry->ent.ent_lba_start; 12804787115dSMarcel Moolenaar if (entry->ent.ent_lba_end > max) 12814787115dSMarcel Moolenaar max = entry->ent.ent_lba_end; 12824787115dSMarcel Moolenaar } 12834787115dSMarcel Moolenaar spb = 4096 / pp->sectorsize; 12844787115dSMarcel Moolenaar if (spb > 1) { 12854787115dSMarcel Moolenaar lba = start + ((start % spb) ? spb - start % spb : 0); 12864787115dSMarcel Moolenaar if (lba <= min) 12874787115dSMarcel Moolenaar start = lba; 12884787115dSMarcel Moolenaar lba = end - (end + 1) % spb; 12894787115dSMarcel Moolenaar if (max <= lba) 12904787115dSMarcel Moolenaar end = lba; 12914787115dSMarcel Moolenaar } 12924787115dSMarcel Moolenaar table->hdr->hdr_lba_start = start; 12934787115dSMarcel Moolenaar table->hdr->hdr_lba_end = end; 12942b9be055SAndrey V. Elsukov 12954787115dSMarcel Moolenaar basetable->gpt_first = start; 12964787115dSMarcel Moolenaar basetable->gpt_last = end; 12972b9be055SAndrey V. Elsukov } 12982b9be055SAndrey V. Elsukov 12992b9be055SAndrey V. Elsukov static void 1300d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 13011d3aed33SMarcel Moolenaar { 13021d3aed33SMarcel Moolenaar u_int bo; 13031d3aed33SMarcel Moolenaar uint32_t ch; 13041d3aed33SMarcel Moolenaar uint16_t c; 13051d3aed33SMarcel Moolenaar 1306d3532631SMarcel Moolenaar bo = LITTLE_ENDIAN; /* GPT is little-endian */ 13071d3aed33SMarcel Moolenaar while (len > 0 && *str != 0) { 13081d3aed33SMarcel Moolenaar ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 13091d3aed33SMarcel Moolenaar str++, len--; 13101d3aed33SMarcel Moolenaar if ((ch & 0xf800) == 0xd800) { 13111d3aed33SMarcel Moolenaar if (len > 0) { 13121d3aed33SMarcel Moolenaar c = (bo == BIG_ENDIAN) ? be16toh(*str) 13131d3aed33SMarcel Moolenaar : le16toh(*str); 13141d3aed33SMarcel Moolenaar str++, len--; 13151d3aed33SMarcel Moolenaar } else 13161d3aed33SMarcel Moolenaar c = 0xfffd; 13171d3aed33SMarcel Moolenaar if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 13181d3aed33SMarcel Moolenaar ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 13191d3aed33SMarcel Moolenaar ch += 0x10000; 13201d3aed33SMarcel Moolenaar } else 13211d3aed33SMarcel Moolenaar ch = 0xfffd; 13221d3aed33SMarcel Moolenaar } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 13231d3aed33SMarcel Moolenaar bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 13241d3aed33SMarcel Moolenaar continue; 13251d3aed33SMarcel Moolenaar } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 13261d3aed33SMarcel Moolenaar continue; 13271d3aed33SMarcel Moolenaar 1328d3532631SMarcel Moolenaar /* Write the Unicode character in UTF-8 */ 13291d3aed33SMarcel Moolenaar if (ch < 0x80) 133039dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c", ch); 13311d3aed33SMarcel Moolenaar else if (ch < 0x800) 133239dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6), 13331d3aed33SMarcel Moolenaar 0x80 | (ch & 0x3f)); 13341d3aed33SMarcel Moolenaar else if (ch < 0x10000) 133539dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12), 13361d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 13371d3aed33SMarcel Moolenaar else if (ch < 0x200000) 133839dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 | 133939dcac84SAndrey V. Elsukov (ch >> 18), 0x80 | ((ch >> 12) & 0x3f), 13401d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 13411d3aed33SMarcel Moolenaar } 13421d3aed33SMarcel Moolenaar } 1343d3532631SMarcel Moolenaar 1344d3532631SMarcel Moolenaar static void 1345d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1346d3532631SMarcel Moolenaar { 1347d3532631SMarcel Moolenaar size_t s16idx, s8idx; 1348d3532631SMarcel Moolenaar uint32_t utfchar; 1349d3532631SMarcel Moolenaar unsigned int c, utfbytes; 1350d3532631SMarcel Moolenaar 1351d3532631SMarcel Moolenaar s8idx = s16idx = 0; 1352d3532631SMarcel Moolenaar utfchar = 0; 1353d3532631SMarcel Moolenaar utfbytes = 0; 1354d3532631SMarcel Moolenaar bzero(s16, s16len << 1); 1355d3532631SMarcel Moolenaar while (s8[s8idx] != 0 && s16idx < s16len) { 1356d3532631SMarcel Moolenaar c = s8[s8idx++]; 1357d3532631SMarcel Moolenaar if ((c & 0xc0) != 0x80) { 1358d3532631SMarcel Moolenaar /* Initial characters. */ 1359d3532631SMarcel Moolenaar if (utfbytes != 0) { 1360d3532631SMarcel Moolenaar /* Incomplete encoding of previous char. */ 1361d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1362d3532631SMarcel Moolenaar } 1363d3532631SMarcel Moolenaar if ((c & 0xf8) == 0xf0) { 1364d3532631SMarcel Moolenaar utfchar = c & 0x07; 1365d3532631SMarcel Moolenaar utfbytes = 3; 1366d3532631SMarcel Moolenaar } else if ((c & 0xf0) == 0xe0) { 1367d3532631SMarcel Moolenaar utfchar = c & 0x0f; 1368d3532631SMarcel Moolenaar utfbytes = 2; 1369d3532631SMarcel Moolenaar } else if ((c & 0xe0) == 0xc0) { 1370d3532631SMarcel Moolenaar utfchar = c & 0x1f; 1371d3532631SMarcel Moolenaar utfbytes = 1; 1372d3532631SMarcel Moolenaar } else { 1373d3532631SMarcel Moolenaar utfchar = c & 0x7f; 1374d3532631SMarcel Moolenaar utfbytes = 0; 1375d3532631SMarcel Moolenaar } 1376d3532631SMarcel Moolenaar } else { 1377d3532631SMarcel Moolenaar /* Followup characters. */ 1378d3532631SMarcel Moolenaar if (utfbytes > 0) { 1379d3532631SMarcel Moolenaar utfchar = (utfchar << 6) + (c & 0x3f); 1380d3532631SMarcel Moolenaar utfbytes--; 1381d3532631SMarcel Moolenaar } else if (utfbytes == 0) 1382d3532631SMarcel Moolenaar utfbytes = ~0; 1383d3532631SMarcel Moolenaar } 1384d3532631SMarcel Moolenaar /* 1385d3532631SMarcel Moolenaar * Write the complete Unicode character as UTF-16 when we 1386d3532631SMarcel Moolenaar * have all the UTF-8 charactars collected. 1387d3532631SMarcel Moolenaar */ 1388d3532631SMarcel Moolenaar if (utfbytes == 0) { 1389d3532631SMarcel Moolenaar /* 1390d3532631SMarcel Moolenaar * If we need to write 2 UTF-16 characters, but 1391d3532631SMarcel Moolenaar * we only have room for 1, then we truncate the 1392d3532631SMarcel Moolenaar * string by writing a 0 instead. 1393d3532631SMarcel Moolenaar */ 1394d3532631SMarcel Moolenaar if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1395d3532631SMarcel Moolenaar s16[s16idx++] = 1396d3532631SMarcel Moolenaar htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1397d3532631SMarcel Moolenaar s16[s16idx++] = 1398d3532631SMarcel Moolenaar htole16(0xdc00 | (utfchar & 0x3ff)); 1399d3532631SMarcel Moolenaar } else 1400d3532631SMarcel Moolenaar s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1401d3532631SMarcel Moolenaar htole16(utfchar); 1402d3532631SMarcel Moolenaar } 1403d3532631SMarcel Moolenaar } 1404d3532631SMarcel Moolenaar /* 1405d3532631SMarcel Moolenaar * If our input string was truncated, append an invalid encoding 1406d3532631SMarcel Moolenaar * character to the output string. 1407d3532631SMarcel Moolenaar */ 1408d3532631SMarcel Moolenaar if (utfbytes != 0 && s16idx < s16len) 1409d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1410d3532631SMarcel Moolenaar } 1411