bad144.c revision 1.17 1 /* $NetBSD: bad144.c,v 1.17 2002/06/13 13:32:47 wiz 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.17 2002/06/13 13:32:47 wiz 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/ufs/dinode.h>
70 #include <ufs/ffs/fs.h>
71
72 #include <err.h>
73 #include <paths.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
78 #include <util.h>
79
80 #define RETRIES 10 /* number of retries on reading old sectors */
81
82 int fflag, add, copy, verbose, nflag;
83 int dups;
84 int badfile = -1; /* copy of badsector table to use, -1 if any */
85 #define MAXSECSIZE 1024
86 struct dkbad curbad, oldbad;
87 #define DKBAD_MAGIC 0x4321
88
89 char label[BBSIZE];
90 daddr_t size;
91 struct disklabel *dp;
92 char name[BUFSIZ];
93
94 daddr_t badsn(const struct bt_bad *);
95 int blkcopy(int, daddr_t, daddr_t);
96 void blkzero(int, daddr_t);
97 int checkold(void);
98 int compare(const void *, const void *);
99 daddr_t getold(int, struct dkbad *);
100 int main(int, char **);
101 void shift(int, int, int);
102
103 int
104 main(int argc, char *argv[])
105 {
106 struct bt_bad *bt;
107 daddr_t sn, bn[126];
108 int i, f, nbad, new, bad, errs;
109 char diskname[MAXPATHLEN];
110
111 argc--, argv++;
112 while (argc > 0 && **argv == '-') {
113 (*argv)++;
114 while (**argv) {
115 switch (**argv) {
116 #if __vax__
117 case 'f':
118 fflag++;
119 break;
120 #endif
121 case 'a':
122 add++;
123 break;
124 case 'c':
125 copy++;
126 break;
127 case 'v':
128 verbose++;
129 break;
130 case 'n':
131 nflag++;
132 verbose++;
133 break;
134 default:
135 if (**argv >= '0' && **argv <= '4') {
136 badfile = **argv - '0';
137 break;
138 }
139 goto usage;
140 }
141 (*argv)++;
142 }
143 argc--, argv++;
144 }
145 if (argc < 1) {
146 usage:
147 fprintf(stderr,
148 "usage: bad144 [ -f ] disk [ snum [ bn ... ] ]\n");
149 fprintf(stderr,
150 "to read or overwrite bad-sector table, e.g.: bad144 hp0\n");
151 fprintf(stderr,
152 "or bad144 -a [ -f ] [ -c ] disk bn ...\n");
153 fprintf(stderr, "where options are:\n");
154 fprintf(stderr, "\t-a add new bad sectors to the table\n");
155 fprintf(stderr, "\t-f reformat listed sectors as bad\n");
156 fprintf(stderr, "\t-c copy original sector to replacement\n");
157 exit(1);
158 }
159 f = opendisk(argv[0], argc == 1 ? O_RDONLY : O_RDWR, diskname,
160 sizeof(diskname), 0);
161 if (f < 0)
162 err(4, "opendisk `%s'", diskname);
163 #ifdef was
164 if (read(f, label, sizeof(label)) < 0)
165 err(4, "read `%s'", diskname);
166 for (dp = (struct disklabel *)(label + LABELOFFSET);
167 dp < (struct disklabel *)
168 (label + sizeof(label) - sizeof(struct disklabel));
169 dp = (struct disklabel *)((char *)dp + 64))
170 if (dp->d_magic == DISKMAGIC && dp->d_magic2 == DISKMAGIC)
171 break;
172 #else
173 /* obtain label and adjust to fit */
174 dp = (struct disklabel *)&label;
175 if (ioctl(f, DIOCGDINFO, dp) < 0)
176 err(4, "ioctl DIOCGDINFO `%s'", diskname);
177 #endif
178 if (dp->d_magic != DISKMAGIC || dp->d_magic2 != DISKMAGIC
179 /* dkcksum(lp) != 0 */ )
180 errx(1, "Bad pack magic number (pack is unlabeled)");
181 if (dp->d_secsize > MAXSECSIZE || dp->d_secsize <= 0)
182 errx(7, "Disk sector size too large/small (%d)",
183 dp->d_secsize);
184 #ifdef __i386__
185 if (dp->d_type == DTYPE_SCSI)
186 errx(1, "SCSI disks don't use bad144!");
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 }
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();
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();
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 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 errx(3,
292 "bad sectors have been duplicated; can't add existing sectors");
293 shift(f, nbad, nbad-new);
294 }
295 if (badfile == -1)
296 i = 0;
297 else
298 i = badfile * 2;
299 for (; i < 10 && i < dp->d_nsectors; i += 2) {
300 if (lseek(f,
301 (off_t)(dp->d_secsize * (size - dp->d_nsectors + i)),
302 SEEK_SET) < 0)
303 err(4, "lseek");
304 if (verbose)
305 printf("write badsect file at %d\n",
306 size - dp->d_nsectors + i);
307 if (nflag == 0 && write(f, (caddr_t)&curbad, sizeof(curbad)) !=
308 sizeof(curbad))
309 err(4, "write bad sector file %d", i/2);
310 if (badfile != -1)
311 break;
312 }
313 #ifdef __vax__
314 if (nflag == 0 && fflag)
315 for (i = nbad - new; i < nbad; i++)
316 format(f, bn[i]);
317 #endif
318 #ifdef DIOCSBAD
319 if (nflag == 0 && ioctl(f, DIOCSBAD, (caddr_t)&curbad) < 0)
320 warnx(
321 "Can't sync bad-sector file; reboot for changes to take effect");
322 #endif
323 if ((dp->d_flags & D_BADSECT) == 0 && nflag == 0) {
324 dp->d_flags |= D_BADSECT;
325 if (ioctl(f, DIOCWDINFO, dp) < 0) {
326 warn("label");
327 errx(1,
328 "Can't write label to enable bad sector handling");
329 }
330 }
331 exit(0);
332 }
333
334 daddr_t
335 getold(int f, struct dkbad *bad)
336 {
337 int i;
338 daddr_t sn;
339
340 if (badfile == -1)
341 i = 0;
342 else
343 i = badfile * 2;
344 for (; i < 10 && i < dp->d_nsectors; i += 2) {
345 sn = size - dp->d_nsectors + i;
346 if (lseek(f, (off_t)(sn * dp->d_secsize), SEEK_SET) < 0)
347 err(4, "lseek");
348 if (read(f, (char *) bad, dp->d_secsize) == dp->d_secsize) {
349 if (i > 0)
350 printf("Using bad-sector file %d\n", i/2);
351 return(sn);
352 }
353 warn("read bad sector file at sn %d", sn);
354 if (badfile != -1)
355 break;
356 }
357 errx(1, "%s: can't read bad block info", name);
358 /*NOTREACHED*/
359 }
360
361 int
362 checkold(void)
363 {
364 int i;
365 struct bt_bad *bt;
366 daddr_t sn, lsn;
367 int errors = 0, warned = 0;
368
369 lsn = 0;
370 if (oldbad.bt_flag != DKBAD_MAGIC) {
371 warnx("%s: bad flag in bad-sector table", name);
372 errors++;
373 }
374 if (oldbad.bt_mbz != 0) {
375 warnx("%s: bad magic number", name);
376 errors++;
377 }
378 bt = oldbad.bt_bad;
379 for (i = 0; i < 126; i++, bt++) {
380 if (bt->bt_cyl == 0xffff && bt->bt_trksec == 0xffff)
381 break;
382 if ((bt->bt_cyl >= dp->d_ncylinders) ||
383 ((bt->bt_trksec >> 8) >= dp->d_ntracks) ||
384 ((bt->bt_trksec & 0xff) >= dp->d_nsectors)) {
385 warnx(
386 "cyl/trk/sect out of range in existing entry: sn=%d, cn=%d, tn=%d, sn=%d",
387 badsn(bt), bt->bt_cyl, bt->bt_trksec>>8,
388 bt->bt_trksec & 0xff);
389 errors++;
390 }
391 sn = (bt->bt_cyl * dp->d_ntracks +
392 (bt->bt_trksec >> 8)) *
393 dp->d_nsectors + (bt->bt_trksec & 0xff);
394 if (i > 0 && sn < lsn && !warned) {
395 warnx("bad sector file is out of order");
396 errors++;
397 warned++;
398 }
399 if (i > 0 && sn == lsn) {
400 warnx("bad sector file contains duplicates (sn %d)", sn);
401 errors++;
402 }
403 lsn = sn;
404 }
405 if (errors)
406 exit(1);
407 return (i);
408 }
409
410 /*
411 * Move the bad sector replacements
412 * to make room for the new bad sectors.
413 * new is the new number of bad sectors, old is the previous count.
414 */
415 void
416 shift(int f, int new, int old)
417 {
418 daddr_t repl;
419
420 /*
421 * First replacement is last sector of second-to-last track.
422 */
423 repl = size - dp->d_nsectors - 1;
424 new--; old--;
425 while (new >= 0 && new != old) {
426 if (old < 0 ||
427 compare(&curbad.bt_bad[new], &oldbad.bt_bad[old]) > 0) {
428 /*
429 * Insert new replacement here-- copy original
430 * sector if requested and possible,
431 * otherwise write a zero block.
432 */
433 if (!copy ||
434 !blkcopy(f, badsn(&curbad.bt_bad[new]), repl - new))
435 blkzero(f, repl - new);
436 } else {
437 if (blkcopy(f, repl - old, repl - new) == 0)
438 warnx("Can't copy replacement sector %d to %d",
439 repl-old, repl-new);
440 old--;
441 }
442 new--;
443 }
444 }
445
446 char *buf;
447
448 /*
449 * Copy disk sector s1 to s2.
450 */
451 int
452 blkcopy(int f, daddr_t s1, daddr_t s2)
453 {
454 int tries, n;
455
456 if (buf == (char *)NULL) {
457 buf = malloc((unsigned)dp->d_secsize);
458 if (buf == (char *)NULL)
459 errx(20, "Out of memory");
460 }
461 for (tries = 0; tries < RETRIES; tries++) {
462 if (lseek(f, (off_t)(dp->d_secsize * s1), SEEK_SET) < 0)
463 err(4, "lseek");
464 if ((n = read(f, buf, dp->d_secsize)) == dp->d_secsize)
465 break;
466 }
467 if (n != dp->d_secsize) {
468 if (n < 0)
469 err(4, "can't read sector, %d", s1);
470 else
471 errx(4, "can't read sector, %d", s1);
472 return(0);
473 }
474 if (lseek(f, (off_t)(dp->d_secsize * s2), SEEK_SET) < 0)
475 err(4, "lseek");
476 if (verbose)
477 printf("copying %d to %d\n", s1, s2);
478 if (nflag == 0 && write(f, buf, dp->d_secsize) != dp->d_secsize) {
479 warn("can't write replacement sector, %d", s2);
480 return(0);
481 }
482 return(1);
483 }
484
485 char *zbuf;
486
487 void
488 blkzero(int f, daddr_t sn)
489 {
490
491 if (zbuf == (char *)NULL) {
492 zbuf = malloc((unsigned)dp->d_secsize);
493 if (zbuf == (char *)NULL)
494 errx(20, "Out of memory");
495 }
496 if (lseek(f, (off_t)(dp->d_secsize * sn), SEEK_SET) < 0)
497 err(4, "lseek");
498 if (verbose)
499 printf("zeroing %d\n", sn);
500 if (nflag == 0 && write(f, zbuf, dp->d_secsize) != dp->d_secsize)
501 warn("can't write replacement sector, %d", sn);
502 }
503
504 int
505 compare(const void *v1, const void *v2)
506 {
507 const struct bt_bad *b1 = v1;
508 const struct bt_bad *b2 = v2;
509
510 if (b1->bt_cyl > b2->bt_cyl)
511 return(1);
512 if (b1->bt_cyl < b2->bt_cyl)
513 return(-1);
514 if (b1->bt_trksec == b2->bt_trksec)
515 dups++;
516 return (b1->bt_trksec - b2->bt_trksec);
517 }
518
519 daddr_t
520 badsn(const struct bt_bad *bt)
521 {
522
523 return ((bt->bt_cyl * dp->d_ntracks
524 + (bt->bt_trksec >> 8)) * dp->d_nsectors
525 + (bt->bt_trksec & 0xff));
526 }
527
528 #ifdef __vax__
529
530 struct rp06hdr {
531 short h_cyl;
532 short h_trksec;
533 short h_key1;
534 short h_key2;
535 char h_data[512];
536 #define RP06_FMT 010000 /* 1 == 16 bit, 0 == 18 bit */
537 };
538
539 /*
540 * Most massbus and unibus drives
541 * have headers of this form
542 */
543 struct hpuphdr {
544 u_short hpup_cyl;
545 u_char hpup_sect;
546 u_char hpup_track;
547 char hpup_data[512];
548 #define HPUP_OKSECT 0xc000 /* this normally means sector is good */
549 #define HPUP_16BIT 0x1000 /* 1 == 16 bit format */
550 };
551 int rp06format(), hpupformat();
552
553 struct formats {
554 char *f_name; /* disk name */
555 int f_bufsize; /* size of sector + header */
556 int f_bic; /* value to bic in hpup_cyl */
557 int (*f_routine)(); /* routine for special handling */
558 } formats[] = {
559 { "rp06", sizeof (struct rp06hdr), RP06_FMT, rp06format },
560 { "eagle", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
561 { "capricorn", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
562 { "rm03", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
563 { "rm05", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
564 { "9300", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
565 { "9766", sizeof (struct hpuphdr), HPUP_OKSECT, hpupformat },
566 { 0, 0, 0, 0 }
567 };
568
569 /*ARGSUSED*/
570 int
571 hpupformat(fp, dp, blk, buf, count)
572 struct formats *fp;
573 struct disklabel *dp;
574 daddr_t blk;
575 char *buf;
576 int count;
577 {
578 struct hpuphdr *hdr = (struct hpuphdr *)buf;
579 int sect;
580
581 if (count < sizeof(struct hpuphdr)) {
582 hdr->hpup_cyl = (HPUP_OKSECT | HPUP_16BIT) |
583 (blk / (dp->d_nsectors * dp->d_ntracks));
584 sect = blk % (dp->d_nsectors * dp->d_ntracks);
585 hdr->hpup_track = (u_char)(sect / dp->d_nsectors);
586 hdr->hpup_sect = (u_char)(sect % dp->d_nsectors);
587 }
588 return (0);
589 }
590
591 /*ARGSUSED*/
592 int
593 rp06format(fp, dp, blk, buf, count)
594 struct formats *fp;
595 struct disklabel *dp;
596 daddr_t blk;
597 char *buf;
598 int count;
599 {
600
601 if (count < sizeof(struct rp06hdr)) {
602 warnx("Can't read header on blk %d, can't reformat", blk);
603 return (-1);
604 }
605 return (0);
606 }
607
608 void
609 format(fd, blk)
610 int fd;
611 daddr_t blk;
612 {
613 struct formats *fp;
614 static char *buf;
615 static char bufsize;
616 struct format_op fop;
617 int n;
618
619 for (fp = formats; fp->f_name; fp++)
620 if (strcmp(dp->d_typename, fp->f_name) == 0)
621 break;
622 if (fp->f_name == 0)
623 errx(2, "don't know how to format %s disks", dp->d_typename);
624 if (buf && bufsize < fp->f_bufsize) {
625 free(buf);
626 buf = NULL;
627 }
628 if (buf == NULL)
629 buf = malloc((unsigned)fp->f_bufsize);
630 if (buf == NULL)
631 errx(3, "can't allocate sector buffer");
632 bufsize = fp->f_bufsize;
633 /*
634 * Here we do the actual formatting. All we really
635 * do is rewrite the sector header and flag the bad sector
636 * according to the format table description. If a special
637 * purpose format routine is specified, we allow it to
638 * process the sector as well.
639 */
640 if (verbose)
641 printf("format blk %d\n", blk);
642 memset((char *)&fop, 0, sizeof(fop));
643 fop.df_buf = buf;
644 fop.df_count = fp->f_bufsize;
645 fop.df_startblk = blk;
646 memset(buf, 0, fp->f_bufsize);
647 if (ioctl(fd, DIOCRFORMAT, &fop) < 0)
648 warn("read format");
649 if (fp->f_routine &&
650 (*fp->f_routine)(fp, dp, blk, buf, fop.df_count) != 0)
651 return;
652 if (fp->f_bic) {
653 struct hpuphdr *xp = (struct hpuphdr *)buf;
654
655 xp->hpup_cyl &= ~fp->f_bic;
656 }
657 if (nflag)
658 return;
659 memset((char *)&fop, 0, sizeof(fop));
660 fop.df_buf = buf;
661 fop.df_count = fp->f_bufsize;
662 fop.df_startblk = blk;
663 if (ioctl(fd, DIOCWFORMAT, &fop) < 0)
664 err(4, "write format");
665 if (fop.df_count != fp->f_bufsize)
666 warn("write format %d", blk);
667 }
668 #endif
669