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