xref: /onnv-gate/usr/src/cmd/rmformat/rmf_main.c (revision 7563:84ec90ffc3f7)
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
52160Szk194757  * Common Development and Distribution License (the "License").
62160Szk194757  * 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*7563SPrasad.Singamsetty@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * rmf_main.c :
280Sstevel@tonic-gate  *	The file containing main() for rmformat. The command line
290Sstevel@tonic-gate  *	options are parsed in this file.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
332160Szk194757 #include <priv_utils.h>
340Sstevel@tonic-gate #include "rmformat.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate int32_t b_flag = 0;
370Sstevel@tonic-gate int32_t c_flag = 0;
380Sstevel@tonic-gate int32_t D_flag = 0;
390Sstevel@tonic-gate int32_t e_flag = 0;
400Sstevel@tonic-gate int32_t F_flag = 0;
410Sstevel@tonic-gate int32_t H_flag = 0;
420Sstevel@tonic-gate int32_t l_flag = 0;
430Sstevel@tonic-gate int32_t p_flag = 0;
440Sstevel@tonic-gate int32_t R_flag = 0;
450Sstevel@tonic-gate int32_t s_flag = 0;
460Sstevel@tonic-gate int32_t U_flag = 0;
470Sstevel@tonic-gate int32_t V_flag = 0;
480Sstevel@tonic-gate int32_t W_flag = 0;
490Sstevel@tonic-gate int32_t w_flag = 0;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static char *myname;
520Sstevel@tonic-gate char *slice_file = NULL;
530Sstevel@tonic-gate char *label;
54*7563SPrasad.Singamsetty@Sun.COM diskaddr_t repair_blk_no;
550Sstevel@tonic-gate int32_t quick_format = 0;
560Sstevel@tonic-gate int32_t long_format = 0;
570Sstevel@tonic-gate int32_t force_format = 0;
580Sstevel@tonic-gate int32_t rw_protect_enable = 0;
590Sstevel@tonic-gate int32_t rw_protect_disable = 0;
600Sstevel@tonic-gate int32_t wp_enable_passwd = 0;
610Sstevel@tonic-gate int32_t wp_disable_passwd = 0;
620Sstevel@tonic-gate int32_t wp_enable = 0;
630Sstevel@tonic-gate int32_t wp_disable = 0;
640Sstevel@tonic-gate int32_t verify_write = 0;
650Sstevel@tonic-gate char *dev_name = NULL;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static void usage(char *);
680Sstevel@tonic-gate void check_invalid_combinations();
690Sstevel@tonic-gate void check_invalid_combinations_again(int32_t);
70*7563SPrasad.Singamsetty@Sun.COM extern uint64_t my_atoll(char *ptr);
710Sstevel@tonic-gate extern void my_perror(char *err_string);
720Sstevel@tonic-gate void process_options();
730Sstevel@tonic-gate 
74796Smathue int
main(int32_t argc,char ** argv)750Sstevel@tonic-gate main(int32_t argc, char **argv)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	char i;
780Sstevel@tonic-gate 	char *tmp_ptr;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * This program requires file_dac_read, file_dac_write,
822160Szk194757 	 * proc_fork, proc_exec, and sys_devices privileges.
830Sstevel@tonic-gate 	 *
840Sstevel@tonic-gate 	 * child processes require the sys_mount privilege
850Sstevel@tonic-gate 	 */
862160Szk194757 	(void) __init_suid_priv(PU_INHERITPRIVS,
872160Szk194757 	    PRIV_FILE_DAC_READ, PRIV_FILE_DAC_WRITE, PRIV_PROC_FORK,
882160Szk194757 	    PRIV_PROC_EXEC, PRIV_SYS_MOUNT, PRIV_SYS_DEVICES, NULL);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
930Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	myname = argv[0];
990Sstevel@tonic-gate 	DPRINTF1("myname %s\n", myname);
1000Sstevel@tonic-gate 
101*7563SPrasad.Singamsetty@Sun.COM 	while ((i = getopt(argc, argv, "b:c:DeF:HlpR:s:tUV:W:w:")) != -1) {
1020Sstevel@tonic-gate 		DPRINTF1("arg %c\n", i);
1030Sstevel@tonic-gate 		switch (i) {
1040Sstevel@tonic-gate 		case 'b' :
1050Sstevel@tonic-gate 			b_flag++;
1060Sstevel@tonic-gate 			label = strdup(optarg);
1070Sstevel@tonic-gate 			if (strlen(label) > 8) {
1080Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Label is \
1090Sstevel@tonic-gate restricted to 8 characters.\n"));
1102160Szk194757 				__priv_relinquish();
1110Sstevel@tonic-gate 				exit(1);
1120Sstevel@tonic-gate 			}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		case 'c' :
1170Sstevel@tonic-gate 			c_flag++;
1180Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1190Sstevel@tonic-gate 			errno = 0;
120*7563SPrasad.Singamsetty@Sun.COM 			repair_blk_no = my_atoll(tmp_ptr);
121*7563SPrasad.Singamsetty@Sun.COM 			if (repair_blk_no == (diskaddr_t)(-1)) {
1220Sstevel@tonic-gate 				free(tmp_ptr);
1230Sstevel@tonic-gate 				usage("invalid block number");
1240Sstevel@tonic-gate 			}
1250Sstevel@tonic-gate 
126*7563SPrasad.Singamsetty@Sun.COM 			DPRINTF1(" block no. %llu\n", repair_blk_no);
1270Sstevel@tonic-gate 			free(tmp_ptr);
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		case 'D' :
1310Sstevel@tonic-gate 			D_flag++;
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		case 'e' :
1350Sstevel@tonic-gate 			e_flag++;
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		case 'F' :
1390Sstevel@tonic-gate 			F_flag++;
1400Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1410Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "quick") == 0) {
1420Sstevel@tonic-gate 				DPRINTF("q");
1430Sstevel@tonic-gate 				quick_format = 1;
1440Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "long") == 0) {
1450Sstevel@tonic-gate 				DPRINTF("l");
1460Sstevel@tonic-gate 				long_format = 1;
1470Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "force") == 0) {
1480Sstevel@tonic-gate 				DPRINTF("f");
1490Sstevel@tonic-gate 				force_format = 1;
1500Sstevel@tonic-gate 			} else {
1510Sstevel@tonic-gate 				free(tmp_ptr);
1520Sstevel@tonic-gate 				usage("invalid argument for option -F");
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 			free(tmp_ptr);
1550Sstevel@tonic-gate 			break;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		case 'H' :
1580Sstevel@tonic-gate 			H_flag++;
1590Sstevel@tonic-gate 			break;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		case 'l' :
1620Sstevel@tonic-gate 			l_flag++;
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		case 'p' :
1660Sstevel@tonic-gate 			p_flag++;
1670Sstevel@tonic-gate 			break;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		case 'R' :
1700Sstevel@tonic-gate 			R_flag++;
1710Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1720Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
1730Sstevel@tonic-gate 				rw_protect_enable++;
1740Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
1750Sstevel@tonic-gate 				rw_protect_disable++;
1760Sstevel@tonic-gate 			} else {
1770Sstevel@tonic-gate 				usage("Invalid argument for -R option");
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			free(tmp_ptr);
1800Sstevel@tonic-gate 			break;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		case 's' :
1830Sstevel@tonic-gate 			s_flag++;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 			slice_file = strdup(optarg);
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		case 'U' :
1890Sstevel@tonic-gate 			U_flag++;
1900Sstevel@tonic-gate 			break;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		case 'V' :
1930Sstevel@tonic-gate 			V_flag++;
1940Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1950Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "read") == 0) {
1960Sstevel@tonic-gate 				verify_write = 0;
1970Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "write") == 0) {
1980Sstevel@tonic-gate 				verify_write = 1;
1990Sstevel@tonic-gate 			} else {
2000Sstevel@tonic-gate 				usage("Invalid argument for -V option");
2010Sstevel@tonic-gate 			}
2020Sstevel@tonic-gate 			free(tmp_ptr);
2030Sstevel@tonic-gate 			break;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		case 'W' :
2060Sstevel@tonic-gate 			W_flag++;
2070Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2080Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
2090Sstevel@tonic-gate 				wp_enable_passwd++;
2100Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
2110Sstevel@tonic-gate 				wp_disable_passwd++;
2120Sstevel@tonic-gate 			} else {
2130Sstevel@tonic-gate 				usage("Invalid argument for -W option");
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 			free(tmp_ptr);
2160Sstevel@tonic-gate 			break;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		case 'w' :
2190Sstevel@tonic-gate 			w_flag++;
2200Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2210Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
2220Sstevel@tonic-gate 				wp_enable++;
2230Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
2240Sstevel@tonic-gate 				wp_disable++;
2250Sstevel@tonic-gate 			} else {
2260Sstevel@tonic-gate 				usage("Invalid arguments for -w option");
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 			free(tmp_ptr);
2290Sstevel@tonic-gate 			break;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		default:
2320Sstevel@tonic-gate 			usage("");
2330Sstevel@tonic-gate 			break;
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	if (optind < argc -1) {
2370Sstevel@tonic-gate 		usage("more than one device name argument");
2380Sstevel@tonic-gate 		/* NOTREACHED */
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (optind == argc -1) {
2420Sstevel@tonic-gate 		dev_name = argv[optind];
2430Sstevel@tonic-gate 	} else if (optind == 1) {
2440Sstevel@tonic-gate 		/* list devices by default */
2450Sstevel@tonic-gate 		l_flag++;
2460Sstevel@tonic-gate 	} else if ((optind == argc) && !l_flag) {
2470Sstevel@tonic-gate 		(void) fprintf(stderr,
2480Sstevel@tonic-gate 		    gettext("No device specified.\n"));
2492160Szk194757 		__priv_relinquish();
2500Sstevel@tonic-gate 		exit(1);
2510Sstevel@tonic-gate #if 0
2520Sstevel@tonic-gate 		(void) printf("Using floppy device\n");
2530Sstevel@tonic-gate 		dev_name = "/dev/rdiskette";
2540Sstevel@tonic-gate #endif /* 0 */
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	process_options();
2582160Szk194757 
2592160Szk194757 	/* Remove the privileges we gave. */
2602160Szk194757 	__priv_relinquish();
2610Sstevel@tonic-gate 	return (0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate static void
usage(char * str)2650Sstevel@tonic-gate usage(char *str)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (strlen(str)) {
2690Sstevel@tonic-gate 		(void) fprintf(stderr, "%s : ", myname);
2700Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(str));
2710Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\n");
2750Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s \
2760Sstevel@tonic-gate [ -DeHpU ] [ -b label ] [ -c blockno ] [ -F quick|long|force ] \
2770Sstevel@tonic-gate [ -R enable|disable ] [ -s filename ] [ -V read|write ] \
2780Sstevel@tonic-gate [ -w enable|disable ] [ -W enable|disable ] devname \n"), myname);
2790Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -l [ devname ]\n"),
2800Sstevel@tonic-gate 	    myname);
2812160Szk194757 	__priv_relinquish();
2820Sstevel@tonic-gate 	exit(1);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate void
check_invalid_combinations()2860Sstevel@tonic-gate check_invalid_combinations()
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/* Inherited from FLOPPY */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (D_flag && H_flag) {
2920Sstevel@tonic-gate 		usage("Options -D and -H incompatible");
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (D_flag && F_flag) {
2960Sstevel@tonic-gate 		usage("Options -D and -F incompatible");
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (H_flag && F_flag) {
3000Sstevel@tonic-gate 		usage("Options -H and -F incompatible");
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* rmformat additions */
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if ((w_flag && W_flag) || (w_flag && R_flag) || (W_flag && R_flag)) {
3060Sstevel@tonic-gate 		usage("Options -w, -W and -R incompatible");
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (c_flag && F_flag) {
3100Sstevel@tonic-gate 		usage("Options -c, -F incompatible");
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* l_flag is mutually exclusive of these flags */
3140Sstevel@tonic-gate 	if (l_flag && (D_flag + e_flag + H_flag + p_flag + U_flag +
3150Sstevel@tonic-gate 	    b_flag + c_flag + F_flag + R_flag + s_flag + V_flag +
3160Sstevel@tonic-gate 	    w_flag + W_flag)) {
3170Sstevel@tonic-gate 		usage("Options incompatible");
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate void
check_invalid_combinations_again(int32_t medium_type)3230Sstevel@tonic-gate check_invalid_combinations_again(int32_t medium_type)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	if ((medium_type != SM_FLOPPY) &&
326*7563SPrasad.Singamsetty@Sun.COM 	    (medium_type != SM_PCMCIA_MEM)) {
3270Sstevel@tonic-gate 		if (D_flag || H_flag) {
3280Sstevel@tonic-gate 			usage("-D, -H  options are compatible with floppy and \
3290Sstevel@tonic-gate PCMCIA memory cards only.");
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate }
333