xref: /csrg-svn/sys/vax/stand/format.c (revision 33648)
123251Smckusick /*
229297Smckusick  * Copyright (c) 1980, 1986 Regents of the University of California.
323251Smckusick  * All rights reserved.  The Berkeley software License Agreement
423251Smckusick  * specifies the terms and conditions for redistribution.
523251Smckusick  */
611367Ssam 
723251Smckusick #ifndef lint
823251Smckusick char copyright[] =
929297Smckusick "@(#) Copyright (c) 1980, 1986 Regents of the University of California.\n\
1023251Smckusick  All rights reserved.\n";
11*33648Sbostic #endif /* not lint */
1215051Skarels 
1323251Smckusick #ifndef lint
14*33648Sbostic static char sccsid[] = "@(#)format.c	7.3 (Berkeley) 03/04/88";
15*33648Sbostic #endif /* not lint */
1623251Smckusick 
1711367Ssam /*
1811367Ssam  * Standalone program to do media checking
1911367Ssam  * and record bad block information on any
2015009Skarels  * disk with the appropriate driver and RM03-style headers.
2125212Skarels  * TODO:
2225212Skarels  *	add new bad sectors to bad-sector table when formatting by track
2325212Skarels  *		(rearranging replacements ala bad144 -a)
2425212Skarels  *	multi-pass format for disks with skip-sector capability
2511367Ssam  */
2633553Skarels #include "param.h"
2733553Skarels #include "fs.h"
2833553Skarels #include "inode.h"
2933553Skarels #include "dkbad.h"
3033553Skarels #include "vmmac.h"
31*33648Sbostic #include "disklabel.h"
3211367Ssam 
33*33648Sbostic #include "../vax/cpu.h"
34*33648Sbostic #include "../vax/mtpr.h"
35*33648Sbostic 
3611367Ssam #include "saio.h"
3711367Ssam #include "savax.h"
3811367Ssam 
3911367Ssam #define MAXBADDESC	126		/* size of bad block table */
4011367Ssam #define CHUNK		48		/* max # of sectors/io operation */
4111367Ssam #define SECTSIZ		512		/* standard sector size */
4211367Ssam #define HDRSIZ		4		/* number of bytes in sector header */
4311367Ssam 
4411367Ssam #define SSERR		0
4511367Ssam #define BSERR		1
4611367Ssam 
4725438Skarels #define SSDEV(fd)	(ioctl((fd), SAIOSSDEV, (char *)0) == 0)
4825438Skarels #define MAXECCBITS	3
4911367Ssam 
5011367Ssam struct sector {
5111367Ssam 	u_short	header1;
5211367Ssam 	u_short header2;
5311367Ssam 	char	buf[SECTSIZ];
5411367Ssam };
5511367Ssam 
5611367Ssam struct	dkbad dkbad;		/* bad sector table */
5725438Skarels struct	dkbad oldbad;		/* old bad sector table */
5811367Ssam struct	dkbad sstab;		/* skip sector table */
5911367Ssam 
6011367Ssam #define	NERRORS		6
6111371Ssam static char *
6211371Ssam errornames[NERRORS] = {
6315009Skarels #define	FE_BSE		0
6415009Skarels 	"Bad sector",
6515009Skarels #define	FE_WCE		1
6611367Ssam 	"Write check",
6711367Ssam #define	FE_ECC		2
6825438Skarels 	"Hard ECC",
6911367Ssam #define	FE_HARD		3
7011367Ssam 	"Other hard",
7111367Ssam #define	FE_TOTAL	4
7225438Skarels 	"Marked bad",
7311367Ssam #define	FE_SSE		5
7425438Skarels 	"Skipped",
7511367Ssam };
7611367Ssam 
7711367Ssam int	errors[NERRORS];	/* histogram of errors */
7811371Ssam int	pattern;
7925438Skarels int	maxeccbits;
8011367Ssam 
8115051Skarels /*
8215051Skarels  * Purdue/EE severe burnin patterns.
8315051Skarels  */
8415051Skarels unsigned short ppat[] = {
8517426Skarels 0xf00f, 0xec6d, 0031463,0070707,0133333,0155555,0161616,0143434,
8615051Skarels 0107070,0016161,0034343,0044444,0022222,0111111,0125252, 052525,
8715051Skarels 0125252,0125252,0125252,0125252,0125252,0125252,0125252,0125252,
8815051Skarels #ifndef	SHORTPASS
8915051Skarels 0125252,0125252,0125252,0125252,0125252,0125252,0125252,0125252,
9015051Skarels  052525, 052525, 052525, 052525, 052525, 052525, 052525, 052525,
9115051Skarels #endif
9215051Skarels  052525, 052525, 052525, 052525, 052525, 052525, 052525, 052525
9315051Skarels  };
9415051Skarels 
9515051Skarels #define	NPT	(sizeof (ppat) / sizeof (short))
9617426Skarels int	maxpass, npat;	/* subscript to ppat[] */
9715051Skarels int	severe;		/* nz if running "severe" burnin */
9825438Skarels int	ssdev;			/* device supports skip sectors */
9925212Skarels int	startcyl, endcyl, starttrack, endtrack;
10025438Skarels int	nbads;			/* subscript for bads */
10125438Skarels daddr_t	bads[2*MAXBADDESC]; 	/* Bad blocks accumulated */
10215051Skarels 
10311367Ssam char	*malloc();
10415051Skarels int	qcompar();
10511367Ssam char	*prompt();
10625212Skarels daddr_t	badsn();
10711367Ssam extern	int end;
10811367Ssam 
10911367Ssam main()
11011367Ssam {
111*33648Sbostic 	register struct sector *hdr;
112*33648Sbostic 	register int sector, sn, i;
113*33648Sbostic 	struct disklabel dl;
114*33648Sbostic 	struct sector *bp, *cbp;
11515051Skarels 	int lastsector, tracksize, rtracksize;
116*33648Sbostic 	int unit, fd, resid, trk, cyl, debug, pass;
117*33648Sbostic 	char *cp, *rbp, *rcbp;
11811367Ssam 
11911367Ssam 	printf("Disk format/check utility\n\n");
12011367Ssam 
121*33648Sbostic 	/* enable the cache, as every little bit helps */
122*33648Sbostic 	switch (cpu) {
123*33648Sbostic 	case VAX_8600:
124*33648Sbostic 		mtpr(CSWP, 3);
125*33648Sbostic 		break;
126*33648Sbostic 	case VAX_8200:
127*33648Sbostic 	case VAX_750:
128*33648Sbostic 		mtpr(CADR, 0);
129*33648Sbostic 		break;
130*33648Sbostic 	case VAX_780:
131*33648Sbostic 		mtpr(SBIMT, 0x200000);
132*33648Sbostic 		break;
133*33648Sbostic 	}
134*33648Sbostic 
13511367Ssam again:
13615051Skarels 	nbads = 0;
13715051Skarels 	cp = prompt("Enable debugging (0=none, 1=bse, 2=ecc, 3=bse+ecc)? ");
13811367Ssam 	debug = atoi(cp);
13911367Ssam 	if (debug < 0)
14011367Ssam 		debug = 0;
14111367Ssam 	for (i = 0; i < NERRORS; i++)
14211367Ssam 		errors[i] = 0;
14311367Ssam 	fd = getdevice();
144*33648Sbostic 	ioctl(fd, SAIODEVDATA, &dl);
14511367Ssam 	printf("Device data: #cylinders=%d, #tracks=%d, #sectors=%d\n",
146*33648Sbostic 	    dl.d_ncylinders, dl.d_ntracks, dl.d_nsectors);
14725438Skarels 	ssdev = SSDEV(fd);
14825438Skarels 	if (ssdev) {
14925438Skarels 		ioctl(fd, SAIOSSI, (char *)0);	/* set skip sector inhibit */
150*33648Sbostic 		dl.d_nsectors++;
151*33648Sbostic 		dl.d_secpercyl += dl.d_ntracks;
15225438Skarels 		printf("(not counting skip-sector replacement)\n");
15325438Skarels 	}
154*33648Sbostic 	getrange(&dl);
15511371Ssam 	if (getpattern())
15611367Ssam 		goto again;
15711367Ssam 	printf("Start formatting...make sure the drive is online\n");
15811367Ssam 	ioctl(fd, SAIONOBAD, (char *)0);
15925438Skarels 	ioctl(fd, SAIORETRIES, (char *)0);
16025438Skarels 	ioctl(fd, SAIOECCLIM, (char *)maxeccbits);
16111367Ssam 	ioctl(fd, SAIODEBUG, (char *)debug);
162*33648Sbostic 	tracksize = sizeof (struct sector) * dl.d_nsectors;
163*33648Sbostic 	rtracksize = SECTSIZ * dl.d_nsectors;
16411367Ssam 	bp = (struct sector *)malloc(tracksize);
16515051Skarels 	rbp = malloc(rtracksize);
16617426Skarels 	pass = 0;
16717426Skarels 	npat = 0;
16817426Skarels more:
16917426Skarels 	for (; pass < maxpass; pass++) {
17015051Skarels 		if (severe)
17115051Skarels 			printf("Begin pass %d\n", pass);
17215051Skarels 		bufinit(bp, tracksize);
17315051Skarels 		if (severe)
17415051Skarels 			npat++;
17511367Ssam 		/*
17615051Skarels 		 * Begin check, for each track,
17711367Ssam 		 *
17815051Skarels 		 * 1) Write header and test pattern.
17915051Skarels 		 * 2) Read data.  Hardware checks header and data ECC.
18017426Skarels 		 *    Read data (esp on Eagles) is much faster than write check.
18111367Ssam 		 */
182*33648Sbostic 		sector = ((startcyl * dl.d_ntracks) + starttrack) *
183*33648Sbostic 			dl.d_nsectors;
184*33648Sbostic 		lastsector = ((endcyl * dl.d_ntracks) + endtrack) *
185*33648Sbostic 			dl.d_nsectors + dl.d_nsectors;
186*33648Sbostic 		for ( ; sector < lastsector; sector += dl.d_nsectors) {
187*33648Sbostic 			cyl = sector / dl.d_secpercyl;
188*33648Sbostic 			trk = ((sector % dl.d_secpercyl) / dl.d_nsectors) << 8;
189*33648Sbostic 			for (i = 0, hdr = bp; i < dl.d_nsectors; i++, hdr++) {
190*33648Sbostic 				hdr->header1 = cyl | HDR1_FMT22 | HDR1_OKSCT;
191*33648Sbostic 				hdr->header2 = trk + i;
19215051Skarels 			}
193*33648Sbostic 			if (sector && (sector % (dl.d_secpercyl * 50)) == 0)
19415051Skarels 				printf("cylinder %d\n", cyl);
19515051Skarels 			/*
19615051Skarels 			 * Try and write the headers and data patterns into
19715051Skarels 			 * each sector in the track.  Continue until such
19815051Skarels 			 * we're done, or until there's less than a sector's
19915051Skarels 			 * worth of data to transfer.
20015051Skarels 			 *
20115051Skarels 			 * The lseek call is necessary because of
20215051Skarels 			 * the odd sector size (516 bytes)
20315051Skarels 			 */
20415051Skarels 			for (resid = tracksize, cbp = bp, sn = sector;;) {
205*33648Sbostic 				register int cc;
20611367Ssam 
20733553Skarels 				lseek(fd, sn * SECTSIZ, L_SET);
20815051Skarels 				ioctl(fd, SAIOHDR, (char *)0);
20915051Skarels 				cc = write(fd, cbp, resid);
21015051Skarels 				if (cc == resid)
21115051Skarels 					break;
21215051Skarels 				/*
21315051Skarels 				 * Don't record errors during write,
21415051Skarels 				 * all errors will be found during
21525438Skarels 				 * check performed below.
21615051Skarels 				 */
21715051Skarels 				sn = iob[fd - 3].i_errblk;
21815051Skarels 				cbp += sn - sector;
21915051Skarels 				resid -= (sn - sector) * sizeof (struct sector);
22015051Skarels 				if (resid < sizeof (struct sector))
22115051Skarels 					break;
22215051Skarels 			}
22311367Ssam 			/*
22415051Skarels 			 * Read test patterns.
22515051Skarels 			 * Retry remainder of track on error until
22615051Skarels 			 * we're done, or until there's less than a
22715051Skarels 			 * sector to verify.
22811367Ssam 			 */
22915051Skarels 			for (resid = rtracksize, rcbp = rbp, sn = sector;;) {
230*33648Sbostic 				register int cc, rsn;
23111367Ssam 
23233553Skarels 				lseek(fd, sn * SECTSIZ, L_SET);
23315051Skarels 				cc = read(fd, rcbp, resid);
23415051Skarels 				if (cc == resid)
23515051Skarels 					break;
23615051Skarels 				sn = iob[fd-3].i_errblk;
23725438Skarels 				if (ssdev) {
238*33648Sbostic 					rsn = sn - (sn / dl.d_nsectors);
23925438Skarels 					printf("data ");
24025438Skarels 				} else
24125438Skarels 					rsn = sn;
24225438Skarels 				printf("sector %d, read error\n\n", rsn);
243*33648Sbostic 				if (recorderror(fd, sn, &dl) < 0 && pass > 0)
24415051Skarels 					goto out;
24515051Skarels 				/* advance past bad sector */
24615051Skarels 				sn++;
24725438Skarels 				resid = rtracksize - ((sn - sector) * SECTSIZ);
24825438Skarels 				rcbp = rbp + ((sn - sector) * SECTSIZ);
24915051Skarels 				if (resid < SECTSIZ)
25015051Skarels 					break;
25115051Skarels 			}
25211367Ssam 		}
25311367Ssam 	}
25411367Ssam 	/*
25511367Ssam 	 * Checking finished.
25611367Ssam 	 */
25715051Skarels out:
25817426Skarels 	if (severe && maxpass < NPT) {
25917426Skarels 		cp = prompt("More passes? (0 or number) ");
26017426Skarels 		maxpass = atoi(cp);
26117426Skarels 		if (maxpass > 0) {
26217426Skarels 			maxpass += pass;
26317426Skarels 			goto more;
26417426Skarels 		}
26517426Skarels 	}
26615051Skarels 	if (severe && nbads) {
26715051Skarels 		/*
26815051Skarels 		 * Sort bads and insert in bad block table.
26915051Skarels 		 */
27025438Skarels 		qsort(bads, nbads, sizeof (daddr_t), qcompar);
27115051Skarels 		severe = 0;
27217426Skarels 		errno = 0;
27317426Skarels 		for (i = 0; i < nbads; i++)
274*33648Sbostic 			recorderror(fd, bads[i], &dl);
27515051Skarels 		severe++;
27615051Skarels 	}
27711367Ssam 	if (errors[FE_TOTAL] || errors[FE_SSE]) {
27811367Ssam 		/* change the headers of all the bad sectors */
279*33648Sbostic 		writebb(fd, errors[FE_SSE], &sstab, &dl, SSERR);
280*33648Sbostic 		writebb(fd, errors[FE_TOTAL], &dkbad, &dl, BSERR);
28111367Ssam 	}
28225438Skarels 	if (errors[FE_TOTAL] || errors[FE_SSE]) {
28325438Skarels 		printf("Errors:\n");
28425438Skarels 		for (i = 0; i < NERRORS; i++)
28525438Skarels 			printf("%s: %d\n", errornames[i], errors[i]);
28625438Skarels 		printf("Total of %d hard errors revectored\n",
28725438Skarels 			errors[FE_TOTAL] + errors[FE_SSE]);
28825438Skarels 	}
289*33648Sbostic 	if (endcyl == dl.d_ncylinders - 1 &&
290*33648Sbostic 	    (startcyl < dl.d_ncylinders - 1 || starttrack == 0)) {
29125212Skarels 		while (errors[FE_TOTAL] < MAXBADDESC) {
29225212Skarels 			int i = errors[FE_TOTAL]++;
29311367Ssam 
29425212Skarels 			dkbad.bt_bad[i].bt_cyl = -1;
29525212Skarels 			dkbad.bt_bad[i].bt_trksec = -1;
29625212Skarels 		}
29725212Skarels 		printf("\nWriting bad sector table at sector #%d\n",
298*33648Sbostic 			dl.d_ncylinders * dl.d_secpercyl - dl.d_nsectors);
29925212Skarels 		/* place on disk */
300*33648Sbostic 		for (i = 0; i < 10 && i < dl.d_nsectors; i += 2) {
301*33648Sbostic 			lseek(fd, SECTSIZ * (dl.d_ncylinders *
302*33648Sbostic 				dl.d_secpercyl - dl.d_nsectors + i), 0);
30325212Skarels 			write(fd, &dkbad, sizeof (dkbad));
30425212Skarels 		}
30525212Skarels 	} else if (errors[FE_TOTAL]) {
30625212Skarels 		struct bt_bad *bt;
30725212Skarels 
30825212Skarels 		printf("New bad sectors (not added to table):\n");
30925212Skarels 		bt = dkbad.bt_bad;
31025212Skarels 		for (i = 0; i < errors[FE_TOTAL]; i++) {
311*33648Sbostic 			printf("bn %d (cn=%d, tn=%d, sn=%d)\n", badsn(bt, &dl),
31225212Skarels 			    bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec&0xff);
31325212Skarels 			bt++;
31425212Skarels 		}
31511367Ssam 	}
31611367Ssam 	printf("Done\n");
31711367Ssam 	ioctl(fd,SAIONOSSI,(char *)0);
31811367Ssam 	close(fd);
31911367Ssam #ifndef JUSTEXIT
32011367Ssam 	goto again;
32111367Ssam #endif
32211367Ssam }
32311367Ssam 
32415051Skarels qcompar(l1, l2)
32525438Skarels register daddr_t *l1, *l2;
32615051Skarels {
32715051Skarels 	if (*l1 < *l2)
32815051Skarels 		return(-1);
32915051Skarels 	if (*l1 == *l2)
33015051Skarels 		return(0);
33115051Skarels 	return(1);
33215051Skarels }
33315051Skarels 
33425212Skarels daddr_t
335*33648Sbostic badsn(bt, lp)
33625212Skarels 	register struct bt_bad *bt;
337*33648Sbostic 	register struct disklabel *lp;
33825212Skarels {
339*33648Sbostic 	register int ssoff = ssdev ? 1 : 0;
34025438Skarels 
341*33648Sbostic 	return ((bt->bt_cyl * lp->d_ntracks + (bt->bt_trksec >> 8)) *
342*33648Sbostic 		(lp->d_nsectors - ssoff) + (bt->bt_trksec & 0xff) - ssoff);
34325212Skarels }
34425212Skarels 
34511367Ssam /*
34625212Skarels  * Mark the bad/skipped sectors.
34725438Skarels  * Bad sectors on skip-sector devices are assumed to be skipped also,
34825438Skarels  * and must be done after the (earlier) first skipped sector.
34911367Ssam  */
350*33648Sbostic writebb(fd, nsects, dbad, lp, sw)
35111367Ssam 	int nsects, fd;
35211367Ssam 	struct dkbad *dbad;
353*33648Sbostic 	register struct disklabel *lp;
35411367Ssam {
35511367Ssam 	struct sector bb_buf; /* buffer for one sector plus 4 byte header */
35611367Ssam 	register int i;
35711367Ssam 	int bn, j;
35811367Ssam 	struct bt_bad *btp;
35911367Ssam 
36011367Ssam 	for (i = 0; i < nsects; i++) {
36111367Ssam 		btp = &dbad->bt_bad[i];
36211367Ssam 		if (sw == BSERR) {
36311367Ssam 			bb_buf.header1 = HDR1_FMT22|btp->bt_cyl;
36425438Skarels 			if (ssdev)
36511367Ssam 				bb_buf.header1 |= HDR1_SSF;
36611367Ssam 		} else
36711367Ssam 			bb_buf.header1 =
36811367Ssam 			       btp->bt_cyl | HDR1_FMT22 | HDR1_SSF | HDR1_OKSCT;
36911367Ssam 		bb_buf.header2 = btp->bt_trksec;
370*33648Sbostic 		bn = lp->d_secpercyl * btp->bt_cyl +
371*33648Sbostic 		     lp->d_nsectors * (btp->bt_trksec >> 8) +
37215009Skarels 		     (btp->bt_trksec & 0xff);
37333553Skarels 		lseek(fd, bn * SECTSIZ, L_SET);
37411367Ssam 		ioctl(fd, SAIOHDR, (char *)0);
37511367Ssam 		write(fd, &bb_buf, sizeof (bb_buf));
37611367Ssam 		/*
37711367Ssam 		 * If skip sector, mark all remaining
37811367Ssam 		 * sectors on the track.
37911367Ssam 		 */
38025438Skarels 		if (sw == SSERR) {
38125438Skarels 			for (j = (btp->bt_trksec & 0xff) + 1, bn++;
382*33648Sbostic 			    j < lp->d_nsectors; j++, bn++) {
38325438Skarels 				bb_buf.header2 = j | (btp->bt_trksec & 0xff00);
38433553Skarels 				lseek(fd, bn * SECTSIZ, L_SET);
38525438Skarels 				ioctl(fd, SAIOHDR, (char *)0);
38625438Skarels 				write(fd, &bb_buf, sizeof (bb_buf));
38725438Skarels 			}
38811367Ssam 		}
38911367Ssam 	}
39011367Ssam }
39111367Ssam 
39211367Ssam /*
39311367Ssam  * Record an error, and if there's room, put
39411367Ssam  * it in the appropriate bad sector table.
39515051Skarels  *
39615051Skarels  * If severe burnin store block in a list after making sure
39715051Skarels  * we have not already found it on a prev pass.
39811367Ssam  */
399*33648Sbostic recorderror(fd, bn, lp)
40011367Ssam 	int fd, bn;
401*33648Sbostic 	register struct disklabel *lp;
40211367Ssam {
40325212Skarels 	int cn, tn, sn;
404*33648Sbostic 	register int i;
40511367Ssam 
40615051Skarels 	if (severe) {
40715051Skarels 		for (i = 0; i < nbads; i++)
40815051Skarels 			if (bads[i] == bn)
40915051Skarels 				return(0);	/* bn already flagged */
41025438Skarels 		if (nbads >= (ssdev ? 2 * MAXBADDESC : MAXBADDESC)) {
41125212Skarels 			printf("Bad sector table full, format terminating\n");
41215051Skarels 			return(-1);
41315051Skarels 		}
41415051Skarels 		bads[nbads++] = bn;
41517426Skarels 		if (errno < EBSE || errno > EHER)
41617426Skarels 			return(0);
41717426Skarels 		errno -= EBSE;
41817426Skarels 		errors[errno]++;
41915051Skarels 		return(0);
42015051Skarels 	}
42117426Skarels 	if (errno >= EBSE && errno <= EHER) {
42217426Skarels 		errno -= EBSE;
42317426Skarels 		errors[errno]++;
42411367Ssam 	}
425*33648Sbostic 	cn = bn / lp->d_secpercyl;
426*33648Sbostic 	sn = bn % lp->d_secpercyl;
427*33648Sbostic 	tn = sn / lp->d_nsectors;
428*33648Sbostic 	sn %= lp->d_nsectors;
42925438Skarels 	if (ssdev) {		/* if drive has skip sector capability */
43025212Skarels 		int ss = errors[FE_SSE];
43111367Ssam 
43225212Skarels 		if (errors[FE_SSE] >= MAXBADDESC) {
43325212Skarels 			/* this is bogus, we don't maintain skip sector table */
43425212Skarels 			printf("Too many skip sector errors\n");
43525212Skarels 			return(-1);
43625212Skarels 		}
43725212Skarels 		  /* only one skip sector/track */
43825212Skarels 		if (ss == 0 ||
43925212Skarels 		    tn != (sstab.bt_bad[ss - 1].bt_trksec >> 8) ||
44025212Skarels 		    cn != sstab.bt_bad[ss - 1].bt_cyl) {
44125438Skarels 			/*
44225438Skarels 			 * Don't bother with skipping the extra sector
44325438Skarels 			 * at the end of the track.
44425438Skarels 			 */
445*33648Sbostic 			if (sn == lp->d_nsectors - 1)
44625438Skarels 				return(0);
44711367Ssam 			sstab.bt_bad[ss].bt_cyl = cn;
44811367Ssam 			sstab.bt_bad[ss].bt_trksec = (tn<<8) + sn;
44925212Skarels 			errors[FE_SSE]++;
45025212Skarels 			return(0);
45111367Ssam 		}
45211367Ssam 	}
45325212Skarels 	if (errors[FE_TOTAL] >= MAXBADDESC) {
45425212Skarels 		printf("Too many bad sectors\n");
45525212Skarels 		return(-1);
45625212Skarels 	}
45711367Ssam 	/* record the bad sector address and continue */
45815009Skarels 	dkbad.bt_bad[errors[FE_TOTAL]].bt_cyl = cn;
45911367Ssam 	dkbad.bt_bad[errors[FE_TOTAL]++].bt_trksec = (tn << 8) + sn;
46015051Skarels 	return(0);
46111367Ssam }
46211367Ssam 
46311367Ssam /*
46411367Ssam  * Allocate memory on a page-aligned address.
46511367Ssam  * Round allocated chunk to a page multiple to
46611367Ssam  * ease next request.
46711367Ssam  */
46811367Ssam char *
46911367Ssam malloc(size)
47011367Ssam 	int size;
47111367Ssam {
47211367Ssam 	char *result;
47311367Ssam 	static caddr_t last = 0;
47411367Ssam 
47511367Ssam 	if (last == 0)
47611367Ssam 		last = (caddr_t)(((int)&end + 511) & ~0x1ff);
47711367Ssam 	size = (size + 511) & ~0x1ff;
47811367Ssam 	result = (char *)last;
47911367Ssam 	last += size;
48011367Ssam 	return (result);
48111367Ssam }
48211367Ssam 
48311367Ssam /*
48411367Ssam  * Prompt and verify a device name from the user.
48511367Ssam  */
48611367Ssam getdevice()
48711367Ssam {
48811367Ssam 	register char *cp;
48911367Ssam 	int fd;
49011367Ssam 
49111367Ssam top:
49233553Skarels 	do {
49333553Skarels 		cp = prompt("Device to format? ");
49433553Skarels 	} while ((fd = open(cp, 2)) < 0);
49515051Skarels 	printf("Formatting drive %c%c%d on adaptor %d: ",
49615051Skarels 		cp[0], cp[1], iob[fd - 3].i_unit % 8, iob[fd - 3].i_unit / 8);
49711367Ssam 	cp = prompt("verify (yes/no)? ");
49811367Ssam 	while (*cp != 'y' && *cp != 'n')
49911367Ssam 		cp = prompt("Huh, yes or no? ");
50011367Ssam 	if (*cp == 'y')
50111367Ssam 		return (fd);
50211367Ssam 	goto top;
50311367Ssam }
50411367Ssam 
50525212Skarels /*
50625212Skarels  * Find range of tracks to format.
50725212Skarels  */
508*33648Sbostic getrange(lp)
509*33648Sbostic 	register struct disklabel *lp;
51025212Skarels {
511*33648Sbostic 	startcyl = getnum("Starting cylinder", 0, lp->d_ncylinders - 1, 0);
512*33648Sbostic 	starttrack = getnum("Starting track", 0, lp->d_ntracks - 1, 0);
513*33648Sbostic 	endcyl = getnum("Ending cylinder", 0, lp->d_ncylinders - 1,
514*33648Sbostic 		lp->d_ncylinders - 1);
515*33648Sbostic 	endtrack = getnum("Ending track", 0, lp->d_ntracks - 1,
516*33648Sbostic 		lp->d_ntracks - 1);
51725212Skarels }
51825212Skarels 
51925212Skarels getnum(s, low, high, dflt)
520*33648Sbostic 	int s, low, high, dflt;
52125212Skarels {
52225212Skarels 	char buf[132];
523*33648Sbostic 	u_int val;
52425212Skarels 
525*33648Sbostic 	for(;;) {
52625212Skarels 		printf("%s (%d): ", s, dflt);
52725212Skarels 		gets(buf);
52825212Skarels 		if (buf[0] == 0)
52925212Skarels 			return (dflt);
53025212Skarels 		val = atoi(buf);
53125212Skarels 		if (val >= low && val <= high)
53225212Skarels 			return ((int)val);
53325212Skarels 		printf("Value must be in range [%d,%d]\n", low, high);
53425212Skarels 	}
53525212Skarels }
53625212Skarels 
53711371Ssam static struct pattern {
53811371Ssam 	long	pa_value;
53911371Ssam 	char	*pa_name;
54011371Ssam } pat[] = {
54111371Ssam 	{ 0xf00ff00f, 	"RH750 worst case" },
54211371Ssam 	{ 0xec6dec6d,	"media worst case" },
54311371Ssam 	{ 0xa5a5a5a5,	"alternate 1's and 0's" },
54417426Skarels 	{ 0xFFFFFFFF,	"Severe burnin (up to 48 passes)" },
54511371Ssam 	{ 0, 0 },
54611371Ssam };
54711371Ssam 
54811371Ssam getpattern()
54911371Ssam {
55011371Ssam 	register struct pattern *p;
55111371Ssam 	int npatterns;
55211371Ssam 	char *cp;
55311371Ssam 
55411371Ssam 	printf("Available test patterns are:\n");
55511371Ssam 	for (p = pat; p->pa_value; p++)
55611371Ssam 		printf("\t%d - (%x) %s\n", (p - pat) + 1,
55711371Ssam 		  p->pa_value & 0xffff, p->pa_name);
55811371Ssam 	npatterns = p - pat;
55911371Ssam 	cp = prompt("Pattern (one of the above, other to restart)? ");
56011371Ssam 	pattern = atoi(cp) - 1;
56117426Skarels 	if (pattern < 0 || pattern >= npatterns)
56217426Skarels 		return(1);
56315051Skarels 	severe = 0;
56417426Skarels 	maxpass = 1;
56517426Skarels 	if (pat[pattern].pa_value == -1) {
56615051Skarels 		severe = 1;
56717426Skarels 		cp = prompt("How many passes (up to 48)? ");
56817426Skarels 		maxpass = atoi(cp);
56917426Skarels 		if (maxpass > NPT)
57017426Skarels 			maxpass = NPT;
57117426Skarels 	}
57225438Skarels 	maxeccbits = getnum(
57325438Skarels 		"Maximum number of bit errors to allow for soft ECC",
57425438Skarels 		0, 11, MAXECCBITS);
57517426Skarels 	return (0);
57611371Ssam }
57711371Ssam 
57811371Ssam struct xsect {
57911371Ssam 	u_short	hd1;
58011371Ssam 	u_short	hd2;
58111371Ssam 	long	buf[128];
58211371Ssam };
58311371Ssam 
58411371Ssam /*
58511371Ssam  * Initialize the buffer with the requested pattern.
58611371Ssam  */
58711371Ssam bufinit(bp, size)
58811371Ssam 	register struct xsect *bp;
58911371Ssam 	int size;
59011371Ssam {
59111371Ssam 	register struct pattern *pptr;
59211371Ssam 	register long *pp, *last;
59311371Ssam 	register struct xsect *lastbuf;
59415051Skarels 	int patt;
59511371Ssam 
59611371Ssam 	size /= sizeof (struct sector);
59711371Ssam 	lastbuf = bp + size;
59815051Skarels 	if (severe) {
59915051Skarels 		patt = ppat[npat] | ((long)ppat[npat] << 16);
60015051Skarels 		printf("Write pattern 0x%x\n", patt&0xffff);
60115051Skarels 	} else {
60215051Skarels 		pptr = &pat[pattern];
60315051Skarels 		patt = pptr->pa_value;
60415051Skarels 	}
60511371Ssam 	while (bp < lastbuf) {
60611371Ssam 		last = &bp->buf[128];
60711371Ssam 		for (pp = bp->buf; pp < last; pp++)
60815051Skarels 			*pp = patt;
60911371Ssam 		bp++;
61011371Ssam 	}
61111371Ssam }
61211371Ssam 
61311367Ssam char *
61411367Ssam prompt(msg)
61511367Ssam 	char *msg;
61611367Ssam {
61711367Ssam 	static char buf[132];
61811367Ssam 
61911367Ssam 	printf("%s", msg);
62011367Ssam 	gets(buf);
62111367Ssam 	return (buf);
62211367Ssam }
623