-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscsi.cpp
More file actions
525 lines (450 loc) · 12.2 KB
/
Copy pathscsi.cpp
File metadata and controls
525 lines (450 loc) · 12.2 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <Python.h>
#include <structmember.h>
#ifdef MS_WINDOWS
#include <windows.h>
#include <ntddscsi.h>
#define SCSI_IOCTL_DATA_OUT 0
#define SCSI_IOCTL_DATA_IN 1
#define SCSI_IOCTL_DATA_IO_SIZE 80
#define TARGET_ID 1
#define SENSE_INFO_OFFSET 48
#endif
#ifdef linux
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
typedef int HANDLE;
#define INTERFACE_ID 'S'
#define INVALID_HANDLE_VALUE -1
#endif
#define SENSE_INFO_LENGTH 24
#define DEFAULT_TIMEOUT 30
/*
Internal Functions
*/
#ifdef MS_WINDOWS
static SCSI_PASS_THROUGH_DIRECT *getSPTD(unsigned char direction, unsigned timeout, unsigned char *cdb, unsigned cdb_size, void *data, unsigned data_size)
{
SCSI_PASS_THROUGH_DIRECT *sptd = (SCSI_PASS_THROUGH_DIRECT *) malloc(sizeof(SCSI_PASS_THROUGH_DIRECT));
memset(sptd, 0, sizeof(SCSI_PASS_THROUGH_DIRECT));
sptd->Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
sptd->TargetId = TARGET_ID;
sptd->DataIn = direction;
sptd->TimeOutValue = timeout;
sptd->DataBuffer = data;
sptd->DataTransferLength = data_size;
memcpy(sptd->Cdb, cdb, cdb_size);
sptd->CdbLength = cdb_size;
sptd->SenseInfoLength = SENSE_INFO_LENGTH;
sptd->SenseInfoOffset = SENSE_INFO_OFFSET;
return sptd;
}
#endif
#ifdef linux
static sg_io_hdr_t *getSGHDR(unsigned char direction, unsigned timeout, unsigned char *cdb, unsigned cdb_size, void *data, unsigned data_size)
{
sg_io_hdr_t *io_hdr = (sg_io_hdr_t *) malloc(sizeof(sg_io_hdr_t));
memset(io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr->interface_id = INTERFACE_ID;
io_hdr->dxfer_direction = direction;
io_hdr->timeout = timeout;
io_hdr->cmdp = cdb;
io_hdr->cmd_len = cdb_size;
io_hdr->dxferp = data;
io_hdr->dxfer_len = data_size;
return io_hdr;
}
#endif
/*
SCSI Device Object
*/
typedef struct {
PyObject_HEAD
PyObject *timeout;
PyObject *handle;
} SCSI_Object;
static void
SCSI_dealloc(SCSI_Object* self)
{
Py_XDECREF(self->handle);
Py_XDECREF(self->timeout);
self->ob_type->tp_free((PyObject*)self);
}
/*
SCSI Object methods
*/
static PyObject *
SCSI_read(PyObject *self, PyObject *args)
{
unsigned char *cdb;
void *data;
unsigned int cdb_size, data_size;
int timeout, status;
HANDLE handle;
PyObject *response;
#ifdef MS_WINDOWS
DWORD BytesReturned = NULL;
SCSI_PASS_THROUGH_DIRECT *sptd;
#endif
#ifdef linux
sg_io_hdr_t *io_hdr;
#endif
if (!PyArg_ParseTuple(args, "s#I", &cdb, &cdb_size, &data_size))
return NULL;
PyArg_Parse(((SCSI_Object *)self)->timeout, "i", &timeout);
PyArg_Parse(((SCSI_Object *)self)->handle, "i", &handle);
data = (void *) malloc(sizeof(char)*data_size);
memset(data, 0, sizeof(char)*data_size);
// check if handle is invalid
#ifdef MS_WINDOWS
if ((HANDLE)handle == INVALID_HANDLE_VALUE)
{
Py_INCREF(Py_None);
return Py_None;
}
#endif
#ifdef linux
if (handle < 0)
{
Py_INCREF(Py_None);
return Py_None;
}
#endif
// send read to scsi device
#ifdef MS_WINDOWS
sptd = getSPTD(SCSI_IOCTL_DATA_IN, timeout, cdb, cdb_size, data, data_size);
status = DeviceIoControl(handle, IOCTL_SCSI_PASS_THROUGH_DIRECT, sptd, SCSI_IOCTL_DATA_IO_SIZE, sptd, SCSI_IOCTL_DATA_IO_SIZE, &BytesReturned, 0);
if (status)
{
// success
response = Py_BuildValue("s#", data, data_size);
free(data);
free(sptd);
return response;
}
else
{
// failure
// TODO: handle better --> throw error according to GetLastError() and/or status ?
free(data);
free(sptd);
Py_INCREF(Py_None);
return Py_None;
}
#endif
#ifdef linux
io_hdr = getSGHDR(SG_DXFER_FROM_DEV, timeout, cdb, cdb_size, data, data_size);
status = ioctl(handle, SG_IO, io_hdr);
// handle this better
// TODO: throw error like: printf("ioctl error: errno=%d (%s)\n", errno, strerror(errno));
// may be able to handle even better: http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/pexample.html
if (status < 0) {
// failure
free(data);
free(io_hdr);
Py_INCREF(Py_None);
return Py_None;
}
else {
// success
response = Py_BuildValue("s#", data, data_size);
free(data);
free(io_hdr);
return response;
}
#endif
}
static PyObject *
SCSI_write(PyObject *self, PyObject *args)
{
unsigned char *cdb;
void *data;
unsigned int cdb_size, data_size;
int timeout, status;
HANDLE handle;
PyObject *response;
#ifdef MS_WINDOWS
DWORD BytesReturned = NULL;
SCSI_PASS_THROUGH_DIRECT *sptd;
#endif
#ifdef linux
sg_io_hdr_t *io_hdr;
#endif
if (!PyArg_ParseTuple(args, "s#s#", &cdb, &cdb_size, &data, &data_size))
return NULL;
PyArg_Parse(((SCSI_Object *)self)->timeout, "i", &timeout);
PyArg_Parse(((SCSI_Object *)self)->handle, "i", &handle);
// check if handle is invalid
#ifdef MS_WINDOWS
if (handle == INVALID_HANDLE_VALUE)
{
Py_INCREF(Py_None);
return Py_None;
}
#endif
#ifdef linux
if (handle <= INVALID_HANDLE_VALUE)
{
Py_INCREF(Py_None);
return Py_None;
}
#endif
// send write to scsi device
#ifdef MS_WINDOWS
sptd = getSPTD(SCSI_IOCTL_DATA_OUT, timeout, cdb, cdb_size, data, data_size);
status = DeviceIoControl(handle, IOCTL_SCSI_PASS_THROUGH_DIRECT, sptd, SCSI_IOCTL_DATA_IO_SIZE, sptd, SCSI_IOCTL_DATA_IO_SIZE, &BytesReturned, 0);
// TODO: handle errors better --> throw error (if(!status)) according to GetLastError() and/or status ?
response = Py_BuildValue("i", status);
free(sptd);
return response;
#endif
#ifdef linux
io_hdr = getSGHDR(SG_DXFER_TO_DEV, timeout, cdb, cdb_size, data, data_size);
status = ioctl(handle, SG_IO, &io_hdr);
// handle this better
// TODO: throw error like: printf("ioctl error: errno=%d (%s)\n", errno, strerror(errno));
// may be able to handle even better: http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/pexample.html
if (status < 0) {
// failure
response = Py_BuildValue("i", 0);
}
else {
// success
response = Py_BuildValue("i", 1);
}
free(io_hdr);
return response;
#endif
}
static PyObject *
SCSI_close(PyObject *self)
{
PyObject *new_handle, *tmp;
int handle;
if (!PyArg_Parse(((SCSI_Object *)self)->handle, "i", &handle))
{
Py_INCREF(Py_None);
return Py_None;
}
#ifdef MS_WINDOWS
if ((HANDLE)handle != INVALID_HANDLE_VALUE)
{
new_handle = Py_BuildValue("i", (int)INVALID_HANDLE_VALUE);
tmp = ((SCSI_Object *)self)->handle;
Py_INCREF(new_handle);
((SCSI_Object *)self)->handle = new_handle;
Py_XDECREF(tmp);
CloseHandle(tmp);
}
#endif
#ifdef linux
if (handle > INVALID_HANDLE_VALUE)
{
new_handle = Py_BuildValue("i", -1);
tmp = ((SCSI_Object *)self)->handle;
Py_INCREF(new_handle);
((SCSI_Object *)self)->handle = new_handle;
Py_XDECREF(tmp);
PyArg_Parse(tmp, "i", &handle);
close(handle);
}
#endif
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
SCSI_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
SCSI_Object *self;
self = (SCSI_Object *)type->tp_alloc(type, 0);
if(self != NULL){
self->timeout = Py_BuildValue("i", DEFAULT_TIMEOUT);
if(self->timeout == NULL){
Py_DECREF(self);
return NULL;
}
#ifdef MS_WINDOWS
self->handle = Py_BuildValue("i", INVALID_HANDLE_VALUE);
#endif
#ifdef linux
self->handle = Py_BuildValue("i", INVALID_HANDLE_VALUE);
#endif
}
return (PyObject *)self;
}
static int
SCSI_init(SCSI_Object *self, PyObject *args, PyObject *kwds)
{
int _timeout = DEFAULT_TIMEOUT,
_handle;
PyObject *timeout, *handle, *tmp;
static char *kwlist[] = {"handle", "timeout", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwlist,
&_handle, &_timeout))
return -1;
// handle
handle = Py_BuildValue("i", _handle);
tmp = self->handle;
Py_INCREF(handle);
self->handle = handle;
Py_DECREF(tmp);
// timeout
if (_timeout){
timeout = Py_BuildValue("i", _timeout);
}
else {
timeout = Py_BuildValue("i", DEFAULT_TIMEOUT);
}
tmp = self->timeout;
Py_INCREF(timeout);
self->timeout = timeout;
Py_DECREF(tmp);
return 0;
}
/*
SCSI Object Setup
*/
static PyMemberDef SCSI_members[] = {
{"handle", T_OBJECT_EX, offsetof(SCSI_Object, handle), 0, "SCSI device handle"},
{"timeout", T_OBJECT_EX, offsetof(SCSI_Object, timeout), 0, "timeout"},
{NULL} /* Sentinel */
};
static PyMethodDef SCSI_methods[] = {
{"write", SCSI_write, METH_VARARGS, "write(cdb, data) -> returns DeviceIoControl response integer"},
{"read", SCSI_read, METH_VARARGS, "read(cdb, data_size) -> returns read string of length data_size after sending cdb"},
{"close",(PyCFunction)SCSI_close, METH_NOARGS, "close() -> closes SCSI object handle"},
{NULL} /* Sentinel */
};
static PyTypeObject SCSIType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"scsi.SCSI", /*tp_name*/
sizeof(SCSI_Object), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)SCSI_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"SCSI handle object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
SCSI_methods, /* tp_methods */
SCSI_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)SCSI_init, /* tp_init */
0, /* tp_alloc */
SCSI_new, /* tp_new */
};
/*
Module Functions
*/
static PyObject *
scsi_open(PyObject *self, PyObject *args)
{
char *device_name;
HANDLE handle;
PyObject *SCSI_args = NULL;
PyObject *SCSI_kwds = NULL;
SCSI_Object *device;
if (!PyArg_ParseTuple(args, "s", &device_name))
return NULL;
#ifdef MS_WINDOWS
#if defined(UNICODE) || defined(_UNICODE)
wchar_t *path = (wchar_t *) malloc((strlen(device_name)/sizeof(char))*sizeof(wchar_t));
mbstowcs(path, device_name, strlen(device_name)+sizeof(char));
handle = CreateFile(path,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL
);
free(path);
#else
handle = CreateFile(device_name,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL
);
#endif
#endif
#ifdef linux
handle = open(device_name, O_RDWR);
#endif
#ifdef MS_WINDOWS
if (handle == INVALID_HANDLE_VALUE){
Py_INCREF(Py_None);
return Py_None;
}
#endif
#ifdef linux
if (handle <= INVALID_HANDLE_VALUE){
Py_INCREF(Py_None);
return Py_None;
}
#endif
Py_INCREF(&SCSIType);
device = (SCSI_Object *) SCSI_new(&SCSIType, NULL, NULL);
SCSI_args = Py_BuildValue("ii", (int) handle, DEFAULT_TIMEOUT);
if(SCSI_init(device, SCSI_args, SCSI_kwds)){
Py_INCREF(Py_None);
return Py_None;
}
return (PyObject *)device;
}
/*
Module Setup
*/
static PyMethodDef scsi_Methods[] = {
{"open", scsi_open, METH_VARARGS, "Returns a SCSI device object if succeeds, otherwise returns None."},
{NULL, NULL, 0, NULL}
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
initscsi(void)
{
PyObject* m;
if (PyType_Ready(&SCSIType) < 0)
return;
m = Py_InitModule3("scsi", scsi_Methods, "Creates a SCSI object.");
if (m == NULL)
return;
Py_INCREF(&SCSIType);
PyModule_AddObject(m, "SCSI", (PyObject *)&SCSIType);
}
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
initscsi();
}