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
5*7563SPrasad.Singamsetty@Sun.COM  * Common Development and Distribution License (the "License").
6*7563SPrasad.Singamsetty@Sun.COM  * 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  *   diskscan:
280Sstevel@tonic-gate  *   performs a verification pass over a device specified on command line;
290Sstevel@tonic-gate  *   display progress on stdout, and print bad sector numbers to stderr
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <stropts.h>
360Sstevel@tonic-gate #include <memory.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <malloc.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/vtoc.h>
440Sstevel@tonic-gate #include <sys/dkio.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void verexit();	/* signal handler and exit routine	*/
470Sstevel@tonic-gate static void report();	   /* tell user how we're getting on */
480Sstevel@tonic-gate static void scandisk(char *device, int devfd, int writeflag);
49*7563SPrasad.Singamsetty@Sun.COM static void report(char *what, diskaddr_t sector);
500Sstevel@tonic-gate static void verexit(int code);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	TRUE		1
530Sstevel@tonic-gate #define	FALSE		0
540Sstevel@tonic-gate #define	VER_WRITE	1
550Sstevel@tonic-gate #define	VER_READ	2
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static char	*progname;
580Sstevel@tonic-gate static struct  dk_geom	dkg;	  /* physical device boot info */
590Sstevel@tonic-gate static char	replybuf[64];	  /* used for user replies to questions */
60*7563SPrasad.Singamsetty@Sun.COM static diskaddr_t unix_base;	  /* first sector of UNIX System partition */
61*7563SPrasad.Singamsetty@Sun.COM static diskaddr_t unix_size;	  /* # sectors in UNIX System partition */
620Sstevel@tonic-gate static long	numbadrd = 0;	  /* number of bad sectors on read */
630Sstevel@tonic-gate static long	numbadwr = 0;	  /* number of bad sectors on write */
640Sstevel@tonic-gate static char	eol = '\n';	  /* end-of-line char (if -n, we set to '\n') */
650Sstevel@tonic-gate static int	print_warn = 1;	  /* should the warning message be printed? */
660Sstevel@tonic-gate static int do_scan = VER_READ;
670Sstevel@tonic-gate 
68362Sbg159949 int
690Sstevel@tonic-gate main(int argc, char *argv[]) {
700Sstevel@tonic-gate 	extern int	optind;
710Sstevel@tonic-gate 	int		devfd;	/* device file descriptor */
720Sstevel@tonic-gate 	struct stat 	statbuf;
730Sstevel@tonic-gate 	struct part_info	part_info;
74*7563SPrasad.Singamsetty@Sun.COM 	struct extpart_info	extpartinfo;
750Sstevel@tonic-gate 	int		c;
760Sstevel@tonic-gate 	int		errflag = 0;
770Sstevel@tonic-gate 	char		*device;
780Sstevel@tonic-gate 	progname = argv[0];
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/* Don't buffer stdout - we don't want to see bursts */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	setbuf(stdout, NULL);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Wny")) != -1)
850Sstevel@tonic-gate 	{
860Sstevel@tonic-gate 		switch (c) {
870Sstevel@tonic-gate 		case 'W':
880Sstevel@tonic-gate 			do_scan = VER_WRITE;
890Sstevel@tonic-gate 			break;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		case 'n':
920Sstevel@tonic-gate 			eol = '\r';
930Sstevel@tonic-gate 			break;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 		case 'y':
960Sstevel@tonic-gate 			print_warn = 0;
970Sstevel@tonic-gate 			break;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		default:
1000Sstevel@tonic-gate 			++errflag;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if ((argc - optind) < 1)
1060Sstevel@tonic-gate 		errflag++;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (errflag) {
1090Sstevel@tonic-gate 		(void) fprintf(stderr,
1100Sstevel@tonic-gate 		    "\nUsage: %s [-W] [-n] [-y] <phys_device_name> \n",
1110Sstevel@tonic-gate 			    progname);
1120Sstevel@tonic-gate 		exit(1);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	device = argv[optind];
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (stat(device, &statbuf)) {
1180Sstevel@tonic-gate 		(void) fprintf(stderr,
1190Sstevel@tonic-gate 		    "%s: invalid device %s, stat failed\n", progname, device);
1200Sstevel@tonic-gate 		perror("");
1210Sstevel@tonic-gate 		exit(4);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
1240Sstevel@tonic-gate 		(void) fprintf(stderr,
1250Sstevel@tonic-gate 		    "%s: device %s is not character special\n",
1260Sstevel@tonic-gate 			    progname, device);
1270Sstevel@tonic-gate 		exit(5);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	if ((devfd = open(device, O_RDWR)) == -1) {
1300Sstevel@tonic-gate 		(void) fprintf(stderr,
1310Sstevel@tonic-gate 		    "%s: open of %s failed\n", progname, device);
1320Sstevel@tonic-gate 		perror("");
1330Sstevel@tonic-gate 		exit(8);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if ((ioctl(devfd, DKIOCGGEOM, &dkg)) == -1) {
1370Sstevel@tonic-gate 		(void) fprintf(stderr,
1380Sstevel@tonic-gate 		    "%s: unable to get disk geometry.\n", progname);
1390Sstevel@tonic-gate 		perror("");
1400Sstevel@tonic-gate 		exit(9);
1410Sstevel@tonic-gate 	}
142*7563SPrasad.Singamsetty@Sun.COM 
143*7563SPrasad.Singamsetty@Sun.COM 	if ((ioctl(devfd, DKIOCEXTPARTINFO, &extpartinfo)) == 0) {
144*7563SPrasad.Singamsetty@Sun.COM 		unix_base = extpartinfo.p_start;
145*7563SPrasad.Singamsetty@Sun.COM 		unix_size = extpartinfo.p_length;
146*7563SPrasad.Singamsetty@Sun.COM 	} else {
147*7563SPrasad.Singamsetty@Sun.COM 		if ((ioctl(devfd, DKIOCPARTINFO, &part_info)) == 0) {
148*7563SPrasad.Singamsetty@Sun.COM 			unix_base = (ulong_t)part_info.p_start;
149*7563SPrasad.Singamsetty@Sun.COM 			unix_size = (uint_t)part_info.p_length;
150*7563SPrasad.Singamsetty@Sun.COM 		} else {
151*7563SPrasad.Singamsetty@Sun.COM 			(void) fprintf(stderr, "%s: unable to get partition "
152*7563SPrasad.Singamsetty@Sun.COM 			    "info.\n", progname);
153*7563SPrasad.Singamsetty@Sun.COM 			perror("");
154*7563SPrasad.Singamsetty@Sun.COM 			exit(9);
155*7563SPrasad.Singamsetty@Sun.COM 		}
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	scandisk(device, devfd, do_scan);
159362Sbg159949 	return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  *  scandisk:
1640Sstevel@tonic-gate  *	  attempt to read every sector of the drive;
1650Sstevel@tonic-gate  *	  display bad sectors found on stderr
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static void
1690Sstevel@tonic-gate scandisk(char *device, int devfd, int writeflag)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	int	 trksiz = NBPSCTR * dkg.dkg_nsect;
1720Sstevel@tonic-gate 	char	*verbuf;
173*7563SPrasad.Singamsetty@Sun.COM 	diskaddr_t cursec;
1740Sstevel@tonic-gate 	int	 cylsiz =  dkg.dkg_nsect * dkg.dkg_nhead;
1750Sstevel@tonic-gate 	int	 i;
1760Sstevel@tonic-gate 	char	*rptr;
177*7563SPrasad.Singamsetty@Sun.COM 	diskaddr_t tmpend = 0;
178*7563SPrasad.Singamsetty@Sun.COM 	diskaddr_t tmpsec = 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /* #define LIBMALLOC */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate #ifdef LIBMALLOC
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	extern int  mallopt();
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* This adds 5k to the binary, but it's a lot prettier */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/* make track buffer sector aligned */
1900Sstevel@tonic-gate 	if (mallopt(M_GRAIN, 0x200)) {
1910Sstevel@tonic-gate 		perror("mallopt");
1920Sstevel@tonic-gate 		exit(1);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	if ((verbuf = malloc(NBPSCTR * dkg.dkg_nsect)) == (char *)NULL) {
1950Sstevel@tonic-gate 		perror("malloc");
1960Sstevel@tonic-gate 		exit(1);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate #else
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if ((verbuf = malloc(0x200 + NBPSCTR * dkg.dkg_nsect))
2020Sstevel@tonic-gate 	    == (char *)NULL) {
2030Sstevel@tonic-gate 		perror("malloc");
2040Sstevel@tonic-gate 		exit(1);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	verbuf = (char *)(((unsigned long)verbuf + 0x00000200) & 0xfffffe00);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* write pattern in track buffer */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	for (i = 0; i < trksiz; i++)
2130Sstevel@tonic-gate 		verbuf[i] = (char)0xe5;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/* Turn off retry, and set trap to turn them on again */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	(void) signal(SIGINT, verexit);
2180Sstevel@tonic-gate 	(void) signal(SIGQUIT, verexit);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (writeflag == VER_READ)
2210Sstevel@tonic-gate 		goto do_readonly;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 *   display warning only if -n arg not passed
2250Sstevel@tonic-gate 	 *   (otherwise the UI system will take care of it)
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (print_warn == 1) {
2290Sstevel@tonic-gate 		(void) printf(
2300Sstevel@tonic-gate 		    "\nCAUTION: ABOUT TO DO DESTRUCTIVE WRITE ON %s\n", device);
2310Sstevel@tonic-gate 		(void) printf("	 THIS WILL DESTROY ANY DATA YOU HAVE ON\n");
2320Sstevel@tonic-gate 		(void) printf("	 THAT PARTITION OR SLICE.\n");
2330Sstevel@tonic-gate 		(void) printf("Do you want to continue (y/n)? ");
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		rptr = fgets(replybuf, 64*sizeof (char), stdin);
2360Sstevel@tonic-gate 		if (!rptr || !((replybuf[0] == 'Y') || (replybuf[0] == 'y')))
2370Sstevel@tonic-gate 			exit(10);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	for (cursec = 0; cursec < unix_size; cursec +=  dkg.dkg_nsect) {
241*7563SPrasad.Singamsetty@Sun.COM 		if (llseek(devfd, cursec * NBPSCTR, 0) == -1) {
2420Sstevel@tonic-gate 			(void) fprintf(stderr,
243*7563SPrasad.Singamsetty@Sun.COM 			    "Error seeking sector %llu Cylinder %llu\n",
244*7563SPrasad.Singamsetty@Sun.COM 			    cursec, cursec / cylsiz);
2450Sstevel@tonic-gate 			verexit(1);
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * verify sector at a time only when
2500Sstevel@tonic-gate 		 * the whole track write fails;
2510Sstevel@tonic-gate 		 *  (if we write a sector at a time, it takes forever)
2520Sstevel@tonic-gate 		 */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		report("Writing", cursec);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		if (write(devfd, verbuf, trksiz) != trksiz) {
2570Sstevel@tonic-gate 			tmpend = cursec +  dkg.dkg_nsect;
2580Sstevel@tonic-gate 			for (tmpsec = cursec; tmpsec < tmpend; tmpsec++) {
2590Sstevel@tonic-gate 				/*
2600Sstevel@tonic-gate 				 *  try writing to it once; if this fails,
2610Sstevel@tonic-gate 				 *  then announce the sector bad on stderr
2620Sstevel@tonic-gate 				 */
2630Sstevel@tonic-gate 
264*7563SPrasad.Singamsetty@Sun.COM 				if (llseek(devfd, tmpsec * NBPSCTR, 0) == -1) {
2650Sstevel@tonic-gate 					(void) fprintf(stderr, "Error seeking "
266*7563SPrasad.Singamsetty@Sun.COM 					    "sector %llu Cylinder %llu\n",
2670Sstevel@tonic-gate 					    tmpsec, cursec / cylsiz);
2680Sstevel@tonic-gate 					verexit(1);
2690Sstevel@tonic-gate 				}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 				report("Writing", tmpsec);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 				if (write(devfd, verbuf, NBPSCTR) != NBPSCTR) {
2740Sstevel@tonic-gate 					(void) fprintf(stderr,
275*7563SPrasad.Singamsetty@Sun.COM 					    "%llu\n", tmpsec + unix_base);
2760Sstevel@tonic-gate 					numbadwr++;
2770Sstevel@tonic-gate 				}
2780Sstevel@tonic-gate 			}
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	(void) putchar(eol);
2830Sstevel@tonic-gate 	do_readonly:
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	for (cursec = 0; cursec < unix_size; cursec +=  dkg.dkg_nsect) {
286*7563SPrasad.Singamsetty@Sun.COM 		if (llseek(devfd, cursec * NBPSCTR, 0) == -1) {
2870Sstevel@tonic-gate 			(void) fprintf(stderr,
288*7563SPrasad.Singamsetty@Sun.COM 			    "Error seeking sector %llu Cylinder %llu\n",
289*7563SPrasad.Singamsetty@Sun.COM 			    cursec, cursec / cylsiz);
2900Sstevel@tonic-gate 			verexit(1);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		/*
2940Sstevel@tonic-gate 		 * read a sector at a time only when
2950Sstevel@tonic-gate 		 * the whole track write fails;
2960Sstevel@tonic-gate 		 * (if we do a sector at a time read, it takes forever)
2970Sstevel@tonic-gate 		 */
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		report("Reading", cursec);
3000Sstevel@tonic-gate 		if (read(devfd, verbuf, trksiz) != trksiz) {
3010Sstevel@tonic-gate 			tmpend = cursec +  dkg.dkg_nsect;
3020Sstevel@tonic-gate 			for (tmpsec = cursec; tmpsec < tmpend; tmpsec++) {
303*7563SPrasad.Singamsetty@Sun.COM 				if (llseek(devfd, tmpsec * NBPSCTR, 0) == -1) {
3040Sstevel@tonic-gate 					(void) fprintf(stderr, "Error seeking"
305*7563SPrasad.Singamsetty@Sun.COM 					    " sector %llu Cylinder %llu\n",
3060Sstevel@tonic-gate 					    tmpsec, cursec / cylsiz);
3070Sstevel@tonic-gate 					verexit(1);
3080Sstevel@tonic-gate 				}
3090Sstevel@tonic-gate 				report("Reading", tmpsec);
3100Sstevel@tonic-gate 				if (read(devfd, verbuf, NBPSCTR) != NBPSCTR) {
311*7563SPrasad.Singamsetty@Sun.COM 					(void) fprintf(stderr, "%llu\n",
3120Sstevel@tonic-gate 					    tmpsec + unix_base);
3130Sstevel@tonic-gate 					numbadrd++;
3140Sstevel@tonic-gate 				}
3150Sstevel@tonic-gate 			}
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	(void) printf("%c%c======== Diskscan complete ========%c", eol,
3190Sstevel@tonic-gate 	    eol, eol);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if ((numbadrd > 0) || (numbadwr > 0)) {
3220Sstevel@tonic-gate 		(void) printf("%cFound %ld bad sector(s) on read,"
3230Sstevel@tonic-gate 		    " %ld bad sector(s) on write%c",
3240Sstevel@tonic-gate 		    eol, numbadrd, numbadwr, eol);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static void
3290Sstevel@tonic-gate verexit(int code)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	(void) printf("\n");
3320Sstevel@tonic-gate 	exit(code);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  *   report where we are...
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate static void
341*7563SPrasad.Singamsetty@Sun.COM report(char *what, diskaddr_t sector)
3420Sstevel@tonic-gate {
343*7563SPrasad.Singamsetty@Sun.COM 	(void) printf("%s sector %-19llu of %-19llu%c", what, sector,
3440Sstevel@tonic-gate 	    unix_size, eol);
3450Sstevel@tonic-gate }
346