122205Sjoerg#!/usr/bin/perl
222205Sjoerg#
322205Sjoerg# Copyright (C) 1997
422205Sjoerg# 	Peter Dufault, Joerg Wunsch.  All rights reserved.
522205Sjoerg#
622205Sjoerg# Redistribution and use in source and binary forms, with or without
722205Sjoerg# modification, are permitted provided that the following conditions
822205Sjoerg# are met:
922205Sjoerg# 1. Redistributions of source code must retain the above copyright
1022205Sjoerg#    notice, this list of conditions and the following disclaimer.
1122205Sjoerg# 2. Redistributions in binary form must reproduce the above copyright
1222205Sjoerg#    notice, this list of conditions and the following disclaimer in the
1322205Sjoerg#    documentation and/or other materials provided with the distribution.
1422205Sjoerg#
1522205Sjoerg# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1622205Sjoerg# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1722205Sjoerg# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1822205Sjoerg# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
1922205Sjoerg# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2022205Sjoerg# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2122205Sjoerg# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2222205Sjoerg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2322205Sjoerg# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2422205Sjoerg# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2522205Sjoerg# SUCH DAMAGE.
2622205Sjoerg#
2750477Speter# $FreeBSD$
2822205Sjoerg#
2922205Sjoerg
3022205Sjoerg#
3122205Sjoerg# Read and decode a SCSI disk's primary or grown defect list.
3222205Sjoerg#
3322205Sjoerg
3422205Sjoergsub usage
3522205Sjoerg{
3622205Sjoerg    die "usage: scsi-defects raw-device-name [Glist|Plist]\n";
3722205Sjoerg}
3822205Sjoerg
3922205Sjoerg
4022205Sjoerg#
4122205Sjoerg# Main
4222205Sjoerg#
4322205Sjoerg
4422205Sjoerg&usage if $#ARGV < 0 || $#ARGV > 1;
4522205Sjoerg
4622205Sjoerg$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin";
4722205Sjoerg
4822205Sjoerg$dev = $ARGV[0];
4922205Sjoerg
5022205Sjoerg# generic device name given?
5122205Sjoergif ($dev =~ /^[so]d\d+$/) { $dev = "/dev/r${dev}.ctl"; }
5222205Sjoerg
5322205Sjoerg#
5422205Sjoerg# Select what you want to read.  PList include the primary defect list
5522205Sjoerg# from the factory.  GList is grown defects only.
5622205Sjoerg#
5722205Sjoergif ($#ARGV > 0) {
5822205Sjoerg    if ($ARGV[1] =~ /^[Gg]/) { $glist = 1; $plist = 0; }
5922205Sjoerg    elsif ($ARGV[1] =~ /^[Pp]/) { $glist = 0; $plist = 1; }
6022205Sjoerg    else { &usage; }
6122205Sjoerg} else {
6222205Sjoerg    $glist = 1; $plist = 0;
6322205Sjoerg}
6422205Sjoerg
6522205Sjoergopen(PIPE, "scsi -f $dev " .
6622205Sjoerg     "-c '{ Op code} 37 0 0:3 v:1 v:1 5:3 0 0 0 0 4:i2 0' $plist $glist " .
6722205Sjoerg     "-i 4 '{ stuff } *i2 { Defect list length } i2' |") ||
6822205Sjoerg    die "Cannot pipe to scsi(8)\n";
6922205Sjoergchop($amnt = <PIPE>);
7022205Sjoergclose(PIPE);
7122205Sjoerg
7222205Sjoergif ($amnt == 0) {
7322205Sjoerg    print "There are no defects (in this list).\n";
7422205Sjoerg    exit 0;
7522205Sjoerg}
7622205Sjoerg
7722205Sjoergprint "There are " . $amnt / 8 . " defects in this list.\n";
7822205Sjoerg
7922205Sjoerg$amnt += 4;
8022205Sjoerg
8122205Sjoergopen(PIPE, "scsi -f $dev " .
8222205Sjoerg     "-c '{ Op code} 37 0 0:3 v:1 v:1 5:3 0 0 0 0 v:i2 0' $plist $glist " .
8322205Sjoerg     "$amnt -i $amnt - |") ||
8422205Sjoerg    die "Cannot pipe to scsi(8)\n";
8522205Sjoerg
8622205Sjoergread(PIPE, $buf, 4);		# defect list header
8722205Sjoerg
8822205Sjoergprint "cylinder head  sector\n";
8922205Sjoerg
9022205Sjoergwhile(read(PIPE, $buf, 8)) {
9122205Sjoerg    ($cylhi, $cyllo, $head, $sec) = unpack("CnCN", $buf);
9222205Sjoerg    printf "%8u %4u  %6u\n", $cylhi*65536+$cyllo, $head, $sec;
9322205Sjoerg}
9422205Sjoergclose(PIPE);
95