Files
Aaru/Aaru.Core/Devices/Reader.cs

192 lines
6.7 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Reader.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains common code for reading 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.Enums;
using Aaru.CommonTypes.Structs.Devices.ATA;
using Aaru.Devices;
2020-02-27 00:33:26 +00:00
namespace Aaru.Core.Devices
{
/// <summary>Reduces common code used for scanning and dumping</summary>
internal partial class Reader
{
readonly Device _dev;
readonly uint _timeout;
internal Reader(Device dev, uint timeout, byte[] identification, bool raw = false)
{
_dev = dev;
_timeout = timeout;
BlocksToRead = 64;
2018-06-22 08:08:38 +01:00
CanReadRaw = raw;
switch(dev.Type)
{
case DeviceType.ATA:
Identify.IdentifyDevice? ataIdNullable = Identify.Decode(identification);
if(ataIdNullable.HasValue)
_ataId = ataIdNullable.Value;
break;
2017-12-19 20:33:03 +00:00
case DeviceType.NVMe: throw new NotImplementedException("NVMe devices not yet supported.");
}
}
2018-06-22 08:08:38 +01:00
internal string ErrorMessage { get; private set; }
internal ulong Blocks { get; private set; }
internal uint BlocksToRead { get; private set; }
internal uint LogicalBlockSize { get; private set; }
internal uint PhysicalBlockSize { get; private set; }
internal uint LongBlockSize { get; private set; }
internal bool CanReadRaw { get; private set; }
internal bool CanSeek => _ataSeek || _seek6 || _seek10;
internal bool CanSeekLba => _ataSeekLba || _seek6 || _seek10;
internal ulong GetDeviceBlocks()
{
switch(_dev.Type)
{
2017-12-19 20:33:03 +00:00
case DeviceType.ATA: return AtaGetBlocks();
case DeviceType.ATAPI:
2017-12-19 20:33:03 +00:00
case DeviceType.SCSI: return ScsiGetBlocks();
default:
ErrorMessage = $"Unknown device type {_dev.Type}.";
return 0;
}
}
internal bool FindReadCommand()
{
switch(_dev.Type)
{
2017-12-19 20:33:03 +00:00
case DeviceType.ATA: return AtaFindReadCommand();
case DeviceType.ATAPI:
2017-12-19 20:33:03 +00:00
case DeviceType.SCSI: return ScsiFindReadCommand();
default:
ErrorMessage = $"Unknown device type {_dev.Type}.";
return true;
}
}
internal bool GetBlockSize()
{
switch(_dev.Type)
{
2017-12-19 20:33:03 +00:00
case DeviceType.ATA: return AtaGetBlockSize();
case DeviceType.ATAPI:
2017-12-19 20:33:03 +00:00
case DeviceType.SCSI: return ScsiGetBlockSize();
default:
ErrorMessage = $"Unknown device type {_dev.Type}.";
return true;
}
}
internal bool GetBlocksToRead(uint startWithBlocks = 64)
{
switch(_dev.Type)
{
2017-12-19 20:33:03 +00:00
case DeviceType.ATA: return AtaGetBlocksToRead(startWithBlocks);
case DeviceType.ATAPI:
2017-12-19 20:33:03 +00:00
case DeviceType.SCSI: return ScsiGetBlocksToRead(startWithBlocks);
default:
ErrorMessage = $"Unknown device type {_dev.Type}.";
return true;
}
}
2018-11-27 00:09:53 +00:00
internal bool ReadBlock(out byte[] buffer, ulong block, out double duration) =>
ReadBlocks(out buffer, block, 1, out duration);
2018-11-27 00:09:53 +00:00
internal bool ReadBlocks(out byte[] buffer, ulong block, out double duration) =>
ReadBlocks(out buffer, block, BlocksToRead, out duration);
internal bool ReadBlocks(out byte[] buffer, ulong block, uint count, out double duration)
{
switch(_dev.Type)
{
2017-12-19 20:33:03 +00:00
case DeviceType.ATA: return AtaReadBlocks(out buffer, block, count, out duration);
case DeviceType.ATAPI:
2017-12-19 20:33:03 +00:00
case DeviceType.SCSI: return ScsiReadBlocks(out buffer, block, count, out duration);
default:
2018-06-22 08:08:38 +01:00
buffer = null;
duration = 0d;
return true;
}
}
internal bool ReadChs(out byte[] buffer, ushort cylinder, byte head, byte sector, out double duration)
{
switch(_dev.Type)
{
case DeviceType.ATA: return AtaReadChs(out buffer, cylinder, head, sector, out duration);
default:
2018-06-22 08:08:38 +01:00
buffer = null;
duration = 0d;
return true;
}
}
internal bool Seek(ulong block, out double duration)
{
switch(_dev.Type)
{
2017-12-19 20:33:03 +00:00
case DeviceType.ATA: return AtaSeek(block, out duration);
case DeviceType.ATAPI:
2017-12-19 20:33:03 +00:00
case DeviceType.SCSI: return ScsiSeek(block, out duration);
default:
duration = 0d;
return true;
}
}
internal bool SeekChs(ushort cylinder, byte head, byte sector, out double duration)
{
switch(_dev.Type)
{
case DeviceType.ATA: return AtaSeekChs(cylinder, head, sector, out duration);
default:
duration = 0;
return true;
}
}
}
2017-12-19 20:33:03 +00:00
}