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 the disk partitioning has changed - get the VTOC */ 471 if (status) { 472 status = ioctl(fd, DKIOCGVTOC, &cur_parts->vtoc); 473 if (status == -1) { 474 i = errno; 475 err_print("Bad ioctl DKIOCGVTOC.\n"); 476 err_print("errno=%d %s\n", i, strerror(i)); 477 err_print("Cannot read vtoc information.\n"); 478 return (-1); 479 } 480 481 status = read_label(fd, &update_label); 482 if (status == -1) { 483 err_print("Cannot read label information.\n"); 484 return (-1); 485 } 486 487 #if defined(_SUNOS_VTOC_16) 488 /* 489 * this is to update the slice table on x86 490 * we don't care about VTOC8 here 491 */ 492 for (i = 0; i < NDKMAP; i ++) { 493 cur_parts->pinfo_map[i].dkl_cylno = 494 update_label.dkl_vtoc.v_part[i].p_start / 495 ((int)(update_label.dkl_nhead * 496 update_label.dkl_nsect)); 497 cur_parts->pinfo_map[i].dkl_nblk = 498 update_label.dkl_vtoc.v_part[i].p_size; 499 } 500 #endif /* defined(_SUNOS_VTOC_16) */ 501 502 cur_dtype->dtype_ncyl = update_label.dkl_ncyl; 503 cur_dtype->dtype_pcyl = update_label.dkl_pcyl; 504 cur_dtype->dtype_acyl = update_label.dkl_acyl; 505 cur_dtype->dtype_nhead = update_label.dkl_nhead; 506 cur_dtype->dtype_nsect = update_label.dkl_nsect; 507 ncyl = cur_dtype->dtype_ncyl; 508 acyl = cur_dtype->dtype_acyl; 509 pcyl = cur_dtype->dtype_pcyl; 510 nsect = cur_dtype->dtype_nsect; 511 nhead = cur_dtype->dtype_nhead; 512 } 513 return (0); 514 } 515 516 517 int 518 copy_solaris_part(struct ipart *ipart) 519 { 520 521 int status, i, fd; 522 struct mboot mboot; 523 struct ipart ip; 524 char buf[MAXPATHLEN]; 525 char *bootptr; 526 struct stat statbuf; 527 528 (void) get_pname(&buf[0]); 529 if (stat(buf, &statbuf) == -1 || 530 !S_ISCHR(statbuf.st_mode) || 531 ((cur_label == L_TYPE_EFI) && 532 (cur_disk->disk_flags & DSK_LABEL_DIRTY))) { 533 /* 534 * Make sure to reset solaris_offset to zero if it is 535 * previously set by a selected disk that 536 * supports the fdisk table. 537 */ 538 solaris_offset = 0; 539 /* 540 * Return if this disk does not support fdisk table or 541 * if it uses an EFI label but has not yet been labelled. 542 * If the EFI label has not been written then the open 543 * on the partition will fail. 544 */ 545 return (0); 546 } 547 548 if ((fd = open(buf, O_RDONLY)) < 0) { 549 err_print("Error: can't open disk '%s'.\n", buf); 550 return (-1); 551 } 552 553 status = read(fd, (caddr_t)&mboot, sizeof (struct mboot)); 554 555 if (status != sizeof (struct mboot)) { 556 err_print("Bad read of fdisk partition.\n"); 557 (void) close(fd); 558 return (-1); 559 } 560 561 for (i = 0; i < FD_NUMPART; i++) { 562 int ipc; 563 564 ipc = i * sizeof (struct ipart); 565 566 /* Handling the alignment problem of struct ipart */ 567 bootptr = &mboot.parts[ipc]; 568 (void) fill_ipart(bootptr, &ip); 569 570 if (ip.systid == SUNIXOS || 571 ip.systid == SUNIXOS2 || 572 ip.systid == EFI_PMBR) { 573 solaris_offset = lel(ip.relsect); 574 bcopy(&ip, ipart, sizeof (struct ipart)); 575 576 /* 577 * if the disk has an EFI label, we typically won't 578 * have values for nhead and nsect. format seems to 579 * work without them, and we need to protect ourselves 580 * from FPE's 581 */ 582 if (nhead != 0 && nsect != 0) { 583 pcyl = lel(ip.numsect) / (nhead * nsect); 584 ncyl = pcyl - acyl; 585 } 586 #ifdef DEBUG 587 else { 588 err_print("Critical geometry values are zero:\n" 589 "\tnhead = %d; nsect = %d\n", nhead, 590 nsect); 591 } 592 #endif /* DEBUG */ 593 594 break; 595 } 596 } 597 598 (void) close(fd); 599 return (0); 600 601 } 602 603 #if defined(_FIRMWARE_NEEDS_FDISK) 604 int 605 auto_solaris_part(struct dk_label *label) 606 { 607 608 int status, i, fd; 609 struct mboot mboot; 610 struct ipart ip; 611 char *bootptr; 612 char pbuf[MAXPATHLEN]; 613 614 615 (void) snprintf(pbuf, sizeof (pbuf), "/dev/rdsk/%sp0", x86_devname); 616 if ((fd = open_disk(pbuf, O_RDONLY)) < 0) { 617 err_print("Error: can't open selected disk '%s'.\n", pbuf); 618 return (-1); 619 } 620 621 status = read(fd, (caddr_t)&mboot, sizeof (struct mboot)); 622 623 if (status != sizeof (struct mboot)) { 624 err_print("Bad read of fdisk partition.\n"); 625 return (-1); 626 } 627 628 for (i = 0; i < FD_NUMPART; i++) { 629 int ipc; 630 631 ipc = i * sizeof (struct ipart); 632 633 /* Handling the alignment problem of struct ipart */ 634 bootptr = &mboot.parts[ipc]; 635 (void) fill_ipart(bootptr, &ip); 636 637 /* 638 * if the disk has an EFI label, the nhead and nsect fields 639 * the label may be zero. This protects us from FPE's, and 640 * format still seems to work happily 641 */ 642 if (ip.systid == SUNIXOS || 643 ip.systid == SUNIXOS2 || 644 ip.systid == EFI_PMBR) { 645 if ((label->dkl_nhead != 0) && 646 (label->dkl_nsect != 0)) { 647 label->dkl_pcyl = lel(ip.numsect) / 648 (label->dkl_nhead * label->dkl_nsect); 649 label->dkl_ncyl = label->dkl_pcyl - 650 label->dkl_acyl; 651 } 652 #ifdef DEBUG 653 else { 654 err_print("Critical label fields aren't " 655 "non-zero:\n" 656 "\tlabel->dkl_nhead = %d; " 657 "label->dkl_nsect = " 658 "%d\n", label->dkl_nhead, 659 label->dkl_nsect); 660 } 661 #endif /* DEBUG */ 662 663 solaris_offset = lel(ip.relsect); 664 break; 665 } 666 } 667 668 (void) close(fd); 669 670 return (0); 671 } 672 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 673 674 675 int 676 good_fdisk() 677 { 678 char buf[MAXPATHLEN]; 679 struct stat statbuf; 680 681 (void) get_pname(&buf[0]); 682 if (stat(buf, &statbuf) == -1 || 683 !S_ISCHR(statbuf.st_mode) || 684 cur_label == L_TYPE_EFI) { 685 /* 686 * Return if this disk does not support fdisk table or 687 * if the disk is labeled with EFI. 688 */ 689 return (1); 690 } 691 692 if (lel(cur_disk->fdisk_part.numsect) > 0) 693 return (1); 694 else 695 return (0); 696 } 697