xref: /netbsd/usr.sbin/sysinst/gpt.c (revision 7920585f)
1*7920585fSmartin /*	$NetBSD: gpt.c,v 1.26 2021/07/17 19:27:22 martin Exp $	*/
2dcc57e24Smartin 
3dcc57e24Smartin /*
4dcc57e24Smartin  * Copyright 2018 The NetBSD Foundation, Inc.
5dcc57e24Smartin  * All rights reserved.
6dcc57e24Smartin  *
7dcc57e24Smartin  * Redistribution and use in source and binary forms, with or without
8dcc57e24Smartin  * modification, are permitted provided that the following conditions
9dcc57e24Smartin  * are met:
10dcc57e24Smartin  * 1. Redistributions of source code must retain the above copyright
11dcc57e24Smartin  *    notice, this list of conditions and the following disclaimer.
12dcc57e24Smartin  * 2. Redistributions in binary form must reproduce the above copyright
13dcc57e24Smartin  *    notice, this list of conditions and the following disclaimer in the
14dcc57e24Smartin  *    documentation and/or other materials provided with the distribution.
15dcc57e24Smartin  *
16dcc57e24Smartin  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
17dcc57e24Smartin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18dcc57e24Smartin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19dcc57e24Smartin  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
20dcc57e24Smartin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21dcc57e24Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22dcc57e24Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23dcc57e24Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24dcc57e24Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25dcc57e24Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26dcc57e24Smartin  * THE POSSIBILITY OF SUCH DAMAGE.
27dcc57e24Smartin  *
28dcc57e24Smartin  */
29dcc57e24Smartin 
30dcc57e24Smartin #include "defs.h"
31dcc57e24Smartin #include "mbr.h"
32dcc57e24Smartin #include "md.h"
33dcc57e24Smartin #include "gpt_uuid.h"
34dcc57e24Smartin #include <assert.h>
35642571f8Smartin #include <errno.h>
368ac26f1aSmartin #include <err.h>
37dcc57e24Smartin #include <paths.h>
38dcc57e24Smartin #include <sys/param.h>
39dcc57e24Smartin #include <sys/ioctl.h>
40dcc57e24Smartin #include <util.h>
418ac26f1aSmartin #include <uuid.h>
42dcc57e24Smartin 
43dcc57e24Smartin bool	gpt_parts_check(void);	/* check for needed binaries */
44dcc57e24Smartin 
45dcc57e24Smartin 
46dcc57e24Smartin /*************** GPT ************************************************/
47dcc57e24Smartin /* a GPT based disk_partitions interface */
48dcc57e24Smartin 
49dcc57e24Smartin #define GUID_STR_LEN	40
508ac26f1aSmartin #define	GPT_PTYPE_ALLOC	32	/* initial type array allocation, should be >
518ac26f1aSmartin 				 * gpt type -l | wc -l */
52dcc57e24Smartin #define	GPT_DEV_LEN	16	/* dkNN */
53dcc57e24Smartin 
540ea03d33Smartin #define	GPT_PARTS_PER_SEC	4	/* a 512 byte sector holds 4 entries */
55dcc57e24Smartin #define	GPT_DEFAULT_MAX_PARTS	128
56dcc57e24Smartin 
57dcc57e24Smartin /* a usable label will be short, so we can get away with an arbitrary limit */
58dcc57e24Smartin #define	GPT_LABEL_LEN		96
59dcc57e24Smartin 
60dcc57e24Smartin #define	GPT_ATTR_BIOSBOOT	1
61dcc57e24Smartin #define	GPT_ATTR_BOOTME		2
62dcc57e24Smartin #define	GPT_ATTR_BOOTONCE	4
63dcc57e24Smartin #define	GPT_ATTR_BOOTFAILED	8
64dcc57e24Smartin #define	GPT_ATTR_NOBLOCKIO	16
65dcc57e24Smartin #define	GPT_ATTR_REQUIRED	32
66dcc57e24Smartin 
67dcc57e24Smartin /* when we don't care for BIOS or UEFI boot, use the combined boot flags */
68dcc57e24Smartin #define	GPT_ATTR_BOOT	(GPT_ATTR_BIOSBOOT|GPT_ATTR_BOOTME)
69dcc57e24Smartin 
70dcc57e24Smartin struct gpt_attr_desc {
71dcc57e24Smartin 	const char *name;
72dcc57e24Smartin 	uint flag;
73dcc57e24Smartin };
74dcc57e24Smartin static const struct gpt_attr_desc gpt_avail_attrs[] = {
75dcc57e24Smartin 	{ "biosboot", GPT_ATTR_BIOSBOOT },
76dcc57e24Smartin 	{ "bootme", GPT_ATTR_BOOTME },
77dcc57e24Smartin 	{ "bootonce", GPT_ATTR_BOOTONCE },
78dcc57e24Smartin 	{ "bootfailed", GPT_ATTR_BOOTFAILED },
79dcc57e24Smartin 	{ "noblockio", GPT_ATTR_NOBLOCKIO },
80dcc57e24Smartin 	{ "required", GPT_ATTR_REQUIRED },
81dcc57e24Smartin 	{ NULL, 0 }
82dcc57e24Smartin };
83dcc57e24Smartin 
84dcc57e24Smartin struct gpt_ptype_desc {
85dcc57e24Smartin 	struct part_type_desc gent;
86dcc57e24Smartin 	char tid[GUID_STR_LEN];
87dcc57e24Smartin 	uint fsflags, default_fs_type;
88dcc57e24Smartin };
89dcc57e24Smartin 
90dcc57e24Smartin static const
91dcc57e24Smartin struct {
92dcc57e24Smartin 	const char *name;
93dcc57e24Smartin 	uint fstype;
94dcc57e24Smartin 	enum part_type ptype;
95dcc57e24Smartin 	uint fsflags;
96dcc57e24Smartin } gpt_fs_types[] = {
97dcc57e24Smartin 	{ .name = "ffs",	.fstype = FS_BSDFFS,	.ptype = PT_root,
98dcc57e24Smartin 	  .fsflags = GLM_LIKELY_FFS },
99dcc57e24Smartin 	{ .name = "swap",	.fstype = FS_SWAP,	.ptype = PT_swap },
100dcc57e24Smartin 	{ .name = "windows",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
101dcc57e24Smartin 	  .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
102dcc57e24Smartin 	{ .name = "windows",	.fstype = FS_NTFS,	.ptype = PT_FAT,
103dcc57e24Smartin 	  .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
104dcc57e24Smartin 	{ .name = "efi",	.fstype = FS_MSDOS,	.ptype = PT_EFI_SYSTEM,
105dcc57e24Smartin 	  .fsflags = GLM_MAYBE_FAT32 },
106dcc57e24Smartin 	{ .name = "bios",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
107dcc57e24Smartin 	  .fsflags = GLM_MAYBE_FAT32 },
108dcc57e24Smartin 	{ .name = "lfs",	.fstype = FS_BSDLFS,	.ptype = PT_root },
109dcc57e24Smartin 	{ .name = "linux-data",	.fstype = FS_EX2FS,	.ptype = PT_root },
110dcc57e24Smartin 	{ .name = "apple",	.fstype = FS_HFS,	.ptype = PT_unknown },
11134f3e937Smartin 	{ .name = "ccd",	.fstype = FS_CCD,	.ptype = PT_root },
11234f3e937Smartin 	{ .name = "cgd",	.fstype = FS_CGD,	.ptype = PT_root },
113dcc57e24Smartin 	{ .name = "raid",	.fstype = FS_RAID,	.ptype = PT_root },
114dcc57e24Smartin 	{ .name = "vmcore",	.fstype = FS_VMKCORE,	.ptype = PT_unknown },
115dcc57e24Smartin 	{ .name = "vmfs",	.fstype = FS_VMFS,	.ptype = PT_unknown },
116dd1127d6Smartin 	{ .name = "vmresered",	.fstype = FS_VMWRESV,	.ptype = PT_unknown },
11750894729Smartin 	{ .name = "zfs",	.fstype = FS_ZFS,	.ptype = PT_root },
118dcc57e24Smartin };
119dcc57e24Smartin 
1208ac26f1aSmartin static size_t gpt_ptype_cnt = 0, gpt_ptype_alloc = 0;
1218ac26f1aSmartin static struct gpt_ptype_desc *gpt_ptype_descs = NULL;
1228ac26f1aSmartin 
1238ac26f1aSmartin /* "well" known types with special handling */
1248ac26f1aSmartin static const struct part_type_desc *gpt_native_root;
125dcc57e24Smartin 
126dcc57e24Smartin /* similar to struct gpt_ent, but matching our needs */
127dcc57e24Smartin struct gpt_part_entry {
128dcc57e24Smartin 	const struct gpt_ptype_desc *gp_type;
129dcc57e24Smartin 	char gp_id[GUID_STR_LEN];	/* partition guid as string */
130dcc57e24Smartin 	daddr_t gp_start, gp_size;
131dcc57e24Smartin 	uint gp_attr;			/* various attribute bits */
132dcc57e24Smartin 	char gp_label[GPT_LABEL_LEN];	/* user defined label */
133dcc57e24Smartin 	char gp_dev_name[GPT_DEV_LEN];	/* name of wedge */
134dcc57e24Smartin 	const char *last_mounted;	/* last mounted if known */
135b162b00bSmartin 	uint fs_type, fs_sub_type,	/* FS_* and maybe sub type */
136b162b00bSmartin 	    fs_opt1, fs_opt2, fs_opt3;	/* transient file system options */
137dcc57e24Smartin 	uint gp_flags;
138dcc57e24Smartin #define	GPEF_ON_DISK	1		/* This entry exists on-disk */
139dcc57e24Smartin #define	GPEF_MODIFIED	2		/* this entry has been changed */
140dcc57e24Smartin #define	GPEF_WEDGE	4		/* wedge for this exists */
141dcc57e24Smartin #define	GPEF_RESIZED	8		/* size has changed */
142bf7de24aSmartin #define	GPEF_TARGET	16		/* marked install target */
143dcc57e24Smartin 	struct gpt_part_entry *gp_next;
144dcc57e24Smartin };
145dcc57e24Smartin 
146dcc57e24Smartin static const struct gpt_ptype_desc *gpt_find_native_type(
147dcc57e24Smartin     const struct part_type_desc *gent);
148dcc57e24Smartin static const struct gpt_ptype_desc *gpt_find_guid_type(const char*);
149dcc57e24Smartin static bool
150dcc57e24Smartin gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
151dcc57e24Smartin     const char **err_msg);
152dcc57e24Smartin 
153dcc57e24Smartin const struct disk_partitioning_scheme gpt_parts;
154dcc57e24Smartin struct gpt_disk_partitions {
155dcc57e24Smartin 	struct disk_partitions dp;
156dcc57e24Smartin 	/*
157dcc57e24Smartin 	 * We keep a list of our current valid partitions, pointed
158dcc57e24Smartin 	 * to by "partitions".
159dcc57e24Smartin 	 * dp.num_part is the number of entries in "partitions".
160dcc57e24Smartin 	 * When partitions that have a representation on disk already
161dcc57e24Smartin 	 * are deleted, we move them to the "obsolete" list so we
162dcc57e24Smartin 	 * can issue the proper commands to remove it when writing back.
163dcc57e24Smartin 	 */
164dcc57e24Smartin 	struct gpt_part_entry *partitions,	/* current partitions */
165dcc57e24Smartin 	    *obsolete;				/* deleted partitions */
166dcc57e24Smartin 	size_t max_num_parts;			/* how many entries max? */
167dcc57e24Smartin 	size_t prologue, epilogue;		/* number of sectors res. */
168dcc57e24Smartin 	bool has_gpt;	/* disk already has a GPT */
169dcc57e24Smartin };
170dcc57e24Smartin 
171dcc57e24Smartin /*
172dcc57e24Smartin  * Init global variables from MD details
173dcc57e24Smartin  */
174dcc57e24Smartin static void
175dcc57e24Smartin gpt_md_init(bool is_boot_disk, size_t *max_parts, size_t *head, size_t *tail)
176dcc57e24Smartin {
177dcc57e24Smartin 	size_t num;
178dcc57e24Smartin 
179dcc57e24Smartin 	if (is_boot_disk) {
180dcc57e24Smartin #ifdef MD_GPT_INITIAL_SIZE
181dcc57e24Smartin #if MD_GPT_INITIAL_SIZE < 2*512
182dcc57e24Smartin #error	impossible small GPT prologue
183dcc57e24Smartin #endif
184dcc57e24Smartin 		num = ((MD_GPT_INITIAL_SIZE-(2*512))/512)*GPT_PARTS_PER_SEC;
185dcc57e24Smartin #else
186dcc57e24Smartin 		num = GPT_DEFAULT_MAX_PARTS;
187dcc57e24Smartin #endif
188dcc57e24Smartin 	} else {
189dcc57e24Smartin 		num = GPT_DEFAULT_MAX_PARTS;
190dcc57e24Smartin 	}
191dcc57e24Smartin 	*max_parts = num;
192dcc57e24Smartin 	*head = 2 + num/GPT_PARTS_PER_SEC;
193dcc57e24Smartin 	*tail = 1 + num/GPT_PARTS_PER_SEC;
194dcc57e24Smartin }
195dcc57e24Smartin 
196dcc57e24Smartin /*
197dcc57e24Smartin  * Parse a part of "gpt show" output into a struct gpt_part_entry.
198dcc57e24Smartin  * Output is from "show -a" format if details = false, otherwise
199dcc57e24Smartin  * from details for a specific partition (show -i or show -b)
200dcc57e24Smartin  */
201dcc57e24Smartin static void
202dcc57e24Smartin gpt_add_info(struct gpt_part_entry *part, const char *tag, char *val,
203dcc57e24Smartin     bool details)
204dcc57e24Smartin {
205dcc57e24Smartin 	char *s, *e;
206dcc57e24Smartin 
207dcc57e24Smartin 	if (details && strcmp(tag, "Start:") == 0) {
208dcc57e24Smartin 		part->gp_start = strtouq(val, NULL, 10);
209dcc57e24Smartin 	} else if (details && strcmp(tag, "Size:") == 0) {
210dcc57e24Smartin 		part->gp_size = strtouq(val, NULL, 10);
211dcc57e24Smartin 	} else if (details && strcmp(tag, "Type:") == 0) {
212dcc57e24Smartin 		s = strchr(val, '(');
213dcc57e24Smartin 		if (!s)
214dcc57e24Smartin 			return;
215dcc57e24Smartin 		e = strchr(s, ')');
216dcc57e24Smartin 		if (!e)
217dcc57e24Smartin 			return;
218dcc57e24Smartin 		*e = 0;
219dcc57e24Smartin 		part->gp_type = gpt_find_guid_type(s+1);
220dcc57e24Smartin 	} else if (strcmp(tag, "TypeID:") == 0) {
221dcc57e24Smartin 		part->gp_type = gpt_find_guid_type(val);
222dcc57e24Smartin 	} else if (strcmp(tag, "GUID:") == 0) {
223dcc57e24Smartin 		strlcpy(part->gp_id, val, sizeof(part->gp_id));
224dcc57e24Smartin 	} else if (strcmp(tag, "Label:") == 0) {
225dcc57e24Smartin 		strlcpy(part->gp_label, val, sizeof(part->gp_label));
226dcc57e24Smartin 	} else if (strcmp(tag, "Attributes:") == 0) {
227dcc57e24Smartin 		char *n;
228dcc57e24Smartin 
229dcc57e24Smartin 		while ((n = strsep(&val, ", ")) != NULL) {
230dcc57e24Smartin 			if (*n == 0)
231dcc57e24Smartin 				continue;
232dcc57e24Smartin 			for (const struct gpt_attr_desc *p = gpt_avail_attrs;
233dcc57e24Smartin 			    p->name != NULL; p++) {
234dcc57e24Smartin 				if (strcmp(p->name, n) == 0)
235dcc57e24Smartin 					part->gp_attr |= p->flag;
236dcc57e24Smartin 			}
237dcc57e24Smartin 		}
238dcc57e24Smartin 	}
239dcc57e24Smartin }
240dcc57e24Smartin 
24167b2b814Smartin /*
24267b2b814Smartin  * Find the partition matching this wedge info and record that we
24367b2b814Smartin  * have a wedge already.
24467b2b814Smartin  */
24567b2b814Smartin static void
24667b2b814Smartin update_part_from_wedge_info(struct gpt_disk_partitions *parts,
24767b2b814Smartin     const struct dkwedge_info *dkw)
24867b2b814Smartin {
24967b2b814Smartin 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
25067b2b814Smartin 	    p = p->gp_next) {
25167b2b814Smartin 		if (p->gp_start != dkw->dkw_offset ||
25267b2b814Smartin 		    (uint64_t)p->gp_size != dkw->dkw_size)
25367b2b814Smartin 			continue;
25467b2b814Smartin 		p->gp_flags |= GPEF_WEDGE;
25567b2b814Smartin 		strlcpy(p->gp_dev_name, dkw->dkw_devname,
25667b2b814Smartin 		    sizeof p->gp_dev_name);
25767b2b814Smartin 		return;
25867b2b814Smartin 	}
25967b2b814Smartin }
26067b2b814Smartin 
261dcc57e24Smartin static struct disk_partitions *
2620ea03d33Smartin gpt_read_from_disk(const char *dev, daddr_t start, daddr_t len, size_t bps,
263ae033681Smartin     const struct disk_partitioning_scheme *scheme)
264dcc57e24Smartin {
265dcc57e24Smartin 	char diskpath[MAXPATHLEN];
266dcc57e24Smartin 	int fd;
26767b2b814Smartin 	struct dkwedge_info *dkw;
26867b2b814Smartin 	struct dkwedge_list dkwl;
26967b2b814Smartin 	size_t bufsize, dk;
270dcc57e24Smartin 
271dcc57e24Smartin 	assert(start == 0);
272dcc57e24Smartin 	assert(have_gpt);
273dcc57e24Smartin 
274dcc57e24Smartin 	if (run_program(RUN_SILENT | RUN_ERROR_OK,
275dcc57e24Smartin 	    "gpt -rq header %s", dev) != 0)
276dcc57e24Smartin 		return NULL;
277dcc57e24Smartin 
278dcc57e24Smartin 	/* read the partitions */
279dcc57e24Smartin 	int i;
280dcc57e24Smartin 	unsigned int p_index;
281dcc57e24Smartin 	daddr_t p_start = 0, p_size = 0, avail_start = 0, avail_size = 0,
282dcc57e24Smartin 	    disk_size = 0;
283dcc57e24Smartin 	char *textbuf, *t, *tt, p_type[STRSIZE];
284dcc57e24Smartin 	static const char regpart_prefix[] = "GPT part - ";
285dcc57e24Smartin 	struct gpt_disk_partitions *parts;
286dcc57e24Smartin 	struct gpt_part_entry *last = NULL, *add_to = NULL;
287bf7de24aSmartin 	const struct gpt_ptype_desc *native_root
288bf7de24aSmartin 	     = gpt_find_native_type(gpt_native_root);
289bf7de24aSmartin 	bool have_target = false;
290dcc57e24Smartin 
291dcc57e24Smartin 	if (collect(T_OUTPUT, &textbuf, "gpt -r show -a %s 2>/dev/null", dev)
292dcc57e24Smartin 	    < 1)
293dcc57e24Smartin 		return NULL;
294dcc57e24Smartin 
295dcc57e24Smartin 	/* parse output and create our list */
296dcc57e24Smartin 	parts = calloc(1, sizeof(*parts));
297dcc57e24Smartin 	if (parts == NULL)
298dcc57e24Smartin 		return NULL;
299dcc57e24Smartin 
300dcc57e24Smartin 	(void)strtok(textbuf, "\n"); /* ignore first line */
301dcc57e24Smartin 	while ((t = strtok(NULL, "\n")) != NULL) {
302dcc57e24Smartin 		i = 0; p_start = 0; p_size = 0; p_index = 0;
303dcc57e24Smartin 		p_type[0] = 0;
304dcc57e24Smartin 		while ((tt = strsep(&t, " \t")) != NULL) {
305dcc57e24Smartin 			if (strlen(tt) == 0)
306dcc57e24Smartin 				continue;
307dcc57e24Smartin 			if (i == 0) {
308dcc57e24Smartin 				if (add_to != NULL)
309dcc57e24Smartin 					gpt_add_info(add_to, tt, t, false);
310dcc57e24Smartin 				p_start = strtouq(tt, NULL, 10);
311dcc57e24Smartin 				if (p_start == 0 && add_to != NULL)
312dcc57e24Smartin 					break;
313dcc57e24Smartin 				else
314dcc57e24Smartin 					add_to = NULL;
315dcc57e24Smartin 			}
316dcc57e24Smartin 			if (i == 1)
317dcc57e24Smartin 				p_size = strtouq(tt, NULL, 10);
318dcc57e24Smartin 			if (i == 2)
319dcc57e24Smartin 				p_index = strtouq(tt, NULL, 10);
320dcc57e24Smartin 			if (i > 2 || (i == 2 && p_index == 0)) {
321dcc57e24Smartin 				if (p_type[0])
322dcc57e24Smartin 					strlcat(p_type, " ", STRSIZE);
323dcc57e24Smartin 				strlcat(p_type, tt, STRSIZE);
324dcc57e24Smartin 			}
325dcc57e24Smartin 			i++;
326dcc57e24Smartin 		}
327dcc57e24Smartin 
328dcc57e24Smartin 		if (p_start == 0 || p_size == 0)
329dcc57e24Smartin 			continue;
330dcc57e24Smartin 		else if (strcmp(p_type, "Pri GPT table") == 0) {
331dcc57e24Smartin 			avail_start = p_start + p_size;
332dcc57e24Smartin 			parts->prologue = avail_start;
333dcc57e24Smartin 			parts->epilogue = p_size + 1;
334dcc57e24Smartin 			parts->max_num_parts = p_size * GPT_PARTS_PER_SEC;
335dcc57e24Smartin 		} else if (strcmp(p_type, "Sec GPT table") == 0)
336dcc57e24Smartin 			avail_size = p_start - avail_start;
337dcc57e24Smartin 		else if(strcmp(p_type, "Sec GPT header") == 0)
338dcc57e24Smartin 			disk_size = p_start + p_size;
339dcc57e24Smartin 		else if (p_index == 0 && strlen(p_type) > 0)
340dcc57e24Smartin 			/* Utilitary entry (PMBR, etc) */
341dcc57e24Smartin 			continue;
342dcc57e24Smartin 		else if (p_index == 0) {
343dcc57e24Smartin 			/* Free space */
344dcc57e24Smartin 			continue;
345dcc57e24Smartin 		} else {
346dcc57e24Smartin 			/* Usual partition */
347dcc57e24Smartin 			tt = p_type;
348dcc57e24Smartin 			if (strncmp(tt, regpart_prefix,
349dcc57e24Smartin 			    strlen(regpart_prefix)) == 0)
350dcc57e24Smartin 				tt += strlen(regpart_prefix);
351dcc57e24Smartin 
352dcc57e24Smartin 			/* Add to our linked list */
353dcc57e24Smartin 			struct gpt_part_entry *np = calloc(1, sizeof(*np));
354dcc57e24Smartin 			if (np == NULL)
355dcc57e24Smartin 				break;
356dcc57e24Smartin 
357dcc57e24Smartin 			strlcpy(np->gp_label, tt, sizeof(np->gp_label));
358dcc57e24Smartin 			np->gp_start = p_start;
359dcc57e24Smartin 			np->gp_size = p_size;
360dcc57e24Smartin 			np->gp_flags |= GPEF_ON_DISK;
361bf7de24aSmartin 			if (!have_target && native_root != NULL &&
362bf7de24aSmartin 			    strcmp(np->gp_id, native_root->tid) == 0) {
363bf7de24aSmartin 				have_target = true;
364bf7de24aSmartin 				np->gp_flags |= GPEF_TARGET;
365bf7de24aSmartin 			}
366dcc57e24Smartin 
367dcc57e24Smartin 			if (last == NULL)
368dcc57e24Smartin 				parts->partitions = np;
369dcc57e24Smartin 			else
370dcc57e24Smartin 				last->gp_next = np;
371dcc57e24Smartin 			last = np;
372dcc57e24Smartin 			add_to = np;
373dcc57e24Smartin 			parts->dp.num_part++;
374dcc57e24Smartin 		}
375dcc57e24Smartin 	}
376dcc57e24Smartin 	free(textbuf);
377dcc57e24Smartin 
3786b0797fdSmartin 	/* If the GPT was not complete (e.g. truncated image), barf */
3796b0797fdSmartin 	if (disk_size <= 0) {
3806b0797fdSmartin 		free(parts);
3816b0797fdSmartin 		return NULL;
3826b0797fdSmartin 	}
3836b0797fdSmartin 
384ae033681Smartin 	parts->dp.pscheme = scheme;
3858ac26f1aSmartin 	parts->dp.disk = strdup(dev);
386dcc57e24Smartin 	parts->dp.disk_start = start;
387dcc57e24Smartin 	parts->dp.disk_size = disk_size;
388dcc57e24Smartin 	parts->dp.free_space = avail_size;
3890ea03d33Smartin 	parts->dp.bytes_per_sector = bps;
390dcc57e24Smartin 	parts->has_gpt = true;
391dcc57e24Smartin 
392dcc57e24Smartin 	fd = opendisk(parts->dp.disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
393dcc57e24Smartin 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
394dcc57e24Smartin 	    p = p->gp_next) {
395dcc57e24Smartin #ifdef DEFAULT_UFS2
396dcc57e24Smartin 		bool fs_is_default = false;
397dcc57e24Smartin #endif
398dcc57e24Smartin 
3996d3e0ec1Smartin 		if (p->gp_type != NULL) {
4006d3e0ec1Smartin 
401dcc57e24Smartin 			if (p->gp_type->fsflags != 0) {
4026d3e0ec1Smartin 				const char *lm = get_last_mounted(fd,
4036d3e0ec1Smartin 				    p->gp_start, &p->fs_type,
4046d3e0ec1Smartin 				    &p->fs_sub_type, p->gp_type->fsflags);
405dcc57e24Smartin 				if (lm != NULL && *lm != 0) {
406f4f78ddcSmartin 					char *path = strdup(lm);
407f4f78ddcSmartin 					canonicalize_last_mounted(path);
408f4f78ddcSmartin 					p->last_mounted = path;
409dcc57e24Smartin 				} else {
4106d3e0ec1Smartin 					p->fs_type = p->gp_type->
4116d3e0ec1Smartin 					    default_fs_type;
412dcc57e24Smartin #ifdef DEFAULT_UFS2
413dcc57e24Smartin 					fs_is_default = true;
414dcc57e24Smartin #endif
415dcc57e24Smartin 				}
416dcc57e24Smartin 			} else {
417dcc57e24Smartin 				p->fs_type = p->gp_type->default_fs_type;
418dcc57e24Smartin #ifdef DEFAULT_UFS2
419dcc57e24Smartin 				fs_is_default = true;
420dcc57e24Smartin #endif
421dcc57e24Smartin 			}
422dcc57e24Smartin #ifdef DEFAULT_UFS2
423dcc57e24Smartin 			if (fs_is_default && p->fs_type == FS_BSDFFS)
424dcc57e24Smartin 				p->fs_sub_type = 2;
425dcc57e24Smartin #endif
4266d3e0ec1Smartin 		}
427dcc57e24Smartin 
428dcc57e24Smartin 		parts->dp.free_space -= p->gp_size;
429dcc57e24Smartin 	}
43067b2b814Smartin 
43167b2b814Smartin 	/*
43267b2b814Smartin 	 * Check if we have any (matching/auto-configured) wedges already
43367b2b814Smartin 	 */
43467b2b814Smartin 	dkw = NULL;
43567b2b814Smartin 	dkwl.dkwl_buf = dkw;
43667b2b814Smartin 	dkwl.dkwl_bufsize = 0;
43767b2b814Smartin 	if (ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
43867b2b814Smartin 		/* do not even try to deal with any races at this point */
43967b2b814Smartin 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
44067b2b814Smartin 		dkw = malloc(bufsize);
44167b2b814Smartin 		dkwl.dkwl_buf = dkw;
44267b2b814Smartin 		dkwl.dkwl_bufsize = bufsize;
44367b2b814Smartin 		if (dkw != NULL && ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
44467b2b814Smartin 			for (dk = 0; dk < dkwl.dkwl_ncopied; dk++)
44567b2b814Smartin 				update_part_from_wedge_info(parts, &dkw[dk]);
44667b2b814Smartin 		}
44767b2b814Smartin 		free(dkw);
44867b2b814Smartin 	}
44967b2b814Smartin 
450dcc57e24Smartin 	close(fd);
451dcc57e24Smartin 
452dcc57e24Smartin 	return &parts->dp;
453dcc57e24Smartin }
454dcc57e24Smartin 
45575761763Smartin static size_t
45675761763Smartin gpt_cyl_size(const struct disk_partitions *arg)
45775761763Smartin {
45875761763Smartin 	return MEG / 512;
45975761763Smartin }
46075761763Smartin 
461dcc57e24Smartin static struct disk_partitions *
4620ea03d33Smartin gpt_create_new(const char *disk, daddr_t start, daddr_t len,
46334f3e937Smartin     bool is_boot_drive, struct disk_partitions *parent)
464dcc57e24Smartin {
465dcc57e24Smartin 	struct gpt_disk_partitions *parts;
4660ea03d33Smartin 	struct disk_geom geo;
467dcc57e24Smartin 
468dcc57e24Smartin 	if (start != 0) {
469dcc57e24Smartin 		assert(0);
470dcc57e24Smartin 		return NULL;
471dcc57e24Smartin 	}
472dcc57e24Smartin 
4730ea03d33Smartin 	if (!get_disk_geom(disk, &geo))
4740ea03d33Smartin 		return NULL;
4750ea03d33Smartin 
47600f87f9fSmartin 	parts = calloc(1, sizeof(*parts));
477dcc57e24Smartin 	if (!parts)
478dcc57e24Smartin 		return NULL;
479dcc57e24Smartin 
480dcc57e24Smartin 	parts->dp.pscheme = &gpt_parts;
4818ac26f1aSmartin 	parts->dp.disk = strdup(disk);
482dcc57e24Smartin 
483dcc57e24Smartin 	gpt_md_init(is_boot_drive, &parts->max_num_parts, &parts->prologue,
484dcc57e24Smartin 	    &parts->epilogue);
485dcc57e24Smartin 
486dcc57e24Smartin 	parts->dp.disk_start = start;
487dcc57e24Smartin 	parts->dp.disk_size = len;
4880ea03d33Smartin 	parts->dp.bytes_per_sector = geo.dg_secsize;
489dcc57e24Smartin 	parts->dp.free_space = len - start - parts->prologue - parts->epilogue;
490dcc57e24Smartin 	parts->has_gpt = false;
491dcc57e24Smartin 
492dcc57e24Smartin 	return &parts->dp;
493dcc57e24Smartin }
494dcc57e24Smartin 
495dcc57e24Smartin static bool
496dcc57e24Smartin gpt_get_part_info(const struct disk_partitions *arg, part_id id,
497dcc57e24Smartin     struct disk_part_info *info)
498dcc57e24Smartin {
4996d3e0ec1Smartin 	static const struct part_type_desc gpt_unknown_type =
5006d3e0ec1Smartin 		{ .generic_ptype = PT_undef,
5016d3e0ec1Smartin 		  .short_desc = "<unknown>" };
502dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
503dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
504dcc57e24Smartin 	const struct gpt_part_entry *p = parts->partitions;
505dcc57e24Smartin 	part_id no;
506dcc57e24Smartin 
507dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
508dcc57e24Smartin 		p = p->gp_next;
509dcc57e24Smartin 
510dcc57e24Smartin 	if (no != id || p == NULL)
511dcc57e24Smartin 		return false;
512dcc57e24Smartin 
513dcc57e24Smartin 	memset(info, 0, sizeof(*info));
514dcc57e24Smartin 	info->start = p->gp_start;
515dcc57e24Smartin 	info->size = p->gp_size;
516dcc57e24Smartin 	if (p->gp_type)
517dcc57e24Smartin 		info->nat_type = &p->gp_type->gent;
5186d3e0ec1Smartin 	else
5196d3e0ec1Smartin 		info->nat_type = &gpt_unknown_type;
520dcc57e24Smartin 	info->last_mounted = p->last_mounted;
521dcc57e24Smartin 	info->fs_type = p->fs_type;
522dcc57e24Smartin 	info->fs_sub_type = p->fs_sub_type;
523b162b00bSmartin 	info->fs_opt1 = p->fs_opt1;
524b162b00bSmartin 	info->fs_opt2 = p->fs_opt2;
525b162b00bSmartin 	info->fs_opt3 = p->fs_opt3;
526bf7de24aSmartin 	if (p->gp_flags & GPEF_TARGET)
527bf7de24aSmartin 		info->flags |= PTI_INSTALL_TARGET;
528dcc57e24Smartin 
529dcc57e24Smartin 	return true;
530dcc57e24Smartin }
531dcc57e24Smartin 
532dcc57e24Smartin static bool
533dcc57e24Smartin gpt_get_part_attr_str(const struct disk_partitions *arg, part_id id,
534dcc57e24Smartin     char *str, size_t avail_space)
535dcc57e24Smartin {
536dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
537dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
538dcc57e24Smartin 	const struct gpt_part_entry *p = parts->partitions;
539dcc57e24Smartin 	part_id no;
540dcc57e24Smartin 	static const char *flags = NULL;
541dcc57e24Smartin 
542dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
543dcc57e24Smartin 		p = p->gp_next;
544dcc57e24Smartin 
545dcc57e24Smartin 	if (no != id || p == NULL)
546dcc57e24Smartin 		return false;
547dcc57e24Smartin 
548dcc57e24Smartin 	if (flags == NULL)
549dcc57e24Smartin 		flags = msg_string(MSG_gpt_flags);
550dcc57e24Smartin 
551dcc57e24Smartin 	if (avail_space < 2)
552dcc57e24Smartin 		return false;
553dcc57e24Smartin 
554dcc57e24Smartin 	if (p->gp_attr & GPT_ATTR_BOOT)
555dcc57e24Smartin 		*str++ = flags[0];
556dcc57e24Smartin 	*str = 0;
557dcc57e24Smartin 
558dcc57e24Smartin 	return true;
559dcc57e24Smartin }
560dcc57e24Smartin 
561dcc57e24Smartin /*
562dcc57e24Smartin  * Find insert position and check for duplicates.
563dcc57e24Smartin  * If all goes well, insert the new "entry" in the "list".
564dcc57e24Smartin  * If there are collisions, report "no free space".
565dcc57e24Smartin  * We keep all lists sorted by start sector number,
566dcc57e24Smartin  */
567dcc57e24Smartin static bool
568dcc57e24Smartin gpt_insert_part_into_list(struct gpt_disk_partitions *parts,
569dcc57e24Smartin     struct gpt_part_entry **list,
570dcc57e24Smartin     struct gpt_part_entry *entry, const char **err_msg)
571dcc57e24Smartin {
572dcc57e24Smartin 	struct gpt_part_entry *p, *last;
573dcc57e24Smartin 
574dcc57e24Smartin 	/* find the first entry past the new one (if any) */
575dcc57e24Smartin 	for (last = NULL, p = *list; p != NULL; last = p, p = p->gp_next) {
576dcc57e24Smartin 		if (p->gp_start > entry->gp_start)
577dcc57e24Smartin 			break;
578dcc57e24Smartin 	}
579dcc57e24Smartin 
580dcc57e24Smartin 	/* check if last partition overlaps with new one */
581dcc57e24Smartin 	if (last) {
582dcc57e24Smartin 		if (last->gp_start + last->gp_size > entry->gp_start) {
583dcc57e24Smartin 			if (err_msg)
584dcc57e24Smartin 				*err_msg = msg_string(MSG_No_free_space);
585dcc57e24Smartin 			return false;
586dcc57e24Smartin 		}
587dcc57e24Smartin 	}
588dcc57e24Smartin 
589dcc57e24Smartin 	if (p == NULL) {
590dcc57e24Smartin 		entry->gp_next = NULL;
591dcc57e24Smartin 		if (last != NULL) {
592dcc57e24Smartin 			last->gp_next = entry;
593dcc57e24Smartin 		}
594dcc57e24Smartin 	} else {
595dcc57e24Smartin 		/* check if new entry overlaps with next */
596dcc57e24Smartin 		if (entry->gp_start + entry->gp_size > p->gp_start) {
597dcc57e24Smartin 			if (err_msg)
598dcc57e24Smartin 				*err_msg = msg_string(MSG_No_free_space);
599dcc57e24Smartin 			return false;
600dcc57e24Smartin 		}
601dcc57e24Smartin 
602dcc57e24Smartin 		entry->gp_next = p;
603dcc57e24Smartin 		if (last != NULL)
604dcc57e24Smartin 			last->gp_next = entry;
605dcc57e24Smartin 		else
606dcc57e24Smartin 			*list = entry;
607dcc57e24Smartin 	}
608dcc57e24Smartin 	if (*list == NULL)
609dcc57e24Smartin 		*list = entry;
610dcc57e24Smartin 
611dcc57e24Smartin 	return true;
612dcc57e24Smartin }
613dcc57e24Smartin 
614dcc57e24Smartin static bool
615dcc57e24Smartin gpt_set_part_info(struct disk_partitions *arg, part_id id,
616dcc57e24Smartin     const struct disk_part_info *info, const char **err_msg)
617dcc57e24Smartin {
618dcc57e24Smartin 	struct gpt_disk_partitions *parts =
619dcc57e24Smartin 	    (struct gpt_disk_partitions*)arg;
620dcc57e24Smartin 	struct gpt_part_entry *p = parts->partitions, *n;
621dcc57e24Smartin 	part_id no;
622dcc57e24Smartin 	daddr_t lendiff;
623bf7de24aSmartin 	bool was_target;
624dcc57e24Smartin 
625dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
626dcc57e24Smartin 		p = p->gp_next;
627dcc57e24Smartin 
628dcc57e24Smartin 	if (no != id || p == NULL)
629dcc57e24Smartin 		return false;
630dcc57e24Smartin 
631bf7de24aSmartin 	/* update target mark - we can only have one */
632bf7de24aSmartin 	was_target = (p->gp_flags & GPEF_TARGET) != 0;
633bf7de24aSmartin 	if (info->flags & PTI_INSTALL_TARGET)
634bf7de24aSmartin 		p->gp_flags |= GPEF_TARGET;
635bf7de24aSmartin 	else
636bf7de24aSmartin 		p->gp_flags &= ~GPEF_TARGET;
637bf7de24aSmartin 	if (was_target)
638bf7de24aSmartin 		for (n = parts->partitions; n != NULL; n = n->gp_next)
639bf7de24aSmartin 			if (n != p)
640bf7de24aSmartin 				n->gp_flags &= ~GPEF_TARGET;
641bf7de24aSmartin 
642dcc57e24Smartin 	if ((p->gp_flags & GPEF_ON_DISK)) {
643dcc57e24Smartin 		if (info->start != p->gp_start) {
644dcc57e24Smartin 			/* partition moved, we need to delete and re-add */
645dcc57e24Smartin 			n = calloc(1, sizeof(*n));
646dcc57e24Smartin 			if (n == NULL) {
647dcc57e24Smartin 				if (err_msg)
648dcc57e24Smartin 					*err_msg = err_outofmem;
649dcc57e24Smartin 				return false;
650dcc57e24Smartin 			}
651dcc57e24Smartin 			*n = *p;
652dcc57e24Smartin 			p->gp_flags &= ~GPEF_ON_DISK;
653dcc57e24Smartin 			if (!gpt_insert_part_into_list(parts, &parts->obsolete,
654dcc57e24Smartin 			    n, err_msg))
655dcc57e24Smartin 				return false;
656dcc57e24Smartin 		} else if (info->size != p->gp_size) {
657dcc57e24Smartin 			p->gp_flags |= GPEF_RESIZED;
658dcc57e24Smartin 		}
659dcc57e24Smartin 	}
660dcc57e24Smartin 
661dcc57e24Smartin 	p->gp_flags |= GPEF_MODIFIED;
662dcc57e24Smartin 
663dcc57e24Smartin 	lendiff = info->size - p->gp_size;
664dcc57e24Smartin 	parts->dp.free_space -= lendiff;
665dcc57e24Smartin 	return gpt_info_to_part(p, info, err_msg);
666dcc57e24Smartin }
667dcc57e24Smartin 
668dcc57e24Smartin static size_t
669dcc57e24Smartin gpt_get_free_spaces_internal(const struct gpt_disk_partitions *parts,
670dcc57e24Smartin     struct disk_part_free_space *result, size_t max_num_result,
671dcc57e24Smartin     daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
672dcc57e24Smartin {
673dcc57e24Smartin 	size_t cnt = 0;
674dcc57e24Smartin 	daddr_t s, e, from, size, end_of_disk;
675dcc57e24Smartin 	struct gpt_part_entry *p;
676dcc57e24Smartin 
677dcc57e24Smartin 	if (align > 1)
678dcc57e24Smartin 		start = max(roundup(start, align), align);
679dcc57e24Smartin 	if (start < 0 || start < (daddr_t)parts->prologue)
680dcc57e24Smartin 		start = parts->prologue;
681dcc57e24Smartin 	if (parts->dp.disk_start != 0 && parts->dp.disk_start > start)
682dcc57e24Smartin 		start = parts->dp.disk_start;
683dcc57e24Smartin 	if (min_space_size < 1)
684dcc57e24Smartin 		min_space_size = 1;
685dcc57e24Smartin 	end_of_disk = parts->dp.disk_start + parts->dp.disk_size
686dcc57e24Smartin 	    - parts->epilogue;
687dcc57e24Smartin 	from = start;
688dcc57e24Smartin 	while (from < end_of_disk && cnt < max_num_result) {
689dcc57e24Smartin again:
690dcc57e24Smartin 		size = parts->dp.disk_start + parts->dp.disk_size - from;
691dcc57e24Smartin 		start = from;
692dcc57e24Smartin 		if (start + size > end_of_disk)
693dcc57e24Smartin 			size = end_of_disk - start;
694dcc57e24Smartin 		for (p = parts->partitions; p != NULL; p = p->gp_next) {
695dcc57e24Smartin 			s = p->gp_start;
696dcc57e24Smartin 			e = p->gp_size + s;
697dcc57e24Smartin 			if (s == ignore)
698dcc57e24Smartin 				continue;
699dcc57e24Smartin 			if (e < from)
700dcc57e24Smartin 				continue;
701dcc57e24Smartin 			if (s <= from && e > from) {
702dcc57e24Smartin 				if (e - 1 >= end_of_disk)
703dcc57e24Smartin 					return cnt;
704dcc57e24Smartin 				from = e + 1;
705dcc57e24Smartin 				if (align > 1) {
706dcc57e24Smartin 					from = max(roundup(from, align), align);
707dcc57e24Smartin 					if (from >= end_of_disk) {
708dcc57e24Smartin 						size = 0;
709dcc57e24Smartin 						break;
710dcc57e24Smartin 					}
711dcc57e24Smartin 				}
712dcc57e24Smartin 				goto again;
713dcc57e24Smartin 			}
714dcc57e24Smartin 			if (s > from && s - from < size) {
715dcc57e24Smartin 				size = s - from;
716dcc57e24Smartin 			}
717dcc57e24Smartin 		}
718dcc57e24Smartin 		if (size >= min_space_size) {
719dcc57e24Smartin 			result->start = start;
720dcc57e24Smartin 			result->size = size;
721dcc57e24Smartin 			result++;
722dcc57e24Smartin 			cnt++;
723dcc57e24Smartin 		}
724dcc57e24Smartin 		from += size + 1;
725dcc57e24Smartin 		if (align > 1)
726dcc57e24Smartin 			from = max(roundup(from, align), align);
727dcc57e24Smartin 	}
728dcc57e24Smartin 
729dcc57e24Smartin 	return cnt;
730dcc57e24Smartin }
731dcc57e24Smartin 
732dcc57e24Smartin static daddr_t
733dcc57e24Smartin gpt_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
734dcc57e24Smartin {
735dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
736dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
737dcc57e24Smartin 	struct disk_part_free_space space;
738dcc57e24Smartin 
739dcc57e24Smartin 	if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 0,
740dcc57e24Smartin 	    start, start) == 1)
741dcc57e24Smartin 		return space.size;
742dcc57e24Smartin 
743dcc57e24Smartin 	return 0;
744dcc57e24Smartin }
745dcc57e24Smartin 
746dcc57e24Smartin static size_t
747dcc57e24Smartin gpt_get_free_spaces(const struct disk_partitions *arg,
748dcc57e24Smartin     struct disk_part_free_space *result, size_t max_num_result,
749dcc57e24Smartin     daddr_t min_space_size, daddr_t align, daddr_t start,
750dcc57e24Smartin     daddr_t ignore)
751dcc57e24Smartin {
752dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
753dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
754dcc57e24Smartin 
755dcc57e24Smartin 	return gpt_get_free_spaces_internal(parts, result,
756dcc57e24Smartin 	    max_num_result, min_space_size, align, start, ignore);
757dcc57e24Smartin }
758dcc57e24Smartin 
759dcc57e24Smartin static void
760dcc57e24Smartin gpt_match_ptype(const char *name, struct gpt_ptype_desc *t)
761dcc57e24Smartin {
762dcc57e24Smartin 	size_t i;
763dcc57e24Smartin 
764dcc57e24Smartin 	for (i = 0; i < __arraycount(gpt_fs_types); i++) {
765dcc57e24Smartin 		if (strcmp(name, gpt_fs_types[i].name) == 0) {
766dcc57e24Smartin 			t->gent.generic_ptype = gpt_fs_types[i].ptype;
767dcc57e24Smartin 			t->fsflags = gpt_fs_types[i].fsflags;
768dcc57e24Smartin 			t->default_fs_type = gpt_fs_types[i].fstype;
7698ac26f1aSmartin 
7708ac26f1aSmartin 			/* recongnize special entries */
7718ac26f1aSmartin 			if (gpt_native_root == NULL && i == 0)
7728ac26f1aSmartin 				gpt_native_root = &t->gent;
7738ac26f1aSmartin 
774dcc57e24Smartin 			return;
775dcc57e24Smartin 		}
776dcc57e24Smartin 	}
777dcc57e24Smartin 
778dcc57e24Smartin 	t->gent.generic_ptype = PT_unknown;
779dcc57e24Smartin 	t->fsflags = 0;
780dcc57e24Smartin 	t->default_fs_type = FS_BSDFFS;
781dcc57e24Smartin }
782dcc57e24Smartin 
783dcc57e24Smartin static void
784dcc57e24Smartin gpt_internal_add_ptype(const char *uid, const char *name, const char *desc)
785dcc57e24Smartin {
7868ac26f1aSmartin 	if (gpt_ptype_cnt >= gpt_ptype_alloc) {
7878ac26f1aSmartin 		gpt_ptype_alloc = gpt_ptype_alloc ? 2*gpt_ptype_alloc
7888ac26f1aSmartin 		    : GPT_PTYPE_ALLOC;
7898ac26f1aSmartin 		struct gpt_ptype_desc *nptypes = realloc(gpt_ptype_descs,
7908ac26f1aSmartin 		    gpt_ptype_alloc*sizeof(*gpt_ptype_descs));
7918ac26f1aSmartin 		if (nptypes == 0)
7928ac26f1aSmartin 			errx(EXIT_FAILURE, "out of memory");
7938ac26f1aSmartin 		gpt_ptype_descs = nptypes;
7948ac26f1aSmartin 	}
7958ac26f1aSmartin 
796dcc57e24Smartin 	strlcpy(gpt_ptype_descs[gpt_ptype_cnt].tid, uid,
797dcc57e24Smartin 	    sizeof(gpt_ptype_descs[gpt_ptype_cnt].tid));
7988ac26f1aSmartin 	gpt_ptype_descs[gpt_ptype_cnt].gent.short_desc = strdup(name);
7998ac26f1aSmartin 	gpt_ptype_descs[gpt_ptype_cnt].gent.description = strdup(desc);
800dcc57e24Smartin 	gpt_match_ptype(name, &gpt_ptype_descs[gpt_ptype_cnt]);
801dcc57e24Smartin 	gpt_ptype_cnt++;
802dcc57e24Smartin }
803dcc57e24Smartin 
804dcc57e24Smartin static void
805dcc57e24Smartin gpt_init_ptypes(void)
806dcc57e24Smartin {
807dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
808dcc57e24Smartin 		gpt_uuid_query(gpt_internal_add_ptype);
809dcc57e24Smartin }
810dcc57e24Smartin 
8118ac26f1aSmartin static void
8128ac26f1aSmartin gpt_cleanup(void)
8138ac26f1aSmartin {
8148ac26f1aSmartin 	/* free all of gpt_ptype_descs */
8158ac26f1aSmartin 	for (size_t i = 0; i < gpt_ptype_cnt; i++) {
8168ac26f1aSmartin 		free(__UNCONST(gpt_ptype_descs[i].gent.short_desc));
8178ac26f1aSmartin 		free(__UNCONST(gpt_ptype_descs[i].gent.description));
8188ac26f1aSmartin 	}
8198ac26f1aSmartin 	free(gpt_ptype_descs);
8208ac26f1aSmartin 	gpt_ptype_descs = NULL;
8218ac26f1aSmartin 	gpt_ptype_cnt = gpt_ptype_alloc = 0;
8228ac26f1aSmartin }
8238ac26f1aSmartin 
824dcc57e24Smartin static size_t
825dcc57e24Smartin gpt_type_count(void)
826dcc57e24Smartin {
827dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
828dcc57e24Smartin 		gpt_init_ptypes();
829dcc57e24Smartin 
830dcc57e24Smartin 	return gpt_ptype_cnt;
831dcc57e24Smartin }
832dcc57e24Smartin 
833dcc57e24Smartin static const struct part_type_desc *
834dcc57e24Smartin gpt_get_ptype(size_t ndx)
835dcc57e24Smartin {
836dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
837dcc57e24Smartin 		gpt_init_ptypes();
838dcc57e24Smartin 
839dcc57e24Smartin 	if (ndx >= gpt_ptype_cnt)
840dcc57e24Smartin 		return NULL;
841dcc57e24Smartin 
842dcc57e24Smartin 	return &gpt_ptype_descs[ndx].gent;
843dcc57e24Smartin }
844dcc57e24Smartin 
845dcc57e24Smartin static const struct part_type_desc *
846dcc57e24Smartin gpt_get_generic_type(enum part_type gent)
847dcc57e24Smartin {
848dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
849dcc57e24Smartin 		gpt_init_ptypes();
850dcc57e24Smartin 
8518ac26f1aSmartin 	if (gent == PT_root)
8528ac26f1aSmartin 		return gpt_native_root;
8538ac26f1aSmartin 	if (gent == PT_unknown)
8548ac26f1aSmartin 		return NULL;
8558ac26f1aSmartin 
856dcc57e24Smartin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
857dcc57e24Smartin 		if (gpt_ptype_descs[i].gent.generic_ptype == gent)
858dcc57e24Smartin 			return &gpt_ptype_descs[i].gent;
859dcc57e24Smartin 
860dcc57e24Smartin 	return NULL;
861dcc57e24Smartin }
862dcc57e24Smartin 
863dcc57e24Smartin static const struct gpt_ptype_desc *
864dcc57e24Smartin gpt_find_native_type(const struct part_type_desc *gent)
865dcc57e24Smartin {
866dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
867dcc57e24Smartin 		gpt_init_ptypes();
868dcc57e24Smartin 
869dcc57e24Smartin 	if (gent == NULL)
870dcc57e24Smartin 		return NULL;
871dcc57e24Smartin 
872dcc57e24Smartin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
873dcc57e24Smartin 		if (gent == &gpt_ptype_descs[i].gent)
874dcc57e24Smartin 			return &gpt_ptype_descs[i];
875dcc57e24Smartin 
876dcc57e24Smartin 	gent = gpt_get_generic_type(gent->generic_ptype);
877dcc57e24Smartin 	if (gent == NULL)
878dcc57e24Smartin 		return NULL;
879dcc57e24Smartin 
880dcc57e24Smartin 	/* this can not recurse deeper than once, we would not have found a
881dcc57e24Smartin 	 * generic type a few lines above if it would. */
882dcc57e24Smartin 	return gpt_find_native_type(gent);
883dcc57e24Smartin }
884dcc57e24Smartin 
885dcc57e24Smartin static const struct gpt_ptype_desc *
886dcc57e24Smartin gpt_find_guid_type(const char *uid)
887dcc57e24Smartin {
888dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
889dcc57e24Smartin 		gpt_init_ptypes();
890dcc57e24Smartin 
891dcc57e24Smartin 	if (uid == NULL || uid[0] == 0)
892dcc57e24Smartin 		return NULL;
893dcc57e24Smartin 
894dcc57e24Smartin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
895dcc57e24Smartin 		if (strcmp(gpt_ptype_descs[i].tid, uid) == 0)
896dcc57e24Smartin 			return &gpt_ptype_descs[i];
897dcc57e24Smartin 
898dcc57e24Smartin 	return NULL;
899dcc57e24Smartin }
900dcc57e24Smartin 
901dcc57e24Smartin static const struct part_type_desc *
902dcc57e24Smartin gpt_find_type(const char *desc)
903dcc57e24Smartin {
904dcc57e24Smartin 	if (gpt_ptype_cnt == 0)
905dcc57e24Smartin 		gpt_init_ptypes();
906dcc57e24Smartin 
907dcc57e24Smartin 	if (desc == NULL || desc[0] == 0)
908dcc57e24Smartin 		return NULL;
909dcc57e24Smartin 
910dcc57e24Smartin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
911dcc57e24Smartin 		if (strcmp(gpt_ptype_descs[i].gent.short_desc, desc) == 0)
912dcc57e24Smartin 			return &gpt_ptype_descs[i].gent;
913dcc57e24Smartin 
914dcc57e24Smartin 	return NULL;
915dcc57e24Smartin }
916dcc57e24Smartin 
917dcc57e24Smartin static const struct part_type_desc *
918f6d1758bSmartin gpt_get_fs_part_type(enum part_type pt, unsigned fstype, unsigned fs_sub_type)
919dcc57e24Smartin {
920dcc57e24Smartin 	size_t i;
921dcc57e24Smartin 
92234f3e937Smartin 	/* Try with complete match (including part_type) first */
923f6d1758bSmartin 	for (i = 0; i < __arraycount(gpt_fs_types); i++)
924f6d1758bSmartin 		if (fstype == gpt_fs_types[i].fstype &&
925f6d1758bSmartin 		    pt == gpt_fs_types[i].ptype)
926f6d1758bSmartin 			return gpt_find_type(gpt_fs_types[i].name);
927f6d1758bSmartin 
928f6d1758bSmartin 	/* If that did not work, ignore part_type */
929dcc57e24Smartin 	for (i = 0; i < __arraycount(gpt_fs_types); i++)
930dcc57e24Smartin 		if (fstype == gpt_fs_types[i].fstype)
931dcc57e24Smartin 			return gpt_find_type(gpt_fs_types[i].name);
932dcc57e24Smartin 
9338ac26f1aSmartin 	return NULL;
9348ac26f1aSmartin }
9358ac26f1aSmartin 
93634f3e937Smartin static bool
93734f3e937Smartin gpt_get_default_fstype(const struct part_type_desc *nat_type,
93834f3e937Smartin     unsigned *fstype, unsigned *fs_sub_type)
93934f3e937Smartin {
94034f3e937Smartin 	const struct gpt_ptype_desc *gtype;
94134f3e937Smartin 
94234f3e937Smartin 	gtype = gpt_find_native_type(nat_type);
94334f3e937Smartin 	if (gtype == NULL)
94434f3e937Smartin 		return false;
94534f3e937Smartin 
94634f3e937Smartin 	*fstype = gtype->default_fs_type;
94734f3e937Smartin #ifdef DEFAULT_UFS2
94834f3e937Smartin 	if (gtype->default_fs_type == FS_BSDFFS)
94934f3e937Smartin 		*fs_sub_type = 2;
95034f3e937Smartin 	else
95134f3e937Smartin #endif
95234f3e937Smartin 		*fs_sub_type = 0;
95334f3e937Smartin 	return true;
95434f3e937Smartin }
95534f3e937Smartin 
9568ac26f1aSmartin static const struct part_type_desc *
9578ac26f1aSmartin gpt_get_uuid_part_type(const uuid_t *id)
9588ac26f1aSmartin {
9598ac26f1aSmartin 	char str[GUID_STR_LEN], desc[GUID_STR_LEN + MENUSTRSIZE];
9608ac26f1aSmartin 	const struct gpt_ptype_desc *t;
9618ac26f1aSmartin 	char *guid = NULL;
9628ac26f1aSmartin 	uint32_t err;
9638ac26f1aSmartin 
9648ac26f1aSmartin 	uuid_to_string(id, &guid, &err);
9658ac26f1aSmartin 	strlcpy(str, err == uuid_s_ok ? guid : "-", sizeof str);
9668ac26f1aSmartin 	free(guid);
9678ac26f1aSmartin 
9688ac26f1aSmartin 	t = gpt_find_guid_type(str);
9698ac26f1aSmartin 	if (t == NULL) {
9708ac26f1aSmartin 		snprintf(desc, sizeof desc, "%s (%s)",
9718ac26f1aSmartin 		    msg_string(MSG_custom_type), str);
9728ac26f1aSmartin 		gpt_internal_add_ptype(str, str, desc);
9738ac26f1aSmartin 		t = gpt_find_guid_type(str);
9748ac26f1aSmartin 		assert(t != NULL);
9758ac26f1aSmartin 	}
9768ac26f1aSmartin 	return &t->gent;
9778ac26f1aSmartin }
9788ac26f1aSmartin 
9798ac26f1aSmartin static const struct part_type_desc *
9808ac26f1aSmartin gpt_create_custom_part_type(const char *custom, const char **err_msg)
9818ac26f1aSmartin {
9828ac26f1aSmartin 	uuid_t id;
9838ac26f1aSmartin 	uint32_t err;
9848ac26f1aSmartin 
9858ac26f1aSmartin 	uuid_from_string(custom, &id, &err);
9868ac26f1aSmartin 	if (err_msg != NULL &&
9878ac26f1aSmartin 	   (err == uuid_s_invalid_string_uuid || err == uuid_s_bad_version)) {
9888ac26f1aSmartin 		*err_msg = MSG_invalid_guid;
9898ac26f1aSmartin 		return NULL;
9908ac26f1aSmartin 	}
9918ac26f1aSmartin 	if (err != uuid_s_ok)
9928ac26f1aSmartin 		return NULL;
9938ac26f1aSmartin 
9948ac26f1aSmartin 	return gpt_get_uuid_part_type(&id);
9958ac26f1aSmartin }
9968ac26f1aSmartin 
9978ac26f1aSmartin static const struct part_type_desc *
9988ac26f1aSmartin gpt_create_unknown_part_type(void)
9998ac26f1aSmartin {
10008ac26f1aSmartin 	uuid_t id;
10018ac26f1aSmartin 	uint32_t err;
10028ac26f1aSmartin 
10038ac26f1aSmartin 	uuid_create(&id, &err);
10048ac26f1aSmartin 	if (err != uuid_s_ok)
10058ac26f1aSmartin 		return NULL;
10068ac26f1aSmartin 
10078ac26f1aSmartin 	return gpt_get_uuid_part_type(&id);
1008dcc57e24Smartin }
1009dcc57e24Smartin 
1010dcc57e24Smartin static daddr_t
1011dcc57e24Smartin gpt_get_part_alignment(const struct disk_partitions *parts)
1012dcc57e24Smartin {
1013dcc57e24Smartin 
1014dcc57e24Smartin 	assert(parts->disk_size > 0);
1015dcc57e24Smartin 	if (parts->disk_size < 0)
1016dcc57e24Smartin 		return 1;
1017dcc57e24Smartin 
1018dcc57e24Smartin 	/* Use 1MB offset/alignemnt for large (>128GB) disks */
1019dcc57e24Smartin 	if (parts->disk_size > HUGE_DISK_SIZE)
1020dcc57e24Smartin 		return 2048;
1021dcc57e24Smartin 	else if (parts->disk_size > TINY_DISK_SIZE)
1022dcc57e24Smartin 		return 64;
1023dcc57e24Smartin 	else
1024dcc57e24Smartin 		return 4;
1025dcc57e24Smartin }
1026dcc57e24Smartin 
1027dcc57e24Smartin static bool
1028dcc57e24Smartin gpt_can_add_partition(const struct disk_partitions *arg)
1029dcc57e24Smartin {
1030dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1031dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1032dcc57e24Smartin 	struct disk_part_free_space space;
1033dcc57e24Smartin 	daddr_t align;
1034dcc57e24Smartin 
1035dcc57e24Smartin 	if (parts->dp.num_part >= parts->max_num_parts)
1036dcc57e24Smartin 		return false;
1037dcc57e24Smartin 
1038dcc57e24Smartin 	align = gpt_get_part_alignment(arg);
1039dcc57e24Smartin 	if (parts->dp.free_space <= align)
1040dcc57e24Smartin 		return false;
1041dcc57e24Smartin 
1042dcc57e24Smartin 	if (gpt_get_free_spaces_internal(parts, &space, 1, align, align,
1043dcc57e24Smartin 	    0, -1) < 1)
1044dcc57e24Smartin 		return false;
1045dcc57e24Smartin 
1046dcc57e24Smartin 	return true;
1047dcc57e24Smartin }
1048dcc57e24Smartin 
1049dcc57e24Smartin static bool
1050dcc57e24Smartin gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
1051dcc57e24Smartin     const char **err_msg)
1052dcc57e24Smartin {
1053dcc57e24Smartin 	p->gp_type = gpt_find_native_type(info->nat_type);
1054dcc57e24Smartin 	p->gp_start = info->start;
1055dcc57e24Smartin 	p->gp_size = info->size;
1056dcc57e24Smartin 	if (info->last_mounted != NULL && info->last_mounted !=
1057dcc57e24Smartin 	    p->last_mounted) {
1058dcc57e24Smartin 		free(__UNCONST(p->last_mounted));
1059dcc57e24Smartin 		p->last_mounted = strdup(info->last_mounted);
1060dcc57e24Smartin 	}
1061dcc57e24Smartin 	p->fs_type = info->fs_type;
1062dcc57e24Smartin 	p->fs_sub_type = info->fs_sub_type;
1063b162b00bSmartin 	p->fs_opt1 = info->fs_opt1;
1064b162b00bSmartin 	p->fs_opt2 = info->fs_opt2;
1065b162b00bSmartin 	p->fs_opt3 = info->fs_opt3;
1066dcc57e24Smartin 
1067dcc57e24Smartin 	return true;
1068dcc57e24Smartin }
1069dcc57e24Smartin 
1070dcc57e24Smartin static part_id
1071dcc57e24Smartin gpt_add_part(struct disk_partitions *arg,
1072dcc57e24Smartin     const struct disk_part_info *info, const char **err_msg)
1073dcc57e24Smartin {
1074dcc57e24Smartin 	struct gpt_disk_partitions *parts =
1075dcc57e24Smartin 	    (struct gpt_disk_partitions*)arg;
1076dcc57e24Smartin 	struct disk_part_free_space space;
1077dcc57e24Smartin 	struct disk_part_info data = *info;
1078dcc57e24Smartin 	struct gpt_part_entry *p;
1079dcc57e24Smartin 	bool ok;
1080dcc57e24Smartin 
1081dcc57e24Smartin 	if (err_msg != NULL)
1082dcc57e24Smartin 		*err_msg = NULL;
1083dcc57e24Smartin 
1084dcc57e24Smartin 	if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 1,
1085dcc57e24Smartin 	    info->start, -1) < 1) {
1086dcc57e24Smartin 		if (err_msg)
1087dcc57e24Smartin 			*err_msg = msg_string(MSG_No_free_space);
1088dcc57e24Smartin 		return NO_PART;
1089dcc57e24Smartin 	}
1090dcc57e24Smartin 	if (parts->dp.num_part >= parts->max_num_parts) {
1091dcc57e24Smartin 		if (err_msg)
1092dcc57e24Smartin 			*err_msg = msg_string(MSG_err_too_many_partitions);
1093dcc57e24Smartin 		return NO_PART;
1094dcc57e24Smartin 	}
1095dcc57e24Smartin 
1096dcc57e24Smartin 	if (data.size > space.size)
1097dcc57e24Smartin 		data.size = space.size;
1098dcc57e24Smartin 
1099dcc57e24Smartin 	p = calloc(1, sizeof(*p));
1100dcc57e24Smartin 	if (p == NULL) {
1101dcc57e24Smartin 		if (err_msg != NULL)
1102dcc57e24Smartin 			*err_msg = INTERNAL_ERROR;
1103dcc57e24Smartin 		return NO_PART;
1104dcc57e24Smartin 	}
1105dcc57e24Smartin 	if (!gpt_info_to_part(p, &data, err_msg)) {
1106dcc57e24Smartin 		free(p);
1107dcc57e24Smartin 		return NO_PART;
1108dcc57e24Smartin 	}
1109dcc57e24Smartin 	p->gp_flags |= GPEF_MODIFIED;
1110dcc57e24Smartin 	ok = gpt_insert_part_into_list(parts, &parts->partitions, p, err_msg);
1111dcc57e24Smartin 	if (ok) {
1112dcc57e24Smartin 		parts->dp.num_part++;
1113dcc57e24Smartin 		parts->dp.free_space -= p->gp_size;
1114dcc57e24Smartin 		return parts->dp.num_part-1;
1115dcc57e24Smartin 	} else {
1116dcc57e24Smartin 		free(p);
1117dcc57e24Smartin 		return NO_PART;
1118dcc57e24Smartin 	}
1119dcc57e24Smartin }
1120dcc57e24Smartin 
1121dcc57e24Smartin static bool
1122dcc57e24Smartin gpt_delete_partition(struct disk_partitions *arg, part_id id,
1123dcc57e24Smartin     const char **err_msg)
1124dcc57e24Smartin {
1125dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1126dcc57e24Smartin 	struct gpt_part_entry *p, *last = NULL;
1127dcc57e24Smartin 	part_id i;
1128dcc57e24Smartin 	bool res;
1129dcc57e24Smartin 
1130dcc57e24Smartin 	if (parts->dp.num_part == 0)
1131dcc57e24Smartin 		return false;
1132dcc57e24Smartin 
1133dcc57e24Smartin 	for (i = 0, p = parts->partitions;
1134dcc57e24Smartin 	    i != id && i < parts->dp.num_part && p != NULL;
1135dcc57e24Smartin 	    i++, p = p->gp_next)
1136dcc57e24Smartin 		last = p;
1137dcc57e24Smartin 
1138dcc57e24Smartin 	if (p == NULL) {
1139dcc57e24Smartin 		if (err_msg)
1140dcc57e24Smartin 			*err_msg = INTERNAL_ERROR;
1141dcc57e24Smartin 		return false;
1142dcc57e24Smartin 	}
1143dcc57e24Smartin 
1144dcc57e24Smartin 	if (last == NULL)
1145dcc57e24Smartin 		parts->partitions = p->gp_next;
1146dcc57e24Smartin 	else
1147dcc57e24Smartin 		last->gp_next = p->gp_next;
1148dcc57e24Smartin 
1149dcc57e24Smartin 	res = true;
1150dcc57e24Smartin 	if (p->gp_flags & GPEF_ON_DISK) {
1151dcc57e24Smartin 		if (!gpt_insert_part_into_list(parts, &parts->obsolete,
1152dcc57e24Smartin 		    p, err_msg))
1153dcc57e24Smartin 			res = false;
1154dcc57e24Smartin 	} else {
1155dcc57e24Smartin 		free(p);
1156dcc57e24Smartin 	}
1157dcc57e24Smartin 
1158dcc57e24Smartin 	if (res) {
1159dcc57e24Smartin 		parts->dp.num_part--;
1160dcc57e24Smartin 		parts->dp.free_space += p->gp_size;
1161dcc57e24Smartin 	}
1162dcc57e24Smartin 
1163dcc57e24Smartin 	return res;
1164dcc57e24Smartin }
1165dcc57e24Smartin 
1166dcc57e24Smartin static bool
1167dcc57e24Smartin gpt_delete_all_partitions(struct disk_partitions *arg)
1168dcc57e24Smartin {
1169dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1170dcc57e24Smartin 
1171dcc57e24Smartin 	while (parts->dp.num_part > 0) {
1172dcc57e24Smartin 		if (!gpt_delete_partition(&parts->dp, 0, NULL))
1173dcc57e24Smartin 			return false;
1174dcc57e24Smartin 	}
1175dcc57e24Smartin 
1176dcc57e24Smartin 	return true;
1177dcc57e24Smartin }
1178dcc57e24Smartin 
1179dcc57e24Smartin static bool
1180dcc57e24Smartin gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
1181dcc57e24Smartin {
1182dcc57e24Smartin 	char *textbuf, *t, *tt;
1183dcc57e24Smartin 	static const char expected_hdr[] = "Details for index ";
1184dcc57e24Smartin 
1185dcc57e24Smartin 	/* run gpt show for this partition */
1186dcc57e24Smartin 	if (collect(T_OUTPUT, &textbuf,
1187dcc57e24Smartin 	    "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
1188dcc57e24Smartin 		return false;
1189dcc57e24Smartin 
1190dcc57e24Smartin 	/*
1191dcc57e24Smartin 	 * gpt show should respond with single partition details, but will
1192dcc57e24Smartin 	 * fall back to "show -a" output if something is wrong
1193dcc57e24Smartin 	 */
1194dcc57e24Smartin 	t = strtok(textbuf, "\n"); /* first line is special */
1195dcc57e24Smartin 	if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
1196dcc57e24Smartin 		free(textbuf);
1197dcc57e24Smartin 		return false;
1198dcc57e24Smartin 	}
1199dcc57e24Smartin 
1200dcc57e24Smartin 	/* parse output into "old" */
1201dcc57e24Smartin 	while ((t = strtok(NULL, "\n")) != NULL) {
1202dcc57e24Smartin 		tt = strsep(&t, " \t");
1203dcc57e24Smartin 		if (strlen(tt) == 0)
1204dcc57e24Smartin 			continue;
1205dcc57e24Smartin 		gpt_add_info(p, tt, t, true);
1206dcc57e24Smartin 	}
1207dcc57e24Smartin 	free(textbuf);
1208dcc57e24Smartin 
1209dcc57e24Smartin 	return true;
1210dcc57e24Smartin }
1211dcc57e24Smartin 
1212dcc57e24Smartin static bool
1213dcc57e24Smartin gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
1214dcc57e24Smartin {
1215dcc57e24Smartin 	size_t i;
1216dcc57e24Smartin 	char attr_str[STRSIZE];
1217dcc57e24Smartin 
1218dcc57e24Smartin 	if (todo == 0)
1219dcc57e24Smartin 		return true;
1220dcc57e24Smartin 
1221dcc57e24Smartin 	strcpy(attr_str, "-a ");
1222dcc57e24Smartin 	for (i = 0; todo != 0; i++) {
1223dcc57e24Smartin 		if (!(gpt_avail_attrs[i].flag & todo))
1224dcc57e24Smartin 			continue;
1225dcc57e24Smartin 		todo &= ~gpt_avail_attrs[i].flag;
1226dcc57e24Smartin 		if (attr_str[0])
1227dcc57e24Smartin 			strlcat(attr_str, ",",
1228dcc57e24Smartin 			    sizeof(attr_str));
1229dcc57e24Smartin 		strlcat(attr_str,
1230dcc57e24Smartin 		    gpt_avail_attrs[i].name,
1231dcc57e24Smartin 		    sizeof(attr_str));
1232dcc57e24Smartin 	}
1233dcc57e24Smartin 	if (run_program(RUN_SILENT,
1234dcc57e24Smartin 	    "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
1235dcc57e24Smartin 		return false;
1236dcc57e24Smartin 	return true;
1237dcc57e24Smartin }
1238dcc57e24Smartin 
1239dcc57e24Smartin /*
1240dcc57e24Smartin  * Modify an existing on-disk partition.
1241dcc57e24Smartin  * Start and size can not be changed here, caller needs to deal
1242dcc57e24Smartin  * with that kind of changes upfront.
1243dcc57e24Smartin  */
1244dcc57e24Smartin static bool
1245dcc57e24Smartin gpt_modify_part(const char *disk, struct gpt_part_entry *p)
1246dcc57e24Smartin {
1247dcc57e24Smartin 	struct gpt_part_entry old;
1248dcc57e24Smartin 	uint todo_set, todo_unset;
1249dcc57e24Smartin 
1250dcc57e24Smartin 	/*
1251dcc57e24Smartin 	 * Query current on-disk state
1252dcc57e24Smartin 	 */
1253dcc57e24Smartin 	memset(&old, 0, sizeof old);
1254dcc57e24Smartin 	if (!gpt_read_part(disk, p->gp_start, &old))
1255dcc57e24Smartin 		return false;
1256dcc57e24Smartin 
1257dcc57e24Smartin 	/* Reject unsupported changes */
1258dcc57e24Smartin 	if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
1259dcc57e24Smartin 		return false;
1260dcc57e24Smartin 
1261dcc57e24Smartin 	/*
1262dcc57e24Smartin 	 * GUID should never change, but the internal copy
1263dcc57e24Smartin 	 * may not yet know it.
1264dcc57e24Smartin 	 */
1265dcc57e24Smartin 	strcpy(p->gp_id, old.gp_id);
1266dcc57e24Smartin 
1267dcc57e24Smartin 	/* Check type */
1268dcc57e24Smartin 	if (p->gp_type != old.gp_type) {
1269dcc57e24Smartin 		if (run_program(RUN_SILENT,
12705d65c236Smartin 		    "gpt type -b %" PRIu64 " -T %s %s",
1271dcc57e24Smartin 		    p->gp_start, p->gp_type->tid, disk) != 0)
1272dcc57e24Smartin 			return false;
1273dcc57e24Smartin 	}
1274dcc57e24Smartin 
1275dcc57e24Smartin 	/* Check label */
1276dcc57e24Smartin 	if (strcmp(p->gp_label, old.gp_label) != 0) {
1277dcc57e24Smartin 		if (run_program(RUN_SILENT,
1278c0e3ce44Smartin 		    "gpt label -b %" PRIu64 " -l \'%s\' %s",
1279dcc57e24Smartin 		    p->gp_start, p->gp_label, disk) != 0)
1280dcc57e24Smartin 			return false;
1281dcc57e24Smartin 	}
1282dcc57e24Smartin 
1283dcc57e24Smartin 	/* Check attributes */
1284dcc57e24Smartin 	if (p->gp_attr != old.gp_attr) {
1285dcc57e24Smartin 		if (p->gp_attr == 0) {
1286dcc57e24Smartin 			if (run_program(RUN_SILENT,
1287dcc57e24Smartin 			    "gpt set -N -b %" PRIu64 " %s",
1288dcc57e24Smartin 			    p->gp_start, disk) != 0)
1289dcc57e24Smartin 				return false;
1290dcc57e24Smartin 		} else {
1291dcc57e24Smartin 			todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
1292dcc57e24Smartin 			todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
1293dcc57e24Smartin 			if (!gpt_apply_attr(disk, "unset", p->gp_start,
1294dcc57e24Smartin 			    todo_unset))
1295dcc57e24Smartin 				return false;
1296dcc57e24Smartin 			if (!gpt_apply_attr(disk, "set", p->gp_start,
1297dcc57e24Smartin 			    todo_set))
1298dcc57e24Smartin 				return false;
1299dcc57e24Smartin 		}
1300dcc57e24Smartin 	}
1301dcc57e24Smartin 
1302dcc57e24Smartin 	return true;
1303dcc57e24Smartin }
1304dcc57e24Smartin 
1305dcc57e24Smartin /*
1306dcc57e24Smartin  * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
1307dcc57e24Smartin  *  map FS_* to wedge strings
1308dcc57e24Smartin  */
1309dcc57e24Smartin static const char *
1310dcc57e24Smartin bsdlabel_fstype_to_str(uint8_t fstype)
1311dcc57e24Smartin {
1312dcc57e24Smartin 	const char *str;
1313dcc57e24Smartin 
1314dcc57e24Smartin 	/*
1315dcc57e24Smartin 	 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
1316dcc57e24Smartin 	 * a suitable case branch will convert the type number to a string.
1317dcc57e24Smartin 	 */
1318dcc57e24Smartin 	switch (fstype) {
1319dcc57e24Smartin #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
1320dcc57e24Smartin 	case __CONCAT(FS_,tag):	str = __CONCAT(DKW_PTYPE_,tag);			break;
1321dcc57e24Smartin 	FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
1322dcc57e24Smartin #undef FSTYPE_TO_STR_CASE
1323dcc57e24Smartin 	default:		str = NULL;			break;
1324dcc57e24Smartin 	}
1325dcc57e24Smartin 
1326dcc57e24Smartin 	return (str);
1327dcc57e24Smartin }
1328dcc57e24Smartin 
1329642571f8Smartin /*
1330642571f8Smartin  * diskfd is an open file descriptor for a disk we had trouble with
1331642571f8Smartin  * creating some new wedges.
1332642571f8Smartin  * Go through all wedges actually on that disk, check if we have a
1333642571f8Smartin  * record for them and remove all others.
1334642571f8Smartin  * This should sync our internal model of partitions with the real state.
1335642571f8Smartin  */
1336642571f8Smartin static void
1337642571f8Smartin gpt_sanitize(int diskfd, const struct gpt_disk_partitions *parts,
1338642571f8Smartin     struct gpt_part_entry *ignore)
1339642571f8Smartin {
1340642571f8Smartin 	struct dkwedge_info *dkw, delw;
1341642571f8Smartin 	struct dkwedge_list dkwl;
1342642571f8Smartin 	size_t bufsize;
1343642571f8Smartin 	u_int i;
1344642571f8Smartin 
1345642571f8Smartin 	dkw = NULL;
1346642571f8Smartin 	dkwl.dkwl_buf = dkw;
1347642571f8Smartin 	dkwl.dkwl_bufsize = 0;
1348642571f8Smartin 
1349642571f8Smartin 	/* get a list of all wedges */
1350642571f8Smartin 	for (;;) {
1351642571f8Smartin 		if (ioctl(diskfd, DIOCLWEDGES, &dkwl) == -1)
1352642571f8Smartin 			return;
1353642571f8Smartin 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
1354642571f8Smartin 			break;
1355642571f8Smartin 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
1356642571f8Smartin 		if (dkwl.dkwl_bufsize < bufsize) {
1357642571f8Smartin 			dkw = realloc(dkwl.dkwl_buf, bufsize);
1358642571f8Smartin 			if (dkw == NULL)
1359642571f8Smartin 				return;
1360642571f8Smartin 			dkwl.dkwl_buf = dkw;
1361642571f8Smartin 			dkwl.dkwl_bufsize = bufsize;
1362642571f8Smartin 		}
1363642571f8Smartin 	}
1364642571f8Smartin 
1365642571f8Smartin 	/* try to remove all the ones we do not know about */
1366642571f8Smartin 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
1367642571f8Smartin 		bool found = false;
1368642571f8Smartin 		const char *devname = dkw[i].dkw_devname;
1369642571f8Smartin 
1370642571f8Smartin 		for (struct gpt_part_entry *pe = parts->partitions;
1371642571f8Smartin 		    pe != NULL; pe = pe->gp_next) {
1372642571f8Smartin 			if (pe == ignore)
1373642571f8Smartin 				continue;
1374642571f8Smartin 			if ((pe->gp_flags & GPEF_WEDGE) &&
1375642571f8Smartin 			    strcmp(pe->gp_dev_name, devname) == 0) {
1376642571f8Smartin 				found = true;
1377642571f8Smartin 				break;
1378642571f8Smartin 			}
1379642571f8Smartin 		}
1380642571f8Smartin 		if (found)
1381642571f8Smartin 			continue;
1382642571f8Smartin 		memset(&delw, 0, sizeof(delw));
1383*7920585fSmartin 		strlcpy(delw.dkw_devname, devname, sizeof(delw.dkw_devname));
1384642571f8Smartin 		(void)ioctl(diskfd, DIOCDWEDGE, &delw);
1385642571f8Smartin 	}
1386642571f8Smartin 
1387642571f8Smartin 	/* cleanup */
1388642571f8Smartin 	free(dkw);
1389642571f8Smartin }
1390642571f8Smartin 
1391dcc57e24Smartin static bool
1392642571f8Smartin gpt_add_wedge(const char *disk, struct gpt_part_entry *p,
1393642571f8Smartin     const struct gpt_disk_partitions *parts)
1394dcc57e24Smartin {
1395dcc57e24Smartin 	struct dkwedge_info dkw;
1396dcc57e24Smartin 	const char *tname;
1397dcc57e24Smartin 	char diskpath[MAXPATHLEN];
1398dcc57e24Smartin 	int fd;
1399dcc57e24Smartin 
1400dcc57e24Smartin 	memset(&dkw, 0, sizeof(dkw));
1401dcc57e24Smartin 	tname = bsdlabel_fstype_to_str(p->fs_type);
1402dcc57e24Smartin 	if (tname)
1403dcc57e24Smartin 		strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
1404dcc57e24Smartin 
1405dcc57e24Smartin 	strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
1406dcc57e24Smartin 	dkw.dkw_offset = p->gp_start;
1407dcc57e24Smartin 	dkw.dkw_size = p->gp_size;
140834f3e937Smartin 	if (dkw.dkw_wname[0] == 0) {
140934f3e937Smartin 		if (p->gp_label[0] != 0)
141034f3e937Smartin 				strlcpy((char*)&dkw.dkw_wname,
141134f3e937Smartin 				    p->gp_label, sizeof(dkw.dkw_wname));
141234f3e937Smartin 	}
141334f3e937Smartin 	if (dkw.dkw_wname[0] == 0) {
141434f3e937Smartin 		snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
141534f3e937Smartin 		    "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
141634f3e937Smartin 	}
1417dcc57e24Smartin 
1418dcc57e24Smartin 	fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1419dcc57e24Smartin 	if (fd < 0)
1420dcc57e24Smartin 		return false;
1421dcc57e24Smartin 	if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
1422642571f8Smartin 		if (errno == EINVAL) {
1423642571f8Smartin 			/* sanitize existing wedges and try again */
1424642571f8Smartin 			gpt_sanitize(fd, parts, p);
1425642571f8Smartin 			if (ioctl(fd, DIOCAWEDGE, &dkw) == 0)
1426642571f8Smartin 				goto ok;
1427642571f8Smartin 		}
1428dcc57e24Smartin 		close(fd);
1429dcc57e24Smartin 		return false;
1430dcc57e24Smartin 	}
1431642571f8Smartin ok:
1432dcc57e24Smartin 	close(fd);
1433dcc57e24Smartin 
1434dcc57e24Smartin 	strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
1435dcc57e24Smartin 	p->gp_flags |= GPEF_WEDGE;
1436dcc57e24Smartin 	return true;
1437dcc57e24Smartin }
1438dcc57e24Smartin 
1439c0e3ce44Smartin static void
1440c0e3ce44Smartin escape_spaces(char *dest, const char *src)
1441c0e3ce44Smartin {
1442c0e3ce44Smartin 	unsigned char c;
1443c0e3ce44Smartin 
1444c0e3ce44Smartin 	while (*src) {
1445c0e3ce44Smartin 		c = *src++;
1446c0e3ce44Smartin 		if (isspace(c) || c == '\\')
1447c0e3ce44Smartin 			*dest++ = '\\';
1448c0e3ce44Smartin 		*dest++ = c;
1449c0e3ce44Smartin 	}
1450c0e3ce44Smartin 	*dest = 0;
1451c0e3ce44Smartin }
1452c0e3ce44Smartin 
1453dcc57e24Smartin static bool
1454dcc57e24Smartin gpt_get_part_device(const struct disk_partitions *arg,
1455dcc57e24Smartin     part_id id, char *devname, size_t max_devname_len, int *part,
145634f3e937Smartin     enum dev_name_usage usage, bool with_path, bool life)
1457dcc57e24Smartin {
1458dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1459dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1460dcc57e24Smartin 	struct  gpt_part_entry *p = parts->partitions;
1461c0e3ce44Smartin 	char tmpname[GPT_LABEL_LEN*2];
1462dcc57e24Smartin 	part_id no;
1463dcc57e24Smartin 
1464dcc57e24Smartin 
1465dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
1466dcc57e24Smartin 		p = p->gp_next;
1467dcc57e24Smartin 
1468dcc57e24Smartin 	if (no != id || p == NULL)
1469dcc57e24Smartin 		return false;
1470dcc57e24Smartin 
1471dcc57e24Smartin 	if (part)
1472dcc57e24Smartin 		*part = -1;
1473dcc57e24Smartin 
147434f3e937Smartin 	if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
147534f3e937Smartin 		usage = plain_name;
147634f3e937Smartin 	if (usage == plain_name || usage == raw_dev_name)
147734f3e937Smartin 		life = true;
147894f5fa4cSmartin 	if (!(p->gp_flags & GPEF_WEDGE) && life &&
1479642571f8Smartin 	    !gpt_add_wedge(arg->disk, p, parts))
148094f5fa4cSmartin 		return false;
1481dcc57e24Smartin 
1482dcc57e24Smartin 	switch (usage) {
1483dcc57e24Smartin 	case logical_name:
1484c0e3ce44Smartin 		if (p->gp_label[0] != 0) {
1485c0e3ce44Smartin 			escape_spaces(tmpname, p->gp_label);
1486dcc57e24Smartin 			snprintf(devname, max_devname_len,
1487c0e3ce44Smartin 			    "NAME=%s", tmpname);
1488c0e3ce44Smartin 		} else {
1489dcc57e24Smartin 			snprintf(devname, max_devname_len,
1490dcc57e24Smartin 			    "NAME=%s", p->gp_id);
1491c0e3ce44Smartin 		}
1492dcc57e24Smartin 		break;
1493dcc57e24Smartin 	case plain_name:
1494dcc57e24Smartin 		assert(p->gp_flags & GPEF_WEDGE);
1495dcc57e24Smartin 		if (with_path)
1496dcc57e24Smartin 			snprintf(devname, max_devname_len, _PATH_DEV "%s",
1497dcc57e24Smartin 			    p->gp_dev_name);
1498dcc57e24Smartin 		else
1499dcc57e24Smartin 			strlcpy(devname, p->gp_dev_name, max_devname_len);
1500dcc57e24Smartin 		break;
1501dcc57e24Smartin 	case raw_dev_name:
1502dcc57e24Smartin 		assert(p->gp_flags & GPEF_WEDGE);
1503dcc57e24Smartin 		if (with_path)
1504dcc57e24Smartin 			snprintf(devname, max_devname_len, _PATH_DEV "r%s",
1505dcc57e24Smartin 			    p->gp_dev_name);
1506dcc57e24Smartin 		else
1507dcc57e24Smartin 			snprintf(devname, max_devname_len, "r%s",
1508dcc57e24Smartin 			    p->gp_dev_name);
1509dcc57e24Smartin 		break;
1510dcc57e24Smartin 	default:
1511dcc57e24Smartin 		return false;
1512dcc57e24Smartin 	}
1513dcc57e24Smartin 
1514dcc57e24Smartin 	return true;
1515dcc57e24Smartin }
1516dcc57e24Smartin 
1517dcc57e24Smartin static bool
1518dcc57e24Smartin gpt_write_to_disk(struct disk_partitions *arg)
1519dcc57e24Smartin {
1520dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1521dcc57e24Smartin 	struct gpt_part_entry *p, *n;
1522c0e3ce44Smartin 	char label_arg[sizeof(p->gp_label) + 10];
1523dcc57e24Smartin 	char diskpath[MAXPATHLEN];
1524dcc57e24Smartin 	int fd, bits = 0;
1525dcc57e24Smartin 	bool root_is_new = false, efi_is_new = false;
1526dcc57e24Smartin 	part_id root_id = NO_PART, efi_id = NO_PART, pno;
1527dcc57e24Smartin 
1528dcc57e24Smartin 	/*
1529dcc57e24Smartin 	 * Remove all wedges on this disk - they may become invalid and we
1530dcc57e24Smartin 	 * have no easy way to associate them with the partitioning data.
1531dcc57e24Smartin 	 * Instead we will explicitly request creation of wedges on demand
1532dcc57e24Smartin 	 * later.
1533dcc57e24Smartin 	 */
1534dcc57e24Smartin 	fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1535dcc57e24Smartin 	if (fd < 0)
1536dcc57e24Smartin 		return false;
1537dcc57e24Smartin 	if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
1538dcc57e24Smartin 		return false;
1539dcc57e24Smartin 	close(fd);
1540dcc57e24Smartin 
1541dcc57e24Smartin 	/*
15428ac26f1aSmartin 	 * Collect first root and efi partition (if available), clear
15438ac26f1aSmartin 	 * "have wedge" flags.
1544dcc57e24Smartin 	 */
1545dcc57e24Smartin 	for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
15468ac26f1aSmartin 		p->gp_flags &= ~GPEF_WEDGE;
15476d3e0ec1Smartin 		if (root_id == NO_PART && p->gp_type != NULL) {
1548dcc57e24Smartin 			if (p->gp_type->gent.generic_ptype == PT_root &&
1549bf7de24aSmartin 			    (p->gp_flags & GPEF_TARGET)) {
1550dcc57e24Smartin 				root_id = pno;
1551dcc57e24Smartin 				root_is_new = !(p->gp_flags & GPEF_ON_DISK);
1552dcc57e24Smartin 			} else if (efi_id == NO_PART &&
1553dcc57e24Smartin 			    p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
1554dcc57e24Smartin 				efi_id = pno;
1555dcc57e24Smartin 				efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
1556dcc57e24Smartin 			}
1557dcc57e24Smartin 		}
1558dcc57e24Smartin 	}
1559dcc57e24Smartin 
1560dcc57e24Smartin 	/*
1561dcc57e24Smartin 	 * If no GPT on disk yet, create it.
1562dcc57e24Smartin 	 */
1563dcc57e24Smartin 	if (!parts->has_gpt) {
1564dcc57e24Smartin 		char limit[30];
1565dcc57e24Smartin 
1566dcc57e24Smartin 		if (parts->max_num_parts > 0)
1567dcc57e24Smartin 			sprintf(limit, "-p %zu", parts->max_num_parts);
1568dcc57e24Smartin 		else
1569dcc57e24Smartin 			limit[0] = 0;
1570dcc57e24Smartin 		if (run_program(RUN_SILENT, "gpt create %s %s",
1571dcc57e24Smartin 		    limit, parts->dp.disk))
1572dcc57e24Smartin 			return false;
1573dcc57e24Smartin 		parts->has_gpt = true;
1574dcc57e24Smartin 	}
1575dcc57e24Smartin 
1576dcc57e24Smartin 	/*
1577dcc57e24Smartin 	 * Delete all old partitions
1578dcc57e24Smartin 	 */
1579dcc57e24Smartin 	for (p = parts->obsolete; p != NULL; p = n) {
1580dcc57e24Smartin 		run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
1581dcc57e24Smartin 		    p->gp_start, arg->disk);
1582dcc57e24Smartin 		n = p->gp_next;
1583dcc57e24Smartin 		free(p);
1584dcc57e24Smartin 	}
1585dcc57e24Smartin 	parts->obsolete = NULL;
1586dcc57e24Smartin 
1587dcc57e24Smartin 	/*
1588dcc57e24Smartin 	 * Modify existing but changed partitions
1589dcc57e24Smartin 	 */
1590dcc57e24Smartin 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
1591dcc57e24Smartin 		if (!(p->gp_flags & GPEF_ON_DISK))
1592dcc57e24Smartin 			continue;
1593dcc57e24Smartin 
1594dcc57e24Smartin 		if (p->gp_flags & GPEF_RESIZED) {
1595dcc57e24Smartin 			run_program(RUN_SILENT,
1596dcc57e24Smartin 			    "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
1597dcc57e24Smartin 			    p->gp_start, p->gp_size, arg->disk);
1598dcc57e24Smartin 			p->gp_flags &= ~GPEF_RESIZED;
1599dcc57e24Smartin 		}
1600dcc57e24Smartin 
1601dcc57e24Smartin 		if (!(p->gp_flags & GPEF_MODIFIED))
1602dcc57e24Smartin 			continue;
1603dcc57e24Smartin 
1604dcc57e24Smartin 		if (!gpt_modify_part(parts->dp.disk, p))
1605dcc57e24Smartin 			return false;
1606dcc57e24Smartin 	}
1607dcc57e24Smartin 
1608dcc57e24Smartin 	/*
1609dcc57e24Smartin 	 * Add new partitions
1610dcc57e24Smartin 	 */
1611dcc57e24Smartin 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
1612dcc57e24Smartin 		if (p->gp_flags & GPEF_ON_DISK)
1613dcc57e24Smartin 			continue;
1614dcc57e24Smartin 		if (!(p->gp_flags & GPEF_MODIFIED))
1615dcc57e24Smartin 			continue;
1616dcc57e24Smartin 
1617dcc57e24Smartin 		if (p->gp_label[0] == 0)
1618dcc57e24Smartin 			label_arg[0] = 0;
1619dcc57e24Smartin 		else
1620c0e3ce44Smartin 			sprintf(label_arg, "-l \'%s\'", p->gp_label);
1621dcc57e24Smartin 
16226d3e0ec1Smartin 		if (p->gp_type != NULL)
1623dcc57e24Smartin 			run_program(RUN_SILENT,
16246d3e0ec1Smartin 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
16256d3e0ec1Smartin 			    "s -t %s %s %s",
1626dcc57e24Smartin 			    p->gp_start, p->gp_size, p->gp_type->tid,
1627dcc57e24Smartin 			    label_arg, arg->disk);
16286d3e0ec1Smartin 		else
16296d3e0ec1Smartin 			run_program(RUN_SILENT,
16306d3e0ec1Smartin 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
16316d3e0ec1Smartin 			    "s %s %s",
16326d3e0ec1Smartin 			    p->gp_start, p->gp_size, label_arg, arg->disk);
1633dcc57e24Smartin 		gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
1634dcc57e24Smartin 		gpt_read_part(arg->disk, p->gp_start, p);
1635dcc57e24Smartin 		p->gp_flags |= GPEF_ON_DISK;
1636dcc57e24Smartin 	}
1637dcc57e24Smartin 
1638dcc57e24Smartin 	/*
1639dcc57e24Smartin 	 * Additional MD bootloader magic...
1640dcc57e24Smartin 	 */
1641dcc57e24Smartin 	if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
1642dcc57e24Smartin 	    efi_is_new))
1643dcc57e24Smartin 		return false;
1644dcc57e24Smartin 
1645dcc57e24Smartin 	return true;
1646dcc57e24Smartin }
1647dcc57e24Smartin 
16486bb5c164Smartin static part_id
16496bb5c164Smartin gpt_find_by_name(struct disk_partitions *arg, const char *name)
16506bb5c164Smartin {
16516bb5c164Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
16526bb5c164Smartin 	struct gpt_part_entry *p;
16536bb5c164Smartin 	part_id pno;
16546bb5c164Smartin 
16556bb5c164Smartin 	for (pno = 0, p = parts->partitions; p != NULL;
16566bb5c164Smartin 	    p = p->gp_next, pno++) {
16576bb5c164Smartin 		if (strcmp(p->gp_label, name) == 0)
16586bb5c164Smartin 			return pno;
16596bb5c164Smartin 		if (strcmp(p->gp_id, name) == 0)
16606bb5c164Smartin 			return pno;
16616bb5c164Smartin 	}
16626bb5c164Smartin 
16636bb5c164Smartin 	return NO_PART;
16646bb5c164Smartin }
16656bb5c164Smartin 
1666dcc57e24Smartin bool
1667dcc57e24Smartin gpt_parts_check(void)
1668dcc57e24Smartin {
1669dcc57e24Smartin 
1670dcc57e24Smartin 	check_available_binaries();
1671dcc57e24Smartin 
1672dcc57e24Smartin 	return have_gpt && have_dk;
1673dcc57e24Smartin }
1674dcc57e24Smartin 
1675dcc57e24Smartin static void
1676dcc57e24Smartin gpt_free(struct disk_partitions *arg)
1677dcc57e24Smartin {
1678dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1679dcc57e24Smartin 	struct gpt_part_entry *p, *n;
1680dcc57e24Smartin 
1681dcc57e24Smartin 	assert(parts != NULL);
1682dcc57e24Smartin 	for (p = parts->partitions; p != NULL; p = n) {
1683dcc57e24Smartin 		free(__UNCONST(p->last_mounted));
1684dcc57e24Smartin 		n = p->gp_next;
1685dcc57e24Smartin 		free(p);
1686dcc57e24Smartin 	}
16878ac26f1aSmartin 	free(__UNCONST(parts->dp.disk));
1688dcc57e24Smartin 	free(parts);
1689dcc57e24Smartin }
1690dcc57e24Smartin 
169180bf2417Smartin static void
169280bf2417Smartin gpt_destroy_part_scheme(struct disk_partitions *arg)
169380bf2417Smartin {
169480bf2417Smartin 
169580bf2417Smartin 	run_program(RUN_SILENT, "gpt destroy %s", arg->disk);
169680bf2417Smartin 	gpt_free(arg);
169780bf2417Smartin }
169880bf2417Smartin 
1699dcc57e24Smartin static bool
1700dcc57e24Smartin gpt_custom_attribute_writable(const struct disk_partitions *arg,
1701dcc57e24Smartin     part_id ptn, size_t attr_no)
1702dcc57e24Smartin {
1703dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1704dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1705dcc57e24Smartin 	size_t i;
1706dcc57e24Smartin 	struct gpt_part_entry *p;
1707dcc57e24Smartin 
1708dcc57e24Smartin 	if (attr_no >= arg->pscheme->custom_attribute_count)
1709dcc57e24Smartin 		return false;
1710dcc57e24Smartin 
1711dcc57e24Smartin 	const msg label = arg->pscheme->custom_attributes[attr_no].label;
1712dcc57e24Smartin 
1713dcc57e24Smartin 	/* we can not edit the uuid attribute */
1714dcc57e24Smartin 	if (label == MSG_ptn_uuid)
1715dcc57e24Smartin 		return false;
1716dcc57e24Smartin 
1717dcc57e24Smartin 	/* the label is always editable */
1718dcc57e24Smartin 	if (label == MSG_ptn_label)
1719dcc57e24Smartin 		return true;
1720dcc57e24Smartin 
1721dcc57e24Smartin 	/* the GPT type is read only */
1722dcc57e24Smartin 	if (label == MSG_ptn_gpt_type)
1723dcc57e24Smartin 		return false;
1724dcc57e24Smartin 
1725dcc57e24Smartin 	/* BOOTME makes no sense on swap partitions */
1726dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1727dcc57e24Smartin 		if (i == ptn)
1728dcc57e24Smartin 			break;
1729dcc57e24Smartin 
1730dcc57e24Smartin 	if (p == NULL)
1731dcc57e24Smartin 		return false;
1732dcc57e24Smartin 
17336d3e0ec1Smartin 	if (p->fs_type == FS_SWAP ||
17346d3e0ec1Smartin 	    (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
1735dcc57e24Smartin 		return false;
1736dcc57e24Smartin 
1737dcc57e24Smartin 	return true;
1738dcc57e24Smartin }
1739dcc57e24Smartin 
17405823c3d0Smartin static const char *
17415823c3d0Smartin gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
17425823c3d0Smartin {
17435823c3d0Smartin 	const struct gpt_disk_partitions *parts =
17445823c3d0Smartin 	    (const struct gpt_disk_partitions*)arg;
17455823c3d0Smartin 	size_t i;
17465823c3d0Smartin 	struct gpt_part_entry *p;
17475823c3d0Smartin 
17485823c3d0Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
17495823c3d0Smartin 		if (i == ptn)
17505823c3d0Smartin 			break;
17515823c3d0Smartin 
17525823c3d0Smartin 	if (p == NULL)
17535823c3d0Smartin 		return NULL;
17545823c3d0Smartin 
17555823c3d0Smartin 	if (p->gp_label[0] != 0)
17565823c3d0Smartin 		return p->gp_label;
17575823c3d0Smartin 	return p->gp_id;
17585823c3d0Smartin }
17595823c3d0Smartin 
1760dcc57e24Smartin static bool
1761dcc57e24Smartin gpt_format_custom_attribute(const struct disk_partitions *arg,
1762dcc57e24Smartin     part_id ptn, size_t attr_no, const struct disk_part_info *info,
1763dcc57e24Smartin     char *out, size_t out_space)
1764dcc57e24Smartin {
1765dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1766dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1767dcc57e24Smartin 	size_t i;
1768dcc57e24Smartin 	struct gpt_part_entry *p, data;
1769dcc57e24Smartin 
1770dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1771dcc57e24Smartin 		if (i == ptn)
1772dcc57e24Smartin 			break;
1773dcc57e24Smartin 
1774dcc57e24Smartin 	if (p == NULL)
1775dcc57e24Smartin 		return false;
1776dcc57e24Smartin 
1777dcc57e24Smartin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1778dcc57e24Smartin 		return false;
1779dcc57e24Smartin 
1780dcc57e24Smartin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1781dcc57e24Smartin 
1782dcc57e24Smartin 	if (info != NULL) {
1783dcc57e24Smartin 		data = *p;
1784dcc57e24Smartin 		gpt_info_to_part(&data, info, NULL);
1785dcc57e24Smartin 		p = &data;
1786dcc57e24Smartin 	}
1787dcc57e24Smartin 
1788dcc57e24Smartin 	if (label == MSG_ptn_label)
1789dcc57e24Smartin 		strlcpy(out, p->gp_label, out_space);
1790dcc57e24Smartin 	else if (label == MSG_ptn_uuid)
1791dcc57e24Smartin 		strlcpy(out, p->gp_id, out_space);
17926d3e0ec1Smartin 	else if (label == MSG_ptn_gpt_type) {
17936d3e0ec1Smartin 		if (p->gp_type != NULL)
1794dcc57e24Smartin 			strlcpy(out, p->gp_type->gent.description, out_space);
17956d3e0ec1Smartin 		else if (out_space > 1)
17966d3e0ec1Smartin 			out[0] = 0;
17976d3e0ec1Smartin 	} else if (label == MSG_ptn_boot)
1798dcc57e24Smartin 		strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
1799dcc57e24Smartin 		    MSG_Yes : MSG_No), out_space);
1800dcc57e24Smartin 	else
1801dcc57e24Smartin 		return false;
1802dcc57e24Smartin 
1803dcc57e24Smartin 	return true;
1804dcc57e24Smartin }
1805dcc57e24Smartin 
1806dcc57e24Smartin static bool
1807dcc57e24Smartin gpt_custom_attribute_toggle(struct disk_partitions *arg,
1808dcc57e24Smartin     part_id ptn, size_t attr_no)
1809dcc57e24Smartin {
1810dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1811dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1812dcc57e24Smartin 	size_t i;
1813dcc57e24Smartin 	struct gpt_part_entry *p;
1814dcc57e24Smartin 
1815dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1816dcc57e24Smartin 		if (i == ptn)
1817dcc57e24Smartin 			break;
1818dcc57e24Smartin 
1819dcc57e24Smartin 	if (p == NULL)
1820dcc57e24Smartin 		return false;
1821dcc57e24Smartin 
1822dcc57e24Smartin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1823dcc57e24Smartin 		return false;
1824dcc57e24Smartin 
1825dcc57e24Smartin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1826dcc57e24Smartin 	if (label != MSG_ptn_boot)
1827dcc57e24Smartin 		return false;
1828dcc57e24Smartin 
1829dcc57e24Smartin 	if (p->gp_attr & GPT_ATTR_BOOT) {
1830dcc57e24Smartin 		p->gp_attr &= ~GPT_ATTR_BOOT;
1831dcc57e24Smartin 	} else {
1832dcc57e24Smartin 		for (i = 0, p = parts->partitions; p != NULL;
1833dcc57e24Smartin 		    i++, p = p->gp_next)
1834dcc57e24Smartin 			if (i == ptn)
1835dcc57e24Smartin 				p->gp_attr |= GPT_ATTR_BOOT;
1836dcc57e24Smartin 			else
1837dcc57e24Smartin 				p->gp_attr &= ~GPT_ATTR_BOOT;
1838dcc57e24Smartin 	}
1839dcc57e24Smartin 	return true;
1840dcc57e24Smartin }
1841dcc57e24Smartin 
1842dcc57e24Smartin static bool
1843dcc57e24Smartin gpt_custom_attribute_set_str(struct disk_partitions *arg,
1844dcc57e24Smartin     part_id ptn, size_t attr_no, const char *new_val)
1845dcc57e24Smartin {
1846dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1847dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1848dcc57e24Smartin 	size_t i;
1849dcc57e24Smartin 	struct gpt_part_entry *p;
1850dcc57e24Smartin 
1851dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1852dcc57e24Smartin 		if (i == ptn)
1853dcc57e24Smartin 			break;
1854dcc57e24Smartin 
1855dcc57e24Smartin 	if (p == NULL)
1856dcc57e24Smartin 		return false;
1857dcc57e24Smartin 
1858dcc57e24Smartin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1859dcc57e24Smartin 		return false;
1860dcc57e24Smartin 
1861dcc57e24Smartin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1862dcc57e24Smartin 
1863dcc57e24Smartin 	if (label != MSG_ptn_label)
1864dcc57e24Smartin 		return false;
1865dcc57e24Smartin 
1866dcc57e24Smartin 	strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
1867dcc57e24Smartin 	return true;
1868dcc57e24Smartin }
1869dcc57e24Smartin 
1870dcc57e24Smartin static bool
1871dcc57e24Smartin gpt_have_boot_support(const char *disk)
1872dcc57e24Smartin {
1873dcc57e24Smartin #ifdef	HAVE_GPT_BOOT
1874dcc57e24Smartin 	return true;
1875dcc57e24Smartin #else
1876dcc57e24Smartin 	return false;
1877dcc57e24Smartin #endif
1878dcc57e24Smartin }
1879dcc57e24Smartin 
1880dcc57e24Smartin const struct disk_part_custom_attribute gpt_custom_attrs[] = {
1881dcc57e24Smartin 	{ .label = MSG_ptn_label,	.type = pet_str },
1882dcc57e24Smartin 	{ .label = MSG_ptn_uuid,	.type = pet_str },
1883dcc57e24Smartin 	{ .label = MSG_ptn_gpt_type,	.type = pet_str },
1884dcc57e24Smartin 	{ .label = MSG_ptn_boot,	.type = pet_bool },
1885dcc57e24Smartin };
1886dcc57e24Smartin 
1887dcc57e24Smartin const struct disk_partitioning_scheme
1888dcc57e24Smartin gpt_parts = {
1889dcc57e24Smartin 	.name = MSG_parttype_gpt,
1890dcc57e24Smartin 	.short_name = MSG_parttype_gpt_short,
1891dcc57e24Smartin 	.part_flag_desc = MSG_gpt_flag_desc,
1892dcc57e24Smartin 	.custom_attribute_count = __arraycount(gpt_custom_attrs),
1893dcc57e24Smartin 	.custom_attributes = gpt_custom_attrs,
1894dcc57e24Smartin 	.get_part_types_count = gpt_type_count,
1895dcc57e24Smartin 	.get_part_type = gpt_get_ptype,
1896dcc57e24Smartin 	.get_generic_part_type = gpt_get_generic_type,
1897dcc57e24Smartin 	.get_fs_part_type = gpt_get_fs_part_type,
189834f3e937Smartin 	.get_default_fstype = gpt_get_default_fstype,
18998ac26f1aSmartin 	.create_custom_part_type = gpt_create_custom_part_type,
19008ac26f1aSmartin 	.create_unknown_part_type = gpt_create_unknown_part_type,
1901dcc57e24Smartin 	.get_part_alignment = gpt_get_part_alignment,
1902dcc57e24Smartin 	.read_from_disk = gpt_read_from_disk,
190375761763Smartin 	.get_cylinder_size = gpt_cyl_size,
1904dcc57e24Smartin 	.create_new_for_disk = gpt_create_new,
1905dcc57e24Smartin 	.have_boot_support = gpt_have_boot_support,
19066bb5c164Smartin 	.find_by_name = gpt_find_by_name,
1907dcc57e24Smartin 	.can_add_partition = gpt_can_add_partition,
1908dcc57e24Smartin 	.custom_attribute_writable = gpt_custom_attribute_writable,
1909dcc57e24Smartin 	.format_custom_attribute = gpt_format_custom_attribute,
1910dcc57e24Smartin 	.custom_attribute_toggle = gpt_custom_attribute_toggle,
1911dcc57e24Smartin 	.custom_attribute_set_str = gpt_custom_attribute_set_str,
19125823c3d0Smartin 	.other_partition_identifier = gpt_get_label_str,
1913dcc57e24Smartin 	.get_part_device = gpt_get_part_device,
1914dcc57e24Smartin 	.max_free_space_at = gpt_max_free_space_at,
1915dcc57e24Smartin 	.get_free_spaces = gpt_get_free_spaces,
19168ac26f1aSmartin 	.adapt_foreign_part_info = generic_adapt_foreign_part_info,
1917dcc57e24Smartin 	.get_part_info = gpt_get_part_info,
1918dcc57e24Smartin 	.get_part_attr_str = gpt_get_part_attr_str,
1919dcc57e24Smartin 	.set_part_info = gpt_set_part_info,
1920dcc57e24Smartin 	.add_partition = gpt_add_part,
1921dcc57e24Smartin 	.delete_all_partitions = gpt_delete_all_partitions,
1922dcc57e24Smartin 	.delete_partition = gpt_delete_partition,
1923dcc57e24Smartin 	.write_to_disk = gpt_write_to_disk,
1924dcc57e24Smartin 	.free = gpt_free,
192580bf2417Smartin 	.destroy_part_scheme = gpt_destroy_part_scheme,
19268ac26f1aSmartin 	.cleanup = gpt_cleanup,
1927dcc57e24Smartin };
1928