xref: /titanic_53/usr/src/cmd/format/main.c (revision 3e1bd7a2aaeb6188caef90679b98088cfef1edc6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23052b6e8aSbg159949  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file contains the main entry point of the program and other
307c478bd9Sstevel@tonic-gate  * routines relating to the general flow.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate #include "global.h"
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <memory.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef sparc
427c478bd9Sstevel@tonic-gate #include <sys/hdio.h>
437c478bd9Sstevel@tonic-gate #include <sys/dkbad.h>
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/time.h>
477c478bd9Sstevel@tonic-gate #include "main.h"
487c478bd9Sstevel@tonic-gate #include "analyze.h"
497c478bd9Sstevel@tonic-gate #include "menu.h"
507c478bd9Sstevel@tonic-gate #include "param.h"
517c478bd9Sstevel@tonic-gate #include "misc.h"
527c478bd9Sstevel@tonic-gate #include "startup.h"
537c478bd9Sstevel@tonic-gate #include "menu_command.h"
547c478bd9Sstevel@tonic-gate #include "menu_partition.h"
557c478bd9Sstevel@tonic-gate #include "prompts.h"
56*3e1bd7a2Ssjelinek #include "checkdev.h"
577c478bd9Sstevel@tonic-gate #include "label.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate extern	struct menu_item menu_command[];
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifdef	__STDC__
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  *	Local prototypes for ANSI C compilers
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static void	get_disk_characteristics(void);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  *	Local prototypes for non-ANSI C compilers
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate static void	get_disk_characteristics();
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * This is the main entry point.
807c478bd9Sstevel@tonic-gate  */
81052b6e8aSbg159949 int
82052b6e8aSbg159949 main(int argc, char *argv[])
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	int	i;
857c478bd9Sstevel@tonic-gate 	int	ret_code = 1;
867c478bd9Sstevel@tonic-gate 	char	**arglist;
877c478bd9Sstevel@tonic-gate 	struct	disk_info *disk = NULL;
887c478bd9Sstevel@tonic-gate 	struct	disk_type *type, *oldtype;
897c478bd9Sstevel@tonic-gate 	struct	partition_info *parts;
907c478bd9Sstevel@tonic-gate 	struct	sigaction act;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	solaris_offset = 0;
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * Decode the command line options.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	i = do_options(argc, argv);
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * If we are to run from a command file, open it up.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 	if (option_f) {
1017c478bd9Sstevel@tonic-gate 		if (freopen(option_f, "r", stdin) == NULL) {
1027c478bd9Sstevel@tonic-gate 			err_print("Unable to open command file '%s'.\n",
1037c478bd9Sstevel@tonic-gate 				option_f);
1047c478bd9Sstevel@tonic-gate 			fullabort();
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * If we are logging, open the log file.
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	if (option_l) {
1117c478bd9Sstevel@tonic-gate 		if ((log_file = fopen(option_l, "w")) == NULL) {
1127c478bd9Sstevel@tonic-gate 			err_print("Unable to open log file '%s'.\n",
1137c478bd9Sstevel@tonic-gate 				option_l);
1147c478bd9Sstevel@tonic-gate 			fullabort();
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Read in the data file and initialize the hardware structs.
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 	sup_init();
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * If there are no disks on the command line, search the
1237c478bd9Sstevel@tonic-gate 	 * appropriate device directory for character devices that
1247c478bd9Sstevel@tonic-gate 	 * look like disks.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	if (i < 0) {
1277c478bd9Sstevel@tonic-gate 		arglist = (char **)NULL;
1287c478bd9Sstevel@tonic-gate 	/*
1297c478bd9Sstevel@tonic-gate 	 * There were disks on the command line.  They comprise the
1307c478bd9Sstevel@tonic-gate 	 * search list.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	} else {
1337c478bd9Sstevel@tonic-gate 		arglist = &argv[i];
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * Perform the search for disks.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	do_search(arglist);
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * Catch ctrl-C and ctrl-Z so critical sections can be
1417c478bd9Sstevel@tonic-gate 	 * implemented.  We use sigaction, as this sets up the
1427c478bd9Sstevel@tonic-gate 	 * signal handler permanently, and also automatically
1437c478bd9Sstevel@tonic-gate 	 * restarts any interrupted system call.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	act.sa_handler = cmdabort;
1467c478bd9Sstevel@tonic-gate 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1477c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_RESTART | SA_NODEFER;
1487c478bd9Sstevel@tonic-gate 	if (sigaction(SIGINT, &act, (struct sigaction *)NULL) == -1) {
1497c478bd9Sstevel@tonic-gate 		err_print("sigaction(SIGINT) failed - %s\n",
1507c478bd9Sstevel@tonic-gate 			strerror(errno));
1517c478bd9Sstevel@tonic-gate 		fullabort();
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	act.sa_handler = onsusp;
1557c478bd9Sstevel@tonic-gate 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1567c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_RESTART | SA_NODEFER;
1577c478bd9Sstevel@tonic-gate 	if (sigaction(SIGTSTP, &act, (struct sigaction *)NULL) == -1) {
1587c478bd9Sstevel@tonic-gate 		err_print("sigaction(SIGTSTP) failed - %s\n",
1597c478bd9Sstevel@tonic-gate 			strerror(errno));
1607c478bd9Sstevel@tonic-gate 		fullabort();
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	act.sa_handler = onalarm;
1647c478bd9Sstevel@tonic-gate 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1657c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_RESTART;
1667c478bd9Sstevel@tonic-gate 	if (sigaction(SIGALRM, &act, (struct sigaction *)NULL) == -1) {
1677c478bd9Sstevel@tonic-gate 		err_print("sigaction(SIGALRM) failed - %s\n",
1687c478bd9Sstevel@tonic-gate 			strerror(errno));
1697c478bd9Sstevel@tonic-gate 		fullabort();
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * If there was only 1 disk on the command line, mark it
1747c478bd9Sstevel@tonic-gate 	 * to be the current disk.  If it wasn't found, it's an error.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	if (i == argc - 1) {
1777c478bd9Sstevel@tonic-gate 		disk = disk_list;
1787c478bd9Sstevel@tonic-gate 		if (disk == NULL) {
1797c478bd9Sstevel@tonic-gate 			err_print("Unable to find specified disk '%s'.\n",
1807c478bd9Sstevel@tonic-gate 			    argv[i]);
1817c478bd9Sstevel@tonic-gate 			fullabort();
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * A disk was forced on the command line.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (option_d) {
1887c478bd9Sstevel@tonic-gate 		/*
1897c478bd9Sstevel@tonic-gate 		 * Find it in the list of found disks and mark it to
1907c478bd9Sstevel@tonic-gate 		 * be the current disk.
1917c478bd9Sstevel@tonic-gate 		 */
1927c478bd9Sstevel@tonic-gate 		for (disk = disk_list; disk != NULL; disk = disk->disk_next)
1937c478bd9Sstevel@tonic-gate 			if (diskname_match(option_d, disk))
1947c478bd9Sstevel@tonic-gate 				break;
1957c478bd9Sstevel@tonic-gate 		/*
1967c478bd9Sstevel@tonic-gate 		 * If it wasn't found, it's an error.
1977c478bd9Sstevel@tonic-gate 		 */
1987c478bd9Sstevel@tonic-gate 		if (disk == NULL) {
1997c478bd9Sstevel@tonic-gate 			err_print("Unable to find specified disk '%s'.\n",
2007c478bd9Sstevel@tonic-gate 			    option_d);
2017c478bd9Sstevel@tonic-gate 			fullabort();
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * A disk type was forced on the command line.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	if (option_t != NULL) {
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * Only legal if a disk was also forced.
2107c478bd9Sstevel@tonic-gate 		 */
2117c478bd9Sstevel@tonic-gate 		if (disk == NULL) {
2127c478bd9Sstevel@tonic-gate 			err_print("Must specify disk as well as type.\n");
2137c478bd9Sstevel@tonic-gate 			fullabort();
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		oldtype = disk->disk_type;
2167c478bd9Sstevel@tonic-gate 		/*
2177c478bd9Sstevel@tonic-gate 		 * Find the specified type in the list of legal types
2187c478bd9Sstevel@tonic-gate 		 * for the disk.
2197c478bd9Sstevel@tonic-gate 		 */
2207c478bd9Sstevel@tonic-gate 		for (type = disk->disk_ctlr->ctlr_ctype->ctype_dlist;
2217c478bd9Sstevel@tonic-gate 		    type != NULL; type = type->dtype_next)
2227c478bd9Sstevel@tonic-gate 			if (strcmp(option_t, type->dtype_asciilabel) == 0)
2237c478bd9Sstevel@tonic-gate 				break;
2247c478bd9Sstevel@tonic-gate 		/*
2257c478bd9Sstevel@tonic-gate 		 * If it wasn't found, it's an error.
2267c478bd9Sstevel@tonic-gate 		 */
2277c478bd9Sstevel@tonic-gate 		if (type == NULL) {
2287c478bd9Sstevel@tonic-gate 			err_print(
2297c478bd9Sstevel@tonic-gate "Specified type '%s' is not a known type.\n", option_t);
2307c478bd9Sstevel@tonic-gate 			fullabort();
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 		/*
2337c478bd9Sstevel@tonic-gate 		 * If the specified type is not the same as the type
2347c478bd9Sstevel@tonic-gate 		 * in the disk label, update the type and nullify the
2357c478bd9Sstevel@tonic-gate 		 * partition map.
2367c478bd9Sstevel@tonic-gate 		 */
2377c478bd9Sstevel@tonic-gate 		if (type != oldtype) {
2387c478bd9Sstevel@tonic-gate 			disk->disk_type = type;
2397c478bd9Sstevel@tonic-gate 			disk->disk_parts = NULL;
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * A partition map was forced on the command line.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	if (option_p) {
2467c478bd9Sstevel@tonic-gate 		/*
2477c478bd9Sstevel@tonic-gate 		 * Only legal if both disk and type were also forced.
2487c478bd9Sstevel@tonic-gate 		 */
2497c478bd9Sstevel@tonic-gate 		if (disk == NULL || disk->disk_type == NULL) {
2507c478bd9Sstevel@tonic-gate 			err_print("Must specify disk and type as well ");
2517c478bd9Sstevel@tonic-gate 			err_print("as partitiion.\n");
2527c478bd9Sstevel@tonic-gate 			fullabort();
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 		/*
2557c478bd9Sstevel@tonic-gate 		 * Find the specified map in the list of legal maps
2567c478bd9Sstevel@tonic-gate 		 * for the type.
2577c478bd9Sstevel@tonic-gate 		 */
2587c478bd9Sstevel@tonic-gate 		for (parts = disk->disk_type->dtype_plist; parts != NULL;
2597c478bd9Sstevel@tonic-gate 		    parts = parts->pinfo_next)
2607c478bd9Sstevel@tonic-gate 			if (strcmp(option_p, parts->pinfo_name) == 0)
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 		/*
2637c478bd9Sstevel@tonic-gate 		 * If it wasn't found, it's an error.
2647c478bd9Sstevel@tonic-gate 		 */
2657c478bd9Sstevel@tonic-gate 		if (parts == NULL) {
2667c478bd9Sstevel@tonic-gate 			err_print(
2677c478bd9Sstevel@tonic-gate "Specified table '%s' is not a known table.\n", option_p);
2687c478bd9Sstevel@tonic-gate 			fullabort();
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * Update the map.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		disk->disk_parts = parts;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * If a disk was marked to become current, initialize the state
2777c478bd9Sstevel@tonic-gate 	 * to make it current.  If not, ask user to pick one.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	if (disk != NULL) {
2807c478bd9Sstevel@tonic-gate 		init_globals(disk);
2817c478bd9Sstevel@tonic-gate 	} else if (option_f == 0 && option_d == 0) {
2827c478bd9Sstevel@tonic-gate 		while (ret_code) {
2837c478bd9Sstevel@tonic-gate 			ret_code = c_disk();
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate #ifdef	BUG1134748
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * if -f command-file is specified, check for disk and disktype
2907c478bd9Sstevel@tonic-gate 	 * input also. For SCSI disks, the type input may not be needed
2917c478bd9Sstevel@tonic-gate 	 * since format would have figured that using inquiry information.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	if (option_f) {
2947c478bd9Sstevel@tonic-gate 		if (cur_disk == NULL) {
2957c478bd9Sstevel@tonic-gate 			err_print("Must specify a disk using -d option.\n");
2967c478bd9Sstevel@tonic-gate 			fullabort();
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		if (cur_dtype == NULL) {
2997c478bd9Sstevel@tonic-gate 			err_print("Must specify disk as well as type.\n");
3007c478bd9Sstevel@tonic-gate 			fullabort();
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate #endif	/* BUG1134748 */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * Run the command menu.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 	cur_menu = last_menu = 0;
3097c478bd9Sstevel@tonic-gate 	run_menu(menu_command, "FORMAT", "format", 1);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
312052b6e8aSbg159949 	 * normal ending. Explicitly return(0);
3137c478bd9Sstevel@tonic-gate 	 */
314052b6e8aSbg159949 	return (0);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * This routine initializes the internal state to ready it for a new
3197c478bd9Sstevel@tonic-gate  * current disk.  There are a zillion state variables that store
3207c478bd9Sstevel@tonic-gate  * information on the current disk, and they must all be updated.
3217c478bd9Sstevel@tonic-gate  * We also tell SunOS about the disk, since it may not know if the
3227c478bd9Sstevel@tonic-gate  * disk wasn't labeled at boot time.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate void
3257c478bd9Sstevel@tonic-gate init_globals(disk)
3267c478bd9Sstevel@tonic-gate 	struct	disk_info *disk;
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	int		status;
3297c478bd9Sstevel@tonic-gate #ifdef sparc
3307c478bd9Sstevel@tonic-gate 	int		i;
3317c478bd9Sstevel@tonic-gate 	caddr_t		bad_ptr = (caddr_t)&badmap;
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * If there was an old current disk, close the file for it.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	if (cur_disk != NULL)
3387c478bd9Sstevel@tonic-gate 		(void) close(cur_file);
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * Kill off any defect lists still lying around.
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	kill_deflist(&cur_list);
3437c478bd9Sstevel@tonic-gate 	kill_deflist(&work_list);
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * If there were any buffers, free them up.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	if ((char *)cur_buf != NULL) {
3487c478bd9Sstevel@tonic-gate 		destroy_data((char *)cur_buf);
3497c478bd9Sstevel@tonic-gate 		cur_buf = NULL;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	if ((char *)pattern_buf != NULL) {
3527c478bd9Sstevel@tonic-gate 		destroy_data((char *)pattern_buf);
3537c478bd9Sstevel@tonic-gate 		pattern_buf = NULL;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * Fill in the hardware struct pointers for the new disk.
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	cur_disk = disk;
3597c478bd9Sstevel@tonic-gate 	cur_dtype = cur_disk->disk_type;
3607c478bd9Sstevel@tonic-gate 	cur_label = cur_disk->label_type;
3617c478bd9Sstevel@tonic-gate 	cur_ctlr = cur_disk->disk_ctlr;
3627c478bd9Sstevel@tonic-gate 	cur_parts = cur_disk->disk_parts;
3637c478bd9Sstevel@tonic-gate 	cur_ctype = cur_ctlr->ctlr_ctype;
3647c478bd9Sstevel@tonic-gate 	cur_ops = cur_ctype->ctype_ops;
3657c478bd9Sstevel@tonic-gate 	cur_flags = 0;
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Open a file for the new disk.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if ((cur_file = open_disk(cur_disk->disk_path,
3707c478bd9Sstevel@tonic-gate 					O_RDWR | O_NDELAY)) < 0) {
3717c478bd9Sstevel@tonic-gate 		err_print(
3727c478bd9Sstevel@tonic-gate "Error: can't open selected disk '%s'.\n", cur_disk->disk_name);
3737c478bd9Sstevel@tonic-gate 		fullabort();
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate #ifdef sparc
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * If the new disk uses bad-144, initialize the bad block table.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	if (cur_ctlr->ctlr_flags & DKI_BAD144) {
3807c478bd9Sstevel@tonic-gate 		badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0;
3817c478bd9Sstevel@tonic-gate 		for (i = 0; i < NDKBAD; i++) {
3827c478bd9Sstevel@tonic-gate 			badmap.bt_bad[i].bt_cyl = -1;
3837c478bd9Sstevel@tonic-gate 			badmap.bt_bad[i].bt_trksec = -1;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * If the type of the new disk is known...
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	if (cur_dtype != NULL) {
3917c478bd9Sstevel@tonic-gate 		/*
3927c478bd9Sstevel@tonic-gate 		 * Initialize the physical characteristics.
3937c478bd9Sstevel@tonic-gate 		 * If need disk specs, prompt for undefined disk
3947c478bd9Sstevel@tonic-gate 		 * characteristics.  If running from a file,
3957c478bd9Sstevel@tonic-gate 		 * use defaults.
3967c478bd9Sstevel@tonic-gate 		 */
3977c478bd9Sstevel@tonic-gate 		if (cur_dtype->dtype_flags & DT_NEED_SPEFS) {
3987c478bd9Sstevel@tonic-gate 			get_disk_characteristics();
3997c478bd9Sstevel@tonic-gate 			cur_dtype->dtype_flags &= ~DT_NEED_SPEFS;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		ncyl = cur_dtype->dtype_ncyl;
4037c478bd9Sstevel@tonic-gate 		acyl = cur_dtype->dtype_acyl;
4047c478bd9Sstevel@tonic-gate 		pcyl = cur_dtype->dtype_pcyl;
4057c478bd9Sstevel@tonic-gate 		nhead = cur_dtype->dtype_nhead;
4067c478bd9Sstevel@tonic-gate 		nsect = cur_dtype->dtype_nsect;
4077c478bd9Sstevel@tonic-gate 		phead = cur_dtype->dtype_phead;
4087c478bd9Sstevel@tonic-gate 		psect = cur_dtype->dtype_psect;
4097c478bd9Sstevel@tonic-gate 		/*
4107c478bd9Sstevel@tonic-gate 		 * Alternates per cylinder are forced to 0 or 1,
4117c478bd9Sstevel@tonic-gate 		 * independent of what the label says.  This works
4127c478bd9Sstevel@tonic-gate 		 * because we know which ctlr we are dealing with.
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		if (cur_ctype->ctype_flags & CF_APC)
4157c478bd9Sstevel@tonic-gate 			apc = 1;
4167c478bd9Sstevel@tonic-gate 		else
4177c478bd9Sstevel@tonic-gate 			apc = 0;
4187c478bd9Sstevel@tonic-gate 		/*
4197c478bd9Sstevel@tonic-gate 		 * Initialize the surface analysis info.  We always start
4207c478bd9Sstevel@tonic-gate 		 * out with scan set for the whole disk.  Note,
4217c478bd9Sstevel@tonic-gate 		 * for SCSI disks, we can only scan the data area.
4227c478bd9Sstevel@tonic-gate 		 */
4237c478bd9Sstevel@tonic-gate 		scan_lower = 0;
4247c478bd9Sstevel@tonic-gate 		scan_size = BUF_SECTS;
4257c478bd9Sstevel@tonic-gate 		if ((cur_ctype->ctype_flags & CF_SCSI) &&
4267c478bd9Sstevel@tonic-gate 		    (cur_disk->label_type == L_TYPE_SOLARIS)) {
4277c478bd9Sstevel@tonic-gate 			scan_upper = datasects() - 1;
4287c478bd9Sstevel@tonic-gate 		} else if (cur_disk->label_type == L_TYPE_SOLARIS) {
4297c478bd9Sstevel@tonic-gate 			scan_upper = physsects() - 1;
4307c478bd9Sstevel@tonic-gate 		} else if (cur_disk->label_type == L_TYPE_EFI) {
4317c478bd9Sstevel@tonic-gate 			scan_upper = cur_parts->etoc->efi_last_lba;
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * Allocate the buffers.
4367c478bd9Sstevel@tonic-gate 		 */
4377c478bd9Sstevel@tonic-gate 		cur_buf = (void *) zalloc(BUF_SECTS * SECSIZE);
4387c478bd9Sstevel@tonic-gate 		pattern_buf = (void *) zalloc(BUF_SECTS * SECSIZE);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/*
4417c478bd9Sstevel@tonic-gate 		 * Tell the user which disk (s)he selected.
4427c478bd9Sstevel@tonic-gate 		 */
4437c478bd9Sstevel@tonic-gate 		if (chk_volname(cur_disk)) {
4447c478bd9Sstevel@tonic-gate 			fmt_print("selecting %s: ", cur_disk->disk_name);
4457c478bd9Sstevel@tonic-gate 			print_volname(cur_disk);
4467c478bd9Sstevel@tonic-gate 			fmt_print("\n");
4477c478bd9Sstevel@tonic-gate 		} else {
4487c478bd9Sstevel@tonic-gate 			fmt_print("selecting %s\n", cur_disk->disk_name);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		/*
4527c478bd9Sstevel@tonic-gate 		 * If the drive is formatted...
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		if ((*cur_ops->op_ck_format)()) {
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 * Mark it formatted.
4577c478bd9Sstevel@tonic-gate 			 */
4587c478bd9Sstevel@tonic-gate 			cur_flags |= DISK_FORMATTED;
4597c478bd9Sstevel@tonic-gate 			/*
4607c478bd9Sstevel@tonic-gate 			 * Read the defect list, if we have one.
4617c478bd9Sstevel@tonic-gate 			 */
4627c478bd9Sstevel@tonic-gate 			if (!EMBEDDED_SCSI) {
4637c478bd9Sstevel@tonic-gate 				read_list(&cur_list);
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate #ifdef sparc
4667c478bd9Sstevel@tonic-gate 			/*
4677c478bd9Sstevel@tonic-gate 			 * If the disk does BAD-144, we do an ioctl to
4687c478bd9Sstevel@tonic-gate 			 * tell SunOS about the bad block table.
4697c478bd9Sstevel@tonic-gate 			 */
4707c478bd9Sstevel@tonic-gate 			if (cur_ctlr->ctlr_flags & DKI_BAD144) {
4717c478bd9Sstevel@tonic-gate 				if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr)) {
4727c478bd9Sstevel@tonic-gate 					err_print(
4737c478bd9Sstevel@tonic-gate "Warning: error telling SunOS bad block map table.\n");
4747c478bd9Sstevel@tonic-gate 				}
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate 			fmt_print("[disk formatted");
4787c478bd9Sstevel@tonic-gate 			if (!EMBEDDED_SCSI) {
4797c478bd9Sstevel@tonic-gate 				if (cur_list.list != NULL) {
4807c478bd9Sstevel@tonic-gate 					fmt_print(", defect list found");
4817c478bd9Sstevel@tonic-gate 				} else {
4827c478bd9Sstevel@tonic-gate 					fmt_print(", no defect list found");
4837c478bd9Sstevel@tonic-gate 				}
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate 			fmt_print("]");
4867c478bd9Sstevel@tonic-gate 		/*
4877c478bd9Sstevel@tonic-gate 		 * Drive wasn't formatted.  Tell the user in case he
4887c478bd9Sstevel@tonic-gate 		 * disagrees.
4897c478bd9Sstevel@tonic-gate 		 */
4907c478bd9Sstevel@tonic-gate 		} else if (EMBEDDED_SCSI) {
4917c478bd9Sstevel@tonic-gate 			fmt_print("[disk unformatted]");
4927c478bd9Sstevel@tonic-gate 		} else {
4937c478bd9Sstevel@tonic-gate 			/*
4947c478bd9Sstevel@tonic-gate 			 * Make sure the user is serious.  Note, for
4957c478bd9Sstevel@tonic-gate 			 * SCSI disks since this is instantaneous, we
4967c478bd9Sstevel@tonic-gate 			 * will just do it and not ask for confirmation.
4977c478bd9Sstevel@tonic-gate 			 */
4987c478bd9Sstevel@tonic-gate 			status = 0;
4997c478bd9Sstevel@tonic-gate 			if (!(cur_ctype->ctype_flags & CF_CONFIRM)) {
5007c478bd9Sstevel@tonic-gate 				if (check("\n\
5017c478bd9Sstevel@tonic-gate Ready to get manufacturer's defect list from unformatted drive.\n\
5027c478bd9Sstevel@tonic-gate This cannot be interrupted and takes a long while.\n\
5037c478bd9Sstevel@tonic-gate Continue"))
5047c478bd9Sstevel@tonic-gate 					status = 1;
5057c478bd9Sstevel@tonic-gate 				else
5067c478bd9Sstevel@tonic-gate 					fmt_print(
5077c478bd9Sstevel@tonic-gate 				"Extracting manufacturer's defect list...");
5087c478bd9Sstevel@tonic-gate 			}
5097c478bd9Sstevel@tonic-gate 			/*
5107c478bd9Sstevel@tonic-gate 			 * Extract manufacturer's defect list.
5117c478bd9Sstevel@tonic-gate 			 */
5127c478bd9Sstevel@tonic-gate 			if ((status == 0) && (cur_ops->op_ex_man != NULL)) {
5137c478bd9Sstevel@tonic-gate 				status = (*cur_ops->op_ex_man)(&cur_list);
5147c478bd9Sstevel@tonic-gate 			} else {
5157c478bd9Sstevel@tonic-gate 				status = 1;
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 			fmt_print("[disk unformatted");
5187c478bd9Sstevel@tonic-gate 			if (status != 0) {
5197c478bd9Sstevel@tonic-gate 				fmt_print(", no defect list found]");
5207c478bd9Sstevel@tonic-gate 			} else {
5217c478bd9Sstevel@tonic-gate 				fmt_print(", defect list found]");
5227c478bd9Sstevel@tonic-gate 			}
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 	} else {
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * Disk type is not known.
5277c478bd9Sstevel@tonic-gate 		 * Initialize physical characteristics to 0 and tell the
5287c478bd9Sstevel@tonic-gate 		 * user we don't know what type the disk is.
5297c478bd9Sstevel@tonic-gate 		 */
5307c478bd9Sstevel@tonic-gate 		ncyl = acyl = nhead = nsect = psect = 0;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	fmt_print("\n");
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * Check to see if there are any mounted file systems on the
5377c478bd9Sstevel@tonic-gate 	 * disk.  If there are, print a warning.
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	if (checkmount((daddr_t)-1, (daddr_t)-1))
5407c478bd9Sstevel@tonic-gate 		err_print("Warning: Current Disk has mounted partitions.\n");
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
543*3e1bd7a2Ssjelinek 	 * If any part of this device is also part of an SVM, VxVM or
544*3e1bd7a2Ssjelinek 	 * Live Upgrade device, print a warning.
545*3e1bd7a2Ssjelinek 	 */
546*3e1bd7a2Ssjelinek 	(void) checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
547*3e1bd7a2Ssjelinek 	    (diskaddr_t)-1, 1, 0);
548*3e1bd7a2Ssjelinek 
549*3e1bd7a2Ssjelinek 	/*
5507c478bd9Sstevel@tonic-gate 	 * Get the Solaris Fdisk Partition information
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	(void) copy_solaris_part(&cur_disk->fdisk_part);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Prompt for some undefined disk characteristics.
5587c478bd9Sstevel@tonic-gate  * Used when there is no disk definition, but the
5597c478bd9Sstevel@tonic-gate  * disk has a valid label, so basically we're
5607c478bd9Sstevel@tonic-gate  * prompting for everything that isn't in the label.
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate static void
5637c478bd9Sstevel@tonic-gate get_disk_characteristics()
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * The need_spefs flag is used to tell us that this disk
5677c478bd9Sstevel@tonic-gate 	 * is not a known type and the ctlr specific info must
5687c478bd9Sstevel@tonic-gate 	 * be prompted for.  We only prompt for the info that applies
5697c478bd9Sstevel@tonic-gate 	 * to this ctlr.
5707c478bd9Sstevel@tonic-gate 	 */
5717c478bd9Sstevel@tonic-gate 	assert(cur_dtype->dtype_flags & DT_NEED_SPEFS);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * If we're running with input from a file, use
5757c478bd9Sstevel@tonic-gate 	 * reasonable defaults, since prompting for the
5767c478bd9Sstevel@tonic-gate 	 * information will probably mess things up.
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	if (option_f) {
5797c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_pcyl = ncyl + acyl;
5807c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_rpm = AVG_RPM;
5817c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_bpt = INFINITY;
5827c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_phead = 0;
5837c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_psect = 0;
5847c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_cyl_skew = 0;
5857c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_trk_skew = 0;
5867c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_trks_zone = 0;
5877c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_atrks = 0;
5887c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_asect = 0;
5897c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_cache = 0;
5907c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_threshold = 0;
5917c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_prefetch_min = 0;
5927c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_prefetch_max = 0;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		if (cur_ctype->ctype_flags & CF_SMD_DEFS) {
5957c478bd9Sstevel@tonic-gate 			cur_dtype->dtype_bps = AVG_BPS;
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 	} else {
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_pcyl = get_pcyl(ncyl, cur_dtype->dtype_acyl);
6007c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_bpt = get_bpt(cur_dtype->dtype_nsect,
6017c478bd9Sstevel@tonic-gate 			&cur_dtype->dtype_options);
6027c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_rpm = get_rpm();
6037c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_fmt_time =
6047c478bd9Sstevel@tonic-gate 			get_fmt_time(&cur_dtype->dtype_options);
6057c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_cyl_skew =
6067c478bd9Sstevel@tonic-gate 			get_cyl_skew(&cur_dtype->dtype_options);
6077c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_trk_skew =
6087c478bd9Sstevel@tonic-gate 			get_trk_skew(&cur_dtype->dtype_options);
6097c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_trks_zone =
6107c478bd9Sstevel@tonic-gate 			get_trks_zone(&cur_dtype->dtype_options);
6117c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_atrks = get_atrks(&cur_dtype->dtype_options);
6127c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_asect = get_asect(&cur_dtype->dtype_options);
6137c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_cache = get_cache(&cur_dtype->dtype_options);
6147c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_threshold =
6157c478bd9Sstevel@tonic-gate 			get_threshold(&cur_dtype->dtype_options);
6167c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_prefetch_min =
6177c478bd9Sstevel@tonic-gate 			get_min_prefetch(&cur_dtype->dtype_options);
6187c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_prefetch_max =
6197c478bd9Sstevel@tonic-gate 			get_max_prefetch(cur_dtype->dtype_prefetch_min,
6207c478bd9Sstevel@tonic-gate 			&cur_dtype->dtype_options);
6217c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_phead =
6227c478bd9Sstevel@tonic-gate 			get_phead(nhead, &cur_dtype->dtype_options);
6237c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_psect = get_psect(&cur_dtype->dtype_options);
6247c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_bps = get_bps();
6257c478bd9Sstevel@tonic-gate #ifdef sparc
6267c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_dr_type = 0;
6277c478bd9Sstevel@tonic-gate #endif
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate }
630