xref: /titanic_44/usr/src/cmd/rmformat/rmf_main.c (revision 342440ec94087b8c751c580ab9ed6c693d31d418)
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*342440ecSPrasad Singamsetty  * Copyright 2008 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_main.c :
287c478bd9Sstevel@tonic-gate  *	The file containing main() for rmformat. The command line
297c478bd9Sstevel@tonic-gate  *	options are parsed in this file.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
33e3397557Szk194757 #include <priv_utils.h>
347c478bd9Sstevel@tonic-gate #include "rmformat.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate int32_t b_flag = 0;
377c478bd9Sstevel@tonic-gate int32_t c_flag = 0;
387c478bd9Sstevel@tonic-gate int32_t D_flag = 0;
397c478bd9Sstevel@tonic-gate int32_t e_flag = 0;
407c478bd9Sstevel@tonic-gate int32_t F_flag = 0;
417c478bd9Sstevel@tonic-gate int32_t H_flag = 0;
427c478bd9Sstevel@tonic-gate int32_t l_flag = 0;
437c478bd9Sstevel@tonic-gate int32_t p_flag = 0;
447c478bd9Sstevel@tonic-gate int32_t R_flag = 0;
457c478bd9Sstevel@tonic-gate int32_t s_flag = 0;
467c478bd9Sstevel@tonic-gate int32_t U_flag = 0;
477c478bd9Sstevel@tonic-gate int32_t V_flag = 0;
487c478bd9Sstevel@tonic-gate int32_t W_flag = 0;
497c478bd9Sstevel@tonic-gate int32_t w_flag = 0;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char *myname;
527c478bd9Sstevel@tonic-gate char *slice_file = NULL;
537c478bd9Sstevel@tonic-gate char *label;
54*342440ecSPrasad Singamsetty diskaddr_t repair_blk_no;
557c478bd9Sstevel@tonic-gate int32_t quick_format = 0;
567c478bd9Sstevel@tonic-gate int32_t long_format = 0;
577c478bd9Sstevel@tonic-gate int32_t force_format = 0;
587c478bd9Sstevel@tonic-gate int32_t rw_protect_enable = 0;
597c478bd9Sstevel@tonic-gate int32_t rw_protect_disable = 0;
607c478bd9Sstevel@tonic-gate int32_t wp_enable_passwd = 0;
617c478bd9Sstevel@tonic-gate int32_t wp_disable_passwd = 0;
627c478bd9Sstevel@tonic-gate int32_t wp_enable = 0;
637c478bd9Sstevel@tonic-gate int32_t wp_disable = 0;
647c478bd9Sstevel@tonic-gate int32_t verify_write = 0;
657c478bd9Sstevel@tonic-gate char *dev_name = NULL;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static void usage(char *);
687c478bd9Sstevel@tonic-gate void check_invalid_combinations();
697c478bd9Sstevel@tonic-gate void check_invalid_combinations_again(int32_t);
70*342440ecSPrasad Singamsetty extern uint64_t my_atoll(char *ptr);
717c478bd9Sstevel@tonic-gate extern void my_perror(char *err_string);
727c478bd9Sstevel@tonic-gate void process_options();
737c478bd9Sstevel@tonic-gate 
74360e6f5eSmathue int
main(int32_t argc,char ** argv)757c478bd9Sstevel@tonic-gate main(int32_t argc, char **argv)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	char i;
787c478bd9Sstevel@tonic-gate 	char *tmp_ptr;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * This program requires file_dac_read, file_dac_write,
82e3397557Szk194757 	 * proc_fork, proc_exec, and sys_devices privileges.
837c478bd9Sstevel@tonic-gate 	 *
847c478bd9Sstevel@tonic-gate 	 * child processes require the sys_mount privilege
857c478bd9Sstevel@tonic-gate 	 */
86e3397557Szk194757 	(void) __init_suid_priv(PU_INHERITPRIVS,
87e3397557Szk194757 	    PRIV_FILE_DAC_READ, PRIV_FILE_DAC_WRITE, PRIV_PROC_FORK,
88e3397557Szk194757 	    PRIV_PROC_EXEC, PRIV_SYS_MOUNT, PRIV_SYS_DEVICES, NULL);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
937c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	myname = argv[0];
997c478bd9Sstevel@tonic-gate 	DPRINTF1("myname %s\n", myname);
1007c478bd9Sstevel@tonic-gate 
101*342440ecSPrasad Singamsetty 	while ((i = getopt(argc, argv, "b:c:DeF:HlpR:s:tUV:W:w:")) != -1) {
1027c478bd9Sstevel@tonic-gate 		DPRINTF1("arg %c\n", i);
1037c478bd9Sstevel@tonic-gate 		switch (i) {
1047c478bd9Sstevel@tonic-gate 		case 'b' :
1057c478bd9Sstevel@tonic-gate 			b_flag++;
1067c478bd9Sstevel@tonic-gate 			label = strdup(optarg);
1077c478bd9Sstevel@tonic-gate 			if (strlen(label) > 8) {
1087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Label is \
1097c478bd9Sstevel@tonic-gate restricted to 8 characters.\n"));
110e3397557Szk194757 				__priv_relinquish();
1117c478bd9Sstevel@tonic-gate 				exit(1);
1127c478bd9Sstevel@tonic-gate 			}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		case 'c' :
1177c478bd9Sstevel@tonic-gate 			c_flag++;
1187c478bd9Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1197c478bd9Sstevel@tonic-gate 			errno = 0;
120*342440ecSPrasad Singamsetty 			repair_blk_no = my_atoll(tmp_ptr);
121*342440ecSPrasad Singamsetty 			if (repair_blk_no == (diskaddr_t)(-1)) {
1227c478bd9Sstevel@tonic-gate 				free(tmp_ptr);
1237c478bd9Sstevel@tonic-gate 				usage("invalid block number");
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 
126*342440ecSPrasad Singamsetty 			DPRINTF1(" block no. %llu\n", repair_blk_no);
1277c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		case 'D' :
1317c478bd9Sstevel@tonic-gate 			D_flag++;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		case 'e' :
1357c478bd9Sstevel@tonic-gate 			e_flag++;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		case 'F' :
1397c478bd9Sstevel@tonic-gate 			F_flag++;
1407c478bd9Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1417c478bd9Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "quick") == 0) {
1427c478bd9Sstevel@tonic-gate 				DPRINTF("q");
1437c478bd9Sstevel@tonic-gate 				quick_format = 1;
1447c478bd9Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "long") == 0) {
1457c478bd9Sstevel@tonic-gate 				DPRINTF("l");
1467c478bd9Sstevel@tonic-gate 				long_format = 1;
1477c478bd9Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "force") == 0) {
1487c478bd9Sstevel@tonic-gate 				DPRINTF("f");
1497c478bd9Sstevel@tonic-gate 				force_format = 1;
1507c478bd9Sstevel@tonic-gate 			} else {
1517c478bd9Sstevel@tonic-gate 				free(tmp_ptr);
1527c478bd9Sstevel@tonic-gate 				usage("invalid argument for option -F");
1537c478bd9Sstevel@tonic-gate 			}
1547c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		case 'H' :
1587c478bd9Sstevel@tonic-gate 			H_flag++;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		case 'l' :
1627c478bd9Sstevel@tonic-gate 			l_flag++;
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		case 'p' :
1667c478bd9Sstevel@tonic-gate 			p_flag++;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		case 'R' :
1707c478bd9Sstevel@tonic-gate 			R_flag++;
1717c478bd9Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1727c478bd9Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
1737c478bd9Sstevel@tonic-gate 				rw_protect_enable++;
1747c478bd9Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
1757c478bd9Sstevel@tonic-gate 				rw_protect_disable++;
1767c478bd9Sstevel@tonic-gate 			} else {
1777c478bd9Sstevel@tonic-gate 				usage("Invalid argument for -R option");
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		case 's' :
1837c478bd9Sstevel@tonic-gate 			s_flag++;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 			slice_file = strdup(optarg);
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		case 'U' :
1897c478bd9Sstevel@tonic-gate 			U_flag++;
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		case 'V' :
1937c478bd9Sstevel@tonic-gate 			V_flag++;
1947c478bd9Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1957c478bd9Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "read") == 0) {
1967c478bd9Sstevel@tonic-gate 				verify_write = 0;
1977c478bd9Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "write") == 0) {
1987c478bd9Sstevel@tonic-gate 				verify_write = 1;
1997c478bd9Sstevel@tonic-gate 			} else {
2007c478bd9Sstevel@tonic-gate 				usage("Invalid argument for -V option");
2017c478bd9Sstevel@tonic-gate 			}
2027c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		case 'W' :
2067c478bd9Sstevel@tonic-gate 			W_flag++;
2077c478bd9Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2087c478bd9Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
2097c478bd9Sstevel@tonic-gate 				wp_enable_passwd++;
2107c478bd9Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
2117c478bd9Sstevel@tonic-gate 				wp_disable_passwd++;
2127c478bd9Sstevel@tonic-gate 			} else {
2137c478bd9Sstevel@tonic-gate 				usage("Invalid argument for -W option");
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		case 'w' :
2197c478bd9Sstevel@tonic-gate 			w_flag++;
2207c478bd9Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2217c478bd9Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
2227c478bd9Sstevel@tonic-gate 				wp_enable++;
2237c478bd9Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
2247c478bd9Sstevel@tonic-gate 				wp_disable++;
2257c478bd9Sstevel@tonic-gate 			} else {
2267c478bd9Sstevel@tonic-gate 				usage("Invalid arguments for -w option");
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		default:
2327c478bd9Sstevel@tonic-gate 			usage("");
2337c478bd9Sstevel@tonic-gate 			break;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	if (optind < argc -1) {
2377c478bd9Sstevel@tonic-gate 		usage("more than one device name argument");
2387c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (optind == argc -1) {
2427c478bd9Sstevel@tonic-gate 		dev_name = argv[optind];
2437c478bd9Sstevel@tonic-gate 	} else if (optind == 1) {
2447c478bd9Sstevel@tonic-gate 		/* list devices by default */
2457c478bd9Sstevel@tonic-gate 		l_flag++;
2467c478bd9Sstevel@tonic-gate 	} else if ((optind == argc) && !l_flag) {
2477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2487c478bd9Sstevel@tonic-gate 		    gettext("No device specified.\n"));
249e3397557Szk194757 		__priv_relinquish();
2507c478bd9Sstevel@tonic-gate 		exit(1);
2517c478bd9Sstevel@tonic-gate #if 0
2527c478bd9Sstevel@tonic-gate 		(void) printf("Using floppy device\n");
2537c478bd9Sstevel@tonic-gate 		dev_name = "/dev/rdiskette";
2547c478bd9Sstevel@tonic-gate #endif /* 0 */
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	process_options();
258e3397557Szk194757 
259e3397557Szk194757 	/* Remove the privileges we gave. */
260e3397557Szk194757 	__priv_relinquish();
2617c478bd9Sstevel@tonic-gate 	return (0);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static void
usage(char * str)2657c478bd9Sstevel@tonic-gate usage(char *str)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (strlen(str)) {
2697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s : ", myname);
2707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(str));
2717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\n");
2757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s \
2767c478bd9Sstevel@tonic-gate [ -DeHpU ] [ -b label ] [ -c blockno ] [ -F quick|long|force ] \
2777c478bd9Sstevel@tonic-gate [ -R enable|disable ] [ -s filename ] [ -V read|write ] \
2787c478bd9Sstevel@tonic-gate [ -w enable|disable ] [ -W enable|disable ] devname \n"), myname);
2797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -l [ devname ]\n"),
2807c478bd9Sstevel@tonic-gate 	    myname);
281e3397557Szk194757 	__priv_relinquish();
2827c478bd9Sstevel@tonic-gate 	exit(1);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate void
check_invalid_combinations()2867c478bd9Sstevel@tonic-gate check_invalid_combinations()
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* Inherited from FLOPPY */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (D_flag && H_flag) {
2927c478bd9Sstevel@tonic-gate 		usage("Options -D and -H incompatible");
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (D_flag && F_flag) {
2967c478bd9Sstevel@tonic-gate 		usage("Options -D and -F incompatible");
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (H_flag && F_flag) {
3007c478bd9Sstevel@tonic-gate 		usage("Options -H and -F incompatible");
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/* rmformat additions */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if ((w_flag && W_flag) || (w_flag && R_flag) || (W_flag && R_flag)) {
3067c478bd9Sstevel@tonic-gate 		usage("Options -w, -W and -R incompatible");
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (c_flag && F_flag) {
3107c478bd9Sstevel@tonic-gate 		usage("Options -c, -F incompatible");
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/* l_flag is mutually exclusive of these flags */
3147c478bd9Sstevel@tonic-gate 	if (l_flag && (D_flag + e_flag + H_flag + p_flag + U_flag +
3157c478bd9Sstevel@tonic-gate 	    b_flag + c_flag + F_flag + R_flag + s_flag + V_flag +
3167c478bd9Sstevel@tonic-gate 	    w_flag + W_flag)) {
3177c478bd9Sstevel@tonic-gate 		usage("Options incompatible");
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate void
check_invalid_combinations_again(int32_t medium_type)3237c478bd9Sstevel@tonic-gate check_invalid_combinations_again(int32_t medium_type)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	if ((medium_type != SM_FLOPPY) &&
3267c478bd9Sstevel@tonic-gate 	    (medium_type != SM_PCMCIA_MEM)) {
3277c478bd9Sstevel@tonic-gate 		if (D_flag || H_flag) {
3287c478bd9Sstevel@tonic-gate 			usage("-D, -H  options are compatible with floppy and \
3297c478bd9Sstevel@tonic-gate PCMCIA memory cards only.");
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate }
333