11d3aed33SMarcel Moolenaar /*- 2369fe59dSMarcel Moolenaar * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar 31d3aed33SMarcel Moolenaar * All rights reserved. 41d3aed33SMarcel Moolenaar * 51d3aed33SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 61d3aed33SMarcel Moolenaar * modification, are permitted provided that the following conditions 71d3aed33SMarcel Moolenaar * are met: 81d3aed33SMarcel Moolenaar * 91d3aed33SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 101d3aed33SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 111d3aed33SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 121d3aed33SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 131d3aed33SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 141d3aed33SMarcel Moolenaar * 151d3aed33SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 161d3aed33SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 171d3aed33SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 181d3aed33SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 191d3aed33SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 201d3aed33SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 211d3aed33SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 221d3aed33SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 231d3aed33SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 241d3aed33SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 251d3aed33SMarcel Moolenaar */ 261d3aed33SMarcel Moolenaar 271d3aed33SMarcel Moolenaar #include <sys/cdefs.h> 281d3aed33SMarcel Moolenaar __FBSDID("$FreeBSD$"); 291d3aed33SMarcel Moolenaar 301d3aed33SMarcel Moolenaar #include <sys/param.h> 311d3aed33SMarcel Moolenaar #include <sys/bio.h> 321d3aed33SMarcel Moolenaar #include <sys/diskmbr.h> 331d3aed33SMarcel Moolenaar #include <sys/endian.h> 341d3aed33SMarcel Moolenaar #include <sys/gpt.h> 351d3aed33SMarcel Moolenaar #include <sys/kernel.h> 361d3aed33SMarcel Moolenaar #include <sys/kobj.h> 371d3aed33SMarcel Moolenaar #include <sys/limits.h> 381d3aed33SMarcel Moolenaar #include <sys/lock.h> 391d3aed33SMarcel Moolenaar #include <sys/malloc.h> 401d3aed33SMarcel Moolenaar #include <sys/mutex.h> 411d3aed33SMarcel Moolenaar #include <sys/queue.h> 421d3aed33SMarcel Moolenaar #include <sys/sbuf.h> 431d3aed33SMarcel Moolenaar #include <sys/systm.h> 44cb08c2ccSAlexander Leidinger #include <sys/sysctl.h> 451d3aed33SMarcel Moolenaar #include <sys/uuid.h> 461d3aed33SMarcel Moolenaar #include <geom/geom.h> 4739dcac84SAndrey V. Elsukov #include <geom/geom_int.h> 481d3aed33SMarcel Moolenaar #include <geom/part/g_part.h> 491d3aed33SMarcel Moolenaar 501d3aed33SMarcel Moolenaar #include "g_part_if.h" 511d3aed33SMarcel Moolenaar 52cb08c2ccSAlexander Leidinger FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support"); 53cb08c2ccSAlexander Leidinger 541d3aed33SMarcel Moolenaar CTASSERT(offsetof(struct gpt_hdr, padding) == 92); 551d3aed33SMarcel Moolenaar CTASSERT(sizeof(struct gpt_ent) == 128); 561d3aed33SMarcel Moolenaar 571d3aed33SMarcel Moolenaar #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 581d3aed33SMarcel Moolenaar 594d32fcb4SMarcel Moolenaar #define MBRSIZE 512 604d32fcb4SMarcel Moolenaar 611d3aed33SMarcel Moolenaar enum gpt_elt { 621d3aed33SMarcel Moolenaar GPT_ELT_PRIHDR, 631d3aed33SMarcel Moolenaar GPT_ELT_PRITBL, 641d3aed33SMarcel Moolenaar GPT_ELT_SECHDR, 651d3aed33SMarcel Moolenaar GPT_ELT_SECTBL, 661d3aed33SMarcel Moolenaar GPT_ELT_COUNT 671d3aed33SMarcel Moolenaar }; 681d3aed33SMarcel Moolenaar 691d3aed33SMarcel Moolenaar enum gpt_state { 701d3aed33SMarcel Moolenaar GPT_STATE_UNKNOWN, /* Not determined. */ 711d3aed33SMarcel Moolenaar GPT_STATE_MISSING, /* No signature found. */ 721d3aed33SMarcel Moolenaar GPT_STATE_CORRUPT, /* Checksum mismatch. */ 731d3aed33SMarcel Moolenaar GPT_STATE_INVALID, /* Nonconformant/invalid. */ 741d3aed33SMarcel Moolenaar GPT_STATE_OK /* Perfectly fine. */ 751d3aed33SMarcel Moolenaar }; 761d3aed33SMarcel Moolenaar 771d3aed33SMarcel Moolenaar struct g_part_gpt_table { 781d3aed33SMarcel Moolenaar struct g_part_table base; 794d32fcb4SMarcel Moolenaar u_char mbr[MBRSIZE]; 801c2dee3cSRobert Noland struct gpt_hdr *hdr; 811d3aed33SMarcel Moolenaar quad_t lba[GPT_ELT_COUNT]; 821d3aed33SMarcel Moolenaar enum gpt_state state[GPT_ELT_COUNT]; 83369fe59dSMarcel Moolenaar int bootcamp; 841d3aed33SMarcel Moolenaar }; 851d3aed33SMarcel Moolenaar 861d3aed33SMarcel Moolenaar struct g_part_gpt_entry { 871d3aed33SMarcel Moolenaar struct g_part_entry base; 881d3aed33SMarcel Moolenaar struct gpt_ent ent; 891d3aed33SMarcel Moolenaar }; 901d3aed33SMarcel Moolenaar 91d3532631SMarcel Moolenaar static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t); 92d3532631SMarcel Moolenaar static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t); 932b9be055SAndrey V. Elsukov static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *); 94d3532631SMarcel Moolenaar 951d3aed33SMarcel Moolenaar static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 961d3aed33SMarcel Moolenaar struct g_part_parms *); 974d32fcb4SMarcel Moolenaar static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 981d3aed33SMarcel Moolenaar static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 991d3aed33SMarcel Moolenaar static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 10051f53a08SWarner Losh static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 1015db67052SMarcel Moolenaar struct sbuf *, const char *); 1021d3aed33SMarcel Moolenaar static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 1031d3aed33SMarcel Moolenaar static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 1041d3aed33SMarcel Moolenaar struct g_part_parms *); 10551f53a08SWarner Losh static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 1061d3aed33SMarcel Moolenaar char *, size_t); 1071d3aed33SMarcel Moolenaar static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 1081d3aed33SMarcel Moolenaar static int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 109cec283baSPawel Jakub Dawidek static int g_part_gpt_setunset(struct g_part_table *table, 110cec283baSPawel Jakub Dawidek struct g_part_entry *baseentry, const char *attrib, unsigned int set); 1111d3aed33SMarcel Moolenaar static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 1121d3aed33SMarcel Moolenaar char *, size_t); 1131d3aed33SMarcel Moolenaar static int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 1143f71c319SMarcel Moolenaar static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 1153f71c319SMarcel Moolenaar struct g_part_parms *); 116e7926a37SAndrey V. Elsukov static int g_part_gpt_recover(struct g_part_table *); 1171d3aed33SMarcel Moolenaar 1181d3aed33SMarcel Moolenaar static kobj_method_t g_part_gpt_methods[] = { 1191d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_add, g_part_gpt_add), 1204d32fcb4SMarcel Moolenaar KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 1211d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_create, g_part_gpt_create), 1221d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 1235db67052SMarcel Moolenaar KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 1241d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 1251d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_modify, g_part_gpt_modify), 1263f71c319SMarcel Moolenaar KOBJMETHOD(g_part_resize, g_part_gpt_resize), 1271d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_name, g_part_gpt_name), 1281d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_probe, g_part_gpt_probe), 1291d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_read, g_part_gpt_read), 130e7926a37SAndrey V. Elsukov KOBJMETHOD(g_part_recover, g_part_gpt_recover), 131cec283baSPawel Jakub Dawidek KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 1321d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_type, g_part_gpt_type), 1331d3aed33SMarcel Moolenaar KOBJMETHOD(g_part_write, g_part_gpt_write), 1341d3aed33SMarcel Moolenaar { 0, 0 } 1351d3aed33SMarcel Moolenaar }; 1361d3aed33SMarcel Moolenaar 1371d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_gpt_scheme = { 1381d3aed33SMarcel Moolenaar "GPT", 1391d3aed33SMarcel Moolenaar g_part_gpt_methods, 1401d3aed33SMarcel Moolenaar sizeof(struct g_part_gpt_table), 1411d3aed33SMarcel Moolenaar .gps_entrysz = sizeof(struct g_part_gpt_entry), 1421d3aed33SMarcel Moolenaar .gps_minent = 128, 143799eac8cSAndrey V. Elsukov .gps_maxent = 4096, 1444d32fcb4SMarcel Moolenaar .gps_bootcodesz = MBRSIZE, 1451d3aed33SMarcel Moolenaar }; 1464ffca444SMarcel Moolenaar G_PART_SCHEME_DECLARE(g_part_gpt); 1471d3aed33SMarcel Moolenaar 148f1317430SRui Paulo static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 14987662ab3SRui Paulo static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 150f1317430SRui Paulo static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 151f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 152f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 153f1317430SRui Paulo static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 154f1317430SRui Paulo static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 15588007f61SAndrey V. Elsukov static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 1561d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 1571d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 158f352a0d4SJohn Baldwin static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 159f24a8224SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 1601d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 1611d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 1621d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 163a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 164f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 165f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 166f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 1671d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 168b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 169b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 170b20e4de3SDmitry Morozovsky static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 1715cc596c4SDmitry Morozovsky static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR; 17233f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 17333f7a412SRui Paulo static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 17433f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 17533f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 17633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 17733f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 17833f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 17933f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 18033f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 18133f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 1821d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 1831d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 1840640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP; 1850640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1; 1860640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM; 1870640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD; 1880640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY; 1890640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER; 1900640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2; 1910640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32; 1920640b71dSAndrey V. Elsukov static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64; 193*1ee0f089SNathan Whitehorn static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT; 1941d3aed33SMarcel Moolenaar 195f1317430SRui Paulo static struct g_part_uuid_alias { 196f1317430SRui Paulo struct uuid *uuid; 197f1317430SRui Paulo int alias; 198369fe59dSMarcel Moolenaar int mbrtype; 199f1317430SRui Paulo } gpt_uuid_alias_match[] = { 200369fe59dSMarcel Moolenaar { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 201369fe59dSMarcel Moolenaar { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 202369fe59dSMarcel Moolenaar { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 203369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 204369fe59dSMarcel Moolenaar { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 205369fe59dSMarcel Moolenaar { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 206369fe59dSMarcel Moolenaar { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 207369fe59dSMarcel Moolenaar { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 208369fe59dSMarcel Moolenaar { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 209369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 210369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 211f24a8224SMarcel Moolenaar { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 }, 212369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 213369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 214369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 215369fe59dSMarcel Moolenaar { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 216369fe59dSMarcel Moolenaar { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 217369fe59dSMarcel Moolenaar { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 218369fe59dSMarcel Moolenaar { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 219369fe59dSMarcel Moolenaar { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 220b20e4de3SDmitry Morozovsky { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 221b20e4de3SDmitry Morozovsky { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 222b20e4de3SDmitry Morozovsky { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 2235cc596c4SDmitry Morozovsky { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 }, 224369fe59dSMarcel Moolenaar { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 225369fe59dSMarcel Moolenaar { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 226369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 227369fe59dSMarcel Moolenaar { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 228369fe59dSMarcel Moolenaar { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 229369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 230369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 231369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 232369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 233369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 234369fe59dSMarcel Moolenaar { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 2350640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_swap, G_PART_ALIAS_DFBSD_SWAP, 0 }, 2360640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_ufs1, G_PART_ALIAS_DFBSD_UFS, 0 }, 2370640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_vinum, G_PART_ALIAS_DFBSD_VINUM, 0 }, 2380640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_ccd, G_PART_ALIAS_DFBSD_CCD, 0 }, 2390640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_legacy, G_PART_ALIAS_DFBSD_LEGACY, 0 }, 2400640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_hammer, G_PART_ALIAS_DFBSD_HAMMER, 0 }, 2410640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_hammer2, G_PART_ALIAS_DFBSD_HAMMER2, 0 }, 2420640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_label32, G_PART_ALIAS_DFBSD, 0xa5 }, 2430640b71dSAndrey V. Elsukov { &gpt_uuid_dfbsd_label64, G_PART_ALIAS_DFBSD64, 0xa5 }, 244*1ee0f089SNathan Whitehorn { &gpt_uuid_prep_boot, G_PART_ALIAS_PREP_BOOT, 0x41 }, 245369fe59dSMarcel Moolenaar { NULL, 0, 0 } 246f1317430SRui Paulo }; 247f1317430SRui Paulo 248369fe59dSMarcel Moolenaar static int 249369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 250369fe59dSMarcel Moolenaar quad_t end) 251369fe59dSMarcel Moolenaar { 252369fe59dSMarcel Moolenaar 253369fe59dSMarcel Moolenaar if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 254369fe59dSMarcel Moolenaar return (EINVAL); 255369fe59dSMarcel Moolenaar 256369fe59dSMarcel Moolenaar mbr += DOSPARTOFF + idx * DOSPARTSIZE; 257369fe59dSMarcel Moolenaar mbr[0] = 0; 258369fe59dSMarcel Moolenaar if (start == 1) { 259369fe59dSMarcel Moolenaar /* 260369fe59dSMarcel Moolenaar * Treat the PMBR partition specially to maximize 261369fe59dSMarcel Moolenaar * interoperability with BIOSes. 262369fe59dSMarcel Moolenaar */ 263369fe59dSMarcel Moolenaar mbr[1] = mbr[3] = 0; 264369fe59dSMarcel Moolenaar mbr[2] = 2; 265369fe59dSMarcel Moolenaar } else 266369fe59dSMarcel Moolenaar mbr[1] = mbr[2] = mbr[3] = 0xff; 267369fe59dSMarcel Moolenaar mbr[4] = typ; 268369fe59dSMarcel Moolenaar mbr[5] = mbr[6] = mbr[7] = 0xff; 269369fe59dSMarcel Moolenaar le32enc(mbr + 8, (uint32_t)start); 270369fe59dSMarcel Moolenaar le32enc(mbr + 12, (uint32_t)(end - start + 1)); 271369fe59dSMarcel Moolenaar return (0); 272369fe59dSMarcel Moolenaar } 273369fe59dSMarcel Moolenaar 274369fe59dSMarcel Moolenaar static int 275369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t) 276369fe59dSMarcel Moolenaar { 277369fe59dSMarcel Moolenaar struct g_part_uuid_alias *uap; 278369fe59dSMarcel Moolenaar 279369fe59dSMarcel Moolenaar for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 280369fe59dSMarcel Moolenaar if (EQUUID(t, uap->uuid)) 281369fe59dSMarcel Moolenaar return (uap->mbrtype); 282369fe59dSMarcel Moolenaar } 283369fe59dSMarcel Moolenaar return (0); 284369fe59dSMarcel Moolenaar } 285369fe59dSMarcel Moolenaar 2863bd22a9cSMarcel Moolenaar static void 2873bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 2883bd22a9cSMarcel Moolenaar { 2893bd22a9cSMarcel Moolenaar 2903bd22a9cSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 2913bd22a9cSMarcel Moolenaar gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 2923bd22a9cSMarcel Moolenaar MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 2933bd22a9cSMarcel Moolenaar le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 2943bd22a9cSMarcel Moolenaar } 2953bd22a9cSMarcel Moolenaar 296369fe59dSMarcel Moolenaar /* 297369fe59dSMarcel Moolenaar * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 298369fe59dSMarcel Moolenaar * whole disk anymore. Rather, it covers the GPT table and the EFI 299369fe59dSMarcel Moolenaar * system partition only. This way the HFS+ partition and any FAT 300369fe59dSMarcel Moolenaar * partitions can be added to the MBR without creating an overlap. 301369fe59dSMarcel Moolenaar */ 302369fe59dSMarcel Moolenaar static int 303369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 304369fe59dSMarcel Moolenaar { 305369fe59dSMarcel Moolenaar uint8_t *p; 306369fe59dSMarcel Moolenaar 307369fe59dSMarcel Moolenaar p = table->mbr + DOSPARTOFF; 308369fe59dSMarcel Moolenaar if (p[4] != 0xee || le32dec(p + 8) != 1) 309369fe59dSMarcel Moolenaar return (0); 310369fe59dSMarcel Moolenaar 311369fe59dSMarcel Moolenaar p += DOSPARTSIZE; 312369fe59dSMarcel Moolenaar if (p[4] != 0xaf) 313369fe59dSMarcel Moolenaar return (0); 314369fe59dSMarcel Moolenaar 315369fe59dSMarcel Moolenaar printf("GEOM: %s: enabling Boot Camp\n", provname); 316369fe59dSMarcel Moolenaar return (1); 317369fe59dSMarcel Moolenaar } 318369fe59dSMarcel Moolenaar 319369fe59dSMarcel Moolenaar static void 3203bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 321369fe59dSMarcel Moolenaar { 322369fe59dSMarcel Moolenaar struct g_part_entry *baseentry; 323369fe59dSMarcel Moolenaar struct g_part_gpt_entry *entry; 324369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 325369fe59dSMarcel Moolenaar int bootable, error, index, slices, typ; 326369fe59dSMarcel Moolenaar 327369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 328369fe59dSMarcel Moolenaar 329369fe59dSMarcel Moolenaar bootable = -1; 330369fe59dSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 331369fe59dSMarcel Moolenaar if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 332369fe59dSMarcel Moolenaar bootable = index; 333369fe59dSMarcel Moolenaar } 334369fe59dSMarcel Moolenaar 335369fe59dSMarcel Moolenaar bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 336369fe59dSMarcel Moolenaar slices = 0; 337369fe59dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 338369fe59dSMarcel Moolenaar if (baseentry->gpe_deleted) 339369fe59dSMarcel Moolenaar continue; 340369fe59dSMarcel Moolenaar index = baseentry->gpe_index - 1; 341369fe59dSMarcel Moolenaar if (index >= NDOSPART) 342369fe59dSMarcel Moolenaar continue; 343369fe59dSMarcel Moolenaar 344369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 345369fe59dSMarcel Moolenaar 346369fe59dSMarcel Moolenaar switch (index) { 347369fe59dSMarcel Moolenaar case 0: /* This must be the EFI system partition. */ 348369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 349369fe59dSMarcel Moolenaar goto disable; 350369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xee, 351369fe59dSMarcel Moolenaar 1ull, entry->ent.ent_lba_end); 352369fe59dSMarcel Moolenaar break; 353369fe59dSMarcel Moolenaar case 1: /* This must be the HFS+ partition. */ 354369fe59dSMarcel Moolenaar if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 355369fe59dSMarcel Moolenaar goto disable; 356369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 357369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 358369fe59dSMarcel Moolenaar break; 359369fe59dSMarcel Moolenaar default: 360369fe59dSMarcel Moolenaar typ = gpt_map_type(&entry->ent.ent_type); 361369fe59dSMarcel Moolenaar error = gpt_write_mbr_entry(table->mbr, index, typ, 362369fe59dSMarcel Moolenaar entry->ent.ent_lba_start, entry->ent.ent_lba_end); 363369fe59dSMarcel Moolenaar break; 364369fe59dSMarcel Moolenaar } 365369fe59dSMarcel Moolenaar if (error) 366369fe59dSMarcel Moolenaar continue; 367369fe59dSMarcel Moolenaar 368369fe59dSMarcel Moolenaar if (index == bootable) 369369fe59dSMarcel Moolenaar table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 370369fe59dSMarcel Moolenaar slices |= 1 << index; 371369fe59dSMarcel Moolenaar } 372369fe59dSMarcel Moolenaar if ((slices & 3) == 3) 373369fe59dSMarcel Moolenaar return; 374369fe59dSMarcel Moolenaar 375369fe59dSMarcel Moolenaar disable: 376369fe59dSMarcel Moolenaar table->bootcamp = 0; 3773bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 378369fe59dSMarcel Moolenaar } 379369fe59dSMarcel Moolenaar 3801c2dee3cSRobert Noland static struct gpt_hdr * 3811d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 3821c2dee3cSRobert Noland enum gpt_elt elt) 3831d3aed33SMarcel Moolenaar { 3841c2dee3cSRobert Noland struct gpt_hdr *buf, *hdr; 3851d3aed33SMarcel Moolenaar struct g_provider *pp; 3861d3aed33SMarcel Moolenaar quad_t lba, last; 3871d3aed33SMarcel Moolenaar int error; 3881d3aed33SMarcel Moolenaar uint32_t crc, sz; 3891d3aed33SMarcel Moolenaar 3901d3aed33SMarcel Moolenaar pp = cp->provider; 3911d3aed33SMarcel Moolenaar last = (pp->mediasize / pp->sectorsize) - 1; 3921d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 393e7926a37SAndrey V. Elsukov /* 394e7926a37SAndrey V. Elsukov * If the primary header is valid look for secondary 395e7926a37SAndrey V. Elsukov * header in AlternateLBA, otherwise in the last medium's LBA. 396e7926a37SAndrey V. Elsukov */ 397e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_SECHDR) { 398e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 399e7926a37SAndrey V. Elsukov table->lba[elt] = last; 400e7926a37SAndrey V. Elsukov } else 401e7926a37SAndrey V. Elsukov table->lba[elt] = 1; 4021d3aed33SMarcel Moolenaar buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 4031d3aed33SMarcel Moolenaar &error); 4041d3aed33SMarcel Moolenaar if (buf == NULL) 4051c2dee3cSRobert Noland return (NULL); 4061c2dee3cSRobert Noland hdr = NULL; 4071c2dee3cSRobert Noland if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 4081c2dee3cSRobert Noland goto fail; 4091d3aed33SMarcel Moolenaar 4101d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 4111c2dee3cSRobert Noland sz = le32toh(buf->hdr_size); 4121d3aed33SMarcel Moolenaar if (sz < 92 || sz > pp->sectorsize) 4131c2dee3cSRobert Noland goto fail; 4141c2dee3cSRobert Noland 4151c2dee3cSRobert Noland hdr = g_malloc(sz, M_WAITOK | M_ZERO); 4161c2dee3cSRobert Noland bcopy(buf, hdr, sz); 4171d3aed33SMarcel Moolenaar hdr->hdr_size = sz; 4181c2dee3cSRobert Noland 4191c2dee3cSRobert Noland crc = le32toh(buf->hdr_crc_self); 4201c2dee3cSRobert Noland buf->hdr_crc_self = 0; 4211c2dee3cSRobert Noland if (crc32(buf, sz) != crc) 4221c2dee3cSRobert Noland goto fail; 4231d3aed33SMarcel Moolenaar hdr->hdr_crc_self = crc; 4241d3aed33SMarcel Moolenaar 4251d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_INVALID; 4261c2dee3cSRobert Noland hdr->hdr_revision = le32toh(buf->hdr_revision); 427e7926a37SAndrey V. Elsukov if (hdr->hdr_revision < GPT_HDR_REVISION) 4281c2dee3cSRobert Noland goto fail; 4291c2dee3cSRobert Noland hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 4301d3aed33SMarcel Moolenaar if (hdr->hdr_lba_self != table->lba[elt]) 4311c2dee3cSRobert Noland goto fail; 4321c2dee3cSRobert Noland hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 433e7926a37SAndrey V. Elsukov if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 434e7926a37SAndrey V. Elsukov hdr->hdr_lba_alt > last) 435e7926a37SAndrey V. Elsukov goto fail; 4361d3aed33SMarcel Moolenaar 4371d3aed33SMarcel Moolenaar /* Check the managed area. */ 4381c2dee3cSRobert Noland hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 4391d3aed33SMarcel Moolenaar if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 4401c2dee3cSRobert Noland goto fail; 4411c2dee3cSRobert Noland hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 4421d3aed33SMarcel Moolenaar if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 4431c2dee3cSRobert Noland goto fail; 4441d3aed33SMarcel Moolenaar 4451d3aed33SMarcel Moolenaar /* Check the table location and size of the table. */ 4461c2dee3cSRobert Noland hdr->hdr_entries = le32toh(buf->hdr_entries); 4471c2dee3cSRobert Noland hdr->hdr_entsz = le32toh(buf->hdr_entsz); 4481d3aed33SMarcel Moolenaar if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 4491d3aed33SMarcel Moolenaar (hdr->hdr_entsz & 7) != 0) 4501c2dee3cSRobert Noland goto fail; 4511c2dee3cSRobert Noland hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 4521d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 4531c2dee3cSRobert Noland goto fail; 4541d3aed33SMarcel Moolenaar if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 4551d3aed33SMarcel Moolenaar hdr->hdr_lba_table <= hdr->hdr_lba_end) 4561c2dee3cSRobert Noland goto fail; 4571d3aed33SMarcel Moolenaar lba = hdr->hdr_lba_table + 4581d3aed33SMarcel Moolenaar (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) / 4591d3aed33SMarcel Moolenaar pp->sectorsize - 1; 4601d3aed33SMarcel Moolenaar if (lba >= last) 4611c2dee3cSRobert Noland goto fail; 4621d3aed33SMarcel Moolenaar if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 4631c2dee3cSRobert Noland goto fail; 4641d3aed33SMarcel Moolenaar 4651d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 4661c2dee3cSRobert Noland le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 4671c2dee3cSRobert Noland hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 4681c2dee3cSRobert Noland 469e7926a37SAndrey V. Elsukov /* save LBA for secondary header */ 470e7926a37SAndrey V. Elsukov if (elt == GPT_ELT_PRIHDR) 471e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 472e7926a37SAndrey V. Elsukov 4731c2dee3cSRobert Noland g_free(buf); 4741c2dee3cSRobert Noland return (hdr); 4751c2dee3cSRobert Noland 4761c2dee3cSRobert Noland fail: 4771c2dee3cSRobert Noland if (hdr != NULL) 4781c2dee3cSRobert Noland g_free(hdr); 4791c2dee3cSRobert Noland g_free(buf); 4801c2dee3cSRobert Noland return (NULL); 4811d3aed33SMarcel Moolenaar } 4821d3aed33SMarcel Moolenaar 4831d3aed33SMarcel Moolenaar static struct gpt_ent * 4841d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 4851d3aed33SMarcel Moolenaar enum gpt_elt elt, struct gpt_hdr *hdr) 4861d3aed33SMarcel Moolenaar { 4871d3aed33SMarcel Moolenaar struct g_provider *pp; 4881d3aed33SMarcel Moolenaar struct gpt_ent *ent, *tbl; 4891d3aed33SMarcel Moolenaar char *buf, *p; 490799eac8cSAndrey V. Elsukov unsigned int idx, sectors, tblsz, size; 4911d3aed33SMarcel Moolenaar int error; 4921d3aed33SMarcel Moolenaar 4931c2dee3cSRobert Noland if (hdr == NULL) 4941c2dee3cSRobert Noland return (NULL); 4951c2dee3cSRobert Noland 4961d3aed33SMarcel Moolenaar pp = cp->provider; 4971d3aed33SMarcel Moolenaar table->lba[elt] = hdr->hdr_lba_table; 4981d3aed33SMarcel Moolenaar 4991d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_MISSING; 5001d3aed33SMarcel Moolenaar tblsz = hdr->hdr_entries * hdr->hdr_entsz; 5011d3aed33SMarcel Moolenaar sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize; 502799eac8cSAndrey V. Elsukov buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 503799eac8cSAndrey V. Elsukov for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 504799eac8cSAndrey V. Elsukov size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 505799eac8cSAndrey V. Elsukov (sectors - idx) * pp->sectorsize; 506799eac8cSAndrey V. Elsukov p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 507799eac8cSAndrey V. Elsukov size, &error); 508799eac8cSAndrey V. Elsukov if (p == NULL) { 509799eac8cSAndrey V. Elsukov g_free(buf); 5101d3aed33SMarcel Moolenaar return (NULL); 511799eac8cSAndrey V. Elsukov } 512799eac8cSAndrey V. Elsukov bcopy(p, buf + idx * pp->sectorsize, size); 513799eac8cSAndrey V. Elsukov g_free(p); 514799eac8cSAndrey V. Elsukov } 5151d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_CORRUPT; 5161d3aed33SMarcel Moolenaar if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 5171d3aed33SMarcel Moolenaar g_free(buf); 5181d3aed33SMarcel Moolenaar return (NULL); 5191d3aed33SMarcel Moolenaar } 5201d3aed33SMarcel Moolenaar 5211d3aed33SMarcel Moolenaar table->state[elt] = GPT_STATE_OK; 5221d3aed33SMarcel Moolenaar tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 5231d3aed33SMarcel Moolenaar M_WAITOK | M_ZERO); 5241d3aed33SMarcel Moolenaar 5251d3aed33SMarcel Moolenaar for (idx = 0, ent = tbl, p = buf; 5261d3aed33SMarcel Moolenaar idx < hdr->hdr_entries; 5271d3aed33SMarcel Moolenaar idx++, ent++, p += hdr->hdr_entsz) { 5281d3aed33SMarcel Moolenaar le_uuid_dec(p, &ent->ent_type); 5291d3aed33SMarcel Moolenaar le_uuid_dec(p + 16, &ent->ent_uuid); 5301d3aed33SMarcel Moolenaar ent->ent_lba_start = le64dec(p + 32); 5311d3aed33SMarcel Moolenaar ent->ent_lba_end = le64dec(p + 40); 5321d3aed33SMarcel Moolenaar ent->ent_attr = le64dec(p + 48); 533d3532631SMarcel Moolenaar /* Keep UTF-16 in little-endian. */ 534d3532631SMarcel Moolenaar bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 5351d3aed33SMarcel Moolenaar } 5361d3aed33SMarcel Moolenaar 5371d3aed33SMarcel Moolenaar g_free(buf); 5381d3aed33SMarcel Moolenaar return (tbl); 5391d3aed33SMarcel Moolenaar } 5401d3aed33SMarcel Moolenaar 5411d3aed33SMarcel Moolenaar static int 5421d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 5431d3aed33SMarcel Moolenaar { 5441d3aed33SMarcel Moolenaar 5451c2dee3cSRobert Noland if (pri == NULL || sec == NULL) 5461c2dee3cSRobert Noland return (0); 5471c2dee3cSRobert Noland 5481d3aed33SMarcel Moolenaar if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 5491d3aed33SMarcel Moolenaar return (0); 5501d3aed33SMarcel Moolenaar return ((pri->hdr_revision == sec->hdr_revision && 5511d3aed33SMarcel Moolenaar pri->hdr_size == sec->hdr_size && 5521d3aed33SMarcel Moolenaar pri->hdr_lba_start == sec->hdr_lba_start && 5531d3aed33SMarcel Moolenaar pri->hdr_lba_end == sec->hdr_lba_end && 5541d3aed33SMarcel Moolenaar pri->hdr_entries == sec->hdr_entries && 5551d3aed33SMarcel Moolenaar pri->hdr_entsz == sec->hdr_entsz && 5561d3aed33SMarcel Moolenaar pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 5571d3aed33SMarcel Moolenaar } 5581d3aed33SMarcel Moolenaar 5591d3aed33SMarcel Moolenaar static int 5601d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid) 5611d3aed33SMarcel Moolenaar { 5621d3aed33SMarcel Moolenaar struct uuid tmp; 563d287f590SMarcel Moolenaar const char *alias; 5641d3aed33SMarcel Moolenaar int error; 565f1317430SRui Paulo struct g_part_uuid_alias *uap; 5661d3aed33SMarcel Moolenaar 567d287f590SMarcel Moolenaar if (type[0] == '!') { 568d287f590SMarcel Moolenaar error = parse_uuid(type + 1, &tmp); 5691d3aed33SMarcel Moolenaar if (error) 5701d3aed33SMarcel Moolenaar return (error); 5711d3aed33SMarcel Moolenaar if (EQUUID(&tmp, &gpt_uuid_unused)) 5721d3aed33SMarcel Moolenaar return (EINVAL); 5731d3aed33SMarcel Moolenaar *uuid = tmp; 5741d3aed33SMarcel Moolenaar return (0); 5751d3aed33SMarcel Moolenaar } 576f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 577f1317430SRui Paulo alias = g_part_alias_name(uap->alias); 578d287f590SMarcel Moolenaar if (!strcasecmp(type, alias)) { 579665bb830SMarcel Moolenaar *uuid = *uap->uuid; 5801d3aed33SMarcel Moolenaar return (0); 5811d3aed33SMarcel Moolenaar } 582d7255ff4SRui Paulo } 583d287f590SMarcel Moolenaar return (EINVAL); 584d287f590SMarcel Moolenaar } 5851d3aed33SMarcel Moolenaar 5861d3aed33SMarcel Moolenaar static int 5871d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 5881d3aed33SMarcel Moolenaar struct g_part_parms *gpp) 5891d3aed33SMarcel Moolenaar { 5901d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 5911d3aed33SMarcel Moolenaar int error; 5921d3aed33SMarcel Moolenaar 5931d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 5941d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 5951d3aed33SMarcel Moolenaar if (error) 5961d3aed33SMarcel Moolenaar return (error); 5971d3aed33SMarcel Moolenaar kern_uuidgen(&entry->ent.ent_uuid, 1); 5981d3aed33SMarcel Moolenaar entry->ent.ent_lba_start = baseentry->gpe_start; 5991d3aed33SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 6001d3aed33SMarcel Moolenaar if (baseentry->gpe_deleted) { 6011d3aed33SMarcel Moolenaar entry->ent.ent_attr = 0; 6021d3aed33SMarcel Moolenaar bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 6031d3aed33SMarcel Moolenaar } 604d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 605d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 606ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 607ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 6081d3aed33SMarcel Moolenaar return (0); 6091d3aed33SMarcel Moolenaar } 6101d3aed33SMarcel Moolenaar 6111d3aed33SMarcel Moolenaar static int 6124d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 6134d32fcb4SMarcel Moolenaar { 6144d32fcb4SMarcel Moolenaar struct g_part_gpt_table *table; 61566477112SMarcel Moolenaar size_t codesz; 6164d32fcb4SMarcel Moolenaar 61766477112SMarcel Moolenaar codesz = DOSPARTOFF; 6184d32fcb4SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 61966477112SMarcel Moolenaar bzero(table->mbr, codesz); 62066477112SMarcel Moolenaar codesz = MIN(codesz, gpp->gpp_codesize); 62166477112SMarcel Moolenaar if (codesz > 0) 62266477112SMarcel Moolenaar bcopy(gpp->gpp_codeptr, table->mbr, codesz); 6234d32fcb4SMarcel Moolenaar return (0); 6244d32fcb4SMarcel Moolenaar } 6254d32fcb4SMarcel Moolenaar 6264d32fcb4SMarcel Moolenaar static int 6271d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 6281d3aed33SMarcel Moolenaar { 6291d3aed33SMarcel Moolenaar struct g_provider *pp; 6301d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 6311d3aed33SMarcel Moolenaar size_t tblsz; 6321d3aed33SMarcel Moolenaar 6332a1c00ffSMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 6342a1c00ffSMarcel Moolenaar if (basetable->gpt_depth != 0) 6352a1c00ffSMarcel Moolenaar return (ENXIO); 6362a1c00ffSMarcel Moolenaar 6371d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 6381d3aed33SMarcel Moolenaar pp = gpp->gpp_provider; 6391d3aed33SMarcel Moolenaar tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 6401d3aed33SMarcel Moolenaar pp->sectorsize - 1) / pp->sectorsize; 6414d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || 6421d3aed33SMarcel Moolenaar pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 6431d3aed33SMarcel Moolenaar pp->sectorsize) 6441d3aed33SMarcel Moolenaar return (ENOSPC); 6451d3aed33SMarcel Moolenaar 6463bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 6473bd22a9cSMarcel Moolenaar 648a59a1310SRobert Noland /* Allocate space for the header */ 649a59a1310SRobert Noland table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 650a59a1310SRobert Noland 6511c2dee3cSRobert Noland bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 6521c2dee3cSRobert Noland table->hdr->hdr_revision = GPT_HDR_REVISION; 6531c2dee3cSRobert Noland table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 6541c2dee3cSRobert Noland kern_uuidgen(&table->hdr->hdr_uuid, 1); 6551c2dee3cSRobert Noland table->hdr->hdr_entries = basetable->gpt_entries; 6561c2dee3cSRobert Noland table->hdr->hdr_entsz = sizeof(struct gpt_ent); 6571d3aed33SMarcel Moolenaar 6582b9be055SAndrey V. Elsukov g_gpt_set_defaults(basetable, pp); 6591d3aed33SMarcel Moolenaar return (0); 6601d3aed33SMarcel Moolenaar } 6611d3aed33SMarcel Moolenaar 6621d3aed33SMarcel Moolenaar static int 6631d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 6641d3aed33SMarcel Moolenaar { 66536066952SMarius Strobl struct g_part_gpt_table *table; 666e7926a37SAndrey V. Elsukov struct g_provider *pp; 66736066952SMarius Strobl 66836066952SMarius Strobl table = (struct g_part_gpt_table *)basetable; 669e7926a37SAndrey V. Elsukov pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 67036066952SMarius Strobl g_free(table->hdr); 67136066952SMarius Strobl table->hdr = NULL; 6721d3aed33SMarcel Moolenaar 6731d3aed33SMarcel Moolenaar /* 674e7926a37SAndrey V. Elsukov * Wipe the first 2 sectors to clear the partitioning. Wipe the last 675e7926a37SAndrey V. Elsukov * sector only if it has valid secondary header. 6761d3aed33SMarcel Moolenaar */ 6771d3aed33SMarcel Moolenaar basetable->gpt_smhead |= 3; 678e7926a37SAndrey V. Elsukov if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 679e7926a37SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 6801d3aed33SMarcel Moolenaar basetable->gpt_smtail |= 1; 6811d3aed33SMarcel Moolenaar return (0); 6821d3aed33SMarcel Moolenaar } 6831d3aed33SMarcel Moolenaar 68451f53a08SWarner Losh static void 6855db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 6865db67052SMarcel Moolenaar struct sbuf *sb, const char *indent) 6875db67052SMarcel Moolenaar { 6885db67052SMarcel Moolenaar struct g_part_gpt_entry *entry; 6895db67052SMarcel Moolenaar 6905db67052SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 6910c132595SMarcel Moolenaar if (indent == NULL) { 6920c132595SMarcel Moolenaar /* conftxt: libdisk compatibility */ 6935db67052SMarcel Moolenaar sbuf_printf(sb, " xs GPT xt "); 6945db67052SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 6950c132595SMarcel Moolenaar } else if (entry != NULL) { 6960c132595SMarcel Moolenaar /* confxml: partition entry information */ 697d3532631SMarcel Moolenaar sbuf_printf(sb, "%s<label>", indent); 698d3532631SMarcel Moolenaar g_gpt_printf_utf16(sb, entry->ent.ent_name, 699d3532631SMarcel Moolenaar sizeof(entry->ent.ent_name) >> 1); 700d3532631SMarcel Moolenaar sbuf_printf(sb, "</label>\n"); 701cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 702cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 703cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 704cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 705cec283baSPawel Jakub Dawidek indent); 706cec283baSPawel Jakub Dawidek } 707cec283baSPawel Jakub Dawidek if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 708cec283baSPawel Jakub Dawidek sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 709cec283baSPawel Jakub Dawidek indent); 710cec283baSPawel Jakub Dawidek } 7110c132595SMarcel Moolenaar sbuf_printf(sb, "%s<rawtype>", indent); 7120c132595SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_type); 7130c132595SMarcel Moolenaar sbuf_printf(sb, "</rawtype>\n"); 714cd18ad83SMarcel Moolenaar sbuf_printf(sb, "%s<rawuuid>", indent); 715cd18ad83SMarcel Moolenaar sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 716cd18ad83SMarcel Moolenaar sbuf_printf(sb, "</rawuuid>\n"); 7170c132595SMarcel Moolenaar } else { 7180c132595SMarcel Moolenaar /* confxml: scheme information */ 7190c132595SMarcel Moolenaar } 7205db67052SMarcel Moolenaar } 7215db67052SMarcel Moolenaar 7225db67052SMarcel Moolenaar static int 7231d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 7241d3aed33SMarcel Moolenaar { 7251d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7261d3aed33SMarcel Moolenaar 7271d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7281d3aed33SMarcel Moolenaar return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 7294042ab48SAndrey V. Elsukov EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) || 7304042ab48SAndrey V. Elsukov EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0); 7311d3aed33SMarcel Moolenaar } 7321d3aed33SMarcel Moolenaar 7331d3aed33SMarcel Moolenaar static int 7341d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable, 7351d3aed33SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7361d3aed33SMarcel Moolenaar { 7371d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7381d3aed33SMarcel Moolenaar int error; 7391d3aed33SMarcel Moolenaar 7401d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 741d287f590SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_TYPE) { 7421d3aed33SMarcel Moolenaar error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 7431d3aed33SMarcel Moolenaar if (error) 7441d3aed33SMarcel Moolenaar return (error); 745d287f590SMarcel Moolenaar } 746d3532631SMarcel Moolenaar if (gpp->gpp_parms & G_PART_PARM_LABEL) 747d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 748ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name) / 749ed7beddcSMatthew D Fleming sizeof(entry->ent.ent_name[0])); 7501d3aed33SMarcel Moolenaar return (0); 7511d3aed33SMarcel Moolenaar } 7521d3aed33SMarcel Moolenaar 7533f71c319SMarcel Moolenaar static int 7543f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable, 7553f71c319SMarcel Moolenaar struct g_part_entry *baseentry, struct g_part_parms *gpp) 7563f71c319SMarcel Moolenaar { 7573f71c319SMarcel Moolenaar struct g_part_gpt_entry *entry; 7583f71c319SMarcel Moolenaar 759884c8e4fSAndrey V. Elsukov if (baseentry == NULL) 760884c8e4fSAndrey V. Elsukov return (EOPNOTSUPP); 761884c8e4fSAndrey V. Elsukov 762884c8e4fSAndrey V. Elsukov entry = (struct g_part_gpt_entry *)baseentry; 7633f71c319SMarcel Moolenaar baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 7643f71c319SMarcel Moolenaar entry->ent.ent_lba_end = baseentry->gpe_end; 7653f71c319SMarcel Moolenaar 7663f71c319SMarcel Moolenaar return (0); 7673f71c319SMarcel Moolenaar } 7683f71c319SMarcel Moolenaar 76951f53a08SWarner Losh static const char * 7701d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 7711d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 7721d3aed33SMarcel Moolenaar { 7731d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 7741d3aed33SMarcel Moolenaar char c; 7751d3aed33SMarcel Moolenaar 7761d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 7771d3aed33SMarcel Moolenaar c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 7781d3aed33SMarcel Moolenaar snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 7791d3aed33SMarcel Moolenaar return (buf); 7801d3aed33SMarcel Moolenaar } 7811d3aed33SMarcel Moolenaar 7821d3aed33SMarcel Moolenaar static int 7831d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 7841d3aed33SMarcel Moolenaar { 7851d3aed33SMarcel Moolenaar struct g_provider *pp; 7863e5a0a6bSMarcel Moolenaar u_char *buf; 7873e5a0a6bSMarcel Moolenaar int error, index, pri, res; 7881d3aed33SMarcel Moolenaar 7891d3aed33SMarcel Moolenaar /* We don't nest, which means that our depth should be 0. */ 7901d3aed33SMarcel Moolenaar if (table->gpt_depth != 0) 7911d3aed33SMarcel Moolenaar return (ENXIO); 7921d3aed33SMarcel Moolenaar 7931d3aed33SMarcel Moolenaar pp = cp->provider; 7941d3aed33SMarcel Moolenaar 7951d3aed33SMarcel Moolenaar /* 7961d3aed33SMarcel Moolenaar * Sanity-check the provider. Since the first sector on the provider 7971d3aed33SMarcel Moolenaar * must be a PMBR and a PMBR is 512 bytes large, the sector size 7981d3aed33SMarcel Moolenaar * must be at least 512 bytes. Also, since the theoretical minimum 7991d3aed33SMarcel Moolenaar * number of sectors needed by GPT is 6, any medium that has less 8001d3aed33SMarcel Moolenaar * than 6 sectors is never going to be able to hold a GPT. The 8011d3aed33SMarcel Moolenaar * number 6 comes from: 8021d3aed33SMarcel Moolenaar * 1 sector for the PMBR 8031d3aed33SMarcel Moolenaar * 2 sectors for the GPT headers (each 1 sector) 8041d3aed33SMarcel Moolenaar * 2 sectors for the GPT tables (each 1 sector) 8051d3aed33SMarcel Moolenaar * 1 sector for an actual partition 8061d3aed33SMarcel Moolenaar * It's better to catch this pathological case early than behaving 8071d3aed33SMarcel Moolenaar * pathologically later on... 8081d3aed33SMarcel Moolenaar */ 8094d32fcb4SMarcel Moolenaar if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 8101d3aed33SMarcel Moolenaar return (ENOSPC); 8111d3aed33SMarcel Moolenaar 8123e5a0a6bSMarcel Moolenaar /* 8133e5a0a6bSMarcel Moolenaar * Check that there's a MBR or a PMBR. If it's a PMBR, we return 8143e5a0a6bSMarcel Moolenaar * as the highest priority on a match, otherwise we assume some 8153e5a0a6bSMarcel Moolenaar * GPT-unaware tool has destroyed the GPT by recreating a MBR and 8163e5a0a6bSMarcel Moolenaar * we really want the MBR scheme to take precedence. 8173e5a0a6bSMarcel Moolenaar */ 8181d3aed33SMarcel Moolenaar buf = g_read_data(cp, 0L, pp->sectorsize, &error); 8191d3aed33SMarcel Moolenaar if (buf == NULL) 8201d3aed33SMarcel Moolenaar return (error); 8211d3aed33SMarcel Moolenaar res = le16dec(buf + DOSMAGICOFFSET); 8223e5a0a6bSMarcel Moolenaar pri = G_PART_PROBE_PRI_LOW; 8233e5a0a6bSMarcel Moolenaar for (index = 0; index < NDOSPART; index++) { 8243e5a0a6bSMarcel Moolenaar if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee) 8253e5a0a6bSMarcel Moolenaar pri = G_PART_PROBE_PRI_HIGH; 8263e5a0a6bSMarcel Moolenaar } 8271d3aed33SMarcel Moolenaar g_free(buf); 8281d3aed33SMarcel Moolenaar if (res != DOSMAGIC) 8291d3aed33SMarcel Moolenaar return (ENXIO); 8301d3aed33SMarcel Moolenaar 8311d3aed33SMarcel Moolenaar /* Check that there's a primary header. */ 8321d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 8331d3aed33SMarcel Moolenaar if (buf == NULL) 8341d3aed33SMarcel Moolenaar return (error); 8351d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8361d3aed33SMarcel Moolenaar g_free(buf); 8371d3aed33SMarcel Moolenaar if (res == 0) 8383e5a0a6bSMarcel Moolenaar return (pri); 8391d3aed33SMarcel Moolenaar 8401d3aed33SMarcel Moolenaar /* No primary? Check that there's a secondary. */ 8411d3aed33SMarcel Moolenaar buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 8421d3aed33SMarcel Moolenaar &error); 8431d3aed33SMarcel Moolenaar if (buf == NULL) 8441d3aed33SMarcel Moolenaar return (error); 8451d3aed33SMarcel Moolenaar res = memcmp(buf, GPT_HDR_SIG, 8); 8461d3aed33SMarcel Moolenaar g_free(buf); 8473e5a0a6bSMarcel Moolenaar return ((res == 0) ? pri : ENXIO); 8481d3aed33SMarcel Moolenaar } 8491d3aed33SMarcel Moolenaar 8501d3aed33SMarcel Moolenaar static int 8511d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 8521d3aed33SMarcel Moolenaar { 8531c2dee3cSRobert Noland struct gpt_hdr *prihdr, *sechdr; 8541d3aed33SMarcel Moolenaar struct gpt_ent *tbl, *pritbl, *sectbl; 8551d3aed33SMarcel Moolenaar struct g_provider *pp; 8561d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 8571d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 8584d32fcb4SMarcel Moolenaar u_char *buf; 859e7926a37SAndrey V. Elsukov uint64_t last; 8604d32fcb4SMarcel Moolenaar int error, index; 8611d3aed33SMarcel Moolenaar 8621d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 8631d3aed33SMarcel Moolenaar pp = cp->provider; 864e7926a37SAndrey V. Elsukov last = (pp->mediasize / pp->sectorsize) - 1; 8651d3aed33SMarcel Moolenaar 8664d32fcb4SMarcel Moolenaar /* Read the PMBR */ 8674d32fcb4SMarcel Moolenaar buf = g_read_data(cp, 0, pp->sectorsize, &error); 8684d32fcb4SMarcel Moolenaar if (buf == NULL) 8694d32fcb4SMarcel Moolenaar return (error); 8704d32fcb4SMarcel Moolenaar bcopy(buf, table->mbr, MBRSIZE); 8714d32fcb4SMarcel Moolenaar g_free(buf); 8724d32fcb4SMarcel Moolenaar 8731d3aed33SMarcel Moolenaar /* Read the primary header and table. */ 8741c2dee3cSRobert Noland prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 8751d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 8761c2dee3cSRobert Noland pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 8771d3aed33SMarcel Moolenaar } else { 8781d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 8791d3aed33SMarcel Moolenaar pritbl = NULL; 8801d3aed33SMarcel Moolenaar } 8811d3aed33SMarcel Moolenaar 8821d3aed33SMarcel Moolenaar /* Read the secondary header and table. */ 8831c2dee3cSRobert Noland sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 8841d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 8851c2dee3cSRobert Noland sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 8861d3aed33SMarcel Moolenaar } else { 8871d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 8881d3aed33SMarcel Moolenaar sectbl = NULL; 8891d3aed33SMarcel Moolenaar } 8901d3aed33SMarcel Moolenaar 8911d3aed33SMarcel Moolenaar /* Fail if we haven't got any good tables at all. */ 8921d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 8931d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 8941d3aed33SMarcel Moolenaar printf("GEOM: %s: corrupt or invalid GPT detected.\n", 8951d3aed33SMarcel Moolenaar pp->name); 8961d3aed33SMarcel Moolenaar printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 8971d3aed33SMarcel Moolenaar pp->name); 8981d3aed33SMarcel Moolenaar return (EINVAL); 8991d3aed33SMarcel Moolenaar } 9001d3aed33SMarcel Moolenaar 9011d3aed33SMarcel Moolenaar /* 9021d3aed33SMarcel Moolenaar * If both headers are good but they disagree with each other, 9031d3aed33SMarcel Moolenaar * then invalidate one. We prefer to keep the primary header, 9041d3aed33SMarcel Moolenaar * unless the primary table is corrupt. 9051d3aed33SMarcel Moolenaar */ 9061d3aed33SMarcel Moolenaar if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 9071d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 9081c2dee3cSRobert Noland !gpt_matched_hdrs(prihdr, sechdr)) { 909dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 9101d3aed33SMarcel Moolenaar table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 911dd0db05dSMarcel Moolenaar table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 9121c2dee3cSRobert Noland g_free(sechdr); 9131c2dee3cSRobert Noland sechdr = NULL; 914dd0db05dSMarcel Moolenaar } else { 9151d3aed33SMarcel Moolenaar table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 916dd0db05dSMarcel Moolenaar table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 9171c2dee3cSRobert Noland g_free(prihdr); 9181c2dee3cSRobert Noland prihdr = NULL; 919dd0db05dSMarcel Moolenaar } 9201d3aed33SMarcel Moolenaar } 9211d3aed33SMarcel Moolenaar 922dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 9231d3aed33SMarcel Moolenaar printf("GEOM: %s: the primary GPT table is corrupt or " 9241d3aed33SMarcel Moolenaar "invalid.\n", pp->name); 9251d3aed33SMarcel Moolenaar printf("GEOM: %s: using the secondary instead -- recovery " 9261d3aed33SMarcel Moolenaar "strongly advised.\n", pp->name); 9271d3aed33SMarcel Moolenaar table->hdr = sechdr; 928e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9291c2dee3cSRobert Noland if (prihdr != NULL) 9301c2dee3cSRobert Noland g_free(prihdr); 9311d3aed33SMarcel Moolenaar tbl = sectbl; 9321d3aed33SMarcel Moolenaar if (pritbl != NULL) 9331d3aed33SMarcel Moolenaar g_free(pritbl); 9341d3aed33SMarcel Moolenaar } else { 935dd0db05dSMarcel Moolenaar if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 9361d3aed33SMarcel Moolenaar printf("GEOM: %s: the secondary GPT table is corrupt " 9371d3aed33SMarcel Moolenaar "or invalid.\n", pp->name); 9381d3aed33SMarcel Moolenaar printf("GEOM: %s: using the primary only -- recovery " 9391d3aed33SMarcel Moolenaar "suggested.\n", pp->name); 940e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 941e7926a37SAndrey V. Elsukov } else if (table->lba[GPT_ELT_SECHDR] != last) { 942e7926a37SAndrey V. Elsukov printf( "GEOM: %s: the secondary GPT header is not in " 943e7926a37SAndrey V. Elsukov "the last LBA.\n", pp->name); 944e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 1; 9451d3aed33SMarcel Moolenaar } 9461d3aed33SMarcel Moolenaar table->hdr = prihdr; 9471c2dee3cSRobert Noland if (sechdr != NULL) 9481c2dee3cSRobert Noland g_free(sechdr); 9491d3aed33SMarcel Moolenaar tbl = pritbl; 9501d3aed33SMarcel Moolenaar if (sectbl != NULL) 9511d3aed33SMarcel Moolenaar g_free(sectbl); 9521d3aed33SMarcel Moolenaar } 9531d3aed33SMarcel Moolenaar 9541c2dee3cSRobert Noland basetable->gpt_first = table->hdr->hdr_lba_start; 9551c2dee3cSRobert Noland basetable->gpt_last = table->hdr->hdr_lba_end; 956b74dd6c7SAndrey V. Elsukov basetable->gpt_entries = (table->hdr->hdr_lba_start - 2) * 957b74dd6c7SAndrey V. Elsukov pp->sectorsize / table->hdr->hdr_entsz; 9581d3aed33SMarcel Moolenaar 959b74dd6c7SAndrey V. Elsukov for (index = table->hdr->hdr_entries - 1; index >= 0; index--) { 9601d3aed33SMarcel Moolenaar if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 9611d3aed33SMarcel Moolenaar continue; 962e7926a37SAndrey V. Elsukov entry = (struct g_part_gpt_entry *)g_part_new_entry( 963e7926a37SAndrey V. Elsukov basetable, index + 1, tbl[index].ent_lba_start, 964e7926a37SAndrey V. Elsukov tbl[index].ent_lba_end); 9651d3aed33SMarcel Moolenaar entry->ent = tbl[index]; 9661d3aed33SMarcel Moolenaar } 9671d3aed33SMarcel Moolenaar 9681d3aed33SMarcel Moolenaar g_free(tbl); 969369fe59dSMarcel Moolenaar 970369fe59dSMarcel Moolenaar /* 971369fe59dSMarcel Moolenaar * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 972369fe59dSMarcel Moolenaar * if (and only if) any FAT32 or FAT16 partitions have been 973369fe59dSMarcel Moolenaar * created. This happens irrespective of whether Boot Camp is 974369fe59dSMarcel Moolenaar * used/enabled, though it's generally understood to be done 975369fe59dSMarcel Moolenaar * to support legacy Windows under Boot Camp. We refer to this 976369fe59dSMarcel Moolenaar * mirroring simply as Boot Camp. We try to detect Boot Camp 977369fe59dSMarcel Moolenaar * so that we can update the MBR if and when GPT changes have 978369fe59dSMarcel Moolenaar * been made. Note that we do not enable Boot Camp if not 979369fe59dSMarcel Moolenaar * previously enabled because we can't assume that we're on a 980369fe59dSMarcel Moolenaar * Mac alongside Mac OS X. 981369fe59dSMarcel Moolenaar */ 982369fe59dSMarcel Moolenaar table->bootcamp = gpt_is_bootcamp(table, pp->name); 983369fe59dSMarcel Moolenaar 9841d3aed33SMarcel Moolenaar return (0); 9851d3aed33SMarcel Moolenaar } 9861d3aed33SMarcel Moolenaar 987cec283baSPawel Jakub Dawidek static int 988e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable) 989e7926a37SAndrey V. Elsukov { 9903bd22a9cSMarcel Moolenaar struct g_part_gpt_table *table; 9913bd22a9cSMarcel Moolenaar struct g_provider *pp; 992e7926a37SAndrey V. Elsukov 9933bd22a9cSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 9943bd22a9cSMarcel Moolenaar pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 9953bd22a9cSMarcel Moolenaar gpt_create_pmbr(table, pp); 9963bd22a9cSMarcel Moolenaar g_gpt_set_defaults(basetable, pp); 997e7926a37SAndrey V. Elsukov basetable->gpt_corrupt = 0; 998e7926a37SAndrey V. Elsukov return (0); 999e7926a37SAndrey V. Elsukov } 1000e7926a37SAndrey V. Elsukov 1001e7926a37SAndrey V. Elsukov static int 1002369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable, 1003369fe59dSMarcel Moolenaar struct g_part_entry *baseentry, const char *attrib, unsigned int set) 1004cec283baSPawel Jakub Dawidek { 1005cec283baSPawel Jakub Dawidek struct g_part_gpt_entry *entry; 1006369fe59dSMarcel Moolenaar struct g_part_gpt_table *table; 10073bd22a9cSMarcel Moolenaar uint8_t *p; 1008369fe59dSMarcel Moolenaar uint64_t attr; 1009369fe59dSMarcel Moolenaar int i; 1010cec283baSPawel Jakub Dawidek 1011369fe59dSMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 1012369fe59dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 1013369fe59dSMarcel Moolenaar 1014369fe59dSMarcel Moolenaar if (strcasecmp(attrib, "active") == 0) { 10153bd22a9cSMarcel Moolenaar if (table->bootcamp) { 10163bd22a9cSMarcel Moolenaar /* The active flag must be set on a valid entry. */ 10173bd22a9cSMarcel Moolenaar if (entry == NULL) 10183bd22a9cSMarcel Moolenaar return (ENXIO); 10193bd22a9cSMarcel Moolenaar if (baseentry->gpe_index > NDOSPART) 1020369fe59dSMarcel Moolenaar return (EINVAL); 1021369fe59dSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 10223bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 10233bd22a9cSMarcel Moolenaar p[0] = (i == baseentry->gpe_index - 1) 10243bd22a9cSMarcel Moolenaar ? ((set) ? 0x80 : 0) : 0; 10253bd22a9cSMarcel Moolenaar } 10263bd22a9cSMarcel Moolenaar } else { 10273bd22a9cSMarcel Moolenaar /* The PMBR is marked as active without an entry. */ 10283bd22a9cSMarcel Moolenaar if (entry != NULL) 10293bd22a9cSMarcel Moolenaar return (ENXIO); 10303bd22a9cSMarcel Moolenaar for (i = 0; i < NDOSPART; i++) { 10313bd22a9cSMarcel Moolenaar p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 10323bd22a9cSMarcel Moolenaar p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 10333bd22a9cSMarcel Moolenaar } 1034369fe59dSMarcel Moolenaar } 1035369fe59dSMarcel Moolenaar return (0); 1036369fe59dSMarcel Moolenaar } 1037369fe59dSMarcel Moolenaar 10383bd22a9cSMarcel Moolenaar if (entry == NULL) 10393bd22a9cSMarcel Moolenaar return (ENODEV); 10403bd22a9cSMarcel Moolenaar 1041369fe59dSMarcel Moolenaar attr = 0; 1042cec283baSPawel Jakub Dawidek if (strcasecmp(attrib, "bootme") == 0) { 1043369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1044cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootonce") == 0) { 1045369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTONCE; 1046cec283baSPawel Jakub Dawidek if (set) 1047369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTME; 1048cec283baSPawel Jakub Dawidek } else if (strcasecmp(attrib, "bootfailed") == 0) { 1049cec283baSPawel Jakub Dawidek /* 1050cec283baSPawel Jakub Dawidek * It should only be possible to unset BOOTFAILED, but it might 1051cec283baSPawel Jakub Dawidek * be useful for test purposes to also be able to set it. 1052cec283baSPawel Jakub Dawidek */ 1053369fe59dSMarcel Moolenaar attr |= GPT_ENT_ATTR_BOOTFAILED; 1054cec283baSPawel Jakub Dawidek } 1055369fe59dSMarcel Moolenaar if (attr == 0) 1056cec283baSPawel Jakub Dawidek return (EINVAL); 1057cec283baSPawel Jakub Dawidek 1058369fe59dSMarcel Moolenaar if (set) 1059369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr | attr; 1060369fe59dSMarcel Moolenaar else 1061369fe59dSMarcel Moolenaar attr = entry->ent.ent_attr & ~attr; 1062369fe59dSMarcel Moolenaar if (attr != entry->ent.ent_attr) { 1063369fe59dSMarcel Moolenaar entry->ent.ent_attr = attr; 1064369fe59dSMarcel Moolenaar if (!baseentry->gpe_created) 1065369fe59dSMarcel Moolenaar baseentry->gpe_modified = 1; 1066cec283baSPawel Jakub Dawidek } 1067cec283baSPawel Jakub Dawidek return (0); 1068cec283baSPawel Jakub Dawidek } 1069cec283baSPawel Jakub Dawidek 10701d3aed33SMarcel Moolenaar static const char * 10711d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 10721d3aed33SMarcel Moolenaar char *buf, size_t bufsz) 10731d3aed33SMarcel Moolenaar { 10741d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 10751d3aed33SMarcel Moolenaar struct uuid *type; 1076f1317430SRui Paulo struct g_part_uuid_alias *uap; 10771d3aed33SMarcel Moolenaar 10781d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 10791d3aed33SMarcel Moolenaar type = &entry->ent.ent_type; 1080f1317430SRui Paulo for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1081f1317430SRui Paulo if (EQUUID(type, uap->uuid)) 1082f1317430SRui Paulo return (g_part_alias_name(uap->alias)); 1083cf231470SMarcel Moolenaar buf[0] = '!'; 1084cf231470SMarcel Moolenaar snprintf_uuid(buf + 1, bufsz - 1, type); 1085f1317430SRui Paulo 10861d3aed33SMarcel Moolenaar return (buf); 10871d3aed33SMarcel Moolenaar } 10881d3aed33SMarcel Moolenaar 10891d3aed33SMarcel Moolenaar static int 10901d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 10911d3aed33SMarcel Moolenaar { 10921d3aed33SMarcel Moolenaar unsigned char *buf, *bp; 10931d3aed33SMarcel Moolenaar struct g_provider *pp; 10941d3aed33SMarcel Moolenaar struct g_part_entry *baseentry; 10951d3aed33SMarcel Moolenaar struct g_part_gpt_entry *entry; 10961d3aed33SMarcel Moolenaar struct g_part_gpt_table *table; 1097e7926a37SAndrey V. Elsukov size_t tblsz; 10981d3aed33SMarcel Moolenaar uint32_t crc; 10991d3aed33SMarcel Moolenaar int error, index; 11001d3aed33SMarcel Moolenaar 11011d3aed33SMarcel Moolenaar pp = cp->provider; 11021d3aed33SMarcel Moolenaar table = (struct g_part_gpt_table *)basetable; 1103e7926a37SAndrey V. Elsukov tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz + 11041d3aed33SMarcel Moolenaar pp->sectorsize - 1) / pp->sectorsize; 11051d3aed33SMarcel Moolenaar 1106369fe59dSMarcel Moolenaar /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1107369fe59dSMarcel Moolenaar if (table->bootcamp) 11083bd22a9cSMarcel Moolenaar gpt_update_bootcamp(basetable, pp); 1109d4746e10SAndrey V. Elsukov 11104d32fcb4SMarcel Moolenaar /* Write the PMBR */ 11111d3aed33SMarcel Moolenaar buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 11124d32fcb4SMarcel Moolenaar bcopy(table->mbr, buf, MBRSIZE); 11131d3aed33SMarcel Moolenaar error = g_write_data(cp, 0, buf, pp->sectorsize); 11141d3aed33SMarcel Moolenaar g_free(buf); 11151d3aed33SMarcel Moolenaar if (error) 11161d3aed33SMarcel Moolenaar return (error); 11171d3aed33SMarcel Moolenaar 11181d3aed33SMarcel Moolenaar /* Allocate space for the header and entries. */ 1119e7926a37SAndrey V. Elsukov buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 11201d3aed33SMarcel Moolenaar 11211c2dee3cSRobert Noland memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 11221c2dee3cSRobert Noland le32enc(buf + 8, table->hdr->hdr_revision); 11231c2dee3cSRobert Noland le32enc(buf + 12, table->hdr->hdr_size); 11241c2dee3cSRobert Noland le64enc(buf + 40, table->hdr->hdr_lba_start); 11251c2dee3cSRobert Noland le64enc(buf + 48, table->hdr->hdr_lba_end); 11261c2dee3cSRobert Noland le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 11271c2dee3cSRobert Noland le32enc(buf + 80, table->hdr->hdr_entries); 11281c2dee3cSRobert Noland le32enc(buf + 84, table->hdr->hdr_entsz); 11291d3aed33SMarcel Moolenaar 11301d3aed33SMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1131d287f590SMarcel Moolenaar if (baseentry->gpe_deleted) 1132d287f590SMarcel Moolenaar continue; 11331d3aed33SMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 11341d3aed33SMarcel Moolenaar index = baseentry->gpe_index - 1; 11351c2dee3cSRobert Noland bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 11361d3aed33SMarcel Moolenaar le_uuid_enc(bp, &entry->ent.ent_type); 11371d3aed33SMarcel Moolenaar le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 11381d3aed33SMarcel Moolenaar le64enc(bp + 32, entry->ent.ent_lba_start); 11391d3aed33SMarcel Moolenaar le64enc(bp + 40, entry->ent.ent_lba_end); 11401d3aed33SMarcel Moolenaar le64enc(bp + 48, entry->ent.ent_attr); 11411d3aed33SMarcel Moolenaar memcpy(bp + 56, entry->ent.ent_name, 11421d3aed33SMarcel Moolenaar sizeof(entry->ent.ent_name)); 11431d3aed33SMarcel Moolenaar } 11441d3aed33SMarcel Moolenaar 11451d3aed33SMarcel Moolenaar crc = crc32(buf + pp->sectorsize, 11461c2dee3cSRobert Noland table->hdr->hdr_entries * table->hdr->hdr_entsz); 11471d3aed33SMarcel Moolenaar le32enc(buf + 88, crc); 11481d3aed33SMarcel Moolenaar 11491d3aed33SMarcel Moolenaar /* Write primary meta-data. */ 11501d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 11511d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 11521d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 11531d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 11541c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 11551d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 11561d3aed33SMarcel Moolenaar 1157799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1158799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1159799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1160799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1161799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1162799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 11631d3aed33SMarcel Moolenaar if (error) 11641d3aed33SMarcel Moolenaar goto out; 1165799eac8cSAndrey V. Elsukov } 11661d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 11671d3aed33SMarcel Moolenaar buf, pp->sectorsize); 11681d3aed33SMarcel Moolenaar if (error) 11691d3aed33SMarcel Moolenaar goto out; 11701d3aed33SMarcel Moolenaar 11711d3aed33SMarcel Moolenaar /* Write secondary meta-data. */ 11721d3aed33SMarcel Moolenaar le32enc(buf + 16, 0); /* hdr_crc_self. */ 11731d3aed33SMarcel Moolenaar le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 11741d3aed33SMarcel Moolenaar le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 11751d3aed33SMarcel Moolenaar le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 11761c2dee3cSRobert Noland crc = crc32(buf, table->hdr->hdr_size); 11771d3aed33SMarcel Moolenaar le32enc(buf + 16, crc); 11781d3aed33SMarcel Moolenaar 1179799eac8cSAndrey V. Elsukov for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1180799eac8cSAndrey V. Elsukov error = g_write_data(cp, 1181799eac8cSAndrey V. Elsukov (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1182799eac8cSAndrey V. Elsukov buf + (index + 1) * pp->sectorsize, 1183799eac8cSAndrey V. Elsukov (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1184799eac8cSAndrey V. Elsukov (tblsz - index) * pp->sectorsize); 11851d3aed33SMarcel Moolenaar if (error) 11861d3aed33SMarcel Moolenaar goto out; 1187799eac8cSAndrey V. Elsukov } 11881d3aed33SMarcel Moolenaar error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 11891d3aed33SMarcel Moolenaar buf, pp->sectorsize); 11901d3aed33SMarcel Moolenaar 11911d3aed33SMarcel Moolenaar out: 11921d3aed33SMarcel Moolenaar g_free(buf); 11931d3aed33SMarcel Moolenaar return (error); 11941d3aed33SMarcel Moolenaar } 11951d3aed33SMarcel Moolenaar 11961d3aed33SMarcel Moolenaar static void 11972b9be055SAndrey V. Elsukov g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 11982b9be055SAndrey V. Elsukov { 11994787115dSMarcel Moolenaar struct g_part_entry *baseentry; 12004787115dSMarcel Moolenaar struct g_part_gpt_entry *entry; 12012b9be055SAndrey V. Elsukov struct g_part_gpt_table *table; 12024787115dSMarcel Moolenaar quad_t start, end, min, max; 12034787115dSMarcel Moolenaar quad_t lba, last; 12044787115dSMarcel Moolenaar size_t spb, tblsz; 12052b9be055SAndrey V. Elsukov 12062b9be055SAndrey V. Elsukov table = (struct g_part_gpt_table *)basetable; 12072b9be055SAndrey V. Elsukov last = pp->mediasize / pp->sectorsize - 1; 12082b9be055SAndrey V. Elsukov tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 12092b9be055SAndrey V. Elsukov pp->sectorsize - 1) / pp->sectorsize; 12102b9be055SAndrey V. Elsukov 12112b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRIHDR] = 1; 12122b9be055SAndrey V. Elsukov table->lba[GPT_ELT_PRITBL] = 2; 12132b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECHDR] = last; 12142b9be055SAndrey V. Elsukov table->lba[GPT_ELT_SECTBL] = last - tblsz; 12152b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 12162b9be055SAndrey V. Elsukov table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 12172b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 12182b9be055SAndrey V. Elsukov table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 12192b9be055SAndrey V. Elsukov 12204787115dSMarcel Moolenaar max = start = 2 + tblsz; 12214787115dSMarcel Moolenaar min = end = last - tblsz - 1; 12224787115dSMarcel Moolenaar LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 12234787115dSMarcel Moolenaar if (baseentry->gpe_deleted) 12244787115dSMarcel Moolenaar continue; 12254787115dSMarcel Moolenaar entry = (struct g_part_gpt_entry *)baseentry; 12264787115dSMarcel Moolenaar if (entry->ent.ent_lba_start < min) 12274787115dSMarcel Moolenaar min = entry->ent.ent_lba_start; 12284787115dSMarcel Moolenaar if (entry->ent.ent_lba_end > max) 12294787115dSMarcel Moolenaar max = entry->ent.ent_lba_end; 12304787115dSMarcel Moolenaar } 12314787115dSMarcel Moolenaar spb = 4096 / pp->sectorsize; 12324787115dSMarcel Moolenaar if (spb > 1) { 12334787115dSMarcel Moolenaar lba = start + ((start % spb) ? spb - start % spb : 0); 12344787115dSMarcel Moolenaar if (lba <= min) 12354787115dSMarcel Moolenaar start = lba; 12364787115dSMarcel Moolenaar lba = end - (end + 1) % spb; 12374787115dSMarcel Moolenaar if (max <= lba) 12384787115dSMarcel Moolenaar end = lba; 12394787115dSMarcel Moolenaar } 12404787115dSMarcel Moolenaar table->hdr->hdr_lba_start = start; 12414787115dSMarcel Moolenaar table->hdr->hdr_lba_end = end; 12422b9be055SAndrey V. Elsukov 12434787115dSMarcel Moolenaar basetable->gpt_first = start; 12444787115dSMarcel Moolenaar basetable->gpt_last = end; 12452b9be055SAndrey V. Elsukov } 12462b9be055SAndrey V. Elsukov 12472b9be055SAndrey V. Elsukov static void 1248d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 12491d3aed33SMarcel Moolenaar { 12501d3aed33SMarcel Moolenaar u_int bo; 12511d3aed33SMarcel Moolenaar uint32_t ch; 12521d3aed33SMarcel Moolenaar uint16_t c; 12531d3aed33SMarcel Moolenaar 1254d3532631SMarcel Moolenaar bo = LITTLE_ENDIAN; /* GPT is little-endian */ 12551d3aed33SMarcel Moolenaar while (len > 0 && *str != 0) { 12561d3aed33SMarcel Moolenaar ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 12571d3aed33SMarcel Moolenaar str++, len--; 12581d3aed33SMarcel Moolenaar if ((ch & 0xf800) == 0xd800) { 12591d3aed33SMarcel Moolenaar if (len > 0) { 12601d3aed33SMarcel Moolenaar c = (bo == BIG_ENDIAN) ? be16toh(*str) 12611d3aed33SMarcel Moolenaar : le16toh(*str); 12621d3aed33SMarcel Moolenaar str++, len--; 12631d3aed33SMarcel Moolenaar } else 12641d3aed33SMarcel Moolenaar c = 0xfffd; 12651d3aed33SMarcel Moolenaar if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 12661d3aed33SMarcel Moolenaar ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 12671d3aed33SMarcel Moolenaar ch += 0x10000; 12681d3aed33SMarcel Moolenaar } else 12691d3aed33SMarcel Moolenaar ch = 0xfffd; 12701d3aed33SMarcel Moolenaar } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 12711d3aed33SMarcel Moolenaar bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 12721d3aed33SMarcel Moolenaar continue; 12731d3aed33SMarcel Moolenaar } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 12741d3aed33SMarcel Moolenaar continue; 12751d3aed33SMarcel Moolenaar 1276d3532631SMarcel Moolenaar /* Write the Unicode character in UTF-8 */ 12771d3aed33SMarcel Moolenaar if (ch < 0x80) 127839dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c", ch); 12791d3aed33SMarcel Moolenaar else if (ch < 0x800) 128039dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6), 12811d3aed33SMarcel Moolenaar 0x80 | (ch & 0x3f)); 12821d3aed33SMarcel Moolenaar else if (ch < 0x10000) 128339dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12), 12841d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 12851d3aed33SMarcel Moolenaar else if (ch < 0x200000) 128639dcac84SAndrey V. Elsukov g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 | 128739dcac84SAndrey V. Elsukov (ch >> 18), 0x80 | ((ch >> 12) & 0x3f), 12881d3aed33SMarcel Moolenaar 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 12891d3aed33SMarcel Moolenaar } 12901d3aed33SMarcel Moolenaar } 1291d3532631SMarcel Moolenaar 1292d3532631SMarcel Moolenaar static void 1293d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1294d3532631SMarcel Moolenaar { 1295d3532631SMarcel Moolenaar size_t s16idx, s8idx; 1296d3532631SMarcel Moolenaar uint32_t utfchar; 1297d3532631SMarcel Moolenaar unsigned int c, utfbytes; 1298d3532631SMarcel Moolenaar 1299d3532631SMarcel Moolenaar s8idx = s16idx = 0; 1300d3532631SMarcel Moolenaar utfchar = 0; 1301d3532631SMarcel Moolenaar utfbytes = 0; 1302d3532631SMarcel Moolenaar bzero(s16, s16len << 1); 1303d3532631SMarcel Moolenaar while (s8[s8idx] != 0 && s16idx < s16len) { 1304d3532631SMarcel Moolenaar c = s8[s8idx++]; 1305d3532631SMarcel Moolenaar if ((c & 0xc0) != 0x80) { 1306d3532631SMarcel Moolenaar /* Initial characters. */ 1307d3532631SMarcel Moolenaar if (utfbytes != 0) { 1308d3532631SMarcel Moolenaar /* Incomplete encoding of previous char. */ 1309d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1310d3532631SMarcel Moolenaar } 1311d3532631SMarcel Moolenaar if ((c & 0xf8) == 0xf0) { 1312d3532631SMarcel Moolenaar utfchar = c & 0x07; 1313d3532631SMarcel Moolenaar utfbytes = 3; 1314d3532631SMarcel Moolenaar } else if ((c & 0xf0) == 0xe0) { 1315d3532631SMarcel Moolenaar utfchar = c & 0x0f; 1316d3532631SMarcel Moolenaar utfbytes = 2; 1317d3532631SMarcel Moolenaar } else if ((c & 0xe0) == 0xc0) { 1318d3532631SMarcel Moolenaar utfchar = c & 0x1f; 1319d3532631SMarcel Moolenaar utfbytes = 1; 1320d3532631SMarcel Moolenaar } else { 1321d3532631SMarcel Moolenaar utfchar = c & 0x7f; 1322d3532631SMarcel Moolenaar utfbytes = 0; 1323d3532631SMarcel Moolenaar } 1324d3532631SMarcel Moolenaar } else { 1325d3532631SMarcel Moolenaar /* Followup characters. */ 1326d3532631SMarcel Moolenaar if (utfbytes > 0) { 1327d3532631SMarcel Moolenaar utfchar = (utfchar << 6) + (c & 0x3f); 1328d3532631SMarcel Moolenaar utfbytes--; 1329d3532631SMarcel Moolenaar } else if (utfbytes == 0) 1330d3532631SMarcel Moolenaar utfbytes = ~0; 1331d3532631SMarcel Moolenaar } 1332d3532631SMarcel Moolenaar /* 1333d3532631SMarcel Moolenaar * Write the complete Unicode character as UTF-16 when we 1334d3532631SMarcel Moolenaar * have all the UTF-8 charactars collected. 1335d3532631SMarcel Moolenaar */ 1336d3532631SMarcel Moolenaar if (utfbytes == 0) { 1337d3532631SMarcel Moolenaar /* 1338d3532631SMarcel Moolenaar * If we need to write 2 UTF-16 characters, but 1339d3532631SMarcel Moolenaar * we only have room for 1, then we truncate the 1340d3532631SMarcel Moolenaar * string by writing a 0 instead. 1341d3532631SMarcel Moolenaar */ 1342d3532631SMarcel Moolenaar if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1343d3532631SMarcel Moolenaar s16[s16idx++] = 1344d3532631SMarcel Moolenaar htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1345d3532631SMarcel Moolenaar s16[s16idx++] = 1346d3532631SMarcel Moolenaar htole16(0xdc00 | (utfchar & 0x3ff)); 1347d3532631SMarcel Moolenaar } else 1348d3532631SMarcel Moolenaar s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1349d3532631SMarcel Moolenaar htole16(utfchar); 1350d3532631SMarcel Moolenaar } 1351d3532631SMarcel Moolenaar } 1352d3532631SMarcel Moolenaar /* 1353d3532631SMarcel Moolenaar * If our input string was truncated, append an invalid encoding 1354d3532631SMarcel Moolenaar * character to the output string. 1355d3532631SMarcel Moolenaar */ 1356d3532631SMarcel Moolenaar if (utfbytes != 0 && s16idx < s16len) 1357d3532631SMarcel Moolenaar s16[s16idx++] = htole16(0xfffd); 1358d3532631SMarcel Moolenaar } 1359