diff options
author | Douglas Gilbert <dgilbert@interlog.com> | 2007-06-27 02:59:37 +0000 |
---|---|---|
committer | Douglas Gilbert <dgilbert@interlog.com> | 2007-06-27 02:59:37 +0000 |
commit | 666c11696c2674af4e765ba20e33d04bf01a1770 (patch) | |
tree | 6d08590913d6a23546b42489cf236379bbc445de /examples | |
parent | 0e9184cde6f3d1a0046c706051a4bd4b36a6aede (diff) | |
download | sg3_utils-666c11696c2674af4e765ba20e33d04bf01a1770.tar.gz |
Load sg3_utils-1.08 into trunk/.
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@41 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 5 | ||||
-rw-r--r-- | examples/sg_err.h | 64 | ||||
-rw-r--r-- | examples/sg_include.h | 12 | ||||
-rw-r--r-- | examples/sg_sense_test.c | 59 | ||||
-rw-r--r-- | examples/transport_ids.txt | 32 |
5 files changed, 161 insertions, 11 deletions
diff --git a/examples/Makefile b/examples/Makefile index 0cc7ef1c..477eca01 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -8,7 +8,7 @@ CC = gcc LD = gcc EXECS = sg_simple1 sg_simple2 sg_simple3 sg_simple4 sg_simple16 \ - sg_iovec_tst scsi_inquiry sg_excl + sg_iovec_tst scsi_inquiry sg_excl sg_sense_test # EXECS = sg_simple1 sg_simple2 sg_simple3 sg_simple4 sg_simple16 \ # sg_simple_aio sg_iovec_tst scsi_inquiry sg_excl @@ -60,6 +60,9 @@ scsi_inquiry: scsi_inquiry.o sg_excl: sg_excl.o ../sg_err.o $(LD) -o $@ $(LDFLAGS) $^ +sg_sense_test: sg_sense_test.o ../sg_err.o + $(LD) -o $@ $(LDFLAGS) $^ + install: $(EXECS) install -d $(INSTDIR) for name in $^; \ diff --git a/examples/sg_err.h b/examples/sg_err.h index 886bac29..2cfabb70 100644 --- a/examples/sg_err.h +++ b/examples/sg_err.h @@ -3,7 +3,7 @@ /* Feel free to copy and modify this GPL-ed code into your applications. */ -/* Version 0.93 (20040708) +/* Version 0.97 (20040830) */ @@ -126,20 +126,50 @@ extern int sg_chk_n_print(const char * leadin, int masked_status, int host_status, int driver_status, const unsigned char * sense_buffer, int sb_len); +/* This is a slightly stretched SCSI sense "descriptor" format header. + The addition is to allow the 0x70 and 0x71 response codes. The idea + is to place the salient data of both "fixed" and "descriptor" sense + format into one structure to ease application processing. + The original sense buffer should be kept around for those cases + in which more information is required (e.g. the LBA of a MEDIUM ERROR). */ +struct sg_scsi_sense_hdr { + unsigned char response_code; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */ + unsigned char sense_key; + unsigned char asc; + unsigned char ascq; + unsigned char byte4; + unsigned char byte5; + unsigned char byte6; + unsigned char additional_length; +}; + +/* Maps the salient data from a sense buffer which is in either fixed or + descriptor format into a structure mimicking a descriptor format + header (i.e. the first 8 bytes). + If zero response code returns 0. Otherwise returns 1 and if 'sshp' is + non-NULL then zero all fields and then set the appropriate fields in + that structure. sshp::additional_length is always 0 for response + codes 0x70 and 0x71 (fixed format). */ +extern int sg_scsi_normalize_sense(const unsigned char * sensep, + int sense_len, + struct sg_scsi_sense_hdr * sshp); + +/* Attempt to find the first SCSI sense data descriptor that matches the + given 'desc_type'. If found return pointer to start of sense data + descriptor; otherwise (including fixed format sense data) returns NULL. */ +extern const unsigned char * sg_scsi_sense_desc_find( + const unsigned char * sensep, int sense_len, int desc_type); + /* The following function declaration is for the sg version 3 driver. Only version 3 sg_err.c defines it. */ struct sg_io_hdr; extern int sg_chk_n_print3(const char * leadin, struct sg_io_hdr * hp); -/* If no sense data returns 0 and places 0 in *response_code (if non-NULL) - and *sense_key (if non-NULL). If sense data found returns 1 and outputs - to *response_code, *sense_key, *asc and *ascq (those that are non-NULL). - Understands both descriptor and fixed sense data format. */ -extern int sg_decode_sense(const struct sg_io_hdr * hp, - unsigned char * response_code, - unsigned char * sense_key, - unsigned char * asc, - unsigned char * ascq); +/* Calls sg_scsi_normalize_sense() after obtaining the sense buffer and + its length from the struct sg_io_hdr pointer. If these cannot be + obtained, 0 is returned. */ +extern int sg_normalize_sense(const struct sg_io_hdr * hp, + struct sg_scsi_sense_hdr * sshp); /* The following "category" function returns one of the following */ @@ -180,4 +210,18 @@ extern void sg_get_opcode_name(unsigned char cmd_byte0, int peri_type, /* Command name given opcode (byte 0) and service action of a command */ extern void sg_get_opcode_sa_name(unsigned char cmd_byte0, int service_action, int peri_type, int buff_len, char * buff); + + +/* <<< General purpose (i.e. not SCSI specific) utility functions >>> */ + +/* Always returns valid string even if errnum is wild (or library problem) */ +extern char * safe_strerror(int errnum); + + +/* Print (to stdout) 'str' of bytes in hex, 16 bytes per line optionally + followed at the right hand side of the line with an ASCII interpretation. + Each line is prefixed with an address, starting at 0 for str[0]..str[15]. + All output numbers are in hex. */ +extern void dStrHex(const char* str, int len, int no_ascii); + #endif diff --git a/examples/sg_include.h b/examples/sg_include.h index 6b6dd6f3..b7310ec0 100644 --- a/examples/sg_include.h +++ b/examples/sg_include.h @@ -1,3 +1,6 @@ +#ifndef SG_INCLUDES_H +#define SG_INCLUDES_H + #ifdef SG_KERNEL_INCLUDES #define __user typedef unsigned char u8; @@ -13,6 +16,13 @@ #endif #endif +#ifdef BLKGETSIZE64 + #ifndef u64 + #include <stdint.h> /* C99 header for exact integer types */ + typedef uint64_t u64; /* problems with BLKGETSIZE64 ioctl in lk 2.4 */ + #endif +#endif + /* Getting the correct include files for the sg interface can be an ordeal. In a perfect world, one would just write: @@ -42,3 +52,5 @@ dpg 20010415, 20030522 */ + +#endif diff --git a/examples/sg_sense_test.c b/examples/sg_sense_test.c new file mode 100644 index 00000000..39dcf5b0 --- /dev/null +++ b/examples/sg_sense_test.c @@ -0,0 +1,59 @@ +#include <unistd.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "sg_include.h" +#include "sg_err.h" + +/* This is a simple program that tests the sense data descriptor format + printout function in sg_err.c + +* Copyright (C) 2004 D. Gilbert +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. + +*/ + +#define EBUFF_SZ 256 + +#define ME "sg_sense_test: " + +int main(int argc, char * argv[]) +{ + unsigned char err1[] = {0x72, 0x5, 0x4, 0x1, 0, 0, 0, 32, + 0x2, 0x6, 0, 0, 0xc8, 0x0, 0x3, 0, + 0, 0xa, 0x80, 0, 1, 2, 3, 4, + 0xaa, 0xbb, 0xcc, 0xdd, + 1, 0xa, 0, 0, 1, 2, 3, 4, + 0xaa, 0xbb, 0xcc, 0xdd}; + unsigned char err2[] = {0x72, MEDIUM_ERROR, 0x4, 0x1, 0, 0, 0, 32, + 0x2, 0x6, 0, 0, 0xc8, 0x0, 0x3, 0, + 0, 0xa, 0x80, 0, 1, 2, 3, 4, + 0xaa, 0xbb, 0xcc, 0xdd, + 1, 0xa, 0, 0, 1, 2, 3, 4, + 0xaa, 0xbb, 0xcc, 0xdd}; + unsigned char err3[] = {0x72, NO_SENSE, 0x4, 0x1, 0, 0, 0, 8, + 0x2, 0x6, 0, 0, 0xc8, 0x0, 0x3, 0}; + unsigned char err4[] = {0x73, COPY_ABORTED, 0x4, 0x1, 0, 0, 0, 22, + 0x2, 0x6, 0, 0, 0xc8, 0x0, 0x3, 0, + 0x3, 0x2, 0, 0x55, + 0x5, 0x2, 0, 0x20, + 0x85, 0x4, 0, 0x20, 0x33, 0x44}; + unsigned char err5[] = {0xf1, 0, (0xf0 | ILLEGAL_REQUEST), 0x11, 0x22, + 0x33, 0x44, 0xa, + 0x0, 0x0, 0, 0, 0x4, 0x1, 0, 0xcf, 0, 5,}; + + sg_print_sense("err1 test", err1, sizeof(err1)); + sg_print_sense("\nerr2 test", err2, sizeof(err2)); + sg_print_sense("\nerr3 test", err3, sizeof(err3)); + sg_print_sense("\nerr4 test", err4, sizeof(err4)); + sg_print_sense("\nerr5 test", err5, sizeof(err5)); + return 0; +} diff --git a/examples/transport_ids.txt b/examples/transport_ids.txt new file mode 100644 index 00000000..ea2b8e70 --- /dev/null +++ b/examples/transport_ids.txt @@ -0,0 +1,32 @@ +# This file is an example for the sg_persist utility. +# That utility can take one or more "transportID"s from stdin when either +# the '--transport-id=-" or "-X -" option is given on the command line. + +# Here is a simple example (for SPI) of a comma separted hex list: +1,0,0,7,0,0,0,1 + + # Leading spaces and tabs before a '#' or ok + +# Leading spaces and tabs are ignored as are redundant separators (space, +# comma or tab). +# 1,2 ,,,3,4,5,6,7,8,9 + +# Playing around with an iSCSI transportID which is the only one defined +# (in SPC-3 rev20) that can be more than 24 bytes in length. +#5, 0, 0, 18, 41,42,43,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0 + +# Just playing around with protocol identifiers that aren't defined +#1c,22,33,44,55,66,77,88,99 +#a b , c d e f + +# 'report and move' requires one (and only one) transprtID. +# 'register' and 'register and ignore existing key' can optionally take +# one or more transportIDs. + +# This file should work for something like this: +# sg_persist --out --register-move --prout-type=1 --transport-id=- +# --param-rk=111 --param-sark=fff --unreg +# /dev/sda < examples/transport_ids.txt +# +# ... since there is only one line in this file that has a +# transportID on it that will be decoded (i.e. the "SPI" one above). |