xref: /netbsd/usr.sbin/sysinst/gpt.c (revision 072292e0)
1*072292e0Smartin /*	$NetBSD: gpt.c,v 1.30 2022/12/15 14:54:27 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 */
52060be59dSmartin #define	GPT_DEV_LEN	DISKNAMESIZE	/* 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 },
106b1e6d40aSmartin 	{ .name = "efi",	.fstype = FS_EFI_SP,	.ptype = PT_EFI_SYSTEM,
107b1e6d40aSmartin 	  .fsflags = GLM_MAYBE_FAT32 },
108dcc57e24Smartin 	{ .name = "bios",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
109dcc57e24Smartin 	  .fsflags = GLM_MAYBE_FAT32 },
110dcc57e24Smartin 	{ .name = "lfs",	.fstype = FS_BSDLFS,	.ptype = PT_root },
111dcc57e24Smartin 	{ .name = "linux-data",	.fstype = FS_EX2FS,	.ptype = PT_root },
112dcc57e24Smartin 	{ .name = "apple",	.fstype = FS_HFS,	.ptype = PT_unknown },
11334f3e937Smartin 	{ .name = "ccd",	.fstype = FS_CCD,	.ptype = PT_root },
11434f3e937Smartin 	{ .name = "cgd",	.fstype = FS_CGD,	.ptype = PT_root },
115dcc57e24Smartin 	{ .name = "raid",	.fstype = FS_RAID,	.ptype = PT_root },
116dcc57e24Smartin 	{ .name = "vmcore",	.fstype = FS_VMKCORE,	.ptype = PT_unknown },
117dcc57e24Smartin 	{ .name = "vmfs",	.fstype = FS_VMFS,	.ptype = PT_unknown },
118dd1127d6Smartin 	{ .name = "vmresered",	.fstype = FS_VMWRESV,	.ptype = PT_unknown },
11950894729Smartin 	{ .name = "zfs",	.fstype = FS_ZFS,	.ptype = PT_root },
120dcc57e24Smartin };
121dcc57e24Smartin 
1228ac26f1aSmartin static size_t gpt_ptype_cnt = 0, gpt_ptype_alloc = 0;
1238ac26f1aSmartin static struct gpt_ptype_desc *gpt_ptype_descs = NULL;
1248ac26f1aSmartin 
1258ac26f1aSmartin /* "well" known types with special handling */
1268ac26f1aSmartin static const struct part_type_desc *gpt_native_root;
127dcc57e24Smartin 
128dcc57e24Smartin /* similar to struct gpt_ent, but matching our needs */
129dcc57e24Smartin struct gpt_part_entry {
130dcc57e24Smartin 	const struct gpt_ptype_desc *gp_type;
131dcc57e24Smartin 	char gp_id[GUID_STR_LEN];	/* partition guid as string */
132dcc57e24Smartin 	daddr_t gp_start, gp_size;
133dcc57e24Smartin 	uint gp_attr;			/* various attribute bits */
134dcc57e24Smartin 	char gp_label[GPT_LABEL_LEN];	/* user defined label */
135dcc57e24Smartin 	char gp_dev_name[GPT_DEV_LEN];	/* name of wedge */
136dcc57e24Smartin 	const char *last_mounted;	/* last mounted if known */
137b162b00bSmartin 	uint fs_type, fs_sub_type,	/* FS_* and maybe sub type */
138b162b00bSmartin 	    fs_opt1, fs_opt2, fs_opt3;	/* transient file system options */
139dcc57e24Smartin 	uint gp_flags;
140dcc57e24Smartin #define	GPEF_ON_DISK	1		/* This entry exists on-disk */
141dcc57e24Smartin #define	GPEF_MODIFIED	2		/* this entry has been changed */
142dcc57e24Smartin #define	GPEF_WEDGE	4		/* wedge for this exists */
143dcc57e24Smartin #define	GPEF_RESIZED	8		/* size has changed */
144bf7de24aSmartin #define	GPEF_TARGET	16		/* marked install target */
145dcc57e24Smartin 	struct gpt_part_entry *gp_next;
146dcc57e24Smartin };
147dcc57e24Smartin 
148dcc57e24Smartin static const struct gpt_ptype_desc *gpt_find_native_type(
149dcc57e24Smartin     const struct part_type_desc *gent);
150dcc57e24Smartin static const struct gpt_ptype_desc *gpt_find_guid_type(const char*);
151dcc57e24Smartin static bool
152dcc57e24Smartin gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
153dcc57e24Smartin     const char **err_msg);
154dcc57e24Smartin 
155dcc57e24Smartin const struct disk_partitioning_scheme gpt_parts;
156dcc57e24Smartin struct gpt_disk_partitions {
157dcc57e24Smartin 	struct disk_partitions dp;
158dcc57e24Smartin 	/*
159dcc57e24Smartin 	 * We keep a list of our current valid partitions, pointed
160dcc57e24Smartin 	 * to by "partitions".
161dcc57e24Smartin 	 * dp.num_part is the number of entries in "partitions".
162dcc57e24Smartin 	 * When partitions that have a representation on disk already
163dcc57e24Smartin 	 * are deleted, we move them to the "obsolete" list so we
164dcc57e24Smartin 	 * can issue the proper commands to remove it when writing back.
165dcc57e24Smartin 	 */
166dcc57e24Smartin 	struct gpt_part_entry *partitions,	/* current partitions */
167dcc57e24Smartin 	    *obsolete;				/* deleted partitions */
168dcc57e24Smartin 	size_t max_num_parts;			/* how many entries max? */
169dcc57e24Smartin 	size_t prologue, epilogue;		/* number of sectors res. */
170dcc57e24Smartin 	bool has_gpt;	/* disk already has a GPT */
171dcc57e24Smartin };
172dcc57e24Smartin 
173dcc57e24Smartin /*
174dcc57e24Smartin  * Init global variables from MD details
175dcc57e24Smartin  */
176dcc57e24Smartin static void
gpt_md_init(bool is_boot_disk,size_t * max_parts,size_t * head,size_t * tail)177dcc57e24Smartin gpt_md_init(bool is_boot_disk, size_t *max_parts, size_t *head, size_t *tail)
178dcc57e24Smartin {
179dcc57e24Smartin 	size_t num;
180dcc57e24Smartin 
181dcc57e24Smartin 	if (is_boot_disk) {
182dcc57e24Smartin #ifdef MD_GPT_INITIAL_SIZE
183dcc57e24Smartin #if MD_GPT_INITIAL_SIZE < 2*512
184dcc57e24Smartin #error	impossible small GPT prologue
185dcc57e24Smartin #endif
186dcc57e24Smartin 		num = ((MD_GPT_INITIAL_SIZE-(2*512))/512)*GPT_PARTS_PER_SEC;
187dcc57e24Smartin #else
188dcc57e24Smartin 		num = GPT_DEFAULT_MAX_PARTS;
189dcc57e24Smartin #endif
190dcc57e24Smartin 	} else {
191dcc57e24Smartin 		num = GPT_DEFAULT_MAX_PARTS;
192dcc57e24Smartin 	}
193dcc57e24Smartin 	*max_parts = num;
194dcc57e24Smartin 	*head = 2 + num/GPT_PARTS_PER_SEC;
195dcc57e24Smartin 	*tail = 1 + num/GPT_PARTS_PER_SEC;
196dcc57e24Smartin }
197dcc57e24Smartin 
198dcc57e24Smartin /*
199dcc57e24Smartin  * Parse a part of "gpt show" output into a struct gpt_part_entry.
200dcc57e24Smartin  * Output is from "show -a" format if details = false, otherwise
201dcc57e24Smartin  * from details for a specific partition (show -i or show -b)
202dcc57e24Smartin  */
203dcc57e24Smartin static void
gpt_add_info(struct gpt_part_entry * part,const char * tag,char * val,bool details)204dcc57e24Smartin gpt_add_info(struct gpt_part_entry *part, const char *tag, char *val,
205dcc57e24Smartin     bool details)
206dcc57e24Smartin {
207dcc57e24Smartin 	char *s, *e;
208dcc57e24Smartin 
209dcc57e24Smartin 	if (details && strcmp(tag, "Start:") == 0) {
210dcc57e24Smartin 		part->gp_start = strtouq(val, NULL, 10);
211dcc57e24Smartin 	} else if (details && strcmp(tag, "Size:") == 0) {
212dcc57e24Smartin 		part->gp_size = strtouq(val, NULL, 10);
213dcc57e24Smartin 	} else if (details && strcmp(tag, "Type:") == 0) {
214dcc57e24Smartin 		s = strchr(val, '(');
215dcc57e24Smartin 		if (!s)
216dcc57e24Smartin 			return;
217dcc57e24Smartin 		e = strchr(s, ')');
218dcc57e24Smartin 		if (!e)
219dcc57e24Smartin 			return;
220dcc57e24Smartin 		*e = 0;
221dcc57e24Smartin 		part->gp_type = gpt_find_guid_type(s+1);
222dcc57e24Smartin 	} else if (strcmp(tag, "TypeID:") == 0) {
223dcc57e24Smartin 		part->gp_type = gpt_find_guid_type(val);
224dcc57e24Smartin 	} else if (strcmp(tag, "GUID:") == 0) {
225dcc57e24Smartin 		strlcpy(part->gp_id, val, sizeof(part->gp_id));
226dcc57e24Smartin 	} else if (strcmp(tag, "Label:") == 0) {
227dcc57e24Smartin 		strlcpy(part->gp_label, val, sizeof(part->gp_label));
228dcc57e24Smartin 	} else if (strcmp(tag, "Attributes:") == 0) {
229dcc57e24Smartin 		char *n;
230dcc57e24Smartin 
231dcc57e24Smartin 		while ((n = strsep(&val, ", ")) != NULL) {
232dcc57e24Smartin 			if (*n == 0)
233dcc57e24Smartin 				continue;
234dcc57e24Smartin 			for (const struct gpt_attr_desc *p = gpt_avail_attrs;
235dcc57e24Smartin 			    p->name != NULL; p++) {
236dcc57e24Smartin 				if (strcmp(p->name, n) == 0)
237dcc57e24Smartin 					part->gp_attr |= p->flag;
238dcc57e24Smartin 			}
239dcc57e24Smartin 		}
240dcc57e24Smartin 	}
241dcc57e24Smartin }
242dcc57e24Smartin 
24367b2b814Smartin /*
24467b2b814Smartin  * Find the partition matching this wedge info and record that we
24567b2b814Smartin  * have a wedge already.
24667b2b814Smartin  */
24767b2b814Smartin static void
update_part_from_wedge_info(struct gpt_disk_partitions * parts,const struct dkwedge_info * dkw)24867b2b814Smartin update_part_from_wedge_info(struct gpt_disk_partitions *parts,
24967b2b814Smartin     const struct dkwedge_info *dkw)
25067b2b814Smartin {
25167b2b814Smartin 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
25267b2b814Smartin 	    p = p->gp_next) {
25367b2b814Smartin 		if (p->gp_start != dkw->dkw_offset ||
25467b2b814Smartin 		    (uint64_t)p->gp_size != dkw->dkw_size)
25567b2b814Smartin 			continue;
25667b2b814Smartin 		p->gp_flags |= GPEF_WEDGE;
25767b2b814Smartin 		strlcpy(p->gp_dev_name, dkw->dkw_devname,
25867b2b814Smartin 		    sizeof p->gp_dev_name);
25967b2b814Smartin 		return;
26067b2b814Smartin 	}
26167b2b814Smartin }
26267b2b814Smartin 
263dcc57e24Smartin static struct disk_partitions *
gpt_read_from_disk(const char * dev,daddr_t start,daddr_t len,size_t bps,const struct disk_partitioning_scheme * scheme)2640ea03d33Smartin gpt_read_from_disk(const char *dev, daddr_t start, daddr_t len, size_t bps,
265ae033681Smartin     const struct disk_partitioning_scheme *scheme)
266dcc57e24Smartin {
267dcc57e24Smartin 	char diskpath[MAXPATHLEN];
268dcc57e24Smartin 	int fd;
26967b2b814Smartin 	struct dkwedge_info *dkw;
27067b2b814Smartin 	struct dkwedge_list dkwl;
27167b2b814Smartin 	size_t bufsize, dk;
272dcc57e24Smartin 
273dcc57e24Smartin 	assert(start == 0);
274dcc57e24Smartin 	assert(have_gpt);
275dcc57e24Smartin 
276dcc57e24Smartin 	if (run_program(RUN_SILENT | RUN_ERROR_OK,
277dcc57e24Smartin 	    "gpt -rq header %s", dev) != 0)
278dcc57e24Smartin 		return NULL;
279dcc57e24Smartin 
280dcc57e24Smartin 	/* read the partitions */
281dcc57e24Smartin 	int i;
282dcc57e24Smartin 	unsigned int p_index;
283dcc57e24Smartin 	daddr_t p_start = 0, p_size = 0, avail_start = 0, avail_size = 0,
284dcc57e24Smartin 	    disk_size = 0;
285dcc57e24Smartin 	char *textbuf, *t, *tt, p_type[STRSIZE];
286dcc57e24Smartin 	static const char regpart_prefix[] = "GPT part - ";
287dcc57e24Smartin 	struct gpt_disk_partitions *parts;
288dcc57e24Smartin 	struct gpt_part_entry *last = NULL, *add_to = NULL;
289bf7de24aSmartin 	const struct gpt_ptype_desc *native_root
290bf7de24aSmartin 	     = gpt_find_native_type(gpt_native_root);
291bf7de24aSmartin 	bool have_target = false;
292dcc57e24Smartin 
293dcc57e24Smartin 	if (collect(T_OUTPUT, &textbuf, "gpt -r show -a %s 2>/dev/null", dev)
294dcc57e24Smartin 	    < 1)
295dcc57e24Smartin 		return NULL;
296dcc57e24Smartin 
297dcc57e24Smartin 	/* parse output and create our list */
298dcc57e24Smartin 	parts = calloc(1, sizeof(*parts));
299dcc57e24Smartin 	if (parts == NULL)
300dcc57e24Smartin 		return NULL;
301dcc57e24Smartin 
302dcc57e24Smartin 	(void)strtok(textbuf, "\n"); /* ignore first line */
303dcc57e24Smartin 	while ((t = strtok(NULL, "\n")) != NULL) {
304dcc57e24Smartin 		i = 0; p_start = 0; p_size = 0; p_index = 0;
305dcc57e24Smartin 		p_type[0] = 0;
306dcc57e24Smartin 		while ((tt = strsep(&t, " \t")) != NULL) {
307dcc57e24Smartin 			if (strlen(tt) == 0)
308dcc57e24Smartin 				continue;
309dcc57e24Smartin 			if (i == 0) {
310dcc57e24Smartin 				if (add_to != NULL)
311dcc57e24Smartin 					gpt_add_info(add_to, tt, t, false);
312dcc57e24Smartin 				p_start = strtouq(tt, NULL, 10);
313dcc57e24Smartin 				if (p_start == 0 && add_to != NULL)
314dcc57e24Smartin 					break;
315dcc57e24Smartin 				else
316dcc57e24Smartin 					add_to = NULL;
317dcc57e24Smartin 			}
318dcc57e24Smartin 			if (i == 1)
319dcc57e24Smartin 				p_size = strtouq(tt, NULL, 10);
320dcc57e24Smartin 			if (i == 2)
321dcc57e24Smartin 				p_index = strtouq(tt, NULL, 10);
322dcc57e24Smartin 			if (i > 2 || (i == 2 && p_index == 0)) {
323dcc57e24Smartin 				if (p_type[0])
324dcc57e24Smartin 					strlcat(p_type, " ", STRSIZE);
325dcc57e24Smartin 				strlcat(p_type, tt, STRSIZE);
326dcc57e24Smartin 			}
327dcc57e24Smartin 			i++;
328dcc57e24Smartin 		}
329dcc57e24Smartin 
330dcc57e24Smartin 		if (p_start == 0 || p_size == 0)
331dcc57e24Smartin 			continue;
332dcc57e24Smartin 		else if (strcmp(p_type, "Pri GPT table") == 0) {
333dcc57e24Smartin 			avail_start = p_start + p_size;
334dcc57e24Smartin 			parts->prologue = avail_start;
335dcc57e24Smartin 			parts->epilogue = p_size + 1;
336dcc57e24Smartin 			parts->max_num_parts = p_size * GPT_PARTS_PER_SEC;
337dcc57e24Smartin 		} else if (strcmp(p_type, "Sec GPT table") == 0)
338dcc57e24Smartin 			avail_size = p_start - avail_start;
339dcc57e24Smartin 		else if(strcmp(p_type, "Sec GPT header") == 0)
340dcc57e24Smartin 			disk_size = p_start + p_size;
341dcc57e24Smartin 		else if (p_index == 0 && strlen(p_type) > 0)
342dcc57e24Smartin 			/* Utilitary entry (PMBR, etc) */
343dcc57e24Smartin 			continue;
344dcc57e24Smartin 		else if (p_index == 0) {
345dcc57e24Smartin 			/* Free space */
346dcc57e24Smartin 			continue;
347dcc57e24Smartin 		} else {
348dcc57e24Smartin 			/* Usual partition */
349dcc57e24Smartin 			tt = p_type;
350dcc57e24Smartin 			if (strncmp(tt, regpart_prefix,
351dcc57e24Smartin 			    strlen(regpart_prefix)) == 0)
352dcc57e24Smartin 				tt += strlen(regpart_prefix);
353dcc57e24Smartin 
354dcc57e24Smartin 			/* Add to our linked list */
355dcc57e24Smartin 			struct gpt_part_entry *np = calloc(1, sizeof(*np));
356dcc57e24Smartin 			if (np == NULL)
357dcc57e24Smartin 				break;
358dcc57e24Smartin 
359dcc57e24Smartin 			strlcpy(np->gp_label, tt, sizeof(np->gp_label));
360dcc57e24Smartin 			np->gp_start = p_start;
361dcc57e24Smartin 			np->gp_size = p_size;
362dcc57e24Smartin 			np->gp_flags |= GPEF_ON_DISK;
363bf7de24aSmartin 			if (!have_target && native_root != NULL &&
364bf7de24aSmartin 			    strcmp(np->gp_id, native_root->tid) == 0) {
365bf7de24aSmartin 				have_target = true;
366bf7de24aSmartin 				np->gp_flags |= GPEF_TARGET;
367bf7de24aSmartin 			}
368dcc57e24Smartin 
369dcc57e24Smartin 			if (last == NULL)
370dcc57e24Smartin 				parts->partitions = np;
371dcc57e24Smartin 			else
372dcc57e24Smartin 				last->gp_next = np;
373dcc57e24Smartin 			last = np;
374dcc57e24Smartin 			add_to = np;
375dcc57e24Smartin 			parts->dp.num_part++;
376dcc57e24Smartin 		}
377dcc57e24Smartin 	}
378dcc57e24Smartin 	free(textbuf);
379dcc57e24Smartin 
3806b0797fdSmartin 	/* If the GPT was not complete (e.g. truncated image), barf */
3816b0797fdSmartin 	if (disk_size <= 0) {
3826b0797fdSmartin 		free(parts);
3836b0797fdSmartin 		return NULL;
3846b0797fdSmartin 	}
3856b0797fdSmartin 
386ae033681Smartin 	parts->dp.pscheme = scheme;
3878ac26f1aSmartin 	parts->dp.disk = strdup(dev);
388dcc57e24Smartin 	parts->dp.disk_start = start;
389dcc57e24Smartin 	parts->dp.disk_size = disk_size;
390dcc57e24Smartin 	parts->dp.free_space = avail_size;
3910ea03d33Smartin 	parts->dp.bytes_per_sector = bps;
392dcc57e24Smartin 	parts->has_gpt = true;
393dcc57e24Smartin 
394dcc57e24Smartin 	fd = opendisk(parts->dp.disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
395dcc57e24Smartin 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
396dcc57e24Smartin 	    p = p->gp_next) {
397dcc57e24Smartin #ifdef DEFAULT_UFS2
398dcc57e24Smartin 		bool fs_is_default = false;
399dcc57e24Smartin #endif
400dcc57e24Smartin 
4016d3e0ec1Smartin 		if (p->gp_type != NULL) {
4026d3e0ec1Smartin 
403dcc57e24Smartin 			if (p->gp_type->fsflags != 0) {
4046d3e0ec1Smartin 				const char *lm = get_last_mounted(fd,
4056d3e0ec1Smartin 				    p->gp_start, &p->fs_type,
4066d3e0ec1Smartin 				    &p->fs_sub_type, p->gp_type->fsflags);
407dcc57e24Smartin 				if (lm != NULL && *lm != 0) {
408f4f78ddcSmartin 					char *path = strdup(lm);
409f4f78ddcSmartin 					canonicalize_last_mounted(path);
410f4f78ddcSmartin 					p->last_mounted = path;
411dcc57e24Smartin 				} else {
4126d3e0ec1Smartin 					p->fs_type = p->gp_type->
4136d3e0ec1Smartin 					    default_fs_type;
414dcc57e24Smartin #ifdef DEFAULT_UFS2
415dcc57e24Smartin 					fs_is_default = true;
416dcc57e24Smartin #endif
417dcc57e24Smartin 				}
418dcc57e24Smartin 			} else {
419dcc57e24Smartin 				p->fs_type = p->gp_type->default_fs_type;
420dcc57e24Smartin #ifdef DEFAULT_UFS2
421dcc57e24Smartin 				fs_is_default = true;
422dcc57e24Smartin #endif
423dcc57e24Smartin 			}
424dcc57e24Smartin #ifdef DEFAULT_UFS2
425dcc57e24Smartin 			if (fs_is_default && p->fs_type == FS_BSDFFS)
426dcc57e24Smartin 				p->fs_sub_type = 2;
427dcc57e24Smartin #endif
4286d3e0ec1Smartin 		}
429dcc57e24Smartin 
430dcc57e24Smartin 		parts->dp.free_space -= p->gp_size;
431dcc57e24Smartin 	}
43267b2b814Smartin 
43367b2b814Smartin 	/*
43467b2b814Smartin 	 * Check if we have any (matching/auto-configured) wedges already
43567b2b814Smartin 	 */
43667b2b814Smartin 	dkw = NULL;
43767b2b814Smartin 	dkwl.dkwl_buf = dkw;
43867b2b814Smartin 	dkwl.dkwl_bufsize = 0;
43967b2b814Smartin 	if (ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
44067b2b814Smartin 		/* do not even try to deal with any races at this point */
44167b2b814Smartin 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
44267b2b814Smartin 		dkw = malloc(bufsize);
44367b2b814Smartin 		dkwl.dkwl_buf = dkw;
44467b2b814Smartin 		dkwl.dkwl_bufsize = bufsize;
44567b2b814Smartin 		if (dkw != NULL && ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
44667b2b814Smartin 			for (dk = 0; dk < dkwl.dkwl_ncopied; dk++)
44767b2b814Smartin 				update_part_from_wedge_info(parts, &dkw[dk]);
44867b2b814Smartin 		}
44967b2b814Smartin 		free(dkw);
45067b2b814Smartin 	}
45167b2b814Smartin 
452dcc57e24Smartin 	close(fd);
453dcc57e24Smartin 
454dcc57e24Smartin 	return &parts->dp;
455dcc57e24Smartin }
456dcc57e24Smartin 
45775761763Smartin static size_t
gpt_cyl_size(const struct disk_partitions * arg)45875761763Smartin gpt_cyl_size(const struct disk_partitions *arg)
45975761763Smartin {
46075761763Smartin 	return MEG / 512;
46175761763Smartin }
46275761763Smartin 
463dcc57e24Smartin static struct disk_partitions *
gpt_create_new(const char * disk,daddr_t start,daddr_t len,bool is_boot_drive,struct disk_partitions * parent)4640ea03d33Smartin gpt_create_new(const char *disk, daddr_t start, daddr_t len,
46534f3e937Smartin     bool is_boot_drive, struct disk_partitions *parent)
466dcc57e24Smartin {
467dcc57e24Smartin 	struct gpt_disk_partitions *parts;
4680ea03d33Smartin 	struct disk_geom geo;
469dcc57e24Smartin 
470dcc57e24Smartin 	if (start != 0) {
471dcc57e24Smartin 		assert(0);
472dcc57e24Smartin 		return NULL;
473dcc57e24Smartin 	}
474dcc57e24Smartin 
4750ea03d33Smartin 	if (!get_disk_geom(disk, &geo))
4760ea03d33Smartin 		return NULL;
4770ea03d33Smartin 
47800f87f9fSmartin 	parts = calloc(1, sizeof(*parts));
479dcc57e24Smartin 	if (!parts)
480dcc57e24Smartin 		return NULL;
481dcc57e24Smartin 
482dcc57e24Smartin 	parts->dp.pscheme = &gpt_parts;
4838ac26f1aSmartin 	parts->dp.disk = strdup(disk);
484dcc57e24Smartin 
485dcc57e24Smartin 	gpt_md_init(is_boot_drive, &parts->max_num_parts, &parts->prologue,
486dcc57e24Smartin 	    &parts->epilogue);
487dcc57e24Smartin 
488dcc57e24Smartin 	parts->dp.disk_start = start;
489dcc57e24Smartin 	parts->dp.disk_size = len;
4900ea03d33Smartin 	parts->dp.bytes_per_sector = geo.dg_secsize;
491dcc57e24Smartin 	parts->dp.free_space = len - start - parts->prologue - parts->epilogue;
492dcc57e24Smartin 	parts->has_gpt = false;
493dcc57e24Smartin 
494dcc57e24Smartin 	return &parts->dp;
495dcc57e24Smartin }
496dcc57e24Smartin 
497dcc57e24Smartin static bool
gpt_get_part_info(const struct disk_partitions * arg,part_id id,struct disk_part_info * info)498dcc57e24Smartin gpt_get_part_info(const struct disk_partitions *arg, part_id id,
499dcc57e24Smartin     struct disk_part_info *info)
500dcc57e24Smartin {
5016d3e0ec1Smartin 	static const struct part_type_desc gpt_unknown_type =
5026d3e0ec1Smartin 		{ .generic_ptype = PT_undef,
5036d3e0ec1Smartin 		  .short_desc = "<unknown>" };
504dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
505dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
506dcc57e24Smartin 	const struct gpt_part_entry *p = parts->partitions;
507dcc57e24Smartin 	part_id no;
508dcc57e24Smartin 
509dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
510dcc57e24Smartin 		p = p->gp_next;
511dcc57e24Smartin 
512dcc57e24Smartin 	if (no != id || p == NULL)
513dcc57e24Smartin 		return false;
514dcc57e24Smartin 
515dcc57e24Smartin 	memset(info, 0, sizeof(*info));
516dcc57e24Smartin 	info->start = p->gp_start;
517dcc57e24Smartin 	info->size = p->gp_size;
518dcc57e24Smartin 	if (p->gp_type)
519dcc57e24Smartin 		info->nat_type = &p->gp_type->gent;
5206d3e0ec1Smartin 	else
5216d3e0ec1Smartin 		info->nat_type = &gpt_unknown_type;
522dcc57e24Smartin 	info->last_mounted = p->last_mounted;
523dcc57e24Smartin 	info->fs_type = p->fs_type;
524dcc57e24Smartin 	info->fs_sub_type = p->fs_sub_type;
525b162b00bSmartin 	info->fs_opt1 = p->fs_opt1;
526b162b00bSmartin 	info->fs_opt2 = p->fs_opt2;
527b162b00bSmartin 	info->fs_opt3 = p->fs_opt3;
528bf7de24aSmartin 	if (p->gp_flags & GPEF_TARGET)
529bf7de24aSmartin 		info->flags |= PTI_INSTALL_TARGET;
530dcc57e24Smartin 
531dcc57e24Smartin 	return true;
532dcc57e24Smartin }
533dcc57e24Smartin 
534dcc57e24Smartin static bool
gpt_get_part_attr_str(const struct disk_partitions * arg,part_id id,char * str,size_t avail_space)535dcc57e24Smartin gpt_get_part_attr_str(const struct disk_partitions *arg, part_id id,
536dcc57e24Smartin     char *str, size_t avail_space)
537dcc57e24Smartin {
538dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
539dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
540dcc57e24Smartin 	const struct gpt_part_entry *p = parts->partitions;
541dcc57e24Smartin 	part_id no;
542dcc57e24Smartin 	static const char *flags = NULL;
543dcc57e24Smartin 
544dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
545dcc57e24Smartin 		p = p->gp_next;
546dcc57e24Smartin 
547dcc57e24Smartin 	if (no != id || p == NULL)
548dcc57e24Smartin 		return false;
549dcc57e24Smartin 
550dcc57e24Smartin 	if (flags == NULL)
551dcc57e24Smartin 		flags = msg_string(MSG_gpt_flags);
552dcc57e24Smartin 
553dcc57e24Smartin 	if (avail_space < 2)
554dcc57e24Smartin 		return false;
555dcc57e24Smartin 
556dcc57e24Smartin 	if (p->gp_attr & GPT_ATTR_BOOT)
557dcc57e24Smartin 		*str++ = flags[0];
558dcc57e24Smartin 	*str = 0;
559dcc57e24Smartin 
560dcc57e24Smartin 	return true;
561dcc57e24Smartin }
562dcc57e24Smartin 
563dcc57e24Smartin /*
564dcc57e24Smartin  * Find insert position and check for duplicates.
565dcc57e24Smartin  * If all goes well, insert the new "entry" in the "list".
566dcc57e24Smartin  * If there are collisions, report "no free space".
567dcc57e24Smartin  * We keep all lists sorted by start sector number,
568dcc57e24Smartin  */
569dcc57e24Smartin static bool
gpt_insert_part_into_list(struct gpt_disk_partitions * parts,struct gpt_part_entry ** list,struct gpt_part_entry * entry,const char ** err_msg)570dcc57e24Smartin gpt_insert_part_into_list(struct gpt_disk_partitions *parts,
571dcc57e24Smartin     struct gpt_part_entry **list,
572dcc57e24Smartin     struct gpt_part_entry *entry, const char **err_msg)
573dcc57e24Smartin {
574dcc57e24Smartin 	struct gpt_part_entry *p, *last;
575dcc57e24Smartin 
576dcc57e24Smartin 	/* find the first entry past the new one (if any) */
577dcc57e24Smartin 	for (last = NULL, p = *list; p != NULL; last = p, p = p->gp_next) {
578dcc57e24Smartin 		if (p->gp_start > entry->gp_start)
579dcc57e24Smartin 			break;
580dcc57e24Smartin 	}
581dcc57e24Smartin 
582dcc57e24Smartin 	/* check if last partition overlaps with new one */
583dcc57e24Smartin 	if (last) {
584dcc57e24Smartin 		if (last->gp_start + last->gp_size > entry->gp_start) {
585dcc57e24Smartin 			if (err_msg)
586dcc57e24Smartin 				*err_msg = msg_string(MSG_No_free_space);
587dcc57e24Smartin 			return false;
588dcc57e24Smartin 		}
589dcc57e24Smartin 	}
590dcc57e24Smartin 
591dcc57e24Smartin 	if (p == NULL) {
592dcc57e24Smartin 		entry->gp_next = NULL;
593dcc57e24Smartin 		if (last != NULL) {
594dcc57e24Smartin 			last->gp_next = entry;
595dcc57e24Smartin 		}
596dcc57e24Smartin 	} else {
597dcc57e24Smartin 		/* check if new entry overlaps with next */
598dcc57e24Smartin 		if (entry->gp_start + entry->gp_size > p->gp_start) {
599dcc57e24Smartin 			if (err_msg)
600dcc57e24Smartin 				*err_msg = msg_string(MSG_No_free_space);
601dcc57e24Smartin 			return false;
602dcc57e24Smartin 		}
603dcc57e24Smartin 
604dcc57e24Smartin 		entry->gp_next = p;
605dcc57e24Smartin 		if (last != NULL)
606dcc57e24Smartin 			last->gp_next = entry;
607dcc57e24Smartin 		else
608dcc57e24Smartin 			*list = entry;
609dcc57e24Smartin 	}
610dcc57e24Smartin 	if (*list == NULL)
611dcc57e24Smartin 		*list = entry;
612dcc57e24Smartin 
613dcc57e24Smartin 	return true;
614dcc57e24Smartin }
615dcc57e24Smartin 
616dcc57e24Smartin static bool
gpt_set_part_info(struct disk_partitions * arg,part_id id,const struct disk_part_info * info,const char ** err_msg)617dcc57e24Smartin gpt_set_part_info(struct disk_partitions *arg, part_id id,
618dcc57e24Smartin     const struct disk_part_info *info, const char **err_msg)
619dcc57e24Smartin {
620dcc57e24Smartin 	struct gpt_disk_partitions *parts =
621dcc57e24Smartin 	    (struct gpt_disk_partitions*)arg;
622dcc57e24Smartin 	struct gpt_part_entry *p = parts->partitions, *n;
623dcc57e24Smartin 	part_id no;
624dcc57e24Smartin 	daddr_t lendiff;
625dcc57e24Smartin 
626dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
627dcc57e24Smartin 		p = p->gp_next;
628dcc57e24Smartin 
629dcc57e24Smartin 	if (no != id || p == NULL)
630dcc57e24Smartin 		return false;
631dcc57e24Smartin 
632bf7de24aSmartin 	/* update target mark - we can only have one */
633*072292e0Smartin 	if (info->flags & PTI_INSTALL_TARGET) {
634bf7de24aSmartin 		p->gp_flags |= GPEF_TARGET;
635bf7de24aSmartin 		for (n = parts->partitions; n != NULL; n = n->gp_next)
636bf7de24aSmartin 			if (n != p)
637bf7de24aSmartin 				n->gp_flags &= ~GPEF_TARGET;
638*072292e0Smartin 	} else {
639*072292e0Smartin 		p->gp_flags &= ~GPEF_TARGET;
640*072292e0Smartin 	}
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
gpt_get_free_spaces_internal(const struct gpt_disk_partitions * parts,struct disk_part_free_space * result,size_t max_num_result,daddr_t min_space_size,daddr_t align,daddr_t start,daddr_t ignore)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
gpt_max_free_space_at(const struct disk_partitions * arg,daddr_t start)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
gpt_get_free_spaces(const struct disk_partitions * arg,struct disk_part_free_space * result,size_t max_num_result,daddr_t min_space_size,daddr_t align,daddr_t start,daddr_t ignore)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
gpt_match_ptype(const char * name,struct gpt_ptype_desc * t)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
gpt_internal_add_ptype(const char * uid,const char * name,const char * desc)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
gpt_init_ptypes(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
gpt_cleanup(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
gpt_type_count(void)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 *
gpt_get_ptype(size_t ndx)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 *
gpt_get_generic_type(enum part_type gent)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 *
gpt_find_native_type(const struct part_type_desc * gent)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 *
gpt_find_guid_type(const char * uid)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 *
gpt_find_type(const char * 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 *
gpt_get_fs_part_type(enum part_type pt,unsigned fstype,unsigned fs_sub_type)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
gpt_get_default_fstype(const struct part_type_desc * nat_type,unsigned * fstype,unsigned * fs_sub_type)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 *
gpt_get_uuid_part_type(const uuid_t * id)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 *
gpt_create_custom_part_type(const char * custom,const char ** err_msg)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 *
gpt_create_unknown_part_type(void)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
gpt_get_part_alignment(const struct disk_partitions * parts)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
gpt_can_add_partition(const struct disk_partitions * arg)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
gpt_info_to_part(struct gpt_part_entry * p,const struct disk_part_info * info,const char ** err_msg)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
gpt_add_part(struct disk_partitions * arg,const struct disk_part_info * info,const char ** err_msg)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;
1078*072292e0Smartin 	struct gpt_part_entry *p, *n;
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) {
1112*072292e0Smartin 		if (info->flags & PTI_INSTALL_TARGET) {
1113*072292e0Smartin 			/* update target mark - we can only have one */
1114*072292e0Smartin 			p->gp_flags |= GPEF_TARGET;
1115*072292e0Smartin 			for (n = parts->partitions; n != NULL; n = n->gp_next)
1116*072292e0Smartin 				if (n != p)
1117*072292e0Smartin 					n->gp_flags &= ~GPEF_TARGET;
1118*072292e0Smartin 		}
1119*072292e0Smartin 
1120dcc57e24Smartin 		parts->dp.num_part++;
1121dcc57e24Smartin 		parts->dp.free_space -= p->gp_size;
1122dcc57e24Smartin 		return parts->dp.num_part-1;
1123dcc57e24Smartin 	} else {
1124dcc57e24Smartin 		free(p);
1125dcc57e24Smartin 		return NO_PART;
1126dcc57e24Smartin 	}
1127dcc57e24Smartin }
1128dcc57e24Smartin 
1129dcc57e24Smartin static bool
gpt_delete_partition(struct disk_partitions * arg,part_id id,const char ** err_msg)1130dcc57e24Smartin gpt_delete_partition(struct disk_partitions *arg, part_id id,
1131dcc57e24Smartin     const char **err_msg)
1132dcc57e24Smartin {
1133dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1134dcc57e24Smartin 	struct gpt_part_entry *p, *last = NULL;
1135dcc57e24Smartin 	part_id i;
1136dcc57e24Smartin 	bool res;
1137dcc57e24Smartin 
1138dcc57e24Smartin 	if (parts->dp.num_part == 0)
1139dcc57e24Smartin 		return false;
1140dcc57e24Smartin 
1141dcc57e24Smartin 	for (i = 0, p = parts->partitions;
1142dcc57e24Smartin 	    i != id && i < parts->dp.num_part && p != NULL;
1143dcc57e24Smartin 	    i++, p = p->gp_next)
1144dcc57e24Smartin 		last = p;
1145dcc57e24Smartin 
1146dcc57e24Smartin 	if (p == NULL) {
1147dcc57e24Smartin 		if (err_msg)
1148dcc57e24Smartin 			*err_msg = INTERNAL_ERROR;
1149dcc57e24Smartin 		return false;
1150dcc57e24Smartin 	}
1151dcc57e24Smartin 
1152dcc57e24Smartin 	if (last == NULL)
1153dcc57e24Smartin 		parts->partitions = p->gp_next;
1154dcc57e24Smartin 	else
1155dcc57e24Smartin 		last->gp_next = p->gp_next;
1156dcc57e24Smartin 
1157dcc57e24Smartin 	res = true;
1158dcc57e24Smartin 	if (p->gp_flags & GPEF_ON_DISK) {
1159dcc57e24Smartin 		if (!gpt_insert_part_into_list(parts, &parts->obsolete,
1160dcc57e24Smartin 		    p, err_msg))
1161dcc57e24Smartin 			res = false;
1162dcc57e24Smartin 	} else {
1163dcc57e24Smartin 		free(p);
1164dcc57e24Smartin 	}
1165dcc57e24Smartin 
1166dcc57e24Smartin 	if (res) {
1167dcc57e24Smartin 		parts->dp.num_part--;
1168dcc57e24Smartin 		parts->dp.free_space += p->gp_size;
1169dcc57e24Smartin 	}
1170dcc57e24Smartin 
1171dcc57e24Smartin 	return res;
1172dcc57e24Smartin }
1173dcc57e24Smartin 
1174dcc57e24Smartin static bool
gpt_delete_all_partitions(struct disk_partitions * arg)1175dcc57e24Smartin gpt_delete_all_partitions(struct disk_partitions *arg)
1176dcc57e24Smartin {
1177dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1178dcc57e24Smartin 
1179dcc57e24Smartin 	while (parts->dp.num_part > 0) {
1180dcc57e24Smartin 		if (!gpt_delete_partition(&parts->dp, 0, NULL))
1181dcc57e24Smartin 			return false;
1182dcc57e24Smartin 	}
1183dcc57e24Smartin 
1184dcc57e24Smartin 	return true;
1185dcc57e24Smartin }
1186dcc57e24Smartin 
1187dcc57e24Smartin static bool
gpt_read_part(const char * disk,daddr_t start,struct gpt_part_entry * p)1188dcc57e24Smartin gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
1189dcc57e24Smartin {
1190dcc57e24Smartin 	char *textbuf, *t, *tt;
1191dcc57e24Smartin 	static const char expected_hdr[] = "Details for index ";
1192dcc57e24Smartin 
1193dcc57e24Smartin 	/* run gpt show for this partition */
1194dcc57e24Smartin 	if (collect(T_OUTPUT, &textbuf,
1195dcc57e24Smartin 	    "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
1196dcc57e24Smartin 		return false;
1197dcc57e24Smartin 
1198dcc57e24Smartin 	/*
1199dcc57e24Smartin 	 * gpt show should respond with single partition details, but will
1200dcc57e24Smartin 	 * fall back to "show -a" output if something is wrong
1201dcc57e24Smartin 	 */
1202dcc57e24Smartin 	t = strtok(textbuf, "\n"); /* first line is special */
1203dcc57e24Smartin 	if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
1204dcc57e24Smartin 		free(textbuf);
1205dcc57e24Smartin 		return false;
1206dcc57e24Smartin 	}
1207dcc57e24Smartin 
1208dcc57e24Smartin 	/* parse output into "old" */
1209dcc57e24Smartin 	while ((t = strtok(NULL, "\n")) != NULL) {
1210dcc57e24Smartin 		tt = strsep(&t, " \t");
1211dcc57e24Smartin 		if (strlen(tt) == 0)
1212dcc57e24Smartin 			continue;
1213dcc57e24Smartin 		gpt_add_info(p, tt, t, true);
1214dcc57e24Smartin 	}
1215dcc57e24Smartin 	free(textbuf);
1216dcc57e24Smartin 
1217dcc57e24Smartin 	return true;
1218dcc57e24Smartin }
1219dcc57e24Smartin 
1220dcc57e24Smartin static bool
gpt_apply_attr(const char * disk,const char * cmd,off_t start,uint todo)1221dcc57e24Smartin gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
1222dcc57e24Smartin {
1223dcc57e24Smartin 	size_t i;
1224dcc57e24Smartin 	char attr_str[STRSIZE];
1225dcc57e24Smartin 
1226dcc57e24Smartin 	if (todo == 0)
1227dcc57e24Smartin 		return true;
1228dcc57e24Smartin 
1229dcc57e24Smartin 	strcpy(attr_str, "-a ");
1230dcc57e24Smartin 	for (i = 0; todo != 0; i++) {
1231dcc57e24Smartin 		if (!(gpt_avail_attrs[i].flag & todo))
1232dcc57e24Smartin 			continue;
1233dcc57e24Smartin 		todo &= ~gpt_avail_attrs[i].flag;
1234dcc57e24Smartin 		if (attr_str[0])
1235dcc57e24Smartin 			strlcat(attr_str, ",",
1236dcc57e24Smartin 			    sizeof(attr_str));
1237dcc57e24Smartin 		strlcat(attr_str,
1238dcc57e24Smartin 		    gpt_avail_attrs[i].name,
1239dcc57e24Smartin 		    sizeof(attr_str));
1240dcc57e24Smartin 	}
1241dcc57e24Smartin 	if (run_program(RUN_SILENT,
1242dcc57e24Smartin 	    "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
1243dcc57e24Smartin 		return false;
1244dcc57e24Smartin 	return true;
1245dcc57e24Smartin }
1246dcc57e24Smartin 
1247dcc57e24Smartin /*
1248dcc57e24Smartin  * Modify an existing on-disk partition.
1249dcc57e24Smartin  * Start and size can not be changed here, caller needs to deal
1250dcc57e24Smartin  * with that kind of changes upfront.
1251dcc57e24Smartin  */
1252dcc57e24Smartin static bool
gpt_modify_part(const char * disk,struct gpt_part_entry * p)1253dcc57e24Smartin gpt_modify_part(const char *disk, struct gpt_part_entry *p)
1254dcc57e24Smartin {
1255dcc57e24Smartin 	struct gpt_part_entry old;
1256dcc57e24Smartin 	uint todo_set, todo_unset;
1257dcc57e24Smartin 
1258dcc57e24Smartin 	/*
1259dcc57e24Smartin 	 * Query current on-disk state
1260dcc57e24Smartin 	 */
1261dcc57e24Smartin 	memset(&old, 0, sizeof old);
1262dcc57e24Smartin 	if (!gpt_read_part(disk, p->gp_start, &old))
1263dcc57e24Smartin 		return false;
1264dcc57e24Smartin 
1265dcc57e24Smartin 	/* Reject unsupported changes */
1266dcc57e24Smartin 	if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
1267dcc57e24Smartin 		return false;
1268dcc57e24Smartin 
1269dcc57e24Smartin 	/*
1270dcc57e24Smartin 	 * GUID should never change, but the internal copy
1271dcc57e24Smartin 	 * may not yet know it.
1272dcc57e24Smartin 	 */
1273dcc57e24Smartin 	strcpy(p->gp_id, old.gp_id);
1274dcc57e24Smartin 
1275dcc57e24Smartin 	/* Check type */
1276dcc57e24Smartin 	if (p->gp_type != old.gp_type) {
1277dcc57e24Smartin 		if (run_program(RUN_SILENT,
12785d65c236Smartin 		    "gpt type -b %" PRIu64 " -T %s %s",
1279dcc57e24Smartin 		    p->gp_start, p->gp_type->tid, disk) != 0)
1280dcc57e24Smartin 			return false;
1281dcc57e24Smartin 	}
1282dcc57e24Smartin 
1283dcc57e24Smartin 	/* Check label */
1284dcc57e24Smartin 	if (strcmp(p->gp_label, old.gp_label) != 0) {
1285dcc57e24Smartin 		if (run_program(RUN_SILENT,
1286c0e3ce44Smartin 		    "gpt label -b %" PRIu64 " -l \'%s\' %s",
1287dcc57e24Smartin 		    p->gp_start, p->gp_label, disk) != 0)
1288dcc57e24Smartin 			return false;
1289dcc57e24Smartin 	}
1290dcc57e24Smartin 
1291dcc57e24Smartin 	/* Check attributes */
1292dcc57e24Smartin 	if (p->gp_attr != old.gp_attr) {
1293dcc57e24Smartin 		if (p->gp_attr == 0) {
1294dcc57e24Smartin 			if (run_program(RUN_SILENT,
1295dcc57e24Smartin 			    "gpt set -N -b %" PRIu64 " %s",
1296dcc57e24Smartin 			    p->gp_start, disk) != 0)
1297dcc57e24Smartin 				return false;
1298dcc57e24Smartin 		} else {
1299dcc57e24Smartin 			todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
1300dcc57e24Smartin 			todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
1301dcc57e24Smartin 			if (!gpt_apply_attr(disk, "unset", p->gp_start,
1302dcc57e24Smartin 			    todo_unset))
1303dcc57e24Smartin 				return false;
1304dcc57e24Smartin 			if (!gpt_apply_attr(disk, "set", p->gp_start,
1305dcc57e24Smartin 			    todo_set))
1306dcc57e24Smartin 				return false;
1307dcc57e24Smartin 		}
1308dcc57e24Smartin 	}
1309dcc57e24Smartin 
1310dcc57e24Smartin 	return true;
1311dcc57e24Smartin }
1312dcc57e24Smartin 
1313dcc57e24Smartin /*
1314dcc57e24Smartin  * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
1315dcc57e24Smartin  *  map FS_* to wedge strings
1316dcc57e24Smartin  */
1317dcc57e24Smartin static const char *
bsdlabel_fstype_to_str(uint8_t fstype)1318dcc57e24Smartin bsdlabel_fstype_to_str(uint8_t fstype)
1319dcc57e24Smartin {
1320dcc57e24Smartin 	const char *str;
1321dcc57e24Smartin 
1322dcc57e24Smartin 	/*
1323dcc57e24Smartin 	 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
1324dcc57e24Smartin 	 * a suitable case branch will convert the type number to a string.
1325dcc57e24Smartin 	 */
1326dcc57e24Smartin 	switch (fstype) {
1327dcc57e24Smartin #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
1328dcc57e24Smartin 	case __CONCAT(FS_,tag):	str = __CONCAT(DKW_PTYPE_,tag);			break;
1329dcc57e24Smartin 	FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
1330dcc57e24Smartin #undef FSTYPE_TO_STR_CASE
1331dcc57e24Smartin 	default:		str = NULL;			break;
1332dcc57e24Smartin 	}
1333dcc57e24Smartin 
1334dcc57e24Smartin 	return (str);
1335dcc57e24Smartin }
1336dcc57e24Smartin 
1337642571f8Smartin /*
1338642571f8Smartin  * diskfd is an open file descriptor for a disk we had trouble with
1339642571f8Smartin  * creating some new wedges.
1340642571f8Smartin  * Go through all wedges actually on that disk, check if we have a
1341642571f8Smartin  * record for them and remove all others.
1342642571f8Smartin  * This should sync our internal model of partitions with the real state.
1343642571f8Smartin  */
1344642571f8Smartin static void
gpt_sanitize(int diskfd,const struct gpt_disk_partitions * parts,struct gpt_part_entry * ignore)1345642571f8Smartin gpt_sanitize(int diskfd, const struct gpt_disk_partitions *parts,
1346642571f8Smartin     struct gpt_part_entry *ignore)
1347642571f8Smartin {
1348642571f8Smartin 	struct dkwedge_info *dkw, delw;
1349642571f8Smartin 	struct dkwedge_list dkwl;
1350642571f8Smartin 	size_t bufsize;
1351642571f8Smartin 	u_int i;
1352642571f8Smartin 
1353642571f8Smartin 	dkw = NULL;
1354642571f8Smartin 	dkwl.dkwl_buf = dkw;
1355642571f8Smartin 	dkwl.dkwl_bufsize = 0;
1356642571f8Smartin 
1357642571f8Smartin 	/* get a list of all wedges */
1358642571f8Smartin 	for (;;) {
1359642571f8Smartin 		if (ioctl(diskfd, DIOCLWEDGES, &dkwl) == -1)
1360642571f8Smartin 			return;
1361642571f8Smartin 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
1362642571f8Smartin 			break;
1363642571f8Smartin 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
1364642571f8Smartin 		if (dkwl.dkwl_bufsize < bufsize) {
1365642571f8Smartin 			dkw = realloc(dkwl.dkwl_buf, bufsize);
1366642571f8Smartin 			if (dkw == NULL)
1367642571f8Smartin 				return;
1368642571f8Smartin 			dkwl.dkwl_buf = dkw;
1369642571f8Smartin 			dkwl.dkwl_bufsize = bufsize;
1370642571f8Smartin 		}
1371642571f8Smartin 	}
1372642571f8Smartin 
1373642571f8Smartin 	/* try to remove all the ones we do not know about */
1374642571f8Smartin 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
1375642571f8Smartin 		bool found = false;
1376642571f8Smartin 		const char *devname = dkw[i].dkw_devname;
1377642571f8Smartin 
1378642571f8Smartin 		for (struct gpt_part_entry *pe = parts->partitions;
1379642571f8Smartin 		    pe != NULL; pe = pe->gp_next) {
1380642571f8Smartin 			if (pe == ignore)
1381642571f8Smartin 				continue;
1382642571f8Smartin 			if ((pe->gp_flags & GPEF_WEDGE) &&
1383642571f8Smartin 			    strcmp(pe->gp_dev_name, devname) == 0) {
1384642571f8Smartin 				found = true;
1385642571f8Smartin 				break;
1386642571f8Smartin 			}
1387642571f8Smartin 		}
1388642571f8Smartin 		if (found)
1389642571f8Smartin 			continue;
1390642571f8Smartin 		memset(&delw, 0, sizeof(delw));
13917920585fSmartin 		strlcpy(delw.dkw_devname, devname, sizeof(delw.dkw_devname));
1392642571f8Smartin 		(void)ioctl(diskfd, DIOCDWEDGE, &delw);
1393642571f8Smartin 	}
1394642571f8Smartin 
1395642571f8Smartin 	/* cleanup */
1396642571f8Smartin 	free(dkw);
1397642571f8Smartin }
1398642571f8Smartin 
1399dcc57e24Smartin static bool
gpt_add_wedge(const char * disk,struct gpt_part_entry * p,const struct gpt_disk_partitions * parts)1400642571f8Smartin gpt_add_wedge(const char *disk, struct gpt_part_entry *p,
1401642571f8Smartin     const struct gpt_disk_partitions *parts)
1402dcc57e24Smartin {
1403dcc57e24Smartin 	struct dkwedge_info dkw;
1404dcc57e24Smartin 	const char *tname;
1405dcc57e24Smartin 	char diskpath[MAXPATHLEN];
1406dcc57e24Smartin 	int fd;
1407dcc57e24Smartin 
1408dcc57e24Smartin 	memset(&dkw, 0, sizeof(dkw));
1409dcc57e24Smartin 	tname = bsdlabel_fstype_to_str(p->fs_type);
1410dcc57e24Smartin 	if (tname)
1411dcc57e24Smartin 		strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
1412dcc57e24Smartin 
1413dcc57e24Smartin 	strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
1414dcc57e24Smartin 	dkw.dkw_offset = p->gp_start;
1415dcc57e24Smartin 	dkw.dkw_size = p->gp_size;
141634f3e937Smartin 	if (dkw.dkw_wname[0] == 0) {
141734f3e937Smartin 		if (p->gp_label[0] != 0)
141834f3e937Smartin 				strlcpy((char*)&dkw.dkw_wname,
141934f3e937Smartin 				    p->gp_label, sizeof(dkw.dkw_wname));
142034f3e937Smartin 	}
142134f3e937Smartin 	if (dkw.dkw_wname[0] == 0) {
142234f3e937Smartin 		snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
142334f3e937Smartin 		    "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
142434f3e937Smartin 	}
1425dcc57e24Smartin 
1426dcc57e24Smartin 	fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1427dcc57e24Smartin 	if (fd < 0)
1428dcc57e24Smartin 		return false;
1429dcc57e24Smartin 	if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
1430642571f8Smartin 		if (errno == EINVAL) {
1431642571f8Smartin 			/* sanitize existing wedges and try again */
1432642571f8Smartin 			gpt_sanitize(fd, parts, p);
1433642571f8Smartin 			if (ioctl(fd, DIOCAWEDGE, &dkw) == 0)
1434642571f8Smartin 				goto ok;
1435642571f8Smartin 		}
1436dcc57e24Smartin 		close(fd);
1437dcc57e24Smartin 		return false;
1438dcc57e24Smartin 	}
1439642571f8Smartin ok:
1440dcc57e24Smartin 	close(fd);
1441dcc57e24Smartin 
1442dcc57e24Smartin 	strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
1443dcc57e24Smartin 	p->gp_flags |= GPEF_WEDGE;
1444dcc57e24Smartin 	return true;
1445dcc57e24Smartin }
1446dcc57e24Smartin 
1447c0e3ce44Smartin static void
escape_spaces(char * dest,const char * src)1448c0e3ce44Smartin escape_spaces(char *dest, const char *src)
1449c0e3ce44Smartin {
1450c0e3ce44Smartin 	unsigned char c;
1451c0e3ce44Smartin 
1452c0e3ce44Smartin 	while (*src) {
1453c0e3ce44Smartin 		c = *src++;
1454c0e3ce44Smartin 		if (isspace(c) || c == '\\')
1455c0e3ce44Smartin 			*dest++ = '\\';
1456c0e3ce44Smartin 		*dest++ = c;
1457c0e3ce44Smartin 	}
1458c0e3ce44Smartin 	*dest = 0;
1459c0e3ce44Smartin }
1460c0e3ce44Smartin 
1461dcc57e24Smartin static bool
gpt_get_part_device(const struct disk_partitions * arg,part_id id,char * devname,size_t max_devname_len,int * part,enum dev_name_usage usage,bool with_path,bool life)1462dcc57e24Smartin gpt_get_part_device(const struct disk_partitions *arg,
1463dcc57e24Smartin     part_id id, char *devname, size_t max_devname_len, int *part,
146434f3e937Smartin     enum dev_name_usage usage, bool with_path, bool life)
1465dcc57e24Smartin {
1466dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1467dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1468dcc57e24Smartin 	struct  gpt_part_entry *p = parts->partitions;
1469c0e3ce44Smartin 	char tmpname[GPT_LABEL_LEN*2];
1470dcc57e24Smartin 	part_id no;
1471dcc57e24Smartin 
1472dcc57e24Smartin 
1473dcc57e24Smartin 	for (no = 0; p != NULL && no < id; no++)
1474dcc57e24Smartin 		p = p->gp_next;
1475dcc57e24Smartin 
1476dcc57e24Smartin 	if (no != id || p == NULL)
1477dcc57e24Smartin 		return false;
1478dcc57e24Smartin 
1479dcc57e24Smartin 	if (part)
1480dcc57e24Smartin 		*part = -1;
1481dcc57e24Smartin 
148234f3e937Smartin 	if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
148334f3e937Smartin 		usage = plain_name;
148434f3e937Smartin 	if (usage == plain_name || usage == raw_dev_name)
148534f3e937Smartin 		life = true;
148694f5fa4cSmartin 	if (!(p->gp_flags & GPEF_WEDGE) && life &&
1487642571f8Smartin 	    !gpt_add_wedge(arg->disk, p, parts))
148894f5fa4cSmartin 		return false;
1489dcc57e24Smartin 
1490dcc57e24Smartin 	switch (usage) {
1491dcc57e24Smartin 	case logical_name:
1492c0e3ce44Smartin 		if (p->gp_label[0] != 0) {
1493c0e3ce44Smartin 			escape_spaces(tmpname, p->gp_label);
1494dcc57e24Smartin 			snprintf(devname, max_devname_len,
1495c0e3ce44Smartin 			    "NAME=%s", tmpname);
1496c0e3ce44Smartin 		} else {
1497dcc57e24Smartin 			snprintf(devname, max_devname_len,
1498dcc57e24Smartin 			    "NAME=%s", p->gp_id);
1499c0e3ce44Smartin 		}
1500dcc57e24Smartin 		break;
1501dcc57e24Smartin 	case plain_name:
1502dcc57e24Smartin 		assert(p->gp_flags & GPEF_WEDGE);
1503dcc57e24Smartin 		if (with_path)
1504dcc57e24Smartin 			snprintf(devname, max_devname_len, _PATH_DEV "%s",
1505dcc57e24Smartin 			    p->gp_dev_name);
1506dcc57e24Smartin 		else
1507dcc57e24Smartin 			strlcpy(devname, p->gp_dev_name, max_devname_len);
1508dcc57e24Smartin 		break;
1509dcc57e24Smartin 	case raw_dev_name:
1510dcc57e24Smartin 		assert(p->gp_flags & GPEF_WEDGE);
1511dcc57e24Smartin 		if (with_path)
1512dcc57e24Smartin 			snprintf(devname, max_devname_len, _PATH_DEV "r%s",
1513dcc57e24Smartin 			    p->gp_dev_name);
1514dcc57e24Smartin 		else
1515dcc57e24Smartin 			snprintf(devname, max_devname_len, "r%s",
1516dcc57e24Smartin 			    p->gp_dev_name);
1517dcc57e24Smartin 		break;
1518dcc57e24Smartin 	default:
1519dcc57e24Smartin 		return false;
1520dcc57e24Smartin 	}
1521dcc57e24Smartin 
1522dcc57e24Smartin 	return true;
1523dcc57e24Smartin }
1524dcc57e24Smartin 
1525dcc57e24Smartin static bool
gpt_write_to_disk(struct disk_partitions * arg)1526dcc57e24Smartin gpt_write_to_disk(struct disk_partitions *arg)
1527dcc57e24Smartin {
1528dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1529dcc57e24Smartin 	struct gpt_part_entry *p, *n;
1530c0e3ce44Smartin 	char label_arg[sizeof(p->gp_label) + 10];
1531dcc57e24Smartin 	char diskpath[MAXPATHLEN];
1532dcc57e24Smartin 	int fd, bits = 0;
1533dcc57e24Smartin 	bool root_is_new = false, efi_is_new = false;
1534dcc57e24Smartin 	part_id root_id = NO_PART, efi_id = NO_PART, pno;
1535dcc57e24Smartin 
1536dcc57e24Smartin 	/*
1537dcc57e24Smartin 	 * Remove all wedges on this disk - they may become invalid and we
1538dcc57e24Smartin 	 * have no easy way to associate them with the partitioning data.
1539dcc57e24Smartin 	 * Instead we will explicitly request creation of wedges on demand
1540dcc57e24Smartin 	 * later.
1541dcc57e24Smartin 	 */
1542dcc57e24Smartin 	fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1543dcc57e24Smartin 	if (fd < 0)
1544dcc57e24Smartin 		return false;
1545dcc57e24Smartin 	if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
1546dcc57e24Smartin 		return false;
1547dcc57e24Smartin 	close(fd);
1548dcc57e24Smartin 
1549dcc57e24Smartin 	/*
15508ac26f1aSmartin 	 * Collect first root and efi partition (if available), clear
15518ac26f1aSmartin 	 * "have wedge" flags.
1552dcc57e24Smartin 	 */
1553dcc57e24Smartin 	for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
15548ac26f1aSmartin 		p->gp_flags &= ~GPEF_WEDGE;
15556d3e0ec1Smartin 		if (root_id == NO_PART && p->gp_type != NULL) {
1556dcc57e24Smartin 			if (p->gp_type->gent.generic_ptype == PT_root &&
1557bf7de24aSmartin 			    (p->gp_flags & GPEF_TARGET)) {
1558dcc57e24Smartin 				root_id = pno;
1559dcc57e24Smartin 				root_is_new = !(p->gp_flags & GPEF_ON_DISK);
1560dcc57e24Smartin 			} else if (efi_id == NO_PART &&
1561dcc57e24Smartin 			    p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
1562dcc57e24Smartin 				efi_id = pno;
1563dcc57e24Smartin 				efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
1564dcc57e24Smartin 			}
1565dcc57e24Smartin 		}
1566dcc57e24Smartin 	}
1567dcc57e24Smartin 
1568dcc57e24Smartin 	/*
1569dcc57e24Smartin 	 * If no GPT on disk yet, create it.
1570dcc57e24Smartin 	 */
1571dcc57e24Smartin 	if (!parts->has_gpt) {
1572dcc57e24Smartin 		char limit[30];
1573dcc57e24Smartin 
1574dcc57e24Smartin 		if (parts->max_num_parts > 0)
1575dcc57e24Smartin 			sprintf(limit, "-p %zu", parts->max_num_parts);
1576dcc57e24Smartin 		else
1577dcc57e24Smartin 			limit[0] = 0;
1578dcc57e24Smartin 		if (run_program(RUN_SILENT, "gpt create %s %s",
1579dcc57e24Smartin 		    limit, parts->dp.disk))
1580dcc57e24Smartin 			return false;
1581dcc57e24Smartin 		parts->has_gpt = true;
1582dcc57e24Smartin 	}
1583dcc57e24Smartin 
1584dcc57e24Smartin 	/*
1585dcc57e24Smartin 	 * Delete all old partitions
1586dcc57e24Smartin 	 */
1587dcc57e24Smartin 	for (p = parts->obsolete; p != NULL; p = n) {
1588dcc57e24Smartin 		run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
1589dcc57e24Smartin 		    p->gp_start, arg->disk);
1590dcc57e24Smartin 		n = p->gp_next;
1591dcc57e24Smartin 		free(p);
1592dcc57e24Smartin 	}
1593dcc57e24Smartin 	parts->obsolete = NULL;
1594dcc57e24Smartin 
1595dcc57e24Smartin 	/*
1596dcc57e24Smartin 	 * Modify existing but changed partitions
1597dcc57e24Smartin 	 */
1598dcc57e24Smartin 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
1599dcc57e24Smartin 		if (!(p->gp_flags & GPEF_ON_DISK))
1600dcc57e24Smartin 			continue;
1601dcc57e24Smartin 
1602dcc57e24Smartin 		if (p->gp_flags & GPEF_RESIZED) {
1603dcc57e24Smartin 			run_program(RUN_SILENT,
1604dcc57e24Smartin 			    "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
1605dcc57e24Smartin 			    p->gp_start, p->gp_size, arg->disk);
1606dcc57e24Smartin 			p->gp_flags &= ~GPEF_RESIZED;
1607dcc57e24Smartin 		}
1608dcc57e24Smartin 
1609dcc57e24Smartin 		if (!(p->gp_flags & GPEF_MODIFIED))
1610dcc57e24Smartin 			continue;
1611dcc57e24Smartin 
1612dcc57e24Smartin 		if (!gpt_modify_part(parts->dp.disk, p))
1613dcc57e24Smartin 			return false;
1614dcc57e24Smartin 	}
1615dcc57e24Smartin 
1616dcc57e24Smartin 	/*
1617dcc57e24Smartin 	 * Add new partitions
1618dcc57e24Smartin 	 */
1619dcc57e24Smartin 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
1620dcc57e24Smartin 		if (p->gp_flags & GPEF_ON_DISK)
1621dcc57e24Smartin 			continue;
1622dcc57e24Smartin 		if (!(p->gp_flags & GPEF_MODIFIED))
1623dcc57e24Smartin 			continue;
1624dcc57e24Smartin 
1625dcc57e24Smartin 		if (p->gp_label[0] == 0)
1626dcc57e24Smartin 			label_arg[0] = 0;
1627dcc57e24Smartin 		else
1628c0e3ce44Smartin 			sprintf(label_arg, "-l \'%s\'", p->gp_label);
1629dcc57e24Smartin 
16306d3e0ec1Smartin 		if (p->gp_type != NULL)
1631dcc57e24Smartin 			run_program(RUN_SILENT,
16326d3e0ec1Smartin 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
16336d3e0ec1Smartin 			    "s -t %s %s %s",
1634dcc57e24Smartin 			    p->gp_start, p->gp_size, p->gp_type->tid,
1635dcc57e24Smartin 			    label_arg, arg->disk);
16366d3e0ec1Smartin 		else
16376d3e0ec1Smartin 			run_program(RUN_SILENT,
16386d3e0ec1Smartin 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
16396d3e0ec1Smartin 			    "s %s %s",
16406d3e0ec1Smartin 			    p->gp_start, p->gp_size, label_arg, arg->disk);
1641dcc57e24Smartin 		gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
1642dcc57e24Smartin 		gpt_read_part(arg->disk, p->gp_start, p);
1643dcc57e24Smartin 		p->gp_flags |= GPEF_ON_DISK;
1644dcc57e24Smartin 	}
1645dcc57e24Smartin 
1646dcc57e24Smartin 	/*
1647dcc57e24Smartin 	 * Additional MD bootloader magic...
1648dcc57e24Smartin 	 */
1649dcc57e24Smartin 	if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
1650dcc57e24Smartin 	    efi_is_new))
1651dcc57e24Smartin 		return false;
1652dcc57e24Smartin 
1653dcc57e24Smartin 	return true;
1654dcc57e24Smartin }
1655dcc57e24Smartin 
16566bb5c164Smartin static part_id
gpt_find_by_name(struct disk_partitions * arg,const char * name)16576bb5c164Smartin gpt_find_by_name(struct disk_partitions *arg, const char *name)
16586bb5c164Smartin {
16596bb5c164Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
16606bb5c164Smartin 	struct gpt_part_entry *p;
16616bb5c164Smartin 	part_id pno;
16626bb5c164Smartin 
16636bb5c164Smartin 	for (pno = 0, p = parts->partitions; p != NULL;
16646bb5c164Smartin 	    p = p->gp_next, pno++) {
16656bb5c164Smartin 		if (strcmp(p->gp_label, name) == 0)
16666bb5c164Smartin 			return pno;
16676bb5c164Smartin 		if (strcmp(p->gp_id, name) == 0)
16686bb5c164Smartin 			return pno;
16696bb5c164Smartin 	}
16706bb5c164Smartin 
16716bb5c164Smartin 	return NO_PART;
16726bb5c164Smartin }
16736bb5c164Smartin 
1674dcc57e24Smartin bool
gpt_parts_check(void)1675dcc57e24Smartin gpt_parts_check(void)
1676dcc57e24Smartin {
1677dcc57e24Smartin 
1678dcc57e24Smartin 	check_available_binaries();
1679dcc57e24Smartin 
1680dcc57e24Smartin 	return have_gpt && have_dk;
1681dcc57e24Smartin }
1682dcc57e24Smartin 
1683dcc57e24Smartin static void
gpt_free(struct disk_partitions * arg)1684dcc57e24Smartin gpt_free(struct disk_partitions *arg)
1685dcc57e24Smartin {
1686dcc57e24Smartin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1687dcc57e24Smartin 	struct gpt_part_entry *p, *n;
1688dcc57e24Smartin 
1689dcc57e24Smartin 	assert(parts != NULL);
1690dcc57e24Smartin 	for (p = parts->partitions; p != NULL; p = n) {
1691da03c2cbSmartin 		if (p->gp_flags & GPEF_WEDGE)
1692da03c2cbSmartin 			register_post_umount_delwedge(parts->dp.disk,
1693da03c2cbSmartin 			    p->gp_dev_name);
1694dcc57e24Smartin 		free(__UNCONST(p->last_mounted));
1695dcc57e24Smartin 		n = p->gp_next;
1696dcc57e24Smartin 		free(p);
1697dcc57e24Smartin 	}
16988ac26f1aSmartin 	free(__UNCONST(parts->dp.disk));
1699dcc57e24Smartin 	free(parts);
1700dcc57e24Smartin }
1701dcc57e24Smartin 
170280bf2417Smartin static void
gpt_destroy_part_scheme(struct disk_partitions * arg)170380bf2417Smartin gpt_destroy_part_scheme(struct disk_partitions *arg)
170480bf2417Smartin {
170580bf2417Smartin 
170680bf2417Smartin 	run_program(RUN_SILENT, "gpt destroy %s", arg->disk);
170780bf2417Smartin 	gpt_free(arg);
170880bf2417Smartin }
170980bf2417Smartin 
1710dcc57e24Smartin static bool
gpt_custom_attribute_writable(const struct disk_partitions * arg,part_id ptn,size_t attr_no)1711dcc57e24Smartin gpt_custom_attribute_writable(const struct disk_partitions *arg,
1712dcc57e24Smartin     part_id ptn, size_t attr_no)
1713dcc57e24Smartin {
1714dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1715dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1716dcc57e24Smartin 	size_t i;
1717dcc57e24Smartin 	struct gpt_part_entry *p;
1718dcc57e24Smartin 
1719dcc57e24Smartin 	if (attr_no >= arg->pscheme->custom_attribute_count)
1720dcc57e24Smartin 		return false;
1721dcc57e24Smartin 
1722dcc57e24Smartin 	const msg label = arg->pscheme->custom_attributes[attr_no].label;
1723dcc57e24Smartin 
1724dcc57e24Smartin 	/* we can not edit the uuid attribute */
1725dcc57e24Smartin 	if (label == MSG_ptn_uuid)
1726dcc57e24Smartin 		return false;
1727dcc57e24Smartin 
1728dcc57e24Smartin 	/* the label is always editable */
1729dcc57e24Smartin 	if (label == MSG_ptn_label)
1730dcc57e24Smartin 		return true;
1731dcc57e24Smartin 
1732dcc57e24Smartin 	/* the GPT type is read only */
1733dcc57e24Smartin 	if (label == MSG_ptn_gpt_type)
1734dcc57e24Smartin 		return false;
1735dcc57e24Smartin 
1736dcc57e24Smartin 	/* BOOTME makes no sense on swap partitions */
1737dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1738dcc57e24Smartin 		if (i == ptn)
1739dcc57e24Smartin 			break;
1740dcc57e24Smartin 
1741dcc57e24Smartin 	if (p == NULL)
1742dcc57e24Smartin 		return false;
1743dcc57e24Smartin 
17446d3e0ec1Smartin 	if (p->fs_type == FS_SWAP ||
17456d3e0ec1Smartin 	    (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
1746dcc57e24Smartin 		return false;
1747dcc57e24Smartin 
1748dcc57e24Smartin 	return true;
1749dcc57e24Smartin }
1750dcc57e24Smartin 
17515823c3d0Smartin static const char *
gpt_get_label_str(const struct disk_partitions * arg,part_id ptn)17525823c3d0Smartin gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
17535823c3d0Smartin {
17545823c3d0Smartin 	const struct gpt_disk_partitions *parts =
17555823c3d0Smartin 	    (const struct gpt_disk_partitions*)arg;
17565823c3d0Smartin 	size_t i;
17575823c3d0Smartin 	struct gpt_part_entry *p;
17585823c3d0Smartin 
17595823c3d0Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
17605823c3d0Smartin 		if (i == ptn)
17615823c3d0Smartin 			break;
17625823c3d0Smartin 
17635823c3d0Smartin 	if (p == NULL)
17645823c3d0Smartin 		return NULL;
17655823c3d0Smartin 
17665823c3d0Smartin 	if (p->gp_label[0] != 0)
17675823c3d0Smartin 		return p->gp_label;
17685823c3d0Smartin 	return p->gp_id;
17695823c3d0Smartin }
17705823c3d0Smartin 
1771dcc57e24Smartin static bool
gpt_format_custom_attribute(const struct disk_partitions * arg,part_id ptn,size_t attr_no,const struct disk_part_info * info,char * out,size_t out_space)1772dcc57e24Smartin gpt_format_custom_attribute(const struct disk_partitions *arg,
1773dcc57e24Smartin     part_id ptn, size_t attr_no, const struct disk_part_info *info,
1774dcc57e24Smartin     char *out, size_t out_space)
1775dcc57e24Smartin {
1776dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1777dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1778dcc57e24Smartin 	size_t i;
1779dcc57e24Smartin 	struct gpt_part_entry *p, data;
1780dcc57e24Smartin 
1781dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1782dcc57e24Smartin 		if (i == ptn)
1783dcc57e24Smartin 			break;
1784dcc57e24Smartin 
1785dcc57e24Smartin 	if (p == NULL)
1786dcc57e24Smartin 		return false;
1787dcc57e24Smartin 
1788dcc57e24Smartin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1789dcc57e24Smartin 		return false;
1790dcc57e24Smartin 
1791dcc57e24Smartin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1792dcc57e24Smartin 
1793dcc57e24Smartin 	if (info != NULL) {
1794dcc57e24Smartin 		data = *p;
1795dcc57e24Smartin 		gpt_info_to_part(&data, info, NULL);
1796dcc57e24Smartin 		p = &data;
1797dcc57e24Smartin 	}
1798dcc57e24Smartin 
1799dcc57e24Smartin 	if (label == MSG_ptn_label)
1800dcc57e24Smartin 		strlcpy(out, p->gp_label, out_space);
1801dcc57e24Smartin 	else if (label == MSG_ptn_uuid)
1802dcc57e24Smartin 		strlcpy(out, p->gp_id, out_space);
18036d3e0ec1Smartin 	else if (label == MSG_ptn_gpt_type) {
18046d3e0ec1Smartin 		if (p->gp_type != NULL)
1805dcc57e24Smartin 			strlcpy(out, p->gp_type->gent.description, out_space);
18066d3e0ec1Smartin 		else if (out_space > 1)
18076d3e0ec1Smartin 			out[0] = 0;
18086d3e0ec1Smartin 	} else if (label == MSG_ptn_boot)
1809dcc57e24Smartin 		strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
1810dcc57e24Smartin 		    MSG_Yes : MSG_No), out_space);
1811dcc57e24Smartin 	else
1812dcc57e24Smartin 		return false;
1813dcc57e24Smartin 
1814dcc57e24Smartin 	return true;
1815dcc57e24Smartin }
1816dcc57e24Smartin 
1817dcc57e24Smartin static bool
gpt_custom_attribute_toggle(struct disk_partitions * arg,part_id ptn,size_t attr_no)1818dcc57e24Smartin gpt_custom_attribute_toggle(struct disk_partitions *arg,
1819dcc57e24Smartin     part_id ptn, size_t attr_no)
1820dcc57e24Smartin {
1821dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1822dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1823dcc57e24Smartin 	size_t i;
1824dcc57e24Smartin 	struct gpt_part_entry *p;
1825dcc57e24Smartin 
1826dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1827dcc57e24Smartin 		if (i == ptn)
1828dcc57e24Smartin 			break;
1829dcc57e24Smartin 
1830dcc57e24Smartin 	if (p == NULL)
1831dcc57e24Smartin 		return false;
1832dcc57e24Smartin 
1833dcc57e24Smartin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1834dcc57e24Smartin 		return false;
1835dcc57e24Smartin 
1836dcc57e24Smartin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1837dcc57e24Smartin 	if (label != MSG_ptn_boot)
1838dcc57e24Smartin 		return false;
1839dcc57e24Smartin 
1840dcc57e24Smartin 	if (p->gp_attr & GPT_ATTR_BOOT) {
1841dcc57e24Smartin 		p->gp_attr &= ~GPT_ATTR_BOOT;
1842dcc57e24Smartin 	} else {
1843dcc57e24Smartin 		for (i = 0, p = parts->partitions; p != NULL;
1844dcc57e24Smartin 		    i++, p = p->gp_next)
1845dcc57e24Smartin 			if (i == ptn)
1846dcc57e24Smartin 				p->gp_attr |= GPT_ATTR_BOOT;
1847dcc57e24Smartin 			else
1848dcc57e24Smartin 				p->gp_attr &= ~GPT_ATTR_BOOT;
1849dcc57e24Smartin 	}
1850dcc57e24Smartin 	return true;
1851dcc57e24Smartin }
1852dcc57e24Smartin 
1853dcc57e24Smartin static bool
gpt_custom_attribute_set_str(struct disk_partitions * arg,part_id ptn,size_t attr_no,const char * new_val)1854dcc57e24Smartin gpt_custom_attribute_set_str(struct disk_partitions *arg,
1855dcc57e24Smartin     part_id ptn, size_t attr_no, const char *new_val)
1856dcc57e24Smartin {
1857dcc57e24Smartin 	const struct gpt_disk_partitions *parts =
1858dcc57e24Smartin 	    (const struct gpt_disk_partitions*)arg;
1859dcc57e24Smartin 	size_t i;
1860dcc57e24Smartin 	struct gpt_part_entry *p;
1861dcc57e24Smartin 
1862dcc57e24Smartin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1863dcc57e24Smartin 		if (i == ptn)
1864dcc57e24Smartin 			break;
1865dcc57e24Smartin 
1866dcc57e24Smartin 	if (p == NULL)
1867dcc57e24Smartin 		return false;
1868dcc57e24Smartin 
1869dcc57e24Smartin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1870dcc57e24Smartin 		return false;
1871dcc57e24Smartin 
1872dcc57e24Smartin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1873dcc57e24Smartin 
1874dcc57e24Smartin 	if (label != MSG_ptn_label)
1875dcc57e24Smartin 		return false;
1876dcc57e24Smartin 
1877dcc57e24Smartin 	strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
1878dcc57e24Smartin 	return true;
1879dcc57e24Smartin }
1880dcc57e24Smartin 
1881dcc57e24Smartin static bool
gpt_have_boot_support(const char * disk)1882dcc57e24Smartin gpt_have_boot_support(const char *disk)
1883dcc57e24Smartin {
1884dcc57e24Smartin #ifdef	HAVE_GPT_BOOT
1885dcc57e24Smartin 	return true;
1886dcc57e24Smartin #else
1887dcc57e24Smartin 	return false;
1888dcc57e24Smartin #endif
1889dcc57e24Smartin }
1890dcc57e24Smartin 
1891dcc57e24Smartin const struct disk_part_custom_attribute gpt_custom_attrs[] = {
1892dcc57e24Smartin 	{ .label = MSG_ptn_label,	.type = pet_str },
1893dcc57e24Smartin 	{ .label = MSG_ptn_uuid,	.type = pet_str },
1894dcc57e24Smartin 	{ .label = MSG_ptn_gpt_type,	.type = pet_str },
1895dcc57e24Smartin 	{ .label = MSG_ptn_boot,	.type = pet_bool },
1896dcc57e24Smartin };
1897dcc57e24Smartin 
1898dcc57e24Smartin const struct disk_partitioning_scheme
1899dcc57e24Smartin gpt_parts = {
1900dcc57e24Smartin 	.name = MSG_parttype_gpt,
1901dcc57e24Smartin 	.short_name = MSG_parttype_gpt_short,
1902dcc57e24Smartin 	.part_flag_desc = MSG_gpt_flag_desc,
1903dcc57e24Smartin 	.custom_attribute_count = __arraycount(gpt_custom_attrs),
1904dcc57e24Smartin 	.custom_attributes = gpt_custom_attrs,
1905dcc57e24Smartin 	.get_part_types_count = gpt_type_count,
1906dcc57e24Smartin 	.get_part_type = gpt_get_ptype,
1907dcc57e24Smartin 	.get_generic_part_type = gpt_get_generic_type,
1908dcc57e24Smartin 	.get_fs_part_type = gpt_get_fs_part_type,
190934f3e937Smartin 	.get_default_fstype = gpt_get_default_fstype,
19108ac26f1aSmartin 	.create_custom_part_type = gpt_create_custom_part_type,
19118ac26f1aSmartin 	.create_unknown_part_type = gpt_create_unknown_part_type,
1912dcc57e24Smartin 	.get_part_alignment = gpt_get_part_alignment,
1913dcc57e24Smartin 	.read_from_disk = gpt_read_from_disk,
191475761763Smartin 	.get_cylinder_size = gpt_cyl_size,
1915dcc57e24Smartin 	.create_new_for_disk = gpt_create_new,
1916dcc57e24Smartin 	.have_boot_support = gpt_have_boot_support,
19176bb5c164Smartin 	.find_by_name = gpt_find_by_name,
1918dcc57e24Smartin 	.can_add_partition = gpt_can_add_partition,
1919dcc57e24Smartin 	.custom_attribute_writable = gpt_custom_attribute_writable,
1920dcc57e24Smartin 	.format_custom_attribute = gpt_format_custom_attribute,
1921dcc57e24Smartin 	.custom_attribute_toggle = gpt_custom_attribute_toggle,
1922dcc57e24Smartin 	.custom_attribute_set_str = gpt_custom_attribute_set_str,
19235823c3d0Smartin 	.other_partition_identifier = gpt_get_label_str,
1924dcc57e24Smartin 	.get_part_device = gpt_get_part_device,
1925dcc57e24Smartin 	.max_free_space_at = gpt_max_free_space_at,
1926dcc57e24Smartin 	.get_free_spaces = gpt_get_free_spaces,
19278ac26f1aSmartin 	.adapt_foreign_part_info = generic_adapt_foreign_part_info,
1928dcc57e24Smartin 	.get_part_info = gpt_get_part_info,
1929dcc57e24Smartin 	.get_part_attr_str = gpt_get_part_attr_str,
1930dcc57e24Smartin 	.set_part_info = gpt_set_part_info,
1931dcc57e24Smartin 	.add_partition = gpt_add_part,
1932dcc57e24Smartin 	.delete_all_partitions = gpt_delete_all_partitions,
1933dcc57e24Smartin 	.delete_partition = gpt_delete_partition,
1934dcc57e24Smartin 	.write_to_disk = gpt_write_to_disk,
1935dcc57e24Smartin 	.free = gpt_free,
193680bf2417Smartin 	.destroy_part_scheme = gpt_destroy_part_scheme,
19378ac26f1aSmartin 	.cleanup = gpt_cleanup,
1938dcc57e24Smartin };
1939