Files
Aaru/Aaru.Core/Devices/ReaderSCSI.cs

601 lines
24 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ReaderSCSI.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains common code for reading SCSI devices.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs.Devices.SCSI;
using Aaru.Console;
using Aaru.Decoders.SCSI;
2020-02-27 00:33:26 +00:00
namespace Aaru.Core.Devices
{
internal partial class Reader
{
// TODO: Raw reading
bool hldtstReadRaw;
bool plextorReadRaw;
bool read10;
bool read12;
bool read16;
bool read6;
bool readLong10;
bool readLong16;
bool readLongDvd;
bool seek10;
bool seek6;
bool syqReadLong10;
bool syqReadLong6;
2018-12-31 13:17:27 +00:00
ulong ScsiGetBlocks() => ScsiGetBlockSize() ? 0 : Blocks;
bool ScsiFindReadCommand()
{
read6 = !dev.Read6(out _, out byte[] senseBuf, 0, LogicalBlockSize, timeout, out _);
read10 = !dev.Read10(out _, out senseBuf, 0, false, true, false, false, 0, LogicalBlockSize, 0, 1, timeout,
out _);
read12 = !dev.Read12(out _, out senseBuf, 0, false, true, false, false, 0, LogicalBlockSize, 0, 1, false,
timeout, out _);
read16 = !dev.Read16(out _, out senseBuf, 0, false, true, false, 0, LogicalBlockSize, 0, 1, false, timeout,
out _);
seek6 = !dev.Seek6(out senseBuf, 0, timeout, out _);
seek10 = !dev.Seek10(out senseBuf, 0, timeout, out _);
if(!read6 &&
!read10 &&
!read12 &&
!read16)
{
ErrorMessage = "Cannot read medium, aborting scan...";
return true;
}
if(read6 &&
!read10 &&
!read12 &&
!read16 &&
Blocks > 0x001FFFFF + 1)
{
ErrorMessage =
$"Device only supports SCSI READ (6) but has more than {0x001FFFFF + 1} blocks ({Blocks} blocks total)";
return true;
}
if(!read16 &&
Blocks > 0xFFFFFFFF + (long)1)
{
ErrorMessage =
$"Device only supports SCSI READ (10) but has more than {0xFFFFFFFF + (long)1} blocks ({Blocks} blocks total)";
return true;
}
if(CanReadRaw)
{
bool testSense;
CanReadRaw = false;
2017-12-21 14:30:38 +00:00
if(dev.ScsiType != PeripheralDeviceTypes.MultiMediaDevice)
{
/*testSense = dev.ReadLong16(out readBuffer, out senseBuf, false, 0, 0xFFFF, timeout, out duration);
if (testSense && !dev.Error)
{
decSense = Decoders.SCSI.Sense.DecodeFixed(senseBuf);
if (decSense.HasValue)
{
if (decSense.Value.SenseKey == Aaru.Decoders.SCSI.SenseKeys.IllegalRequest &&
decSense.Value.ASC == 0x24 && decSense.Value.ASCQ == 0x00)
{
readRaw = true;
if (decSense.Value.InformationValid && decSense.Value.ILI)
{
longBlockSize = 0xFFFF - (decSense.Value.Information & 0xFFFF);
readLong16 = !dev.ReadLong16(out readBuffer, out senseBuf, false, 0, longBlockSize, timeout, out duration);
}
}
}
}*/
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, 0xFFFF, timeout, out _);
FixedSense? decSense;
if(testSense && !dev.Error)
{
2017-12-21 14:30:38 +00:00
decSense = Sense.DecodeFixed(senseBuf);
if(decSense.HasValue)
if(decSense.Value.SenseKey == SenseKeys.IllegalRequest &&
decSense.Value.ASC == 0x24 &&
2018-06-22 08:08:38 +01:00
decSense.Value.ASCQ == 0x00)
{
CanReadRaw = true;
if(decSense.Value.InformationValid &&
decSense.Value.ILI)
{
LongBlockSize = 0xFFFF - (decSense.Value.Information & 0xFFFF);
readLong10 = !dev.ReadLong10(out _, out senseBuf, false, false, 0,
(ushort)LongBlockSize, timeout, out _);
}
}
}
if(CanReadRaw && LongBlockSize == LogicalBlockSize)
if(LogicalBlockSize == 512)
2018-06-20 22:22:21 +01:00
foreach(int i in new[]
2017-12-19 20:33:03 +00:00
{
// Long sector sizes for floppies
514,
2017-12-19 20:33:03 +00:00
// Long sector sizes for SuperDisk
536, 558,
2017-12-19 20:33:03 +00:00
// Long sector sizes for 512-byte magneto-opticals
600, 610, 630
})
{
2018-06-20 22:22:21 +01:00
ushort testSize = (ushort)i;
testSense = dev.ReadLong16(out _, out senseBuf, false, 0, testSize, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong16 = true;
LongBlockSize = testSize;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
break;
}
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, testSize, timeout,
out _);
if(testSense || dev.Error)
continue;
2018-06-22 08:08:38 +01:00
readLong10 = true;
LongBlockSize = testSize;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
break;
}
else if(LogicalBlockSize == 1024)
2018-06-20 22:22:21 +01:00
foreach(int i in new[]
2017-12-19 20:33:03 +00:00
{
// Long sector sizes for floppies
1026,
2017-12-19 20:33:03 +00:00
// Long sector sizes for 1024-byte magneto-opticals
1200
})
{
2018-06-20 22:22:21 +01:00
ushort testSize = (ushort)i;
testSense = dev.ReadLong16(out _, out senseBuf, false, 0, testSize, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong16 = true;
LongBlockSize = testSize;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
break;
}
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, testSize, timeout,
out _);
if(testSense || dev.Error)
continue;
2018-06-22 08:08:38 +01:00
readLong10 = true;
LongBlockSize = testSize;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
break;
}
else if(LogicalBlockSize == 2048)
{
testSense = dev.ReadLong16(out _, out senseBuf, false, 0, 2380, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong16 = true;
LongBlockSize = 2380;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
else
{
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, 2380, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong10 = true;
LongBlockSize = 2380;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
}
}
else if(LogicalBlockSize == 4096)
{
testSense = dev.ReadLong16(out _, out senseBuf, false, 0, 4760, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong16 = true;
LongBlockSize = 4760;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
else
{
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, 4760, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong10 = true;
LongBlockSize = 4760;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
}
}
else if(LogicalBlockSize == 8192)
{
testSense = dev.ReadLong16(out _, out senseBuf, false, 0, 9424, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong16 = true;
LongBlockSize = 9424;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
else
{
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, 9424, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLong10 = true;
LongBlockSize = 9424;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
}
}
if(!CanReadRaw &&
dev.Manufacturer == "SYQUEST")
{
testSense = dev.SyQuestReadLong10(out _, out senseBuf, 0, 0xFFFF, timeout, out _);
if(testSense)
{
2017-12-21 14:30:38 +00:00
decSense = Sense.DecodeFixed(senseBuf);
if(decSense.HasValue)
if(decSense.Value.SenseKey == SenseKeys.IllegalRequest &&
decSense.Value.ASC == 0x24 &&
2018-06-22 08:08:38 +01:00
decSense.Value.ASCQ == 0x00)
{
CanReadRaw = true;
if(decSense.Value.InformationValid &&
decSense.Value.ILI)
{
LongBlockSize = 0xFFFF - (decSense.Value.Information & 0xFFFF);
2017-12-19 20:33:03 +00:00
syqReadLong10 =
!dev.SyQuestReadLong10(out _, out senseBuf, 0, LongBlockSize, timeout,
out _);
}
}
else
{
testSense = dev.SyQuestReadLong6(out _, out senseBuf, 0, 0xFFFF, timeout, out _);
if(testSense)
{
2017-12-21 14:30:38 +00:00
decSense = Sense.DecodeFixed(senseBuf);
if(decSense.HasValue)
2017-12-21 14:30:38 +00:00
if(decSense.Value.SenseKey == SenseKeys.IllegalRequest &&
2018-06-22 08:08:38 +01:00
decSense.Value.ASC == 0x24 &&
decSense.Value.ASCQ == 0x00)
{
CanReadRaw = true;
if(decSense.Value.InformationValid &&
decSense.Value.ILI)
{
LongBlockSize = 0xFFFF - (decSense.Value.Information & 0xFFFF);
2017-12-19 20:33:03 +00:00
syqReadLong6 =
!dev.SyQuestReadLong6(out _, out senseBuf, 0, LongBlockSize,
timeout, out _);
}
}
}
}
}
if(!CanReadRaw &&
LogicalBlockSize == 256)
{
testSense = dev.SyQuestReadLong6(out _, out senseBuf, 0, 262, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
syqReadLong6 = true;
LongBlockSize = 262;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
}
}
}
else
{
switch(dev.Manufacturer)
{
case"HL-DT-ST":
hldtstReadRaw = !dev.HlDtStReadRawDvd(out _, out senseBuf, 0, 1, timeout, out _);
break;
case"PLEXTOR":
plextorReadRaw = !dev.PlextorReadRawDvd(out _, out senseBuf, 0, 1, timeout, out _);
break;
}
if(hldtstReadRaw || plextorReadRaw)
{
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
LongBlockSize = 2064;
}
// READ LONG (10) for some DVD drives
if(!CanReadRaw &&
dev.Manufacturer == "MATSHITA")
{
testSense = dev.ReadLong10(out _, out senseBuf, false, false, 0, 37856, timeout, out _);
if(!testSense &&
!dev.Error)
{
2018-06-22 08:08:38 +01:00
readLongDvd = true;
LongBlockSize = 37856;
2018-06-22 08:08:38 +01:00
CanReadRaw = true;
}
}
}
}
if(CanReadRaw)
{
if(readLong16)
DicConsole.WriteLine("Using SCSI READ LONG (16) command.");
else if(readLong10 || readLongDvd)
DicConsole.WriteLine("Using SCSI READ LONG (10) command.");
else if(syqReadLong10)
DicConsole.WriteLine("Using SyQuest READ LONG (10) command.");
else if(syqReadLong6)
DicConsole.WriteLine("Using SyQuest READ LONG (6) command.");
else if(hldtstReadRaw)
DicConsole.WriteLine("Using HL-DT-ST raw DVD reading.");
else if(plextorReadRaw)
DicConsole.WriteLine("Using Plextor raw DVD reading.");
}
else if(read16)
DicConsole.WriteLine("Using SCSI READ (16) command.");
else if(read12)
DicConsole.WriteLine("Using SCSI READ (12) command.");
else if(read10)
DicConsole.WriteLine("Using SCSI READ (10) command.");
else if(read6)
DicConsole.WriteLine("Using SCSI READ (6) command.");
return false;
}
bool ScsiGetBlockSize()
{
bool sense;
Blocks = 0;
sense = dev.ReadCapacity(out byte[] cmdBuf, out byte[] senseBuf, timeout, out _);
if(!sense)
{
2018-06-22 08:08:38 +01:00
Blocks = (ulong)((cmdBuf[0] << 24) + (cmdBuf[1] << 16) + (cmdBuf[2] << 8) + cmdBuf[3]);
LogicalBlockSize = (uint)((cmdBuf[5] << 24) + (cmdBuf[5] << 16) + (cmdBuf[6] << 8) + cmdBuf[7]);
}
if(sense || Blocks == 0xFFFFFFFF)
{
sense = dev.ReadCapacity16(out cmdBuf, out senseBuf, timeout, out _);
if(sense && Blocks == 0)
2017-12-21 14:30:38 +00:00
if(dev.ScsiType != PeripheralDeviceTypes.MultiMediaDevice)
{
ErrorMessage = "Unable to get media capacity\n" + $"{Sense.PrettifySense(senseBuf)}";
return true;
}
if(!sense)
{
byte[] temp = new byte[8];
Array.Copy(cmdBuf, 0, temp, 0, 8);
Array.Reverse(temp);
2018-06-22 08:08:38 +01:00
Blocks = BitConverter.ToUInt64(temp, 0);
LogicalBlockSize = (uint)((cmdBuf[5] << 24) + (cmdBuf[5] << 16) + (cmdBuf[6] << 8) + cmdBuf[7]);
}
}
PhysicalBlockSize = LogicalBlockSize;
2018-06-22 08:08:38 +01:00
LongBlockSize = LogicalBlockSize;
return false;
}
bool ScsiGetBlocksToRead(uint startWithBlocks)
{
BlocksToRead = startWithBlocks;
while(true)
{
if(read16)
{
dev.Read16(out _, out _, 0, false, true, false, 0, LogicalBlockSize, 0, BlocksToRead, false,
timeout, out _);
if(dev.Error)
BlocksToRead /= 2;
}
else if(read12)
{
dev.Read12(out _, out _, 0, false, false, false, false, 0, LogicalBlockSize, 0, BlocksToRead, false,
timeout, out _);
if(dev.Error)
BlocksToRead /= 2;
}
else if(read10)
{
dev.Read10(out _, out _, 0, false, true, false, false, 0, LogicalBlockSize, 0, (ushort)BlocksToRead,
timeout, out _);
if(dev.Error)
BlocksToRead /= 2;
}
else if(read6)
{
dev.Read6(out _, out _, 0, LogicalBlockSize, (byte)BlocksToRead, timeout, out _);
if(dev.Error)
BlocksToRead /= 2;
}
if(!dev.Error ||
BlocksToRead == 1)
break;
}
if(!dev.Error)
return false;
BlocksToRead = 1;
ErrorMessage = $"Device error {dev.LastError} trying to guess ideal transfer length.";
return true;
}
bool ScsiReadBlocks(out byte[] buffer, ulong block, uint count, out double duration)
{
2018-06-22 08:08:38 +01:00
bool sense;
byte[] senseBuf;
2018-06-22 08:08:38 +01:00
buffer = null;
duration = 0;
if(CanReadRaw)
if(readLong16)
sense = dev.ReadLong16(out buffer, out senseBuf, false, block, LongBlockSize, timeout,
2017-12-19 20:33:03 +00:00
out duration);
else if(readLong10)
sense = dev.ReadLong10(out buffer, out senseBuf, false, false, (uint)block, (ushort)LongBlockSize,
2017-12-19 20:33:03 +00:00
timeout, out duration);
else if(syqReadLong10)
sense = dev.SyQuestReadLong10(out buffer, out senseBuf, (uint)block, LongBlockSize, timeout,
2017-12-19 20:33:03 +00:00
out duration);
else if(syqReadLong6)
sense = dev.SyQuestReadLong6(out buffer, out senseBuf, (uint)block, LongBlockSize, timeout,
2017-12-19 20:33:03 +00:00
out duration);
else if(hldtstReadRaw)
sense = dev.HlDtStReadRawDvd(out buffer, out senseBuf, (uint)block, LongBlockSize, timeout,
2017-12-19 20:33:03 +00:00
out duration);
else if(plextorReadRaw)
sense = dev.PlextorReadRawDvd(out buffer, out senseBuf, (uint)block, LongBlockSize, timeout,
2017-12-19 20:33:03 +00:00
out duration);
else
return true;
else
{
if(read16)
sense = dev.Read16(out buffer, out senseBuf, 0, false, true, false, block, LogicalBlockSize, 0,
count, false, timeout, out duration);
else if(read12)
sense = dev.Read12(out buffer, out senseBuf, 0, false, false, false, false, (uint)block,
LogicalBlockSize, 0, count, false, timeout, out duration);
else if(read10)
sense = dev.Read10(out buffer, out senseBuf, 0, false, true, false, false, (uint)block,
LogicalBlockSize, 0, (ushort)count, timeout, out duration);
else if(read6)
sense = dev.Read6(out buffer, out senseBuf, (uint)block, LogicalBlockSize, (byte)count, timeout,
2017-12-19 20:33:03 +00:00
out duration);
else
return true;
}
if(!sense &&
!dev.Error)
return false;
DicConsole.DebugWriteLine("SCSI Reader", "READ error:\n{0}", Sense.PrettifySense(senseBuf));
2017-12-21 16:07:20 +00:00
return sense;
}
bool ScsiSeek(ulong block, out double duration)
{
bool sense = true;
duration = 0;
if(seek6)
sense = dev.Seek6(out _, (uint)block, timeout, out duration);
else if(seek10)
sense = dev.Seek10(out _, (uint)block, timeout, out duration);
return sense;
}
}
2017-12-19 20:33:03 +00:00
}