xref: /freebsd/sys/geom/part/g_part_gpt.c (revision 2e543af13ab3746c7626c53293c007c8747eff9d)
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>
35f89d2072SXin LI #include <sys/gsb_crc32.h>
361d3aed33SMarcel Moolenaar #include <sys/endian.h>
371d3aed33SMarcel Moolenaar #include <sys/gpt.h>
381d3aed33SMarcel Moolenaar #include <sys/kernel.h>
391d3aed33SMarcel Moolenaar #include <sys/kobj.h>
401d3aed33SMarcel Moolenaar #include <sys/limits.h>
411d3aed33SMarcel Moolenaar #include <sys/lock.h>
421d3aed33SMarcel Moolenaar #include <sys/malloc.h>
431d3aed33SMarcel Moolenaar #include <sys/mutex.h>
441d3aed33SMarcel Moolenaar #include <sys/queue.h>
451d3aed33SMarcel Moolenaar #include <sys/sbuf.h>
461d3aed33SMarcel Moolenaar #include <sys/systm.h>
47cb08c2ccSAlexander Leidinger #include <sys/sysctl.h>
481d3aed33SMarcel Moolenaar #include <sys/uuid.h>
491d3aed33SMarcel Moolenaar #include <geom/geom.h>
5039dcac84SAndrey V. Elsukov #include <geom/geom_int.h>
511d3aed33SMarcel Moolenaar #include <geom/part/g_part.h>
521d3aed33SMarcel Moolenaar 
531d3aed33SMarcel Moolenaar #include "g_part_if.h"
541d3aed33SMarcel Moolenaar 
55cb08c2ccSAlexander Leidinger FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
56cb08c2ccSAlexander Leidinger 
57ef03f57dSKyle Evans SYSCTL_DECL(_kern_geom_part);
587029da5cSPawel Biernacki static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt,
597029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60ef03f57dSKyle Evans     "GEOM_PART_GPT GUID Partition Table");
61ef03f57dSKyle Evans 
62ef03f57dSKyle Evans static u_int allow_nesting = 0;
63ef03f57dSKyle Evans SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting,
64ef03f57dSKyle Evans     CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes");
65ef03f57dSKyle Evans 
661d3aed33SMarcel Moolenaar CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
671d3aed33SMarcel Moolenaar CTASSERT(sizeof(struct gpt_ent) == 128);
681d3aed33SMarcel Moolenaar 
69cea05ed9SEugene Grosbein extern u_int geom_part_check_integrity;
70cea05ed9SEugene Grosbein 
711d3aed33SMarcel Moolenaar #define	EQUUID(a,b)	(memcmp(a, b, sizeof(struct uuid)) == 0)
721d3aed33SMarcel Moolenaar 
734d32fcb4SMarcel Moolenaar #define	MBRSIZE		512
744d32fcb4SMarcel Moolenaar 
751d3aed33SMarcel Moolenaar enum gpt_elt {
761d3aed33SMarcel Moolenaar 	GPT_ELT_PRIHDR,
771d3aed33SMarcel Moolenaar 	GPT_ELT_PRITBL,
781d3aed33SMarcel Moolenaar 	GPT_ELT_SECHDR,
791d3aed33SMarcel Moolenaar 	GPT_ELT_SECTBL,
801d3aed33SMarcel Moolenaar 	GPT_ELT_COUNT
811d3aed33SMarcel Moolenaar };
821d3aed33SMarcel Moolenaar 
831d3aed33SMarcel Moolenaar enum gpt_state {
841d3aed33SMarcel Moolenaar 	GPT_STATE_UNKNOWN,	/* Not determined. */
851d3aed33SMarcel Moolenaar 	GPT_STATE_MISSING,	/* No signature found. */
861d3aed33SMarcel Moolenaar 	GPT_STATE_CORRUPT,	/* Checksum mismatch. */
871d3aed33SMarcel Moolenaar 	GPT_STATE_INVALID,	/* Nonconformant/invalid. */
885be5d0d5SZhenlei Huang 	GPT_STATE_UNSUPPORTED,  /* Not supported. */
891d3aed33SMarcel Moolenaar 	GPT_STATE_OK		/* Perfectly fine. */
901d3aed33SMarcel Moolenaar };
911d3aed33SMarcel Moolenaar 
921d3aed33SMarcel Moolenaar struct g_part_gpt_table {
931d3aed33SMarcel Moolenaar 	struct g_part_table	base;
944d32fcb4SMarcel Moolenaar 	u_char			mbr[MBRSIZE];
951c2dee3cSRobert Noland 	struct gpt_hdr		*hdr;
961d3aed33SMarcel Moolenaar 	quad_t			lba[GPT_ELT_COUNT];
971d3aed33SMarcel Moolenaar 	enum gpt_state		state[GPT_ELT_COUNT];
98369fe59dSMarcel Moolenaar 	int			bootcamp;
991d3aed33SMarcel Moolenaar };
1001d3aed33SMarcel Moolenaar 
1011d3aed33SMarcel Moolenaar struct g_part_gpt_entry {
1021d3aed33SMarcel Moolenaar 	struct g_part_entry	base;
1031d3aed33SMarcel Moolenaar 	struct gpt_ent		ent;
1041d3aed33SMarcel Moolenaar };
1051d3aed33SMarcel Moolenaar 
106d3532631SMarcel Moolenaar static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
107d3532631SMarcel Moolenaar static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
1082b9be055SAndrey V. Elsukov static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *);
109d3532631SMarcel Moolenaar 
1101d3aed33SMarcel Moolenaar static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
1111d3aed33SMarcel Moolenaar     struct g_part_parms *);
1124d32fcb4SMarcel Moolenaar static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
1131d3aed33SMarcel Moolenaar static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
1141d3aed33SMarcel Moolenaar static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
11551f53a08SWarner Losh static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
1165db67052SMarcel Moolenaar     struct sbuf *, const char *);
1171d3aed33SMarcel Moolenaar static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
1181d3aed33SMarcel Moolenaar static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
1191d3aed33SMarcel Moolenaar     struct g_part_parms *);
12051f53a08SWarner Losh static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
1211d3aed33SMarcel Moolenaar     char *, size_t);
1221d3aed33SMarcel Moolenaar static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
1231d3aed33SMarcel Moolenaar static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
124cec283baSPawel Jakub Dawidek static int g_part_gpt_setunset(struct g_part_table *table,
125cec283baSPawel Jakub Dawidek     struct g_part_entry *baseentry, const char *attrib, unsigned int set);
1261d3aed33SMarcel Moolenaar static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
1271d3aed33SMarcel Moolenaar     char *, size_t);
1281d3aed33SMarcel Moolenaar static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
1293f71c319SMarcel Moolenaar static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
1303f71c319SMarcel Moolenaar     struct g_part_parms *);
131e7926a37SAndrey V. Elsukov static int g_part_gpt_recover(struct g_part_table *);
1321d3aed33SMarcel Moolenaar 
1331d3aed33SMarcel Moolenaar static kobj_method_t g_part_gpt_methods[] = {
1341d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_add,		g_part_gpt_add),
1354d32fcb4SMarcel Moolenaar 	KOBJMETHOD(g_part_bootcode,	g_part_gpt_bootcode),
1361d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_create,	g_part_gpt_create),
1371d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_destroy,	g_part_gpt_destroy),
1385db67052SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpconf,	g_part_gpt_dumpconf),
1391d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpto,	g_part_gpt_dumpto),
1401d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_modify,	g_part_gpt_modify),
1413f71c319SMarcel Moolenaar 	KOBJMETHOD(g_part_resize,	g_part_gpt_resize),
1421d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_name,		g_part_gpt_name),
1431d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_probe,	g_part_gpt_probe),
1441d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_read,		g_part_gpt_read),
145e7926a37SAndrey V. Elsukov 	KOBJMETHOD(g_part_recover,	g_part_gpt_recover),
146cec283baSPawel Jakub Dawidek 	KOBJMETHOD(g_part_setunset,	g_part_gpt_setunset),
1471d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_type,		g_part_gpt_type),
1481d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_write,	g_part_gpt_write),
1491d3aed33SMarcel Moolenaar 	{ 0, 0 }
1501d3aed33SMarcel Moolenaar };
1511d3aed33SMarcel Moolenaar 
1525be5d0d5SZhenlei Huang #define MAXENTSIZE 1024
1535be5d0d5SZhenlei Huang 
1541d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_gpt_scheme = {
1551d3aed33SMarcel Moolenaar 	"GPT",
1561d3aed33SMarcel Moolenaar 	g_part_gpt_methods,
1571d3aed33SMarcel Moolenaar 	sizeof(struct g_part_gpt_table),
1581d3aed33SMarcel Moolenaar 	.gps_entrysz = sizeof(struct g_part_gpt_entry),
1591d3aed33SMarcel Moolenaar 	.gps_minent = 128,
160799eac8cSAndrey V. Elsukov 	.gps_maxent = 4096,
1614d32fcb4SMarcel Moolenaar 	.gps_bootcodesz = MBRSIZE,
1621d3aed33SMarcel Moolenaar };
1634ffca444SMarcel Moolenaar G_PART_SCHEME_DECLARE(g_part_gpt);
16474d6c131SKyle Evans MODULE_VERSION(geom_part_gpt, 0);
1651d3aed33SMarcel Moolenaar 
166b42712a8SConrad Meyer static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
167f1317430SRui Paulo static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
1684fb4ebe0SAndrey V. Elsukov static struct uuid gpt_uuid_apple_core_storage =
1694fb4ebe0SAndrey V. Elsukov     GPT_ENT_TYPE_APPLE_CORE_STORAGE;
17087662ab3SRui Paulo static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
171f1317430SRui Paulo static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
172f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
173f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
174f1317430SRui Paulo static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
175f1317430SRui Paulo static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
176cb1480f8SConrad Meyer static struct uuid gpt_uuid_apple_zfs = GPT_ENT_TYPE_APPLE_ZFS;
17788007f61SAndrey V. Elsukov static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
1789c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
1799c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
1809c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
1819c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT;
1829c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD;
1839c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER;
1849c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2;
1859c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32;
1869c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64;
1879c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY;
1889c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP;
1899c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1;
1909c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM;
1911d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
1921d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
193f352a0d4SJohn Baldwin static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
194f24a8224SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
1951d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
1961d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
1971d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
198a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
1999c296a21SEd Maste static struct uuid gpt_uuid_hifive_fsbl = GPT_ENT_TYPE_HIFIVE_FSBL;
2009c296a21SEd Maste static struct uuid gpt_uuid_hifive_bbl = GPT_ENT_TYPE_HIFIVE_BBL;
201f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
202f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
203f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
2041d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
2059c0c355fSAllan Jude static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
20633f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
20733f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
20833f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
2099c0c355fSAllan Jude static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY;
2109c0c355fSAllan Jude static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
2119c0c355fSAllan Jude static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES;
21233f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
21333f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
21433f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
21533f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
21633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
21733f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
2189c0c355fSAllan Jude static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA;
2191ee0f089SNathan Whitehorn static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT;
220cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_boot = GPT_ENT_TYPE_SOLARIS_BOOT;
221cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_root = GPT_ENT_TYPE_SOLARIS_ROOT;
222cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_swap = GPT_ENT_TYPE_SOLARIS_SWAP;
223cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_backup = GPT_ENT_TYPE_SOLARIS_BACKUP;
224cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_var = GPT_ENT_TYPE_SOLARIS_VAR;
225cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_home = GPT_ENT_TYPE_SOLARIS_HOME;
226cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_altsec = GPT_ENT_TYPE_SOLARIS_ALTSEC;
227cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_reserved = GPT_ENT_TYPE_SOLARIS_RESERVED;
2289c0c355fSAllan Jude static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
2299c0c355fSAllan Jude static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
2309c0c355fSAllan Jude static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
2319c0c355fSAllan Jude static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
2329c0c355fSAllan Jude static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR;
2331d3aed33SMarcel Moolenaar 
234f1317430SRui Paulo static struct g_part_uuid_alias {
235f1317430SRui Paulo 	struct uuid *uuid;
236f1317430SRui Paulo 	int alias;
237369fe59dSMarcel Moolenaar 	int mbrtype;
238f1317430SRui Paulo } gpt_uuid_alias_match[] = {
239b42712a8SConrad Meyer 	{ &gpt_uuid_apple_apfs,		G_PART_ALIAS_APPLE_APFS,	 0 },
240369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_boot,		G_PART_ALIAS_APPLE_BOOT,	 0xab },
2414fb4ebe0SAndrey V. Elsukov 	{ &gpt_uuid_apple_core_storage,	G_PART_ALIAS_APPLE_CORE_STORAGE, 0 },
242369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_hfs,		G_PART_ALIAS_APPLE_HFS,		 0xaf },
243369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_label,	G_PART_ALIAS_APPLE_LABEL,	 0 },
244369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_raid,		G_PART_ALIAS_APPLE_RAID,	 0 },
245369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_raid_offline,	G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
246369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_tv_recovery,	G_PART_ALIAS_APPLE_TV_RECOVERY,	 0 },
247369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_ufs,		G_PART_ALIAS_APPLE_UFS,		 0 },
248cb1480f8SConrad Meyer 	{ &gpt_uuid_apple_zfs,		G_PART_ALIAS_APPLE_ZFS,		 0 },
249369fe59dSMarcel Moolenaar 	{ &gpt_uuid_bios_boot,		G_PART_ALIAS_BIOS_BOOT,		 0 },
2509c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_firmware,	G_PART_ALIAS_CHROMEOS_FIRMWARE,	 0 },
2519c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_kernel,	G_PART_ALIAS_CHROMEOS_KERNEL,	 0 },
2529c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_reserved,	G_PART_ALIAS_CHROMEOS_RESERVED,	 0 },
2539c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_root,	G_PART_ALIAS_CHROMEOS_ROOT,	 0 },
2549c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_ccd,		G_PART_ALIAS_DFBSD_CCD,		 0 },
2559c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_hammer,	G_PART_ALIAS_DFBSD_HAMMER,	 0 },
2569c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_hammer2,	G_PART_ALIAS_DFBSD_HAMMER2,	 0 },
2579c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_label32,	G_PART_ALIAS_DFBSD,		 0xa5 },
2589c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_label64,	G_PART_ALIAS_DFBSD64,		 0xa5 },
2599c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_legacy,	G_PART_ALIAS_DFBSD_LEGACY,	 0 },
2609c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_swap,		G_PART_ALIAS_DFBSD_SWAP,	 0 },
2619c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_ufs1,		G_PART_ALIAS_DFBSD_UFS,		 0 },
2629c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_vinum,	G_PART_ALIAS_DFBSD_VINUM,	 0 },
263369fe59dSMarcel Moolenaar 	{ &gpt_uuid_efi, 		G_PART_ALIAS_EFI,		 0xee },
264369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd,		G_PART_ALIAS_FREEBSD,		 0xa5 },
265369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_boot, 	G_PART_ALIAS_FREEBSD_BOOT,	 0 },
266f24a8224SMarcel Moolenaar 	{ &gpt_uuid_freebsd_nandfs, 	G_PART_ALIAS_FREEBSD_NANDFS,	 0 },
267369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_swap,	G_PART_ALIAS_FREEBSD_SWAP,	 0 },
268369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_ufs,	G_PART_ALIAS_FREEBSD_UFS,	 0 },
269369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_vinum,	G_PART_ALIAS_FREEBSD_VINUM,	 0 },
270369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_zfs,	G_PART_ALIAS_FREEBSD_ZFS,	 0 },
2719c296a21SEd Maste 	{ &gpt_uuid_hifive_fsbl,	G_PART_ALIAS_HIFIVE_FSBL,	 0 },
2729c296a21SEd Maste 	{ &gpt_uuid_hifive_bbl,		G_PART_ALIAS_HIFIVE_BBL,	 0 },
273369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_data,		G_PART_ALIAS_LINUX_DATA,	 0x0b },
274369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_lvm,		G_PART_ALIAS_LINUX_LVM,		 0 },
275369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_raid,		G_PART_ALIAS_LINUX_RAID,	 0 },
276369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_swap,		G_PART_ALIAS_LINUX_SWAP,	 0 },
277369fe59dSMarcel Moolenaar 	{ &gpt_uuid_mbr,		G_PART_ALIAS_MBR,		 0 },
278369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_basic_data,	G_PART_ALIAS_MS_BASIC_DATA,	 0x0b },
279369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_ldm_data,	G_PART_ALIAS_MS_LDM_DATA,	 0 },
280369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_ldm_metadata,	G_PART_ALIAS_MS_LDM_METADATA,	 0 },
2819c0c355fSAllan Jude 	{ &gpt_uuid_ms_recovery,	G_PART_ALIAS_MS_RECOVERY,	 0 },
282369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_reserved,	G_PART_ALIAS_MS_RESERVED,	 0 },
2839c0c355fSAllan Jude 	{ &gpt_uuid_ms_spaces,		G_PART_ALIAS_MS_SPACES,		 0 },
284369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_ccd,		G_PART_ALIAS_NETBSD_CCD,	 0 },
285369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_cgd,		G_PART_ALIAS_NETBSD_CGD,	 0 },
286369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_ffs,		G_PART_ALIAS_NETBSD_FFS,	 0 },
287369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_lfs,		G_PART_ALIAS_NETBSD_LFS,	 0 },
288369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_raid,	G_PART_ALIAS_NETBSD_RAID,	 0 },
289369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_swap,	G_PART_ALIAS_NETBSD_SWAP,	 0 },
2909c0c355fSAllan Jude 	{ &gpt_uuid_openbsd_data,	G_PART_ALIAS_OPENBSD_DATA,	 0 },
2911ee0f089SNathan Whitehorn 	{ &gpt_uuid_prep_boot,		G_PART_ALIAS_PREP_BOOT,		 0x41 },
292cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_boot,	G_PART_ALIAS_SOLARIS_BOOT,	 0 },
293cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_root,	G_PART_ALIAS_SOLARIS_ROOT,	 0 },
294cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_swap,	G_PART_ALIAS_SOLARIS_SWAP,	 0 },
295cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_backup,	G_PART_ALIAS_SOLARIS_BACKUP,	 0 },
296cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_var,	G_PART_ALIAS_SOLARIS_VAR,	 0 },
297cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_home,	G_PART_ALIAS_SOLARIS_HOME,	 0 },
298cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_altsec,	G_PART_ALIAS_SOLARIS_ALTSEC,	 0 },
299cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_reserved,	G_PART_ALIAS_SOLARIS_RESERVED,	 0 },
3009c0c355fSAllan Jude 	{ &gpt_uuid_vmfs,		G_PART_ALIAS_VMFS,		 0 },
3019c0c355fSAllan Jude 	{ &gpt_uuid_vmkdiag,		G_PART_ALIAS_VMKDIAG,		 0 },
3029c0c355fSAllan Jude 	{ &gpt_uuid_vmreserved,		G_PART_ALIAS_VMRESERVED,	 0 },
3039c0c355fSAllan Jude 	{ &gpt_uuid_vmvsanhdr,		G_PART_ALIAS_VMVSANHDR,		 0 },
304369fe59dSMarcel Moolenaar 	{ NULL, 0, 0 }
305f1317430SRui Paulo };
306f1317430SRui Paulo 
307369fe59dSMarcel Moolenaar static int
308369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
309369fe59dSMarcel Moolenaar     quad_t end)
310369fe59dSMarcel Moolenaar {
311369fe59dSMarcel Moolenaar 
312369fe59dSMarcel Moolenaar 	if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
313369fe59dSMarcel Moolenaar 		return (EINVAL);
314369fe59dSMarcel Moolenaar 
315369fe59dSMarcel Moolenaar 	mbr += DOSPARTOFF + idx * DOSPARTSIZE;
316369fe59dSMarcel Moolenaar 	mbr[0] = 0;
317369fe59dSMarcel Moolenaar 	if (start == 1) {
318369fe59dSMarcel Moolenaar 		/*
319369fe59dSMarcel Moolenaar 		 * Treat the PMBR partition specially to maximize
320369fe59dSMarcel Moolenaar 		 * interoperability with BIOSes.
321369fe59dSMarcel Moolenaar 		 */
322369fe59dSMarcel Moolenaar 		mbr[1] = mbr[3] = 0;
323369fe59dSMarcel Moolenaar 		mbr[2] = 2;
324369fe59dSMarcel Moolenaar 	} else
325369fe59dSMarcel Moolenaar 		mbr[1] = mbr[2] = mbr[3] = 0xff;
326369fe59dSMarcel Moolenaar 	mbr[4] = typ;
327369fe59dSMarcel Moolenaar 	mbr[5] = mbr[6] = mbr[7] = 0xff;
328369fe59dSMarcel Moolenaar 	le32enc(mbr + 8, (uint32_t)start);
329369fe59dSMarcel Moolenaar 	le32enc(mbr + 12, (uint32_t)(end - start + 1));
330369fe59dSMarcel Moolenaar 	return (0);
331369fe59dSMarcel Moolenaar }
332369fe59dSMarcel Moolenaar 
333369fe59dSMarcel Moolenaar static int
334369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t)
335369fe59dSMarcel Moolenaar {
336369fe59dSMarcel Moolenaar 	struct g_part_uuid_alias *uap;
337369fe59dSMarcel Moolenaar 
338369fe59dSMarcel Moolenaar 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
339369fe59dSMarcel Moolenaar 		if (EQUUID(t, uap->uuid))
340369fe59dSMarcel Moolenaar 			return (uap->mbrtype);
341369fe59dSMarcel Moolenaar 	}
342369fe59dSMarcel Moolenaar 	return (0);
343369fe59dSMarcel Moolenaar }
344369fe59dSMarcel Moolenaar 
3453bd22a9cSMarcel Moolenaar static void
3463bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
3473bd22a9cSMarcel Moolenaar {
3483bd22a9cSMarcel Moolenaar 
3493bd22a9cSMarcel Moolenaar 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
3503bd22a9cSMarcel Moolenaar 	gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
3513bd22a9cSMarcel Moolenaar 	    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
3523bd22a9cSMarcel Moolenaar 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
3533bd22a9cSMarcel Moolenaar }
3543bd22a9cSMarcel Moolenaar 
355369fe59dSMarcel Moolenaar /*
356369fe59dSMarcel Moolenaar  * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
357369fe59dSMarcel Moolenaar  * whole disk anymore. Rather, it covers the GPT table and the EFI
358369fe59dSMarcel Moolenaar  * system partition only. This way the HFS+ partition and any FAT
359369fe59dSMarcel Moolenaar  * partitions can be added to the MBR without creating an overlap.
360369fe59dSMarcel Moolenaar  */
361369fe59dSMarcel Moolenaar static int
362369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
363369fe59dSMarcel Moolenaar {
364369fe59dSMarcel Moolenaar 	uint8_t *p;
365369fe59dSMarcel Moolenaar 
366369fe59dSMarcel Moolenaar 	p = table->mbr + DOSPARTOFF;
367369fe59dSMarcel Moolenaar 	if (p[4] != 0xee || le32dec(p + 8) != 1)
368369fe59dSMarcel Moolenaar 		return (0);
369369fe59dSMarcel Moolenaar 
370369fe59dSMarcel Moolenaar 	p += DOSPARTSIZE;
371369fe59dSMarcel Moolenaar 	if (p[4] != 0xaf)
372369fe59dSMarcel Moolenaar 		return (0);
373369fe59dSMarcel Moolenaar 
374369fe59dSMarcel Moolenaar 	printf("GEOM: %s: enabling Boot Camp\n", provname);
375369fe59dSMarcel Moolenaar 	return (1);
376369fe59dSMarcel Moolenaar }
377369fe59dSMarcel Moolenaar 
378369fe59dSMarcel Moolenaar static void
3793bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
380369fe59dSMarcel Moolenaar {
381369fe59dSMarcel Moolenaar 	struct g_part_entry *baseentry;
382369fe59dSMarcel Moolenaar 	struct g_part_gpt_entry *entry;
383369fe59dSMarcel Moolenaar 	struct g_part_gpt_table *table;
384369fe59dSMarcel Moolenaar 	int bootable, error, index, slices, typ;
385369fe59dSMarcel Moolenaar 
386369fe59dSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
387369fe59dSMarcel Moolenaar 
388369fe59dSMarcel Moolenaar 	bootable = -1;
389369fe59dSMarcel Moolenaar 	for (index = 0; index < NDOSPART; index++) {
390369fe59dSMarcel Moolenaar 		if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
391369fe59dSMarcel Moolenaar 			bootable = index;
392369fe59dSMarcel Moolenaar 	}
393369fe59dSMarcel Moolenaar 
394369fe59dSMarcel Moolenaar 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
395369fe59dSMarcel Moolenaar 	slices = 0;
396369fe59dSMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
397369fe59dSMarcel Moolenaar 		if (baseentry->gpe_deleted)
398369fe59dSMarcel Moolenaar 			continue;
399369fe59dSMarcel Moolenaar 		index = baseentry->gpe_index - 1;
400369fe59dSMarcel Moolenaar 		if (index >= NDOSPART)
401369fe59dSMarcel Moolenaar 			continue;
402369fe59dSMarcel Moolenaar 
403369fe59dSMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
404369fe59dSMarcel Moolenaar 
405369fe59dSMarcel Moolenaar 		switch (index) {
406369fe59dSMarcel Moolenaar 		case 0:	/* This must be the EFI system partition. */
407369fe59dSMarcel Moolenaar 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
408369fe59dSMarcel Moolenaar 				goto disable;
409369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, 0xee,
410369fe59dSMarcel Moolenaar 			    1ull, entry->ent.ent_lba_end);
411369fe59dSMarcel Moolenaar 			break;
412369fe59dSMarcel Moolenaar 		case 1:	/* This must be the HFS+ partition. */
413369fe59dSMarcel Moolenaar 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
414369fe59dSMarcel Moolenaar 				goto disable;
415369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
416369fe59dSMarcel Moolenaar 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
417369fe59dSMarcel Moolenaar 			break;
418369fe59dSMarcel Moolenaar 		default:
419369fe59dSMarcel Moolenaar 			typ = gpt_map_type(&entry->ent.ent_type);
420369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, typ,
421369fe59dSMarcel Moolenaar 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
422369fe59dSMarcel Moolenaar 			break;
423369fe59dSMarcel Moolenaar 		}
424369fe59dSMarcel Moolenaar 		if (error)
425369fe59dSMarcel Moolenaar 			continue;
426369fe59dSMarcel Moolenaar 
427369fe59dSMarcel Moolenaar 		if (index == bootable)
428369fe59dSMarcel Moolenaar 			table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
429369fe59dSMarcel Moolenaar 		slices |= 1 << index;
430369fe59dSMarcel Moolenaar 	}
431369fe59dSMarcel Moolenaar 	if ((slices & 3) == 3)
432369fe59dSMarcel Moolenaar 		return;
433369fe59dSMarcel Moolenaar 
434369fe59dSMarcel Moolenaar  disable:
435369fe59dSMarcel Moolenaar 	table->bootcamp = 0;
4363bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
437369fe59dSMarcel Moolenaar }
438369fe59dSMarcel Moolenaar 
4391c2dee3cSRobert Noland static struct gpt_hdr *
4401d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
4411c2dee3cSRobert Noland     enum gpt_elt elt)
4421d3aed33SMarcel Moolenaar {
4431c2dee3cSRobert Noland 	struct gpt_hdr *buf, *hdr;
4441d3aed33SMarcel Moolenaar 	struct g_provider *pp;
4451d3aed33SMarcel Moolenaar 	quad_t lba, last;
4461d3aed33SMarcel Moolenaar 	int error;
4471d3aed33SMarcel Moolenaar 	uint32_t crc, sz;
4481d3aed33SMarcel Moolenaar 
4491d3aed33SMarcel Moolenaar 	pp = cp->provider;
4501d3aed33SMarcel Moolenaar 	last = (pp->mediasize / pp->sectorsize) - 1;
4511d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_MISSING;
452e7926a37SAndrey V. Elsukov 	/*
453e7926a37SAndrey V. Elsukov 	 * If the primary header is valid look for secondary
454e7926a37SAndrey V. Elsukov 	 * header in AlternateLBA, otherwise in the last medium's LBA.
455e7926a37SAndrey V. Elsukov 	 */
456e7926a37SAndrey V. Elsukov 	if (elt == GPT_ELT_SECHDR) {
457e7926a37SAndrey V. Elsukov 		if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
458e7926a37SAndrey V. Elsukov 			table->lba[elt] = last;
459e7926a37SAndrey V. Elsukov 	} else
460e7926a37SAndrey V. Elsukov 		table->lba[elt] = 1;
4611d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
4621d3aed33SMarcel Moolenaar 	    &error);
4631d3aed33SMarcel Moolenaar 	if (buf == NULL)
4641c2dee3cSRobert Noland 		return (NULL);
4651c2dee3cSRobert Noland 	hdr = NULL;
4661c2dee3cSRobert Noland 	if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
4671c2dee3cSRobert Noland 		goto fail;
4681d3aed33SMarcel Moolenaar 
4691d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_CORRUPT;
4701c2dee3cSRobert Noland 	sz = le32toh(buf->hdr_size);
4711d3aed33SMarcel Moolenaar 	if (sz < 92 || sz > pp->sectorsize)
4721c2dee3cSRobert Noland 		goto fail;
4731c2dee3cSRobert Noland 
4741c2dee3cSRobert Noland 	hdr = g_malloc(sz, M_WAITOK | M_ZERO);
4751c2dee3cSRobert Noland 	bcopy(buf, hdr, sz);
4761d3aed33SMarcel Moolenaar 	hdr->hdr_size = sz;
4771c2dee3cSRobert Noland 
4781c2dee3cSRobert Noland 	crc = le32toh(buf->hdr_crc_self);
4791c2dee3cSRobert Noland 	buf->hdr_crc_self = 0;
4801c2dee3cSRobert Noland 	if (crc32(buf, sz) != crc)
4811c2dee3cSRobert Noland 		goto fail;
4821d3aed33SMarcel Moolenaar 	hdr->hdr_crc_self = crc;
4831d3aed33SMarcel Moolenaar 
4841d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_INVALID;
4851c2dee3cSRobert Noland 	hdr->hdr_revision = le32toh(buf->hdr_revision);
486e7926a37SAndrey V. Elsukov 	if (hdr->hdr_revision < GPT_HDR_REVISION)
4871c2dee3cSRobert Noland 		goto fail;
4881c2dee3cSRobert Noland 	hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
4891d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_self != table->lba[elt])
4901c2dee3cSRobert Noland 		goto fail;
4911c2dee3cSRobert Noland 	hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
492cea05ed9SEugene Grosbein 	if (hdr->hdr_lba_alt == hdr->hdr_lba_self)
493cea05ed9SEugene Grosbein 		goto fail;
494cea05ed9SEugene Grosbein 	if (hdr->hdr_lba_alt > last && geom_part_check_integrity)
495e7926a37SAndrey V. Elsukov 		goto fail;
4961d3aed33SMarcel Moolenaar 
4971d3aed33SMarcel Moolenaar 	/* Check the managed area. */
4981c2dee3cSRobert Noland 	hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
4991d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
5001c2dee3cSRobert Noland 		goto fail;
5011c2dee3cSRobert Noland 	hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
5021d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
5031c2dee3cSRobert Noland 		goto fail;
5041d3aed33SMarcel Moolenaar 
5051d3aed33SMarcel Moolenaar 	/* Check the table location and size of the table. */
5061c2dee3cSRobert Noland 	hdr->hdr_entries = le32toh(buf->hdr_entries);
5071c2dee3cSRobert Noland 	hdr->hdr_entsz = le32toh(buf->hdr_entsz);
5081d3aed33SMarcel Moolenaar 	if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
5091d3aed33SMarcel Moolenaar 	    (hdr->hdr_entsz & 7) != 0)
5101c2dee3cSRobert Noland 		goto fail;
5111c2dee3cSRobert Noland 	hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
5121d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
5131c2dee3cSRobert Noland 		goto fail;
5141d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
5151d3aed33SMarcel Moolenaar 	    hdr->hdr_lba_table <= hdr->hdr_lba_end)
5161c2dee3cSRobert Noland 		goto fail;
5171d3aed33SMarcel Moolenaar 	lba = hdr->hdr_lba_table +
518*2e543af1SZhenlei Huang 	    howmany((uint64_t)hdr->hdr_entries * hdr->hdr_entsz,
519*2e543af1SZhenlei Huang 	        pp->sectorsize) - 1;
5201d3aed33SMarcel Moolenaar 	if (lba >= last)
5211c2dee3cSRobert Noland 		goto fail;
5221d3aed33SMarcel Moolenaar 	if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
5231c2dee3cSRobert Noland 		goto fail;
5241d3aed33SMarcel Moolenaar 
5251d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_OK;
5261c2dee3cSRobert Noland 	le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
5271c2dee3cSRobert Noland 	hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
5281c2dee3cSRobert Noland 
529e7926a37SAndrey V. Elsukov 	/* save LBA for secondary header */
530e7926a37SAndrey V. Elsukov 	if (elt == GPT_ELT_PRIHDR)
531e7926a37SAndrey V. Elsukov 		table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
532e7926a37SAndrey V. Elsukov 
5331c2dee3cSRobert Noland 	g_free(buf);
5341c2dee3cSRobert Noland 	return (hdr);
5351c2dee3cSRobert Noland 
5361c2dee3cSRobert Noland  fail:
5371c2dee3cSRobert Noland 	if (hdr != NULL)
5381c2dee3cSRobert Noland 		g_free(hdr);
5391c2dee3cSRobert Noland 	g_free(buf);
5401c2dee3cSRobert Noland 	return (NULL);
5411d3aed33SMarcel Moolenaar }
5421d3aed33SMarcel Moolenaar 
5431d3aed33SMarcel Moolenaar static struct gpt_ent *
5441d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
5451d3aed33SMarcel Moolenaar     enum gpt_elt elt, struct gpt_hdr *hdr)
5461d3aed33SMarcel Moolenaar {
5471d3aed33SMarcel Moolenaar 	struct g_provider *pp;
5481d3aed33SMarcel Moolenaar 	struct gpt_ent *ent, *tbl;
5491d3aed33SMarcel Moolenaar 	char *buf, *p;
550799eac8cSAndrey V. Elsukov 	unsigned int idx, sectors, tblsz, size;
5511d3aed33SMarcel Moolenaar 	int error;
5521d3aed33SMarcel Moolenaar 
5531c2dee3cSRobert Noland 	if (hdr == NULL)
5541c2dee3cSRobert Noland 		return (NULL);
5555be5d0d5SZhenlei Huang 	if (hdr->hdr_entries > g_part_gpt_scheme.gps_maxent ||
5565be5d0d5SZhenlei Huang 	    hdr->hdr_entsz > MAXENTSIZE) {
5575be5d0d5SZhenlei Huang 		table->state[elt] = GPT_STATE_UNSUPPORTED;
5585be5d0d5SZhenlei Huang 		return (NULL);
5595be5d0d5SZhenlei Huang 	}
5601c2dee3cSRobert Noland 
5611d3aed33SMarcel Moolenaar 	pp = cp->provider;
5621d3aed33SMarcel Moolenaar 	table->lba[elt] = hdr->hdr_lba_table;
5631d3aed33SMarcel Moolenaar 
5641d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_MISSING;
5651d3aed33SMarcel Moolenaar 	tblsz = hdr->hdr_entries * hdr->hdr_entsz;
56655e0987aSPedro F. Giffuni 	sectors = howmany(tblsz, pp->sectorsize);
567799eac8cSAndrey V. Elsukov 	buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
568cd853791SKonstantin Belousov 	for (idx = 0; idx < sectors; idx += maxphys / pp->sectorsize) {
569cd853791SKonstantin Belousov 		size = (sectors - idx > maxphys / pp->sectorsize) ?  maxphys:
570799eac8cSAndrey V. Elsukov 		    (sectors - idx) * pp->sectorsize;
571799eac8cSAndrey V. Elsukov 		p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
572799eac8cSAndrey V. Elsukov 		    size, &error);
573799eac8cSAndrey V. Elsukov 		if (p == NULL) {
574799eac8cSAndrey V. Elsukov 			g_free(buf);
5751d3aed33SMarcel Moolenaar 			return (NULL);
576799eac8cSAndrey V. Elsukov 		}
577799eac8cSAndrey V. Elsukov 		bcopy(p, buf + idx * pp->sectorsize, size);
578799eac8cSAndrey V. Elsukov 		g_free(p);
579799eac8cSAndrey V. Elsukov 	}
5801d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_CORRUPT;
5811d3aed33SMarcel Moolenaar 	if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
5821d3aed33SMarcel Moolenaar 		g_free(buf);
5831d3aed33SMarcel Moolenaar 		return (NULL);
5841d3aed33SMarcel Moolenaar 	}
5851d3aed33SMarcel Moolenaar 
5861d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_OK;
5871d3aed33SMarcel Moolenaar 	tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
5881d3aed33SMarcel Moolenaar 	    M_WAITOK | M_ZERO);
5891d3aed33SMarcel Moolenaar 
5901d3aed33SMarcel Moolenaar 	for (idx = 0, ent = tbl, p = buf;
5911d3aed33SMarcel Moolenaar 	     idx < hdr->hdr_entries;
5921d3aed33SMarcel Moolenaar 	     idx++, ent++, p += hdr->hdr_entsz) {
5931d3aed33SMarcel Moolenaar 		le_uuid_dec(p, &ent->ent_type);
5941d3aed33SMarcel Moolenaar 		le_uuid_dec(p + 16, &ent->ent_uuid);
5951d3aed33SMarcel Moolenaar 		ent->ent_lba_start = le64dec(p + 32);
5961d3aed33SMarcel Moolenaar 		ent->ent_lba_end = le64dec(p + 40);
5971d3aed33SMarcel Moolenaar 		ent->ent_attr = le64dec(p + 48);
598d3532631SMarcel Moolenaar 		/* Keep UTF-16 in little-endian. */
599d3532631SMarcel Moolenaar 		bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
6001d3aed33SMarcel Moolenaar 	}
6011d3aed33SMarcel Moolenaar 
6021d3aed33SMarcel Moolenaar 	g_free(buf);
6031d3aed33SMarcel Moolenaar 	return (tbl);
6041d3aed33SMarcel Moolenaar }
6051d3aed33SMarcel Moolenaar 
6061d3aed33SMarcel Moolenaar static int
6071d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
6081d3aed33SMarcel Moolenaar {
6091d3aed33SMarcel Moolenaar 
6101c2dee3cSRobert Noland 	if (pri == NULL || sec == NULL)
6111c2dee3cSRobert Noland 		return (0);
6121c2dee3cSRobert Noland 
6131d3aed33SMarcel Moolenaar 	if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
6141d3aed33SMarcel Moolenaar 		return (0);
6151d3aed33SMarcel Moolenaar 	return ((pri->hdr_revision == sec->hdr_revision &&
6161d3aed33SMarcel Moolenaar 	    pri->hdr_size == sec->hdr_size &&
6171d3aed33SMarcel Moolenaar 	    pri->hdr_lba_start == sec->hdr_lba_start &&
6181d3aed33SMarcel Moolenaar 	    pri->hdr_lba_end == sec->hdr_lba_end &&
6191d3aed33SMarcel Moolenaar 	    pri->hdr_entries == sec->hdr_entries &&
6201d3aed33SMarcel Moolenaar 	    pri->hdr_entsz == sec->hdr_entsz &&
6211d3aed33SMarcel Moolenaar 	    pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
6221d3aed33SMarcel Moolenaar }
6231d3aed33SMarcel Moolenaar 
6241d3aed33SMarcel Moolenaar static int
6251d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid)
6261d3aed33SMarcel Moolenaar {
6271d3aed33SMarcel Moolenaar 	struct uuid tmp;
628d287f590SMarcel Moolenaar 	const char *alias;
6291d3aed33SMarcel Moolenaar 	int error;
630f1317430SRui Paulo 	struct g_part_uuid_alias *uap;
6311d3aed33SMarcel Moolenaar 
632d287f590SMarcel Moolenaar 	if (type[0] == '!') {
633d287f590SMarcel Moolenaar 		error = parse_uuid(type + 1, &tmp);
6341d3aed33SMarcel Moolenaar 		if (error)
6351d3aed33SMarcel Moolenaar 			return (error);
6361d3aed33SMarcel Moolenaar 		if (EQUUID(&tmp, &gpt_uuid_unused))
6371d3aed33SMarcel Moolenaar 			return (EINVAL);
6381d3aed33SMarcel Moolenaar 		*uuid = tmp;
6391d3aed33SMarcel Moolenaar 		return (0);
6401d3aed33SMarcel Moolenaar 	}
641f1317430SRui Paulo 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
642f1317430SRui Paulo 		alias = g_part_alias_name(uap->alias);
643d287f590SMarcel Moolenaar 		if (!strcasecmp(type, alias)) {
644665bb830SMarcel Moolenaar 			*uuid = *uap->uuid;
6451d3aed33SMarcel Moolenaar 			return (0);
6461d3aed33SMarcel Moolenaar 		}
647d7255ff4SRui Paulo 	}
648d287f590SMarcel Moolenaar 	return (EINVAL);
649d287f590SMarcel Moolenaar }
6501d3aed33SMarcel Moolenaar 
6511d3aed33SMarcel Moolenaar static int
6521d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
6531d3aed33SMarcel Moolenaar     struct g_part_parms *gpp)
6541d3aed33SMarcel Moolenaar {
6551d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
6561d3aed33SMarcel Moolenaar 	int error;
6571d3aed33SMarcel Moolenaar 
6581d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
6591d3aed33SMarcel Moolenaar 	error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
6601d3aed33SMarcel Moolenaar 	if (error)
6611d3aed33SMarcel Moolenaar 		return (error);
6621d3aed33SMarcel Moolenaar 	kern_uuidgen(&entry->ent.ent_uuid, 1);
6631d3aed33SMarcel Moolenaar 	entry->ent.ent_lba_start = baseentry->gpe_start;
6641d3aed33SMarcel Moolenaar 	entry->ent.ent_lba_end = baseentry->gpe_end;
6651d3aed33SMarcel Moolenaar 	if (baseentry->gpe_deleted) {
6661d3aed33SMarcel Moolenaar 		entry->ent.ent_attr = 0;
6671d3aed33SMarcel Moolenaar 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
6681d3aed33SMarcel Moolenaar 	}
669d3532631SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
670d3532631SMarcel Moolenaar 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
671ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name) /
672ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name[0]));
6731d3aed33SMarcel Moolenaar 	return (0);
6741d3aed33SMarcel Moolenaar }
6751d3aed33SMarcel Moolenaar 
6761d3aed33SMarcel Moolenaar static int
6774d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
6784d32fcb4SMarcel Moolenaar {
6794d32fcb4SMarcel Moolenaar 	struct g_part_gpt_table *table;
68066477112SMarcel Moolenaar 	size_t codesz;
6814d32fcb4SMarcel Moolenaar 
68266477112SMarcel Moolenaar 	codesz = DOSPARTOFF;
6834d32fcb4SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
68466477112SMarcel Moolenaar 	bzero(table->mbr, codesz);
68566477112SMarcel Moolenaar 	codesz = MIN(codesz, gpp->gpp_codesize);
68666477112SMarcel Moolenaar 	if (codesz > 0)
68766477112SMarcel Moolenaar 		bcopy(gpp->gpp_codeptr, table->mbr, codesz);
6884d32fcb4SMarcel Moolenaar 	return (0);
6894d32fcb4SMarcel Moolenaar }
6904d32fcb4SMarcel Moolenaar 
6914d32fcb4SMarcel Moolenaar static int
6921d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
6931d3aed33SMarcel Moolenaar {
6941d3aed33SMarcel Moolenaar 	struct g_provider *pp;
6951d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
6961d3aed33SMarcel Moolenaar 	size_t tblsz;
6971d3aed33SMarcel Moolenaar 
698ef03f57dSKyle Evans 	/* Our depth should be 0 unless nesting was explicitly enabled. */
699ef03f57dSKyle Evans 	if (!allow_nesting && basetable->gpt_depth != 0)
7002a1c00ffSMarcel Moolenaar 		return (ENXIO);
7012a1c00ffSMarcel Moolenaar 
7021d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
7031d3aed33SMarcel Moolenaar 	pp = gpp->gpp_provider;
70455e0987aSPedro F. Giffuni 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
70555e0987aSPedro F. Giffuni 	    pp->sectorsize);
7064d32fcb4SMarcel Moolenaar 	if (pp->sectorsize < MBRSIZE ||
7071d3aed33SMarcel Moolenaar 	    pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
7081d3aed33SMarcel Moolenaar 	    pp->sectorsize)
7091d3aed33SMarcel Moolenaar 		return (ENOSPC);
7101d3aed33SMarcel Moolenaar 
7113bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
7123bd22a9cSMarcel Moolenaar 
713a59a1310SRobert Noland 	/* Allocate space for the header */
714a59a1310SRobert Noland 	table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
715a59a1310SRobert Noland 
7161c2dee3cSRobert Noland 	bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
7171c2dee3cSRobert Noland 	table->hdr->hdr_revision = GPT_HDR_REVISION;
7181c2dee3cSRobert Noland 	table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
7191c2dee3cSRobert Noland 	kern_uuidgen(&table->hdr->hdr_uuid, 1);
7201c2dee3cSRobert Noland 	table->hdr->hdr_entries = basetable->gpt_entries;
7211c2dee3cSRobert Noland 	table->hdr->hdr_entsz = sizeof(struct gpt_ent);
7221d3aed33SMarcel Moolenaar 
7232b9be055SAndrey V. Elsukov 	g_gpt_set_defaults(basetable, pp);
7241d3aed33SMarcel Moolenaar 	return (0);
7251d3aed33SMarcel Moolenaar }
7261d3aed33SMarcel Moolenaar 
7271d3aed33SMarcel Moolenaar static int
7281d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
7291d3aed33SMarcel Moolenaar {
73036066952SMarius Strobl 	struct g_part_gpt_table *table;
731e7926a37SAndrey V. Elsukov 	struct g_provider *pp;
73236066952SMarius Strobl 
73336066952SMarius Strobl 	table = (struct g_part_gpt_table *)basetable;
734e7926a37SAndrey V. Elsukov 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
73536066952SMarius Strobl 	g_free(table->hdr);
73636066952SMarius Strobl 	table->hdr = NULL;
7371d3aed33SMarcel Moolenaar 
7381d3aed33SMarcel Moolenaar 	/*
7399ef60043SAndrey V. Elsukov 	 * Wipe the first 2 sectors and last one to clear the partitioning.
7409ef60043SAndrey V. Elsukov 	 * Wipe sectors only if they have valid metadata.
7411d3aed33SMarcel Moolenaar 	 */
7429ef60043SAndrey V. Elsukov 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK)
7431d3aed33SMarcel Moolenaar 		basetable->gpt_smhead |= 3;
744e7926a37SAndrey V. Elsukov 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
745e7926a37SAndrey V. Elsukov 	    table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
7461d3aed33SMarcel Moolenaar 		basetable->gpt_smtail |= 1;
7471d3aed33SMarcel Moolenaar 	return (0);
7481d3aed33SMarcel Moolenaar }
7491d3aed33SMarcel Moolenaar 
75051f53a08SWarner Losh static void
751e3ab141fSWarner Losh g_part_gpt_efimedia(struct g_part_gpt_entry *entry, struct sbuf *sb)
752e3ab141fSWarner Losh {
753e3ab141fSWarner Losh 	sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index);
754e3ab141fSWarner Losh 	sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
755e3ab141fSWarner Losh 	sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start,
756e3ab141fSWarner Losh 	    (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
757e3ab141fSWarner Losh }
758e3ab141fSWarner Losh 
759e3ab141fSWarner Losh static void
7605db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
7615db67052SMarcel Moolenaar     struct sbuf *sb, const char *indent)
7625db67052SMarcel Moolenaar {
7635db67052SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
7645db67052SMarcel Moolenaar 
7655db67052SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
7660c132595SMarcel Moolenaar 	if (indent == NULL) {
7670c132595SMarcel Moolenaar 		/* conftxt: libdisk compatibility */
76849ee0fceSAlexander Motin 		sbuf_cat(sb, " xs GPT xt ");
7695db67052SMarcel Moolenaar 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
7700c132595SMarcel Moolenaar 	} else if (entry != NULL) {
7710c132595SMarcel Moolenaar 		/* confxml: partition entry information */
772d3532631SMarcel Moolenaar 		sbuf_printf(sb, "%s<label>", indent);
773d3532631SMarcel Moolenaar 		g_gpt_printf_utf16(sb, entry->ent.ent_name,
774d3532631SMarcel Moolenaar 		    sizeof(entry->ent.ent_name) >> 1);
77549ee0fceSAlexander Motin 		sbuf_cat(sb, "</label>\n");
776cec283baSPawel Jakub Dawidek 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
777cec283baSPawel Jakub Dawidek 			sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
778cec283baSPawel Jakub Dawidek 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
779cec283baSPawel Jakub Dawidek 			sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
780cec283baSPawel Jakub Dawidek 			    indent);
781cec283baSPawel Jakub Dawidek 		}
782cec283baSPawel Jakub Dawidek 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
783cec283baSPawel Jakub Dawidek 			sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
784cec283baSPawel Jakub Dawidek 			    indent);
785cec283baSPawel Jakub Dawidek 		}
7860c132595SMarcel Moolenaar 		sbuf_printf(sb, "%s<rawtype>", indent);
7870c132595SMarcel Moolenaar 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
78849ee0fceSAlexander Motin 		sbuf_cat(sb, "</rawtype>\n");
789cd18ad83SMarcel Moolenaar 		sbuf_printf(sb, "%s<rawuuid>", indent);
790cd18ad83SMarcel Moolenaar 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
79149ee0fceSAlexander Motin 		sbuf_cat(sb, "</rawuuid>\n");
792ab4effdcSWarner Losh 		sbuf_printf(sb, "%s<efimedia>", indent);
793e3ab141fSWarner Losh 		g_part_gpt_efimedia(entry, sb);
79449ee0fceSAlexander Motin 		sbuf_cat(sb, "</efimedia>\n");
7950c132595SMarcel Moolenaar 	} else {
7960c132595SMarcel Moolenaar 		/* confxml: scheme information */
7970c132595SMarcel Moolenaar 	}
7985db67052SMarcel Moolenaar }
7995db67052SMarcel Moolenaar 
8005db67052SMarcel Moolenaar static int
8011d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
8021d3aed33SMarcel Moolenaar {
8031d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8041d3aed33SMarcel Moolenaar 
8051d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
8061d3aed33SMarcel Moolenaar 	return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
8074042ab48SAndrey V. Elsukov 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
8084042ab48SAndrey V. Elsukov 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
8091d3aed33SMarcel Moolenaar }
8101d3aed33SMarcel Moolenaar 
8111d3aed33SMarcel Moolenaar static int
8121d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable,
8131d3aed33SMarcel Moolenaar     struct g_part_entry *baseentry, struct g_part_parms *gpp)
8141d3aed33SMarcel Moolenaar {
8151d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8161d3aed33SMarcel Moolenaar 	int error;
8171d3aed33SMarcel Moolenaar 
8181d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
819d287f590SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
8201d3aed33SMarcel Moolenaar 		error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
8211d3aed33SMarcel Moolenaar 		if (error)
8221d3aed33SMarcel Moolenaar 			return (error);
823d287f590SMarcel Moolenaar 	}
824d3532631SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
825d3532631SMarcel Moolenaar 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
826ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name) /
827ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name[0]));
8281d3aed33SMarcel Moolenaar 	return (0);
8291d3aed33SMarcel Moolenaar }
8301d3aed33SMarcel Moolenaar 
8313f71c319SMarcel Moolenaar static int
8323f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable,
8333f71c319SMarcel Moolenaar     struct g_part_entry *baseentry, struct g_part_parms *gpp)
8343f71c319SMarcel Moolenaar {
8353f71c319SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8363f71c319SMarcel Moolenaar 
837884c8e4fSAndrey V. Elsukov 	if (baseentry == NULL)
838e7d0c7e4SAndrey V. Elsukov 		return (g_part_gpt_recover(basetable));
839884c8e4fSAndrey V. Elsukov 
840884c8e4fSAndrey V. Elsukov 	entry = (struct g_part_gpt_entry *)baseentry;
8413f71c319SMarcel Moolenaar 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
8423f71c319SMarcel Moolenaar 	entry->ent.ent_lba_end = baseentry->gpe_end;
8433f71c319SMarcel Moolenaar 
8443f71c319SMarcel Moolenaar 	return (0);
8453f71c319SMarcel Moolenaar }
8463f71c319SMarcel Moolenaar 
84751f53a08SWarner Losh static const char *
8481d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
8491d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
8501d3aed33SMarcel Moolenaar {
8511d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8521d3aed33SMarcel Moolenaar 	char c;
8531d3aed33SMarcel Moolenaar 
8541d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
8551d3aed33SMarcel Moolenaar 	c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
8561d3aed33SMarcel Moolenaar 	snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
8571d3aed33SMarcel Moolenaar 	return (buf);
8581d3aed33SMarcel Moolenaar }
8591d3aed33SMarcel Moolenaar 
8601d3aed33SMarcel Moolenaar static int
8611d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
8621d3aed33SMarcel Moolenaar {
8631d3aed33SMarcel Moolenaar 	struct g_provider *pp;
8643e5a0a6bSMarcel Moolenaar 	u_char *buf;
8653e5a0a6bSMarcel Moolenaar 	int error, index, pri, res;
8661d3aed33SMarcel Moolenaar 
867ef03f57dSKyle Evans 	/* Our depth should be 0 unless nesting was explicitly enabled. */
868ef03f57dSKyle Evans 	if (!allow_nesting && table->gpt_depth != 0)
8691d3aed33SMarcel Moolenaar 		return (ENXIO);
8701d3aed33SMarcel Moolenaar 
8711d3aed33SMarcel Moolenaar 	pp = cp->provider;
8721d3aed33SMarcel Moolenaar 
8731d3aed33SMarcel Moolenaar 	/*
8741d3aed33SMarcel Moolenaar 	 * Sanity-check the provider. Since the first sector on the provider
8751d3aed33SMarcel Moolenaar 	 * must be a PMBR and a PMBR is 512 bytes large, the sector size
8761d3aed33SMarcel Moolenaar 	 * must be at least 512 bytes.  Also, since the theoretical minimum
8771d3aed33SMarcel Moolenaar 	 * number of sectors needed by GPT is 6, any medium that has less
8781d3aed33SMarcel Moolenaar 	 * than 6 sectors is never going to be able to hold a GPT. The
8791d3aed33SMarcel Moolenaar 	 * number 6 comes from:
8801d3aed33SMarcel Moolenaar 	 *	1 sector for the PMBR
8811d3aed33SMarcel Moolenaar 	 *	2 sectors for the GPT headers (each 1 sector)
8821d3aed33SMarcel Moolenaar 	 *	2 sectors for the GPT tables (each 1 sector)
8831d3aed33SMarcel Moolenaar 	 *	1 sector for an actual partition
8841d3aed33SMarcel Moolenaar 	 * It's better to catch this pathological case early than behaving
8851d3aed33SMarcel Moolenaar 	 * pathologically later on...
8861d3aed33SMarcel Moolenaar 	 */
8874d32fcb4SMarcel Moolenaar 	if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
8881d3aed33SMarcel Moolenaar 		return (ENOSPC);
8891d3aed33SMarcel Moolenaar 
8903e5a0a6bSMarcel Moolenaar 	/*
8913e5a0a6bSMarcel Moolenaar 	 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
8923e5a0a6bSMarcel Moolenaar 	 * as the highest priority on a match, otherwise we assume some
8933e5a0a6bSMarcel Moolenaar 	 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
8943e5a0a6bSMarcel Moolenaar 	 * we really want the MBR scheme to take precedence.
8953e5a0a6bSMarcel Moolenaar 	 */
8961d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
8971d3aed33SMarcel Moolenaar 	if (buf == NULL)
8981d3aed33SMarcel Moolenaar 		return (error);
8991d3aed33SMarcel Moolenaar 	res = le16dec(buf + DOSMAGICOFFSET);
9003e5a0a6bSMarcel Moolenaar 	pri = G_PART_PROBE_PRI_LOW;
901af90a872SAndrey V. Elsukov 	if (res == DOSMAGIC) {
9023e5a0a6bSMarcel Moolenaar 		for (index = 0; index < NDOSPART; index++) {
9033e5a0a6bSMarcel Moolenaar 			if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
9043e5a0a6bSMarcel Moolenaar 				pri = G_PART_PROBE_PRI_HIGH;
9053e5a0a6bSMarcel Moolenaar 		}
9061d3aed33SMarcel Moolenaar 		g_free(buf);
9071d3aed33SMarcel Moolenaar 
9081d3aed33SMarcel Moolenaar 		/* Check that there's a primary header. */
9091d3aed33SMarcel Moolenaar 		buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
9101d3aed33SMarcel Moolenaar 		if (buf == NULL)
9111d3aed33SMarcel Moolenaar 			return (error);
9121d3aed33SMarcel Moolenaar 		res = memcmp(buf, GPT_HDR_SIG, 8);
9131d3aed33SMarcel Moolenaar 		g_free(buf);
9141d3aed33SMarcel Moolenaar 		if (res == 0)
9153e5a0a6bSMarcel Moolenaar 			return (pri);
916af90a872SAndrey V. Elsukov 	} else
917af90a872SAndrey V. Elsukov 		g_free(buf);
9181d3aed33SMarcel Moolenaar 
9191d3aed33SMarcel Moolenaar 	/* No primary? Check that there's a secondary. */
9201d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
9211d3aed33SMarcel Moolenaar 	    &error);
9221d3aed33SMarcel Moolenaar 	if (buf == NULL)
9231d3aed33SMarcel Moolenaar 		return (error);
9241d3aed33SMarcel Moolenaar 	res = memcmp(buf, GPT_HDR_SIG, 8);
9251d3aed33SMarcel Moolenaar 	g_free(buf);
9263e5a0a6bSMarcel Moolenaar 	return ((res == 0) ? pri : ENXIO);
9271d3aed33SMarcel Moolenaar }
9281d3aed33SMarcel Moolenaar 
9291d3aed33SMarcel Moolenaar static int
9301d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
9311d3aed33SMarcel Moolenaar {
9321c2dee3cSRobert Noland 	struct gpt_hdr *prihdr, *sechdr;
9331d3aed33SMarcel Moolenaar 	struct gpt_ent *tbl, *pritbl, *sectbl;
9341d3aed33SMarcel Moolenaar 	struct g_provider *pp;
9351d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
9361d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
9374d32fcb4SMarcel Moolenaar 	u_char *buf;
938e7926a37SAndrey V. Elsukov 	uint64_t last;
9394d32fcb4SMarcel Moolenaar 	int error, index;
9401d3aed33SMarcel Moolenaar 
9411d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
9421d3aed33SMarcel Moolenaar 	pp = cp->provider;
943e7926a37SAndrey V. Elsukov 	last = (pp->mediasize / pp->sectorsize) - 1;
9441d3aed33SMarcel Moolenaar 
9454d32fcb4SMarcel Moolenaar 	/* Read the PMBR */
9464d32fcb4SMarcel Moolenaar 	buf = g_read_data(cp, 0, pp->sectorsize, &error);
9474d32fcb4SMarcel Moolenaar 	if (buf == NULL)
9484d32fcb4SMarcel Moolenaar 		return (error);
9494d32fcb4SMarcel Moolenaar 	bcopy(buf, table->mbr, MBRSIZE);
9504d32fcb4SMarcel Moolenaar 	g_free(buf);
9514d32fcb4SMarcel Moolenaar 
9521d3aed33SMarcel Moolenaar 	/* Read the primary header and table. */
9531c2dee3cSRobert Noland 	prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
9541d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
9551c2dee3cSRobert Noland 		pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
9561d3aed33SMarcel Moolenaar 	} else {
9571d3aed33SMarcel Moolenaar 		table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
9581d3aed33SMarcel Moolenaar 		pritbl = NULL;
9591d3aed33SMarcel Moolenaar 	}
9601d3aed33SMarcel Moolenaar 
9611d3aed33SMarcel Moolenaar 	/* Read the secondary header and table. */
9621c2dee3cSRobert Noland 	sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
9631d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
9641c2dee3cSRobert Noland 		sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
9651d3aed33SMarcel Moolenaar 	} else {
9661d3aed33SMarcel Moolenaar 		table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
9671d3aed33SMarcel Moolenaar 		sectbl = NULL;
9681d3aed33SMarcel Moolenaar 	}
9691d3aed33SMarcel Moolenaar 
9701d3aed33SMarcel Moolenaar 	/* Fail if we haven't got any good tables at all. */
9711d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
9721d3aed33SMarcel Moolenaar 	    table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
9735be5d0d5SZhenlei Huang 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_UNSUPPORTED &&
9745be5d0d5SZhenlei Huang 		    table->state[GPT_ELT_SECTBL] == GPT_STATE_UNSUPPORTED &&
9755be5d0d5SZhenlei Huang 		    gpt_matched_hdrs(prihdr, sechdr)) {
9765be5d0d5SZhenlei Huang 			printf("GEOM: %s: unsupported GPT detected.\n",
9775be5d0d5SZhenlei Huang 			    pp->name);
9785be5d0d5SZhenlei Huang 			printf(
9795be5d0d5SZhenlei Huang 		    "GEOM: %s: number of GPT entries: %u, entry size: %uB.\n",
9805be5d0d5SZhenlei Huang 			    pp->name, prihdr->hdr_entries, prihdr->hdr_entsz);
9815be5d0d5SZhenlei Huang 			printf(
9825be5d0d5SZhenlei Huang     "GEOM: %s: maximum supported number of GPT entries: %u, entry size: %uB.\n",
9835be5d0d5SZhenlei Huang 			    pp->name, g_part_gpt_scheme.gps_maxent, MAXENTSIZE);
9845be5d0d5SZhenlei Huang 			printf("GEOM: %s: GPT rejected.\n", pp->name);
9855be5d0d5SZhenlei Huang 		} else {
9861d3aed33SMarcel Moolenaar 			printf("GEOM: %s: corrupt or invalid GPT detected.\n",
9871d3aed33SMarcel Moolenaar 			    pp->name);
9885be5d0d5SZhenlei Huang 			printf(
9895be5d0d5SZhenlei Huang 		    "GEOM: %s: GPT rejected -- may not be recoverable.\n",
9901d3aed33SMarcel Moolenaar 			    pp->name);
9915be5d0d5SZhenlei Huang 		}
992ee4d316fSConrad Meyer 		if (prihdr != NULL)
993ee4d316fSConrad Meyer 			g_free(prihdr);
994ee4d316fSConrad Meyer 		if (pritbl != NULL)
995ee4d316fSConrad Meyer 			g_free(pritbl);
996ee4d316fSConrad Meyer 		if (sechdr != NULL)
997ee4d316fSConrad Meyer 			g_free(sechdr);
998ee4d316fSConrad Meyer 		if (sectbl != NULL)
999ee4d316fSConrad Meyer 			g_free(sectbl);
10001d3aed33SMarcel Moolenaar 		return (EINVAL);
10011d3aed33SMarcel Moolenaar 	}
10021d3aed33SMarcel Moolenaar 
10031d3aed33SMarcel Moolenaar 	/*
10041d3aed33SMarcel Moolenaar 	 * If both headers are good but they disagree with each other,
10051d3aed33SMarcel Moolenaar 	 * then invalidate one. We prefer to keep the primary header,
10061d3aed33SMarcel Moolenaar 	 * unless the primary table is corrupt.
10071d3aed33SMarcel Moolenaar 	 */
10081d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
10091d3aed33SMarcel Moolenaar 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
10101c2dee3cSRobert Noland 	    !gpt_matched_hdrs(prihdr, sechdr)) {
1011dd0db05dSMarcel Moolenaar 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
10121d3aed33SMarcel Moolenaar 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
1013dd0db05dSMarcel Moolenaar 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
10141c2dee3cSRobert Noland 			g_free(sechdr);
10151c2dee3cSRobert Noland 			sechdr = NULL;
1016dd0db05dSMarcel Moolenaar 		} else {
10171d3aed33SMarcel Moolenaar 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
1018dd0db05dSMarcel Moolenaar 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
10191c2dee3cSRobert Noland 			g_free(prihdr);
10201c2dee3cSRobert Noland 			prihdr = NULL;
1021dd0db05dSMarcel Moolenaar 		}
10221d3aed33SMarcel Moolenaar 	}
10231d3aed33SMarcel Moolenaar 
1024dd0db05dSMarcel Moolenaar 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
10251d3aed33SMarcel Moolenaar 		printf("GEOM: %s: the primary GPT table is corrupt or "
10261d3aed33SMarcel Moolenaar 		    "invalid.\n", pp->name);
10271d3aed33SMarcel Moolenaar 		printf("GEOM: %s: using the secondary instead -- recovery "
10281d3aed33SMarcel Moolenaar 		    "strongly advised.\n", pp->name);
10291d3aed33SMarcel Moolenaar 		table->hdr = sechdr;
1030e7926a37SAndrey V. Elsukov 		basetable->gpt_corrupt = 1;
10311c2dee3cSRobert Noland 		if (prihdr != NULL)
10321c2dee3cSRobert Noland 			g_free(prihdr);
10331d3aed33SMarcel Moolenaar 		tbl = sectbl;
10341d3aed33SMarcel Moolenaar 		if (pritbl != NULL)
10351d3aed33SMarcel Moolenaar 			g_free(pritbl);
10361d3aed33SMarcel Moolenaar 	} else {
1037dd0db05dSMarcel Moolenaar 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
10381d3aed33SMarcel Moolenaar 			printf("GEOM: %s: the secondary GPT table is corrupt "
10391d3aed33SMarcel Moolenaar 			    "or invalid.\n", pp->name);
10401d3aed33SMarcel Moolenaar 			printf("GEOM: %s: using the primary only -- recovery "
10411d3aed33SMarcel Moolenaar 			    "suggested.\n", pp->name);
1042e7926a37SAndrey V. Elsukov 			basetable->gpt_corrupt = 1;
1043e7926a37SAndrey V. Elsukov 		} else if (table->lba[GPT_ELT_SECHDR] != last) {
1044e7926a37SAndrey V. Elsukov 			printf( "GEOM: %s: the secondary GPT header is not in "
1045e7926a37SAndrey V. Elsukov 			    "the last LBA.\n", pp->name);
1046e7926a37SAndrey V. Elsukov 			basetable->gpt_corrupt = 1;
10471d3aed33SMarcel Moolenaar 		}
10481d3aed33SMarcel Moolenaar 		table->hdr = prihdr;
10491c2dee3cSRobert Noland 		if (sechdr != NULL)
10501c2dee3cSRobert Noland 			g_free(sechdr);
10511d3aed33SMarcel Moolenaar 		tbl = pritbl;
10521d3aed33SMarcel Moolenaar 		if (sectbl != NULL)
10531d3aed33SMarcel Moolenaar 			g_free(sectbl);
10541d3aed33SMarcel Moolenaar 	}
10551d3aed33SMarcel Moolenaar 
10561c2dee3cSRobert Noland 	basetable->gpt_first = table->hdr->hdr_lba_start;
10571c2dee3cSRobert Noland 	basetable->gpt_last = table->hdr->hdr_lba_end;
105896937e3bSMarcel Moolenaar 	basetable->gpt_entries = table->hdr->hdr_entries;
10591d3aed33SMarcel Moolenaar 
106096937e3bSMarcel Moolenaar 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
10611d3aed33SMarcel Moolenaar 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
10621d3aed33SMarcel Moolenaar 			continue;
1063e7926a37SAndrey V. Elsukov 		entry = (struct g_part_gpt_entry *)g_part_new_entry(
1064e7926a37SAndrey V. Elsukov 		    basetable, index + 1, tbl[index].ent_lba_start,
1065e7926a37SAndrey V. Elsukov 		    tbl[index].ent_lba_end);
10661d3aed33SMarcel Moolenaar 		entry->ent = tbl[index];
10671d3aed33SMarcel Moolenaar 	}
10681d3aed33SMarcel Moolenaar 
10691d3aed33SMarcel Moolenaar 	g_free(tbl);
1070369fe59dSMarcel Moolenaar 
1071369fe59dSMarcel Moolenaar 	/*
1072369fe59dSMarcel Moolenaar 	 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1073369fe59dSMarcel Moolenaar 	 * if (and only if) any FAT32 or FAT16 partitions have been
1074369fe59dSMarcel Moolenaar 	 * created. This happens irrespective of whether Boot Camp is
1075369fe59dSMarcel Moolenaar 	 * used/enabled, though it's generally understood to be done
1076369fe59dSMarcel Moolenaar 	 * to support legacy Windows under Boot Camp. We refer to this
1077369fe59dSMarcel Moolenaar 	 * mirroring simply as Boot Camp. We try to detect Boot Camp
1078369fe59dSMarcel Moolenaar 	 * so that we can update the MBR if and when GPT changes have
1079369fe59dSMarcel Moolenaar 	 * been made. Note that we do not enable Boot Camp if not
1080369fe59dSMarcel Moolenaar 	 * previously enabled because we can't assume that we're on a
1081369fe59dSMarcel Moolenaar 	 * Mac alongside Mac OS X.
1082369fe59dSMarcel Moolenaar 	 */
1083369fe59dSMarcel Moolenaar 	table->bootcamp = gpt_is_bootcamp(table, pp->name);
1084369fe59dSMarcel Moolenaar 
10851d3aed33SMarcel Moolenaar 	return (0);
10861d3aed33SMarcel Moolenaar }
10871d3aed33SMarcel Moolenaar 
1088cec283baSPawel Jakub Dawidek static int
1089e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable)
1090e7926a37SAndrey V. Elsukov {
10913bd22a9cSMarcel Moolenaar 	struct g_part_gpt_table *table;
10923bd22a9cSMarcel Moolenaar 	struct g_provider *pp;
1093e7926a37SAndrey V. Elsukov 
10943bd22a9cSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
10953bd22a9cSMarcel Moolenaar 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
10963bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
10973bd22a9cSMarcel Moolenaar 	g_gpt_set_defaults(basetable, pp);
1098e7926a37SAndrey V. Elsukov 	basetable->gpt_corrupt = 0;
1099e7926a37SAndrey V. Elsukov 	return (0);
1100e7926a37SAndrey V. Elsukov }
1101e7926a37SAndrey V. Elsukov 
1102e7926a37SAndrey V. Elsukov static int
1103369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable,
1104369fe59dSMarcel Moolenaar     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1105cec283baSPawel Jakub Dawidek {
1106cec283baSPawel Jakub Dawidek 	struct g_part_gpt_entry *entry;
1107369fe59dSMarcel Moolenaar 	struct g_part_gpt_table *table;
1108ce808c7aSAllan Jude 	struct g_provider *pp;
11093bd22a9cSMarcel Moolenaar 	uint8_t *p;
1110369fe59dSMarcel Moolenaar 	uint64_t attr;
1111369fe59dSMarcel Moolenaar 	int i;
1112cec283baSPawel Jakub Dawidek 
1113369fe59dSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
1114369fe59dSMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
1115369fe59dSMarcel Moolenaar 
1116369fe59dSMarcel Moolenaar 	if (strcasecmp(attrib, "active") == 0) {
11173bd22a9cSMarcel Moolenaar 		if (table->bootcamp) {
11183bd22a9cSMarcel Moolenaar 			/* The active flag must be set on a valid entry. */
11193bd22a9cSMarcel Moolenaar 			if (entry == NULL)
11203bd22a9cSMarcel Moolenaar 				return (ENXIO);
11213bd22a9cSMarcel Moolenaar 			if (baseentry->gpe_index > NDOSPART)
1122369fe59dSMarcel Moolenaar 				return (EINVAL);
1123369fe59dSMarcel Moolenaar 			for (i = 0; i < NDOSPART; i++) {
11243bd22a9cSMarcel Moolenaar 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
11253bd22a9cSMarcel Moolenaar 				p[0] = (i == baseentry->gpe_index - 1)
11263bd22a9cSMarcel Moolenaar 				    ? ((set) ? 0x80 : 0) : 0;
11273bd22a9cSMarcel Moolenaar 			}
11283bd22a9cSMarcel Moolenaar 		} else {
11293bd22a9cSMarcel Moolenaar 			/* The PMBR is marked as active without an entry. */
11303bd22a9cSMarcel Moolenaar 			if (entry != NULL)
11313bd22a9cSMarcel Moolenaar 				return (ENXIO);
11323bd22a9cSMarcel Moolenaar 			for (i = 0; i < NDOSPART; i++) {
11333bd22a9cSMarcel Moolenaar 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
11343bd22a9cSMarcel Moolenaar 				p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
11353bd22a9cSMarcel Moolenaar 			}
1136369fe59dSMarcel Moolenaar 		}
1137369fe59dSMarcel Moolenaar 		return (0);
1138ce808c7aSAllan Jude 	} else if (strcasecmp(attrib, "lenovofix") == 0) {
1139ce808c7aSAllan Jude 		/*
1140ce808c7aSAllan Jude 		 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1141ce808c7aSAllan Jude 		 * This workaround allows Lenovo X220, T420, T520, etc to boot
1142ce808c7aSAllan Jude 		 * from GPT Partitions in BIOS mode.
1143ce808c7aSAllan Jude 		 */
1144ce808c7aSAllan Jude 
1145ce808c7aSAllan Jude 		if (entry != NULL)
1146ce808c7aSAllan Jude 			return (ENXIO);
1147ce808c7aSAllan Jude 
1148ce808c7aSAllan Jude 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1149ce808c7aSAllan Jude 		bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1150ce808c7aSAllan Jude 		gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1151ce808c7aSAllan Jude 		    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1152ce808c7aSAllan Jude 		return (0);
1153369fe59dSMarcel Moolenaar 	}
1154369fe59dSMarcel Moolenaar 
11553bd22a9cSMarcel Moolenaar 	if (entry == NULL)
11563bd22a9cSMarcel Moolenaar 		return (ENODEV);
11573bd22a9cSMarcel Moolenaar 
1158369fe59dSMarcel Moolenaar 	attr = 0;
1159cec283baSPawel Jakub Dawidek 	if (strcasecmp(attrib, "bootme") == 0) {
1160369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTME;
1161cec283baSPawel Jakub Dawidek 	} else if (strcasecmp(attrib, "bootonce") == 0) {
1162369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTONCE;
1163cec283baSPawel Jakub Dawidek 		if (set)
1164369fe59dSMarcel Moolenaar 			attr |= GPT_ENT_ATTR_BOOTME;
1165cec283baSPawel Jakub Dawidek 	} else if (strcasecmp(attrib, "bootfailed") == 0) {
1166cec283baSPawel Jakub Dawidek 		/*
1167cec283baSPawel Jakub Dawidek 		 * It should only be possible to unset BOOTFAILED, but it might
1168cec283baSPawel Jakub Dawidek 		 * be useful for test purposes to also be able to set it.
1169cec283baSPawel Jakub Dawidek 		 */
1170369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTFAILED;
1171cec283baSPawel Jakub Dawidek 	}
1172369fe59dSMarcel Moolenaar 	if (attr == 0)
1173cec283baSPawel Jakub Dawidek 		return (EINVAL);
1174cec283baSPawel Jakub Dawidek 
1175369fe59dSMarcel Moolenaar 	if (set)
1176369fe59dSMarcel Moolenaar 		attr = entry->ent.ent_attr | attr;
1177369fe59dSMarcel Moolenaar 	else
1178369fe59dSMarcel Moolenaar 		attr = entry->ent.ent_attr & ~attr;
1179369fe59dSMarcel Moolenaar 	if (attr != entry->ent.ent_attr) {
1180369fe59dSMarcel Moolenaar 		entry->ent.ent_attr = attr;
1181369fe59dSMarcel Moolenaar 		if (!baseentry->gpe_created)
1182369fe59dSMarcel Moolenaar 			baseentry->gpe_modified = 1;
1183cec283baSPawel Jakub Dawidek 	}
1184cec283baSPawel Jakub Dawidek 	return (0);
1185cec283baSPawel Jakub Dawidek }
1186cec283baSPawel Jakub Dawidek 
11871d3aed33SMarcel Moolenaar static const char *
11881d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
11891d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
11901d3aed33SMarcel Moolenaar {
11911d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
11921d3aed33SMarcel Moolenaar 	struct uuid *type;
1193f1317430SRui Paulo 	struct g_part_uuid_alias *uap;
11941d3aed33SMarcel Moolenaar 
11951d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
11961d3aed33SMarcel Moolenaar 	type = &entry->ent.ent_type;
1197f1317430SRui Paulo 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1198f1317430SRui Paulo 		if (EQUUID(type, uap->uuid))
1199f1317430SRui Paulo 			return (g_part_alias_name(uap->alias));
1200cf231470SMarcel Moolenaar 	buf[0] = '!';
1201cf231470SMarcel Moolenaar 	snprintf_uuid(buf + 1, bufsz - 1, type);
1202f1317430SRui Paulo 
12031d3aed33SMarcel Moolenaar 	return (buf);
12041d3aed33SMarcel Moolenaar }
12051d3aed33SMarcel Moolenaar 
12061d3aed33SMarcel Moolenaar static int
12071d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
12081d3aed33SMarcel Moolenaar {
12091d3aed33SMarcel Moolenaar 	unsigned char *buf, *bp;
12101d3aed33SMarcel Moolenaar 	struct g_provider *pp;
12111d3aed33SMarcel Moolenaar 	struct g_part_entry *baseentry;
12121d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
12131d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
1214e7926a37SAndrey V. Elsukov 	size_t tblsz;
12151d3aed33SMarcel Moolenaar 	uint32_t crc;
12161d3aed33SMarcel Moolenaar 	int error, index;
12171d3aed33SMarcel Moolenaar 
12181d3aed33SMarcel Moolenaar 	pp = cp->provider;
12191d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
122055e0987aSPedro F. Giffuni 	tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
122155e0987aSPedro F. Giffuni 	    pp->sectorsize);
12221d3aed33SMarcel Moolenaar 
1223369fe59dSMarcel Moolenaar 	/* Reconstruct the MBR from the GPT if under Boot Camp. */
1224369fe59dSMarcel Moolenaar 	if (table->bootcamp)
12253bd22a9cSMarcel Moolenaar 		gpt_update_bootcamp(basetable, pp);
1226d4746e10SAndrey V. Elsukov 
12274d32fcb4SMarcel Moolenaar 	/* Write the PMBR */
12281d3aed33SMarcel Moolenaar 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
12294d32fcb4SMarcel Moolenaar 	bcopy(table->mbr, buf, MBRSIZE);
12301d3aed33SMarcel Moolenaar 	error = g_write_data(cp, 0, buf, pp->sectorsize);
12311d3aed33SMarcel Moolenaar 	g_free(buf);
12321d3aed33SMarcel Moolenaar 	if (error)
12331d3aed33SMarcel Moolenaar 		return (error);
12341d3aed33SMarcel Moolenaar 
12351d3aed33SMarcel Moolenaar 	/* Allocate space for the header and entries. */
1236e7926a37SAndrey V. Elsukov 	buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
12371d3aed33SMarcel Moolenaar 
12381c2dee3cSRobert Noland 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
12391c2dee3cSRobert Noland 	le32enc(buf + 8, table->hdr->hdr_revision);
12401c2dee3cSRobert Noland 	le32enc(buf + 12, table->hdr->hdr_size);
12411c2dee3cSRobert Noland 	le64enc(buf + 40, table->hdr->hdr_lba_start);
12421c2dee3cSRobert Noland 	le64enc(buf + 48, table->hdr->hdr_lba_end);
12431c2dee3cSRobert Noland 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
12441c2dee3cSRobert Noland 	le32enc(buf + 80, table->hdr->hdr_entries);
12451c2dee3cSRobert Noland 	le32enc(buf + 84, table->hdr->hdr_entsz);
12461d3aed33SMarcel Moolenaar 
12471d3aed33SMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1248d287f590SMarcel Moolenaar 		if (baseentry->gpe_deleted)
1249d287f590SMarcel Moolenaar 			continue;
12501d3aed33SMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
12511d3aed33SMarcel Moolenaar 		index = baseentry->gpe_index - 1;
12521c2dee3cSRobert Noland 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
12531d3aed33SMarcel Moolenaar 		le_uuid_enc(bp, &entry->ent.ent_type);
12541d3aed33SMarcel Moolenaar 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
12551d3aed33SMarcel Moolenaar 		le64enc(bp + 32, entry->ent.ent_lba_start);
12561d3aed33SMarcel Moolenaar 		le64enc(bp + 40, entry->ent.ent_lba_end);
12571d3aed33SMarcel Moolenaar 		le64enc(bp + 48, entry->ent.ent_attr);
12581d3aed33SMarcel Moolenaar 		memcpy(bp + 56, entry->ent.ent_name,
12591d3aed33SMarcel Moolenaar 		    sizeof(entry->ent.ent_name));
12601d3aed33SMarcel Moolenaar 	}
12611d3aed33SMarcel Moolenaar 
12621d3aed33SMarcel Moolenaar 	crc = crc32(buf + pp->sectorsize,
12631c2dee3cSRobert Noland 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
12641d3aed33SMarcel Moolenaar 	le32enc(buf + 88, crc);
12651d3aed33SMarcel Moolenaar 
12661d3aed33SMarcel Moolenaar 	/* Write primary meta-data. */
12671d3aed33SMarcel Moolenaar 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
12681d3aed33SMarcel Moolenaar 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
12691d3aed33SMarcel Moolenaar 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
12701d3aed33SMarcel Moolenaar 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
12711c2dee3cSRobert Noland 	crc = crc32(buf, table->hdr->hdr_size);
12721d3aed33SMarcel Moolenaar 	le32enc(buf + 16, crc);
12731d3aed33SMarcel Moolenaar 
1274cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1275799eac8cSAndrey V. Elsukov 		error = g_write_data(cp,
1276799eac8cSAndrey V. Elsukov 		    (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1277799eac8cSAndrey V. Elsukov 		    buf + (index + 1) * pp->sectorsize,
1278cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1279799eac8cSAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
12801d3aed33SMarcel Moolenaar 		if (error)
12811d3aed33SMarcel Moolenaar 			goto out;
1282799eac8cSAndrey V. Elsukov 	}
12831d3aed33SMarcel Moolenaar 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
12841d3aed33SMarcel Moolenaar 	    buf, pp->sectorsize);
12851d3aed33SMarcel Moolenaar 	if (error)
12861d3aed33SMarcel Moolenaar 		goto out;
12871d3aed33SMarcel Moolenaar 
12881d3aed33SMarcel Moolenaar 	/* Write secondary meta-data. */
12891d3aed33SMarcel Moolenaar 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
12901d3aed33SMarcel Moolenaar 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
12911d3aed33SMarcel Moolenaar 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
12921d3aed33SMarcel Moolenaar 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
12931c2dee3cSRobert Noland 	crc = crc32(buf, table->hdr->hdr_size);
12941d3aed33SMarcel Moolenaar 	le32enc(buf + 16, crc);
12951d3aed33SMarcel Moolenaar 
1296cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1297799eac8cSAndrey V. Elsukov 		error = g_write_data(cp,
1298799eac8cSAndrey V. Elsukov 		    (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1299799eac8cSAndrey V. Elsukov 		    buf + (index + 1) * pp->sectorsize,
1300cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1301799eac8cSAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
13021d3aed33SMarcel Moolenaar 		if (error)
13031d3aed33SMarcel Moolenaar 			goto out;
1304799eac8cSAndrey V. Elsukov 	}
13051d3aed33SMarcel Moolenaar 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
13061d3aed33SMarcel Moolenaar 	    buf, pp->sectorsize);
13071d3aed33SMarcel Moolenaar 
13081d3aed33SMarcel Moolenaar  out:
13091d3aed33SMarcel Moolenaar 	g_free(buf);
13101d3aed33SMarcel Moolenaar 	return (error);
13111d3aed33SMarcel Moolenaar }
13121d3aed33SMarcel Moolenaar 
13131d3aed33SMarcel Moolenaar static void
13142b9be055SAndrey V. Elsukov g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
13152b9be055SAndrey V. Elsukov {
13164787115dSMarcel Moolenaar 	struct g_part_entry *baseentry;
13174787115dSMarcel Moolenaar 	struct g_part_gpt_entry *entry;
13182b9be055SAndrey V. Elsukov 	struct g_part_gpt_table *table;
13194787115dSMarcel Moolenaar 	quad_t start, end, min, max;
13204787115dSMarcel Moolenaar 	quad_t lba, last;
13214787115dSMarcel Moolenaar 	size_t spb, tblsz;
13222b9be055SAndrey V. Elsukov 
13232b9be055SAndrey V. Elsukov 	table = (struct g_part_gpt_table *)basetable;
13242b9be055SAndrey V. Elsukov 	last = pp->mediasize / pp->sectorsize - 1;
132555e0987aSPedro F. Giffuni 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
132655e0987aSPedro F. Giffuni 	    pp->sectorsize);
13272b9be055SAndrey V. Elsukov 
13282b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_PRIHDR] = 1;
13292b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_PRITBL] = 2;
13302b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_SECHDR] = last;
13312b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
13322b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
13332b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
13342b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
13352b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
13362b9be055SAndrey V. Elsukov 
13374787115dSMarcel Moolenaar 	max = start = 2 + tblsz;
13384787115dSMarcel Moolenaar 	min = end = last - tblsz - 1;
13394787115dSMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
13404787115dSMarcel Moolenaar 		if (baseentry->gpe_deleted)
13414787115dSMarcel Moolenaar 			continue;
13424787115dSMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
13434787115dSMarcel Moolenaar 		if (entry->ent.ent_lba_start < min)
13444787115dSMarcel Moolenaar 			min = entry->ent.ent_lba_start;
13454787115dSMarcel Moolenaar 		if (entry->ent.ent_lba_end > max)
13464787115dSMarcel Moolenaar 			max = entry->ent.ent_lba_end;
13474787115dSMarcel Moolenaar 	}
13484787115dSMarcel Moolenaar 	spb = 4096 / pp->sectorsize;
13494787115dSMarcel Moolenaar 	if (spb > 1) {
13504787115dSMarcel Moolenaar 		lba = start + ((start % spb) ? spb - start % spb : 0);
13514787115dSMarcel Moolenaar 		if (lba <= min)
13524787115dSMarcel Moolenaar 			start = lba;
13534787115dSMarcel Moolenaar 		lba = end - (end + 1) % spb;
13544787115dSMarcel Moolenaar 		if (max <= lba)
13554787115dSMarcel Moolenaar 			end = lba;
13564787115dSMarcel Moolenaar 	}
13574787115dSMarcel Moolenaar 	table->hdr->hdr_lba_start = start;
13584787115dSMarcel Moolenaar 	table->hdr->hdr_lba_end = end;
13592b9be055SAndrey V. Elsukov 
13604787115dSMarcel Moolenaar 	basetable->gpt_first = start;
13614787115dSMarcel Moolenaar 	basetable->gpt_last = end;
13622b9be055SAndrey V. Elsukov }
13632b9be055SAndrey V. Elsukov 
13642b9be055SAndrey V. Elsukov static void
1365d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
13661d3aed33SMarcel Moolenaar {
13671d3aed33SMarcel Moolenaar 	u_int bo;
13681d3aed33SMarcel Moolenaar 	uint32_t ch;
13691d3aed33SMarcel Moolenaar 	uint16_t c;
13701d3aed33SMarcel Moolenaar 
1371d3532631SMarcel Moolenaar 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
13721d3aed33SMarcel Moolenaar 	while (len > 0 && *str != 0) {
13731d3aed33SMarcel Moolenaar 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
13741d3aed33SMarcel Moolenaar 		str++, len--;
13751d3aed33SMarcel Moolenaar 		if ((ch & 0xf800) == 0xd800) {
13761d3aed33SMarcel Moolenaar 			if (len > 0) {
13771d3aed33SMarcel Moolenaar 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
13781d3aed33SMarcel Moolenaar 				    : le16toh(*str);
13791d3aed33SMarcel Moolenaar 				str++, len--;
13801d3aed33SMarcel Moolenaar 			} else
13811d3aed33SMarcel Moolenaar 				c = 0xfffd;
13821d3aed33SMarcel Moolenaar 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
13831d3aed33SMarcel Moolenaar 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
13841d3aed33SMarcel Moolenaar 				ch += 0x10000;
13851d3aed33SMarcel Moolenaar 			} else
13861d3aed33SMarcel Moolenaar 				ch = 0xfffd;
13871d3aed33SMarcel Moolenaar 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
13881d3aed33SMarcel Moolenaar 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
13891d3aed33SMarcel Moolenaar 			continue;
13901d3aed33SMarcel Moolenaar 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
13911d3aed33SMarcel Moolenaar 			continue;
13921d3aed33SMarcel Moolenaar 
1393d3532631SMarcel Moolenaar 		/* Write the Unicode character in UTF-8 */
13941d3aed33SMarcel Moolenaar 		if (ch < 0x80)
139539dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c", ch);
13961d3aed33SMarcel Moolenaar 		else if (ch < 0x800)
139739dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
13981d3aed33SMarcel Moolenaar 			    0x80 | (ch & 0x3f));
13991d3aed33SMarcel Moolenaar 		else if (ch < 0x10000)
140039dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
14011d3aed33SMarcel Moolenaar 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
14021d3aed33SMarcel Moolenaar 		else if (ch < 0x200000)
140339dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
140439dcac84SAndrey V. Elsukov 			    (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
14051d3aed33SMarcel Moolenaar 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
14061d3aed33SMarcel Moolenaar 	}
14071d3aed33SMarcel Moolenaar }
1408d3532631SMarcel Moolenaar 
1409d3532631SMarcel Moolenaar static void
1410d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1411d3532631SMarcel Moolenaar {
1412d3532631SMarcel Moolenaar 	size_t s16idx, s8idx;
1413d3532631SMarcel Moolenaar 	uint32_t utfchar;
1414d3532631SMarcel Moolenaar 	unsigned int c, utfbytes;
1415d3532631SMarcel Moolenaar 
1416d3532631SMarcel Moolenaar 	s8idx = s16idx = 0;
1417d3532631SMarcel Moolenaar 	utfchar = 0;
1418d3532631SMarcel Moolenaar 	utfbytes = 0;
1419d3532631SMarcel Moolenaar 	bzero(s16, s16len << 1);
1420d3532631SMarcel Moolenaar 	while (s8[s8idx] != 0 && s16idx < s16len) {
1421d3532631SMarcel Moolenaar 		c = s8[s8idx++];
1422d3532631SMarcel Moolenaar 		if ((c & 0xc0) != 0x80) {
1423d3532631SMarcel Moolenaar 			/* Initial characters. */
1424d3532631SMarcel Moolenaar 			if (utfbytes != 0) {
1425d3532631SMarcel Moolenaar 				/* Incomplete encoding of previous char. */
1426d3532631SMarcel Moolenaar 				s16[s16idx++] = htole16(0xfffd);
1427d3532631SMarcel Moolenaar 			}
1428d3532631SMarcel Moolenaar 			if ((c & 0xf8) == 0xf0) {
1429d3532631SMarcel Moolenaar 				utfchar = c & 0x07;
1430d3532631SMarcel Moolenaar 				utfbytes = 3;
1431d3532631SMarcel Moolenaar 			} else if ((c & 0xf0) == 0xe0) {
1432d3532631SMarcel Moolenaar 				utfchar = c & 0x0f;
1433d3532631SMarcel Moolenaar 				utfbytes = 2;
1434d3532631SMarcel Moolenaar 			} else if ((c & 0xe0) == 0xc0) {
1435d3532631SMarcel Moolenaar 				utfchar = c & 0x1f;
1436d3532631SMarcel Moolenaar 				utfbytes = 1;
1437d3532631SMarcel Moolenaar 			} else {
1438d3532631SMarcel Moolenaar 				utfchar = c & 0x7f;
1439d3532631SMarcel Moolenaar 				utfbytes = 0;
1440d3532631SMarcel Moolenaar 			}
1441d3532631SMarcel Moolenaar 		} else {
1442d3532631SMarcel Moolenaar 			/* Followup characters. */
1443d3532631SMarcel Moolenaar 			if (utfbytes > 0) {
1444d3532631SMarcel Moolenaar 				utfchar = (utfchar << 6) + (c & 0x3f);
1445d3532631SMarcel Moolenaar 				utfbytes--;
1446d3532631SMarcel Moolenaar 			} else if (utfbytes == 0)
1447d3532631SMarcel Moolenaar 				utfbytes = ~0;
1448d3532631SMarcel Moolenaar 		}
1449d3532631SMarcel Moolenaar 		/*
1450d3532631SMarcel Moolenaar 		 * Write the complete Unicode character as UTF-16 when we
1451d3532631SMarcel Moolenaar 		 * have all the UTF-8 charactars collected.
1452d3532631SMarcel Moolenaar 		 */
1453d3532631SMarcel Moolenaar 		if (utfbytes == 0) {
1454d3532631SMarcel Moolenaar 			/*
1455d3532631SMarcel Moolenaar 			 * If we need to write 2 UTF-16 characters, but
1456d3532631SMarcel Moolenaar 			 * we only have room for 1, then we truncate the
1457d3532631SMarcel Moolenaar 			 * string by writing a 0 instead.
1458d3532631SMarcel Moolenaar 			 */
1459d3532631SMarcel Moolenaar 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1460d3532631SMarcel Moolenaar 				s16[s16idx++] =
1461d3532631SMarcel Moolenaar 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
1462d3532631SMarcel Moolenaar 				s16[s16idx++] =
1463d3532631SMarcel Moolenaar 				    htole16(0xdc00 | (utfchar & 0x3ff));
1464d3532631SMarcel Moolenaar 			} else
1465d3532631SMarcel Moolenaar 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1466d3532631SMarcel Moolenaar 				    htole16(utfchar);
1467d3532631SMarcel Moolenaar 		}
1468d3532631SMarcel Moolenaar 	}
1469d3532631SMarcel Moolenaar 	/*
1470d3532631SMarcel Moolenaar 	 * If our input string was truncated, append an invalid encoding
1471d3532631SMarcel Moolenaar 	 * character to the output string.
1472d3532631SMarcel Moolenaar 	 */
1473d3532631SMarcel Moolenaar 	if (utfbytes != 0 && s16idx < s16len)
1474d3532631SMarcel Moolenaar 		s16[s16idx++] = htole16(0xfffd);
1475d3532631SMarcel Moolenaar }
1476