fdisk.c (20627B)
1 /* see LICENSE file for copyright and license details */ 2 #include "diskutil.h" 3 #include "libdisk/gpt.h" 4 #include "util.h" 5 6 #include <fcntl.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <strings.h> 11 #include <unistd.h> 12 13 struct BsdPartition { 14 uint32_t size; 15 uint32_t offset; 16 uint16_t offset_h; 17 uint16_t size_h; 18 uint8_t fstype; 19 uint8_t fragblock; 20 uint16_t cpg; 21 } __attribute__((packed)); 22 23 struct BsdDisklabel { 24 uint32_t d_magic; 25 uint16_t d_type; 26 uint16_t d_subtype; 27 char d_typename[16]; 28 char d_packname[16]; 29 uint32_t d_secsize; 30 uint32_t d_nsectors; 31 uint32_t d_ntracks; 32 uint32_t d_ncylinders; 33 uint32_t d_secpercyl; 34 uint32_t d_secperunit; 35 uint8_t d_uid[8]; 36 uint32_t d_acylinders; 37 uint16_t d_bstarth; 38 uint16_t d_bendh; 39 uint32_t d_bstart; 40 uint32_t d_bend; 41 uint32_t d_flags; 42 uint32_t d_spare4[5]; 43 uint16_t d_secperunith; 44 uint16_t d_version; 45 uint32_t d_spare[4]; 46 uint32_t d_magic2; 47 uint16_t d_checksum; 48 uint16_t d_npartitions; 49 uint32_t d_spare2; 50 uint32_t d_spare3; 51 struct BsdPartition d_partitions[16]; 52 } __attribute__((packed)); 53 54 /* resolves -b for a GPT add: a plain sector number, or 0/unset for 55 * the first free sector right after every existing partition's own 56 * end_lba (gpt_hdr.first_usable if the table is still empty), the 57 * same "0 means auto-place" convention gdisk-family tools already use */ 58 static uint64_t 59 gpt_resolve_start(struct GptHeader *hdr, struct GptPartition *parts, const char *s) 60 { 61 uint32_t i; 62 uint64_t next = hdr->first_usable; 63 64 if (s && strcmp(s, "0") != 0) 65 return (uint64_t)estrtoul(s, 10); 66 67 for (i = 0; i < hdr->num_parts; i++) { 68 if (parts[i].start_lba == 0 && parts[i].end_lba == 0) 69 continue; 70 if (parts[i].end_lba + 1 > next) 71 next = parts[i].end_lba + 1; 72 } 73 return next; 74 } 75 76 /* resolves -e for a GPT add: a plain sector number, 0/unset for the 77 * disk's own last usable sector, or +SIZE for a size relative to the 78 * already-resolved start */ 79 static uint64_t 80 gpt_resolve_end(struct GptHeader *hdr, size_t sec_size, uint64_t start, const char *s) 81 { 82 off_t size_bytes; 83 uint64_t sectors; 84 85 if (!s || strcmp(s, "0") == 0) 86 return hdr->last_usable; 87 if (s[0] == '+') { 88 size_bytes = parseoffset(s + 1); 89 if (size_bytes < 0) 90 eprintf("fdisk: malformed size '%s'\n", s); 91 sectors = ((uint64_t)size_bytes + sec_size - 1) / sec_size; 92 return start + sectors - 1; 93 } 94 return (uint64_t)estrtoul(s, 10); 95 } 96 97 static int opt_list = 0; 98 static int opt_print = 0; 99 static int opt_init_gpt = 0; 100 static int opt_init_mbr = 0; 101 static int opt_init_bsd = 0; 102 static int opt_add = 0; 103 static int opt_del = 0; 104 static int opt_part_idx = -1; 105 static int opt_part_is_letter = 0; 106 static const char *opt_start_str = NULL; 107 static const char *opt_end_str = NULL; 108 static const char *opt_type = NULL; 109 static const char *opt_name = NULL; 110 111 static void 112 usage(void) 113 { 114 eprintf( 115 "usage: %s [-l] [-p] [-g] [-m] [-s] [-a] [-d] [-n index] [-b " 116 "start] [-e end] [-t type] [-c name] [device]\n", 117 argv0 118 ); 119 } 120 121 static void 122 mbr_print(struct MbrHeader *mbr, const char *path) 123 { 124 int i; 125 printf("Disk: %s\n", path); 126 printf("Partition table (MBR/dos):\n"); 127 printf("%-5s %-4s %-12s %-12s %-4s\n", "Index", "Boot", "Start", "Size", "Type"); 128 for (i = 0; i < 4; i++) { 129 struct MbrPartition *p = &mbr->parts[i]; 130 if (p->size == 0) 131 continue; 132 printf( 133 "%-5d %-4s %-12u %-12u 0x%02x\n", 134 i + 1, 135 p->boot == 0x80 ? "Yes" : "No", 136 p->start_lba, 137 p->size, 138 p->type 139 ); 140 } 141 } 142 143 static void 144 gpt_print(struct GptHeader *hdr, struct GptPartition *parts, const char *path) 145 { 146 uint32_t i; 147 char guid[37]; 148 printf("Disk: %s\n", path); 149 printf("Partition table (GPT):\n"); 150 printf("%-5s %-12s %-12s %-36s\n", "Index", "Start", "End", "Type GUID"); 151 for (i = 0; i < hdr->num_parts; i++) { 152 struct GptPartition *p = &parts[i]; 153 if (p->start_lba == 0 && p->end_lba == 0) 154 continue; 155 156 snprintf( 157 guid, 158 sizeof(guid), 159 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%" 160 "02x%02x%02x%02x", 161 p->type_guid[3], 162 p->type_guid[2], 163 p->type_guid[1], 164 p->type_guid[0], 165 p->type_guid[5], 166 p->type_guid[4], 167 p->type_guid[7], 168 p->type_guid[6], 169 p->type_guid[8], 170 p->type_guid[9], 171 p->type_guid[10], 172 p->type_guid[11], 173 p->type_guid[12], 174 p->type_guid[13], 175 p->type_guid[14], 176 p->type_guid[15] 177 ); 178 179 printf( 180 "%-5d %-12llu %-12llu %-36s\n", 181 i + 1, 182 (unsigned long long)p->start_lba, 183 (unsigned long long)p->end_lba, 184 guid 185 ); 186 } 187 } 188 189 static uint16_t 190 bsd_dkcksum(void *lp, size_t nparts) 191 { 192 uint16_t *start; 193 uint16_t *end; 194 uint16_t sum = 0; 195 196 start = (uint16_t *)lp; 197 end = (uint16_t *)((char *)lp + 148 + nparts * 16); 198 while (start < end) 199 sum ^= *start++; 200 return sum; 201 } 202 203 static int 204 bsd_is_partition_type(uint8_t type) 205 { 206 return type == 0xa5 || type == 0xa6 || type == 0xa9; 207 } 208 209 static int 210 bsd_read(struct BlockDev *dev, uint64_t base_sec, struct BsdDisklabel *lp) 211 { 212 unsigned char sector[512]; 213 if (blockdev_read(dev, base_sec + 1, sector, 1) < 0) 214 return -1; 215 memcpy(lp, sector, sizeof(*lp)); 216 if (lp->d_magic != 0x82564557U || lp->d_magic2 != 0x82564557U) 217 return -1; 218 return 0; 219 } 220 221 static int 222 bsd_write(struct BlockDev *dev, uint64_t base_sec, struct BsdDisklabel *lp) 223 { 224 unsigned char sector[512]; 225 memset(sector, 0, sizeof(sector)); 226 lp->d_checksum = 0; 227 lp->d_checksum = bsd_dkcksum(lp, lp->d_npartitions); 228 memcpy(sector, lp, sizeof(*lp)); 229 if (blockdev_write(dev, base_sec + 1, sector, 1) < 0) 230 return -1; 231 return 0; 232 } 233 234 static void 235 bsd_print(struct BsdDisklabel *lp, const char *path, uint64_t base_sec) 236 { 237 uint64_t start, size; 238 int i; 239 240 printf("Disk: %s (at sector %llu)\n", path, (unsigned long long)base_sec); 241 printf("Partition table (BSD disklabel):\n"); 242 printf("%-5s %-12s %-12s %-10s\n", "Index", "Start", "Size", "Type"); 243 for (i = 0; i < lp->d_npartitions; i++) { 244 struct BsdPartition *p = &lp->d_partitions[i]; 245 start = ((uint64_t)p->offset_h << 32) | p->offset; 246 size = ((uint64_t)p->size_h << 32) | p->size; 247 if (size == 0) 248 continue; 249 printf( 250 "%-5c %-12llu %-12llu 0x%02x\n", 251 'a' + i, 252 (unsigned long long)start, 253 (unsigned long long)size, 254 p->fstype 255 ); 256 } 257 } 258 259 static int 260 bsd_find_label(struct BlockDev *dev, uint64_t *base_sec, struct BsdDisklabel *lp) 261 { 262 struct MbrHeader mbr; 263 int i; 264 265 if (bsd_read(dev, 0, lp) == 0) { 266 *base_sec = 0; 267 return 0; 268 } 269 270 if (blockdev_read(dev, 0, &mbr, 1) == 0 && mbr.sig[0] == 0x55 && mbr.sig[1] == 0xAA) { 271 for (i = 0; i < 4; i++) { 272 if (mbr.parts[i].size > 0 && bsd_is_partition_type(mbr.parts[i].type)) { 273 if (bsd_read(dev, mbr.parts[i].start_lba, lp) == 0) { 274 *base_sec = mbr.parts[i].start_lba; 275 return 0; 276 } 277 } 278 } 279 } 280 281 return -1; 282 } 283 284 static int 285 do_print(const char *path) 286 { 287 struct BlockDev dev; 288 struct MbrHeader mbr; 289 struct GptHeader gpt_hdr; 290 struct GptPartition gpt_parts[128]; 291 struct BsdDisklabel bsd_lp; 292 int i; 293 int found_bsd = 0; 294 295 if (blockdev_open(&dev, path, 0) < 0) { 296 weprintf("cannot open %s:", path); 297 return -1; 298 } 299 300 if (gpt_read(&dev, &gpt_hdr, gpt_parts) == 0) { 301 gpt_print(&gpt_hdr, gpt_parts, path); 302 } else if (blockdev_read(&dev, 0, &mbr, 1) == 0 && mbr.sig[0] == 0x55 && mbr.sig[1] == 0xAA) { 303 for (i = 0; i < 4; i++) { 304 if (mbr.parts[i].size > 0 && bsd_is_partition_type(mbr.parts[i].type)) { 305 if (bsd_read(&dev, mbr.parts[i].start_lba, &bsd_lp) == 0) { 306 bsd_print(&bsd_lp, path, mbr.parts[i].start_lba); 307 found_bsd = 1; 308 } 309 } 310 } 311 if (!found_bsd) { 312 if (bsd_read(&dev, 0, &bsd_lp) == 0) { 313 bsd_print(&bsd_lp, path, 0); 314 } else { 315 mbr_print(&mbr, path); 316 } 317 } 318 } else { 319 if (bsd_read(&dev, 0, &bsd_lp) == 0) { 320 bsd_print(&bsd_lp, path, 0); 321 } else { 322 printf("Disk %s: unpartitioned or unknown format\n", path); 323 } 324 } 325 326 blockdev_close(&dev); 327 return 0; 328 } 329 330 static int 331 do_fdisk(const char *path) 332 { 333 struct BlockDev dev; 334 struct MbrHeader mbr; 335 struct GptHeader gpt_hdr; 336 struct GptPartition gpt_parts[128]; 337 struct BsdDisklabel lp; 338 struct BsdDisklabel bsd_lp; 339 struct GptPartition *gp; 340 struct BsdPartition *bp; 341 struct MbrPartition *mp; 342 uint64_t base_sec; 343 uint64_t limit; 344 uint64_t bsd_base; 345 uint64_t size; 346 int idx; 347 int has_gpt = 0; 348 int has_bsd = 0; 349 uint64_t bsd_start, bsd_end; 350 uint64_t mbr_start, mbr_end; 351 352 if (blockdev_open(&dev, path, 1) < 0) { 353 weprintf("cannot open %s:", path); 354 return -1; 355 } 356 357 has_gpt = (gpt_read(&dev, &gpt_hdr, gpt_parts) == 0); 358 359 if (opt_init_mbr) { 360 memset(&mbr, 0, sizeof(mbr)); 361 mbr.sig[0] = 0x55; 362 mbr.sig[1] = 0xAA; 363 if (blockdev_write(&dev, 0, &mbr, 1) < 0) { 364 weprintf("cannot write MBR to %s:", path); 365 blockdev_close(&dev); 366 return -1; 367 } 368 printf("Initialized MBR partition table on %s\n", path); 369 blockdev_close(&dev); 370 return 0; 371 } 372 373 if (opt_init_gpt) { 374 memset(&gpt_hdr, 0, sizeof(gpt_hdr)); 375 memcpy(gpt_hdr.sig, "EFI PART", 8); 376 gpt_hdr.rev = 0x00010000; 377 gpt_hdr.size = 92; 378 gpt_hdr.current_lba = 1; 379 gpt_hdr.backup_lba = dev.size / dev.sec_size - 1; 380 gpt_hdr.first_usable = 34; 381 gpt_hdr.last_usable = gpt_hdr.backup_lba - 34; 382 gpt_hdr.partition_lba = 2; 383 gpt_hdr.num_parts = 128; 384 gpt_hdr.part_size = 128; 385 if (gpt_random_guid(gpt_hdr.disk_guid) < 0) { 386 weprintf("cannot read %s:", ARUU_PATH_DEVURANDOM); 387 blockdev_close(&dev); 388 return -1; 389 } 390 memset(gpt_parts, 0, sizeof(gpt_parts)); 391 392 if (gpt_write(&dev, &gpt_hdr, gpt_parts) < 0) { 393 weprintf("cannot write GPT to %s:", path); 394 blockdev_close(&dev); 395 return -1; 396 } 397 printf("Initialized GPT partition table on %s\n", path); 398 blockdev_close(&dev); 399 return 0; 400 } 401 402 if (opt_init_bsd) { 403 base_sec = 0; 404 limit = dev.size / dev.sec_size; 405 406 if (blockdev_read(&dev, 0, &mbr, 1) == 0 && mbr.sig[0] == 0x55 && mbr.sig[1] == 0xAA) { 407 if (!opt_part_is_letter && opt_part_idx >= 1 && opt_part_idx <= 4) { 408 mp = &mbr.parts[opt_part_idx - 1]; 409 if (mp->size > 0) { 410 base_sec = mp->start_lba; 411 limit = base_sec + mp->size; 412 } 413 } 414 } 415 416 memset(&lp, 0, sizeof(lp)); 417 lp.d_magic = 0x82564557U; 418 lp.d_magic2 = 0x82564557U; 419 lp.d_secsize = dev.sec_size; 420 lp.d_npartitions = 8; 421 lp.d_partitions[2].offset = (uint32_t)base_sec; 422 lp.d_partitions[2].offset_h = (uint16_t)(base_sec >> 32); 423 size = limit - base_sec; 424 lp.d_partitions[2].size = (uint32_t)size; 425 lp.d_partitions[2].size_h = (uint16_t)(size >> 32); 426 lp.d_partitions[2].fstype = 0; 427 428 if (bsd_write(&dev, base_sec, &lp) < 0) { 429 weprintf("cannot write BSD disklabel to %s:", path); 430 blockdev_close(&dev); 431 return -1; 432 } 433 printf( 434 "Initialized BSD disklabel on %s (at sector %llu)\n", path, (unsigned long long)base_sec 435 ); 436 blockdev_close(&dev); 437 return 0; 438 } 439 440 if (opt_add) { 441 if (opt_part_idx < 1) { 442 weprintf("add requires partition index (-n)\n"); 443 blockdev_close(&dev); 444 return -1; 445 } 446 447 if (has_gpt) { 448 if (opt_part_idx > 128) { 449 weprintf("GPT partition index must be 1-128\n"); 450 blockdev_close(&dev); 451 return -1; 452 } 453 gp = &gpt_parts[opt_part_idx - 1]; 454 gp->start_lba = gpt_resolve_start(&gpt_hdr, gpt_parts, opt_start_str); 455 gp->end_lba = gpt_resolve_end(&gpt_hdr, dev.sec_size, gp->start_lba, opt_end_str); 456 gpt_type_guid(opt_type, gp->type_guid); 457 if (gpt_random_guid(gp->part_guid) < 0) { 458 weprintf("cannot read %s:", ARUU_PATH_DEVURANDOM); 459 blockdev_close(&dev); 460 return -1; 461 } 462 gpt_set_name(gp->name, opt_name); 463 464 if (gpt_write(&dev, &gpt_hdr, gpt_parts) < 0) { 465 weprintf("cannot update GPT on %s:", path); 466 blockdev_close(&dev); 467 return -1; 468 } 469 printf("Added GPT partition %d to %s\n", opt_part_idx, path); 470 } else { 471 has_bsd = (bsd_find_label(&dev, &bsd_base, &bsd_lp) == 0); 472 if (opt_part_is_letter || has_bsd) { 473 if (!has_bsd) { 474 weprintf( 475 "BSD disklabel not found on " 476 "%s\n", 477 path 478 ); 479 blockdev_close(&dev); 480 return -1; 481 } 482 idx = opt_part_idx - 1; 483 if (idx >= 16) { 484 weprintf( 485 "BSD partition index must be " 486 "a-p\n" 487 ); 488 blockdev_close(&dev); 489 return -1; 490 } 491 bp = &bsd_lp.d_partitions[idx]; 492 bsd_start = opt_start_str ? (uint64_t)estrtoul(opt_start_str, 10) : 0; 493 bsd_end = opt_end_str ? (uint64_t)estrtoul(opt_end_str, 10) : 0; 494 bp->offset = (uint32_t)bsd_start; 495 bp->offset_h = (uint16_t)(bsd_start >> 32); 496 size = bsd_end - bsd_start + 1; 497 bp->size = (uint32_t)size; 498 bp->size_h = (uint16_t)(size >> 32); 499 bp->fstype = opt_type ? (uint8_t)strtoul(opt_type, NULL, 0) : 7; 500 if (idx >= bsd_lp.d_npartitions) 501 bsd_lp.d_npartitions = idx + 1; 502 503 if (bsd_write(&dev, bsd_base, &bsd_lp) < 0) { 504 weprintf( 505 "cannot update BSD disklabel " 506 "on %s:", 507 path 508 ); 509 blockdev_close(&dev); 510 return -1; 511 } 512 printf("Added BSD partition %c to %s\n", 'a' + idx, path); 513 } else { 514 if (blockdev_read(&dev, 0, &mbr, 1) < 0) { 515 weprintf("cannot read MBR from %s:", path); 516 blockdev_close(&dev); 517 return -1; 518 } 519 if (opt_part_idx > 4) { 520 weprintf( 521 "MBR partition index must be " 522 "1-4\n" 523 ); 524 blockdev_close(&dev); 525 return -1; 526 } 527 mp = &mbr.parts[opt_part_idx - 1]; 528 mbr_start = opt_start_str ? (uint64_t)estrtoul(opt_start_str, 10) : 0; 529 mbr_end = opt_end_str ? (uint64_t)estrtoul(opt_end_str, 10) : 0; 530 mp->start_lba = (uint32_t)mbr_start; 531 mp->size = (uint32_t)(mbr_end - mbr_start + 1); 532 mp->type = opt_type ? (uint8_t)strtoul(opt_type, NULL, 0) : 0x83; 533 mbr.sig[0] = 0x55; 534 mbr.sig[1] = 0xAA; 535 536 if (blockdev_write(&dev, 0, &mbr, 1) < 0) { 537 weprintf("cannot update MBR on %s:", path); 538 blockdev_close(&dev); 539 return -1; 540 } 541 printf("Added MBR partition %d to %s\n", opt_part_idx, path); 542 } 543 } 544 } 545 546 if (opt_del) { 547 if (opt_part_idx < 1) { 548 weprintf("delete requires partition index (-n)\n"); 549 blockdev_close(&dev); 550 return -1; 551 } 552 553 if (has_gpt) { 554 if (opt_part_idx > 128) { 555 weprintf("GPT partition index must be 1-128\n"); 556 blockdev_close(&dev); 557 return -1; 558 } 559 memset(&gpt_parts[opt_part_idx - 1], 0, sizeof(struct GptPartition)); 560 if (gpt_write(&dev, &gpt_hdr, gpt_parts) < 0) { 561 weprintf("cannot update GPT on %s:", path); 562 blockdev_close(&dev); 563 return -1; 564 } 565 printf("Deleted GPT partition %d from %s\n", opt_part_idx, path); 566 } else { 567 has_bsd = (bsd_find_label(&dev, &bsd_base, &bsd_lp) == 0); 568 if (opt_part_is_letter || has_bsd) { 569 if (!has_bsd) { 570 weprintf( 571 "BSD disklabel not found on " 572 "%s\n", 573 path 574 ); 575 blockdev_close(&dev); 576 return -1; 577 } 578 idx = opt_part_idx - 1; 579 if (idx >= 16) { 580 weprintf( 581 "BSD partition index must be " 582 "a-p\n" 583 ); 584 blockdev_close(&dev); 585 return -1; 586 } 587 memset(&bsd_lp.d_partitions[idx], 0, sizeof(struct BsdPartition)); 588 if (bsd_write(&dev, bsd_base, &bsd_lp) < 0) { 589 weprintf( 590 "cannot update BSD disklabel " 591 "on %s:", 592 path 593 ); 594 blockdev_close(&dev); 595 return -1; 596 } 597 printf("Deleted BSD partition %c from %s\n", 'a' + idx, path); 598 } else { 599 if (blockdev_read(&dev, 0, &mbr, 1) < 0) { 600 weprintf("cannot read MBR from %s:", path); 601 blockdev_close(&dev); 602 return -1; 603 } 604 if (opt_part_idx > 4) { 605 weprintf( 606 "MBR partition index must be " 607 "1-4\n" 608 ); 609 blockdev_close(&dev); 610 return -1; 611 } 612 mp = &mbr.parts[opt_part_idx - 1]; 613 memset(mp, 0, sizeof(struct MbrPartition)); 614 if (blockdev_write(&dev, 0, &mbr, 1) < 0) { 615 weprintf("cannot update MBR on %s:", path); 616 blockdev_close(&dev); 617 return -1; 618 } 619 printf("Deleted MBR partition %d from %s\n", opt_part_idx, path); 620 } 621 } 622 } 623 624 blockdev_close(&dev); 625 return 0; 626 } 627 628 // ?man fdisk: partition table manipulator 629 // ?man arguments: [-l] [-p] [-g] [-m] [-s] [-a] [-d] [-n index] [-b start] [-e end] [-t type] [-c name] [device] 630 // ?man fdisk performs partition table operations for MBR, GPT and BSD disklabels 631 int 632 main(int argc, char *argv[]) 633 { 634 int ret = 0; 635 636 ARGBEGIN 637 { 638 // ?man -l:list partitions of all devices 639 case 'l': 640 opt_list = 1; 641 break; 642 // ?man -p:print partition table of specified device 643 case 'p': 644 opt_print = 1; 645 break; 646 // ?man -g:initialize disk with GPT 647 case 'g': 648 opt_init_gpt = 1; 649 break; 650 // ?man -m:initialize disk with MBR 651 case 'm': 652 opt_init_mbr = 1; 653 break; 654 // ?man -s:initialize disk with BSD disklabel 655 case 's': 656 opt_init_bsd = 1; 657 break; 658 // ?man -a:add partition 659 case 'a': 660 opt_add = 1; 661 break; 662 // ?man -d:delete partition 663 case 'd': 664 opt_del = 1; 665 break; 666 // ?man -n:partition index or letter 667 case 'n': { 668 char *arg = EARGF(usage()); 669 if (arg[0] >= 'a' && arg[0] <= 'p' && arg[1] == '\0') { 670 opt_part_idx = arg[0] - 'a' + 1; 671 opt_part_is_letter = 1; 672 } else { 673 opt_part_idx = (int)estrtol(arg, 10); 674 opt_part_is_letter = 0; 675 } 676 break; 677 } 678 // ?man -b:starting sector LBA, or 0 for right after the last 679 // ?man partition already on the disk 680 case 'b': 681 opt_start_str = EARGF(usage()); 682 break; 683 // ?man -e:ending sector LBA, 0 for the disk's last usable 684 // ?man sector, or +SIZE (K/M/G/T, e.g. +512M) for a size 685 // ?man relative to the start 686 case 'e': 687 opt_end_str = EARGF(usage()); 688 break; 689 // ?man -t:partition type: MBR/BSD hex, or for GPT a gdisk-style 690 // ?man code (ef00, ef02, 8200, 8300) or a full GUID string 691 case 't': 692 opt_type = EARGF(usage()); 693 break; 694 // ?man -c:GPT partition name, ignored for MBR/BSD 695 case 'c': 696 opt_name = EARGF(usage()); 697 break; 698 default: 699 usage(); 700 } 701 ARGEND 702 703 if (opt_list) { 704 struct BlockDevInfo *list = blockdev_get_list(); 705 struct BlockDevInfo *curr; 706 if (!list) 707 return 1; 708 for (curr = list; curr; curr = curr->next) { 709 do_print(curr->path); 710 } 711 blockdev_free_list(list); 712 if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) 713 ret = 2; 714 return ret; 715 } 716 717 if (argc != 1) 718 usage(); 719 720 if (opt_print) { 721 ret = do_print(argv[0]) < 0 ? 1 : 0; 722 } else { 723 ret = do_fdisk(argv[0]) < 0 ? 1 : 0; 724 } 725 726 if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) 727 ret = 2; 728 return ret; 729 }