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