Files
Aaru/Aaru.Core/Devices/Dumping/Sbc/Optical.cs

284 lines
11 KiB
C#
Raw Normal View History

2022-03-07 07:36:44 +00:00
// ReSharper disable JoinDeclarationAndInitializer
// ReSharper disable InlineOutVariableDeclaration
// ReSharper disable TooWideLocalVariableScope
using System;
using System.Linq;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Extents;
2021-12-28 00:14:33 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.Core.Logging;
using Aaru.Decoders.SCSI;
using Aaru.Logging;
2023-09-26 02:40:11 +01:00
using Humanizer;
using Humanizer.Bytes;
namespace Aaru.Core.Devices.Dumping;
2022-03-06 13:29:38 +00:00
partial class Dump
{
2022-03-06 13:29:38 +00:00
/// <summary>
/// Dumps data when dumping from a SCSI Block Commands compliant device, optical variant (magneto-optical and
/// successors)
/// </summary>
/// <param name="blocks">Media blocks</param>
/// <param name="maxBlocksToRead">Maximum number of blocks to read in a single command</param>
/// <param name="blockSize">Block size in bytes</param>
/// <param name="currentTry">Resume information</param>
/// <param name="extents">Correctly dump extents</param>
/// <param name="currentSpeed">Current speed</param>
/// <param name="minSpeed">Minimum speed</param>
/// <param name="maxSpeed">Maximum speed</param>
/// <param name="totalDuration">Total time spent in commands</param>
/// <param name="scsiReader">SCSI reader</param>
/// <param name="mhddLog">MHDD log</param>
/// <param name="ibgLog">ImgBurn log</param>
/// <param name="imageWriteDuration">Total time spent writing to image</param>
/// <param name="newTrim">Set if we need to start a trim</param>
/// <param name="blankExtents">Blank extents</param>
2023-10-03 22:57:50 +01:00
void ReadOpticalData(in ulong blocks, in uint maxBlocksToRead, in uint blockSize, DumpHardware currentTry,
2022-03-06 13:29:38 +00:00
ExtentsULong extents, ref double currentSpeed, ref double minSpeed, ref double maxSpeed,
2023-10-03 22:57:50 +01:00
ref double totalDuration, Reader scsiReader, MhddLog mhddLog, IbgLog ibgLog,
ref double imageWriteDuration, ref bool newTrim, ref ExtentsULong blankExtents)
{
2022-03-06 13:29:38 +00:00
const uint maxBlocks = 256;
var writtenExtents = new ExtentsULong();
bool written;
uint c = maxBlocks;
bool conditionMet;
bool changingCounter;
bool changingWritten;
uint blocksToRead = maxBlocksToRead;
bool sense;
byte[] buffer;
ulong sectorSpeedStart = 0;
bool canMediumScan = true;
2022-03-06 13:29:38 +00:00
var outputFormat = _outputPlugin as IWritableImage;
InitProgress?.Invoke();
if(blankExtents is null)
{
2022-03-06 13:29:38 +00:00
blankExtents = new ExtentsULong();
2025-08-22 19:57:09 +01:00
written = _dev.MediumScan(out ReadOnlySpan<byte> senseBuffer,
2024-05-01 04:05:22 +01:00
true,
false,
false,
false,
false,
0,
1,
1,
out _,
out _,
uint.MaxValue,
out _);
2022-03-06 13:29:38 +00:00
2025-08-22 19:57:09 +01:00
DecodedSense? decodedSense = Sense.Decode(senseBuffer.ToArray());
2022-03-06 13:29:38 +00:00
if(_dev.LastError != 0 || decodedSense?.SenseKey == SenseKeys.IllegalRequest)
{
UpdateStatus?.Invoke(Localization.Core.The_current_environment_doesn_t_support_the_medium_scan_command);
2022-03-06 13:29:38 +00:00
canMediumScan = false;
writtenExtents.Add(0, blocks - 1);
}
2022-03-06 13:29:38 +00:00
// TODO: Find a place where MEDIUM SCAN works properly
2025-08-22 19:57:09 +01:00
else if(senseBuffer.IsEmpty) AaruLogging.WriteLine(Localization.Core.MEDIUM_SCAN_github_plead_message);
2022-03-06 13:29:38 +00:00
changingCounter = false;
changingWritten = false;
2022-03-06 13:29:38 +00:00
for(uint b = 0; b < blocks; b += c)
{
2024-05-01 04:05:22 +01:00
if(!canMediumScan) break;
2022-03-06 13:29:38 +00:00
if(_aborted)
{
2022-03-06 13:29:38 +00:00
_resume.BlankExtents = null;
UpdateStatus?.Invoke(Localization.Core.Aborted);
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
if(changingWritten)
{
changingWritten = false;
written = !written;
c = maxBlocks;
}
if(changingCounter)
{
b -= c;
changingCounter = false;
}
2024-05-01 04:05:22 +01:00
if(b + c >= blocks) c = (uint)(blocks - b);
UpdateProgress?.Invoke(written
? string.Format(Localization.Core
.Scanning_for_0_written_blocks_starting_in_block_1,
c,
b)
: string.Format(Localization.Core
.Scanning_for_0_blank_blocks_starting_in_block_1,
c,
b),
b,
(long)blocks);
conditionMet = _dev.MediumScan(out _,
written,
false,
false,
false,
false,
b,
c,
c,
out _,
out _,
uint.MaxValue,
out _);
2022-03-06 13:29:38 +00:00
if(conditionMet)
{
2022-03-06 13:29:38 +00:00
if(written)
writtenExtents.Add(b, c, true);
else
2022-03-06 13:29:38 +00:00
blankExtents.Add(b, c, true);
2024-05-01 04:05:22 +01:00
if(c < maxBlocks) changingWritten = true;
2022-03-06 13:29:38 +00:00
}
else
{
if(c > 64)
c /= 2;
else
c--;
changingCounter = true;
2024-05-01 04:05:22 +01:00
if(c != 0) continue;
2022-03-06 13:29:38 +00:00
written = !written;
c = maxBlocks;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(_resume != null && canMediumScan)
_resume.BlankExtents = ExtentsConverter.ToMetadata(blankExtents).ToArray();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
}
else
{
writtenExtents.Add(0, blocks - 1);
2022-03-06 13:29:38 +00:00
foreach(Tuple<ulong, ulong> blank in blankExtents.ToArray())
2025-08-22 19:57:09 +01:00
{
for(ulong b = blank.Item1; b <= blank.Item2; b++) writtenExtents.Remove(b);
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(writtenExtents.Count == 0)
{
UpdateStatus?.Invoke(Localization.Core.Cannot_dump_empty_media);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2022-03-06 13:29:38 +00:00
Tuple<ulong, ulong>[] extentsToDump = writtenExtents.ToArray();
2022-03-06 13:29:38 +00:00
foreach(Tuple<ulong, ulong> extent in extentsToDump)
{
2024-05-01 04:05:22 +01:00
if(extent.Item2 < _resume.NextBlock) continue; // Skip this extent
2022-03-06 13:29:38 +00:00
ulong nextBlock = extent.Item1;
2024-05-01 04:05:22 +01:00
if(extent.Item1 < _resume.NextBlock) nextBlock = (uint)_resume.NextBlock;
_speedStopwatch.Restart();
2022-03-06 13:29:38 +00:00
for(ulong i = nextBlock; i <= extent.Item2; i += blocksToRead)
{
if(_aborted)
{
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
2022-03-06 13:29:38 +00:00
break;
}
2024-05-01 04:05:22 +01:00
if(extent.Item2 + 1 - i < blocksToRead) blocksToRead = (uint)(extent.Item2 + 1 - i);
2024-05-01 04:05:22 +01:00
if(currentSpeed > maxSpeed && currentSpeed > 0) maxSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
if(currentSpeed < minSpeed && currentSpeed > 0) minSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
UpdateProgress?.Invoke(string.Format(Localization.Core.Reading_sector_0_of_1_2,
i,
blocks,
ByteSize.FromMegabytes(currentSpeed).Per(_oneSecond).Humanize()),
(long)i,
(long)blocks);
2022-03-06 13:29:38 +00:00
sense = scsiReader.ReadBlocks(out buffer, i, blocksToRead, out double cmdDuration, out _, out _);
totalDuration += cmdDuration;
if(!sense && !_dev.Error)
2022-03-06 13:29:38 +00:00
{
2022-12-05 22:03:12 +00:00
mhddLog.Write(i, cmdDuration, blocksToRead);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, currentSpeed * 1024);
_writeStopwatch.Restart();
2022-03-06 13:29:38 +00:00
outputFormat.WriteSectors(buffer, i, blocksToRead);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
extents.Add(i, blocksToRead, true);
}
else
{
// TODO: Reset device after X errors
2024-05-01 04:05:22 +01:00
if(_stopOnError) return; // TODO: Return more cleanly
2024-05-01 04:05:22 +01:00
if(i + _skip > extent.Item2 + 1) _skip = (uint)(extent.Item2 + 1 - i);
2022-03-06 13:29:38 +00:00
// Write empty data
_writeStopwatch.Restart();
2022-03-06 13:29:38 +00:00
outputFormat.WriteSectors(new byte[blockSize * _skip], i, _skip);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2024-05-01 04:05:22 +01:00
for(ulong b = i; b < i + _skip; b++) _resume.BadBlocks.Add(b);
2022-03-06 13:29:38 +00:00
2022-12-05 22:03:12 +00:00
mhddLog.Write(i, cmdDuration < 500 ? 65535 : cmdDuration, _skip);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, 0);
AaruLogging.WriteLine(Localization.Core.Skipping_0_blocks_from_errored_block_1, _skip, i);
2022-03-06 13:29:38 +00:00
i += _skip - blocksToRead;
newTrim = true;
}
_writeStopwatch.Stop();
2022-03-06 13:29:38 +00:00
sectorSpeedStart += blocksToRead;
_resume.NextBlock = i + blocksToRead;
double elapsed = _speedStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
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);
sectorSpeedStart = 0;
_speedStopwatch.Restart();
2022-03-06 13:29:38 +00:00
}
}
2022-03-06 13:29:38 +00:00
_speedStopwatch.Stop();
2022-03-06 13:29:38 +00:00
_resume.BadBlocks = _resume.BadBlocks.Distinct().ToList();
EndProgress?.Invoke();
}
}