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