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