2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-07-28 18:13:49 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Ata48.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : ATA commands.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Contains 48-bit LBA ATA 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2016-02-07 20:11:41 +00:00
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
using DiscImageChef.Decoders.ATA;
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Devices
|
|
|
|
|
{
|
|
|
|
|
public partial class Device
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool GetNativeMaxAddressExt(out ulong lba, out AtaErrorRegistersLba48 statusRegisters, uint timeout,
|
2017-12-19 20:33:03 +00:00
|
|
|
out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
|
|
|
|
lba = 0;
|
|
|
|
|
byte[] buffer = new byte[0];
|
2017-12-22 03:13:43 +00:00
|
|
|
AtaRegistersLba48 registers =
|
|
|
|
|
new AtaRegistersLba48 {Command = (byte)AtaCommands.NativeMaxAddress, Feature = 0x0000};
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData,
|
2017-12-19 20:33:03 +00:00
|
|
|
AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
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
|
|
|
if((statusRegisters.Status & 0x23) == 0)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
lba = statusRegisters.LbaHigh;
|
2016-02-08 00:13:49 +00:00
|
|
|
lba *= 0x100000000;
|
2017-12-22 02:04:18 +00:00
|
|
|
lba += (ulong)(statusRegisters.LbaMid << 16);
|
|
|
|
|
lba += statusRegisters.LbaLow;
|
2016-02-08 00:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "GET NATIVE MAX ADDRESS EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool ReadDma(out byte[] buffer, out AtaErrorRegistersLba48 statusRegisters, ulong lba, ushort count,
|
2017-12-19 20:33:03 +00:00
|
|
|
uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
buffer = count == 0 ? new byte[512 * 65536] : new byte[512 * count];
|
|
|
|
|
AtaRegistersLba48 registers = new AtaRegistersLba48
|
|
|
|
|
{
|
|
|
|
|
Command = (byte)AtaCommands.ReadDmaExt,
|
|
|
|
|
SectorCount = count,
|
|
|
|
|
LbaHigh = (ushort)((lba & 0xFFFF00000000) / 0x100000000),
|
|
|
|
|
LbaMid = (ushort)((lba & 0xFFFF0000) / 0x10000),
|
|
|
|
|
LbaLow = (ushort)((lba & 0xFFFF) / 0x1)
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.DeviceHead += 0x40;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.Dma, AtaTransferRegister.SectorCount,
|
2017-12-22 03:13:43 +00:00
|
|
|
ref buffer, timeout, true, out duration, out bool sense);
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "READ DMA EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool ReadLog(out byte[] buffer, out AtaErrorRegistersLba48 statusRegisters, byte logAddress,
|
2017-12-19 20:33:03 +00:00
|
|
|
ushort pageNumber, ushort count, uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
|
|
|
|
buffer = new byte[512 * count];
|
2017-12-22 03:13:43 +00:00
|
|
|
AtaRegistersLba48 registers = new AtaRegistersLba48
|
|
|
|
|
{
|
|
|
|
|
Command = (byte)AtaCommands.ReadLogExt,
|
|
|
|
|
SectorCount = count,
|
|
|
|
|
LbaLow = (ushort)((pageNumber & 0xFF) * 0x100),
|
|
|
|
|
LbaHigh = (ushort)((pageNumber & 0xFF00) / 0x100)
|
|
|
|
|
};
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.LbaLow += logAddress;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn,
|
2017-12-19 20:33:03 +00:00
|
|
|
AtaTransferRegister.SectorCount, ref buffer, timeout, true, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "READ LOG EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool ReadLogDma(out byte[] buffer, out AtaErrorRegistersLba48 statusRegisters, byte logAddress,
|
2017-12-19 20:33:03 +00:00
|
|
|
ushort pageNumber, ushort count, uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
|
|
|
|
buffer = new byte[512 * count];
|
2017-12-22 03:13:43 +00:00
|
|
|
AtaRegistersLba48 registers = new AtaRegistersLba48
|
|
|
|
|
{
|
|
|
|
|
Command = (byte)AtaCommands.ReadLogDmaExt,
|
|
|
|
|
SectorCount = count,
|
|
|
|
|
LbaLow = (ushort)((pageNumber & 0xFF) * 0x100),
|
|
|
|
|
LbaHigh = (ushort)((pageNumber & 0xFF00) / 0x100)
|
|
|
|
|
};
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.LbaLow += logAddress;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.Dma, AtaTransferRegister.SectorCount,
|
2017-12-22 03:13:43 +00:00
|
|
|
ref buffer, timeout, true, out duration, out bool sense);
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "READ LOG DMA EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool ReadMultiple(out byte[] buffer, out AtaErrorRegistersLba48 statusRegisters, ulong lba, ushort count,
|
2017-12-19 20:33:03 +00:00
|
|
|
uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
buffer = count == 0 ? new byte[512 * 65536] : new byte[512 * count];
|
|
|
|
|
AtaRegistersLba48 registers = new AtaRegistersLba48
|
|
|
|
|
{
|
|
|
|
|
Command = (byte)AtaCommands.ReadMultipleExt,
|
|
|
|
|
SectorCount = count,
|
|
|
|
|
LbaHigh = (ushort)((lba & 0xFFFF00000000) / 0x100000000),
|
|
|
|
|
LbaMid = (ushort)((lba & 0xFFFF0000) / 0x10000),
|
|
|
|
|
LbaLow = (ushort)((lba & 0xFFFF) / 0x1)
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.DeviceHead += 0x40;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn,
|
2017-12-19 20:33:03 +00:00
|
|
|
AtaTransferRegister.SectorCount, ref buffer, timeout, true, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "READ MULTIPLE EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool ReadNativeMaxAddress(out ulong lba, out AtaErrorRegistersLba48 statusRegisters, uint timeout,
|
2017-12-19 20:33:03 +00:00
|
|
|
out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
|
|
|
|
lba = 0;
|
|
|
|
|
byte[] buffer = new byte[0];
|
2017-12-22 03:13:43 +00:00
|
|
|
AtaRegistersLba48 registers = new AtaRegistersLba48 {Command = (byte)AtaCommands.ReadNativeMaxAddressExt};
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.DeviceHead += 0x40;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData,
|
2017-12-19 20:33:03 +00:00
|
|
|
AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
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
|
|
|
if((statusRegisters.Status & 0x23) == 0)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
lba = statusRegisters.LbaHigh;
|
2016-02-08 00:13:49 +00:00
|
|
|
lba *= 0x100000000;
|
2017-12-22 02:04:18 +00:00
|
|
|
lba += (ulong)(statusRegisters.LbaMid << 16);
|
|
|
|
|
lba += statusRegisters.LbaLow;
|
2016-02-08 00:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "READ NATIVE MAX ADDRESS EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
public bool Read(out byte[] buffer, out AtaErrorRegistersLba48 statusRegisters, ulong lba, ushort count,
|
2017-12-19 20:33:03 +00:00
|
|
|
uint timeout, out double duration)
|
2016-02-08 00:13:49 +00:00
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
buffer = count == 0 ? new byte[512 * 65536] : new byte[512 * count];
|
|
|
|
|
AtaRegistersLba48 registers = new AtaRegistersLba48
|
|
|
|
|
{
|
|
|
|
|
Command = (byte)AtaCommands.ReadExt,
|
|
|
|
|
SectorCount = count,
|
|
|
|
|
LbaHigh = (ushort)((lba & 0xFFFF00000000) / 0x100000000),
|
|
|
|
|
LbaMid = (ushort)((lba & 0xFFFF0000) / 0x10000),
|
|
|
|
|
LbaLow = (ushort)((lba & 0xFFFF) / 0x1)
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
registers.DeviceHead += 0x40;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn,
|
2017-12-19 20:33:03 +00:00
|
|
|
AtaTransferRegister.SectorCount, ref buffer, timeout, true, out duration,
|
2017-12-22 03:13:43 +00:00
|
|
|
out bool sense);
|
2017-12-21 07:19:46 +00:00
|
|
|
Error = LastError != 0;
|
2016-02-08 00:13:49 +00:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("ATA Device", "READ SECTORS EXT took {0} ms.", duration);
|
|
|
|
|
|
|
|
|
|
return sense;
|
|
|
|
|
}
|
2016-02-07 20:11:41 +00:00
|
|
|
}
|
|
|
|
|
}
|