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