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 2005 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 /* 192 * Convert cn[tn]dnsn to cn[tn]dnp0 path 193 */ 194 static void 195 get_pname(char *name) 196 { 197 char buf[MAXPATHLEN]; 198 char *devp = "/dev/dsk"; 199 char *rdevp = "/dev/rdsk"; 200 char np[MAXNAMELEN]; 201 char *npt; 202 203 /* 204 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path 205 */ 206 (void) strcpy(np, cur_disk->disk_name); 207 if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 || 208 strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) { 209 /* 210 * Skip if the path is already included with pN 211 */ 212 if (strchr(np, 'p') == NULL) { 213 npt = strrchr(np, 's'); 214 /* If sN is found, do not include it */ 215 if (isdigit(*++npt)) { 216 *--npt = '\0'; 217 } 218 (void) snprintf(buf, sizeof (buf), "%sp0", np); 219 } else { 220 (void) snprintf(buf, sizeof (buf), "%s", np); 221 } 222 } else { 223 (void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np); 224 } 225 (void) strcpy(name, buf); 226 } 227 228 /* 229 * Open file descriptor for current disk (cur_file) 230 * with "p0" path or cur_disk->disk_path 231 */ 232 void 233 open_cur_file(int mode) 234 { 235 char *dkpath; 236 char pbuf[MAXPATHLEN]; 237 238 switch (mode) { 239 case FD_USE_P0_PATH: 240 (void) get_pname(&pbuf[0]); 241 dkpath = pbuf; 242 break; 243 case FD_USE_CUR_DISK_PATH: 244 dkpath = cur_disk->disk_path; 245 break; 246 default: 247 err_print("Error: Invalid mode option for opening cur_file\n"); 248 fullabort(); 249 } 250 251 /* Close previous cur_file */ 252 (void) close(cur_file); 253 /* Open cur_file with the required path dkpath */ 254 if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) { 255 err_print( 256 "Error: can't open selected disk '%s'.\n", dkpath); 257 fullabort(); 258 } 259 } 260 261 262 /* 263 * This routine implements the 'fdisk' command. It simply runs 264 * the fdisk command on the current disk. 265 * Use of this is restricted to interactive mode only. 266 */ 267 int 268 c_fdisk() 269 { 270 271 char buf[MAXPATHLEN]; 272 char pbuf[MAXPATHLEN]; 273 struct stat statbuf; 274 275 /* 276 * We must be in interactive mode to use the fdisk command 277 */ 278 if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) { 279 err_print("Fdisk command is for interactive use only!\n"); 280 return (-1); 281 } 282 283 /* 284 * There must be a current disk type and a current disk 285 */ 286 if (cur_dtype == NULL) { 287 err_print("Current Disk Type is not set.\n"); 288 return (-1); 289 } 290 291 /* 292 * If disk is larger than 1TB then an EFI label is required 293 * and there is no point in running fdisk 294 */ 295 if (cur_dtype->capacity > INFINITY) { 296 err_print("This disk must use an EFI label.\n"); 297 return (-1); 298 } 299 300 /* 301 * Before running the fdisk command, get file status of 302 * /dev/rdsk/cn[tn]dnp0 path to see if this disk 303 * supports fixed disk partition table. 304 */ 305 (void) get_pname(&pbuf[0]); 306 if (stat(pbuf, (struct stat *)&statbuf) == -1 || 307 !S_ISCHR(statbuf.st_mode)) { 308 err_print( 309 "Disk does not support fixed disk partition table\n"); 310 return (0); 311 } 312 313 /* 314 * Run the fdisk program. 315 */ 316 (void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf); 317 (void) system(buf); 318 319 /* 320 * Open cur_file with "p0" path for accessing the fdisk table 321 */ 322 (void) open_cur_file(FD_USE_P0_PATH); 323 324 /* 325 * Get solaris partition information in the fdisk partition table 326 */ 327 if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) { 328 err_print("No fdisk solaris partition found\n"); 329 cur_disk->fdisk_part.numsect = 0; /* No Solaris */ 330 } 331 332 /* 333 * Restore cur_file with cur_disk->disk_path 334 */ 335 (void) open_cur_file(FD_USE_CUR_DISK_PATH); 336 337 return (0); 338 } 339 340 /* 341 * Read MBR on the disk 342 * if the Solaris partition has changed, 343 * reread the vtoc 344 */ 345 #ifdef DEADCODE 346 static void 347 update_cur_parts() 348 { 349 350 int i; 351 register struct partition_info *parts; 352 353 for (i = 0; i < NDKMAP; i++) { 354 #if defined(_SUNOS_VTOC_16) 355 if (cur_parts->vtoc.v_part[i].p_tag && 356 cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) { 357 cur_parts->vtoc.v_part[i].p_start = 0; 358 cur_parts->vtoc.v_part[i].p_size = 0; 359 360 #endif 361 cur_parts->pinfo_map[i].dkl_nblk = 0; 362 cur_parts->pinfo_map[i].dkl_cylno = 0; 363 cur_parts->vtoc.v_part[i].p_tag = 364 default_vtoc_map[i].p_tag; 365 cur_parts->vtoc.v_part[i].p_flag = 366 default_vtoc_map[i].p_flag; 367 #if defined(_SUNOS_VTOC_16) 368 } 369 #endif 370 } 371 cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc(); 372 373 #if defined(_SUNOS_VTOC_16) 374 /* 375 * Adjust for the boot partitions 376 */ 377 cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc(); 378 cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0; 379 cur_parts->vtoc.v_part[C_PARTITION].p_start = 380 cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect; 381 cur_parts->vtoc.v_part[C_PARTITION].p_size = 382 cur_parts->pinfo_map[C_PARTITION].dkl_nblk; 383 384 cur_parts->vtoc.v_part[I_PARTITION].p_start = 385 cur_parts->pinfo_map[I_PARTITION].dkl_cylno; 386 cur_parts->vtoc.v_part[I_PARTITION].p_size = 387 cur_parts->pinfo_map[I_PARTITION].dkl_nblk; 388 389 #endif /* defined(_SUNOS_VTOC_16) */ 390 parts = cur_dtype->dtype_plist; 391 cur_dtype->dtype_ncyl = ncyl; 392 cur_dtype->dtype_plist = cur_parts; 393 parts->pinfo_name = cur_parts->pinfo_name; 394 cur_disk->disk_parts = cur_parts; 395 cur_ctype->ctype_dlist = cur_dtype; 396 397 } 398 #endif /* DEADCODE */ 399 400 static int 401 get_solaris_part(int fd, struct ipart *ipart) 402 { 403 int i; 404 struct ipart ip; 405 int status; 406 char *bootptr; 407 struct dk_label update_label; 408 409 (void) lseek(fd, 0, 0); 410 status = read(fd, (caddr_t)&boot_sec, NBPSCTR); 411 412 if (status != NBPSCTR) { 413 err_print("Bad read of fdisk partition. Status = %x\n", status); 414 err_print("Cannot read fdisk partition information.\n"); 415 return (-1); 416 } 417 418 for (i = 0; i < FD_NUMPART; i++) { 419 int ipc; 420 421 ipc = i * sizeof (struct ipart); 422 423 /* Handling the alignment problem of struct ipart */ 424 bootptr = &boot_sec.parts[ipc]; 425 (void) fill_ipart(bootptr, &ip); 426 427 /* 428 * we are interested in Solaris and EFI partition types 429 */ 430 if (ip.systid == SUNIXOS || 431 ip.systid == SUNIXOS2 || 432 ip.systid == EFI_PMBR) { 433 /* 434 * if the disk has an EFI label, nhead and nsect may 435 * be zero. This test protects us from FPE's, and 436 * format still seems to work fine 437 */ 438 if (nhead != 0 && nsect != 0) { 439 pcyl = lel(ip.numsect) / (nhead * nsect); 440 xstart = lel(ip.relsect) / (nhead * nsect); 441 ncyl = pcyl - acyl; 442 } 443 #ifdef DEBUG 444 else { 445 err_print("Critical geometry values are zero:\n" 446 "\tnhead = %d; nsect = %d\n", nhead, 447 nsect); 448 } 449 #endif /* DEBUG */ 450 451 solaris_offset = lel(ip.relsect); 452 break; 453 } 454 } 455 456 if (i == FD_NUMPART) { 457 err_print("Solaris fdisk partition not found\n"); 458 return (-1); 459 } 460 461 /* 462 * compare the previous and current Solaris partition 463 * but don't use bootid in determination of Solaris partition changes 464 */ 465 ipart->bootid = ip.bootid; 466 status = bcmp(&ip, ipart, sizeof (struct ipart)); 467 468 bcopy(&ip, ipart, sizeof (struct ipart)); 469 470 /* if it is not a Solaris partition, skip the updating */ 471 if (is_efi_type(fd)) 472 return (0); 473 474 /* 475 * if the disk partitioning has changed - get the VTOC 476 * and the partition information 477 */ 478 if (status) { 479 status = ioctl(fd, DKIOCGVTOC, &cur_parts->vtoc); 480 if (status == -1) { 481 i = errno; 482 err_print("Bad ioctl DKIOCGVTOC.\n"); 483 err_print("errno=%d %s\n", i, strerror(i)); 484 err_print("Cannot read vtoc information.\n"); 485 return (-1); 486 } 487 488 status = read_label(fd, &update_label); 489 if (status == -1) { 490 err_print("Cannot read label information.\n"); 491 return (-1); 492 } 493 for (i = 0; i < NDKMAP; i ++) { 494 cur_parts->pinfo_map[i].dkl_cylno = 495 update_label.dkl_vtoc.v_part[i].p_start / 496 ((int)(update_label.dkl_nhead * 497 update_label.dkl_nsect)); 498 cur_parts->pinfo_map[i].dkl_nblk = 499 update_label.dkl_vtoc.v_part[i].p_size; 500 } 501 cur_dtype->dtype_ncyl = update_label.dkl_ncyl; 502 cur_dtype->dtype_pcyl = update_label.dkl_pcyl; 503 cur_dtype->dtype_acyl = update_label.dkl_acyl; 504 cur_dtype->dtype_nhead = update_label.dkl_nhead; 505 cur_dtype->dtype_nsect = update_label.dkl_nsect; 506 ncyl = cur_dtype->dtype_ncyl; 507 acyl = cur_dtype->dtype_acyl; 508 pcyl = cur_dtype->dtype_pcyl; 509 nsect = cur_dtype->dtype_nsect; 510 nhead = cur_dtype->dtype_nhead; 511 } 512 return (0); 513 } 514 515 516 int 517 copy_solaris_part(struct ipart *ipart) 518 { 519 520 int status, i, fd; 521 struct mboot mboot; 522 struct ipart ip; 523 char buf[MAXPATHLEN]; 524 char *bootptr; 525 struct stat statbuf; 526 527 (void) get_pname(&buf[0]); 528 if (stat(buf, &statbuf) == -1 || 529 !S_ISCHR(statbuf.st_mode) || 530 ((cur_label == L_TYPE_EFI) && 531 (cur_disk->disk_flags & DSK_LABEL_DIRTY))) { 532 /* 533 * Make sure to reset solaris_offset to zero if it is 534 * previously set by a selected disk that 535 * supports the fdisk table. 536 */ 537 solaris_offset = 0; 538 /* 539 * Return if this disk does not support fdisk table or 540 * if it uses an EFI label but has not yet been labelled. 541 * If the EFI label has not been written then the open 542 * on the partition will fail. 543 */ 544 return (0); 545 } 546 547 if ((fd = open(buf, O_RDONLY)) < 0) { 548 err_print("Error: can't open disk '%s'.\n", buf); 549 return (-1); 550 } 551 552 status = read(fd, (caddr_t)&mboot, sizeof (struct mboot)); 553 554 if (status != sizeof (struct mboot)) { 555 err_print("Bad read of fdisk partition.\n"); 556 (void) close(fd); 557 return (-1); 558 } 559 560 for (i = 0; i < FD_NUMPART; i++) { 561 int ipc; 562 563 ipc = i * sizeof (struct ipart); 564 565 /* Handling the alignment problem of struct ipart */ 566 bootptr = &mboot.parts[ipc]; 567 (void) fill_ipart(bootptr, &ip); 568 569 if (ip.systid == SUNIXOS || 570 ip.systid == SUNIXOS2 || 571 ip.systid == EFI_PMBR) { 572 solaris_offset = lel(ip.relsect); 573 bcopy(&ip, ipart, sizeof (struct ipart)); 574 575 /* 576 * if the disk has an EFI label, we typically won't 577 * have values for nhead and nsect. format seems to 578 * work without them, and we need to protect ourselves 579 * from FPE's 580 */ 581 if (nhead != 0 && nsect != 0) { 582 pcyl = lel(ip.numsect) / (nhead * nsect); 583 ncyl = pcyl - acyl; 584 } 585 #ifdef DEBUG 586 else { 587 err_print("Critical geometry values are zero:\n" 588 "\tnhead = %d; nsect = %d\n", nhead, 589 nsect); 590 } 591 #endif /* DEBUG */ 592 593 break; 594 } 595 } 596 597 (void) close(fd); 598 return (0); 599 600 } 601 602 #if defined(_FIRMWARE_NEEDS_FDISK) 603 int 604 auto_solaris_part(struct dk_label *label) 605 { 606 607 int status, i, fd; 608 struct mboot mboot; 609 struct ipart ip; 610 char *bootptr; 611 char pbuf[MAXPATHLEN]; 612 613 614 (void) snprintf(pbuf, sizeof (pbuf), "/dev/rdsk/%sp0", x86_devname); 615 if ((fd = open_disk(pbuf, O_RDONLY)) < 0) { 616 err_print("Error: can't open selected disk '%s'.\n", pbuf); 617 return (-1); 618 } 619 620 status = read(fd, (caddr_t)&mboot, sizeof (struct mboot)); 621 622 if (status != sizeof (struct mboot)) { 623 err_print("Bad read of fdisk partition.\n"); 624 return (-1); 625 } 626 627 for (i = 0; i < FD_NUMPART; i++) { 628 int ipc; 629 630 ipc = i * sizeof (struct ipart); 631 632 /* Handling the alignment problem of struct ipart */ 633 bootptr = &mboot.parts[ipc]; 634 (void) fill_ipart(bootptr, &ip); 635 636 /* 637 * if the disk has an EFI label, the nhead and nsect fields 638 * the label may be zero. This protects us from FPE's, and 639 * format still seems to work happily 640 */ 641 if (ip.systid == SUNIXOS || 642 ip.systid == SUNIXOS2 || 643 ip.systid == EFI_PMBR) { 644 if ((label->dkl_nhead != 0) && 645 (label->dkl_nsect != 0)) { 646 label->dkl_pcyl = lel(ip.numsect) / 647 (label->dkl_nhead * label->dkl_nsect); 648 label->dkl_ncyl = label->dkl_pcyl - 649 label->dkl_acyl; 650 } 651 #ifdef DEBUG 652 else { 653 err_print("Critical label fields aren't " 654 "non-zero:\n" 655 "\tlabel->dkl_nhead = %d; " 656 "label->dkl_nsect = " 657 "%d\n", label->dkl_nhead, 658 label->dkl_nsect); 659 } 660 #endif /* DEBUG */ 661 662 solaris_offset = lel(ip.relsect); 663 break; 664 } 665 } 666 667 (void) close(fd); 668 669 return (0); 670 } 671 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 672 673 674 int 675 good_fdisk() 676 { 677 char buf[MAXPATHLEN]; 678 struct stat statbuf; 679 680 (void) get_pname(&buf[0]); 681 if (stat(buf, &statbuf) == -1 || 682 !S_ISCHR(statbuf.st_mode) || 683 cur_label == L_TYPE_EFI) { 684 /* 685 * Return if this disk does not support fdisk table or 686 * if the disk is labeled with EFI. 687 */ 688 return (1); 689 } 690 691 if (lel(cur_disk->fdisk_part.numsect) > 0) 692 return (1); 693 else 694 return (0); 695 } 696