1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * This file contains the main entry point of the program and other 31*7c478bd9Sstevel@tonic-gate * routines relating to the general flow. 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate #include "global.h" 34*7c478bd9Sstevel@tonic-gate #include <stdio.h> 35*7c478bd9Sstevel@tonic-gate #include <unistd.h> 36*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 37*7c478bd9Sstevel@tonic-gate #include <signal.h> 38*7c478bd9Sstevel@tonic-gate #include <memory.h> 39*7c478bd9Sstevel@tonic-gate #include <string.h> 40*7c478bd9Sstevel@tonic-gate #include <errno.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #ifdef sparc 43*7c478bd9Sstevel@tonic-gate #include <sys/hdio.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/dkbad.h> 45*7c478bd9Sstevel@tonic-gate #endif 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 48*7c478bd9Sstevel@tonic-gate #include "main.h" 49*7c478bd9Sstevel@tonic-gate #include "analyze.h" 50*7c478bd9Sstevel@tonic-gate #include "menu.h" 51*7c478bd9Sstevel@tonic-gate #include "param.h" 52*7c478bd9Sstevel@tonic-gate #include "misc.h" 53*7c478bd9Sstevel@tonic-gate #include "startup.h" 54*7c478bd9Sstevel@tonic-gate #include "menu_command.h" 55*7c478bd9Sstevel@tonic-gate #include "menu_partition.h" 56*7c478bd9Sstevel@tonic-gate #include "prompts.h" 57*7c478bd9Sstevel@tonic-gate #include "checkmount.h" 58*7c478bd9Sstevel@tonic-gate #include "label.h" 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate extern struct menu_item menu_command[]; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate #ifdef __STDC__ 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* 65*7c478bd9Sstevel@tonic-gate * Local prototypes for ANSI C compilers 66*7c478bd9Sstevel@tonic-gate */ 67*7c478bd9Sstevel@tonic-gate static void get_disk_characteristics(void); 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate #else /* __STDC__ */ 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /* 73*7c478bd9Sstevel@tonic-gate * Local prototypes for non-ANSI C compilers 74*7c478bd9Sstevel@tonic-gate */ 75*7c478bd9Sstevel@tonic-gate static void get_disk_characteristics(); 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate #endif /* __STDC__ */ 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate /* 80*7c478bd9Sstevel@tonic-gate * This is the main entry point. 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate void 83*7c478bd9Sstevel@tonic-gate main(argc, argv) 84*7c478bd9Sstevel@tonic-gate int argc; 85*7c478bd9Sstevel@tonic-gate char *argv[]; 86*7c478bd9Sstevel@tonic-gate { 87*7c478bd9Sstevel@tonic-gate int i; 88*7c478bd9Sstevel@tonic-gate int ret_code = 1; 89*7c478bd9Sstevel@tonic-gate char **arglist; 90*7c478bd9Sstevel@tonic-gate struct disk_info *disk = NULL; 91*7c478bd9Sstevel@tonic-gate struct disk_type *type, *oldtype; 92*7c478bd9Sstevel@tonic-gate struct partition_info *parts; 93*7c478bd9Sstevel@tonic-gate struct sigaction act; 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate solaris_offset = 0; 96*7c478bd9Sstevel@tonic-gate /* 97*7c478bd9Sstevel@tonic-gate * Decode the command line options. 98*7c478bd9Sstevel@tonic-gate */ 99*7c478bd9Sstevel@tonic-gate i = do_options(argc, argv); 100*7c478bd9Sstevel@tonic-gate /* 101*7c478bd9Sstevel@tonic-gate * If we are to run from a command file, open it up. 102*7c478bd9Sstevel@tonic-gate */ 103*7c478bd9Sstevel@tonic-gate if (option_f) { 104*7c478bd9Sstevel@tonic-gate if (freopen(option_f, "r", stdin) == NULL) { 105*7c478bd9Sstevel@tonic-gate err_print("Unable to open command file '%s'.\n", 106*7c478bd9Sstevel@tonic-gate option_f); 107*7c478bd9Sstevel@tonic-gate fullabort(); 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate /* 111*7c478bd9Sstevel@tonic-gate * If we are logging, open the log file. 112*7c478bd9Sstevel@tonic-gate */ 113*7c478bd9Sstevel@tonic-gate if (option_l) { 114*7c478bd9Sstevel@tonic-gate if ((log_file = fopen(option_l, "w")) == NULL) { 115*7c478bd9Sstevel@tonic-gate err_print("Unable to open log file '%s'.\n", 116*7c478bd9Sstevel@tonic-gate option_l); 117*7c478bd9Sstevel@tonic-gate fullabort(); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate /* 121*7c478bd9Sstevel@tonic-gate * Read in the data file and initialize the hardware structs. 122*7c478bd9Sstevel@tonic-gate */ 123*7c478bd9Sstevel@tonic-gate sup_init(); 124*7c478bd9Sstevel@tonic-gate /* 125*7c478bd9Sstevel@tonic-gate * If there are no disks on the command line, search the 126*7c478bd9Sstevel@tonic-gate * appropriate device directory for character devices that 127*7c478bd9Sstevel@tonic-gate * look like disks. 128*7c478bd9Sstevel@tonic-gate */ 129*7c478bd9Sstevel@tonic-gate if (i < 0) { 130*7c478bd9Sstevel@tonic-gate arglist = (char **)NULL; 131*7c478bd9Sstevel@tonic-gate /* 132*7c478bd9Sstevel@tonic-gate * There were disks on the command line. They comprise the 133*7c478bd9Sstevel@tonic-gate * search list. 134*7c478bd9Sstevel@tonic-gate */ 135*7c478bd9Sstevel@tonic-gate } else { 136*7c478bd9Sstevel@tonic-gate arglist = &argv[i]; 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate /* 139*7c478bd9Sstevel@tonic-gate * Perform the search for disks. 140*7c478bd9Sstevel@tonic-gate */ 141*7c478bd9Sstevel@tonic-gate do_search(arglist); 142*7c478bd9Sstevel@tonic-gate /* 143*7c478bd9Sstevel@tonic-gate * Catch ctrl-C and ctrl-Z so critical sections can be 144*7c478bd9Sstevel@tonic-gate * implemented. We use sigaction, as this sets up the 145*7c478bd9Sstevel@tonic-gate * signal handler permanently, and also automatically 146*7c478bd9Sstevel@tonic-gate * restarts any interrupted system call. 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate act.sa_handler = cmdabort; 149*7c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t)); 150*7c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART | SA_NODEFER; 151*7c478bd9Sstevel@tonic-gate if (sigaction(SIGINT, &act, (struct sigaction *)NULL) == -1) { 152*7c478bd9Sstevel@tonic-gate err_print("sigaction(SIGINT) failed - %s\n", 153*7c478bd9Sstevel@tonic-gate strerror(errno)); 154*7c478bd9Sstevel@tonic-gate fullabort(); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate act.sa_handler = onsusp; 158*7c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t)); 159*7c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART | SA_NODEFER; 160*7c478bd9Sstevel@tonic-gate if (sigaction(SIGTSTP, &act, (struct sigaction *)NULL) == -1) { 161*7c478bd9Sstevel@tonic-gate err_print("sigaction(SIGTSTP) failed - %s\n", 162*7c478bd9Sstevel@tonic-gate strerror(errno)); 163*7c478bd9Sstevel@tonic-gate fullabort(); 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate act.sa_handler = onalarm; 167*7c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t)); 168*7c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART; 169*7c478bd9Sstevel@tonic-gate if (sigaction(SIGALRM, &act, (struct sigaction *)NULL) == -1) { 170*7c478bd9Sstevel@tonic-gate err_print("sigaction(SIGALRM) failed - %s\n", 171*7c478bd9Sstevel@tonic-gate strerror(errno)); 172*7c478bd9Sstevel@tonic-gate fullabort(); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate /* 176*7c478bd9Sstevel@tonic-gate * If there was only 1 disk on the command line, mark it 177*7c478bd9Sstevel@tonic-gate * to be the current disk. If it wasn't found, it's an error. 178*7c478bd9Sstevel@tonic-gate */ 179*7c478bd9Sstevel@tonic-gate if (i == argc - 1) { 180*7c478bd9Sstevel@tonic-gate disk = disk_list; 181*7c478bd9Sstevel@tonic-gate if (disk == NULL) { 182*7c478bd9Sstevel@tonic-gate err_print("Unable to find specified disk '%s'.\n", 183*7c478bd9Sstevel@tonic-gate argv[i]); 184*7c478bd9Sstevel@tonic-gate fullabort(); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate /* 188*7c478bd9Sstevel@tonic-gate * A disk was forced on the command line. 189*7c478bd9Sstevel@tonic-gate */ 190*7c478bd9Sstevel@tonic-gate if (option_d) { 191*7c478bd9Sstevel@tonic-gate /* 192*7c478bd9Sstevel@tonic-gate * Find it in the list of found disks and mark it to 193*7c478bd9Sstevel@tonic-gate * be the current disk. 194*7c478bd9Sstevel@tonic-gate */ 195*7c478bd9Sstevel@tonic-gate for (disk = disk_list; disk != NULL; disk = disk->disk_next) 196*7c478bd9Sstevel@tonic-gate if (diskname_match(option_d, disk)) 197*7c478bd9Sstevel@tonic-gate break; 198*7c478bd9Sstevel@tonic-gate /* 199*7c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error. 200*7c478bd9Sstevel@tonic-gate */ 201*7c478bd9Sstevel@tonic-gate if (disk == NULL) { 202*7c478bd9Sstevel@tonic-gate err_print("Unable to find specified disk '%s'.\n", 203*7c478bd9Sstevel@tonic-gate option_d); 204*7c478bd9Sstevel@tonic-gate fullabort(); 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate /* 208*7c478bd9Sstevel@tonic-gate * A disk type was forced on the command line. 209*7c478bd9Sstevel@tonic-gate */ 210*7c478bd9Sstevel@tonic-gate if (option_t != NULL) { 211*7c478bd9Sstevel@tonic-gate /* 212*7c478bd9Sstevel@tonic-gate * Only legal if a disk was also forced. 213*7c478bd9Sstevel@tonic-gate */ 214*7c478bd9Sstevel@tonic-gate if (disk == NULL) { 215*7c478bd9Sstevel@tonic-gate err_print("Must specify disk as well as type.\n"); 216*7c478bd9Sstevel@tonic-gate fullabort(); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate oldtype = disk->disk_type; 219*7c478bd9Sstevel@tonic-gate /* 220*7c478bd9Sstevel@tonic-gate * Find the specified type in the list of legal types 221*7c478bd9Sstevel@tonic-gate * for the disk. 222*7c478bd9Sstevel@tonic-gate */ 223*7c478bd9Sstevel@tonic-gate for (type = disk->disk_ctlr->ctlr_ctype->ctype_dlist; 224*7c478bd9Sstevel@tonic-gate type != NULL; type = type->dtype_next) 225*7c478bd9Sstevel@tonic-gate if (strcmp(option_t, type->dtype_asciilabel) == 0) 226*7c478bd9Sstevel@tonic-gate break; 227*7c478bd9Sstevel@tonic-gate /* 228*7c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error. 229*7c478bd9Sstevel@tonic-gate */ 230*7c478bd9Sstevel@tonic-gate if (type == NULL) { 231*7c478bd9Sstevel@tonic-gate err_print( 232*7c478bd9Sstevel@tonic-gate "Specified type '%s' is not a known type.\n", option_t); 233*7c478bd9Sstevel@tonic-gate fullabort(); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate /* 236*7c478bd9Sstevel@tonic-gate * If the specified type is not the same as the type 237*7c478bd9Sstevel@tonic-gate * in the disk label, update the type and nullify the 238*7c478bd9Sstevel@tonic-gate * partition map. 239*7c478bd9Sstevel@tonic-gate */ 240*7c478bd9Sstevel@tonic-gate if (type != oldtype) { 241*7c478bd9Sstevel@tonic-gate disk->disk_type = type; 242*7c478bd9Sstevel@tonic-gate disk->disk_parts = NULL; 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate } 245*7c478bd9Sstevel@tonic-gate /* 246*7c478bd9Sstevel@tonic-gate * A partition map was forced on the command line. 247*7c478bd9Sstevel@tonic-gate */ 248*7c478bd9Sstevel@tonic-gate if (option_p) { 249*7c478bd9Sstevel@tonic-gate /* 250*7c478bd9Sstevel@tonic-gate * Only legal if both disk and type were also forced. 251*7c478bd9Sstevel@tonic-gate */ 252*7c478bd9Sstevel@tonic-gate if (disk == NULL || disk->disk_type == NULL) { 253*7c478bd9Sstevel@tonic-gate err_print("Must specify disk and type as well "); 254*7c478bd9Sstevel@tonic-gate err_print("as partitiion.\n"); 255*7c478bd9Sstevel@tonic-gate fullabort(); 256*7c478bd9Sstevel@tonic-gate } 257*7c478bd9Sstevel@tonic-gate /* 258*7c478bd9Sstevel@tonic-gate * Find the specified map in the list of legal maps 259*7c478bd9Sstevel@tonic-gate * for the type. 260*7c478bd9Sstevel@tonic-gate */ 261*7c478bd9Sstevel@tonic-gate for (parts = disk->disk_type->dtype_plist; parts != NULL; 262*7c478bd9Sstevel@tonic-gate parts = parts->pinfo_next) 263*7c478bd9Sstevel@tonic-gate if (strcmp(option_p, parts->pinfo_name) == 0) 264*7c478bd9Sstevel@tonic-gate break; 265*7c478bd9Sstevel@tonic-gate /* 266*7c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error. 267*7c478bd9Sstevel@tonic-gate */ 268*7c478bd9Sstevel@tonic-gate if (parts == NULL) { 269*7c478bd9Sstevel@tonic-gate err_print( 270*7c478bd9Sstevel@tonic-gate "Specified table '%s' is not a known table.\n", option_p); 271*7c478bd9Sstevel@tonic-gate fullabort(); 272*7c478bd9Sstevel@tonic-gate } 273*7c478bd9Sstevel@tonic-gate /* 274*7c478bd9Sstevel@tonic-gate * Update the map. 275*7c478bd9Sstevel@tonic-gate */ 276*7c478bd9Sstevel@tonic-gate disk->disk_parts = parts; 277*7c478bd9Sstevel@tonic-gate } 278*7c478bd9Sstevel@tonic-gate /* 279*7c478bd9Sstevel@tonic-gate * If a disk was marked to become current, initialize the state 280*7c478bd9Sstevel@tonic-gate * to make it current. If not, ask user to pick one. 281*7c478bd9Sstevel@tonic-gate */ 282*7c478bd9Sstevel@tonic-gate if (disk != NULL) { 283*7c478bd9Sstevel@tonic-gate init_globals(disk); 284*7c478bd9Sstevel@tonic-gate } else if (option_f == 0 && option_d == 0) { 285*7c478bd9Sstevel@tonic-gate while (ret_code) { 286*7c478bd9Sstevel@tonic-gate ret_code = c_disk(); 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate #ifdef BUG1134748 291*7c478bd9Sstevel@tonic-gate /* 292*7c478bd9Sstevel@tonic-gate * if -f command-file is specified, check for disk and disktype 293*7c478bd9Sstevel@tonic-gate * input also. For SCSI disks, the type input may not be needed 294*7c478bd9Sstevel@tonic-gate * since format would have figured that using inquiry information. 295*7c478bd9Sstevel@tonic-gate */ 296*7c478bd9Sstevel@tonic-gate if (option_f) { 297*7c478bd9Sstevel@tonic-gate if (cur_disk == NULL) { 298*7c478bd9Sstevel@tonic-gate err_print("Must specify a disk using -d option.\n"); 299*7c478bd9Sstevel@tonic-gate fullabort(); 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate if (cur_dtype == NULL) { 302*7c478bd9Sstevel@tonic-gate err_print("Must specify disk as well as type.\n"); 303*7c478bd9Sstevel@tonic-gate fullabort(); 304*7c478bd9Sstevel@tonic-gate } 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate #endif /* BUG1134748 */ 307*7c478bd9Sstevel@tonic-gate 308*7c478bd9Sstevel@tonic-gate /* 309*7c478bd9Sstevel@tonic-gate * Run the command menu. 310*7c478bd9Sstevel@tonic-gate */ 311*7c478bd9Sstevel@tonic-gate cur_menu = last_menu = 0; 312*7c478bd9Sstevel@tonic-gate run_menu(menu_command, "FORMAT", "format", 1); 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate /* 315*7c478bd9Sstevel@tonic-gate * normal ending. Explicitly exit(0); 316*7c478bd9Sstevel@tonic-gate */ 317*7c478bd9Sstevel@tonic-gate exit(0); 318*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 319*7c478bd9Sstevel@tonic-gate } 320*7c478bd9Sstevel@tonic-gate 321*7c478bd9Sstevel@tonic-gate /* 322*7c478bd9Sstevel@tonic-gate * This routine initializes the internal state to ready it for a new 323*7c478bd9Sstevel@tonic-gate * current disk. There are a zillion state variables that store 324*7c478bd9Sstevel@tonic-gate * information on the current disk, and they must all be updated. 325*7c478bd9Sstevel@tonic-gate * We also tell SunOS about the disk, since it may not know if the 326*7c478bd9Sstevel@tonic-gate * disk wasn't labeled at boot time. 327*7c478bd9Sstevel@tonic-gate */ 328*7c478bd9Sstevel@tonic-gate void 329*7c478bd9Sstevel@tonic-gate init_globals(disk) 330*7c478bd9Sstevel@tonic-gate struct disk_info *disk; 331*7c478bd9Sstevel@tonic-gate { 332*7c478bd9Sstevel@tonic-gate int status; 333*7c478bd9Sstevel@tonic-gate #ifdef sparc 334*7c478bd9Sstevel@tonic-gate int i; 335*7c478bd9Sstevel@tonic-gate caddr_t bad_ptr = (caddr_t)&badmap; 336*7c478bd9Sstevel@tonic-gate #endif 337*7c478bd9Sstevel@tonic-gate 338*7c478bd9Sstevel@tonic-gate /* 339*7c478bd9Sstevel@tonic-gate * If there was an old current disk, close the file for it. 340*7c478bd9Sstevel@tonic-gate */ 341*7c478bd9Sstevel@tonic-gate if (cur_disk != NULL) 342*7c478bd9Sstevel@tonic-gate (void) close(cur_file); 343*7c478bd9Sstevel@tonic-gate /* 344*7c478bd9Sstevel@tonic-gate * Kill off any defect lists still lying around. 345*7c478bd9Sstevel@tonic-gate */ 346*7c478bd9Sstevel@tonic-gate kill_deflist(&cur_list); 347*7c478bd9Sstevel@tonic-gate kill_deflist(&work_list); 348*7c478bd9Sstevel@tonic-gate /* 349*7c478bd9Sstevel@tonic-gate * If there were any buffers, free them up. 350*7c478bd9Sstevel@tonic-gate */ 351*7c478bd9Sstevel@tonic-gate if ((char *)cur_buf != NULL) { 352*7c478bd9Sstevel@tonic-gate destroy_data((char *)cur_buf); 353*7c478bd9Sstevel@tonic-gate cur_buf = NULL; 354*7c478bd9Sstevel@tonic-gate } 355*7c478bd9Sstevel@tonic-gate if ((char *)pattern_buf != NULL) { 356*7c478bd9Sstevel@tonic-gate destroy_data((char *)pattern_buf); 357*7c478bd9Sstevel@tonic-gate pattern_buf = NULL; 358*7c478bd9Sstevel@tonic-gate } 359*7c478bd9Sstevel@tonic-gate /* 360*7c478bd9Sstevel@tonic-gate * Fill in the hardware struct pointers for the new disk. 361*7c478bd9Sstevel@tonic-gate */ 362*7c478bd9Sstevel@tonic-gate cur_disk = disk; 363*7c478bd9Sstevel@tonic-gate cur_dtype = cur_disk->disk_type; 364*7c478bd9Sstevel@tonic-gate cur_label = cur_disk->label_type; 365*7c478bd9Sstevel@tonic-gate cur_ctlr = cur_disk->disk_ctlr; 366*7c478bd9Sstevel@tonic-gate cur_parts = cur_disk->disk_parts; 367*7c478bd9Sstevel@tonic-gate cur_ctype = cur_ctlr->ctlr_ctype; 368*7c478bd9Sstevel@tonic-gate cur_ops = cur_ctype->ctype_ops; 369*7c478bd9Sstevel@tonic-gate cur_flags = 0; 370*7c478bd9Sstevel@tonic-gate /* 371*7c478bd9Sstevel@tonic-gate * Open a file for the new disk. 372*7c478bd9Sstevel@tonic-gate */ 373*7c478bd9Sstevel@tonic-gate if ((cur_file = open_disk(cur_disk->disk_path, 374*7c478bd9Sstevel@tonic-gate O_RDWR | O_NDELAY)) < 0) { 375*7c478bd9Sstevel@tonic-gate err_print( 376*7c478bd9Sstevel@tonic-gate "Error: can't open selected disk '%s'.\n", cur_disk->disk_name); 377*7c478bd9Sstevel@tonic-gate fullabort(); 378*7c478bd9Sstevel@tonic-gate } 379*7c478bd9Sstevel@tonic-gate #ifdef sparc 380*7c478bd9Sstevel@tonic-gate /* 381*7c478bd9Sstevel@tonic-gate * If the new disk uses bad-144, initialize the bad block table. 382*7c478bd9Sstevel@tonic-gate */ 383*7c478bd9Sstevel@tonic-gate if (cur_ctlr->ctlr_flags & DKI_BAD144) { 384*7c478bd9Sstevel@tonic-gate badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0; 385*7c478bd9Sstevel@tonic-gate for (i = 0; i < NDKBAD; i++) { 386*7c478bd9Sstevel@tonic-gate badmap.bt_bad[i].bt_cyl = -1; 387*7c478bd9Sstevel@tonic-gate badmap.bt_bad[i].bt_trksec = -1; 388*7c478bd9Sstevel@tonic-gate } 389*7c478bd9Sstevel@tonic-gate } 390*7c478bd9Sstevel@tonic-gate #endif 391*7c478bd9Sstevel@tonic-gate /* 392*7c478bd9Sstevel@tonic-gate * If the type of the new disk is known... 393*7c478bd9Sstevel@tonic-gate */ 394*7c478bd9Sstevel@tonic-gate if (cur_dtype != NULL) { 395*7c478bd9Sstevel@tonic-gate /* 396*7c478bd9Sstevel@tonic-gate * Initialize the physical characteristics. 397*7c478bd9Sstevel@tonic-gate * If need disk specs, prompt for undefined disk 398*7c478bd9Sstevel@tonic-gate * characteristics. If running from a file, 399*7c478bd9Sstevel@tonic-gate * use defaults. 400*7c478bd9Sstevel@tonic-gate */ 401*7c478bd9Sstevel@tonic-gate if (cur_dtype->dtype_flags & DT_NEED_SPEFS) { 402*7c478bd9Sstevel@tonic-gate get_disk_characteristics(); 403*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_flags &= ~DT_NEED_SPEFS; 404*7c478bd9Sstevel@tonic-gate } 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate ncyl = cur_dtype->dtype_ncyl; 407*7c478bd9Sstevel@tonic-gate acyl = cur_dtype->dtype_acyl; 408*7c478bd9Sstevel@tonic-gate pcyl = cur_dtype->dtype_pcyl; 409*7c478bd9Sstevel@tonic-gate nhead = cur_dtype->dtype_nhead; 410*7c478bd9Sstevel@tonic-gate nsect = cur_dtype->dtype_nsect; 411*7c478bd9Sstevel@tonic-gate phead = cur_dtype->dtype_phead; 412*7c478bd9Sstevel@tonic-gate psect = cur_dtype->dtype_psect; 413*7c478bd9Sstevel@tonic-gate /* 414*7c478bd9Sstevel@tonic-gate * Alternates per cylinder are forced to 0 or 1, 415*7c478bd9Sstevel@tonic-gate * independent of what the label says. This works 416*7c478bd9Sstevel@tonic-gate * because we know which ctlr we are dealing with. 417*7c478bd9Sstevel@tonic-gate */ 418*7c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_APC) 419*7c478bd9Sstevel@tonic-gate apc = 1; 420*7c478bd9Sstevel@tonic-gate else 421*7c478bd9Sstevel@tonic-gate apc = 0; 422*7c478bd9Sstevel@tonic-gate /* 423*7c478bd9Sstevel@tonic-gate * Initialize the surface analysis info. We always start 424*7c478bd9Sstevel@tonic-gate * out with scan set for the whole disk. Note, 425*7c478bd9Sstevel@tonic-gate * for SCSI disks, we can only scan the data area. 426*7c478bd9Sstevel@tonic-gate */ 427*7c478bd9Sstevel@tonic-gate scan_lower = 0; 428*7c478bd9Sstevel@tonic-gate scan_size = BUF_SECTS; 429*7c478bd9Sstevel@tonic-gate if ((cur_ctype->ctype_flags & CF_SCSI) && 430*7c478bd9Sstevel@tonic-gate (cur_disk->label_type == L_TYPE_SOLARIS)) { 431*7c478bd9Sstevel@tonic-gate scan_upper = datasects() - 1; 432*7c478bd9Sstevel@tonic-gate } else if (cur_disk->label_type == L_TYPE_SOLARIS) { 433*7c478bd9Sstevel@tonic-gate scan_upper = physsects() - 1; 434*7c478bd9Sstevel@tonic-gate } else if (cur_disk->label_type == L_TYPE_EFI) { 435*7c478bd9Sstevel@tonic-gate scan_upper = cur_parts->etoc->efi_last_lba; 436*7c478bd9Sstevel@tonic-gate } 437*7c478bd9Sstevel@tonic-gate 438*7c478bd9Sstevel@tonic-gate /* 439*7c478bd9Sstevel@tonic-gate * Allocate the buffers. 440*7c478bd9Sstevel@tonic-gate */ 441*7c478bd9Sstevel@tonic-gate cur_buf = (void *) zalloc(BUF_SECTS * SECSIZE); 442*7c478bd9Sstevel@tonic-gate pattern_buf = (void *) zalloc(BUF_SECTS * SECSIZE); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate /* 445*7c478bd9Sstevel@tonic-gate * Tell the user which disk (s)he selected. 446*7c478bd9Sstevel@tonic-gate */ 447*7c478bd9Sstevel@tonic-gate if (chk_volname(cur_disk)) { 448*7c478bd9Sstevel@tonic-gate fmt_print("selecting %s: ", cur_disk->disk_name); 449*7c478bd9Sstevel@tonic-gate print_volname(cur_disk); 450*7c478bd9Sstevel@tonic-gate fmt_print("\n"); 451*7c478bd9Sstevel@tonic-gate } else { 452*7c478bd9Sstevel@tonic-gate fmt_print("selecting %s\n", cur_disk->disk_name); 453*7c478bd9Sstevel@tonic-gate } 454*7c478bd9Sstevel@tonic-gate 455*7c478bd9Sstevel@tonic-gate /* 456*7c478bd9Sstevel@tonic-gate * If the drive is formatted... 457*7c478bd9Sstevel@tonic-gate */ 458*7c478bd9Sstevel@tonic-gate if ((*cur_ops->op_ck_format)()) { 459*7c478bd9Sstevel@tonic-gate /* 460*7c478bd9Sstevel@tonic-gate * Mark it formatted. 461*7c478bd9Sstevel@tonic-gate */ 462*7c478bd9Sstevel@tonic-gate cur_flags |= DISK_FORMATTED; 463*7c478bd9Sstevel@tonic-gate /* 464*7c478bd9Sstevel@tonic-gate * Read the defect list, if we have one. 465*7c478bd9Sstevel@tonic-gate */ 466*7c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) { 467*7c478bd9Sstevel@tonic-gate read_list(&cur_list); 468*7c478bd9Sstevel@tonic-gate } 469*7c478bd9Sstevel@tonic-gate #ifdef sparc 470*7c478bd9Sstevel@tonic-gate /* 471*7c478bd9Sstevel@tonic-gate * If the disk does BAD-144, we do an ioctl to 472*7c478bd9Sstevel@tonic-gate * tell SunOS about the bad block table. 473*7c478bd9Sstevel@tonic-gate */ 474*7c478bd9Sstevel@tonic-gate if (cur_ctlr->ctlr_flags & DKI_BAD144) { 475*7c478bd9Sstevel@tonic-gate if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr)) { 476*7c478bd9Sstevel@tonic-gate err_print( 477*7c478bd9Sstevel@tonic-gate "Warning: error telling SunOS bad block map table.\n"); 478*7c478bd9Sstevel@tonic-gate } 479*7c478bd9Sstevel@tonic-gate } 480*7c478bd9Sstevel@tonic-gate #endif 481*7c478bd9Sstevel@tonic-gate fmt_print("[disk formatted"); 482*7c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) { 483*7c478bd9Sstevel@tonic-gate if (cur_list.list != NULL) { 484*7c478bd9Sstevel@tonic-gate fmt_print(", defect list found"); 485*7c478bd9Sstevel@tonic-gate } else { 486*7c478bd9Sstevel@tonic-gate fmt_print(", no defect list found"); 487*7c478bd9Sstevel@tonic-gate } 488*7c478bd9Sstevel@tonic-gate } 489*7c478bd9Sstevel@tonic-gate fmt_print("]"); 490*7c478bd9Sstevel@tonic-gate /* 491*7c478bd9Sstevel@tonic-gate * Drive wasn't formatted. Tell the user in case he 492*7c478bd9Sstevel@tonic-gate * disagrees. 493*7c478bd9Sstevel@tonic-gate */ 494*7c478bd9Sstevel@tonic-gate } else if (EMBEDDED_SCSI) { 495*7c478bd9Sstevel@tonic-gate fmt_print("[disk unformatted]"); 496*7c478bd9Sstevel@tonic-gate } else { 497*7c478bd9Sstevel@tonic-gate /* 498*7c478bd9Sstevel@tonic-gate * Make sure the user is serious. Note, for 499*7c478bd9Sstevel@tonic-gate * SCSI disks since this is instantaneous, we 500*7c478bd9Sstevel@tonic-gate * will just do it and not ask for confirmation. 501*7c478bd9Sstevel@tonic-gate */ 502*7c478bd9Sstevel@tonic-gate status = 0; 503*7c478bd9Sstevel@tonic-gate if (!(cur_ctype->ctype_flags & CF_CONFIRM)) { 504*7c478bd9Sstevel@tonic-gate if (check("\n\ 505*7c478bd9Sstevel@tonic-gate Ready to get manufacturer's defect list from unformatted drive.\n\ 506*7c478bd9Sstevel@tonic-gate This cannot be interrupted and takes a long while.\n\ 507*7c478bd9Sstevel@tonic-gate Continue")) 508*7c478bd9Sstevel@tonic-gate status = 1; 509*7c478bd9Sstevel@tonic-gate else 510*7c478bd9Sstevel@tonic-gate fmt_print( 511*7c478bd9Sstevel@tonic-gate "Extracting manufacturer's defect list..."); 512*7c478bd9Sstevel@tonic-gate } 513*7c478bd9Sstevel@tonic-gate /* 514*7c478bd9Sstevel@tonic-gate * Extract manufacturer's defect list. 515*7c478bd9Sstevel@tonic-gate */ 516*7c478bd9Sstevel@tonic-gate if ((status == 0) && (cur_ops->op_ex_man != NULL)) { 517*7c478bd9Sstevel@tonic-gate status = (*cur_ops->op_ex_man)(&cur_list); 518*7c478bd9Sstevel@tonic-gate } else { 519*7c478bd9Sstevel@tonic-gate status = 1; 520*7c478bd9Sstevel@tonic-gate } 521*7c478bd9Sstevel@tonic-gate fmt_print("[disk unformatted"); 522*7c478bd9Sstevel@tonic-gate if (status != 0) { 523*7c478bd9Sstevel@tonic-gate fmt_print(", no defect list found]"); 524*7c478bd9Sstevel@tonic-gate } else { 525*7c478bd9Sstevel@tonic-gate fmt_print(", defect list found]"); 526*7c478bd9Sstevel@tonic-gate } 527*7c478bd9Sstevel@tonic-gate } 528*7c478bd9Sstevel@tonic-gate } else { 529*7c478bd9Sstevel@tonic-gate /* 530*7c478bd9Sstevel@tonic-gate * Disk type is not known. 531*7c478bd9Sstevel@tonic-gate * Initialize physical characteristics to 0 and tell the 532*7c478bd9Sstevel@tonic-gate * user we don't know what type the disk is. 533*7c478bd9Sstevel@tonic-gate */ 534*7c478bd9Sstevel@tonic-gate ncyl = acyl = nhead = nsect = psect = 0; 535*7c478bd9Sstevel@tonic-gate } 536*7c478bd9Sstevel@tonic-gate 537*7c478bd9Sstevel@tonic-gate fmt_print("\n"); 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate /* 540*7c478bd9Sstevel@tonic-gate * Check to see if there are any mounted file systems on the 541*7c478bd9Sstevel@tonic-gate * disk. If there are, print a warning. 542*7c478bd9Sstevel@tonic-gate */ 543*7c478bd9Sstevel@tonic-gate if (checkmount((daddr_t)-1, (daddr_t)-1)) 544*7c478bd9Sstevel@tonic-gate err_print("Warning: Current Disk has mounted partitions.\n"); 545*7c478bd9Sstevel@tonic-gate 546*7c478bd9Sstevel@tonic-gate /* 547*7c478bd9Sstevel@tonic-gate * Get the Solaris Fdisk Partition information 548*7c478bd9Sstevel@tonic-gate */ 549*7c478bd9Sstevel@tonic-gate (void) copy_solaris_part(&cur_disk->fdisk_part); 550*7c478bd9Sstevel@tonic-gate } 551*7c478bd9Sstevel@tonic-gate 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate /* 554*7c478bd9Sstevel@tonic-gate * Prompt for some undefined disk characteristics. 555*7c478bd9Sstevel@tonic-gate * Used when there is no disk definition, but the 556*7c478bd9Sstevel@tonic-gate * disk has a valid label, so basically we're 557*7c478bd9Sstevel@tonic-gate * prompting for everything that isn't in the label. 558*7c478bd9Sstevel@tonic-gate */ 559*7c478bd9Sstevel@tonic-gate static void 560*7c478bd9Sstevel@tonic-gate get_disk_characteristics() 561*7c478bd9Sstevel@tonic-gate { 562*7c478bd9Sstevel@tonic-gate /* 563*7c478bd9Sstevel@tonic-gate * The need_spefs flag is used to tell us that this disk 564*7c478bd9Sstevel@tonic-gate * is not a known type and the ctlr specific info must 565*7c478bd9Sstevel@tonic-gate * be prompted for. We only prompt for the info that applies 566*7c478bd9Sstevel@tonic-gate * to this ctlr. 567*7c478bd9Sstevel@tonic-gate */ 568*7c478bd9Sstevel@tonic-gate assert(cur_dtype->dtype_flags & DT_NEED_SPEFS); 569*7c478bd9Sstevel@tonic-gate 570*7c478bd9Sstevel@tonic-gate /* 571*7c478bd9Sstevel@tonic-gate * If we're running with input from a file, use 572*7c478bd9Sstevel@tonic-gate * reasonable defaults, since prompting for the 573*7c478bd9Sstevel@tonic-gate * information will probably mess things up. 574*7c478bd9Sstevel@tonic-gate */ 575*7c478bd9Sstevel@tonic-gate if (option_f) { 576*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_pcyl = ncyl + acyl; 577*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_rpm = AVG_RPM; 578*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_bpt = INFINITY; 579*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_phead = 0; 580*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_psect = 0; 581*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_cyl_skew = 0; 582*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_trk_skew = 0; 583*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_trks_zone = 0; 584*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_atrks = 0; 585*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_asect = 0; 586*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_cache = 0; 587*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_threshold = 0; 588*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_min = 0; 589*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_max = 0; 590*7c478bd9Sstevel@tonic-gate 591*7c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_SMD_DEFS) { 592*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_bps = AVG_BPS; 593*7c478bd9Sstevel@tonic-gate } 594*7c478bd9Sstevel@tonic-gate } else { 595*7c478bd9Sstevel@tonic-gate 596*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_pcyl = get_pcyl(ncyl, cur_dtype->dtype_acyl); 597*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_bpt = get_bpt(cur_dtype->dtype_nsect, 598*7c478bd9Sstevel@tonic-gate &cur_dtype->dtype_options); 599*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_rpm = get_rpm(); 600*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_fmt_time = 601*7c478bd9Sstevel@tonic-gate get_fmt_time(&cur_dtype->dtype_options); 602*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_cyl_skew = 603*7c478bd9Sstevel@tonic-gate get_cyl_skew(&cur_dtype->dtype_options); 604*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_trk_skew = 605*7c478bd9Sstevel@tonic-gate get_trk_skew(&cur_dtype->dtype_options); 606*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_trks_zone = 607*7c478bd9Sstevel@tonic-gate get_trks_zone(&cur_dtype->dtype_options); 608*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_atrks = get_atrks(&cur_dtype->dtype_options); 609*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_asect = get_asect(&cur_dtype->dtype_options); 610*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_cache = get_cache(&cur_dtype->dtype_options); 611*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_threshold = 612*7c478bd9Sstevel@tonic-gate get_threshold(&cur_dtype->dtype_options); 613*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_min = 614*7c478bd9Sstevel@tonic-gate get_min_prefetch(&cur_dtype->dtype_options); 615*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_max = 616*7c478bd9Sstevel@tonic-gate get_max_prefetch(cur_dtype->dtype_prefetch_min, 617*7c478bd9Sstevel@tonic-gate &cur_dtype->dtype_options); 618*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_phead = 619*7c478bd9Sstevel@tonic-gate get_phead(nhead, &cur_dtype->dtype_options); 620*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_psect = get_psect(&cur_dtype->dtype_options); 621*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_bps = get_bps(); 622*7c478bd9Sstevel@tonic-gate #ifdef sparc 623*7c478bd9Sstevel@tonic-gate cur_dtype->dtype_dr_type = 0; 624*7c478bd9Sstevel@tonic-gate #endif 625*7c478bd9Sstevel@tonic-gate } 626*7c478bd9Sstevel@tonic-gate } 627