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