-
Notifications
You must be signed in to change notification settings - Fork 0
/
scsi_inquiry.c
238 lines (213 loc) · 8.13 KB
/
scsi_inquiry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
*
* Copyright (c) 2007 Felix Bruns
* Parts Copyright (c) 1999 Douglas 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef WIN32
#include <tchar.h>
#include <stddef.h>
#include <windows.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
#endif
#include "scsi_inquiry.h"
int scsi_inquiry_get_data (int dev, unsigned char *buf, int buf_size)
{
#ifdef WIN32
HANDLE *fd;
SCSI_PASS_THROUGH_WITH_BUFFERS sptwb;
unsigned long returned, length;
wchar_t device[] = _TEXT("\\\\.\\PhysicalDriveX");
device[17] = dev + '0';
#else
int fd;
sg_io_hdr_t io_hdr;
unsigned char cdb[6] = {SCSI_INQUIRY_CMD, 1, 0xC0, 0, SCSI_INQUIRY_DBUFSIZE, 0};
unsigned char sbuf[SCSI_INQUIRY_SBUFSIZE];
unsigned char dbuf[SCSI_INQUIRY_DBUFSIZE];
char device[] = "/dev/sdX";
device[7] = dev + 'a';
#endif
int page_code, page_start, page_end, status, size = 0;
#ifdef WIN32
fd = CreateFile(device, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
#else
fd = open(device, O_RDONLY);
#endif
#ifdef WIN32
if (fd == INVALID_HANDLE_VALUE) {
#else
if (fd < 0) {
#endif
fprintf(stderr, "Error opening device.\n");
return 1;
}
else {
#ifdef WIN32
memset(&sptwb, 0, sizeof(SCSI_PASS_THROUGH_WITH_BUFFERS));
sptwb.Spt.Length = sizeof(SCSI_PASS_THROUGH);
sptwb.Spt.PathId = 0;
sptwb.Spt.TargetId = 1;
sptwb.Spt.Lun = 0;
sptwb.Spt.CdbLength = 6;
sptwb.Spt.SenseInfoLength = SCSI_INQUIRY_SBUFSIZE;
sptwb.Spt.DataIn = SCSI_IOCTL_DATA_IN;
sptwb.Spt.DataTransferLength = SCSI_INQUIRY_DBUFSIZE;
sptwb.Spt.TimeOutValue = SCSI_INQUIRY_TIMEOUT;
sptwb.Spt.DataBufferOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, dbuf);
sptwb.Spt.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, sbuf);
sptwb.Spt.Cdb[0] = SCSI_INQUIRY_CMD;
sptwb.Spt.Cdb[1] |= 1;
sptwb.Spt.Cdb[2] = 0xC0;
sptwb.Spt.Cdb[4] = SCSI_INQUIRY_DBUFSIZE;
length = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, dbuf) + sptwb.Spt.DataTransferLength;
status = DeviceIoControl(fd, IOCTL_SCSI_PASS_THROUGH, &sptwb, sizeof(SCSI_PASS_THROUGH), &sptwb, length, &returned, NULL);
#else
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = sizeof(cdb);
io_hdr.mx_sb_len = sizeof(sbuf);
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr.dxfer_len = SCSI_INQUIRY_DBUFSIZE;
io_hdr.dxferp = dbuf;
io_hdr.cmdp = cdb;
io_hdr.sbp = sbuf;
io_hdr.timeout = SCSI_INQUIRY_TIMEOUT * 1000;
status = ioctl(fd, SG_IO, &io_hdr);
#endif
if (status < 0) {
fprintf(stderr, "Error performing SCSI INQUIRY command.\n");
return 1;
}
else {
#ifdef WIN32
if (sptwb.dbuf[1] != 0xC0) {
fprintf(stderr, "Device didn't send expected data.\n");
return 1;
}
page_start = sptwb.dbuf[4];
page_end = sptwb.dbuf[3+sptwb.dbuf[3]];
#else
if (dbuf[1] != 0xC0) {
fprintf(stderr, "Device didn't send expected data.\n");
return 1;
}
page_start = dbuf[4];
page_end = dbuf[3+dbuf[3]];
#endif
for (page_code = page_start; page_code <= page_end; page_code++) {
#ifdef WIN32
memset(&sptwb, 0, sizeof(SCSI_PASS_THROUGH_WITH_BUFFERS));
sptwb.Spt.Length = sizeof(SCSI_PASS_THROUGH);
sptwb.Spt.PathId = 0;
sptwb.Spt.TargetId = 1;
sptwb.Spt.Lun = 0;
sptwb.Spt.CdbLength = 6;
sptwb.Spt.SenseInfoLength = SCSI_INQUIRY_SBUFSIZE;
sptwb.Spt.DataIn = SCSI_IOCTL_DATA_IN;
sptwb.Spt.DataTransferLength = SCSI_INQUIRY_DBUFSIZE;
sptwb.Spt.TimeOutValue = SCSI_INQUIRY_TIMEOUT;
sptwb.Spt.DataBufferOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, dbuf);
sptwb.Spt.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, sbuf);
sptwb.Spt.Cdb[0] = SCSI_INQUIRY_CMD;
sptwb.Spt.Cdb[1] |= 1;
sptwb.Spt.Cdb[2] = page_code;
sptwb.Spt.Cdb[4] = SCSI_INQUIRY_DBUFSIZE;
length = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, dbuf) + sptwb.Spt.DataTransferLength;
status = DeviceIoControl(fd, IOCTL_SCSI_PASS_THROUGH, &sptwb, sizeof(SCSI_PASS_THROUGH), &sptwb, length, &returned, NULL);
#else
io_hdr.cmdp[2] = page_code;
status = ioctl(fd, SG_IO, &io_hdr);
#endif
#ifdef WIN32
if (!status) {
fprintf(stderr, "Error performing SCSI INQUIRY command.\n");
return 1;
}
else {
if (size+sptwb.dbuf[3] > buf_size) return 1;
memcpy(buf+size, sptwb.dbuf+4, sptwb.dbuf[3]);
size += sptwb.dbuf[3];
}
#else
if (status < 0) {
fprintf(stderr, "Error performing SCSI INQUIRY command.\n");
return 1;
}
else {
if (size+dbuf[3] > buf_size) return 1;
memcpy(buf+size, dbuf+4, dbuf[3]);
size += dbuf[3];
}
#endif
}
}
buf[size] = '\0';
#ifdef WIN32
CloseHandle(fd);
#else
close(fd);
#endif
}
return 0;
}
int scsi_inquiry_get_hw_ver (int dev)
{
unsigned char buffer[10240], *ptr;
int err, ufid, hw_ver;
err = scsi_inquiry_get_data(dev, buffer, 10240);
if (err) {
hw_ver = -1;
}
else {
ptr = (unsigned char *)strstr((char *)buffer, "UpdaterFamilyID");
while (!isdigit(*ptr)) ptr++;
ufid = atoi((char *)ptr);
int ufid_hw_ver[] = { 0x0, // Unknown
0x1, // 1G/2G */*** (0x1 or 0x2)
0x3, // 3G *
0x4, // Mini 1G *
0x5, // 4G
0x6, // 4G Photo/Color
0x4, // Mini 1G *
0x7, // Mini 2G
0x0, // Unknown
0x0, // Unknown
0x5, // 4G
0x6, // 4G Photo/Color
0x0, // Unknown
0xB, // 5G
0xC, // Nano 1G
0x0, // Unknown
0x0, // Unknown
0xC, // Nano 1G
0x0, // Unknown
0x0, // Nano 2G **
0xB, // 5G
0x0, // Unknown
0x0, // Unknown
0x0, // Unknown
0x0, // Unknown
0x0, // 5.5G **
};
// * (Shouldn't respond to SCSI INQUIRY)
// ** (Not supported)
// *** (UpdaterFamilyID is same for 1G and 2G)
hw_ver = ufid_hw_ver[ufid];
}
return hw_ver;
}