// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Commands.cs // Author(s) : Natalia Portillo // // Component : Direct device access. // // --[ Description ] ---------------------------------------------------------- // // Sends commands to 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 . // // ---------------------------------------------------------------------------- // Copyright © 2011-2021 Natalia Portillo // ****************************************************************************/ using System; using System.Diagnostics.CodeAnalysis; using Aaru.Decoders.ATA; namespace Aaru.Devices { [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] public sealed partial class Device { /// Sends a SCSI command to this device /// 0 if no error occurred, otherwise, errno /// SCSI CDB /// Buffer for SCSI command response /// Buffer with the SCSI sense /// Timeout in seconds /// SCSI command transfer direction /// Time it took to execute the command in milliseconds /// /// True if SCSI command returned non-OK status and contains /// SCSI sense /// public int SendScsiCommand(byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout, ScsiDirection direction, out double duration, out bool sense) { // We need a timeout if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15; if(!(_remote is null)) return _remote.SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, direction, out duration, out sense); return Command.SendScsiCommand(PlatformId, FileHandle, cdb, ref buffer, out senseBuffer, timeout, direction, out duration, out sense); } /// Sends an ATA/ATAPI command to this device using CHS addressing /// 0 if no error occurred, otherwise, errno /// ATA registers. /// Status/error registers. /// ATA Protocol. /// Indicates which register indicates the transfer length /// Buffer for ATA/ATAPI command response /// Timeout in seconds /// /// If set to true, transfer is indicated in blocks, otherwise, it is indicated in /// bytes. /// /// Time it took to execute the command in milliseconds /// True if ATA/ATAPI command returned non-OK status public int SendAtaCommand(AtaRegistersChs registers, out AtaErrorRegistersChs errorRegisters, AtaProtocol protocol, AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout, bool transferBlocks, out double duration, out bool sense) { // We need a timeout if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15; if(!(_remote is null)) return _remote.SendAtaCommand(registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); return Command.SendAtaCommand(PlatformId, FileHandle, registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); } /// Sends an ATA/ATAPI command to this device using 28-bit LBA addressing /// 0 if no error occurred, otherwise, errno /// ATA registers. /// Status/error registers. /// ATA Protocol. /// Indicates which register indicates the transfer length /// Buffer for ATA/ATAPI command response /// Timeout in seconds /// /// If set to true, transfer is indicated in blocks, otherwise, it is indicated in /// bytes. /// /// Time it took to execute the command in milliseconds /// True if ATA/ATAPI command returned non-OK status public int SendAtaCommand(AtaRegistersLba28 registers, out AtaErrorRegistersLba28 errorRegisters, AtaProtocol protocol, AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout, bool transferBlocks, out double duration, out bool sense) { // We need a timeout if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15; if(!(_remote is null)) return _remote.SendAtaCommand(registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); return Command.SendAtaCommand(PlatformId, FileHandle, registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); } /// Sends an ATA/ATAPI command to this device using 48-bit LBA addressing /// 0 if no error occurred, otherwise, errno /// ATA registers. /// Status/error registers. /// ATA Protocol. /// Indicates which register indicates the transfer length /// Buffer for ATA/ATAPI command response /// Timeout in seconds /// /// If set to true, transfer is indicated in blocks, otherwise, it is indicated in /// bytes. /// /// Time it took to execute the command in milliseconds /// True if ATA/ATAPI command returned non-OK status public int SendAtaCommand(AtaRegistersLba48 registers, out AtaErrorRegistersLba48 errorRegisters, AtaProtocol protocol, AtaTransferRegister transferRegister, ref byte[] buffer, uint timeout, bool transferBlocks, out double duration, out bool sense) { // We need a timeout if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15; if(!(_remote is null)) return _remote.SendAtaCommand(registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); return Command.SendAtaCommand(PlatformId, FileHandle, registers, out errorRegisters, protocol, transferRegister, ref buffer, timeout, transferBlocks, out duration, out sense); } /// Sends a MMC/SD command to this device /// The result of the command. /// MMC/SD opcode /// Buffer for MMC/SD command response /// Timeout in seconds /// Time it took to execute the command in milliseconds /// True if MMC/SD returned non-OK status /// True if data is sent from host to card /// True if command should be preceded with CMD55 /// Flags indicating kind and place of response /// How many blocks to transfer /// Command argument /// Response registers /// Size of block in bytes public int SendMmcCommand(MmcCommands command, bool write, bool isApplication, MmcFlags flags, uint argument, uint blockSize, uint blocks, ref byte[] buffer, out uint[] response, out double duration, out bool sense, uint timeout = 15) { // We need a timeout if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15; switch(command) { case MmcCommands.SendCid when _cachedCid != null: { DateTime start = DateTime.Now; buffer = new byte[_cachedCid.Length]; Array.Copy(_cachedCid, buffer, buffer.Length); response = new uint[4]; sense = false; DateTime end = DateTime.Now; duration = (end - start).TotalMilliseconds; return 0; } case MmcCommands.SendCsd when _cachedCid != null: { DateTime start = DateTime.Now; buffer = new byte[_cachedCsd.Length]; Array.Copy(_cachedCsd, buffer, buffer.Length); response = new uint[4]; sense = false; DateTime end = DateTime.Now; duration = (end - start).TotalMilliseconds; return 0; } case (MmcCommands)SecureDigitalCommands.SendScr when _cachedScr != null: { DateTime start = DateTime.Now; buffer = new byte[_cachedScr.Length]; Array.Copy(_cachedScr, buffer, buffer.Length); response = new uint[4]; sense = false; DateTime end = DateTime.Now; duration = (end - start).TotalMilliseconds; return 0; } case (MmcCommands)SecureDigitalCommands.SendOperatingCondition when _cachedOcr != null: case MmcCommands.SendOpCond when _cachedOcr != null: { DateTime start = DateTime.Now; buffer = new byte[_cachedOcr.Length]; Array.Copy(_cachedOcr, buffer, buffer.Length); response = new uint[4]; sense = false; DateTime end = DateTime.Now; duration = (end - start).TotalMilliseconds; return 0; } } return _remote is null ? Command.SendMmcCommand(PlatformId, FileHandle, command, write, isApplication, flags, argument, blockSize, blocks, ref buffer, out response, out duration, out sense, timeout) : _remote.SendMmcCommand(command, write, isApplication, flags, argument, blockSize, blocks, ref buffer, out response, out duration, out sense, timeout); } public class MmcSingleCommand { public uint argument; public uint blocks; public uint blockSize; public byte[] buffer; public MmcCommands command; public MmcFlags flags; public bool isApplication; public uint[] response; public bool write; } public int SendMultipleMmcCommands(MmcSingleCommand[] commands, out double duration, out bool sense, uint timeout = 15) { // We need a timeout if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15; if(_remote is null) return Command.SendMultipleMmcCommands(PlatformId, FileHandle, commands, out duration, out sense, timeout); if(_remote.ServerProtocolVersion >= 2) return _remote.SendMultipleMmcCommands(commands, out duration, out sense, timeout); int error = 0; duration = 0; sense = false; foreach(MmcSingleCommand command in commands) { int singleError = _remote.SendMmcCommand(command.command, command.write, command.isApplication, command.flags, command.argument, command.blockSize, command.blocks, ref command.buffer, out command.response, out double cmdDuration, out bool cmdSense, timeout); if(error == 0 && singleError != 0) error = singleError; duration += cmdDuration; if(cmdSense) sense = true; } return error; } public bool ReOpen() { if(!(_remote is null)) return _remote.ReOpen(); int ret = Command.ReOpen(PlatformId, _devicePath, FileHandle, out object newFileHandle); FileHandle = newFileHandle; Error = ret != 0; LastError = ret; return Error; } public bool BufferedOsRead(out byte[] buffer, long offset, uint length, out double duration) { if(!(_remote is null)) return _remote.BufferedOsRead(out buffer, offset, length, out duration); int ret = Command.BufferedOsRead(PlatformId, FileHandle, out buffer, offset, length, out duration); Error = ret != 0; LastError = ret; return Error; } } }