File: scsi_win32.c

package info (click to toggle)
mtx 1.3.12-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,148 kB
  • sloc: ansic: 7,121; sh: 3,199; python: 203; perl: 117; makefile: 113
file content (355 lines) | stat: -rw-r--r-- 8,193 bytes parent folder | download | duplicates (5)
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
/* Copyright 2006-2008 Robert Nelson <robertn@the-nelsons.org>

$Date: 2008-08-19 03:03:38 -0700 (Tue, 19 Aug 2008) $
$Revision: 193 $

  This program is free software; you may redistribute and/or modify it under
  the terms of the GNU General Public License Version 2 as published by the
  Free Software Foundation.

  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 General Public License
  for complete details.

*/

/*
 * This is the SCSI commands for Windows.
 */

#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#ifdef _MSC_VER
#include <ntddscsi.h>
#else
#include <ddk/ntddscsi.h>
#endif

#define SCSI_DEFAULT_TIMEOUT	300		/* 1 minutes */
#define SCSI_MAX_TIMEOUT		108000	/* 30 hours */

typedef	struct	_HANDLE_ENTRY
{
	HANDLE  hDevice;
	UCHAR   PortId;
	UCHAR   PathId;
	UCHAR   TargetId;
	UCHAR   Lun;
} HANDLE_ENTRY, *PHANDLE_ENTRY;

PHANDLE_ENTRY	HandleTable = NULL;
int				nEntries = 0;

DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
{
	int		DeviceIndex;
	TCHAR	szDevicePath[256];

	int		nColons = 0;
	int		index;

	int		port, path, target, lun;

	for (DeviceIndex = 0; DeviceIndex < nEntries; DeviceIndex++)
	{
		if (HandleTable[DeviceIndex].hDevice == INVALID_HANDLE_VALUE)
			break;
	}

	if (DeviceIndex >= nEntries)
	{
		PHANDLE_ENTRY pNewTable;

		nEntries += 4;

		if (HandleTable == NULL)
		{
			pNewTable = (PHANDLE_ENTRY)malloc(nEntries * sizeof(HANDLE_ENTRY));
		}
		else
		{
			pNewTable = (PHANDLE_ENTRY)realloc(HandleTable, nEntries * sizeof(HANDLE_ENTRY));
		}

		if (pNewTable == NULL)
		{
			FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
		}

		HandleTable = pNewTable;
	}

	for (index = 0; DeviceName[index] != '\0'; index++)
	{
		if (DeviceName[index] == ':')
			nColons++;
		else if (DeviceName[index] < '0' || DeviceName[index] > '9')
			break;
	}

	if (DeviceName[index] == '\0' && nColons == 3 && 
		sscanf(DeviceName, "%d:%d:%d:%d", &port, &path, &target, &lun) == 4)
	{
		HandleTable[DeviceIndex].PortId = (UCHAR)port;
		HandleTable[DeviceIndex].PathId = (UCHAR)path;
		HandleTable[DeviceIndex].TargetId = (UCHAR)target;
		HandleTable[DeviceIndex].Lun = (UCHAR)lun;

		sprintf(szDevicePath, "\\\\.\\scsi%d:", port);
	}
	else 
	{
		int nPrefixLength = 0;

		if (DeviceName[0] != '\\') {
			memcpy(szDevicePath, "\\\\.\\", 4 * sizeof(TCHAR));
			nPrefixLength = 4;
		}

		HandleTable[DeviceIndex].PortId = 0;
		HandleTable[DeviceIndex].PathId = 0;
		HandleTable[DeviceIndex].TargetId = 0;
		HandleTable[DeviceIndex].Lun = 0;

		strncpy(&szDevicePath[nPrefixLength], 
				DeviceName, 
				sizeof(szDevicePath) / sizeof(TCHAR) - nPrefixLength - 1);

		szDevicePath[sizeof(szDevicePath) / sizeof(TCHAR) - 1] = '\0';
	}

	HandleTable[DeviceIndex].hDevice = CreateFile(szDevicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

	if (HandleTable[DeviceIndex].hDevice == INVALID_HANDLE_VALUE)
	{
		DWORD dwError = GetLastError();

#if DEBUG
		LPSTR lpszMessage;

		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, 0, (LPSTR)&lpszMessage, 0, NULL);
		fputs(lpszMessage, stderr);
#endif

		switch (dwError)
		{
		case ERROR_FILE_NOT_FOUND:
		case ERROR_PATH_NOT_FOUND:
			errno = ENOENT;
			break;

		case ERROR_TOO_MANY_OPEN_FILES:
			errno =  EMFILE;
			break;

		default:
		case ERROR_ACCESS_DENIED:
		case ERROR_SHARING_VIOLATION:
		case ERROR_LOCK_VIOLATION:
		case ERROR_INVALID_NAME:
			errno = EACCES;
			break;

		case ERROR_FILE_EXISTS:
			errno = EEXIST;
			break;

		case ERROR_INVALID_PARAMETER:
			errno = EINVAL;
			break;
		}

		FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
	}

	return DeviceIndex;
}

static int scsi_timeout = SCSI_DEFAULT_TIMEOUT;

void SCSI_Set_Timeout(int secs)
{
	if (secs > SCSI_MAX_TIMEOUT)
	{
		secs = SCSI_MAX_TIMEOUT;
	}

	scsi_timeout = secs;
}
 
void SCSI_Default_Timeout(void)
{
	scsi_timeout = SCSI_DEFAULT_TIMEOUT;
}

void SCSI_CloseDevice(char *DeviceName, DEVICE_TYPE DeviceFD)
{
	if (DeviceFD < nEntries)
	{
		CloseHandle(HandleTable[DeviceFD].hDevice);
		HandleTable[DeviceFD].hDevice = INVALID_HANDLE_VALUE;
	}
	else
	{
		errno = EBADF;
		FatalError("cannot close SCSI device '%s' - %m\n", DeviceName);
	}
}


/* Get the SCSI ID and LUN... */
scsi_id_t *SCSI_GetIDLun(DEVICE_TYPE fd)
{
	scsi_id_t *		retval;

	SCSI_ADDRESS	ScsiAddress;
	BOOL			bResult;
	DWORD			dwBytesReturned;

	if (fd < nEntries)
	{
		retval = (scsi_id_t *)xmalloc(sizeof(scsi_id_t));
		retval->id = HandleTable[fd].TargetId;
		retval->lun = HandleTable[fd].Lun;

#ifdef DEBUG
		fprintf(stderr,"SCSI:ID=%d LUN=%d\n", retval->id, retval->lun);
#endif
		return retval;
	}
	else
	{
		errno = EBADF;
		FatalError("cannot close SCSI device - %m\n");
	}

	memset(&ScsiAddress, 0, sizeof(ScsiAddress));

	ScsiAddress.Length = sizeof(ScsiAddress);

	bResult = DeviceIoControl(	HandleTable[fd].hDevice, 
								IOCTL_SCSI_GET_ADDRESS, 
								&ScsiAddress, sizeof(ScsiAddress), 
								&ScsiAddress, sizeof(ScsiAddress), 
								&dwBytesReturned, 
								NULL);

	if (!bResult)
	{
		return NULL;
	}

	retval = (scsi_id_t *)xmalloc(sizeof(scsi_id_t));
	retval->id = ScsiAddress.TargetId;
	retval->lun = ScsiAddress.Lun;

#ifdef DEBUG
	fprintf(stderr,"SCSI:ID=%d LUN=%d\n",retval->id,retval->lun);
#endif
	return retval;
}

int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
						Direction_T Direction,
						CDB_T *CDB,
						int CDB_Length,
						void *DataBuffer,
						int DataBufferLength,
						RequestSense_T *RequestSense)
{
	PSCSI_PASS_THROUGH ScsiPassThrough;

	const DWORD	dwDataBufferOffset = sizeof(SCSI_PASS_THROUGH) + (sizeof(RequestSense_T) + 3) / 4 * 4;
	const DWORD	dwBufferSize = dwDataBufferOffset + DataBufferLength;

	BOOL		bResult;
	DWORD		dwBytesReturned;
	DWORD		dwInputLength;
	DWORD		dwOutputLength;

	if (DeviceFD >= nEntries || HandleTable[DeviceFD].hDevice == INVALID_HANDLE_VALUE)
	{
		errno = EBADF;
		return -1;
	}

	ScsiPassThrough = (PSCSI_PASS_THROUGH)malloc(dwBufferSize);

	memset(ScsiPassThrough, 0, dwDataBufferOffset);

	ScsiPassThrough->Length = sizeof(SCSI_PASS_THROUGH);

	ScsiPassThrough->PathId = HandleTable[DeviceFD].PathId;
	ScsiPassThrough->TargetId = HandleTable[DeviceFD].TargetId;
	ScsiPassThrough->Lun = HandleTable[DeviceFD].Lun;
	ScsiPassThrough->CdbLength = (UCHAR)CDB_Length;
	ScsiPassThrough->DataIn = Direction == Input;
	ScsiPassThrough->DataBufferOffset = dwDataBufferOffset;
	ScsiPassThrough->DataTransferLength = DataBufferLength;
	ScsiPassThrough->SenseInfoOffset = sizeof(SCSI_PASS_THROUGH);
	ScsiPassThrough->SenseInfoLength = sizeof(RequestSense_T);
	ScsiPassThrough->TimeOutValue = scsi_timeout;

	memcpy(ScsiPassThrough->Cdb, CDB, CDB_Length);
	dwBytesReturned = 0;

	if (Direction == Output)
	{
		memcpy((void *)(((char *)ScsiPassThrough) + dwDataBufferOffset), DataBuffer, DataBufferLength);
		dwInputLength = dwBufferSize;
		dwOutputLength = dwDataBufferOffset;
	}
	else
	{
		dwInputLength = sizeof(SCSI_PASS_THROUGH);
		dwOutputLength = dwBufferSize;
	}

	bResult = DeviceIoControl(	HandleTable[DeviceFD].hDevice, 
								IOCTL_SCSI_PASS_THROUGH, 
								ScsiPassThrough, dwInputLength, 
								ScsiPassThrough, dwOutputLength, 
								&dwBytesReturned, 
								NULL);
	if (bResult)
	{
		if (ScsiPassThrough->ScsiStatus != 0)
		{
			memcpy(RequestSense, &ScsiPassThrough[1], sizeof(RequestSense_T));
#if DEBUG
			fprintf(stderr, "Command failed - ScsiStatus = %d\n", ScsiPassThrough->ScsiStatus);
			PrintRequestSense(RequestSense);
#endif
			bResult = false;
		}
		else
		{
			if (Direction == Input)
			{
				memcpy(	DataBuffer, 
						(void *)(((char *)ScsiPassThrough) + dwDataBufferOffset),
						DataBufferLength);
			}
		}
	}
	else
	{
#if DEBUG
		DWORD   dwError = GetLastError();
		LPSTR   lpszMessage;

		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, 0, (LPSTR)&lpszMessage, 0, NULL);
		fputs(lpszMessage, stderr);
		LocalFree(lpszMessage);
#endif

		memset(RequestSense, 0, sizeof(RequestSense_T));
	}

	free(ScsiPassThrough);

	return bResult ? 0 : -1;
}