xref: /onnv-gate/usr/src/cmd/format/menu_command.c (revision 12594:d6a845bf8951)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51524Slh195018  * Common Development and Distribution License (the "License").
61524Slh195018  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12594SShengliang.Zhang@Sun.COM  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * This file contains functions that implement the command menu commands.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "global.h"
300Sstevel@tonic-gate #include <time.h>
310Sstevel@tonic-gate #include <sys/time.h>
320Sstevel@tonic-gate #include <sys/resource.h>
330Sstevel@tonic-gate #include <sys/wait.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <signal.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #if defined(sparc)
400Sstevel@tonic-gate #include <sys/hdio.h>
410Sstevel@tonic-gate #endif /* defined(sparc) */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "main.h"
440Sstevel@tonic-gate #include "analyze.h"
450Sstevel@tonic-gate #include "menu.h"
460Sstevel@tonic-gate #include "menu_command.h"
470Sstevel@tonic-gate #include "menu_defect.h"
480Sstevel@tonic-gate #include "menu_partition.h"
490Sstevel@tonic-gate #include "param.h"
500Sstevel@tonic-gate #include "misc.h"
510Sstevel@tonic-gate #include "label.h"
520Sstevel@tonic-gate #include "startup.h"
530Sstevel@tonic-gate #include "partition.h"
540Sstevel@tonic-gate #include "prompts.h"
55767Ssjelinek #include "checkdev.h"
560Sstevel@tonic-gate #include "io.h"
570Sstevel@tonic-gate #include "ctlr_scsi.h"
580Sstevel@tonic-gate #include "auto_sense.h"
590Sstevel@tonic-gate #include "modify_partition.h"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 
620Sstevel@tonic-gate extern	struct menu_item menu_partition[];
630Sstevel@tonic-gate extern	struct menu_item menu_analyze[];
640Sstevel@tonic-gate extern	struct menu_item menu_defect[];
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Choices for the p_tag vtoc field
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate slist_t	ptag_choices[] = {
700Sstevel@tonic-gate 	{ "unassigned",	"",	V_UNASSIGNED	},
710Sstevel@tonic-gate 	{ "boot",	"",	V_BOOT		},
720Sstevel@tonic-gate 	{ "root",	"",	V_ROOT		},
730Sstevel@tonic-gate 	{ "swap",	"",	V_SWAP		},
740Sstevel@tonic-gate 	{ "usr",	"",	V_USR		},
750Sstevel@tonic-gate 	{ "backup",	"",	V_BACKUP	},
760Sstevel@tonic-gate 	{ "stand",	"",	V_STAND		},
770Sstevel@tonic-gate 	{ "var",	"",	V_VAR		},
780Sstevel@tonic-gate 	{ "home",	"",	V_HOME		},
790Sstevel@tonic-gate 	{ "alternates",	"",	V_ALTSCTR	},
800Sstevel@tonic-gate 	{ "reserved",	"",	V_RESERVED	},
810Sstevel@tonic-gate 	{ NULL }
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * Choices for the p_flag vtoc field
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate slist_t	pflag_choices[] = {
890Sstevel@tonic-gate 	{ "wm",	"read-write, mountable",	0		},
900Sstevel@tonic-gate 	{ "wu",	"read-write, unmountable",	V_UNMNT		},
910Sstevel@tonic-gate 	{ "rm",	"read-only, mountable",		V_RONLY		},
920Sstevel@tonic-gate 	{ "ru",	"read-only, unmountable",	V_RONLY|V_UNMNT	},
930Sstevel@tonic-gate 	{ NULL }
940Sstevel@tonic-gate };
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * This routine implements the 'disk' command.  It allows the user to
990Sstevel@tonic-gate  * select a disk to be current.  The list of choices is the list of
1000Sstevel@tonic-gate  * disks that were found at startup time.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate int
c_disk()1030Sstevel@tonic-gate c_disk()
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	struct disk_info	*disk;
1060Sstevel@tonic-gate 	u_ioparam_t		ioparam;
1070Sstevel@tonic-gate 	int			i;
1080Sstevel@tonic-gate 	int			ndisks = 0;
1090Sstevel@tonic-gate 	int			blind_select = 0;
1100Sstevel@tonic-gate 	int			deflt;
1110Sstevel@tonic-gate 	int			index;
1120Sstevel@tonic-gate 	int			*defltptr = NULL;
1130Sstevel@tonic-gate 	int			more = 0;
1140Sstevel@tonic-gate 	int			more_quit = 0;
1150Sstevel@tonic-gate 	int			one_line = 0;
1160Sstevel@tonic-gate 	int			tty_lines;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * This buffer holds the check() prompt that verifies we've got the right
1200Sstevel@tonic-gate  * disk when performing a blind selection.  The size should be sufficient
1210Sstevel@tonic-gate  * to hold the prompt string, plus 256 characters for the disk name -
1220Sstevel@tonic-gate  * way more than should ever be necessary.  See the #define in misc.h.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate 	char			chk_buf[BLIND_SELECT_VER_PROMPT];
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (istokenpresent()) {
1270Sstevel@tonic-gate 		/*
1280Sstevel@tonic-gate 		 * disk number to be selected is already in the
1290Sstevel@tonic-gate 		 * input stream .
1300Sstevel@tonic-gate 		 */
1310Sstevel@tonic-gate 		TOKEN token, cleantoken;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		/*
1340Sstevel@tonic-gate 		 * Get the disk number the user has given.
1350Sstevel@tonic-gate 		 */
1360Sstevel@tonic-gate 		i = 0;
1370Sstevel@tonic-gate 		for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
1380Sstevel@tonic-gate 			i++;
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		ioparam.io_bounds.lower = 0;
1420Sstevel@tonic-gate 		ioparam.io_bounds.upper = i - 1;
1430Sstevel@tonic-gate 		(void) gettoken(token);
1440Sstevel@tonic-gate 		clean_token(cleantoken, token);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		/*
1470Sstevel@tonic-gate 		 * Convert the token into an integer.
1480Sstevel@tonic-gate 		 */
1497563SPrasad.Singamsetty@Sun.COM 		if (geti(cleantoken, &index, (int *)NULL))
1500Sstevel@tonic-gate 			return (0);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		/*
1530Sstevel@tonic-gate 		 * Check to be sure it is within the legal bounds.
1540Sstevel@tonic-gate 		 */
1550Sstevel@tonic-gate 		if ((index < 0) || (index >= i)) {
1560Sstevel@tonic-gate 			err_print("`%d' is out of range.\n", index);
1570Sstevel@tonic-gate 			return (0);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		goto checkdisk;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	fmt_print("\n\nAVAILABLE DISK SELECTIONS:\n");
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	i = 0;
1650Sstevel@tonic-gate 	if ((option_f == (char *)NULL) && isatty(0) == 1 && isatty(1) == 1) {
1660Sstevel@tonic-gate 		/*
1670Sstevel@tonic-gate 		 * We have a real terminal for std input and output, enable
1680Sstevel@tonic-gate 		 * more style of output for disk selection list.
1690Sstevel@tonic-gate 		 */
1700Sstevel@tonic-gate 		more = 1;
1710Sstevel@tonic-gate 		tty_lines = get_tty_lines();
1720Sstevel@tonic-gate 		enter_critical();
1730Sstevel@tonic-gate 		echo_off();
1740Sstevel@tonic-gate 		charmode_on();
1750Sstevel@tonic-gate 		exit_critical();
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/*
1790Sstevel@tonic-gate 	 * Loop through the list of found disks.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
1820Sstevel@tonic-gate 		/*
1830Sstevel@tonic-gate 		 * If using more output, account 2 lines for each disk.
1840Sstevel@tonic-gate 		 */
1850Sstevel@tonic-gate 		if (more && !more_quit && i && (one_line ||
1860Sstevel@tonic-gate 		    ((2 * i + 1) % (tty_lines - 2) <= 1))) {
1870Sstevel@tonic-gate 			int	c;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 			/*
1900Sstevel@tonic-gate 			 * Get the next character.
1910Sstevel@tonic-gate 			 */
1920Sstevel@tonic-gate 			fmt_print("- hit space for more or s to select - ");
1930Sstevel@tonic-gate 			c = getchar();
1940Sstevel@tonic-gate 			fmt_print("\015");
1950Sstevel@tonic-gate 			one_line = 0;
1960Sstevel@tonic-gate 			/*
1970Sstevel@tonic-gate 			 * Handle display one line command
1980Sstevel@tonic-gate 			 * (return key)
1990Sstevel@tonic-gate 			 */
2000Sstevel@tonic-gate 			if (c == '\012') {
2010Sstevel@tonic-gate 				one_line++;
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 			/* Handle Quit command */
2040Sstevel@tonic-gate 			if (c == 'q') {
2050Sstevel@tonic-gate 				fmt_print(
2060Sstevel@tonic-gate 				"                       \015");
2070Sstevel@tonic-gate 				more_quit++;
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 			/* Handle ^D command */
2100Sstevel@tonic-gate 			if (c == '\004')
2110Sstevel@tonic-gate 				fullabort();
2120Sstevel@tonic-gate 			/* or get on with the show */
2130Sstevel@tonic-gate 			if (c == 's' || c == 'S') {
2140Sstevel@tonic-gate 				fmt_print("%80s\n", " ");
2150Sstevel@tonic-gate 				break;
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 		/*
2190Sstevel@tonic-gate 		 * If this is the current disk, mark it as
2200Sstevel@tonic-gate 		 * the default.
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		if (cur_disk == disk) {
2230Sstevel@tonic-gate 			deflt = i;
2240Sstevel@tonic-gate 			defltptr = &deflt;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		if (!more || !more_quit)
2270Sstevel@tonic-gate 			pr_diskline(disk, i);
2280Sstevel@tonic-gate 		i++;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 	if (more) {
2310Sstevel@tonic-gate 		enter_critical();
2320Sstevel@tonic-gate 		charmode_off();
2330Sstevel@tonic-gate 		echo_on();
2340Sstevel@tonic-gate 		exit_critical();
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * Determine total number of disks, and ask the user which disk he
2390Sstevel@tonic-gate 	 * would like to make current.
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
2430Sstevel@tonic-gate 		ndisks++;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	ioparam.io_bounds.lower = 0;
2470Sstevel@tonic-gate 	ioparam.io_bounds.upper = ndisks - 1;
2480Sstevel@tonic-gate 	index = input(FIO_INT, "Specify disk (enter its number)", ':',
2490Sstevel@tonic-gate 	    &ioparam, defltptr, DATA_INPUT);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (index >= i) {
2520Sstevel@tonic-gate 		blind_select = 1;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Find the disk chosen.  Search through controllers/disks
2570Sstevel@tonic-gate 	 * in the same original order, so we match what the user
2580Sstevel@tonic-gate 	 * chose.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate checkdisk:
2610Sstevel@tonic-gate 	i = 0;
2620Sstevel@tonic-gate 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
2630Sstevel@tonic-gate 		if (i == index)
2640Sstevel@tonic-gate 			goto found;
2650Sstevel@tonic-gate 		i++;
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	/*
2680Sstevel@tonic-gate 	 * Should never happen.
2690Sstevel@tonic-gate 	 */
2700Sstevel@tonic-gate 	impossible("no disk found");
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate found:
2730Sstevel@tonic-gate 	if (blind_select) {
2740Sstevel@tonic-gate 		(void) snprintf(chk_buf, sizeof (chk_buf),
2750Sstevel@tonic-gate "Disk %s selected - is this the desired disk? ", disk->disk_name);
2760Sstevel@tonic-gate 		if (check(chk_buf)) {
2770Sstevel@tonic-gate 			return (-1);
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Update the state.  We lock out interrupts so the state can't
2830Sstevel@tonic-gate 	 * get half-updated.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	enter_critical();
2870Sstevel@tonic-gate 	init_globals(disk);
2880Sstevel@tonic-gate 	exit_critical();
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * If type unknown and interactive, ask user to specify type.
2920Sstevel@tonic-gate 	 * Also, set partition table (best guess) too.
2930Sstevel@tonic-gate 	 */
2940Sstevel@tonic-gate 	if (!option_f && ncyl == 0 && nhead == 0 && nsect == 0 &&
2959889SLarry.Liu@Sun.COM 	    (disk->label_type != L_TYPE_EFI)) {
2969889SLarry.Liu@Sun.COM 		(void) c_type();
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/*
3000Sstevel@tonic-gate 	 * Get the Solaris Fdisk Partition information
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	if (nhead != 0 && nsect != 0)
3030Sstevel@tonic-gate 		(void) copy_solaris_part(&cur_disk->fdisk_part);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if ((cur_disk->label_type == L_TYPE_EFI) &&
3060Sstevel@tonic-gate 	    (cur_disk->disk_parts->etoc->efi_flags &
3079889SLarry.Liu@Sun.COM 	    EFI_GPT_PRIMARY_CORRUPT)) {
3089889SLarry.Liu@Sun.COM 		err_print("Reading the primary EFI GPT label ");
3099889SLarry.Liu@Sun.COM 		err_print("failed.  Using backup label.\n");
3109889SLarry.Liu@Sun.COM 		err_print("Use the 'backup' command to restore ");
3119889SLarry.Liu@Sun.COM 		err_print("the primary label.\n");
3120Sstevel@tonic-gate 	}
3137910SBo.Zhou@Sun.COM 
3147910SBo.Zhou@Sun.COM #if defined(_SUNOS_VTOC_16)
3157910SBo.Zhou@Sun.COM 	/*
3167910SBo.Zhou@Sun.COM 	 * If there is no fdisk solaris partition.
3177910SBo.Zhou@Sun.COM 	 */
3187910SBo.Zhou@Sun.COM 	if (cur_disk->fdisk_part.numsect == 0) {
3197910SBo.Zhou@Sun.COM 		err_print("No Solaris fdisk partition found.\n");
3207910SBo.Zhou@Sun.COM 		goto exit;
3217910SBo.Zhou@Sun.COM 	}
3227910SBo.Zhou@Sun.COM #endif /* defined(_SUNOS_VTOC_16) */
3237910SBo.Zhou@Sun.COM 
3240Sstevel@tonic-gate 	/*
3250Sstevel@tonic-gate 	 * If the label of the disk is marked dirty,
3260Sstevel@tonic-gate 	 * see if they'd like to label the disk now.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	if (cur_disk->disk_flags & DSK_LABEL_DIRTY) {
3290Sstevel@tonic-gate 		if (check("Disk not labeled.  Label it now") == 0) {
3300Sstevel@tonic-gate 			if (write_label()) {
3310Sstevel@tonic-gate 				err_print("Write label failed\n");
3320Sstevel@tonic-gate 			} else {
3330Sstevel@tonic-gate 				cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 	}
3377611SBo.Zhou@Sun.COM exit:
3380Sstevel@tonic-gate 	return (0);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * This routine implements the 'type' command.  It allows the user to
3430Sstevel@tonic-gate  * specify the type of the current disk.  It should be necessary only
3440Sstevel@tonic-gate  * if the disk was not labelled or was somehow labelled incorrectly.
3450Sstevel@tonic-gate  * The list of legal types for the disk comes from information that was
3460Sstevel@tonic-gate  * in the data file.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate int
c_type()3490Sstevel@tonic-gate c_type()
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	struct disk_type	*type, *tptr, *oldtype;
3520Sstevel@tonic-gate 	u_ioparam_t		ioparam;
3530Sstevel@tonic-gate 	int			i, index, deflt, *defltptr = NULL;
3540Sstevel@tonic-gate 	struct disk_type	disk_type;
3550Sstevel@tonic-gate 	struct disk_type	*d = &disk_type;
3560Sstevel@tonic-gate 	int			first_disk;
3570Sstevel@tonic-gate 	int			auto_conf_choice;
3580Sstevel@tonic-gate 	int			other_choice;
3590Sstevel@tonic-gate 	struct dk_label		label;
3600Sstevel@tonic-gate 	struct efi_info		efi_info;
3610Sstevel@tonic-gate 	uint64_t		maxLBA;
3620Sstevel@tonic-gate 	char			volname[LEN_DKL_VVOL];
3630Sstevel@tonic-gate 	int			volinit = 0;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * There must be a current disk.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	if (cur_disk == NULL) {
3690Sstevel@tonic-gate 		err_print("Current Disk is not set.\n");
3700Sstevel@tonic-gate 		return (-1);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 	oldtype = cur_disk->disk_type;
3730Sstevel@tonic-gate 	type = cur_ctype->ctype_dlist;
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * Print out the list of choices.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	fmt_print("\n\nAVAILABLE DRIVE TYPES:\n");
3780Sstevel@tonic-gate 	first_disk = 0;
3790Sstevel@tonic-gate 	if (cur_ctype->ctype_ctype == DKC_SCSI_CCS) {
3800Sstevel@tonic-gate 		auto_conf_choice = 0;
3810Sstevel@tonic-gate 		fmt_print("        %d. Auto configure\n", first_disk++);
3820Sstevel@tonic-gate 	} else {
3830Sstevel@tonic-gate 		auto_conf_choice = -1;
3840Sstevel@tonic-gate 	}
3859889SLarry.Liu@Sun.COM 
3860Sstevel@tonic-gate 	i = first_disk;
3870Sstevel@tonic-gate 	for (tptr = type; tptr != NULL; tptr = tptr->dtype_next) {
3880Sstevel@tonic-gate 		/*
3890Sstevel@tonic-gate 		 * If we pass the current type, mark it to be the default.
3900Sstevel@tonic-gate 		 */
3910Sstevel@tonic-gate 		if (cur_dtype == tptr) {
3920Sstevel@tonic-gate 			deflt = i;
3930Sstevel@tonic-gate 			defltptr = &deflt;
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		if (cur_disk->label_type == L_TYPE_EFI) {
3960Sstevel@tonic-gate 			continue;
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		if (tptr->dtype_asciilabel)
3999889SLarry.Liu@Sun.COM 			fmt_print("        %d. %s\n", i++,
4009889SLarry.Liu@Sun.COM 			    tptr->dtype_asciilabel);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 	other_choice = i;
4030Sstevel@tonic-gate 	fmt_print("        %d. other\n", i);
4040Sstevel@tonic-gate 	ioparam.io_bounds.lower = 0;
4050Sstevel@tonic-gate 	ioparam.io_bounds.upper = i;
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Ask the user which type the disk is.
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	index = input(FIO_INT, "Specify disk type (enter its number)", ':',
4100Sstevel@tonic-gate 	    &ioparam, defltptr, DATA_INPUT);
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * Find the type s/he chose.
4130Sstevel@tonic-gate 	 */
4140Sstevel@tonic-gate 	if (index == auto_conf_choice) {
4150Sstevel@tonic-gate 		float			scaled;
4167563SPrasad.Singamsetty@Sun.COM 		diskaddr_t		nblks;
4170Sstevel@tonic-gate 		int			nparts;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		/*
4200Sstevel@tonic-gate 		 * User chose "auto configure".
4210Sstevel@tonic-gate 		 */
4229889SLarry.Liu@Sun.COM 		(void) strcpy(x86_devname, cur_disk->disk_name);
4239889SLarry.Liu@Sun.COM 		switch (cur_disk->label_type) {
4249889SLarry.Liu@Sun.COM 		case L_TYPE_SOLARIS:
4259889SLarry.Liu@Sun.COM 			if ((tptr = auto_sense(cur_file, 1, &label)) == NULL) {
4269889SLarry.Liu@Sun.COM 				err_print("Auto configure failed\n");
4279889SLarry.Liu@Sun.COM 				return (-1);
4289889SLarry.Liu@Sun.COM 			}
4299889SLarry.Liu@Sun.COM 			fmt_print("%s: configured with capacity of ",
4309889SLarry.Liu@Sun.COM 			    cur_disk->disk_name);
4319889SLarry.Liu@Sun.COM 			nblks = (diskaddr_t)tptr->dtype_ncyl *
4329889SLarry.Liu@Sun.COM 			    tptr->dtype_nhead * tptr->dtype_nsect;
4339889SLarry.Liu@Sun.COM 			scaled = bn2mb(nblks);
4349889SLarry.Liu@Sun.COM 			if (scaled > 1024.0) {
4359889SLarry.Liu@Sun.COM 				fmt_print("%1.2fGB\n", scaled/1024.0);
4369889SLarry.Liu@Sun.COM 			} else {
4379889SLarry.Liu@Sun.COM 				fmt_print("%1.2fMB\n", scaled);
4389889SLarry.Liu@Sun.COM 			}
4399889SLarry.Liu@Sun.COM 			fmt_print("<%s cyl %d alt %d hd %d sec %d>\n",
4409889SLarry.Liu@Sun.COM 			    tptr->dtype_asciilabel, tptr->dtype_ncyl,
4419889SLarry.Liu@Sun.COM 			    tptr->dtype_acyl, tptr->dtype_nhead,
4429889SLarry.Liu@Sun.COM 			    tptr->dtype_nsect);
4439889SLarry.Liu@Sun.COM 			break;
4449889SLarry.Liu@Sun.COM 		case L_TYPE_EFI:
4459889SLarry.Liu@Sun.COM 			if ((tptr = auto_efi_sense(cur_file, &efi_info))
4469889SLarry.Liu@Sun.COM 			    == NULL) {
4479889SLarry.Liu@Sun.COM 				err_print("Auto configure failed\n");
4489889SLarry.Liu@Sun.COM 				return (-1);
4499889SLarry.Liu@Sun.COM 			}
4509889SLarry.Liu@Sun.COM 			fmt_print("%s: configured with capacity of ",
4519889SLarry.Liu@Sun.COM 			    cur_disk->disk_name);
4529889SLarry.Liu@Sun.COM 			scaled = bn2mb(efi_info.capacity);
4539889SLarry.Liu@Sun.COM 			if (scaled > 1024.0) {
4549889SLarry.Liu@Sun.COM 				fmt_print("%1.2fGB\n", scaled/1024.0);
4559889SLarry.Liu@Sun.COM 			} else {
4569889SLarry.Liu@Sun.COM 				fmt_print("%1.2fMB\n", scaled);
4570Sstevel@tonic-gate 			}
4589889SLarry.Liu@Sun.COM 			cur_blksz = efi_info.e_parts->efi_lbasize;
4599889SLarry.Liu@Sun.COM 			print_efi_string(efi_info.vendor, efi_info.product,
4609889SLarry.Liu@Sun.COM 			    efi_info.revision, efi_info.capacity);
4619889SLarry.Liu@Sun.COM 			fmt_print("\n");
4629889SLarry.Liu@Sun.COM 			for (nparts = 0; nparts < cur_parts->etoc->efi_nparts;
4639889SLarry.Liu@Sun.COM 			    nparts++) {
4649889SLarry.Liu@Sun.COM 				if (cur_parts->etoc->efi_parts[nparts].p_tag ==
4659889SLarry.Liu@Sun.COM 				    V_RESERVED) {
4669889SLarry.Liu@Sun.COM 					if (cur_parts->etoc->efi_parts[nparts].
4679889SLarry.Liu@Sun.COM 					    p_name) {
4689889SLarry.Liu@Sun.COM 						(void) strcpy(volname,
4699889SLarry.Liu@Sun.COM 						    cur_parts->etoc->efi_parts
4709889SLarry.Liu@Sun.COM 						    [nparts].p_name);
4719889SLarry.Liu@Sun.COM 						volinit = 1;
4729889SLarry.Liu@Sun.COM 					}
4739889SLarry.Liu@Sun.COM 					break;
4749889SLarry.Liu@Sun.COM 				}
4759889SLarry.Liu@Sun.COM 			}
4769889SLarry.Liu@Sun.COM 			enter_critical();
4779889SLarry.Liu@Sun.COM 			if (delete_disk_type(cur_disk->disk_type) != 0) {
4789889SLarry.Liu@Sun.COM 				fmt_print("Autoconfiguration failed.\n");
4799889SLarry.Liu@Sun.COM 				return (-1);
4809889SLarry.Liu@Sun.COM 			}
4819889SLarry.Liu@Sun.COM 			cur_disk->disk_type = tptr;
4829889SLarry.Liu@Sun.COM 			cur_disk->disk_parts = tptr->dtype_plist;
4839889SLarry.Liu@Sun.COM 			init_globals(cur_disk);
4849889SLarry.Liu@Sun.COM 			exit_critical();
4859889SLarry.Liu@Sun.COM 			if (volinit) {
4869889SLarry.Liu@Sun.COM 				for (nparts = 0; nparts <
4879889SLarry.Liu@Sun.COM 				    cur_parts->etoc->efi_nparts; nparts++) {
4889889SLarry.Liu@Sun.COM 				if (cur_parts->etoc->efi_parts[nparts].p_tag ==
4899889SLarry.Liu@Sun.COM 				    V_RESERVED) {
4909889SLarry.Liu@Sun.COM 				(void) strcpy(
4919889SLarry.Liu@Sun.COM 				    cur_parts->etoc->efi_parts[nparts].p_name,
4929889SLarry.Liu@Sun.COM 				    volname);
4939889SLarry.Liu@Sun.COM 				(void) strlcpy(cur_disk->v_volume, volname,
4949889SLarry.Liu@Sun.COM 				    LEN_DKL_VVOL);
4959889SLarry.Liu@Sun.COM 				break;
4969889SLarry.Liu@Sun.COM 				}
4979889SLarry.Liu@Sun.COM 				}
4989889SLarry.Liu@Sun.COM 			}
4999889SLarry.Liu@Sun.COM 			return (0);
5000Sstevel@tonic-gate 			break;
5019889SLarry.Liu@Sun.COM 		default:
5020Sstevel@tonic-gate 		/* Should never happen */
5030Sstevel@tonic-gate 		return (-1);
5049889SLarry.Liu@Sun.COM 		}
5050Sstevel@tonic-gate 	} else if ((index == other_choice) && (cur_label == L_TYPE_SOLARIS)) {
5060Sstevel@tonic-gate 		/*
5070Sstevel@tonic-gate 		 * User chose "other".
5080Sstevel@tonic-gate 		 * Get the standard information on the new type.
5090Sstevel@tonic-gate 		 * Put all information in a tmp structure, in
5100Sstevel@tonic-gate 		 * case user aborts.
5110Sstevel@tonic-gate 		 */
5120Sstevel@tonic-gate 		bzero((char *)d, sizeof (struct disk_type));
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 		d->dtype_ncyl = get_ncyl();
5150Sstevel@tonic-gate 		d->dtype_acyl = get_acyl(d->dtype_ncyl);
5160Sstevel@tonic-gate 		d->dtype_pcyl = get_pcyl(d->dtype_ncyl, d->dtype_acyl);
5170Sstevel@tonic-gate 		d->dtype_nhead = get_nhead();
5180Sstevel@tonic-gate 		d->dtype_phead = get_phead(d->dtype_nhead, &d->dtype_options);
5190Sstevel@tonic-gate 		d->dtype_nsect = get_nsect();
5200Sstevel@tonic-gate 		d->dtype_psect = get_psect(&d->dtype_options);
5210Sstevel@tonic-gate 		d->dtype_bpt = get_bpt(d->dtype_nsect, &d->dtype_options);
5220Sstevel@tonic-gate 		d->dtype_rpm = get_rpm();
5230Sstevel@tonic-gate 		d->dtype_fmt_time = get_fmt_time(&d->dtype_options);
5240Sstevel@tonic-gate 		d->dtype_cyl_skew = get_cyl_skew(&d->dtype_options);
5250Sstevel@tonic-gate 		d->dtype_trk_skew = get_trk_skew(&d->dtype_options);
5260Sstevel@tonic-gate 		d->dtype_trks_zone = get_trks_zone(&d->dtype_options);
5270Sstevel@tonic-gate 		d->dtype_atrks = get_atrks(&d->dtype_options);
5280Sstevel@tonic-gate 		d->dtype_asect = get_asect(&d->dtype_options);
5290Sstevel@tonic-gate 		d->dtype_cache = get_cache(&d->dtype_options);
5300Sstevel@tonic-gate 		d->dtype_threshold = get_threshold(&d->dtype_options);
5310Sstevel@tonic-gate 		d->dtype_prefetch_min = get_min_prefetch(&d->dtype_options);
5320Sstevel@tonic-gate 		d->dtype_prefetch_max = get_max_prefetch(d->dtype_prefetch_min,
5339889SLarry.Liu@Sun.COM 		    &d->dtype_options);
5340Sstevel@tonic-gate 		d->dtype_bps = get_bps();
5350Sstevel@tonic-gate #if defined(sparc)
5360Sstevel@tonic-gate 		d->dtype_dr_type = 0;
5370Sstevel@tonic-gate #endif /* defined(sparc) */
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		d->dtype_asciilabel = get_asciilabel();
5400Sstevel@tonic-gate 		/*
5410Sstevel@tonic-gate 		 * Add the new type to the list of possible types for
5420Sstevel@tonic-gate 		 * this controller.  We lock out interrupts so the lists
5430Sstevel@tonic-gate 		 * can't get munged.  We put off actually allocating the
5440Sstevel@tonic-gate 		 * structure till here in case the user wanted to
5450Sstevel@tonic-gate 		 * interrupt while still inputting information.
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 		enter_critical();
5480Sstevel@tonic-gate 		tptr = (struct disk_type *)zalloc(sizeof (struct disk_type));
5490Sstevel@tonic-gate 		if (type == NULL)
5500Sstevel@tonic-gate 			cur_ctype->ctype_dlist = tptr;
5510Sstevel@tonic-gate 		else {
5520Sstevel@tonic-gate 			while (type->dtype_next != NULL)
5530Sstevel@tonic-gate 				type = type->dtype_next;
5540Sstevel@tonic-gate 			type->dtype_next = tptr;
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 		bcopy((char *)d, (char *)tptr, sizeof (disk_type));
5570Sstevel@tonic-gate 		tptr->dtype_next = NULL;
5580Sstevel@tonic-gate 		/*
5590Sstevel@tonic-gate 		 * the new disk type does not have any defined
5600Sstevel@tonic-gate 		 * partition table . Hence copy the current partition
5610Sstevel@tonic-gate 		 * table if possible else create a default
5620Sstevel@tonic-gate 		 * paritition table.
5630Sstevel@tonic-gate 		 */
5640Sstevel@tonic-gate 		new_partitiontable(tptr, oldtype);
5650Sstevel@tonic-gate 	} else if ((index == other_choice) && (cur_label == L_TYPE_EFI)) {
5660Sstevel@tonic-gate 		maxLBA = get_mlba();
5670Sstevel@tonic-gate 		cur_parts->etoc->efi_last_lba = maxLBA;
5680Sstevel@tonic-gate 		cur_parts->etoc->efi_last_u_lba = maxLBA - 34;
5690Sstevel@tonic-gate 		for (i = 0; i < cur_parts->etoc->efi_nparts; i++) {
5709889SLarry.Liu@Sun.COM 			cur_parts->etoc->efi_parts[i].p_start = 0;
5719889SLarry.Liu@Sun.COM 			cur_parts->etoc->efi_parts[i].p_size = 0;
5729889SLarry.Liu@Sun.COM 			cur_parts->etoc->efi_parts[i].p_tag = V_UNASSIGNED;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 		cur_parts->etoc->efi_parts[8].p_start =
5759889SLarry.Liu@Sun.COM 		    maxLBA - 34 - (1024 * 16);
5760Sstevel@tonic-gate 		cur_parts->etoc->efi_parts[8].p_size = (1024 * 16);
5770Sstevel@tonic-gate 		cur_parts->etoc->efi_parts[8].p_tag = V_RESERVED;
5780Sstevel@tonic-gate 		if (write_label()) {
5799889SLarry.Liu@Sun.COM 			err_print("Write label failed\n");
5800Sstevel@tonic-gate 		} else {
5819889SLarry.Liu@Sun.COM 			cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 		return (0);
5840Sstevel@tonic-gate 	} else {
5850Sstevel@tonic-gate 		/*
5860Sstevel@tonic-gate 		 * User picked an existing disk type.
5870Sstevel@tonic-gate 		 */
5880Sstevel@tonic-gate 		i = first_disk;
5890Sstevel@tonic-gate 		tptr = type;
5900Sstevel@tonic-gate 		while (i < index) {
5919889SLarry.Liu@Sun.COM 			if (tptr->dtype_asciilabel) {
5929889SLarry.Liu@Sun.COM 				i++;
5939889SLarry.Liu@Sun.COM 			}
5949889SLarry.Liu@Sun.COM 			tptr = tptr->dtype_next;
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 		if ((tptr->dtype_asciilabel == NULL) &&
5970Sstevel@tonic-gate 		    (tptr->dtype_next != NULL)) {
5980Sstevel@tonic-gate 			while (tptr->dtype_asciilabel == NULL) {
5990Sstevel@tonic-gate 				tptr = tptr->dtype_next;
6000Sstevel@tonic-gate 			}
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * Check for mounted file systems in the format zone.
6050Sstevel@tonic-gate 	 * One potential problem with this would be that check()
6060Sstevel@tonic-gate 	 * always returns 'yes' when running out of a file.  However,
6070Sstevel@tonic-gate 	 * it is actually ok because we don't let the program get
6080Sstevel@tonic-gate 	 * started if there are mounted file systems and we are
6090Sstevel@tonic-gate 	 * running from a file.
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	if ((tptr != oldtype) &&
6129889SLarry.Liu@Sun.COM 	    checkmount((diskaddr_t)-1, (diskaddr_t)-1)) {
6130Sstevel@tonic-gate 		err_print(
6149889SLarry.Liu@Sun.COM 		    "Cannot set disk type while it has mounted "
6159889SLarry.Liu@Sun.COM 		    "partitions.\n\n");
6160Sstevel@tonic-gate 		return (-1);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * check for partitions being used for swapping in format zone
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	if ((tptr != oldtype) &&
6229889SLarry.Liu@Sun.COM 	    checkswap((diskaddr_t)-1, (diskaddr_t)-1)) {
6239889SLarry.Liu@Sun.COM 		err_print("Cannot set disk type while its partition are "
6249889SLarry.Liu@Sun.COM 		"currently being used for swapping.\n");
6250Sstevel@tonic-gate 		return (-1);
6260Sstevel@tonic-gate 	}
627767Ssjelinek 
628767Ssjelinek 	/*
629767Ssjelinek 	 * Check for partitions being used in SVM, VxVM or LU devices
630767Ssjelinek 	 */
631767Ssjelinek 
632767Ssjelinek 	if ((tptr != oldtype) &&
6339889SLarry.Liu@Sun.COM 	    checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
6349889SLarry.Liu@Sun.COM 	    (diskaddr_t)-1, 0, 0)) {
635767Ssjelinek 		err_print("Cannot set disk type while its "
636767Ssjelinek 		    "partitions are currently in use.\n");
6379889SLarry.Liu@Sun.COM 		return (-1);
638767Ssjelinek 	}
6390Sstevel@tonic-gate 	/*
6400Sstevel@tonic-gate 	 * If the type selected is different from the previous type,
6410Sstevel@tonic-gate 	 * mark the disk as not labelled and reload the current
6420Sstevel@tonic-gate 	 * partition info.  This is not essential but probably the
6430Sstevel@tonic-gate 	 * right thing to do, since the size of the disk has probably
6440Sstevel@tonic-gate 	 * changed.
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	enter_critical();
6470Sstevel@tonic-gate 	if (tptr != oldtype) {
6480Sstevel@tonic-gate 		cur_disk->disk_type = tptr;
6490Sstevel@tonic-gate 		cur_disk->disk_parts = NULL;
6500Sstevel@tonic-gate 		cur_disk->disk_flags &= ~DSK_LABEL;
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * Initialize the state of the current disk.
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 	init_globals(cur_disk);
6560Sstevel@tonic-gate 	(void) get_partition();
6570Sstevel@tonic-gate 	exit_critical();
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * If the label of the disk is marked dirty,
6610Sstevel@tonic-gate 	 * see if they'd like to label the disk now.
6620Sstevel@tonic-gate 	 */
6630Sstevel@tonic-gate 	if (cur_disk->disk_flags & DSK_LABEL_DIRTY) {
6640Sstevel@tonic-gate 		if (check("Disk not labeled.  Label it now") == 0) {
6650Sstevel@tonic-gate 			if (write_label()) {
6660Sstevel@tonic-gate 				err_print("Write label failed\n");
6670Sstevel@tonic-gate 			} else {
6680Sstevel@tonic-gate 				cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
6690Sstevel@tonic-gate 			}
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	return (0);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * This routine implements the 'partition' command.  It simply runs
6780Sstevel@tonic-gate  * the partition menu.
6790Sstevel@tonic-gate  */
6800Sstevel@tonic-gate int
c_partition()6810Sstevel@tonic-gate c_partition()
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/*
6850Sstevel@tonic-gate 	 * There must be a current disk type and a current disk
6860Sstevel@tonic-gate 	 */
6870Sstevel@tonic-gate 	if (cur_dtype == NULL) {
6880Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
6890Sstevel@tonic-gate 		return (-1);
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * Check for a valid fdisk table entry for Solaris
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if (!good_fdisk()) {
6950Sstevel@tonic-gate 		return (-1);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	cur_menu++;
6990Sstevel@tonic-gate 	last_menu = cur_menu;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate #ifdef	not
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * If there is no current partition table, make one.  This is
7040Sstevel@tonic-gate 	 * so the commands within the menu never have to check for
7050Sstevel@tonic-gate 	 * a non-existent table.
7060Sstevel@tonic-gate 	 */
7070Sstevel@tonic-gate 	if (cur_parts == NULL)
7080Sstevel@tonic-gate 		err_print("making partition.\n");
7090Sstevel@tonic-gate 		make_partition();
7100Sstevel@tonic-gate #endif	/* not */
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * Run the menu.
7140Sstevel@tonic-gate 	 */
7150Sstevel@tonic-gate 	run_menu(menu_partition, "PARTITION", "partition", 0);
7160Sstevel@tonic-gate 	cur_menu--;
7170Sstevel@tonic-gate 	return (0);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate  * This routine implements the 'current' command.  It describes the
7220Sstevel@tonic-gate  * current disk.
7230Sstevel@tonic-gate  */
7240Sstevel@tonic-gate int
c_current()7250Sstevel@tonic-gate c_current()
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * If there is no current disk, say so.  Note that this is
7300Sstevel@tonic-gate 	 * not an error since it is a legitimate response to the inquiry.
7310Sstevel@tonic-gate 	 */
7320Sstevel@tonic-gate 	if (cur_disk == NULL) {
7330Sstevel@tonic-gate 		fmt_print("No Current Disk.\n");
7340Sstevel@tonic-gate 		return (0);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Print out the info we have on the current disk.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	fmt_print("Current Disk = %s", cur_disk->disk_name);
7400Sstevel@tonic-gate 	if (chk_volname(cur_disk)) {
7410Sstevel@tonic-gate 		fmt_print(": ");
7420Sstevel@tonic-gate 		print_volname(cur_disk);
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 	fmt_print("\n");
7450Sstevel@tonic-gate 	if (cur_disk->devfs_name != NULL) {
7460Sstevel@tonic-gate 		if (cur_dtype == NULL) {
7470Sstevel@tonic-gate 			fmt_print("<type unknown>\n");
7480Sstevel@tonic-gate 		} else if (cur_label == L_TYPE_SOLARIS) {
7490Sstevel@tonic-gate 			fmt_print("<%s cyl %d alt %d hd %d sec %d>\n",
7509889SLarry.Liu@Sun.COM 			    cur_dtype->dtype_asciilabel, ncyl,
7519889SLarry.Liu@Sun.COM 			    acyl, nhead, nsect);
7520Sstevel@tonic-gate 		} else if (cur_label == L_TYPE_EFI) {
7530Sstevel@tonic-gate 			print_efi_string(cur_dtype->vendor,
7540Sstevel@tonic-gate 			    cur_dtype->product, cur_dtype->revision,
7550Sstevel@tonic-gate 			    cur_dtype->capacity);
7560Sstevel@tonic-gate 			fmt_print("\n");
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 		fmt_print("%s\n", cur_disk->devfs_name);
7590Sstevel@tonic-gate 	} else {
7600Sstevel@tonic-gate 		fmt_print("%s%d: <", cur_ctlr->ctlr_dname,
7619889SLarry.Liu@Sun.COM 		    cur_disk->disk_dkinfo.dki_unit);
7620Sstevel@tonic-gate 		if (cur_dtype == NULL) {
7630Sstevel@tonic-gate 			fmt_print("type unknown");
7640Sstevel@tonic-gate 		} else if (cur_label == L_TYPE_SOLARIS) {
7650Sstevel@tonic-gate 			fmt_print("%s cyl %d alt %d hd %d sec %d",
7669889SLarry.Liu@Sun.COM 			    cur_dtype->dtype_asciilabel, ncyl,
7679889SLarry.Liu@Sun.COM 			    acyl, nhead, nsect);
7680Sstevel@tonic-gate 		} else if (cur_label == L_TYPE_EFI) {
7690Sstevel@tonic-gate 			print_efi_string(cur_dtype->vendor,
7700Sstevel@tonic-gate 			    cur_dtype->product, cur_dtype->revision,
7710Sstevel@tonic-gate 			    cur_dtype->capacity);
7720Sstevel@tonic-gate 			fmt_print("\n");
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 		fmt_print(">\n");
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	fmt_print("\n");
7770Sstevel@tonic-gate 	return (0);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * This routine implements the 'format' command.  It allows the user
7810Sstevel@tonic-gate  * to format and verify any portion of the disk.
7820Sstevel@tonic-gate  */
7830Sstevel@tonic-gate int
c_format()7840Sstevel@tonic-gate c_format()
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	diskaddr_t		start, end;
787767Ssjelinek 	time_t			clock;
788767Ssjelinek 	int			format_time, format_tracks, format_cyls;
789767Ssjelinek 	int			format_interval;
7907563SPrasad.Singamsetty@Sun.COM 	diskaddr_t		deflt;
7917563SPrasad.Singamsetty@Sun.COM 	int			status;
792767Ssjelinek 	u_ioparam_t		ioparam;
793*12594SShengliang.Zhang@Sun.COM 	struct scsi_inquiry	*inq;
794*12594SShengliang.Zhang@Sun.COM 	char	rawbuf[MAX_MODE_SENSE_SIZE];
795*12594SShengliang.Zhang@Sun.COM 	struct scsi_capacity_16	capacity;
796*12594SShengliang.Zhang@Sun.COM 	struct vpd_hdr	*vpdhdr;
797*12594SShengliang.Zhang@Sun.COM 	uint8_t	protect;
798*12594SShengliang.Zhang@Sun.COM 	uint8_t	pagecode;
799*12594SShengliang.Zhang@Sun.COM 	uint8_t	spt;
800*12594SShengliang.Zhang@Sun.COM 	uint8_t	p_type;
801*12594SShengliang.Zhang@Sun.COM 	uint8_t	prot_flag[NUM_PROT_TYPE] = {1, 0, 0, 0};
802*12594SShengliang.Zhang@Sun.COM 	int	i;
803*12594SShengliang.Zhang@Sun.COM 	char	*prot_descriptor[NUM_PROT_TYPE] = {
804*12594SShengliang.Zhang@Sun.COM 	    "Protection Information is disabled.",
805*12594SShengliang.Zhang@Sun.COM 	    "Protection Information is enabled.",
806*12594SShengliang.Zhang@Sun.COM 	    "Protection Information is enabled.",
807*12594SShengliang.Zhang@Sun.COM 	    "Protection Information is enabled.", };
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * There must be a current disk type and a current disk
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 	if (cur_dtype == NULL) {
8130Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
8140Sstevel@tonic-gate 		return (-1);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * There must be a format routine in cur_ops structure to have
8190Sstevel@tonic-gate 	 *  this routine work.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (cur_ops->op_format == NULL) {
8220Sstevel@tonic-gate 		err_print(
8230Sstevel@tonic-gate "Cannot format this drive. Please use your Manufacturer supplied formatting "
8240Sstevel@tonic-gate "utility.\n");
8250Sstevel@tonic-gate 		return (-1);
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/*
8290Sstevel@tonic-gate 	 * There must be a current defect list.  Except for
8300Sstevel@tonic-gate 	 * unformatted SCSI disks.  For them the defect list
8310Sstevel@tonic-gate 	 * can only be retrieved after formatting the disk.
8320Sstevel@tonic-gate 	 */
8330Sstevel@tonic-gate 	if ((cur_ctype->ctype_flags & CF_SCSI) && !EMBEDDED_SCSI &&
8349889SLarry.Liu@Sun.COM 	    (cur_ctype->ctype_flags & CF_DEFECTS) &&
8359889SLarry.Liu@Sun.COM 	    ! (cur_flags & DISK_FORMATTED)) {
8360Sstevel@tonic-gate 		cur_list.flags |= LIST_RELOAD;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	} else if (cur_list.list == NULL && !EMBEDDED_SCSI) {
8390Sstevel@tonic-gate 		err_print("Current Defect List must be initialized.\n");
8400Sstevel@tonic-gate 		return (-1);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 	/*
8430Sstevel@tonic-gate 	 * Ask for the bounds of the format.  We always use the whole
8440Sstevel@tonic-gate 	 * disk as the default, since that is the most likely case.
8450Sstevel@tonic-gate 	 * Note, for disks which must be formatted accross the whole disk,
8460Sstevel@tonic-gate 	 * don't bother the user.
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	ioparam.io_bounds.lower = start = 0;
8490Sstevel@tonic-gate 	if (cur_label == L_TYPE_SOLARIS) {
8509889SLarry.Liu@Sun.COM 		if (cur_ctype->ctype_flags & CF_SCSI) {
8519889SLarry.Liu@Sun.COM 			ioparam.io_bounds.upper = end = datasects() - 1;
8529889SLarry.Liu@Sun.COM 		} else {
8539889SLarry.Liu@Sun.COM 			ioparam.io_bounds.upper = end = physsects() - 1;
8549889SLarry.Liu@Sun.COM 		}
8550Sstevel@tonic-gate 	} else {
8569889SLarry.Liu@Sun.COM 		ioparam.io_bounds.upper = end = cur_parts->etoc->efi_last_lba;
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (! (cur_ctlr->ctlr_flags & DKI_FMTVOL)) {
8600Sstevel@tonic-gate 		deflt = ioparam.io_bounds.lower;
8610Sstevel@tonic-gate 		start = input(FIO_BN,
8629889SLarry.Liu@Sun.COM 		    "Enter starting block number", ':',
8639889SLarry.Liu@Sun.COM 		    &ioparam, (int *)&deflt, DATA_INPUT);
8640Sstevel@tonic-gate 		ioparam.io_bounds.lower = start;
8650Sstevel@tonic-gate 		deflt = ioparam.io_bounds.upper;
8660Sstevel@tonic-gate 		end = input(FIO_BN,
8679889SLarry.Liu@Sun.COM 		    "Enter ending block number", ':',
8689889SLarry.Liu@Sun.COM 		    &ioparam, (int *)&deflt, DATA_INPUT);
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * Some disks can format tracks.  Make sure the whole track is
8720Sstevel@tonic-gate 	 * specified for them.
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	if (cur_ctlr->ctlr_flags & DKI_FMTTRK) {
8750Sstevel@tonic-gate 		if (bn2s(start) != 0 ||
8769889SLarry.Liu@Sun.COM 		    bn2s(end) != sectors(bn2h(end)) - 1) {
8770Sstevel@tonic-gate 			err_print("Controller requires formatting of ");
8780Sstevel@tonic-gate 			err_print("entire tracks.\n");
8790Sstevel@tonic-gate 			return (-1);
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 	/*
8830Sstevel@tonic-gate 	 * Check for mounted file systems in the format zone, and if we
8840Sstevel@tonic-gate 	 * find any, make sure they are really serious.  One potential
8850Sstevel@tonic-gate 	 * problem with this would be that check() always returns 'yes'
8860Sstevel@tonic-gate 	 * when running out of a file.  However, it is actually ok
8870Sstevel@tonic-gate 	 * because we don't let the program get started if there are
8880Sstevel@tonic-gate 	 * mounted file systems and we are running from a file.
8890Sstevel@tonic-gate 	 */
8900Sstevel@tonic-gate 	if (checkmount(start, end)) {
8910Sstevel@tonic-gate 		err_print(
8920Sstevel@tonic-gate 		"Cannot format disk while it has mounted partitions.\n\n");
8930Sstevel@tonic-gate 		return (-1);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * check for partitions being used for swapping in format zone
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	if (checkswap(start, end)) {
8990Sstevel@tonic-gate 		err_print("Cannot format disk while its partition are \
9000Sstevel@tonic-gate currently being used for swapping.\n");
9010Sstevel@tonic-gate 		return (-1);
9020Sstevel@tonic-gate 	}
903767Ssjelinek 	/*
904767Ssjelinek 	 * Check for partitions being used in SVM, VxVM or LU devices
905767Ssjelinek 	 * in this format zone
906767Ssjelinek 	 */
907767Ssjelinek 	if (checkdevinuse(cur_disk->disk_name, start, end, 0, 0)) {
908767Ssjelinek 		err_print("Cannot format disk while its partitions "
9099889SLarry.Liu@Sun.COM 		    "are currently in use.\n");
9109889SLarry.Liu@Sun.COM 		return (-1);
9119889SLarry.Liu@Sun.COM 	}
9129889SLarry.Liu@Sun.COM 
9139889SLarry.Liu@Sun.COM 	if (cur_disk->disk_lbasize != DEV_BSIZE) {
9149889SLarry.Liu@Sun.COM 		fmt_print("Current disk sector size is %d Byte, format\n"
9159889SLarry.Liu@Sun.COM 		    "will change the sector size to 512 Byte. ",
9169889SLarry.Liu@Sun.COM 		    cur_disk->disk_lbasize);
9179889SLarry.Liu@Sun.COM 		if (check("Continue")) {
918767Ssjelinek 			return (-1);
9199889SLarry.Liu@Sun.COM 		}
920767Ssjelinek 	}
921767Ssjelinek 
922*12594SShengliang.Zhang@Sun.COM 	/*
923*12594SShengliang.Zhang@Sun.COM 	 * set the default protection type
924*12594SShengliang.Zhang@Sun.COM 	 */
925*12594SShengliang.Zhang@Sun.COM 	prot_type = PROT_TYPE_0;
926*12594SShengliang.Zhang@Sun.COM 
927*12594SShengliang.Zhang@Sun.COM 	/*
928*12594SShengliang.Zhang@Sun.COM 	 * Check if the protect information of this disk is enabled
929*12594SShengliang.Zhang@Sun.COM 	 */
930*12594SShengliang.Zhang@Sun.COM 	if (uscsi_inquiry(cur_file, rawbuf, sizeof (rawbuf))) {
931*12594SShengliang.Zhang@Sun.COM 		err_print("Inquiry failed\n");
932*12594SShengliang.Zhang@Sun.COM 		return (-1);
933*12594SShengliang.Zhang@Sun.COM 	}
934*12594SShengliang.Zhang@Sun.COM 	inq = (struct scsi_inquiry *)rawbuf;
935*12594SShengliang.Zhang@Sun.COM 	protect = inq->inq_protect;
936*12594SShengliang.Zhang@Sun.COM 	if (protect == 0) {
937*12594SShengliang.Zhang@Sun.COM 		fmt_print("The protection information is not enabled\n");
938*12594SShengliang.Zhang@Sun.COM 		fmt_print(
939*12594SShengliang.Zhang@Sun.COM 		    "The disk will be formatted with protection type 0\n");
940*12594SShengliang.Zhang@Sun.COM 	} else {
941*12594SShengliang.Zhang@Sun.COM 		(void) memset(rawbuf, 0, MAX_MODE_SENSE_SIZE);
942*12594SShengliang.Zhang@Sun.COM 		if (uscsi_inquiry_page_86h(cur_file, rawbuf, sizeof (rawbuf))) {
943*12594SShengliang.Zhang@Sun.COM 			err_print("Inquiry with page 86h failed\n");
944*12594SShengliang.Zhang@Sun.COM 			return (-1);
945*12594SShengliang.Zhang@Sun.COM 		}
946*12594SShengliang.Zhang@Sun.COM 		vpdhdr = (struct vpd_hdr *)rawbuf;
947*12594SShengliang.Zhang@Sun.COM 		pagecode = vpdhdr->page_code;
948*12594SShengliang.Zhang@Sun.COM 		if (pagecode != 0x86) {
949*12594SShengliang.Zhang@Sun.COM 			err_print("Inquiry with page 86h failed\n");
950*12594SShengliang.Zhang@Sun.COM 			return (-1);
951*12594SShengliang.Zhang@Sun.COM 		}
952*12594SShengliang.Zhang@Sun.COM 		spt = (rawbuf[4] << 2) >> 5;
953*12594SShengliang.Zhang@Sun.COM 		fmt_print("This disk can support protection types:\n");
954*12594SShengliang.Zhang@Sun.COM 
955*12594SShengliang.Zhang@Sun.COM 		switch (spt) {
956*12594SShengliang.Zhang@Sun.COM 		case 0:
957*12594SShengliang.Zhang@Sun.COM 			prot_flag[1] = 1;
958*12594SShengliang.Zhang@Sun.COM 			break;
959*12594SShengliang.Zhang@Sun.COM 		case 1:
960*12594SShengliang.Zhang@Sun.COM 			prot_flag[1] = 1;
961*12594SShengliang.Zhang@Sun.COM 			prot_flag[2] = 1;
962*12594SShengliang.Zhang@Sun.COM 			break;
963*12594SShengliang.Zhang@Sun.COM 		case 2:
964*12594SShengliang.Zhang@Sun.COM 			prot_flag[2] = 1;
965*12594SShengliang.Zhang@Sun.COM 			break;
966*12594SShengliang.Zhang@Sun.COM 		case 3:
967*12594SShengliang.Zhang@Sun.COM 			prot_flag[1] = 1;
968*12594SShengliang.Zhang@Sun.COM 			prot_flag[3] = 1;
969*12594SShengliang.Zhang@Sun.COM 			break;
970*12594SShengliang.Zhang@Sun.COM 		case 4:
971*12594SShengliang.Zhang@Sun.COM 			prot_flag[3] = 1;
972*12594SShengliang.Zhang@Sun.COM 			break;
973*12594SShengliang.Zhang@Sun.COM 		case 5:
974*12594SShengliang.Zhang@Sun.COM 			prot_flag[2] = 1;
975*12594SShengliang.Zhang@Sun.COM 			prot_flag[3] = 1;
976*12594SShengliang.Zhang@Sun.COM 			break;
977*12594SShengliang.Zhang@Sun.COM 		case 7:
978*12594SShengliang.Zhang@Sun.COM 			prot_flag[1] = 1;
979*12594SShengliang.Zhang@Sun.COM 			prot_flag[2] = 1;
980*12594SShengliang.Zhang@Sun.COM 			prot_flag[3] = 1;
981*12594SShengliang.Zhang@Sun.COM 			break;
982*12594SShengliang.Zhang@Sun.COM 		default:
983*12594SShengliang.Zhang@Sun.COM 			err_print(
984*12594SShengliang.Zhang@Sun.COM 			    "Invalid supported protection types\n");
985*12594SShengliang.Zhang@Sun.COM 			return (-1);
986*12594SShengliang.Zhang@Sun.COM 		}
987*12594SShengliang.Zhang@Sun.COM 		for (i = 0; i < NUM_PROT_TYPE; i++) {
988*12594SShengliang.Zhang@Sun.COM 			if (prot_flag[i] == 1) {
989*12594SShengliang.Zhang@Sun.COM 				fmt_print("[%d] TYPE_%d : ", i, i);
990*12594SShengliang.Zhang@Sun.COM 				fmt_print("%s\n", prot_descriptor[i]);
991*12594SShengliang.Zhang@Sun.COM 			}
992*12594SShengliang.Zhang@Sun.COM 		}
993*12594SShengliang.Zhang@Sun.COM 
994*12594SShengliang.Zhang@Sun.COM 		/*
995*12594SShengliang.Zhang@Sun.COM 		 * Get the current protection type
996*12594SShengliang.Zhang@Sun.COM 		 */
997*12594SShengliang.Zhang@Sun.COM 		if (uscsi_read_capacity_16(cur_file, &capacity)) {
998*12594SShengliang.Zhang@Sun.COM 			err_print("Read capacity_16 failed\n");
999*12594SShengliang.Zhang@Sun.COM 			return (-1);
1000*12594SShengliang.Zhang@Sun.COM 		}
1001*12594SShengliang.Zhang@Sun.COM 		p_type = get_cur_protection_type(&capacity);
1002*12594SShengliang.Zhang@Sun.COM 		fmt_print("\nThe disk is currently formatted with TYPE_%d.\n",
1003*12594SShengliang.Zhang@Sun.COM 		    p_type);
1004*12594SShengliang.Zhang@Sun.COM 
1005*12594SShengliang.Zhang@Sun.COM 		/*
1006*12594SShengliang.Zhang@Sun.COM 		 * Ask user what protection type to use
1007*12594SShengliang.Zhang@Sun.COM 		 */
1008*12594SShengliang.Zhang@Sun.COM 		ioparam.io_bounds.lower = PROT_TYPE_0;
1009*12594SShengliang.Zhang@Sun.COM 		ioparam.io_bounds.upper = PROT_TYPE_3;
1010*12594SShengliang.Zhang@Sun.COM 		prot_type = input(FIO_INT, "Specify the New Protection Type",
1011*12594SShengliang.Zhang@Sun.COM 		    ':', &ioparam, NULL, DATA_INPUT);
1012*12594SShengliang.Zhang@Sun.COM 		/*
1013*12594SShengliang.Zhang@Sun.COM 		 * if get a unsupported protection type, then use the
1014*12594SShengliang.Zhang@Sun.COM 		 * current type: p_type.
1015*12594SShengliang.Zhang@Sun.COM 		 */
1016*12594SShengliang.Zhang@Sun.COM 		if (prot_flag[prot_type] == 0) {
1017*12594SShengliang.Zhang@Sun.COM 			fmt_print("Unsupported protection type.\n");
1018*12594SShengliang.Zhang@Sun.COM 			prot_type = p_type;
1019*12594SShengliang.Zhang@Sun.COM 		}
1020*12594SShengliang.Zhang@Sun.COM 		fmt_print("The disk will be formatted to type %d\n", prot_type);
1021*12594SShengliang.Zhang@Sun.COM 	}
1022*12594SShengliang.Zhang@Sun.COM 
10230Sstevel@tonic-gate 	if (SCSI && (format_time = scsi_format_time()) > 0) {
10240Sstevel@tonic-gate 		fmt_print(
10259889SLarry.Liu@Sun.COM 		    "\nReady to format.  Formatting cannot be interrupted\n"
10260Sstevel@tonic-gate 		    "and takes %d minutes (estimated). ", format_time);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	} else if (cur_dtype->dtype_options & SUP_FMTTIME) {
10290Sstevel@tonic-gate 		/*
10300Sstevel@tonic-gate 		 * Formatting time is (2 * time of 1 spin * number of
10310Sstevel@tonic-gate 		 * tracks) + (step rate * number of cylinders) rounded
10320Sstevel@tonic-gate 		 * up to the nearest minute.  Note, a 10% fudge factor
10330Sstevel@tonic-gate 		 * is thrown in for insurance.
10340Sstevel@tonic-gate 		 */
10350Sstevel@tonic-gate 		if (cur_dtype->dtype_fmt_time == 0)
10360Sstevel@tonic-gate 			cur_dtype->dtype_fmt_time = 2;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 		format_tracks = ((end-start) / cur_dtype->dtype_nsect) + 1;
10390Sstevel@tonic-gate 		format_cyls = format_tracks / cur_dtype->dtype_nhead;
10400Sstevel@tonic-gate 		format_tracks = format_tracks * cur_dtype->dtype_fmt_time;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 		/*
10430Sstevel@tonic-gate 		 * ms.
10440Sstevel@tonic-gate 		 */
10450Sstevel@tonic-gate 		format_time = ((60000 / cur_dtype->dtype_rpm) +1) *
10469889SLarry.Liu@Sun.COM 		    format_tracks + format_cyls * 7;
10470Sstevel@tonic-gate 		/*
10480Sstevel@tonic-gate 		 * 20% done tick (sec)
10490Sstevel@tonic-gate 		 */
10500Sstevel@tonic-gate 		format_interval = format_time / 5000;
10510Sstevel@tonic-gate 		/*
10520Sstevel@tonic-gate 		 * min.
10530Sstevel@tonic-gate 		 */
10540Sstevel@tonic-gate 		format_time = (format_time + 59999) / 60000;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 		/*
10570Sstevel@tonic-gate 		 * Check format time values and make adjustments
10580Sstevel@tonic-gate 		 * to prevent sleeping too long (forever?) or
10590Sstevel@tonic-gate 		 * too short.
10600Sstevel@tonic-gate 		 */
10610Sstevel@tonic-gate 		if (format_time <= 1) {
10620Sstevel@tonic-gate 			/*
10630Sstevel@tonic-gate 			 * Format time is less than 1 min..
10640Sstevel@tonic-gate 			 */
10650Sstevel@tonic-gate 			format_time = 1;
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		if (format_interval < 11) {
10690Sstevel@tonic-gate 			/* Format time is less than 1 minute. */
10700Sstevel@tonic-gate 			if (format_interval < 2)
10710Sstevel@tonic-gate 				format_interval = 2;	/* failsafe */
10720Sstevel@tonic-gate 			format_interval = 10;
10730Sstevel@tonic-gate 		} else {
10740Sstevel@tonic-gate 			/* Format time is greater than 1 minute. */
10750Sstevel@tonic-gate 			format_interval -= 10;
10760Sstevel@tonic-gate 		}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		fmt_print(
10790Sstevel@tonic-gate 		    "Ready to format.  Formatting cannot be interrupted\n"
10800Sstevel@tonic-gate 		    "and takes %d minutes (estimated). ", format_time);
10810Sstevel@tonic-gate 	} else {
10820Sstevel@tonic-gate 		fmt_print(
10830Sstevel@tonic-gate 		    "Ready to format.  Formatting cannot be interrupted.\n");
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 	if (check("Continue")) {
10860Sstevel@tonic-gate 		return (-1);
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	/*
10900Sstevel@tonic-gate 	 * Print the time so that the user will know when format started.
10910Sstevel@tonic-gate 	 * Lock out interrupts.  This could be a problem, since it could
10920Sstevel@tonic-gate 	 * cause the user to sit for quite awhile with no control, but we
10930Sstevel@tonic-gate 	 * don't have any other good way of keeping his gun from going off.
10940Sstevel@tonic-gate 	 */
10950Sstevel@tonic-gate 	clock = time((time_t *)0);
10960Sstevel@tonic-gate 	fmt_print("Beginning format. The current time is %s\n",
10979889SLarry.Liu@Sun.COM 	    ctime(&clock));
10980Sstevel@tonic-gate 	enter_critical();
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * Mark the defect list dirty so it will be rewritten when we are
11010Sstevel@tonic-gate 	 * done.  It is possible to qualify this so it doesn't always
11020Sstevel@tonic-gate 	 * get rewritten, but it's not worth the trouble.
11030Sstevel@tonic-gate 	 * Note: no defect lists for embedded scsi drives.
11040Sstevel@tonic-gate 	 */
11050Sstevel@tonic-gate 	if (!EMBEDDED_SCSI) {
11060Sstevel@tonic-gate 		cur_list.flags |= LIST_DIRTY;
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * If we are formatting over any of the labels, mark the label
11100Sstevel@tonic-gate 	 * dirty so it will be rewritten.
11110Sstevel@tonic-gate 	 */
11120Sstevel@tonic-gate 	if (cur_disk->label_type == L_TYPE_SOLARIS) {
11139889SLarry.Liu@Sun.COM 		if (start < totalsects() && end >= datasects()) {
11149889SLarry.Liu@Sun.COM 			if (cur_disk->disk_flags & DSK_LABEL)
11159889SLarry.Liu@Sun.COM 				cur_flags |= LABEL_DIRTY;
11169889SLarry.Liu@Sun.COM 		}
11170Sstevel@tonic-gate 	} else if (cur_disk->label_type == L_TYPE_EFI) {
11189889SLarry.Liu@Sun.COM 		if (start < 34) {
11199889SLarry.Liu@Sun.COM 			if (cur_disk->disk_flags & DSK_LABEL)
11209889SLarry.Liu@Sun.COM 				cur_flags |= LABEL_DIRTY;
11219889SLarry.Liu@Sun.COM 		}
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 	if (start == 0) {
11240Sstevel@tonic-gate 		cur_flags |= LABEL_DIRTY;
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	/*
11270Sstevel@tonic-gate 	 * Do the format. bugid 1009138 removed the use of fork to
11280Sstevel@tonic-gate 	 * background the format and print a tick.
11290Sstevel@tonic-gate 	 */
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	status = (*cur_ops->op_format)(start, end, &cur_list);
11320Sstevel@tonic-gate 	if (status) {
11330Sstevel@tonic-gate 		exit_critical();
11340Sstevel@tonic-gate 		err_print("failed\n");
11350Sstevel@tonic-gate 		return (-1);
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 	fmt_print("done\n");
11380Sstevel@tonic-gate 	if (option_msg && diag_msg) {
11390Sstevel@tonic-gate 		clock = time((time_t *)0);
11400Sstevel@tonic-gate 		fmt_print("The current time is %s\n", ctime(&clock));
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 	cur_flags |= DISK_FORMATTED;
11430Sstevel@tonic-gate 	/*
11440Sstevel@tonic-gate 	 * If the defect list or label is dirty, write them out again.
11450Sstevel@tonic-gate 	 * Note, for SCSI we have to wait til now to load defect list
11460Sstevel@tonic-gate 	 * since we can't access it until after formatting a virgin disk.
11470Sstevel@tonic-gate 	 */
11480Sstevel@tonic-gate 	/* enter_critical(); */
11490Sstevel@tonic-gate 	if (cur_list.flags & LIST_RELOAD) {
11500Sstevel@tonic-gate 		assert(!EMBEDDED_SCSI);
11510Sstevel@tonic-gate 		if (*cur_ops->op_ex_man == NULL ||
11520Sstevel@tonic-gate 		    (*cur_ops->op_ex_man)(&cur_list)) {
11530Sstevel@tonic-gate 			err_print("Warning: unable to reload defect list\n");
11540Sstevel@tonic-gate 			cur_list.flags &= ~LIST_DIRTY;
11550Sstevel@tonic-gate 			return (-1);
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		cur_list.flags |= LIST_DIRTY;
11580Sstevel@tonic-gate 	}
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	if (cur_list.flags & LIST_DIRTY) {
11610Sstevel@tonic-gate 		assert(!EMBEDDED_SCSI);
11620Sstevel@tonic-gate 		write_deflist(&cur_list);
11630Sstevel@tonic-gate 		cur_list.flags = 0;
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 	if (cur_flags & LABEL_DIRTY) {
11660Sstevel@tonic-gate 		(void) write_label();
11670Sstevel@tonic-gate 		cur_flags &= ~LABEL_DIRTY;
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * Come up for air, since the verify step does not need to
11710Sstevel@tonic-gate 	 * be atomic (it does it's own lockouts when necessary).
11720Sstevel@tonic-gate 	 */
11730Sstevel@tonic-gate 	exit_critical();
11740Sstevel@tonic-gate 	/*
11750Sstevel@tonic-gate 	 * If we are supposed to verify, we do the 'write' test over
11760Sstevel@tonic-gate 	 * the format zone.  The rest of the analysis parameters are
11770Sstevel@tonic-gate 	 * left the way they were.
11780Sstevel@tonic-gate 	 */
11790Sstevel@tonic-gate 	if (scan_auto) {
11800Sstevel@tonic-gate 		scan_entire = 0;
11810Sstevel@tonic-gate 		scan_lower = start;
11820Sstevel@tonic-gate 		scan_upper = end;
11830Sstevel@tonic-gate 		fmt_print("\nVerifying media...");
11840Sstevel@tonic-gate 		status = do_scan(SCAN_PATTERN, F_SILENT);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 	/*
11870Sstevel@tonic-gate 	 * If the defect list or label is dirty, write them out again.
11880Sstevel@tonic-gate 	 */
11890Sstevel@tonic-gate 	if (cur_list.flags & LIST_DIRTY) {
11900Sstevel@tonic-gate 		assert(!EMBEDDED_SCSI);
11910Sstevel@tonic-gate 		cur_list.flags = 0;
11920Sstevel@tonic-gate 		write_deflist(&cur_list);
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 	if (cur_flags & LABEL_DIRTY) {
11950Sstevel@tonic-gate 		cur_flags &= ~LABEL_DIRTY;
11960Sstevel@tonic-gate 		(void) write_label();
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 	return (status);
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate /*
12020Sstevel@tonic-gate  * This routine implements the 'repair' command.  It allows the user
12030Sstevel@tonic-gate  * to reallocate sectors on the disk that have gone bad.
12040Sstevel@tonic-gate  */
12050Sstevel@tonic-gate int
c_repair()12060Sstevel@tonic-gate c_repair()
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	diskaddr_t	bn;
12090Sstevel@tonic-gate 	int		status;
12100Sstevel@tonic-gate 	u_ioparam_t	ioparam;
12119889SLarry.Liu@Sun.COM 	char		*buf;
12120Sstevel@tonic-gate 	int		buf_is_good;
12130Sstevel@tonic-gate 	int		block_has_error;
12140Sstevel@tonic-gate 	int		i;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
12170Sstevel@tonic-gate 	 * There must be a current disk type (and therefore a current disk).
12180Sstevel@tonic-gate 	 */
12190Sstevel@tonic-gate 	if (cur_dtype == NULL) {
12200Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
12210Sstevel@tonic-gate 		return (-1);
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 	/*
12240Sstevel@tonic-gate 	 * The current disk must be formatted for repair to work.
12250Sstevel@tonic-gate 	 */
12260Sstevel@tonic-gate 	if (!(cur_flags & DISK_FORMATTED)) {
12270Sstevel@tonic-gate 		err_print("Current Disk is unformatted.\n");
12280Sstevel@tonic-gate 		return (-1);
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 	/*
12310Sstevel@tonic-gate 	 * Check for a valid fdisk table entry for Solaris
12320Sstevel@tonic-gate 	 */
12330Sstevel@tonic-gate 	if (!good_fdisk()) {
12340Sstevel@tonic-gate 		return (-1);
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * Repair is an optional command for controllers, so it may
12380Sstevel@tonic-gate 	 * not be supported.
12390Sstevel@tonic-gate 	 */
12400Sstevel@tonic-gate 	if (cur_ops->op_repair == NULL) {
12410Sstevel@tonic-gate 		err_print("Controller does not support repairing.\n");
12420Sstevel@tonic-gate 		err_print("or disk supports automatic defect management.\n");
12430Sstevel@tonic-gate 		return (-1);
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 	/*
12460Sstevel@tonic-gate 	 * There must be a defect list for non-embedded scsi devices,
12470Sstevel@tonic-gate 	 * since we will add to it.
12480Sstevel@tonic-gate 	 */
12490Sstevel@tonic-gate 	if (!EMBEDDED_SCSI && cur_list.list == NULL) {
12500Sstevel@tonic-gate 		err_print("Current Defect List must be initialized.\n");
12510Sstevel@tonic-gate 		return (-1);
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * Ask the user which sector has gone bad.
12550Sstevel@tonic-gate 	 */
12560Sstevel@tonic-gate 	ioparam.io_bounds.lower = 0;
12570Sstevel@tonic-gate 	if (cur_disk->label_type == L_TYPE_SOLARIS) {
12589889SLarry.Liu@Sun.COM 		ioparam.io_bounds.upper = physsects() - 1;
12590Sstevel@tonic-gate 	} else {
12609889SLarry.Liu@Sun.COM 		ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba;
12610Sstevel@tonic-gate 	}
12620Sstevel@tonic-gate 	bn = input(FIO_BN,
12639889SLarry.Liu@Sun.COM 	    "Enter absolute block number of defect", ':',
12649889SLarry.Liu@Sun.COM 	    &ioparam, (int *)NULL, DATA_INPUT);
12650Sstevel@tonic-gate 	/*
12660Sstevel@tonic-gate 	 * Check to see if there is a mounted file system over the
12670Sstevel@tonic-gate 	 * specified sector.  If there is, make sure the user is
12680Sstevel@tonic-gate 	 * really serious.
12690Sstevel@tonic-gate 	 */
12700Sstevel@tonic-gate 	if (checkmount(bn, bn)) {
12710Sstevel@tonic-gate 		if (check("Repair is in a mounted partition, continue"))
12720Sstevel@tonic-gate 			return (-1);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 	/*
12750Sstevel@tonic-gate 	 * check for partitions being used for swapping in format zone
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	if (checkswap(bn, bn)) {
12780Sstevel@tonic-gate 		if (check("Repair is in a partition which is currently \
12790Sstevel@tonic-gate being used for swapping.\ncontinue"))
12800Sstevel@tonic-gate 		return (-1);
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 
1283767Ssjelinek 	if (checkdevinuse(cur_disk->disk_name, bn, bn, 0, 0)) {
1284767Ssjelinek 		if (check("Repair is in a partition which is currently "
1285767Ssjelinek 		    "in use.\ncontinue"))
1286767Ssjelinek 			return (-1);
1287767Ssjelinek 	}
1288767Ssjelinek 
12899889SLarry.Liu@Sun.COM 	buf = zalloc((cur_disk->disk_lbasize == 0) ?
12909889SLarry.Liu@Sun.COM 	    SECSIZE : cur_disk->disk_lbasize);
12919889SLarry.Liu@Sun.COM 
12920Sstevel@tonic-gate 	/*
12930Sstevel@tonic-gate 	 * Try to read the sector before repairing it.  If we can
12940Sstevel@tonic-gate 	 * get good data out of it, we can write that data back
12950Sstevel@tonic-gate 	 * after the repair.  If the sector looks ok, ask the
12960Sstevel@tonic-gate 	 * user to confirm the repair, since it doesn't appear
12970Sstevel@tonic-gate 	 * necessary.  Try reading the block several times to
12980Sstevel@tonic-gate 	 * see if we can read it consistently.
12990Sstevel@tonic-gate 	 *
13000Sstevel@tonic-gate 	 * First, let's see if the block appears to have problems...
13010Sstevel@tonic-gate 	 */
13020Sstevel@tonic-gate 	block_has_error = 1;
13030Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
13040Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
13059889SLarry.Liu@Sun.COM 		    1, buf, (F_SILENT | F_ALLERRS), NULL);
13060Sstevel@tonic-gate 		if (status)
13070Sstevel@tonic-gate 			break;		/* one of the tries failed */
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 	if (status == 0) {
13100Sstevel@tonic-gate 		block_has_error = 0;
13110Sstevel@tonic-gate 		if (check("\
13120Sstevel@tonic-gate This block doesn't appear to be bad.  Repair it anyway")) {
13139889SLarry.Liu@Sun.COM 			free(buf);
13140Sstevel@tonic-gate 			return (0);
13150Sstevel@tonic-gate 		}
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * Last chance...
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	if (check("Ready to repair defect, continue")) {
13219889SLarry.Liu@Sun.COM 		free(buf);
13220Sstevel@tonic-gate 		return (-1);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * We're committed to repairing it.  Try to get any good
13260Sstevel@tonic-gate 	 * data out of the block if possible.  Note that we do
13270Sstevel@tonic-gate 	 * not set the F_ALLERRS flag.
13280Sstevel@tonic-gate 	 */
13290Sstevel@tonic-gate 	buf_is_good = 0;
13300Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
13310Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
13329889SLarry.Liu@Sun.COM 		    1, buf, F_SILENT, NULL);
13330Sstevel@tonic-gate 		if (status == 0) {
13340Sstevel@tonic-gate 			buf_is_good = 1;
13350Sstevel@tonic-gate 			break;
13360Sstevel@tonic-gate 		}
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 	/*
13390Sstevel@tonic-gate 	 * Lock out interrupts so the disk can't get out of sync with
13400Sstevel@tonic-gate 	 * the defect list.
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 	enter_critical();
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	fmt_print("Repairing ");
13450Sstevel@tonic-gate 	if (block_has_error) {
13460Sstevel@tonic-gate 		fmt_print("%s error on ", buf_is_good ? "soft" : "hard");
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 	fmt_print("block %llu (", bn);
13490Sstevel@tonic-gate 	pr_dblock(fmt_print, bn);
13500Sstevel@tonic-gate 	fmt_print(")...");
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * Do the repair.
13530Sstevel@tonic-gate 	 */
13540Sstevel@tonic-gate 	status = (*cur_ops->op_repair)(bn, F_NORMAL);
13550Sstevel@tonic-gate 	if (status) {
13560Sstevel@tonic-gate 		fmt_print("failed.\n\n");
13570Sstevel@tonic-gate 	} else {
13580Sstevel@tonic-gate 		/*
13590Sstevel@tonic-gate 		 * The repair worked.  Write the old data to the new
13600Sstevel@tonic-gate 		 * block if we were able to read it, otherwise
13610Sstevel@tonic-gate 		 * zero out the new block.  If it looks like the
13620Sstevel@tonic-gate 		 * new block is bad, let the user know that, too.
13630Sstevel@tonic-gate 		 * Should we attempt auto-repair in this case?
13640Sstevel@tonic-gate 		 */
13650Sstevel@tonic-gate 		fmt_print("ok.\n");
13660Sstevel@tonic-gate 		if (!buf_is_good) {
13679889SLarry.Liu@Sun.COM 			bzero(buf, cur_disk->disk_lbasize);
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
13709889SLarry.Liu@Sun.COM 		    1, buf, (F_SILENT | F_ALLERRS), NULL);
13710Sstevel@tonic-gate 		if (status == 0) {
13720Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file,
13739889SLarry.Liu@Sun.COM 			    bn, 1, buf, (F_SILENT | F_ALLERRS), NULL);
13740Sstevel@tonic-gate 		}
13750Sstevel@tonic-gate 		if (status) {
13760Sstevel@tonic-gate 			fmt_print("The new block %llu (", bn);
13770Sstevel@tonic-gate 			pr_dblock(fmt_print, bn);
13780Sstevel@tonic-gate 			fmt_print(") also appears defective.\n");
13790Sstevel@tonic-gate 		}
13800Sstevel@tonic-gate 		fmt_print("\n");
13810Sstevel@tonic-gate 		/*
13820Sstevel@tonic-gate 		 * Add the bad sector to the defect list, write out
13830Sstevel@tonic-gate 		 * the defect list, and kill off the working list so
13840Sstevel@tonic-gate 		 * it will get synced up with the current defect list
13850Sstevel@tonic-gate 		 * next time we need it.
13860Sstevel@tonic-gate 		 *
13870Sstevel@tonic-gate 		 * For embedded scsi, we don't require a defect list.
13880Sstevel@tonic-gate 		 * However, if we have one, add the defect if the
13890Sstevel@tonic-gate 		 * list includes the grown list.  If not, kill it
13900Sstevel@tonic-gate 		 * to force a resync if we need the list later.
13910Sstevel@tonic-gate 		 */
13920Sstevel@tonic-gate 		if (EMBEDDED_SCSI) {
13930Sstevel@tonic-gate 			if (cur_list.list != NULL) {
13940Sstevel@tonic-gate 				if (cur_list.flags & LIST_PGLIST) {
13950Sstevel@tonic-gate 					add_ldef(bn, &cur_list);
13960Sstevel@tonic-gate 				} else {
13970Sstevel@tonic-gate 					kill_deflist(&cur_list);
13980Sstevel@tonic-gate 				}
13990Sstevel@tonic-gate 			}
14000Sstevel@tonic-gate 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
14010Sstevel@tonic-gate 			kill_deflist(&cur_list);
14020Sstevel@tonic-gate 			if (*cur_ops->op_ex_cur != NULL) {
14030Sstevel@tonic-gate 				(*cur_ops->op_ex_cur)(&cur_list);
14040Sstevel@tonic-gate 				fmt_print("Current list updated\n");
14050Sstevel@tonic-gate 			}
14060Sstevel@tonic-gate 		} else {
14070Sstevel@tonic-gate 			add_ldef(bn, &cur_list);
14080Sstevel@tonic-gate 			write_deflist(&cur_list);
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 		kill_deflist(&work_list);
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 	exit_critical();
14139889SLarry.Liu@Sun.COM 	free(buf);
14149889SLarry.Liu@Sun.COM 
14150Sstevel@tonic-gate 	/*
14160Sstevel@tonic-gate 	 * Return status.
14170Sstevel@tonic-gate 	 */
14180Sstevel@tonic-gate 	return (status);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate /*
14220Sstevel@tonic-gate  * This routine implements the 'show' command.  It translates a disk
14230Sstevel@tonic-gate  * block given in any format into decimal, hexadecimal, and
14240Sstevel@tonic-gate  * cylinder/head/sector format.
14250Sstevel@tonic-gate  */
14260Sstevel@tonic-gate int
c_show()14270Sstevel@tonic-gate c_show()
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	u_ioparam_t	ioparam;
14307563SPrasad.Singamsetty@Sun.COM 	diskaddr_t	bn;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	/*
14330Sstevel@tonic-gate 	 * There must be a current disk type, so we will know the geometry.
14340Sstevel@tonic-gate 	 */
14350Sstevel@tonic-gate 	if (cur_dtype == NULL) {
14360Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
14370Sstevel@tonic-gate 		return (-1);
14380Sstevel@tonic-gate 	}
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 * Ask the user for a disk block.
14410Sstevel@tonic-gate 	 */
14420Sstevel@tonic-gate 	ioparam.io_bounds.lower = 0;
14430Sstevel@tonic-gate 	if (cur_disk->label_type == L_TYPE_SOLARIS) {
14449889SLarry.Liu@Sun.COM 		ioparam.io_bounds.upper = physsects() - 1;
14450Sstevel@tonic-gate 	} else {
14469889SLarry.Liu@Sun.COM 		ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba;
14470Sstevel@tonic-gate 	}
14487563SPrasad.Singamsetty@Sun.COM 	bn = input(FIO_BN, "Enter a disk block", ':',
14490Sstevel@tonic-gate 	    &ioparam, (int *)NULL, DATA_INPUT);
14500Sstevel@tonic-gate 	/*
14510Sstevel@tonic-gate 	 * Echo it back.
14520Sstevel@tonic-gate 	 */
14537563SPrasad.Singamsetty@Sun.COM 	fmt_print("Disk block = %lld = 0x%llx = (", bn, bn);
14540Sstevel@tonic-gate 	pr_dblock(fmt_print, bn);
14550Sstevel@tonic-gate 	fmt_print(")\n\n");
14560Sstevel@tonic-gate 	return (0);
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate /*
14600Sstevel@tonic-gate  * This routine implements the 'label' command.  It writes the
14610Sstevel@tonic-gate  * primary and backup labels onto the current disk.
14620Sstevel@tonic-gate  */
14630Sstevel@tonic-gate int
c_label()14640Sstevel@tonic-gate c_label()
14650Sstevel@tonic-gate {
1466767Ssjelinek 	int			status;
1467767Ssjelinek 	int			deflt, *defltptr = NULL;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	/*
14700Sstevel@tonic-gate 	 * There must be a current disk type (and therefore a current disk).
14710Sstevel@tonic-gate 	 */
14720Sstevel@tonic-gate 	if (cur_dtype == NULL) {
14730Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
14740Sstevel@tonic-gate 		return (-1);
14750Sstevel@tonic-gate 	}
14760Sstevel@tonic-gate 	/*
14770Sstevel@tonic-gate 	 * The current disk must be formatted to label it.
14780Sstevel@tonic-gate 	 */
14790Sstevel@tonic-gate 	if (!(cur_flags & DISK_FORMATTED)) {
14800Sstevel@tonic-gate 		err_print("Current Disk is unformatted.\n");
14810Sstevel@tonic-gate 		return (-1);
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 	/*
14840Sstevel@tonic-gate 	 * Check for a valid fdisk table entry for Solaris
14850Sstevel@tonic-gate 	 */
14860Sstevel@tonic-gate 	if (!good_fdisk()) {
14870Sstevel@tonic-gate 		return (-1);
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 	/*
14900Sstevel@tonic-gate 	 * Check to see if there are any mounted file systems anywhere
14910Sstevel@tonic-gate 	 * on the current disk.  If so, refuse to label the disk, but
14920Sstevel@tonic-gate 	 * only if the partitions would change for the mounted partitions.
14930Sstevel@tonic-gate 	 *
14940Sstevel@tonic-gate 	 */
14957563SPrasad.Singamsetty@Sun.COM 	if (checkmount((diskaddr_t)-1, (diskaddr_t)-1)) {
14960Sstevel@tonic-gate 		/* Bleagh, too descriptive */
14970Sstevel@tonic-gate 		if (check_label_with_mount()) {
14980Sstevel@tonic-gate 			err_print("Cannot label disk while it has "
14999889SLarry.Liu@Sun.COM 			    "mounted partitions.\n\n");
15000Sstevel@tonic-gate 			return (-1);
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 	}
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * check to see if there any partitions being used for swapping
15060Sstevel@tonic-gate 	 * on the current disk.  If so, refuse to label the disk, but
15070Sstevel@tonic-gate 	 * only if the partitions would change for the mounted partitions.
15080Sstevel@tonic-gate 	 */
15097563SPrasad.Singamsetty@Sun.COM 	if (checkswap((diskaddr_t)-1, (diskaddr_t)-1)) {
15100Sstevel@tonic-gate 		if (check_label_with_swap()) {
15110Sstevel@tonic-gate 			err_print("Cannot label disk while its "
15120Sstevel@tonic-gate 			    "partitions are currently being used for "
15130Sstevel@tonic-gate 			    "swapping.\n");
15140Sstevel@tonic-gate 			return (-1);
15150Sstevel@tonic-gate 		}
15160Sstevel@tonic-gate 	}
15170Sstevel@tonic-gate 
1518767Ssjelinek 	/*
1519767Ssjelinek 	 * Check to see if any partitions used for svm, vxvm or live upgrade
1520767Ssjelinek 	 * are on the disk. If so, refuse to label the disk, but only
1521767Ssjelinek 	 * if we are trying to shrink a partition in use.
1522767Ssjelinek 	 */
1523767Ssjelinek 	if (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
1524767Ssjelinek 	    (diskaddr_t)-1, 0, 1)) {
1525767Ssjelinek 		err_print("Cannot label disk when "
1526767Ssjelinek 		    "partitions are in use as described.\n");
1527767Ssjelinek 		return (-1);
1528767Ssjelinek 	}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	/*
15310Sstevel@tonic-gate 	 * If there is not a current partition map, warn the user we
15320Sstevel@tonic-gate 	 * are going to use the default.  The default is the first
15330Sstevel@tonic-gate 	 * partition map we encountered in the data file.  If there is
15340Sstevel@tonic-gate 	 * no default we give up.
15350Sstevel@tonic-gate 	 */
15360Sstevel@tonic-gate 	if (cur_parts == NULL) {
15370Sstevel@tonic-gate 		fmt_print("Current Partition Table is not set, "
15389889SLarry.Liu@Sun.COM 		    "using default.\n");
15390Sstevel@tonic-gate 		cur_disk->disk_parts = cur_parts = cur_dtype->dtype_plist;
15400Sstevel@tonic-gate 		if (cur_parts == NULL) {
15410Sstevel@tonic-gate 			err_print("No default available, cannot label.\n");
15420Sstevel@tonic-gate 			return (-1);
15430Sstevel@tonic-gate 		}
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 	/*
15460Sstevel@tonic-gate 	 * If expert (-e) mode, then ask user if they wish
15470Sstevel@tonic-gate 	 * to change the current solaris label into an EFI one
15480Sstevel@tonic-gate 	 */
15490Sstevel@tonic-gate 	if (expert_mode) {
15500Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
15519889SLarry.Liu@Sun.COM 		int 		i;
15520Sstevel@tonic-gate #endif
15539889SLarry.Liu@Sun.COM 		int 		choice;
15549889SLarry.Liu@Sun.COM 		u_ioparam_t		ioparam;
15559889SLarry.Liu@Sun.COM 		struct extvtoc	vtoc;
15569889SLarry.Liu@Sun.COM 		struct dk_label	label;
15579889SLarry.Liu@Sun.COM 		struct dk_gpt	*vtoc64;
15589889SLarry.Liu@Sun.COM 		struct efi_info	efinfo;
15599889SLarry.Liu@Sun.COM 		struct disk_type	*dptr;
15600Sstevel@tonic-gate 
15619889SLarry.Liu@Sun.COM 		/* Ask user what label to use */
15629889SLarry.Liu@Sun.COM 		fmt_print("[0] SMI Label\n");
15639889SLarry.Liu@Sun.COM 		fmt_print("[1] EFI Label\n");
15649889SLarry.Liu@Sun.COM 		ioparam.io_bounds.lower = 0;
15659889SLarry.Liu@Sun.COM 		ioparam.io_bounds.upper = 1;
15669889SLarry.Liu@Sun.COM 		if (cur_label == L_TYPE_SOLARIS)
15679889SLarry.Liu@Sun.COM 			deflt = 0;
15689889SLarry.Liu@Sun.COM 		else
15699889SLarry.Liu@Sun.COM 			deflt = 1;
15709889SLarry.Liu@Sun.COM 		defltptr = &deflt;
15719889SLarry.Liu@Sun.COM 		choice = input(FIO_INT, "Specify Label type", ':',
15729889SLarry.Liu@Sun.COM 		    &ioparam, defltptr, DATA_INPUT);
15739889SLarry.Liu@Sun.COM 		if ((choice == 0) && (cur_label == L_TYPE_SOLARIS)) {
15749889SLarry.Liu@Sun.COM 			goto expert_end;
15759889SLarry.Liu@Sun.COM 		} else if ((choice == 1) && (cur_label == L_TYPE_EFI)) {
15769889SLarry.Liu@Sun.COM 			goto expert_end;
15779889SLarry.Liu@Sun.COM 		}
15789889SLarry.Liu@Sun.COM 		switch (choice) {
15799889SLarry.Liu@Sun.COM 		case 0:
15800Sstevel@tonic-gate 		/*
15810Sstevel@tonic-gate 		 * EFI label to SMI label
15820Sstevel@tonic-gate 		 */
15830Sstevel@tonic-gate 		if (cur_dtype->capacity > INFINITY) {
15849889SLarry.Liu@Sun.COM 			fmt_print("Warning: SMI labels only support up to "
15859889SLarry.Liu@Sun.COM 			    "2 TB.\n");
15860Sstevel@tonic-gate 		}
15870Sstevel@tonic-gate 
15887563SPrasad.Singamsetty@Sun.COM 		if (cur_disk->fdisk_part.systid == EFI_PMBR) {
15899889SLarry.Liu@Sun.COM 			fmt_print("Warning: This disk has an EFI label. "
15909889SLarry.Liu@Sun.COM 			    "Changing to SMI label will erase all\n"
15919889SLarry.Liu@Sun.COM 			    "current partitions.\n");
15929889SLarry.Liu@Sun.COM 			if (check("Continue"))
1593786Slclee 			return (-1);
15947563SPrasad.Singamsetty@Sun.COM #if defined(_FIRMWARE_NEEDS_FDISK)
15959889SLarry.Liu@Sun.COM 			fmt_print("You must use fdisk to delete the current "
15961344Syl194034 			    "EFI partition and create a new\n"
15971344Syl194034 			    "Solaris partition before you can convert the "
15981344Syl194034 			    "label.\n");
15999889SLarry.Liu@Sun.COM 			return (-1);
16007563SPrasad.Singamsetty@Sun.COM #endif
16017563SPrasad.Singamsetty@Sun.COM 		}
16027563SPrasad.Singamsetty@Sun.COM 
16037563SPrasad.Singamsetty@Sun.COM #if defined(_FIRMWARE_NEEDS_FDISK)
16047563SPrasad.Singamsetty@Sun.COM 		if (!(((cur_disk->fdisk_part.systid != SUNIXOS) ||
16057563SPrasad.Singamsetty@Sun.COM 		    (cur_disk->fdisk_part.systid != SUNIXOS2)) &&
16067563SPrasad.Singamsetty@Sun.COM 		    (cur_disk->fdisk_part.numsect > 0))) {
16077563SPrasad.Singamsetty@Sun.COM 			fmt_print("You must use fdisk to create a Solaris "
16087563SPrasad.Singamsetty@Sun.COM 			    "partition before you can convert the label.\n");
16091344Syl194034 			return (-1);
16101344Syl194034 		}
16117563SPrasad.Singamsetty@Sun.COM #endif
16127563SPrasad.Singamsetty@Sun.COM 
16137563SPrasad.Singamsetty@Sun.COM 		(void) memset((char *)&label, 0, sizeof (struct dk_label));
16141344Syl194034 
1615791Sshidokht 		(void) strcpy(x86_devname, cur_disk->disk_name);
1616786Slclee 		if (cur_ctype->ctype_ctype == DKC_DIRECT)
1617786Slclee 			dptr = auto_direct_get_geom_label(cur_file,  &label);
1618786Slclee 		else
1619786Slclee 			dptr = auto_sense(cur_file, 1, &label);
16200Sstevel@tonic-gate 		if (dptr == NULL) {
1621786Slclee 			fmt_print("Autoconfiguration failed.\n");
1622786Slclee 			return (-1);
16230Sstevel@tonic-gate 		}
16240Sstevel@tonic-gate 
1625786Slclee 		pcyl = label.dkl_pcyl;
1626786Slclee 		ncyl = label.dkl_ncyl;
1627786Slclee 		acyl = label.dkl_acyl;
1628786Slclee 		nhead = label.dkl_nhead;
1629786Slclee 		nsect = label.dkl_nsect;
1630786Slclee 
16311524Slh195018 		if (delete_disk_type(cur_disk->disk_type) == 0) {
16321524Slh195018 			cur_label = L_TYPE_SOLARIS;
16331524Slh195018 			cur_disk->label_type = L_TYPE_SOLARIS;
16341524Slh195018 			cur_disk->disk_type = dptr;
16351524Slh195018 			cur_disk->disk_parts = dptr->dtype_plist;
16361524Slh195018 			cur_dtype = dptr;
16371524Slh195018 			cur_parts = dptr->dtype_plist;
1638786Slclee 
16391524Slh195018 			if (status = write_label())
16401524Slh195018 				err_print("Label failed.\n");
16411524Slh195018 			else
16421524Slh195018 				cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
16431524Slh195018 
16441524Slh195018 			return (status);
16451524Slh195018 		} else {
1646786Slclee 			err_print("Label failed.\n");
16471524Slh195018 			return (-1);
16481524Slh195018 		}
1649786Slclee 
1650786Slclee 
16519889SLarry.Liu@Sun.COM 		case 1:
16520Sstevel@tonic-gate 		/*
16530Sstevel@tonic-gate 		 * SMI label to EFI label
16540Sstevel@tonic-gate 		 */
1655786Slclee 
1656786Slclee 
1657786Slclee 		fmt_print("Warning: This disk has an SMI label. Changing to "
1658786Slclee 		    "EFI label will erase all\ncurrent partitions.\n");
1659786Slclee 
1660786Slclee 		if (check("Continue")) {
16610Sstevel@tonic-gate 			return (-1);
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 		if (get_disk_info(cur_file, &efinfo) != 0) {
16659889SLarry.Liu@Sun.COM 			return (-1);
16660Sstevel@tonic-gate 		}
16670Sstevel@tonic-gate 		(void) memset((char *)&label, 0, sizeof (struct dk_label));
16680Sstevel@tonic-gate 		label.dkl_pcyl = pcyl;
16690Sstevel@tonic-gate 		label.dkl_ncyl = ncyl;
16700Sstevel@tonic-gate 		label.dkl_acyl = acyl;
16710Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
16720Sstevel@tonic-gate 		label.dkl_bcyl = bcyl;
16730Sstevel@tonic-gate #endif			/* defined(_SUNOC_VTOC_16) */
16740Sstevel@tonic-gate 		label.dkl_nhead = nhead;
16750Sstevel@tonic-gate 		label.dkl_nsect = nsect;
16760Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
16770Sstevel@tonic-gate 		for (i = 0; i < NDKMAP; i++) {
16789889SLarry.Liu@Sun.COM 			label.dkl_map[i] = cur_parts->pinfo_map[i];
16790Sstevel@tonic-gate 		}
16800Sstevel@tonic-gate #endif			/* defined(_SUNOS_VTOC_8) */
16810Sstevel@tonic-gate 		label.dkl_magic = DKL_MAGIC;
16820Sstevel@tonic-gate 		label.dkl_vtoc = cur_parts->vtoc;
16830Sstevel@tonic-gate 		if (label_to_vtoc(&vtoc, &label) == -1) {
16849889SLarry.Liu@Sun.COM 			return (-1);
16850Sstevel@tonic-gate 		}
16861067Sgz161490 		if (SMI_vtoc_to_EFI(cur_file, &vtoc64) == -1) {
16879889SLarry.Liu@Sun.COM 			return (-1);
16880Sstevel@tonic-gate 		}
16890Sstevel@tonic-gate 		if (efi_write(cur_file, vtoc64) != 0) {
16909889SLarry.Liu@Sun.COM 			err_check(vtoc64);
16919889SLarry.Liu@Sun.COM 			err_print("Warning: error writing EFI.\n");
16929889SLarry.Liu@Sun.COM 			return (-1);
16931524Slh195018 		} else {
16941524Slh195018 			cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
16950Sstevel@tonic-gate 		}
16960Sstevel@tonic-gate 		/*
16970Sstevel@tonic-gate 		 * copy over the EFI vtoc onto the SMI vtoc and return
16980Sstevel@tonic-gate 		 * okay.
16990Sstevel@tonic-gate 		 */
17001524Slh195018 		dptr = auto_efi_sense(cur_file, &efinfo);
17011524Slh195018 		if (dptr == NULL) {
17021524Slh195018 			fmt_print("Autoconfiguration failed.\n");
17031524Slh195018 			return (-1);
17041524Slh195018 		}
17051524Slh195018 
17060Sstevel@tonic-gate 		cur_label = L_TYPE_EFI;
17070Sstevel@tonic-gate 		cur_disk->label_type = L_TYPE_EFI;
17081524Slh195018 		cur_disk->disk_type = dptr;
17091524Slh195018 		cur_disk->disk_parts = dptr->dtype_plist;
17101524Slh195018 		cur_dtype = dptr;
17111524Slh195018 		cur_parts = dptr->dtype_plist;
17121524Slh195018 		cur_parts->etoc = vtoc64;
17131524Slh195018 
1714362Sbg159949 		ncyl = pcyl = nsect = psect = acyl = phead = 0;
17150Sstevel@tonic-gate 
17161524Slh195018 		/*
17171524Slh195018 		 * Get the Solais Fdisk Partition information.
17181524Slh195018 		 */
17191524Slh195018 		(void) copy_solaris_part(&cur_disk->fdisk_part);
17201524Slh195018 
17210Sstevel@tonic-gate 		return (0);
17229889SLarry.Liu@Sun.COM 		}
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate expert_end:
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * Make sure the user is serious.
17280Sstevel@tonic-gate 	 */
17290Sstevel@tonic-gate 	if (check("Ready to label disk, continue")) {
17300Sstevel@tonic-gate 		return (-1);
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 	/*
17330Sstevel@tonic-gate 	 * Write the labels out (this will also notify unix) and
17340Sstevel@tonic-gate 	 * return status.
17350Sstevel@tonic-gate 	 */
17360Sstevel@tonic-gate 	fmt_print("\n");
17370Sstevel@tonic-gate 	if (status = write_label())
17380Sstevel@tonic-gate 		err_print("Label failed.\n");
17390Sstevel@tonic-gate 	return (status);
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate /*
17430Sstevel@tonic-gate  * This routine implements the 'analyze' command.  It simply runs
17440Sstevel@tonic-gate  * the analyze menu.
17450Sstevel@tonic-gate  */
17460Sstevel@tonic-gate int
c_analyze()17470Sstevel@tonic-gate c_analyze()
17480Sstevel@tonic-gate {
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	/*
17510Sstevel@tonic-gate 	 * There must be a current disk type (and therefor a current disk).
17520Sstevel@tonic-gate 	 */
17530Sstevel@tonic-gate 	if (cur_dtype == NULL) {
17540Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
17550Sstevel@tonic-gate 		return (-1);
17560Sstevel@tonic-gate 	}
17570Sstevel@tonic-gate 	cur_menu++;
17580Sstevel@tonic-gate 	last_menu = cur_menu;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	/*
17610Sstevel@tonic-gate 	 * Run the menu.
17620Sstevel@tonic-gate 	 */
17630Sstevel@tonic-gate 	run_menu(menu_analyze, "ANALYZE", "analyze", 0);
17640Sstevel@tonic-gate 	cur_menu--;
17650Sstevel@tonic-gate 	return (0);
17660Sstevel@tonic-gate }
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate /*
17690Sstevel@tonic-gate  * This routine implements the 'defect' command.  It simply runs
17700Sstevel@tonic-gate  * the defect menu.
17710Sstevel@tonic-gate  */
17720Sstevel@tonic-gate int
c_defect()17730Sstevel@tonic-gate c_defect()
17740Sstevel@tonic-gate {
17750Sstevel@tonic-gate 	int	i;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/*
17780Sstevel@tonic-gate 	 * There must be a current disk type (and therefor a current disk).
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	if (cur_dtype == NULL) {
17810Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
17820Sstevel@tonic-gate 		return (-1);
17830Sstevel@tonic-gate 	}
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	/*
17860Sstevel@tonic-gate 	 * Check for the defect management and list management ops and
17870Sstevel@tonic-gate 	 * display appropriate message.
17880Sstevel@tonic-gate 	 */
17890Sstevel@tonic-gate 	if ((cur_ops->op_ex_man == NULL) && (cur_ops->op_ex_cur == NULL) &&
17909889SLarry.Liu@Sun.COM 	    (cur_ops->op_create == NULL) && (cur_ops->op_wr_cur == NULL)) {
17910Sstevel@tonic-gate 		err_print("Controller does not support defect management\n");
17920Sstevel@tonic-gate 		err_print("or disk supports automatic defect management.\n");
17930Sstevel@tonic-gate 		return (-1);
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 	cur_menu++;
17960Sstevel@tonic-gate 	last_menu = cur_menu;
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	/*
17990Sstevel@tonic-gate 	 * Lock out interrupt while we manipulate the defect lists.
18000Sstevel@tonic-gate 	 */
18010Sstevel@tonic-gate 	enter_critical();
18020Sstevel@tonic-gate 	/*
18030Sstevel@tonic-gate 	 * If the working list is null but there is a current list,
18040Sstevel@tonic-gate 	 * update the working list to be a copy of the current list.
18050Sstevel@tonic-gate 	 */
18060Sstevel@tonic-gate 	if ((work_list.list == NULL) && (cur_list.list != NULL)) {
18070Sstevel@tonic-gate 		work_list.header = cur_list.header;
18080Sstevel@tonic-gate 		work_list.list = (struct defect_entry *)zalloc(
18099889SLarry.Liu@Sun.COM 		    deflist_size(cur_blksz, work_list.header.count) *
18109889SLarry.Liu@Sun.COM 		    cur_blksz);
18110Sstevel@tonic-gate 		for (i = 0; i < work_list.header.count; i++)
18120Sstevel@tonic-gate 			*(work_list.list + i) = *(cur_list.list + i);
18130Sstevel@tonic-gate 		work_list.flags = cur_list.flags & LIST_PGLIST;
18140Sstevel@tonic-gate 	}
18150Sstevel@tonic-gate 	exit_critical();
18160Sstevel@tonic-gate 	/*
18170Sstevel@tonic-gate 	 * Run the menu.
18180Sstevel@tonic-gate 	 */
18190Sstevel@tonic-gate 	run_menu(menu_defect, "DEFECT", "defect", 0);
18200Sstevel@tonic-gate 	cur_menu--;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	/*
18230Sstevel@tonic-gate 	 * If the user has modified the working list but not committed
18240Sstevel@tonic-gate 	 * it, warn him that he is probably making a mistake.
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	if (work_list.flags & LIST_DIRTY) {
18270Sstevel@tonic-gate 		if (!EMBEDDED_SCSI) {
18280Sstevel@tonic-gate 			err_print(
18290Sstevel@tonic-gate 		"Warning: working defect list modified; but not committed.\n");
18300Sstevel@tonic-gate 			if (!check(
18310Sstevel@tonic-gate 		"Do you wish to commit changes to current defect list"))
18320Sstevel@tonic-gate 			(void) do_commit();
18330Sstevel@tonic-gate 		}
18340Sstevel@tonic-gate 	}
18350Sstevel@tonic-gate 	return (0);
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate /*
18390Sstevel@tonic-gate  * This routine implements the 'backup' command.  It allows the user
18400Sstevel@tonic-gate  * to search for backup labels on the current disk.  This is useful
18410Sstevel@tonic-gate  * if the primary label was lost and the user wishes to recover the
18420Sstevel@tonic-gate  * partition information for the disk. The disk is relabeled and
18430Sstevel@tonic-gate  * the current defect list is written out if a backup label is found.
18440Sstevel@tonic-gate  */
18450Sstevel@tonic-gate int
c_backup()18460Sstevel@tonic-gate c_backup()
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	struct	dk_label label;
18490Sstevel@tonic-gate 	struct	disk_type *dtype;
18500Sstevel@tonic-gate 	struct	partition_info *parts, *plist;
18517563SPrasad.Singamsetty@Sun.COM 	diskaddr_t	bn;
18520Sstevel@tonic-gate 	int	sec, head, i;
18539889SLarry.Liu@Sun.COM 	char	*buf;
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * There must be a current disk type (and therefore a current disk).
18570Sstevel@tonic-gate 	 */
18580Sstevel@tonic-gate 	if (cur_dtype == NULL) {
18590Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
18600Sstevel@tonic-gate 		return (-1);
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	/*
18630Sstevel@tonic-gate 	 * The disk must be formatted to read backup labels.
18640Sstevel@tonic-gate 	 */
18650Sstevel@tonic-gate 	if (!(cur_flags & DISK_FORMATTED)) {
18660Sstevel@tonic-gate 		err_print("Current Disk is unformatted.\n");
18670Sstevel@tonic-gate 		return (-1);
18680Sstevel@tonic-gate 	}
18690Sstevel@tonic-gate 	/*
18700Sstevel@tonic-gate 	 * Check for a valid fdisk table entry for Solaris
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	if (!good_fdisk()) {
18730Sstevel@tonic-gate 		return (-1);
18740Sstevel@tonic-gate 	}
18750Sstevel@tonic-gate 	/*
18760Sstevel@tonic-gate 	 * If we found a primary label on this disk, make sure
18770Sstevel@tonic-gate 	 * the user is serious.
18780Sstevel@tonic-gate 	 */
18790Sstevel@tonic-gate 	if (cur_disk->label_type == L_TYPE_EFI) {
18809889SLarry.Liu@Sun.COM 		if (((cur_disk->disk_parts->etoc->efi_flags &
18819889SLarry.Liu@Sun.COM 		    EFI_GPT_PRIMARY_CORRUPT) == 0) &&
18829889SLarry.Liu@Sun.COM 		    check("Disk has a primary label, still continue"))
18839889SLarry.Liu@Sun.COM 			return (-1);
18849889SLarry.Liu@Sun.COM 		fmt_print("Restoring primary label.\n");
18859889SLarry.Liu@Sun.COM 		if (write_label()) {
18869889SLarry.Liu@Sun.COM 			err_print("Failed\n");
18879889SLarry.Liu@Sun.COM 			return (-1);
18889889SLarry.Liu@Sun.COM 		}
18899889SLarry.Liu@Sun.COM 		return (0);
18900Sstevel@tonic-gate 	} else if (((cur_disk->disk_flags & (DSK_LABEL | DSK_LABEL_DIRTY)) ==
18919889SLarry.Liu@Sun.COM 	    DSK_LABEL) &&
18920Sstevel@tonic-gate 	    (check("Disk has a primary label, still continue"))) {
18930Sstevel@tonic-gate 		return (-1);
18940Sstevel@tonic-gate 	}
18959889SLarry.Liu@Sun.COM 
18969889SLarry.Liu@Sun.COM 	buf = zalloc(cur_blksz);
18970Sstevel@tonic-gate 	fmt_print("Searching for backup labels...");
18980Sstevel@tonic-gate 	(void) fflush(stdout);
18999889SLarry.Liu@Sun.COM 
19000Sstevel@tonic-gate 	/*
19010Sstevel@tonic-gate 	 * Some disks have the backup labels in a strange place.
19020Sstevel@tonic-gate 	 */
19030Sstevel@tonic-gate 	if (cur_ctype->ctype_flags & CF_BLABEL)
19040Sstevel@tonic-gate 		head = 2;
19050Sstevel@tonic-gate 	else
19060Sstevel@tonic-gate 		head = nhead - 1;
19070Sstevel@tonic-gate 	/*
19080Sstevel@tonic-gate 	 * Loop through each copy of the backup label.
19090Sstevel@tonic-gate 	 */
19100Sstevel@tonic-gate 	for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect));
19110Sstevel@tonic-gate 	    sec += 2) {
19120Sstevel@tonic-gate 		bn = chs2bn(ncyl + acyl - 1, head, sec) + solaris_offset;
19130Sstevel@tonic-gate 		/*
19140Sstevel@tonic-gate 		 * Attempt to read it.
19150Sstevel@tonic-gate 		 */
19167563SPrasad.Singamsetty@Sun.COM 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
19179889SLarry.Liu@Sun.COM 		    1, buf, F_NORMAL, NULL)) {
19180Sstevel@tonic-gate 			continue;
19190Sstevel@tonic-gate 		}
19209889SLarry.Liu@Sun.COM 
19219889SLarry.Liu@Sun.COM 		(void *) memcpy((char *)&label, buf, sizeof (struct dk_label));
19229889SLarry.Liu@Sun.COM 
19230Sstevel@tonic-gate 		/*
19240Sstevel@tonic-gate 		 * Verify that it is a reasonable label.
19250Sstevel@tonic-gate 		 */
19260Sstevel@tonic-gate 		if (!checklabel(&label))
19270Sstevel@tonic-gate 			continue;
19280Sstevel@tonic-gate 		if (trim_id(label.dkl_asciilabel))
19290Sstevel@tonic-gate 			continue;
19300Sstevel@tonic-gate 		/*
19310Sstevel@tonic-gate 		 * Lock out interrupts while we manipulate lists.
19320Sstevel@tonic-gate 		 */
19330Sstevel@tonic-gate 		enter_critical();
19340Sstevel@tonic-gate 		fmt_print("found.\n");
19350Sstevel@tonic-gate 		/*
19360Sstevel@tonic-gate 		 * Find out which disk type the backup label claims.
19370Sstevel@tonic-gate 		 */
19380Sstevel@tonic-gate 		for (dtype = cur_ctype->ctype_dlist; dtype != NULL;
19390Sstevel@tonic-gate 		    dtype = dtype->dtype_next)
19400Sstevel@tonic-gate 			if (dtype_match(&label, dtype))
19410Sstevel@tonic-gate 				break;
19420Sstevel@tonic-gate 		/*
19430Sstevel@tonic-gate 		 * If it disagrees with our current type, something
19440Sstevel@tonic-gate 		 * real bad is happening.
19450Sstevel@tonic-gate 		 */
19460Sstevel@tonic-gate 		if (dtype != cur_dtype) {
19470Sstevel@tonic-gate 			if (dtype == NULL) {
19480Sstevel@tonic-gate 				fmt_print("\
19490Sstevel@tonic-gate Unknown disk type in backup label\n");
19500Sstevel@tonic-gate 				exit_critical();
19519889SLarry.Liu@Sun.COM 				free(buf);
19520Sstevel@tonic-gate 				return (-1);
19530Sstevel@tonic-gate 			}
19540Sstevel@tonic-gate 			fmt_print("Backup label claims different type:\n");
19550Sstevel@tonic-gate 			fmt_print("    <%s cyl %d alt %d hd %d sec %d>\n",
19569889SLarry.Liu@Sun.COM 			    label.dkl_asciilabel, label.dkl_ncyl,
19579889SLarry.Liu@Sun.COM 			    label.dkl_acyl, label.dkl_nhead,
19589889SLarry.Liu@Sun.COM 			    label.dkl_nsect);
19590Sstevel@tonic-gate 			if (check("Continue")) {
19600Sstevel@tonic-gate 				exit_critical();
19619889SLarry.Liu@Sun.COM 				free(buf);
19620Sstevel@tonic-gate 				return (-1);
19630Sstevel@tonic-gate 			}
19640Sstevel@tonic-gate 			cur_dtype = dtype;
19650Sstevel@tonic-gate 		}
19660Sstevel@tonic-gate 		/*
19670Sstevel@tonic-gate 		 * Try to match the partition map with a known map.
19680Sstevel@tonic-gate 		 */
19690Sstevel@tonic-gate 		for (parts = dtype->dtype_plist; parts != NULL;
19700Sstevel@tonic-gate 		    parts = parts->pinfo_next)
19710Sstevel@tonic-gate 			if (parts_match(&label, parts))
19720Sstevel@tonic-gate 				break;
19730Sstevel@tonic-gate 		/*
19740Sstevel@tonic-gate 		 * If we couldn't match it, allocate space for a new one,
19750Sstevel@tonic-gate 		 * fill in the info, and add it to the list.  The name
19760Sstevel@tonic-gate 		 * for the new map is derived from the disk name.
19770Sstevel@tonic-gate 		 */
19780Sstevel@tonic-gate 		if (parts == NULL) {
19790Sstevel@tonic-gate 			parts = (struct partition_info *)
19809889SLarry.Liu@Sun.COM 			    zalloc(sizeof (struct partition_info));
19810Sstevel@tonic-gate 			plist = dtype->dtype_plist;
19820Sstevel@tonic-gate 			if (plist == NULL)
19830Sstevel@tonic-gate 				dtype->dtype_plist = parts;
19840Sstevel@tonic-gate 			else {
19850Sstevel@tonic-gate 				while (plist->pinfo_next != NULL)
19860Sstevel@tonic-gate 					plist = plist->pinfo_next;
19870Sstevel@tonic-gate 				plist->pinfo_next = parts;
19880Sstevel@tonic-gate 			}
19890Sstevel@tonic-gate 			parts->pinfo_name = alloc_string("original");
19900Sstevel@tonic-gate 			for (i = 0; i < NDKMAP; i++)
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
19930Sstevel@tonic-gate 				parts->pinfo_map[i] = label.dkl_map[i];
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_16)
19960Sstevel@tonic-gate 				parts->pinfo_map[i].dkl_cylno  =
19970Sstevel@tonic-gate 				    label.dkl_vtoc.v_part[i].p_start / spc();
19980Sstevel@tonic-gate 				parts->pinfo_map[i].dkl_nblk =
19990Sstevel@tonic-gate 				    label.dkl_vtoc.v_part[i].p_size;
20000Sstevel@tonic-gate #else
20010Sstevel@tonic-gate #error No VTOC layout defined.
20020Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_8) */
20030Sstevel@tonic-gate 			parts->vtoc = label.dkl_vtoc;
20040Sstevel@tonic-gate 		}
20050Sstevel@tonic-gate 		/*
20060Sstevel@tonic-gate 		 * We now have a partition map.  Make it the current map.
20070Sstevel@tonic-gate 		 */
20080Sstevel@tonic-gate 		cur_disk->disk_parts = cur_parts = parts;
20090Sstevel@tonic-gate 		exit_critical();
20100Sstevel@tonic-gate 		/*
20110Sstevel@tonic-gate 		 * Rewrite the labels and defect lists, as appropriate.
20120Sstevel@tonic-gate 		 */
20130Sstevel@tonic-gate 		if (EMBEDDED_SCSI) {
20140Sstevel@tonic-gate 			fmt_print("Restoring primary label.\n");
20159889SLarry.Liu@Sun.COM 			if (write_label()) {
20169889SLarry.Liu@Sun.COM 				free(buf);
20170Sstevel@tonic-gate 				return (-1);
20189889SLarry.Liu@Sun.COM 			}
20190Sstevel@tonic-gate 		} else {
20200Sstevel@tonic-gate 			fmt_print("Restoring primary label and defect list.\n");
20219889SLarry.Liu@Sun.COM 			if (write_label()) {
20229889SLarry.Liu@Sun.COM 				free(buf);
20230Sstevel@tonic-gate 				return (-1);
20249889SLarry.Liu@Sun.COM 			}
20250Sstevel@tonic-gate 			if (cur_list.list != NULL)
20260Sstevel@tonic-gate 				write_deflist(&cur_list);
20270Sstevel@tonic-gate 		}
20280Sstevel@tonic-gate 		fmt_print("\n");
20299889SLarry.Liu@Sun.COM 		free(buf);
20300Sstevel@tonic-gate 		return (0);
20310Sstevel@tonic-gate 	}
20320Sstevel@tonic-gate 	/*
20330Sstevel@tonic-gate 	 * If we didn't find any backup labels, say so.
20340Sstevel@tonic-gate 	 */
20350Sstevel@tonic-gate 	fmt_print("not found.\n\n");
20369889SLarry.Liu@Sun.COM 	free(buf);
20370Sstevel@tonic-gate 	return (0);
20380Sstevel@tonic-gate }
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate /*
20410Sstevel@tonic-gate  * This routine is called by c_verify() for an EFI labeled disk
20420Sstevel@tonic-gate  */
20430Sstevel@tonic-gate static int
c_verify_efi()20440Sstevel@tonic-gate c_verify_efi()
20450Sstevel@tonic-gate {
20460Sstevel@tonic-gate 	struct efi_info efi_info;
20470Sstevel@tonic-gate 	struct	partition_info	tmp_pinfo;
20480Sstevel@tonic-gate 	int status;
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	status = read_efi_label(cur_file, &efi_info);
20510Sstevel@tonic-gate 	if (status != 0) {
20529889SLarry.Liu@Sun.COM 		err_print("Warning: Could not read label.\n");
20539889SLarry.Liu@Sun.COM 		return (-1);
20540Sstevel@tonic-gate 	}
20550Sstevel@tonic-gate 	if (cur_parts->etoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
20560Sstevel@tonic-gate 		err_print("Reading the primary EFI GPT label ");
20570Sstevel@tonic-gate 		err_print("failed.  Using backup label.\n");
20580Sstevel@tonic-gate 		err_print("Use the 'backup' command to restore ");
20590Sstevel@tonic-gate 		err_print("the primary label.\n");
20600Sstevel@tonic-gate 	}
20610Sstevel@tonic-gate 	tmp_pinfo.etoc = efi_info.e_parts;
20620Sstevel@tonic-gate 	fmt_print("\n");
20630Sstevel@tonic-gate 	if (cur_parts->etoc->efi_parts[8].p_name) {
20649889SLarry.Liu@Sun.COM 		fmt_print("Volume name = <%8s>\n",
20659889SLarry.Liu@Sun.COM 		    cur_parts->etoc->efi_parts[8].p_name);
20660Sstevel@tonic-gate 	} else {
20679889SLarry.Liu@Sun.COM 		fmt_print("Volume name = <        >\n");
20680Sstevel@tonic-gate 	}
20690Sstevel@tonic-gate 	fmt_print("ascii name  = ");
20700Sstevel@tonic-gate 	print_efi_string(efi_info.vendor, efi_info.product,
20710Sstevel@tonic-gate 	    efi_info.revision, efi_info.capacity);
20720Sstevel@tonic-gate 	fmt_print("\n");
20730Sstevel@tonic-gate 
20749889SLarry.Liu@Sun.COM 	fmt_print("bytes/sector	=  %d\n", cur_blksz);
20750Sstevel@tonic-gate 	fmt_print("sectors = %llu\n", cur_parts->etoc->efi_last_lba);
20760Sstevel@tonic-gate 	fmt_print("accessible sectors = %llu\n",
20779889SLarry.Liu@Sun.COM 	    cur_parts->etoc->efi_last_u_lba);
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	print_map(&tmp_pinfo);
20800Sstevel@tonic-gate 	return (0);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate /*
20840Sstevel@tonic-gate  * This routine implements the 'verify' command.  It allows the user
20850Sstevel@tonic-gate  * to read the labels on the current disk.
20860Sstevel@tonic-gate  */
20870Sstevel@tonic-gate int
c_verify()20880Sstevel@tonic-gate c_verify()
20890Sstevel@tonic-gate {
20900Sstevel@tonic-gate 	struct	dk_label p_label, b_label, *label;
20910Sstevel@tonic-gate 	struct	partition_info tmp_pinfo;
20927563SPrasad.Singamsetty@Sun.COM 	diskaddr_t	bn;
20930Sstevel@tonic-gate 	int	sec, head, i, status;
20940Sstevel@tonic-gate 	int	p_label_bad = 0;
20950Sstevel@tonic-gate 	int	b_label_bad = 0;
20960Sstevel@tonic-gate 	int	p_label_found = 0;
20970Sstevel@tonic-gate 	int	b_label_found = 0;
20980Sstevel@tonic-gate 	char	id_str[128];
20999889SLarry.Liu@Sun.COM 	char	*buf;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	/*
21020Sstevel@tonic-gate 	 * There must be a current disk type (and therefore a current disk).
21030Sstevel@tonic-gate 	 */
21040Sstevel@tonic-gate 	if (cur_dtype == NULL) {
21050Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
21060Sstevel@tonic-gate 		return (-1);
21070Sstevel@tonic-gate 	}
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * The disk must be formatted to read labels.
21100Sstevel@tonic-gate 	 */
21110Sstevel@tonic-gate 	if (!(cur_flags & DISK_FORMATTED)) {
21120Sstevel@tonic-gate 		err_print("Current Disk is unformatted.\n");
21130Sstevel@tonic-gate 		return (-1);
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 	/*
21160Sstevel@tonic-gate 	 * Check for a valid fdisk table entry for Solaris
21170Sstevel@tonic-gate 	 */
21180Sstevel@tonic-gate 	if (!good_fdisk()) {
21190Sstevel@tonic-gate 		return (-1);
21200Sstevel@tonic-gate 	}
21210Sstevel@tonic-gate 	/*
21220Sstevel@tonic-gate 	 * Branch off here if the disk is EFI labelled.
21230Sstevel@tonic-gate 	 */
21240Sstevel@tonic-gate 	if (cur_label == L_TYPE_EFI) {
21259889SLarry.Liu@Sun.COM 		return (c_verify_efi());
21260Sstevel@tonic-gate 	}
21270Sstevel@tonic-gate 	/*
21280Sstevel@tonic-gate 	 * Attempt to read the primary label.
21290Sstevel@tonic-gate 	 */
21300Sstevel@tonic-gate 	status = read_label(cur_file, &p_label);
21310Sstevel@tonic-gate 	if (status == -1) {
21320Sstevel@tonic-gate 		err_print("Warning: Could not read primary label.\n");
21330Sstevel@tonic-gate 		p_label_bad = 1;
21340Sstevel@tonic-gate 	} else {
21350Sstevel@tonic-gate 		/*
21360Sstevel@tonic-gate 		 * Verify that it is a reasonable label.
21370Sstevel@tonic-gate 		 */
21380Sstevel@tonic-gate 		/*
21390Sstevel@tonic-gate 		 * Save complete ascii string for printing later.
21400Sstevel@tonic-gate 		 */
21410Sstevel@tonic-gate 		(void) strncpy(id_str, p_label.dkl_asciilabel, 128);
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 		if ((!checklabel((struct dk_label *)&p_label)) ||
21449889SLarry.Liu@Sun.COM 		    (trim_id(p_label.dkl_asciilabel))) {
21450Sstevel@tonic-gate 			err_print("\
21460Sstevel@tonic-gate Warning: Primary label appears to be corrupt.\n");
21470Sstevel@tonic-gate 			p_label_bad = 1;
21480Sstevel@tonic-gate 		} else {
21490Sstevel@tonic-gate 			p_label_found = 1;
21500Sstevel@tonic-gate 			/*
21510Sstevel@tonic-gate 			 * Make sure it matches current label
21520Sstevel@tonic-gate 			 */
21530Sstevel@tonic-gate 			if ((!dtype_match(&p_label, cur_dtype)) ||
21549889SLarry.Liu@Sun.COM 			    (!parts_match(&p_label, cur_parts))) {
21550Sstevel@tonic-gate 				err_print("\
21560Sstevel@tonic-gate Warning: Primary label on disk appears to be different from\ncurrent label.\n");
21579889SLarry.Liu@Sun.COM 				p_label_bad = 1;
21580Sstevel@tonic-gate 			}
21590Sstevel@tonic-gate 		}
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	/*
21630Sstevel@tonic-gate 	 * Read backup labels.
21640Sstevel@tonic-gate 	 * Some disks have the backup labels in a strange place.
21650Sstevel@tonic-gate 	 */
21660Sstevel@tonic-gate 	if (cur_ctype->ctype_flags & CF_BLABEL)
21670Sstevel@tonic-gate 		head = 2;
21680Sstevel@tonic-gate 	else
21690Sstevel@tonic-gate 		head = nhead - 1;
21709889SLarry.Liu@Sun.COM 
21719889SLarry.Liu@Sun.COM 	buf = zalloc(cur_blksz);
21720Sstevel@tonic-gate 	/*
21730Sstevel@tonic-gate 	 * Loop through each copy of the backup label.
21740Sstevel@tonic-gate 	 */
21750Sstevel@tonic-gate 	for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect));
21760Sstevel@tonic-gate 	    sec += 2) {
21770Sstevel@tonic-gate 		bn = chs2bn(ncyl + acyl - 1, head, sec) + solaris_offset;
21780Sstevel@tonic-gate 		/*
21790Sstevel@tonic-gate 		 * Attempt to read it.
21800Sstevel@tonic-gate 		 */
21817563SPrasad.Singamsetty@Sun.COM 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
21829889SLarry.Liu@Sun.COM 		    1, buf, F_NORMAL, NULL))
21830Sstevel@tonic-gate 			continue;
21849889SLarry.Liu@Sun.COM 
21859889SLarry.Liu@Sun.COM 		(void *) memcpy((char *)&b_label, buf,
21869889SLarry.Liu@Sun.COM 		    sizeof (struct dk_label));
21879889SLarry.Liu@Sun.COM 
21880Sstevel@tonic-gate 		/*
21890Sstevel@tonic-gate 		 * Verify that it is a reasonable label.
21900Sstevel@tonic-gate 		 */
21910Sstevel@tonic-gate 		if (!checklabel(&b_label))
21920Sstevel@tonic-gate 			continue;
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 		/*
21950Sstevel@tonic-gate 		 * Save complete label only if no primary label exists
21960Sstevel@tonic-gate 		 */
21970Sstevel@tonic-gate 		if (!p_label_found)
21980Sstevel@tonic-gate 			(void) strncpy(id_str, b_label.dkl_asciilabel, 128);
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 		if (trim_id(b_label.dkl_asciilabel))
22010Sstevel@tonic-gate 			continue;
22020Sstevel@tonic-gate 		b_label_found = 1;
22030Sstevel@tonic-gate 		/*
22040Sstevel@tonic-gate 		 * Compare against primary label
22050Sstevel@tonic-gate 		 */
22060Sstevel@tonic-gate 		if (p_label_found) {
22070Sstevel@tonic-gate 			if ((strcmp(b_label.dkl_asciilabel,
22089889SLarry.Liu@Sun.COM 			    p_label.dkl_asciilabel) != 0) ||
22099889SLarry.Liu@Sun.COM 			    (b_label.dkl_ncyl != p_label.dkl_ncyl) ||
22109889SLarry.Liu@Sun.COM 			    (b_label.dkl_acyl != p_label.dkl_acyl) ||
22119889SLarry.Liu@Sun.COM 			    (b_label.dkl_nhead != p_label.dkl_nhead) ||
22129889SLarry.Liu@Sun.COM 			    (b_label.dkl_nsect != p_label.dkl_nsect)) {
22130Sstevel@tonic-gate 				b_label_bad = 1;
22140Sstevel@tonic-gate 			} else {
22150Sstevel@tonic-gate 				for (i = 0; i < NDKMAP; i++) {
22160Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
22170Sstevel@tonic-gate 					if ((b_label.dkl_map[i].dkl_cylno !=
22180Sstevel@tonic-gate 					    p_label.dkl_map[i].dkl_cylno) ||
22190Sstevel@tonic-gate 					    (b_label.dkl_map[i].dkl_nblk !=
22200Sstevel@tonic-gate 					    p_label.dkl_map[i].dkl_nblk)) {
22210Sstevel@tonic-gate 						b_label_bad = 1;
22220Sstevel@tonic-gate 						break;
22230Sstevel@tonic-gate 					}
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_16)
22260Sstevel@tonic-gate 					if ((b_label.dkl_vtoc.v_part[i].p_tag !=
22279889SLarry.Liu@Sun.COM 					    p_label.dkl_vtoc.v_part[i].p_tag) ||
22289889SLarry.Liu@Sun.COM 					    (b_label.dkl_vtoc.v_part[i].p_flag
22299889SLarry.Liu@Sun.COM 					    != p_label.dkl_vtoc.v_part[i].
22309889SLarry.Liu@Sun.COM 					    p_flag) ||
22319889SLarry.Liu@Sun.COM 					    (b_label.dkl_vtoc.v_part[i].p_start
22329889SLarry.Liu@Sun.COM 					    != p_label.dkl_vtoc.v_part[i].
22339889SLarry.Liu@Sun.COM 					    p_start) ||
22349889SLarry.Liu@Sun.COM 					    (b_label.dkl_vtoc.v_part[i].p_size
22359889SLarry.Liu@Sun.COM 					    != p_label.dkl_vtoc.v_part[i].
22369889SLarry.Liu@Sun.COM 					    p_size)) {
22370Sstevel@tonic-gate 						b_label_bad = 1;
22380Sstevel@tonic-gate 						break;
22390Sstevel@tonic-gate 					}
22400Sstevel@tonic-gate #else
22410Sstevel@tonic-gate #error No VTOC layout defined.
22420Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_8) */
22430Sstevel@tonic-gate 				}
22440Sstevel@tonic-gate 			}
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 		if (b_label_bad)
22470Sstevel@tonic-gate 			err_print(
22480Sstevel@tonic-gate "Warning: Primary and backup labels do not match.\n");
22490Sstevel@tonic-gate 		break;
22500Sstevel@tonic-gate 	}
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * If we didn't find any backup labels, say so.
22530Sstevel@tonic-gate 	 */
22540Sstevel@tonic-gate 	if (!b_label_found)
22550Sstevel@tonic-gate 		err_print("Warning: Could not read backup labels.\n");
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if ((!b_label_found) || (p_label_bad) || (b_label_bad))
22580Sstevel@tonic-gate 		err_print("\n\
22590Sstevel@tonic-gate Warning: Check the current partitioning and 'label' the disk or use the\n\
22600Sstevel@tonic-gate \t 'backup' command.\n");
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	/*
22630Sstevel@tonic-gate 	 * Print label information.
22640Sstevel@tonic-gate 	 */
22650Sstevel@tonic-gate 	if (p_label_found) {
22660Sstevel@tonic-gate 		fmt_print("\nPrimary label contents:\n");
22670Sstevel@tonic-gate 		label = &p_label;
22680Sstevel@tonic-gate 	} else if (b_label_found) {
22690Sstevel@tonic-gate 		fmt_print("\nBackup label contents:\n");
22700Sstevel@tonic-gate 		label = &b_label;
22710Sstevel@tonic-gate 	} else {
22729889SLarry.Liu@Sun.COM 		free(buf);
22730Sstevel@tonic-gate 		return (0);
22740Sstevel@tonic-gate 	}
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	/*
22770Sstevel@tonic-gate 	 * Must put info into partition_info struct for
22780Sstevel@tonic-gate 	 * for print routine.
22790Sstevel@tonic-gate 	 */
22800Sstevel@tonic-gate 	bzero(&tmp_pinfo, sizeof (struct partition_info));
22810Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
22840Sstevel@tonic-gate 		tmp_pinfo.pinfo_map[i] = label->dkl_map[i];
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_16)
22870Sstevel@tonic-gate 		tmp_pinfo.pinfo_map[i].dkl_cylno =
22889889SLarry.Liu@Sun.COM 		    label->dkl_vtoc.v_part[i].p_start / spc();
22890Sstevel@tonic-gate 		tmp_pinfo.pinfo_map[i].dkl_nblk =
22909889SLarry.Liu@Sun.COM 		    label->dkl_vtoc.v_part[i].p_size;
22910Sstevel@tonic-gate #else
22920Sstevel@tonic-gate #error No VTOC layout defined.
22930Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_8) */
22940Sstevel@tonic-gate 	}
22950Sstevel@tonic-gate 	tmp_pinfo.vtoc = label->dkl_vtoc;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	fmt_print("\n");
22980Sstevel@tonic-gate 	fmt_print("Volume name = <%8s>\n", label->dkl_vtoc.v_volume);
22990Sstevel@tonic-gate 	fmt_print("ascii name  = <%s>\n", id_str);
23000Sstevel@tonic-gate 	fmt_print("pcyl        = %4d\n", label->dkl_pcyl);
23010Sstevel@tonic-gate 	fmt_print("ncyl        = %4d\n", label->dkl_ncyl);
23020Sstevel@tonic-gate 	fmt_print("acyl        = %4d\n", label->dkl_acyl);
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
23050Sstevel@tonic-gate 	fmt_print("bcyl        = %4d\n", label->dkl_bcyl);
23060Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	fmt_print("nhead       = %4d\n", label->dkl_nhead);
23090Sstevel@tonic-gate 	fmt_print("nsect       = %4d\n", label->dkl_nsect);
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	print_map(&tmp_pinfo);
23129889SLarry.Liu@Sun.COM 	free(buf);
23130Sstevel@tonic-gate 	return (0);
23140Sstevel@tonic-gate }
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate /*
23180Sstevel@tonic-gate  * This command implements the inquiry command, for embedded SCSI
23190Sstevel@tonic-gate  * disks only, which issues a SCSI inquiry command, and
23200Sstevel@tonic-gate  * displays the resulting vendor, product id and revision level.
23210Sstevel@tonic-gate  */
23220Sstevel@tonic-gate int
c_inquiry()23230Sstevel@tonic-gate c_inquiry()
23240Sstevel@tonic-gate {
23250Sstevel@tonic-gate 	char			inqbuf[255];
23260Sstevel@tonic-gate 	struct scsi_inquiry	*inq;
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	assert(SCSI);
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	inq = (struct scsi_inquiry *)inqbuf;
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	if (uscsi_inquiry(cur_file, inqbuf, sizeof (inqbuf))) {
23330Sstevel@tonic-gate 		err_print("Failed\n");
23340Sstevel@tonic-gate 		return (-1);
23350Sstevel@tonic-gate 	} else {
23360Sstevel@tonic-gate 		fmt_print("Vendor:   ");
23370Sstevel@tonic-gate 		print_buf(inq->inq_vid, sizeof (inq->inq_vid));
23380Sstevel@tonic-gate 		fmt_print("\nProduct:  ");
23390Sstevel@tonic-gate 		print_buf(inq->inq_pid, sizeof (inq->inq_pid));
23400Sstevel@tonic-gate 		fmt_print("\nRevision: ");
23410Sstevel@tonic-gate 		print_buf(inq->inq_revision, sizeof (inq->inq_revision));
23420Sstevel@tonic-gate 		fmt_print("\n");
23430Sstevel@tonic-gate 	}
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	return (0);
23460Sstevel@tonic-gate }
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate /*
23500Sstevel@tonic-gate  * This routine allows the user to set the 8-character
23510Sstevel@tonic-gate  * volume name in the vtoc.  It then writes both the
23520Sstevel@tonic-gate  * primary and backup labels onto the current disk.
23530Sstevel@tonic-gate  */
23540Sstevel@tonic-gate int
c_volname()23550Sstevel@tonic-gate c_volname()
23560Sstevel@tonic-gate {
23570Sstevel@tonic-gate 	int	 status;
23580Sstevel@tonic-gate 	char	*prompt;
23590Sstevel@tonic-gate 	union {
23600Sstevel@tonic-gate 		int	xfoo;
23610Sstevel@tonic-gate 		char	defvolname[LEN_DKL_VVOL+1];
23620Sstevel@tonic-gate 	} x;
23630Sstevel@tonic-gate 	char    s1[MAXPATHLEN], nclean[MAXPATHLEN];
23640Sstevel@tonic-gate 	char	*volname;
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * There must be a current disk type (and therefore a current disk).
23690Sstevel@tonic-gate 	 */
23700Sstevel@tonic-gate 	if (cur_dtype == NULL) {
23710Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
23720Sstevel@tonic-gate 		return (-1);
23730Sstevel@tonic-gate 	}
23740Sstevel@tonic-gate 	/*
23750Sstevel@tonic-gate 	 * The current disk must be formatted to label it.
23760Sstevel@tonic-gate 	 */
23770Sstevel@tonic-gate 	if (!(cur_flags & DISK_FORMATTED)) {
23780Sstevel@tonic-gate 		err_print("Current Disk is unformatted.\n");
23790Sstevel@tonic-gate 		return (-1);
23800Sstevel@tonic-gate 	}
23810Sstevel@tonic-gate 	/*
23820Sstevel@tonic-gate 	 * Check for a valid fdisk table entry for Solaris
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	if (!good_fdisk()) {
23850Sstevel@tonic-gate 		return (-1);
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 	/*
23880Sstevel@tonic-gate 	 * The current disk must be formatted to label it.
23890Sstevel@tonic-gate 	 */
23900Sstevel@tonic-gate 	if (cur_parts == NULL) {
23910Sstevel@tonic-gate 	err_print(
23920Sstevel@tonic-gate "Please select a partition map for the disk first.\n");
23930Sstevel@tonic-gate 	return (-1);
23940Sstevel@tonic-gate 	}
23955421Smishra 
23960Sstevel@tonic-gate 	/*
23970Sstevel@tonic-gate 	 * Check to see if there are any mounted file systems anywhere
23980Sstevel@tonic-gate 	 * on the current disk.  If so, refuse to label the disk, but
23990Sstevel@tonic-gate 	 * only if the partitions would change for the mounted partitions.
24000Sstevel@tonic-gate 	 *
24010Sstevel@tonic-gate 	 */
24027563SPrasad.Singamsetty@Sun.COM 	if (checkmount((diskaddr_t)-1, (diskaddr_t)-1)) {
24030Sstevel@tonic-gate 		/* Bleagh, too descriptive */
24040Sstevel@tonic-gate 		if (check_label_with_mount()) {
24050Sstevel@tonic-gate 			err_print(
24060Sstevel@tonic-gate "Cannot label disk while it has mounted partitions.\n\n");
24070Sstevel@tonic-gate 			return (-1);
24080Sstevel@tonic-gate 		}
24090Sstevel@tonic-gate 	}
24105421Smishra 
24110Sstevel@tonic-gate 	/*
24120Sstevel@tonic-gate 	 * Check to see if there are partitions being used for swapping
24130Sstevel@tonic-gate 	 * on the current disk.  If so, refuse to label the disk, but
24140Sstevel@tonic-gate 	 * only if the partitions would change for the swap partitions.
24150Sstevel@tonic-gate 	 *
24160Sstevel@tonic-gate 	 */
24177563SPrasad.Singamsetty@Sun.COM 	if (checkswap((diskaddr_t)-1, (diskaddr_t)-1)) {
24180Sstevel@tonic-gate 		/* Bleagh, too descriptive */
24190Sstevel@tonic-gate 		if (check_label_with_swap()) {
24200Sstevel@tonic-gate 			err_print(
24210Sstevel@tonic-gate "Cannot label disk while its partitions are currently \
24220Sstevel@tonic-gate being used for swapping.\n\n");
24230Sstevel@tonic-gate 			return (-1);
24240Sstevel@tonic-gate 		}
24250Sstevel@tonic-gate 	}
24265421Smishra 
24275421Smishra 	/*
24285421Smishra 	 * Check to see if any partitions used for svm, vxvm, ZFS zpool
24295421Smishra 	 * or live upgrade are on the disk. If so, refuse to label the
24305421Smishra 	 * disk, but only if we are trying to shrink a partition in
24315421Smishra 	 * use.
24325421Smishra 	 */
24335421Smishra 	if (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
24345421Smishra 	    (diskaddr_t)-1, 0, 1)) {
24355421Smishra 		err_print("Cannot label disk while its partitions "
24365421Smishra 		    "are in use as described.\n");
24375421Smishra 		return (-1);
24385421Smishra 	}
24395421Smishra 
24400Sstevel@tonic-gate 	/*
24410Sstevel@tonic-gate 	 * Prompt for the disk volume name.
24420Sstevel@tonic-gate 	 */
24430Sstevel@tonic-gate 	prompt = "Enter 8-character volume name (remember quotes)";
24440Sstevel@tonic-gate 	bzero(x.defvolname, LEN_DKL_VVOL+1);
24450Sstevel@tonic-gate 	bcopy(cur_disk->v_volume, x.defvolname, LEN_DKL_VVOL);
24460Sstevel@tonic-gate 	/*
24470Sstevel@tonic-gate 	 *  Get the input using "get_inputline" since
24480Sstevel@tonic-gate 	 *  input would never return null string.
24490Sstevel@tonic-gate 	 */
24500Sstevel@tonic-gate 	fmt_print("%s[\"%s\"]:", prompt, x.defvolname);
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	/*
24530Sstevel@tonic-gate 	 * Get input from the user.
24540Sstevel@tonic-gate 	 */
24550Sstevel@tonic-gate 	get_inputline(nclean, MAXPATHLEN);
24560Sstevel@tonic-gate 	clean_token(s1, nclean);
24570Sstevel@tonic-gate 	/*
24580Sstevel@tonic-gate 	 * check for return.
24590Sstevel@tonic-gate 	 */
24600Sstevel@tonic-gate 	if (s1[0] == 0) {
24610Sstevel@tonic-gate 		volname = x.defvolname;
24620Sstevel@tonic-gate 	} else {
24630Sstevel@tonic-gate 		/*
24640Sstevel@tonic-gate 		 * remove the " mark from volname.
24650Sstevel@tonic-gate 		 */
24660Sstevel@tonic-gate 		if (s1[0] == '"') {
24670Sstevel@tonic-gate 			int i = 1;
24680Sstevel@tonic-gate 			volname = &s1[1];
24690Sstevel@tonic-gate 			while (s1[i] != '"' && s1[i] != '\0')
24700Sstevel@tonic-gate 				i++;
24710Sstevel@tonic-gate 			s1[i] = '\0';
24720Sstevel@tonic-gate 			clean_token(nclean, volname);
24730Sstevel@tonic-gate 			volname = nclean;
24740Sstevel@tonic-gate 		} else {
24750Sstevel@tonic-gate 			(void) sscanf(&s1[0], "%1024s", nclean);
24760Sstevel@tonic-gate 			volname = nclean;
24770Sstevel@tonic-gate 		};
24780Sstevel@tonic-gate 	}
24790Sstevel@tonic-gate 	/*
24800Sstevel@tonic-gate 	 * Make sure the user is serious.
24810Sstevel@tonic-gate 	 */
24820Sstevel@tonic-gate 	if (check("Ready to label disk, continue")) {
24830Sstevel@tonic-gate 		fmt_print("\n");
24840Sstevel@tonic-gate 		return (-1);
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 	/*
24870Sstevel@tonic-gate 	 * Use the volume name chosen above
24880Sstevel@tonic-gate 	 */
24890Sstevel@tonic-gate 	bzero(cur_disk->v_volume, LEN_DKL_VVOL);
24900Sstevel@tonic-gate 	bcopy(volname, cur_disk->v_volume, min((int)strlen(volname),
24910Sstevel@tonic-gate 	    LEN_DKL_VVOL));
24920Sstevel@tonic-gate 	if (cur_label == L_TYPE_EFI) {
24939889SLarry.Liu@Sun.COM 		bzero(cur_parts->etoc->efi_parts[8].p_name, LEN_DKL_VVOL);
24949889SLarry.Liu@Sun.COM 		bcopy(volname, cur_parts->etoc->efi_parts[8].p_name,
24959889SLarry.Liu@Sun.COM 		    LEN_DKL_VVOL);
24960Sstevel@tonic-gate 	}
24970Sstevel@tonic-gate 	/*
24980Sstevel@tonic-gate 	 * Write the labels out (this will also notify unix) and
24990Sstevel@tonic-gate 	 * return status.
25000Sstevel@tonic-gate 	 */
25010Sstevel@tonic-gate 	fmt_print("\n");
25020Sstevel@tonic-gate 	if (status = write_label())
25030Sstevel@tonic-gate 		err_print("Label failed.\n");
25040Sstevel@tonic-gate 	return (status);
25050Sstevel@tonic-gate }
2506