xref: /titanic_41/usr/src/cmd/rmformat/rmf_menu.c (revision afb89a98de480696a03f176e1a95a24c69913d06)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5e3397557Szk194757  * Common Development and Distribution License (the "License").
6e3397557Szk194757  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*afb89a98SPavel Potoplyak  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * rmf_menu.c :
287c478bd9Sstevel@tonic-gate  *	Command line options to rmformat are processed in this file.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "rmformat.h"
327c478bd9Sstevel@tonic-gate #include <sys/smedia.h>
33bbed02dfSzk194757 #include <priv_utils.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate extern int32_t D_flag;
367c478bd9Sstevel@tonic-gate extern int32_t e_flag;
377c478bd9Sstevel@tonic-gate extern int32_t H_flag;
387c478bd9Sstevel@tonic-gate extern int32_t U_flag;
397c478bd9Sstevel@tonic-gate extern int32_t V_flag;
407c478bd9Sstevel@tonic-gate extern int32_t b_flag;
417c478bd9Sstevel@tonic-gate extern int32_t w_flag;
427c478bd9Sstevel@tonic-gate extern int32_t W_flag;
437c478bd9Sstevel@tonic-gate extern int32_t s_flag;
447c478bd9Sstevel@tonic-gate extern int32_t c_flag;
457c478bd9Sstevel@tonic-gate extern int32_t F_flag;
467c478bd9Sstevel@tonic-gate extern int32_t R_flag;
477c478bd9Sstevel@tonic-gate extern int32_t p_flag;
487c478bd9Sstevel@tonic-gate extern int32_t l_flag;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern char *myname;
517c478bd9Sstevel@tonic-gate extern char *slice_file;
52342440ecSPrasad Singamsetty extern diskaddr_t repair_blk_no;
537c478bd9Sstevel@tonic-gate extern int32_t quick_format;
547c478bd9Sstevel@tonic-gate extern int32_t long_format;
557c478bd9Sstevel@tonic-gate extern int32_t force_format;
567c478bd9Sstevel@tonic-gate extern int32_t rw_protect_enable;
577c478bd9Sstevel@tonic-gate extern int32_t rw_protect_disable;
587c478bd9Sstevel@tonic-gate extern int32_t wp_enable_passwd;
597c478bd9Sstevel@tonic-gate extern int32_t wp_disable_passwd;
607c478bd9Sstevel@tonic-gate extern int32_t wp_enable;
617c478bd9Sstevel@tonic-gate extern int32_t wp_disable;
627c478bd9Sstevel@tonic-gate extern int32_t verify_write;
637c478bd9Sstevel@tonic-gate extern char *dev_name;
647c478bd9Sstevel@tonic-gate extern char *label;
657c478bd9Sstevel@tonic-gate extern int total_devices_found;
667c478bd9Sstevel@tonic-gate extern int removable_found;
677c478bd9Sstevel@tonic-gate char *global_intr_msg;
687c478bd9Sstevel@tonic-gate smmedium_prop_t med_info;
697c478bd9Sstevel@tonic-gate int vol_running;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate extern void check_invalid_combinations();
727c478bd9Sstevel@tonic-gate extern void check_invalid_combinations_again(int32_t);
737c478bd9Sstevel@tonic-gate extern void process_options();
747c478bd9Sstevel@tonic-gate extern void get_passwd(struct smwp_state *wp, int32_t confirm);
757c478bd9Sstevel@tonic-gate extern int32_t valid_slice_file(smedia_handle_t, int32_t, char *,
76342440ecSPrasad Singamsetty 	struct extvtoc *);
777c478bd9Sstevel@tonic-gate extern void trap_SIGINT();
787c478bd9Sstevel@tonic-gate extern void release_SIGINT();
797c478bd9Sstevel@tonic-gate extern int32_t verify(smedia_handle_t handle, int32_t fd,
80342440ecSPrasad Singamsetty 	diskaddr_t start_sector, uint32_t nblocks,
817c478bd9Sstevel@tonic-gate 	char *buf, int32_t flag, int32_t blocksize, int32_t no_raw_rw);
827c478bd9Sstevel@tonic-gate extern void my_perror(char *err_string);
837c478bd9Sstevel@tonic-gate extern void write_default_label(smedia_handle_t, int32_t fd);
847c478bd9Sstevel@tonic-gate extern int find_device(int defer, char *tmpstr);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate void overwrite_metadata(int32_t fd, smedia_handle_t handle);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int32_t write_sunos_label(int32_t fd, int32_t media_type);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int32_t my_open(char *device_name, int32_t flags);
917c478bd9Sstevel@tonic-gate int32_t check_and_unmount_vold(char *device_name, int32_t flag);
927c478bd9Sstevel@tonic-gate int32_t check_and_unmount_scsi(char *device_name, int32_t flag);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int32_t check_and_unmount_floppy(int32_t fd, int32_t flag);
957c478bd9Sstevel@tonic-gate int32_t get_confirmation(void);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static void	process_F_flag(smedia_handle_t handle, int32_t fd);
997c478bd9Sstevel@tonic-gate static void	process_w_flag(smedia_handle_t handle);
1007c478bd9Sstevel@tonic-gate static void	process_W_flag(smedia_handle_t handle);
1017c478bd9Sstevel@tonic-gate static void	process_R_flag(smedia_handle_t handle);
1027c478bd9Sstevel@tonic-gate void		process_p_flag(smedia_handle_t handle, int32_t fd);
1037c478bd9Sstevel@tonic-gate static void	process_c_flag(smedia_handle_t handle);
1047c478bd9Sstevel@tonic-gate static void	process_V_flag(smedia_handle_t handle, int32_t fd);
1057c478bd9Sstevel@tonic-gate static void	process_s_flag(smedia_handle_t, int32_t fd);
1067c478bd9Sstevel@tonic-gate static void	process_e_flag(smedia_handle_t handle);
1077c478bd9Sstevel@tonic-gate static void	process_H_flag(smedia_handle_t handle, int32_t fd);
1087c478bd9Sstevel@tonic-gate static void	process_D_flag(smedia_handle_t handle, int32_t fd);
1097c478bd9Sstevel@tonic-gate static void	process_b_flag(int32_t fd);
1107c478bd9Sstevel@tonic-gate static void	process_l_flag(void);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate void
process_options()1137c478bd9Sstevel@tonic-gate process_options()
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	int32_t fd;
1167c478bd9Sstevel@tonic-gate 	smedia_handle_t handle;
1177c478bd9Sstevel@tonic-gate 	int32_t m_scsi_umount = 0;
1187c478bd9Sstevel@tonic-gate 	int32_t m_flp_umount = 0;
1197c478bd9Sstevel@tonic-gate 	int32_t v_device_umount = 0;
1207c478bd9Sstevel@tonic-gate 	int32_t umount_required = 0;
1217c478bd9Sstevel@tonic-gate 	int32_t removable;
1227c478bd9Sstevel@tonic-gate 	int32_t umount_failed = 0;
1237c478bd9Sstevel@tonic-gate 	struct dk_minfo media;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	check_invalid_combinations();
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (l_flag && !dev_name) {
1287c478bd9Sstevel@tonic-gate 		process_l_flag();
1297c478bd9Sstevel@tonic-gate 		return;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (U_flag) {
1337c478bd9Sstevel@tonic-gate 		if (!(F_flag || H_flag || D_flag)) {
1347c478bd9Sstevel@tonic-gate 			F_flag = 1;
1357c478bd9Sstevel@tonic-gate 			long_format = 1;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (F_flag || w_flag || W_flag || R_flag || D_flag || H_flag ||
1407c478bd9Sstevel@tonic-gate 	    V_flag || c_flag || b_flag || s_flag || e_flag) {
1417c478bd9Sstevel@tonic-gate 		umount_required = 1;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	fd = my_open(dev_name, O_RDONLY|O_NDELAY);
1457c478bd9Sstevel@tonic-gate 	if (fd < 0)  {
1467c478bd9Sstevel@tonic-gate 		PERROR("Could not open device");
1477c478bd9Sstevel@tonic-gate 		(void) close(fd);
1487c478bd9Sstevel@tonic-gate 		exit(1);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCREMOVABLE, &removable) < 0) {
1527c478bd9Sstevel@tonic-gate 		PERROR("DKIOCREMOVABLE ioctl failed");
1537c478bd9Sstevel@tonic-gate 		(void) close(fd);
1547c478bd9Sstevel@tonic-gate 		exit(1);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	if (!removable) {
1577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1587c478bd9Sstevel@tonic-gate 		    gettext("Not a removable media device\n"));
1597c478bd9Sstevel@tonic-gate 		(void) close(fd);
1607c478bd9Sstevel@tonic-gate 		exit(1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGMEDIAINFO, &media) < 0) {
1647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1657c478bd9Sstevel@tonic-gate 		    gettext("No media in specified device\n"));
1667c478bd9Sstevel@tonic-gate 		(void) close(fd);
1677c478bd9Sstevel@tonic-gate 		exit(1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Check if volume manager has mounted this */
1717c478bd9Sstevel@tonic-gate 	if (umount_required) {
1727c478bd9Sstevel@tonic-gate 		v_device_umount = check_and_unmount_vold(dev_name, U_flag);
1737c478bd9Sstevel@tonic-gate 		if (v_device_umount != 1) {
1747c478bd9Sstevel@tonic-gate 			m_scsi_umount = check_and_unmount_scsi(dev_name,
1757c478bd9Sstevel@tonic-gate 			    U_flag);
1767c478bd9Sstevel@tonic-gate 			if (m_scsi_umount != 1) {
1777c478bd9Sstevel@tonic-gate 				m_flp_umount = check_and_unmount_floppy(fd,
1787c478bd9Sstevel@tonic-gate 				    U_flag);
1797c478bd9Sstevel@tonic-gate 				if (m_flp_umount != 1) {
1807c478bd9Sstevel@tonic-gate 					umount_failed = 1;
1817c478bd9Sstevel@tonic-gate 				}
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (umount_required && U_flag && umount_failed) {
1877c478bd9Sstevel@tonic-gate 		if (v_device_umount || m_scsi_umount || m_flp_umount) {
1887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1897c478bd9Sstevel@tonic-gate 			    gettext("Could not unmount device.\n"));
1907c478bd9Sstevel@tonic-gate 			(void) close(fd);
1917c478bd9Sstevel@tonic-gate 			exit(1);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (umount_required && !U_flag) {
1967c478bd9Sstevel@tonic-gate 		if (v_device_umount || m_scsi_umount || m_flp_umount) {
1977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Device mounted.\n"));
1987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1997c478bd9Sstevel@tonic-gate 			    gettext("Requested operation can not be \
2007c478bd9Sstevel@tonic-gate performed on a mounted device.\n"));
2017c478bd9Sstevel@tonic-gate 			(void) close(fd);
2027c478bd9Sstevel@tonic-gate 			exit(1);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	/* register the fd with the libsmedia */
2067c478bd9Sstevel@tonic-gate 	handle = smedia_get_handle(fd);
2077c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2097c478bd9Sstevel@tonic-gate 		    gettext("Failed to get libsmedia handle.\n"));
2107c478bd9Sstevel@tonic-gate 		(void) close(fd);
2117c478bd9Sstevel@tonic-gate 		exit(1);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (smedia_get_medium_property(handle, &med_info) < 0) {
2157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2167c478bd9Sstevel@tonic-gate 		    gettext("Get medium property failed \n"));
2177c478bd9Sstevel@tonic-gate 		(void) smedia_release_handle(handle);
2187c478bd9Sstevel@tonic-gate 		(void) close(fd);
2197c478bd9Sstevel@tonic-gate 		exit(1);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	DPRINTF1("media type %x\n", med_info.sm_media_type);
2237c478bd9Sstevel@tonic-gate 	DPRINTF1("media block size %x\n", med_info.sm_blocksize);
224342440ecSPrasad Singamsetty 	DPRINTF1("media capacity %u\n", (uint32_t)med_info.sm_capacity);
2257c478bd9Sstevel@tonic-gate 	DPRINTF3("media cyl %d head %d sect %d\n",
2267c478bd9Sstevel@tonic-gate 	    med_info.sm_pcyl, med_info.sm_nhead, med_info.sm_nsect);
2277c478bd9Sstevel@tonic-gate 	check_invalid_combinations_again(med_info.sm_media_type);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Special handling for pcmcia, sometimes open the file in
2317c478bd9Sstevel@tonic-gate 	 * read-write mode.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (med_info.sm_media_type == SM_PCMCIA_MEM) {
2357c478bd9Sstevel@tonic-gate 		if (F_flag || H_flag || D_flag || (V_flag && verify_write)) {
2367c478bd9Sstevel@tonic-gate 			(void) close(fd);
2377c478bd9Sstevel@tonic-gate 			DPRINTF("Reopening device\n");
2387c478bd9Sstevel@tonic-gate 			fd = my_open(dev_name, O_RDWR|O_NDELAY);
2397c478bd9Sstevel@tonic-gate 			if (fd < 0)  {
2407c478bd9Sstevel@tonic-gate 				PERROR("Could not open device");
2417c478bd9Sstevel@tonic-gate 				(void) smedia_release_handle(handle);
2427c478bd9Sstevel@tonic-gate 				(void) close(fd);
2437c478bd9Sstevel@tonic-gate 				exit(1);
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (med_info.sm_media_type == SM_PCMCIA_ATA) {
2497c478bd9Sstevel@tonic-gate 		if (V_flag || c_flag) {
2507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2517c478bd9Sstevel@tonic-gate 			    gettext("Option not supported on PC ATA cards\n"));
2527c478bd9Sstevel@tonic-gate 			(void) smedia_release_handle(handle);
2537c478bd9Sstevel@tonic-gate 			(void) close(fd);
2547c478bd9Sstevel@tonic-gate 			exit(1);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 		if (F_flag) {
2577c478bd9Sstevel@tonic-gate 			/* same text as used by the format command */
2587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2597c478bd9Sstevel@tonic-gate 			    gettext("Cannot format this drive. Please use your \
2607c478bd9Sstevel@tonic-gate Manufacturer supplied formatting utility.\n"));
2617c478bd9Sstevel@tonic-gate 			(void) smedia_release_handle(handle);
2627c478bd9Sstevel@tonic-gate 			(void) close(fd);
2637c478bd9Sstevel@tonic-gate 			exit(1);
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (F_flag)
2687c478bd9Sstevel@tonic-gate 		process_F_flag(handle, fd);
2697c478bd9Sstevel@tonic-gate 	if (w_flag)
2707c478bd9Sstevel@tonic-gate 		process_w_flag(handle);
2717c478bd9Sstevel@tonic-gate 	if (W_flag)
2727c478bd9Sstevel@tonic-gate 		process_W_flag(handle);
2737c478bd9Sstevel@tonic-gate 	if (R_flag)
2747c478bd9Sstevel@tonic-gate 		process_R_flag(handle);
2757c478bd9Sstevel@tonic-gate 	if (p_flag)
2767c478bd9Sstevel@tonic-gate 		process_p_flag(handle, fd);
2777c478bd9Sstevel@tonic-gate 	if (D_flag)
2787c478bd9Sstevel@tonic-gate 		process_D_flag(handle, fd);
2797c478bd9Sstevel@tonic-gate 	if (H_flag)
2807c478bd9Sstevel@tonic-gate 		process_H_flag(handle, fd);
2817c478bd9Sstevel@tonic-gate 	if (V_flag)
2827c478bd9Sstevel@tonic-gate 		process_V_flag(handle, fd);
2837c478bd9Sstevel@tonic-gate 	if (c_flag)
2847c478bd9Sstevel@tonic-gate 		process_c_flag(handle);
2857c478bd9Sstevel@tonic-gate 	if (b_flag)
2867c478bd9Sstevel@tonic-gate 		process_b_flag(fd);
2877c478bd9Sstevel@tonic-gate 	if (s_flag)
2887c478bd9Sstevel@tonic-gate 		process_s_flag(handle, fd);
2897c478bd9Sstevel@tonic-gate 	if (e_flag)
2907c478bd9Sstevel@tonic-gate 		process_e_flag(handle);
2917c478bd9Sstevel@tonic-gate 	if (l_flag) {
2927c478bd9Sstevel@tonic-gate 		process_l_flag();
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	(void) smedia_release_handle(handle);
2967c478bd9Sstevel@tonic-gate 	(void) close(fd);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * This routine handles the F_flag.
3017c478bd9Sstevel@tonic-gate  * This options should not be used for floppy. However,
3027c478bd9Sstevel@tonic-gate  * if this option is used for floppy, the option will
3037c478bd9Sstevel@tonic-gate  * be forced to SM_FORMAT_HD and smedia_format is called.
3047c478bd9Sstevel@tonic-gate  * Note that smedia_format is a blocked mode format and it
3057c478bd9Sstevel@tonic-gate  * returns only after the complete formatting is over.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static void
process_F_flag(smedia_handle_t handle,int32_t fd)3097c478bd9Sstevel@tonic-gate process_F_flag(smedia_handle_t handle, int32_t fd)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	uint32_t format_flag;
3127c478bd9Sstevel@tonic-gate 	int32_t old_per = 0;
3137c478bd9Sstevel@tonic-gate 	int32_t new_per, ret_val;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (force_format) {
3167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3177c478bd9Sstevel@tonic-gate 		    gettext("Formatting disk.\n"));
3187c478bd9Sstevel@tonic-gate 	} else {
3197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3207c478bd9Sstevel@tonic-gate 		    gettext("Formatting will erase all the data on disk.\n"));
3217c478bd9Sstevel@tonic-gate 		if (!get_confirmation())
3227c478bd9Sstevel@tonic-gate 			return;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (quick_format)
3267c478bd9Sstevel@tonic-gate 		format_flag = SM_FORMAT_QUICK;
3277c478bd9Sstevel@tonic-gate 	else if (long_format)
3287c478bd9Sstevel@tonic-gate 		format_flag = SM_FORMAT_LONG;
3297c478bd9Sstevel@tonic-gate 	else if (force_format)
3307c478bd9Sstevel@tonic-gate 		format_flag = SM_FORMAT_FORCE;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (med_info.sm_media_type == SM_FLOPPY)
3337c478bd9Sstevel@tonic-gate 		format_flag = SM_FORMAT_HD;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if ((med_info.sm_media_type != SM_FLOPPY) &&
3367c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type != SM_PCMCIA_MEM) &&
3377c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type != SM_SCSI_FLOPPY)) {
3387c478bd9Sstevel@tonic-gate 		global_intr_msg = "Interrupting format may render the \
3397c478bd9Sstevel@tonic-gate medium useless";
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 		global_intr_msg = "";
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 		trap_SIGINT();
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (smedia_format(handle, format_flag, SM_FORMAT_IMMEDIATE) != 0) {
3467c478bd9Sstevel@tonic-gate 		if (errno == EINVAL) {
3477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Format failed.\n"));
3487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("The medium may not \
3497c478bd9Sstevel@tonic-gate be compatible for format operation.\n"));
3507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("read/write surface \
3517c478bd9Sstevel@tonic-gate scan may be used to get the effect of formatting.\n"));
3527c478bd9Sstevel@tonic-gate 		} else {
3537c478bd9Sstevel@tonic-gate 			PERROR("Format failed");
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 		(void) smedia_release_handle(handle);
3567c478bd9Sstevel@tonic-gate 		(void) close(fd);
3577c478bd9Sstevel@tonic-gate 		exit(1);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
3617c478bd9Sstevel@tonic-gate 	while (1) {
3627c478bd9Sstevel@tonic-gate 		ret_val = smedia_check_format_status(handle);
3637c478bd9Sstevel@tonic-gate 		if (ret_val == -1) {
3647c478bd9Sstevel@tonic-gate 			if (errno != ENOTSUP) {
3657c478bd9Sstevel@tonic-gate 				PERROR("Format failed");
3667c478bd9Sstevel@tonic-gate 				(void) smedia_release_handle(handle);
3677c478bd9Sstevel@tonic-gate 				(void) close(fd);
3687c478bd9Sstevel@tonic-gate 				exit(1);
3697c478bd9Sstevel@tonic-gate 			} else {
3707c478bd9Sstevel@tonic-gate 				/* Background formatting is not supported */
3717c478bd9Sstevel@tonic-gate 				break;
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		if (ret_val == 100) {
3757c478bd9Sstevel@tonic-gate 			(void) printf("\n");
3767c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
3777c478bd9Sstevel@tonic-gate 			break;
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 		new_per = (ret_val * 80)/100;
3807c478bd9Sstevel@tonic-gate 		while (new_per >= old_per) {
3817c478bd9Sstevel@tonic-gate 			(void) printf(".");
3827c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
3837c478bd9Sstevel@tonic-gate 			old_per++;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		(void) sleep(6);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if ((med_info.sm_media_type == SM_FLOPPY) ||
3897c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_PCMCIA_MEM) ||
3907c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_SCSI_FLOPPY)) {
3917c478bd9Sstevel@tonic-gate 		(void) write_sunos_label(fd, med_info.sm_media_type);
3927c478bd9Sstevel@tonic-gate 	} else {
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		/*
3957c478bd9Sstevel@tonic-gate 		 * Iomega drives don't destroy the data in quick format.
3967c478bd9Sstevel@tonic-gate 		 * Do a best effort write to first 1024 sectors.
3977c478bd9Sstevel@tonic-gate 		 */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		if (quick_format)
4007c478bd9Sstevel@tonic-gate 			overwrite_metadata(fd, handle);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		(void) write_default_label(handle, fd);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	release_SIGINT();
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * List removable devices.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate static void
process_l_flag()4127c478bd9Sstevel@tonic-gate process_l_flag()
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	int retry;
4157c478bd9Sstevel@tonic-gate 	int removable;
4167c478bd9Sstevel@tonic-gate 	int total_devices_found_last_time;
4177c478bd9Sstevel@tonic-gate 	int defer = 0;
4187c478bd9Sstevel@tonic-gate 	char *tmpstr;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate #define	MAX_RETRIES_FOR_SCANNING 3
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	vol_running = volmgt_running();
4237c478bd9Sstevel@tonic-gate 	if (vol_running)
4247c478bd9Sstevel@tonic-gate 		defer = 1;
4257c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Looking for devices...\n"));
4267c478bd9Sstevel@tonic-gate 	total_devices_found_last_time = 0;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*
4297c478bd9Sstevel@tonic-gate 	 * Strip out any leading path.  For example, /dev/rdsk/c3t0d0s2
4307c478bd9Sstevel@tonic-gate 	 * will result in tmpstr = c3t0d0s2.  dev_name is given as input
4317c478bd9Sstevel@tonic-gate 	 * argument.
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	if (dev_name) {
4347c478bd9Sstevel@tonic-gate 		if ((tmpstr = strrchr(dev_name, '/')) != NULL) {
4357c478bd9Sstevel@tonic-gate 			tmpstr += sizeof (char);
4367c478bd9Sstevel@tonic-gate 		} else {
4377c478bd9Sstevel@tonic-gate 			tmpstr = dev_name;
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < MAX_RETRIES_FOR_SCANNING; retry++) {
4427c478bd9Sstevel@tonic-gate 		removable = find_device(defer, tmpstr);
4437c478bd9Sstevel@tonic-gate 		if (removable == -1)
4447c478bd9Sstevel@tonic-gate 			break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		/*
4477c478bd9Sstevel@tonic-gate 		 * We'll do a small sleep and retry the command if volume
4487c478bd9Sstevel@tonic-gate 		 * manager is running and no removable devices are found.
4497c478bd9Sstevel@tonic-gate 		 * This is because the device may be busy.
4507c478bd9Sstevel@tonic-gate 		 */
4517c478bd9Sstevel@tonic-gate 		if (defer || (vol_running && (removable == 0))) {
4527c478bd9Sstevel@tonic-gate 			if ((total_devices_found == 0) ||
4537c478bd9Sstevel@tonic-gate 			    (total_devices_found !=
4547c478bd9Sstevel@tonic-gate 			    total_devices_found_last_time)) {
4557c478bd9Sstevel@tonic-gate 				total_devices_found_last_time =
4567c478bd9Sstevel@tonic-gate 				    total_devices_found;
4577c478bd9Sstevel@tonic-gate 				(void) sleep(2);
4587c478bd9Sstevel@tonic-gate 			} else {
4597c478bd9Sstevel@tonic-gate 				/* Do the printing this time */
4607c478bd9Sstevel@tonic-gate 				defer = 0;
4617c478bd9Sstevel@tonic-gate 				removable_found = 0;
4627c478bd9Sstevel@tonic-gate 			}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		} else
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if (removable_found == 0)
4687c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No removables found.\n"));
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * The following three routines handle the write protect
4737c478bd9Sstevel@tonic-gate  * options. These options are mostly Iomega ZIP/Jaz centric.
4747c478bd9Sstevel@tonic-gate  * The following options are allowed :
4757c478bd9Sstevel@tonic-gate  *  No write protect <=> write protect without passwd : use -w flag
4767c478bd9Sstevel@tonic-gate  *  from any state to WP with passwd : use -W flag
4777c478bd9Sstevel@tonic-gate  *  from WP with passwd to no write protect : use -W flag
4787c478bd9Sstevel@tonic-gate  *  from any state to RWP with passwd : use -R flag
4797c478bd9Sstevel@tonic-gate  *  from RWP with passwd to no write protect : use -R flag
4807c478bd9Sstevel@tonic-gate  *
4817c478bd9Sstevel@tonic-gate  * The following transitions is not allowed
4827c478bd9Sstevel@tonic-gate  * WP with passwd or RWP to WP without passwd.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static void
process_w_flag(smedia_handle_t handle)4867c478bd9Sstevel@tonic-gate process_w_flag(smedia_handle_t handle)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	int32_t rval;
4897c478bd9Sstevel@tonic-gate 	int32_t med_status;
4907c478bd9Sstevel@tonic-gate 	struct smwp_state wps;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if ((rval = smedia_get_protection_status((handle), &wps)) < 0) {
4937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4947c478bd9Sstevel@tonic-gate 		    gettext("Could not get medium status \n"));
4957c478bd9Sstevel@tonic-gate 		return;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 	med_status = wps.sm_new_state;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	wps.sm_version = SMWP_STATE_V_1;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (wp_enable) {	/* Enable write protect no password */
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		switch (med_status) {
5047c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_DISABLE  :
5057c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
5067c478bd9Sstevel@tonic-gate 				    SM_WRITE_PROTECT_NOPASSWD;
5077c478bd9Sstevel@tonic-gate 				wps.sm_passwd_len = 0;
5087c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
5097c478bd9Sstevel@tonic-gate 				    &wps);
5107c478bd9Sstevel@tonic-gate 				if (rval == -1)
5117c478bd9Sstevel@tonic-gate 					PERROR(WP_ERROR);
5127c478bd9Sstevel@tonic-gate 				break;
5137c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_NOPASSWD :
5147c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_0));
5157c478bd9Sstevel@tonic-gate 				break;
5167c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_PASSWD :
5177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_1));
5187c478bd9Sstevel@tonic-gate 				break;
5197c478bd9Sstevel@tonic-gate 			case SM_READ_WRITE_PROTECT :
5207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_2));
5217c478bd9Sstevel@tonic-gate 				break;
5227c478bd9Sstevel@tonic-gate 			case SM_STATUS_UNKNOWN :
5237c478bd9Sstevel@tonic-gate 				default :
5247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_UNKNOWN));
5257c478bd9Sstevel@tonic-gate 				break;
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	} else if (wp_disable) {
5287c478bd9Sstevel@tonic-gate 		switch (med_status) {
5297c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_NOPASSWD :
5307c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
5317c478bd9Sstevel@tonic-gate 				    SM_WRITE_PROTECT_DISABLE;
5327c478bd9Sstevel@tonic-gate 				wps.sm_passwd_len = 0;
5337c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
5347c478bd9Sstevel@tonic-gate 				    &wps);
5357c478bd9Sstevel@tonic-gate 				if (rval == -1)
5367c478bd9Sstevel@tonic-gate 					PERROR(WP_ERROR);
5377c478bd9Sstevel@tonic-gate 				break;
5387c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_DISABLE  :
5397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_3));
5407c478bd9Sstevel@tonic-gate 				break;
5417c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_PASSWD :
5427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_1));
5437c478bd9Sstevel@tonic-gate 				break;
5447c478bd9Sstevel@tonic-gate 			case SM_READ_WRITE_PROTECT :
5457c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_2));
5467c478bd9Sstevel@tonic-gate 				break;
5477c478bd9Sstevel@tonic-gate 			case SM_STATUS_UNKNOWN :
5487c478bd9Sstevel@tonic-gate 				default :
5497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_UNKNOWN));
5507c478bd9Sstevel@tonic-gate 				break;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate static void
process_W_flag(smedia_handle_t handle)5567c478bd9Sstevel@tonic-gate process_W_flag(smedia_handle_t handle)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	int32_t rval;
5597c478bd9Sstevel@tonic-gate 	int32_t med_status;
5607c478bd9Sstevel@tonic-gate 	struct smwp_state wps;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	DPRINTF("Write protect with password\n");
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if ((rval = smedia_get_protection_status((handle), &wps)) < 0) {
5657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5667c478bd9Sstevel@tonic-gate 		    gettext("Could not get medium status \n"));
5677c478bd9Sstevel@tonic-gate 		return;
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	med_status = wps.sm_new_state;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	wps.sm_version = SMWP_STATE_V_1;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if (wp_enable_passwd) {	/* Enable write protect  */
5747c478bd9Sstevel@tonic-gate 		switch (med_status) {
5757c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_DISABLE  :
5767c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_NOPASSWD :
5777c478bd9Sstevel@tonic-gate 				DPRINTF("Getting passwd\n");
5787c478bd9Sstevel@tonic-gate 				get_passwd(&wps, 1);
5797c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
5807c478bd9Sstevel@tonic-gate 				    SM_WRITE_PROTECT_PASSWD;
5817c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
5827c478bd9Sstevel@tonic-gate 				    &wps);
5837c478bd9Sstevel@tonic-gate 				if (rval == -1) {
5847c478bd9Sstevel@tonic-gate 					PERROR(WP_ERROR);
5857c478bd9Sstevel@tonic-gate 				}
5867c478bd9Sstevel@tonic-gate 				break;
5877c478bd9Sstevel@tonic-gate 			case SM_READ_WRITE_PROTECT :
5887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_4));
5897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_5));
5907c478bd9Sstevel@tonic-gate 				get_passwd(&wps, 0);
5917c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
5927c478bd9Sstevel@tonic-gate 				    SM_WRITE_PROTECT_PASSWD;
5937c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
5947c478bd9Sstevel@tonic-gate 				    &wps);
5957c478bd9Sstevel@tonic-gate 				if (rval == -1) {
5967c478bd9Sstevel@tonic-gate 					if (errno == EACCES) {
5977c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5987c478bd9Sstevel@tonic-gate 						    gettext(WP_MSG_10));
5997c478bd9Sstevel@tonic-gate 					} else {
6007c478bd9Sstevel@tonic-gate 						PERROR(WP_ERROR);
6017c478bd9Sstevel@tonic-gate 					}
6027c478bd9Sstevel@tonic-gate 				}
6037c478bd9Sstevel@tonic-gate 				break;
6047c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_PASSWD :
6057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_6));
6067c478bd9Sstevel@tonic-gate 				break;
6077c478bd9Sstevel@tonic-gate 			case SM_STATUS_UNKNOWN :
6087c478bd9Sstevel@tonic-gate 				default :
6097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6107c478bd9Sstevel@tonic-gate 				    gettext(WP_UNKNOWN));
6117c478bd9Sstevel@tonic-gate 				break;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	} else if (wp_disable_passwd) {
6147c478bd9Sstevel@tonic-gate 		switch (med_status) {
6157c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_PASSWD :
6167c478bd9Sstevel@tonic-gate 				get_passwd(&wps, 0);
6177c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
6187c478bd9Sstevel@tonic-gate 				    SM_WRITE_PROTECT_DISABLE;
6197c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
6207c478bd9Sstevel@tonic-gate 				    &wps);
6217c478bd9Sstevel@tonic-gate 				if (rval == -1) {
6227c478bd9Sstevel@tonic-gate 					if (errno == EACCES) {
6237c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6247c478bd9Sstevel@tonic-gate 						    gettext(WP_MSG_10));
6257c478bd9Sstevel@tonic-gate 					} else {
6267c478bd9Sstevel@tonic-gate 						PERROR(WP_ERROR);
6277c478bd9Sstevel@tonic-gate 					}
6287c478bd9Sstevel@tonic-gate 				}
6297c478bd9Sstevel@tonic-gate 				break;
6307c478bd9Sstevel@tonic-gate 			case SM_READ_WRITE_PROTECT :
6317c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_2));
6327c478bd9Sstevel@tonic-gate 				break;
6337c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_NOPASSWD :
6347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_7));
6357c478bd9Sstevel@tonic-gate 				break;
6367c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_DISABLE  :
6377c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_3));
6387c478bd9Sstevel@tonic-gate 				break;
6397c478bd9Sstevel@tonic-gate 			case SM_STATUS_UNKNOWN :
6407c478bd9Sstevel@tonic-gate 				default :
6417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_UNKNOWN));
6427c478bd9Sstevel@tonic-gate 				break;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static void
process_R_flag(smedia_handle_t handle)6487c478bd9Sstevel@tonic-gate process_R_flag(smedia_handle_t handle)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	int32_t rval;
6517c478bd9Sstevel@tonic-gate 	int32_t med_status;
6527c478bd9Sstevel@tonic-gate 	struct smwp_state wps;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	DPRINTF("Read Write protect \n");
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if ((rval = smedia_get_protection_status((handle), &wps)) < 0) {
6577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6587c478bd9Sstevel@tonic-gate 		    gettext("Could not get medium status \n"));
6597c478bd9Sstevel@tonic-gate 		return;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 	med_status = wps.sm_new_state;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	wps.sm_version = SMWP_STATE_V_1;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (rw_protect_enable) {	/* Enable write protect  */
6667c478bd9Sstevel@tonic-gate 		switch (med_status) {
6677c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_DISABLE  :
6687c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_NOPASSWD :
6697c478bd9Sstevel@tonic-gate 				DPRINTF("Getting passwd\n");
6707c478bd9Sstevel@tonic-gate 				get_passwd(&wps, 1);
6717c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
6727c478bd9Sstevel@tonic-gate 				    SM_READ_WRITE_PROTECT;
6737c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
6747c478bd9Sstevel@tonic-gate 				    &wps);
6757c478bd9Sstevel@tonic-gate 				if (rval == -1)
6767c478bd9Sstevel@tonic-gate 					PERROR(WP_ERROR);
6777c478bd9Sstevel@tonic-gate 				break;
6787c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_PASSWD :
6797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_8));
6807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_9));
6817c478bd9Sstevel@tonic-gate 				get_passwd(&wps, 0);
6827c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
6837c478bd9Sstevel@tonic-gate 				    SM_READ_WRITE_PROTECT;
6847c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
6857c478bd9Sstevel@tonic-gate 				    &wps);
6867c478bd9Sstevel@tonic-gate 				if (rval == -1) {
6877c478bd9Sstevel@tonic-gate 					if (errno == EACCES) {
6887c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6897c478bd9Sstevel@tonic-gate 						    gettext(WP_MSG_10));
6907c478bd9Sstevel@tonic-gate 					} else {
6917c478bd9Sstevel@tonic-gate 						PERROR(WP_ERROR);
6927c478bd9Sstevel@tonic-gate 					}
6937c478bd9Sstevel@tonic-gate 				}
6947c478bd9Sstevel@tonic-gate 				break;
6957c478bd9Sstevel@tonic-gate 			case SM_READ_WRITE_PROTECT :
6967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_4));
6977c478bd9Sstevel@tonic-gate 				break;
6987c478bd9Sstevel@tonic-gate 			case SM_STATUS_UNKNOWN :
6997c478bd9Sstevel@tonic-gate 				default :
7007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_UNKNOWN));
7017c478bd9Sstevel@tonic-gate 				break;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 	} else if (rw_protect_disable) {
7047c478bd9Sstevel@tonic-gate 		switch (med_status) {
7057c478bd9Sstevel@tonic-gate 			case SM_READ_WRITE_PROTECT :
7067c478bd9Sstevel@tonic-gate 			case SM_STATUS_UNKNOWN :
7077c478bd9Sstevel@tonic-gate 				get_passwd(&wps, 0);
7087c478bd9Sstevel@tonic-gate 				wps.sm_new_state =
7097c478bd9Sstevel@tonic-gate 				    SM_WRITE_PROTECT_DISABLE;
7107c478bd9Sstevel@tonic-gate 				rval = smedia_set_protection_status(handle,
7117c478bd9Sstevel@tonic-gate 				    &wps);
7127c478bd9Sstevel@tonic-gate 				if (rval == -1) {
7137c478bd9Sstevel@tonic-gate 					if (errno == EACCES) {
7147c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
7157c478bd9Sstevel@tonic-gate 						    gettext(WP_MSG_10));
7167c478bd9Sstevel@tonic-gate 					} else {
7177c478bd9Sstevel@tonic-gate 						PERROR(WP_ERROR);
7187c478bd9Sstevel@tonic-gate 					}
7197c478bd9Sstevel@tonic-gate 				}
7207c478bd9Sstevel@tonic-gate 				break;
7217c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_PASSWD :
7227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_1));
7237c478bd9Sstevel@tonic-gate 					break;
7247c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_NOPASSWD :
7257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_7));
7267c478bd9Sstevel@tonic-gate 				break;
7277c478bd9Sstevel@tonic-gate 			case SM_WRITE_PROTECT_DISABLE  :
7287c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_MSG_3));
7297c478bd9Sstevel@tonic-gate 				break;
7307c478bd9Sstevel@tonic-gate 			default :
7317c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(WP_UNKNOWN));
7327c478bd9Sstevel@tonic-gate 				break;
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate void
process_p_flag(smedia_handle_t handle,int32_t fd)7387c478bd9Sstevel@tonic-gate process_p_flag(smedia_handle_t handle, int32_t fd)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	int32_t med_status;
7417c478bd9Sstevel@tonic-gate 	smwp_state_t	wps;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	med_status = smedia_get_protection_status((handle), &wps);
7447c478bd9Sstevel@tonic-gate 	DPRINTF("Could not get medium status \n");
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate 	 * Workaround in case mode sense fails.
7487c478bd9Sstevel@tonic-gate 	 *
7497c478bd9Sstevel@tonic-gate 	 * Also, special handling for PCMCIA. PCMCIA does not have any
7507c478bd9Sstevel@tonic-gate 	 * ioctl to find out the write protect status. So, open the
7517c478bd9Sstevel@tonic-gate 	 * device with O_RDWR. If it passes, it is not write protected,
7527c478bd9Sstevel@tonic-gate 	 * otherwise it is write protected.
7537c478bd9Sstevel@tonic-gate 	 * If it fails, reopen with O_RDONLY, may be some other
7547c478bd9Sstevel@tonic-gate 	 * operation can go through.
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	if ((med_status < 0) || (med_info.sm_media_type == SM_PCMCIA_MEM) ||
7577c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_PCMCIA_ATA)) {
7587c478bd9Sstevel@tonic-gate 		(void) close(fd);
7597c478bd9Sstevel@tonic-gate 		DPRINTF("Reopening device for -p option\n");
7607c478bd9Sstevel@tonic-gate 		fd = my_open(dev_name, O_RDONLY|O_NDELAY);
7617c478bd9Sstevel@tonic-gate 		if (fd < 0)  {
7627c478bd9Sstevel@tonic-gate 			if (p_flag)  {
7637c478bd9Sstevel@tonic-gate 				PERROR("Could not open device");
7647c478bd9Sstevel@tonic-gate 				(void) smedia_release_handle(handle);
7657c478bd9Sstevel@tonic-gate 				(void) close(fd);
7667c478bd9Sstevel@tonic-gate 				exit(1);
7677c478bd9Sstevel@tonic-gate 			} else {
768*afb89a98SPavel Potoplyak 				(void) fprintf(stdout,
7697c478bd9Sstevel@tonic-gate 				    gettext("<Unknown>\n"));
7707c478bd9Sstevel@tonic-gate 				(void) smedia_release_handle(handle);
7717c478bd9Sstevel@tonic-gate 				(void) close(fd);
7727c478bd9Sstevel@tonic-gate 				return;
7737c478bd9Sstevel@tonic-gate 			}
7747c478bd9Sstevel@tonic-gate 			fd = my_open(dev_name, O_RDWR|O_NDELAY);
7757c478bd9Sstevel@tonic-gate 			if (fd < 0)  {
776*afb89a98SPavel Potoplyak 				(void) fprintf(stdout,
7777c478bd9Sstevel@tonic-gate 				gettext("Medium is write protected.\n"));
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 		} else { /* Open succeeded */
780*afb89a98SPavel Potoplyak 			(void) fprintf(stdout,
7817c478bd9Sstevel@tonic-gate 			    gettext("Medium is not write protected.\n"));
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 		return;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	med_status = wps.sm_new_state;
7867c478bd9Sstevel@tonic-gate 	switch (med_status) {
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		case SM_READ_WRITE_PROTECT :
789*afb89a98SPavel Potoplyak 			(void) fprintf(stdout,
7907c478bd9Sstevel@tonic-gate 			gettext("Medium is read-write protected.\n"));
7917c478bd9Sstevel@tonic-gate 			break;
7927c478bd9Sstevel@tonic-gate 		case SM_WRITE_PROTECT_PASSWD :
793*afb89a98SPavel Potoplyak 			(void) fprintf(stdout,
7947c478bd9Sstevel@tonic-gate 			gettext("Medium is write protected with password.\n"));
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 		case SM_WRITE_PROTECT_NOPASSWD :
797*afb89a98SPavel Potoplyak 			(void) fprintf(stdout,
7987c478bd9Sstevel@tonic-gate 			gettext("Medium is write protected.\n"));
7997c478bd9Sstevel@tonic-gate 			break;
8007c478bd9Sstevel@tonic-gate 		case SM_WRITE_PROTECT_DISABLE  :
801*afb89a98SPavel Potoplyak 			(void) fprintf(stdout,
8027c478bd9Sstevel@tonic-gate 			gettext("Medium is not write protected.\n"));
8037c478bd9Sstevel@tonic-gate 			break;
8047c478bd9Sstevel@tonic-gate 		case SM_STATUS_UNKNOWN :
8057c478bd9Sstevel@tonic-gate 			default:
806*afb89a98SPavel Potoplyak 			(void) fprintf(stdout,
8077c478bd9Sstevel@tonic-gate 			    gettext("Unknown write protect status.\n"));
8087c478bd9Sstevel@tonic-gate 			break;
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate static void
process_c_flag(smedia_handle_t handle)8137c478bd9Sstevel@tonic-gate process_c_flag(smedia_handle_t handle)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	char error_string[256];
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (smedia_reassign_block(handle, repair_blk_no) != 0) {
8187c478bd9Sstevel@tonic-gate 		(void) snprintf(error_string, 255,
819342440ecSPrasad Singamsetty 		    gettext("Could not repair block no %llu"), repair_blk_no);
8207c478bd9Sstevel@tonic-gate 		PERROR(error_string);
8217c478bd9Sstevel@tonic-gate 		return;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * This routine handles the -V (verify) option.
8277c478bd9Sstevel@tonic-gate  * There can be devices without rw_read option. If the raw_read
8287c478bd9Sstevel@tonic-gate  * and raw_write are not supported by the interface, then read and
8297c478bd9Sstevel@tonic-gate  * write system calls are used. It is assumed that either both
8307c478bd9Sstevel@tonic-gate  * raw_read and raw_write are supported or both are unsupported.
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate static void
process_V_flag(smedia_handle_t handle,int32_t fd)8347c478bd9Sstevel@tonic-gate process_V_flag(smedia_handle_t handle, int32_t fd)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	int32_t ret;
837342440ecSPrasad Singamsetty 	uint32_t j;
838342440ecSPrasad Singamsetty 	diskaddr_t bn;
8397c478bd9Sstevel@tonic-gate 	char *read_buf, *write_buf;
8407c478bd9Sstevel@tonic-gate 	int32_t old_per = 0;
8417c478bd9Sstevel@tonic-gate 	int32_t new_per;
8427c478bd9Sstevel@tonic-gate 	int32_t no_raw_rw = 0;
8437c478bd9Sstevel@tonic-gate 	int32_t verify_size;
844342440ecSPrasad Singamsetty 	diskaddr_t capacity;
8457c478bd9Sstevel@tonic-gate 	int32_t blocksize;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	DPRINTF("ANALYSE MEDIA \n");
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	ret = smedia_get_medium_property(handle, &med_info);
8507c478bd9Sstevel@tonic-gate 	if (ret == -1) {
8517c478bd9Sstevel@tonic-gate 		DPRINTF("get_media_info failed\n");
8527c478bd9Sstevel@tonic-gate 		return;
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	DPRINTF1("media_type %d\n", med_info.sm_media_type);
8567c478bd9Sstevel@tonic-gate 	DPRINTF1("sector_size %d\n", med_info.sm_blocksize);
857342440ecSPrasad Singamsetty 	DPRINTF1("num_sectors %u\n", (uint32_t)med_info.sm_capacity);
8587c478bd9Sstevel@tonic-gate 	DPRINTF1("nsect	 %d\n", med_info.sm_nsect);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	blocksize = med_info.sm_blocksize;
8617c478bd9Sstevel@tonic-gate 
862342440ecSPrasad Singamsetty 	capacity = (uint32_t)med_info.sm_capacity;
8637c478bd9Sstevel@tonic-gate 	verify_size = (med_info.sm_nsect > 64) ? 64 : med_info.sm_nsect;
8647c478bd9Sstevel@tonic-gate 	read_buf = (char *)malloc(blocksize * verify_size);
8657c478bd9Sstevel@tonic-gate 	if (read_buf == NULL) {
8667c478bd9Sstevel@tonic-gate 		DPRINTF("Could not allocate memory\n");
8677c478bd9Sstevel@tonic-gate 		return;
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 	write_buf = (char *)malloc(blocksize * verify_size);
8707c478bd9Sstevel@tonic-gate 	if (write_buf == NULL) {
8717c478bd9Sstevel@tonic-gate 		DPRINTF("Could not allocate memory\n");
8727c478bd9Sstevel@tonic-gate 		free(read_buf);
8737c478bd9Sstevel@tonic-gate 		return;
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (!verify_write) {
8777c478bd9Sstevel@tonic-gate 		DPRINTF("Non-destructive verify \n");
878342440ecSPrasad Singamsetty 		for (bn = 0; bn < (uint32_t)med_info.sm_capacity;
879342440ecSPrasad Singamsetty 		    bn += verify_size) {
880342440ecSPrasad Singamsetty 			new_per = (bn * 80)/(uint32_t)med_info.sm_capacity;
8817c478bd9Sstevel@tonic-gate 			if (new_per >= old_per) {
8827c478bd9Sstevel@tonic-gate 				(void) printf(".");
8837c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
8847c478bd9Sstevel@tonic-gate 				old_per++;
8857c478bd9Sstevel@tonic-gate 			}
886342440ecSPrasad Singamsetty 			DPRINTF2("Reading %d blks starting at %llu\n",
887342440ecSPrasad Singamsetty 			    verify_size, bn);
888342440ecSPrasad Singamsetty 			ret = verify(handle, fd, bn, verify_size, read_buf,
8897c478bd9Sstevel@tonic-gate 			    VERIFY_READ, blocksize, no_raw_rw);
8907c478bd9Sstevel@tonic-gate 			if ((ret == -1) && (errno == ENOTSUP)) {
8917c478bd9Sstevel@tonic-gate 				no_raw_rw = 1;
892342440ecSPrasad Singamsetty 				ret = verify(handle, fd, bn, verify_size,
8937c478bd9Sstevel@tonic-gate 				    read_buf,
8947c478bd9Sstevel@tonic-gate 				    VERIFY_READ, blocksize, no_raw_rw);
895342440ecSPrasad Singamsetty 				capacity = (diskaddr_t)med_info.sm_pcyl *
896342440ecSPrasad Singamsetty 				    med_info.sm_nhead * med_info.sm_nsect;
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 			if (ret != 0) {
9007c478bd9Sstevel@tonic-gate 				for (j = 0; j < verify_size; j++) {
901342440ecSPrasad Singamsetty 					if ((bn + j) >= capacity)
9027c478bd9Sstevel@tonic-gate 							return;
9037c478bd9Sstevel@tonic-gate 					DPRINTF2(
904342440ecSPrasad Singamsetty 					    "Reading %d blks starting "
905342440ecSPrasad Singamsetty 					    "at %llu\n", 1, bn + j);
906342440ecSPrasad Singamsetty 					ret = verify(handle, fd, bn + j, 1,
9077c478bd9Sstevel@tonic-gate 					    read_buf,
9087c478bd9Sstevel@tonic-gate 					    VERIFY_READ, blocksize,
9097c478bd9Sstevel@tonic-gate 					    no_raw_rw);
9107c478bd9Sstevel@tonic-gate 					if (ret == -1) {
911342440ecSPrasad Singamsetty 						(void) printf(
912342440ecSPrasad Singamsetty 						    "Bad block %llu\n",
913342440ecSPrasad Singamsetty 						    bn + j);
9147c478bd9Sstevel@tonic-gate 					}
9157c478bd9Sstevel@tonic-gate 				}
9167c478bd9Sstevel@tonic-gate 			}
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 	} else {
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		DPRINTF("Destrutive verify \n");
921342440ecSPrasad Singamsetty 		for (bn = 0; bn < (uint32_t)med_info.sm_capacity;
922342440ecSPrasad Singamsetty 		    bn += verify_size) {
923342440ecSPrasad Singamsetty 			new_per = (bn * 80)/(uint32_t)med_info.sm_capacity;
9247c478bd9Sstevel@tonic-gate 			if (new_per >= old_per) {
9257c478bd9Sstevel@tonic-gate 				(void) printf(".");
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
9287c478bd9Sstevel@tonic-gate 				old_per++;
9297c478bd9Sstevel@tonic-gate 			}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 			for (j = 0; j < blocksize * verify_size; j++) {
932342440ecSPrasad Singamsetty 				write_buf[j] = (bn | j) & 0xFF;
9337c478bd9Sstevel@tonic-gate 			}
934342440ecSPrasad Singamsetty 			DPRINTF2("Writing %d blks starting at %llu\n",
935342440ecSPrasad Singamsetty 			    verify_size, bn);
936342440ecSPrasad Singamsetty 			ret = verify(handle, fd, bn, verify_size, write_buf,
9377c478bd9Sstevel@tonic-gate 			    VERIFY_WRITE, blocksize, no_raw_rw);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 			if (ret != 0) {
9407c478bd9Sstevel@tonic-gate 				for (j = 0; j < verify_size; j++) {
941342440ecSPrasad Singamsetty 					if ((bn + j) >= capacity)
9427c478bd9Sstevel@tonic-gate 							break;
9437c478bd9Sstevel@tonic-gate 					DPRINTF2(
944342440ecSPrasad Singamsetty 					    "Writing %d blks starting "
945342440ecSPrasad Singamsetty 					    "at %llu\n", 1, bn + j);
946342440ecSPrasad Singamsetty 					ret = verify(handle, fd, bn + j, 1,
9477c478bd9Sstevel@tonic-gate 					    write_buf,
9487c478bd9Sstevel@tonic-gate 					    VERIFY_WRITE, blocksize,
9497c478bd9Sstevel@tonic-gate 					    no_raw_rw);
9507c478bd9Sstevel@tonic-gate 					if (ret == -1) {
951342440ecSPrasad Singamsetty 						(void) printf(
952342440ecSPrasad Singamsetty 						    "Bad block %llu\n", bn + j);
9537c478bd9Sstevel@tonic-gate 					}
9547c478bd9Sstevel@tonic-gate 				}
9557c478bd9Sstevel@tonic-gate 			}
956342440ecSPrasad Singamsetty 			DPRINTF2("Read after write  %d blks starting at %llu\n",
957342440ecSPrasad Singamsetty 			    verify_size, bn);
958342440ecSPrasad Singamsetty 			ret = verify(handle, fd, bn, verify_size,
9597c478bd9Sstevel@tonic-gate 			    read_buf, VERIFY_READ, blocksize, no_raw_rw);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 			if (ret != 0) {
9627c478bd9Sstevel@tonic-gate 				for (j = 0; j < verify_size; j++) {
963342440ecSPrasad Singamsetty 					if ((bn + j) >= capacity)
9647c478bd9Sstevel@tonic-gate 							return;
9657c478bd9Sstevel@tonic-gate 					DPRINTF2(
966342440ecSPrasad Singamsetty 					    "Read after write  %d blks "
967342440ecSPrasad Singamsetty 					    "starting at %llu\n", 1, bn + j);
968342440ecSPrasad Singamsetty 					ret = verify(handle, fd, bn + j, 1,
9697c478bd9Sstevel@tonic-gate 					    read_buf, VERIFY_READ,
9707c478bd9Sstevel@tonic-gate 					    blocksize, no_raw_rw);
9717c478bd9Sstevel@tonic-gate 					if (ret == -1) {
972342440ecSPrasad Singamsetty 						(void) printf(
973342440ecSPrasad Singamsetty 						    "Bad block %llu\n", bn + j);
9747c478bd9Sstevel@tonic-gate 					}
9757c478bd9Sstevel@tonic-gate 				}
9767c478bd9Sstevel@tonic-gate 			}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate static void
process_s_flag(smedia_handle_t handle,int32_t fd)9847c478bd9Sstevel@tonic-gate process_s_flag(smedia_handle_t handle, int32_t fd)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	int32_t i, ret;
987342440ecSPrasad Singamsetty 	struct extvtoc v_toc, t_vtoc;
9887c478bd9Sstevel@tonic-gate 	if (valid_slice_file(handle, fd, slice_file, &v_toc)) {
9897c478bd9Sstevel@tonic-gate 			(void) smedia_release_handle(handle);
9907c478bd9Sstevel@tonic-gate 			(void) close(fd);
9917c478bd9Sstevel@tonic-gate 			exit(1);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	(void) memset(&t_vtoc, 0, sizeof (t_vtoc));
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	t_vtoc.v_nparts = V_NUMPAR;
9987c478bd9Sstevel@tonic-gate 	t_vtoc.v_sanity = VTOC_SANE;
9997c478bd9Sstevel@tonic-gate 	t_vtoc.v_version = V_VERSION;
10007c478bd9Sstevel@tonic-gate 	t_vtoc.v_sectorsz = DEV_BSIZE;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/* Get existing Vtoc, don't bother if it fails. */
10037c478bd9Sstevel@tonic-gate 
1004e3397557Szk194757 	/* Turn on privileges. */
1005e3397557Szk194757 	(void) __priv_bracket(PRIV_ON);
10067c478bd9Sstevel@tonic-gate 
1007342440ecSPrasad Singamsetty 	(void) read_extvtoc(fd, &t_vtoc);
10087c478bd9Sstevel@tonic-gate 
1009e3397557Szk194757 	/* Turn off privileges. */
1010e3397557Szk194757 	(void) __priv_bracket(PRIV_OFF);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++) {
10137c478bd9Sstevel@tonic-gate 		t_vtoc.v_part[i].p_start = v_toc.v_part[i].p_start;
10147c478bd9Sstevel@tonic-gate 		t_vtoc.v_part[i].p_size = v_toc.v_part[i].p_size;
10157c478bd9Sstevel@tonic-gate 		t_vtoc.v_part[i].p_tag	= v_toc.v_part[i].p_tag;
10167c478bd9Sstevel@tonic-gate 		t_vtoc.v_part[i].p_flag = v_toc.v_part[i].p_flag;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	errno = 0;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 
1022e3397557Szk194757 	/* Turn on privileges. */
1023e3397557Szk194757 	(void) __priv_bracket(PRIV_ON);
10247c478bd9Sstevel@tonic-gate 
1025342440ecSPrasad Singamsetty 	ret = write_extvtoc(fd, &t_vtoc);
10267c478bd9Sstevel@tonic-gate 
1027e3397557Szk194757 	/* Turn off privileges. */
1028e3397557Szk194757 	(void) __priv_bracket(PRIV_OFF);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if (ret < 0)  {
10317c478bd9Sstevel@tonic-gate #ifdef sparc
10327c478bd9Sstevel@tonic-gate 		PERROR("write VTOC failed");
10337c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
10347c478bd9Sstevel@tonic-gate #else /* i386 */
10357c478bd9Sstevel@tonic-gate 		if (errno == EIO) {
10367c478bd9Sstevel@tonic-gate 			PERROR("No Solaris partition, eject & retry");
10377c478bd9Sstevel@tonic-gate 			DPRINTF1("Errno = %d\n", errno);
10387c478bd9Sstevel@tonic-gate 		} else {
10397c478bd9Sstevel@tonic-gate 			PERROR("write VTOC failed");
10407c478bd9Sstevel@tonic-gate 			DPRINTF1("Errno = %d\n", errno);
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate #endif
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate static void
process_e_flag(smedia_handle_t handle)10467c478bd9Sstevel@tonic-gate process_e_flag(smedia_handle_t handle)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	if (smedia_eject(handle) < 0) {
10497c478bd9Sstevel@tonic-gate 		PERROR("Eject failed");
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate static void
process_H_flag(smedia_handle_t handle,int32_t fd)10537c478bd9Sstevel@tonic-gate process_H_flag(smedia_handle_t handle, int32_t fd)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	uint32_t cyl, head;
10567c478bd9Sstevel@tonic-gate 	int32_t old_per = 0;
10577c478bd9Sstevel@tonic-gate 	int32_t new_per;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
10607c478bd9Sstevel@tonic-gate 	    gettext("Formatting will erase all the data on disk.\n"));
10617c478bd9Sstevel@tonic-gate 	if (!get_confirmation())
10627c478bd9Sstevel@tonic-gate 		return;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	for (cyl = 0; cyl < med_info.sm_pcyl; cyl++) {
10657c478bd9Sstevel@tonic-gate 		for (head = 0; head < med_info.sm_nhead; head++) {
10667c478bd9Sstevel@tonic-gate 			if (smedia_format_track(handle, cyl, head, SM_FORMAT_HD)
10677c478bd9Sstevel@tonic-gate 			    < 0) {
10687c478bd9Sstevel@tonic-gate 					PERROR("Format failed");
10697c478bd9Sstevel@tonic-gate 					return;
10707c478bd9Sstevel@tonic-gate 			}
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 		new_per = (cyl * 80)/med_info.sm_pcyl;
10737c478bd9Sstevel@tonic-gate 		while (new_per >= old_per) {
10747c478bd9Sstevel@tonic-gate 			(void) printf(".");
10757c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
10767c478bd9Sstevel@tonic-gate 			old_per++;
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	(void) write_sunos_label(fd, med_info.sm_media_type);
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate static void
process_D_flag(smedia_handle_t handle,int32_t fd)10847c478bd9Sstevel@tonic-gate process_D_flag(smedia_handle_t handle, int32_t fd)
10857c478bd9Sstevel@tonic-gate {
10867c478bd9Sstevel@tonic-gate 	uint32_t cyl, head;
10877c478bd9Sstevel@tonic-gate 	int32_t old_per = 0;
10887c478bd9Sstevel@tonic-gate 	int32_t new_per;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
10917c478bd9Sstevel@tonic-gate 	    gettext("Formatting will erase all the data on disk.\n"));
10927c478bd9Sstevel@tonic-gate 	if (!get_confirmation())
10937c478bd9Sstevel@tonic-gate 		return;
10947c478bd9Sstevel@tonic-gate 	for (cyl = 0; cyl < med_info.sm_pcyl; cyl++) {
10957c478bd9Sstevel@tonic-gate 		for (head = 0; head < med_info.sm_nhead; head++) {
10967c478bd9Sstevel@tonic-gate 			if (smedia_format_track(handle, cyl, head, SM_FORMAT_DD)
10977c478bd9Sstevel@tonic-gate 			    < 0) {
10987c478bd9Sstevel@tonic-gate 					PERROR("Format failed");
10997c478bd9Sstevel@tonic-gate 					return;
11007c478bd9Sstevel@tonic-gate 			}
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 		new_per = (cyl * 80)/med_info.sm_pcyl;
11037c478bd9Sstevel@tonic-gate 		while (new_per >= old_per) {
11047c478bd9Sstevel@tonic-gate 			(void) printf(".");
11057c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
11067c478bd9Sstevel@tonic-gate 			old_per++;
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 	(void) write_sunos_label(fd, med_info.sm_media_type);
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate /*
11137c478bd9Sstevel@tonic-gate  * This routine handles the -b (label) option.
11147c478bd9Sstevel@tonic-gate  * Please note that, this will fail if there is no valid vtoc is
11157c478bd9Sstevel@tonic-gate  * there on the medium and the vtoc is not faked.
11167c478bd9Sstevel@tonic-gate  */
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate static void
process_b_flag(int32_t fd)11197c478bd9Sstevel@tonic-gate process_b_flag(int32_t fd)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	int32_t ret, nparts;
1122342440ecSPrasad Singamsetty 	struct extvtoc v_toc;
11237c478bd9Sstevel@tonic-gate 	struct dk_gpt *vtoc64;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/* For EFI disks. */
11267c478bd9Sstevel@tonic-gate 	if (efi_type(fd)) {
11277c478bd9Sstevel@tonic-gate 		if (efi_alloc_and_read(fd, &vtoc64) < 0) {
11287c478bd9Sstevel@tonic-gate 			/*
11297c478bd9Sstevel@tonic-gate 			 * If reading the vtoc failed, try to
11307c478bd9Sstevel@tonic-gate 			 * auto-sense the disk configuration.
11317c478bd9Sstevel@tonic-gate 			 */
11327c478bd9Sstevel@tonic-gate 			if (efi_auto_sense(fd, &vtoc64) < 0) {
11337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11347c478bd9Sstevel@tonic-gate 				    gettext("Could not write label.\n"));
11357c478bd9Sstevel@tonic-gate 				return;
11367c478bd9Sstevel@tonic-gate 			}
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 		for (nparts = 0; nparts < vtoc64->efi_nparts;
11397c478bd9Sstevel@tonic-gate 		    nparts++) {
11407c478bd9Sstevel@tonic-gate 			if (vtoc64->efi_parts[nparts].p_tag ==
11417c478bd9Sstevel@tonic-gate 			    V_RESERVED) {
11427c478bd9Sstevel@tonic-gate 			if (vtoc64->efi_parts[nparts].p_name) {
11437c478bd9Sstevel@tonic-gate 				(void) strncpy(
11447c478bd9Sstevel@tonic-gate 				    vtoc64->efi_parts[nparts].p_name, label,
11457c478bd9Sstevel@tonic-gate 				    EFI_PART_NAME_LEN);
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 			break;
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 		}
11507c478bd9Sstevel@tonic-gate 		if (efi_write(fd, vtoc64) != 0) {
11517c478bd9Sstevel@tonic-gate 			(void) efi_err_check(vtoc64);
11527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11537c478bd9Sstevel@tonic-gate 			    gettext("Could not write label.\n"));
11547c478bd9Sstevel@tonic-gate 		}
11557c478bd9Sstevel@tonic-gate 		return;
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/* Get existing Vtoc */
11597c478bd9Sstevel@tonic-gate 
1160e3397557Szk194757 	/* Turn on privileges. */
1161e3397557Szk194757 	(void) __priv_bracket(PRIV_ON);
11627c478bd9Sstevel@tonic-gate 
1163342440ecSPrasad Singamsetty 	ret = read_extvtoc(fd, &v_toc);
11647c478bd9Sstevel@tonic-gate 
1165e3397557Szk194757 	/* Turn off privileges */
1166e3397557Szk194757 	(void) __priv_bracket(PRIV_OFF);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (ret < 0) {
11697c478bd9Sstevel@tonic-gate #ifdef sparc
11707c478bd9Sstevel@tonic-gate 		PERROR("read VTOC failed");
11717c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
11727c478bd9Sstevel@tonic-gate #else /* i386 */
11737c478bd9Sstevel@tonic-gate 		if (errno == EIO) {
11747c478bd9Sstevel@tonic-gate 			PERROR("No Solaris partition, eject & retry");
11757c478bd9Sstevel@tonic-gate 			DPRINTF1("Errno = %d\n", errno);
11767c478bd9Sstevel@tonic-gate 		} else {
11777c478bd9Sstevel@tonic-gate 			PERROR("read VTOC failed");
11787c478bd9Sstevel@tonic-gate 			DPRINTF1("Errno = %d\n", errno);
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate #endif
11817c478bd9Sstevel@tonic-gate 		return;
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	(void) strncpy(v_toc.v_volume, label, LEN_DKL_VVOL);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 
1187e3397557Szk194757 	/* Turn on the privileges. */
1188e3397557Szk194757 	(void) __priv_bracket(PRIV_ON);
11897c478bd9Sstevel@tonic-gate 
1190342440ecSPrasad Singamsetty 	ret = write_extvtoc(fd, &v_toc);
11917c478bd9Sstevel@tonic-gate 
1192e3397557Szk194757 	/* Turn off the privileges. */
1193e3397557Szk194757 	(void) __priv_bracket(PRIV_OFF);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	if (ret < 0) {
11967c478bd9Sstevel@tonic-gate #ifdef sparc
11977c478bd9Sstevel@tonic-gate 		PERROR("write VTOC failed");
11987c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
11997c478bd9Sstevel@tonic-gate #else /* i386 */
12007c478bd9Sstevel@tonic-gate 		if (errno == EIO) {
12017c478bd9Sstevel@tonic-gate 			PERROR("No Solaris partition, eject & retry");
12027c478bd9Sstevel@tonic-gate 			DPRINTF1("Errno = %d\n", errno);
12037c478bd9Sstevel@tonic-gate 		} else {
12047c478bd9Sstevel@tonic-gate 			PERROR("write VTOC failed");
12057c478bd9Sstevel@tonic-gate 			DPRINTF1("Errno = %d\n", errno);
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate #endif
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate }
1210