2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-02-08 00:13:49 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Cfa.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : ATA commands.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Contains Compact Flash Association commands.
|
|
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2021-08-17 18:21:12 +01:00
|
|
|
using System;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Console;
|
|
|
|
|
using Aaru.Decoders.ATA;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
namespace Aaru.Devices
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
public sealed partial class Device
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2021-08-17 21:23:10 +01:00
|
|
|
/// <summary>Requests to translate an LBA to a card physical address</summary>
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <param name="buffer">Data buffer</param>
|
|
|
|
|
/// <param name="statusRegisters">Returned status registers</param>
|
|
|
|
|
/// <param name="lba">LBA to start reading from</param>
|
|
|
|
|
/// <param name="timeout">Timeout to wait for command execution</param>
|
|
|
|
|
/// <param name="duration">Time the device took to execute the command in milliseconds</param>
|
|
|
|
|
/// <returns><c>true</c> if the device set an error condition, <c>false</c> otherwise</returns>
|
2020-02-29 18:03:35 +00:00
|
|
|
public bool TranslateSector(out byte[] buffer, out AtaErrorRegistersLba28 statusRegisters, uint lba,
|
|
|
|
|
uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
|
|
|
|
buffer = new byte[512];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
var registers = new AtaRegistersLba28
|
2017-12-22 03:13:43 +00:00
|
|
|
{
|
2020-07-20 04:34:16 +01:00
|
|
|
Command = (byte)AtaCommands.TranslateSector,
|
|
|
|
|
DeviceHead = (byte)((lba & 0xF000000) / 0x1000000),
|
|
|
|
|
LbaHigh = (byte)((lba & 0xFF0000) / 0x10000),
|
|
|
|
|
LbaMid = (byte)((lba & 0xFF00) / 0x100),
|
|
|
|
|
LbaLow = (byte)((lba & 0xFF) / 0x1)
|
2017-12-22 03:13:43 +00:00
|
|
|
};
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.DeviceHead += 0x40;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn,
|
|
|
|
|
AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("ATA Device", "CFA TRANSLATE SECTOR took {0} ms.", duration);
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 21:23:10 +01:00
|
|
|
/// <summary>Requests to translate a CHS to a card physical address</summary>
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <param name="buffer">Data buffer</param>
|
|
|
|
|
/// <param name="statusRegisters">Returned status registers</param>
|
|
|
|
|
/// <param name="cylinder">Cylinder</param>
|
|
|
|
|
/// <param name="head">Head</param>
|
|
|
|
|
/// <param name="sector">Sector</param>
|
|
|
|
|
/// <param name="timeout">Timeout to wait for command execution</param>
|
|
|
|
|
/// <param name="duration">Time the device took to execute the command in milliseconds</param>
|
|
|
|
|
/// <returns><c>true</c> if the device set an error condition, <c>false</c> otherwise</returns>
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool TranslateSector(out byte[] buffer, out AtaErrorRegistersChs statusRegisters, ushort cylinder,
|
2020-02-29 18:03:35 +00:00
|
|
|
byte head, byte sector, uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
|
|
|
|
buffer = new byte[512];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
var registers = new AtaRegistersChs
|
2017-12-22 03:13:43 +00:00
|
|
|
{
|
2020-07-20 04:34:16 +01:00
|
|
|
Command = (byte)AtaCommands.TranslateSector,
|
|
|
|
|
CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100),
|
|
|
|
|
CylinderLow = (byte)((cylinder & 0xFF) / 0x1),
|
|
|
|
|
Sector = sector,
|
|
|
|
|
DeviceHead = (byte)(head & 0x0F)
|
2017-12-22 03:13:43 +00:00
|
|
|
};
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn,
|
|
|
|
|
AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("ATA Device", "CFA TRANSLATE SECTOR took {0} ms.", duration);
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 21:23:10 +01:00
|
|
|
/// <summary>Requests an extended error code</summary>
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <param name="errorCode">Error code</param>
|
|
|
|
|
/// <param name="statusRegisters">Returned status registers</param>
|
|
|
|
|
/// <param name="timeout">Timeout to wait for command execution</param>
|
|
|
|
|
/// <param name="duration">Time the device took to execute the command in milliseconds</param>
|
|
|
|
|
/// <returns><c>true</c> if the device set an error condition, <c>false</c> otherwise</returns>
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool RequestExtendedErrorCode(out byte errorCode, out AtaErrorRegistersLba28 statusRegisters,
|
2020-02-29 18:03:35 +00:00
|
|
|
uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2021-08-17 18:21:12 +01:00
|
|
|
byte[] buffer = Array.Empty<byte>();
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
var registers = new AtaRegistersLba28
|
|
|
|
|
{
|
|
|
|
|
Command = (byte)AtaCommands.RequestSense
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn,
|
|
|
|
|
AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
errorCode = statusRegisters.Error;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
AaruConsole.DebugWriteLine("ATA Device", "CFA REQUEST EXTENDED ERROR CODE took {0} ms.", duration);
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|