2017-05-19 20:28:49 +01:00
// /***************************************************************************
2016-01-11 19:40:58 +00:00
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : SBC.cs
2016-07-28 18:13:49 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
2016-01-11 19:40:58 +00:00
//
2016-07-28 18:13:49 +01:00
// Component : SCSI Block Commands.
2016-01-11 19:40:58 +00:00
//
// --[ Description ] ----------------------------------------------------------
//
2016-07-28 18:13:49 +01:00
// Contains SCSI commands defined in SBC standards.
2016-01-11 19:40:58 +00:00
//
// --[ License ] --------------------------------------------------------------
//
2016-07-28 18:13:49 +01:00
// 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
2016-01-11 19:40:58 +00:00
// License, or (at your option) any later version.
//
2016-07-28 18:13:49 +01:00
// 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.
2016-01-11 19:40:58 +00:00
//
2016-07-28 18:13:49 +01:00
// 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/>.
2016-01-11 19:40:58 +00:00
//
// ----------------------------------------------------------------------------
2017-12-19 03:50:57 +00:00
// Copyright © 2011-2018 Natalia Portillo
2016-01-11 19:40:58 +00:00
// ****************************************************************************/
2016-07-28 18:13:49 +01:00
2016-01-11 19:40:58 +00:00
using System ;
using DiscImageChef.Console ;
namespace DiscImageChef.Devices
{
public partial class Device
{
/// <summary>
/// Sends the SBC READ (6) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Starting block.</param>
/// <param name="blockSize">Block size in bytes.</param>
2017-12-19 20:33:03 +00:00
public bool Read6 ( out byte [ ] buffer , out byte [ ] senseBuffer , uint lba , uint blockSize , uint timeout ,
out double duration )
2016-01-11 19:40:58 +00:00
{
return Read6 ( out buffer , out senseBuffer , lba , blockSize , 1 , timeout , out duration ) ;
}
/// <summary>
/// Sends the SBC READ (6) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Starting block.</param>
/// <param name="blockSize">Block size in bytes.</param>
/// <param name="transferLength">How many blocks to read.</param>
2017-12-19 20:33:03 +00:00
public bool Read6 ( out byte [ ] buffer , out byte [ ] senseBuffer , uint lba , uint blockSize , byte transferLength ,
uint timeout , out double duration )
2016-01-11 19:40:58 +00:00
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 6 ] ;
cdb [ 0 ] = ( byte ) ScsiCommands . Read6 ;
cdb [ 1 ] = ( byte ) ( ( lba & 0x1F0000 ) > > 16 ) ;
cdb [ 2 ] = ( byte ) ( ( lba & 0xFF00 ) > > 8 ) ;
cdb [ 3 ] = ( byte ) ( lba & 0xFF ) ;
cdb [ 4 ] = transferLength ;
2017-12-19 20:33:03 +00:00
if ( transferLength = = 0 ) buffer = new byte [ 256 * blockSize ] ;
else buffer = new byte [ transferLength * blockSize ] ;
2016-01-11 19:40:58 +00:00
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . In , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "READ (6) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Sends the SBC READ (10) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="rdprotect">Instructs the drive how to check for protection information on the medium.</param>
/// <param name="dpo">If set to <c>true</c> requested blocks shall be assigned the lowest retention priority on cache fetch/retain.</param>
/// <param name="fua">If set to <c>true</c> requested blocks MUST bu read from medium and not the cache.</param>
/// <param name="fuaNv">If set to <c>true</c> requested blocks will be returned from non-volatile cache. If they're not present they shall be stored there.</param>
/// <param name="lba">Starting block.</param>
/// <param name="blockSize">Block size in bytes.</param>
/// <param name="groupNumber">Group number where attributes associated with this command should be collected.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="relAddr">If set to <c>true</c> address is relative to current position.</param>
2017-12-19 20:33:03 +00:00
public bool Read10 ( out byte [ ] buffer , out byte [ ] senseBuffer , byte rdprotect , bool dpo , bool fua , bool fuaNv ,
bool relAddr , uint lba , uint blockSize , byte groupNumber , ushort transferLength ,
uint timeout , out double duration )
2016-01-11 19:40:58 +00:00
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 10 ] ;
cdb [ 0 ] = ( byte ) ScsiCommands . Read10 ;
cdb [ 1 ] = ( byte ) ( ( rdprotect & 0x07 ) < < 5 ) ;
2017-12-19 20:33:03 +00:00
if ( dpo ) cdb [ 1 ] + = 0x10 ;
if ( fua ) cdb [ 1 ] + = 0x08 ;
if ( fuaNv ) cdb [ 1 ] + = 0x02 ;
if ( relAddr ) cdb [ 1 ] + = 0x01 ;
2016-01-11 19:40:58 +00:00
cdb [ 2 ] = ( byte ) ( ( lba & 0xFF000000 ) > > 24 ) ;
cdb [ 3 ] = ( byte ) ( ( lba & 0xFF0000 ) > > 16 ) ;
cdb [ 4 ] = ( byte ) ( ( lba & 0xFF00 ) > > 8 ) ;
cdb [ 5 ] = ( byte ) ( lba & 0xFF ) ;
cdb [ 6 ] = ( byte ) ( groupNumber & 0x1F ) ;
cdb [ 7 ] = ( byte ) ( ( transferLength & 0xFF00 ) > > 8 ) ;
cdb [ 8 ] = ( byte ) ( transferLength & 0xFF ) ;
buffer = new byte [ transferLength * blockSize ] ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . In , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "READ (10) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Sends the SBC READ (12) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="rdprotect">Instructs the drive how to check for protection information on the medium.</param>
/// <param name="dpo">If set to <c>true</c> requested blocks shall be assigned the lowest retention priority on cache fetch/retain.</param>
/// <param name="fua">If set to <c>true</c> requested blocks MUST bu read from medium and not the cache.</param>
/// <param name="fuaNv">If set to <c>true</c> requested blocks will be returned from non-volatile cache. If they're not present they shall be stored there.</param>
/// <param name="lba">Starting block.</param>
/// <param name="blockSize">Block size in bytes.</param>
/// <param name="groupNumber">Group number where attributes associated with this command should be collected.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="streaming">If set to <c>true</c> the stream playback operation should be used (MMC only).</param>
/// <param name="relAddr">If set to <c>true</c> address is relative to current position.</param>
2017-12-19 20:33:03 +00:00
public bool Read12 ( out byte [ ] buffer , out byte [ ] senseBuffer , byte rdprotect , bool dpo , bool fua , bool fuaNv ,
bool relAddr , uint lba , uint blockSize , byte groupNumber , uint transferLength ,
bool streaming , uint timeout , out double duration )
2016-01-11 19:40:58 +00:00
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 12 ] ;
cdb [ 0 ] = ( byte ) ScsiCommands . Read12 ;
cdb [ 1 ] = ( byte ) ( ( rdprotect & 0x07 ) < < 5 ) ;
2017-12-19 20:33:03 +00:00
if ( dpo ) cdb [ 1 ] + = 0x10 ;
if ( fua ) cdb [ 1 ] + = 0x08 ;
if ( fuaNv ) cdb [ 1 ] + = 0x02 ;
if ( relAddr ) cdb [ 1 ] + = 0x01 ;
2016-01-11 19:40:58 +00:00
cdb [ 2 ] = ( byte ) ( ( lba & 0xFF000000 ) > > 24 ) ;
cdb [ 3 ] = ( byte ) ( ( lba & 0xFF0000 ) > > 16 ) ;
cdb [ 4 ] = ( byte ) ( ( lba & 0xFF00 ) > > 8 ) ;
cdb [ 5 ] = ( byte ) ( lba & 0xFF ) ;
cdb [ 6 ] = ( byte ) ( ( transferLength & 0xFF000000 ) > > 24 ) ;
cdb [ 7 ] = ( byte ) ( ( transferLength & 0xFF0000 ) > > 16 ) ;
cdb [ 8 ] = ( byte ) ( ( transferLength & 0xFF00 ) > > 8 ) ;
cdb [ 9 ] = ( byte ) ( transferLength & 0xFF ) ;
cdb [ 10 ] = ( byte ) ( groupNumber & 0x1F ) ;
2017-12-19 20:33:03 +00:00
if ( streaming ) cdb [ 10 ] + = 0x80 ;
2016-01-11 19:40:58 +00:00
buffer = new byte [ transferLength * blockSize ] ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . In , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "READ (12) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Sends the SBC READ (16) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="rdprotect">Instructs the drive how to check for protection information on the medium.</param>
/// <param name="dpo">If set to <c>true</c> requested blocks shall be assigned the lowest retention priority on cache fetch/retain.</param>
/// <param name="fua">If set to <c>true</c> requested blocks MUST bu read from medium and not the cache.</param>
/// <param name="fuaNv">If set to <c>true</c> requested blocks will be returned from non-volatile cache. If they're not present they shall be stored there.</param>
/// <param name="lba">Starting block.</param>
/// <param name="blockSize">Block size in bytes.</param>
/// <param name="groupNumber">Group number where attributes associated with this command should be collected.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="streaming">If set to <c>true</c> the stream playback operation should be used (MMC only).</param>
2017-12-19 20:33:03 +00:00
public bool Read16 ( out byte [ ] buffer , out byte [ ] senseBuffer , byte rdprotect , bool dpo , bool fua , bool fuaNv ,
ulong lba , uint blockSize , byte groupNumber , uint transferLength , bool streaming ,
uint timeout , out double duration )
2016-01-11 19:40:58 +00:00
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 16 ] ;
byte [ ] lbaBytes = BitConverter . GetBytes ( lba ) ;
cdb [ 0 ] = ( byte ) ScsiCommands . Read16 ;
cdb [ 1 ] = ( byte ) ( ( rdprotect & 0x07 ) < < 5 ) ;
2017-12-19 20:33:03 +00:00
if ( dpo ) cdb [ 1 ] + = 0x10 ;
if ( fua ) cdb [ 1 ] + = 0x08 ;
if ( fuaNv ) cdb [ 1 ] + = 0x02 ;
2016-01-11 19:40:58 +00:00
cdb [ 2 ] = lbaBytes [ 7 ] ;
cdb [ 3 ] = lbaBytes [ 6 ] ;
cdb [ 4 ] = lbaBytes [ 5 ] ;
cdb [ 5 ] = lbaBytes [ 4 ] ;
cdb [ 6 ] = lbaBytes [ 3 ] ;
cdb [ 7 ] = lbaBytes [ 2 ] ;
cdb [ 8 ] = lbaBytes [ 1 ] ;
cdb [ 9 ] = lbaBytes [ 0 ] ;
cdb [ 10 ] = ( byte ) ( ( transferLength & 0xFF000000 ) > > 24 ) ;
cdb [ 11 ] = ( byte ) ( ( transferLength & 0xFF0000 ) > > 16 ) ;
cdb [ 12 ] = ( byte ) ( ( transferLength & 0xFF00 ) > > 8 ) ;
cdb [ 13 ] = ( byte ) ( transferLength & 0xFF ) ;
cdb [ 14 ] = ( byte ) ( groupNumber & 0x1F ) ;
2017-12-19 20:33:03 +00:00
if ( streaming ) cdb [ 14 ] + = 0x80 ;
2016-01-11 19:40:58 +00:00
buffer = new byte [ transferLength * blockSize ] ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . In , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "READ (16) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Sends the SBC READ LONG (10) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ LONG response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name = "relAddr"></param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="correct">If set to <c>true</c> ask the drive to try to correct errors in the sector.</param>
/// <param name="lba">LBA to read.</param>
/// <param name="transferBytes">How many bytes to read. If the number is not exactly the drive's size, the command will fail and incidate a delta of the size in SENSE.</param>
2017-12-19 20:33:03 +00:00
public bool ReadLong10 ( out byte [ ] buffer , out byte [ ] senseBuffer , bool correct , bool relAddr , uint lba ,
ushort transferBytes , uint timeout , out double duration )
2016-01-11 19:40:58 +00:00
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 10 ] ;
cdb [ 0 ] = ( byte ) ScsiCommands . ReadLong ;
2017-12-19 20:33:03 +00:00
if ( correct ) cdb [ 1 ] + = 0x02 ;
if ( relAddr ) cdb [ 1 ] + = 0x01 ;
2016-01-11 19:40:58 +00:00
cdb [ 2 ] = ( byte ) ( ( lba & 0xFF000000 ) > > 24 ) ;
cdb [ 3 ] = ( byte ) ( ( lba & 0xFF0000 ) > > 16 ) ;
cdb [ 4 ] = ( byte ) ( ( lba & 0xFF00 ) > > 8 ) ;
cdb [ 5 ] = ( byte ) ( lba & 0xFF ) ;
cdb [ 7 ] = ( byte ) ( ( transferBytes & 0xFF00 ) > > 8 ) ;
cdb [ 8 ] = ( byte ) ( transferBytes & 0xFF ) ;
buffer = new byte [ transferBytes ] ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . In , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "READ LONG (10) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Sends the SBC READ LONG (16) command
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer"/> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the SCSI READ LONG response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="correct">If set to <c>true</c> ask the drive to try to correct errors in the sector.</param>
/// <param name="lba">LBA to read.</param>
/// <param name="transferBytes">How many bytes to read. If the number is not exactly the drive's size, the command will fail and incidate a delta of the size in SENSE.</param>
2017-12-19 20:33:03 +00:00
public bool ReadLong16 ( out byte [ ] buffer , out byte [ ] senseBuffer , bool correct , ulong lba , uint transferBytes ,
uint timeout , out double duration )
2016-01-11 19:40:58 +00:00
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 16 ] ;
byte [ ] lbaBytes = BitConverter . GetBytes ( lba ) ;
cdb [ 0 ] = ( byte ) ScsiCommands . ServiceActionIn ;
cdb [ 1 ] = ( byte ) ScsiServiceActions . ReadLong16 ;
cdb [ 2 ] = lbaBytes [ 7 ] ;
cdb [ 3 ] = lbaBytes [ 6 ] ;
cdb [ 4 ] = lbaBytes [ 5 ] ;
cdb [ 5 ] = lbaBytes [ 4 ] ;
cdb [ 6 ] = lbaBytes [ 3 ] ;
cdb [ 7 ] = lbaBytes [ 2 ] ;
cdb [ 8 ] = lbaBytes [ 1 ] ;
cdb [ 9 ] = lbaBytes [ 0 ] ;
cdb [ 12 ] = ( byte ) ( ( transferBytes & 0xFF00 ) > > 8 ) ;
cdb [ 13 ] = ( byte ) ( transferBytes & 0xFF ) ;
2017-12-19 20:33:03 +00:00
if ( correct ) cdb [ 14 ] + = 0x01 ;
2016-01-11 19:40:58 +00:00
buffer = new byte [ transferBytes ] ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . In , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "READ LONG (16) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Moves the device reading element to the specified block address
/// </summary>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="lba">LBA.</param>
/// <param name="timeout">Timeout.</param>
/// <param name="duration">Duration.</param>
public bool Seek6 ( out byte [ ] senseBuffer , uint lba , uint timeout , out double duration )
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 6 ] ;
byte [ ] buffer = new byte [ 0 ] ;
cdb [ 0 ] = ( byte ) ScsiCommands . Seek6 ;
cdb [ 1 ] = ( byte ) ( ( lba & 0x1F0000 ) > > 16 ) ;
cdb [ 2 ] = ( byte ) ( ( lba & 0xFF00 ) > > 8 ) ;
cdb [ 3 ] = ( byte ) ( lba & 0xFF ) ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . None , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "SEEK (6) took {0} ms." , duration ) ;
return sense ;
}
/// <summary>
/// Moves the device reading element to the specified block address
/// </summary>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="lba">LBA.</param>
/// <param name="timeout">Timeout.</param>
/// <param name="duration">Duration.</param>
public bool Seek10 ( out byte [ ] senseBuffer , uint lba , uint timeout , out double duration )
{
senseBuffer = new byte [ 32 ] ;
byte [ ] cdb = new byte [ 10 ] ;
byte [ ] buffer = new byte [ 0 ] ;
cdb [ 0 ] = ( byte ) ScsiCommands . Seek10 ;
cdb [ 2 ] = ( byte ) ( ( lba & 0xFF000000 ) > > 24 ) ;
cdb [ 3 ] = ( byte ) ( ( lba & 0xFF0000 ) > > 16 ) ;
cdb [ 4 ] = ( byte ) ( ( lba & 0xFF00 ) > > 8 ) ;
cdb [ 5 ] = ( byte ) ( lba & 0xFF ) ;
2017-12-21 07:19:46 +00:00
LastError = SendScsiCommand ( cdb , ref buffer , out senseBuffer , timeout , ScsiDirection . None , out duration ,
2017-12-22 03:13:43 +00:00
out bool sense ) ;
2017-12-21 07:19:46 +00:00
Error = LastError ! = 0 ;
2016-01-11 19:40:58 +00:00
DicConsole . DebugWriteLine ( "SCSI Device" , "SEEK (10) took {0} ms." , duration ) ;
return sense ;
}
}
2017-12-19 20:33:03 +00:00
}