scsi-defects.pl revision 50477
1139804Simp#!/usr/bin/perl
2126324Sjhb#
3126324Sjhb# Copyright (C) 1997
4126324Sjhb# 	Peter Dufault, Joerg Wunsch.  All rights reserved.
5126324Sjhb#
6126324Sjhb# Redistribution and use in source and binary forms, with or without
7126324Sjhb# modification, are permitted provided that the following conditions
8126324Sjhb# are met:
9126324Sjhb# 1. Redistributions of source code must retain the above copyright
10126324Sjhb#    notice, this list of conditions and the following disclaimer.
11126324Sjhb# 2. Redistributions in binary form must reproduce the above copyright
12126324Sjhb#    notice, this list of conditions and the following disclaimer in the
13126324Sjhb#    documentation and/or other materials provided with the distribution.
14126324Sjhb#
15126324Sjhb# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16126324Sjhb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17126324Sjhb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18126324Sjhb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19126324Sjhb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20126324Sjhb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21126324Sjhb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22126324Sjhb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23126324Sjhb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24126324Sjhb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25126324Sjhb# SUCH DAMAGE.
26126324Sjhb#
27126324Sjhb# $FreeBSD: head/tools/tools/scsi-defects/scsi-defects.pl 50477 1999-08-28 01:08:13Z peter $
28126324Sjhb#
29126324Sjhb
30126324Sjhb#
31126324Sjhb# Read and decode a SCSI disk's primary or grown defect list.
32126324Sjhb#
33126324Sjhb
34126324Sjhbsub usage
35126324Sjhb{
36126324Sjhb    die "usage: scsi-defects raw-device-name [Glist|Plist]\n";
37126324Sjhb}
38126324Sjhb
39126324Sjhb
40126324Sjhb#
41126324Sjhb# Main
42126324Sjhb#
43126324Sjhb
44126324Sjhb&usage if $#ARGV < 0 || $#ARGV > 1;
45126324Sjhb
46126324Sjhb$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin";
47126324Sjhb
48126324Sjhb$dev = $ARGV[0];
49126324Sjhb
50126324Sjhb# generic device name given?
51126324Sjhbif ($dev =~ /^[so]d\d+$/) { $dev = "/dev/r${dev}.ctl"; }
52126324Sjhb
53126324Sjhb#
54126324Sjhb# Select what you want to read.  PList include the primary defect list
55126324Sjhb# from the factory.  GList is grown defects only.
56126324Sjhb#
57126324Sjhbif ($#ARGV > 0) {
58126324Sjhb    if ($ARGV[1] =~ /^[Gg]/) { $glist = 1; $plist = 0; }
59126324Sjhb    elsif ($ARGV[1] =~ /^[Pp]/) { $glist = 0; $plist = 1; }
60126324Sjhb    else { &usage; }
61126324Sjhb} else {
62154936Sjhb    $glist = 1; $plist = 0;
63154936Sjhb}
64170640Sjeff
65296973Scemopen(PIPE, "scsi -f $dev " .
66154936Sjhb     "-c '{ Op code} 37 0 0:3 v:1 v:1 5:3 0 0 0 0 4:i2 0' $plist $glist " .
67126324Sjhb     "-i 4 '{ stuff } *i2 { Defect list length } i2' |") ||
68126324Sjhb    die "Cannot pipe to scsi(8)\n";
69126324Sjhbchop($amnt = <PIPE>);
70126324Sjhbclose(PIPE);
71126324Sjhb
72126324Sjhbif ($amnt == 0) {
73126324Sjhb    print "There are no defects (in this list).\n";
74177372Sjeff    exit 0;
75126324Sjhb}
76235459Srstone
77126324Sjhbprint "There are " . $amnt / 8 . " defects in this list.\n";
78126324Sjhb
79296927Scem$amnt += 4;
80131259Sjhb
81126324Sjhbopen(PIPE, "scsi -f $dev " .
82169666Sjeff     "-c '{ Op code} 37 0 0:3 v:1 v:1 5:3 0 0 0 0 v:i2 0' $plist $glist " .
83169666Sjeff     "$amnt -i $amnt - |") ||
84154936Sjhb    die "Cannot pipe to scsi(8)\n";
85154936Sjhb
86154936Sjhbread(PIPE, $buf, 4);		# defect list header
87154936Sjhb
88296927Scemprint "cylinder head  sector\n";
89126324Sjhb
90248186Smavwhile(read(PIPE, $buf, 8)) {
91248186Smav    ($cylhi, $cyllo, $head, $sec) = unpack("CnCN", $buf);
92126324Sjhb    printf "%8u %4u  %6u\n", $cylhi*65536+$cyllo, $head, $sec;
93248186Smav}
94126324Sjhbclose(PIPE);
95126324Sjhb