xref: /freebsd/sys/geom/part/g_part_gpt.c (revision 5c9f0f72f47ea5315e5147185e47c2efca2e8c85)
11d3aed33SMarcel Moolenaar /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
311d3aed33SMarcel Moolenaar #include <sys/bio.h>
321d3aed33SMarcel Moolenaar #include <sys/diskmbr.h>
33f89d2072SXin LI #include <sys/gsb_crc32.h>
341d3aed33SMarcel Moolenaar #include <sys/endian.h>
351d3aed33SMarcel Moolenaar #include <sys/gpt.h>
361d3aed33SMarcel Moolenaar #include <sys/kernel.h>
371d3aed33SMarcel Moolenaar #include <sys/kobj.h>
381d3aed33SMarcel Moolenaar #include <sys/limits.h>
391d3aed33SMarcel Moolenaar #include <sys/lock.h>
401d3aed33SMarcel Moolenaar #include <sys/malloc.h>
411d3aed33SMarcel Moolenaar #include <sys/mutex.h>
421d3aed33SMarcel Moolenaar #include <sys/queue.h>
431d3aed33SMarcel Moolenaar #include <sys/sbuf.h>
441d3aed33SMarcel Moolenaar #include <sys/systm.h>
45cb08c2ccSAlexander Leidinger #include <sys/sysctl.h>
461d3aed33SMarcel Moolenaar #include <sys/uuid.h>
471d3aed33SMarcel Moolenaar #include <geom/geom.h>
4839dcac84SAndrey V. Elsukov #include <geom/geom_int.h>
491d3aed33SMarcel Moolenaar #include <geom/part/g_part.h>
501d3aed33SMarcel Moolenaar 
511d3aed33SMarcel Moolenaar #include "g_part_if.h"
521d3aed33SMarcel Moolenaar 
53cb08c2ccSAlexander Leidinger FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
54cb08c2ccSAlexander Leidinger 
55ef03f57dSKyle Evans SYSCTL_DECL(_kern_geom_part);
567029da5cSPawel Biernacki static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt,
577029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
58ef03f57dSKyle Evans     "GEOM_PART_GPT GUID Partition Table");
59ef03f57dSKyle Evans 
60ef03f57dSKyle Evans static u_int allow_nesting = 0;
61ef03f57dSKyle Evans SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting,
62ef03f57dSKyle Evans     CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes");
63ef03f57dSKyle Evans 
641d3aed33SMarcel Moolenaar CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
651d3aed33SMarcel Moolenaar CTASSERT(sizeof(struct gpt_ent) == 128);
661d3aed33SMarcel Moolenaar 
67cea05ed9SEugene Grosbein extern u_int geom_part_check_integrity;
68cea05ed9SEugene Grosbein 
691d3aed33SMarcel Moolenaar #define	EQUUID(a,b)	(memcmp(a, b, sizeof(struct uuid)) == 0)
701d3aed33SMarcel Moolenaar 
714d32fcb4SMarcel Moolenaar #define	MBRSIZE		512
724d32fcb4SMarcel Moolenaar 
731d3aed33SMarcel Moolenaar enum gpt_elt {
741d3aed33SMarcel Moolenaar 	GPT_ELT_PRIHDR,
751d3aed33SMarcel Moolenaar 	GPT_ELT_PRITBL,
761d3aed33SMarcel Moolenaar 	GPT_ELT_SECHDR,
771d3aed33SMarcel Moolenaar 	GPT_ELT_SECTBL,
781d3aed33SMarcel Moolenaar 	GPT_ELT_COUNT
791d3aed33SMarcel Moolenaar };
801d3aed33SMarcel Moolenaar 
811d3aed33SMarcel Moolenaar enum gpt_state {
821d3aed33SMarcel Moolenaar 	GPT_STATE_UNKNOWN,	/* Not determined. */
831d3aed33SMarcel Moolenaar 	GPT_STATE_MISSING,	/* No signature found. */
841d3aed33SMarcel Moolenaar 	GPT_STATE_CORRUPT,	/* Checksum mismatch. */
851d3aed33SMarcel Moolenaar 	GPT_STATE_INVALID,	/* Nonconformant/invalid. */
865be5d0d5SZhenlei Huang 	GPT_STATE_UNSUPPORTED,  /* Not supported. */
871d3aed33SMarcel Moolenaar 	GPT_STATE_OK		/* Perfectly fine. */
881d3aed33SMarcel Moolenaar };
891d3aed33SMarcel Moolenaar 
901d3aed33SMarcel Moolenaar struct g_part_gpt_table {
911d3aed33SMarcel Moolenaar 	struct g_part_table	base;
924d32fcb4SMarcel Moolenaar 	u_char			mbr[MBRSIZE];
931c2dee3cSRobert Noland 	struct gpt_hdr		*hdr;
941d3aed33SMarcel Moolenaar 	quad_t			lba[GPT_ELT_COUNT];
951d3aed33SMarcel Moolenaar 	enum gpt_state		state[GPT_ELT_COUNT];
96369fe59dSMarcel Moolenaar 	int			bootcamp;
971d3aed33SMarcel Moolenaar };
981d3aed33SMarcel Moolenaar 
991d3aed33SMarcel Moolenaar struct g_part_gpt_entry {
1001d3aed33SMarcel Moolenaar 	struct g_part_entry	base;
1011d3aed33SMarcel Moolenaar 	struct gpt_ent		ent;
1021d3aed33SMarcel Moolenaar };
1031d3aed33SMarcel Moolenaar 
104d3532631SMarcel Moolenaar static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
105d3532631SMarcel Moolenaar static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
106*5c9f0f72SWarner Losh static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *,
107*5c9f0f72SWarner Losh     struct g_part_parms *);
108d3532631SMarcel Moolenaar 
1091d3aed33SMarcel Moolenaar static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
1101d3aed33SMarcel Moolenaar     struct g_part_parms *);
1114d32fcb4SMarcel Moolenaar static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
1121d3aed33SMarcel Moolenaar static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
1131d3aed33SMarcel Moolenaar static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
11451f53a08SWarner Losh static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
1155db67052SMarcel Moolenaar     struct sbuf *, const char *);
1161d3aed33SMarcel Moolenaar static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
1171d3aed33SMarcel Moolenaar static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
1181d3aed33SMarcel Moolenaar     struct g_part_parms *);
11951f53a08SWarner Losh static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
1201d3aed33SMarcel Moolenaar     char *, size_t);
1211d3aed33SMarcel Moolenaar static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
1221d3aed33SMarcel Moolenaar static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
123cec283baSPawel Jakub Dawidek static int g_part_gpt_setunset(struct g_part_table *table,
124cec283baSPawel Jakub Dawidek     struct g_part_entry *baseentry, const char *attrib, unsigned int set);
1251d3aed33SMarcel Moolenaar static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
1261d3aed33SMarcel Moolenaar     char *, size_t);
1271d3aed33SMarcel Moolenaar static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
1283f71c319SMarcel Moolenaar static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
1293f71c319SMarcel Moolenaar     struct g_part_parms *);
130e7926a37SAndrey V. Elsukov static int g_part_gpt_recover(struct g_part_table *);
1311d3aed33SMarcel Moolenaar 
1321d3aed33SMarcel Moolenaar static kobj_method_t g_part_gpt_methods[] = {
1331d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_add,		g_part_gpt_add),
1344d32fcb4SMarcel Moolenaar 	KOBJMETHOD(g_part_bootcode,	g_part_gpt_bootcode),
1351d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_create,	g_part_gpt_create),
1361d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_destroy,	g_part_gpt_destroy),
1375db67052SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpconf,	g_part_gpt_dumpconf),
1381d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpto,	g_part_gpt_dumpto),
1391d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_modify,	g_part_gpt_modify),
1403f71c319SMarcel Moolenaar 	KOBJMETHOD(g_part_resize,	g_part_gpt_resize),
1411d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_name,		g_part_gpt_name),
1421d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_probe,	g_part_gpt_probe),
1431d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_read,		g_part_gpt_read),
144e7926a37SAndrey V. Elsukov 	KOBJMETHOD(g_part_recover,	g_part_gpt_recover),
145cec283baSPawel Jakub Dawidek 	KOBJMETHOD(g_part_setunset,	g_part_gpt_setunset),
1461d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_type,		g_part_gpt_type),
1471d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_write,	g_part_gpt_write),
1481d3aed33SMarcel Moolenaar 	{ 0, 0 }
1491d3aed33SMarcel Moolenaar };
1501d3aed33SMarcel Moolenaar 
1515be5d0d5SZhenlei Huang #define MAXENTSIZE 1024
1525be5d0d5SZhenlei Huang 
1531d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_gpt_scheme = {
1541d3aed33SMarcel Moolenaar 	"GPT",
1551d3aed33SMarcel Moolenaar 	g_part_gpt_methods,
1561d3aed33SMarcel Moolenaar 	sizeof(struct g_part_gpt_table),
1571d3aed33SMarcel Moolenaar 	.gps_entrysz = sizeof(struct g_part_gpt_entry),
1581d3aed33SMarcel Moolenaar 	.gps_minent = 128,
159799eac8cSAndrey V. Elsukov 	.gps_maxent = 4096,
1604d32fcb4SMarcel Moolenaar 	.gps_bootcodesz = MBRSIZE,
1611d3aed33SMarcel Moolenaar };
1624ffca444SMarcel Moolenaar G_PART_SCHEME_DECLARE(g_part_gpt);
16374d6c131SKyle Evans MODULE_VERSION(geom_part_gpt, 0);
1641d3aed33SMarcel Moolenaar 
165b42712a8SConrad Meyer static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
166f1317430SRui Paulo static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
1674fb4ebe0SAndrey V. Elsukov static struct uuid gpt_uuid_apple_core_storage =
1684fb4ebe0SAndrey V. Elsukov     GPT_ENT_TYPE_APPLE_CORE_STORAGE;
16987662ab3SRui Paulo static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
170f1317430SRui Paulo static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
171f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
172f1317430SRui Paulo static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
173f1317430SRui Paulo static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
174f1317430SRui Paulo static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
175cb1480f8SConrad Meyer static struct uuid gpt_uuid_apple_zfs = GPT_ENT_TYPE_APPLE_ZFS;
17688007f61SAndrey V. Elsukov static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
1779c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
1789c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
1799c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
1809c0c355fSAllan Jude static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT;
1819c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD;
1829c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER;
1839c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2;
1849c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32;
1859c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64;
1869c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY;
1879c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP;
1889c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1;
1899c0c355fSAllan Jude static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM;
1901d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
1911d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
192f352a0d4SJohn Baldwin static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
193f24a8224SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
1941d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
1951d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
1961d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
197a1fedf91SMarcel Moolenaar static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
1989c296a21SEd Maste static struct uuid gpt_uuid_hifive_fsbl = GPT_ENT_TYPE_HIFIVE_FSBL;
1999c296a21SEd Maste static struct uuid gpt_uuid_hifive_bbl = GPT_ENT_TYPE_HIFIVE_BBL;
200f1317430SRui Paulo static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
201f1317430SRui Paulo static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
202f1317430SRui Paulo static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
2031d3aed33SMarcel Moolenaar static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
2049c0c355fSAllan Jude static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
20533f7a412SRui Paulo static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
20633f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
20733f7a412SRui Paulo static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
2089c0c355fSAllan Jude static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY;
2099c0c355fSAllan Jude static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
2109c0c355fSAllan Jude static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES;
21133f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
21233f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
21333f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
21433f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
21533f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
21633f7a412SRui Paulo static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
2179c0c355fSAllan Jude static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA;
2181ee0f089SNathan Whitehorn static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT;
219cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_boot = GPT_ENT_TYPE_SOLARIS_BOOT;
220cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_root = GPT_ENT_TYPE_SOLARIS_ROOT;
221cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_swap = GPT_ENT_TYPE_SOLARIS_SWAP;
222cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_backup = GPT_ENT_TYPE_SOLARIS_BACKUP;
223cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_var = GPT_ENT_TYPE_SOLARIS_VAR;
224cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_home = GPT_ENT_TYPE_SOLARIS_HOME;
225cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_altsec = GPT_ENT_TYPE_SOLARIS_ALTSEC;
226cb1480f8SConrad Meyer static struct uuid gpt_uuid_solaris_reserved = GPT_ENT_TYPE_SOLARIS_RESERVED;
2279c0c355fSAllan Jude static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
2289c0c355fSAllan Jude static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
2299c0c355fSAllan Jude static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
2309c0c355fSAllan Jude static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
2319c0c355fSAllan Jude static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR;
2321d3aed33SMarcel Moolenaar 
233f1317430SRui Paulo static struct g_part_uuid_alias {
234f1317430SRui Paulo 	struct uuid *uuid;
235f1317430SRui Paulo 	int alias;
236369fe59dSMarcel Moolenaar 	int mbrtype;
237f1317430SRui Paulo } gpt_uuid_alias_match[] = {
238b42712a8SConrad Meyer 	{ &gpt_uuid_apple_apfs,		G_PART_ALIAS_APPLE_APFS,	 0 },
239369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_boot,		G_PART_ALIAS_APPLE_BOOT,	 0xab },
2404fb4ebe0SAndrey V. Elsukov 	{ &gpt_uuid_apple_core_storage,	G_PART_ALIAS_APPLE_CORE_STORAGE, 0 },
241369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_hfs,		G_PART_ALIAS_APPLE_HFS,		 0xaf },
242369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_label,	G_PART_ALIAS_APPLE_LABEL,	 0 },
243369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_raid,		G_PART_ALIAS_APPLE_RAID,	 0 },
244369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_raid_offline,	G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
245369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_tv_recovery,	G_PART_ALIAS_APPLE_TV_RECOVERY,	 0 },
246369fe59dSMarcel Moolenaar 	{ &gpt_uuid_apple_ufs,		G_PART_ALIAS_APPLE_UFS,		 0 },
247cb1480f8SConrad Meyer 	{ &gpt_uuid_apple_zfs,		G_PART_ALIAS_APPLE_ZFS,		 0 },
248369fe59dSMarcel Moolenaar 	{ &gpt_uuid_bios_boot,		G_PART_ALIAS_BIOS_BOOT,		 0 },
2499c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_firmware,	G_PART_ALIAS_CHROMEOS_FIRMWARE,	 0 },
2509c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_kernel,	G_PART_ALIAS_CHROMEOS_KERNEL,	 0 },
2519c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_reserved,	G_PART_ALIAS_CHROMEOS_RESERVED,	 0 },
2529c0c355fSAllan Jude 	{ &gpt_uuid_chromeos_root,	G_PART_ALIAS_CHROMEOS_ROOT,	 0 },
2539c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_ccd,		G_PART_ALIAS_DFBSD_CCD,		 0 },
2549c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_hammer,	G_PART_ALIAS_DFBSD_HAMMER,	 0 },
2559c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_hammer2,	G_PART_ALIAS_DFBSD_HAMMER2,	 0 },
2569c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_label32,	G_PART_ALIAS_DFBSD,		 0xa5 },
2579c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_label64,	G_PART_ALIAS_DFBSD64,		 0xa5 },
2589c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_legacy,	G_PART_ALIAS_DFBSD_LEGACY,	 0 },
2599c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_swap,		G_PART_ALIAS_DFBSD_SWAP,	 0 },
2609c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_ufs1,		G_PART_ALIAS_DFBSD_UFS,		 0 },
2619c0c355fSAllan Jude 	{ &gpt_uuid_dfbsd_vinum,	G_PART_ALIAS_DFBSD_VINUM,	 0 },
262369fe59dSMarcel Moolenaar 	{ &gpt_uuid_efi, 		G_PART_ALIAS_EFI,		 0xee },
263369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd,		G_PART_ALIAS_FREEBSD,		 0xa5 },
264369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_boot, 	G_PART_ALIAS_FREEBSD_BOOT,	 0 },
265f24a8224SMarcel Moolenaar 	{ &gpt_uuid_freebsd_nandfs, 	G_PART_ALIAS_FREEBSD_NANDFS,	 0 },
266369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_swap,	G_PART_ALIAS_FREEBSD_SWAP,	 0 },
267369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_ufs,	G_PART_ALIAS_FREEBSD_UFS,	 0 },
268369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_vinum,	G_PART_ALIAS_FREEBSD_VINUM,	 0 },
269369fe59dSMarcel Moolenaar 	{ &gpt_uuid_freebsd_zfs,	G_PART_ALIAS_FREEBSD_ZFS,	 0 },
2709c296a21SEd Maste 	{ &gpt_uuid_hifive_fsbl,	G_PART_ALIAS_HIFIVE_FSBL,	 0 },
2719c296a21SEd Maste 	{ &gpt_uuid_hifive_bbl,		G_PART_ALIAS_HIFIVE_BBL,	 0 },
272369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_data,		G_PART_ALIAS_LINUX_DATA,	 0x0b },
273369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_lvm,		G_PART_ALIAS_LINUX_LVM,		 0 },
274369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_raid,		G_PART_ALIAS_LINUX_RAID,	 0 },
275369fe59dSMarcel Moolenaar 	{ &gpt_uuid_linux_swap,		G_PART_ALIAS_LINUX_SWAP,	 0 },
276369fe59dSMarcel Moolenaar 	{ &gpt_uuid_mbr,		G_PART_ALIAS_MBR,		 0 },
277369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_basic_data,	G_PART_ALIAS_MS_BASIC_DATA,	 0x0b },
278369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_ldm_data,	G_PART_ALIAS_MS_LDM_DATA,	 0 },
279369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_ldm_metadata,	G_PART_ALIAS_MS_LDM_METADATA,	 0 },
2809c0c355fSAllan Jude 	{ &gpt_uuid_ms_recovery,	G_PART_ALIAS_MS_RECOVERY,	 0 },
281369fe59dSMarcel Moolenaar 	{ &gpt_uuid_ms_reserved,	G_PART_ALIAS_MS_RESERVED,	 0 },
2829c0c355fSAllan Jude 	{ &gpt_uuid_ms_spaces,		G_PART_ALIAS_MS_SPACES,		 0 },
283369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_ccd,		G_PART_ALIAS_NETBSD_CCD,	 0 },
284369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_cgd,		G_PART_ALIAS_NETBSD_CGD,	 0 },
285369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_ffs,		G_PART_ALIAS_NETBSD_FFS,	 0 },
286369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_lfs,		G_PART_ALIAS_NETBSD_LFS,	 0 },
287369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_raid,	G_PART_ALIAS_NETBSD_RAID,	 0 },
288369fe59dSMarcel Moolenaar 	{ &gpt_uuid_netbsd_swap,	G_PART_ALIAS_NETBSD_SWAP,	 0 },
2899c0c355fSAllan Jude 	{ &gpt_uuid_openbsd_data,	G_PART_ALIAS_OPENBSD_DATA,	 0 },
2901ee0f089SNathan Whitehorn 	{ &gpt_uuid_prep_boot,		G_PART_ALIAS_PREP_BOOT,		 0x41 },
291cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_boot,	G_PART_ALIAS_SOLARIS_BOOT,	 0 },
292cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_root,	G_PART_ALIAS_SOLARIS_ROOT,	 0 },
293cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_swap,	G_PART_ALIAS_SOLARIS_SWAP,	 0 },
294cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_backup,	G_PART_ALIAS_SOLARIS_BACKUP,	 0 },
295cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_var,	G_PART_ALIAS_SOLARIS_VAR,	 0 },
296cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_home,	G_PART_ALIAS_SOLARIS_HOME,	 0 },
297cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_altsec,	G_PART_ALIAS_SOLARIS_ALTSEC,	 0 },
298cb1480f8SConrad Meyer 	{ &gpt_uuid_solaris_reserved,	G_PART_ALIAS_SOLARIS_RESERVED,	 0 },
2999c0c355fSAllan Jude 	{ &gpt_uuid_vmfs,		G_PART_ALIAS_VMFS,		 0 },
3009c0c355fSAllan Jude 	{ &gpt_uuid_vmkdiag,		G_PART_ALIAS_VMKDIAG,		 0 },
3019c0c355fSAllan Jude 	{ &gpt_uuid_vmreserved,		G_PART_ALIAS_VMRESERVED,	 0 },
3029c0c355fSAllan Jude 	{ &gpt_uuid_vmvsanhdr,		G_PART_ALIAS_VMVSANHDR,		 0 },
303369fe59dSMarcel Moolenaar 	{ NULL, 0, 0 }
304f1317430SRui Paulo };
305f1317430SRui Paulo 
306369fe59dSMarcel Moolenaar static int
307369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
308369fe59dSMarcel Moolenaar     quad_t end)
309369fe59dSMarcel Moolenaar {
310369fe59dSMarcel Moolenaar 
311369fe59dSMarcel Moolenaar 	if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
312369fe59dSMarcel Moolenaar 		return (EINVAL);
313369fe59dSMarcel Moolenaar 
314369fe59dSMarcel Moolenaar 	mbr += DOSPARTOFF + idx * DOSPARTSIZE;
315369fe59dSMarcel Moolenaar 	mbr[0] = 0;
316369fe59dSMarcel Moolenaar 	if (start == 1) {
317369fe59dSMarcel Moolenaar 		/*
318369fe59dSMarcel Moolenaar 		 * Treat the PMBR partition specially to maximize
319369fe59dSMarcel Moolenaar 		 * interoperability with BIOSes.
320369fe59dSMarcel Moolenaar 		 */
321369fe59dSMarcel Moolenaar 		mbr[1] = mbr[3] = 0;
322369fe59dSMarcel Moolenaar 		mbr[2] = 2;
323369fe59dSMarcel Moolenaar 	} else
324369fe59dSMarcel Moolenaar 		mbr[1] = mbr[2] = mbr[3] = 0xff;
325369fe59dSMarcel Moolenaar 	mbr[4] = typ;
326369fe59dSMarcel Moolenaar 	mbr[5] = mbr[6] = mbr[7] = 0xff;
327369fe59dSMarcel Moolenaar 	le32enc(mbr + 8, (uint32_t)start);
328369fe59dSMarcel Moolenaar 	le32enc(mbr + 12, (uint32_t)(end - start + 1));
329369fe59dSMarcel Moolenaar 	return (0);
330369fe59dSMarcel Moolenaar }
331369fe59dSMarcel Moolenaar 
332369fe59dSMarcel Moolenaar static int
333369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t)
334369fe59dSMarcel Moolenaar {
335369fe59dSMarcel Moolenaar 	struct g_part_uuid_alias *uap;
336369fe59dSMarcel Moolenaar 
337369fe59dSMarcel Moolenaar 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
338369fe59dSMarcel Moolenaar 		if (EQUUID(t, uap->uuid))
339369fe59dSMarcel Moolenaar 			return (uap->mbrtype);
340369fe59dSMarcel Moolenaar 	}
341369fe59dSMarcel Moolenaar 	return (0);
342369fe59dSMarcel Moolenaar }
343369fe59dSMarcel Moolenaar 
3443bd22a9cSMarcel Moolenaar static void
3453bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
3463bd22a9cSMarcel Moolenaar {
3473bd22a9cSMarcel Moolenaar 
3483bd22a9cSMarcel Moolenaar 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
3493bd22a9cSMarcel Moolenaar 	gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
3503bd22a9cSMarcel Moolenaar 	    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
3513bd22a9cSMarcel Moolenaar 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
3523bd22a9cSMarcel Moolenaar }
3533bd22a9cSMarcel Moolenaar 
354369fe59dSMarcel Moolenaar /*
355369fe59dSMarcel Moolenaar  * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
356369fe59dSMarcel Moolenaar  * whole disk anymore. Rather, it covers the GPT table and the EFI
357369fe59dSMarcel Moolenaar  * system partition only. This way the HFS+ partition and any FAT
358369fe59dSMarcel Moolenaar  * partitions can be added to the MBR without creating an overlap.
359369fe59dSMarcel Moolenaar  */
360369fe59dSMarcel Moolenaar static int
361369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
362369fe59dSMarcel Moolenaar {
363369fe59dSMarcel Moolenaar 	uint8_t *p;
364369fe59dSMarcel Moolenaar 
365369fe59dSMarcel Moolenaar 	p = table->mbr + DOSPARTOFF;
366369fe59dSMarcel Moolenaar 	if (p[4] != 0xee || le32dec(p + 8) != 1)
367369fe59dSMarcel Moolenaar 		return (0);
368369fe59dSMarcel Moolenaar 
369369fe59dSMarcel Moolenaar 	p += DOSPARTSIZE;
370369fe59dSMarcel Moolenaar 	if (p[4] != 0xaf)
371369fe59dSMarcel Moolenaar 		return (0);
372369fe59dSMarcel Moolenaar 
373369fe59dSMarcel Moolenaar 	printf("GEOM: %s: enabling Boot Camp\n", provname);
374369fe59dSMarcel Moolenaar 	return (1);
375369fe59dSMarcel Moolenaar }
376369fe59dSMarcel Moolenaar 
377369fe59dSMarcel Moolenaar static void
3783bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
379369fe59dSMarcel Moolenaar {
380369fe59dSMarcel Moolenaar 	struct g_part_entry *baseentry;
381369fe59dSMarcel Moolenaar 	struct g_part_gpt_entry *entry;
382369fe59dSMarcel Moolenaar 	struct g_part_gpt_table *table;
383369fe59dSMarcel Moolenaar 	int bootable, error, index, slices, typ;
384369fe59dSMarcel Moolenaar 
385369fe59dSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
386369fe59dSMarcel Moolenaar 
387369fe59dSMarcel Moolenaar 	bootable = -1;
388369fe59dSMarcel Moolenaar 	for (index = 0; index < NDOSPART; index++) {
389369fe59dSMarcel Moolenaar 		if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
390369fe59dSMarcel Moolenaar 			bootable = index;
391369fe59dSMarcel Moolenaar 	}
392369fe59dSMarcel Moolenaar 
393369fe59dSMarcel Moolenaar 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
394369fe59dSMarcel Moolenaar 	slices = 0;
395369fe59dSMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
396369fe59dSMarcel Moolenaar 		if (baseentry->gpe_deleted)
397369fe59dSMarcel Moolenaar 			continue;
398369fe59dSMarcel Moolenaar 		index = baseentry->gpe_index - 1;
399369fe59dSMarcel Moolenaar 		if (index >= NDOSPART)
400369fe59dSMarcel Moolenaar 			continue;
401369fe59dSMarcel Moolenaar 
402369fe59dSMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
403369fe59dSMarcel Moolenaar 
404369fe59dSMarcel Moolenaar 		switch (index) {
405369fe59dSMarcel Moolenaar 		case 0:	/* This must be the EFI system partition. */
406369fe59dSMarcel Moolenaar 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
407369fe59dSMarcel Moolenaar 				goto disable;
408369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, 0xee,
409369fe59dSMarcel Moolenaar 			    1ull, entry->ent.ent_lba_end);
410369fe59dSMarcel Moolenaar 			break;
411369fe59dSMarcel Moolenaar 		case 1:	/* This must be the HFS+ partition. */
412369fe59dSMarcel Moolenaar 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
413369fe59dSMarcel Moolenaar 				goto disable;
414369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
415369fe59dSMarcel Moolenaar 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
416369fe59dSMarcel Moolenaar 			break;
417369fe59dSMarcel Moolenaar 		default:
418369fe59dSMarcel Moolenaar 			typ = gpt_map_type(&entry->ent.ent_type);
419369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, typ,
420369fe59dSMarcel Moolenaar 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
421369fe59dSMarcel Moolenaar 			break;
422369fe59dSMarcel Moolenaar 		}
423369fe59dSMarcel Moolenaar 		if (error)
424369fe59dSMarcel Moolenaar 			continue;
425369fe59dSMarcel Moolenaar 
426369fe59dSMarcel Moolenaar 		if (index == bootable)
427369fe59dSMarcel Moolenaar 			table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
428369fe59dSMarcel Moolenaar 		slices |= 1 << index;
429369fe59dSMarcel Moolenaar 	}
430369fe59dSMarcel Moolenaar 	if ((slices & 3) == 3)
431369fe59dSMarcel Moolenaar 		return;
432369fe59dSMarcel Moolenaar 
433369fe59dSMarcel Moolenaar  disable:
434369fe59dSMarcel Moolenaar 	table->bootcamp = 0;
4353bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
436369fe59dSMarcel Moolenaar }
437369fe59dSMarcel Moolenaar 
4381c2dee3cSRobert Noland static struct gpt_hdr *
4391d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
4401c2dee3cSRobert Noland     enum gpt_elt elt)
4411d3aed33SMarcel Moolenaar {
4421c2dee3cSRobert Noland 	struct gpt_hdr *buf, *hdr;
4431d3aed33SMarcel Moolenaar 	struct g_provider *pp;
4441d3aed33SMarcel Moolenaar 	quad_t lba, last;
4451d3aed33SMarcel Moolenaar 	int error;
4461d3aed33SMarcel Moolenaar 	uint32_t crc, sz;
4471d3aed33SMarcel Moolenaar 
4481d3aed33SMarcel Moolenaar 	pp = cp->provider;
4491d3aed33SMarcel Moolenaar 	last = (pp->mediasize / pp->sectorsize) - 1;
4501d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_MISSING;
451e7926a37SAndrey V. Elsukov 	/*
452e7926a37SAndrey V. Elsukov 	 * If the primary header is valid look for secondary
453e7926a37SAndrey V. Elsukov 	 * header in AlternateLBA, otherwise in the last medium's LBA.
454e7926a37SAndrey V. Elsukov 	 */
455e7926a37SAndrey V. Elsukov 	if (elt == GPT_ELT_SECHDR) {
456e7926a37SAndrey V. Elsukov 		if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
457e7926a37SAndrey V. Elsukov 			table->lba[elt] = last;
458e7926a37SAndrey V. Elsukov 	} else
459e7926a37SAndrey V. Elsukov 		table->lba[elt] = 1;
4601d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
4611d3aed33SMarcel Moolenaar 	    &error);
4621d3aed33SMarcel Moolenaar 	if (buf == NULL)
4631c2dee3cSRobert Noland 		return (NULL);
4641c2dee3cSRobert Noland 	hdr = NULL;
4651c2dee3cSRobert Noland 	if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
4661c2dee3cSRobert Noland 		goto fail;
4671d3aed33SMarcel Moolenaar 
4681d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_CORRUPT;
4691c2dee3cSRobert Noland 	sz = le32toh(buf->hdr_size);
4701d3aed33SMarcel Moolenaar 	if (sz < 92 || sz > pp->sectorsize)
4711c2dee3cSRobert Noland 		goto fail;
4721c2dee3cSRobert Noland 
4731c2dee3cSRobert Noland 	hdr = g_malloc(sz, M_WAITOK | M_ZERO);
4741c2dee3cSRobert Noland 	bcopy(buf, hdr, sz);
4751d3aed33SMarcel Moolenaar 	hdr->hdr_size = sz;
4761c2dee3cSRobert Noland 
4771c2dee3cSRobert Noland 	crc = le32toh(buf->hdr_crc_self);
4781c2dee3cSRobert Noland 	buf->hdr_crc_self = 0;
4791c2dee3cSRobert Noland 	if (crc32(buf, sz) != crc)
4801c2dee3cSRobert Noland 		goto fail;
4811d3aed33SMarcel Moolenaar 	hdr->hdr_crc_self = crc;
4821d3aed33SMarcel Moolenaar 
4831d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_INVALID;
4841c2dee3cSRobert Noland 	hdr->hdr_revision = le32toh(buf->hdr_revision);
485e7926a37SAndrey V. Elsukov 	if (hdr->hdr_revision < GPT_HDR_REVISION)
4861c2dee3cSRobert Noland 		goto fail;
4871c2dee3cSRobert Noland 	hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
4881d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_self != table->lba[elt])
4891c2dee3cSRobert Noland 		goto fail;
4901c2dee3cSRobert Noland 	hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
491cea05ed9SEugene Grosbein 	if (hdr->hdr_lba_alt == hdr->hdr_lba_self)
492cea05ed9SEugene Grosbein 		goto fail;
493cea05ed9SEugene Grosbein 	if (hdr->hdr_lba_alt > last && geom_part_check_integrity)
494e7926a37SAndrey V. Elsukov 		goto fail;
4951d3aed33SMarcel Moolenaar 
4961d3aed33SMarcel Moolenaar 	/* Check the managed area. */
4971c2dee3cSRobert Noland 	hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
4981d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
4991c2dee3cSRobert Noland 		goto fail;
5001c2dee3cSRobert Noland 	hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
5011d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
5021c2dee3cSRobert Noland 		goto fail;
5031d3aed33SMarcel Moolenaar 
5041d3aed33SMarcel Moolenaar 	/* Check the table location and size of the table. */
5051c2dee3cSRobert Noland 	hdr->hdr_entries = le32toh(buf->hdr_entries);
5061c2dee3cSRobert Noland 	hdr->hdr_entsz = le32toh(buf->hdr_entsz);
5071d3aed33SMarcel Moolenaar 	if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
5081d3aed33SMarcel Moolenaar 	    (hdr->hdr_entsz & 7) != 0)
5091c2dee3cSRobert Noland 		goto fail;
5101c2dee3cSRobert Noland 	hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
5111d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
5121c2dee3cSRobert Noland 		goto fail;
5131d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
5141d3aed33SMarcel Moolenaar 	    hdr->hdr_lba_table <= hdr->hdr_lba_end)
5151c2dee3cSRobert Noland 		goto fail;
5161d3aed33SMarcel Moolenaar 	lba = hdr->hdr_lba_table +
5172e543af1SZhenlei Huang 	    howmany((uint64_t)hdr->hdr_entries * hdr->hdr_entsz,
5182e543af1SZhenlei Huang 	        pp->sectorsize) - 1;
5191d3aed33SMarcel Moolenaar 	if (lba >= last)
5201c2dee3cSRobert Noland 		goto fail;
5211d3aed33SMarcel Moolenaar 	if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
5221c2dee3cSRobert Noland 		goto fail;
5231d3aed33SMarcel Moolenaar 
5241d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_OK;
5251c2dee3cSRobert Noland 	le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
5261c2dee3cSRobert Noland 	hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
5271c2dee3cSRobert Noland 
528e7926a37SAndrey V. Elsukov 	/* save LBA for secondary header */
529e7926a37SAndrey V. Elsukov 	if (elt == GPT_ELT_PRIHDR)
530e7926a37SAndrey V. Elsukov 		table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
531e7926a37SAndrey V. Elsukov 
5321c2dee3cSRobert Noland 	g_free(buf);
5331c2dee3cSRobert Noland 	return (hdr);
5341c2dee3cSRobert Noland 
5351c2dee3cSRobert Noland  fail:
5361c2dee3cSRobert Noland 	g_free(hdr);
5371c2dee3cSRobert Noland 	g_free(buf);
5381c2dee3cSRobert Noland 	return (NULL);
5391d3aed33SMarcel Moolenaar }
5401d3aed33SMarcel Moolenaar 
5411d3aed33SMarcel Moolenaar static struct gpt_ent *
5421d3aed33SMarcel Moolenaar gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
5431d3aed33SMarcel Moolenaar     enum gpt_elt elt, struct gpt_hdr *hdr)
5441d3aed33SMarcel Moolenaar {
5451d3aed33SMarcel Moolenaar 	struct g_provider *pp;
5461d3aed33SMarcel Moolenaar 	struct gpt_ent *ent, *tbl;
5471d3aed33SMarcel Moolenaar 	char *buf, *p;
548799eac8cSAndrey V. Elsukov 	unsigned int idx, sectors, tblsz, size;
5491d3aed33SMarcel Moolenaar 	int error;
5501d3aed33SMarcel Moolenaar 
5511c2dee3cSRobert Noland 	if (hdr == NULL)
5521c2dee3cSRobert Noland 		return (NULL);
5535be5d0d5SZhenlei Huang 	if (hdr->hdr_entries > g_part_gpt_scheme.gps_maxent ||
5545be5d0d5SZhenlei Huang 	    hdr->hdr_entsz > MAXENTSIZE) {
5555be5d0d5SZhenlei Huang 		table->state[elt] = GPT_STATE_UNSUPPORTED;
5565be5d0d5SZhenlei Huang 		return (NULL);
5575be5d0d5SZhenlei Huang 	}
5581c2dee3cSRobert Noland 
5591d3aed33SMarcel Moolenaar 	pp = cp->provider;
5601d3aed33SMarcel Moolenaar 	table->lba[elt] = hdr->hdr_lba_table;
5611d3aed33SMarcel Moolenaar 
5621d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_MISSING;
5631d3aed33SMarcel Moolenaar 	tblsz = hdr->hdr_entries * hdr->hdr_entsz;
56455e0987aSPedro F. Giffuni 	sectors = howmany(tblsz, pp->sectorsize);
565799eac8cSAndrey V. Elsukov 	buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
566cd853791SKonstantin Belousov 	for (idx = 0; idx < sectors; idx += maxphys / pp->sectorsize) {
567cd853791SKonstantin Belousov 		size = (sectors - idx > maxphys / pp->sectorsize) ?  maxphys:
568799eac8cSAndrey V. Elsukov 		    (sectors - idx) * pp->sectorsize;
569799eac8cSAndrey V. Elsukov 		p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
570799eac8cSAndrey V. Elsukov 		    size, &error);
571799eac8cSAndrey V. Elsukov 		if (p == NULL) {
572799eac8cSAndrey V. Elsukov 			g_free(buf);
5731d3aed33SMarcel Moolenaar 			return (NULL);
574799eac8cSAndrey V. Elsukov 		}
575799eac8cSAndrey V. Elsukov 		bcopy(p, buf + idx * pp->sectorsize, size);
576799eac8cSAndrey V. Elsukov 		g_free(p);
577799eac8cSAndrey V. Elsukov 	}
5781d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_CORRUPT;
5791d3aed33SMarcel Moolenaar 	if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
5801d3aed33SMarcel Moolenaar 		g_free(buf);
5811d3aed33SMarcel Moolenaar 		return (NULL);
5821d3aed33SMarcel Moolenaar 	}
5831d3aed33SMarcel Moolenaar 
5841d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_OK;
5851d3aed33SMarcel Moolenaar 	tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
5861d3aed33SMarcel Moolenaar 	    M_WAITOK | M_ZERO);
5871d3aed33SMarcel Moolenaar 
5881d3aed33SMarcel Moolenaar 	for (idx = 0, ent = tbl, p = buf;
5891d3aed33SMarcel Moolenaar 	     idx < hdr->hdr_entries;
5901d3aed33SMarcel Moolenaar 	     idx++, ent++, p += hdr->hdr_entsz) {
5911d3aed33SMarcel Moolenaar 		le_uuid_dec(p, &ent->ent_type);
5921d3aed33SMarcel Moolenaar 		le_uuid_dec(p + 16, &ent->ent_uuid);
5931d3aed33SMarcel Moolenaar 		ent->ent_lba_start = le64dec(p + 32);
5941d3aed33SMarcel Moolenaar 		ent->ent_lba_end = le64dec(p + 40);
5951d3aed33SMarcel Moolenaar 		ent->ent_attr = le64dec(p + 48);
596d3532631SMarcel Moolenaar 		/* Keep UTF-16 in little-endian. */
597d3532631SMarcel Moolenaar 		bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
5981d3aed33SMarcel Moolenaar 	}
5991d3aed33SMarcel Moolenaar 
6001d3aed33SMarcel Moolenaar 	g_free(buf);
6011d3aed33SMarcel Moolenaar 	return (tbl);
6021d3aed33SMarcel Moolenaar }
6031d3aed33SMarcel Moolenaar 
6041d3aed33SMarcel Moolenaar static int
6051d3aed33SMarcel Moolenaar gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
6061d3aed33SMarcel Moolenaar {
6071d3aed33SMarcel Moolenaar 
6081c2dee3cSRobert Noland 	if (pri == NULL || sec == NULL)
6091c2dee3cSRobert Noland 		return (0);
6101c2dee3cSRobert Noland 
6111d3aed33SMarcel Moolenaar 	if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
6121d3aed33SMarcel Moolenaar 		return (0);
6131d3aed33SMarcel Moolenaar 	return ((pri->hdr_revision == sec->hdr_revision &&
6141d3aed33SMarcel Moolenaar 	    pri->hdr_size == sec->hdr_size &&
6151d3aed33SMarcel Moolenaar 	    pri->hdr_lba_start == sec->hdr_lba_start &&
6161d3aed33SMarcel Moolenaar 	    pri->hdr_lba_end == sec->hdr_lba_end &&
6171d3aed33SMarcel Moolenaar 	    pri->hdr_entries == sec->hdr_entries &&
6181d3aed33SMarcel Moolenaar 	    pri->hdr_entsz == sec->hdr_entsz &&
6191d3aed33SMarcel Moolenaar 	    pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
6201d3aed33SMarcel Moolenaar }
6211d3aed33SMarcel Moolenaar 
6221d3aed33SMarcel Moolenaar static int
6231d3aed33SMarcel Moolenaar gpt_parse_type(const char *type, struct uuid *uuid)
6241d3aed33SMarcel Moolenaar {
6251d3aed33SMarcel Moolenaar 	struct uuid tmp;
626d287f590SMarcel Moolenaar 	const char *alias;
6271d3aed33SMarcel Moolenaar 	int error;
628f1317430SRui Paulo 	struct g_part_uuid_alias *uap;
6291d3aed33SMarcel Moolenaar 
630d287f590SMarcel Moolenaar 	if (type[0] == '!') {
631d287f590SMarcel Moolenaar 		error = parse_uuid(type + 1, &tmp);
6321d3aed33SMarcel Moolenaar 		if (error)
6331d3aed33SMarcel Moolenaar 			return (error);
6341d3aed33SMarcel Moolenaar 		if (EQUUID(&tmp, &gpt_uuid_unused))
6351d3aed33SMarcel Moolenaar 			return (EINVAL);
6361d3aed33SMarcel Moolenaar 		*uuid = tmp;
6371d3aed33SMarcel Moolenaar 		return (0);
6381d3aed33SMarcel Moolenaar 	}
639f1317430SRui Paulo 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
640f1317430SRui Paulo 		alias = g_part_alias_name(uap->alias);
641d287f590SMarcel Moolenaar 		if (!strcasecmp(type, alias)) {
642665bb830SMarcel Moolenaar 			*uuid = *uap->uuid;
6431d3aed33SMarcel Moolenaar 			return (0);
6441d3aed33SMarcel Moolenaar 		}
645d7255ff4SRui Paulo 	}
646d287f590SMarcel Moolenaar 	return (EINVAL);
647d287f590SMarcel Moolenaar }
6481d3aed33SMarcel Moolenaar 
6491d3aed33SMarcel Moolenaar static int
6501d3aed33SMarcel Moolenaar g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
6511d3aed33SMarcel Moolenaar     struct g_part_parms *gpp)
6521d3aed33SMarcel Moolenaar {
6531d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
6541d3aed33SMarcel Moolenaar 	int error;
6551d3aed33SMarcel Moolenaar 
6561d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
6571d3aed33SMarcel Moolenaar 	error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
6581d3aed33SMarcel Moolenaar 	if (error)
6591d3aed33SMarcel Moolenaar 		return (error);
6601d3aed33SMarcel Moolenaar 	kern_uuidgen(&entry->ent.ent_uuid, 1);
6611d3aed33SMarcel Moolenaar 	entry->ent.ent_lba_start = baseentry->gpe_start;
6621d3aed33SMarcel Moolenaar 	entry->ent.ent_lba_end = baseentry->gpe_end;
6631d3aed33SMarcel Moolenaar 	if (baseentry->gpe_deleted) {
6641d3aed33SMarcel Moolenaar 		entry->ent.ent_attr = 0;
6651d3aed33SMarcel Moolenaar 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
6661d3aed33SMarcel Moolenaar 	}
667d3532631SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
668d3532631SMarcel Moolenaar 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
669ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name) /
670ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name[0]));
6711d3aed33SMarcel Moolenaar 	return (0);
6721d3aed33SMarcel Moolenaar }
6731d3aed33SMarcel Moolenaar 
6741d3aed33SMarcel Moolenaar static int
6754d32fcb4SMarcel Moolenaar g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
6764d32fcb4SMarcel Moolenaar {
6774d32fcb4SMarcel Moolenaar 	struct g_part_gpt_table *table;
67866477112SMarcel Moolenaar 	size_t codesz;
6794d32fcb4SMarcel Moolenaar 
68066477112SMarcel Moolenaar 	codesz = DOSPARTOFF;
6814d32fcb4SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
68266477112SMarcel Moolenaar 	bzero(table->mbr, codesz);
68366477112SMarcel Moolenaar 	codesz = MIN(codesz, gpp->gpp_codesize);
68466477112SMarcel Moolenaar 	if (codesz > 0)
68566477112SMarcel Moolenaar 		bcopy(gpp->gpp_codeptr, table->mbr, codesz);
6864d32fcb4SMarcel Moolenaar 	return (0);
6874d32fcb4SMarcel Moolenaar }
6884d32fcb4SMarcel Moolenaar 
6894d32fcb4SMarcel Moolenaar static int
6901d3aed33SMarcel Moolenaar g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
6911d3aed33SMarcel Moolenaar {
6921d3aed33SMarcel Moolenaar 	struct g_provider *pp;
6931d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
6941d3aed33SMarcel Moolenaar 	size_t tblsz;
6951d3aed33SMarcel Moolenaar 
696ef03f57dSKyle Evans 	/* Our depth should be 0 unless nesting was explicitly enabled. */
697ef03f57dSKyle Evans 	if (!allow_nesting && basetable->gpt_depth != 0)
6982a1c00ffSMarcel Moolenaar 		return (ENXIO);
6992a1c00ffSMarcel Moolenaar 
7001d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
7011d3aed33SMarcel Moolenaar 	pp = gpp->gpp_provider;
70255e0987aSPedro F. Giffuni 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
70355e0987aSPedro F. Giffuni 	    pp->sectorsize);
7044d32fcb4SMarcel Moolenaar 	if (pp->sectorsize < MBRSIZE ||
7051d3aed33SMarcel Moolenaar 	    pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
7061d3aed33SMarcel Moolenaar 	    pp->sectorsize)
7071d3aed33SMarcel Moolenaar 		return (ENOSPC);
7081d3aed33SMarcel Moolenaar 
7093bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
7103bd22a9cSMarcel Moolenaar 
711a59a1310SRobert Noland 	/* Allocate space for the header */
712a59a1310SRobert Noland 	table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
713a59a1310SRobert Noland 
7141c2dee3cSRobert Noland 	bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
7151c2dee3cSRobert Noland 	table->hdr->hdr_revision = GPT_HDR_REVISION;
7161c2dee3cSRobert Noland 	table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
7171c2dee3cSRobert Noland 	kern_uuidgen(&table->hdr->hdr_uuid, 1);
7181c2dee3cSRobert Noland 	table->hdr->hdr_entries = basetable->gpt_entries;
7191c2dee3cSRobert Noland 	table->hdr->hdr_entsz = sizeof(struct gpt_ent);
7201d3aed33SMarcel Moolenaar 
721*5c9f0f72SWarner Losh 	g_gpt_set_defaults(basetable, pp, gpp);
7221d3aed33SMarcel Moolenaar 	return (0);
7231d3aed33SMarcel Moolenaar }
7241d3aed33SMarcel Moolenaar 
7251d3aed33SMarcel Moolenaar static int
7261d3aed33SMarcel Moolenaar g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
7271d3aed33SMarcel Moolenaar {
72836066952SMarius Strobl 	struct g_part_gpt_table *table;
729e7926a37SAndrey V. Elsukov 	struct g_provider *pp;
73036066952SMarius Strobl 
73136066952SMarius Strobl 	table = (struct g_part_gpt_table *)basetable;
732e7926a37SAndrey V. Elsukov 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
73336066952SMarius Strobl 	g_free(table->hdr);
73436066952SMarius Strobl 	table->hdr = NULL;
7351d3aed33SMarcel Moolenaar 
7361d3aed33SMarcel Moolenaar 	/*
7379ef60043SAndrey V. Elsukov 	 * Wipe the first 2 sectors and last one to clear the partitioning.
7389ef60043SAndrey V. Elsukov 	 * Wipe sectors only if they have valid metadata.
7391d3aed33SMarcel Moolenaar 	 */
7409ef60043SAndrey V. Elsukov 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK)
7411d3aed33SMarcel Moolenaar 		basetable->gpt_smhead |= 3;
742e7926a37SAndrey V. Elsukov 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
743e7926a37SAndrey V. Elsukov 	    table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
7441d3aed33SMarcel Moolenaar 		basetable->gpt_smtail |= 1;
7451d3aed33SMarcel Moolenaar 	return (0);
7461d3aed33SMarcel Moolenaar }
7471d3aed33SMarcel Moolenaar 
74851f53a08SWarner Losh static void
749e3ab141fSWarner Losh g_part_gpt_efimedia(struct g_part_gpt_entry *entry, struct sbuf *sb)
750e3ab141fSWarner Losh {
751e3ab141fSWarner Losh 	sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index);
752e3ab141fSWarner Losh 	sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
753e3ab141fSWarner Losh 	sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start,
754e3ab141fSWarner Losh 	    (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
755e3ab141fSWarner Losh }
756e3ab141fSWarner Losh 
757e3ab141fSWarner Losh static void
7585db67052SMarcel Moolenaar g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
7595db67052SMarcel Moolenaar     struct sbuf *sb, const char *indent)
7605db67052SMarcel Moolenaar {
7615db67052SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
7625db67052SMarcel Moolenaar 
7635db67052SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
7640c132595SMarcel Moolenaar 	if (indent == NULL) {
7650c132595SMarcel Moolenaar 		/* conftxt: libdisk compatibility */
76649ee0fceSAlexander Motin 		sbuf_cat(sb, " xs GPT xt ");
7675db67052SMarcel Moolenaar 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
7680c132595SMarcel Moolenaar 	} else if (entry != NULL) {
7690c132595SMarcel Moolenaar 		/* confxml: partition entry information */
770d3532631SMarcel Moolenaar 		sbuf_printf(sb, "%s<label>", indent);
771d3532631SMarcel Moolenaar 		g_gpt_printf_utf16(sb, entry->ent.ent_name,
772d3532631SMarcel Moolenaar 		    sizeof(entry->ent.ent_name) >> 1);
77349ee0fceSAlexander Motin 		sbuf_cat(sb, "</label>\n");
774cec283baSPawel Jakub Dawidek 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
775cec283baSPawel Jakub Dawidek 			sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
776cec283baSPawel Jakub Dawidek 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
777cec283baSPawel Jakub Dawidek 			sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
778cec283baSPawel Jakub Dawidek 			    indent);
779cec283baSPawel Jakub Dawidek 		}
780cec283baSPawel Jakub Dawidek 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
781cec283baSPawel Jakub Dawidek 			sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
782cec283baSPawel Jakub Dawidek 			    indent);
783cec283baSPawel Jakub Dawidek 		}
7840c132595SMarcel Moolenaar 		sbuf_printf(sb, "%s<rawtype>", indent);
7850c132595SMarcel Moolenaar 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
78649ee0fceSAlexander Motin 		sbuf_cat(sb, "</rawtype>\n");
787cd18ad83SMarcel Moolenaar 		sbuf_printf(sb, "%s<rawuuid>", indent);
788cd18ad83SMarcel Moolenaar 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
78949ee0fceSAlexander Motin 		sbuf_cat(sb, "</rawuuid>\n");
790ab4effdcSWarner Losh 		sbuf_printf(sb, "%s<efimedia>", indent);
791e3ab141fSWarner Losh 		g_part_gpt_efimedia(entry, sb);
79249ee0fceSAlexander Motin 		sbuf_cat(sb, "</efimedia>\n");
7930c132595SMarcel Moolenaar 	} else {
7940c132595SMarcel Moolenaar 		/* confxml: scheme information */
7950c132595SMarcel Moolenaar 	}
7965db67052SMarcel Moolenaar }
7975db67052SMarcel Moolenaar 
7985db67052SMarcel Moolenaar static int
7991d3aed33SMarcel Moolenaar g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
8001d3aed33SMarcel Moolenaar {
8011d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8021d3aed33SMarcel Moolenaar 
8031d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
8041d3aed33SMarcel Moolenaar 	return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
8054042ab48SAndrey V. Elsukov 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
8064042ab48SAndrey V. Elsukov 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
8071d3aed33SMarcel Moolenaar }
8081d3aed33SMarcel Moolenaar 
8091d3aed33SMarcel Moolenaar static int
8101d3aed33SMarcel Moolenaar g_part_gpt_modify(struct g_part_table *basetable,
8111d3aed33SMarcel Moolenaar     struct g_part_entry *baseentry, struct g_part_parms *gpp)
8121d3aed33SMarcel Moolenaar {
8131d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8141d3aed33SMarcel Moolenaar 	int error;
8151d3aed33SMarcel Moolenaar 
8161d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
817d287f590SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
8181d3aed33SMarcel Moolenaar 		error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
8191d3aed33SMarcel Moolenaar 		if (error)
8201d3aed33SMarcel Moolenaar 			return (error);
821d287f590SMarcel Moolenaar 	}
822d3532631SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
823d3532631SMarcel Moolenaar 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
824ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name) /
825ed7beddcSMatthew D Fleming 		    sizeof(entry->ent.ent_name[0]));
8261d3aed33SMarcel Moolenaar 	return (0);
8271d3aed33SMarcel Moolenaar }
8281d3aed33SMarcel Moolenaar 
8293f71c319SMarcel Moolenaar static int
8303f71c319SMarcel Moolenaar g_part_gpt_resize(struct g_part_table *basetable,
8313f71c319SMarcel Moolenaar     struct g_part_entry *baseentry, struct g_part_parms *gpp)
8323f71c319SMarcel Moolenaar {
8333f71c319SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8343f71c319SMarcel Moolenaar 
835884c8e4fSAndrey V. Elsukov 	if (baseentry == NULL)
836e7d0c7e4SAndrey V. Elsukov 		return (g_part_gpt_recover(basetable));
837884c8e4fSAndrey V. Elsukov 
838884c8e4fSAndrey V. Elsukov 	entry = (struct g_part_gpt_entry *)baseentry;
8393f71c319SMarcel Moolenaar 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
8403f71c319SMarcel Moolenaar 	entry->ent.ent_lba_end = baseentry->gpe_end;
8413f71c319SMarcel Moolenaar 
8423f71c319SMarcel Moolenaar 	return (0);
8433f71c319SMarcel Moolenaar }
8443f71c319SMarcel Moolenaar 
84551f53a08SWarner Losh static const char *
8461d3aed33SMarcel Moolenaar g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
8471d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
8481d3aed33SMarcel Moolenaar {
8491d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
8501d3aed33SMarcel Moolenaar 	char c;
8511d3aed33SMarcel Moolenaar 
8521d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
8531d3aed33SMarcel Moolenaar 	c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
8541d3aed33SMarcel Moolenaar 	snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
8551d3aed33SMarcel Moolenaar 	return (buf);
8561d3aed33SMarcel Moolenaar }
8571d3aed33SMarcel Moolenaar 
8581d3aed33SMarcel Moolenaar static int
8591d3aed33SMarcel Moolenaar g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
8601d3aed33SMarcel Moolenaar {
8611d3aed33SMarcel Moolenaar 	struct g_provider *pp;
8623e5a0a6bSMarcel Moolenaar 	u_char *buf;
8633e5a0a6bSMarcel Moolenaar 	int error, index, pri, res;
8641d3aed33SMarcel Moolenaar 
865ef03f57dSKyle Evans 	/* Our depth should be 0 unless nesting was explicitly enabled. */
866ef03f57dSKyle Evans 	if (!allow_nesting && table->gpt_depth != 0)
8671d3aed33SMarcel Moolenaar 		return (ENXIO);
8681d3aed33SMarcel Moolenaar 
8691d3aed33SMarcel Moolenaar 	pp = cp->provider;
8701d3aed33SMarcel Moolenaar 
8711d3aed33SMarcel Moolenaar 	/*
8721d3aed33SMarcel Moolenaar 	 * Sanity-check the provider. Since the first sector on the provider
8731d3aed33SMarcel Moolenaar 	 * must be a PMBR and a PMBR is 512 bytes large, the sector size
8741d3aed33SMarcel Moolenaar 	 * must be at least 512 bytes.  Also, since the theoretical minimum
8751d3aed33SMarcel Moolenaar 	 * number of sectors needed by GPT is 6, any medium that has less
8761d3aed33SMarcel Moolenaar 	 * than 6 sectors is never going to be able to hold a GPT. The
8771d3aed33SMarcel Moolenaar 	 * number 6 comes from:
8781d3aed33SMarcel Moolenaar 	 *	1 sector for the PMBR
8791d3aed33SMarcel Moolenaar 	 *	2 sectors for the GPT headers (each 1 sector)
8801d3aed33SMarcel Moolenaar 	 *	2 sectors for the GPT tables (each 1 sector)
8811d3aed33SMarcel Moolenaar 	 *	1 sector for an actual partition
8821d3aed33SMarcel Moolenaar 	 * It's better to catch this pathological case early than behaving
8831d3aed33SMarcel Moolenaar 	 * pathologically later on...
8841d3aed33SMarcel Moolenaar 	 */
8854d32fcb4SMarcel Moolenaar 	if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
8861d3aed33SMarcel Moolenaar 		return (ENOSPC);
8871d3aed33SMarcel Moolenaar 
8883e5a0a6bSMarcel Moolenaar 	/*
8893e5a0a6bSMarcel Moolenaar 	 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
8903e5a0a6bSMarcel Moolenaar 	 * as the highest priority on a match, otherwise we assume some
8913e5a0a6bSMarcel Moolenaar 	 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
8923e5a0a6bSMarcel Moolenaar 	 * we really want the MBR scheme to take precedence.
8933e5a0a6bSMarcel Moolenaar 	 */
8941d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
8951d3aed33SMarcel Moolenaar 	if (buf == NULL)
8961d3aed33SMarcel Moolenaar 		return (error);
8971d3aed33SMarcel Moolenaar 	res = le16dec(buf + DOSMAGICOFFSET);
8983e5a0a6bSMarcel Moolenaar 	pri = G_PART_PROBE_PRI_LOW;
899af90a872SAndrey V. Elsukov 	if (res == DOSMAGIC) {
9003e5a0a6bSMarcel Moolenaar 		for (index = 0; index < NDOSPART; index++) {
9013e5a0a6bSMarcel Moolenaar 			if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
9023e5a0a6bSMarcel Moolenaar 				pri = G_PART_PROBE_PRI_HIGH;
9033e5a0a6bSMarcel Moolenaar 		}
9041d3aed33SMarcel Moolenaar 		g_free(buf);
9051d3aed33SMarcel Moolenaar 
9061d3aed33SMarcel Moolenaar 		/* Check that there's a primary header. */
9071d3aed33SMarcel Moolenaar 		buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
9081d3aed33SMarcel Moolenaar 		if (buf == NULL)
9091d3aed33SMarcel Moolenaar 			return (error);
9101d3aed33SMarcel Moolenaar 		res = memcmp(buf, GPT_HDR_SIG, 8);
9111d3aed33SMarcel Moolenaar 		g_free(buf);
9121d3aed33SMarcel Moolenaar 		if (res == 0)
9133e5a0a6bSMarcel Moolenaar 			return (pri);
914af90a872SAndrey V. Elsukov 	} else
915af90a872SAndrey V. Elsukov 		g_free(buf);
9161d3aed33SMarcel Moolenaar 
9171d3aed33SMarcel Moolenaar 	/* No primary? Check that there's a secondary. */
9181d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
9191d3aed33SMarcel Moolenaar 	    &error);
9201d3aed33SMarcel Moolenaar 	if (buf == NULL)
9211d3aed33SMarcel Moolenaar 		return (error);
9221d3aed33SMarcel Moolenaar 	res = memcmp(buf, GPT_HDR_SIG, 8);
9231d3aed33SMarcel Moolenaar 	g_free(buf);
9243e5a0a6bSMarcel Moolenaar 	return ((res == 0) ? pri : ENXIO);
9251d3aed33SMarcel Moolenaar }
9261d3aed33SMarcel Moolenaar 
9271d3aed33SMarcel Moolenaar static int
9281d3aed33SMarcel Moolenaar g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
9291d3aed33SMarcel Moolenaar {
9301c2dee3cSRobert Noland 	struct gpt_hdr *prihdr, *sechdr;
9311d3aed33SMarcel Moolenaar 	struct gpt_ent *tbl, *pritbl, *sectbl;
9321d3aed33SMarcel Moolenaar 	struct g_provider *pp;
9331d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
9341d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
9354d32fcb4SMarcel Moolenaar 	u_char *buf;
936e7926a37SAndrey V. Elsukov 	uint64_t last;
9374d32fcb4SMarcel Moolenaar 	int error, index;
9381d3aed33SMarcel Moolenaar 
9391d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
9401d3aed33SMarcel Moolenaar 	pp = cp->provider;
941e7926a37SAndrey V. Elsukov 	last = (pp->mediasize / pp->sectorsize) - 1;
9421d3aed33SMarcel Moolenaar 
9434d32fcb4SMarcel Moolenaar 	/* Read the PMBR */
9444d32fcb4SMarcel Moolenaar 	buf = g_read_data(cp, 0, pp->sectorsize, &error);
9454d32fcb4SMarcel Moolenaar 	if (buf == NULL)
9464d32fcb4SMarcel Moolenaar 		return (error);
9474d32fcb4SMarcel Moolenaar 	bcopy(buf, table->mbr, MBRSIZE);
9484d32fcb4SMarcel Moolenaar 	g_free(buf);
9494d32fcb4SMarcel Moolenaar 
9501d3aed33SMarcel Moolenaar 	/* Read the primary header and table. */
9511c2dee3cSRobert Noland 	prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
9521d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
9531c2dee3cSRobert Noland 		pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
9541d3aed33SMarcel Moolenaar 	} else {
9551d3aed33SMarcel Moolenaar 		table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
9561d3aed33SMarcel Moolenaar 		pritbl = NULL;
9571d3aed33SMarcel Moolenaar 	}
9581d3aed33SMarcel Moolenaar 
9591d3aed33SMarcel Moolenaar 	/* Read the secondary header and table. */
9601c2dee3cSRobert Noland 	sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
9611d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
9621c2dee3cSRobert Noland 		sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
9631d3aed33SMarcel Moolenaar 	} else {
9641d3aed33SMarcel Moolenaar 		table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
9651d3aed33SMarcel Moolenaar 		sectbl = NULL;
9661d3aed33SMarcel Moolenaar 	}
9671d3aed33SMarcel Moolenaar 
9681d3aed33SMarcel Moolenaar 	/* Fail if we haven't got any good tables at all. */
9691d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
9701d3aed33SMarcel Moolenaar 	    table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
9715be5d0d5SZhenlei Huang 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_UNSUPPORTED &&
9725be5d0d5SZhenlei Huang 		    table->state[GPT_ELT_SECTBL] == GPT_STATE_UNSUPPORTED &&
9735be5d0d5SZhenlei Huang 		    gpt_matched_hdrs(prihdr, sechdr)) {
9745be5d0d5SZhenlei Huang 			printf("GEOM: %s: unsupported GPT detected.\n",
9755be5d0d5SZhenlei Huang 			    pp->name);
9765be5d0d5SZhenlei Huang 			printf(
9775be5d0d5SZhenlei Huang 		    "GEOM: %s: number of GPT entries: %u, entry size: %uB.\n",
9785be5d0d5SZhenlei Huang 			    pp->name, prihdr->hdr_entries, prihdr->hdr_entsz);
9795be5d0d5SZhenlei Huang 			printf(
9805be5d0d5SZhenlei Huang     "GEOM: %s: maximum supported number of GPT entries: %u, entry size: %uB.\n",
9815be5d0d5SZhenlei Huang 			    pp->name, g_part_gpt_scheme.gps_maxent, MAXENTSIZE);
9825be5d0d5SZhenlei Huang 			printf("GEOM: %s: GPT rejected.\n", pp->name);
9835be5d0d5SZhenlei Huang 		} else {
9841d3aed33SMarcel Moolenaar 			printf("GEOM: %s: corrupt or invalid GPT detected.\n",
9851d3aed33SMarcel Moolenaar 			    pp->name);
9865be5d0d5SZhenlei Huang 			printf(
9875be5d0d5SZhenlei Huang 		    "GEOM: %s: GPT rejected -- may not be recoverable.\n",
9881d3aed33SMarcel Moolenaar 			    pp->name);
9895be5d0d5SZhenlei Huang 		}
990ee4d316fSConrad Meyer 		g_free(prihdr);
991ee4d316fSConrad Meyer 		g_free(pritbl);
992ee4d316fSConrad Meyer 		g_free(sechdr);
993ee4d316fSConrad Meyer 		g_free(sectbl);
9941d3aed33SMarcel Moolenaar 		return (EINVAL);
9951d3aed33SMarcel Moolenaar 	}
9961d3aed33SMarcel Moolenaar 
9971d3aed33SMarcel Moolenaar 	/*
9981d3aed33SMarcel Moolenaar 	 * If both headers are good but they disagree with each other,
9991d3aed33SMarcel Moolenaar 	 * then invalidate one. We prefer to keep the primary header,
10001d3aed33SMarcel Moolenaar 	 * unless the primary table is corrupt.
10011d3aed33SMarcel Moolenaar 	 */
10021d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
10031d3aed33SMarcel Moolenaar 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
10041c2dee3cSRobert Noland 	    !gpt_matched_hdrs(prihdr, sechdr)) {
1005dd0db05dSMarcel Moolenaar 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
10061d3aed33SMarcel Moolenaar 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
1007dd0db05dSMarcel Moolenaar 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
10081c2dee3cSRobert Noland 			g_free(sechdr);
10091c2dee3cSRobert Noland 			sechdr = NULL;
1010dd0db05dSMarcel Moolenaar 		} else {
10111d3aed33SMarcel Moolenaar 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
1012dd0db05dSMarcel Moolenaar 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
10131c2dee3cSRobert Noland 			g_free(prihdr);
10141c2dee3cSRobert Noland 			prihdr = NULL;
1015dd0db05dSMarcel Moolenaar 		}
10161d3aed33SMarcel Moolenaar 	}
10171d3aed33SMarcel Moolenaar 
1018dd0db05dSMarcel Moolenaar 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
10191d3aed33SMarcel Moolenaar 		printf("GEOM: %s: the primary GPT table is corrupt or "
10201d3aed33SMarcel Moolenaar 		    "invalid.\n", pp->name);
10211d3aed33SMarcel Moolenaar 		printf("GEOM: %s: using the secondary instead -- recovery "
10221d3aed33SMarcel Moolenaar 		    "strongly advised.\n", pp->name);
10231d3aed33SMarcel Moolenaar 		table->hdr = sechdr;
1024e7926a37SAndrey V. Elsukov 		basetable->gpt_corrupt = 1;
10251c2dee3cSRobert Noland 		g_free(prihdr);
10261d3aed33SMarcel Moolenaar 		tbl = sectbl;
10271d3aed33SMarcel Moolenaar 		g_free(pritbl);
10281d3aed33SMarcel Moolenaar 	} else {
1029dd0db05dSMarcel Moolenaar 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
10301d3aed33SMarcel Moolenaar 			printf("GEOM: %s: the secondary GPT table is corrupt "
10311d3aed33SMarcel Moolenaar 			    "or invalid.\n", pp->name);
10321d3aed33SMarcel Moolenaar 			printf("GEOM: %s: using the primary only -- recovery "
10331d3aed33SMarcel Moolenaar 			    "suggested.\n", pp->name);
1034e7926a37SAndrey V. Elsukov 			basetable->gpt_corrupt = 1;
1035e7926a37SAndrey V. Elsukov 		} else if (table->lba[GPT_ELT_SECHDR] != last) {
1036e7926a37SAndrey V. Elsukov 			printf( "GEOM: %s: the secondary GPT header is not in "
1037e7926a37SAndrey V. Elsukov 			    "the last LBA.\n", pp->name);
1038e7926a37SAndrey V. Elsukov 			basetable->gpt_corrupt = 1;
10391d3aed33SMarcel Moolenaar 		}
10401d3aed33SMarcel Moolenaar 		table->hdr = prihdr;
10411c2dee3cSRobert Noland 		g_free(sechdr);
10421d3aed33SMarcel Moolenaar 		tbl = pritbl;
10431d3aed33SMarcel Moolenaar 		g_free(sectbl);
10441d3aed33SMarcel Moolenaar 	}
10451d3aed33SMarcel Moolenaar 
10461c2dee3cSRobert Noland 	basetable->gpt_first = table->hdr->hdr_lba_start;
10471c2dee3cSRobert Noland 	basetable->gpt_last = table->hdr->hdr_lba_end;
104896937e3bSMarcel Moolenaar 	basetable->gpt_entries = table->hdr->hdr_entries;
10491d3aed33SMarcel Moolenaar 
105096937e3bSMarcel Moolenaar 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
10511d3aed33SMarcel Moolenaar 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
10521d3aed33SMarcel Moolenaar 			continue;
1053e7926a37SAndrey V. Elsukov 		entry = (struct g_part_gpt_entry *)g_part_new_entry(
1054e7926a37SAndrey V. Elsukov 		    basetable, index + 1, tbl[index].ent_lba_start,
1055e7926a37SAndrey V. Elsukov 		    tbl[index].ent_lba_end);
10561d3aed33SMarcel Moolenaar 		entry->ent = tbl[index];
10571d3aed33SMarcel Moolenaar 	}
10581d3aed33SMarcel Moolenaar 
10591d3aed33SMarcel Moolenaar 	g_free(tbl);
1060369fe59dSMarcel Moolenaar 
1061369fe59dSMarcel Moolenaar 	/*
1062369fe59dSMarcel Moolenaar 	 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1063369fe59dSMarcel Moolenaar 	 * if (and only if) any FAT32 or FAT16 partitions have been
1064369fe59dSMarcel Moolenaar 	 * created. This happens irrespective of whether Boot Camp is
1065369fe59dSMarcel Moolenaar 	 * used/enabled, though it's generally understood to be done
1066369fe59dSMarcel Moolenaar 	 * to support legacy Windows under Boot Camp. We refer to this
1067369fe59dSMarcel Moolenaar 	 * mirroring simply as Boot Camp. We try to detect Boot Camp
1068369fe59dSMarcel Moolenaar 	 * so that we can update the MBR if and when GPT changes have
1069369fe59dSMarcel Moolenaar 	 * been made. Note that we do not enable Boot Camp if not
1070369fe59dSMarcel Moolenaar 	 * previously enabled because we can't assume that we're on a
1071369fe59dSMarcel Moolenaar 	 * Mac alongside Mac OS X.
1072369fe59dSMarcel Moolenaar 	 */
1073369fe59dSMarcel Moolenaar 	table->bootcamp = gpt_is_bootcamp(table, pp->name);
1074369fe59dSMarcel Moolenaar 
10751d3aed33SMarcel Moolenaar 	return (0);
10761d3aed33SMarcel Moolenaar }
10771d3aed33SMarcel Moolenaar 
1078cec283baSPawel Jakub Dawidek static int
1079e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable)
1080e7926a37SAndrey V. Elsukov {
10813bd22a9cSMarcel Moolenaar 	struct g_part_gpt_table *table;
10823bd22a9cSMarcel Moolenaar 	struct g_provider *pp;
1083e7926a37SAndrey V. Elsukov 
10843bd22a9cSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
10853bd22a9cSMarcel Moolenaar 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
10863bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
1087*5c9f0f72SWarner Losh 	g_gpt_set_defaults(basetable, pp, NULL);
1088e7926a37SAndrey V. Elsukov 	basetable->gpt_corrupt = 0;
1089e7926a37SAndrey V. Elsukov 	return (0);
1090e7926a37SAndrey V. Elsukov }
1091e7926a37SAndrey V. Elsukov 
1092e7926a37SAndrey V. Elsukov static int
1093369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable,
1094369fe59dSMarcel Moolenaar     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1095cec283baSPawel Jakub Dawidek {
1096cec283baSPawel Jakub Dawidek 	struct g_part_gpt_entry *entry;
1097369fe59dSMarcel Moolenaar 	struct g_part_gpt_table *table;
1098ce808c7aSAllan Jude 	struct g_provider *pp;
10993bd22a9cSMarcel Moolenaar 	uint8_t *p;
1100369fe59dSMarcel Moolenaar 	uint64_t attr;
1101369fe59dSMarcel Moolenaar 	int i;
1102cec283baSPawel Jakub Dawidek 
1103369fe59dSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
1104369fe59dSMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
1105369fe59dSMarcel Moolenaar 
1106369fe59dSMarcel Moolenaar 	if (strcasecmp(attrib, "active") == 0) {
11073bd22a9cSMarcel Moolenaar 		if (table->bootcamp) {
11083bd22a9cSMarcel Moolenaar 			/* The active flag must be set on a valid entry. */
11093bd22a9cSMarcel Moolenaar 			if (entry == NULL)
11103bd22a9cSMarcel Moolenaar 				return (ENXIO);
11113bd22a9cSMarcel Moolenaar 			if (baseentry->gpe_index > NDOSPART)
1112369fe59dSMarcel Moolenaar 				return (EINVAL);
1113369fe59dSMarcel Moolenaar 			for (i = 0; i < NDOSPART; i++) {
11143bd22a9cSMarcel Moolenaar 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
11153bd22a9cSMarcel Moolenaar 				p[0] = (i == baseentry->gpe_index - 1)
11163bd22a9cSMarcel Moolenaar 				    ? ((set) ? 0x80 : 0) : 0;
11173bd22a9cSMarcel Moolenaar 			}
11183bd22a9cSMarcel Moolenaar 		} else {
11193bd22a9cSMarcel Moolenaar 			/* The PMBR is marked as active without an entry. */
11203bd22a9cSMarcel Moolenaar 			if (entry != NULL)
11213bd22a9cSMarcel Moolenaar 				return (ENXIO);
11223bd22a9cSMarcel Moolenaar 			for (i = 0; i < NDOSPART; i++) {
11233bd22a9cSMarcel Moolenaar 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
11243bd22a9cSMarcel Moolenaar 				p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
11253bd22a9cSMarcel Moolenaar 			}
1126369fe59dSMarcel Moolenaar 		}
1127369fe59dSMarcel Moolenaar 		return (0);
1128ce808c7aSAllan Jude 	} else if (strcasecmp(attrib, "lenovofix") == 0) {
1129ce808c7aSAllan Jude 		/*
1130ce808c7aSAllan Jude 		 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1131ce808c7aSAllan Jude 		 * This workaround allows Lenovo X220, T420, T520, etc to boot
1132ce808c7aSAllan Jude 		 * from GPT Partitions in BIOS mode.
1133ce808c7aSAllan Jude 		 */
1134ce808c7aSAllan Jude 
1135ce808c7aSAllan Jude 		if (entry != NULL)
1136ce808c7aSAllan Jude 			return (ENXIO);
1137ce808c7aSAllan Jude 
1138ce808c7aSAllan Jude 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1139ce808c7aSAllan Jude 		bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1140ce808c7aSAllan Jude 		gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1141ce808c7aSAllan Jude 		    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1142ce808c7aSAllan Jude 		return (0);
1143369fe59dSMarcel Moolenaar 	}
1144369fe59dSMarcel Moolenaar 
11453bd22a9cSMarcel Moolenaar 	if (entry == NULL)
11463bd22a9cSMarcel Moolenaar 		return (ENODEV);
11473bd22a9cSMarcel Moolenaar 
1148369fe59dSMarcel Moolenaar 	attr = 0;
1149cec283baSPawel Jakub Dawidek 	if (strcasecmp(attrib, "bootme") == 0) {
1150369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTME;
1151cec283baSPawel Jakub Dawidek 	} else if (strcasecmp(attrib, "bootonce") == 0) {
1152369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTONCE;
1153cec283baSPawel Jakub Dawidek 		if (set)
1154369fe59dSMarcel Moolenaar 			attr |= GPT_ENT_ATTR_BOOTME;
1155cec283baSPawel Jakub Dawidek 	} else if (strcasecmp(attrib, "bootfailed") == 0) {
1156cec283baSPawel Jakub Dawidek 		/*
1157cec283baSPawel Jakub Dawidek 		 * It should only be possible to unset BOOTFAILED, but it might
1158cec283baSPawel Jakub Dawidek 		 * be useful for test purposes to also be able to set it.
1159cec283baSPawel Jakub Dawidek 		 */
1160369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTFAILED;
1161cec283baSPawel Jakub Dawidek 	}
1162369fe59dSMarcel Moolenaar 	if (attr == 0)
1163cec283baSPawel Jakub Dawidek 		return (EINVAL);
1164cec283baSPawel Jakub Dawidek 
1165369fe59dSMarcel Moolenaar 	if (set)
1166369fe59dSMarcel Moolenaar 		attr = entry->ent.ent_attr | attr;
1167369fe59dSMarcel Moolenaar 	else
1168369fe59dSMarcel Moolenaar 		attr = entry->ent.ent_attr & ~attr;
1169369fe59dSMarcel Moolenaar 	if (attr != entry->ent.ent_attr) {
1170369fe59dSMarcel Moolenaar 		entry->ent.ent_attr = attr;
1171369fe59dSMarcel Moolenaar 		if (!baseentry->gpe_created)
1172369fe59dSMarcel Moolenaar 			baseentry->gpe_modified = 1;
1173cec283baSPawel Jakub Dawidek 	}
1174cec283baSPawel Jakub Dawidek 	return (0);
1175cec283baSPawel Jakub Dawidek }
1176cec283baSPawel Jakub Dawidek 
11771d3aed33SMarcel Moolenaar static const char *
11781d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
11791d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
11801d3aed33SMarcel Moolenaar {
11811d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
11821d3aed33SMarcel Moolenaar 	struct uuid *type;
1183f1317430SRui Paulo 	struct g_part_uuid_alias *uap;
11841d3aed33SMarcel Moolenaar 
11851d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
11861d3aed33SMarcel Moolenaar 	type = &entry->ent.ent_type;
1187f1317430SRui Paulo 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1188f1317430SRui Paulo 		if (EQUUID(type, uap->uuid))
1189f1317430SRui Paulo 			return (g_part_alias_name(uap->alias));
1190cf231470SMarcel Moolenaar 	buf[0] = '!';
1191cf231470SMarcel Moolenaar 	snprintf_uuid(buf + 1, bufsz - 1, type);
1192f1317430SRui Paulo 
11931d3aed33SMarcel Moolenaar 	return (buf);
11941d3aed33SMarcel Moolenaar }
11951d3aed33SMarcel Moolenaar 
11961d3aed33SMarcel Moolenaar static int
11971d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
11981d3aed33SMarcel Moolenaar {
11991d3aed33SMarcel Moolenaar 	unsigned char *buf, *bp;
12001d3aed33SMarcel Moolenaar 	struct g_provider *pp;
12011d3aed33SMarcel Moolenaar 	struct g_part_entry *baseentry;
12021d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
12031d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
1204e7926a37SAndrey V. Elsukov 	size_t tblsz;
12051d3aed33SMarcel Moolenaar 	uint32_t crc;
12061d3aed33SMarcel Moolenaar 	int error, index;
12071d3aed33SMarcel Moolenaar 
12081d3aed33SMarcel Moolenaar 	pp = cp->provider;
12091d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
121055e0987aSPedro F. Giffuni 	tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
121155e0987aSPedro F. Giffuni 	    pp->sectorsize);
12121d3aed33SMarcel Moolenaar 
1213369fe59dSMarcel Moolenaar 	/* Reconstruct the MBR from the GPT if under Boot Camp. */
1214369fe59dSMarcel Moolenaar 	if (table->bootcamp)
12153bd22a9cSMarcel Moolenaar 		gpt_update_bootcamp(basetable, pp);
1216d4746e10SAndrey V. Elsukov 
12174d32fcb4SMarcel Moolenaar 	/* Write the PMBR */
12181d3aed33SMarcel Moolenaar 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
12194d32fcb4SMarcel Moolenaar 	bcopy(table->mbr, buf, MBRSIZE);
12201d3aed33SMarcel Moolenaar 	error = g_write_data(cp, 0, buf, pp->sectorsize);
12211d3aed33SMarcel Moolenaar 	g_free(buf);
12221d3aed33SMarcel Moolenaar 	if (error)
12231d3aed33SMarcel Moolenaar 		return (error);
12241d3aed33SMarcel Moolenaar 
12251d3aed33SMarcel Moolenaar 	/* Allocate space for the header and entries. */
1226e7926a37SAndrey V. Elsukov 	buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
12271d3aed33SMarcel Moolenaar 
12281c2dee3cSRobert Noland 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
12291c2dee3cSRobert Noland 	le32enc(buf + 8, table->hdr->hdr_revision);
12301c2dee3cSRobert Noland 	le32enc(buf + 12, table->hdr->hdr_size);
12311c2dee3cSRobert Noland 	le64enc(buf + 40, table->hdr->hdr_lba_start);
12321c2dee3cSRobert Noland 	le64enc(buf + 48, table->hdr->hdr_lba_end);
12331c2dee3cSRobert Noland 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
12341c2dee3cSRobert Noland 	le32enc(buf + 80, table->hdr->hdr_entries);
12351c2dee3cSRobert Noland 	le32enc(buf + 84, table->hdr->hdr_entsz);
12361d3aed33SMarcel Moolenaar 
12371d3aed33SMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1238d287f590SMarcel Moolenaar 		if (baseentry->gpe_deleted)
1239d287f590SMarcel Moolenaar 			continue;
12401d3aed33SMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
12411d3aed33SMarcel Moolenaar 		index = baseentry->gpe_index - 1;
12421c2dee3cSRobert Noland 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
12431d3aed33SMarcel Moolenaar 		le_uuid_enc(bp, &entry->ent.ent_type);
12441d3aed33SMarcel Moolenaar 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
12451d3aed33SMarcel Moolenaar 		le64enc(bp + 32, entry->ent.ent_lba_start);
12461d3aed33SMarcel Moolenaar 		le64enc(bp + 40, entry->ent.ent_lba_end);
12471d3aed33SMarcel Moolenaar 		le64enc(bp + 48, entry->ent.ent_attr);
12481d3aed33SMarcel Moolenaar 		memcpy(bp + 56, entry->ent.ent_name,
12491d3aed33SMarcel Moolenaar 		    sizeof(entry->ent.ent_name));
12501d3aed33SMarcel Moolenaar 	}
12511d3aed33SMarcel Moolenaar 
12521d3aed33SMarcel Moolenaar 	crc = crc32(buf + pp->sectorsize,
12531c2dee3cSRobert Noland 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
12541d3aed33SMarcel Moolenaar 	le32enc(buf + 88, crc);
12551d3aed33SMarcel Moolenaar 
12561d3aed33SMarcel Moolenaar 	/* Write primary meta-data. */
12571d3aed33SMarcel Moolenaar 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
12581d3aed33SMarcel Moolenaar 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
12591d3aed33SMarcel Moolenaar 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
12601d3aed33SMarcel Moolenaar 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
12611c2dee3cSRobert Noland 	crc = crc32(buf, table->hdr->hdr_size);
12621d3aed33SMarcel Moolenaar 	le32enc(buf + 16, crc);
12631d3aed33SMarcel Moolenaar 
1264cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1265799eac8cSAndrey V. Elsukov 		error = g_write_data(cp,
1266799eac8cSAndrey V. Elsukov 		    (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1267799eac8cSAndrey V. Elsukov 		    buf + (index + 1) * pp->sectorsize,
1268cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1269799eac8cSAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
12701d3aed33SMarcel Moolenaar 		if (error)
12711d3aed33SMarcel Moolenaar 			goto out;
1272799eac8cSAndrey V. Elsukov 	}
12731d3aed33SMarcel Moolenaar 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
12741d3aed33SMarcel Moolenaar 	    buf, pp->sectorsize);
12751d3aed33SMarcel Moolenaar 	if (error)
12761d3aed33SMarcel Moolenaar 		goto out;
12771d3aed33SMarcel Moolenaar 
12781d3aed33SMarcel Moolenaar 	/* Write secondary meta-data. */
12791d3aed33SMarcel Moolenaar 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
12801d3aed33SMarcel Moolenaar 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
12811d3aed33SMarcel Moolenaar 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
12821d3aed33SMarcel Moolenaar 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
12831c2dee3cSRobert Noland 	crc = crc32(buf, table->hdr->hdr_size);
12841d3aed33SMarcel Moolenaar 	le32enc(buf + 16, crc);
12851d3aed33SMarcel Moolenaar 
1286cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1287799eac8cSAndrey V. Elsukov 		error = g_write_data(cp,
1288799eac8cSAndrey V. Elsukov 		    (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1289799eac8cSAndrey V. Elsukov 		    buf + (index + 1) * pp->sectorsize,
1290cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1291799eac8cSAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
12921d3aed33SMarcel Moolenaar 		if (error)
12931d3aed33SMarcel Moolenaar 			goto out;
1294799eac8cSAndrey V. Elsukov 	}
12951d3aed33SMarcel Moolenaar 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
12961d3aed33SMarcel Moolenaar 	    buf, pp->sectorsize);
12971d3aed33SMarcel Moolenaar 
12981d3aed33SMarcel Moolenaar  out:
12991d3aed33SMarcel Moolenaar 	g_free(buf);
13001d3aed33SMarcel Moolenaar 	return (error);
13011d3aed33SMarcel Moolenaar }
13021d3aed33SMarcel Moolenaar 
13031d3aed33SMarcel Moolenaar static void
1304*5c9f0f72SWarner Losh g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp,
1305*5c9f0f72SWarner Losh 	struct g_part_parms *gpp)
13062b9be055SAndrey V. Elsukov {
13074787115dSMarcel Moolenaar 	struct g_part_entry *baseentry;
13084787115dSMarcel Moolenaar 	struct g_part_gpt_entry *entry;
13092b9be055SAndrey V. Elsukov 	struct g_part_gpt_table *table;
13104787115dSMarcel Moolenaar 	quad_t start, end, min, max;
13114787115dSMarcel Moolenaar 	quad_t lba, last;
13124787115dSMarcel Moolenaar 	size_t spb, tblsz;
13132b9be055SAndrey V. Elsukov 
13142b9be055SAndrey V. Elsukov 	table = (struct g_part_gpt_table *)basetable;
13152b9be055SAndrey V. Elsukov 	last = pp->mediasize / pp->sectorsize - 1;
131655e0987aSPedro F. Giffuni 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
131755e0987aSPedro F. Giffuni 	    pp->sectorsize);
13182b9be055SAndrey V. Elsukov 
13192b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_PRIHDR] = 1;
13202b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_PRITBL] = 2;
13212b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_SECHDR] = last;
13222b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
13232b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
13242b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
13252b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
13262b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
13272b9be055SAndrey V. Elsukov 
13284787115dSMarcel Moolenaar 	max = start = 2 + tblsz;
13294787115dSMarcel Moolenaar 	min = end = last - tblsz - 1;
13304787115dSMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
13314787115dSMarcel Moolenaar 		if (baseentry->gpe_deleted)
13324787115dSMarcel Moolenaar 			continue;
13334787115dSMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
13344787115dSMarcel Moolenaar 		if (entry->ent.ent_lba_start < min)
13354787115dSMarcel Moolenaar 			min = entry->ent.ent_lba_start;
13364787115dSMarcel Moolenaar 		if (entry->ent.ent_lba_end > max)
13374787115dSMarcel Moolenaar 			max = entry->ent.ent_lba_end;
13384787115dSMarcel Moolenaar 	}
1339*5c9f0f72SWarner Losh 	/*
1340*5c9f0f72SWarner Losh 	 * Don't force alignment of any kind whatsoever on resize, restore or
1341*5c9f0f72SWarner Losh 	 * recover. resize doesn't go through this path, recover has a NULL gpp
1342*5c9f0f72SWarner Losh 	 * and restore has flags == restore (maybe with an appended 'C' to
1343*5c9f0f72SWarner Losh 	 * commit the operation). For these operations, we have to trust the
1344*5c9f0f72SWarner Losh 	 * user knows what they are doing.
1345*5c9f0f72SWarner Losh 	 *
1346*5c9f0f72SWarner Losh 	 * Otherwise it some flavor of creation of a new partition, so we align
1347*5c9f0f72SWarner Losh 	 * to a 4k offset on the drive, to make 512e/4kn drives more performant
1348*5c9f0f72SWarner Losh 	 * by default.
1349*5c9f0f72SWarner Losh 	 */
1350*5c9f0f72SWarner Losh 	if (gpp == NULL ||
1351*5c9f0f72SWarner Losh 	    (gpp->gpp_parms & G_PART_PARM_FLAGS) == 0 ||
1352*5c9f0f72SWarner Losh 	    strstr(gpp->gpp_flags, "restore") == NULL) {
13534787115dSMarcel Moolenaar 		spb = 4096 / pp->sectorsize;
13544787115dSMarcel Moolenaar 		if (spb > 1) {
13554787115dSMarcel Moolenaar 			lba = start + ((start % spb) ? spb - start % spb : 0);
13564787115dSMarcel Moolenaar 			if (lba <= min)
13574787115dSMarcel Moolenaar 				start = lba;
13584787115dSMarcel Moolenaar 			lba = end - (end + 1) % spb;
13594787115dSMarcel Moolenaar 			if (max <= lba)
13604787115dSMarcel Moolenaar 				end = lba;
13614787115dSMarcel Moolenaar 		}
1362*5c9f0f72SWarner Losh 	}
13634787115dSMarcel Moolenaar 	table->hdr->hdr_lba_start = start;
13644787115dSMarcel Moolenaar 	table->hdr->hdr_lba_end = end;
13652b9be055SAndrey V. Elsukov 
13664787115dSMarcel Moolenaar 	basetable->gpt_first = start;
13674787115dSMarcel Moolenaar 	basetable->gpt_last = end;
13682b9be055SAndrey V. Elsukov }
13692b9be055SAndrey V. Elsukov 
13702b9be055SAndrey V. Elsukov static void
1371d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
13721d3aed33SMarcel Moolenaar {
13731d3aed33SMarcel Moolenaar 	u_int bo;
13741d3aed33SMarcel Moolenaar 	uint32_t ch;
13751d3aed33SMarcel Moolenaar 	uint16_t c;
13761d3aed33SMarcel Moolenaar 
1377d3532631SMarcel Moolenaar 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
13781d3aed33SMarcel Moolenaar 	while (len > 0 && *str != 0) {
13791d3aed33SMarcel Moolenaar 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
13801d3aed33SMarcel Moolenaar 		str++, len--;
13811d3aed33SMarcel Moolenaar 		if ((ch & 0xf800) == 0xd800) {
13821d3aed33SMarcel Moolenaar 			if (len > 0) {
13831d3aed33SMarcel Moolenaar 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
13841d3aed33SMarcel Moolenaar 				    : le16toh(*str);
13851d3aed33SMarcel Moolenaar 				str++, len--;
13861d3aed33SMarcel Moolenaar 			} else
13871d3aed33SMarcel Moolenaar 				c = 0xfffd;
13881d3aed33SMarcel Moolenaar 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
13891d3aed33SMarcel Moolenaar 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
13901d3aed33SMarcel Moolenaar 				ch += 0x10000;
13911d3aed33SMarcel Moolenaar 			} else
13921d3aed33SMarcel Moolenaar 				ch = 0xfffd;
13931d3aed33SMarcel Moolenaar 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
13941d3aed33SMarcel Moolenaar 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
13951d3aed33SMarcel Moolenaar 			continue;
13961d3aed33SMarcel Moolenaar 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
13971d3aed33SMarcel Moolenaar 			continue;
13981d3aed33SMarcel Moolenaar 
1399d3532631SMarcel Moolenaar 		/* Write the Unicode character in UTF-8 */
14001d3aed33SMarcel Moolenaar 		if (ch < 0x80)
140139dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c", ch);
14021d3aed33SMarcel Moolenaar 		else if (ch < 0x800)
140339dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
14041d3aed33SMarcel Moolenaar 			    0x80 | (ch & 0x3f));
14051d3aed33SMarcel Moolenaar 		else if (ch < 0x10000)
140639dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
14071d3aed33SMarcel Moolenaar 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
14081d3aed33SMarcel Moolenaar 		else if (ch < 0x200000)
140939dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
141039dcac84SAndrey V. Elsukov 			    (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
14111d3aed33SMarcel Moolenaar 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
14121d3aed33SMarcel Moolenaar 	}
14131d3aed33SMarcel Moolenaar }
1414d3532631SMarcel Moolenaar 
1415d3532631SMarcel Moolenaar static void
1416d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1417d3532631SMarcel Moolenaar {
1418d3532631SMarcel Moolenaar 	size_t s16idx, s8idx;
1419d3532631SMarcel Moolenaar 	uint32_t utfchar;
1420d3532631SMarcel Moolenaar 	unsigned int c, utfbytes;
1421d3532631SMarcel Moolenaar 
1422d3532631SMarcel Moolenaar 	s8idx = s16idx = 0;
1423d3532631SMarcel Moolenaar 	utfchar = 0;
1424d3532631SMarcel Moolenaar 	utfbytes = 0;
1425d3532631SMarcel Moolenaar 	bzero(s16, s16len << 1);
1426d3532631SMarcel Moolenaar 	while (s8[s8idx] != 0 && s16idx < s16len) {
1427d3532631SMarcel Moolenaar 		c = s8[s8idx++];
1428d3532631SMarcel Moolenaar 		if ((c & 0xc0) != 0x80) {
1429d3532631SMarcel Moolenaar 			/* Initial characters. */
1430d3532631SMarcel Moolenaar 			if (utfbytes != 0) {
1431d3532631SMarcel Moolenaar 				/* Incomplete encoding of previous char. */
1432d3532631SMarcel Moolenaar 				s16[s16idx++] = htole16(0xfffd);
1433d3532631SMarcel Moolenaar 			}
1434d3532631SMarcel Moolenaar 			if ((c & 0xf8) == 0xf0) {
1435d3532631SMarcel Moolenaar 				utfchar = c & 0x07;
1436d3532631SMarcel Moolenaar 				utfbytes = 3;
1437d3532631SMarcel Moolenaar 			} else if ((c & 0xf0) == 0xe0) {
1438d3532631SMarcel Moolenaar 				utfchar = c & 0x0f;
1439d3532631SMarcel Moolenaar 				utfbytes = 2;
1440d3532631SMarcel Moolenaar 			} else if ((c & 0xe0) == 0xc0) {
1441d3532631SMarcel Moolenaar 				utfchar = c & 0x1f;
1442d3532631SMarcel Moolenaar 				utfbytes = 1;
1443d3532631SMarcel Moolenaar 			} else {
1444d3532631SMarcel Moolenaar 				utfchar = c & 0x7f;
1445d3532631SMarcel Moolenaar 				utfbytes = 0;
1446d3532631SMarcel Moolenaar 			}
1447d3532631SMarcel Moolenaar 		} else {
1448d3532631SMarcel Moolenaar 			/* Followup characters. */
1449d3532631SMarcel Moolenaar 			if (utfbytes > 0) {
1450d3532631SMarcel Moolenaar 				utfchar = (utfchar << 6) + (c & 0x3f);
1451d3532631SMarcel Moolenaar 				utfbytes--;
1452d3532631SMarcel Moolenaar 			} else if (utfbytes == 0)
1453d3532631SMarcel Moolenaar 				utfbytes = ~0;
1454d3532631SMarcel Moolenaar 		}
1455d3532631SMarcel Moolenaar 		/*
1456d3532631SMarcel Moolenaar 		 * Write the complete Unicode character as UTF-16 when we
1457d3532631SMarcel Moolenaar 		 * have all the UTF-8 charactars collected.
1458d3532631SMarcel Moolenaar 		 */
1459d3532631SMarcel Moolenaar 		if (utfbytes == 0) {
1460d3532631SMarcel Moolenaar 			/*
1461d3532631SMarcel Moolenaar 			 * If we need to write 2 UTF-16 characters, but
1462d3532631SMarcel Moolenaar 			 * we only have room for 1, then we truncate the
1463d3532631SMarcel Moolenaar 			 * string by writing a 0 instead.
1464d3532631SMarcel Moolenaar 			 */
1465d3532631SMarcel Moolenaar 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1466d3532631SMarcel Moolenaar 				s16[s16idx++] =
1467d3532631SMarcel Moolenaar 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
1468d3532631SMarcel Moolenaar 				s16[s16idx++] =
1469d3532631SMarcel Moolenaar 				    htole16(0xdc00 | (utfchar & 0x3ff));
1470d3532631SMarcel Moolenaar 			} else
1471d3532631SMarcel Moolenaar 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1472d3532631SMarcel Moolenaar 				    htole16(utfchar);
1473d3532631SMarcel Moolenaar 		}
1474d3532631SMarcel Moolenaar 	}
1475d3532631SMarcel Moolenaar 	/*
1476d3532631SMarcel Moolenaar 	 * If our input string was truncated, append an invalid encoding
1477d3532631SMarcel Moolenaar 	 * character to the output string.
1478d3532631SMarcel Moolenaar 	 */
1479d3532631SMarcel Moolenaar 	if (utfbytes != 0 && s16idx < s16len)
1480d3532631SMarcel Moolenaar 		s16[s16idx++] = htole16(0xfffd);
1481d3532631SMarcel Moolenaar }
1482