-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscsi.cpp
534 lines (490 loc) · 12.6 KB
/
scsi.cpp
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
526
527
528
529
530
531
532
533
534
/**
** scsi.cpp
** Saturday. September 21, 2002
**
** Talk thru a Scsi connection to a device.
**
** Provide a single common interface for all the types of Scsi
** connection known to be available locally. Choose by call:
**
** 1: pass thru to all
** 2: pass thru til one succeeds
** 3: pass thru only to the open connection
**
** Grep views include:
**
** grep "^[A-Za-z_]" // routines
**/
#ifndef CHECK_CDB_LENGTH
#define CHECK_CDB_LENGTH 1 // default is to validate CDB length
#endif
/**
** Link with standard C libraries.
**/
#include <limits.h> /* INT_MAX ... */
#include <stdio.h> /* FILE ... */
#include <stdlib.h> /* calloc ... */
#include <string.h> /* memset ... */
/**
** Link with the *.c* of ../plscsi/.
**/
#include "plscsi.h"
/**
** Describe a Scsi connection to a device.
**/
struct Scsi /* connection */
{
void * theConnection;
#ifdef STUC
Stuc * theStuc; /* MacOS X */
#endif
#ifdef SGIO
Sgio * theSgio; /* Linux since kernel 2.4 */
#endif
#ifdef XXXASPI
Aspi * theAspi; /* Dos/Win 95/98/ME */
#endif
#ifdef SPTX
Sptx * theSptx; /* Win NT/2K/XP */
#endif
};
/**
** Construct a new Scsi connection.
**
** Return null to fail, else don't.
**/
Scsi * newScsi(void)
{
/* Alloc and zero a new Scsi. */
Scsi * scsi = (Scsi *) calloc(1, sizeof (Scsi));
if (scsi == NULL) return NULL;
/* Say closed but allow open. */
scsi->theConnection = NULL;
#ifdef STUC
scsi->theStuc = newStuc();
#endif
#ifdef SGIO
scsi->theSgio = newSgio();
#endif
#ifdef XXXASPI
scsi->theAspi = newAspi();
#endif
#ifdef SPTX
int maxSptLength = -1; /* -1 = don't care */
scsi->theSptx = newSptx(maxSptLength);
#endif
/* Succeed. */
return scsi;
}
/**
** Redirect the stderr of this Scsi connection.
**/
void scsiSetErr(Scsi * scsi, FILE * fi)
{
if (scsi == NULL) return;
#ifdef STUC
stucSetErr(scsi->theStuc, fi);
#endif
#ifdef SGIO
sgioSetErr(scsi->theSgio, fi);
#endif
#ifdef XXXASPI
aspiSetErr(scsi->theAspi, fi);
#endif
#ifdef SPTX
sptxSetErr(scsi->theSptx, fi);
#endif
}
/**
** Close this Scsi connection.
**/
void scsiClose(Scsi * scsi)
{
if (scsi == NULL) return;
#ifdef STUC
stucClose(scsi->theStuc);
#endif
#ifdef SGIO
sgioClose(scsi->theSgio);
#endif
#ifdef XXXASPI
;
#endif
#ifdef SPTX
sptxClose(scsi->theSptx);
#endif
scsi->theConnection = NULL;
}
/**
** Open this Scsi connection.
**
** Return zero to succeed, else don't.
**/
int scsiOpen(Scsi * scsi, char const * name)
{
if (scsi == NULL) return -1;
scsi->theConnection = NULL;
#ifdef STUC
if (stucOpen(scsi->theStuc, name) == 0)
{
scsi->theConnection = scsi->theStuc;
return 0;
}
#endif
#ifdef SGIO
if (sgioOpen(scsi->theSgio, name) == 0)
{
scsi->theConnection = scsi->theSgio;
return 0;
}
#endif
#ifdef XXXASPI
if (aspiOpen(scsi->theAspi, name) == 0)
{
scsi->theConnection = scsi->theAspi;
return 0;
}
#endif
#ifdef SPTX
if (sptxOpen(scsi->theSptx, name) == 0)
{
scsi->theConnection = scsi->theSptx;
return 0;
}
#endif
return -1;
}
/**
** Limit the count of sense bytes copied In by scsiSay.
**
** Return zero if ok, else negative.
**/
int scsiLimitSense(Scsi * scsi, int maxSenseLength)
{
if (scsi == NULL) return -1;
#ifdef STUC
if (scsi->theConnection == scsi->theStuc)
{
int exitInt = stucLimitSense(scsi->theStuc, maxSenseLength);
return exitInt;
}
#endif
#ifdef SGIO
if (scsi->theConnection == scsi->theSgio)
{
int exitInt = sgioLimitSense(scsi->theSgio, maxSenseLength);
return exitInt;
}
#endif
#ifdef XXXASPI
if (scsi->theConnection == scsi->theAspi)
{
int exitInt = aspiLimitSense(scsi->theAspi, maxSenseLength);
return exitInt;
}
#endif
#ifdef SPTX
if (scsi->theConnection == scsi->theSptx)
{
int exitInt = sptxLimitSense(scsi->theSptx, maxSenseLength);
return exitInt;
}
#endif
return -1;
}
/**
** Specify the min time to wait before cancelling scsiSay.
**
** Let negative time mean allow the max possible time.
**
** Return zero if ok, else negative.
**/
int scsiLimitSeconds(Scsi * scsi, INT s, INT ns)
{
if (scsi == NULL) return -1;
#ifdef STUC
if (scsi->theConnection == scsi->theStuc)
{
int exitInt = stucLimitSeconds(scsi->theStuc, s, ns);
return exitInt;
}
#endif
#ifdef SGIO
if (scsi->theConnection == scsi->theSgio)
{
int exitInt = sgioLimitSeconds(scsi->theSgio, s, ns);
return exitInt;
}
#endif
#ifdef DOSASPI
s = s; ns = ns;
return -1;
#endif
#ifdef WINASPI
if (scsi->theConnection == scsi->theAspi)
{
int exitInt = aspiLimitSeconds(scsi->theAspi, s, ns);
return exitInt;
}
#endif
#ifdef SPTX
if (scsi->theConnection == scsi->theSptx)
{
int exitInt = sptxLimitSeconds(scsi->theSptx, s, ns);
return exitInt;
}
#endif
return -1;
}
/**
** Speak a sentence of Scsi through a Scsi connection.
**
** That is, copy the cdbLength of the cdbChars Out, copy between zero
** and the maxLength of the dataChars In or Out, copy status In, and
** copy sense bytes In if available.
**
** Return zero if all was ok, positive to say specifically how many
** less than all data bytes were copied, else negative. Also return
** zero if all was ok except that the number of data bytes copied is
** unknown.
**/
INT scsiSay(Scsi * scsi,
char const * cdbChars, int cdbLength, char * dataChars, INT maxLength, int direction)
{
if (scsi == NULL) return -1;
#if CHECK_CDB_LENGTH
{
int officialCDBLength;
int i;
static unsigned char CDBLength[256] =
{
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // 0 000
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // 1
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 2 001
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 3
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 4 010
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, // 5
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // 6 011
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,98, // 7
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, // 8 100
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, // 9
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, // a 101
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, // b
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // c 110
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // d
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // e 111
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99 // f
};
// compute the official CDB length
officialCDBLength = CDBLength[(int) (cdbChars[0]&0xFF)];
switch (officialCDBLength)
{
case 99: // reserved or vendor specific, assume it's OK.
officialCDBLength = cdbLength;
break;
case 98: // special case for the 7F command.
officialCDBLength = ( (unsigned char *) cdbChars)[7] + 8;
officialCDBLength = (officialCDBLength + 3) & ~3;
break;
default: // official length properly set
break;
}
if (officialCDBLength != cdbLength)
{ // probably a programming error, warn the developer!!!
fprintf(stderr, "=== PLEASE REPORT FOLLOWING ERROR ===\n");
fprintf(stderr, "CDB length should be %lu instead of %lu:\n", (unsigned long) officialCDBLength, (unsigned long) cdbLength);
cdbLength = officialCDBLength;
for (i = 0; i < cdbLength; i++)
{
fprintf(stderr, "%02X ",( (unsigned char *) cdbChars)[i]);
}
fprintf(stderr, "\n");
}
}
#endif
#ifdef STUC
if (scsi->theConnection == scsi->theStuc)
{
int exitInt = stucSay(scsi->theStuc, cdbChars, cdbLength, dataChars, maxLength, direction);
return exitInt;
}
#endif
#ifdef SGIO
if (scsi->theConnection == scsi->theSgio)
{
int exitInt = sgioSay(scsi->theSgio, cdbChars, cdbLength, dataChars, maxLength, direction);
return exitInt;
}
#endif
#ifdef XXXASPI
if (scsi->theConnection == scsi->theAspi)
{
INT exitInt = aspiSay(scsi->theAspi, cdbChars, cdbLength, dataChars, maxLength, direction);
return exitInt;
}
#endif
#ifdef SPTX
if (scsi->theConnection == scsi->theSptx)
{
int exitInt = sptxSay(scsi->theSptx, cdbChars, cdbLength, dataChars, maxLength, direction);
return exitInt;
}
#endif
return -1;
}
/**
** Count the data bytes copied In or Out by scsiSay.
**
** Guess the elseLength if the actual length of data bytes copied is
** unknown. Also if the scsi is null.
**/
INT scsiGetLength(Scsi * scsi, INT elseLength)
{
if (scsi == NULL) return elseLength;
#ifdef STUC
if (scsi->theConnection == scsi->theStuc)
{
int exitInt = stucGetLength(scsi->theStuc, elseLength);
return exitInt;
}
#endif
#ifdef SGIO
if (scsi->theConnection == scsi->theSgio)
{
int exitInt = sgioGetLength(scsi->theSgio, elseLength);
return exitInt;
}
#endif
#ifdef XXXASPI
if (scsi->theConnection == scsi->theAspi)
{
INT exitInt = aspiGetLength(scsi->theAspi, elseLength);
return exitInt;
}
#endif
#ifdef SPTX
if (scsi->theConnection == scsi->theSptx)
{
int exitInt = sptxGetLength(scsi->theSptx, elseLength);
return exitInt;
}
#endif
return -1;
}
/**
** Copy some or all of the sense bytes copied by scsiSay.
**
** Guess the elseLength were copied if the length copied is unknown,
** except guess 0 if scsi is null.
**
** Copy the min of the charsLength with the actual or guessed copy
** length.
**
** Return the count of bytes copied.
**/
int scsiGetSense(Scsi * scsi, char * chars, int charsLength, int elseLength)
{
if (scsi == NULL) return 0;
#ifdef STUC
if (scsi->theConnection == scsi->theStuc)
{
int exitInt = stucGetSense(scsi->theStuc, chars, charsLength, elseLength);
return exitInt;
}
#endif
#ifdef SGIO
if (scsi->theConnection == scsi->theSgio)
{
int exitInt = sgioGetSense(scsi->theSgio, chars, charsLength, elseLength);
return exitInt;
}
#endif
#ifdef XXXASPI
if (scsi->theConnection == scsi->theAspi)
{
int exitInt = aspiGetSense(scsi->theAspi, chars, charsLength, elseLength);
return exitInt;
}
#endif
#ifdef SPTX
if (scsi->theConnection == scsi->theSptx)
{
int exitInt = sptxGetSense(scsi->theSptx, chars, charsLength, elseLength);
return exitInt;
}
#endif
return -1;
}
/**
** Read the first else the next well-known device path name.
**
** Copy a name of up to charsLength to the chars.
**
** Return the positive length of chars assigned, else zero to say the
** name was too long (or if chars is null), else negative if no more
** names exist (or if scsi is null).
**/
int scsiReadName(Scsi * scsi, char * chars, int charsLength)
{
if (scsi == NULL) return -1;
int exitInt = 0;
#ifdef STUC
exitInt = stucReadName(scsi->theStuc, chars, charsLength);
if (0 <= exitInt) return exitInt;
#endif
#ifdef SGIO
exitInt = sgioReadName(scsi->theSgio, chars, charsLength);
if (0 <= exitInt) return exitInt;
#endif
#ifdef XXXASPI
exitInt = aspiReadName(scsi->theAspi, chars, charsLength);
if (0 <= exitInt) return exitInt;
#endif
#ifdef SPTX
exitInt = sptxReadName(scsi->theSptx, chars, charsLength);
if (0 <= exitInt) return exitInt;
#endif
return -1;
}
/**
** Accept a -X option of a Scsi command line.
**
** Return zero if ok, else negative.
**/
int scsiSwallowArg(Scsi * scsi, char const * arg)
{
if (scsi == NULL) return -1;
if (arg == NULL) return -1;
int exitInt = -1;
#ifdef STUC
int stucInt = stucSwallowArg(scsi->theStuc, arg);
if (0 <= stucInt) exitInt = 0;
#endif
#ifdef SGIO
int sgioInt = sgioSwallowArg(scsi->theSgio, arg);
if (0 <= sgioInt) exitInt = 0;
#endif
#ifdef XXXASPI
int aspiInt = aspiSwallowArg(scsi->theAspi, arg);
if (0 <= aspiInt)
{
#ifdef SPTX
scsi->theSptx = NULL;
#endif
exitInt = 0;
}
#endif
#ifdef SPTX
int sptxInt = sptxSwallowArg(scsi->theSptx, arg);
if (0 <= sptxInt)
{
#ifdef XXXASPI
scsi->theAspi = NULL;
#endif
exitInt = 0;
}
#endif
return exitInt;
}
/* end of file */