Dec 312017
 
SCSI diagnostics for LINUX, C source code.

Full Description of File


C source code: SCSI diagnostics for LINUX


File LOOKSCSI.ZIP from The Programmer’s Corner in
Category Linux Files
SCSI diagnostics for LINUX, C source code.
File Name File Size Zip Size Zip Type
FILE_ID.DIZ 43 43 stored
LOOKSCSI.TXT 4067 1641 deflated

Download File LOOKSCSI.ZIP Here

Contents of the LOOKSCSI.TXT file



From: [email protected] (Lawrence Foard)
Newsgroups: comp.os.linux.development
Organization: The World Public Access UNIX, Brookline, MA


Sample generic scsi code that several people have requested follows:

Run this shell script first

setupsg:
#!/bin/sh
mknod /dev/sga c 21 0
mknod /dev/sgb c 21 1
mknod /dev/sgc c 21 2
mknod /dev/sgd c 21 3
mknod /dev/sge c 21 4
mknod /dev/sgf c 21 5
mknod /dev/sgg c 21 6
mknod /dev/sgh c 21 7
#Could be more if LUN's used
ln -s /usr/src/linux/drivers/scsi/sg.h /usr/include/linux
-----------------END-------------------------------------------

scsi.h:
/* list of SCSI commands taken from kernel code */

#define TEST_UNIT_READY 0x00
#define REZERO_UNIT 0x01
#define REQUEST_SENSE 0x03
#define FORMAT_UNIT 0x04
#define READ_BLOCK_LIMITS 0x05
#define REASSIGN_BLOCKS 0x07
#define READ_6 0x08
#define WRITE_6 0x0a
#define SEEK_6 0x0b
#define READ_REVERSE 0x0f
#define WRITE_FILEMARKS 0x10
#define SPACE 0x11
#define INQUIRY 0x12
#define RECOVER_BUFFERED_DATA 0x14
#define MODE_SELECT 0x15
#define RESERVE 0x16
#define RELEASE 0x17
#define COPY 0x18
#define ERASE 0x19
#define MODE_SENSE 0x1a
#define START_STOP 0x1b
#define RECEIVE_DIAGNOSTIC 0x1c
#define SEND_DIAGNOSTIC 0x1d
#define ALLOW_MEDIUM_REMOVAL 0x1e

#define READ_CAPACITY 0x25
#define READ_10 0x28
#define WRITE_10 0x2a
#define SEEK_10 0x2b
#define WRITE_VERIFY 0x2e
#define VERIFY 0x2f
#define SEARCH_HIGH 0x30
#define SEARCH_EQUAL 0x31
#define SEARCH_LOW 0x32
#define SET_LIMITS 0x33
#define PRE_FETCH 0x34
#define READ_POSITION 0x34
#define SYNCRONIZE_CACHE 0x35
#define LOCK_UNLOCK_CACHE 0x36
#define READ_DEFECT_DATA 0x37
#define COMPARE 0x39
#define COPY_VERIFY 0x3a
#define WRITE_BUFFER 0x3b
#define READ_BUFFER 0x3c
#define READ_LONG 0x3e
#define CHANGE_DEFINITION 0x40
#define LOG_SELECT 0x4c
#define LOG_SENSE 0x4d
#define MODE_SELECT_10 0x55
#define MODE_SENSE_10 0x5a

/* command packets */

struct inquiry
{
struct sg_header header;
unsigned char cmd;
unsigned char lun;
unsigned char junk1[2];
unsigned char ttf;
unsigned char junk2;
};

------------------------------------END-------------------------

Compile this program, it will list the scsi devices on your machine
using the inquiry SCSI command.

scsi.c:
/*
Simple sample program for generic scsi devices written by Lawrence Foard
this code is distributed as is, and may be used in anyway you wish,
however you must accept full responsibility for any and all problems
resulting from its use.
*/

#include
#include
#include
#include "scsi.h"

do_inquiry(int fd)
{
struct inquiry inquiry;
struct sg_header header;
char rbuff[300],*result;
int i;
inquiry.cmd=INQUIRY;
inquiry.lun=0;
inquiry.junk1[0]=inquiry.junk1[1]=inquiry.junk2=0;
inquiry.ttf=255;
inquiry.header.pack_len=sizeof(struct inquiry);
inquiry.header.reply_len=300;
/* printf("writting packet\n");
fflush(stdout);*/
if (-1==write(fd,&inquiry,sizeof(inquiry)))
{
perror("write");
exit(1);
}
/* printf("waiting for response\n");
fflush(stdout);*/
if (-1==read(fd,rbuff,300))
{
perror("read");
exit(1);
}
result=rbuff+sizeof(struct sg_header);
memcpy(&header,rbuff,sizeof(struct sg_header));
/* printf("%d %d\n",header.pack_len,header.reply_len);*/
printf("Type: %d\n",result[0]);
printf("Vendor: ");
for(i=8;i<16;i++)
{
if ((result[i]>=20) && i printf("%c",result[i]);
else
printf(" ");
}
printf("\nModel: ");
for(i=16;i<32;i++)
{
if ((result[i]>=20) && i printf("%c",result[i]);
else
printf(" ");
}
printf("\n");
}

main()
{
int fd;
char device;
char device_name[100];
for(device='a';device<='z';device++)
{
sprintf(device_name,"/dev/sg%c",device);
if (-1==(fd=open(device_name,O_RDWR)))
{
/* perror(device_name);*/
continue;
}
printf("\nDevice: %s\n",device_name);
do_inquiry(fd);
close(fd);
}
}

/* end */


 December 31, 2017  Add comments

Leave a Reply