-
Notifications
You must be signed in to change notification settings - Fork 0
/
pslr_scsi_win.c
347 lines (310 loc) · 9.45 KB
/
pslr_scsi_win.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
pkTriggerCord
Copyright (C) 2011-2014 Andras Salamon <[email protected]>
Remote control of Pentax DSLR cameras.
based on:
PK-Remote
Remote control of Pentax DSLR cameras.
Copyright (C) 2008 Pontus Lidman <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
and GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include "pslr_scsi.h"
#define IOCTL_SCSI_PASS_THROUGH_DIRECT 0x4D014
#define SCSI_IOCTL_DATA_IN 1
#define SCSI_IOCTL_DATA_OUT 0
/*
#define IOCTL_STORAGE_QUERY_PROPERTY 0x2d1400
typedef enum _STORAGE_PROPERTY_ID
{
StorageDeviceProperty = 0,
StorageAdapterProperty,
StorageDeviceIdProperty,
StorageDeviceUniqueIdProperty, // See storduid.h for details
StorageDeviceWriteCacheProperty,
StorageMiniportProperty,
StorageAccessAlignmentProperty,
StorageDeviceSeekPenaltyProperty,
StorageDeviceTrimProperty,
StorageDeviceWriteAggregationProperty
} STORAGE_PROPERTY_ID;
typedef enum _STORAGE_QUERY_TYPE
{
PropertyStandardQuery = 0, // Retrieves the descriptor
PropertyExistsQuery, // Used to test whether the descriptor is supported
PropertyMaskQuery, // Used to retrieve a mask of writeable fields in the descriptor
PropertyQueryMaxDefined // use to validate the value
} STORAGE_QUERY_TYPE;
typedef struct _STORAGE_PROPERTY_QUERY
{
STORAGE_PROPERTY_ID PropertyId;
STORAGE_QUERY_TYPE QueryType;
BYTE AdditionalParameters[1];
} STORAGE_PROPERTY_QUERY;
typedef enum _STORAGE_BUS_TYPE {
BusTypeUnknown = 0x00,
BusTypeScsi,
BusTypeAtapi,
BusTypeAta,
BusType1394,
BusTypeSsa,
BusTypeFibre,
BusTypeUsb,
BusTypeRAID,
BusTypeiScsi,
BusTypeSas,
BusTypeSata,
BusTypeSd,
BusTypeMmc,
BusTypeVirtual,
BusTypeFileBackedVirtual,
BusTypeMax,
BusTypeMaxReserved = 0x7F
} STORAGE_BUS_TYPE;
typedef struct _STORAGE_DEVICE_DESCRIPTOR
{
DWORD Version;
DWORD Size;
BYTE DeviceType;
BYTE DeviceTypeModifier;
BOOLEAN RemovableMedia;
BOOLEAN CommandQueueing;
DWORD VendorIdOffset;
DWORD ProductIdOffset;
DWORD ProductRevisionOffset;
DWORD SerialNumberOffset;
STORAGE_BUS_TYPE BusType;
DWORD RawPropertiesLength;
BYTE RawDeviceProperties[1];
} STORAGE_DEVICE_DESCRIPTOR;
*/
typedef struct _SCSI_PASS_THROUGH_DIRECT
{
USHORT Length;
UCHAR ScsiStatus;
UCHAR PathId;
UCHAR TargetId;
UCHAR Lun;
UCHAR CdbLength;
UCHAR SenseInfoLength;
UCHAR DataIn;
ULONG DataTransferLength;
ULONG TimeOutValue;
PVOID DataBuffer;
ULONG SenseInfoOffset;
UCHAR Cdb[16];
} SCSI_PASS_THROUGH_DIRECT;
typedef struct _SCSI_PASS_THROUGH_WITH_BUFFER
{
SCSI_PASS_THROUGH_DIRECT sptd;
ULONG Filler; // realign buffers to double word boundary
UCHAR ucSenseBuf[32];
} SCSI_PASS_THROUGH_WITH_BUFFER;
char **get_drives(int *driveNum) {
char **ret;
ret = malloc( ('Z'-'C'+1) * sizeof(char *));
int driveLetter;
int j=0;
for( driveLetter = 'C'; driveLetter<='Z'; ++driveLetter ) {
ret[j] = malloc( 2 * sizeof (char) );
snprintf(ret[j], 2, "%c", driveLetter);
++j;
}
*driveNum = j;
return ret;
}
pslr_result get_drive_info(char* driveName, int* hDevice,
char* vendorId, int vendorIdSizeMax,
char* productId, int productIdSizeMax
)
{
bool Status;
STORAGE_PROPERTY_QUERY query;
STORAGE_DEVICE_DESCRIPTOR* pdescriptor;
byte descriptorBuf[256];
DWORD bytesRead;
pslr_result drive_status = PSLR_DEVICE_ERROR;
HANDLE hDrive;
char fullDriveName[7];
vendorId[0] = '\0';
productId[0] = '\0';
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
snprintf( fullDriveName, 7, "\\\\.\\%s:", driveName);
hDrive = CreateFile(fullDriveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDrive != INVALID_HANDLE_VALUE)
{
Status = DeviceIoControl(hDrive,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof(query),
&descriptorBuf,
sizeof(descriptorBuf),
&bytesRead,
(LPOVERLAPPED)0);
if (Status==FALSE)
{
int LastError = GetLastError(); //lastError alwasy return 1450
if(LastError != 0)
{
CancelIo(hDrive);
}
}
else
{
*hDevice = (int)hDrive;
drive_status = PSLR_OK;
pdescriptor = (STORAGE_DEVICE_DESCRIPTOR *)descriptorBuf;
if(pdescriptor->VendorIdOffset != 0) {
int i = 0;
while((descriptorBuf[pdescriptor->VendorIdOffset + i] != 0)
&&(i < vendorIdSizeMax)
) {
vendorId[i] = descriptorBuf[pdescriptor->VendorIdOffset + i];
i++;
}
vendorId[i]='\0';
}
if(pdescriptor->ProductIdOffset != 0) {
int i = 0;
while((descriptorBuf[pdescriptor->ProductIdOffset + i] != 0)
&&(i < productIdSizeMax)
) {
productId[i] = descriptorBuf[pdescriptor->ProductIdOffset + i];
i++;
}
productId[i]='\0';
}
}
}
return drive_status;
}
void close_drive(int *hDevice)
{
CloseHandle((HANDLE)*hDevice);
}
int scsi_read(int sg_fd, uint8_t *cmd, uint32_t cmdLen,
uint8_t *buf, uint32_t bufLen)
{
SCSI_PASS_THROUGH_WITH_BUFFER sptdwb;
DWORD outByte=0;
int Status;
int LastError=0;
uint8_t dataIn[64*1024];
sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
sptdwb.sptd.ScsiStatus = 0;
sptdwb.sptd.PathId = 0;
sptdwb.sptd.TargetId = 0;
sptdwb.sptd.Lun = 0;
sptdwb.sptd.CdbLength = cmdLen;
sptdwb.sptd.SenseInfoLength = sizeof(sptdwb.ucSenseBuf);
sptdwb.sptd.DataIn = SCSI_IOCTL_DATA_IN;
sptdwb.sptd.DataTransferLength = bufLen;
sptdwb.sptd.TimeOutValue = 10;
sptdwb.sptd.DataBuffer = dataIn;
sptdwb.sptd.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFER,ucSenseBuf);
memset(sptdwb.sptd.Cdb, 0, sizeof(sptdwb.sptd.Cdb));
memcpy(sptdwb.sptd.Cdb, cmd, cmdLen);
Status=DeviceIoControl((HANDLE)sg_fd,
IOCTL_SCSI_PASS_THROUGH_DIRECT,
&sptdwb,
sizeof(sptdwb),
&sptdwb,
sizeof(sptdwb),
&outByte ,
NULL);
if (Status==0)
{
LastError = GetLastError();
if(LastError != 0)
{
CancelIo((HANDLE)sg_fd);
}
}
memcpy(buf,sptdwb.sptd.DataBuffer,bufLen);
if(LastError != 0)
{
return -PSLR_SCSI_ERROR;
}
else
{
if (sptdwb.sptd.DataTransferLength == bufLen)
{
return bufLen;
}
else
{
return bufLen - sptdwb.sptd.DataTransferLength;
}
}
}
int scsi_write(int sg_fd, uint8_t *cmd, uint32_t cmdLen,
uint8_t *buf, uint32_t bufLen)
{
SCSI_PASS_THROUGH_WITH_BUFFER sptdwb;
DWORD outByte=0;
int Status;
int LastError=0;
sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
sptdwb.sptd.ScsiStatus = 0;
sptdwb.sptd.PathId = 0;
sptdwb.sptd.TargetId = 0;
sptdwb.sptd.Lun = 0;
sptdwb.sptd.CdbLength = cmdLen;
sptdwb.sptd.SenseInfoLength = sizeof(sptdwb.ucSenseBuf);
sptdwb.sptd.DataIn = SCSI_IOCTL_DATA_OUT;
sptdwb.sptd.DataTransferLength = bufLen;
sptdwb.sptd.TimeOutValue = 10;
sptdwb.sptd.DataBuffer = buf;
sptdwb.sptd.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFER,ucSenseBuf);
memset(sptdwb.sptd.Cdb, 0, sizeof(sptdwb.sptd.Cdb));
memcpy(sptdwb.sptd.Cdb, cmd, cmdLen);
Status=DeviceIoControl((HANDLE)sg_fd,
IOCTL_SCSI_PASS_THROUGH_DIRECT,
&sptdwb,
sizeof(sptdwb),
&sptdwb,
sizeof(sptdwb),
&outByte ,
NULL);
if (Status==0)
{
LastError = GetLastError();
if(LastError != 0)
{
CancelIo((HANDLE)sg_fd);
}
}
if(LastError != 0)
{
return PSLR_SCSI_ERROR;
}
else
{
return PSLR_OK;
}
}