1 /* $OpenBSD: cmd.c,v 1.158 2022/04/24 12:13:37 krw Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Tobias Weingartner 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/disklabel.h> 21 22 #include <err.h> 23 #include <signal.h> 24 #include <stdint.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <uuid.h> 29 30 #include "part.h" 31 #include "disk.h" 32 #include "misc.h" 33 #include "mbr.h" 34 #include "gpt.h" 35 #include "user.h" 36 #include "cmd.h" 37 38 int gedit(const int); 39 int edit(const int, struct mbr *); 40 int gsetpid(const int); 41 int setpid(const int, struct mbr *); 42 int parsepn(const char *); 43 44 int ask_num(const char *, int, int, int); 45 int ask_pid(const int); 46 struct uuid *ask_uuid(const struct uuid *); 47 48 extern const unsigned char manpage[]; 49 extern const int manpage_sz; 50 51 int 52 Xreinit(char *args, struct mbr *mbr) 53 { 54 int dogpt; 55 56 dogpt = 0; 57 58 if (strcasecmp(args, "gpt") == 0) 59 dogpt = 1; 60 else if (strcasecmp(args, "mbr") == 0) 61 dogpt = 0; 62 else if (strlen(args) > 0) { 63 printf("Unrecognized modifier '%s'\n", args); 64 return CMD_CONT; 65 } 66 67 if (dogpt) { 68 GPT_init(GHANDGP); 69 GPT_print("s", TERSE); 70 } else { 71 MBR_init(mbr); 72 MBR_print(mbr, "s"); 73 } 74 75 printf("Use 'write' to update disk.\n"); 76 77 return CMD_DIRTY; 78 } 79 80 int 81 Xswap(char *args, struct mbr *mbr) 82 { 83 char *from, *to; 84 int pf, pt; 85 struct prt pp; 86 struct gpt_partition gg; 87 88 to = args; 89 from = strsep(&to, WHITESPACE); 90 91 pt = parsepn(to); 92 if (pt == -1) 93 return CMD_CONT; 94 95 pf = parsepn(from); 96 if (pf == -1) 97 return CMD_CONT; 98 99 if (pt == pf) { 100 printf("%d same partition as %d, doing nothing.\n", pt, pf); 101 return CMD_CONT; 102 } 103 104 if (gh.gh_sig == GPTSIGNATURE) { 105 gg = gp[pt]; 106 gp[pt] = gp[pf]; 107 gp[pf] = gg; 108 } else { 109 pp = mbr->mbr_prt[pt]; 110 mbr->mbr_prt[pt] = mbr->mbr_prt[pf]; 111 mbr->mbr_prt[pf] = pp; 112 } 113 114 return CMD_DIRTY; 115 } 116 117 int 118 gedit(const int pn) 119 { 120 struct uuid oldtype; 121 122 oldtype = gp[pn].gp_type; 123 124 if (gsetpid(pn)) 125 return -1; 126 127 if (uuid_is_nil(&gp[pn].gp_type, NULL)) { 128 if (uuid_is_nil(&oldtype, NULL) == 0) { 129 memset(&gp[pn], 0, sizeof(gp[pn])); 130 printf("Partition %d is disabled.\n", pn); 131 } 132 return 0; 133 } 134 135 if (GPT_get_lba_start(pn) == -1 || 136 GPT_get_lba_end(pn) == -1 || 137 GPT_get_name(pn) == -1) { 138 return -1; 139 } 140 141 return 0; 142 } 143 144 int 145 parsepn(const char *pnstr) 146 { 147 const char *errstr; 148 int maxpn, pn; 149 150 if (pnstr == NULL) { 151 printf("no partition number\n"); 152 return -1; 153 } 154 155 if (gh.gh_sig == GPTSIGNATURE) 156 maxpn = gh.gh_part_num - 1; 157 else 158 maxpn = NDOSPART - 1; 159 160 pn = strtonum(pnstr, 0, maxpn, &errstr); 161 if (errstr) { 162 printf("partition number is %s: %s\n", errstr, pnstr); 163 return -1; 164 } 165 166 return pn; 167 } 168 169 int 170 edit(const int pn, struct mbr *mbr) 171 { 172 struct chs start, end; 173 struct prt *pp; 174 uint64_t track; 175 unsigned char oldid; 176 177 pp = &mbr->mbr_prt[pn]; 178 oldid = pp->prt_id; 179 180 if (setpid(pn, mbr)) 181 return -1; 182 183 if (pp->prt_id == DOSPTYP_UNUSED) { 184 if (oldid != DOSPTYP_UNUSED) { 185 memset(pp, 0, sizeof(*pp)); 186 printf("Partition %d is disabled.\n", pn); 187 } 188 return 0; 189 } 190 191 if (ask_yn("Do you wish to edit in CHS mode?")) { 192 PRT_lba_to_chs(pp, &start, &end); 193 start.chs_cyl = ask_num("BIOS Starting cylinder", start.chs_cyl, 194 0, disk.dk_cylinders - 1); 195 start.chs_head = ask_num("BIOS Starting head", start.chs_head, 196 0, disk.dk_heads - 1); 197 start.chs_sect = ask_num("BIOS Starting sector", start.chs_sect, 198 1, disk.dk_sectors); 199 200 end.chs_cyl = ask_num("BIOS Ending cylinder", end.chs_cyl, 201 start.chs_cyl, disk.dk_cylinders - 1); 202 end.chs_head = ask_num("BIOS Ending head", end.chs_head, 203 (start.chs_cyl == end.chs_cyl) ? start.chs_head : 0, 204 disk.dk_heads - 1); 205 end.chs_sect = ask_num("BIOS Ending sector", end.chs_sect, 206 (start.chs_cyl == end.chs_cyl && start.chs_head == 207 end.chs_head) ? start.chs_sect : 1, disk.dk_sectors); 208 209 /* The ATA/ATAPI spec says LBA = (C × HPC + H) × SPT + (S − 1) */ 210 track = start.chs_cyl * disk.dk_heads + start.chs_head; 211 pp->prt_bs = track * disk.dk_sectors + (start.chs_sect - 1); 212 track = end.chs_cyl * disk.dk_heads + end.chs_head; 213 pp->prt_ns = track * disk.dk_sectors + (end.chs_sect - 1) - 214 pp->prt_bs + 1; 215 } else { 216 pp->prt_bs = getuint64("Partition offset", pp->prt_bs, 0, 217 disk.dk_size - 1); 218 pp->prt_ns = getuint64("Partition size", pp->prt_ns, 1, 219 disk.dk_size - pp->prt_bs); 220 } 221 222 return 0; 223 } 224 225 int 226 Xedit(char *args, struct mbr *mbr) 227 { 228 struct gpt_partition oldgg; 229 struct prt oldprt; 230 int pn; 231 232 pn = parsepn(args); 233 if (pn == -1) 234 return CMD_CONT; 235 236 if (gh.gh_sig == GPTSIGNATURE) { 237 oldgg = gp[pn]; 238 if (gedit(pn)) 239 gp[pn] = oldgg; 240 else if (memcmp(&gp[pn], &oldgg, sizeof(gp[pn]))) 241 return CMD_DIRTY; 242 } else { 243 oldprt = mbr->mbr_prt[pn]; 244 if (edit(pn, mbr)) 245 mbr->mbr_prt[pn] = oldprt; 246 else if (memcmp(&mbr->mbr_prt[pn], &oldprt, sizeof(oldprt))) 247 return CMD_DIRTY; 248 } 249 250 return CMD_CONT; 251 } 252 253 int 254 gsetpid(const int pn) 255 { 256 struct uuid gp_type, gp_guid; 257 uint32_t status; 258 259 GPT_print_parthdr(TERSE); 260 GPT_print_part(pn, "s", TERSE); 261 262 uuid_dec_le(&gp[pn].gp_type, &gp_type); 263 if (PRT_protected_guid(&gp_type)) { 264 printf("can't edit partition type %s\n", 265 PRT_uuid_to_typename(&gp_type)); 266 return -1; 267 } 268 269 gp_type = *ask_uuid(&gp_type); 270 if (PRT_protected_guid(&gp_type)) { 271 printf("can't change partition type to %s\n", 272 PRT_uuid_to_typename(&gp_type)); 273 return -1; 274 } 275 276 uuid_dec_le(&gp[pn].gp_guid, &gp_guid); 277 if (uuid_is_nil(&gp_guid, NULL)) { 278 uuid_create(&gp_guid, &status); 279 if (status != uuid_s_ok) { 280 printf("could not create guid for partition\n"); 281 return -1; 282 } 283 } 284 285 uuid_enc_le(&gp[pn].gp_type, &gp_type); 286 uuid_enc_le(&gp[pn].gp_guid, &gp_guid); 287 288 return 0; 289 } 290 291 int 292 setpid(const int pn, struct mbr *mbr) 293 { 294 struct prt *pp; 295 296 pp = &mbr->mbr_prt[pn]; 297 298 PRT_print_parthdr(); 299 PRT_print_part(pn, pp, "s"); 300 301 pp->prt_id = ask_pid(pp->prt_id); 302 303 return 0; 304 } 305 306 int 307 Xsetpid(char *args, struct mbr *mbr) 308 { 309 struct gpt_partition oldgg; 310 struct prt oldprt; 311 int pn; 312 313 pn = parsepn(args); 314 if (pn == -1) 315 return CMD_CONT; 316 317 if (gh.gh_sig == GPTSIGNATURE) { 318 oldgg = gp[pn]; 319 if (gsetpid(pn)) 320 gp[pn] = oldgg; 321 else if (memcmp(&gp[pn], &oldgg, sizeof(gp[pn]))) 322 return CMD_DIRTY; 323 } else { 324 oldprt = mbr->mbr_prt[pn]; 325 if (setpid(pn, mbr)) 326 mbr->mbr_prt[pn] = oldprt; 327 else if (memcmp(&mbr->mbr_prt[pn], &oldprt, sizeof(oldprt))) 328 return CMD_DIRTY; 329 } 330 331 return CMD_CONT; 332 } 333 334 int 335 Xselect(char *args, struct mbr *mbr) 336 { 337 static uint64_t lba_firstembr = 0; 338 uint64_t lba_self; 339 int pn; 340 341 pn = parsepn(args); 342 if (pn == -1) 343 return CMD_CONT; 344 345 lba_self = mbr->mbr_prt[pn].prt_bs; 346 347 if ((mbr->mbr_prt[pn].prt_id != DOSPTYP_EXTEND) && 348 (mbr->mbr_prt[pn].prt_id != DOSPTYP_EXTENDL)) { 349 printf("Partition %d is not an extended partition.\n", pn); 350 return CMD_CONT; 351 } 352 353 if (lba_firstembr == 0) 354 lba_firstembr = lba_self; 355 356 if (lba_self == 0) { 357 printf("Loop to MBR (sector 0)! Not selected.\n"); 358 return CMD_CONT; 359 } else { 360 printf("Selected extended partition %d\n", pn); 361 printf("New EMBR at offset %llu.\n", lba_self); 362 } 363 364 USER_edit(lba_self, lba_firstembr); 365 366 return CMD_CONT; 367 } 368 369 int 370 Xprint(char *args, struct mbr *mbr) 371 { 372 if (gh.gh_sig == GPTSIGNATURE) 373 GPT_print(args, VERBOSE); 374 else 375 MBR_print(mbr, args); 376 377 return CMD_CONT; 378 } 379 380 int 381 Xwrite(char *args, struct mbr *mbr) 382 { 383 int i, n; 384 385 for (i = 0, n = 0; i < NDOSPART; i++) 386 if (mbr->mbr_prt[i].prt_id == 0xA6) 387 n++; 388 if (n >= 2) { 389 warnx("MBR contains more than one OpenBSD partition!"); 390 if (!ask_yn("Write MBR anyway?")) 391 return CMD_CONT; 392 } 393 394 if (gh.gh_sig == GPTSIGNATURE) { 395 printf("Writing GPT.\n"); 396 if (GPT_write() == -1) { 397 warnx("error writing GPT"); 398 return CMD_CONT; 399 } 400 } else { 401 printf("Writing MBR at offset %llu.\n", mbr->mbr_lba_self); 402 if (MBR_write(mbr) == -1) { 403 warnx("error writing MBR"); 404 return CMD_CONT; 405 } 406 GPT_zap_headers(); 407 } 408 409 return CMD_CLEAN; 410 } 411 412 int 413 Xquit(char *args, struct mbr *mbr) 414 { 415 return CMD_QUIT; 416 } 417 418 int 419 Xabort(char *args, struct mbr *mbr) 420 { 421 exit(0); 422 } 423 424 int 425 Xexit(char *args, struct mbr *mbr) 426 { 427 return CMD_EXIT; 428 } 429 430 int 431 Xhelp(char *args, struct mbr *mbr) 432 { 433 USER_help(); 434 435 return CMD_CONT; 436 } 437 438 int 439 Xupdate(char *args, struct mbr *mbr) 440 { 441 memcpy(mbr->mbr_code, default_dmbr.dmbr_boot, sizeof(mbr->mbr_code)); 442 mbr->mbr_signature = DOSMBR_SIGNATURE; 443 printf("Machine code updated.\n"); 444 return CMD_DIRTY; 445 } 446 447 int 448 Xflag(char *args, struct mbr *mbr) 449 { 450 const char *errstr; 451 char *part, *flag; 452 long long val = -1; 453 int i, pn; 454 455 flag = args; 456 part = strsep(&flag, WHITESPACE); 457 458 pn = parsepn(part); 459 if (pn == -1) 460 return CMD_CONT; 461 462 if (flag != NULL) { 463 if (gh.gh_sig == GPTSIGNATURE) 464 val = strtonum(flag, 0, INT64_MAX, &errstr); 465 else 466 val = strtonum(flag, 0, 0xff, &errstr); 467 if (errstr) { 468 printf("flag value is %s: %s.\n", errstr, flag); 469 return CMD_CONT; 470 } 471 if (gh.gh_sig == GPTSIGNATURE) 472 gp[pn].gp_attrs = htole64(val); 473 else 474 mbr->mbr_prt[pn].prt_flag = val; 475 printf("Partition %d flag value set to 0x%llx.\n", pn, val); 476 } else { 477 if (gh.gh_sig == GPTSIGNATURE) { 478 for (i = 0; i < gh.gh_part_num; i++) { 479 if (i == pn) 480 gp[i].gp_attrs = htole64(GPTDOSACTIVE); 481 else 482 gp[i].gp_attrs = htole64(0); 483 } 484 } else { 485 for (i = 0; i < NDOSPART; i++) { 486 if (i == pn) 487 mbr->mbr_prt[i].prt_flag = DOSACTIVE; 488 else 489 mbr->mbr_prt[i].prt_flag = 0x00; 490 } 491 } 492 printf("Partition %d marked active.\n", pn); 493 } 494 495 return CMD_DIRTY; 496 } 497 498 int 499 Xmanual(char *args, struct mbr *mbr) 500 { 501 char *pager = "/usr/bin/less"; 502 char *p; 503 FILE *f; 504 sig_t opipe; 505 506 opipe = signal(SIGPIPE, SIG_IGN); 507 if ((p = getenv("PAGER")) != NULL && (*p != '\0')) 508 pager = p; 509 if (asprintf(&p, "gunzip -qc|%s", pager) != -1) { 510 f = popen(p, "w"); 511 if (f) { 512 fwrite(manpage, manpage_sz, 1, f); 513 pclose(f); 514 } 515 free(p); 516 } 517 518 signal(SIGPIPE, opipe); 519 520 return CMD_CONT; 521 } 522 523 int 524 ask_num(const char *str, int dflt, int low, int high) 525 { 526 char lbuf[100]; 527 const char *errstr; 528 int num; 529 530 if (dflt < low) 531 dflt = low; 532 else if (dflt > high) 533 dflt = high; 534 535 do { 536 printf("%s [%d - %d]: [%d] ", str, low, high, dflt); 537 string_from_line(lbuf, sizeof(lbuf), TRIMMED); 538 539 if (lbuf[0] == '\0') { 540 num = dflt; 541 errstr = NULL; 542 } else { 543 num = (int)strtonum(lbuf, low, high, &errstr); 544 if (errstr) 545 printf("%s is %s: %s.\n", str, errstr, lbuf); 546 } 547 } while (errstr); 548 549 return num; 550 } 551 552 int 553 ask_pid(const int dflt) 554 { 555 char lbuf[100]; 556 int num; 557 558 for (;;) { 559 printf("Partition id ('0' to disable) [01 - FF]: [%02X] ", dflt); 560 printf("(? for help) "); 561 string_from_line(lbuf, sizeof(lbuf), TRIMMED); 562 563 if (strlen(lbuf) == 0) 564 return dflt; 565 if (strcmp(lbuf, "?") == 0) { 566 PRT_print_mbrtypes(); 567 continue; 568 } 569 570 num = hex_octet(lbuf); 571 if (num != -1) 572 return num; 573 574 printf("'%s' is not a valid partition id.\n", lbuf); 575 } 576 } 577 578 struct uuid * 579 ask_uuid(const struct uuid *olduuid) 580 { 581 static struct uuid uuid; 582 char lbuf[100]; 583 char *dflt = NULL; 584 uint32_t status; 585 int num = 0; 586 587 uuid = *olduuid; 588 if (uuid_is_nil(&uuid, NULL) == 0) { 589 num = PRT_uuid_to_type(&uuid); 590 if (num == 0) { 591 uuid_to_string(&uuid, &dflt, &status); 592 if (status != uuid_s_ok) { 593 printf("uuid_to_string() failed\n"); 594 goto done; 595 } 596 } 597 } 598 if (dflt == NULL) { 599 if (asprintf(&dflt, "%X", num) == -1) { 600 warn("asprintf()"); 601 goto done; 602 } 603 } 604 605 for (;;) { 606 printf("Partition id ('0' to disable) [01 - FF, <uuid>]: [%s] ", 607 dflt); 608 printf("(? for help) "); 609 string_from_line(lbuf, sizeof(lbuf), TRIMMED); 610 611 if (strcmp(lbuf, "?") == 0) { 612 PRT_print_gpttypes(); 613 continue; 614 } else if (strlen(lbuf) == 0) { 615 uuid = *olduuid; 616 goto done; 617 } 618 619 uuid_from_string(lbuf, &uuid, &status); 620 if (status == uuid_s_ok) 621 goto done; 622 623 num = hex_octet(lbuf); 624 switch (num) { 625 case -1: 626 printf("'%s' is not a valid partition id\n", lbuf); 627 break; 628 case 0: 629 uuid_create_nil(&uuid, NULL); 630 goto done; 631 default: 632 uuid = *PRT_type_to_uuid(num); 633 if (uuid_is_nil(&uuid, NULL) == 0) 634 goto done; 635 printf("'%s' has no associated UUID\n", lbuf); 636 break; 637 } 638 } 639 640 done: 641 free(dflt); 642 return &uuid; 643 } 644