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