1 /*
2 * Copyright (C) 1999-2018 D. Gilbert
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 *
10 * This is a simple program executing a SCSI INQUIRY command and a
11 * TEST UNIT READY command using the SCSI generic (sg) driver
12 * There is another variant of this program called "sg_simple2"
13 * which does not include the sg_lib.h header and logic and so has
14 * simpler but more primitive error processing.
15 * In the lk 2.6 series devices nodes such as /dev/sda also support
16 * the SG_IO ioctl.
17 *
18 * Invocation: sg_simple1 [-x] <scsi_device>
19 *
20 * Version 3.60 (20181207)
21 *
22 * 6 byte INQUIRY command:
23 * [0x12][ |lu][pg cde][res ][al len][cntrl ]
24 *
25 * 6 byte TEST UNIT READY command:
26 * [0x00][ |lu][res ][res ][res ][res ]
27 *
28 */
29
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include "sg_lib.h"
40 #include "sg_io_linux.h"
41
42
43 #define INQ_REPLY_LEN 96
44 #define INQ_CMD_LEN 6
45 #define TUR_CMD_LEN 6
46
47 #define EBUFF_SZ 256
48
main(int argc,char * argv[])49 int main(int argc, char * argv[])
50 {
51 int sg_fd, k, ok;
52 unsigned char inq_cdb [INQ_CMD_LEN] =
53 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
54 unsigned char tur_cdb [TUR_CMD_LEN] =
55 {0x00, 0, 0, 0, 0, 0};
56 unsigned char inqBuff[INQ_REPLY_LEN];
57 sg_io_hdr_t io_hdr;
58 char * file_name = 0;
59 char ebuff[EBUFF_SZ];
60 unsigned char sense_buffer[32];
61 int do_extra = 0;
62
63 for (k = 1; k < argc; ++k) {
64 if (0 == memcmp("-x", argv[k], 2))
65 do_extra = 1;
66 else if (*argv[k] == '-') {
67 printf("Unrecognized switch: %s\n", argv[k]);
68 file_name = 0;
69 break;
70 }
71 else if (0 == file_name)
72 file_name = argv[k];
73 else {
74 printf("too many arguments\n");
75 file_name = 0;
76 break;
77 }
78 }
79 if (0 == file_name) {
80 printf("Usage: 'sg_simple1 [-x] <sg_device>'\n");
81 return 1;
82 }
83
84 /* N.B. An access mode of O_RDWR is required for some SCSI commands */
85 if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
86 snprintf(ebuff, EBUFF_SZ,
87 "sg_simple1: error opening file: %s", file_name);
88 perror(ebuff);
89 return 1;
90 }
91 /* Just to be safe, check we have a new sg device by trying an ioctl */
92 if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
93 printf("sg_simple1: %s doesn't seem to be an new sg device\n",
94 file_name);
95 close(sg_fd);
96 return 1;
97 }
98
99 /* Prepare INQUIRY command */
100 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
101 io_hdr.interface_id = 'S';
102 io_hdr.cmd_len = sizeof(inq_cdb);
103 /* io_hdr.iovec_count = 0; */ /* memset takes care of this */
104 io_hdr.mx_sb_len = sizeof(sense_buffer);
105 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
106 io_hdr.dxfer_len = INQ_REPLY_LEN;
107 io_hdr.dxferp = inqBuff;
108 io_hdr.cmdp = inq_cdb;
109 io_hdr.sbp = sense_buffer;
110 io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
111 /* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
112 /* io_hdr.pack_id = 0; */
113 /* io_hdr.usr_ptr = NULL; */
114
115 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
116 perror("sg_simple1: Inquiry SG_IO ioctl error");
117 close(sg_fd);
118 return 1;
119 }
120
121 /* now for the error processing */
122 ok = 0;
123 switch (sg_err_category3(&io_hdr)) {
124 case SG_LIB_CAT_CLEAN:
125 ok = 1;
126 break;
127 case SG_LIB_CAT_RECOVERED:
128 printf("Recovered error on INQUIRY, continuing\n");
129 ok = 1;
130 break;
131 default: /* won't bother decoding other categories */
132 sg_chk_n_print3("INQUIRY command error", &io_hdr, 1);
133 break;
134 }
135
136 if (ok) { /* output result if it is available */
137 char * p = (char *)inqBuff;
138 int f = (int)*(p + 7);
139 printf("Some of the INQUIRY command's results:\n");
140 printf(" %.8s %.16s %.4s ", p + 8, p + 16, p + 32);
141 printf("[wide=%d sync=%d cmdque=%d sftre=%d]\n",
142 !!(f & 0x20), !!(f & 0x10), !!(f & 2), !!(f & 1));
143 /* Extra info, not necessary to look at */
144 if (do_extra)
145 printf("INQUIRY duration=%u millisecs, resid=%d, msg_status=%d\n",
146 io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
147 }
148
149
150 /* Prepare TEST UNIT READY command */
151 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
152 io_hdr.interface_id = 'S';
153 io_hdr.cmd_len = sizeof(tur_cdb);
154 io_hdr.mx_sb_len = sizeof(sense_buffer);
155 io_hdr.dxfer_direction = SG_DXFER_NONE;
156 io_hdr.cmdp = tur_cdb;
157 io_hdr.sbp = sense_buffer;
158 io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
159
160 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
161 perror("sg_simple1: Test Unit Ready SG_IO ioctl error");
162 close(sg_fd);
163 return 1;
164 }
165
166 /* now for the error processing */
167 ok = 0;
168 switch (sg_err_category3(&io_hdr)) {
169 case SG_LIB_CAT_CLEAN:
170 ok = 1;
171 break;
172 case SG_LIB_CAT_RECOVERED:
173 printf("Recovered error on Test Unit Ready, continuing\n");
174 ok = 1;
175 break;
176 default: /* won't bother decoding other categories */
177 sg_chk_n_print3("Test Unit Ready command error", &io_hdr, 1);
178 break;
179 }
180
181 if (ok)
182 printf("Test Unit Ready successful so unit is ready!\n");
183 else
184 printf("Test Unit Ready failed so unit may _not_ be ready!\n");
185
186 if (do_extra)
187 printf("TEST UNIT READY duration=%u millisecs, resid=%d, "
188 "msg_status=%d\n", io_hdr.duration, io_hdr.resid,
189 (int)io_hdr.msg_status);
190
191 close(sg_fd);
192 return 0;
193 }
194