Home | History | Annotate | Line # | Download | only in bad144
bad144.c revision 1.1.1.3
      1      1.1      cgd /*
      2  1.1.1.3    mikel  * Copyright (c) 1980, 1986, 1988, 1993
      3  1.1.1.2  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *	This product includes software developed by the University of
     16      1.1      cgd  *	California, Berkeley and its contributors.
     17      1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1      cgd  *    may be used to endorse or promote products derived from this software
     19      1.1      cgd  *    without specific prior written permission.
     20      1.1      cgd  *
     21      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1      cgd  * SUCH DAMAGE.
     32      1.1      cgd  */
     33      1.1      cgd 
     34      1.1      cgd #ifndef lint
     35  1.1.1.2  mycroft static char copyright[] =
     36  1.1.1.3    mikel "@(#) Copyright (c) 1980, 1986, 1988, 1993\n\
     37  1.1.1.2  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1      cgd #endif not lint
     39      1.1      cgd 
     40      1.1      cgd #ifndef lint
     41  1.1.1.3    mikel static char sccsid[] = "@(#)bad144.c	8.2 (Berkeley) 4/27/95";
     42      1.1      cgd #endif not lint
     43      1.1      cgd 
     44      1.1      cgd /*
     45      1.1      cgd  * bad144
     46      1.1      cgd  *
     47      1.1      cgd  * This program prints and/or initializes a bad block record for a pack,
     48      1.1      cgd  * in the format used by the DEC standard 144.
     49      1.1      cgd  * It can also add bad sector(s) to the record, moving the sector
     50      1.1      cgd  * replacements as necessary.
     51      1.1      cgd  *
     52      1.1      cgd  * It is preferable to write the bad information with a standard formatter,
     53      1.1      cgd  * but this program will do.
     54      1.1      cgd  *
     55      1.1      cgd  * RP06 sectors are marked as bad by inverting the format bit in the
     56      1.1      cgd  * header; on other drives the valid-sector bit is cleared.
     57      1.1      cgd  */
     58      1.1      cgd #include <sys/param.h>
     59      1.1      cgd #include <sys/dkbad.h>
     60      1.1      cgd #include <sys/ioctl.h>
     61      1.1      cgd #include <sys/file.h>
     62      1.1      cgd #include <sys/disklabel.h>
     63  1.1.1.2  mycroft #include <ufs/ffs/fs.h>
     64      1.1      cgd 
     65      1.1      cgd #include <stdio.h>
     66      1.1      cgd #include <paths.h>
     67      1.1      cgd 
     68      1.1      cgd #define RETRIES	10		/* number of retries on reading old sectors */
     69      1.1      cgd #define	RAWPART	"c"		/* disk partition containing badsector tables */
     70      1.1      cgd 
     71      1.1      cgd int	fflag, add, copy, verbose, nflag;
     72      1.1      cgd int	compare();
     73      1.1      cgd int	dups;
     74      1.1      cgd int	badfile = -1;		/* copy of badsector table to use, -1 if any */
     75      1.1      cgd #define MAXSECSIZE	1024
     76      1.1      cgd struct	dkbad curbad, oldbad;
     77  1.1.1.2  mycroft #define	DKBAD_MAGIC	0
     78      1.1      cgd 
     79      1.1      cgd char	label[BBSIZE];
     80      1.1      cgd daddr_t	size, getold(), badsn();
     81      1.1      cgd struct	disklabel *dp;
     82      1.1      cgd char	name[BUFSIZ];
     83      1.1      cgd char	*malloc();
     84      1.1      cgd off_t	lseek();
     85      1.1      cgd 
     86      1.1      cgd main(argc, argv)
     87      1.1      cgd 	int argc;
     88      1.1      cgd 	char *argv[];
     89      1.1      cgd {
     90      1.1      cgd 	register struct bt_bad *bt;
     91      1.1      cgd 	daddr_t	sn, bn[126];
     92      1.1      cgd 	int i, f, nbad, new, bad, errs;
     93      1.1      cgd 
     94      1.1      cgd 	argc--, argv++;
     95      1.1      cgd 	while (argc > 0 && **argv == '-') {
     96      1.1      cgd 		(*argv)++;
     97      1.1      cgd 		while (**argv) {
     98      1.1      cgd 			switch (**argv) {
     99      1.1      cgd #if vax
    100      1.1      cgd 			    case 'f':
    101      1.1      cgd 				fflag++;
    102      1.1      cgd 				break;
    103      1.1      cgd #endif
    104      1.1      cgd 			    case 'a':
    105      1.1      cgd 				add++;
    106      1.1      cgd 				break;
    107      1.1      cgd 			    case 'c':
    108      1.1      cgd 				copy++;
    109      1.1      cgd 				break;
    110      1.1      cgd 			    case 'v':
    111      1.1      cgd 				verbose++;
    112      1.1      cgd 				break;
    113      1.1      cgd 			    case 'n':
    114      1.1      cgd 				nflag++;
    115      1.1      cgd 				verbose++;
    116      1.1      cgd 				break;
    117      1.1      cgd 			    default:
    118      1.1      cgd 				if (**argv >= '0' && **argv <= '4') {
    119      1.1      cgd 					badfile = **argv - '0';
    120      1.1      cgd 					break;
    121      1.1      cgd 				}
    122      1.1      cgd 				goto usage;
    123      1.1      cgd 			}
    124      1.1      cgd 			(*argv)++;
    125      1.1      cgd 		}
    126      1.1      cgd 		argc--, argv++;
    127      1.1      cgd 	}
    128      1.1      cgd 	if (argc < 1) {
    129      1.1      cgd usage:
    130      1.1      cgd 		fprintf(stderr,
    131      1.1      cgd 		  "usage: bad144 [ -f ] disk [ snum [ bn ... ] ]\n");
    132      1.1      cgd 		fprintf(stderr,
    133      1.1      cgd 	      "to read or overwrite bad-sector table, e.g.: bad144 hp0\n");
    134      1.1      cgd 		fprintf(stderr,
    135      1.1      cgd 		  "or bad144 -a [ -f ] [ -c ] disk  bn ...\n");
    136      1.1      cgd 		fprintf(stderr, "where options are:\n");
    137      1.1      cgd 		fprintf(stderr, "\t-a  add new bad sectors to the table\n");
    138      1.1      cgd 		fprintf(stderr, "\t-f  reformat listed sectors as bad\n");
    139      1.1      cgd 		fprintf(stderr, "\t-c  copy original sector to replacement\n");
    140      1.1      cgd 		exit(1);
    141      1.1      cgd 	}
    142      1.1      cgd 	if (argv[0][0] != '/')
    143      1.1      cgd 		(void)sprintf(name, "%s/r%s%s", _PATH_DEV, argv[0], RAWPART);
    144      1.1      cgd 	else
    145      1.1      cgd 		strcpy(name, argv[0]);
    146      1.1      cgd 	f = open(name, argc == 1? O_RDONLY : O_RDWR);
    147      1.1      cgd 	if (f < 0)
    148      1.1      cgd 		Perror(name);
    149      1.1      cgd 	if (read(f, label, sizeof(label)) < 0)
    150      1.1      cgd 		Perror("read");
    151      1.1      cgd 	for (dp = (struct disklabel *)(label + LABELOFFSET);
    152      1.1      cgd 	    dp < (struct disklabel *)
    153      1.1      cgd 		(label + sizeof(label) - sizeof(struct disklabel));
    154      1.1      cgd 	    dp = (struct disklabel *)((char *)dp + 64))
    155      1.1      cgd 		if (dp->d_magic == DISKMAGIC && dp->d_magic2 == DISKMAGIC)
    156      1.1      cgd 			break;
    157  1.1.1.2  mycroft 	if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC) {
    158      1.1      cgd 		fprintf(stderr, "Bad pack magic number (pack is unlabeled)\n");
    159      1.1      cgd 		exit(1);
    160      1.1      cgd 	}
    161      1.1      cgd 	if (dp->d_secsize > MAXSECSIZE || dp->d_secsize <= 0) {
    162      1.1      cgd 		fprintf(stderr, "Disk sector size too large/small (%d)\n",
    163      1.1      cgd 			dp->d_secsize);
    164      1.1      cgd 		exit(7);
    165      1.1      cgd 	}
    166      1.1      cgd 	size = dp->d_nsectors * dp->d_ntracks * dp->d_ncylinders;
    167      1.1      cgd 	argc--;
    168      1.1      cgd 	argv++;
    169      1.1      cgd 	if (argc == 0) {
    170      1.1      cgd 		sn = getold(f, &oldbad);
    171      1.1      cgd 		printf("bad block information at sector %d in %s:\n",
    172      1.1      cgd 		    sn, name);
    173      1.1      cgd 		printf("cartridge serial number: %d(10)\n", oldbad.bt_csn);
    174      1.1      cgd 		switch (oldbad.bt_flag) {
    175      1.1      cgd 
    176      1.1      cgd 		case (u_short)-1:
    177      1.1      cgd 			printf("alignment cartridge\n");
    178      1.1      cgd 			break;
    179      1.1      cgd 
    180      1.1      cgd 		case DKBAD_MAGIC:
    181      1.1      cgd 			break;
    182      1.1      cgd 
    183      1.1      cgd 		default:
    184      1.1      cgd 			printf("bt_flag=%x(16)?\n", oldbad.bt_flag);
    185      1.1      cgd 			break;
    186      1.1      cgd 		}
    187      1.1      cgd 		bt = oldbad.bt_bad;
    188      1.1      cgd 		for (i = 0; i < 126; i++) {
    189      1.1      cgd 			bad = (bt->bt_cyl<<16) + bt->bt_trksec;
    190      1.1      cgd 			if (bad < 0)
    191      1.1      cgd 				break;
    192      1.1      cgd 			printf("sn=%d, cn=%d, tn=%d, sn=%d\n", badsn(bt),
    193      1.1      cgd 			    bt->bt_cyl, bt->bt_trksec>>8, bt->bt_trksec&0xff);
    194      1.1      cgd 			bt++;
    195      1.1      cgd 		}
    196      1.1      cgd 		(void) checkold(&oldbad);
    197      1.1      cgd 		exit(0);
    198      1.1      cgd 	}
    199      1.1      cgd 	if (add) {
    200      1.1      cgd 		/*
    201      1.1      cgd 		 * Read in the old badsector table.
    202      1.1      cgd 		 * Verify that it makes sense, and the bad sectors
    203      1.1      cgd 		 * are in order.  Copy the old table to the new one.
    204      1.1      cgd 		 */
    205      1.1      cgd 		(void) getold(f, &oldbad);
    206      1.1      cgd 		i = checkold(&oldbad);
    207      1.1      cgd 		if (verbose)
    208      1.1      cgd 			printf("Had %d bad sectors, adding %d\n", i, argc);
    209      1.1      cgd 		if (i + argc > 126) {
    210      1.1      cgd 			printf("bad144: not enough room for %d more sectors\n",
    211      1.1      cgd 				argc);
    212      1.1      cgd 			printf("limited to 126 by information format\n");
    213      1.1      cgd 			exit(1);
    214      1.1      cgd 		}
    215      1.1      cgd 		curbad = oldbad;
    216      1.1      cgd 	} else {
    217      1.1      cgd 		curbad.bt_csn = atoi(*argv++);
    218      1.1      cgd 		argc--;
    219      1.1      cgd 		curbad.bt_mbz = 0;
    220      1.1      cgd 		curbad.bt_flag = DKBAD_MAGIC;
    221      1.1      cgd 		if (argc > 126) {
    222      1.1      cgd 			printf("bad144: too many bad sectors specified\n");
    223      1.1      cgd 			printf("limited to 126 by information format\n");
    224      1.1      cgd 			exit(1);
    225      1.1      cgd 		}
    226      1.1      cgd 		i = 0;
    227      1.1      cgd 	}
    228      1.1      cgd 	errs = 0;
    229      1.1      cgd 	new = argc;
    230      1.1      cgd 	while (argc > 0) {
    231      1.1      cgd 		daddr_t sn = atoi(*argv++);
    232      1.1      cgd 		argc--;
    233      1.1      cgd 		if (sn < 0 || sn >= size) {
    234      1.1      cgd 			printf("%d: out of range [0,%d) for disk %s\n",
    235      1.1      cgd 			    sn, size, dp->d_typename);
    236      1.1      cgd 			errs++;
    237      1.1      cgd 			continue;
    238      1.1      cgd 		}
    239      1.1      cgd 		bn[i] = sn;
    240      1.1      cgd 		curbad.bt_bad[i].bt_cyl = sn / (dp->d_nsectors*dp->d_ntracks);
    241      1.1      cgd 		sn %= (dp->d_nsectors*dp->d_ntracks);
    242      1.1      cgd 		curbad.bt_bad[i].bt_trksec =
    243      1.1      cgd 		    ((sn/dp->d_nsectors) << 8) + (sn%dp->d_nsectors);
    244      1.1      cgd 		i++;
    245      1.1      cgd 	}
    246      1.1      cgd 	if (errs)
    247      1.1      cgd 		exit(1);
    248      1.1      cgd 	nbad = i;
    249      1.1      cgd 	while (i < 126) {
    250      1.1      cgd 		curbad.bt_bad[i].bt_trksec = -1;
    251      1.1      cgd 		curbad.bt_bad[i].bt_cyl = -1;
    252      1.1      cgd 		i++;
    253      1.1      cgd 	}
    254      1.1      cgd 	if (add) {
    255      1.1      cgd 		/*
    256      1.1      cgd 		 * Sort the new bad sectors into the list.
    257      1.1      cgd 		 * Then shuffle the replacement sectors so that
    258      1.1      cgd 		 * the previous bad sectors get the same replacement data.
    259      1.1      cgd 		 */
    260      1.1      cgd 		qsort((char *)curbad.bt_bad, nbad, sizeof (struct bt_bad),
    261      1.1      cgd 		    compare);
    262      1.1      cgd 		if (dups) {
    263      1.1      cgd 			fprintf(stderr,
    264      1.1      cgd "bad144: bad sectors have been duplicated; can't add existing sectors\n");
    265      1.1      cgd 			exit(3);
    266      1.1      cgd 		}
    267      1.1      cgd 		shift(f, nbad, nbad-new);
    268      1.1      cgd 	}
    269      1.1      cgd 	if (badfile == -1)
    270      1.1      cgd 		i = 0;
    271      1.1      cgd 	else
    272      1.1      cgd 		i = badfile * 2;
    273      1.1      cgd 	for (; i < 10 && i < dp->d_nsectors; i += 2) {
    274      1.1      cgd 		if (lseek(f, dp->d_secsize * (size - dp->d_nsectors + i),
    275      1.1      cgd 		    L_SET) < 0)
    276      1.1      cgd 			Perror("lseek");
    277      1.1      cgd 		if (verbose)
    278      1.1      cgd 			printf("write badsect file at %d\n",
    279      1.1      cgd 				size - dp->d_nsectors + i);
    280      1.1      cgd 		if (nflag == 0 && write(f, (caddr_t)&curbad, sizeof(curbad)) !=
    281      1.1      cgd 		    sizeof(curbad)) {
    282      1.1      cgd 			char msg[80];
    283      1.1      cgd 			(void)sprintf(msg, "bad144: write bad sector file %d",
    284      1.1      cgd 			    i/2);
    285      1.1      cgd 			perror(msg);
    286      1.1      cgd 		}
    287      1.1      cgd 		if (badfile != -1)
    288      1.1      cgd 			break;
    289      1.1      cgd 	}
    290      1.1      cgd #ifdef vax
    291      1.1      cgd 	if (nflag == 0 && fflag)
    292      1.1      cgd 		for (i = nbad - new; i < nbad; i++)
    293      1.1      cgd 			format(f, bn[i]);
    294      1.1      cgd #endif
    295      1.1      cgd #ifdef DIOCSBAD
    296      1.1      cgd 	if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0)
    297      1.1      cgd 		fprintf(stderr,
    298      1.1      cgd 	"Can't sync bad-sector file; reboot for changes to take effect\n");
    299      1.1      cgd #endif
    300      1.1      cgd 	exit(0);
    301      1.1      cgd }
    302      1.1      cgd 
    303      1.1      cgd daddr_t
    304      1.1      cgd getold(f, bad)
    305      1.1      cgd struct dkbad *bad;
    306      1.1      cgd {
    307      1.1      cgd 	register int i;
    308      1.1      cgd 	daddr_t sn;
    309      1.1      cgd 	char msg[80];
    310      1.1      cgd 
    311      1.1      cgd 	if (badfile == -1)
    312      1.1      cgd 		i = 0;
    313      1.1      cgd 	else
    314      1.1      cgd 		i = badfile * 2;
    315      1.1      cgd 	for (; i < 10 && i < dp->d_nsectors; i += 2) {
    316      1.1      cgd 		sn = size - dp->d_nsectors + i;
    317      1.1      cgd 		if (lseek(f, sn * dp->d_secsize, L_SET) < 0)
    318      1.1      cgd 			Perror("lseek");
    319      1.1      cgd 		if (read(f, (char *) bad, dp->d_secsize) == dp->d_secsize) {
    320      1.1      cgd 			if (i > 0)
    321      1.1      cgd 				printf("Using bad-sector file %d\n", i/2);
    322      1.1      cgd 			return(sn);
    323      1.1      cgd 		}
    324      1.1      cgd 		(void)sprintf(msg, "bad144: read bad sector file at sn %d", sn);
    325      1.1      cgd 		perror(msg);
    326      1.1      cgd 		if (badfile != -1)
    327      1.1      cgd 			break;
    328      1.1      cgd 	}
    329      1.1      cgd 	fprintf(stderr, "bad144: %s: can't read bad block info\n", name);
    330      1.1      cgd 	exit(1);
    331      1.1      cgd 	/*NOTREACHED*/
    332      1.1      cgd }
    333      1.1      cgd 
    334      1.1      cgd checkold()
    335      1.1      cgd {
    336      1.1      cgd 	register int i;
    337      1.1      cgd 	register struct bt_bad *bt;
    338      1.1      cgd 	daddr_t sn, lsn;
    339      1.1      cgd 	int errors = 0, warned = 0;
    340      1.1      cgd 
    341      1.1      cgd 	if (oldbad.bt_flag != DKBAD_MAGIC) {
    342      1.1      cgd 		fprintf(stderr, "bad144: %s: bad flag in bad-sector table\n",
    343      1.1      cgd 			name);
    344      1.1      cgd 		errors++;
    345      1.1      cgd 	}
    346      1.1      cgd 	if (oldbad.bt_mbz != 0) {
    347      1.1      cgd 		fprintf(stderr, "bad144: %s: bad magic number\n", name);
    348      1.1      cgd 		errors++;
    349      1.1      cgd 	}
    350      1.1      cgd 	bt = oldbad.bt_bad;
    351      1.1      cgd 	for (i = 0; i < 126; i++, bt++) {
    352      1.1      cgd 		if (bt->bt_cyl == 0xffff && bt->bt_trksec == 0xffff)
    353      1.1      cgd 			break;
    354      1.1      cgd 		if ((bt->bt_cyl >= dp->d_ncylinders) ||
    355      1.1      cgd 		    ((bt->bt_trksec >> 8) >= dp->d_ntracks) ||
    356      1.1      cgd 		    ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) {
    357      1.1      cgd 			fprintf(stderr,
    358      1.1      cgd 		     "bad144: cyl/trk/sect out of range in existing entry: ");
    359      1.1      cgd 			fprintf(stderr, "sn=%d, cn=%d, tn=%d, sn=%d\n",
    360      1.1      cgd 				badsn(bt), bt->bt_cyl, bt->bt_trksec>>8,
    361      1.1      cgd 				bt->bt_trksec & 0xff);
    362      1.1      cgd 			errors++;
    363      1.1      cgd 		}
    364      1.1      cgd 		sn = (bt->bt_cyl * dp->d_ntracks +
    365      1.1      cgd 		    (bt->bt_trksec >> 8)) *
    366      1.1      cgd 		    dp->d_nsectors + (bt->bt_trksec & 0xff);
    367      1.1      cgd 		if (i > 0 && sn < lsn && !warned) {
    368      1.1      cgd 		    fprintf(stderr,
    369      1.1      cgd 			"bad144: bad sector file is out of order\n");
    370      1.1      cgd 		    errors++;
    371      1.1      cgd 		    warned++;
    372      1.1      cgd 		}
    373      1.1      cgd 		if (i > 0 && sn == lsn) {
    374      1.1      cgd 		    fprintf(stderr,
    375      1.1      cgd 			"bad144: bad sector file contains duplicates (sn %d)\n",
    376      1.1      cgd 			sn);
    377      1.1      cgd 		    errors++;
    378      1.1      cgd 		}
    379      1.1      cgd 		lsn = sn;
    380      1.1      cgd 	}
    381      1.1      cgd 	if (errors)
    382      1.1      cgd 		exit(1);
    383      1.1      cgd 	return (i);
    384      1.1      cgd }
    385      1.1      cgd 
    386      1.1      cgd /*
    387      1.1      cgd  * Move the bad sector replacements
    388      1.1      cgd  * to make room for the new bad sectors.
    389      1.1      cgd  * new is the new number of bad sectors, old is the previous count.
    390      1.1      cgd  */
    391      1.1      cgd shift(f, new, old)
    392      1.1      cgd {
    393      1.1      cgd 	daddr_t repl;
    394      1.1      cgd 
    395      1.1      cgd 	/*
    396      1.1      cgd 	 * First replacement is last sector of second-to-last track.
    397      1.1      cgd 	 */
    398      1.1      cgd 	repl = size - dp->d_nsectors - 1;
    399      1.1      cgd 	new--; old--;
    400      1.1      cgd 	while (new >= 0 && new != old) {
    401      1.1      cgd 		if (old < 0 ||
    402      1.1      cgd 		    compare(&curbad.bt_bad[new], &oldbad.bt_bad[old]) > 0) {
    403      1.1      cgd 			/*
    404      1.1      cgd 			 * Insert new replacement here-- copy original
    405      1.1      cgd 			 * sector if requested and possible,
    406      1.1      cgd 			 * otherwise write a zero block.
    407      1.1      cgd 			 */
    408      1.1      cgd 			if (!copy ||
    409      1.1      cgd 			    !blkcopy(f, badsn(&curbad.bt_bad[new]), repl - new))
    410      1.1      cgd 				blkzero(f, repl - new);
    411      1.1      cgd 		} else {
    412      1.1      cgd 			if (blkcopy(f, repl - old, repl - new) == 0)
    413      1.1      cgd 			    fprintf(stderr,
    414      1.1      cgd 				"Can't copy replacement sector %d to %d\n",
    415      1.1      cgd 				repl-old, repl-new);
    416      1.1      cgd 			old--;
    417      1.1      cgd 		}
    418      1.1      cgd 		new--;
    419      1.1      cgd 	}
    420      1.1      cgd }
    421      1.1      cgd 
    422      1.1      cgd char *buf;
    423      1.1      cgd 
    424      1.1      cgd /*
    425      1.1      cgd  *  Copy disk sector s1 to s2.
    426      1.1      cgd  */
    427      1.1      cgd blkcopy(f, s1, s2)
    428      1.1      cgd daddr_t s1, s2;
    429      1.1      cgd {
    430      1.1      cgd 	register tries, n;
    431      1.1      cgd 
    432      1.1      cgd 	if (buf == (char *)NULL) {
    433      1.1      cgd 		buf = malloc((unsigned)dp->d_secsize);
    434      1.1      cgd 		if (buf == (char *)NULL) {
    435      1.1      cgd 			fprintf(stderr, "Out of memory\n");
    436      1.1      cgd 			exit(20);
    437      1.1      cgd 		}
    438      1.1      cgd 	}
    439      1.1      cgd 	for (tries = 0; tries < RETRIES; tries++) {
    440      1.1      cgd 		if (lseek(f, dp->d_secsize * s1, L_SET) < 0)
    441      1.1      cgd 			Perror("lseek");
    442      1.1      cgd 		if ((n = read(f, buf, dp->d_secsize)) == dp->d_secsize)
    443      1.1      cgd 			break;
    444      1.1      cgd 	}
    445      1.1      cgd 	if (n != dp->d_secsize) {
    446      1.1      cgd 		fprintf(stderr, "bad144: can't read sector, %d: ", s1);
    447      1.1      cgd 		if (n < 0)
    448      1.1      cgd 			perror((char *)0);
    449      1.1      cgd 		return(0);
    450      1.1      cgd 	}
    451      1.1      cgd 	if (lseek(f, dp->d_secsize * s2, L_SET) < 0)
    452      1.1      cgd 		Perror("lseek");
    453      1.1      cgd 	if (verbose)
    454      1.1      cgd 		printf("copying %d to %d\n", s1, s2);
    455      1.1      cgd 	if (nflag == 0 && write(f, buf, dp->d_secsize) != dp->d_secsize) {
    456      1.1      cgd 		fprintf(stderr,
    457      1.1      cgd 		    "bad144: can't write replacement sector, %d: ", s2);
    458      1.1      cgd 		perror((char *)0);
    459      1.1      cgd 		return(0);
    460      1.1      cgd 	}
    461      1.1      cgd 	return(1);
    462      1.1      cgd }
    463      1.1      cgd 
    464      1.1      cgd char *zbuf;
    465      1.1      cgd 
    466      1.1      cgd blkzero(f, sn)
    467      1.1      cgd daddr_t sn;
    468      1.1      cgd {
    469      1.1      cgd 
    470      1.1      cgd 	if (zbuf == (char *)NULL) {
    471      1.1      cgd 		zbuf = malloc((unsigned)dp->d_secsize);
    472      1.1      cgd 		if (zbuf == (char *)NULL) {
    473      1.1      cgd 			fprintf(stderr, "Out of memory\n");
    474      1.1      cgd 			exit(20);
    475      1.1      cgd 		}
    476      1.1      cgd 	}
    477      1.1      cgd 	if (lseek(f, dp->d_secsize * sn, L_SET) < 0)
    478      1.1      cgd 		Perror("lseek");
    479      1.1      cgd 	if (verbose)
    480      1.1      cgd 		printf("zeroing %d\n", sn);
    481      1.1      cgd 	if (nflag == 0 && write(f, zbuf, dp->d_secsize) != dp->d_secsize) {
    482      1.1      cgd 		fprintf(stderr,
    483      1.1      cgd 		    "bad144: can't write replacement sector, %d: ", sn);
    484      1.1      cgd 		perror((char *)0);
    485      1.1      cgd 	}
    486      1.1      cgd }
    487      1.1      cgd 
    488      1.1      cgd compare(b1, b2)
    489      1.1      cgd register struct bt_bad *b1, *b2;
    490      1.1      cgd {
    491      1.1      cgd 	if (b1->bt_cyl > b2->bt_cyl)
    492      1.1      cgd 		return(1);
    493      1.1      cgd 	if (b1->bt_cyl < b2->bt_cyl)
    494      1.1      cgd 		return(-1);
    495      1.1      cgd 	if (b1->bt_trksec == b2->bt_trksec)
    496      1.1      cgd 		dups++;
    497      1.1      cgd 	return (b1->bt_trksec - b2->bt_trksec);
    498      1.1      cgd }
    499      1.1      cgd 
    500      1.1      cgd daddr_t
    501      1.1      cgd badsn(bt)
    502      1.1      cgd register struct bt_bad *bt;
    503      1.1      cgd {
    504      1.1      cgd 	return ((bt->bt_cyl*dp->d_ntracks + (bt->bt_trksec>>8)) * dp->d_nsectors
    505      1.1      cgd 		+ (bt->bt_trksec&0xff));
    506      1.1      cgd }
    507      1.1      cgd 
    508      1.1      cgd #ifdef vax
    509      1.1      cgd 
    510      1.1      cgd struct rp06hdr {
    511      1.1      cgd 	short	h_cyl;
    512      1.1      cgd 	short	h_trksec;
    513      1.1      cgd 	short	h_key1;
    514      1.1      cgd 	short	h_key2;
    515      1.1      cgd 	char	h_data[512];
    516      1.1      cgd #define	RP06_FMT	010000		/* 1 == 16 bit, 0 == 18 bit */
    517      1.1      cgd };
    518      1.1      cgd 
    519      1.1      cgd /*
    520      1.1      cgd  * Most massbus and unibus drives
    521      1.1      cgd  * have headers of this form
    522      1.1      cgd  */
    523      1.1      cgd struct hpuphdr {
    524      1.1      cgd 	u_short	hpup_cyl;
    525      1.1      cgd 	u_char	hpup_sect;
    526      1.1      cgd 	u_char	hpup_track;
    527      1.1      cgd 	char	hpup_data[512];
    528      1.1      cgd #define	HPUP_OKSECT	0xc000		/* this normally means sector is good */
    529      1.1      cgd #define	HPUP_16BIT	0x1000		/* 1 == 16 bit format */
    530      1.1      cgd };
    531      1.1      cgd int rp06format(), hpupformat();
    532      1.1      cgd 
    533      1.1      cgd struct	formats {
    534      1.1      cgd 	char	*f_name;		/* disk name */
    535      1.1      cgd 	int	f_bufsize;		/* size of sector + header */
    536      1.1      cgd 	int	f_bic;			/* value to bic in hpup_cyl */
    537      1.1      cgd 	int	(*f_routine)();		/* routine for special handling */
    538      1.1      cgd } formats[] = {
    539      1.1      cgd 	{ "rp06",	sizeof (struct rp06hdr), RP06_FMT,	rp06format },
    540      1.1      cgd 	{ "eagle",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
    541      1.1      cgd 	{ "capricorn",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
    542      1.1      cgd 	{ "rm03",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
    543      1.1      cgd 	{ "rm05",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
    544      1.1      cgd 	{ "9300",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
    545      1.1      cgd 	{ "9766",	sizeof (struct hpuphdr), HPUP_OKSECT,	hpupformat },
    546      1.1      cgd 	{ 0, 0, 0, 0 }
    547      1.1      cgd };
    548      1.1      cgd 
    549      1.1      cgd /*ARGSUSED*/
    550      1.1      cgd hpupformat(fp, dp, blk, buf, count)
    551      1.1      cgd 	struct formats *fp;
    552      1.1      cgd 	struct disklabel *dp;
    553      1.1      cgd 	daddr_t blk;
    554      1.1      cgd 	char *buf;
    555      1.1      cgd 	int count;
    556      1.1      cgd {
    557      1.1      cgd 	struct hpuphdr *hdr = (struct hpuphdr *)buf;
    558      1.1      cgd 	int sect;
    559      1.1      cgd 
    560      1.1      cgd 	if (count < sizeof(struct hpuphdr)) {
    561      1.1      cgd 		hdr->hpup_cyl = (HPUP_OKSECT | HPUP_16BIT) |
    562      1.1      cgd 			(blk / (dp->d_nsectors * dp->d_ntracks));
    563      1.1      cgd 		sect = blk % (dp->d_nsectors * dp->d_ntracks);
    564      1.1      cgd 		hdr->hpup_track = (u_char)(sect / dp->d_nsectors);
    565      1.1      cgd 		hdr->hpup_sect = (u_char)(sect % dp->d_nsectors);
    566      1.1      cgd 	}
    567      1.1      cgd 	return (0);
    568      1.1      cgd }
    569      1.1      cgd 
    570      1.1      cgd /*ARGSUSED*/
    571      1.1      cgd rp06format(fp, dp, blk, buf, count)
    572      1.1      cgd 	struct formats *fp;
    573      1.1      cgd 	struct disklabel *dp;
    574      1.1      cgd 	daddr_t blk;
    575      1.1      cgd 	char *buf;
    576      1.1      cgd 	int count;
    577      1.1      cgd {
    578      1.1      cgd 
    579      1.1      cgd 	if (count < sizeof(struct rp06hdr)) {
    580      1.1      cgd 		fprintf(stderr, "Can't read header on blk %d, can't reformat\n",
    581      1.1      cgd 			blk);
    582      1.1      cgd 		return (-1);
    583      1.1      cgd 	}
    584      1.1      cgd 	return (0);
    585      1.1      cgd }
    586      1.1      cgd 
    587      1.1      cgd format(fd, blk)
    588      1.1      cgd 	int fd;
    589      1.1      cgd 	daddr_t blk;
    590      1.1      cgd {
    591      1.1      cgd 	register struct formats *fp;
    592      1.1      cgd 	static char *buf;
    593      1.1      cgd 	static char bufsize;
    594      1.1      cgd 	struct format_op fop;
    595      1.1      cgd 	int n;
    596      1.1      cgd 
    597      1.1      cgd 	for (fp = formats; fp->f_name; fp++)
    598      1.1      cgd 		if (strcmp(dp->d_typename, fp->f_name) == 0)
    599      1.1      cgd 			break;
    600      1.1      cgd 	if (fp->f_name == 0) {
    601      1.1      cgd 		fprintf(stderr, "bad144: don't know how to format %s disks\n",
    602      1.1      cgd 			dp->d_typename);
    603      1.1      cgd 		exit(2);
    604      1.1      cgd 	}
    605      1.1      cgd 	if (buf && bufsize < fp->f_bufsize) {
    606      1.1      cgd 		free(buf);
    607      1.1      cgd 		buf = NULL;
    608      1.1      cgd 	}
    609      1.1      cgd 	if (buf == NULL)
    610      1.1      cgd 		buf = malloc((unsigned)fp->f_bufsize);
    611      1.1      cgd 	if (buf == NULL) {
    612      1.1      cgd 		fprintf(stderr, "bad144: can't allocate sector buffer\n");
    613      1.1      cgd 		exit(3);
    614      1.1      cgd 	}
    615      1.1      cgd 	bufsize = fp->f_bufsize;
    616      1.1      cgd 	/*
    617      1.1      cgd 	 * Here we do the actual formatting.  All we really
    618      1.1      cgd 	 * do is rewrite the sector header and flag the bad sector
    619      1.1      cgd 	 * according to the format table description.  If a special
    620      1.1      cgd 	 * purpose format routine is specified, we allow it to
    621      1.1      cgd 	 * process the sector as well.
    622      1.1      cgd 	 */
    623      1.1      cgd 	if (verbose)
    624      1.1      cgd 		printf("format blk %d\n", blk);
    625      1.1      cgd 	bzero((char *)&fop, sizeof(fop));
    626      1.1      cgd 	fop.df_buf = buf;
    627      1.1      cgd 	fop.df_count = fp->f_bufsize;
    628      1.1      cgd 	fop.df_startblk = blk;
    629      1.1      cgd 	bzero(buf, fp->f_bufsize);
    630      1.1      cgd 	if (ioctl(fd, DIOCRFORMAT, &fop) < 0)
    631      1.1      cgd 		perror("bad144: read format");
    632      1.1      cgd 	if (fp->f_routine &&
    633      1.1      cgd 	    (*fp->f_routine)(fp, dp, blk, buf, fop.df_count) != 0)
    634      1.1      cgd 		return;
    635      1.1      cgd 	if (fp->f_bic) {
    636      1.1      cgd 		struct hpuphdr *xp = (struct hpuphdr *)buf;
    637      1.1      cgd 
    638      1.1      cgd 		xp->hpup_cyl &= ~fp->f_bic;
    639      1.1      cgd 	}
    640      1.1      cgd 	if (nflag)
    641      1.1      cgd 		return;
    642      1.1      cgd 	bzero((char *)&fop, sizeof(fop));
    643      1.1      cgd 	fop.df_buf = buf;
    644      1.1      cgd 	fop.df_count = fp->f_bufsize;
    645      1.1      cgd 	fop.df_startblk = blk;
    646      1.1      cgd 	if (ioctl(fd, DIOCWFORMAT, &fop) < 0)
    647      1.1      cgd 		Perror("write format");
    648      1.1      cgd 	if (fop.df_count != fp->f_bufsize) {
    649      1.1      cgd 		char msg[80];
    650      1.1      cgd 		(void)sprintf(msg, "bad144: write format %d", blk);
    651      1.1      cgd 		perror(msg);
    652      1.1      cgd 	}
    653      1.1      cgd }
    654      1.1      cgd #endif
    655      1.1      cgd 
    656      1.1      cgd Perror(op)
    657      1.1      cgd 	char *op;
    658      1.1      cgd {
    659      1.1      cgd 
    660      1.1      cgd 	fprintf(stderr, "bad144: "); perror(op);
    661      1.1      cgd 	exit(4);
    662      1.1      cgd }
    663