Home | History | Annotate | Line # | Download | only in sysinst
gpt.c revision 1.6.2.10
      1  1.6.2.10  msaitoh /*	$NetBSD: gpt.c,v 1.6.2.10 2022/02/02 04:25:36 msaitoh Exp $	*/
      2       1.1   martin 
      3       1.1   martin /*
      4       1.1   martin  * Copyright 2018 The NetBSD Foundation, Inc.
      5       1.1   martin  * All rights reserved.
      6       1.1   martin  *
      7       1.1   martin  * Redistribution and use in source and binary forms, with or without
      8       1.1   martin  * modification, are permitted provided that the following conditions
      9       1.1   martin  * are met:
     10       1.1   martin  * 1. Redistributions of source code must retain the above copyright
     11       1.1   martin  *    notice, this list of conditions and the following disclaimer.
     12       1.1   martin  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   martin  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   martin  *    documentation and/or other materials provided with the distribution.
     15       1.1   martin  *
     16       1.1   martin  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     17       1.1   martin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1   martin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1   martin  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     20       1.1   martin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1   martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1   martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1   martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1   martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1   martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26       1.1   martin  * THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1   martin  *
     28       1.1   martin  */
     29       1.1   martin 
     30       1.1   martin #include "defs.h"
     31       1.1   martin #include "mbr.h"
     32       1.1   martin #include "md.h"
     33       1.1   martin #include "gpt_uuid.h"
     34       1.1   martin #include <assert.h>
     35   1.6.2.6  msaitoh #include <err.h>
     36       1.1   martin #include <paths.h>
     37       1.1   martin #include <sys/param.h>
     38       1.1   martin #include <sys/ioctl.h>
     39       1.1   martin #include <util.h>
     40   1.6.2.6  msaitoh #include <uuid.h>
     41       1.1   martin 
     42       1.1   martin bool	gpt_parts_check(void);	/* check for needed binaries */
     43       1.1   martin 
     44       1.1   martin 
     45       1.1   martin /*************** GPT ************************************************/
     46       1.1   martin /* a GPT based disk_partitions interface */
     47       1.1   martin 
     48       1.1   martin #define GUID_STR_LEN	40
     49   1.6.2.6  msaitoh #define	GPT_PTYPE_ALLOC	32	/* initial type array allocation, should be >
     50   1.6.2.6  msaitoh 				 * gpt type -l | wc -l */
     51  1.6.2.10  msaitoh #define	GPT_DEV_LEN	DISKNAMESIZE	/* dkNN */
     52       1.1   martin 
     53   1.6.2.8  msaitoh #define	GPT_PARTS_PER_SEC	4	/* a 512 byte sector holds 4 entries */
     54       1.1   martin #define	GPT_DEFAULT_MAX_PARTS	128
     55       1.1   martin 
     56       1.1   martin /* a usable label will be short, so we can get away with an arbitrary limit */
     57       1.1   martin #define	GPT_LABEL_LEN		96
     58       1.1   martin 
     59       1.1   martin #define	GPT_ATTR_BIOSBOOT	1
     60       1.1   martin #define	GPT_ATTR_BOOTME		2
     61       1.1   martin #define	GPT_ATTR_BOOTONCE	4
     62       1.1   martin #define	GPT_ATTR_BOOTFAILED	8
     63       1.1   martin #define	GPT_ATTR_NOBLOCKIO	16
     64       1.1   martin #define	GPT_ATTR_REQUIRED	32
     65       1.1   martin 
     66       1.1   martin /* when we don't care for BIOS or UEFI boot, use the combined boot flags */
     67       1.1   martin #define	GPT_ATTR_BOOT	(GPT_ATTR_BIOSBOOT|GPT_ATTR_BOOTME)
     68       1.1   martin 
     69       1.1   martin struct gpt_attr_desc {
     70       1.1   martin 	const char *name;
     71       1.1   martin 	uint flag;
     72       1.1   martin };
     73       1.1   martin static const struct gpt_attr_desc gpt_avail_attrs[] = {
     74       1.1   martin 	{ "biosboot", GPT_ATTR_BIOSBOOT },
     75       1.1   martin 	{ "bootme", GPT_ATTR_BOOTME },
     76       1.1   martin 	{ "bootonce", GPT_ATTR_BOOTONCE },
     77       1.1   martin 	{ "bootfailed", GPT_ATTR_BOOTFAILED },
     78       1.1   martin 	{ "noblockio", GPT_ATTR_NOBLOCKIO },
     79       1.1   martin 	{ "required", GPT_ATTR_REQUIRED },
     80       1.1   martin 	{ NULL, 0 }
     81       1.1   martin };
     82       1.1   martin 
     83       1.1   martin struct gpt_ptype_desc {
     84       1.1   martin 	struct part_type_desc gent;
     85       1.1   martin 	char tid[GUID_STR_LEN];
     86       1.1   martin 	uint fsflags, default_fs_type;
     87       1.1   martin };
     88       1.1   martin 
     89       1.1   martin static const
     90       1.1   martin struct {
     91       1.1   martin 	const char *name;
     92       1.1   martin 	uint fstype;
     93       1.1   martin 	enum part_type ptype;
     94       1.1   martin 	uint fsflags;
     95       1.1   martin } gpt_fs_types[] = {
     96       1.1   martin 	{ .name = "ffs",	.fstype = FS_BSDFFS,	.ptype = PT_root,
     97       1.1   martin 	  .fsflags = GLM_LIKELY_FFS },
     98       1.1   martin 	{ .name = "swap",	.fstype = FS_SWAP,	.ptype = PT_swap },
     99       1.1   martin 	{ .name = "windows",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
    100       1.1   martin 	  .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
    101       1.1   martin 	{ .name = "windows",	.fstype = FS_NTFS,	.ptype = PT_FAT,
    102       1.1   martin 	  .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
    103       1.1   martin 	{ .name = "efi",	.fstype = FS_MSDOS,	.ptype = PT_EFI_SYSTEM,
    104       1.1   martin 	  .fsflags = GLM_MAYBE_FAT32 },
    105       1.1   martin 	{ .name = "bios",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
    106       1.1   martin 	  .fsflags = GLM_MAYBE_FAT32 },
    107       1.1   martin 	{ .name = "lfs",	.fstype = FS_BSDLFS,	.ptype = PT_root },
    108       1.1   martin 	{ .name = "linux-data",	.fstype = FS_EX2FS,	.ptype = PT_root },
    109       1.1   martin 	{ .name = "apple",	.fstype = FS_HFS,	.ptype = PT_unknown },
    110   1.6.2.8  msaitoh 	{ .name = "ccd",	.fstype = FS_CCD,	.ptype = PT_root },
    111   1.6.2.8  msaitoh 	{ .name = "cgd",	.fstype = FS_CGD,	.ptype = PT_root },
    112       1.1   martin 	{ .name = "raid",	.fstype = FS_RAID,	.ptype = PT_root },
    113       1.1   martin 	{ .name = "vmcore",	.fstype = FS_VMKCORE,	.ptype = PT_unknown },
    114       1.1   martin 	{ .name = "vmfs",	.fstype = FS_VMFS,	.ptype = PT_unknown },
    115       1.1   martin 	{ .name = "vmresered",	.fstype = FS_VMWRESV,	.ptype = PT_unknown }
    116       1.1   martin };
    117       1.1   martin 
    118   1.6.2.6  msaitoh static size_t gpt_ptype_cnt = 0, gpt_ptype_alloc = 0;
    119   1.6.2.6  msaitoh static struct gpt_ptype_desc *gpt_ptype_descs = NULL;
    120   1.6.2.6  msaitoh 
    121   1.6.2.6  msaitoh /* "well" known types with special handling */
    122   1.6.2.6  msaitoh static const struct part_type_desc *gpt_native_root;
    123       1.1   martin 
    124       1.1   martin /* similar to struct gpt_ent, but matching our needs */
    125       1.1   martin struct gpt_part_entry {
    126       1.1   martin 	const struct gpt_ptype_desc *gp_type;
    127       1.1   martin 	char gp_id[GUID_STR_LEN];	/* partition guid as string */
    128       1.1   martin 	daddr_t gp_start, gp_size;
    129       1.1   martin 	uint gp_attr;			/* various attribute bits */
    130       1.1   martin 	char gp_label[GPT_LABEL_LEN];	/* user defined label */
    131       1.1   martin 	char gp_dev_name[GPT_DEV_LEN];	/* name of wedge */
    132       1.1   martin 	const char *last_mounted;	/* last mounted if known */
    133   1.6.2.9   bouyer 	uint fs_type, fs_sub_type,	/* FS_* and maybe sub type */
    134   1.6.2.9   bouyer 	    fs_opt1, fs_opt2, fs_opt3;	/* transient file system options */
    135       1.1   martin 	uint gp_flags;
    136       1.1   martin #define	GPEF_ON_DISK	1		/* This entry exists on-disk */
    137       1.1   martin #define	GPEF_MODIFIED	2		/* this entry has been changed */
    138       1.1   martin #define	GPEF_WEDGE	4		/* wedge for this exists */
    139       1.1   martin #define	GPEF_RESIZED	8		/* size has changed */
    140   1.6.2.9   bouyer #define	GPEF_TARGET	16		/* marked install target */
    141       1.1   martin 	struct gpt_part_entry *gp_next;
    142       1.1   martin };
    143       1.1   martin 
    144       1.1   martin static const struct gpt_ptype_desc *gpt_find_native_type(
    145       1.1   martin     const struct part_type_desc *gent);
    146       1.1   martin static const struct gpt_ptype_desc *gpt_find_guid_type(const char*);
    147       1.1   martin static bool
    148       1.1   martin gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
    149       1.1   martin     const char **err_msg);
    150       1.1   martin 
    151       1.1   martin const struct disk_partitioning_scheme gpt_parts;
    152       1.1   martin struct gpt_disk_partitions {
    153       1.1   martin 	struct disk_partitions dp;
    154       1.1   martin 	/*
    155       1.1   martin 	 * We keep a list of our current valid partitions, pointed
    156       1.1   martin 	 * to by "partitions".
    157       1.1   martin 	 * dp.num_part is the number of entries in "partitions".
    158       1.1   martin 	 * When partitions that have a representation on disk already
    159       1.1   martin 	 * are deleted, we move them to the "obsolete" list so we
    160       1.1   martin 	 * can issue the proper commands to remove it when writing back.
    161       1.1   martin 	 */
    162       1.1   martin 	struct gpt_part_entry *partitions,	/* current partitions */
    163       1.1   martin 	    *obsolete;				/* deleted partitions */
    164       1.1   martin 	size_t max_num_parts;			/* how many entries max? */
    165       1.1   martin 	size_t prologue, epilogue;		/* number of sectors res. */
    166       1.1   martin 	bool has_gpt;	/* disk already has a GPT */
    167       1.1   martin };
    168       1.1   martin 
    169       1.1   martin /*
    170       1.1   martin  * Init global variables from MD details
    171       1.1   martin  */
    172       1.1   martin static void
    173       1.1   martin gpt_md_init(bool is_boot_disk, size_t *max_parts, size_t *head, size_t *tail)
    174       1.1   martin {
    175       1.1   martin 	size_t num;
    176       1.1   martin 
    177       1.1   martin 	if (is_boot_disk) {
    178       1.1   martin #ifdef MD_GPT_INITIAL_SIZE
    179       1.1   martin #if MD_GPT_INITIAL_SIZE < 2*512
    180       1.1   martin #error	impossible small GPT prologue
    181       1.1   martin #endif
    182       1.1   martin 		num = ((MD_GPT_INITIAL_SIZE-(2*512))/512)*GPT_PARTS_PER_SEC;
    183       1.1   martin #else
    184       1.1   martin 		num = GPT_DEFAULT_MAX_PARTS;
    185       1.1   martin #endif
    186       1.1   martin 	} else {
    187       1.1   martin 		num = GPT_DEFAULT_MAX_PARTS;
    188       1.1   martin 	}
    189       1.1   martin 	*max_parts = num;
    190       1.1   martin 	*head = 2 + num/GPT_PARTS_PER_SEC;
    191       1.1   martin 	*tail = 1 + num/GPT_PARTS_PER_SEC;
    192       1.1   martin }
    193       1.1   martin 
    194       1.1   martin /*
    195       1.1   martin  * Parse a part of "gpt show" output into a struct gpt_part_entry.
    196       1.1   martin  * Output is from "show -a" format if details = false, otherwise
    197       1.1   martin  * from details for a specific partition (show -i or show -b)
    198       1.1   martin  */
    199       1.1   martin static void
    200       1.1   martin gpt_add_info(struct gpt_part_entry *part, const char *tag, char *val,
    201       1.1   martin     bool details)
    202       1.1   martin {
    203       1.1   martin 	char *s, *e;
    204       1.1   martin 
    205       1.1   martin 	if (details && strcmp(tag, "Start:") == 0) {
    206       1.1   martin 		part->gp_start = strtouq(val, NULL, 10);
    207       1.1   martin 	} else if (details && strcmp(tag, "Size:") == 0) {
    208       1.1   martin 		part->gp_size = strtouq(val, NULL, 10);
    209       1.1   martin 	} else if (details && strcmp(tag, "Type:") == 0) {
    210       1.1   martin 		s = strchr(val, '(');
    211       1.1   martin 		if (!s)
    212       1.1   martin 			return;
    213       1.1   martin 		e = strchr(s, ')');
    214       1.1   martin 		if (!e)
    215       1.1   martin 			return;
    216       1.1   martin 		*e = 0;
    217       1.1   martin 		part->gp_type = gpt_find_guid_type(s+1);
    218       1.1   martin 	} else if (strcmp(tag, "TypeID:") == 0) {
    219       1.1   martin 		part->gp_type = gpt_find_guid_type(val);
    220       1.1   martin 	} else if (strcmp(tag, "GUID:") == 0) {
    221       1.1   martin 		strlcpy(part->gp_id, val, sizeof(part->gp_id));
    222       1.1   martin 	} else if (strcmp(tag, "Label:") == 0) {
    223   1.6.2.1  msaitoh 		strlcpy(part->gp_label, val, sizeof(part->gp_label));
    224       1.1   martin 	} else if (strcmp(tag, "Attributes:") == 0) {
    225       1.1   martin 		char *n;
    226       1.1   martin 
    227       1.1   martin 		while ((n = strsep(&val, ", ")) != NULL) {
    228       1.1   martin 			if (*n == 0)
    229       1.1   martin 				continue;
    230       1.1   martin 			for (const struct gpt_attr_desc *p = gpt_avail_attrs;
    231       1.1   martin 			    p->name != NULL; p++) {
    232       1.1   martin 				if (strcmp(p->name, n) == 0)
    233       1.1   martin 					part->gp_attr |= p->flag;
    234       1.1   martin 			}
    235       1.1   martin 		}
    236       1.1   martin 	}
    237       1.1   martin }
    238       1.1   martin 
    239   1.6.2.2  msaitoh /*
    240   1.6.2.2  msaitoh  * Find the partition matching this wedge info and record that we
    241   1.6.2.2  msaitoh  * have a wedge already.
    242   1.6.2.2  msaitoh  */
    243   1.6.2.2  msaitoh static void
    244   1.6.2.2  msaitoh update_part_from_wedge_info(struct gpt_disk_partitions *parts,
    245   1.6.2.2  msaitoh     const struct dkwedge_info *dkw)
    246   1.6.2.2  msaitoh {
    247   1.6.2.2  msaitoh 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
    248   1.6.2.2  msaitoh 	    p = p->gp_next) {
    249   1.6.2.2  msaitoh 		if (p->gp_start != dkw->dkw_offset ||
    250   1.6.2.2  msaitoh 		    (uint64_t)p->gp_size != dkw->dkw_size)
    251   1.6.2.2  msaitoh 			continue;
    252   1.6.2.2  msaitoh 		p->gp_flags |= GPEF_WEDGE;
    253   1.6.2.2  msaitoh 		strlcpy(p->gp_dev_name, dkw->dkw_devname,
    254   1.6.2.2  msaitoh 		    sizeof p->gp_dev_name);
    255   1.6.2.2  msaitoh 		return;
    256   1.6.2.2  msaitoh 	}
    257   1.6.2.2  msaitoh }
    258   1.6.2.2  msaitoh 
    259       1.1   martin static struct disk_partitions *
    260   1.6.2.8  msaitoh gpt_read_from_disk(const char *dev, daddr_t start, daddr_t len, size_t bps,
    261   1.6.2.4  msaitoh     const struct disk_partitioning_scheme *scheme)
    262       1.1   martin {
    263       1.1   martin 	char diskpath[MAXPATHLEN];
    264       1.1   martin 	int fd;
    265   1.6.2.2  msaitoh 	struct dkwedge_info *dkw;
    266   1.6.2.2  msaitoh 	struct dkwedge_list dkwl;
    267   1.6.2.2  msaitoh 	size_t bufsize, dk;
    268       1.1   martin 
    269       1.1   martin 	assert(start == 0);
    270       1.1   martin 	assert(have_gpt);
    271       1.1   martin 
    272       1.1   martin 	if (run_program(RUN_SILENT | RUN_ERROR_OK,
    273       1.1   martin 	    "gpt -rq header %s", dev) != 0)
    274       1.1   martin 		return NULL;
    275       1.1   martin 
    276       1.1   martin 	/* read the partitions */
    277       1.1   martin 	int i;
    278       1.1   martin 	unsigned int p_index;
    279       1.1   martin 	daddr_t p_start = 0, p_size = 0, avail_start = 0, avail_size = 0,
    280       1.1   martin 	    disk_size = 0;
    281       1.1   martin 	char *textbuf, *t, *tt, p_type[STRSIZE];
    282       1.1   martin 	static const char regpart_prefix[] = "GPT part - ";
    283       1.1   martin 	struct gpt_disk_partitions *parts;
    284       1.1   martin 	struct gpt_part_entry *last = NULL, *add_to = NULL;
    285   1.6.2.9   bouyer 	const struct gpt_ptype_desc *native_root
    286   1.6.2.9   bouyer 	     = gpt_find_native_type(gpt_native_root);
    287   1.6.2.9   bouyer 	bool have_target = false;
    288       1.1   martin 
    289       1.1   martin 	if (collect(T_OUTPUT, &textbuf, "gpt -r show -a %s 2>/dev/null", dev)
    290       1.1   martin 	    < 1)
    291       1.1   martin 		return NULL;
    292       1.1   martin 
    293       1.1   martin 	/* parse output and create our list */
    294       1.1   martin 	parts = calloc(1, sizeof(*parts));
    295       1.1   martin 	if (parts == NULL)
    296       1.1   martin 		return NULL;
    297       1.1   martin 
    298       1.1   martin 	(void)strtok(textbuf, "\n"); /* ignore first line */
    299       1.1   martin 	while ((t = strtok(NULL, "\n")) != NULL) {
    300       1.1   martin 		i = 0; p_start = 0; p_size = 0; p_index = 0;
    301       1.1   martin 		p_type[0] = 0;
    302       1.1   martin 		while ((tt = strsep(&t, " \t")) != NULL) {
    303       1.1   martin 			if (strlen(tt) == 0)
    304       1.1   martin 				continue;
    305       1.1   martin 			if (i == 0) {
    306       1.1   martin 				if (add_to != NULL)
    307       1.1   martin 					gpt_add_info(add_to, tt, t, false);
    308       1.1   martin 				p_start = strtouq(tt, NULL, 10);
    309       1.1   martin 				if (p_start == 0 && add_to != NULL)
    310       1.1   martin 					break;
    311       1.1   martin 				else
    312       1.1   martin 					add_to = NULL;
    313       1.1   martin 			}
    314       1.1   martin 			if (i == 1)
    315       1.1   martin 				p_size = strtouq(tt, NULL, 10);
    316       1.1   martin 			if (i == 2)
    317       1.1   martin 				p_index = strtouq(tt, NULL, 10);
    318       1.1   martin 			if (i > 2 || (i == 2 && p_index == 0)) {
    319       1.1   martin 				if (p_type[0])
    320       1.1   martin 					strlcat(p_type, " ", STRSIZE);
    321       1.1   martin 				strlcat(p_type, tt, STRSIZE);
    322       1.1   martin 			}
    323       1.1   martin 			i++;
    324       1.1   martin 		}
    325       1.1   martin 
    326       1.1   martin 		if (p_start == 0 || p_size == 0)
    327       1.1   martin 			continue;
    328       1.1   martin 		else if (strcmp(p_type, "Pri GPT table") == 0) {
    329       1.1   martin 			avail_start = p_start + p_size;
    330       1.1   martin 			parts->prologue = avail_start;
    331       1.1   martin 			parts->epilogue = p_size + 1;
    332       1.1   martin 			parts->max_num_parts = p_size * GPT_PARTS_PER_SEC;
    333       1.1   martin 		} else if (strcmp(p_type, "Sec GPT table") == 0)
    334       1.1   martin 			avail_size = p_start - avail_start;
    335       1.1   martin 		else if(strcmp(p_type, "Sec GPT header") == 0)
    336       1.1   martin 			disk_size = p_start + p_size;
    337       1.1   martin 		else if (p_index == 0 && strlen(p_type) > 0)
    338       1.1   martin 			/* Utilitary entry (PMBR, etc) */
    339       1.1   martin 			continue;
    340       1.1   martin 		else if (p_index == 0) {
    341       1.1   martin 			/* Free space */
    342       1.1   martin 			continue;
    343       1.1   martin 		} else {
    344       1.1   martin 			/* Usual partition */
    345       1.1   martin 			tt = p_type;
    346       1.1   martin 			if (strncmp(tt, regpart_prefix,
    347       1.1   martin 			    strlen(regpart_prefix)) == 0)
    348       1.1   martin 				tt += strlen(regpart_prefix);
    349       1.1   martin 
    350       1.1   martin 			/* Add to our linked list */
    351       1.1   martin 			struct gpt_part_entry *np = calloc(1, sizeof(*np));
    352       1.1   martin 			if (np == NULL)
    353       1.1   martin 				break;
    354       1.1   martin 
    355       1.1   martin 			strlcpy(np->gp_label, tt, sizeof(np->gp_label));
    356       1.1   martin 			np->gp_start = p_start;
    357       1.1   martin 			np->gp_size = p_size;
    358       1.1   martin 			np->gp_flags |= GPEF_ON_DISK;
    359   1.6.2.9   bouyer 			if (!have_target && native_root != NULL &&
    360   1.6.2.9   bouyer 			    strcmp(np->gp_id, native_root->tid) == 0) {
    361   1.6.2.9   bouyer 				have_target = true;
    362   1.6.2.9   bouyer 				np->gp_flags |= GPEF_TARGET;
    363   1.6.2.9   bouyer 			}
    364       1.1   martin 
    365       1.1   martin 			if (last == NULL)
    366       1.1   martin 				parts->partitions = np;
    367       1.1   martin 			else
    368       1.1   martin 				last->gp_next = np;
    369       1.1   martin 			last = np;
    370       1.1   martin 			add_to = np;
    371       1.1   martin 			parts->dp.num_part++;
    372       1.1   martin 		}
    373       1.1   martin 	}
    374       1.1   martin 	free(textbuf);
    375       1.1   martin 
    376       1.2   martin 	/* If the GPT was not complete (e.g. truncated image), barf */
    377       1.2   martin 	if (disk_size <= 0) {
    378       1.2   martin 		free(parts);
    379       1.2   martin 		return NULL;
    380       1.2   martin 	}
    381       1.2   martin 
    382   1.6.2.4  msaitoh 	parts->dp.pscheme = scheme;
    383   1.6.2.6  msaitoh 	parts->dp.disk = strdup(dev);
    384       1.1   martin 	parts->dp.disk_start = start;
    385       1.1   martin 	parts->dp.disk_size = disk_size;
    386       1.1   martin 	parts->dp.free_space = avail_size;
    387   1.6.2.8  msaitoh 	parts->dp.bytes_per_sector = bps;
    388       1.1   martin 	parts->has_gpt = true;
    389       1.1   martin 
    390       1.1   martin 	fd = opendisk(parts->dp.disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
    391       1.1   martin 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
    392       1.1   martin 	    p = p->gp_next) {
    393       1.1   martin #ifdef DEFAULT_UFS2
    394       1.1   martin 		bool fs_is_default = false;
    395       1.1   martin #endif
    396       1.1   martin 
    397       1.5   martin 		if (p->gp_type != NULL) {
    398       1.5   martin 
    399       1.5   martin 			if (p->gp_type->fsflags != 0) {
    400       1.5   martin 				const char *lm = get_last_mounted(fd,
    401       1.5   martin 				    p->gp_start, &p->fs_type,
    402       1.5   martin 				    &p->fs_sub_type, p->gp_type->fsflags);
    403       1.5   martin 				if (lm != NULL && *lm != 0) {
    404       1.5   martin 					char *path = strdup(lm);
    405       1.5   martin 					canonicalize_last_mounted(path);
    406       1.5   martin 					p->last_mounted = path;
    407       1.5   martin 				} else {
    408       1.5   martin 					p->fs_type = p->gp_type->
    409       1.5   martin 					    default_fs_type;
    410       1.5   martin #ifdef DEFAULT_UFS2
    411       1.5   martin 					fs_is_default = true;
    412       1.5   martin #endif
    413       1.5   martin 				}
    414       1.1   martin 			} else {
    415       1.1   martin 				p->fs_type = p->gp_type->default_fs_type;
    416       1.1   martin #ifdef DEFAULT_UFS2
    417       1.1   martin 				fs_is_default = true;
    418       1.1   martin #endif
    419       1.1   martin 			}
    420       1.1   martin #ifdef DEFAULT_UFS2
    421       1.5   martin 			if (fs_is_default && p->fs_type == FS_BSDFFS)
    422       1.5   martin 				p->fs_sub_type = 2;
    423       1.1   martin #endif
    424       1.1   martin 		}
    425       1.1   martin 
    426       1.1   martin 		parts->dp.free_space -= p->gp_size;
    427       1.1   martin 	}
    428   1.6.2.2  msaitoh 
    429   1.6.2.2  msaitoh 	/*
    430   1.6.2.2  msaitoh 	 * Check if we have any (matching/auto-configured) wedges already
    431   1.6.2.2  msaitoh 	 */
    432   1.6.2.2  msaitoh 	dkw = NULL;
    433   1.6.2.2  msaitoh 	dkwl.dkwl_buf = dkw;
    434   1.6.2.2  msaitoh 	dkwl.dkwl_bufsize = 0;
    435   1.6.2.2  msaitoh 	if (ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
    436   1.6.2.2  msaitoh 		/* do not even try to deal with any races at this point */
    437   1.6.2.2  msaitoh 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
    438   1.6.2.2  msaitoh 		dkw = malloc(bufsize);
    439   1.6.2.2  msaitoh 		dkwl.dkwl_buf = dkw;
    440   1.6.2.2  msaitoh 		dkwl.dkwl_bufsize = bufsize;
    441   1.6.2.2  msaitoh 		if (dkw != NULL && ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
    442   1.6.2.2  msaitoh 			for (dk = 0; dk < dkwl.dkwl_ncopied; dk++)
    443   1.6.2.2  msaitoh 				update_part_from_wedge_info(parts, &dkw[dk]);
    444   1.6.2.2  msaitoh 		}
    445   1.6.2.2  msaitoh 		free(dkw);
    446   1.6.2.2  msaitoh 	}
    447   1.6.2.2  msaitoh 
    448       1.1   martin 	close(fd);
    449       1.1   martin 
    450       1.1   martin 	return &parts->dp;
    451       1.1   martin }
    452       1.1   martin 
    453   1.6.2.8  msaitoh static size_t
    454   1.6.2.8  msaitoh gpt_cyl_size(const struct disk_partitions *arg)
    455   1.6.2.8  msaitoh {
    456   1.6.2.8  msaitoh 	return MEG / 512;
    457   1.6.2.8  msaitoh }
    458   1.6.2.8  msaitoh 
    459       1.1   martin static struct disk_partitions *
    460   1.6.2.8  msaitoh gpt_create_new(const char *disk, daddr_t start, daddr_t len,
    461   1.6.2.8  msaitoh     bool is_boot_drive, struct disk_partitions *parent)
    462       1.1   martin {
    463       1.1   martin 	struct gpt_disk_partitions *parts;
    464   1.6.2.8  msaitoh 	struct disk_geom geo;
    465       1.1   martin 
    466       1.1   martin 	if (start != 0) {
    467       1.1   martin 		assert(0);
    468       1.1   martin 		return NULL;
    469       1.1   martin 	}
    470       1.1   martin 
    471   1.6.2.8  msaitoh 	if (!get_disk_geom(disk, &geo))
    472   1.6.2.8  msaitoh 		return NULL;
    473   1.6.2.8  msaitoh 
    474       1.3   martin 	parts = calloc(1, sizeof(*parts));
    475       1.1   martin 	if (!parts)
    476       1.1   martin 		return NULL;
    477       1.1   martin 
    478       1.1   martin 	parts->dp.pscheme = &gpt_parts;
    479   1.6.2.6  msaitoh 	parts->dp.disk = strdup(disk);
    480       1.1   martin 
    481       1.1   martin 	gpt_md_init(is_boot_drive, &parts->max_num_parts, &parts->prologue,
    482       1.1   martin 	    &parts->epilogue);
    483       1.1   martin 
    484       1.1   martin 	parts->dp.disk_start = start;
    485       1.1   martin 	parts->dp.disk_size = len;
    486   1.6.2.8  msaitoh 	parts->dp.bytes_per_sector = geo.dg_secsize;
    487       1.1   martin 	parts->dp.free_space = len - start - parts->prologue - parts->epilogue;
    488       1.1   martin 	parts->has_gpt = false;
    489       1.1   martin 
    490       1.1   martin 	return &parts->dp;
    491       1.1   martin }
    492       1.1   martin 
    493       1.1   martin static bool
    494       1.1   martin gpt_get_part_info(const struct disk_partitions *arg, part_id id,
    495       1.1   martin     struct disk_part_info *info)
    496       1.1   martin {
    497       1.5   martin 	static const struct part_type_desc gpt_unknown_type =
    498       1.5   martin 		{ .generic_ptype = PT_undef,
    499       1.5   martin 		  .short_desc = "<unknown>" };
    500       1.1   martin 	const struct gpt_disk_partitions *parts =
    501       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
    502       1.1   martin 	const struct gpt_part_entry *p = parts->partitions;
    503       1.1   martin 	part_id no;
    504       1.1   martin 
    505       1.1   martin 	for (no = 0; p != NULL && no < id; no++)
    506       1.1   martin 		p = p->gp_next;
    507       1.1   martin 
    508       1.1   martin 	if (no != id || p == NULL)
    509       1.1   martin 		return false;
    510       1.1   martin 
    511       1.1   martin 	memset(info, 0, sizeof(*info));
    512       1.1   martin 	info->start = p->gp_start;
    513       1.1   martin 	info->size = p->gp_size;
    514       1.1   martin 	if (p->gp_type)
    515       1.1   martin 		info->nat_type = &p->gp_type->gent;
    516       1.5   martin 	else
    517       1.5   martin 		info->nat_type = &gpt_unknown_type;
    518       1.1   martin 	info->last_mounted = p->last_mounted;
    519       1.1   martin 	info->fs_type = p->fs_type;
    520       1.1   martin 	info->fs_sub_type = p->fs_sub_type;
    521   1.6.2.9   bouyer 	info->fs_opt1 = p->fs_opt1;
    522   1.6.2.9   bouyer 	info->fs_opt2 = p->fs_opt2;
    523   1.6.2.9   bouyer 	info->fs_opt3 = p->fs_opt3;
    524   1.6.2.9   bouyer 	if (p->gp_flags & GPEF_TARGET)
    525   1.6.2.9   bouyer 		info->flags |= PTI_INSTALL_TARGET;
    526       1.1   martin 
    527       1.1   martin 	return true;
    528       1.1   martin }
    529       1.1   martin 
    530       1.1   martin static bool
    531       1.1   martin gpt_get_part_attr_str(const struct disk_partitions *arg, part_id id,
    532       1.1   martin     char *str, size_t avail_space)
    533       1.1   martin {
    534       1.1   martin 	const struct gpt_disk_partitions *parts =
    535       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
    536       1.1   martin 	const struct gpt_part_entry *p = parts->partitions;
    537       1.1   martin 	part_id no;
    538       1.1   martin 	static const char *flags = NULL;
    539       1.1   martin 
    540       1.1   martin 	for (no = 0; p != NULL && no < id; no++)
    541       1.1   martin 		p = p->gp_next;
    542       1.1   martin 
    543       1.1   martin 	if (no != id || p == NULL)
    544       1.1   martin 		return false;
    545       1.1   martin 
    546       1.1   martin 	if (flags == NULL)
    547       1.1   martin 		flags = msg_string(MSG_gpt_flags);
    548       1.1   martin 
    549       1.1   martin 	if (avail_space < 2)
    550       1.1   martin 		return false;
    551       1.1   martin 
    552       1.1   martin 	if (p->gp_attr & GPT_ATTR_BOOT)
    553       1.1   martin 		*str++ = flags[0];
    554       1.1   martin 	*str = 0;
    555       1.1   martin 
    556       1.1   martin 	return true;
    557       1.1   martin }
    558       1.1   martin 
    559       1.1   martin /*
    560       1.1   martin  * Find insert position and check for duplicates.
    561       1.1   martin  * If all goes well, insert the new "entry" in the "list".
    562       1.1   martin  * If there are collisions, report "no free space".
    563       1.1   martin  * We keep all lists sorted by start sector number,
    564       1.1   martin  */
    565       1.1   martin static bool
    566       1.1   martin gpt_insert_part_into_list(struct gpt_disk_partitions *parts,
    567       1.1   martin     struct gpt_part_entry **list,
    568       1.1   martin     struct gpt_part_entry *entry, const char **err_msg)
    569       1.1   martin {
    570       1.1   martin 	struct gpt_part_entry *p, *last;
    571       1.1   martin 
    572       1.1   martin 	/* find the first entry past the new one (if any) */
    573       1.1   martin 	for (last = NULL, p = *list; p != NULL; last = p, p = p->gp_next) {
    574       1.1   martin 		if (p->gp_start > entry->gp_start)
    575       1.1   martin 			break;
    576       1.1   martin 	}
    577       1.1   martin 
    578       1.1   martin 	/* check if last partition overlaps with new one */
    579       1.1   martin 	if (last) {
    580       1.1   martin 		if (last->gp_start + last->gp_size > entry->gp_start) {
    581       1.1   martin 			if (err_msg)
    582       1.1   martin 				*err_msg = msg_string(MSG_No_free_space);
    583       1.1   martin 			return false;
    584       1.1   martin 		}
    585       1.1   martin 	}
    586       1.1   martin 
    587       1.1   martin 	if (p == NULL) {
    588       1.1   martin 		entry->gp_next = NULL;
    589       1.1   martin 		if (last != NULL) {
    590       1.1   martin 			last->gp_next = entry;
    591       1.1   martin 		}
    592       1.1   martin 	} else {
    593       1.1   martin 		/* check if new entry overlaps with next */
    594       1.1   martin 		if (entry->gp_start + entry->gp_size > p->gp_start) {
    595       1.1   martin 			if (err_msg)
    596       1.1   martin 				*err_msg = msg_string(MSG_No_free_space);
    597       1.1   martin 			return false;
    598       1.1   martin 		}
    599       1.1   martin 
    600       1.1   martin 		entry->gp_next = p;
    601       1.1   martin 		if (last != NULL)
    602       1.1   martin 			last->gp_next = entry;
    603       1.1   martin 		else
    604       1.1   martin 			*list = entry;
    605       1.1   martin 	}
    606       1.1   martin 	if (*list == NULL)
    607       1.1   martin 		*list = entry;
    608       1.1   martin 
    609       1.1   martin 	return true;
    610       1.1   martin }
    611       1.1   martin 
    612       1.1   martin static bool
    613       1.1   martin gpt_set_part_info(struct disk_partitions *arg, part_id id,
    614       1.1   martin     const struct disk_part_info *info, const char **err_msg)
    615       1.1   martin {
    616       1.1   martin 	struct gpt_disk_partitions *parts =
    617       1.1   martin 	    (struct gpt_disk_partitions*)arg;
    618       1.1   martin 	struct gpt_part_entry *p = parts->partitions, *n;
    619       1.1   martin 	part_id no;
    620       1.1   martin 	daddr_t lendiff;
    621   1.6.2.9   bouyer 	bool was_target;
    622       1.1   martin 
    623       1.1   martin 	for (no = 0; p != NULL && no < id; no++)
    624       1.1   martin 		p = p->gp_next;
    625       1.1   martin 
    626       1.1   martin 	if (no != id || p == NULL)
    627       1.1   martin 		return false;
    628       1.1   martin 
    629   1.6.2.9   bouyer 	/* update target mark - we can only have one */
    630   1.6.2.9   bouyer 	was_target = (p->gp_flags & GPEF_TARGET) != 0;
    631   1.6.2.9   bouyer 	if (info->flags & PTI_INSTALL_TARGET)
    632   1.6.2.9   bouyer 		p->gp_flags |= GPEF_TARGET;
    633   1.6.2.9   bouyer 	else
    634   1.6.2.9   bouyer 		p->gp_flags &= ~GPEF_TARGET;
    635   1.6.2.9   bouyer 	if (was_target)
    636   1.6.2.9   bouyer 		for (n = parts->partitions; n != NULL; n = n->gp_next)
    637   1.6.2.9   bouyer 			if (n != p)
    638   1.6.2.9   bouyer 				n->gp_flags &= ~GPEF_TARGET;
    639   1.6.2.9   bouyer 
    640       1.1   martin 	if ((p->gp_flags & GPEF_ON_DISK)) {
    641       1.1   martin 		if (info->start != p->gp_start) {
    642       1.1   martin 			/* partition moved, we need to delete and re-add */
    643       1.1   martin 			n = calloc(1, sizeof(*n));
    644       1.1   martin 			if (n == NULL) {
    645       1.1   martin 				if (err_msg)
    646       1.1   martin 					*err_msg = err_outofmem;
    647       1.1   martin 				return false;
    648       1.1   martin 			}
    649       1.1   martin 			*n = *p;
    650       1.1   martin 			p->gp_flags &= ~GPEF_ON_DISK;
    651       1.1   martin 			if (!gpt_insert_part_into_list(parts, &parts->obsolete,
    652       1.1   martin 			    n, err_msg))
    653       1.1   martin 				return false;
    654       1.1   martin 		} else if (info->size != p->gp_size) {
    655       1.1   martin 			p->gp_flags |= GPEF_RESIZED;
    656       1.1   martin 		}
    657       1.1   martin 	}
    658       1.1   martin 
    659       1.1   martin 	p->gp_flags |= GPEF_MODIFIED;
    660       1.1   martin 
    661       1.1   martin 	lendiff = info->size - p->gp_size;
    662       1.1   martin 	parts->dp.free_space -= lendiff;
    663       1.1   martin 	return gpt_info_to_part(p, info, err_msg);
    664       1.1   martin }
    665       1.1   martin 
    666       1.1   martin static size_t
    667       1.1   martin gpt_get_free_spaces_internal(const struct gpt_disk_partitions *parts,
    668       1.1   martin     struct disk_part_free_space *result, size_t max_num_result,
    669       1.1   martin     daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
    670       1.1   martin {
    671       1.1   martin 	size_t cnt = 0;
    672       1.1   martin 	daddr_t s, e, from, size, end_of_disk;
    673       1.1   martin 	struct gpt_part_entry *p;
    674       1.1   martin 
    675       1.1   martin 	if (align > 1)
    676       1.1   martin 		start = max(roundup(start, align), align);
    677       1.1   martin 	if (start < 0 || start < (daddr_t)parts->prologue)
    678       1.1   martin 		start = parts->prologue;
    679       1.1   martin 	if (parts->dp.disk_start != 0 && parts->dp.disk_start > start)
    680       1.1   martin 		start = parts->dp.disk_start;
    681       1.1   martin 	if (min_space_size < 1)
    682       1.1   martin 		min_space_size = 1;
    683       1.1   martin 	end_of_disk = parts->dp.disk_start + parts->dp.disk_size
    684       1.1   martin 	    - parts->epilogue;
    685       1.1   martin 	from = start;
    686       1.1   martin 	while (from < end_of_disk && cnt < max_num_result) {
    687       1.1   martin again:
    688       1.1   martin 		size = parts->dp.disk_start + parts->dp.disk_size - from;
    689       1.1   martin 		start = from;
    690       1.1   martin 		if (start + size > end_of_disk)
    691       1.1   martin 			size = end_of_disk - start;
    692       1.1   martin 		for (p = parts->partitions; p != NULL; p = p->gp_next) {
    693       1.1   martin 			s = p->gp_start;
    694       1.1   martin 			e = p->gp_size + s;
    695       1.1   martin 			if (s == ignore)
    696       1.1   martin 				continue;
    697       1.1   martin 			if (e < from)
    698       1.1   martin 				continue;
    699       1.1   martin 			if (s <= from && e > from) {
    700       1.1   martin 				if (e - 1 >= end_of_disk)
    701       1.1   martin 					return cnt;
    702       1.1   martin 				from = e + 1;
    703       1.1   martin 				if (align > 1) {
    704       1.1   martin 					from = max(roundup(from, align), align);
    705       1.1   martin 					if (from >= end_of_disk) {
    706       1.1   martin 						size = 0;
    707       1.1   martin 						break;
    708       1.1   martin 					}
    709       1.1   martin 				}
    710       1.1   martin 				goto again;
    711       1.1   martin 			}
    712       1.1   martin 			if (s > from && s - from < size) {
    713       1.1   martin 				size = s - from;
    714       1.1   martin 			}
    715       1.1   martin 		}
    716       1.1   martin 		if (size >= min_space_size) {
    717       1.1   martin 			result->start = start;
    718       1.1   martin 			result->size = size;
    719       1.1   martin 			result++;
    720       1.1   martin 			cnt++;
    721       1.1   martin 		}
    722       1.1   martin 		from += size + 1;
    723       1.1   martin 		if (align > 1)
    724       1.1   martin 			from = max(roundup(from, align), align);
    725       1.1   martin 	}
    726       1.1   martin 
    727       1.1   martin 	return cnt;
    728       1.1   martin }
    729       1.1   martin 
    730       1.1   martin static daddr_t
    731       1.1   martin gpt_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
    732       1.1   martin {
    733       1.1   martin 	const struct gpt_disk_partitions *parts =
    734       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
    735       1.1   martin 	struct disk_part_free_space space;
    736       1.1   martin 
    737       1.1   martin 	if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 0,
    738       1.1   martin 	    start, start) == 1)
    739       1.1   martin 		return space.size;
    740       1.1   martin 
    741       1.1   martin 	return 0;
    742       1.1   martin }
    743       1.1   martin 
    744       1.1   martin static size_t
    745       1.1   martin gpt_get_free_spaces(const struct disk_partitions *arg,
    746       1.1   martin     struct disk_part_free_space *result, size_t max_num_result,
    747       1.1   martin     daddr_t min_space_size, daddr_t align, daddr_t start,
    748       1.1   martin     daddr_t ignore)
    749       1.1   martin {
    750       1.1   martin 	const struct gpt_disk_partitions *parts =
    751       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
    752       1.1   martin 
    753       1.1   martin 	return gpt_get_free_spaces_internal(parts, result,
    754       1.1   martin 	    max_num_result, min_space_size, align, start, ignore);
    755       1.1   martin }
    756       1.1   martin 
    757       1.1   martin static void
    758       1.1   martin gpt_match_ptype(const char *name, struct gpt_ptype_desc *t)
    759       1.1   martin {
    760       1.1   martin 	size_t i;
    761       1.1   martin 
    762       1.1   martin 	for (i = 0; i < __arraycount(gpt_fs_types); i++) {
    763       1.1   martin 		if (strcmp(name, gpt_fs_types[i].name) == 0) {
    764       1.1   martin 			t->gent.generic_ptype = gpt_fs_types[i].ptype;
    765       1.1   martin 			t->fsflags = gpt_fs_types[i].fsflags;
    766       1.1   martin 			t->default_fs_type = gpt_fs_types[i].fstype;
    767   1.6.2.6  msaitoh 
    768   1.6.2.6  msaitoh 			/* recongnize special entries */
    769   1.6.2.6  msaitoh 			if (gpt_native_root == NULL && i == 0)
    770   1.6.2.6  msaitoh 				gpt_native_root = &t->gent;
    771   1.6.2.6  msaitoh 
    772       1.1   martin 			return;
    773       1.1   martin 		}
    774       1.1   martin 	}
    775       1.1   martin 
    776       1.1   martin 	t->gent.generic_ptype = PT_unknown;
    777       1.1   martin 	t->fsflags = 0;
    778       1.1   martin 	t->default_fs_type = FS_BSDFFS;
    779       1.1   martin }
    780       1.1   martin 
    781       1.1   martin static void
    782       1.1   martin gpt_internal_add_ptype(const char *uid, const char *name, const char *desc)
    783       1.1   martin {
    784   1.6.2.6  msaitoh 	if (gpt_ptype_cnt >= gpt_ptype_alloc) {
    785   1.6.2.6  msaitoh 		gpt_ptype_alloc = gpt_ptype_alloc ? 2*gpt_ptype_alloc
    786   1.6.2.6  msaitoh 		    : GPT_PTYPE_ALLOC;
    787   1.6.2.6  msaitoh 		struct gpt_ptype_desc *nptypes = realloc(gpt_ptype_descs,
    788   1.6.2.6  msaitoh 		    gpt_ptype_alloc*sizeof(*gpt_ptype_descs));
    789   1.6.2.6  msaitoh 		if (nptypes == 0)
    790   1.6.2.6  msaitoh 			errx(EXIT_FAILURE, "out of memory");
    791   1.6.2.6  msaitoh 		gpt_ptype_descs = nptypes;
    792   1.6.2.6  msaitoh 	}
    793   1.6.2.6  msaitoh 
    794       1.1   martin 	strlcpy(gpt_ptype_descs[gpt_ptype_cnt].tid, uid,
    795       1.1   martin 	    sizeof(gpt_ptype_descs[gpt_ptype_cnt].tid));
    796   1.6.2.6  msaitoh 	gpt_ptype_descs[gpt_ptype_cnt].gent.short_desc = strdup(name);
    797   1.6.2.6  msaitoh 	gpt_ptype_descs[gpt_ptype_cnt].gent.description = strdup(desc);
    798       1.1   martin 	gpt_match_ptype(name, &gpt_ptype_descs[gpt_ptype_cnt]);
    799       1.1   martin 	gpt_ptype_cnt++;
    800       1.1   martin }
    801       1.1   martin 
    802       1.1   martin static void
    803       1.1   martin gpt_init_ptypes(void)
    804       1.1   martin {
    805       1.1   martin 	if (gpt_ptype_cnt == 0)
    806       1.1   martin 		gpt_uuid_query(gpt_internal_add_ptype);
    807       1.1   martin }
    808       1.1   martin 
    809   1.6.2.6  msaitoh static void
    810   1.6.2.6  msaitoh gpt_cleanup(void)
    811   1.6.2.6  msaitoh {
    812   1.6.2.6  msaitoh 	/* free all of gpt_ptype_descs */
    813   1.6.2.6  msaitoh 	for (size_t i = 0; i < gpt_ptype_cnt; i++) {
    814   1.6.2.6  msaitoh 		free(__UNCONST(gpt_ptype_descs[i].gent.short_desc));
    815   1.6.2.6  msaitoh 		free(__UNCONST(gpt_ptype_descs[i].gent.description));
    816   1.6.2.6  msaitoh 	}
    817   1.6.2.6  msaitoh 	free(gpt_ptype_descs);
    818   1.6.2.6  msaitoh 	gpt_ptype_descs = NULL;
    819   1.6.2.6  msaitoh 	gpt_ptype_cnt = gpt_ptype_alloc = 0;
    820   1.6.2.6  msaitoh }
    821   1.6.2.6  msaitoh 
    822       1.1   martin static size_t
    823       1.1   martin gpt_type_count(void)
    824       1.1   martin {
    825       1.1   martin 	if (gpt_ptype_cnt == 0)
    826       1.1   martin 		gpt_init_ptypes();
    827       1.1   martin 
    828       1.1   martin 	return gpt_ptype_cnt;
    829       1.1   martin }
    830       1.1   martin 
    831       1.1   martin static const struct part_type_desc *
    832       1.1   martin gpt_get_ptype(size_t ndx)
    833       1.1   martin {
    834       1.1   martin 	if (gpt_ptype_cnt == 0)
    835       1.1   martin 		gpt_init_ptypes();
    836       1.1   martin 
    837       1.1   martin 	if (ndx >= gpt_ptype_cnt)
    838       1.1   martin 		return NULL;
    839       1.1   martin 
    840       1.1   martin 	return &gpt_ptype_descs[ndx].gent;
    841       1.1   martin }
    842       1.1   martin 
    843       1.1   martin static const struct part_type_desc *
    844       1.1   martin gpt_get_generic_type(enum part_type gent)
    845       1.1   martin {
    846       1.1   martin 	if (gpt_ptype_cnt == 0)
    847       1.1   martin 		gpt_init_ptypes();
    848       1.1   martin 
    849   1.6.2.6  msaitoh 	if (gent == PT_root)
    850   1.6.2.6  msaitoh 		return gpt_native_root;
    851   1.6.2.6  msaitoh 	if (gent == PT_unknown)
    852   1.6.2.6  msaitoh 		return NULL;
    853   1.6.2.6  msaitoh 
    854       1.1   martin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
    855       1.1   martin 		if (gpt_ptype_descs[i].gent.generic_ptype == gent)
    856       1.1   martin 			return &gpt_ptype_descs[i].gent;
    857       1.1   martin 
    858       1.1   martin 	return NULL;
    859       1.1   martin }
    860       1.1   martin 
    861       1.1   martin static const struct gpt_ptype_desc *
    862       1.1   martin gpt_find_native_type(const struct part_type_desc *gent)
    863       1.1   martin {
    864       1.1   martin 	if (gpt_ptype_cnt == 0)
    865       1.1   martin 		gpt_init_ptypes();
    866       1.1   martin 
    867       1.1   martin 	if (gent == NULL)
    868       1.1   martin 		return NULL;
    869       1.1   martin 
    870       1.1   martin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
    871       1.1   martin 		if (gent == &gpt_ptype_descs[i].gent)
    872       1.1   martin 			return &gpt_ptype_descs[i];
    873       1.1   martin 
    874       1.1   martin 	gent = gpt_get_generic_type(gent->generic_ptype);
    875       1.1   martin 	if (gent == NULL)
    876       1.1   martin 		return NULL;
    877       1.1   martin 
    878       1.1   martin 	/* this can not recurse deeper than once, we would not have found a
    879       1.1   martin 	 * generic type a few lines above if it would. */
    880       1.1   martin 	return gpt_find_native_type(gent);
    881       1.1   martin }
    882       1.1   martin 
    883       1.1   martin static const struct gpt_ptype_desc *
    884       1.1   martin gpt_find_guid_type(const char *uid)
    885       1.1   martin {
    886       1.1   martin 	if (gpt_ptype_cnt == 0)
    887       1.1   martin 		gpt_init_ptypes();
    888       1.1   martin 
    889       1.1   martin 	if (uid == NULL || uid[0] == 0)
    890       1.1   martin 		return NULL;
    891       1.1   martin 
    892       1.1   martin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
    893       1.1   martin 		if (strcmp(gpt_ptype_descs[i].tid, uid) == 0)
    894       1.1   martin 			return &gpt_ptype_descs[i];
    895       1.1   martin 
    896       1.1   martin 	return NULL;
    897       1.1   martin }
    898       1.1   martin 
    899       1.1   martin static const struct part_type_desc *
    900       1.1   martin gpt_find_type(const char *desc)
    901       1.1   martin {
    902       1.1   martin 	if (gpt_ptype_cnt == 0)
    903       1.1   martin 		gpt_init_ptypes();
    904       1.1   martin 
    905       1.1   martin 	if (desc == NULL || desc[0] == 0)
    906       1.1   martin 		return NULL;
    907       1.1   martin 
    908       1.1   martin 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
    909       1.1   martin 		if (strcmp(gpt_ptype_descs[i].gent.short_desc, desc) == 0)
    910       1.1   martin 			return &gpt_ptype_descs[i].gent;
    911       1.1   martin 
    912       1.1   martin 	return NULL;
    913       1.1   martin }
    914       1.1   martin 
    915       1.1   martin static const struct part_type_desc *
    916   1.6.2.7  msaitoh gpt_get_fs_part_type(enum part_type pt, unsigned fstype, unsigned fs_sub_type)
    917       1.1   martin {
    918       1.1   martin 	size_t i;
    919       1.1   martin 
    920   1.6.2.8  msaitoh 	/* Try with complete match (including part_type) first */
    921   1.6.2.7  msaitoh 	for (i = 0; i < __arraycount(gpt_fs_types); i++)
    922   1.6.2.7  msaitoh 		if (fstype == gpt_fs_types[i].fstype &&
    923   1.6.2.7  msaitoh 		    pt == gpt_fs_types[i].ptype)
    924   1.6.2.7  msaitoh 			return gpt_find_type(gpt_fs_types[i].name);
    925   1.6.2.7  msaitoh 
    926   1.6.2.7  msaitoh 	/* If that did not work, ignore part_type */
    927       1.1   martin 	for (i = 0; i < __arraycount(gpt_fs_types); i++)
    928       1.1   martin 		if (fstype == gpt_fs_types[i].fstype)
    929       1.1   martin 			return gpt_find_type(gpt_fs_types[i].name);
    930       1.1   martin 
    931   1.6.2.6  msaitoh 	return NULL;
    932   1.6.2.6  msaitoh }
    933   1.6.2.6  msaitoh 
    934   1.6.2.8  msaitoh static bool
    935   1.6.2.8  msaitoh gpt_get_default_fstype(const struct part_type_desc *nat_type,
    936   1.6.2.8  msaitoh     unsigned *fstype, unsigned *fs_sub_type)
    937   1.6.2.8  msaitoh {
    938   1.6.2.8  msaitoh 	const struct gpt_ptype_desc *gtype;
    939   1.6.2.8  msaitoh 
    940   1.6.2.8  msaitoh 	gtype = gpt_find_native_type(nat_type);
    941   1.6.2.8  msaitoh 	if (gtype == NULL)
    942   1.6.2.8  msaitoh 		return false;
    943   1.6.2.8  msaitoh 
    944   1.6.2.8  msaitoh 	*fstype = gtype->default_fs_type;
    945   1.6.2.8  msaitoh #ifdef DEFAULT_UFS2
    946   1.6.2.8  msaitoh 	if (gtype->default_fs_type == FS_BSDFFS)
    947   1.6.2.8  msaitoh 		*fs_sub_type = 2;
    948   1.6.2.8  msaitoh 	else
    949   1.6.2.8  msaitoh #endif
    950   1.6.2.8  msaitoh 		*fs_sub_type = 0;
    951   1.6.2.8  msaitoh 	return true;
    952   1.6.2.8  msaitoh }
    953   1.6.2.8  msaitoh 
    954   1.6.2.6  msaitoh static const struct part_type_desc *
    955   1.6.2.6  msaitoh gpt_get_uuid_part_type(const uuid_t *id)
    956   1.6.2.6  msaitoh {
    957   1.6.2.6  msaitoh 	char str[GUID_STR_LEN], desc[GUID_STR_LEN + MENUSTRSIZE];
    958   1.6.2.6  msaitoh 	const struct gpt_ptype_desc *t;
    959   1.6.2.6  msaitoh 	char *guid = NULL;
    960   1.6.2.6  msaitoh 	uint32_t err;
    961   1.6.2.6  msaitoh 
    962   1.6.2.6  msaitoh 	uuid_to_string(id, &guid, &err);
    963   1.6.2.6  msaitoh 	strlcpy(str, err == uuid_s_ok ? guid : "-", sizeof str);
    964   1.6.2.6  msaitoh 	free(guid);
    965   1.6.2.6  msaitoh 
    966   1.6.2.6  msaitoh 	t = gpt_find_guid_type(str);
    967   1.6.2.6  msaitoh 	if (t == NULL) {
    968   1.6.2.6  msaitoh 		snprintf(desc, sizeof desc, "%s (%s)",
    969   1.6.2.6  msaitoh 		    msg_string(MSG_custom_type), str);
    970   1.6.2.6  msaitoh 		gpt_internal_add_ptype(str, str, desc);
    971   1.6.2.6  msaitoh 		t = gpt_find_guid_type(str);
    972   1.6.2.6  msaitoh 		assert(t != NULL);
    973   1.6.2.6  msaitoh 	}
    974   1.6.2.6  msaitoh 	return &t->gent;
    975   1.6.2.6  msaitoh }
    976   1.6.2.6  msaitoh 
    977   1.6.2.6  msaitoh static const struct part_type_desc *
    978   1.6.2.6  msaitoh gpt_create_custom_part_type(const char *custom, const char **err_msg)
    979   1.6.2.6  msaitoh {
    980   1.6.2.6  msaitoh 	uuid_t id;
    981   1.6.2.6  msaitoh 	uint32_t err;
    982   1.6.2.6  msaitoh 
    983   1.6.2.6  msaitoh 	uuid_from_string(custom, &id, &err);
    984   1.6.2.6  msaitoh 	if (err_msg != NULL &&
    985   1.6.2.6  msaitoh 	   (err == uuid_s_invalid_string_uuid || err == uuid_s_bad_version)) {
    986   1.6.2.6  msaitoh 		*err_msg = MSG_invalid_guid;
    987   1.6.2.6  msaitoh 		return NULL;
    988   1.6.2.6  msaitoh 	}
    989   1.6.2.6  msaitoh 	if (err != uuid_s_ok)
    990   1.6.2.6  msaitoh 		return NULL;
    991   1.6.2.6  msaitoh 
    992   1.6.2.6  msaitoh 	return gpt_get_uuid_part_type(&id);
    993   1.6.2.6  msaitoh }
    994   1.6.2.6  msaitoh 
    995   1.6.2.6  msaitoh static const struct part_type_desc *
    996   1.6.2.6  msaitoh gpt_create_unknown_part_type(void)
    997   1.6.2.6  msaitoh {
    998   1.6.2.6  msaitoh 	uuid_t id;
    999   1.6.2.6  msaitoh 	uint32_t err;
   1000   1.6.2.6  msaitoh 
   1001   1.6.2.6  msaitoh 	uuid_create(&id, &err);
   1002   1.6.2.6  msaitoh 	if (err != uuid_s_ok)
   1003   1.6.2.6  msaitoh 		return NULL;
   1004   1.6.2.6  msaitoh 
   1005   1.6.2.6  msaitoh 	return gpt_get_uuid_part_type(&id);
   1006       1.1   martin }
   1007       1.1   martin 
   1008       1.1   martin static daddr_t
   1009       1.1   martin gpt_get_part_alignment(const struct disk_partitions *parts)
   1010       1.1   martin {
   1011       1.1   martin 
   1012       1.1   martin 	assert(parts->disk_size > 0);
   1013       1.1   martin 	if (parts->disk_size < 0)
   1014       1.1   martin 		return 1;
   1015       1.1   martin 
   1016       1.1   martin 	/* Use 1MB offset/alignemnt for large (>128GB) disks */
   1017       1.1   martin 	if (parts->disk_size > HUGE_DISK_SIZE)
   1018       1.1   martin 		return 2048;
   1019       1.1   martin 	else if (parts->disk_size > TINY_DISK_SIZE)
   1020       1.1   martin 		return 64;
   1021       1.1   martin 	else
   1022       1.1   martin 		return 4;
   1023       1.1   martin }
   1024       1.1   martin 
   1025       1.1   martin static bool
   1026       1.1   martin gpt_can_add_partition(const struct disk_partitions *arg)
   1027       1.1   martin {
   1028       1.1   martin 	const struct gpt_disk_partitions *parts =
   1029       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
   1030       1.1   martin 	struct disk_part_free_space space;
   1031       1.1   martin 	daddr_t align;
   1032       1.1   martin 
   1033       1.1   martin 	if (parts->dp.num_part >= parts->max_num_parts)
   1034       1.1   martin 		return false;
   1035       1.1   martin 
   1036       1.1   martin 	align = gpt_get_part_alignment(arg);
   1037       1.1   martin 	if (parts->dp.free_space <= align)
   1038       1.1   martin 		return false;
   1039       1.1   martin 
   1040       1.1   martin 	if (gpt_get_free_spaces_internal(parts, &space, 1, align, align,
   1041       1.1   martin 	    0, -1) < 1)
   1042       1.1   martin 		return false;
   1043       1.1   martin 
   1044       1.1   martin 	return true;
   1045       1.1   martin }
   1046       1.1   martin 
   1047       1.1   martin static bool
   1048       1.1   martin gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
   1049       1.1   martin     const char **err_msg)
   1050       1.1   martin {
   1051       1.1   martin 	p->gp_type = gpt_find_native_type(info->nat_type);
   1052       1.1   martin 	p->gp_start = info->start;
   1053       1.1   martin 	p->gp_size = info->size;
   1054       1.1   martin 	if (info->last_mounted != NULL && info->last_mounted !=
   1055       1.1   martin 	    p->last_mounted) {
   1056       1.1   martin 		free(__UNCONST(p->last_mounted));
   1057       1.1   martin 		p->last_mounted = strdup(info->last_mounted);
   1058       1.1   martin 	}
   1059       1.1   martin 	p->fs_type = info->fs_type;
   1060       1.1   martin 	p->fs_sub_type = info->fs_sub_type;
   1061   1.6.2.9   bouyer 	p->fs_opt1 = info->fs_opt1;
   1062   1.6.2.9   bouyer 	p->fs_opt2 = info->fs_opt2;
   1063   1.6.2.9   bouyer 	p->fs_opt3 = info->fs_opt3;
   1064       1.1   martin 
   1065       1.1   martin 	return true;
   1066       1.1   martin }
   1067       1.1   martin 
   1068       1.1   martin static part_id
   1069       1.1   martin gpt_add_part(struct disk_partitions *arg,
   1070       1.1   martin     const struct disk_part_info *info, const char **err_msg)
   1071       1.1   martin {
   1072       1.1   martin 	struct gpt_disk_partitions *parts =
   1073       1.1   martin 	    (struct gpt_disk_partitions*)arg;
   1074       1.1   martin 	struct disk_part_free_space space;
   1075       1.1   martin 	struct disk_part_info data = *info;
   1076       1.1   martin 	struct gpt_part_entry *p;
   1077       1.1   martin 	bool ok;
   1078       1.1   martin 
   1079       1.1   martin 	if (err_msg != NULL)
   1080       1.1   martin 		*err_msg = NULL;
   1081       1.1   martin 
   1082       1.1   martin 	if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 1,
   1083       1.1   martin 	    info->start, -1) < 1) {
   1084       1.1   martin 		if (err_msg)
   1085       1.1   martin 			*err_msg = msg_string(MSG_No_free_space);
   1086       1.1   martin 		return NO_PART;
   1087       1.1   martin 	}
   1088       1.1   martin 	if (parts->dp.num_part >= parts->max_num_parts) {
   1089       1.1   martin 		if (err_msg)
   1090       1.1   martin 			*err_msg = msg_string(MSG_err_too_many_partitions);
   1091       1.1   martin 		return NO_PART;
   1092       1.1   martin 	}
   1093       1.1   martin 
   1094       1.1   martin 	if (data.size > space.size)
   1095       1.1   martin 		data.size = space.size;
   1096       1.1   martin 
   1097       1.1   martin 	p = calloc(1, sizeof(*p));
   1098       1.1   martin 	if (p == NULL) {
   1099       1.1   martin 		if (err_msg != NULL)
   1100       1.1   martin 			*err_msg = INTERNAL_ERROR;
   1101       1.1   martin 		return NO_PART;
   1102       1.1   martin 	}
   1103       1.1   martin 	if (!gpt_info_to_part(p, &data, err_msg)) {
   1104       1.1   martin 		free(p);
   1105       1.1   martin 		return NO_PART;
   1106       1.1   martin 	}
   1107       1.1   martin 	p->gp_flags |= GPEF_MODIFIED;
   1108       1.1   martin 	ok = gpt_insert_part_into_list(parts, &parts->partitions, p, err_msg);
   1109       1.1   martin 	if (ok) {
   1110       1.1   martin 		parts->dp.num_part++;
   1111       1.1   martin 		parts->dp.free_space -= p->gp_size;
   1112       1.1   martin 		return parts->dp.num_part-1;
   1113       1.1   martin 	} else {
   1114       1.1   martin 		free(p);
   1115       1.1   martin 		return NO_PART;
   1116       1.1   martin 	}
   1117       1.1   martin }
   1118       1.1   martin 
   1119       1.1   martin static bool
   1120       1.1   martin gpt_delete_partition(struct disk_partitions *arg, part_id id,
   1121       1.1   martin     const char **err_msg)
   1122       1.1   martin {
   1123       1.1   martin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
   1124       1.1   martin 	struct gpt_part_entry *p, *last = NULL;
   1125       1.1   martin 	part_id i;
   1126       1.1   martin 	bool res;
   1127       1.1   martin 
   1128       1.1   martin 	if (parts->dp.num_part == 0)
   1129       1.1   martin 		return false;
   1130       1.1   martin 
   1131       1.1   martin 	for (i = 0, p = parts->partitions;
   1132       1.1   martin 	    i != id && i < parts->dp.num_part && p != NULL;
   1133       1.1   martin 	    i++, p = p->gp_next)
   1134       1.1   martin 		last = p;
   1135       1.1   martin 
   1136       1.1   martin 	if (p == NULL) {
   1137       1.1   martin 		if (err_msg)
   1138       1.1   martin 			*err_msg = INTERNAL_ERROR;
   1139       1.1   martin 		return false;
   1140       1.1   martin 	}
   1141       1.1   martin 
   1142       1.1   martin 	if (last == NULL)
   1143       1.1   martin 		parts->partitions = p->gp_next;
   1144       1.1   martin 	else
   1145       1.1   martin 		last->gp_next = p->gp_next;
   1146       1.1   martin 
   1147       1.1   martin 	res = true;
   1148       1.1   martin 	if (p->gp_flags & GPEF_ON_DISK) {
   1149       1.1   martin 		if (!gpt_insert_part_into_list(parts, &parts->obsolete,
   1150       1.1   martin 		    p, err_msg))
   1151       1.1   martin 			res = false;
   1152       1.1   martin 	} else {
   1153       1.1   martin 		free(p);
   1154       1.1   martin 	}
   1155       1.1   martin 
   1156       1.1   martin 	if (res) {
   1157       1.1   martin 		parts->dp.num_part--;
   1158       1.1   martin 		parts->dp.free_space += p->gp_size;
   1159       1.1   martin 	}
   1160       1.1   martin 
   1161       1.1   martin 	return res;
   1162       1.1   martin }
   1163       1.1   martin 
   1164       1.1   martin static bool
   1165       1.1   martin gpt_delete_all_partitions(struct disk_partitions *arg)
   1166       1.1   martin {
   1167       1.1   martin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
   1168       1.1   martin 
   1169       1.1   martin 	while (parts->dp.num_part > 0) {
   1170       1.1   martin 		if (!gpt_delete_partition(&parts->dp, 0, NULL))
   1171       1.1   martin 			return false;
   1172       1.1   martin 	}
   1173       1.1   martin 
   1174       1.1   martin 	return true;
   1175       1.1   martin }
   1176       1.1   martin 
   1177       1.1   martin static bool
   1178       1.1   martin gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
   1179       1.1   martin {
   1180       1.1   martin 	char *textbuf, *t, *tt;
   1181       1.1   martin 	static const char expected_hdr[] = "Details for index ";
   1182       1.1   martin 
   1183       1.1   martin 	/* run gpt show for this partition */
   1184       1.1   martin 	if (collect(T_OUTPUT, &textbuf,
   1185       1.1   martin 	    "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
   1186       1.1   martin 		return false;
   1187       1.1   martin 
   1188       1.1   martin 	/*
   1189       1.1   martin 	 * gpt show should respond with single partition details, but will
   1190       1.1   martin 	 * fall back to "show -a" output if something is wrong
   1191       1.1   martin 	 */
   1192       1.1   martin 	t = strtok(textbuf, "\n"); /* first line is special */
   1193       1.1   martin 	if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
   1194       1.1   martin 		free(textbuf);
   1195       1.1   martin 		return false;
   1196       1.1   martin 	}
   1197       1.1   martin 
   1198       1.1   martin 	/* parse output into "old" */
   1199       1.1   martin 	while ((t = strtok(NULL, "\n")) != NULL) {
   1200       1.1   martin 		tt = strsep(&t, " \t");
   1201       1.1   martin 		if (strlen(tt) == 0)
   1202       1.1   martin 			continue;
   1203       1.1   martin 		gpt_add_info(p, tt, t, true);
   1204       1.1   martin 	}
   1205       1.1   martin 	free(textbuf);
   1206       1.1   martin 
   1207       1.1   martin 	return true;
   1208       1.1   martin }
   1209       1.1   martin 
   1210       1.1   martin static bool
   1211       1.1   martin gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
   1212       1.1   martin {
   1213       1.1   martin 	size_t i;
   1214       1.1   martin 	char attr_str[STRSIZE];
   1215       1.1   martin 
   1216       1.1   martin 	if (todo == 0)
   1217       1.1   martin 		return true;
   1218       1.1   martin 
   1219       1.1   martin 	strcpy(attr_str, "-a ");
   1220       1.1   martin 	for (i = 0; todo != 0; i++) {
   1221       1.1   martin 		if (!(gpt_avail_attrs[i].flag & todo))
   1222       1.1   martin 			continue;
   1223       1.1   martin 		todo &= ~gpt_avail_attrs[i].flag;
   1224       1.1   martin 		if (attr_str[0])
   1225       1.1   martin 			strlcat(attr_str, ",",
   1226       1.1   martin 			    sizeof(attr_str));
   1227       1.1   martin 		strlcat(attr_str,
   1228       1.1   martin 		    gpt_avail_attrs[i].name,
   1229       1.1   martin 		    sizeof(attr_str));
   1230       1.1   martin 	}
   1231       1.1   martin 	if (run_program(RUN_SILENT,
   1232       1.1   martin 	    "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
   1233       1.1   martin 		return false;
   1234       1.1   martin 	return true;
   1235       1.1   martin }
   1236       1.1   martin 
   1237       1.1   martin /*
   1238       1.1   martin  * Modify an existing on-disk partition.
   1239       1.1   martin  * Start and size can not be changed here, caller needs to deal
   1240       1.1   martin  * with that kind of changes upfront.
   1241       1.1   martin  */
   1242       1.1   martin static bool
   1243       1.1   martin gpt_modify_part(const char *disk, struct gpt_part_entry *p)
   1244       1.1   martin {
   1245       1.1   martin 	struct gpt_part_entry old;
   1246       1.1   martin 	uint todo_set, todo_unset;
   1247       1.1   martin 
   1248       1.1   martin 	/*
   1249       1.1   martin 	 * Query current on-disk state
   1250       1.1   martin 	 */
   1251       1.1   martin 	memset(&old, 0, sizeof old);
   1252       1.1   martin 	if (!gpt_read_part(disk, p->gp_start, &old))
   1253       1.1   martin 		return false;
   1254       1.1   martin 
   1255       1.1   martin 	/* Reject unsupported changes */
   1256       1.1   martin 	if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
   1257       1.1   martin 		return false;
   1258       1.1   martin 
   1259       1.1   martin 	/*
   1260       1.1   martin 	 * GUID should never change, but the internal copy
   1261       1.1   martin 	 * may not yet know it.
   1262       1.1   martin 	 */
   1263       1.1   martin 	strcpy(p->gp_id, old.gp_id);
   1264       1.1   martin 
   1265       1.1   martin 	/* Check type */
   1266       1.1   martin 	if (p->gp_type != old.gp_type) {
   1267       1.1   martin 		if (run_program(RUN_SILENT,
   1268   1.6.2.9   bouyer 		    "gpt type -b %" PRIu64 " -T %s %s",
   1269       1.1   martin 		    p->gp_start, p->gp_type->tid, disk) != 0)
   1270       1.1   martin 			return false;
   1271       1.1   martin 	}
   1272       1.1   martin 
   1273       1.1   martin 	/* Check label */
   1274       1.1   martin 	if (strcmp(p->gp_label, old.gp_label) != 0) {
   1275       1.1   martin 		if (run_program(RUN_SILENT,
   1276   1.6.2.5  msaitoh 		    "gpt label -b %" PRIu64 " -l \'%s\' %s",
   1277       1.1   martin 		    p->gp_start, p->gp_label, disk) != 0)
   1278       1.1   martin 			return false;
   1279       1.1   martin 	}
   1280       1.1   martin 
   1281       1.1   martin 	/* Check attributes */
   1282       1.1   martin 	if (p->gp_attr != old.gp_attr) {
   1283       1.1   martin 		if (p->gp_attr == 0) {
   1284       1.1   martin 			if (run_program(RUN_SILENT,
   1285       1.1   martin 			    "gpt set -N -b %" PRIu64 " %s",
   1286       1.1   martin 			    p->gp_start, disk) != 0)
   1287       1.1   martin 				return false;
   1288       1.1   martin 		} else {
   1289       1.1   martin 			todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
   1290       1.1   martin 			todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
   1291       1.1   martin 			if (!gpt_apply_attr(disk, "unset", p->gp_start,
   1292       1.1   martin 			    todo_unset))
   1293       1.1   martin 				return false;
   1294       1.1   martin 			if (!gpt_apply_attr(disk, "set", p->gp_start,
   1295       1.1   martin 			    todo_set))
   1296       1.1   martin 				return false;
   1297       1.1   martin 		}
   1298       1.1   martin 	}
   1299       1.1   martin 
   1300       1.1   martin 	return true;
   1301       1.1   martin }
   1302       1.1   martin 
   1303       1.1   martin /*
   1304       1.1   martin  * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
   1305       1.1   martin  *  map FS_* to wedge strings
   1306       1.1   martin  */
   1307       1.1   martin static const char *
   1308       1.1   martin bsdlabel_fstype_to_str(uint8_t fstype)
   1309       1.1   martin {
   1310       1.1   martin 	const char *str;
   1311       1.1   martin 
   1312       1.1   martin 	/*
   1313       1.1   martin 	 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
   1314       1.1   martin 	 * a suitable case branch will convert the type number to a string.
   1315       1.1   martin 	 */
   1316       1.1   martin 	switch (fstype) {
   1317       1.1   martin #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
   1318       1.1   martin 	case __CONCAT(FS_,tag):	str = __CONCAT(DKW_PTYPE_,tag);			break;
   1319       1.1   martin 	FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
   1320       1.1   martin #undef FSTYPE_TO_STR_CASE
   1321       1.1   martin 	default:		str = NULL;			break;
   1322       1.1   martin 	}
   1323       1.1   martin 
   1324       1.1   martin 	return (str);
   1325       1.1   martin }
   1326       1.1   martin 
   1327       1.1   martin static bool
   1328       1.1   martin gpt_add_wedge(const char *disk, struct gpt_part_entry *p)
   1329       1.1   martin {
   1330       1.1   martin 	struct dkwedge_info dkw;
   1331       1.1   martin 	const char *tname;
   1332       1.1   martin 	char diskpath[MAXPATHLEN];
   1333       1.1   martin 	int fd;
   1334       1.1   martin 
   1335       1.1   martin 	memset(&dkw, 0, sizeof(dkw));
   1336       1.1   martin 	tname = bsdlabel_fstype_to_str(p->fs_type);
   1337       1.1   martin 	if (tname)
   1338       1.1   martin 		strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
   1339       1.1   martin 
   1340       1.1   martin 	strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
   1341       1.1   martin 	dkw.dkw_offset = p->gp_start;
   1342       1.1   martin 	dkw.dkw_size = p->gp_size;
   1343   1.6.2.8  msaitoh 	if (dkw.dkw_wname[0] == 0) {
   1344   1.6.2.8  msaitoh 		if (p->gp_label[0] != 0)
   1345   1.6.2.8  msaitoh 				strlcpy((char*)&dkw.dkw_wname,
   1346   1.6.2.8  msaitoh 				    p->gp_label, sizeof(dkw.dkw_wname));
   1347   1.6.2.8  msaitoh 	}
   1348   1.6.2.8  msaitoh 	if (dkw.dkw_wname[0] == 0) {
   1349   1.6.2.8  msaitoh 		snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
   1350   1.6.2.8  msaitoh 		    "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
   1351   1.6.2.8  msaitoh 	}
   1352       1.1   martin 
   1353       1.1   martin 	fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
   1354       1.1   martin 	if (fd < 0)
   1355       1.1   martin 		return false;
   1356       1.1   martin 	if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
   1357       1.1   martin 		close(fd);
   1358       1.1   martin 		return false;
   1359       1.1   martin 	}
   1360       1.1   martin 	close(fd);
   1361       1.1   martin 
   1362       1.1   martin 	strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
   1363       1.1   martin 	p->gp_flags |= GPEF_WEDGE;
   1364       1.1   martin 	return true;
   1365       1.1   martin }
   1366       1.1   martin 
   1367   1.6.2.5  msaitoh static void
   1368   1.6.2.5  msaitoh escape_spaces(char *dest, const char *src)
   1369   1.6.2.5  msaitoh {
   1370   1.6.2.5  msaitoh 	unsigned char c;
   1371   1.6.2.5  msaitoh 
   1372   1.6.2.5  msaitoh 	while (*src) {
   1373   1.6.2.5  msaitoh 		c = *src++;
   1374   1.6.2.5  msaitoh 		if (isspace(c) || c == '\\')
   1375   1.6.2.5  msaitoh 			*dest++ = '\\';
   1376   1.6.2.5  msaitoh 		*dest++ = c;
   1377   1.6.2.5  msaitoh 	}
   1378   1.6.2.5  msaitoh 	*dest = 0;
   1379   1.6.2.5  msaitoh }
   1380   1.6.2.5  msaitoh 
   1381       1.1   martin static bool
   1382       1.1   martin gpt_get_part_device(const struct disk_partitions *arg,
   1383       1.1   martin     part_id id, char *devname, size_t max_devname_len, int *part,
   1384   1.6.2.8  msaitoh     enum dev_name_usage usage, bool with_path, bool life)
   1385       1.1   martin {
   1386       1.1   martin 	const struct gpt_disk_partitions *parts =
   1387       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
   1388       1.1   martin 	struct  gpt_part_entry *p = parts->partitions;
   1389   1.6.2.5  msaitoh 	char tmpname[GPT_LABEL_LEN*2];
   1390       1.1   martin 	part_id no;
   1391       1.1   martin 
   1392       1.1   martin 
   1393       1.1   martin 	for (no = 0; p != NULL && no < id; no++)
   1394       1.1   martin 		p = p->gp_next;
   1395       1.1   martin 
   1396       1.1   martin 	if (no != id || p == NULL)
   1397       1.1   martin 		return false;
   1398       1.1   martin 
   1399       1.1   martin 	if (part)
   1400       1.1   martin 		*part = -1;
   1401       1.1   martin 
   1402   1.6.2.8  msaitoh 	if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
   1403   1.6.2.8  msaitoh 		usage = plain_name;
   1404   1.6.2.8  msaitoh 	if (usage == plain_name || usage == raw_dev_name)
   1405   1.6.2.8  msaitoh 		life = true;
   1406   1.6.2.8  msaitoh 	if (!(p->gp_flags & GPEF_WEDGE) && life)
   1407       1.1   martin 		gpt_add_wedge(arg->disk, p);
   1408       1.1   martin 
   1409       1.1   martin 	switch (usage) {
   1410       1.1   martin 	case logical_name:
   1411   1.6.2.5  msaitoh 		if (p->gp_label[0] != 0) {
   1412   1.6.2.5  msaitoh 			escape_spaces(tmpname, p->gp_label);
   1413       1.1   martin 			snprintf(devname, max_devname_len,
   1414   1.6.2.5  msaitoh 			    "NAME=%s", tmpname);
   1415   1.6.2.5  msaitoh 		} else {
   1416       1.1   martin 			snprintf(devname, max_devname_len,
   1417       1.1   martin 			    "NAME=%s", p->gp_id);
   1418   1.6.2.5  msaitoh 		}
   1419       1.1   martin 		break;
   1420       1.1   martin 	case plain_name:
   1421       1.1   martin 		assert(p->gp_flags & GPEF_WEDGE);
   1422       1.1   martin 		if (with_path)
   1423       1.1   martin 			snprintf(devname, max_devname_len, _PATH_DEV "%s",
   1424       1.1   martin 			    p->gp_dev_name);
   1425       1.1   martin 		else
   1426       1.1   martin 			strlcpy(devname, p->gp_dev_name, max_devname_len);
   1427       1.1   martin 		break;
   1428       1.1   martin 	case raw_dev_name:
   1429       1.1   martin 		assert(p->gp_flags & GPEF_WEDGE);
   1430       1.1   martin 		if (with_path)
   1431       1.1   martin 			snprintf(devname, max_devname_len, _PATH_DEV "r%s",
   1432       1.1   martin 			    p->gp_dev_name);
   1433       1.1   martin 		else
   1434       1.1   martin 			snprintf(devname, max_devname_len, "r%s",
   1435       1.1   martin 			    p->gp_dev_name);
   1436       1.1   martin 		break;
   1437       1.1   martin 	default:
   1438       1.1   martin 		return false;
   1439       1.1   martin 	}
   1440       1.1   martin 
   1441       1.1   martin 	return true;
   1442       1.1   martin }
   1443       1.1   martin 
   1444       1.1   martin static bool
   1445       1.1   martin gpt_write_to_disk(struct disk_partitions *arg)
   1446       1.1   martin {
   1447       1.1   martin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
   1448       1.1   martin 	struct gpt_part_entry *p, *n;
   1449   1.6.2.5  msaitoh 	char label_arg[sizeof(p->gp_label) + 10];
   1450       1.1   martin 	char diskpath[MAXPATHLEN];
   1451       1.1   martin 	int fd, bits = 0;
   1452       1.1   martin 	bool root_is_new = false, efi_is_new = false;
   1453       1.1   martin 	part_id root_id = NO_PART, efi_id = NO_PART, pno;
   1454       1.1   martin 
   1455       1.1   martin 	/*
   1456       1.1   martin 	 * Remove all wedges on this disk - they may become invalid and we
   1457       1.1   martin 	 * have no easy way to associate them with the partitioning data.
   1458       1.1   martin 	 * Instead we will explicitly request creation of wedges on demand
   1459       1.1   martin 	 * later.
   1460       1.1   martin 	 */
   1461       1.1   martin 	fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
   1462       1.1   martin 	if (fd < 0)
   1463       1.1   martin 		return false;
   1464       1.1   martin 	if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
   1465       1.1   martin 		return false;
   1466       1.1   martin 	close(fd);
   1467       1.1   martin 
   1468       1.1   martin 	/*
   1469   1.6.2.6  msaitoh 	 * Collect first root and efi partition (if available), clear
   1470   1.6.2.6  msaitoh 	 * "have wedge" flags.
   1471       1.1   martin 	 */
   1472       1.1   martin 	for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
   1473   1.6.2.6  msaitoh 		p->gp_flags &= ~GPEF_WEDGE;
   1474       1.5   martin 		if (root_id == NO_PART && p->gp_type != NULL) {
   1475       1.1   martin 			if (p->gp_type->gent.generic_ptype == PT_root &&
   1476   1.6.2.9   bouyer 			    (p->gp_flags & GPEF_TARGET)) {
   1477       1.1   martin 				root_id = pno;
   1478       1.1   martin 				root_is_new = !(p->gp_flags & GPEF_ON_DISK);
   1479       1.1   martin 			} else if (efi_id == NO_PART &&
   1480       1.1   martin 			    p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
   1481       1.1   martin 				efi_id = pno;
   1482       1.1   martin 				efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
   1483       1.1   martin 			}
   1484       1.1   martin 		}
   1485       1.1   martin 	}
   1486       1.1   martin 
   1487       1.1   martin 	/*
   1488       1.1   martin 	 * If no GPT on disk yet, create it.
   1489       1.1   martin 	 */
   1490       1.1   martin 	if (!parts->has_gpt) {
   1491       1.1   martin 		char limit[30];
   1492       1.1   martin 
   1493       1.1   martin 		if (parts->max_num_parts > 0)
   1494       1.1   martin 			sprintf(limit, "-p %zu", parts->max_num_parts);
   1495       1.1   martin 		else
   1496       1.1   martin 			limit[0] = 0;
   1497       1.1   martin 		if (run_program(RUN_SILENT, "gpt create %s %s",
   1498       1.1   martin 		    limit, parts->dp.disk))
   1499       1.1   martin 			return false;
   1500       1.1   martin 		parts->has_gpt = true;
   1501       1.1   martin 	}
   1502       1.1   martin 
   1503       1.1   martin 	/*
   1504       1.1   martin 	 * Delete all old partitions
   1505       1.1   martin 	 */
   1506       1.1   martin 	for (p = parts->obsolete; p != NULL; p = n) {
   1507       1.1   martin 		run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
   1508       1.1   martin 		    p->gp_start, arg->disk);
   1509       1.1   martin 		n = p->gp_next;
   1510       1.1   martin 		free(p);
   1511       1.1   martin 	}
   1512       1.1   martin 	parts->obsolete = NULL;
   1513       1.1   martin 
   1514       1.1   martin 	/*
   1515       1.1   martin 	 * Modify existing but changed partitions
   1516       1.1   martin 	 */
   1517       1.1   martin 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
   1518       1.1   martin 		if (!(p->gp_flags & GPEF_ON_DISK))
   1519       1.1   martin 			continue;
   1520       1.1   martin 
   1521       1.1   martin 		if (p->gp_flags & GPEF_RESIZED) {
   1522       1.1   martin 			run_program(RUN_SILENT,
   1523       1.1   martin 			    "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
   1524       1.1   martin 			    p->gp_start, p->gp_size, arg->disk);
   1525       1.1   martin 			p->gp_flags &= ~GPEF_RESIZED;
   1526       1.1   martin 		}
   1527       1.1   martin 
   1528       1.1   martin 		if (!(p->gp_flags & GPEF_MODIFIED))
   1529       1.1   martin 			continue;
   1530       1.1   martin 
   1531       1.1   martin 		if (!gpt_modify_part(parts->dp.disk, p))
   1532       1.1   martin 			return false;
   1533       1.1   martin 	}
   1534       1.1   martin 
   1535       1.1   martin 	/*
   1536       1.1   martin 	 * Add new partitions
   1537       1.1   martin 	 */
   1538       1.1   martin 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
   1539       1.1   martin 		if (p->gp_flags & GPEF_ON_DISK)
   1540       1.1   martin 			continue;
   1541       1.1   martin 		if (!(p->gp_flags & GPEF_MODIFIED))
   1542       1.1   martin 			continue;
   1543       1.1   martin 
   1544       1.1   martin 		if (p->gp_label[0] == 0)
   1545       1.1   martin 			label_arg[0] = 0;
   1546       1.1   martin 		else
   1547   1.6.2.5  msaitoh 			sprintf(label_arg, "-l \'%s\'", p->gp_label);
   1548       1.1   martin 
   1549       1.5   martin 		if (p->gp_type != NULL)
   1550       1.5   martin 			run_program(RUN_SILENT,
   1551       1.5   martin 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
   1552       1.5   martin 			    "s -t %s %s %s",
   1553       1.5   martin 			    p->gp_start, p->gp_size, p->gp_type->tid,
   1554       1.5   martin 			    label_arg, arg->disk);
   1555       1.5   martin 		else
   1556       1.5   martin 			run_program(RUN_SILENT,
   1557       1.5   martin 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
   1558       1.5   martin 			    "s %s %s",
   1559       1.5   martin 			    p->gp_start, p->gp_size, label_arg, arg->disk);
   1560       1.1   martin 		gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
   1561       1.1   martin 		gpt_read_part(arg->disk, p->gp_start, p);
   1562       1.1   martin 		p->gp_flags |= GPEF_ON_DISK;
   1563       1.1   martin 	}
   1564       1.1   martin 
   1565       1.1   martin 	/*
   1566       1.1   martin 	 * Additional MD bootloader magic...
   1567       1.1   martin 	 */
   1568       1.1   martin 	if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
   1569       1.1   martin 	    efi_is_new))
   1570       1.1   martin 		return false;
   1571       1.1   martin 
   1572       1.1   martin 	return true;
   1573       1.1   martin }
   1574       1.1   martin 
   1575   1.6.2.3  msaitoh static part_id
   1576   1.6.2.3  msaitoh gpt_find_by_name(struct disk_partitions *arg, const char *name)
   1577   1.6.2.3  msaitoh {
   1578   1.6.2.3  msaitoh 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
   1579   1.6.2.3  msaitoh 	struct gpt_part_entry *p;
   1580   1.6.2.3  msaitoh 	part_id pno;
   1581   1.6.2.3  msaitoh 
   1582   1.6.2.3  msaitoh 	for (pno = 0, p = parts->partitions; p != NULL;
   1583   1.6.2.3  msaitoh 	    p = p->gp_next, pno++) {
   1584   1.6.2.3  msaitoh 		if (strcmp(p->gp_label, name) == 0)
   1585   1.6.2.3  msaitoh 			return pno;
   1586   1.6.2.3  msaitoh 		if (strcmp(p->gp_id, name) == 0)
   1587   1.6.2.3  msaitoh 			return pno;
   1588   1.6.2.3  msaitoh 	}
   1589   1.6.2.3  msaitoh 
   1590   1.6.2.3  msaitoh 	return NO_PART;
   1591   1.6.2.3  msaitoh }
   1592   1.6.2.3  msaitoh 
   1593       1.1   martin bool
   1594       1.1   martin gpt_parts_check(void)
   1595       1.1   martin {
   1596       1.1   martin 
   1597       1.1   martin 	check_available_binaries();
   1598       1.1   martin 
   1599       1.1   martin 	return have_gpt && have_dk;
   1600       1.1   martin }
   1601       1.1   martin 
   1602       1.1   martin static void
   1603       1.1   martin gpt_free(struct disk_partitions *arg)
   1604       1.1   martin {
   1605       1.1   martin 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
   1606       1.1   martin 	struct gpt_part_entry *p, *n;
   1607       1.1   martin 
   1608       1.1   martin 	assert(parts != NULL);
   1609       1.1   martin 	for (p = parts->partitions; p != NULL; p = n) {
   1610  1.6.2.10  msaitoh 		if (p->gp_flags & GPEF_WEDGE)
   1611  1.6.2.10  msaitoh 			register_post_umount_delwedge(parts->dp.disk,
   1612  1.6.2.10  msaitoh 			    p->gp_dev_name);
   1613       1.1   martin 		free(__UNCONST(p->last_mounted));
   1614       1.1   martin 		n = p->gp_next;
   1615       1.1   martin 		free(p);
   1616       1.1   martin 	}
   1617   1.6.2.6  msaitoh 	free(__UNCONST(parts->dp.disk));
   1618       1.1   martin 	free(parts);
   1619       1.1   martin }
   1620       1.1   martin 
   1621   1.6.2.9   bouyer static void
   1622   1.6.2.9   bouyer gpt_destroy_part_scheme(struct disk_partitions *arg)
   1623   1.6.2.9   bouyer {
   1624   1.6.2.9   bouyer 
   1625   1.6.2.9   bouyer 	run_program(RUN_SILENT, "gpt destroy %s", arg->disk);
   1626   1.6.2.9   bouyer 	gpt_free(arg);
   1627   1.6.2.9   bouyer }
   1628   1.6.2.9   bouyer 
   1629       1.1   martin static bool
   1630       1.1   martin gpt_custom_attribute_writable(const struct disk_partitions *arg,
   1631       1.1   martin     part_id ptn, size_t attr_no)
   1632       1.1   martin {
   1633       1.1   martin 	const struct gpt_disk_partitions *parts =
   1634       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
   1635       1.1   martin 	size_t i;
   1636       1.1   martin 	struct gpt_part_entry *p;
   1637       1.1   martin 
   1638       1.1   martin 	if (attr_no >= arg->pscheme->custom_attribute_count)
   1639       1.1   martin 		return false;
   1640       1.1   martin 
   1641       1.1   martin 	const msg label = arg->pscheme->custom_attributes[attr_no].label;
   1642       1.1   martin 
   1643       1.1   martin 	/* we can not edit the uuid attribute */
   1644       1.1   martin 	if (label == MSG_ptn_uuid)
   1645       1.1   martin 		return false;
   1646       1.1   martin 
   1647       1.1   martin 	/* the label is always editable */
   1648       1.1   martin 	if (label == MSG_ptn_label)
   1649       1.1   martin 		return true;
   1650       1.1   martin 
   1651       1.1   martin 	/* the GPT type is read only */
   1652       1.1   martin 	if (label == MSG_ptn_gpt_type)
   1653       1.1   martin 		return false;
   1654       1.1   martin 
   1655       1.1   martin 	/* BOOTME makes no sense on swap partitions */
   1656       1.1   martin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
   1657       1.1   martin 		if (i == ptn)
   1658       1.1   martin 			break;
   1659       1.1   martin 
   1660       1.1   martin 	if (p == NULL)
   1661       1.1   martin 		return false;
   1662       1.1   martin 
   1663       1.5   martin 	if (p->fs_type == FS_SWAP ||
   1664       1.5   martin 	    (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
   1665       1.1   martin 		return false;
   1666       1.1   martin 
   1667       1.1   martin 	return true;
   1668       1.1   martin }
   1669       1.1   martin 
   1670       1.6   martin static const char *
   1671       1.6   martin gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
   1672       1.6   martin {
   1673       1.6   martin 	const struct gpt_disk_partitions *parts =
   1674       1.6   martin 	    (const struct gpt_disk_partitions*)arg;
   1675       1.6   martin 	size_t i;
   1676       1.6   martin 	struct gpt_part_entry *p;
   1677       1.6   martin 
   1678       1.6   martin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
   1679       1.6   martin 		if (i == ptn)
   1680       1.6   martin 			break;
   1681       1.6   martin 
   1682       1.6   martin 	if (p == NULL)
   1683       1.6   martin 		return NULL;
   1684       1.6   martin 
   1685       1.6   martin 	if (p->gp_label[0] != 0)
   1686       1.6   martin 		return p->gp_label;
   1687       1.6   martin 	return p->gp_id;
   1688       1.6   martin }
   1689       1.6   martin 
   1690       1.1   martin static bool
   1691       1.1   martin gpt_format_custom_attribute(const struct disk_partitions *arg,
   1692       1.1   martin     part_id ptn, size_t attr_no, const struct disk_part_info *info,
   1693       1.1   martin     char *out, size_t out_space)
   1694       1.1   martin {
   1695       1.1   martin 	const struct gpt_disk_partitions *parts =
   1696       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
   1697       1.1   martin 	size_t i;
   1698       1.1   martin 	struct gpt_part_entry *p, data;
   1699       1.1   martin 
   1700       1.1   martin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
   1701       1.1   martin 		if (i == ptn)
   1702       1.1   martin 			break;
   1703       1.1   martin 
   1704       1.1   martin 	if (p == NULL)
   1705       1.1   martin 		return false;
   1706       1.1   martin 
   1707       1.1   martin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
   1708       1.1   martin 		return false;
   1709       1.1   martin 
   1710       1.1   martin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
   1711       1.1   martin 
   1712       1.1   martin 	if (info != NULL) {
   1713       1.1   martin 		data = *p;
   1714       1.1   martin 		gpt_info_to_part(&data, info, NULL);
   1715       1.1   martin 		p = &data;
   1716       1.1   martin 	}
   1717       1.1   martin 
   1718       1.1   martin 	if (label == MSG_ptn_label)
   1719       1.1   martin 		strlcpy(out, p->gp_label, out_space);
   1720       1.1   martin 	else if (label == MSG_ptn_uuid)
   1721       1.1   martin 		strlcpy(out, p->gp_id, out_space);
   1722       1.5   martin 	else if (label == MSG_ptn_gpt_type) {
   1723       1.5   martin 		if (p->gp_type != NULL)
   1724       1.5   martin 			strlcpy(out, p->gp_type->gent.description, out_space);
   1725       1.5   martin 		else if (out_space > 1)
   1726       1.5   martin 			out[0] = 0;
   1727       1.5   martin 	} else if (label == MSG_ptn_boot)
   1728       1.1   martin 		strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
   1729       1.1   martin 		    MSG_Yes : MSG_No), out_space);
   1730       1.1   martin 	else
   1731       1.1   martin 		return false;
   1732       1.1   martin 
   1733       1.1   martin 	return true;
   1734       1.1   martin }
   1735       1.1   martin 
   1736       1.1   martin static bool
   1737       1.1   martin gpt_custom_attribute_toggle(struct disk_partitions *arg,
   1738       1.1   martin     part_id ptn, size_t attr_no)
   1739       1.1   martin {
   1740       1.1   martin 	const struct gpt_disk_partitions *parts =
   1741       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
   1742       1.1   martin 	size_t i;
   1743       1.1   martin 	struct gpt_part_entry *p;
   1744       1.1   martin 
   1745       1.1   martin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
   1746       1.1   martin 		if (i == ptn)
   1747       1.1   martin 			break;
   1748       1.1   martin 
   1749       1.1   martin 	if (p == NULL)
   1750       1.1   martin 		return false;
   1751       1.1   martin 
   1752       1.1   martin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
   1753       1.1   martin 		return false;
   1754       1.1   martin 
   1755       1.1   martin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
   1756       1.1   martin 	if (label != MSG_ptn_boot)
   1757       1.1   martin 		return false;
   1758       1.1   martin 
   1759       1.1   martin 	if (p->gp_attr & GPT_ATTR_BOOT) {
   1760       1.1   martin 		p->gp_attr &= ~GPT_ATTR_BOOT;
   1761       1.1   martin 	} else {
   1762       1.1   martin 		for (i = 0, p = parts->partitions; p != NULL;
   1763       1.1   martin 		    i++, p = p->gp_next)
   1764       1.1   martin 			if (i == ptn)
   1765       1.1   martin 				p->gp_attr |= GPT_ATTR_BOOT;
   1766       1.1   martin 			else
   1767       1.1   martin 				p->gp_attr &= ~GPT_ATTR_BOOT;
   1768       1.1   martin 	}
   1769       1.1   martin 	return true;
   1770       1.1   martin }
   1771       1.1   martin 
   1772       1.1   martin static bool
   1773       1.1   martin gpt_custom_attribute_set_str(struct disk_partitions *arg,
   1774       1.1   martin     part_id ptn, size_t attr_no, const char *new_val)
   1775       1.1   martin {
   1776       1.1   martin 	const struct gpt_disk_partitions *parts =
   1777       1.1   martin 	    (const struct gpt_disk_partitions*)arg;
   1778       1.1   martin 	size_t i;
   1779       1.1   martin 	struct gpt_part_entry *p;
   1780       1.1   martin 
   1781       1.1   martin 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
   1782       1.1   martin 		if (i == ptn)
   1783       1.1   martin 			break;
   1784       1.1   martin 
   1785       1.1   martin 	if (p == NULL)
   1786       1.1   martin 		return false;
   1787       1.1   martin 
   1788       1.1   martin 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
   1789       1.1   martin 		return false;
   1790       1.1   martin 
   1791       1.1   martin 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
   1792       1.1   martin 
   1793       1.1   martin 	if (label != MSG_ptn_label)
   1794       1.1   martin 		return false;
   1795       1.1   martin 
   1796       1.1   martin 	strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
   1797       1.1   martin 	return true;
   1798       1.1   martin }
   1799       1.1   martin 
   1800       1.1   martin static bool
   1801       1.1   martin gpt_have_boot_support(const char *disk)
   1802       1.1   martin {
   1803       1.1   martin #ifdef	HAVE_GPT_BOOT
   1804       1.1   martin 	return true;
   1805       1.1   martin #else
   1806       1.1   martin 	return false;
   1807       1.1   martin #endif
   1808       1.1   martin }
   1809       1.1   martin 
   1810       1.1   martin const struct disk_part_custom_attribute gpt_custom_attrs[] = {
   1811       1.1   martin 	{ .label = MSG_ptn_label,	.type = pet_str },
   1812       1.1   martin 	{ .label = MSG_ptn_uuid,	.type = pet_str },
   1813       1.1   martin 	{ .label = MSG_ptn_gpt_type,	.type = pet_str },
   1814       1.1   martin 	{ .label = MSG_ptn_boot,	.type = pet_bool },
   1815       1.1   martin };
   1816       1.1   martin 
   1817       1.1   martin const struct disk_partitioning_scheme
   1818       1.1   martin gpt_parts = {
   1819       1.1   martin 	.name = MSG_parttype_gpt,
   1820       1.1   martin 	.short_name = MSG_parttype_gpt_short,
   1821       1.1   martin 	.part_flag_desc = MSG_gpt_flag_desc,
   1822       1.1   martin 	.custom_attribute_count = __arraycount(gpt_custom_attrs),
   1823       1.1   martin 	.custom_attributes = gpt_custom_attrs,
   1824       1.1   martin 	.get_part_types_count = gpt_type_count,
   1825       1.1   martin 	.get_part_type = gpt_get_ptype,
   1826       1.1   martin 	.get_generic_part_type = gpt_get_generic_type,
   1827       1.1   martin 	.get_fs_part_type = gpt_get_fs_part_type,
   1828   1.6.2.8  msaitoh 	.get_default_fstype = gpt_get_default_fstype,
   1829   1.6.2.6  msaitoh 	.create_custom_part_type = gpt_create_custom_part_type,
   1830   1.6.2.6  msaitoh 	.create_unknown_part_type = gpt_create_unknown_part_type,
   1831       1.1   martin 	.get_part_alignment = gpt_get_part_alignment,
   1832       1.1   martin 	.read_from_disk = gpt_read_from_disk,
   1833   1.6.2.8  msaitoh 	.get_cylinder_size = gpt_cyl_size,
   1834       1.1   martin 	.create_new_for_disk = gpt_create_new,
   1835       1.1   martin 	.have_boot_support = gpt_have_boot_support,
   1836   1.6.2.3  msaitoh 	.find_by_name = gpt_find_by_name,
   1837       1.1   martin 	.can_add_partition = gpt_can_add_partition,
   1838       1.1   martin 	.custom_attribute_writable = gpt_custom_attribute_writable,
   1839       1.1   martin 	.format_custom_attribute = gpt_format_custom_attribute,
   1840       1.1   martin 	.custom_attribute_toggle = gpt_custom_attribute_toggle,
   1841       1.1   martin 	.custom_attribute_set_str = gpt_custom_attribute_set_str,
   1842       1.6   martin 	.other_partition_identifier = gpt_get_label_str,
   1843       1.1   martin 	.get_part_device = gpt_get_part_device,
   1844       1.1   martin 	.max_free_space_at = gpt_max_free_space_at,
   1845       1.1   martin 	.get_free_spaces = gpt_get_free_spaces,
   1846   1.6.2.6  msaitoh 	.adapt_foreign_part_info = generic_adapt_foreign_part_info,
   1847       1.1   martin 	.get_part_info = gpt_get_part_info,
   1848       1.1   martin 	.get_part_attr_str = gpt_get_part_attr_str,
   1849       1.1   martin 	.set_part_info = gpt_set_part_info,
   1850       1.1   martin 	.add_partition = gpt_add_part,
   1851       1.1   martin 	.delete_all_partitions = gpt_delete_all_partitions,
   1852       1.1   martin 	.delete_partition = gpt_delete_partition,
   1853       1.1   martin 	.write_to_disk = gpt_write_to_disk,
   1854       1.1   martin 	.free = gpt_free,
   1855   1.6.2.9   bouyer 	.destroy_part_scheme = gpt_destroy_part_scheme,
   1856   1.6.2.6  msaitoh 	.cleanup = gpt_cleanup,
   1857       1.1   martin };
   1858