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