Files
Aaru/Aaru.Core/Devices/Scanning/ATA.cs

401 lines
16 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ATA.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Scans media from ATA 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/>.
//
// ----------------------------------------------------------------------------
2024-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
using System;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Structs.Devices.ATA;
2020-02-29 18:03:35 +00:00
using Aaru.Core.Logging;
2023-09-26 02:40:11 +01:00
using Humanizer;
using Humanizer.Bytes;
namespace Aaru.Core.Devices.Scanning;
2022-03-06 13:29:38 +00:00
/// <summary>Implements scanning the media from an ATA device</summary>
public sealed partial class MediaScan
{
2022-03-06 13:29:38 +00:00
/// <summary>Scans the media from an ATA device</summary>
/// <returns>Scanning results</returns>
ScanResults Ata()
{
2022-03-06 13:29:38 +00:00
var results = new ScanResults
{
2022-03-06 13:29:38 +00:00
Blocks = 0
};
const ushort ataProfile = 0x0001;
const uint timeout = 5;
2020-07-22 13:20:25 +01:00
2022-03-06 13:29:38 +00:00
bool sense = _dev.AtaIdentify(out byte[] cmdBuf, out _);
if(!sense && Identify.Decode(cmdBuf).HasValue)
2022-03-06 13:29:38 +00:00
{
// Initialize reader
var ataReader = new Reader(_dev, timeout, cmdBuf, null);
2022-03-06 13:29:38 +00:00
// Fill reader blocks
results.Blocks = ataReader.GetDeviceBlocks();
2022-03-06 13:29:38 +00:00
if(ataReader.FindReadCommand())
{
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke(ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
return results;
}
2022-03-06 13:29:38 +00:00
// Check block sizes
if(ataReader.GetBlockSize())
{
StoppingErrorMessage?.Invoke(ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
return results;
}
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
uint blockSize = ataReader.LogicalBlockSize;
2022-03-06 13:29:38 +00:00
// Check how many blocks to read, if error show and return
if(ataReader.GetBlocksToRead())
{
StoppingErrorMessage?.Invoke(ataReader.ErrorMessage);
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
return results;
}
2022-03-06 13:29:38 +00:00
uint blocksToRead = ataReader.BlocksToRead;
ushort cylinders = ataReader.Cylinders;
byte heads = ataReader.Heads;
byte sectors = ataReader.Sectors;
2023-10-03 22:57:50 +01:00
results.A = 0; // <3ms
results.B = 0; // >=3ms, <10ms
results.C = 0; // >=10ms, <50ms
results.D = 0; // >=50ms, <150ms
results.E = 0; // >=150ms, <500ms
results.F = 0; // >=500ms
results.Errored = 0;
2022-03-06 13:29:38 +00:00
results.ProcessingTime = 0;
double currentSpeed = 0;
results.MaxSpeed = double.MinValue;
results.MinSpeed = double.MaxValue;
2024-05-01 04:39:38 +01:00
results.UnreadableSectors = [];
2022-03-06 13:29:38 +00:00
results.SeekMax = double.MinValue;
results.SeekMin = double.MaxValue;
results.SeekTotal = 0;
const int seekTimes = 1000;
double seekCur;
var rnd = new Random();
MhddLog mhddLog;
IbgLog ibgLog;
double duration;
if(ataReader.IsLba)
{
UpdateStatus?.Invoke(string.Format(Localization.Core.Reading_0_sectors_at_a_time, blocksToRead));
2022-03-06 13:29:38 +00:00
InitBlockMap?.Invoke(results.Blocks, blockSize, blocksToRead, ataProfile);
mhddLog = new MhddLog(_mhddLogPath, _dev, results.Blocks, blockSize, blocksToRead, false);
ibgLog = new IbgLog(_ibgLogPath, ataProfile);
2017-12-19 20:33:03 +00:00
_scanStopwatch.Restart();
_speedStopwatch.Restart();
2023-10-03 22:57:50 +01:00
ulong sectorSpeedStart = 0;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
for(ulong i = 0; i < results.Blocks; i += blocksToRead)
{
2024-05-01 04:05:22 +01:00
if(_aborted) break;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(results.Blocks - i < blocksToRead) blocksToRead = (byte)(results.Blocks - i);
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(currentSpeed > results.MaxSpeed && currentSpeed > 0) results.MaxSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
if(currentSpeed < results.MinSpeed && currentSpeed > 0) results.MinSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
UpdateProgress?.Invoke(string.Format(Localization.Core.Reading_sector_0_of_1_2,
i,
results.Blocks,
ByteSize.FromMegabytes(currentSpeed)
.Per(_oneSecond)
.Humanize()),
(long)i,
(long)results.Blocks);
2022-03-06 13:29:38 +00:00
bool error = ataReader.ReadBlocks(out cmdBuf, i, blocksToRead, out duration, out _, out _);
if(!error)
{
2022-11-13 19:38:03 +00:00
switch(duration)
{
case >= 500:
results.F += blocksToRead;
break;
case >= 150:
results.E += blocksToRead;
break;
case >= 50:
results.D += blocksToRead;
break;
case >= 10:
results.C += blocksToRead;
break;
case >= 3:
results.B += blocksToRead;
break;
default:
results.A += blocksToRead;
break;
}
2022-03-06 13:29:38 +00:00
ScanTime?.Invoke(i, duration);
mhddLog.Write(i, duration);
ibgLog.Write(i, currentSpeed * 1024);
}
else
{
ScanUnreadable?.Invoke(i);
results.Errored += blocksToRead;
2024-05-01 04:05:22 +01:00
for(ulong b = i; b < i + blocksToRead; b++) results.UnreadableSectors.Add(b);
2022-03-06 13:29:38 +00:00
mhddLog.Write(i, duration < 500 ? 65535 : duration);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, 0);
}
2022-03-06 13:29:38 +00:00
sectorSpeedStart += blocksToRead;
double elapsed = _speedStopwatch.Elapsed.TotalSeconds;
2024-05-01 04:05:22 +01:00
if(elapsed <= 0) continue;
2022-03-06 13:29:38 +00:00
currentSpeed = sectorSpeedStart * blockSize / (1048576 * elapsed);
ScanSpeed?.Invoke(i, currentSpeed * 1024);
sectorSpeedStart = 0;
_speedStopwatch.Restart();
2022-03-06 13:29:38 +00:00
}
2017-12-19 20:33:03 +00:00
_speedStopwatch.Stop();
_scanStopwatch.Stop();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
mhddLog.Close();
2024-05-01 04:05:22 +01:00
ibgLog.Close(_dev,
results.Blocks,
blockSize,
_scanStopwatch.Elapsed.TotalSeconds,
currentSpeed * 1024,
2022-03-06 13:29:38 +00:00
blockSize * (double)(results.Blocks + 1) / 1024 / (results.ProcessingTime / 1000),
_devicePath);
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2022-03-06 13:29:38 +00:00
if(ataReader.CanSeekLba && _seekTest)
2023-10-03 22:57:50 +01:00
{
for(var i = 0; i < seekTimes; i++)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01:00
if(_aborted) break;
2023-10-03 22:57:50 +01:00
var seekPos = (uint)rnd.Next((int)results.Blocks);
PulseProgress?.Invoke(string.Format(Localization.Core.Seeking_to_sector_0, seekPos));
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
ataReader.Seek(seekPos, out seekCur);
2024-05-01 04:05:22 +01:00
if(seekCur > results.SeekMax && seekCur > 0) results.SeekMax = seekCur;
2024-05-01 04:05:22 +01:00
if(seekCur < results.SeekMin && seekCur > 0) results.SeekMin = seekCur;
2022-03-06 13:29:38 +00:00
results.SeekTotal += seekCur;
GC.Collect();
}
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
}
else
{
InitBlockMap?.Invoke(results.Blocks, blockSize, blocksToRead, ataProfile);
mhddLog = new MhddLog(_mhddLogPath, _dev, results.Blocks, blockSize, blocksToRead, false);
ibgLog = new IbgLog(_ibgLogPath, ataProfile);
ulong currentBlock = 0;
results.Blocks = (ulong)(cylinders * heads * sectors);
_scanStopwatch.Restart();
_speedStopwatch.Restart();
2023-10-03 22:57:50 +01:00
ulong sectorSpeedStart = 0;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
for(ushort cy = 0; cy < cylinders; cy++)
{
for(byte hd = 0; hd < heads; hd++)
{
for(byte sc = 1; sc < sectors; sc++)
{
2024-05-01 04:05:22 +01:00
if(_aborted) break;
2024-05-01 04:05:22 +01:00
if(currentSpeed > results.MaxSpeed && currentSpeed > 0) results.MaxSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
if(currentSpeed < results.MinSpeed && currentSpeed > 0) results.MinSpeed = currentSpeed;
2023-09-26 02:40:11 +01:00
PulseProgress?.Invoke(string.Format(Localization.Core.Reading_cylinder_0_head_1_sector_2_3,
2024-05-01 04:05:22 +01:00
cy,
hd,
sc,
ByteSize.FromMegabytes(currentSpeed)
.Per(_oneSecond)
.Humanize()));
2022-03-06 13:29:38 +00:00
bool error = ataReader.ReadChs(out cmdBuf, cy, hd, sc, out duration, out _);
2022-03-06 13:29:38 +00:00
if(!error)
{
2022-11-13 19:38:03 +00:00
switch(duration)
{
case >= 500:
results.F += blocksToRead;
break;
case >= 150:
results.E += blocksToRead;
break;
case >= 50:
results.D += blocksToRead;
break;
case >= 10:
results.C += blocksToRead;
break;
case >= 3:
results.B += blocksToRead;
break;
default:
results.A += blocksToRead;
break;
}
2022-03-06 13:29:38 +00:00
ScanTime?.Invoke(currentBlock, duration);
mhddLog.Write(currentBlock, duration);
ibgLog.Write(currentBlock, currentSpeed * 1024);
}
else
{
ScanUnreadable?.Invoke(currentBlock);
results.Errored += blocksToRead;
results.UnreadableSectors.Add(currentBlock);
mhddLog.Write(currentBlock, duration < 500 ? 65535 : duration);
2022-03-06 13:29:38 +00:00
ibgLog.Write(currentBlock, 0);
}
2022-03-06 13:29:38 +00:00
sectorSpeedStart++;
currentBlock++;
double elapsed = _speedStopwatch.Elapsed.TotalSeconds;
2024-05-01 04:05:22 +01:00
if(elapsed <= 0) continue;
2022-03-06 13:29:38 +00:00
currentSpeed = sectorSpeedStart * blockSize / (1048576 * elapsed);
ScanSpeed?.Invoke(currentBlock, currentSpeed * 1024);
sectorSpeedStart = 0;
_speedStopwatch.Restart();
}
}
2022-03-06 13:29:38 +00:00
}
2017-12-19 20:33:03 +00:00
_speedStopwatch.Stop();
_scanStopwatch.Stop();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
mhddLog.Close();
2024-05-01 04:05:22 +01:00
ibgLog.Close(_dev,
results.Blocks,
blockSize,
_scanStopwatch.Elapsed.TotalSeconds,
currentSpeed * 1024,
2022-03-06 13:29:38 +00:00
blockSize * (double)(results.Blocks + 1) / 1024 / (results.ProcessingTime / 1000),
_devicePath);
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2022-03-06 13:29:38 +00:00
if(ataReader.CanSeek)
2023-10-03 22:57:50 +01:00
{
for(var i = 0; i < seekTimes; i++)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01:00
if(_aborted) break;
2023-10-03 22:57:50 +01:00
var seekCy = (ushort)rnd.Next(cylinders);
var seekHd = (byte)rnd.Next(heads);
var seekSc = (byte)rnd.Next(sectors);
PulseProgress?.Invoke(string.Format(Localization.Core.Seeking_to_cylinder_0_head_1_sector_2,
2024-05-01 04:05:22 +01:00
seekCy,
seekHd,
seekSc));
2022-03-06 13:29:38 +00:00
ataReader.SeekChs(seekCy, seekHd, seekSc, out seekCur);
2024-05-01 04:05:22 +01:00
if(seekCur > results.SeekMax && seekCur > 0) results.SeekMax = seekCur;
2024-05-01 04:05:22 +01:00
if(seekCur < results.SeekMin && seekCur > 0) results.SeekMin = seekCur;
2022-03-06 13:29:38 +00:00
results.SeekTotal += seekCur;
GC.Collect();
}
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
}
2022-03-06 13:29:38 +00:00
results.ProcessingTime /= 1000;
results.TotalTime = _scanStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
results.AvgSpeed = blockSize * (double)(results.Blocks + 1) / 1048576 / results.ProcessingTime;
results.SeekTimes = seekTimes;
return results;
}
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke(Localization.Core.Unable_to_communicate_with_ATA_device);
2022-03-06 13:29:38 +00:00
return results;
}
2017-12-19 20:33:03 +00:00
}