// /*************************************************************************** // The Disc Image Chef // ---------------------------------------------------------------------------- // // Filename : SBC.cs // Version : 1.0 // Author(s) : Natalia Portillo // // Component : SCSI Block Commands // // Revision : $Revision$ // Last change by : $Author$ // Date : $Date$ // // --[ Description ] ---------------------------------------------------------- // // Description // // --[ 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 . // // ---------------------------------------------------------------------------- // Copyright (C) 2011-2015 Claunia.com // ****************************************************************************/ // //$Id$ using System; using DiscImageChef.Console; namespace DiscImageChef.Devices { public partial class Device { /// /// Sends the SBC READ (6) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ response will be stored /// Sense buffer. /// Timeout in seconds. /// Duration in milliseconds it took for the device to execute the command. /// Starting block. /// Block size in bytes. public bool Read6(out byte[] buffer, out byte[] senseBuffer, uint lba, uint blockSize, uint timeout, out double duration) { return Read6(out buffer, out senseBuffer, lba, blockSize, 1, timeout, out duration); } /// /// Sends the SBC READ (6) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ response will be stored /// Sense buffer. /// Timeout in seconds. /// Duration in milliseconds it took for the device to execute the command. /// Starting block. /// Block size in bytes. /// How many blocks to read. public bool Read6(out byte[] buffer, out byte[] senseBuffer, uint lba, uint blockSize, byte transferLength, uint timeout, out double duration) { senseBuffer = new byte[32]; byte[] cdb = new byte[6]; bool sense; 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; if(transferLength == 0) buffer = new byte[256 * blockSize]; else buffer = new byte[transferLength * blockSize]; lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.In, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "READ (6) took {0} ms.", duration); return sense; } /// /// Sends the SBC READ (10) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ response will be stored /// Sense buffer. /// Timeout in seconds. /// Duration in milliseconds it took for the device to execute the command. /// Instructs the drive how to check for protection information on the medium. /// If set to true requested blocks shall be assigned the lowest retention priority on cache fetch/retain. /// If set to true requested blocks MUST bu read from medium and not the cache. /// If set to true requested blocks will be returned from non-volatile cache. If they're not present they shall be stored there. /// Starting block. /// Block size in bytes. /// Group number where attributes associated with this command should be collected. /// How many blocks to read. /// If set to true address is relative to current position. 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) { senseBuffer = new byte[32]; byte[] cdb = new byte[10]; bool sense; cdb[0] = (byte)ScsiCommands.Read10; cdb[1] = (byte)((rdprotect & 0x07) << 5); if(dpo) cdb[1] += 0x10; if(fua) cdb[1] += 0x08; if(fuaNv) cdb[1] += 0x02; if(relAddr) cdb[1] += 0x01; 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]; lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.In, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "READ (10) took {0} ms.", duration); return sense; } /// /// Sends the SBC READ (12) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ response will be stored /// Sense buffer. /// Timeout in seconds. /// Duration in milliseconds it took for the device to execute the command. /// Instructs the drive how to check for protection information on the medium. /// If set to true requested blocks shall be assigned the lowest retention priority on cache fetch/retain. /// If set to true requested blocks MUST bu read from medium and not the cache. /// If set to true requested blocks will be returned from non-volatile cache. If they're not present they shall be stored there. /// Starting block. /// Block size in bytes. /// Group number where attributes associated with this command should be collected. /// How many blocks to read. /// If set to true the stream playback operation should be used (MMC only). /// If set to true address is relative to current position. 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) { senseBuffer = new byte[32]; byte[] cdb = new byte[12]; bool sense; cdb[0] = (byte)ScsiCommands.Read12; cdb[1] = (byte)((rdprotect & 0x07) << 5); if(dpo) cdb[1] += 0x10; if(fua) cdb[1] += 0x08; if(fuaNv) cdb[1] += 0x02; if(relAddr) cdb[1] += 0x01; 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); if(streaming) cdb[10] += 0x80; buffer = new byte[transferLength * blockSize]; lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.In, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "READ (12) took {0} ms.", duration); return sense; } /// /// Sends the SBC READ (16) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ response will be stored /// Sense buffer. /// Timeout in seconds. /// Duration in milliseconds it took for the device to execute the command. /// Instructs the drive how to check for protection information on the medium. /// If set to true requested blocks shall be assigned the lowest retention priority on cache fetch/retain. /// If set to true requested blocks MUST bu read from medium and not the cache. /// If set to true requested blocks will be returned from non-volatile cache. If they're not present they shall be stored there. /// Starting block. /// Block size in bytes. /// Group number where attributes associated with this command should be collected. /// How many blocks to read. /// If set to true the stream playback operation should be used (MMC only). 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) { senseBuffer = new byte[32]; byte[] cdb = new byte[16]; bool sense; byte[] lbaBytes = BitConverter.GetBytes(lba); cdb[0] = (byte)ScsiCommands.Read16; cdb[1] = (byte)((rdprotect & 0x07) << 5); if(dpo) cdb[1] += 0x10; if(fua) cdb[1] += 0x08; if(fuaNv) cdb[1] += 0x02; 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); if(streaming) cdb[14] += 0x80; buffer = new byte[transferLength * blockSize]; lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.In, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "READ (16) took {0} ms.", duration); return sense; } /// /// Sends the SBC READ LONG (10) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ LONG response will be stored /// Sense buffer. /// Timeout in seconds. /// /// Duration in milliseconds it took for the device to execute the command. /// If set to true ask the drive to try to correct errors in the sector. /// LBA to read. /// 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. public bool ReadLong10(out byte[] buffer, out byte[] senseBuffer, bool correct, bool relAddr, uint lba, ushort transferBytes, uint timeout, out double duration) { senseBuffer = new byte[32]; byte[] cdb = new byte[10]; bool sense; cdb[0] = (byte)ScsiCommands.ReadLong; if(correct) cdb[1] += 0x02; if(relAddr) cdb[1] += 0x01; 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]; lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.In, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "READ LONG (10) took {0} ms.", duration); return sense; } /// /// Sends the SBC READ LONG (16) command /// /// true if the command failed and contains the sense buffer. /// Buffer where the SCSI READ LONG response will be stored /// Sense buffer. /// Timeout in seconds. /// Duration in milliseconds it took for the device to execute the command. /// If set to true ask the drive to try to correct errors in the sector. /// LBA to read. /// 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. public bool ReadLong16(out byte[] buffer, out byte[] senseBuffer, bool correct, ulong lba, uint transferBytes, uint timeout, out double duration) { senseBuffer = new byte[32]; byte[] cdb = new byte[16]; bool sense; 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); if(correct) cdb[14] += 0x01; buffer = new byte[transferBytes]; lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.In, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "READ LONG (16) took {0} ms.", duration); return sense; } /// /// Moves the device reading element to the specified block address /// /// Sense buffer. /// LBA. /// Timeout. /// Duration. 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]; bool sense; cdb[0] = (byte)ScsiCommands.Seek6; cdb[1] = (byte)((lba & 0x1F0000) >> 16); cdb[2] = (byte)((lba & 0xFF00) >> 8); cdb[3] = (byte)(lba & 0xFF); lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.None, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "SEEK (6) took {0} ms.", duration); return sense; } /// /// Moves the device reading element to the specified block address /// /// Sense buffer. /// LBA. /// Timeout. /// Duration. 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]; bool sense; 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); lastError = SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, ScsiDirection.None, out duration, out sense); error = lastError != 0; DicConsole.DebugWriteLine("SCSI Device", "SEEK (10) took {0} ms.", duration); return sense; } } }