xref: /freebsd/sys/geom/part/g_part_gpt.c (revision dab8138e13dea539a387c458979403980a137bf2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bio.h>
31 #include <sys/diskmbr.h>
32 #include <sys/gsb_crc32.h>
33 #include <sys/endian.h>
34 #include <sys/gpt.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/sbuf.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/uuid.h>
46 #include <geom/geom.h>
47 #include <geom/geom_int.h>
48 #include <geom/part/g_part.h>
49 
50 #include "g_part_if.h"
51 
52 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
53 
54 SYSCTL_DECL(_kern_geom_part);
55 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt,
56     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
57     "GEOM_PART_GPT GUID Partition Table");
58 
59 static u_int allow_nesting = 0;
60 SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting,
61     CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes");
62 
63 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
64 CTASSERT(sizeof(struct gpt_ent) == 128);
65 
66 extern u_int geom_part_check_integrity;
67 
68 #define	EQUUID(a,b)	(memcmp(a, b, sizeof(struct uuid)) == 0)
69 
70 #define	MBRSIZE		512
71 
72 enum gpt_elt {
73 	GPT_ELT_PRIHDR,
74 	GPT_ELT_PRITBL,
75 	GPT_ELT_SECHDR,
76 	GPT_ELT_SECTBL,
77 	GPT_ELT_COUNT
78 };
79 
80 enum gpt_state {
81 	GPT_STATE_UNKNOWN,	/* Not determined. */
82 	GPT_STATE_MISSING,	/* No signature found. */
83 	GPT_STATE_CORRUPT,	/* Checksum mismatch. */
84 	GPT_STATE_INVALID,	/* Nonconformant/invalid. */
85 	GPT_STATE_UNSUPPORTED,  /* Not supported. */
86 	GPT_STATE_OK		/* Perfectly fine. */
87 };
88 
89 struct g_part_gpt_table {
90 	struct g_part_table	base;
91 	u_char			mbr[MBRSIZE];
92 	struct gpt_hdr		*hdr;
93 	quad_t			lba[GPT_ELT_COUNT];
94 	enum gpt_state		state[GPT_ELT_COUNT];
95 	int			bootcamp;
96 };
97 
98 struct g_part_gpt_entry {
99 	struct g_part_entry	base;
100 	struct gpt_ent		ent;
101 };
102 
103 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
104 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
105 static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *,
106     struct g_part_parms *);
107 
108 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
109     struct g_part_parms *);
110 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
111 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
112 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
113 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
114     struct sbuf *, const char *);
115 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
116 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
117     struct g_part_parms *);
118 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
119     char *, size_t);
120 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
121 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
122 static int g_part_gpt_setunset(struct g_part_table *table,
123     struct g_part_entry *baseentry, const char *attrib, unsigned int set);
124 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
125     char *, size_t);
126 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
127 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
128     struct g_part_parms *);
129 static int g_part_gpt_recover(struct g_part_table *);
130 
131 static kobj_method_t g_part_gpt_methods[] = {
132 	KOBJMETHOD(g_part_add,		g_part_gpt_add),
133 	KOBJMETHOD(g_part_bootcode,	g_part_gpt_bootcode),
134 	KOBJMETHOD(g_part_create,	g_part_gpt_create),
135 	KOBJMETHOD(g_part_destroy,	g_part_gpt_destroy),
136 	KOBJMETHOD(g_part_dumpconf,	g_part_gpt_dumpconf),
137 	KOBJMETHOD(g_part_dumpto,	g_part_gpt_dumpto),
138 	KOBJMETHOD(g_part_modify,	g_part_gpt_modify),
139 	KOBJMETHOD(g_part_resize,	g_part_gpt_resize),
140 	KOBJMETHOD(g_part_name,		g_part_gpt_name),
141 	KOBJMETHOD(g_part_probe,	g_part_gpt_probe),
142 	KOBJMETHOD(g_part_read,		g_part_gpt_read),
143 	KOBJMETHOD(g_part_recover,	g_part_gpt_recover),
144 	KOBJMETHOD(g_part_setunset,	g_part_gpt_setunset),
145 	KOBJMETHOD(g_part_type,		g_part_gpt_type),
146 	KOBJMETHOD(g_part_write,	g_part_gpt_write),
147 	{ 0, 0 }
148 };
149 
150 #define MAXENTSIZE 1024
151 
152 static struct g_part_scheme g_part_gpt_scheme = {
153 	"GPT",
154 	g_part_gpt_methods,
155 	sizeof(struct g_part_gpt_table),
156 	.gps_entrysz = sizeof(struct g_part_gpt_entry),
157 	.gps_minent = 1,
158 	.gps_defent = 128,
159 	.gps_maxent = 4096,
160 	.gps_bootcodesz = MBRSIZE,
161 };
162 G_PART_SCHEME_DECLARE(g_part_gpt);
163 MODULE_VERSION(geom_part_gpt, 0);
164 
165 static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
166 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
167 static struct uuid gpt_uuid_apple_core_storage =
168     GPT_ENT_TYPE_APPLE_CORE_STORAGE;
169 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
170 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
171 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
172 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
173 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
174 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
175 static struct uuid gpt_uuid_apple_zfs = GPT_ENT_TYPE_APPLE_ZFS;
176 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
177 static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
178 static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
179 static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
180 static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT;
181 static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD;
182 static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER;
183 static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2;
184 static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32;
185 static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64;
186 static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY;
187 static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP;
188 static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1;
189 static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM;
190 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
191 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
192 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
193 static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
194 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
195 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
196 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
197 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
198 static struct uuid gpt_uuid_hifive_fsbl = GPT_ENT_TYPE_HIFIVE_FSBL;
199 static struct uuid gpt_uuid_hifive_bbl = GPT_ENT_TYPE_HIFIVE_BBL;
200 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
201 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
202 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
203 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
204 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
205 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
206 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
207 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
208 static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY;
209 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
210 static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES;
211 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
212 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
213 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
214 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
215 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
216 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
217 static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA;
218 static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT;
219 static struct uuid gpt_uuid_solaris_boot = GPT_ENT_TYPE_SOLARIS_BOOT;
220 static struct uuid gpt_uuid_solaris_root = GPT_ENT_TYPE_SOLARIS_ROOT;
221 static struct uuid gpt_uuid_solaris_swap = GPT_ENT_TYPE_SOLARIS_SWAP;
222 static struct uuid gpt_uuid_solaris_backup = GPT_ENT_TYPE_SOLARIS_BACKUP;
223 static struct uuid gpt_uuid_solaris_var = GPT_ENT_TYPE_SOLARIS_VAR;
224 static struct uuid gpt_uuid_solaris_home = GPT_ENT_TYPE_SOLARIS_HOME;
225 static struct uuid gpt_uuid_solaris_altsec = GPT_ENT_TYPE_SOLARIS_ALTSEC;
226 static struct uuid gpt_uuid_solaris_reserved = GPT_ENT_TYPE_SOLARIS_RESERVED;
227 static struct uuid gpt_uuid_u_boot_env = GPT_ENT_TYPE_U_BOOT_ENV;
228 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
229 static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
230 static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
231 static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
232 static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR;
233 static struct uuid gpt_uuid_xbootldr = GPT_ENT_TYPE_XBOOTLDR;
234 
235 static struct g_part_uuid_alias {
236 	struct uuid *uuid;
237 	int alias;
238 	int mbrtype;
239 } gpt_uuid_alias_match[] = {
240 	{ &gpt_uuid_apple_apfs,		G_PART_ALIAS_APPLE_APFS,	 0 },
241 	{ &gpt_uuid_apple_boot,		G_PART_ALIAS_APPLE_BOOT,	 0xab },
242 	{ &gpt_uuid_apple_core_storage,	G_PART_ALIAS_APPLE_CORE_STORAGE, 0 },
243 	{ &gpt_uuid_apple_hfs,		G_PART_ALIAS_APPLE_HFS,		 0xaf },
244 	{ &gpt_uuid_apple_label,	G_PART_ALIAS_APPLE_LABEL,	 0 },
245 	{ &gpt_uuid_apple_raid,		G_PART_ALIAS_APPLE_RAID,	 0 },
246 	{ &gpt_uuid_apple_raid_offline,	G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
247 	{ &gpt_uuid_apple_tv_recovery,	G_PART_ALIAS_APPLE_TV_RECOVERY,	 0 },
248 	{ &gpt_uuid_apple_ufs,		G_PART_ALIAS_APPLE_UFS,		 0 },
249 	{ &gpt_uuid_apple_zfs,		G_PART_ALIAS_APPLE_ZFS,		 0 },
250 	{ &gpt_uuid_bios_boot,		G_PART_ALIAS_BIOS_BOOT,		 0 },
251 	{ &gpt_uuid_chromeos_firmware,	G_PART_ALIAS_CHROMEOS_FIRMWARE,	 0 },
252 	{ &gpt_uuid_chromeos_kernel,	G_PART_ALIAS_CHROMEOS_KERNEL,	 0 },
253 	{ &gpt_uuid_chromeos_reserved,	G_PART_ALIAS_CHROMEOS_RESERVED,	 0 },
254 	{ &gpt_uuid_chromeos_root,	G_PART_ALIAS_CHROMEOS_ROOT,	 0 },
255 	{ &gpt_uuid_dfbsd_ccd,		G_PART_ALIAS_DFBSD_CCD,		 0 },
256 	{ &gpt_uuid_dfbsd_hammer,	G_PART_ALIAS_DFBSD_HAMMER,	 0 },
257 	{ &gpt_uuid_dfbsd_hammer2,	G_PART_ALIAS_DFBSD_HAMMER2,	 0 },
258 	{ &gpt_uuid_dfbsd_label32,	G_PART_ALIAS_DFBSD,		 0xa5 },
259 	{ &gpt_uuid_dfbsd_label64,	G_PART_ALIAS_DFBSD64,		 0xa5 },
260 	{ &gpt_uuid_dfbsd_legacy,	G_PART_ALIAS_DFBSD_LEGACY,	 0 },
261 	{ &gpt_uuid_dfbsd_swap,		G_PART_ALIAS_DFBSD_SWAP,	 0 },
262 	{ &gpt_uuid_dfbsd_ufs1,		G_PART_ALIAS_DFBSD_UFS,		 0 },
263 	{ &gpt_uuid_dfbsd_vinum,	G_PART_ALIAS_DFBSD_VINUM,	 0 },
264 	{ &gpt_uuid_efi, 		G_PART_ALIAS_EFI,		 0xee },
265 	{ &gpt_uuid_freebsd,		G_PART_ALIAS_FREEBSD,		 0xa5 },
266 	{ &gpt_uuid_freebsd_boot, 	G_PART_ALIAS_FREEBSD_BOOT,	 0 },
267 	{ &gpt_uuid_freebsd_nandfs, 	G_PART_ALIAS_FREEBSD_NANDFS,	 0 },
268 	{ &gpt_uuid_freebsd_swap,	G_PART_ALIAS_FREEBSD_SWAP,	 0 },
269 	{ &gpt_uuid_freebsd_ufs,	G_PART_ALIAS_FREEBSD_UFS,	 0 },
270 	{ &gpt_uuid_freebsd_vinum,	G_PART_ALIAS_FREEBSD_VINUM,	 0 },
271 	{ &gpt_uuid_freebsd_zfs,	G_PART_ALIAS_FREEBSD_ZFS,	 0 },
272 	{ &gpt_uuid_hifive_fsbl,	G_PART_ALIAS_HIFIVE_FSBL,	 0 },
273 	{ &gpt_uuid_hifive_bbl,		G_PART_ALIAS_HIFIVE_BBL,	 0 },
274 	{ &gpt_uuid_linux_data,		G_PART_ALIAS_LINUX_DATA,	 0x0b },
275 	{ &gpt_uuid_linux_lvm,		G_PART_ALIAS_LINUX_LVM,		 0 },
276 	{ &gpt_uuid_linux_raid,		G_PART_ALIAS_LINUX_RAID,	 0 },
277 	{ &gpt_uuid_linux_swap,		G_PART_ALIAS_LINUX_SWAP,	 0 },
278 	{ &gpt_uuid_mbr,		G_PART_ALIAS_MBR,		 0 },
279 	{ &gpt_uuid_ms_basic_data,	G_PART_ALIAS_MS_BASIC_DATA,	 0x0b },
280 	{ &gpt_uuid_ms_ldm_data,	G_PART_ALIAS_MS_LDM_DATA,	 0 },
281 	{ &gpt_uuid_ms_ldm_metadata,	G_PART_ALIAS_MS_LDM_METADATA,	 0 },
282 	{ &gpt_uuid_ms_recovery,	G_PART_ALIAS_MS_RECOVERY,	 0 },
283 	{ &gpt_uuid_ms_reserved,	G_PART_ALIAS_MS_RESERVED,	 0 },
284 	{ &gpt_uuid_ms_spaces,		G_PART_ALIAS_MS_SPACES,		 0 },
285 	{ &gpt_uuid_netbsd_ccd,		G_PART_ALIAS_NETBSD_CCD,	 0 },
286 	{ &gpt_uuid_netbsd_cgd,		G_PART_ALIAS_NETBSD_CGD,	 0 },
287 	{ &gpt_uuid_netbsd_ffs,		G_PART_ALIAS_NETBSD_FFS,	 0 },
288 	{ &gpt_uuid_netbsd_lfs,		G_PART_ALIAS_NETBSD_LFS,	 0 },
289 	{ &gpt_uuid_netbsd_raid,	G_PART_ALIAS_NETBSD_RAID,	 0 },
290 	{ &gpt_uuid_netbsd_swap,	G_PART_ALIAS_NETBSD_SWAP,	 0 },
291 	{ &gpt_uuid_openbsd_data,	G_PART_ALIAS_OPENBSD_DATA,	 0 },
292 	{ &gpt_uuid_prep_boot,		G_PART_ALIAS_PREP_BOOT,		 0x41 },
293 	{ &gpt_uuid_solaris_boot,	G_PART_ALIAS_SOLARIS_BOOT,	 0 },
294 	{ &gpt_uuid_solaris_root,	G_PART_ALIAS_SOLARIS_ROOT,	 0 },
295 	{ &gpt_uuid_solaris_swap,	G_PART_ALIAS_SOLARIS_SWAP,	 0 },
296 	{ &gpt_uuid_solaris_backup,	G_PART_ALIAS_SOLARIS_BACKUP,	 0 },
297 	{ &gpt_uuid_solaris_var,	G_PART_ALIAS_SOLARIS_VAR,	 0 },
298 	{ &gpt_uuid_solaris_home,	G_PART_ALIAS_SOLARIS_HOME,	 0 },
299 	{ &gpt_uuid_solaris_altsec,	G_PART_ALIAS_SOLARIS_ALTSEC,	 0 },
300 	{ &gpt_uuid_solaris_reserved,	G_PART_ALIAS_SOLARIS_RESERVED,	 0 },
301 	{ &gpt_uuid_u_boot_env,		G_PART_ALIAS_U_BOOT_ENV,	 0 },
302 	{ &gpt_uuid_vmfs,		G_PART_ALIAS_VMFS,		 0 },
303 	{ &gpt_uuid_vmkdiag,		G_PART_ALIAS_VMKDIAG,		 0 },
304 	{ &gpt_uuid_vmreserved,		G_PART_ALIAS_VMRESERVED,	 0 },
305 	{ &gpt_uuid_vmvsanhdr,		G_PART_ALIAS_VMVSANHDR,		 0 },
306 	{ &gpt_uuid_xbootldr,		G_PART_ALIAS_XBOOTLDR,		 0 },
307 	{ NULL, 0, 0 }
308 };
309 
310 static int
gpt_write_mbr_entry(u_char * mbr,int idx,int typ,quad_t start,quad_t end)311 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
312     quad_t end)
313 {
314 
315 	if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
316 		return (EINVAL);
317 
318 	mbr += DOSPARTOFF + idx * DOSPARTSIZE;
319 	mbr[0] = 0;
320 	if (start == 1) {
321 		/*
322 		 * Treat the PMBR partition specially to maximize
323 		 * interoperability with BIOSes.
324 		 */
325 		mbr[1] = mbr[3] = 0;
326 		mbr[2] = 2;
327 	} else
328 		mbr[1] = mbr[2] = mbr[3] = 0xff;
329 	mbr[4] = typ;
330 	mbr[5] = mbr[6] = mbr[7] = 0xff;
331 	le32enc(mbr + 8, (uint32_t)start);
332 	le32enc(mbr + 12, (uint32_t)(end - start + 1));
333 	return (0);
334 }
335 
336 static int
gpt_map_type(struct uuid * t)337 gpt_map_type(struct uuid *t)
338 {
339 	struct g_part_uuid_alias *uap;
340 
341 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
342 		if (EQUUID(t, uap->uuid))
343 			return (uap->mbrtype);
344 	}
345 	return (0);
346 }
347 
348 static void
gpt_create_pmbr(struct g_part_gpt_table * table,struct g_provider * pp)349 gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
350 {
351 
352 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
353 	gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
354 	    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
355 	le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
356 }
357 
358 /*
359  * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
360  * whole disk anymore. Rather, it covers the GPT table and the EFI
361  * system partition only. This way the HFS+ partition and any FAT
362  * partitions can be added to the MBR without creating an overlap.
363  */
364 static int
gpt_is_bootcamp(struct g_part_gpt_table * table,const char * provname)365 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
366 {
367 	uint8_t *p;
368 
369 	p = table->mbr + DOSPARTOFF;
370 	if (p[4] != 0xee || le32dec(p + 8) != 1)
371 		return (0);
372 
373 	p += DOSPARTSIZE;
374 	if (p[4] != 0xaf)
375 		return (0);
376 
377 	printf("GEOM: %s: enabling Boot Camp\n", provname);
378 	return (1);
379 }
380 
381 static void
gpt_update_bootcamp(struct g_part_table * basetable,struct g_provider * pp)382 gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
383 {
384 	struct g_part_entry *baseentry;
385 	struct g_part_gpt_entry *entry;
386 	struct g_part_gpt_table *table;
387 	int bootable, error, index, slices, typ;
388 
389 	table = (struct g_part_gpt_table *)basetable;
390 
391 	bootable = -1;
392 	for (index = 0; index < NDOSPART; index++) {
393 		if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
394 			bootable = index;
395 	}
396 
397 	bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
398 	slices = 0;
399 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
400 		if (baseentry->gpe_deleted)
401 			continue;
402 		index = baseentry->gpe_index - 1;
403 		if (index >= NDOSPART)
404 			continue;
405 
406 		entry = (struct g_part_gpt_entry *)baseentry;
407 
408 		switch (index) {
409 		case 0:	/* This must be the EFI system partition. */
410 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
411 				goto disable;
412 			error = gpt_write_mbr_entry(table->mbr, index, 0xee,
413 			    1ull, entry->ent.ent_lba_end);
414 			break;
415 		case 1:	/* This must be the HFS+ partition. */
416 			if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
417 				goto disable;
418 			error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
419 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
420 			break;
421 		default:
422 			typ = gpt_map_type(&entry->ent.ent_type);
423 			error = gpt_write_mbr_entry(table->mbr, index, typ,
424 			    entry->ent.ent_lba_start, entry->ent.ent_lba_end);
425 			break;
426 		}
427 		if (error)
428 			continue;
429 
430 		if (index == bootable)
431 			table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
432 		slices |= 1 << index;
433 	}
434 	if ((slices & 3) == 3)
435 		return;
436 
437  disable:
438 	table->bootcamp = 0;
439 	gpt_create_pmbr(table, pp);
440 }
441 
442 static struct gpt_hdr *
gpt_read_hdr(struct g_part_gpt_table * table,struct g_consumer * cp,enum gpt_elt elt)443 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
444     enum gpt_elt elt)
445 {
446 	struct gpt_hdr *buf, *hdr;
447 	struct g_provider *pp;
448 	quad_t lba, last;
449 	int error;
450 	uint32_t crc, sz;
451 
452 	pp = cp->provider;
453 	last = (pp->mediasize / pp->sectorsize) - 1;
454 	table->state[elt] = GPT_STATE_MISSING;
455 	/*
456 	 * If the primary header is valid look for secondary
457 	 * header in AlternateLBA, otherwise in the last medium's LBA.
458 	 */
459 	if (elt == GPT_ELT_SECHDR) {
460 		if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
461 			table->lba[elt] = last;
462 	} else
463 		table->lba[elt] = 1;
464 	buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
465 	    &error);
466 	if (buf == NULL)
467 		return (NULL);
468 	hdr = NULL;
469 	if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
470 		goto fail;
471 
472 	table->state[elt] = GPT_STATE_CORRUPT;
473 	sz = le32toh(buf->hdr_size);
474 	if (sz < 92 || sz > pp->sectorsize)
475 		goto fail;
476 
477 	hdr = g_malloc(sz, M_WAITOK | M_ZERO);
478 	bcopy(buf, hdr, sz);
479 	hdr->hdr_size = sz;
480 
481 	crc = le32toh(buf->hdr_crc_self);
482 	buf->hdr_crc_self = 0;
483 	if (crc32(buf, sz) != crc)
484 		goto fail;
485 	hdr->hdr_crc_self = crc;
486 
487 	table->state[elt] = GPT_STATE_INVALID;
488 	hdr->hdr_revision = le32toh(buf->hdr_revision);
489 	if (hdr->hdr_revision < GPT_HDR_REVISION)
490 		goto fail;
491 	hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
492 	if (hdr->hdr_lba_self != table->lba[elt])
493 		goto fail;
494 	hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
495 	if (hdr->hdr_lba_alt == hdr->hdr_lba_self)
496 		goto fail;
497 	if (hdr->hdr_lba_alt > last && geom_part_check_integrity)
498 		goto fail;
499 
500 	/* Check the managed area. */
501 	hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
502 	if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
503 		goto fail;
504 	hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
505 	if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
506 		goto fail;
507 
508 	/* Check the table location and size of the table. */
509 	hdr->hdr_entries = le32toh(buf->hdr_entries);
510 	hdr->hdr_entsz = le32toh(buf->hdr_entsz);
511 	if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
512 	    (hdr->hdr_entsz & 7) != 0)
513 		goto fail;
514 	hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
515 	if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
516 		goto fail;
517 	if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
518 	    hdr->hdr_lba_table <= hdr->hdr_lba_end)
519 		goto fail;
520 	lba = hdr->hdr_lba_table +
521 	    howmany((uint64_t)hdr->hdr_entries * hdr->hdr_entsz,
522 	        pp->sectorsize) - 1;
523 	if (lba >= last)
524 		goto fail;
525 	if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
526 		goto fail;
527 
528 	table->state[elt] = GPT_STATE_OK;
529 	le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
530 	hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
531 
532 	/* save LBA for secondary header */
533 	if (elt == GPT_ELT_PRIHDR)
534 		table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
535 
536 	g_free(buf);
537 	return (hdr);
538 
539  fail:
540 	g_free(hdr);
541 	g_free(buf);
542 	return (NULL);
543 }
544 
545 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)546 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
547     enum gpt_elt elt, struct gpt_hdr *hdr)
548 {
549 	struct g_provider *pp;
550 	struct gpt_ent *ent, *tbl;
551 	char *buf, *p;
552 	unsigned int idx, sectors, tblsz, size;
553 	int error;
554 
555 	if (hdr == NULL)
556 		return (NULL);
557 	if (hdr->hdr_entries > g_part_gpt_scheme.gps_maxent ||
558 	    hdr->hdr_entsz > MAXENTSIZE) {
559 		table->state[elt] = GPT_STATE_UNSUPPORTED;
560 		return (NULL);
561 	}
562 
563 	pp = cp->provider;
564 	table->lba[elt] = hdr->hdr_lba_table;
565 
566 	table->state[elt] = GPT_STATE_MISSING;
567 	tblsz = hdr->hdr_entries * hdr->hdr_entsz;
568 	sectors = howmany(tblsz, pp->sectorsize);
569 	buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
570 	for (idx = 0; idx < sectors; idx += maxphys / pp->sectorsize) {
571 		size = (sectors - idx > maxphys / pp->sectorsize) ?  maxphys:
572 		    (sectors - idx) * pp->sectorsize;
573 		p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
574 		    size, &error);
575 		if (p == NULL) {
576 			g_free(buf);
577 			return (NULL);
578 		}
579 		bcopy(p, buf + idx * pp->sectorsize, size);
580 		g_free(p);
581 	}
582 	table->state[elt] = GPT_STATE_CORRUPT;
583 	if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
584 		g_free(buf);
585 		return (NULL);
586 	}
587 
588 	table->state[elt] = GPT_STATE_OK;
589 	tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
590 	    M_WAITOK | M_ZERO);
591 
592 	for (idx = 0, ent = tbl, p = buf;
593 	     idx < hdr->hdr_entries;
594 	     idx++, ent++, p += hdr->hdr_entsz) {
595 		le_uuid_dec(p, &ent->ent_type);
596 		le_uuid_dec(p + 16, &ent->ent_uuid);
597 		ent->ent_lba_start = le64dec(p + 32);
598 		ent->ent_lba_end = le64dec(p + 40);
599 		ent->ent_attr = le64dec(p + 48);
600 		/* Keep UTF-16 in little-endian. */
601 		bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
602 	}
603 
604 	g_free(buf);
605 	return (tbl);
606 }
607 
608 static int
gpt_matched_hdrs(struct gpt_hdr * pri,struct gpt_hdr * sec)609 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
610 {
611 
612 	if (pri == NULL || sec == NULL)
613 		return (0);
614 
615 	if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
616 		return (0);
617 	return ((pri->hdr_revision == sec->hdr_revision &&
618 	    pri->hdr_size == sec->hdr_size &&
619 	    pri->hdr_lba_start == sec->hdr_lba_start &&
620 	    pri->hdr_lba_end == sec->hdr_lba_end &&
621 	    pri->hdr_entries == sec->hdr_entries &&
622 	    pri->hdr_entsz == sec->hdr_entsz &&
623 	    pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
624 }
625 
626 static int
gpt_parse_type(const char * type,struct uuid * uuid)627 gpt_parse_type(const char *type, struct uuid *uuid)
628 {
629 	struct uuid tmp;
630 	const char *alias;
631 	int error;
632 	struct g_part_uuid_alias *uap;
633 
634 	if (type[0] == '!') {
635 		error = parse_uuid(type + 1, &tmp);
636 		if (error)
637 			return (error);
638 		if (EQUUID(&tmp, &gpt_uuid_unused))
639 			return (EINVAL);
640 		*uuid = tmp;
641 		return (0);
642 	}
643 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
644 		alias = g_part_alias_name(uap->alias);
645 		if (!strcasecmp(type, alias)) {
646 			*uuid = *uap->uuid;
647 			return (0);
648 		}
649 	}
650 	return (EINVAL);
651 }
652 
653 static int
g_part_gpt_add(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)654 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
655     struct g_part_parms *gpp)
656 {
657 	struct g_part_gpt_entry *entry;
658 	int error;
659 
660 	entry = (struct g_part_gpt_entry *)baseentry;
661 	error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
662 	if (error)
663 		return (error);
664 	kern_uuidgen(&entry->ent.ent_uuid, 1);
665 	entry->ent.ent_lba_start = baseentry->gpe_start;
666 	entry->ent.ent_lba_end = baseentry->gpe_end;
667 	if (baseentry->gpe_deleted) {
668 		entry->ent.ent_attr = 0;
669 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
670 	}
671 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
672 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
673 		    sizeof(entry->ent.ent_name) /
674 		    sizeof(entry->ent.ent_name[0]));
675 	return (0);
676 }
677 
678 static int
g_part_gpt_bootcode(struct g_part_table * basetable,struct g_part_parms * gpp)679 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
680 {
681 	struct g_part_gpt_table *table;
682 	size_t codesz;
683 
684 	codesz = DOSPARTOFF;
685 	table = (struct g_part_gpt_table *)basetable;
686 	bzero(table->mbr, codesz);
687 	codesz = MIN(codesz, gpp->gpp_codesize);
688 	if (codesz > 0)
689 		bcopy(gpp->gpp_codeptr, table->mbr, codesz);
690 	return (0);
691 }
692 
693 static int
g_part_gpt_create(struct g_part_table * basetable,struct g_part_parms * gpp)694 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
695 {
696 	struct g_provider *pp;
697 	struct g_part_gpt_table *table;
698 	size_t tblsz;
699 
700 	/* Our depth should be 0 unless nesting was explicitly enabled. */
701 	if (!allow_nesting && basetable->gpt_depth != 0)
702 		return (ENXIO);
703 
704 	table = (struct g_part_gpt_table *)basetable;
705 	pp = gpp->gpp_provider;
706 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
707 	    pp->sectorsize);
708 	if (pp->sectorsize < MBRSIZE ||
709 	    pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
710 	    pp->sectorsize)
711 		return (ENOSPC);
712 
713 	gpt_create_pmbr(table, pp);
714 
715 	/* Allocate space for the header */
716 	table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
717 
718 	bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
719 	table->hdr->hdr_revision = GPT_HDR_REVISION;
720 	table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
721 	kern_uuidgen(&table->hdr->hdr_uuid, 1);
722 	table->hdr->hdr_entries = basetable->gpt_entries;
723 	table->hdr->hdr_entsz = sizeof(struct gpt_ent);
724 
725 	g_gpt_set_defaults(basetable, pp, gpp);
726 	return (0);
727 }
728 
729 static int
g_part_gpt_destroy(struct g_part_table * basetable,struct g_part_parms * gpp)730 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
731 {
732 	struct g_part_gpt_table *table;
733 	struct g_provider *pp;
734 
735 	table = (struct g_part_gpt_table *)basetable;
736 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
737 	g_free(table->hdr);
738 	table->hdr = NULL;
739 
740 	/*
741 	 * Wipe the first 2 sectors and last one to clear the partitioning.
742 	 * Wipe sectors only if they have valid metadata.
743 	 */
744 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK)
745 		basetable->gpt_smhead |= 3;
746 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
747 	    table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
748 		basetable->gpt_smtail |= 1;
749 	return (0);
750 }
751 
752 static void
g_part_gpt_efimedia(struct g_part_gpt_entry * entry,struct sbuf * sb)753 g_part_gpt_efimedia(struct g_part_gpt_entry *entry, struct sbuf *sb)
754 {
755 	sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index);
756 	sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
757 	sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start,
758 	    (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
759 }
760 
761 static void
g_part_gpt_dumpconf(struct g_part_table * table,struct g_part_entry * baseentry,struct sbuf * sb,const char * indent)762 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
763     struct sbuf *sb, const char *indent)
764 {
765 	struct g_part_gpt_entry *entry;
766 
767 	entry = (struct g_part_gpt_entry *)baseentry;
768 	if (indent == NULL) {
769 		/* conftxt: libdisk compatibility */
770 		sbuf_cat(sb, " xs GPT xt ");
771 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
772 	} else if (entry != NULL) {
773 		/* confxml: partition entry information */
774 		sbuf_printf(sb, "%s<label>", indent);
775 		g_gpt_printf_utf16(sb, entry->ent.ent_name,
776 		    sizeof(entry->ent.ent_name) >> 1);
777 		sbuf_cat(sb, "</label>\n");
778 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
779 			sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
780 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
781 			sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
782 			    indent);
783 		}
784 		if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
785 			sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
786 			    indent);
787 		}
788 		sbuf_printf(sb, "%s<rawtype>", indent);
789 		sbuf_printf_uuid(sb, &entry->ent.ent_type);
790 		sbuf_cat(sb, "</rawtype>\n");
791 		sbuf_printf(sb, "%s<rawuuid>", indent);
792 		sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
793 		sbuf_cat(sb, "</rawuuid>\n");
794 		sbuf_printf(sb, "%s<efimedia>", indent);
795 		g_part_gpt_efimedia(entry, sb);
796 		sbuf_cat(sb, "</efimedia>\n");
797 	} else {
798 		/* confxml: scheme information */
799 	}
800 }
801 
802 static int
g_part_gpt_dumpto(struct g_part_table * table,struct g_part_entry * baseentry)803 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
804 {
805 	struct g_part_gpt_entry *entry;
806 
807 	entry = (struct g_part_gpt_entry *)baseentry;
808 	return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
809 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
810 	    EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
811 }
812 
813 static int
g_part_gpt_modify(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)814 g_part_gpt_modify(struct g_part_table *basetable,
815     struct g_part_entry *baseentry, struct g_part_parms *gpp)
816 {
817 	struct g_part_gpt_entry *entry;
818 	int error;
819 
820 	entry = (struct g_part_gpt_entry *)baseentry;
821 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
822 		error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
823 		if (error)
824 			return (error);
825 	}
826 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
827 		g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
828 		    sizeof(entry->ent.ent_name) /
829 		    sizeof(entry->ent.ent_name[0]));
830 	return (0);
831 }
832 
833 static int
g_part_gpt_resize(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)834 g_part_gpt_resize(struct g_part_table *basetable,
835     struct g_part_entry *baseentry, struct g_part_parms *gpp)
836 {
837 	struct g_part_gpt_entry *entry;
838 
839 	if (baseentry == NULL)
840 		return (g_part_gpt_recover(basetable));
841 
842 	entry = (struct g_part_gpt_entry *)baseentry;
843 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
844 	entry->ent.ent_lba_end = baseentry->gpe_end;
845 
846 	return (0);
847 }
848 
849 static const char *
g_part_gpt_name(struct g_part_table * table,struct g_part_entry * baseentry,char * buf,size_t bufsz)850 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
851     char *buf, size_t bufsz)
852 {
853 	struct g_part_gpt_entry *entry;
854 	char c;
855 
856 	entry = (struct g_part_gpt_entry *)baseentry;
857 	c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
858 	snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
859 	return (buf);
860 }
861 
862 static int
g_part_gpt_probe(struct g_part_table * table,struct g_consumer * cp)863 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
864 {
865 	struct g_provider *pp;
866 	u_char *buf;
867 	int error, index, pri, res;
868 
869 	/* Our depth should be 0 unless nesting was explicitly enabled. */
870 	if (!allow_nesting && table->gpt_depth != 0)
871 		return (ENXIO);
872 
873 	pp = cp->provider;
874 
875 	/*
876 	 * Sanity-check the provider. Since the first sector on the provider
877 	 * must be a PMBR and a PMBR is 512 bytes large, the sector size
878 	 * must be at least 512 bytes.  Also, since the theoretical minimum
879 	 * number of sectors needed by GPT is 6, any medium that has less
880 	 * than 6 sectors is never going to be able to hold a GPT. The
881 	 * number 6 comes from:
882 	 *	1 sector for the PMBR
883 	 *	2 sectors for the GPT headers (each 1 sector)
884 	 *	2 sectors for the GPT tables (each 1 sector)
885 	 *	1 sector for an actual partition
886 	 * It's better to catch this pathological case early than behaving
887 	 * pathologically later on...
888 	 */
889 	if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
890 		return (ENOSPC);
891 
892 	/*
893 	 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
894 	 * as the highest priority on a match, otherwise we assume some
895 	 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
896 	 * we really want the MBR scheme to take precedence.
897 	 */
898 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
899 	if (buf == NULL)
900 		return (error);
901 	res = le16dec(buf + DOSMAGICOFFSET);
902 	pri = G_PART_PROBE_PRI_LOW;
903 	if (res == DOSMAGIC) {
904 		for (index = 0; index < NDOSPART; index++) {
905 			if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
906 				pri = G_PART_PROBE_PRI_HIGH;
907 		}
908 		g_free(buf);
909 
910 		/* Check that there's a primary header. */
911 		buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
912 		if (buf == NULL)
913 			return (error);
914 		res = memcmp(buf, GPT_HDR_SIG, 8);
915 		g_free(buf);
916 		if (res == 0)
917 			return (pri);
918 	} else
919 		g_free(buf);
920 
921 	/* No primary? Check that there's a secondary. */
922 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
923 	    &error);
924 	if (buf == NULL)
925 		return (error);
926 	res = memcmp(buf, GPT_HDR_SIG, 8);
927 	g_free(buf);
928 	return ((res == 0) ? pri : ENXIO);
929 }
930 
931 static int
g_part_gpt_read(struct g_part_table * basetable,struct g_consumer * cp)932 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
933 {
934 	struct gpt_hdr *prihdr, *sechdr;
935 	struct gpt_ent *tbl, *pritbl, *sectbl;
936 	struct g_provider *pp;
937 	struct g_part_gpt_table *table;
938 	struct g_part_gpt_entry *entry;
939 	u_char *buf;
940 	uint64_t last;
941 	int error, index;
942 
943 	table = (struct g_part_gpt_table *)basetable;
944 	pp = cp->provider;
945 	last = (pp->mediasize / pp->sectorsize) - 1;
946 
947 	/* Read the PMBR */
948 	buf = g_read_data(cp, 0, pp->sectorsize, &error);
949 	if (buf == NULL)
950 		return (error);
951 	bcopy(buf, table->mbr, MBRSIZE);
952 	g_free(buf);
953 
954 	/* Read the primary header and table. */
955 	prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
956 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
957 		pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
958 	} else {
959 		table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
960 		pritbl = NULL;
961 	}
962 
963 	/* Read the secondary header and table. */
964 	sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
965 	if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
966 		sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
967 	} else {
968 		table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
969 		sectbl = NULL;
970 	}
971 
972 	/* Fail if we haven't got any good tables at all. */
973 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
974 	    table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
975 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_UNSUPPORTED &&
976 		    table->state[GPT_ELT_SECTBL] == GPT_STATE_UNSUPPORTED &&
977 		    gpt_matched_hdrs(prihdr, sechdr)) {
978 			printf("GEOM: %s: unsupported GPT detected.\n",
979 			    pp->name);
980 			printf(
981 		    "GEOM: %s: number of GPT entries: %u, entry size: %uB.\n",
982 			    pp->name, prihdr->hdr_entries, prihdr->hdr_entsz);
983 			printf(
984     "GEOM: %s: maximum supported number of GPT entries: %u, entry size: %uB.\n",
985 			    pp->name, g_part_gpt_scheme.gps_maxent, MAXENTSIZE);
986 			printf("GEOM: %s: GPT rejected.\n", pp->name);
987 		} else {
988 			printf("GEOM: %s: corrupt or invalid GPT detected.\n",
989 			    pp->name);
990 			printf(
991 		    "GEOM: %s: GPT rejected -- may not be recoverable.\n",
992 			    pp->name);
993 		}
994 		g_free(prihdr);
995 		g_free(pritbl);
996 		g_free(sechdr);
997 		g_free(sectbl);
998 		return (EINVAL);
999 	}
1000 
1001 	/*
1002 	 * If both headers are good but they disagree with each other,
1003 	 * then invalidate one. We prefer to keep the primary header,
1004 	 * unless the primary table is corrupt.
1005 	 */
1006 	if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
1007 	    table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
1008 	    !gpt_matched_hdrs(prihdr, sechdr)) {
1009 		if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
1010 			table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
1011 			table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
1012 			g_free(sechdr);
1013 			sechdr = NULL;
1014 		} else {
1015 			table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
1016 			table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
1017 			g_free(prihdr);
1018 			prihdr = NULL;
1019 		}
1020 	}
1021 
1022 	if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
1023 		printf("GEOM: %s: the primary GPT table is corrupt or "
1024 		    "invalid.\n", pp->name);
1025 		printf("GEOM: %s: using the secondary instead -- recovery "
1026 		    "strongly advised.\n", pp->name);
1027 		table->hdr = sechdr;
1028 		basetable->gpt_corrupt = 1;
1029 		g_free(prihdr);
1030 		tbl = sectbl;
1031 		g_free(pritbl);
1032 	} else {
1033 		if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
1034 			printf("GEOM: %s: the secondary GPT table is corrupt "
1035 			    "or invalid.\n", pp->name);
1036 			printf("GEOM: %s: using the primary only -- recovery "
1037 			    "suggested.\n", pp->name);
1038 			basetable->gpt_corrupt = 1;
1039 		} else if (table->lba[GPT_ELT_SECHDR] != last) {
1040 			printf( "GEOM: %s: the secondary GPT header is not in "
1041 			    "the last LBA.\n", pp->name);
1042 			basetable->gpt_corrupt = 1;
1043 		}
1044 		table->hdr = prihdr;
1045 		g_free(sechdr);
1046 		tbl = pritbl;
1047 		g_free(sectbl);
1048 	}
1049 
1050 	/*
1051 	 * The reserved area preceeds the valid area for partitions. Warn when
1052 	 * the lba_start doesn't meet the standard's minimum size for the gpt
1053 	 * entry array. UEFI 2.10 section 5.3 specifies that the LBA must be 32
1054 	 * (for 512 byte sectors) or 6 (4k sectors) or larger. This is different
1055 	 * than the number of valid entries in the GPT entry array, which can be
1056 	 * smaller.
1057 	 */
1058 	if (table->hdr->hdr_lba_start < GPT_MIN_RESERVED / pp->sectorsize + 2) {
1059 		printf("GEOM: warning: %s lba_start %llu < required min %d\n",
1060 		    pp->name, (unsigned long long)table->hdr->hdr_lba_start,
1061 		    GPT_MIN_RESERVED / pp->sectorsize + 2);
1062 	}
1063 
1064 	basetable->gpt_first = table->hdr->hdr_lba_start;
1065 	basetable->gpt_last = table->hdr->hdr_lba_end;
1066 	basetable->gpt_entries = table->hdr->hdr_entries;
1067 
1068 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
1069 		if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
1070 			continue;
1071 		entry = (struct g_part_gpt_entry *)g_part_new_entry(
1072 		    basetable, index + 1, tbl[index].ent_lba_start,
1073 		    tbl[index].ent_lba_end);
1074 		entry->ent = tbl[index];
1075 	}
1076 
1077 	g_free(tbl);
1078 
1079 	/*
1080 	 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1081 	 * if (and only if) any FAT32 or FAT16 partitions have been
1082 	 * created. This happens irrespective of whether Boot Camp is
1083 	 * used/enabled, though it's generally understood to be done
1084 	 * to support legacy Windows under Boot Camp. We refer to this
1085 	 * mirroring simply as Boot Camp. We try to detect Boot Camp
1086 	 * so that we can update the MBR if and when GPT changes have
1087 	 * been made. Note that we do not enable Boot Camp if not
1088 	 * previously enabled because we can't assume that we're on a
1089 	 * Mac alongside Mac OS X.
1090 	 */
1091 	table->bootcamp = gpt_is_bootcamp(table, pp->name);
1092 
1093 	return (0);
1094 }
1095 
1096 static int
g_part_gpt_recover(struct g_part_table * basetable)1097 g_part_gpt_recover(struct g_part_table *basetable)
1098 {
1099 	struct g_part_gpt_table *table;
1100 	struct g_provider *pp;
1101 
1102 	table = (struct g_part_gpt_table *)basetable;
1103 	pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1104 	gpt_create_pmbr(table, pp);
1105 	g_gpt_set_defaults(basetable, pp, NULL);
1106 	basetable->gpt_corrupt = 0;
1107 	return (0);
1108 }
1109 
1110 static int
g_part_gpt_setunset(struct g_part_table * basetable,struct g_part_entry * baseentry,const char * attrib,unsigned int set)1111 g_part_gpt_setunset(struct g_part_table *basetable,
1112     struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1113 {
1114 	struct g_part_gpt_entry *entry;
1115 	struct g_part_gpt_table *table;
1116 	struct g_provider *pp;
1117 	uint8_t *p;
1118 	uint64_t attr;
1119 	int i;
1120 
1121 	table = (struct g_part_gpt_table *)basetable;
1122 	entry = (struct g_part_gpt_entry *)baseentry;
1123 
1124 	if (strcasecmp(attrib, "active") == 0) {
1125 		if (table->bootcamp) {
1126 			/* The active flag must be set on a valid entry. */
1127 			if (entry == NULL)
1128 				return (ENXIO);
1129 			if (baseentry->gpe_index > NDOSPART)
1130 				return (EINVAL);
1131 			for (i = 0; i < NDOSPART; i++) {
1132 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1133 				p[0] = (i == baseentry->gpe_index - 1)
1134 				    ? ((set) ? 0x80 : 0) : 0;
1135 			}
1136 		} else {
1137 			/* The PMBR is marked as active without an entry. */
1138 			if (entry != NULL)
1139 				return (ENXIO);
1140 			for (i = 0; i < NDOSPART; i++) {
1141 				p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1142 				p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
1143 			}
1144 		}
1145 		return (0);
1146 	} else if (strcasecmp(attrib, "lenovofix") == 0) {
1147 		/*
1148 		 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1149 		 * This workaround allows Lenovo X220, T420, T520, etc to boot
1150 		 * from GPT Partitions in BIOS mode.
1151 		 */
1152 
1153 		if (entry != NULL)
1154 			return (ENXIO);
1155 
1156 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1157 		bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1158 		gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1159 		    MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1160 		return (0);
1161 	}
1162 
1163 	if (entry == NULL)
1164 		return (ENODEV);
1165 
1166 	attr = 0;
1167 	if (strcasecmp(attrib, "bootme") == 0) {
1168 		attr |= GPT_ENT_ATTR_BOOTME;
1169 	} else if (strcasecmp(attrib, "bootonce") == 0) {
1170 		attr |= GPT_ENT_ATTR_BOOTONCE;
1171 		if (set)
1172 			attr |= GPT_ENT_ATTR_BOOTME;
1173 	} else if (strcasecmp(attrib, "bootfailed") == 0) {
1174 		/*
1175 		 * It should only be possible to unset BOOTFAILED, but it might
1176 		 * be useful for test purposes to also be able to set it.
1177 		 */
1178 		attr |= GPT_ENT_ATTR_BOOTFAILED;
1179 	}
1180 	if (attr == 0)
1181 		return (EINVAL);
1182 
1183 	if (set)
1184 		attr = entry->ent.ent_attr | attr;
1185 	else
1186 		attr = entry->ent.ent_attr & ~attr;
1187 	if (attr != entry->ent.ent_attr) {
1188 		entry->ent.ent_attr = attr;
1189 		if (!baseentry->gpe_created)
1190 			baseentry->gpe_modified = 1;
1191 	}
1192 	return (0);
1193 }
1194 
1195 static const char *
g_part_gpt_type(struct g_part_table * basetable,struct g_part_entry * baseentry,char * buf,size_t bufsz)1196 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1197     char *buf, size_t bufsz)
1198 {
1199 	struct g_part_gpt_entry *entry;
1200 	struct uuid *type;
1201 	struct g_part_uuid_alias *uap;
1202 
1203 	entry = (struct g_part_gpt_entry *)baseentry;
1204 	type = &entry->ent.ent_type;
1205 	for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1206 		if (EQUUID(type, uap->uuid))
1207 			return (g_part_alias_name(uap->alias));
1208 	buf[0] = '!';
1209 	snprintf_uuid(buf + 1, bufsz - 1, type);
1210 
1211 	return (buf);
1212 }
1213 
1214 static int
g_part_gpt_write(struct g_part_table * basetable,struct g_consumer * cp)1215 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1216 {
1217 	unsigned char *buf, *bp;
1218 	struct g_provider *pp;
1219 	struct g_part_entry *baseentry;
1220 	struct g_part_gpt_entry *entry;
1221 	struct g_part_gpt_table *table;
1222 	size_t tblsz;
1223 	uint32_t crc;
1224 	int error, index;
1225 
1226 	pp = cp->provider;
1227 	table = (struct g_part_gpt_table *)basetable;
1228 	tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
1229 	    pp->sectorsize);
1230 
1231 	/* Reconstruct the MBR from the GPT if under Boot Camp. */
1232 	if (table->bootcamp)
1233 		gpt_update_bootcamp(basetable, pp);
1234 
1235 	/* Write the PMBR */
1236 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1237 	bcopy(table->mbr, buf, MBRSIZE);
1238 	error = g_write_data(cp, 0, buf, pp->sectorsize);
1239 	g_free(buf);
1240 	if (error)
1241 		return (error);
1242 
1243 	/* Allocate space for the header and entries. */
1244 	buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1245 
1246 	memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
1247 	le32enc(buf + 8, table->hdr->hdr_revision);
1248 	le32enc(buf + 12, table->hdr->hdr_size);
1249 	le64enc(buf + 40, table->hdr->hdr_lba_start);
1250 	le64enc(buf + 48, table->hdr->hdr_lba_end);
1251 	le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
1252 	le32enc(buf + 80, table->hdr->hdr_entries);
1253 	le32enc(buf + 84, table->hdr->hdr_entsz);
1254 
1255 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1256 		if (baseentry->gpe_deleted)
1257 			continue;
1258 		entry = (struct g_part_gpt_entry *)baseentry;
1259 		index = baseentry->gpe_index - 1;
1260 		bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
1261 		le_uuid_enc(bp, &entry->ent.ent_type);
1262 		le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
1263 		le64enc(bp + 32, entry->ent.ent_lba_start);
1264 		le64enc(bp + 40, entry->ent.ent_lba_end);
1265 		le64enc(bp + 48, entry->ent.ent_attr);
1266 		memcpy(bp + 56, entry->ent.ent_name,
1267 		    sizeof(entry->ent.ent_name));
1268 	}
1269 
1270 	crc = crc32(buf + pp->sectorsize,
1271 	    table->hdr->hdr_entries * table->hdr->hdr_entsz);
1272 	le32enc(buf + 88, crc);
1273 
1274 	/* Write primary meta-data. */
1275 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
1276 	le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_self. */
1277 	le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_alt. */
1278 	le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]);	/* hdr_lba_table. */
1279 	crc = crc32(buf, table->hdr->hdr_size);
1280 	le32enc(buf + 16, crc);
1281 
1282 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1283 		error = g_write_data(cp,
1284 		    (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1285 		    buf + (index + 1) * pp->sectorsize,
1286 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1287 		    (tblsz - index) * pp->sectorsize);
1288 		if (error)
1289 			goto out;
1290 	}
1291 	error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1292 	    buf, pp->sectorsize);
1293 	if (error)
1294 		goto out;
1295 
1296 	/* Write secondary meta-data. */
1297 	le32enc(buf + 16, 0);	/* hdr_crc_self. */
1298 	le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]);	/* hdr_lba_self. */
1299 	le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]);	/* hdr_lba_alt. */
1300 	le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]);	/* hdr_lba_table. */
1301 	crc = crc32(buf, table->hdr->hdr_size);
1302 	le32enc(buf + 16, crc);
1303 
1304 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1305 		error = g_write_data(cp,
1306 		    (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1307 		    buf + (index + 1) * pp->sectorsize,
1308 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1309 		    (tblsz - index) * pp->sectorsize);
1310 		if (error)
1311 			goto out;
1312 	}
1313 	error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1314 	    buf, pp->sectorsize);
1315 
1316  out:
1317 	g_free(buf);
1318 	return (error);
1319 }
1320 
1321 static void
g_gpt_set_defaults(struct g_part_table * basetable,struct g_provider * pp,struct g_part_parms * gpp)1322 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp,
1323 	struct g_part_parms *gpp)
1324 {
1325 	struct g_part_entry *baseentry;
1326 	struct g_part_gpt_entry *entry;
1327 	struct g_part_gpt_table *table;
1328 	quad_t start, end, min, max;
1329 	quad_t lba, last;
1330 	size_t spb, tblsz;
1331 
1332 	table = (struct g_part_gpt_table *)basetable;
1333 	last = pp->mediasize / pp->sectorsize - 1;
1334 	tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
1335 	    pp->sectorsize);
1336 
1337 	table->lba[GPT_ELT_PRIHDR] = 1;
1338 	table->lba[GPT_ELT_PRITBL] = 2;
1339 	table->lba[GPT_ELT_SECHDR] = last;
1340 	table->lba[GPT_ELT_SECTBL] = last - tblsz;
1341 	table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
1342 	table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
1343 	table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
1344 	table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
1345 
1346 	max = start = 2 + tblsz;
1347 	min = end = last - tblsz - 1;
1348 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1349 		if (baseentry->gpe_deleted)
1350 			continue;
1351 		entry = (struct g_part_gpt_entry *)baseentry;
1352 		if (entry->ent.ent_lba_start < min)
1353 			min = entry->ent.ent_lba_start;
1354 		if (entry->ent.ent_lba_end > max)
1355 			max = entry->ent.ent_lba_end;
1356 	}
1357 	/*
1358 	 * Don't force alignment of any kind whatsoever on resize, restore or
1359 	 * recover. resize doesn't go through this path, recover has a NULL gpp
1360 	 * and restore has flags == restore (maybe with an appended 'C' to
1361 	 * commit the operation). For these operations, we have to trust the
1362 	 * user knows what they are doing.
1363 	 *
1364 	 * Otherwise it some flavor of creation of a new partition, so we align
1365 	 * to a 4k offset on the drive, to make 512e/4kn drives more performant
1366 	 * by default.
1367 	 */
1368 	if (gpp == NULL ||
1369 	    (gpp->gpp_parms & G_PART_PARM_FLAGS) == 0 ||
1370 	    strstr(gpp->gpp_flags, "restore") == NULL) {
1371 		spb = 4096 / pp->sectorsize;
1372 		if (spb > 1) {
1373 			lba = start + ((start % spb) ? spb - start % spb : 0);
1374 			if (lba <= min)
1375 				start = lba;
1376 			lba = end - (end + 1) % spb;
1377 			if (max <= lba)
1378 				end = lba;
1379 		}
1380 	}
1381 	table->hdr->hdr_lba_start = start;
1382 	table->hdr->hdr_lba_end = end;
1383 
1384 	basetable->gpt_first = start;
1385 	basetable->gpt_last = end;
1386 }
1387 
1388 static void
g_gpt_printf_utf16(struct sbuf * sb,uint16_t * str,size_t len)1389 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1390 {
1391 	u_int bo;
1392 	uint32_t ch;
1393 	uint16_t c;
1394 
1395 	bo = LITTLE_ENDIAN;	/* GPT is little-endian */
1396 	while (len > 0 && *str != 0) {
1397 		ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1398 		str++, len--;
1399 		if ((ch & 0xf800) == 0xd800) {
1400 			if (len > 0) {
1401 				c = (bo == BIG_ENDIAN) ? be16toh(*str)
1402 				    : le16toh(*str);
1403 				str++, len--;
1404 			} else
1405 				c = 0xfffd;
1406 			if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1407 				ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1408 				ch += 0x10000;
1409 			} else
1410 				ch = 0xfffd;
1411 		} else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1412 			bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1413 			continue;
1414 		} else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1415 			continue;
1416 
1417 		/* Write the Unicode character in UTF-8 */
1418 		if (ch < 0x80)
1419 			g_conf_printf_escaped(sb, "%c", ch);
1420 		else if (ch < 0x800)
1421 			g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
1422 			    0x80 | (ch & 0x3f));
1423 		else if (ch < 0x10000)
1424 			g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
1425 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1426 		else if (ch < 0x200000)
1427 			g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
1428 			    (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
1429 			    0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1430 	}
1431 }
1432 
1433 static void
g_gpt_utf8_to_utf16(const uint8_t * s8,uint16_t * s16,size_t s16len)1434 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1435 {
1436 	size_t s16idx, s8idx;
1437 	uint32_t utfchar;
1438 	unsigned int c, utfbytes;
1439 
1440 	s8idx = s16idx = 0;
1441 	utfchar = 0;
1442 	utfbytes = 0;
1443 	bzero(s16, s16len << 1);
1444 	while (s8[s8idx] != 0 && s16idx < s16len) {
1445 		c = s8[s8idx++];
1446 		if ((c & 0xc0) != 0x80) {
1447 			/* Initial characters. */
1448 			if (utfbytes != 0) {
1449 				/* Incomplete encoding of previous char. */
1450 				s16[s16idx++] = htole16(0xfffd);
1451 			}
1452 			if ((c & 0xf8) == 0xf0) {
1453 				utfchar = c & 0x07;
1454 				utfbytes = 3;
1455 			} else if ((c & 0xf0) == 0xe0) {
1456 				utfchar = c & 0x0f;
1457 				utfbytes = 2;
1458 			} else if ((c & 0xe0) == 0xc0) {
1459 				utfchar = c & 0x1f;
1460 				utfbytes = 1;
1461 			} else {
1462 				utfchar = c & 0x7f;
1463 				utfbytes = 0;
1464 			}
1465 		} else {
1466 			/* Followup characters. */
1467 			if (utfbytes > 0) {
1468 				utfchar = (utfchar << 6) + (c & 0x3f);
1469 				utfbytes--;
1470 			} else if (utfbytes == 0)
1471 				utfbytes = ~0;
1472 		}
1473 		/*
1474 		 * Write the complete Unicode character as UTF-16 when we
1475 		 * have all the UTF-8 charactars collected.
1476 		 */
1477 		if (utfbytes == 0) {
1478 			/*
1479 			 * If we need to write 2 UTF-16 characters, but
1480 			 * we only have room for 1, then we truncate the
1481 			 * string by writing a 0 instead.
1482 			 */
1483 			if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1484 				s16[s16idx++] =
1485 				    htole16(0xd800 | ((utfchar >> 10) - 0x40));
1486 				s16[s16idx++] =
1487 				    htole16(0xdc00 | (utfchar & 0x3ff));
1488 			} else
1489 				s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1490 				    htole16(utfchar);
1491 		}
1492 	}
1493 	/*
1494 	 * If our input string was truncated, append an invalid encoding
1495 	 * character to the output string.
1496 	 */
1497 	if (utfbytes != 0 && s16idx < s16len)
1498 		s16[s16idx++] = htole16(0xfffd);
1499 }
1500