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