1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This file contains functions that implement the fdisk menu commands. 31 */ 32 #include "global.h" 33 #include <errno.h> 34 #include <sys/time.h> 35 #include <sys/resource.h> 36 #include <sys/wait.h> 37 #include <signal.h> 38 #include <string.h> 39 #include <fcntl.h> 40 #include <stdlib.h> 41 #include <sys/dktp/fdisk.h> 42 #include <sys/stat.h> 43 #include <sys/dklabel.h> 44 45 #include "main.h" 46 #include "analyze.h" 47 #include "menu.h" 48 #include "menu_command.h" 49 #include "menu_defect.h" 50 #include "menu_partition.h" 51 #include "menu_fdisk.h" 52 #include "param.h" 53 #include "misc.h" 54 #include "label.h" 55 #include "startup.h" 56 #include "partition.h" 57 #include "prompts.h" 58 #include "checkdev.h" 59 #include "io.h" 60 #include "ctlr_scsi.h" 61 #include "auto_sense.h" 62 63 extern struct menu_item menu_fdisk[]; 64 65 /* 66 * Byte swapping macros for accessing struct ipart 67 * to resolve little endian on Sparc. 68 */ 69 #if defined(sparc) 70 #define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF)) 71 #define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \ 72 (les((unsigned)((val)&0xffff0000)>>16))) 73 74 #elif defined(i386) 75 76 #define les(val) (val) 77 #define lel(val) (val) 78 79 #else /* defined(sparc) */ 80 81 #error No Platform defined 82 83 #endif /* defined(sparc) */ 84 85 86 /* Function prototypes */ 87 #ifdef __STDC__ 88 89 #if defined(sparc) 90 91 static int getbyte(uchar_t **); 92 static int getlong(uchar_t **); 93 94 #endif /* defined(sparc) */ 95 96 static int get_solaris_part(int fd, struct ipart *ipart); 97 98 #else /* __STDC__ */ 99 100 #if defined(sparc) 101 102 static int getbyte(); 103 static int getlong(); 104 105 #endif /* defined(sparc) */ 106 107 static int get_solaris_part(); 108 109 #endif /* __STDC__ */ 110 111 /* 112 * Handling the alignment problem of struct ipart. 113 */ 114 static void 115 fill_ipart(char *bootptr, struct ipart *partp) 116 { 117 #if defined(sparc) 118 /* 119 * Sparc platform: 120 * 121 * Packing short/word for struct ipart to resolve 122 * little endian on Sparc since it is not 123 * properly aligned on Sparc. 124 */ 125 partp->bootid = getbyte((uchar_t **)&bootptr); 126 partp->beghead = getbyte((uchar_t **)&bootptr); 127 partp->begsect = getbyte((uchar_t **)&bootptr); 128 partp->begcyl = getbyte((uchar_t **)&bootptr); 129 partp->systid = getbyte((uchar_t **)&bootptr); 130 partp->endhead = getbyte((uchar_t **)&bootptr); 131 partp->endsect = getbyte((uchar_t **)&bootptr); 132 partp->endcyl = getbyte((uchar_t **)&bootptr); 133 partp->relsect = getlong((uchar_t **)&bootptr); 134 partp->numsect = getlong((uchar_t **)&bootptr); 135 #elif defined(i386) 136 /* 137 * i386 platform: 138 * 139 * The fdisk table does not begin on a 4-byte boundary within 140 * the master boot record; so, we need to recopy its contents 141 * to another data structure to avoid an alignment exception. 142 */ 143 (void) bcopy(bootptr, partp, sizeof (struct ipart)); 144 #else 145 #error No Platform defined 146 #endif /* defined(sparc) */ 147 } 148 149 /* 150 * Get a correct byte/short/word routines for Sparc platform. 151 */ 152 #if defined(sparc) 153 static int 154 getbyte(uchar_t **bp) 155 { 156 int b; 157 158 b = **bp; 159 *bp = *bp + 1; 160 return (b); 161 } 162 163 #ifdef DEADCODE 164 static int 165 getshort(uchar_t **bp) 166 { 167 int b; 168 169 b = ((**bp) << 8) | *(*bp + 1); 170 *bp += 2; 171 return (b); 172 } 173 #endif /* DEADCODE */ 174 175 static int 176 getlong(uchar_t **bp) 177 { 178 int b, bh, bl; 179 180 bh = ((**bp) << 8) | *(*bp + 1); 181 *bp += 2; 182 bl = ((**bp) << 8) | *(*bp + 1); 183 *bp += 2; 184 185 b = (bh << 16) | bl; 186 return (b); 187 } 188 #endif /* defined(sparc) */ 189 190 /* 191 * Convert cn[tn]dn to cn[tn]dns2 path 192 */ 193 static void 194 get_sname(char *name) 195 { 196 char buf[MAXPATHLEN]; 197 char *devp = "/dev/dsk"; 198 char *rdevp = "/dev/rdsk"; 199 char np[MAXNAMELEN]; 200 char *npt; 201 202 /* 203 * If it is a full path /dev/[r]dsk/cn[tn]dn, use this path 204 */ 205 (void) strcpy(np, cur_disk->disk_name); 206 if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 || 207 strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) { 208 /* 209 * Skip if the path is already included with sN 210 */ 211 if (strchr(np, 's') == strrchr(np, 's')) { 212 npt = strrchr(np, 'p'); 213 /* If pN is found, do not include it */ 214 if (isdigit(*++npt)) { 215 *--npt = '\0'; 216 } 217 (void) snprintf(buf, sizeof (buf), "%ss2", np); 218 } else { 219 (void) snprintf(buf, sizeof (buf), "%s", np); 220 } 221 } else { 222 (void) snprintf(buf, sizeof (buf), "/dev/rdsk/%ss2", np); 223 } 224 (void) strcpy(name, buf); 225 } 226 227 /* 228 * Convert cn[tn]dnsn to cn[tn]dnp0 path 229 */ 230 static void 231 get_pname(char *name) 232 { 233 char buf[MAXPATHLEN]; 234 char *devp = "/dev/dsk"; 235 char *rdevp = "/dev/rdsk"; 236 char np[MAXNAMELEN]; 237 char *npt; 238 239 /* 240 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path 241 */ 242 (void) strcpy(np, cur_disk->disk_name); 243 if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 || 244 strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) { 245 /* 246 * Skip if the path is already included with pN 247 */ 248 if (strchr(np, 'p') == NULL) { 249 npt = strrchr(np, 's'); 250 /* If sN is found, do not include it */ 251 if (isdigit(*++npt)) { 252 *--npt = '\0'; 253 } 254 (void) snprintf(buf, sizeof (buf), "%sp0", np); 255 } else { 256 (void) snprintf(buf, sizeof (buf), "%s", np); 257 } 258 } else { 259 (void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np); 260 } 261 (void) strcpy(name, buf); 262 } 263 264 /* 265 * Open file descriptor for current disk (cur_file) 266 * with "p0" path or cur_disk->disk_path 267 */ 268 void 269 open_cur_file(int mode) 270 { 271 char *dkpath; 272 char pbuf[MAXPATHLEN]; 273 274 switch (mode) { 275 case FD_USE_P0_PATH: 276 (void) get_pname(&pbuf[0]); 277 dkpath = pbuf; 278 break; 279 case FD_USE_CUR_DISK_PATH: 280 if (cur_disk->fdisk_part.systid == SUNIXOS || 281 cur_disk->fdisk_part.systid == SUNIXOS2) { 282 (void) get_sname(&pbuf[0]); 283 dkpath = pbuf; 284 } else { 285 dkpath = cur_disk->disk_path; 286 } 287 break; 288 default: 289 err_print("Error: Invalid mode option for opening cur_file\n"); 290 fullabort(); 291 } 292 293 /* Close previous cur_file */ 294 (void) close(cur_file); 295 /* Open cur_file with the required path dkpath */ 296 if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) { 297 err_print( 298 "Error: can't open selected disk '%s'.\n", dkpath); 299 fullabort(); 300 } 301 } 302 303 304 /* 305 * This routine implements the 'fdisk' command. It simply runs 306 * the fdisk command on the current disk. 307 * Use of this is restricted to interactive mode only. 308 */ 309 int 310 c_fdisk() 311 { 312 313 char buf[MAXPATHLEN]; 314 char pbuf[MAXPATHLEN]; 315 struct stat statbuf; 316 317 /* 318 * We must be in interactive mode to use the fdisk command 319 */ 320 if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) { 321 err_print("Fdisk command is for interactive use only!\n"); 322 return (-1); 323 } 324 325 /* 326 * There must be a current disk type and a current disk 327 */ 328 if (cur_dtype == NULL) { 329 err_print("Current Disk Type is not set.\n"); 330 return (-1); 331 } 332 333 /* 334 * If disk is larger than 1TB then an EFI label is required 335 * and there is no point in running fdisk 336 */ 337 if (cur_dtype->capacity > INFINITY) { 338 err_print("This disk must use an EFI label.\n"); 339 return (-1); 340 } 341 342 /* 343 * Before running the fdisk command, get file status of 344 * /dev/rdsk/cn[tn]dnp0 path to see if this disk 345 * supports fixed disk partition table. 346 */ 347 (void) get_pname(&pbuf[0]); 348 if (stat(pbuf, (struct stat *)&statbuf) == -1 || 349 !S_ISCHR(statbuf.st_mode)) { 350 err_print( 351 "Disk does not support fixed disk partition table\n"); 352 return (0); 353 } 354 355 /* 356 * Run the fdisk program. 357 */ 358 (void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf); 359 (void) system(buf); 360 361 /* 362 * Open cur_file with "p0" path for accessing the fdisk table 363 */ 364 (void) open_cur_file(FD_USE_P0_PATH); 365 366 /* 367 * Get solaris partition information in the fdisk partition table 368 */ 369 if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) { 370 err_print("No fdisk solaris partition found\n"); 371 cur_disk->fdisk_part.numsect = 0; /* No Solaris */ 372 } 373 374 /* 375 * Restore cur_file with cur_disk->disk_path 376 */ 377 (void) open_cur_file(FD_USE_CUR_DISK_PATH); 378 379 return (0); 380 } 381 382 /* 383 * Read MBR on the disk 384 * if the Solaris partition has changed, 385 * reread the vtoc 386 */ 387 #ifdef DEADCODE 388 static void 389 update_cur_parts() 390 { 391 392 int i; 393 register struct partition_info *parts; 394 395 for (i = 0; i < NDKMAP; i++) { 396 #if defined(_SUNOS_VTOC_16) 397 if (cur_parts->vtoc.v_part[i].p_tag && 398 cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) { 399 cur_parts->vtoc.v_part[i].p_start = 0; 400 cur_parts->vtoc.v_part[i].p_size = 0; 401 402 #endif 403 cur_parts->pinfo_map[i].dkl_nblk = 0; 404 cur_parts->pinfo_map[i].dkl_cylno = 0; 405 cur_parts->vtoc.v_part[i].p_tag = 406 default_vtoc_map[i].p_tag; 407 cur_parts->vtoc.v_part[i].p_flag = 408 default_vtoc_map[i].p_flag; 409 #if defined(_SUNOS_VTOC_16) 410 } 411 #endif 412 } 413 cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc(); 414 415 #if defined(_SUNOS_VTOC_16) 416 /* 417 * Adjust for the boot partitions 418 */ 419 cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc(); 420 cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0; 421 cur_parts->vtoc.v_part[C_PARTITION].p_start = 422 cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect; 423 cur_parts->vtoc.v_part[C_PARTITION].p_size = 424 cur_parts->pinfo_map[C_PARTITION].dkl_nblk; 425 426 cur_parts->vtoc.v_part[I_PARTITION].p_start = 427 cur_parts->pinfo_map[I_PARTITION].dkl_cylno; 428 cur_parts->vtoc.v_part[I_PARTITION].p_size = 429 cur_parts->pinfo_map[I_PARTITION].dkl_nblk; 430 431 #endif /* defined(_SUNOS_VTOC_16) */ 432 parts = cur_dtype->dtype_plist; 433 cur_dtype->dtype_ncyl = ncyl; 434 cur_dtype->dtype_plist = cur_parts; 435 parts->pinfo_name = cur_parts->pinfo_name; 436 cur_disk->disk_parts = cur_parts; 437 cur_ctype->ctype_dlist = cur_dtype; 438 439 } 440 #endif /* DEADCODE */ 441 442 static int 443 get_solaris_part(int fd, struct ipart *ipart) 444 { 445 int i; 446 struct ipart ip; 447 int status; 448 char *bootptr; 449 struct dk_label update_label; 450 451 (void) lseek(fd, 0, 0); 452 status = read(fd, (caddr_t)&boot_sec, NBPSCTR); 453 454 if (status != NBPSCTR) { 455 err_print("Bad read of fdisk partition. Status = %x\n", status); 456 err_print("Cannot read fdisk partition information.\n"); 457 return (-1); 458 } 459 460 for (i = 0; i < FD_NUMPART; i++) { 461 int ipc; 462 463 ipc = i * sizeof (struct ipart); 464 465 /* Handling the alignment problem of struct ipart */ 466 bootptr = &boot_sec.parts[ipc]; 467 (void) fill_ipart(bootptr, &ip); 468 469 /* 470 * we are interested in Solaris and EFI partition types 471 */ 472 if (ip.systid == SUNIXOS || 473 ip.systid == SUNIXOS2 || 474 ip.systid == EFI_PMBR) { 475 /* 476 * if the disk has an EFI label, nhead and nsect may 477 * be zero. This test protects us from FPE's, and 478 * format still seems to work fine 479 */ 480 if (nhead != 0 && nsect != 0) { 481 pcyl = lel(ip.numsect) / (nhead * nsect); 482 xstart = lel(ip.relsect) / (nhead * nsect); 483 ncyl = pcyl - acyl; 484 } 485 #ifdef DEBUG 486 else { 487 err_print("Critical geometry values are zero:\n" 488 "\tnhead = %d; nsect = %d\n", nhead, 489 nsect); 490 } 491 #endif /* DEBUG */ 492 493 solaris_offset = lel(ip.relsect); 494 break; 495 } 496 } 497 498 if (i == FD_NUMPART) { 499 err_print("Solaris fdisk partition not found\n"); 500 return (-1); 501 } 502 503 /* 504 * compare the previous and current Solaris partition 505 * but don't use bootid in determination of Solaris partition changes 506 */ 507 ipart->bootid = ip.bootid; 508 status = bcmp(&ip, ipart, sizeof (struct ipart)); 509 510 bcopy(&ip, ipart, sizeof (struct ipart)); 511 512 /* if the disk partitioning has changed - get the VTOC */ 513 if (status) { 514 status = ioctl(fd, DKIOCGVTOC, &cur_parts->vtoc); 515 if (status == -1) { 516 i = errno; 517 err_print("Bad ioctl DKIOCGVTOC.\n"); 518 err_print("errno=%d %s\n", i, strerror(i)); 519 err_print("Cannot read vtoc information.\n"); 520 return (-1); 521 } 522 523 status = read_label(fd, &update_label); 524 if (status == -1) { 525 err_print("Cannot read label information.\n"); 526 return (-1); 527 } 528 529 #if defined(_SUNOS_VTOC_16) 530 /* 531 * this is to update the slice table on x86 532 * we don't care about VTOC8 here 533 */ 534 for (i = 0; i < NDKMAP; i ++) { 535 cur_parts->pinfo_map[i].dkl_cylno = 536 update_label.dkl_vtoc.v_part[i].p_start / 537 ((int)(update_label.dkl_nhead * 538 update_label.dkl_nsect)); 539 cur_parts->pinfo_map[i].dkl_nblk = 540 update_label.dkl_vtoc.v_part[i].p_size; 541 } 542 #endif /* defined(_SUNOS_VTOC_16) */ 543 544 cur_dtype->dtype_ncyl = update_label.dkl_ncyl; 545 cur_dtype->dtype_pcyl = update_label.dkl_pcyl; 546 cur_dtype->dtype_acyl = update_label.dkl_acyl; 547 cur_dtype->dtype_nhead = update_label.dkl_nhead; 548 cur_dtype->dtype_nsect = update_label.dkl_nsect; 549 ncyl = cur_dtype->dtype_ncyl; 550 acyl = cur_dtype->dtype_acyl; 551 pcyl = cur_dtype->dtype_pcyl; 552 nsect = cur_dtype->dtype_nsect; 553 nhead = cur_dtype->dtype_nhead; 554 } 555 return (0); 556 } 557 558 559 int 560 copy_solaris_part(struct ipart *ipart) 561 { 562 563 int status, i, fd; 564 struct mboot mboot; 565 struct ipart ip; 566 char buf[MAXPATHLEN]; 567 char *bootptr; 568 struct stat statbuf; 569 570 (void) get_pname(&buf[0]); 571 if (stat(buf, &statbuf) == -1 || 572 !S_ISCHR(statbuf.st_mode) || 573 ((cur_label == L_TYPE_EFI) && 574 (cur_disk->disk_flags & DSK_LABEL_DIRTY))) { 575 /* 576 * Make sure to reset solaris_offset to zero if it is 577 * previously set by a selected disk that 578 * supports the fdisk table. 579 */ 580 solaris_offset = 0; 581 /* 582 * Return if this disk does not support fdisk table or 583 * if it uses an EFI label but has not yet been labelled. 584 * If the EFI label has not been written then the open 585 * on the partition will fail. 586 */ 587 return (0); 588 } 589 590 if ((fd = open(buf, O_RDONLY)) < 0) { 591 err_print("Error: can't open disk '%s'.\n", buf); 592 return (-1); 593 } 594 595 status = read(fd, (caddr_t)&mboot, sizeof (struct mboot)); 596 597 if (status != sizeof (struct mboot)) { 598 err_print("Bad read of fdisk partition.\n"); 599 (void) close(fd); 600 return (-1); 601 } 602 603 for (i = 0; i < FD_NUMPART; i++) { 604 int ipc; 605 606 ipc = i * sizeof (struct ipart); 607 608 /* Handling the alignment problem of struct ipart */ 609 bootptr = &mboot.parts[ipc]; 610 (void) fill_ipart(bootptr, &ip); 611 612 if (ip.systid == SUNIXOS || 613 ip.systid == SUNIXOS2 || 614 ip.systid == EFI_PMBR) { 615 solaris_offset = lel(ip.relsect); 616 bcopy(&ip, ipart, sizeof (struct ipart)); 617 618 /* 619 * if the disk has an EFI label, we typically won't 620 * have values for nhead and nsect. format seems to 621 * work without them, and we need to protect ourselves 622 * from FPE's 623 */ 624 if (nhead != 0 && nsect != 0) { 625 pcyl = lel(ip.numsect) / (nhead * nsect); 626 ncyl = pcyl - acyl; 627 } 628 #ifdef DEBUG 629 else { 630 err_print("Critical geometry values are zero:\n" 631 "\tnhead = %d; nsect = %d\n", nhead, 632 nsect); 633 } 634 #endif /* DEBUG */ 635 636 break; 637 } 638 } 639 640 (void) close(fd); 641 return (0); 642 643 } 644 645 #if defined(_FIRMWARE_NEEDS_FDISK) 646 int 647 auto_solaris_part(struct dk_label *label) 648 { 649 650 int status, i, fd; 651 struct mboot mboot; 652 struct ipart ip; 653 char *bootptr; 654 char pbuf[MAXPATHLEN]; 655 656 657 (void) snprintf(pbuf, sizeof (pbuf), "/dev/rdsk/%sp0", x86_devname); 658 if ((fd = open_disk(pbuf, O_RDONLY)) < 0) { 659 err_print("Error: can't open selected disk '%s'.\n", pbuf); 660 return (-1); 661 } 662 663 status = read(fd, (caddr_t)&mboot, sizeof (struct mboot)); 664 665 if (status != sizeof (struct mboot)) { 666 err_print("Bad read of fdisk partition.\n"); 667 return (-1); 668 } 669 670 for (i = 0; i < FD_NUMPART; i++) { 671 int ipc; 672 673 ipc = i * sizeof (struct ipart); 674 675 /* Handling the alignment problem of struct ipart */ 676 bootptr = &mboot.parts[ipc]; 677 (void) fill_ipart(bootptr, &ip); 678 679 /* 680 * if the disk has an EFI label, the nhead and nsect fields 681 * the label may be zero. This protects us from FPE's, and 682 * format still seems to work happily 683 */ 684 if (ip.systid == SUNIXOS || 685 ip.systid == SUNIXOS2 || 686 ip.systid == EFI_PMBR) { 687 if ((label->dkl_nhead != 0) && 688 (label->dkl_nsect != 0)) { 689 label->dkl_pcyl = lel(ip.numsect) / 690 (label->dkl_nhead * label->dkl_nsect); 691 label->dkl_ncyl = label->dkl_pcyl - 692 label->dkl_acyl; 693 } 694 #ifdef DEBUG 695 else { 696 err_print("Critical label fields aren't " 697 "non-zero:\n" 698 "\tlabel->dkl_nhead = %d; " 699 "label->dkl_nsect = " 700 "%d\n", label->dkl_nhead, 701 label->dkl_nsect); 702 } 703 #endif /* DEBUG */ 704 705 solaris_offset = lel(ip.relsect); 706 break; 707 } 708 } 709 710 (void) close(fd); 711 712 return (0); 713 } 714 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 715 716 717 int 718 good_fdisk() 719 { 720 char buf[MAXPATHLEN]; 721 struct stat statbuf; 722 723 (void) get_pname(&buf[0]); 724 if (stat(buf, &statbuf) == -1 || 725 !S_ISCHR(statbuf.st_mode) || 726 cur_label == L_TYPE_EFI) { 727 /* 728 * Return if this disk does not support fdisk table or 729 * if the disk is labeled with EFI. 730 */ 731 return (1); 732 } 733 734 if (lel(cur_disk->fdisk_part.numsect) > 0) 735 return (1); 736 else 737 return (0); 738 } 739