-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Command.cs
558 lines (470 loc) · 23.9 KB
/
Command.cs
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Command.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : FeeBSD direct device access.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains a high level representation of the FreeBSD syscalls used to
// directly interface devices.
//
// --[ License ] --------------------------------------------------------------
//
// This library 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 2.1 of the
// License, or (at your option) any later version.
//
// This library 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 Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2017 Natalia Portillo
// ****************************************************************************/
using System;
using System.Runtime.InteropServices;
using DiscImageChef.Console;
using DiscImageChef.Decoders.ATA;
using static DiscImageChef.Devices.FreeBSD.Extern;
namespace DiscImageChef.Devices.FreeBSD
{
static class Command
{
const int CAM_MAX_CDBLEN = 16;
/// <summary>
/// Sends a SCSI command (64-bit arch)
/// </summary>
/// <returns>0 if no error occurred, otherwise, errno</returns>
/// <param name="dev">CAM device</param>
/// <param name="cdb">SCSI CDB</param>
/// <param name="buffer">Buffer for SCSI command response</param>
/// <param name="senseBuffer">Buffer with the SCSI sense</param>
/// <param name="timeout">Timeout in seconds</param>
/// <param name="direction">SCSI command transfer direction</param>
/// <param name="duration">Time it took to execute the command in milliseconds</param>
/// <param name="sense"><c>True</c> if SCSI error returned non-OK status and <paramref name="senseBuffer"/> contains SCSI sense</param>
internal static int SendScsiCommand64(IntPtr dev, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout, ccb_flags direction, out double duration, out bool sense)
{
senseBuffer = null;
duration = 0;
sense = false;
if(buffer == null)
return -1;
IntPtr ccbPtr = cam_getccb(dev);
IntPtr cdbPtr = IntPtr.Zero;
if(ccbPtr.ToInt64() == 0)
{
sense = true;
return Marshal.GetLastWin32Error();
}
ccb_scsiio64 csio = (ccb_scsiio64)Marshal.PtrToStructure(ccbPtr, typeof(ccb_scsiio64));
csio.ccb_h.func_code = xpt_opcode.XPT_SCSI_IO;
csio.ccb_h.flags = direction;
csio.ccb_h.xflags = 0;
csio.ccb_h.retry_count = 1;
csio.ccb_h.cbfcnp = IntPtr.Zero;
csio.ccb_h.timeout = timeout;
csio.data_ptr = Marshal.AllocHGlobal(buffer.Length);
csio.dxfer_len = (uint)buffer.Length;
csio.sense_len = 32;
csio.cdb_len = (byte)cdb.Length;
// TODO: Create enum?
csio.tag_action = 0x20;
csio.cdb_bytes = new byte[CAM_MAX_CDBLEN];
if(cdb.Length <= CAM_MAX_CDBLEN)
Array.Copy(cdb, 0, csio.cdb_bytes, 0, cdb.Length);
else
{
cdbPtr = Marshal.AllocHGlobal(cdb.Length);
byte[] cdbPtrBytes = BitConverter.GetBytes(cdbPtr.ToInt64());
Array.Copy(cdbPtrBytes, 0, csio.cdb_bytes, 0, IntPtr.Size);
csio.ccb_h.flags |= ccb_flags.CAM_CDB_POINTER;
}
csio.ccb_h.flags |= ccb_flags.CAM_DEV_QFRZDIS;
Marshal.Copy(buffer, 0, csio.data_ptr, buffer.Length);
Marshal.StructureToPtr(csio, ccbPtr, false);
DateTime start = DateTime.UtcNow;
int error = cam_send_ccb(dev, ccbPtr);
DateTime end = DateTime.UtcNow;
if(error < 0)
error = Marshal.GetLastWin32Error();
csio = (ccb_scsiio64)Marshal.PtrToStructure(ccbPtr, typeof(ccb_scsiio64));
if((csio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_REQ_CMP &&
(csio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_SCSI_STATUS_ERROR)
{
error = Marshal.GetLastWin32Error();
DicConsole.DebugWriteLine("FreeBSD devices", "CAM status {0} error {1}", csio.ccb_h.status, error);
sense = true;
}
if((csio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_SCSI_STATUS_ERROR)
{
sense = true;
senseBuffer = new byte[1];
senseBuffer[0] = csio.scsi_status;
}
if((csio.ccb_h.status & cam_status.CAM_AUTOSNS_VALID) != 0)
{
if(csio.sense_len - csio.sense_resid > 0)
{
sense = (csio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_SCSI_STATUS_ERROR;
senseBuffer = new byte[csio.sense_len - csio.sense_resid];
senseBuffer[0] = csio.sense_data.error_code;
Array.Copy(csio.sense_data.sense_buf, 0, senseBuffer, 1, senseBuffer.Length - 1);
}
}
buffer = new byte[csio.dxfer_len];
cdb = new byte[csio.cdb_len];
Marshal.Copy(csio.data_ptr, buffer, 0, buffer.Length);
if(csio.ccb_h.flags.HasFlag(ccb_flags.CAM_CDB_POINTER))
Marshal.Copy(new IntPtr(BitConverter.ToInt64(csio.cdb_bytes, 0)), cdb, 0, cdb.Length);
else
Array.Copy(csio.cdb_bytes, 0, cdb, 0, cdb.Length);
duration = (end - start).TotalMilliseconds;
if(csio.ccb_h.flags.HasFlag(ccb_flags.CAM_CDB_POINTER))
Marshal.FreeHGlobal(cdbPtr);
Marshal.FreeHGlobal(csio.data_ptr);
cam_freeccb(ccbPtr);
return error;
}
/// <summary>
/// Sends a SCSI command (32-bit arch)
/// </summary>
/// <returns>0 if no error occurred, otherwise, errno</returns>
/// <param name="dev">CAM device</param>
/// <param name="cdb">SCSI CDB</param>
/// <param name="buffer">Buffer for SCSI command response</param>
/// <param name="senseBuffer">Buffer with the SCSI sense</param>
/// <param name="timeout">Timeout in seconds</param>
/// <param name="direction">SCSI command transfer direction</param>
/// <param name="duration">Time it took to execute the command in milliseconds</param>
/// <param name="sense"><c>True</c> if SCSI error returned non-OK status and <paramref name="senseBuffer"/> contains SCSI sense</param>
internal static int SendScsiCommand(IntPtr dev, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout, ccb_flags direction, out double duration, out bool sense)
{
senseBuffer = null;
duration = 0;
sense = false;
if(buffer == null)
return -1;
IntPtr ccbPtr = cam_getccb(dev);
IntPtr cdbPtr = IntPtr.Zero;
if(ccbPtr.ToInt32() == 0)
{
sense = true;
return Marshal.GetLastWin32Error();
}
ccb_scsiio csio = (ccb_scsiio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_scsiio));
csio.ccb_h.func_code = xpt_opcode.XPT_SCSI_IO;
csio.ccb_h.flags = direction;
csio.ccb_h.xflags = 0;
csio.ccb_h.retry_count = 1;
csio.ccb_h.cbfcnp = IntPtr.Zero;
csio.ccb_h.timeout = timeout;
csio.data_ptr = Marshal.AllocHGlobal(buffer.Length);
csio.dxfer_len = (uint)buffer.Length;
csio.sense_len = 32;
csio.cdb_len = (byte)cdb.Length;
// TODO: Create enum?
csio.tag_action = 0x20;
csio.cdb_bytes = new byte[CAM_MAX_CDBLEN];
if(cdb.Length <= CAM_MAX_CDBLEN)
Array.Copy(cdb, 0, csio.cdb_bytes, 0, cdb.Length);
else
{
cdbPtr = Marshal.AllocHGlobal(cdb.Length);
byte[] cdbPtrBytes = BitConverter.GetBytes(cdbPtr.ToInt32());
Array.Copy(cdbPtrBytes, 0, csio.cdb_bytes, 0, IntPtr.Size);
csio.ccb_h.flags |= ccb_flags.CAM_CDB_POINTER;
}
csio.ccb_h.flags |= ccb_flags.CAM_DEV_QFRZDIS;
Marshal.Copy(buffer, 0, csio.data_ptr, buffer.Length);
Marshal.StructureToPtr(csio, ccbPtr, false);
DateTime start = DateTime.UtcNow;
int error = cam_send_ccb(dev, ccbPtr);
DateTime end = DateTime.UtcNow;
if(error < 0)
error = Marshal.GetLastWin32Error();
csio = (ccb_scsiio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_scsiio));
if((csio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_REQ_CMP &&
(csio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_SCSI_STATUS_ERROR)
{
error = Marshal.GetLastWin32Error();
DicConsole.DebugWriteLine("FreeBSD devices", "CAM status {0} error {1}", csio.ccb_h.status, error);
sense = true;
}
if((csio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_SCSI_STATUS_ERROR)
{
sense = true;
senseBuffer = new byte[1];
senseBuffer[0] = csio.scsi_status;
}
if((csio.ccb_h.status & cam_status.CAM_AUTOSNS_VALID) != 0)
{
if(csio.sense_len - csio.sense_resid > 0)
{
sense = (csio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_SCSI_STATUS_ERROR;
senseBuffer = new byte[csio.sense_len - csio.sense_resid];
senseBuffer[0] = csio.sense_data.error_code;
Array.Copy(csio.sense_data.sense_buf, 0, senseBuffer, 1, senseBuffer.Length - 1);
}
}
buffer = new byte[csio.dxfer_len];
cdb = new byte[csio.cdb_len];
Marshal.Copy(csio.data_ptr, buffer, 0, buffer.Length);
if(csio.ccb_h.flags.HasFlag(ccb_flags.CAM_CDB_POINTER))
Marshal.Copy(new IntPtr(BitConverter.ToInt32(csio.cdb_bytes, 0)), cdb, 0, cdb.Length);
else
Array.Copy(csio.cdb_bytes, 0, cdb, 0, cdb.Length);
duration = (end - start).TotalMilliseconds;
if(csio.ccb_h.flags.HasFlag(ccb_flags.CAM_CDB_POINTER))
Marshal.FreeHGlobal(cdbPtr);
Marshal.FreeHGlobal(csio.data_ptr);
cam_freeccb(ccbPtr);
return error;
}
static ccb_flags AtaProtocolToCamFlags(AtaProtocol protocol)
{
switch(protocol)
{
case AtaProtocol.DeviceDiagnostic:
case AtaProtocol.DeviceReset:
case AtaProtocol.HardReset:
case AtaProtocol.NonData:
case AtaProtocol.SoftReset:
case AtaProtocol.ReturnResponse:
return ccb_flags.CAM_DIR_NONE;
case AtaProtocol.PioIn:
case AtaProtocol.UDmaIn:
return ccb_flags.CAM_DIR_IN;
case AtaProtocol.PioOut:
case AtaProtocol.UDmaOut:
return ccb_flags.CAM_DIR_OUT;
default:
return ccb_flags.CAM_DIR_NONE;
}
}
internal static int SendAtaCommand(IntPtr dev, AtaRegistersCHS registers,
out AtaErrorRegistersCHS errorRegisters, AtaProtocol protocol,
ref byte[] buffer, uint timeout, out double duration, out bool sense)
{
duration = 0;
sense = false;
errorRegisters = new AtaErrorRegistersCHS();
if(buffer == null) return -1;
IntPtr ccbPtr = cam_getccb(dev);
ccb_ataio ataio = (ccb_ataio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_ataio));
ataio.ccb_h.func_code = xpt_opcode.XPT_ATA_IO;
ataio.ccb_h.flags = AtaProtocolToCamFlags(protocol);
ataio.ccb_h.xflags = 0;
ataio.ccb_h.retry_count = 1;
ataio.ccb_h.cbfcnp = IntPtr.Zero;
ataio.ccb_h.timeout = timeout;
ataio.data_ptr = Marshal.AllocHGlobal(buffer.Length);
ataio.dxfer_len = (uint)buffer.Length;
ataio.ccb_h.flags |= ccb_flags.CAM_DEV_QFRZDIS;
ataio.cmd.flags = CamAtaIoFlags.NeedResult;
switch(protocol)
{
case AtaProtocol.Dma:
case AtaProtocol.DmaQueued:
case AtaProtocol.UDmaIn:
case AtaProtocol.UDmaOut:
ataio.cmd.flags |= CamAtaIoFlags.DMA;
break;
case AtaProtocol.FPDma:
ataio.cmd.flags |= CamAtaIoFlags.FPDMA;
break;
}
ataio.cmd.command = registers.command;
ataio.cmd.lba_high = registers.cylinderHigh;
ataio.cmd.lba_mid = registers.cylinderLow;
ataio.cmd.device = (byte)(0x40 | registers.deviceHead);
ataio.cmd.features = registers.feature;
ataio.cmd.sector_count = registers.sectorCount;
ataio.cmd.lba_low = registers.sector;
Marshal.Copy(buffer, 0, ataio.data_ptr, buffer.Length);
Marshal.StructureToPtr(ataio, ccbPtr, false);
DateTime start = DateTime.UtcNow;
int error = cam_send_ccb(dev, ccbPtr);
DateTime end = DateTime.UtcNow;
if(error < 0) error = Marshal.GetLastWin32Error();
ataio = (ccb_ataio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_ataio));
if((ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_REQ_CMP &&
(ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_SCSI_STATUS_ERROR)
{
error = Marshal.GetLastWin32Error();
DicConsole.DebugWriteLine("FreeBSD devices", "CAM status {0} error {1}", ataio.ccb_h.status, error);
sense = true;
}
if((ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_ATA_STATUS_ERROR) sense = true;
errorRegisters.cylinderHigh = ataio.res.lba_high;
errorRegisters.cylinderLow = ataio.res.lba_mid;
errorRegisters.deviceHead = ataio.res.device;
errorRegisters.error = ataio.res.error;
errorRegisters.sector = ataio.res.lba_low;
errorRegisters.sectorCount = ataio.res.sector_count;
errorRegisters.status = ataio.res.status;
buffer = new byte[ataio.dxfer_len];
Marshal.Copy(ataio.data_ptr, buffer, 0, buffer.Length);
duration = (end - start).TotalMilliseconds;
Marshal.FreeHGlobal(ataio.data_ptr);
cam_freeccb(ccbPtr);
sense = errorRegisters.error != 0 || (errorRegisters.status & 0xA5) != 0 || error != 0;
return error;
}
internal static int SendAtaCommand(IntPtr dev, AtaRegistersLBA28 registers,
out AtaErrorRegistersLBA28 errorRegisters, AtaProtocol protocol,
ref byte[] buffer, uint timeout, out double duration, out bool sense)
{
duration = 0;
sense = false;
errorRegisters = new AtaErrorRegistersLBA28();
if(buffer == null) return -1;
IntPtr ccbPtr = cam_getccb(dev);
ccb_ataio ataio = (ccb_ataio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_ataio));
ataio.ccb_h.func_code = xpt_opcode.XPT_ATA_IO;
ataio.ccb_h.flags = AtaProtocolToCamFlags(protocol);
ataio.ccb_h.xflags = 0;
ataio.ccb_h.retry_count = 1;
ataio.ccb_h.cbfcnp = IntPtr.Zero;
ataio.ccb_h.timeout = timeout;
ataio.data_ptr = Marshal.AllocHGlobal(buffer.Length);
ataio.dxfer_len = (uint)buffer.Length;
ataio.ccb_h.flags |= ccb_flags.CAM_DEV_QFRZDIS;
ataio.cmd.flags = CamAtaIoFlags.NeedResult;
switch(protocol)
{
case AtaProtocol.Dma:
case AtaProtocol.DmaQueued:
case AtaProtocol.UDmaIn:
case AtaProtocol.UDmaOut:
ataio.cmd.flags |= CamAtaIoFlags.DMA;
break;
case AtaProtocol.FPDma:
ataio.cmd.flags |= CamAtaIoFlags.FPDMA;
break;
}
ataio.cmd.command = registers.command;
ataio.cmd.lba_high = registers.lbaHigh;
ataio.cmd.lba_mid = registers.lbaMid;
ataio.cmd.device = (byte)(0x40 | registers.deviceHead);
ataio.cmd.features = registers.feature;
ataio.cmd.sector_count = registers.sectorCount;
ataio.cmd.lba_low = registers.lbaLow;
Marshal.Copy(buffer, 0, ataio.data_ptr, buffer.Length);
Marshal.StructureToPtr(ataio, ccbPtr, false);
DateTime start = DateTime.UtcNow;
int error = cam_send_ccb(dev, ccbPtr);
DateTime end = DateTime.UtcNow;
if(error < 0) error = Marshal.GetLastWin32Error();
ataio = (ccb_ataio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_ataio));
if((ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_REQ_CMP &&
(ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_SCSI_STATUS_ERROR)
{
error = Marshal.GetLastWin32Error();
DicConsole.DebugWriteLine("FreeBSD devices", "CAM status {0} error {1}", ataio.ccb_h.status, error);
sense = true;
}
if((ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_ATA_STATUS_ERROR) sense = true;
errorRegisters.lbaHigh = ataio.res.lba_high;
errorRegisters.lbaMid = ataio.res.lba_mid;
errorRegisters.deviceHead = ataio.res.device;
errorRegisters.error = ataio.res.error;
errorRegisters.lbaLow = ataio.res.lba_low;
errorRegisters.sectorCount = ataio.res.sector_count;
errorRegisters.status = ataio.res.status;
buffer = new byte[ataio.dxfer_len];
Marshal.Copy(ataio.data_ptr, buffer, 0, buffer.Length);
duration = (end - start).TotalMilliseconds;
Marshal.FreeHGlobal(ataio.data_ptr);
cam_freeccb(ccbPtr);
sense = errorRegisters.error != 0 || (errorRegisters.status & 0xA5) != 0 || error != 0;
return error;
}
internal static int SendAtaCommand(IntPtr dev, AtaRegistersLBA48 registers,
out AtaErrorRegistersLBA48 errorRegisters, AtaProtocol protocol,
ref byte[] buffer, uint timeout, out double duration, out bool sense)
{
duration = 0;
sense = false;
errorRegisters = new AtaErrorRegistersLBA48();
// 48-bit ATA CAM commands can crash FreeBSD < 9.2-RELEASE
if((System.Environment.Version.Major == 9 && System.Environment.Version.Minor < 2) ||
System.Environment.Version.Major < 9) return -1;
if(buffer == null) return -1;
IntPtr ccbPtr = cam_getccb(dev);
ccb_ataio ataio = (ccb_ataio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_ataio));
ataio.ccb_h.func_code = xpt_opcode.XPT_ATA_IO;
ataio.ccb_h.flags = AtaProtocolToCamFlags(protocol);
ataio.ccb_h.xflags = 0;
ataio.ccb_h.retry_count = 1;
ataio.ccb_h.cbfcnp = IntPtr.Zero;
ataio.ccb_h.timeout = timeout;
ataio.data_ptr = Marshal.AllocHGlobal(buffer.Length);
ataio.dxfer_len = (uint)buffer.Length;
ataio.ccb_h.flags |= ccb_flags.CAM_DEV_QFRZDIS;
ataio.cmd.flags = CamAtaIoFlags.NeedResult | CamAtaIoFlags.ExtendedCommand;
switch(protocol)
{
case AtaProtocol.Dma:
case AtaProtocol.DmaQueued:
case AtaProtocol.UDmaIn:
case AtaProtocol.UDmaOut:
ataio.cmd.flags |= CamAtaIoFlags.DMA;
break;
case AtaProtocol.FPDma:
ataio.cmd.flags |= CamAtaIoFlags.FPDMA;
break;
}
ataio.cmd.lba_high_exp = (byte)((registers.lbaHigh & 0xFF00) >> 8);
ataio.cmd.lba_mid_exp = (byte)((registers.lbaMid & 0xFF00) >> 8);
ataio.cmd.features_exp = (byte)((registers.feature & 0xFF00) >> 8);
ataio.cmd.sector_count_exp = (byte)((registers.sectorCount & 0xFF00) >> 8);
ataio.cmd.lba_low_exp = (byte)((registers.lbaLow & 0xFF00) >> 8);
ataio.cmd.lba_high = (byte)(registers.lbaHigh & 0xFF);
ataio.cmd.lba_mid = (byte)(registers.lbaMid & 0xFF);
ataio.cmd.features = (byte)(registers.feature & 0xFF);
ataio.cmd.sector_count = (byte)(registers.sectorCount & 0xFF);
ataio.cmd.lba_low = (byte)(registers.lbaLow & 0xFF);
ataio.cmd.command = registers.command;
ataio.cmd.device = (byte)(0x40 | registers.deviceHead);
Marshal.Copy(buffer, 0, ataio.data_ptr, buffer.Length);
Marshal.StructureToPtr(ataio, ccbPtr, false);
DateTime start = DateTime.UtcNow;
int error = cam_send_ccb(dev, ccbPtr);
DateTime end = DateTime.UtcNow;
if(error < 0) error = Marshal.GetLastWin32Error();
ataio = (ccb_ataio)Marshal.PtrToStructure(ccbPtr, typeof(ccb_ataio));
if((ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_REQ_CMP &&
(ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) != cam_status.CAM_SCSI_STATUS_ERROR)
{
error = Marshal.GetLastWin32Error();
DicConsole.DebugWriteLine("FreeBSD devices", "CAM status {0} error {1}", ataio.ccb_h.status, error);
sense = true;
}
if((ataio.ccb_h.status & cam_status.CAM_STATUS_MASK) == cam_status.CAM_ATA_STATUS_ERROR) sense = true;
errorRegisters.sectorCount = (ushort)((ataio.res.sector_count_exp << 8) + ataio.res.sector_count);
errorRegisters.lbaLow = (ushort)((ataio.res.lba_low_exp << 8) + ataio.res.lba_low);
errorRegisters.lbaMid = (ushort)((ataio.res.lba_mid_exp << 8) + ataio.res.lba_mid);
errorRegisters.lbaHigh = (ushort)((ataio.res.lba_high_exp << 8) + ataio.res.lba_high);
errorRegisters.deviceHead = ataio.res.device;
errorRegisters.error = ataio.res.error;
errorRegisters.status = ataio.res.status;
buffer = new byte[ataio.dxfer_len];
Marshal.Copy(ataio.data_ptr, buffer, 0, buffer.Length);
duration = (end - start).TotalMilliseconds;
Marshal.FreeHGlobal(ataio.data_ptr);
cam_freeccb(ccbPtr);
sense = errorRegisters.error != 0 || (errorRegisters.status & 0xA5) != 0 || error != 0;
return error;
}
}
}