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