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