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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * This file contains functions to implement automatic configuration 30 * of scsi disks. 31 */ 32 #include "global.h" 33 34 #include <fcntl.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <strings.h> 38 #include <stdlib.h> 39 #include <ctype.h> 40 41 #include "misc.h" 42 #include "param.h" 43 #include "ctlr_scsi.h" 44 #include "auto_sense.h" 45 #include "partition.h" 46 #include "label.h" 47 #include "startup.h" 48 #include "analyze.h" 49 #include "io.h" 50 #include "hardware_structs.h" 51 #include "menu_fdisk.h" 52 53 54 #define DISK_NAME_MAX 256 55 56 extern int nctypes; 57 extern struct ctlr_type ctlr_types[]; 58 59 60 /* 61 * Marker for free hog partition 62 */ 63 #define HOG (-1) 64 65 66 67 /* 68 * Default partition tables 69 * 70 * Disk capacity root swap usr 71 * ------------- ---- ---- --- 72 * 0mb to 64mb 0 0 remainder 73 * 64mb to 180mb 16mb 16mb remainder 74 * 180mb to 280mb 16mb 32mb remainder 75 * 280mb to 380mb 24mb 32mb remainder 76 * 380mb to 600mb 32mb 32mb remainder 77 * 600mb to 1gb 32mb 64mb remainder 78 * 1gb to 2gb 64mb 128mb remainder 79 * 2gb on up 128mb 128mb remainder 80 */ 81 struct part_table { 82 int partitions[NDKMAP]; 83 }; 84 85 static struct part_table part_table_64mb = { 86 { 0, 0, 0, 0, 0, 0, HOG, 0} 87 }; 88 89 static struct part_table part_table_180mb = { 90 { 16, 16, 0, 0, 0, 0, HOG, 0} 91 }; 92 93 static struct part_table part_table_280mb = { 94 { 16, 32, 0, 0, 0, 0, HOG, 0} 95 }; 96 97 static struct part_table part_table_380mb = { 98 { 24, 32, 0, 0, 0, 0, HOG, 0} 99 }; 100 101 static struct part_table part_table_600mb = { 102 { 32, 32, 0, 0, 0, 0, HOG, 0} 103 }; 104 105 static struct part_table part_table_1gb = { 106 { 32, 64, 0, 0, 0, 0, HOG, 0} 107 }; 108 109 static struct part_table part_table_2gb = { 110 { 64, 128, 0, 0, 0, 0, HOG, 0} 111 }; 112 113 static struct part_table part_table_infinity = { 114 { 128, 128, 0, 0, 0, 0, HOG, 0} 115 }; 116 117 118 static struct default_partitions { 119 long min_capacity; 120 long max_capacity; 121 struct part_table *part_table; 122 } default_partitions[] = { 123 { 0, 64, &part_table_64mb }, /* 0 to 64 mb */ 124 { 64, 180, &part_table_180mb }, /* 64 to 180 mb */ 125 { 180, 280, &part_table_280mb }, /* 180 to 280 mb */ 126 { 280, 380, &part_table_380mb }, /* 280 to 380 mb */ 127 { 380, 600, &part_table_600mb }, /* 380 to 600 mb */ 128 { 600, 1024, &part_table_1gb }, /* 600 to 1 gb */ 129 { 1024, 2048, &part_table_2gb }, /* 1 to 2 gb */ 130 { 2048, INFINITY, &part_table_infinity }, /* 2 gb on up */ 131 }; 132 133 #define DEFAULT_PARTITION_TABLE_SIZE \ 134 (sizeof (default_partitions) / sizeof (struct default_partitions)) 135 136 /* 137 * msgs for check() 138 */ 139 #define FORMAT_MSG "Auto configuration via format.dat" 140 #define GENERIC_MSG "Auto configuration via generic SCSI-2" 141 142 /* 143 * Disks on symbios(Hardwire raid controller) return a fixed number 144 * of heads(64)/cylinders(64) and adjust the cylinders depending 145 * capacity of the configured lun. 146 * In such a case we get number of physical cylinders < 3 which 147 * is the minimum required by solaris(2 reserved + 1 data cylinders). 148 * Hence try to adjust the cylinders by reducing the "nsect/nhead". 149 * 150 */ 151 /* 152 * assuming a minimum of 32 block cylinders. 153 */ 154 #define MINIMUM_NO_HEADS 2 155 #define MINIMUM_NO_SECTORS 16 156 157 #define MINIMUM_NO_CYLINDERS 128 158 159 #if defined(_SUNOS_VTOC_8) 160 161 /* These are 16-bit fields */ 162 #define MAXIMUM_NO_HEADS 65535 163 #define MAXIMUM_NO_SECTORS 65535 164 #define MAXIMUM_NO_CYLINDERS 65535 165 166 #endif /* defined(_SUNOS_VTOC_8) */ 167 168 /* 169 * minimum number of cylinders required by Solaris. 170 */ 171 #define SUN_MIN_CYL 3 172 173 174 175 /* 176 * ANSI prototypes for local static functions 177 */ 178 static struct disk_type *generic_disk_sense( 179 int fd, 180 int can_prompt, 181 struct dk_label *label, 182 struct scsi_inquiry *inquiry, 183 struct scsi_capacity_16 *capacity, 184 char *disk_name); 185 static int use_existing_disk_type( 186 int fd, 187 int can_prompt, 188 struct dk_label *label, 189 struct scsi_inquiry *inquiry, 190 struct disk_type *disk_type, 191 struct scsi_capacity_16 *capacity); 192 int build_default_partition(struct dk_label *label, 193 int ctrl_type); 194 static struct disk_type *find_scsi_disk_type( 195 char *disk_name, 196 struct dk_label *label); 197 static struct disk_type *find_scsi_disk_by_name( 198 char *disk_name); 199 static struct ctlr_type *find_scsi_ctlr_type(void); 200 static struct ctlr_info *find_scsi_ctlr_info( 201 struct dk_cinfo *dkinfo); 202 static struct disk_type *new_scsi_disk_type( 203 int fd, 204 char *disk_name, 205 struct dk_label *label); 206 static struct disk_info *find_scsi_disk_info( 207 struct dk_cinfo *dkinfo); 208 209 static struct disk_type *new_direct_disk_type(int fd, char *disk_name, 210 struct dk_label *label); 211 212 static struct disk_info *find_direct_disk_info(struct dk_cinfo *dkinfo); 213 static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 214 static int auto_label_init(struct dk_label *label); 215 static struct ctlr_type *find_direct_ctlr_type(void); 216 static struct ctlr_info *find_direct_ctlr_info(struct dk_cinfo *dkinfo); 217 static struct disk_info *find_direct_disk_info(struct dk_cinfo *dkinfo); 218 219 static char *get_sun_disk_name( 220 char *disk_name, 221 struct scsi_inquiry *inquiry); 222 static char *get_generic_disk_name( 223 char *disk_name, 224 struct scsi_inquiry *inquiry); 225 static int force_blocksize(int fd); 226 static int raw_format(int fd); 227 static char *strcopy( 228 char *dst, 229 char *src, 230 int n); 231 static int adjust_disk_geometry(int capacity, int *cyl, 232 int *nsect, int *nhead); 233 #if defined(_SUNOS_VTOC_8) 234 static int square_box( 235 int capacity, 236 int *dim1, int lim1, 237 int *dim2, int lim2, 238 int *dim3, int lim3); 239 #endif /* defined(_SUNOS_VTOC_8) */ 240 241 242 /* 243 * We need to get information necessary to construct a *new* efi 244 * label type 245 */ 246 struct disk_type * 247 auto_efi_sense(int fd, struct efi_info *label) 248 { 249 250 struct dk_gpt *vtoc; 251 int i; 252 253 struct disk_type *disk, *dp; 254 struct disk_info *disk_info; 255 struct ctlr_info *ctlr; 256 struct dk_cinfo dkinfo; 257 struct partition_info *part; 258 259 /* 260 * get vendor, product, revision and capacity info. 261 */ 262 if (get_disk_info(fd, label) == -1) { 263 return ((struct disk_type *)NULL); 264 } 265 /* 266 * Now build the default partition table 267 */ 268 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 269 err_print("efi_alloc_and_init failed. \n"); 270 return ((struct disk_type *)NULL); 271 } 272 273 label->e_parts = vtoc; 274 275 for (i = 0; i < min(vtoc->efi_nparts, V_NUMPAR); i++) { 276 vtoc->efi_parts[i].p_tag = default_vtoc_map[i].p_tag; 277 vtoc->efi_parts[i].p_flag = default_vtoc_map[i].p_flag; 278 vtoc->efi_parts[i].p_start = 0; 279 vtoc->efi_parts[i].p_size = 0; 280 } 281 /* 282 * Make constants first 283 * and variable partitions later 284 */ 285 286 /* root partition - s0 128 MB */ 287 vtoc->efi_parts[0].p_start = 34; 288 vtoc->efi_parts[0].p_size = 262144; 289 290 /* partition - s1 128 MB */ 291 vtoc->efi_parts[1].p_start = 262178; 292 vtoc->efi_parts[1].p_size = 262144; 293 294 /* partition -s2 is NOT the Backup disk */ 295 vtoc->efi_parts[2].p_tag = V_UNASSIGNED; 296 297 /* partition -s6 /usr partition - HOG */ 298 vtoc->efi_parts[6].p_start = 524322; 299 vtoc->efi_parts[6].p_size = vtoc->efi_last_u_lba - 524322 300 - (1024 * 16); 301 302 /* efi reserved partition - s9 16K */ 303 vtoc->efi_parts[8].p_start = vtoc->efi_last_u_lba - (1024 * 16); 304 vtoc->efi_parts[8].p_size = (1024 * 16); 305 vtoc->efi_parts[8].p_tag = V_RESERVED; 306 /* 307 * Now stick all of it into the disk_type struct 308 */ 309 310 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { 311 if (option_msg && diag_msg) { 312 err_print("DKIOCINFO failed\n"); 313 } 314 return (NULL); 315 } 316 if ((cur_ctype != NULL) && (cur_ctype->ctype_ctype == DKC_DIRECT)) { 317 ctlr = find_direct_ctlr_info(&dkinfo); 318 disk_info = find_direct_disk_info(&dkinfo); 319 } else { 320 ctlr = find_scsi_ctlr_info(&dkinfo); 321 disk_info = find_scsi_disk_info(&dkinfo); 322 } 323 disk = (struct disk_type *)zalloc(sizeof (struct disk_type)); 324 assert(disk_info->disk_ctlr == ctlr); 325 dp = ctlr->ctlr_ctype->ctype_dlist; 326 if (dp == NULL) { 327 ctlr->ctlr_ctype->ctype_dlist = dp; 328 } else { 329 while (dp->dtype_next != NULL) { 330 dp = dp->dtype_next; 331 } 332 dp->dtype_next = disk; 333 } 334 disk->dtype_next = NULL; 335 336 (void) strlcpy(disk->vendor, label->vendor, 337 sizeof (disk->vendor)); 338 (void) strlcpy(disk->product, label->product, 339 sizeof (disk->product)); 340 (void) strlcpy(disk->revision, label->revision, 341 sizeof (disk->revision)); 342 disk->capacity = label->capacity; 343 344 part = (struct partition_info *) 345 zalloc(sizeof (struct partition_info)); 346 disk->dtype_plist = part; 347 348 part->pinfo_name = alloc_string("default"); 349 part->pinfo_next = NULL; 350 part->etoc = vtoc; 351 352 bzero(disk_info->v_volume, LEN_DKL_VVOL); 353 disk_info->disk_parts = part; 354 return (disk); 355 } 356 357 static int 358 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 359 { 360 void *data = dk_ioc->dki_data; 361 int error; 362 363 dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data; 364 error = ioctl(fd, cmd, (void *)dk_ioc); 365 dk_ioc->dki_data = data; 366 367 return (error); 368 } 369 370 static struct ctlr_type * 371 find_direct_ctlr_type() 372 { 373 struct mctlr_list *mlp; 374 375 mlp = controlp; 376 377 while (mlp != NULL) { 378 if (mlp->ctlr_type->ctype_ctype == DKC_DIRECT) { 379 return (mlp->ctlr_type); 380 } 381 mlp = mlp->next; 382 } 383 384 impossible("no DIRECT controller type"); 385 386 return ((struct ctlr_type *)NULL); 387 } 388 389 static struct ctlr_info * 390 find_direct_ctlr_info( 391 struct dk_cinfo *dkinfo) 392 { 393 struct ctlr_info *ctlr; 394 395 if (dkinfo->dki_ctype != DKC_DIRECT) 396 return (NULL); 397 398 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) { 399 if (ctlr->ctlr_addr == dkinfo->dki_addr && 400 ctlr->ctlr_space == dkinfo->dki_space && 401 ctlr->ctlr_ctype->ctype_ctype == DKC_DIRECT) { 402 return (ctlr); 403 } 404 } 405 406 impossible("no DIRECT controller info"); 407 /*NOTREACHED*/ 408 } 409 410 static struct disk_info * 411 find_direct_disk_info( 412 struct dk_cinfo *dkinfo) 413 { 414 struct disk_info *disk; 415 struct dk_cinfo *dp; 416 417 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 418 assert(dkinfo->dki_ctype == DKC_DIRECT); 419 dp = &disk->disk_dkinfo; 420 if (dp->dki_ctype == dkinfo->dki_ctype && 421 dp->dki_cnum == dkinfo->dki_cnum && 422 dp->dki_unit == dkinfo->dki_unit && 423 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) { 424 return (disk); 425 } 426 } 427 428 impossible("No DIRECT disk info instance\n"); 429 /*NOTREACHED*/ 430 } 431 432 /* 433 * To convert EFI to SMI labels, we need to get label geometry. 434 * Unfortunately at this time there is no good way to do so. 435 * DKIOCGGEOM will fail if disk is EFI labeled. So we hack around 436 * it and clear EFI label, do a DKIOCGGEOM and put the EFI label 437 * back on disk. 438 * This routine gets the label geometry and initializes the label 439 * It uses cur_file as opened device. 440 * returns 0 if succeeds or -1 if failed. 441 */ 442 static int 443 auto_label_init(struct dk_label *label) 444 { 445 dk_efi_t dk_ioc; 446 dk_efi_t dk_ioc_back; 447 efi_gpt_t *data = NULL; 448 efi_gpt_t *databack = NULL; 449 struct dk_geom disk_geom; 450 struct dk_minfo disk_info; 451 efi_gpt_t *backsigp; 452 int fd = cur_file; 453 int rval = -1; 454 int efisize = EFI_LABEL_SIZE * 2; 455 int success = 0; 456 uint64_t sig; 457 uint64_t backsig; 458 459 if ((data = calloc(efisize, 1)) == NULL) { 460 err_print("auto_label_init: calloc failed\n"); 461 goto auto_label_init_out; 462 } 463 464 dk_ioc.dki_data = data; 465 dk_ioc.dki_lba = 1; 466 dk_ioc.dki_length = efisize; 467 468 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) != 0) { 469 err_print("auto_label_init: GETEFI failed\n"); 470 goto auto_label_init_out; 471 } 472 473 if ((databack = calloc(efisize, 1)) == NULL) { 474 err_print("auto_label_init calloc2 failed"); 475 goto auto_label_init_out; 476 } 477 478 /* get the LBA size and capacity */ 479 if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) { 480 err_print("auto_label_init: dkiocgmediainfo failed\n"); 481 goto auto_label_init_out; 482 } 483 484 if (disk_info.dki_lbsize == 0) { 485 if (option_msg && diag_msg) { 486 err_print("auto_lbal_init: assuming 512 byte" 487 "block size"); 488 } 489 disk_info.dki_lbsize = DEV_BSIZE; 490 } 491 492 if (disk_info.dki_lbsize != DEV_BSIZE) { 493 err_print("auto_label_init: lbasize is not 512\n"); 494 goto auto_label_init_out; 495 } 496 497 dk_ioc_back.dki_data = databack; 498 499 /* 500 * back up efi label goes to capacity - 1, we are reading an extra block 501 * before the back up label. 502 */ 503 dk_ioc_back.dki_lba = disk_info.dki_capacity - 1 - 1; 504 dk_ioc_back.dki_length = efisize; 505 506 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc_back) != 0) { 507 err_print("auto_label_init: GETEFI backup failed\n"); 508 goto auto_label_init_out; 509 } 510 511 sig = dk_ioc.dki_data->efi_gpt_Signature; 512 dk_ioc.dki_data->efi_gpt_Signature = 0x0; 513 514 enter_critical(); 515 516 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) { 517 err_print("auto_label_init: SETEFI failed\n"); 518 exit_critical(); 519 goto auto_label_init_out; 520 } 521 522 backsigp = (efi_gpt_t *)((uintptr_t)dk_ioc_back.dki_data + DEV_BSIZE); 523 524 backsig = backsigp->efi_gpt_Signature; 525 526 backsigp->efi_gpt_Signature = 0; 527 528 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc_back) == -1) { 529 err_print("auto_label_init: SETEFI backup failed\n"); 530 } 531 532 if (ioctl(cur_file, DKIOCGGEOM, &disk_geom) != 0) 533 err_print("auto_label_init: GGEOM failed\n"); 534 else 535 success = 1; 536 537 dk_ioc.dki_data->efi_gpt_Signature = sig; 538 backsigp->efi_gpt_Signature = backsig; 539 540 if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc_back) == -1) { 541 err_print("auto_label_init: SETEFI revert backup failed\n"); 542 success = 0; 543 } 544 545 if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc) == -1) { 546 err_print("auto_label_init: SETEFI revert failed\n"); 547 success = 0; 548 } 549 550 exit_critical(); 551 552 if (success == 0) 553 goto auto_label_init_out; 554 555 ncyl = disk_geom.dkg_ncyl; 556 acyl = disk_geom.dkg_acyl; 557 nhead = disk_geom.dkg_nhead; 558 nsect = disk_geom.dkg_nsect; 559 pcyl = ncyl + acyl; 560 561 label->dkl_pcyl = pcyl; 562 label->dkl_ncyl = ncyl; 563 label->dkl_acyl = acyl; 564 label->dkl_nhead = nhead; 565 label->dkl_nsect = nsect; 566 label->dkl_apc = 0; 567 label->dkl_intrlv = 1; 568 label->dkl_rpm = disk_geom.dkg_rpm; 569 570 label->dkl_magic = DKL_MAGIC; 571 572 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel), 573 "%s cyl %d alt %d hd %d sec %d", 574 "DEFAULT", ncyl, acyl, nhead, nsect); 575 576 rval = 0; 577 #if defined(_FIRMWARE_NEEDS_FDISK) 578 (void) auto_solaris_part(label); 579 ncyl = label->dkl_ncyl; 580 581 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 582 583 if (!build_default_partition(label, DKC_DIRECT)) { 584 rval = -1; 585 } 586 587 (void) checksum(label, CK_MAKESUM); 588 589 590 auto_label_init_out: 591 if (data) 592 free(data); 593 if (databack) 594 free(databack); 595 596 return (rval); 597 } 598 599 static struct disk_type * 600 new_direct_disk_type( 601 int fd, 602 char *disk_name, 603 struct dk_label *label) 604 { 605 struct disk_type *dp; 606 struct disk_type *disk; 607 struct ctlr_info *ctlr; 608 struct dk_cinfo dkinfo; 609 struct partition_info *part = NULL; 610 struct partition_info *pt; 611 struct disk_info *disk_info; 612 int i; 613 614 /* 615 * Get the disk controller info for this disk 616 */ 617 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { 618 if (option_msg && diag_msg) { 619 err_print("DKIOCINFO failed\n"); 620 } 621 return (NULL); 622 } 623 624 /* 625 * Find the ctlr_info for this disk. 626 */ 627 ctlr = find_direct_ctlr_info(&dkinfo); 628 629 /* 630 * Allocate a new disk type for the direct controller. 631 */ 632 disk = (struct disk_type *)zalloc(sizeof (struct disk_type)); 633 634 /* 635 * Find the disk_info instance for this disk. 636 */ 637 disk_info = find_direct_disk_info(&dkinfo); 638 639 /* 640 * The controller and the disk should match. 641 */ 642 assert(disk_info->disk_ctlr == ctlr); 643 644 /* 645 * Link the disk into the list of disks 646 */ 647 dp = ctlr->ctlr_ctype->ctype_dlist; 648 if (dp == NULL) { 649 ctlr->ctlr_ctype->ctype_dlist = dp; 650 } else { 651 while (dp->dtype_next != NULL) { 652 dp = dp->dtype_next; 653 } 654 dp->dtype_next = disk; 655 } 656 disk->dtype_next = NULL; 657 658 /* 659 * Allocate and initialize the disk name. 660 */ 661 disk->dtype_asciilabel = alloc_string(disk_name); 662 663 /* 664 * Initialize disk geometry info 665 */ 666 disk->dtype_pcyl = label->dkl_pcyl; 667 disk->dtype_ncyl = label->dkl_ncyl; 668 disk->dtype_acyl = label->dkl_acyl; 669 disk->dtype_nhead = label->dkl_nhead; 670 disk->dtype_nsect = label->dkl_nsect; 671 disk->dtype_rpm = label->dkl_rpm; 672 673 part = (struct partition_info *) 674 zalloc(sizeof (struct partition_info)); 675 pt = disk->dtype_plist; 676 if (pt == NULL) { 677 disk->dtype_plist = part; 678 } else { 679 while (pt->pinfo_next != NULL) { 680 pt = pt->pinfo_next; 681 } 682 pt->pinfo_next = part; 683 } 684 685 part->pinfo_next = NULL; 686 687 /* 688 * Set up the partition name 689 */ 690 part->pinfo_name = alloc_string("default"); 691 692 /* 693 * Fill in the partition info from the label 694 */ 695 for (i = 0; i < NDKMAP; i++) { 696 697 #if defined(_SUNOS_VTOC_8) 698 part->pinfo_map[i] = label->dkl_map[i]; 699 700 #elif defined(_SUNOS_VTOC_16) 701 part->pinfo_map[i].dkl_cylno = 702 label->dkl_vtoc.v_part[i].p_start / 703 ((int)(disk->dtype_nhead * 704 disk->dtype_nsect - apc)); 705 part->pinfo_map[i].dkl_nblk = 706 label->dkl_vtoc.v_part[i].p_size; 707 #else 708 #error No VTOC format defined. 709 #endif /* defined(_SUNOS_VTOC_8) */ 710 } 711 712 /* 713 * Use the VTOC if valid, or install a default 714 */ 715 if (label->dkl_vtoc.v_version == V_VERSION) { 716 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume, 717 LEN_DKL_VVOL); 718 part->vtoc = label->dkl_vtoc; 719 } else { 720 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL); 721 set_vtoc_defaults(part); 722 } 723 724 /* 725 * Link the disk to the partition map 726 */ 727 disk_info->disk_parts = part; 728 729 return (disk); 730 } 731 732 /* 733 * Get a disk type that has label info. This is used to convert 734 * EFI label to SMI label 735 */ 736 struct disk_type * 737 auto_direct_get_geom_label(int fd, struct dk_label *label) 738 { 739 struct disk_type *disk_type; 740 741 if (auto_label_init(label) != 0) { 742 err_print("auto_direct_get_geom_label: failed to get label" 743 "geometry"); 744 return (NULL); 745 } else { 746 disk_type = new_direct_disk_type(fd, "DEFAULT", label); 747 return (disk_type); 748 } 749 } 750 751 /* 752 * Auto-sense a scsi disk configuration, ie get the information 753 * necessary to construct a label. We have two different 754 * ways to auto-sense a scsi disk: 755 * - format.dat override, via inquiry name 756 * - generic scsi, via standard mode sense and inquiry 757 * Depending on how and when we are called, and/or 758 * change geometry and reformat. 759 */ 760 struct disk_type * 761 auto_sense( 762 int fd, 763 int can_prompt, 764 struct dk_label *label) 765 { 766 struct scsi_inquiry inquiry; 767 struct scsi_capacity_16 capacity; 768 struct disk_type *disk_type; 769 char disk_name[DISK_NAME_MAX]; 770 int force_format_dat = 0; 771 int force_generic = 0; 772 u_ioparam_t ioparam; 773 int deflt; 774 775 /* 776 * First, if expert mode, find out if the user 777 * wants to override any of the standard methods. 778 */ 779 if (can_prompt && expert_mode) { 780 deflt = 1; 781 ioparam.io_charlist = confirm_list; 782 if (input(FIO_MSTR, FORMAT_MSG, '?', &ioparam, 783 &deflt, DATA_INPUT) == 0) { 784 force_format_dat = 1; 785 } else if (input(FIO_MSTR, GENERIC_MSG, '?', &ioparam, 786 &deflt, DATA_INPUT) == 0) { 787 force_generic = 1; 788 } 789 } 790 791 /* 792 * Get the Inquiry data. If this fails, there's 793 * no hope for this disk, so give up. 794 */ 795 if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) { 796 return ((struct disk_type *)NULL); 797 } 798 if (option_msg && diag_msg) { 799 err_print("Product id: "); 800 print_buf(inquiry.inq_pid, sizeof (inquiry.inq_pid)); 801 err_print("\n"); 802 } 803 804 /* 805 * Get the Read Capacity 806 */ 807 if (uscsi_read_capacity(fd, &capacity)) { 808 return ((struct disk_type *)NULL); 809 } 810 if (option_msg && diag_msg) { 811 err_print("blocks: %llu (0x%llx)\n", 812 capacity.sc_capacity, capacity.sc_capacity); 813 err_print("blksize: %u\n", capacity.sc_lbasize); 814 } 815 816 /* 817 * Extract the disk name for the format.dat override 818 */ 819 (void) get_sun_disk_name(disk_name, &inquiry); 820 if (option_msg && diag_msg) { 821 err_print("disk name: `%s`\n", disk_name); 822 } 823 824 /* 825 * Figure out which method we use for auto sense. 826 * If a particular method fails, we fall back to 827 * the next possibility. 828 */ 829 830 if (force_generic) { 831 return (generic_disk_sense(fd, can_prompt, label, 832 &inquiry, &capacity, disk_name)); 833 } 834 835 /* 836 * Try for an existing format.dat first 837 */ 838 if ((disk_type = find_scsi_disk_by_name(disk_name)) != NULL) { 839 if (use_existing_disk_type(fd, can_prompt, label, 840 &inquiry, disk_type, &capacity)) { 841 return (disk_type); 842 } 843 if (force_format_dat) { 844 return (NULL); 845 } 846 } 847 848 /* 849 * Otherwise, try using generic SCSI-2 sense and inquiry. 850 */ 851 852 return (generic_disk_sense(fd, can_prompt, label, 853 &inquiry, &capacity, disk_name)); 854 } 855 856 857 858 /*ARGSUSED*/ 859 static struct disk_type * 860 generic_disk_sense( 861 int fd, 862 int can_prompt, 863 struct dk_label *label, 864 struct scsi_inquiry *inquiry, 865 struct scsi_capacity_16 *capacity, 866 char *disk_name) 867 { 868 struct disk_type *disk; 869 int pcyl; 870 int ncyl; 871 int acyl; 872 int nhead; 873 int nsect; 874 int rpm; 875 long nblocks; 876 union { 877 struct mode_format page3; 878 uchar_t buf3[MAX_MODE_SENSE_SIZE]; 879 } u_page3; 880 union { 881 struct mode_geometry page4; 882 uchar_t buf4[MAX_MODE_SENSE_SIZE]; 883 } u_page4; 884 struct scsi_capacity_16 new_capacity; 885 struct mode_format *page3 = &u_page3.page3; 886 struct mode_geometry *page4 = &u_page4.page4; 887 struct scsi_ms_header header; 888 889 /* 890 * If the name of this disk appears to be "SUN", use it, 891 * otherwise construct a name out of the generic 892 * Inquiry info. If it turns out that we already 893 * have a SUN disk type of this name that differs 894 * in geometry, we will revert to the generic name 895 * anyway. 896 */ 897 if (memcmp(disk_name, "SUN", strlen("SUN")) != 0) { 898 (void) get_generic_disk_name(disk_name, inquiry); 899 } 900 901 /* 902 * If the device's block size is not 512, we have to 903 * change block size, reformat, and then sense the 904 * geometry. To do this, we must be able to prompt 905 * the user. 906 */ 907 if (capacity->sc_lbasize != DEV_BSIZE) { 908 if (!can_prompt) { 909 return (NULL); 910 } 911 if (force_blocksize(fd)) { 912 goto err; 913 } 914 915 /* 916 * Get the capacity again, since this has changed 917 */ 918 if (uscsi_read_capacity(fd, &new_capacity)) { 919 goto err; 920 } 921 if (option_msg && diag_msg) { 922 err_print("blocks: %llu (0x%llx)\n", 923 new_capacity.sc_capacity, 924 new_capacity.sc_capacity); 925 err_print("blksize: %u\n", new_capacity.sc_lbasize); 926 } 927 capacity = &new_capacity; 928 if (capacity->sc_lbasize != DEV_BSIZE) { 929 goto err; 930 } 931 } 932 933 /* 934 * Get current Page 3 - Format Parameters page 935 */ 936 if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, MODE_SENSE_PC_CURRENT, 937 (caddr_t)&u_page3, MAX_MODE_SENSE_SIZE, &header)) { 938 goto err; 939 } 940 941 /* 942 * Get current Page 4 - Drive Geometry page 943 */ 944 if (uscsi_mode_sense(fd, DAD_MODE_GEOMETRY, MODE_SENSE_PC_CURRENT, 945 (caddr_t)&u_page4, MAX_MODE_SENSE_SIZE, &header)) { 946 goto err; 947 } 948 949 /* 950 * Correct for byte order if necessary 951 */ 952 page4->rpm = BE_16(page4->rpm); 953 page4->step_rate = BE_16(page4->step_rate); 954 page3->tracks_per_zone = BE_16(page3->tracks_per_zone); 955 page3->alt_sect_zone = BE_16(page3->alt_sect_zone); 956 page3->alt_tracks_zone = BE_16(page3->alt_tracks_zone); 957 page3->alt_tracks_vol = BE_16(page3->alt_tracks_vol); 958 page3->sect_track = BE_16(page3->sect_track); 959 page3->data_bytes_sect = BE_16(page3->data_bytes_sect); 960 page3->interleave = BE_16(page3->interleave); 961 page3->track_skew = BE_16(page3->track_skew); 962 page3->cylinder_skew = BE_16(page3->cylinder_skew); 963 964 965 /* 966 * Construct a new label out of the sense data, 967 * Inquiry and Capacity. 968 */ 969 pcyl = (page4->cyl_ub << 16) + (page4->cyl_mb << 8) + page4->cyl_lb; 970 nhead = page4->heads; 971 nsect = page3->sect_track; 972 rpm = page4->rpm; 973 974 /* 975 * If the number of physical cylinders reported is less 976 * the SUN_MIN_CYL(3) then try to adjust the geometry so that 977 * we have atleast SUN_MIN_CYL cylinders. 978 */ 979 if (pcyl < SUN_MIN_CYL) { 980 if (adjust_disk_geometry((int)(capacity->sc_capacity + 1), 981 &pcyl, &nhead, &nsect)) { 982 goto err; 983 } 984 } 985 986 /* 987 * The sd driver reserves 2 cylinders the backup disk label and 988 * the deviceid. Set the number of data cylinders to pcyl-acyl. 989 */ 990 acyl = DK_ACYL; 991 ncyl = pcyl - acyl; 992 993 if (option_msg && diag_msg) { 994 err_print("Geometry:\n"); 995 err_print(" pcyl: %d\n", pcyl); 996 err_print(" ncyl: %d\n", ncyl); 997 err_print(" heads: %d\n", nhead); 998 err_print(" nsects: %d\n", nsect); 999 err_print(" acyl: %d\n", acyl); 1000 1001 #if defined(_SUNOS_VTOC_16) 1002 err_print(" bcyl: %d\n", bcyl); 1003 #endif /* defined(_SUNOS_VTOC_16) */ 1004 1005 err_print(" rpm: %d\n", rpm); 1006 } 1007 1008 /* 1009 * Some drives report 0 for page4->rpm, adjust it to AVG_RPM, 3600. 1010 */ 1011 if (rpm < MIN_RPM || rpm > MAX_RPM) { 1012 err_print("Mode sense page(4) reports rpm value as %d," 1013 " adjusting it to %d\n", rpm, AVG_RPM); 1014 rpm = AVG_RPM; 1015 } 1016 1017 /* 1018 * Get the number of blocks from Read Capacity data. Note that 1019 * the logical block address range from 0 to capacity->sc_capacity. 1020 */ 1021 nblocks = (long)(capacity->sc_capacity + 1); 1022 1023 /* 1024 * Some drives report 0 for nsect (page 3, byte 10 and 11) if they 1025 * have variable number of sectors per track. So adjust nsect. 1026 * Also the value is defined as vendor specific, hence check if 1027 * it is in a tolerable range. The values (32 and 4 below) are 1028 * chosen so that this change below does not generate a different 1029 * geometry for currently supported sun disks. 1030 */ 1031 if ((nsect <= 0) || 1032 (pcyl * nhead * nsect) < (nblocks - nblocks/32) || 1033 (pcyl * nhead * nsect) > (nblocks + nblocks/4)) { 1034 err_print("Mode sense page(3) reports nsect value as %d, " 1035 "adjusting it to %ld\n", nsect, nblocks / (pcyl * nhead)); 1036 nsect = nblocks / (pcyl * nhead); 1037 } 1038 1039 /* 1040 * Some drives report their physical geometry such that 1041 * it is greater than the actual capacity. Adjust the 1042 * geometry to allow for this, so we don't run off 1043 * the end of the disk. 1044 */ 1045 if ((pcyl * nhead * nsect) > nblocks) { 1046 int p = pcyl; 1047 if (option_msg && diag_msg) { 1048 err_print("Computed capacity (%ld) exceeds actual " 1049 "disk capacity (%ld)\n", 1050 (long)(pcyl * nhead * nsect), nblocks); 1051 } 1052 do { 1053 pcyl--; 1054 } while ((pcyl * nhead * nsect) > nblocks); 1055 1056 if (can_prompt && expert_mode && !option_f) { 1057 /* 1058 * Try to adjust nsect instead of pcyl to see if we 1059 * can optimize. For compatability reasons do this 1060 * only in expert mode (refer to bug 1144812). 1061 */ 1062 int n = nsect; 1063 do { 1064 n--; 1065 } while ((p * nhead * n) > nblocks); 1066 if ((p * nhead * n) > (pcyl * nhead * nsect)) { 1067 u_ioparam_t ioparam; 1068 int deflt = 1; 1069 /* 1070 * Ask the user for a choice here. 1071 */ 1072 ioparam.io_bounds.lower = 1; 1073 ioparam.io_bounds.upper = 2; 1074 err_print("1. Capacity = %d, with pcyl = %d " 1075 "nhead = %d nsect = %d\n", 1076 (pcyl * nhead * nsect), 1077 pcyl, nhead, nsect); 1078 err_print("2. Capacity = %d, with pcyl = %d " 1079 "nhead = %d nsect = %d\n", 1080 (p * nhead * n), 1081 p, nhead, n); 1082 if (input(FIO_INT, "Select one of the above " 1083 "choices ", ':', &ioparam, 1084 &deflt, DATA_INPUT) == 2) { 1085 pcyl = p; 1086 nsect = n; 1087 } 1088 } 1089 } 1090 } 1091 1092 #if defined(_SUNOS_VTOC_8) 1093 /* 1094 * Finally, we need to make sure we don't overflow any of the 1095 * fields in our disk label. To do this we need to `square 1096 * the box' so to speak. We will lose bits here. 1097 */ 1098 1099 if ((pcyl > MAXIMUM_NO_CYLINDERS && 1100 ((nsect > MAXIMUM_NO_SECTORS) || 1101 (nhead > MAXIMUM_NO_HEADS))) || 1102 ((nsect > MAXIMUM_NO_SECTORS) && 1103 (nhead > MAXIMUM_NO_HEADS))) { 1104 err_print("This disk is too big to label. " 1105 " You will lose some blocks.\n"); 1106 } 1107 if ((pcyl > MAXIMUM_NO_CYLINDERS) || 1108 (nsect > MAXIMUM_NO_SECTORS) || 1109 (nhead > MAXIMUM_NO_HEADS)) { 1110 u_ioparam_t ioparam; 1111 int order; 1112 char msg[256]; 1113 1114 order = ((ncyl > nhead)<<2) | 1115 ((ncyl > nsect)<<1) | 1116 (nhead > nsect); 1117 switch (order) { 1118 case 0x7: /* ncyl > nhead > nsect */ 1119 nblocks = 1120 square_box(nblocks, 1121 &pcyl, MAXIMUM_NO_CYLINDERS, 1122 &nhead, MAXIMUM_NO_HEADS, 1123 &nsect, MAXIMUM_NO_SECTORS); 1124 break; 1125 case 0x6: /* ncyl > nsect > nhead */ 1126 nblocks = 1127 square_box(nblocks, 1128 &pcyl, MAXIMUM_NO_CYLINDERS, 1129 &nsect, MAXIMUM_NO_SECTORS, 1130 &nhead, MAXIMUM_NO_HEADS); 1131 break; 1132 case 0x4: /* nsect > ncyl > nhead */ 1133 nblocks = 1134 square_box(nblocks, 1135 &nsect, MAXIMUM_NO_SECTORS, 1136 &pcyl, MAXIMUM_NO_CYLINDERS, 1137 &nhead, MAXIMUM_NO_HEADS); 1138 break; 1139 case 0x0: /* nsect > nhead > ncyl */ 1140 nblocks = 1141 square_box(nblocks, 1142 &nsect, MAXIMUM_NO_SECTORS, 1143 &nhead, MAXIMUM_NO_HEADS, 1144 &pcyl, MAXIMUM_NO_CYLINDERS); 1145 break; 1146 case 0x3: /* nhead > ncyl > nsect */ 1147 nblocks = 1148 square_box(nblocks, 1149 &nhead, MAXIMUM_NO_HEADS, 1150 &pcyl, MAXIMUM_NO_CYLINDERS, 1151 &nsect, MAXIMUM_NO_SECTORS); 1152 break; 1153 case 0x1: /* nhead > nsect > ncyl */ 1154 nblocks = 1155 square_box(nblocks, 1156 &nhead, MAXIMUM_NO_HEADS, 1157 &nsect, MAXIMUM_NO_SECTORS, 1158 &pcyl, MAXIMUM_NO_CYLINDERS); 1159 break; 1160 default: 1161 /* How did we get here? */ 1162 impossible("label overflow adjustment"); 1163 1164 /* Do something useful */ 1165 nblocks = 1166 square_box(nblocks, 1167 &nhead, MAXIMUM_NO_HEADS, 1168 &nsect, MAXIMUM_NO_SECTORS, 1169 &pcyl, MAXIMUM_NO_CYLINDERS); 1170 break; 1171 } 1172 if (option_msg && diag_msg && 1173 (capacity->sc_capacity + 1 != nblocks)) { 1174 err_print("After adjusting geometry you lost" 1175 " %llu of %lld blocks.\n", 1176 (capacity->sc_capacity + 1 - nblocks), 1177 capacity->sc_capacity + 1); 1178 } 1179 while (can_prompt && expert_mode && !option_f) { 1180 int deflt = 1; 1181 1182 /* 1183 * Allow user to modify this by hand if desired. 1184 */ 1185 (void) sprintf(msg, 1186 "\nGeometry: %d heads, %d sectors %d " 1187 " cylinders result in %d out of %lld blocks.\n" 1188 "Do you want to modify the device geometry", 1189 nhead, nsect, pcyl, 1190 (int)nblocks, capacity->sc_capacity + 1); 1191 1192 ioparam.io_charlist = confirm_list; 1193 if (input(FIO_MSTR, msg, '?', &ioparam, 1194 &deflt, DATA_INPUT) != 0) 1195 break; 1196 1197 ioparam.io_bounds.lower = MINIMUM_NO_HEADS; 1198 ioparam.io_bounds.upper = MAXIMUM_NO_HEADS; 1199 nhead = input(FIO_INT, "Number of heads", ':', 1200 &ioparam, &nhead, DATA_INPUT); 1201 ioparam.io_bounds.lower = MINIMUM_NO_SECTORS; 1202 ioparam.io_bounds.upper = MAXIMUM_NO_SECTORS; 1203 nsect = input(FIO_INT, 1204 "Number of sectors per track", 1205 ':', &ioparam, &nsect, DATA_INPUT); 1206 ioparam.io_bounds.lower = SUN_MIN_CYL; 1207 ioparam.io_bounds.upper = MAXIMUM_NO_CYLINDERS; 1208 pcyl = input(FIO_INT, "Number of cylinders", 1209 ':', &ioparam, &pcyl, DATA_INPUT); 1210 nblocks = nhead * nsect * pcyl; 1211 if (nblocks > capacity->sc_capacity + 1) { 1212 err_print("Warning: %ld blocks exceeds " 1213 "disk capacity of %lld blocks\n", 1214 nblocks, 1215 capacity->sc_capacity + 1); 1216 } 1217 } 1218 } 1219 #endif /* defined(_SUNOS_VTOC_8) */ 1220 1221 ncyl = pcyl - acyl; 1222 1223 if (option_msg && diag_msg) { 1224 err_print("\nGeometry after adjusting for capacity:\n"); 1225 err_print(" pcyl: %d\n", pcyl); 1226 err_print(" ncyl: %d\n", ncyl); 1227 err_print(" heads: %d\n", nhead); 1228 err_print(" nsects: %d\n", nsect); 1229 err_print(" acyl: %d\n", acyl); 1230 err_print(" rpm: %d\n", rpm); 1231 } 1232 1233 (void) memset((char *)label, 0, sizeof (struct dk_label)); 1234 1235 label->dkl_magic = DKL_MAGIC; 1236 1237 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel), 1238 "%s cyl %d alt %d hd %d sec %d", 1239 disk_name, ncyl, acyl, nhead, nsect); 1240 1241 label->dkl_pcyl = pcyl; 1242 label->dkl_ncyl = ncyl; 1243 label->dkl_acyl = acyl; 1244 label->dkl_nhead = nhead; 1245 label->dkl_nsect = nsect; 1246 label->dkl_apc = 0; 1247 label->dkl_intrlv = 1; 1248 label->dkl_rpm = rpm; 1249 1250 #if defined(_FIRMWARE_NEEDS_FDISK) 1251 (void) auto_solaris_part(label); 1252 ncyl = label->dkl_ncyl; 1253 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 1254 1255 1256 if (!build_default_partition(label, DKC_SCSI_CCS)) { 1257 goto err; 1258 } 1259 1260 (void) checksum(label, CK_MAKESUM); 1261 1262 /* 1263 * Find an existing disk type defined for this disk. 1264 * For this to work, both the name and geometry must 1265 * match. If there is no such type, but there already 1266 * is a disk defined with that name, but with a different 1267 * geometry, construct a new generic disk name out of 1268 * the inquiry information. Whatever name we're 1269 * finally using, if there's no such disk type defined, 1270 * build a new disk definition. 1271 */ 1272 if ((disk = find_scsi_disk_type(disk_name, label)) == NULL) { 1273 if (find_scsi_disk_by_name(disk_name) != NULL) { 1274 char old_name[DISK_NAME_MAX]; 1275 (void) strcpy(old_name, disk_name); 1276 (void) get_generic_disk_name(disk_name, 1277 inquiry); 1278 if (option_msg && diag_msg) { 1279 err_print( 1280 "Changing disk type name from '%s' to '%s'\n", old_name, disk_name); 1281 } 1282 (void) snprintf(label->dkl_asciilabel, 1283 sizeof (label->dkl_asciilabel), 1284 "%s cyl %d alt %d hd %d sec %d", 1285 disk_name, ncyl, acyl, nhead, nsect); 1286 (void) checksum(label, CK_MAKESUM); 1287 disk = find_scsi_disk_type(disk_name, label); 1288 } 1289 if (disk == NULL) { 1290 disk = new_scsi_disk_type(fd, disk_name, label); 1291 if (disk == NULL) 1292 goto err; 1293 } 1294 } 1295 1296 return (disk); 1297 1298 err: 1299 if (option_msg && diag_msg) { 1300 err_print( 1301 "Configuration via generic SCSI-2 information failed\n"); 1302 } 1303 return (NULL); 1304 } 1305 1306 1307 /*ARGSUSED*/ 1308 static int 1309 use_existing_disk_type( 1310 int fd, 1311 int can_prompt, 1312 struct dk_label *label, 1313 struct scsi_inquiry *inquiry, 1314 struct disk_type *disk_type, 1315 struct scsi_capacity_16 *capacity) 1316 { 1317 struct scsi_capacity_16 new_capacity; 1318 int pcyl; 1319 int acyl; 1320 int nhead; 1321 int nsect; 1322 int rpm; 1323 1324 /* 1325 * If the device's block size is not 512, we have to 1326 * change block size, reformat, and then sense the 1327 * geometry. To do this, we must be able to prompt 1328 * the user. 1329 */ 1330 if (capacity->sc_lbasize != DEV_BSIZE) { 1331 if (!can_prompt) { 1332 return (0); 1333 } 1334 if (force_blocksize(fd)) { 1335 goto err; 1336 } 1337 1338 /* 1339 * Get the capacity again, since this has changed 1340 */ 1341 if (uscsi_read_capacity(fd, &new_capacity)) { 1342 goto err; 1343 } 1344 1345 if (option_msg && diag_msg) { 1346 err_print("blocks: %llu (0x%llx)\n", 1347 new_capacity.sc_capacity, 1348 new_capacity.sc_capacity); 1349 err_print("blksize: %u\n", new_capacity.sc_lbasize); 1350 } 1351 1352 capacity = &new_capacity; 1353 if (capacity->sc_lbasize != DEV_BSIZE) { 1354 goto err; 1355 } 1356 } 1357 1358 /* 1359 * Construct a new label out of the format.dat 1360 */ 1361 pcyl = disk_type->dtype_pcyl; 1362 acyl = disk_type->dtype_acyl; 1363 ncyl = disk_type->dtype_ncyl; 1364 nhead = disk_type->dtype_nhead; 1365 nsect = disk_type->dtype_nsect; 1366 rpm = disk_type->dtype_rpm; 1367 1368 if (option_msg && diag_msg) { 1369 err_print("Format.dat geometry:\n"); 1370 err_print(" pcyl: %d\n", pcyl); 1371 err_print(" heads: %d\n", nhead); 1372 err_print(" nsects: %d\n", nsect); 1373 err_print(" acyl: %d\n", acyl); 1374 err_print(" rpm: %d\n", rpm); 1375 } 1376 1377 (void) memset((char *)label, 0, sizeof (struct dk_label)); 1378 1379 label->dkl_magic = DKL_MAGIC; 1380 1381 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel), 1382 "%s cyl %d alt %d hd %d sec %d", 1383 disk_type->dtype_asciilabel, 1384 ncyl, acyl, nhead, nsect); 1385 1386 label->dkl_pcyl = pcyl; 1387 label->dkl_ncyl = ncyl; 1388 label->dkl_acyl = acyl; 1389 label->dkl_nhead = nhead; 1390 label->dkl_nsect = nsect; 1391 label->dkl_apc = 0; 1392 label->dkl_intrlv = 1; 1393 label->dkl_rpm = rpm; 1394 1395 if (!build_default_partition(label, DKC_SCSI_CCS)) { 1396 goto err; 1397 } 1398 1399 (void) checksum(label, CK_MAKESUM); 1400 return (1); 1401 1402 err: 1403 if (option_msg && diag_msg) { 1404 err_print( 1405 "Configuration via format.dat geometry failed\n"); 1406 } 1407 return (0); 1408 } 1409 1410 int 1411 build_default_partition( 1412 struct dk_label *label, 1413 int ctrl_type) 1414 { 1415 int i; 1416 int ncyls[NDKMAP]; 1417 int nblks; 1418 int cyl; 1419 struct dk_vtoc *vtoc; 1420 struct part_table *pt; 1421 struct default_partitions *dpt; 1422 long capacity; 1423 int freecyls; 1424 int blks_per_cyl; 1425 int ncyl; 1426 1427 #ifdef lint 1428 ctrl_type = ctrl_type; 1429 #endif 1430 1431 /* 1432 * Install a default vtoc 1433 */ 1434 vtoc = &label->dkl_vtoc; 1435 vtoc->v_version = V_VERSION; 1436 vtoc->v_nparts = NDKMAP; 1437 vtoc->v_sanity = VTOC_SANE; 1438 1439 for (i = 0; i < NDKMAP; i++) { 1440 vtoc->v_part[i].p_tag = default_vtoc_map[i].p_tag; 1441 vtoc->v_part[i].p_flag = default_vtoc_map[i].p_flag; 1442 } 1443 1444 /* 1445 * Find a partition that matches this disk. Capacity 1446 * is in integral number of megabytes. 1447 */ 1448 capacity = (long)(label->dkl_ncyl * label->dkl_nhead * 1449 label->dkl_nsect) / (long)((1024 * 1024) / DEV_BSIZE); 1450 dpt = default_partitions; 1451 for (i = 0; i < DEFAULT_PARTITION_TABLE_SIZE; i++, dpt++) { 1452 if (capacity >= dpt->min_capacity && 1453 capacity < dpt->max_capacity) { 1454 break; 1455 } 1456 } 1457 if (i == DEFAULT_PARTITION_TABLE_SIZE) { 1458 if (option_msg && diag_msg) { 1459 err_print("No matching default partition (%ld)\n", 1460 capacity); 1461 } 1462 return (0); 1463 } 1464 pt = dpt->part_table; 1465 1466 /* 1467 * Go through default partition table, finding fixed 1468 * sized entries. 1469 */ 1470 freecyls = label->dkl_ncyl; 1471 blks_per_cyl = label->dkl_nhead * label->dkl_nsect; 1472 for (i = 0; i < NDKMAP; i++) { 1473 if (pt->partitions[i] == HOG || pt->partitions[i] == 0) { 1474 ncyls[i] = 0; 1475 } else { 1476 /* 1477 * Calculate number of cylinders necessary 1478 * for specified size, rounding up to 1479 * the next greatest integral number of 1480 * cylinders. Always give what they 1481 * asked or more, never less. 1482 */ 1483 nblks = pt->partitions[i] * ((1024*1024)/DEV_BSIZE); 1484 nblks += (blks_per_cyl - 1); 1485 ncyls[i] = nblks / blks_per_cyl; 1486 freecyls -= ncyls[i]; 1487 } 1488 } 1489 1490 if (freecyls < 0) { 1491 if (option_msg && diag_msg) { 1492 for (i = 0; i < NDKMAP; i++) { 1493 if (ncyls[i] == 0) 1494 continue; 1495 err_print("Partition %d: %d cyls\n", 1496 i, ncyls[i]); 1497 } 1498 err_print("Free cylinders exhausted (%d)\n", 1499 freecyls); 1500 } 1501 return (0); 1502 } 1503 #if defined(i386) 1504 /* 1505 * Set the default boot partition to 1 cylinder 1506 */ 1507 ncyls[8] = 1; 1508 freecyls -= 1; 1509 1510 /* 1511 * If current disk type is not a SCSI disk, 1512 * set the default alternates partition to 2 cylinders 1513 */ 1514 if (ctrl_type != DKC_SCSI_CCS) { 1515 ncyls[9] = 2; 1516 freecyls -= 2; 1517 } 1518 #endif /* defined(i386) */ 1519 1520 /* 1521 * Set the free hog partition to whatever space remains. 1522 * It's an error to have more than one HOG partition, 1523 * but we don't verify that here. 1524 */ 1525 for (i = 0; i < NDKMAP; i++) { 1526 if (pt->partitions[i] == HOG) { 1527 assert(ncyls[i] == 0); 1528 ncyls[i] = freecyls; 1529 break; 1530 } 1531 } 1532 1533 /* 1534 * Error checking 1535 */ 1536 ncyl = 0; 1537 for (i = 0; i < NDKMAP; i++) { 1538 ncyl += ncyls[i]; 1539 } 1540 assert(ncyl == (label->dkl_ncyl)); 1541 1542 /* 1543 * Finally, install the partition in the label. 1544 */ 1545 cyl = 0; 1546 1547 #if defined(_SUNOS_VTOC_16) 1548 for (i = NDKMAP/2; i < NDKMAP; i++) { 1549 if (i == 2 || ncyls[i] == 0) 1550 continue; 1551 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl; 1552 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl; 1553 cyl += ncyls[i]; 1554 } 1555 for (i = 0; i < NDKMAP/2; i++) { 1556 1557 #elif defined(_SUNOS_VTOC_8) 1558 for (i = 0; i < NDKMAP; i++) { 1559 1560 #else 1561 #error No VTOC format defined. 1562 #endif /* defined(_SUNOS_VTOC_16) */ 1563 1564 if (i == 2 || ncyls[i] == 0) { 1565 #if defined(_SUNOS_VTOC_8) 1566 if (i != 2) { 1567 label->dkl_map[i].dkl_cylno = 0; 1568 label->dkl_map[i].dkl_nblk = 0; 1569 } 1570 #endif 1571 continue; 1572 } 1573 #if defined(_SUNOS_VTOC_8) 1574 label->dkl_map[i].dkl_cylno = cyl; 1575 label->dkl_map[i].dkl_nblk = ncyls[i] * blks_per_cyl; 1576 #elif defined(_SUNOS_VTOC_16) 1577 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl; 1578 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl; 1579 1580 #else 1581 #error No VTOC format defined. 1582 #endif /* defined(_SUNOS_VTOC_8) */ 1583 1584 cyl += ncyls[i]; 1585 } 1586 1587 /* 1588 * Set the whole disk partition 1589 */ 1590 #if defined(_SUNOS_VTOC_8) 1591 label->dkl_map[2].dkl_cylno = 0; 1592 label->dkl_map[2].dkl_nblk = 1593 label->dkl_ncyl * label->dkl_nhead * label->dkl_nsect; 1594 1595 #elif defined(_SUNOS_VTOC_16) 1596 label->dkl_vtoc.v_part[2].p_start = 0; 1597 label->dkl_vtoc.v_part[2].p_size = 1598 (label->dkl_ncyl + label->dkl_acyl) * label->dkl_nhead * 1599 label->dkl_nsect; 1600 #else 1601 #error No VTOC format defined. 1602 #endif /* defined(_SUNOS_VTOC_8) */ 1603 1604 1605 if (option_msg && diag_msg) { 1606 float scaled; 1607 err_print("\n"); 1608 for (i = 0; i < NDKMAP; i++) { 1609 #if defined(_SUNOS_VTOC_8) 1610 if (label->dkl_map[i].dkl_nblk == 0) 1611 1612 #elif defined(_SUNOS_VTOC_16) 1613 if (label->dkl_vtoc.v_part[i].p_size == 0) 1614 1615 #else 1616 #error No VTOC format defined. 1617 #endif /* defined(_SUNOS_VTOC_8) */ 1618 1619 continue; 1620 err_print("Partition %d: ", i); 1621 #if defined(_SUNOS_VTOC_8) 1622 scaled = bn2mb(label->dkl_map[i].dkl_nblk); 1623 1624 #elif defined(_SUNOS_VTOC_16) 1625 1626 scaled = bn2mb(label->dkl_vtoc.v_part[i].p_size); 1627 #else 1628 #error No VTOC format defined. 1629 #endif /* defined(_SUNOS_VTOC_8) */ 1630 1631 if (scaled > 1024.0) { 1632 err_print("%6.2fGB ", scaled/1024.0); 1633 } else { 1634 err_print("%6.2fMB ", scaled); 1635 } 1636 err_print(" %6d cylinders\n", 1637 #if defined(_SUNOS_VTOC_8) 1638 label->dkl_map[i].dkl_nblk/blks_per_cyl); 1639 1640 #elif defined(_SUNOS_VTOC_16) 1641 label->dkl_vtoc.v_part[i].p_size/blks_per_cyl); 1642 1643 #else 1644 #error No VTOC format defined. 1645 #endif /* defined(_SUNOS_VTOC_8) */ 1646 1647 } 1648 err_print("\n"); 1649 } 1650 1651 return (1); 1652 } 1653 1654 1655 1656 /* 1657 * Find an existing scsi disk definition by this name, 1658 * if possible. 1659 */ 1660 static struct disk_type * 1661 find_scsi_disk_type( 1662 char *disk_name, 1663 struct dk_label *label) 1664 { 1665 struct ctlr_type *ctlr; 1666 struct disk_type *dp; 1667 1668 ctlr = find_scsi_ctlr_type(); 1669 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) { 1670 if (dp->dtype_asciilabel) { 1671 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0) && 1672 dp->dtype_pcyl == label->dkl_pcyl && 1673 dp->dtype_ncyl == label->dkl_ncyl && 1674 dp->dtype_acyl == label->dkl_acyl && 1675 dp->dtype_nhead == label->dkl_nhead && 1676 dp->dtype_nsect == label->dkl_nsect) { 1677 return (dp); 1678 } 1679 } 1680 } 1681 1682 return ((struct disk_type *)NULL); 1683 } 1684 1685 1686 /* 1687 * Find an existing scsi disk definition by this name, 1688 * if possible. 1689 */ 1690 static struct disk_type * 1691 find_scsi_disk_by_name( 1692 char *disk_name) 1693 { 1694 struct ctlr_type *ctlr; 1695 struct disk_type *dp; 1696 1697 ctlr = find_scsi_ctlr_type(); 1698 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) { 1699 if (dp->dtype_asciilabel) { 1700 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0)) { 1701 return (dp); 1702 } 1703 } 1704 } 1705 1706 return ((struct disk_type *)NULL); 1707 } 1708 1709 1710 /* 1711 * Return a pointer to the ctlr_type structure for SCSI 1712 * disks. This list is built into the program, so there's 1713 * no chance of not being able to find it, unless someone 1714 * totally mangles the code. 1715 */ 1716 static struct ctlr_type * 1717 find_scsi_ctlr_type() 1718 { 1719 struct mctlr_list *mlp; 1720 1721 mlp = controlp; 1722 1723 while (mlp != NULL) { 1724 if (mlp->ctlr_type->ctype_ctype == DKC_SCSI_CCS) { 1725 return (mlp->ctlr_type); 1726 } 1727 mlp = mlp->next; 1728 } 1729 1730 impossible("no SCSI controller type"); 1731 1732 return ((struct ctlr_type *)NULL); 1733 } 1734 1735 1736 1737 /* 1738 * Return a pointer to the scsi ctlr_info structure. This 1739 * structure is allocated the first time format sees a 1740 * disk on this controller, so it must be present. 1741 */ 1742 static struct ctlr_info * 1743 find_scsi_ctlr_info( 1744 struct dk_cinfo *dkinfo) 1745 { 1746 struct ctlr_info *ctlr; 1747 1748 if (dkinfo->dki_ctype != DKC_SCSI_CCS) { 1749 return (NULL); 1750 } 1751 1752 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) { 1753 if (ctlr->ctlr_addr == dkinfo->dki_addr && 1754 ctlr->ctlr_space == dkinfo->dki_space && 1755 ctlr->ctlr_ctype->ctype_ctype == 1756 DKC_SCSI_CCS) { 1757 return (ctlr); 1758 } 1759 } 1760 1761 impossible("no SCSI controller info"); 1762 1763 return ((struct ctlr_info *)NULL); 1764 } 1765 1766 1767 1768 static struct disk_type * 1769 new_scsi_disk_type( 1770 int fd, 1771 char *disk_name, 1772 struct dk_label *label) 1773 { 1774 struct disk_type *dp; 1775 struct disk_type *disk; 1776 struct ctlr_info *ctlr; 1777 struct dk_cinfo dkinfo; 1778 struct partition_info *part; 1779 struct partition_info *pt; 1780 struct disk_info *disk_info; 1781 int i; 1782 1783 /* 1784 * Get the disk controller info for this disk 1785 */ 1786 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { 1787 if (option_msg && diag_msg) { 1788 err_print("DKIOCINFO failed\n"); 1789 } 1790 return (NULL); 1791 } 1792 1793 /* 1794 * Find the ctlr_info for this disk. 1795 */ 1796 ctlr = find_scsi_ctlr_info(&dkinfo); 1797 1798 /* 1799 * Allocate a new disk type for the SCSI controller. 1800 */ 1801 disk = (struct disk_type *)zalloc(sizeof (struct disk_type)); 1802 1803 /* 1804 * Find the disk_info instance for this disk. 1805 */ 1806 disk_info = find_scsi_disk_info(&dkinfo); 1807 1808 /* 1809 * The controller and the disk should match. 1810 */ 1811 assert(disk_info->disk_ctlr == ctlr); 1812 1813 /* 1814 * Link the disk into the list of disks 1815 */ 1816 dp = ctlr->ctlr_ctype->ctype_dlist; 1817 if (dp == NULL) { 1818 ctlr->ctlr_ctype->ctype_dlist = dp; 1819 } else { 1820 while (dp->dtype_next != NULL) { 1821 dp = dp->dtype_next; 1822 } 1823 dp->dtype_next = disk; 1824 } 1825 disk->dtype_next = NULL; 1826 1827 /* 1828 * Allocate and initialize the disk name. 1829 */ 1830 disk->dtype_asciilabel = alloc_string(disk_name); 1831 1832 /* 1833 * Initialize disk geometry info 1834 */ 1835 disk->dtype_pcyl = label->dkl_pcyl; 1836 disk->dtype_ncyl = label->dkl_ncyl; 1837 disk->dtype_acyl = label->dkl_acyl; 1838 disk->dtype_nhead = label->dkl_nhead; 1839 disk->dtype_nsect = label->dkl_nsect; 1840 disk->dtype_rpm = label->dkl_rpm; 1841 1842 /* 1843 * Attempt to match the partition map in the label 1844 * with a know partition for this disk type. 1845 */ 1846 for (part = disk->dtype_plist; part; part = part->pinfo_next) { 1847 if (parts_match(label, part)) { 1848 break; 1849 } 1850 } 1851 1852 /* 1853 * If no match was made, we need to create a partition 1854 * map for this disk. 1855 */ 1856 if (part == NULL) { 1857 part = (struct partition_info *) 1858 zalloc(sizeof (struct partition_info)); 1859 pt = disk->dtype_plist; 1860 if (pt == NULL) { 1861 disk->dtype_plist = part; 1862 } else { 1863 while (pt->pinfo_next != NULL) { 1864 pt = pt->pinfo_next; 1865 } 1866 pt->pinfo_next = part; 1867 } 1868 part->pinfo_next = NULL; 1869 1870 /* 1871 * Set up the partition name 1872 */ 1873 part->pinfo_name = alloc_string("default"); 1874 1875 /* 1876 * Fill in the partition info from the label 1877 */ 1878 for (i = 0; i < NDKMAP; i++) { 1879 1880 #if defined(_SUNOS_VTOC_8) 1881 part->pinfo_map[i] = label->dkl_map[i]; 1882 1883 #elif defined(_SUNOS_VTOC_16) 1884 part->pinfo_map[i].dkl_cylno = 1885 label->dkl_vtoc.v_part[i].p_start / 1886 ((int)(disk->dtype_nhead * 1887 disk->dtype_nsect - apc)); 1888 part->pinfo_map[i].dkl_nblk = 1889 label->dkl_vtoc.v_part[i].p_size; 1890 #else 1891 #error No VTOC format defined. 1892 #endif /* defined(_SUNOS_VTOC_8) */ 1893 1894 } 1895 } 1896 1897 1898 /* 1899 * Use the VTOC if valid, or install a default 1900 */ 1901 if (label->dkl_vtoc.v_version == V_VERSION) { 1902 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume, 1903 LEN_DKL_VVOL); 1904 part->vtoc = label->dkl_vtoc; 1905 } else { 1906 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL); 1907 set_vtoc_defaults(part); 1908 } 1909 1910 /* 1911 * Link the disk to the partition map 1912 */ 1913 disk_info->disk_parts = part; 1914 1915 return (disk); 1916 } 1917 1918 1919 /* 1920 * Delete a disk type from disk type list. 1921 */ 1922 int 1923 delete_disk_type( 1924 struct disk_type *disk_type) 1925 { 1926 struct ctlr_type *ctlr; 1927 struct disk_type *dp, *disk; 1928 1929 if (cur_ctype->ctype_ctype == DKC_DIRECT) 1930 ctlr = find_direct_ctlr_type(); 1931 else 1932 ctlr = find_scsi_ctlr_type(); 1933 if (ctlr == NULL || ctlr->ctype_dlist == NULL) { 1934 return (-1); 1935 } 1936 1937 disk = ctlr->ctype_dlist; 1938 if (disk == disk_type) { 1939 ctlr->ctype_dlist = disk->dtype_next; 1940 if (cur_label == L_TYPE_EFI) 1941 free(disk->dtype_plist->etoc); 1942 free(disk->dtype_plist); 1943 free(disk); 1944 return (0); 1945 } else { 1946 for (dp = disk->dtype_next; dp != NULL; 1947 disk = disk->dtype_next, dp = dp->dtype_next) { 1948 if (dp == disk_type) { 1949 disk->dtype_next = dp->dtype_next; 1950 if (cur_label == L_TYPE_EFI) 1951 free(dp->dtype_plist->etoc); 1952 free(dp->dtype_plist); 1953 free(dp); 1954 return (0); 1955 } 1956 } 1957 return (-1); 1958 } 1959 } 1960 1961 1962 static struct disk_info * 1963 find_scsi_disk_info( 1964 struct dk_cinfo *dkinfo) 1965 { 1966 struct disk_info *disk; 1967 struct dk_cinfo *dp; 1968 1969 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 1970 assert(dkinfo->dki_ctype == DKC_SCSI_CCS); 1971 dp = &disk->disk_dkinfo; 1972 if (dp->dki_ctype == dkinfo->dki_ctype && 1973 dp->dki_cnum == dkinfo->dki_cnum && 1974 dp->dki_unit == dkinfo->dki_unit && 1975 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) { 1976 return (disk); 1977 } 1978 } 1979 1980 impossible("No SCSI disk info instance\n"); 1981 1982 return ((struct disk_info *)NULL); 1983 } 1984 1985 1986 static char * 1987 get_sun_disk_name( 1988 char *disk_name, 1989 struct scsi_inquiry *inquiry) 1990 { 1991 /* 1992 * Extract the sun name of the disk 1993 */ 1994 (void) memset(disk_name, 0, DISK_NAME_MAX); 1995 (void) memcpy(disk_name, (char *)&inquiry->inq_pid[9], 7); 1996 1997 return (disk_name); 1998 } 1999 2000 2001 static char * 2002 get_generic_disk_name( 2003 char *disk_name, 2004 struct scsi_inquiry *inquiry) 2005 { 2006 char *p; 2007 2008 (void) memset(disk_name, 0, DISK_NAME_MAX); 2009 p = strcopy(disk_name, inquiry->inq_vid, 2010 sizeof (inquiry->inq_vid)); 2011 *p++ = '-'; 2012 p = strcopy(p, inquiry->inq_pid, sizeof (inquiry->inq_pid)); 2013 *p++ = '-'; 2014 p = strcopy(p, inquiry->inq_revision, 2015 sizeof (inquiry->inq_revision)); 2016 2017 return (disk_name); 2018 } 2019 2020 2021 2022 static int 2023 force_blocksize( 2024 int fd) 2025 { 2026 union { 2027 struct mode_format page3; 2028 uchar_t buf3[MAX_MODE_SENSE_SIZE]; 2029 } u_page3; 2030 struct mode_format *page3 = &u_page3.page3; 2031 struct scsi_ms_header header; 2032 2033 if (check("\ 2034 Must reformat device to 512-byte blocksize. Continue") == 0) { 2035 2036 /* 2037 * Get current Page 3 - Format Parameters page 2038 */ 2039 if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, 2040 MODE_SENSE_PC_CURRENT, (caddr_t)&u_page3, 2041 MAX_MODE_SENSE_SIZE, &header)) { 2042 goto err; 2043 } 2044 2045 /* 2046 * Make our changes to the geometry 2047 */ 2048 header.mode_header.length = 0; 2049 header.mode_header.device_specific = 0; 2050 page3->mode_page.ps = 0; 2051 page3->data_bytes_sect = DEV_BSIZE; 2052 2053 /* 2054 * make sure that logical block size is of 2055 * DEV_BSIZE. 2056 */ 2057 header.block_descriptor.blksize_hi = (DEV_BSIZE >> 16); 2058 header.block_descriptor.blksize_mid = (DEV_BSIZE >> 8); 2059 header.block_descriptor.blksize_lo = (char)(DEV_BSIZE); 2060 /* 2061 * Select current Page 3 - Format Parameters page 2062 */ 2063 if (uscsi_mode_select(fd, DAD_MODE_FORMAT, 2064 MODE_SELECT_PF, (caddr_t)&u_page3, 2065 MODESENSE_PAGE_LEN(&u_page3), &header)) { 2066 goto err; 2067 } 2068 2069 /* 2070 * Now reformat the device 2071 */ 2072 if (raw_format(fd)) { 2073 goto err; 2074 } 2075 return (0); 2076 } 2077 2078 err: 2079 if (option_msg && diag_msg) { 2080 err_print( 2081 "Reformat device to 512-byte blocksize failed\n"); 2082 } 2083 return (1); 2084 } 2085 2086 static int 2087 raw_format( 2088 int fd) 2089 { 2090 union scsi_cdb cdb; 2091 struct uscsi_cmd ucmd; 2092 struct scsi_defect_hdr defect_hdr; 2093 2094 (void) memset((char *)&ucmd, 0, sizeof (ucmd)); 2095 (void) memset((char *)&cdb, 0, sizeof (union scsi_cdb)); 2096 (void) memset((char *)&defect_hdr, 0, sizeof (defect_hdr)); 2097 cdb.scc_cmd = SCMD_FORMAT; 2098 ucmd.uscsi_cdb = (caddr_t)&cdb; 2099 ucmd.uscsi_cdblen = CDB_GROUP0; 2100 ucmd.uscsi_bufaddr = (caddr_t)&defect_hdr; 2101 ucmd.uscsi_buflen = sizeof (defect_hdr); 2102 cdb.cdb_opaque[1] = FPB_DATA; 2103 2104 /* 2105 * Issue the format ioctl 2106 */ 2107 fmt_print("Formatting...\n"); 2108 (void) fflush(stdout); 2109 if (uscsi_cmd(fd, &ucmd, 2110 (option_msg && diag_msg) ? F_NORMAL : F_SILENT)) { 2111 return (1); 2112 } 2113 return (0); 2114 } 2115 2116 /* 2117 * Copy a string of characters from src to dst, for at 2118 * most n bytes. Strip all leading and trailing spaces, 2119 * and stop if there are any non-printable characters. 2120 * Return ptr to the next character to be filled. 2121 */ 2122 static char * 2123 strcopy( 2124 char *dst, 2125 char *src, 2126 int n) 2127 { 2128 int i; 2129 2130 while (*src == ' ' && n > 0) { 2131 src++; 2132 n--; 2133 } 2134 2135 for (i = 0; n-- > 0 && isascii(*src) && isprint(*src); src++) { 2136 if (*src == ' ') { 2137 i++; 2138 } else { 2139 while (i-- > 0) 2140 *dst++ = ' '; 2141 *dst++ = *src; 2142 } 2143 } 2144 2145 *dst = 0; 2146 return (dst); 2147 } 2148 2149 /* 2150 * adjust disk geometry. 2151 * This is used when disk reports a disk geometry page having 2152 * no of physical cylinders is < 3 which is the minimum required 2153 * by Solaris (2 for storing labels and at least one as a data 2154 * cylinder ) 2155 */ 2156 int 2157 adjust_disk_geometry(int capacity, int *cyl, int *nhead, int *nsect) 2158 { 2159 int lcyl = *cyl; 2160 int lnhead = *nhead; 2161 int lnsect = *nsect; 2162 2163 assert(lcyl < SUN_MIN_CYL); 2164 2165 /* 2166 * reduce nsect by 2 for each iteration and re-calculate 2167 * the number of cylinders. 2168 */ 2169 while (lnsect > MINIMUM_NO_SECTORS && 2170 lcyl < MINIMUM_NO_CYLINDERS) { 2171 /* 2172 * make sure that we do not go below MINIMUM_NO_SECTORS. 2173 */ 2174 lnsect = max(MINIMUM_NO_SECTORS, lnsect / 2); 2175 lcyl = (capacity) / (lnhead * lnsect); 2176 } 2177 /* 2178 * If the geometry still does not satisfy 2179 * MINIMUM_NO_CYLINDERS then try to reduce the 2180 * no of heads. 2181 */ 2182 while (lnhead > MINIMUM_NO_HEADS && 2183 (lcyl < MINIMUM_NO_CYLINDERS)) { 2184 lnhead = max(MINIMUM_NO_HEADS, lnhead / 2); 2185 lcyl = (capacity) / (lnhead * lnsect); 2186 } 2187 /* 2188 * now we should have atleast SUN_MIN_CYL cylinders. 2189 * If we still do not get SUN_MIN_CYL with MINIMUM_NO_HEADS 2190 * and MINIMUM_NO_HEADS then return error. 2191 */ 2192 if (lcyl < SUN_MIN_CYL) 2193 return (1); 2194 else { 2195 *cyl = lcyl; 2196 *nhead = lnhead; 2197 *nsect = lnsect; 2198 return (0); 2199 } 2200 } 2201 2202 #if defined(_SUNOS_VTOC_8) 2203 /* 2204 * Reduce the size of one dimention below a specified 2205 * limit with a minimum loss of volume. Dimenstions are 2206 * assumed to be passed in form the largest value (the one 2207 * that needs to be reduced) to the smallest value. The 2208 * values will be twiddled until they are all less than or 2209 * equal to their limit. Returns the number in the new geometry. 2210 */ 2211 static int 2212 square_box( 2213 int capacity, 2214 int *dim1, int lim1, 2215 int *dim2, int lim2, 2216 int *dim3, int lim3) 2217 { 2218 int i; 2219 2220 /* 2221 * Although the routine should work with any ordering of 2222 * parameters, it's most efficient if they are passed in 2223 * in decreasing magnitude. 2224 */ 2225 assert(*dim1 >= *dim2); 2226 assert(*dim2 >= *dim3); 2227 2228 /* 2229 * This is done in a very arbitrary manner. We could try to 2230 * find better values but I can't come up with a method that 2231 * would run in a reasonable amount of time. That could take 2232 * approximately 65535 * 65535 iterations of a dozen flops each 2233 * or well over 4G flops. 2234 * 2235 * First: 2236 * 2237 * Let's see how far we can go with bitshifts w/o losing 2238 * any blocks. 2239 */ 2240 2241 for (i = 0; (((*dim1)>>i)&1) == 0 && ((*dim1)>>i) > lim1; i++); 2242 if (i) { 2243 *dim1 = ((*dim1)>>i); 2244 *dim3 = ((*dim3)<<i); 2245 } 2246 2247 if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) { 2248 double d[4]; 2249 2250 /* 2251 * Second: 2252 * 2253 * Set the highest value at its limit then calculate errors, 2254 * adjusting the 2nd highest value (we get better resolution 2255 * that way). 2256 */ 2257 d[1] = lim1; 2258 d[3] = *dim3; 2259 d[2] = (double)capacity/(d[1]*d[3]); 2260 2261 /* 2262 * If we overflowed the middle term, set it to its limit and 2263 * chose a new low term. 2264 */ 2265 if (d[2] > lim2) { 2266 d[2] = lim2; 2267 d[3] = (double)capacity/(d[1]*d[2]); 2268 } 2269 /* 2270 * Convert to integers. 2271 */ 2272 *dim1 = (int)d[1]; 2273 *dim2 = (int)d[2]; 2274 *dim3 = (int)d[3]; 2275 } 2276 /* 2277 * Fixup any other possible problems. 2278 * If this happens, we need a new disklabel format. 2279 */ 2280 if (*dim1 > lim1) *dim1 = lim1; 2281 if (*dim2 > lim2) *dim2 = lim2; 2282 if (*dim3 > lim3) *dim3 = lim3; 2283 return (*dim1 * *dim2 * *dim3); 2284 } 2285 #endif /* defined(_SUNOS_VTOC_8) */ 2286