From: jbev@iscden.jbsys.com (Jim Bevier - J B Systems)
Newsgroups: comp.unix.bsd
Subject: Bad sector scanning program!
Date: 26 Aug 92 23:46:22 GMT
Organization: J B Systems on HiPeak, Morrison, Co.

I have updated bad144.c to recognize a -s option.  This will scan
the 386bsd partition (rwd0c).  It will list the sector numbers that
you can then use to input to bad144 with the -a -c options.  I 
thought this might be useful to others.  The best time to run
this scan is after you have written the label to the disk, but
before making the newfs.  However, it can be run at anytime.
I plan to update these changes later to automatically add any
defects found to the bad sector table.  For now, it just scans.
It reads a track at a time until an error is found.  It then
rereads the track to find the defective sector.  It also prints
out your disk label table showing offset and size in both sectors
and cylinders.  Type bad144 -s wd0.  Here is what the output looks like:

device /dev/rwd0 is a ESDI disk
rwd0a ROOT   offset:  196365 ( 247), size:  508800 ( 640), type = 4.2BSD
rwd0b SWAP   offset:  705165 ( 887), size:   63600 (  80), type = swap
rwd0c 386BSD offset:  196365 ( 247), size: 1120155 (1409), type = unused
rwd0d DISK   offset:       0 (   0), size: 1316520 (1656), type = unused
rwd0e DOS?   offset:       0 (   0), size:  196365 ( 247), type = unused
rwd0h USR    offset:  768765 ( 967), size:  546165 ( 687), type = 4.2BSD
secs: 53, tracks: 15, cyl: 1656, sec/cyl: 795, start: 196365, end: 1316341
Starting scan of /dev/rwd0c at cylinder 247
 247( 196365)

The scan stops before the bad track table.  That is the end of patition c
less 1 cylinder plus 126 sectors.

The bad tracking will not work without a fix to wd.c.  This was posted
earlier to the net.  Apply this patch:

---- snip snip ----
*** wd.c.ORIG	Wed Aug 19 15:43:21 1992
--- wd.c	Wed Aug 19 16:16:18 1992
***************
*** 402,407 ****
--- 402,408 ----
  			cylin = blknum / secpercyl;
  			head = (blknum % secpercyl) / secpertrk;
  			sector = blknum % secpertrk;
+ 			du->dk_flags |= DKFL_SINGLE;
  #ifdef	WDDEBUG
  			    printf("new = %d\n", blknum);
  #endif
---- snip snip ----
from:
UUCP: {hplabs!hp-sdd ucsd nosc}!crash!fpm
ARPA: crash!fpm@nosc.mil
INET: fpm@crash.cts.com

Following is the updated bad144.c program.  Let me know what you think.
I think this will also work for a second disk drive.  If you find any
errors or think of any other changes, let me know.

Cheers,

Jim Bevier
jbev@jbsys.com

---- snip snip ----
/*
 * Copyright (c) 1980,1986,1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980,1986,1988 Regents of the University of California.\n\
 All rights reserved.\n";
#endif not lint

#ifndef lint
static char sccsid[] = "@(#)bad144.c	5.19 (Berkeley) 4/11/91";
#endif not lint

/*
 * bad144
 *
 * This program prints and/or initializes a bad block record for a pack,
 * in the format used by the DEC standard 144.
 * It can also add bad sector(s) to the record, moving the sector
 * replacements as necessary.
 *
 * It is preferable to write the bad information with a standard formatter,
 * but this program will do.
 * 
 * RP06 sectors are marked as bad by inverting the format bit in the
 * header; on other drives the valid-sector bit is cleared.
 */

#define DKTYPENAMES

#include <sys/param.h>
#include <sys/dkbad.h>
#include <sys/ioctl.h>
#include <ufs/fs.h>
#include <sys/file.h>
#include <sys/disklabel.h>

#include <stdio.h>
#include <paths.h>
#include <string.h>

#define RETRIES	10		/* number of retries on reading old sectors */
#ifdef __386BSD__
#define	RAWPART	"d"		/* disk partition containing badsector tables */
#else
#define	RAWPART	"c"		/* disk partition containing badsector tables */
#endif

int	fflag, add, copy, verbose, nflag, sflag;
int	compare();
int	dups;
int	badfile = -1;		/* copy of badsector table to use, -1 if any */
#define MAXSECSIZE	1024
struct	dkbad curbad, oldbad;
#define	DKBAD_MAGIC	0x4321

char *buf;
char	label[BBSIZE];
daddr_t	size, getold(), badsn();
struct	disklabel *dp;
char	name[BUFSIZ];
char	*malloc();
off_t	lseek();
int	bstart, bend;		/* start and ending block numbers */
char	*p;			/* temp dev name pointer */
char	devname[BUFSIZ];
main(argc, argv)
	int argc;
	char *argv[];
{
	register struct bt_bad *bt;
	daddr_t	sn, bn[126];
	int i, j, f, nbad, new, bad, errs;

    	setbuf(stdout, NULL);
	argc--, argv++;
	while (argc > 0 && **argv == '-') {
		(*argv)++;
		while (**argv) {
			switch (**argv) {
			    case 'a':
				add++;
				break;
			    case 'c':
				copy++;
				break;
			    case 'v':
				verbose++;
				break;
			    case 'n':
				nflag++;
				verbose++;
				break;
			    case 's':		/* scan partition */
				sflag++;
				verbose++;
				break;
			    default:
				if (**argv >= '0' && **argv <= '4') {
					badfile = **argv - '0';
					break;
				}
				goto usage;
			}
			(*argv)++;
		}
		argc--, argv++;
	}
	if (argc < 1) {
usage:
		fprintf(stderr,
		  "usage: bad144 [ -f ] disk [ snum [ bn ... ] ]\n");
		fprintf(stderr,
	      "to read or overwrite bad-sector table, e.g.: bad144 hp0\n");
		fprintf(stderr,
		  "or bad144 -a [ -f ] [ -c ] disk  bn ...\n");
		fprintf(stderr, "where options are:\n");
		fprintf(stderr, "\t-a  add new bad sectors to the table\n");
		fprintf(stderr, "\t-f  reformat listed sectors as bad\n");
		fprintf(stderr, "\t-c  copy original sector to replacement\n");
		fprintf(stderr, "\t-s  scan partition for bad sectors\n");
		exit(1);
	}
	if (argv[0][0] != '/')
		(void)sprintf(name, "%sr%s%s", _PATH_DEV, argv[0], RAWPART);
	else
		strcpy(name, argv[0]);
	f = open(name, argc == 1? O_RDONLY : O_RDWR);
	if (f < 0)
		Perror(name);
    	p = rindex(name, '/');
    	strcpy(devname,++p);
    	devname[strlen(p)-1] = '\0';
	/* obtain label and adjust to fit */
	dp = (struct disklabel *)&label;
	if (ioctl(f, DIOCGDINFO, dp) < 0)
		Perror("ioctl DIOCGDINFO");
	if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC
		/* dkcksum(lp) != 0 */ ) {
		fprintf(stderr, "Bad pack magic number (pack is unlabeled)\n");
		exit(1);
	}
	if (dp->d_secsize > MAXSECSIZE || dp->d_secsize <= 0) {
		fprintf(stderr, "Disk sector size too large/small (%d)\n",
			dp->d_secsize);
		exit(7);
	}
#ifdef __386BSD__
	if (dp->d_type == DTYPE_SCSI) {
		fprintf(stderr, "SCSI disks don't use bad144!\n");
		exit(1);
	}
	/* are we inside a DOS partition? */
    if (verbose) {
static char * parts[] = {
    "ROOT  ",
    "SWAP  ",
    "386BSD",
    "DISK  ",
    "DOS?  ",
    "USR2  ",
    "USR1  ",
    "USR   "
    };
    	printf("device /dev/%s is a %s disk\n",
    	 devname, dktypenames[dp->d_type]);
    	j = dp->d_secpercyl;
    	for (i=0; i < dp->d_npartitions; i++) {
    	    if (dp->d_partitions[i].p_size == 0)continue;
    	printf("%s%c %s offset: %7d (%4d), size: %7d (%4d), type = %s\n",
    	 devname, 'a'+i, parts[i], dp->d_partitions[i].p_offset,
    	  dp->d_partitions[i].p_offset/j,
    	 dp->d_partitions[i].p_size,
    	 dp->d_partitions[i].p_size/j,
    	 fstypenames[dp->d_partitions[i].p_fstype]);
    	}
    }
	if (dp->d_partitions[0].p_offset) {
		/* yes, rules change. assume bad tables at end of partition C,
		   which maps all of DOS partition we are within -wfj */
		size = dp->d_partitions[2].p_offset +
    		   dp->d_partitions[2].p_size;
	} else
#endif
	size = dp->d_nsectors * dp->d_ntracks * dp->d_ncylinders; 
    bstart = 0;
    if (dp->d_partitions[2].p_size) {
 	bstart = dp->d_partitions[2].p_offset;
    }
    /* determine where to stop scanning */
    bend = dp->d_partitions[2].p_size + dp->d_partitions[2].p_offset;
    bend -= (dp->d_nsectors + 126);
    if (verbose) {
  printf("secs: %d, tracks: %d, cyl: %d, sec/cyl: %d, start: %d, end: %d\n",
    dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders,
     dp->d_secpercyl, bstart, bend);
    }
    if (sflag) {		/* search for bad sectors */
    	int curr_sec, tries, n;
    	int spc = dp->d_secpercyl;
    	int ss = dp->d_secsize;
    	int trk = dp->d_nsectors;
    	int step;
    	
    	if (buf == (char *)NULL) {
    	    buf = malloc((unsigned)(trk*ss));
    	    if (buf == (char *)NULL) {
    		fprintf(stderr, "Out of memory\n");
    		exit(20);
    	    }
    	}

    	printf("Starting scan of /dev/%sc at cylinder %d\n",
    	 devname, bstart/spc);
    	/* seek to start of parition c, we are in d */
    	for (tries = 0; tries < RETRIES; tries++) {
    	    if (lseek(f, bstart * ss, L_SET) < 0) {
    		Perror("lseek");
    	    } else
    		break;
    	}
    	step = trk;
    	for (curr_sec = bstart; curr_sec < bend; curr_sec += step) {
    	    int gotone = 0;

    	    if (verbose) {
    		if ((curr_sec % spc) == 0)
    		    printf("\r%4d(%7d)", curr_sec/spc, curr_sec);
    	    }
    	    for (tries = 0; tries < RETRIES; tries++) {
    		if (lseek(f, curr_sec * ss, L_SET) < 0) {
fprintf(stderr, "\nbad144: can't seek sector, %d\n", curr_sec);
    		    gotone = 1;
    		} else
    		    break;
    	    }
    	    if (gotone) {
fprintf(stderr, "\nbad144: bad sector (seek), %d\n", curr_sec);
    		step = 1;
    		continue;
    	    }
    	    if (step == trk) {
    		if ((n = read(f, buf, (ss * trk))) == (ss * trk)) {
    		    continue;
    		}
    	    }
    	    /* switch to single sector reads */
    	    lseek(f, curr_sec * ss, L_SET);
    	    step = 1;
    	    for (tries = 0; tries < RETRIES; tries++) {
    		if ((n = read(f, buf, ss)) != ss) {
fprintf(stderr, "\nbad144: can't read sector, %d\n", curr_sec);
    		    gotone = 1;
    		    lseek(f, curr_sec * ss, L_SET);
    		} else {
    		    if ((curr_sec % trk) == 0) {
    			step = trk;
    		    }
    		    break;
    		}
    	    }
    	    if (gotone) {
fprintf(stderr, "\nbad144: bad sector (read), %d\n", curr_sec);
    		continue;
    	    }
    	}
    }
	argc--;
	argv++;
	if (argc == 0) {
		sn = getold(f, &oldbad);
		printf("\nbad block information at sector %d in %s:\n",
		    sn, name);
		switch (oldbad.bt_flag) {

		case (u_short)-1:
			printf("alignment cartridge\n");
			break;

		case DKBAD_MAGIC:
			break;

		default:
			printf("bt_flag=%x(16)?\n", oldbad.bt_flag);
			break;
		}
		bt = oldbad.bt_bad;
		for (i = 0; i < 126; i++) {
			bad = (bt->bt_cyl<<16) + bt->bt_trksec;
			if (bad < 0)
				break;
			printf("sn=%d, cn=%d, tn=%d, sn=%d\n", badsn(bt),
			    bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec&0xff);
			bt++;
		}
		(void) checkold(&oldbad);
		exit(0);
	}
	if (add) {
		/*
		 * Read in the old badsector table.
		 * Verify that it makes sense, and the bad sectors
		 * are in order.  Copy the old table to the new one.
		 */
		(void) getold(f, &oldbad);
		i = checkold(&oldbad);
		if (verbose)
			printf("Had %d bad sectors, adding %d\n", i, argc);
		if (i + argc > 126) {
			printf("bad144: not enough room for %d more sectors\n",
				argc);
			printf("limited to 126 by information format\n");
			exit(1);
		}
		curbad = oldbad;
	} else {
		curbad.bt_csn = atoi(*argv++);
		argc--;
		curbad.bt_mbz = 0;
		curbad.bt_flag = DKBAD_MAGIC;
		if (argc > 126) {
			printf("bad144: too many bad sectors specified\n");
			printf("limited to 126 by information format\n");
			exit(1);
		}
		i = 0;
	}
	errs = 0;
	new = argc;
	while (argc > 0) {
		daddr_t sn = atoi(*argv++);
		argc--;
		if (sn < 0 || sn >= size) {
			printf("%d: out of range [0,%d) for disk %s\n",
			    sn, size, dp->d_typename);
			errs++;
			continue;
		}
		bn[i] = sn;
		curbad.bt_bad[i].bt_cyl = sn / (dp->d_nsectors*dp->d_ntracks);
		sn %= (dp->d_nsectors*dp->d_ntracks);
		curbad.bt_bad[i].bt_trksec =
		    ((sn/dp->d_nsectors) << 8) + (sn%dp->d_nsectors);
		i++;
	}
	if (errs)
		exit(1);
	nbad = i;
	while (i < 126) {
		curbad.bt_bad[i].bt_trksec = -1;
		curbad.bt_bad[i].bt_cyl = -1;
		i++;
	}
	if (add) {
		/*
		 * Sort the new bad sectors into the list.
		 * Then shuffle the replacement sectors so that
		 * the previous bad sectors get the same replacement data.
		 */
		qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad),
		    compare);
		if (dups) {
			fprintf(stderr,
"bad144: bad sectors have been duplicated; can't add existing sectors\n");
			exit(3);
		}
		shift(f, nbad, nbad-new);
	}
	if (badfile == -1)
		i = 0;
	else
		i = badfile * 2;
	for (; i < 10 && i < dp->d_nsectors; i += 2) {
		if (lseek(f, dp->d_secsize * (size - dp->d_nsectors + i),
		    L_SET) < 0)
			Perror("lseek");
		if (verbose)
			printf("write badsect file at %d\n",
				size - dp->d_nsectors + i);
		if (nflag == 0 && write(f, (caddr_t)&curbad, sizeof(curbad)) !=
		    sizeof(curbad)) {
			char msg[80];
			(void)sprintf(msg, "bad144: write bad sector file %d",
			    i/2);
			perror(msg);
		}
		if (badfile != -1)
			break;
	}
#ifdef DIOCSBAD
	if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0)
		fprintf(stderr,
	"Can't sync bad-sector file; reboot for changes to take effect\n");
#endif
	if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) {
		dp->d_flags |= D_BADSECT;
		if (ioctl(f, DIOCWDINFO, dp) < 0) {
			perror("label");
			fprintf(stderr, "Can't write disklabel to enable bad secctor handling by the drive\n");
			exit(1);
		}
	}
	exit(0);
}

daddr_t
getold(f, bad)
struct dkbad *bad;
{
	register int i;
	daddr_t sn;
	char msg[80];

	if (badfile == -1)
		i = 0;
	else
		i = badfile * 2;
	for (; i < 10 && i < dp->d_nsectors; i += 2) {
		sn = size - dp->d_nsectors + i;
		if (lseek(f, sn * dp->d_secsize, L_SET) < 0)
			Perror("lseek");
		if (read(f, (char *) bad, dp->d_secsize) == dp->d_secsize) {
			if (i > 0)
				printf("Using bad-sector file %d\n", i/2);
			return(sn);
		}
		(void)sprintf(msg, "bad144: read bad sector file at sn %d", sn);
		perror(msg);
		if (badfile != -1)
			break;
	}
	fprintf(stderr, "bad144: %s: can't read bad block info\n", name);
	exit(1);
	/*NOTREACHED*/
}

checkold()
{
	register int i;
	register struct bt_bad *bt;
	daddr_t sn, lsn;
	int errors = 0, warned = 0;

	if (oldbad.bt_flag != DKBAD_MAGIC) {
		fprintf(stderr, "bad144: %s: bad flag in bad-sector table\n",
			name);
		errors++;
	}
	if (oldbad.bt_mbz != 0) {
		fprintf(stderr, "bad144: %s: bad magic number\n", name);
		errors++;
	}
	bt = oldbad.bt_bad;
	for (i = 0; i < 126; i++, bt++) {
		if (bt->bt_cyl == 0xffff && bt->bt_trksec == 0xffff)
			break;
		if ((bt->bt_cyl >= dp->d_ncylinders) ||
		    ((bt->bt_trksec >> 8) >= dp->d_ntracks) ||
		    ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) {
			fprintf(stderr,
		     "bad144: cyl/trk/sect out of range in existing entry: ");
			fprintf(stderr, "sn=%d, cn=%d, tn=%d, sn=%d\n",
				badsn(bt), bt->bt_cyl, bt->bt_trksec>>8,
				bt->bt_trksec & 0xff);
			errors++;
		}
		sn = (bt->bt_cyl * dp->d_ntracks +
		    (bt->bt_trksec >> 8)) *
		    dp->d_nsectors + (bt->bt_trksec & 0xff);
		if (i > 0 && sn < lsn && !warned) {
		    fprintf(stderr,
			"bad144: bad sector file is out of order\n");
		    errors++;
		    warned++;
		}
		if (i > 0 && sn == lsn) {
		    fprintf(stderr,
			"bad144: bad sector file contains duplicates (sn %d)\n",
			sn);
		    errors++;
		}
		lsn = sn;
	}
	if (errors)
		exit(1);
	return (i);
}

/*
 * Move the bad sector replacements
 * to make room for the new bad sectors.
 * new is the new number of bad sectors, old is the previous count.
 */
shift(f, new, old)
{
	daddr_t repl;

	/*
	 * First replacement is last sector of second-to-last track.
	 */
	repl = size - dp->d_nsectors - 1;
	new--; old--;
	while (new >= 0 && new != old) {
		if (old < 0 ||
		    compare(&curbad.bt_bad[new], &oldbad.bt_bad[old]) > 0) {
			/*
			 * Insert new replacement here-- copy original
			 * sector if requested and possible,
			 * otherwise write a zero block.
			 */
			if (!copy ||
			    !blkcopy(f, badsn(&curbad.bt_bad[new]), repl - new))
				blkzero(f, repl - new);
		} else {
			if (blkcopy(f, repl - old, repl - new) == 0)
			    fprintf(stderr,
				"Can't copy replacement sector %d to %d\n",
				repl-old, repl-new);
			old--;
		}
		new--;
	}
}

/*
 *  Copy disk sector s1 to s2.
 */
blkcopy(f, s1, s2)
daddr_t s1, s2;
{
	register tries, n;

	if (buf == (char *)NULL) {
		buf = malloc((unsigned)dp->d_secsize);
		if (buf == (char *)NULL) {
			fprintf(stderr, "Out of memory\n");
			exit(20);
		}
	}
	for (tries = 0; tries < RETRIES; tries++) {
		if (lseek(f, dp->d_secsize * s1, L_SET) < 0)
			Perror("lseek");
		if ((n = read(f, buf, dp->d_secsize)) == dp->d_secsize)
			break;
	}
	if (n != dp->d_secsize) {
		fprintf(stderr, "bad144: can't read sector, %d: ", s1);
		if (n < 0)
			perror((char *)0);
		return(0);
	}
	if (lseek(f, dp->d_secsize * s2, L_SET) < 0)
		Perror("lseek");
	if (verbose)
		printf("copying %d to %d\n", s1, s2);
	if (nflag == 0 && write(f, buf, dp->d_secsize) != dp->d_secsize) {
		fprintf(stderr,
		    "bad144: can't write replacement sector, %d: ", s2);
		perror((char *)0);
		return(0);
	}
	return(1);
}

char *zbuf;

blkzero(f, sn)
daddr_t sn;
{

	if (zbuf == (char *)NULL) {
		zbuf = malloc((unsigned)dp->d_secsize);
		if (zbuf == (char *)NULL) {
			fprintf(stderr, "Out of memory\n");
			exit(20);
		}
	}
	if (lseek(f, dp->d_secsize * sn, L_SET) < 0)
		Perror("lseek");
	if (verbose)
		printf("zeroing %d\n", sn);
	if (nflag == 0 && write(f, zbuf, dp->d_secsize) != dp->d_secsize) {
		fprintf(stderr,
		    "bad144: can't write replacement sector, %d: ", sn);
		perror((char *)0);
	}
}

compare(b1, b2)
register struct bt_bad *b1, *b2;
{
	if (b1->bt_cyl > b2->bt_cyl)
		return(1);
	if (b1->bt_cyl < b2->bt_cyl)
		return(-1);
	if (b1->bt_trksec == b2->bt_trksec)
		dups++;
	return (b1->bt_trksec - b2->bt_trksec);
}

daddr_t
badsn(bt)
register struct bt_bad *bt;
{
	return ((bt->bt_cyl*dp->d_ntracks + (bt->bt_trksec>>8)) * dp->d_nsectors
		+ (bt->bt_trksec&0xff));
}

Perror(op)
	char *op;
{

	fprintf(stderr, "bad144: "); perror(op);
	exit(4);
}

---- snip snip ----

-- 
+=+     From the mountains of Colorado:   jbev@jbsys.com      +=+
+=+           when it's not snowing, the sun is shining       +=+
+=+     Jim Bevier    -   J B Systems   (303)-697-1038        +=+
+=+         10136 Horizon View, Morrison, CO 80465            +=+