xref: /freebsd/sys/geom/part/g_part_gpt.c (revision 2cbda736cea8f82cfc5caab0f6099f0fbfe28537)
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/param.h>
301d3aed33SMarcel Moolenaar #include <sys/bio.h>
311d3aed33SMarcel Moolenaar #include <sys/diskmbr.h>
32f89d2072SXin LI #include <sys/gsb_crc32.h>
331d3aed33SMarcel Moolenaar #include <sys/endian.h>
341d3aed33SMarcel Moolenaar #include <sys/gpt.h>
351d3aed33SMarcel Moolenaar #include <sys/kernel.h>
361d3aed33SMarcel Moolenaar #include <sys/kobj.h>
371d3aed33SMarcel Moolenaar #include <sys/limits.h>
381d3aed33SMarcel Moolenaar #include <sys/lock.h>
391d3aed33SMarcel Moolenaar #include <sys/malloc.h>
401d3aed33SMarcel Moolenaar #include <sys/mutex.h>
411d3aed33SMarcel Moolenaar #include <sys/queue.h>
421d3aed33SMarcel Moolenaar #include <sys/sbuf.h>
431d3aed33SMarcel Moolenaar #include <sys/systm.h>
44cb08c2ccSAlexander Leidinger #include <sys/sysctl.h>
451d3aed33SMarcel Moolenaar #include <sys/uuid.h>
461d3aed33SMarcel Moolenaar #include <geom/geom.h>
4739dcac84SAndrey V. Elsukov #include <geom/geom_int.h>
481d3aed33SMarcel Moolenaar #include <geom/part/g_part.h>
491d3aed33SMarcel Moolenaar 
501d3aed33SMarcel Moolenaar #include "g_part_if.h"
511d3aed33SMarcel Moolenaar 
52cb08c2ccSAlexander Leidinger FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
53cb08c2ccSAlexander Leidinger 
54ef03f57dSKyle Evans SYSCTL_DECL(_kern_geom_part);
557029da5cSPawel Biernacki static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt,
567029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
57ef03f57dSKyle Evans     "GEOM_PART_GPT GUID Partition Table");
58ef03f57dSKyle Evans 
59ef03f57dSKyle Evans static u_int allow_nesting = 0;
60ef03f57dSKyle Evans SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting,
61ef03f57dSKyle Evans     CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes");
62ef03f57dSKyle Evans 
631d3aed33SMarcel Moolenaar CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
641d3aed33SMarcel Moolenaar CTASSERT(sizeof(struct gpt_ent) == 128);
651d3aed33SMarcel Moolenaar 
66cea05ed9SEugene Grosbein extern u_int geom_part_check_integrity;
67cea05ed9SEugene Grosbein 
681d3aed33SMarcel Moolenaar #define	EQUUID(a,b)	(memcmp(a, b, sizeof(struct uuid)) == 0)
691d3aed33SMarcel Moolenaar 
704d32fcb4SMarcel Moolenaar #define	MBRSIZE		512
714d32fcb4SMarcel Moolenaar 
721d3aed33SMarcel Moolenaar enum gpt_elt {
731d3aed33SMarcel Moolenaar 	GPT_ELT_PRIHDR,
741d3aed33SMarcel Moolenaar 	GPT_ELT_PRITBL,
751d3aed33SMarcel Moolenaar 	GPT_ELT_SECHDR,
761d3aed33SMarcel Moolenaar 	GPT_ELT_SECTBL,
771d3aed33SMarcel Moolenaar 	GPT_ELT_COUNT
781d3aed33SMarcel Moolenaar };
791d3aed33SMarcel Moolenaar 
801d3aed33SMarcel Moolenaar enum gpt_state {
811d3aed33SMarcel Moolenaar 	GPT_STATE_UNKNOWN,	/* Not determined. */
821d3aed33SMarcel Moolenaar 	GPT_STATE_MISSING,	/* No signature found. */
831d3aed33SMarcel Moolenaar 	GPT_STATE_CORRUPT,	/* Checksum mismatch. */
841d3aed33SMarcel Moolenaar 	GPT_STATE_INVALID,	/* Nonconformant/invalid. */
855be5d0d5SZhenlei Huang 	GPT_STATE_UNSUPPORTED,  /* Not supported. */
861d3aed33SMarcel Moolenaar 	GPT_STATE_OK		/* Perfectly fine. */
871d3aed33SMarcel Moolenaar };
881d3aed33SMarcel Moolenaar 
891d3aed33SMarcel Moolenaar struct g_part_gpt_table {
901d3aed33SMarcel Moolenaar 	struct g_part_table	base;
914d32fcb4SMarcel Moolenaar 	u_char			mbr[MBRSIZE];
921c2dee3cSRobert Noland 	struct gpt_hdr		*hdr;
931d3aed33SMarcel Moolenaar 	quad_t			lba[GPT_ELT_COUNT];
941d3aed33SMarcel Moolenaar 	enum gpt_state		state[GPT_ELT_COUNT];
95369fe59dSMarcel Moolenaar 	int			bootcamp;
961d3aed33SMarcel Moolenaar };
971d3aed33SMarcel Moolenaar 
981d3aed33SMarcel Moolenaar struct g_part_gpt_entry {
991d3aed33SMarcel Moolenaar 	struct g_part_entry	base;
1001d3aed33SMarcel Moolenaar 	struct gpt_ent		ent;
1011d3aed33SMarcel Moolenaar };
1021d3aed33SMarcel Moolenaar 
103d3532631SMarcel Moolenaar static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
104d3532631SMarcel Moolenaar static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
1055c9f0f72SWarner Losh static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *,
1065c9f0f72SWarner Losh     struct g_part_parms *);
107d3532631SMarcel Moolenaar 
1081d3aed33SMarcel Moolenaar static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
1091d3aed33SMarcel Moolenaar     struct g_part_parms *);
1104d32fcb4SMarcel Moolenaar static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
1111d3aed33SMarcel Moolenaar static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
1121d3aed33SMarcel Moolenaar static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
11351f53a08SWarner Losh static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
1145db67052SMarcel Moolenaar     struct sbuf *, const char *);
1151d3aed33SMarcel Moolenaar static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
1161d3aed33SMarcel Moolenaar static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
1171d3aed33SMarcel Moolenaar     struct g_part_parms *);
11851f53a08SWarner Losh static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
1191d3aed33SMarcel Moolenaar     char *, size_t);
1201d3aed33SMarcel Moolenaar static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
1211d3aed33SMarcel Moolenaar static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
122cec283baSPawel Jakub Dawidek static int g_part_gpt_setunset(struct g_part_table *table,
123cec283baSPawel Jakub Dawidek     struct g_part_entry *baseentry, const char *attrib, unsigned int set);
1241d3aed33SMarcel Moolenaar static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
1251d3aed33SMarcel Moolenaar     char *, size_t);
1261d3aed33SMarcel Moolenaar static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
1273f71c319SMarcel Moolenaar static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
1283f71c319SMarcel Moolenaar     struct g_part_parms *);
129e7926a37SAndrey V. Elsukov static int g_part_gpt_recover(struct g_part_table *);
1301d3aed33SMarcel Moolenaar 
1311d3aed33SMarcel Moolenaar static kobj_method_t g_part_gpt_methods[] = {
1321d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_add,		g_part_gpt_add),
1334d32fcb4SMarcel Moolenaar 	KOBJMETHOD(g_part_bootcode,	g_part_gpt_bootcode),
1341d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_create,	g_part_gpt_create),
1351d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_destroy,	g_part_gpt_destroy),
1365db67052SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpconf,	g_part_gpt_dumpconf),
1371d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpto,	g_part_gpt_dumpto),
1381d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_modify,	g_part_gpt_modify),
1393f71c319SMarcel Moolenaar 	KOBJMETHOD(g_part_resize,	g_part_gpt_resize),
1401d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_name,		g_part_gpt_name),
1411d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_probe,	g_part_gpt_probe),
1421d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_read,		g_part_gpt_read),
143e7926a37SAndrey V. Elsukov 	KOBJMETHOD(g_part_recover,	g_part_gpt_recover),
144cec283baSPawel Jakub Dawidek 	KOBJMETHOD(g_part_setunset,	g_part_gpt_setunset),
1451d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_type,		g_part_gpt_type),
1461d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_write,	g_part_gpt_write),
1471d3aed33SMarcel Moolenaar 	{ 0, 0 }
1481d3aed33SMarcel Moolenaar };
1491d3aed33SMarcel Moolenaar 
1505be5d0d5SZhenlei Huang #define MAXENTSIZE 1024
1515be5d0d5SZhenlei Huang 
1521d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_gpt_scheme = {
1531d3aed33SMarcel Moolenaar 	"GPT",
1541d3aed33SMarcel Moolenaar 	g_part_gpt_methods,
1551d3aed33SMarcel Moolenaar 	sizeof(struct g_part_gpt_table),
1561d3aed33SMarcel Moolenaar 	.gps_entrysz = sizeof(struct g_part_gpt_entry),
15709c999b1SWarner Losh 	.gps_minent = 1,
15809c999b1SWarner Losh 	.gps_defent = 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;
227b162fc3fSJessica Clarke static struct uuid gpt_uuid_u_boot_env = GPT_ENT_TYPE_U_BOOT_ENV;
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 },
300b162fc3fSJessica Clarke 	{ &gpt_uuid_u_boot_env,		G_PART_ALIAS_U_BOOT_ENV,	 0 },
3019c0c355fSAllan Jude 	{ &gpt_uuid_vmfs,		G_PART_ALIAS_VMFS,		 0 },
3029c0c355fSAllan Jude 	{ &gpt_uuid_vmkdiag,		G_PART_ALIAS_VMKDIAG,		 0 },
3039c0c355fSAllan Jude 	{ &gpt_uuid_vmreserved,		G_PART_ALIAS_VMRESERVED,	 0 },
3049c0c355fSAllan Jude 	{ &gpt_uuid_vmvsanhdr,		G_PART_ALIAS_VMVSANHDR,		 0 },
305369fe59dSMarcel Moolenaar 	{ NULL, 0, 0 }
306f1317430SRui Paulo };
307f1317430SRui Paulo 
308369fe59dSMarcel Moolenaar static int
gpt_write_mbr_entry(u_char * mbr,int idx,int typ,quad_t start,quad_t end)309369fe59dSMarcel Moolenaar gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
310369fe59dSMarcel Moolenaar     quad_t end)
311369fe59dSMarcel Moolenaar {
312369fe59dSMarcel Moolenaar 
313369fe59dSMarcel Moolenaar 	if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
314369fe59dSMarcel Moolenaar 		return (EINVAL);
315369fe59dSMarcel Moolenaar 
316369fe59dSMarcel Moolenaar 	mbr += DOSPARTOFF + idx * DOSPARTSIZE;
317369fe59dSMarcel Moolenaar 	mbr[0] = 0;
318369fe59dSMarcel Moolenaar 	if (start == 1) {
319369fe59dSMarcel Moolenaar 		/*
320369fe59dSMarcel Moolenaar 		 * Treat the PMBR partition specially to maximize
321369fe59dSMarcel Moolenaar 		 * interoperability with BIOSes.
322369fe59dSMarcel Moolenaar 		 */
323369fe59dSMarcel Moolenaar 		mbr[1] = mbr[3] = 0;
324369fe59dSMarcel Moolenaar 		mbr[2] = 2;
325369fe59dSMarcel Moolenaar 	} else
326369fe59dSMarcel Moolenaar 		mbr[1] = mbr[2] = mbr[3] = 0xff;
327369fe59dSMarcel Moolenaar 	mbr[4] = typ;
328369fe59dSMarcel Moolenaar 	mbr[5] = mbr[6] = mbr[7] = 0xff;
329369fe59dSMarcel Moolenaar 	le32enc(mbr + 8, (uint32_t)start);
330369fe59dSMarcel Moolenaar 	le32enc(mbr + 12, (uint32_t)(end - start + 1));
331369fe59dSMarcel Moolenaar 	return (0);
332369fe59dSMarcel Moolenaar }
333369fe59dSMarcel Moolenaar 
334369fe59dSMarcel Moolenaar static int
gpt_map_type(struct uuid * t)335369fe59dSMarcel Moolenaar gpt_map_type(struct uuid *t)
336369fe59dSMarcel Moolenaar {
337369fe59dSMarcel Moolenaar 	struct g_part_uuid_alias *uap;
338369fe59dSMarcel Moolenaar 
339369fe59dSMarcel Moolenaar 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
340369fe59dSMarcel Moolenaar 		if (EQUUID(t, uap->uuid))
341369fe59dSMarcel Moolenaar 			return (uap->mbrtype);
342369fe59dSMarcel Moolenaar 	}
343369fe59dSMarcel Moolenaar 	return (0);
344369fe59dSMarcel Moolenaar }
345369fe59dSMarcel Moolenaar 
3463bd22a9cSMarcel Moolenaar static void
gpt_create_pmbr(struct g_part_gpt_table * table,struct g_provider * pp)3473bd22a9cSMarcel Moolenaar gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
3483bd22a9cSMarcel Moolenaar {
3493bd22a9cSMarcel Moolenaar 
3503bd22a9cSMarcel Moolenaar 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
3513bd22a9cSMarcel Moolenaar 	gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
3523bd22a9cSMarcel Moolenaar 	    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
3533bd22a9cSMarcel Moolenaar 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
3543bd22a9cSMarcel Moolenaar }
3553bd22a9cSMarcel Moolenaar 
356369fe59dSMarcel Moolenaar /*
357369fe59dSMarcel Moolenaar  * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
358369fe59dSMarcel Moolenaar  * whole disk anymore. Rather, it covers the GPT table and the EFI
359369fe59dSMarcel Moolenaar  * system partition only. This way the HFS+ partition and any FAT
360369fe59dSMarcel Moolenaar  * partitions can be added to the MBR without creating an overlap.
361369fe59dSMarcel Moolenaar  */
362369fe59dSMarcel Moolenaar static int
gpt_is_bootcamp(struct g_part_gpt_table * table,const char * provname)363369fe59dSMarcel Moolenaar gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
364369fe59dSMarcel Moolenaar {
365369fe59dSMarcel Moolenaar 	uint8_t *p;
366369fe59dSMarcel Moolenaar 
367369fe59dSMarcel Moolenaar 	p = table->mbr + DOSPARTOFF;
368369fe59dSMarcel Moolenaar 	if (p[4] != 0xee || le32dec(p + 8) != 1)
369369fe59dSMarcel Moolenaar 		return (0);
370369fe59dSMarcel Moolenaar 
371369fe59dSMarcel Moolenaar 	p += DOSPARTSIZE;
372369fe59dSMarcel Moolenaar 	if (p[4] != 0xaf)
373369fe59dSMarcel Moolenaar 		return (0);
374369fe59dSMarcel Moolenaar 
375369fe59dSMarcel Moolenaar 	printf("GEOM: %s: enabling Boot Camp\n", provname);
376369fe59dSMarcel Moolenaar 	return (1);
377369fe59dSMarcel Moolenaar }
378369fe59dSMarcel Moolenaar 
379369fe59dSMarcel Moolenaar static void
gpt_update_bootcamp(struct g_part_table * basetable,struct g_provider * pp)3803bd22a9cSMarcel Moolenaar gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
381369fe59dSMarcel Moolenaar {
382369fe59dSMarcel Moolenaar 	struct g_part_entry *baseentry;
383369fe59dSMarcel Moolenaar 	struct g_part_gpt_entry *entry;
384369fe59dSMarcel Moolenaar 	struct g_part_gpt_table *table;
385369fe59dSMarcel Moolenaar 	int bootable, error, index, slices, typ;
386369fe59dSMarcel Moolenaar 
387369fe59dSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
388369fe59dSMarcel Moolenaar 
389369fe59dSMarcel Moolenaar 	bootable = -1;
390369fe59dSMarcel Moolenaar 	for (index = 0; index < NDOSPART; index++) {
391369fe59dSMarcel Moolenaar 		if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
392369fe59dSMarcel Moolenaar 			bootable = index;
393369fe59dSMarcel Moolenaar 	}
394369fe59dSMarcel Moolenaar 
395369fe59dSMarcel Moolenaar 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
396369fe59dSMarcel Moolenaar 	slices = 0;
397369fe59dSMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
398369fe59dSMarcel Moolenaar 		if (baseentry->gpe_deleted)
399369fe59dSMarcel Moolenaar 			continue;
400369fe59dSMarcel Moolenaar 		index = baseentry->gpe_index - 1;
401369fe59dSMarcel Moolenaar 		if (index >= NDOSPART)
402369fe59dSMarcel Moolenaar 			continue;
403369fe59dSMarcel Moolenaar 
404369fe59dSMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
405369fe59dSMarcel Moolenaar 
406369fe59dSMarcel Moolenaar 		switch (index) {
407369fe59dSMarcel Moolenaar 		case 0:	/* This must be the EFI system partition. */
408369fe59dSMarcel Moolenaar 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
409369fe59dSMarcel Moolenaar 				goto disable;
410369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, 0xee,
411369fe59dSMarcel Moolenaar 			    1ull, entry->ent.ent_lba_end);
412369fe59dSMarcel Moolenaar 			break;
413369fe59dSMarcel Moolenaar 		case 1:	/* This must be the HFS+ partition. */
414369fe59dSMarcel Moolenaar 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
415369fe59dSMarcel Moolenaar 				goto disable;
416369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
417369fe59dSMarcel Moolenaar 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
418369fe59dSMarcel Moolenaar 			break;
419369fe59dSMarcel Moolenaar 		default:
420369fe59dSMarcel Moolenaar 			typ = gpt_map_type(&entry->ent.ent_type);
421369fe59dSMarcel Moolenaar 			error = gpt_write_mbr_entry(table->mbr, index, typ,
422369fe59dSMarcel Moolenaar 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
423369fe59dSMarcel Moolenaar 			break;
424369fe59dSMarcel Moolenaar 		}
425369fe59dSMarcel Moolenaar 		if (error)
426369fe59dSMarcel Moolenaar 			continue;
427369fe59dSMarcel Moolenaar 
428369fe59dSMarcel Moolenaar 		if (index == bootable)
429369fe59dSMarcel Moolenaar 			table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
430369fe59dSMarcel Moolenaar 		slices |= 1 << index;
431369fe59dSMarcel Moolenaar 	}
432369fe59dSMarcel Moolenaar 	if ((slices & 3) == 3)
433369fe59dSMarcel Moolenaar 		return;
434369fe59dSMarcel Moolenaar 
435369fe59dSMarcel Moolenaar  disable:
436369fe59dSMarcel Moolenaar 	table->bootcamp = 0;
4373bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
438369fe59dSMarcel Moolenaar }
439369fe59dSMarcel Moolenaar 
4401c2dee3cSRobert Noland static struct gpt_hdr *
gpt_read_hdr(struct g_part_gpt_table * table,struct g_consumer * cp,enum gpt_elt elt)4411d3aed33SMarcel Moolenaar gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
4421c2dee3cSRobert Noland     enum gpt_elt elt)
4431d3aed33SMarcel Moolenaar {
4441c2dee3cSRobert Noland 	struct gpt_hdr *buf, *hdr;
4451d3aed33SMarcel Moolenaar 	struct g_provider *pp;
4461d3aed33SMarcel Moolenaar 	quad_t lba, last;
4471d3aed33SMarcel Moolenaar 	int error;
4481d3aed33SMarcel Moolenaar 	uint32_t crc, sz;
4491d3aed33SMarcel Moolenaar 
4501d3aed33SMarcel Moolenaar 	pp = cp->provider;
4511d3aed33SMarcel Moolenaar 	last = (pp->mediasize / pp->sectorsize) - 1;
4521d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_MISSING;
453e7926a37SAndrey V. Elsukov 	/*
454e7926a37SAndrey V. Elsukov 	 * If the primary header is valid look for secondary
455e7926a37SAndrey V. Elsukov 	 * header in AlternateLBA, otherwise in the last medium's LBA.
456e7926a37SAndrey V. Elsukov 	 */
457e7926a37SAndrey V. Elsukov 	if (elt == GPT_ELT_SECHDR) {
458e7926a37SAndrey V. Elsukov 		if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
459e7926a37SAndrey V. Elsukov 			table->lba[elt] = last;
460e7926a37SAndrey V. Elsukov 	} else
461e7926a37SAndrey V. Elsukov 		table->lba[elt] = 1;
4621d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
4631d3aed33SMarcel Moolenaar 	    &error);
4641d3aed33SMarcel Moolenaar 	if (buf == NULL)
4651c2dee3cSRobert Noland 		return (NULL);
4661c2dee3cSRobert Noland 	hdr = NULL;
4671c2dee3cSRobert Noland 	if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
4681c2dee3cSRobert Noland 		goto fail;
4691d3aed33SMarcel Moolenaar 
4701d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_CORRUPT;
4711c2dee3cSRobert Noland 	sz = le32toh(buf->hdr_size);
4721d3aed33SMarcel Moolenaar 	if (sz < 92 || sz > pp->sectorsize)
4731c2dee3cSRobert Noland 		goto fail;
4741c2dee3cSRobert Noland 
4751c2dee3cSRobert Noland 	hdr = g_malloc(sz, M_WAITOK | M_ZERO);
4761c2dee3cSRobert Noland 	bcopy(buf, hdr, sz);
4771d3aed33SMarcel Moolenaar 	hdr->hdr_size = sz;
4781c2dee3cSRobert Noland 
4791c2dee3cSRobert Noland 	crc = le32toh(buf->hdr_crc_self);
4801c2dee3cSRobert Noland 	buf->hdr_crc_self = 0;
4811c2dee3cSRobert Noland 	if (crc32(buf, sz) != crc)
4821c2dee3cSRobert Noland 		goto fail;
4831d3aed33SMarcel Moolenaar 	hdr->hdr_crc_self = crc;
4841d3aed33SMarcel Moolenaar 
4851d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_INVALID;
4861c2dee3cSRobert Noland 	hdr->hdr_revision = le32toh(buf->hdr_revision);
487e7926a37SAndrey V. Elsukov 	if (hdr->hdr_revision < GPT_HDR_REVISION)
4881c2dee3cSRobert Noland 		goto fail;
4891c2dee3cSRobert Noland 	hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
4901d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_self != table->lba[elt])
4911c2dee3cSRobert Noland 		goto fail;
4921c2dee3cSRobert Noland 	hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
493cea05ed9SEugene Grosbein 	if (hdr->hdr_lba_alt == hdr->hdr_lba_self)
494cea05ed9SEugene Grosbein 		goto fail;
495cea05ed9SEugene Grosbein 	if (hdr->hdr_lba_alt > last && geom_part_check_integrity)
496e7926a37SAndrey V. Elsukov 		goto fail;
4971d3aed33SMarcel Moolenaar 
4981d3aed33SMarcel Moolenaar 	/* Check the managed area. */
4991c2dee3cSRobert Noland 	hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
5001d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
5011c2dee3cSRobert Noland 		goto fail;
5021c2dee3cSRobert Noland 	hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
5031d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
5041c2dee3cSRobert Noland 		goto fail;
5051d3aed33SMarcel Moolenaar 
5061d3aed33SMarcel Moolenaar 	/* Check the table location and size of the table. */
5071c2dee3cSRobert Noland 	hdr->hdr_entries = le32toh(buf->hdr_entries);
5081c2dee3cSRobert Noland 	hdr->hdr_entsz = le32toh(buf->hdr_entsz);
5091d3aed33SMarcel Moolenaar 	if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
5101d3aed33SMarcel Moolenaar 	    (hdr->hdr_entsz & 7) != 0)
5111c2dee3cSRobert Noland 		goto fail;
5121c2dee3cSRobert Noland 	hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
5131d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
5141c2dee3cSRobert Noland 		goto fail;
5151d3aed33SMarcel Moolenaar 	if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
5161d3aed33SMarcel Moolenaar 	    hdr->hdr_lba_table <= hdr->hdr_lba_end)
5171c2dee3cSRobert Noland 		goto fail;
5181d3aed33SMarcel Moolenaar 	lba = hdr->hdr_lba_table +
5192e543af1SZhenlei Huang 	    howmany((uint64_t)hdr->hdr_entries * hdr->hdr_entsz,
5202e543af1SZhenlei Huang 	        pp->sectorsize) - 1;
5211d3aed33SMarcel Moolenaar 	if (lba >= last)
5221c2dee3cSRobert Noland 		goto fail;
5231d3aed33SMarcel Moolenaar 	if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
5241c2dee3cSRobert Noland 		goto fail;
5251d3aed33SMarcel Moolenaar 
5261d3aed33SMarcel Moolenaar 	table->state[elt] = GPT_STATE_OK;
5271c2dee3cSRobert Noland 	le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
5281c2dee3cSRobert Noland 	hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
5291c2dee3cSRobert Noland 
530e7926a37SAndrey V. Elsukov 	/* save LBA for secondary header */
531e7926a37SAndrey V. Elsukov 	if (elt == GPT_ELT_PRIHDR)
532e7926a37SAndrey V. Elsukov 		table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
533e7926a37SAndrey V. Elsukov 
5341c2dee3cSRobert Noland 	g_free(buf);
5351c2dee3cSRobert Noland 	return (hdr);
5361c2dee3cSRobert Noland 
5371c2dee3cSRobert Noland  fail:
5381c2dee3cSRobert Noland 	g_free(hdr);
5391c2dee3cSRobert Noland 	g_free(buf);
5401c2dee3cSRobert Noland 	return (NULL);
5411d3aed33SMarcel Moolenaar }
5421d3aed33SMarcel Moolenaar 
5431d3aed33SMarcel Moolenaar static struct gpt_ent *
gpt_read_tbl(struct g_part_gpt_table * table,struct g_consumer * cp,enum gpt_elt elt,struct gpt_hdr * hdr)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
gpt_matched_hdrs(struct gpt_hdr * pri,struct gpt_hdr * sec)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
gpt_parse_type(const char * type,struct uuid * uuid)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
g_part_gpt_add(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)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
g_part_gpt_bootcode(struct g_part_table * basetable,struct g_part_parms * gpp)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
g_part_gpt_create(struct g_part_table * basetable,struct g_part_parms * gpp)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 
7235c9f0f72SWarner Losh 	g_gpt_set_defaults(basetable, pp, gpp);
7241d3aed33SMarcel Moolenaar 	return (0);
7251d3aed33SMarcel Moolenaar }
7261d3aed33SMarcel Moolenaar 
7271d3aed33SMarcel Moolenaar static int
g_part_gpt_destroy(struct g_part_table * basetable,struct g_part_parms * gpp)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
g_part_gpt_efimedia(struct g_part_gpt_entry * entry,struct sbuf * sb)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
g_part_gpt_dumpconf(struct g_part_table * table,struct g_part_entry * baseentry,struct sbuf * sb,const char * indent)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
g_part_gpt_dumpto(struct g_part_table * table,struct g_part_entry * baseentry)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
g_part_gpt_modify(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)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
g_part_gpt_resize(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)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 *
g_part_gpt_name(struct g_part_table * table,struct g_part_entry * baseentry,char * buf,size_t bufsz)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
g_part_gpt_probe(struct g_part_table * table,struct g_consumer * cp)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
g_part_gpt_read(struct g_part_table * basetable,struct g_consumer * cp)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 		g_free(prihdr);
993ee4d316fSConrad Meyer 		g_free(pritbl);
994ee4d316fSConrad Meyer 		g_free(sechdr);
995ee4d316fSConrad Meyer 		g_free(sectbl);
9961d3aed33SMarcel Moolenaar 		return (EINVAL);
9971d3aed33SMarcel Moolenaar 	}
9981d3aed33SMarcel Moolenaar 
9991d3aed33SMarcel Moolenaar 	/*
10001d3aed33SMarcel Moolenaar 	 * If both headers are good but they disagree with each other,
10011d3aed33SMarcel Moolenaar 	 * then invalidate one. We prefer to keep the primary header,
10021d3aed33SMarcel Moolenaar 	 * unless the primary table is corrupt.
10031d3aed33SMarcel Moolenaar 	 */
10041d3aed33SMarcel Moolenaar 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
10051d3aed33SMarcel Moolenaar 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
10061c2dee3cSRobert Noland 	    !gpt_matched_hdrs(prihdr, sechdr)) {
1007dd0db05dSMarcel Moolenaar 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
10081d3aed33SMarcel Moolenaar 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
1009dd0db05dSMarcel Moolenaar 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
10101c2dee3cSRobert Noland 			g_free(sechdr);
10111c2dee3cSRobert Noland 			sechdr = NULL;
1012dd0db05dSMarcel Moolenaar 		} else {
10131d3aed33SMarcel Moolenaar 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
1014dd0db05dSMarcel Moolenaar 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
10151c2dee3cSRobert Noland 			g_free(prihdr);
10161c2dee3cSRobert Noland 			prihdr = NULL;
1017dd0db05dSMarcel Moolenaar 		}
10181d3aed33SMarcel Moolenaar 	}
10191d3aed33SMarcel Moolenaar 
1020dd0db05dSMarcel Moolenaar 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
10211d3aed33SMarcel Moolenaar 		printf("GEOM: %s: the primary GPT table is corrupt or "
10221d3aed33SMarcel Moolenaar 		    "invalid.\n", pp->name);
10231d3aed33SMarcel Moolenaar 		printf("GEOM: %s: using the secondary instead -- recovery "
10241d3aed33SMarcel Moolenaar 		    "strongly advised.\n", pp->name);
10251d3aed33SMarcel Moolenaar 		table->hdr = sechdr;
1026e7926a37SAndrey V. Elsukov 		basetable->gpt_corrupt = 1;
10271c2dee3cSRobert Noland 		g_free(prihdr);
10281d3aed33SMarcel Moolenaar 		tbl = sectbl;
10291d3aed33SMarcel Moolenaar 		g_free(pritbl);
10301d3aed33SMarcel Moolenaar 	} else {
1031dd0db05dSMarcel Moolenaar 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
10321d3aed33SMarcel Moolenaar 			printf("GEOM: %s: the secondary GPT table is corrupt "
10331d3aed33SMarcel Moolenaar 			    "or invalid.\n", pp->name);
10341d3aed33SMarcel Moolenaar 			printf("GEOM: %s: using the primary only -- recovery "
10351d3aed33SMarcel Moolenaar 			    "suggested.\n", pp->name);
1036e7926a37SAndrey V. Elsukov 			basetable->gpt_corrupt = 1;
1037e7926a37SAndrey V. Elsukov 		} else if (table->lba[GPT_ELT_SECHDR] != last) {
1038e7926a37SAndrey V. Elsukov 			printf( "GEOM: %s: the secondary GPT header is not in "
1039e7926a37SAndrey V. Elsukov 			    "the last LBA.\n", pp->name);
1040e7926a37SAndrey V. Elsukov 			basetable->gpt_corrupt = 1;
10411d3aed33SMarcel Moolenaar 		}
10421d3aed33SMarcel Moolenaar 		table->hdr = prihdr;
10431c2dee3cSRobert Noland 		g_free(sechdr);
10441d3aed33SMarcel Moolenaar 		tbl = pritbl;
10451d3aed33SMarcel Moolenaar 		g_free(sectbl);
10461d3aed33SMarcel Moolenaar 	}
10471d3aed33SMarcel Moolenaar 
1048*2cbda736SWarner Losh 	/*
1049*2cbda736SWarner Losh 	 * The reserved area preceeds the valid area for partitions. Warn when
1050*2cbda736SWarner Losh 	 * the lba_start doesn't meet the standard's minimum size for the gpt
1051*2cbda736SWarner Losh 	 * entry array. UEFI 2.10 section 5.3 specifies that the LBA must be 32
1052*2cbda736SWarner Losh 	 * (for 512 byte sectors) or 6 (4k sectors) or larger. This is different
1053*2cbda736SWarner Losh 	 * than the number of valid entries in the GPT entry array, which can be
1054*2cbda736SWarner Losh 	 * smaller.
1055*2cbda736SWarner Losh 	 */
1056*2cbda736SWarner Losh 	if (table->hdr->hdr_lba_start < GPT_MIN_RESERVED / pp->sectorsize + 2) {
1057*2cbda736SWarner Losh 		printf("GEOM: warning: %s lba_start %llu < required min %d\n",
1058*2cbda736SWarner Losh 		    pp->name, (unsigned long long)table->hdr->hdr_lba_start,
1059*2cbda736SWarner Losh 		    GPT_MIN_RESERVED / pp->sectorsize + 2);
1060*2cbda736SWarner Losh 	}
1061*2cbda736SWarner Losh 
10621c2dee3cSRobert Noland 	basetable->gpt_first = table->hdr->hdr_lba_start;
10631c2dee3cSRobert Noland 	basetable->gpt_last = table->hdr->hdr_lba_end;
106496937e3bSMarcel Moolenaar 	basetable->gpt_entries = table->hdr->hdr_entries;
10651d3aed33SMarcel Moolenaar 
106696937e3bSMarcel Moolenaar 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
10671d3aed33SMarcel Moolenaar 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
10681d3aed33SMarcel Moolenaar 			continue;
1069e7926a37SAndrey V. Elsukov 		entry = (struct g_part_gpt_entry *)g_part_new_entry(
1070e7926a37SAndrey V. Elsukov 		    basetable, index + 1, tbl[index].ent_lba_start,
1071e7926a37SAndrey V. Elsukov 		    tbl[index].ent_lba_end);
10721d3aed33SMarcel Moolenaar 		entry->ent = tbl[index];
10731d3aed33SMarcel Moolenaar 	}
10741d3aed33SMarcel Moolenaar 
10751d3aed33SMarcel Moolenaar 	g_free(tbl);
1076369fe59dSMarcel Moolenaar 
1077369fe59dSMarcel Moolenaar 	/*
1078369fe59dSMarcel Moolenaar 	 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1079369fe59dSMarcel Moolenaar 	 * if (and only if) any FAT32 or FAT16 partitions have been
1080369fe59dSMarcel Moolenaar 	 * created. This happens irrespective of whether Boot Camp is
1081369fe59dSMarcel Moolenaar 	 * used/enabled, though it's generally understood to be done
1082369fe59dSMarcel Moolenaar 	 * to support legacy Windows under Boot Camp. We refer to this
1083369fe59dSMarcel Moolenaar 	 * mirroring simply as Boot Camp. We try to detect Boot Camp
1084369fe59dSMarcel Moolenaar 	 * so that we can update the MBR if and when GPT changes have
1085369fe59dSMarcel Moolenaar 	 * been made. Note that we do not enable Boot Camp if not
1086369fe59dSMarcel Moolenaar 	 * previously enabled because we can't assume that we're on a
1087369fe59dSMarcel Moolenaar 	 * Mac alongside Mac OS X.
1088369fe59dSMarcel Moolenaar 	 */
1089369fe59dSMarcel Moolenaar 	table->bootcamp = gpt_is_bootcamp(table, pp->name);
1090369fe59dSMarcel Moolenaar 
10911d3aed33SMarcel Moolenaar 	return (0);
10921d3aed33SMarcel Moolenaar }
10931d3aed33SMarcel Moolenaar 
1094cec283baSPawel Jakub Dawidek static int
g_part_gpt_recover(struct g_part_table * basetable)1095e7926a37SAndrey V. Elsukov g_part_gpt_recover(struct g_part_table *basetable)
1096e7926a37SAndrey V. Elsukov {
10973bd22a9cSMarcel Moolenaar 	struct g_part_gpt_table *table;
10983bd22a9cSMarcel Moolenaar 	struct g_provider *pp;
1099e7926a37SAndrey V. Elsukov 
11003bd22a9cSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
11013bd22a9cSMarcel Moolenaar 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
11023bd22a9cSMarcel Moolenaar 	gpt_create_pmbr(table, pp);
11035c9f0f72SWarner Losh 	g_gpt_set_defaults(basetable, pp, NULL);
1104e7926a37SAndrey V. Elsukov 	basetable->gpt_corrupt = 0;
1105e7926a37SAndrey V. Elsukov 	return (0);
1106e7926a37SAndrey V. Elsukov }
1107e7926a37SAndrey V. Elsukov 
1108e7926a37SAndrey V. Elsukov static int
g_part_gpt_setunset(struct g_part_table * basetable,struct g_part_entry * baseentry,const char * attrib,unsigned int set)1109369fe59dSMarcel Moolenaar g_part_gpt_setunset(struct g_part_table *basetable,
1110369fe59dSMarcel Moolenaar     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1111cec283baSPawel Jakub Dawidek {
1112cec283baSPawel Jakub Dawidek 	struct g_part_gpt_entry *entry;
1113369fe59dSMarcel Moolenaar 	struct g_part_gpt_table *table;
1114ce808c7aSAllan Jude 	struct g_provider *pp;
11153bd22a9cSMarcel Moolenaar 	uint8_t *p;
1116369fe59dSMarcel Moolenaar 	uint64_t attr;
1117369fe59dSMarcel Moolenaar 	int i;
1118cec283baSPawel Jakub Dawidek 
1119369fe59dSMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
1120369fe59dSMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
1121369fe59dSMarcel Moolenaar 
1122369fe59dSMarcel Moolenaar 	if (strcasecmp(attrib, "active") == 0) {
11233bd22a9cSMarcel Moolenaar 		if (table->bootcamp) {
11243bd22a9cSMarcel Moolenaar 			/* The active flag must be set on a valid entry. */
11253bd22a9cSMarcel Moolenaar 			if (entry == NULL)
11263bd22a9cSMarcel Moolenaar 				return (ENXIO);
11273bd22a9cSMarcel Moolenaar 			if (baseentry->gpe_index > NDOSPART)
1128369fe59dSMarcel Moolenaar 				return (EINVAL);
1129369fe59dSMarcel Moolenaar 			for (i = 0; i < NDOSPART; i++) {
11303bd22a9cSMarcel Moolenaar 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
11313bd22a9cSMarcel Moolenaar 				p[0] = (i == baseentry->gpe_index - 1)
11323bd22a9cSMarcel Moolenaar 				    ? ((set) ? 0x80 : 0) : 0;
11333bd22a9cSMarcel Moolenaar 			}
11343bd22a9cSMarcel Moolenaar 		} else {
11353bd22a9cSMarcel Moolenaar 			/* The PMBR is marked as active without an entry. */
11363bd22a9cSMarcel Moolenaar 			if (entry != NULL)
11373bd22a9cSMarcel Moolenaar 				return (ENXIO);
11383bd22a9cSMarcel Moolenaar 			for (i = 0; i < NDOSPART; i++) {
11393bd22a9cSMarcel Moolenaar 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
11403bd22a9cSMarcel Moolenaar 				p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
11413bd22a9cSMarcel Moolenaar 			}
1142369fe59dSMarcel Moolenaar 		}
1143369fe59dSMarcel Moolenaar 		return (0);
1144ce808c7aSAllan Jude 	} else if (strcasecmp(attrib, "lenovofix") == 0) {
1145ce808c7aSAllan Jude 		/*
1146ce808c7aSAllan Jude 		 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1147ce808c7aSAllan Jude 		 * This workaround allows Lenovo X220, T420, T520, etc to boot
1148ce808c7aSAllan Jude 		 * from GPT Partitions in BIOS mode.
1149ce808c7aSAllan Jude 		 */
1150ce808c7aSAllan Jude 
1151ce808c7aSAllan Jude 		if (entry != NULL)
1152ce808c7aSAllan Jude 			return (ENXIO);
1153ce808c7aSAllan Jude 
1154ce808c7aSAllan Jude 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1155ce808c7aSAllan Jude 		bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1156ce808c7aSAllan Jude 		gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1157ce808c7aSAllan Jude 		    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1158ce808c7aSAllan Jude 		return (0);
1159369fe59dSMarcel Moolenaar 	}
1160369fe59dSMarcel Moolenaar 
11613bd22a9cSMarcel Moolenaar 	if (entry == NULL)
11623bd22a9cSMarcel Moolenaar 		return (ENODEV);
11633bd22a9cSMarcel Moolenaar 
1164369fe59dSMarcel Moolenaar 	attr = 0;
1165cec283baSPawel Jakub Dawidek 	if (strcasecmp(attrib, "bootme") == 0) {
1166369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTME;
1167cec283baSPawel Jakub Dawidek 	} else if (strcasecmp(attrib, "bootonce") == 0) {
1168369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTONCE;
1169cec283baSPawel Jakub Dawidek 		if (set)
1170369fe59dSMarcel Moolenaar 			attr |= GPT_ENT_ATTR_BOOTME;
1171cec283baSPawel Jakub Dawidek 	} else if (strcasecmp(attrib, "bootfailed") == 0) {
1172cec283baSPawel Jakub Dawidek 		/*
1173cec283baSPawel Jakub Dawidek 		 * It should only be possible to unset BOOTFAILED, but it might
1174cec283baSPawel Jakub Dawidek 		 * be useful for test purposes to also be able to set it.
1175cec283baSPawel Jakub Dawidek 		 */
1176369fe59dSMarcel Moolenaar 		attr |= GPT_ENT_ATTR_BOOTFAILED;
1177cec283baSPawel Jakub Dawidek 	}
1178369fe59dSMarcel Moolenaar 	if (attr == 0)
1179cec283baSPawel Jakub Dawidek 		return (EINVAL);
1180cec283baSPawel Jakub Dawidek 
1181369fe59dSMarcel Moolenaar 	if (set)
1182369fe59dSMarcel Moolenaar 		attr = entry->ent.ent_attr | attr;
1183369fe59dSMarcel Moolenaar 	else
1184369fe59dSMarcel Moolenaar 		attr = entry->ent.ent_attr & ~attr;
1185369fe59dSMarcel Moolenaar 	if (attr != entry->ent.ent_attr) {
1186369fe59dSMarcel Moolenaar 		entry->ent.ent_attr = attr;
1187369fe59dSMarcel Moolenaar 		if (!baseentry->gpe_created)
1188369fe59dSMarcel Moolenaar 			baseentry->gpe_modified = 1;
1189cec283baSPawel Jakub Dawidek 	}
1190cec283baSPawel Jakub Dawidek 	return (0);
1191cec283baSPawel Jakub Dawidek }
1192cec283baSPawel Jakub Dawidek 
11931d3aed33SMarcel Moolenaar static const char *
g_part_gpt_type(struct g_part_table * basetable,struct g_part_entry * baseentry,char * buf,size_t bufsz)11941d3aed33SMarcel Moolenaar g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
11951d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
11961d3aed33SMarcel Moolenaar {
11971d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
11981d3aed33SMarcel Moolenaar 	struct uuid *type;
1199f1317430SRui Paulo 	struct g_part_uuid_alias *uap;
12001d3aed33SMarcel Moolenaar 
12011d3aed33SMarcel Moolenaar 	entry = (struct g_part_gpt_entry *)baseentry;
12021d3aed33SMarcel Moolenaar 	type = &entry->ent.ent_type;
1203f1317430SRui Paulo 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1204f1317430SRui Paulo 		if (EQUUID(type, uap->uuid))
1205f1317430SRui Paulo 			return (g_part_alias_name(uap->alias));
1206cf231470SMarcel Moolenaar 	buf[0] = '!';
1207cf231470SMarcel Moolenaar 	snprintf_uuid(buf + 1, bufsz - 1, type);
1208f1317430SRui Paulo 
12091d3aed33SMarcel Moolenaar 	return (buf);
12101d3aed33SMarcel Moolenaar }
12111d3aed33SMarcel Moolenaar 
12121d3aed33SMarcel Moolenaar static int
g_part_gpt_write(struct g_part_table * basetable,struct g_consumer * cp)12131d3aed33SMarcel Moolenaar g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
12141d3aed33SMarcel Moolenaar {
12151d3aed33SMarcel Moolenaar 	unsigned char *buf, *bp;
12161d3aed33SMarcel Moolenaar 	struct g_provider *pp;
12171d3aed33SMarcel Moolenaar 	struct g_part_entry *baseentry;
12181d3aed33SMarcel Moolenaar 	struct g_part_gpt_entry *entry;
12191d3aed33SMarcel Moolenaar 	struct g_part_gpt_table *table;
1220e7926a37SAndrey V. Elsukov 	size_t tblsz;
12211d3aed33SMarcel Moolenaar 	uint32_t crc;
12221d3aed33SMarcel Moolenaar 	int error, index;
12231d3aed33SMarcel Moolenaar 
12241d3aed33SMarcel Moolenaar 	pp = cp->provider;
12251d3aed33SMarcel Moolenaar 	table = (struct g_part_gpt_table *)basetable;
122655e0987aSPedro F. Giffuni 	tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
122755e0987aSPedro F. Giffuni 	    pp->sectorsize);
12281d3aed33SMarcel Moolenaar 
1229369fe59dSMarcel Moolenaar 	/* Reconstruct the MBR from the GPT if under Boot Camp. */
1230369fe59dSMarcel Moolenaar 	if (table->bootcamp)
12313bd22a9cSMarcel Moolenaar 		gpt_update_bootcamp(basetable, pp);
1232d4746e10SAndrey V. Elsukov 
12334d32fcb4SMarcel Moolenaar 	/* Write the PMBR */
12341d3aed33SMarcel Moolenaar 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
12354d32fcb4SMarcel Moolenaar 	bcopy(table->mbr, buf, MBRSIZE);
12361d3aed33SMarcel Moolenaar 	error = g_write_data(cp, 0, buf, pp->sectorsize);
12371d3aed33SMarcel Moolenaar 	g_free(buf);
12381d3aed33SMarcel Moolenaar 	if (error)
12391d3aed33SMarcel Moolenaar 		return (error);
12401d3aed33SMarcel Moolenaar 
12411d3aed33SMarcel Moolenaar 	/* Allocate space for the header and entries. */
1242e7926a37SAndrey V. Elsukov 	buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
12431d3aed33SMarcel Moolenaar 
12441c2dee3cSRobert Noland 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
12451c2dee3cSRobert Noland 	le32enc(buf + 8, table->hdr->hdr_revision);
12461c2dee3cSRobert Noland 	le32enc(buf + 12, table->hdr->hdr_size);
12471c2dee3cSRobert Noland 	le64enc(buf + 40, table->hdr->hdr_lba_start);
12481c2dee3cSRobert Noland 	le64enc(buf + 48, table->hdr->hdr_lba_end);
12491c2dee3cSRobert Noland 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
12501c2dee3cSRobert Noland 	le32enc(buf + 80, table->hdr->hdr_entries);
12511c2dee3cSRobert Noland 	le32enc(buf + 84, table->hdr->hdr_entsz);
12521d3aed33SMarcel Moolenaar 
12531d3aed33SMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1254d287f590SMarcel Moolenaar 		if (baseentry->gpe_deleted)
1255d287f590SMarcel Moolenaar 			continue;
12561d3aed33SMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
12571d3aed33SMarcel Moolenaar 		index = baseentry->gpe_index - 1;
12581c2dee3cSRobert Noland 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
12591d3aed33SMarcel Moolenaar 		le_uuid_enc(bp, &entry->ent.ent_type);
12601d3aed33SMarcel Moolenaar 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
12611d3aed33SMarcel Moolenaar 		le64enc(bp + 32, entry->ent.ent_lba_start);
12621d3aed33SMarcel Moolenaar 		le64enc(bp + 40, entry->ent.ent_lba_end);
12631d3aed33SMarcel Moolenaar 		le64enc(bp + 48, entry->ent.ent_attr);
12641d3aed33SMarcel Moolenaar 		memcpy(bp + 56, entry->ent.ent_name,
12651d3aed33SMarcel Moolenaar 		    sizeof(entry->ent.ent_name));
12661d3aed33SMarcel Moolenaar 	}
12671d3aed33SMarcel Moolenaar 
12681d3aed33SMarcel Moolenaar 	crc = crc32(buf + pp->sectorsize,
12691c2dee3cSRobert Noland 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
12701d3aed33SMarcel Moolenaar 	le32enc(buf + 88, crc);
12711d3aed33SMarcel Moolenaar 
12721d3aed33SMarcel Moolenaar 	/* Write primary meta-data. */
12731d3aed33SMarcel Moolenaar 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
12741d3aed33SMarcel Moolenaar 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
12751d3aed33SMarcel Moolenaar 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
12761d3aed33SMarcel Moolenaar 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
12771c2dee3cSRobert Noland 	crc = crc32(buf, table->hdr->hdr_size);
12781d3aed33SMarcel Moolenaar 	le32enc(buf + 16, crc);
12791d3aed33SMarcel Moolenaar 
1280cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1281799eac8cSAndrey V. Elsukov 		error = g_write_data(cp,
1282799eac8cSAndrey V. Elsukov 		    (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1283799eac8cSAndrey V. Elsukov 		    buf + (index + 1) * pp->sectorsize,
1284cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1285799eac8cSAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
12861d3aed33SMarcel Moolenaar 		if (error)
12871d3aed33SMarcel Moolenaar 			goto out;
1288799eac8cSAndrey V. Elsukov 	}
12891d3aed33SMarcel Moolenaar 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
12901d3aed33SMarcel Moolenaar 	    buf, pp->sectorsize);
12911d3aed33SMarcel Moolenaar 	if (error)
12921d3aed33SMarcel Moolenaar 		goto out;
12931d3aed33SMarcel Moolenaar 
12941d3aed33SMarcel Moolenaar 	/* Write secondary meta-data. */
12951d3aed33SMarcel Moolenaar 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
12961d3aed33SMarcel Moolenaar 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
12971d3aed33SMarcel Moolenaar 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
12981d3aed33SMarcel Moolenaar 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
12991c2dee3cSRobert Noland 	crc = crc32(buf, table->hdr->hdr_size);
13001d3aed33SMarcel Moolenaar 	le32enc(buf + 16, crc);
13011d3aed33SMarcel Moolenaar 
1302cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1303799eac8cSAndrey V. Elsukov 		error = g_write_data(cp,
1304799eac8cSAndrey V. Elsukov 		    (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1305799eac8cSAndrey V. Elsukov 		    buf + (index + 1) * pp->sectorsize,
1306cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1307799eac8cSAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
13081d3aed33SMarcel Moolenaar 		if (error)
13091d3aed33SMarcel Moolenaar 			goto out;
1310799eac8cSAndrey V. Elsukov 	}
13111d3aed33SMarcel Moolenaar 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
13121d3aed33SMarcel Moolenaar 	    buf, pp->sectorsize);
13131d3aed33SMarcel Moolenaar 
13141d3aed33SMarcel Moolenaar  out:
13151d3aed33SMarcel Moolenaar 	g_free(buf);
13161d3aed33SMarcel Moolenaar 	return (error);
13171d3aed33SMarcel Moolenaar }
13181d3aed33SMarcel Moolenaar 
13191d3aed33SMarcel Moolenaar static void
g_gpt_set_defaults(struct g_part_table * basetable,struct g_provider * pp,struct g_part_parms * gpp)13205c9f0f72SWarner Losh g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp,
13215c9f0f72SWarner Losh 	struct g_part_parms *gpp)
13222b9be055SAndrey V. Elsukov {
13234787115dSMarcel Moolenaar 	struct g_part_entry *baseentry;
13244787115dSMarcel Moolenaar 	struct g_part_gpt_entry *entry;
13252b9be055SAndrey V. Elsukov 	struct g_part_gpt_table *table;
13264787115dSMarcel Moolenaar 	quad_t start, end, min, max;
13274787115dSMarcel Moolenaar 	quad_t lba, last;
13284787115dSMarcel Moolenaar 	size_t spb, tblsz;
13292b9be055SAndrey V. Elsukov 
13302b9be055SAndrey V. Elsukov 	table = (struct g_part_gpt_table *)basetable;
13312b9be055SAndrey V. Elsukov 	last = pp->mediasize / pp->sectorsize - 1;
133255e0987aSPedro F. Giffuni 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
133355e0987aSPedro F. Giffuni 	    pp->sectorsize);
13342b9be055SAndrey V. Elsukov 
13352b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_PRIHDR] = 1;
13362b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_PRITBL] = 2;
13372b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_SECHDR] = last;
13382b9be055SAndrey V. Elsukov 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
13392b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
13402b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
13412b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
13422b9be055SAndrey V. Elsukov 	table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
13432b9be055SAndrey V. Elsukov 
13444787115dSMarcel Moolenaar 	max = start = 2 + tblsz;
13454787115dSMarcel Moolenaar 	min = end = last - tblsz - 1;
13464787115dSMarcel Moolenaar 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
13474787115dSMarcel Moolenaar 		if (baseentry->gpe_deleted)
13484787115dSMarcel Moolenaar 			continue;
13494787115dSMarcel Moolenaar 		entry = (struct g_part_gpt_entry *)baseentry;
13504787115dSMarcel Moolenaar 		if (entry->ent.ent_lba_start < min)
13514787115dSMarcel Moolenaar 			min = entry->ent.ent_lba_start;
13524787115dSMarcel Moolenaar 		if (entry->ent.ent_lba_end > max)
13534787115dSMarcel Moolenaar 			max = entry->ent.ent_lba_end;
13544787115dSMarcel Moolenaar 	}
13555c9f0f72SWarner Losh 	/*
13565c9f0f72SWarner Losh 	 * Don't force alignment of any kind whatsoever on resize, restore or
13575c9f0f72SWarner Losh 	 * recover. resize doesn't go through this path, recover has a NULL gpp
13585c9f0f72SWarner Losh 	 * and restore has flags == restore (maybe with an appended 'C' to
13595c9f0f72SWarner Losh 	 * commit the operation). For these operations, we have to trust the
13605c9f0f72SWarner Losh 	 * user knows what they are doing.
13615c9f0f72SWarner Losh 	 *
13625c9f0f72SWarner Losh 	 * Otherwise it some flavor of creation of a new partition, so we align
13635c9f0f72SWarner Losh 	 * to a 4k offset on the drive, to make 512e/4kn drives more performant
13645c9f0f72SWarner Losh 	 * by default.
13655c9f0f72SWarner Losh 	 */
13665c9f0f72SWarner Losh 	if (gpp == NULL ||
13675c9f0f72SWarner Losh 	    (gpp->gpp_parms & G_PART_PARM_FLAGS) == 0 ||
13685c9f0f72SWarner Losh 	    strstr(gpp->gpp_flags, "restore") == NULL) {
13694787115dSMarcel Moolenaar 		spb = 4096 / pp->sectorsize;
13704787115dSMarcel Moolenaar 		if (spb > 1) {
13714787115dSMarcel Moolenaar 			lba = start + ((start % spb) ? spb - start % spb : 0);
13724787115dSMarcel Moolenaar 			if (lba <= min)
13734787115dSMarcel Moolenaar 				start = lba;
13744787115dSMarcel Moolenaar 			lba = end - (end + 1) % spb;
13754787115dSMarcel Moolenaar 			if (max <= lba)
13764787115dSMarcel Moolenaar 				end = lba;
13774787115dSMarcel Moolenaar 		}
13785c9f0f72SWarner Losh 	}
13794787115dSMarcel Moolenaar 	table->hdr->hdr_lba_start = start;
13804787115dSMarcel Moolenaar 	table->hdr->hdr_lba_end = end;
13812b9be055SAndrey V. Elsukov 
13824787115dSMarcel Moolenaar 	basetable->gpt_first = start;
13834787115dSMarcel Moolenaar 	basetable->gpt_last = end;
13842b9be055SAndrey V. Elsukov }
13852b9be055SAndrey V. Elsukov 
13862b9be055SAndrey V. Elsukov static void
g_gpt_printf_utf16(struct sbuf * sb,uint16_t * str,size_t len)1387d3532631SMarcel Moolenaar g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
13881d3aed33SMarcel Moolenaar {
13891d3aed33SMarcel Moolenaar 	u_int bo;
13901d3aed33SMarcel Moolenaar 	uint32_t ch;
13911d3aed33SMarcel Moolenaar 	uint16_t c;
13921d3aed33SMarcel Moolenaar 
1393d3532631SMarcel Moolenaar 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
13941d3aed33SMarcel Moolenaar 	while (len > 0 && *str != 0) {
13951d3aed33SMarcel Moolenaar 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
13961d3aed33SMarcel Moolenaar 		str++, len--;
13971d3aed33SMarcel Moolenaar 		if ((ch & 0xf800) == 0xd800) {
13981d3aed33SMarcel Moolenaar 			if (len > 0) {
13991d3aed33SMarcel Moolenaar 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
14001d3aed33SMarcel Moolenaar 				    : le16toh(*str);
14011d3aed33SMarcel Moolenaar 				str++, len--;
14021d3aed33SMarcel Moolenaar 			} else
14031d3aed33SMarcel Moolenaar 				c = 0xfffd;
14041d3aed33SMarcel Moolenaar 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
14051d3aed33SMarcel Moolenaar 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
14061d3aed33SMarcel Moolenaar 				ch += 0x10000;
14071d3aed33SMarcel Moolenaar 			} else
14081d3aed33SMarcel Moolenaar 				ch = 0xfffd;
14091d3aed33SMarcel Moolenaar 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
14101d3aed33SMarcel Moolenaar 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
14111d3aed33SMarcel Moolenaar 			continue;
14121d3aed33SMarcel Moolenaar 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
14131d3aed33SMarcel Moolenaar 			continue;
14141d3aed33SMarcel Moolenaar 
1415d3532631SMarcel Moolenaar 		/* Write the Unicode character in UTF-8 */
14161d3aed33SMarcel Moolenaar 		if (ch < 0x80)
141739dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c", ch);
14181d3aed33SMarcel Moolenaar 		else if (ch < 0x800)
141939dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
14201d3aed33SMarcel Moolenaar 			    0x80 | (ch & 0x3f));
14211d3aed33SMarcel Moolenaar 		else if (ch < 0x10000)
142239dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
14231d3aed33SMarcel Moolenaar 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
14241d3aed33SMarcel Moolenaar 		else if (ch < 0x200000)
142539dcac84SAndrey V. Elsukov 			g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
142639dcac84SAndrey V. Elsukov 			    (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
14271d3aed33SMarcel Moolenaar 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
14281d3aed33SMarcel Moolenaar 	}
14291d3aed33SMarcel Moolenaar }
1430d3532631SMarcel Moolenaar 
1431d3532631SMarcel Moolenaar static void
g_gpt_utf8_to_utf16(const uint8_t * s8,uint16_t * s16,size_t s16len)1432d3532631SMarcel Moolenaar g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1433d3532631SMarcel Moolenaar {
1434d3532631SMarcel Moolenaar 	size_t s16idx, s8idx;
1435d3532631SMarcel Moolenaar 	uint32_t utfchar;
1436d3532631SMarcel Moolenaar 	unsigned int c, utfbytes;
1437d3532631SMarcel Moolenaar 
1438d3532631SMarcel Moolenaar 	s8idx = s16idx = 0;
1439d3532631SMarcel Moolenaar 	utfchar = 0;
1440d3532631SMarcel Moolenaar 	utfbytes = 0;
1441d3532631SMarcel Moolenaar 	bzero(s16, s16len << 1);
1442d3532631SMarcel Moolenaar 	while (s8[s8idx] != 0 && s16idx < s16len) {
1443d3532631SMarcel Moolenaar 		c = s8[s8idx++];
1444d3532631SMarcel Moolenaar 		if ((c & 0xc0) != 0x80) {
1445d3532631SMarcel Moolenaar 			/* Initial characters. */
1446d3532631SMarcel Moolenaar 			if (utfbytes != 0) {
1447d3532631SMarcel Moolenaar 				/* Incomplete encoding of previous char. */
1448d3532631SMarcel Moolenaar 				s16[s16idx++] = htole16(0xfffd);
1449d3532631SMarcel Moolenaar 			}
1450d3532631SMarcel Moolenaar 			if ((c & 0xf8) == 0xf0) {
1451d3532631SMarcel Moolenaar 				utfchar = c & 0x07;
1452d3532631SMarcel Moolenaar 				utfbytes = 3;
1453d3532631SMarcel Moolenaar 			} else if ((c & 0xf0) == 0xe0) {
1454d3532631SMarcel Moolenaar 				utfchar = c & 0x0f;
1455d3532631SMarcel Moolenaar 				utfbytes = 2;
1456d3532631SMarcel Moolenaar 			} else if ((c & 0xe0) == 0xc0) {
1457d3532631SMarcel Moolenaar 				utfchar = c & 0x1f;
1458d3532631SMarcel Moolenaar 				utfbytes = 1;
1459d3532631SMarcel Moolenaar 			} else {
1460d3532631SMarcel Moolenaar 				utfchar = c & 0x7f;
1461d3532631SMarcel Moolenaar 				utfbytes = 0;
1462d3532631SMarcel Moolenaar 			}
1463d3532631SMarcel Moolenaar 		} else {
1464d3532631SMarcel Moolenaar 			/* Followup characters. */
1465d3532631SMarcel Moolenaar 			if (utfbytes > 0) {
1466d3532631SMarcel Moolenaar 				utfchar = (utfchar << 6) + (c & 0x3f);
1467d3532631SMarcel Moolenaar 				utfbytes--;
1468d3532631SMarcel Moolenaar 			} else if (utfbytes == 0)
1469d3532631SMarcel Moolenaar 				utfbytes = ~0;
1470d3532631SMarcel Moolenaar 		}
1471d3532631SMarcel Moolenaar 		/*
1472d3532631SMarcel Moolenaar 		 * Write the complete Unicode character as UTF-16 when we
1473d3532631SMarcel Moolenaar 		 * have all the UTF-8 charactars collected.
1474d3532631SMarcel Moolenaar 		 */
1475d3532631SMarcel Moolenaar 		if (utfbytes == 0) {
1476d3532631SMarcel Moolenaar 			/*
1477d3532631SMarcel Moolenaar 			 * If we need to write 2 UTF-16 characters, but
1478d3532631SMarcel Moolenaar 			 * we only have room for 1, then we truncate the
1479d3532631SMarcel Moolenaar 			 * string by writing a 0 instead.
1480d3532631SMarcel Moolenaar 			 */
1481d3532631SMarcel Moolenaar 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1482d3532631SMarcel Moolenaar 				s16[s16idx++] =
1483d3532631SMarcel Moolenaar 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
1484d3532631SMarcel Moolenaar 				s16[s16idx++] =
1485d3532631SMarcel Moolenaar 				    htole16(0xdc00 | (utfchar & 0x3ff));
1486d3532631SMarcel Moolenaar 			} else
1487d3532631SMarcel Moolenaar 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1488d3532631SMarcel Moolenaar 				    htole16(utfchar);
1489d3532631SMarcel Moolenaar 		}
1490d3532631SMarcel Moolenaar 	}
1491d3532631SMarcel Moolenaar 	/*
1492d3532631SMarcel Moolenaar 	 * If our input string was truncated, append an invalid encoding
1493d3532631SMarcel Moolenaar 	 * character to the output string.
1494d3532631SMarcel Moolenaar 	 */
1495d3532631SMarcel Moolenaar 	if (utfbytes != 0 && s16idx < s16len)
1496d3532631SMarcel Moolenaar 		s16[s16idx++] = htole16(0xfffd);
1497d3532631SMarcel Moolenaar }
1498