2017-05-31 01:00:58 +01:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : SSC.cs
2017-12-19 03:50:57 +00:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
2017-05-31 01:00:58 +01:00
//
2017-12-19 03:50:57 +00:00
// Component : Core algorithms.
2017-05-31 01:00:58 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
2017-12-19 03:50:57 +00:00
// Dumps media from SCSI Streaming devices.
2017-05-31 01:00:58 +01:00
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2017-12-19 03:50:57 +00:00
// Copyright © 2011-2018 Natalia Portillo
2017-05-31 01:00:58 +01:00
// ****************************************************************************/
2017-12-19 03:50:57 +00:00
2017-05-31 01:00:58 +01:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2017-12-19 01:38:11 +00:00
using System.Text ;
2017-05-31 01:00:58 +01:00
using DiscImageChef.CommonTypes ;
using DiscImageChef.Console ;
using DiscImageChef.Core.Logging ;
using DiscImageChef.Devices ;
using Schemas ;
namespace DiscImageChef.Core.Devices.Dumping
{
internal static class SSC
{
2017-11-20 05:07:16 +00:00
internal static void Dump ( Device dev , string outputPrefix , string devicePath , ref CICMMetadataType sidecar , ref Metadata . Resume resume , ref DumpLog dumpLog )
2017-05-31 01:00:58 +01:00
{
Decoders . SCSI . FixedSense ? fxSense ;
bool aborted ;
MHDDLog mhddLog ;
IBGLog ibgLog ;
bool sense = false ;
ulong blocks = 0 ;
uint blockSize = 0 ;
MediaType dskType = MediaType . Unknown ;
DateTime start ;
DateTime end ;
double totalDuration = 0 ;
double totalChkDuration = 0 ;
double currentSpeed = 0 ;
double maxSpeed = double . MinValue ;
double minSpeed = double . MaxValue ;
List < ulong > unreadableSectors = new List < ulong > ( ) ;
Checksum dataChk ;
2017-06-08 21:12:05 +01:00
dev . RequestSense ( out byte [ ] senseBuf , dev . Timeout , out double duration ) ;
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out string strSense ) ;
2017-05-31 01:00:58 +01:00
if ( fxSense . HasValue & & fxSense . Value . SenseKey ! = Decoders . SCSI . SenseKeys . NoSense )
{
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
DicConsole . ErrorWriteLine ( "Drive has status error, please correct. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
return ;
}
// Not in BOM/P
2017-06-05 15:57:10 +01:00
if ( fxSense . HasValue & & fxSense . Value . ASC = = 0x00 & & fxSense . Value . ASCQ ! = 0x00 & & fxSense . Value . ASCQ ! = 0x04 & & fxSense . Value . SenseKey ! = Decoders . SCSI . SenseKeys . IllegalRequest )
2017-05-31 01:00:58 +01:00
{
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Rewinding, please wait..." ) ;
2017-05-31 01:00:58 +01:00
DicConsole . Write ( "Rewinding, please wait..." ) ;
// Rewind, let timeout apply
sense = dev . Rewind ( out senseBuf , dev . Timeout , out duration ) ;
// Still rewinding?
// TODO: Pause?
do
{
DicConsole . Write ( "\rRewinding, please wait..." ) ;
dev . RequestSense ( out senseBuf , dev . Timeout , out duration ) ;
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
}
while ( fxSense . HasValue & & fxSense . Value . ASC = = 0x00 & & ( fxSense . Value . ASCQ = = 0x1A | | fxSense . Value . ASCQ ! = 0x04 ) ) ;
dev . RequestSense ( out senseBuf , dev . Timeout , out duration ) ;
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
// And yet, did not rewind!
if ( fxSense . HasValue & & ( ( fxSense . Value . ASC = = 0x00 & & fxSense . Value . ASCQ ! = 0x04 ) | | fxSense . Value . ASC ! = 0x00 ) )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
DicConsole . WriteLine ( ) ;
}
// Check position
2017-06-08 21:12:05 +01:00
sense = dev . ReadPosition ( out byte [ ] cmdBuf , out senseBuf , SscPositionForms . Short , dev . Timeout , out duration ) ;
2017-05-31 01:00:58 +01:00
if ( sense )
{
// READ POSITION is mandatory starting SCSI-2, so do not cry if the drive does not recognize the command (SCSI-1 or earlier)
// Anyway, <=SCSI-1 tapes do not support partitions
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
2017-06-05 15:57:10 +01:00
if ( fxSense . HasValue & & ( ( fxSense . Value . ASC = = 0x20 & & fxSense . Value . ASCQ ! = 0x00 ) | | fxSense . Value . ASC ! = 0x20 & & fxSense . Value . SenseKey ! = Decoders . SCSI . SenseKeys . IllegalRequest ) )
2017-05-31 01:00:58 +01:00
{
DicConsole . ErrorWriteLine ( "Could not get position. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Could not get position. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
}
else
{
// Not in partition 0
if ( cmdBuf [ 1 ] ! = 0 )
{
DicConsole . Write ( "Drive not in partition 0. Rewinding, please wait..." ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive not in partition 0. Rewinding, please wait..." ) ;
2017-05-31 01:00:58 +01:00
// Rewind, let timeout apply
sense = dev . Locate ( out senseBuf , false , 0 , 0 , dev . Timeout , out duration ) ;
if ( sense )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
// Still rewinding?
// TODO: Pause?
do
{
System . Threading . Thread . Sleep ( 1000 ) ;
DicConsole . Write ( "\rRewinding, please wait..." ) ;
dev . RequestSense ( out senseBuf , dev . Timeout , out duration ) ;
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
}
while ( fxSense . HasValue & & fxSense . Value . ASC = = 0x00 & & ( fxSense . Value . ASCQ = = 0x1A | | fxSense . Value . ASCQ = = 0x19 ) ) ;
// And yet, did not rewind!
if ( fxSense . HasValue & & ( ( fxSense . Value . ASC = = 0x00 & & fxSense . Value . ASCQ ! = 0x04 ) | | fxSense . Value . ASC ! = 0x00 ) )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
sense = dev . ReadPosition ( out cmdBuf , out senseBuf , SscPositionForms . Short , dev . Timeout , out duration ) ;
if ( sense )
{
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
DicConsole . ErrorWriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not rewind, please correct. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
// Still not in partition 0!!!?
if ( cmdBuf [ 1 ] ! = 0 )
{
DicConsole . ErrorWriteLine ( "Drive could not rewind to partition 0 but no error occurred..." ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not rewind to partition 0 but no error occurred..." ) ;
2017-05-31 01:00:58 +01:00
return ;
}
DicConsole . WriteLine ( ) ;
}
}
sidecar . BlockMedia = new BlockMediaType [ 1 ] ;
2017-06-08 21:12:05 +01:00
sidecar . BlockMedia [ 0 ] = new BlockMediaType
{
SCSI = new SCSIType ( )
} ;
2017-05-31 01:00:58 +01:00
byte scsiMediumTypeTape = 0 ;
byte scsiDensityCodeTape = 0 ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Requesting MODE SENSE (10)." ) ;
2017-05-31 01:00:58 +01:00
sense = dev . ModeSense10 ( out cmdBuf , out senseBuf , false , true , ScsiModeSensePageControl . Current , 0x3F , 0xFF , 5 , out duration ) ;
if ( ! sense | | dev . Error )
{
sense = dev . ModeSense10 ( out cmdBuf , out senseBuf , false , true , ScsiModeSensePageControl . Current , 0x3F , 0x00 , 5 , out duration ) ;
}
Decoders . SCSI . Modes . DecodedMode ? decMode = null ;
if ( ! sense & & ! dev . Error )
{
if ( Decoders . SCSI . Modes . DecodeMode10 ( cmdBuf , dev . SCSIType ) . HasValue )
{
decMode = Decoders . SCSI . Modes . DecodeMode10 ( cmdBuf , dev . SCSIType ) ;
2017-06-08 21:12:05 +01:00
sidecar . BlockMedia [ 0 ] . SCSI . ModeSense10 = new DumpType
{
Image = outputPrefix + ".modesense10.bin" ,
Size = cmdBuf . Length ,
Checksums = Checksum . GetChecksums ( cmdBuf ) . ToArray ( )
} ;
2017-05-31 01:00:58 +01:00
DataFile . WriteTo ( "SCSI Dump" , sidecar . BlockMedia [ 0 ] . SCSI . ModeSense10 . Image , cmdBuf ) ;
}
}
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Requesting MODE SENSE (6)." ) ;
2017-05-31 01:00:58 +01:00
sense = dev . ModeSense6 ( out cmdBuf , out senseBuf , false , ScsiModeSensePageControl . Current , 0x3F , 0x00 , 5 , out duration ) ;
if ( sense | | dev . Error )
sense = dev . ModeSense6 ( out cmdBuf , out senseBuf , false , ScsiModeSensePageControl . Current , 0x3F , 0x00 , 5 , out duration ) ;
if ( sense | | dev . Error )
sense = dev . ModeSense ( out cmdBuf , out senseBuf , 5 , out duration ) ;
if ( ! sense & & ! dev . Error )
{
if ( Decoders . SCSI . Modes . DecodeMode6 ( cmdBuf , dev . SCSIType ) . HasValue )
{
decMode = Decoders . SCSI . Modes . DecodeMode6 ( cmdBuf , dev . SCSIType ) ;
2017-06-08 21:12:05 +01:00
sidecar . BlockMedia [ 0 ] . SCSI . ModeSense = new DumpType
{
Image = outputPrefix + ".modesense.bin" ,
Size = cmdBuf . Length ,
Checksums = Checksum . GetChecksums ( cmdBuf ) . ToArray ( )
} ;
2017-05-31 01:00:58 +01:00
DataFile . WriteTo ( "SCSI Dump" , sidecar . BlockMedia [ 0 ] . SCSI . ModeSense . Image , cmdBuf ) ;
}
}
// TODO: Check partitions page
if ( decMode . HasValue )
{
scsiMediumTypeTape = ( byte ) decMode . Value . Header . MediumType ;
if ( decMode . Value . Header . BlockDescriptors ! = null & & decMode . Value . Header . BlockDescriptors . Length > = 1 )
scsiDensityCodeTape = ( byte ) decMode . Value . Header . BlockDescriptors [ 0 ] . Density ;
2017-06-05 15:57:10 +01:00
blockSize = decMode . Value . Header . BlockDescriptors [ 0 ] . BlockLength ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Device reports {0} blocks ({1} bytes)." , blocks , blocks * blockSize ) ;
2017-05-31 01:00:58 +01:00
}
2017-06-05 15:57:10 +01:00
else
blockSize = 1 ;
2017-05-31 01:00:58 +01:00
if ( dskType = = MediaType . Unknown )
dskType = MediaTypeFromSCSI . Get ( ( byte ) dev . SCSIType , dev . Manufacturer , dev . Model , scsiMediumTypeTape , scsiDensityCodeTape , blocks , blockSize ) ;
DicConsole . WriteLine ( "Media identified as {0}" , dskType ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "SCSI device type: {0}." , dev . SCSIType ) ;
dumpLog . WriteLine ( "SCSI medium type: {0}." , scsiMediumTypeTape ) ;
dumpLog . WriteLine ( "SCSI density type: {0}." , scsiDensityCodeTape ) ;
dumpLog . WriteLine ( "Media identified as {0}." , dskType ) ;
2017-05-31 01:00:58 +01:00
bool endOfMedia = false ;
ulong currentBlock = 0 ;
ulong currentFile = 0 ;
byte currentPartition = 0 ;
byte totalPartitions = 1 ; // TODO: Handle partitions.
ulong currentSize = 0 ;
ulong currentPartitionSize = 0 ;
ulong currentFileSize = 0 ;
2017-06-05 15:57:10 +01:00
bool fixedLen = false ;
uint transferLen = blockSize ;
sense = dev . Read6 ( out cmdBuf , out senseBuf , false , fixedLen , transferLen , blockSize , dev . Timeout , out duration ) ;
if ( sense )
{
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
if ( fxSense . HasValue )
{
if ( fxSense . Value . SenseKey = = Decoders . SCSI . SenseKeys . IllegalRequest )
{
sense = dev . Space ( out senseBuf , SscSpaceCodes . LogicalBlock , - 1 , dev . Timeout , out duration ) ;
if ( sense )
{
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
if ( ! fxSense . HasValue | | ! fxSense . Value . EOM )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not return back. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not return back. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-06-05 15:57:10 +01:00
return ;
}
}
fixedLen = true ;
transferLen = 1 ;
sense = dev . Read6 ( out cmdBuf , out senseBuf , false , fixedLen , transferLen , blockSize , dev . Timeout , out duration ) ;
if ( sense )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not read. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not read. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-06-05 15:57:10 +01:00
return ;
}
}
else
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not read. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not read. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-06-05 15:57:10 +01:00
return ;
}
}
else
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Cannot read device, don't know why, exiting..." ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Cannot read device, don't know why, exiting..." ) ;
2017-06-05 15:57:10 +01:00
return ;
}
}
sense = dev . Space ( out senseBuf , SscSpaceCodes . LogicalBlock , - 1 , dev . Timeout , out duration ) ;
if ( sense )
{
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
if ( ! fxSense . HasValue | | ! fxSense . Value . EOM )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not return back. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not return back. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-06-05 15:57:10 +01:00
return ;
}
}
2017-05-31 01:00:58 +01:00
Checksum partitionChk ;
Checksum fileChk ;
List < TapePartitionType > partitions = new List < TapePartitionType > ( ) ;
List < TapeFileType > files = new List < TapeFileType > ( ) ;
TapeFileType currentTapeFile = new TapeFileType ( ) ;
TapePartitionType currentTapePartition = new TapePartitionType ( ) ;
DicConsole . WriteLine ( ) ;
DataFile dumpFile = new DataFile ( outputPrefix + ".bin" ) ;
dataChk = new Checksum ( ) ;
start = DateTime . UtcNow ;
mhddLog = new MHDDLog ( outputPrefix + ".mhddlog.bin" , dev , blocks , blockSize , 1 ) ;
ibgLog = new IBGLog ( outputPrefix + ".ibg" , 0x0008 ) ;
2017-06-08 21:12:05 +01:00
currentTapeFile = new TapeFileType
{
Image = new ImageType
{
format = "BINARY" ,
offset = ( long ) currentSize ,
offsetSpecified = true ,
Value = outputPrefix + ".bin"
} ,
Sequence = ( long ) currentFile ,
StartBlock = ( long ) currentBlock ,
BlockSize = blockSize
} ;
2017-05-31 01:00:58 +01:00
fileChk = new Checksum ( ) ;
2017-06-08 21:12:05 +01:00
currentTapePartition = new TapePartitionType
{
Image = new ImageType
{
format = "BINARY" ,
offset = ( long ) currentSize ,
offsetSpecified = true ,
Value = outputPrefix + ".bin"
} ,
Sequence = ( long ) currentPartition ,
StartBlock = ( long ) currentBlock
} ;
2017-05-31 01:00:58 +01:00
partitionChk = new Checksum ( ) ;
aborted = false ;
System . Console . CancelKeyPress + = ( sender , e ) = >
{
e . Cancel = aborted = true ;
} ;
while ( currentPartition < totalPartitions )
{
if ( aborted )
2017-11-20 05:07:16 +00:00
{
dumpLog . WriteLine ( "Aborted!" ) ;
2017-05-31 01:00:58 +01:00
break ;
2017-11-20 05:07:16 +00:00
}
2017-05-31 01:00:58 +01:00
if ( endOfMedia )
{
DicConsole . WriteLine ( ) ;
DicConsole . WriteLine ( "Finished partition {0}" , currentPartition ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Finished partition {0}" , currentPartition ) ;
2017-05-31 01:00:58 +01:00
currentTapePartition . File = files . ToArray ( ) ;
currentTapePartition . Checksums = partitionChk . End ( ) . ToArray ( ) ;
currentTapePartition . EndBlock = ( long ) ( currentBlock - 1 ) ;
currentTapePartition . Size = ( long ) currentPartitionSize ;
partitions . Add ( currentTapePartition ) ;
currentPartition + + ;
if ( currentPartition < totalPartitions )
{
currentFile + + ;
2017-06-08 21:12:05 +01:00
currentTapeFile = new TapeFileType
{
Image = new ImageType
{
format = "BINARY" ,
offset = ( long ) currentSize ,
offsetSpecified = true ,
Value = outputPrefix + ".bin"
} ,
Sequence = ( long ) currentFile ,
StartBlock = ( long ) currentBlock ,
BlockSize = blockSize
} ;
2017-05-31 01:00:58 +01:00
currentFileSize = 0 ;
fileChk = new Checksum ( ) ;
files = new List < TapeFileType > ( ) ;
2017-06-08 21:12:05 +01:00
currentTapePartition = new TapePartitionType
{
Image = new ImageType
{
format = "BINARY" ,
offset = ( long ) currentSize ,
offsetSpecified = true ,
Value = outputPrefix + ".bin"
} ,
Sequence = currentPartition ,
StartBlock = ( long ) currentBlock
} ;
2017-05-31 01:00:58 +01:00
currentPartitionSize = 0 ;
partitionChk = new Checksum ( ) ;
DicConsole . WriteLine ( "Seeking to partition {0}" , currentPartition ) ;
sense = dev . Locate ( out senseBuf , false , currentPartition , 0 , dev . Timeout , out duration ) ;
totalDuration + = duration ;
}
continue ;
}
#pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator
if ( currentSpeed > maxSpeed & & currentSpeed ! = 0 )
maxSpeed = currentSpeed ;
if ( currentSpeed < minSpeed & & currentSpeed ! = 0 )
minSpeed = currentSpeed ;
#pragma warning restore RECS0018 // Comparison of floating point numbers with equality operator
DicConsole . Write ( "\rReading block {0} ({1:F3} MiB/sec.)" , currentBlock , currentSpeed ) ;
2017-06-05 15:57:10 +01:00
sense = dev . Read6 ( out cmdBuf , out senseBuf , false , fixedLen , transferLen , blockSize , dev . Timeout , out duration ) ;
2017-05-31 01:00:58 +01:00
totalDuration + = duration ;
if ( sense )
{
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
if ( fxSense . Value . ASC = = 0x00 & & fxSense . Value . ASCQ = = 0x00 & & fxSense . Value . ILI & & fxSense . Value . InformationValid )
{
blockSize = ( uint ) ( ( int ) blockSize - BitConverter . ToInt32 ( BitConverter . GetBytes ( fxSense . Value . Information ) , 0 ) ) ;
2017-06-04 22:47:13 +01:00
currentTapeFile . BlockSize = blockSize ;
2017-05-31 01:00:58 +01:00
DicConsole . WriteLine ( ) ;
DicConsole . WriteLine ( "Blocksize changed to {0} bytes at block {1}" , blockSize , currentBlock ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Blocksize changed to {0} bytes at block {1}" , blockSize , currentBlock ) ;
2017-05-31 01:00:58 +01:00
sense = dev . Space ( out senseBuf , SscSpaceCodes . LogicalBlock , - 1 , dev . Timeout , out duration ) ;
totalDuration + = duration ;
if ( sense )
{
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Drive could not go back one block. Sense follows..." ) ;
DicConsole . ErrorWriteLine ( "{0}" , strSense ) ;
dumpFile . Close ( ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not go back one block. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
continue ;
}
if ( fxSense . Value . SenseKey = = Decoders . SCSI . SenseKeys . BlankCheck )
{
if ( currentBlock = = 0 )
{
DicConsole . WriteLine ( ) ;
DicConsole . ErrorWriteLine ( "Cannot dump a blank tape..." ) ;
dumpFile . Close ( ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Cannot dump a blank tape..." ) ;
2017-05-31 01:00:58 +01:00
return ;
}
// For sure this is an end-of-tape/partition
2017-06-05 15:57:10 +01:00
if ( fxSense . Value . ASC = = 0x00 & & ( fxSense . Value . ASCQ = = 0x02 | | fxSense . Value . ASCQ = = 0x05 | | fxSense . Value . EOM ) )
2017-05-31 01:00:58 +01:00
{
// TODO: Detect end of partition
endOfMedia = true ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Found end-of-tape/partition..." ) ;
2017-05-31 01:00:58 +01:00
continue ;
}
DicConsole . WriteLine ( ) ;
DicConsole . WriteLine ( "Blank block found, end of tape?" ) ;
endOfMedia = true ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Blank block found, end of tape?..." ) ;
2017-05-31 01:00:58 +01:00
continue ;
}
if ( ( fxSense . Value . SenseKey = = Decoders . SCSI . SenseKeys . NoSense | | fxSense . Value . SenseKey = = Decoders . SCSI . SenseKeys . RecoveredError ) & &
2017-06-05 15:57:10 +01:00
( fxSense . Value . ASCQ = = 0x02 | | fxSense . Value . ASCQ = = 0x05 | | fxSense . Value . EOM ) )
2017-05-31 01:00:58 +01:00
{
// TODO: Detect end of partition
endOfMedia = true ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Found end-of-tape/partition..." ) ;
2017-05-31 01:00:58 +01:00
continue ;
}
if ( ( fxSense . Value . SenseKey = = Decoders . SCSI . SenseKeys . NoSense | | fxSense . Value . SenseKey = = Decoders . SCSI . SenseKeys . RecoveredError ) & &
2017-06-05 15:57:10 +01:00
( fxSense . Value . ASCQ = = 0x01 | | fxSense . Value . Filemark ) )
2017-05-31 01:00:58 +01:00
{
currentTapeFile . Checksums = fileChk . End ( ) . ToArray ( ) ;
currentTapeFile . EndBlock = ( long ) ( currentBlock - 1 ) ;
currentTapeFile . Size = ( long ) currentFileSize ;
files . Add ( currentTapeFile ) ;
currentFile + + ;
2017-06-08 21:12:05 +01:00
currentTapeFile = new TapeFileType
{
Image = new ImageType
{
format = "BINARY" ,
offset = ( long ) currentSize ,
offsetSpecified = true ,
Value = outputPrefix + ".bin"
} ,
Sequence = ( long ) currentFile ,
StartBlock = ( long ) currentBlock ,
BlockSize = blockSize
} ;
2017-05-31 01:00:58 +01:00
currentFileSize = 0 ;
fileChk = new Checksum ( ) ;
DicConsole . WriteLine ( ) ;
DicConsole . WriteLine ( "Changed to file {0} at block {1}" , currentFile , currentBlock ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Changed to file {0} at block {1}" , currentFile , currentBlock ) ;
2017-05-31 01:00:58 +01:00
continue ;
}
// TODO: Add error recovering for tapes
fxSense = Decoders . SCSI . Sense . DecodeFixed ( senseBuf , out strSense ) ;
DicConsole . ErrorWriteLine ( "Drive could not read block. Sense follows..." ) ;
2017-06-05 15:57:10 +01:00
DicConsole . ErrorWriteLine ( "{0} {1}" , fxSense . Value . SenseKey , strSense ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Drive could not read block. Sense follows..." ) ;
2017-11-26 16:28:45 +00:00
dumpLog . WriteLine ( "Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h" , fxSense . Value . SenseKey , fxSense . Value . ASC , fxSense . Value . ASCQ ) ;
2017-05-31 01:00:58 +01:00
return ;
}
mhddLog . Write ( currentBlock , duration ) ;
ibgLog . Write ( currentBlock , currentSpeed * 1024 ) ;
dumpFile . Write ( cmdBuf ) ;
DateTime chkStart = DateTime . UtcNow ;
dataChk . Update ( cmdBuf ) ;
fileChk . Update ( cmdBuf ) ;
partitionChk . Update ( cmdBuf ) ;
DateTime chkEnd = DateTime . UtcNow ;
double chkDuration = ( chkEnd - chkStart ) . TotalMilliseconds ;
totalChkDuration + = chkDuration ;
if ( currentBlock % 10 = = 0 )
2017-06-04 22:47:13 +01:00
currentSpeed = ( ( double ) blockSize / ( double ) 1048576 ) / ( duration / ( double ) 1000 ) ;
2017-05-31 01:00:58 +01:00
currentBlock + + ;
currentSize + = blockSize ;
currentFileSize + = blockSize ;
currentPartitionSize + = blockSize ;
}
blocks = currentBlock + 1 ;
DicConsole . WriteLine ( ) ;
end = DateTime . UtcNow ;
mhddLog . Close ( ) ;
ibgLog . Close ( dev , blocks , blockSize , ( end - start ) . TotalSeconds , currentSpeed * 1024 , ( ( ( double ) blockSize * ( double ) ( blocks + 1 ) ) / 1024 ) / ( totalDuration / 1000 ) , devicePath ) ;
2017-11-20 05:07:16 +00:00
dumpLog . WriteLine ( "Dump finished in {0} seconds." , ( end - start ) . TotalSeconds ) ;
dumpLog . WriteLine ( "Average dump speed {0:F3} KiB/sec." , ( ( ( double ) blockSize * ( double ) ( blocks + 1 ) ) / 1024 ) / ( totalDuration / 1000 ) ) ;
dumpLog . WriteLine ( "Average checksum speed {0:F3} KiB/sec." , ( ( ( double ) blockSize * ( double ) ( blocks + 1 ) ) / 1024 ) / ( totalChkDuration / 1000 ) ) ;
2017-05-31 01:00:58 +01:00
DicConsole . WriteLine ( "Took a total of {0:F3} seconds ({1:F3} processing commands, {2:F3} checksumming)." , ( end - start ) . TotalSeconds , totalDuration / 1000 , totalChkDuration / 1000 ) ;
#pragma warning disable IDE0004 // Cast is necessary, otherwise incorrect value is created
DicConsole . WriteLine ( "Avegare speed: {0:F3} MiB/sec." , ( ( ( double ) blockSize * ( double ) ( blocks + 1 ) ) / 1048576 ) / ( totalDuration / 1000 ) ) ;
#pragma warning restore IDE0004 // Cast is necessary, otherwise incorrect value is created
DicConsole . WriteLine ( "Fastest speed burst: {0:F3} MiB/sec." , maxSpeed ) ;
DicConsole . WriteLine ( "Slowest speed burst: {0:F3} MiB/sec." , minSpeed ) ;
sidecar . BlockMedia [ 0 ] . Checksums = dataChk . End ( ) . ToArray ( ) ;
sidecar . BlockMedia [ 0 ] . Dimensions = Metadata . Dimensions . DimensionsFromMediaType ( dskType ) ;
2017-06-08 21:12:05 +01:00
Metadata . MediaType . MediaTypeToString ( dskType , out string xmlDskTyp , out string xmlDskSubTyp ) ;
2017-05-31 01:00:58 +01:00
sidecar . BlockMedia [ 0 ] . DiskType = xmlDskTyp ;
sidecar . BlockMedia [ 0 ] . DiskSubType = xmlDskSubTyp ;
// TODO: Implement device firmware revision
2017-06-08 21:12:05 +01:00
sidecar . BlockMedia [ 0 ] . Image = new ImageType
{
format = "Raw disk image (sector by sector copy)" ,
Value = outputPrefix + ".bin"
} ;
2017-05-31 01:00:58 +01:00
sidecar . BlockMedia [ 0 ] . LogicalBlocks = ( long ) blocks ;
sidecar . BlockMedia [ 0 ] . Size = ( long ) ( currentSize ) ;
sidecar . BlockMedia [ 0 ] . DumpHardwareArray = new DumpHardwareType [ 1 ] ;
2017-06-08 21:12:05 +01:00
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] = new DumpHardwareType
{
Extents = new ExtentType [ 1 ]
} ;
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] . Extents [ 0 ] = new ExtentType
{
Start = 0 ,
2017-06-08 22:01:06 +01:00
End = blocks - 1
2017-06-08 21:12:05 +01:00
} ;
2017-05-31 01:00:58 +01:00
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] . Manufacturer = dev . Manufacturer ;
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] . Model = dev . Model ;
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] . Revision = dev . Revision ;
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] . Serial = dev . Serial ;
sidecar . BlockMedia [ 0 ] . DumpHardwareArray [ 0 ] . Software = Version . GetSoftwareType ( dev . PlatformID ) ;
sidecar . BlockMedia [ 0 ] . TapeInformation = partitions . ToArray ( ) ;
if ( ! aborted )
{
DicConsole . WriteLine ( "Writing metadata sidecar" ) ;
FileStream xmlFs = new FileStream ( outputPrefix + ".cicm.xml" ,
FileMode . Create ) ;
System . Xml . Serialization . XmlSerializer xmlSer = new System . Xml . Serialization . XmlSerializer ( typeof ( CICMMetadataType ) ) ;
xmlSer . Serialize ( xmlFs , sidecar ) ;
xmlFs . Close ( ) ;
}
Statistics . AddMedia ( dskType , true ) ;
return ;
}
}
}