scsi-defects.pl revision 267654
1101704Smjacob#!/usr/bin/perl
2139749Simp#
3101704Smjacob# Copyright (C) 1997
4101704Smjacob# 	Peter Dufault, Joerg Wunsch.  All rights reserved.
5101704Smjacob#
6101704Smjacob# Redistribution and use in source and binary forms, with or without
7101704Smjacob# modification, are permitted provided that the following conditions
8101704Smjacob# are met:
9101704Smjacob# 1. Redistributions of source code must retain the above copyright
10101704Smjacob#    notice, this list of conditions and the following disclaimer.
11101704Smjacob# 2. Redistributions in binary form must reproduce the above copyright
12101704Smjacob#    notice, this list of conditions and the following disclaimer in the
13101704Smjacob#    documentation and/or other materials provided with the distribution.
14101704Smjacob#
15101704Smjacob# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16101704Smjacob# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17101704Smjacob# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18101704Smjacob# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19101704Smjacob# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20101704Smjacob# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21101704Smjacob# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22101704Smjacob# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23101704Smjacob# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24101704Smjacob# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25101704Smjacob# SUCH DAMAGE.
26101704Smjacob#
27101704Smjacob# $FreeBSD: releng/9.3/tools/tools/scsi-defects/scsi-defects.pl 50477 1999-08-28 01:08:13Z peter $
28156000Smjacob#
29156000Smjacob
30156000Smjacob#
31156000Smjacob# Read and decode a SCSI disk's primary or grown defect list.
32156000Smjacob#
33156000Smjacob
34156000Smjacobsub usage
35156000Smjacob{
36156000Smjacob    die "usage: scsi-defects raw-device-name [Glist|Plist]\n";
37156000Smjacob}
38156000Smjacob
39156000Smjacob
40156000Smjacob#
41156000Smjacob# Main
42156000Smjacob#
43156000Smjacob
44156000Smjacob&usage if $#ARGV < 0 || $#ARGV > 1;
45156000Smjacob
46156000Smjacob$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin";
47156000Smjacob
48156000Smjacob$dev = $ARGV[0];
49156000Smjacob
50156000Smjacob# generic device name given?
51156000Smjacobif ($dev =~ /^[so]d\d+$/) { $dev = "/dev/r${dev}.ctl"; }
52156000Smjacob
53156000Smjacob#
54156000Smjacob# Select what you want to read.  PList include the primary defect list
55156000Smjacob# from the factory.  GList is grown defects only.
56156000Smjacob#
57156000Smjacobif ($#ARGV > 0) {
58147883Sscottl    if ($ARGV[1] =~ /^[Gg]/) { $glist = 1; $plist = 0; }
59156000Smjacob    elsif ($ARGV[1] =~ /^[Pp]/) { $glist = 0; $plist = 1; }
60156000Smjacob    else { &usage; }
61159052Smjacob} else {
62159052Smjacob    $glist = 1; $plist = 0;
63159052Smjacob}
64159052Smjacob
65101704Smjacobopen(PIPE, "scsi -f $dev " .
66101704Smjacob     "-c '{ Op code} 37 0 0:3 v:1 v:1 5:3 0 0 0 0 4:i2 0' $plist $glist " .
67147883Sscottl     "-i 4 '{ stuff } *i2 { Defect list length } i2' |") ||
68147883Sscottl    die "Cannot pipe to scsi(8)\n";
69147883Sscottlchop($amnt = <PIPE>);
70147883Sscottlclose(PIPE);
71147883Sscottl
72147883Sscottlif ($amnt == 0) {
73147883Sscottl    print "There are no defects (in this list).\n";
74147883Sscottl    exit 0;
75147883Sscottl}
76147883Sscottl
77147883Sscottlprint "There are " . $amnt / 8 . " defects in this list.\n";
78147883Sscottl
79147883Sscottl$amnt += 4;
80147883Sscottl
81147883Sscottlopen(PIPE, "scsi -f $dev " .
82148679Sgibbs     "-c '{ Op code} 37 0 0:3 v:1 v:1 5:3 0 0 0 0 v:i2 0' $plist $glist " .
83148679Sgibbs     "$amnt -i $amnt - |") ||
84148679Sgibbs    die "Cannot pipe to scsi(8)\n";
85147883Sscottl
86147883Sscottlread(PIPE, $buf, 4);		# defect list header
87147883Sscottl
88147883Sscottlprint "cylinder head  sector\n";
89147883Sscottl
90147883Sscottlwhile(read(PIPE, $buf, 8)) {
91147883Sscottl    ($cylhi, $cyllo, $head, $sec) = unpack("CnCN", $buf);
92147883Sscottl    printf "%8u %4u  %6u\n", $cylhi*65536+$cyllo, $head, $sec;
93147883Sscottl}
94147883Sscottlclose(PIPE);
95147883Sscottl