2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2015-10-12 19:55:00 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Commands.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Direct device access.
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Sends commands to devices.
|
2015-10-12 19:55:00 +01: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
|
2015-10-12 19:55:00 +01: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.
|
2015-10-12 19:55:00 +01: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/>.
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2015-10-12 19:55:00 +01:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
using System;
|
2017-12-22 03:13:43 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Decoders.ATA;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
namespace Aaru.Devices
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class Device
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
/// <summary>Sends a SCSI command to this device</summary>
|
2015-10-12 20:08:56 +01:00
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <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>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// <param name="sense">
|
|
|
|
|
/// <c>True</c> if SCSI command returned non-OK status and <paramref name="senseBuffer" /> contains
|
|
|
|
|
/// SCSI sense
|
|
|
|
|
/// </param>
|
2019-10-13 23:31:56 +01:00
|
|
|
public int SendScsiCommand(byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout,
|
2020-02-29 18:03:35 +00:00
|
|
|
ScsiDirection direction, out double duration, out bool sense)
|
2019-10-13 23:31:56 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!(_remote is null))
|
|
|
|
|
return _remote.SendScsiCommand(cdb, ref buffer, out senseBuffer, timeout, direction, out duration,
|
|
|
|
|
out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
|
|
|
|
|
return Command.SendScsiCommand(PlatformId, FileHandle, cdb, ref buffer, out senseBuffer, timeout, direction,
|
2020-02-29 18:03:35 +00:00
|
|
|
out duration, out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
}
|
2015-10-14 02:53:46 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
/// <summary>Sends an ATA/ATAPI command to this device using CHS addressing</summary>
|
2015-10-14 03:24:25 +01:00
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="registers">ATA registers.</param>
|
|
|
|
|
/// <param name="errorRegisters">Status/error registers.</param>
|
|
|
|
|
/// <param name="protocol">ATA Protocol.</param>
|
|
|
|
|
/// <param name="transferRegister">Indicates which register indicates the transfer length</param>
|
|
|
|
|
/// <param name="buffer">Buffer for ATA/ATAPI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// <param name="transferBlocks">
|
|
|
|
|
/// If set to <c>true</c>, transfer is indicated in blocks, otherwise, it is indicated in
|
|
|
|
|
/// bytes.
|
|
|
|
|
/// </param>
|
2015-10-14 03:24:25 +01:00
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA/ATAPI command returned non-OK status</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
public int SendAtaCommand(AtaRegistersChs registers, out AtaErrorRegistersChs errorRegisters,
|
2020-02-29 18:03:35 +00:00
|
|
|
AtaProtocol protocol, AtaTransferRegister transferRegister, ref byte[] buffer,
|
|
|
|
|
uint timeout, bool transferBlocks, out double duration, out bool sense)
|
2019-10-13 23:31:56 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!(_remote is null))
|
|
|
|
|
return _remote.SendAtaCommand(registers, out errorRegisters, protocol, transferRegister, ref buffer,
|
|
|
|
|
timeout, transferBlocks, out duration, out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
|
|
|
|
|
return Command.SendAtaCommand(PlatformId, FileHandle, registers, out errorRegisters, protocol,
|
2020-02-29 18:03:35 +00:00
|
|
|
transferRegister, ref buffer, timeout, transferBlocks, out duration,
|
|
|
|
|
out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
}
|
2015-10-14 02:53:46 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
/// <summary>Sends an ATA/ATAPI command to this device using 28-bit LBA addressing</summary>
|
2015-10-14 03:24:25 +01:00
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="registers">ATA registers.</param>
|
|
|
|
|
/// <param name="errorRegisters">Status/error registers.</param>
|
|
|
|
|
/// <param name="protocol">ATA Protocol.</param>
|
|
|
|
|
/// <param name="transferRegister">Indicates which register indicates the transfer length</param>
|
|
|
|
|
/// <param name="buffer">Buffer for ATA/ATAPI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// <param name="transferBlocks">
|
|
|
|
|
/// If set to <c>true</c>, transfer is indicated in blocks, otherwise, it is indicated in
|
|
|
|
|
/// bytes.
|
|
|
|
|
/// </param>
|
2015-10-14 03:24:25 +01:00
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA/ATAPI command returned non-OK status</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
public int SendAtaCommand(AtaRegistersLba28 registers, out AtaErrorRegistersLba28 errorRegisters,
|
2020-02-29 18:03:35 +00:00
|
|
|
AtaProtocol protocol, AtaTransferRegister transferRegister, ref byte[] buffer,
|
|
|
|
|
uint timeout, bool transferBlocks, out double duration, out bool sense)
|
2019-10-13 23:31:56 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!(_remote is null))
|
|
|
|
|
return _remote.SendAtaCommand(registers, out errorRegisters, protocol, transferRegister, ref buffer,
|
|
|
|
|
timeout, transferBlocks, out duration, out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
|
|
|
|
|
return Command.SendAtaCommand(PlatformId, FileHandle, registers, out errorRegisters, protocol,
|
2020-02-29 18:03:35 +00:00
|
|
|
transferRegister, ref buffer, timeout, transferBlocks, out duration,
|
|
|
|
|
out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
}
|
2015-10-14 02:53:46 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
/// <summary>Sends an ATA/ATAPI command to this device using 48-bit LBA addressing</summary>
|
2015-10-14 03:24:25 +01:00
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="registers">ATA registers.</param>
|
|
|
|
|
/// <param name="errorRegisters">Status/error registers.</param>
|
|
|
|
|
/// <param name="protocol">ATA Protocol.</param>
|
|
|
|
|
/// <param name="transferRegister">Indicates which register indicates the transfer length</param>
|
|
|
|
|
/// <param name="buffer">Buffer for ATA/ATAPI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// <param name="transferBlocks">
|
|
|
|
|
/// If set to <c>true</c>, transfer is indicated in blocks, otherwise, it is indicated in
|
|
|
|
|
/// bytes.
|
|
|
|
|
/// </param>
|
2015-10-14 03:24:25 +01:00
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA/ATAPI command returned non-OK status</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
public int SendAtaCommand(AtaRegistersLba48 registers, out AtaErrorRegistersLba48 errorRegisters,
|
2020-02-29 18:03:35 +00:00
|
|
|
AtaProtocol protocol, AtaTransferRegister transferRegister, ref byte[] buffer,
|
|
|
|
|
uint timeout, bool transferBlocks, out double duration, out bool sense)
|
2019-10-13 23:31:56 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
if(!(_remote is null))
|
|
|
|
|
return _remote.SendAtaCommand(registers, out errorRegisters, protocol, transferRegister, ref buffer,
|
|
|
|
|
timeout, transferBlocks, out duration, out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
|
|
|
|
|
return Command.SendAtaCommand(PlatformId, FileHandle, registers, out errorRegisters, protocol,
|
2020-02-29 18:03:35 +00:00
|
|
|
transferRegister, ref buffer, timeout, transferBlocks, out duration,
|
|
|
|
|
out sense);
|
2019-10-13 23:31:56 +01:00
|
|
|
}
|
2016-10-22 22:58:01 +01:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
/// <summary>Sends a MMC/SD command to this device</summary>
|
2016-10-22 22:58:01 +01:00
|
|
|
/// <returns>The result of the command.</returns>
|
|
|
|
|
/// <param name="command">MMC/SD opcode</param>
|
|
|
|
|
/// <param name="buffer">Buffer for MMC/SD command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if MMC/SD returned non-OK status</param>
|
|
|
|
|
/// <param name="write"><c>True</c> if data is sent from host to card</param>
|
|
|
|
|
/// <param name="isApplication"><c>True</c> if command should be preceded with CMD55</param>
|
|
|
|
|
/// <param name="flags">Flags indicating kind and place of response</param>
|
|
|
|
|
/// <param name="blocks">How many blocks to transfer</param>
|
|
|
|
|
/// <param name="argument">Command argument</param>
|
|
|
|
|
/// <param name="response">Response registers</param>
|
|
|
|
|
/// <param name="blockSize">Size of block in bytes</param>
|
2020-02-29 18:03:35 +00:00
|
|
|
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 = 0)
|
2016-10-22 22:58:01 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
switch(command)
|
2017-12-23 20:04:36 +00:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
case MmcCommands.SendCid when _cachedCid != null:
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
DateTime start = DateTime.Now;
|
2020-07-20 21:11:32 +01:00
|
|
|
buffer = new byte[_cachedCid.Length];
|
|
|
|
|
Array.Copy(_cachedCid, buffer, buffer.Length);
|
2017-12-21 04:43:29 +00:00
|
|
|
response = new uint[4];
|
2020-02-29 18:03:35 +00:00
|
|
|
sense = false;
|
|
|
|
|
DateTime end = DateTime.Now;
|
2017-12-21 04:43:29 +00:00
|
|
|
duration = (end - start).TotalMilliseconds;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
case MmcCommands.SendCsd when _cachedCid != null:
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
DateTime start = DateTime.Now;
|
2020-07-20 21:11:32 +01:00
|
|
|
buffer = new byte[_cachedCsd.Length];
|
|
|
|
|
Array.Copy(_cachedCsd, buffer, buffer.Length);
|
2017-12-21 04:43:29 +00:00
|
|
|
response = new uint[4];
|
2020-02-29 18:03:35 +00:00
|
|
|
sense = false;
|
|
|
|
|
DateTime end = DateTime.Now;
|
2017-12-21 04:43:29 +00:00
|
|
|
duration = (end - start).TotalMilliseconds;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
case (MmcCommands)SecureDigitalCommands.SendScr when _cachedScr != null:
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
DateTime start = DateTime.Now;
|
2020-07-20 21:11:32 +01:00
|
|
|
buffer = new byte[_cachedScr.Length];
|
|
|
|
|
Array.Copy(_cachedScr, buffer, buffer.Length);
|
2017-12-21 04:43:29 +00:00
|
|
|
response = new uint[4];
|
2020-02-29 18:03:35 +00:00
|
|
|
sense = false;
|
|
|
|
|
DateTime end = DateTime.Now;
|
2019-10-13 23:31:56 +01:00
|
|
|
duration = (end - start).TotalMilliseconds;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2019-10-13 23:31:56 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
case (MmcCommands)SecureDigitalCommands.SendOperatingCondition when _cachedOcr != null:
|
|
|
|
|
case MmcCommands.SendOpCond when _cachedOcr != null:
|
2019-10-13 23:31:56 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
DateTime start = DateTime.Now;
|
2020-07-20 21:11:32 +01:00
|
|
|
buffer = new byte[_cachedOcr.Length];
|
|
|
|
|
Array.Copy(_cachedOcr, buffer, buffer.Length);
|
2019-10-13 23:31:56 +01:00
|
|
|
response = new uint[4];
|
2020-02-29 18:03:35 +00:00
|
|
|
sense = false;
|
|
|
|
|
DateTime end = DateTime.Now;
|
2017-12-21 04:43:29 +00:00
|
|
|
duration = (end - start).TotalMilliseconds;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2017-09-28 19:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:58:58 +00:00
|
|
|
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 = 0)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2020-12-12 20:58:58 +00:00
|
|
|
return error;
|
2016-10-22 22:58:01 +01:00
|
|
|
}
|
2020-12-12 21:03:01 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2015-10-12 19:55:00 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|