2017-05-30 02:00:55 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : ReaderATA.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-05-30 02:00:55 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-05-30 02:00:55 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Contains common code for reading ATA devices.
|
2017-05-30 02:00:55 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-05-30 02:00:55 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2017-05-30 02:00:55 +01:00
|
|
|
|
using System;
|
2018-11-27 00:09:53 +00:00
|
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
|
using DiscImageChef.Decoders.ATA;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Core.Devices
|
|
|
|
|
|
{
|
2017-12-20 02:08:37 +00:00
|
|
|
|
partial class Reader
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-23 17:41:23 +00:00
|
|
|
|
Identify.IdentifyDevice ataId;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
bool ataRead;
|
|
|
|
|
|
bool ataReadDma;
|
|
|
|
|
|
bool ataReadDmaLba;
|
|
|
|
|
|
bool ataReadDmaLba48;
|
|
|
|
|
|
bool ataReadDmaRetry;
|
|
|
|
|
|
bool ataReadDmaRetryLba;
|
|
|
|
|
|
bool ataReadLba;
|
|
|
|
|
|
bool ataReadLba48;
|
|
|
|
|
|
bool ataReadRetry;
|
|
|
|
|
|
bool ataReadRetryLba;
|
|
|
|
|
|
bool ataSeek;
|
|
|
|
|
|
bool ataSeekLba;
|
|
|
|
|
|
|
|
|
|
|
|
internal bool IsLba { get; private set; }
|
2017-12-21 07:19:46 +00:00
|
|
|
|
internal ushort Cylinders { get; private set; }
|
2018-06-22 08:08:38 +01:00
|
|
|
|
internal byte Heads { get; private set; }
|
|
|
|
|
|
internal byte Sectors { get; private set; }
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
(uint, byte, byte) GetDeviceChs()
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(dev.Type != DeviceType.ATA) return (0, 0, 0);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
if(ataId.CurrentCylinders > 0 && ataId.CurrentHeads > 0 && ataId.CurrentSectorsPerTrack > 0)
|
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
Cylinders = ataId.CurrentCylinders;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Heads = (byte)ataId.CurrentHeads;
|
|
|
|
|
|
Sectors = (byte)ataId.CurrentSectorsPerTrack;
|
|
|
|
|
|
Blocks = (ulong)(Cylinders * Heads * Sectors);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-23 17:41:23 +00:00
|
|
|
|
if(ataId.CurrentCylinders != 0 && ataId.CurrentHeads != 0 && ataId.CurrentSectorsPerTrack != 0 ||
|
2018-06-22 08:08:38 +01:00
|
|
|
|
ataId.Cylinders <= 0 ||
|
|
|
|
|
|
ataId.Heads <= 0 ||
|
2017-12-21 07:19:46 +00:00
|
|
|
|
ataId.SectorsPerTrack <= 0) return (Cylinders, Heads, Sectors);
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
Cylinders = ataId.Cylinders;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Heads = (byte)ataId.Heads;
|
|
|
|
|
|
Sectors = (byte)ataId.SectorsPerTrack;
|
|
|
|
|
|
Blocks = (ulong)(Cylinders * Heads * Sectors);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
return (Cylinders, Heads, Sectors);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ulong AtaGetBlocks()
|
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
GetDeviceChs();
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
if(ataId.Capabilities.HasFlag(Identify.CapabilitiesBit.LBASupport))
|
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
Blocks = ataId.LBASectors;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
IsLba = true;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(!ataId.CommandSet2.HasFlag(Identify.CommandSetBit2.LBA48)) return Blocks;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
Blocks = ataId.LBA48Sectors;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
IsLba = true;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
return Blocks;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AtaFindReadCommand()
|
|
|
|
|
|
{
|
2017-12-23 17:41:23 +00:00
|
|
|
|
bool sense = dev.Read(out byte[] cmdBuf, out AtaErrorRegistersChs errorChs, false, 0, 0, 1, 1, timeout,
|
|
|
|
|
|
out _);
|
2018-06-22 08:08:38 +01:00
|
|
|
|
ataRead = !sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.Read(out cmdBuf, out errorChs, true, 0, 0, 1, 1, timeout, out _);
|
|
|
|
|
|
ataReadRetry = !sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorChs, false, 0, 0, 1, 1, timeout, out _);
|
|
|
|
|
|
ataReadDma = !sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorChs, true, 0, 0, 1, 1, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
ataReadDmaRetry = !sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && cmdBuf.Length > 0;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out cmdBuf, out AtaErrorRegistersLba28 errorLba, false, 0, 1, timeout, out _);
|
|
|
|
|
|
ataReadLba = !sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.Read(out cmdBuf, out errorLba, true, 0, 1, timeout, out _);
|
|
|
|
|
|
ataReadRetryLba = !sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorLba, false, 0, 1, timeout, out _);
|
|
|
|
|
|
ataReadDmaLba = !sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorLba, true, 0, 1, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
ataReadDmaRetryLba = !sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out cmdBuf, out AtaErrorRegistersLba48 errorLba48, 0, 1, timeout, out _);
|
|
|
|
|
|
ataReadLba48 = !sense && (errorLba48.Status & 0x27) == 0 && errorLba48.Error == 0 && cmdBuf.Length > 0;
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorLba48, 0, 1, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
ataReadDmaLba48 = !sense && (errorLba48.Status & 0x27) == 0 && errorLba48.Error == 0 && cmdBuf.Length > 0;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Seek(out errorChs, 0, 0, 1, timeout, out _);
|
|
|
|
|
|
ataSeek = !sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0;
|
|
|
|
|
|
sense = dev.Seek(out errorLba, 0, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
ataSeekLba = !sense && (errorLba.Status & 0x27) == 0 && errorChs.Error == 0;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(IsLba)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(Blocks > 0xFFFFFFF && !ataReadLba48 && !ataReadDmaLba48)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
ErrorMessage = "Device needs 48-bit LBA commands but I can't issue them... Aborting.";
|
2017-05-30 02:00:55 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!ataReadLba && !ataReadRetryLba && !ataReadDmaLba && !ataReadDmaRetryLba)
|
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
ErrorMessage = "Device needs 28-bit LBA commands but I can't issue them... Aborting.";
|
2017-05-30 02:00:55 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!ataRead && !ataReadRetry && !ataReadDma && !ataReadDmaRetry)
|
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
ErrorMessage = "Device needs CHS commands but I can't issue them... Aborting.";
|
2017-05-30 02:00:55 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(ataReadDmaLba48) DicConsole.WriteLine("Using ATA READ DMA EXT command.");
|
|
|
|
|
|
else if(ataReadLba48) DicConsole.WriteLine("Using ATA READ EXT command.");
|
|
|
|
|
|
else if(ataReadDmaRetryLba) DicConsole.WriteLine("Using ATA READ DMA command with retries (LBA).");
|
|
|
|
|
|
else if(ataReadDmaLba) DicConsole.WriteLine("Using ATA READ DMA command (LBA).");
|
|
|
|
|
|
else if(ataReadRetryLba) DicConsole.WriteLine("Using ATA READ command with retries (LBA).");
|
|
|
|
|
|
else if(ataReadLba) DicConsole.WriteLine("Using ATA READ command (LBA).");
|
|
|
|
|
|
else if(ataReadDmaRetry) DicConsole.WriteLine("Using ATA READ DMA command with retries (CHS).");
|
|
|
|
|
|
else if(ataReadDma) DicConsole.WriteLine("Using ATA READ DMA command (CHS).");
|
|
|
|
|
|
else if(ataReadRetry) DicConsole.WriteLine("Using ATA READ command with retries (CHS).");
|
|
|
|
|
|
else if(ataRead) DicConsole.WriteLine("Using ATA READ command (CHS).");
|
2017-06-03 01:25:13 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
ErrorMessage = "Could not get a working read command!";
|
2017-06-03 01:25:13 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AtaGetBlockSize()
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if((ataId.PhysLogSectorSize & 0x8000) == 0x0000 && (ataId.PhysLogSectorSize & 0x4000) == 0x4000)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
if((ataId.PhysLogSectorSize & 0x1000) == 0x1000)
|
2018-06-22 08:08:38 +01:00
|
|
|
|
if(ataId.LogicalSectorWords <= 255 || ataId.LogicalAlignment == 0xFFFF)
|
|
|
|
|
|
LogicalBlockSize = 512;
|
|
|
|
|
|
else
|
|
|
|
|
|
LogicalBlockSize = ataId.LogicalSectorWords * 2;
|
2017-12-21 07:19:46 +00:00
|
|
|
|
else LogicalBlockSize = 512;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
if((ataId.PhysLogSectorSize & 0x2000) == 0x2000)
|
2018-06-22 08:08:38 +01:00
|
|
|
|
PhysicalBlockSize = LogicalBlockSize * (uint)Math.Pow(2, ataId.PhysLogSectorSize & 0xF);
|
2017-12-21 07:19:46 +00:00
|
|
|
|
else PhysicalBlockSize = LogicalBlockSize;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
LogicalBlockSize = 512;
|
2017-12-21 07:19:46 +00:00
|
|
|
|
PhysicalBlockSize = 512;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: ATA READ LONG
|
2017-12-21 07:19:46 +00:00
|
|
|
|
LongBlockSize = 0;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AtaGetBlocksToRead(uint startWithBlocks)
|
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
BlocksToRead = startWithBlocks;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(!IsLba)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
|
BlocksToRead = 1;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool error = true;
|
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
while(IsLba)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
byte[] cmdBuf;
|
|
|
|
|
|
bool sense;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
AtaErrorRegistersLba48 errorLba48;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
if(ataReadDmaLba48)
|
|
|
|
|
|
{
|
2017-12-21 23:00:30 +00:00
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorLba48, 0, (byte)BlocksToRead, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
error = !(!sense && (errorLba48.Status & 0x27) == 0 && errorLba48.Error == 0 && cmdBuf.Length > 0);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadLba48)
|
|
|
|
|
|
{
|
2017-12-21 23:00:30 +00:00
|
|
|
|
sense = dev.Read(out cmdBuf, out errorLba48, 0, (byte)BlocksToRead, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
error = !(!sense && (errorLba48.Status & 0x27) == 0 && errorLba48.Error == 0 && cmdBuf.Length > 0);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
2017-12-21 23:00:30 +00:00
|
|
|
|
else
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
|
AtaErrorRegistersLba28 errorLba;
|
2017-12-21 23:00:30 +00:00
|
|
|
|
if(ataReadDmaRetryLba)
|
|
|
|
|
|
{
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorLba, true, 0, (byte)BlocksToRead, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadDmaLba)
|
|
|
|
|
|
{
|
|
|
|
|
|
sense = dev.ReadDma(out cmdBuf, out errorLba, false, 0, (byte)BlocksToRead, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadRetryLba)
|
|
|
|
|
|
{
|
|
|
|
|
|
sense = dev.Read(out cmdBuf, out errorLba, true, 0, (byte)BlocksToRead, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadLba)
|
|
|
|
|
|
{
|
|
|
|
|
|
sense = dev.Read(out cmdBuf, out errorLba, false, 0, (byte)BlocksToRead, timeout, out _);
|
2017-12-22 02:04:18 +00:00
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && cmdBuf.Length > 0);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
}
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(error) BlocksToRead /= 2;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(!error || BlocksToRead == 1) break;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
if(!error || !IsLba) return false;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
|
BlocksToRead = 1;
|
2017-12-21 17:58:51 +00:00
|
|
|
|
ErrorMessage = $"Device error {dev.LastError} trying to guess ideal transfer length.";
|
2017-12-21 06:06:19 +00:00
|
|
|
|
return true;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AtaReadBlocks(out byte[] buffer, ulong block, uint count, out double duration)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
bool error = true;
|
|
|
|
|
|
bool sense;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
AtaErrorRegistersLba28 errorLba;
|
|
|
|
|
|
AtaErrorRegistersLba48 errorLba48;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
byte status = 0, errorByte = 0;
|
|
|
|
|
|
buffer = null;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
duration = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if(ataReadDmaLba48)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.ReadDma(out buffer, out errorLba48, block, (byte)count, timeout, out duration);
|
|
|
|
|
|
error = !(!sense && (errorLba48.Status & 0x27) == 0 && errorLba48.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorLba48.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorLba48.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadLba48)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out buffer, out errorLba48, block, (byte)count, timeout, out duration);
|
|
|
|
|
|
error = !(!sense && (errorLba48.Status & 0x27) == 0 && errorLba48.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorLba48.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorLba48.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadDmaRetryLba)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.ReadDma(out buffer, out errorLba, true, (uint)block, (byte)count, timeout,
|
|
|
|
|
|
out duration);
|
|
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorLba.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorLba.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadDmaLba)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.ReadDma(out buffer, out errorLba, false, (uint)block, (byte)count, timeout,
|
|
|
|
|
|
out duration);
|
|
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorLba.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorLba.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadRetryLba)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out buffer, out errorLba, true, (uint)block, (byte)count, timeout, out duration);
|
|
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorLba.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorLba.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadLba)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out buffer, out errorLba, false, (uint)block, (byte)count, timeout, out duration);
|
|
|
|
|
|
error = !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorLba.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorLba.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(error) DicConsole.DebugWriteLine("ATA Reader", "ATA ERROR: {0} STATUS: {1}", errorByte, status);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
bool AtaReadChs(out byte[] buffer, ushort cylinder, byte head, byte sectir, out double duration)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
bool error = true;
|
|
|
|
|
|
bool sense;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
AtaErrorRegistersChs errorChs;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
byte status = 0, errorByte = 0;
|
|
|
|
|
|
buffer = null;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
duration = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if(ataReadDmaRetry)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.ReadDma(out buffer, out errorChs, true, cylinder, head, sectir, 1, timeout,
|
|
|
|
|
|
out duration);
|
|
|
|
|
|
error = !(!sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorChs.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorChs.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadDma)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.ReadDma(out buffer, out errorChs, false, cylinder, head, sectir, 1, timeout,
|
|
|
|
|
|
out duration);
|
|
|
|
|
|
error = !(!sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorChs.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorChs.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataReadRetry)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out buffer, out errorChs, true, cylinder, head, sectir, 1, timeout, out duration);
|
|
|
|
|
|
error = !(!sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorChs.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorChs.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if(ataRead)
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
sense = dev.Read(out buffer, out errorChs, false, cylinder, head, sectir, 1, timeout, out duration);
|
|
|
|
|
|
error = !(!sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0 && buffer.Length > 0);
|
|
|
|
|
|
status = errorChs.Status;
|
2017-12-22 02:04:18 +00:00
|
|
|
|
errorByte = errorChs.Error;
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(error) DicConsole.DebugWriteLine("ATA Reader", "ATA ERROR: {0} STATUS: {1}", errorByte, status);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AtaSeek(ulong block, out double duration)
|
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
|
bool sense = dev.Seek(out AtaErrorRegistersLba28 errorLba, (uint)block, timeout, out duration);
|
|
|
|
|
|
return !(!sense && (errorLba.Status & 0x27) == 0 && errorLba.Error == 0);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
bool AtaSeekChs(ushort cylinder, byte head, byte sector, out double duration)
|
2017-05-30 02:00:55 +01:00
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
|
bool sense = dev.Seek(out AtaErrorRegistersChs errorChs, cylinder, head, sector, timeout, out duration);
|
|
|
|
|
|
return !(!sense && (errorChs.Status & 0x27) == 0 && errorChs.Error == 0);
|
2017-05-30 02:00:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|