1*44704f69SBart Van Assche /*
2*44704f69SBart Van Assche * Copyright (C) 1999-2018 D. Gilbert
3*44704f69SBart Van Assche * This program is free software; you can redistribute it and/or modify
4*44704f69SBart Van Assche * it under the terms of the GNU General Public License as published by
5*44704f69SBart Van Assche * the Free Software Foundation; either version 2, or (at your option)
6*44704f69SBart Van Assche * any later version.
7*44704f69SBart Van Assche *
8*44704f69SBart Van Assche * SPDX-License-Identifier: GPL-2.0-or-later
9*44704f69SBart Van Assche *
10*44704f69SBart Van Assche * Test code for D. Gilbert's extensions to the Linux OS SCSI generic ("sg")
11*44704f69SBart Van Assche * device driver.
12*44704f69SBart Van Assche * This program does a SCSI inquiry command on the given device and
13*44704f69SBart Van Assche * outputs some of the result. This program highlights the use of the
14*44704f69SBart Van Assche * SCSI_IOCTL_SEND_COMMAND ioctl. This should be able to be applied to
15*44704f69SBart Van Assche * any SCSI device file descriptor (not just one related to sg). [Whether
16*44704f69SBart Van Assche * this is a good idea on a disk while it is mounted is debatable.
17*44704f69SBart Van Assche * No detrimental effects when this was tested ...]
18*44704f69SBart Van Assche *
19*44704f69SBart Van Assche * Version 0.16 20181207
20*44704f69SBart Van Assche */
21*44704f69SBart Van Assche
22*44704f69SBart Van Assche #include <unistd.h>
23*44704f69SBart Van Assche #include <signal.h>
24*44704f69SBart Van Assche #include <fcntl.h>
25*44704f69SBart Van Assche #include <stdio.h>
26*44704f69SBart Van Assche #include <stdlib.h>
27*44704f69SBart Van Assche #include <string.h>
28*44704f69SBart Van Assche #include <errno.h>
29*44704f69SBart Van Assche #include <sys/ioctl.h>
30*44704f69SBart Van Assche #include <sys/types.h>
31*44704f69SBart Van Assche #include <sys/stat.h>
32*44704f69SBart Van Assche #include <scsi/scsi.h>
33*44704f69SBart Van Assche /* #include <scsi/scsi_ioctl.h> */ /* glibc hides this file sometimes */
34*44704f69SBart Van Assche
35*44704f69SBart Van Assche typedef struct my_scsi_ioctl_command {
36*44704f69SBart Van Assche unsigned int inlen; /* _excluding_ scsi command length */
37*44704f69SBart Van Assche unsigned int outlen;
38*44704f69SBart Van Assche unsigned char data[1]; /* was 0 but that's not ISO C!! */
39*44704f69SBart Van Assche /* on input, scsi command starts here then opt. data */
40*44704f69SBart Van Assche } My_Scsi_Ioctl_Command;
41*44704f69SBart Van Assche
42*44704f69SBart Van Assche #define OFF (2 * sizeof(unsigned int))
43*44704f69SBart Van Assche
44*44704f69SBart Van Assche #ifndef SCSI_IOCTL_SEND_COMMAND
45*44704f69SBart Van Assche #define SCSI_IOCTL_SEND_COMMAND 1
46*44704f69SBart Van Assche #endif
47*44704f69SBart Van Assche
48*44704f69SBart Van Assche #define INQUIRY_CMD 0x12
49*44704f69SBart Van Assche #define INQUIRY_CMDLEN 6
50*44704f69SBart Van Assche #define INQUIRY_REPLY_LEN 96
51*44704f69SBart Van Assche
52*44704f69SBart Van Assche
main(int argc,char * argv[])53*44704f69SBart Van Assche int main(int argc, char * argv[])
54*44704f69SBart Van Assche {
55*44704f69SBart Van Assche int s_fd, res, k, to;
56*44704f69SBart Van Assche unsigned char inq_cdb [INQUIRY_CMDLEN] = {INQUIRY_CMD, 0, 0, 0,
57*44704f69SBart Van Assche INQUIRY_REPLY_LEN, 0};
58*44704f69SBart Van Assche unsigned char * inqBuff = (unsigned char *)
59*44704f69SBart Van Assche malloc(OFF + sizeof(inq_cdb) + 512);
60*44704f69SBart Van Assche unsigned char * buffp = inqBuff + OFF;
61*44704f69SBart Van Assche My_Scsi_Ioctl_Command * ishp = (My_Scsi_Ioctl_Command *)inqBuff;
62*44704f69SBart Van Assche char * file_name = 0;
63*44704f69SBart Van Assche int do_nonblock = 0;
64*44704f69SBart Van Assche int oflags = 0;
65*44704f69SBart Van Assche
66*44704f69SBart Van Assche for (k = 1; k < argc; ++k) {
67*44704f69SBart Van Assche if (0 == strcmp(argv[k], "-n"))
68*44704f69SBart Van Assche do_nonblock = 1;
69*44704f69SBart Van Assche else if (*argv[k] != '-')
70*44704f69SBart Van Assche file_name = argv[k];
71*44704f69SBart Van Assche else {
72*44704f69SBart Van Assche printf("Unrecognized argument '%s'\n", argv[k]);
73*44704f69SBart Van Assche file_name = 0;
74*44704f69SBart Van Assche break;
75*44704f69SBart Van Assche }
76*44704f69SBart Van Assche }
77*44704f69SBart Van Assche if (0 == file_name) {
78*44704f69SBart Van Assche printf("Usage: 'scsi_inquiry [-n] <scsi_device>'\n");
79*44704f69SBart Van Assche printf(" where: -n open device in non-blocking mode\n");
80*44704f69SBart Van Assche printf(" Examples: scsi_inquiry /dev/sda\n");
81*44704f69SBart Van Assche printf(" scsi_inquiry /dev/sg0\n");
82*44704f69SBart Van Assche printf(" scsi_inquiry -n /dev/scd0\n");
83*44704f69SBart Van Assche return 1;
84*44704f69SBart Van Assche }
85*44704f69SBart Van Assche
86*44704f69SBart Van Assche if (do_nonblock)
87*44704f69SBart Van Assche oflags = O_NONBLOCK;
88*44704f69SBart Van Assche s_fd = open(file_name, oflags | O_RDWR);
89*44704f69SBart Van Assche if (s_fd < 0) {
90*44704f69SBart Van Assche if ((EROFS == errno) || (EACCES == errno)) {
91*44704f69SBart Van Assche s_fd = open(file_name, oflags | O_RDONLY);
92*44704f69SBart Van Assche if (s_fd < 0) {
93*44704f69SBart Van Assche perror("scsi_inquiry: open error");
94*44704f69SBart Van Assche return 1;
95*44704f69SBart Van Assche }
96*44704f69SBart Van Assche }
97*44704f69SBart Van Assche else {
98*44704f69SBart Van Assche perror("scsi_inquiry: open error");
99*44704f69SBart Van Assche return 1;
100*44704f69SBart Van Assche }
101*44704f69SBart Van Assche }
102*44704f69SBart Van Assche /* Don't worry, being very careful not to write to a none-scsi file ... */
103*44704f69SBart Van Assche res = ioctl(s_fd, SCSI_IOCTL_GET_BUS_NUMBER, &to);
104*44704f69SBart Van Assche if (res < 0) {
105*44704f69SBart Van Assche /* perror("ioctl on scsi device, error"); */
106*44704f69SBart Van Assche printf("scsi_inquiry: not a scsi device\n");
107*44704f69SBart Van Assche return 1;
108*44704f69SBart Van Assche }
109*44704f69SBart Van Assche
110*44704f69SBart Van Assche ishp->inlen = 0;
111*44704f69SBart Van Assche ishp->outlen = INQUIRY_REPLY_LEN;
112*44704f69SBart Van Assche memcpy(buffp, inq_cdb, INQUIRY_CMDLEN);
113*44704f69SBart Van Assche res = ioctl(s_fd, SCSI_IOCTL_SEND_COMMAND, inqBuff);
114*44704f69SBart Van Assche if (0 == res) {
115*44704f69SBart Van Assche to = (int)*(buffp + 7);
116*44704f69SBart Van Assche printf(" %.8s %.16s %.4s, byte_7=0x%x\n", buffp + 8,
117*44704f69SBart Van Assche buffp + 16, buffp + 32, to);
118*44704f69SBart Van Assche }
119*44704f69SBart Van Assche else if (res < 0)
120*44704f69SBart Van Assche perror("scsi_inquiry: SCSI_IOCTL_SEND_COMMAND err");
121*44704f69SBart Van Assche else
122*44704f69SBart Van Assche printf("scsi_inquiry: SCSI_IOCTL_SEND_COMMAND status=0x%x\n", res);
123*44704f69SBart Van Assche
124*44704f69SBart Van Assche res = close(s_fd);
125*44704f69SBart Van Assche if (res < 0) {
126*44704f69SBart Van Assche perror("scsi_inquiry: close error");
127*44704f69SBart Van Assche return 1;
128*44704f69SBart Van Assche }
129*44704f69SBart Van Assche return 0;
130*44704f69SBart Van Assche }
131