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