2022-03-26 19:35:13 +00:00
|
|
|
// /***************************************************************************
|
|
|
|
|
// Aaru Data Preservation Suite
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Commands.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2022-03-26 19:35:13 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2023-09-26 20:16:24 +01:00
|
|
|
using System.Diagnostics;
|
2022-03-26 19:35:13 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using Aaru.Decoders.ATA;
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Devices.Remote;
|
|
|
|
|
|
2022-03-26 19:35:13 +00:00
|
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
|
|
|
|
public partial class Device
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
2025-08-22 19:57:09 +01:00
|
|
|
public override int SendScsiCommand(Span<byte> cdb, ref byte[] buffer, uint timeout, ScsiDirection direction,
|
|
|
|
|
out double duration, out bool sense)
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
|
|
|
|
// We need a timeout
|
2024-05-01 04:05:22 +01:00
|
|
|
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
2025-08-22 19:57:09 +01:00
|
|
|
return _remote.SendScsiCommand(cdb, ref buffer, SenseBuffer, timeout, direction, out duration, out sense);
|
2022-03-26 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override 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
|
2024-05-01 04:05:22 +01:00
|
|
|
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
|
|
|
|
|
|
|
|
|
|
return _remote.SendAtaCommand(registers,
|
|
|
|
|
out errorRegisters,
|
|
|
|
|
protocol,
|
|
|
|
|
transferRegister,
|
|
|
|
|
ref buffer,
|
|
|
|
|
timeout,
|
|
|
|
|
transferBlocks,
|
|
|
|
|
out duration,
|
|
|
|
|
out sense);
|
2022-03-26 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override 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
|
2024-05-01 04:05:22 +01:00
|
|
|
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
|
|
|
|
|
|
|
|
|
|
return _remote.SendAtaCommand(registers,
|
|
|
|
|
out errorRegisters,
|
|
|
|
|
protocol,
|
|
|
|
|
transferRegister,
|
|
|
|
|
ref buffer,
|
|
|
|
|
timeout,
|
|
|
|
|
transferBlocks,
|
|
|
|
|
out duration,
|
|
|
|
|
out sense);
|
2022-03-26 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override 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
|
2024-05-01 04:05:22 +01:00
|
|
|
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
|
|
|
|
|
|
|
|
|
|
return _remote.SendAtaCommand(registers,
|
|
|
|
|
out errorRegisters,
|
|
|
|
|
protocol,
|
|
|
|
|
transferRegister,
|
|
|
|
|
ref buffer,
|
|
|
|
|
timeout,
|
|
|
|
|
transferBlocks,
|
|
|
|
|
out duration,
|
|
|
|
|
out sense);
|
2022-03-26 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-10-03 23:12:01 +01:00
|
|
|
public override 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)
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
|
|
|
|
// We need a timeout
|
2024-05-01 04:05:22 +01:00
|
|
|
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
2023-09-26 20:16:24 +01:00
|
|
|
var cmdStopwatch = new Stopwatch();
|
|
|
|
|
|
2022-03-26 19:35:13 +00:00
|
|
|
switch(command)
|
|
|
|
|
{
|
2023-10-05 01:52:48 +01:00
|
|
|
case MmcCommands.SendCid when CachedCid != null:
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Restart();
|
2023-10-05 01:52:48 +01:00
|
|
|
buffer = new byte[CachedCid.Length];
|
|
|
|
|
Array.Copy(CachedCid, buffer, buffer.Length);
|
2022-03-26 19:35:13 +00:00
|
|
|
response = new uint[4];
|
|
|
|
|
sense = false;
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Stop();
|
|
|
|
|
duration = cmdStopwatch.Elapsed.TotalMilliseconds;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-10-05 01:52:48 +01:00
|
|
|
case MmcCommands.SendCsd when CachedCid != null:
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Restart();
|
2023-10-05 01:52:48 +01:00
|
|
|
buffer = new byte[CachedCsd.Length];
|
|
|
|
|
Array.Copy(CachedCsd, buffer, buffer.Length);
|
2022-03-26 19:35:13 +00:00
|
|
|
response = new uint[4];
|
|
|
|
|
sense = false;
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Stop();
|
|
|
|
|
duration = cmdStopwatch.Elapsed.TotalMilliseconds;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-10-05 01:52:48 +01:00
|
|
|
case (MmcCommands)SecureDigitalCommands.SendScr when CachedScr != null:
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Restart();
|
2023-10-05 01:52:48 +01:00
|
|
|
buffer = new byte[CachedScr.Length];
|
|
|
|
|
Array.Copy(CachedScr, buffer, buffer.Length);
|
2022-03-26 19:35:13 +00:00
|
|
|
response = new uint[4];
|
|
|
|
|
sense = false;
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Stop();
|
|
|
|
|
duration = cmdStopwatch.Elapsed.TotalMilliseconds;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-10-05 01:52:48 +01:00
|
|
|
case (MmcCommands)SecureDigitalCommands.SendOperatingCondition when CachedOcr != null:
|
|
|
|
|
case MmcCommands.SendOpCond when CachedOcr != null:
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Restart();
|
2023-10-05 01:52:48 +01:00
|
|
|
buffer = new byte[CachedOcr.Length];
|
|
|
|
|
Array.Copy(CachedOcr, buffer, buffer.Length);
|
2022-03-26 19:35:13 +00:00
|
|
|
response = new uint[4];
|
|
|
|
|
sense = false;
|
2023-09-26 20:16:24 +01:00
|
|
|
cmdStopwatch.Stop();
|
|
|
|
|
duration = cmdStopwatch.Elapsed.TotalMilliseconds;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
return _remote.SendMmcCommand(command,
|
|
|
|
|
write,
|
|
|
|
|
isApplication,
|
|
|
|
|
flags,
|
|
|
|
|
argument,
|
|
|
|
|
blockSize,
|
|
|
|
|
blocks,
|
|
|
|
|
ref buffer,
|
|
|
|
|
out response,
|
|
|
|
|
out duration,
|
|
|
|
|
out sense,
|
|
|
|
|
timeout);
|
2022-03-26 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override int SendMultipleMmcCommands(MmcSingleCommand[] commands, out double duration, out bool sense,
|
2023-10-03 23:12:01 +01:00
|
|
|
uint timeout = 15)
|
2022-03-26 19:35:13 +00:00
|
|
|
{
|
|
|
|
|
// We need a timeout
|
2024-05-01 04:05:22 +01:00
|
|
|
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
|
|
|
|
if(_remote.ServerProtocolVersion >= 2)
|
|
|
|
|
return _remote.SendMultipleMmcCommands(commands, out duration, out sense, timeout);
|
|
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
var error = 0;
|
2022-03-26 19:35:13 +00:00
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
|
|
|
|
|
|
|
|
|
foreach(MmcSingleCommand command in commands)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
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;
|
2022-03-26 19:35:13 +00:00
|
|
|
|
|
|
|
|
duration += cmdDuration;
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(cmdSense) sense = true;
|
2022-03-26 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool ReOpen() => _remote.ReOpen();
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool BufferedOsRead(out byte[] buffer, long offset, uint length, out double duration) =>
|
|
|
|
|
_remote.BufferedOsRead(out buffer, offset, length, out duration);
|
|
|
|
|
}
|