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

998 lines
47 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 ] ----------------------------------------------------------
//
// Dumps 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-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.AaruMetadata;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Extents;
using Aaru.CommonTypes.Interfaces;
using Aaru.Core.Devices.Report;
2022-12-05 21:29:04 +00:00
using Aaru.Core.Graphics;
2020-02-29 18:03:35 +00:00
using Aaru.Core.Logging;
2020-07-13 18:54:41 +01:00
using Aaru.Decoders.ATA;
2020-02-27 00:33:26 +00:00
using Aaru.Decoders.PCMCIA;
using Aaru.Logging;
2023-09-26 02:40:11 +01:00
using Humanizer;
2020-07-13 18:54:41 +01:00
using Identify = Aaru.CommonTypes.Structs.Devices.ATA.Identify;
2020-02-27 00:33:26 +00:00
using Tuple = Aaru.Decoders.PCMCIA.Tuple;
using Version = Aaru.CommonTypes.Interop.Version;
namespace Aaru.Core.Devices.Dumping;
2022-03-06 13:29:38 +00:00
/// <summary>Implements dumping ATA devices</summary>
public partial class Dump
{
2022-03-06 13:29:38 +00:00
/// <summary>Dumps an ATA device</summary>
void Ata()
{
2022-03-17 00:46:26 +00:00
if(_outputPlugin is not IWritableImage outputFormat)
{
StoppingErrorMessage?.Invoke(Localization.Core.Image_is_not_writable_aborting);
2022-03-17 00:46:26 +00:00
return;
}
2022-03-06 13:29:38 +00:00
if(_dumpRaw)
{
if(_force)
ErrorMessage?.Invoke(Localization.Core.Raw_dumping_not_yet_supported_in_ATA_devices_continuing);
2022-03-06 13:29:38 +00:00
else
{
StoppingErrorMessage?.Invoke(Localization.Core.Raw_dumping_not_yet_supported_in_ATA_devices_aborting);
2022-03-06 13:29:38 +00:00
return;
}
}
const ushort ataProfile = 0x0001;
const uint timeout = 5;
double imageWriteDuration = 0;
MediaType mediaType = MediaType.Unknown;
UpdateStatus?.Invoke(Localization.Core.Requesting_ATA_IDENTIFY_DEVICE);
2022-03-06 13:29:38 +00:00
bool sense = _dev.AtaIdentify(out byte[] cmdBuf, out AtaErrorRegistersChs errorChs);
if(sense)
_errorLog?.WriteLine("ATA IDENTIFY DEVICE", _dev.Error, _dev.LastError, errorChs);
else if(Identify.Decode(cmdBuf).HasValue)
{
2022-03-06 13:29:38 +00:00
Identify.IdentifyDevice? ataIdNullable = Identify.Decode(cmdBuf);
2022-03-06 13:29:38 +00:00
if(ataIdNullable != null)
{
// Guaranteed to never fall into default
Identify.IdentifyDevice ataId = ataIdNullable ?? default(Identify.IdentifyDevice);
byte[] ataIdentify = cmdBuf;
double totalDuration = 0;
double currentSpeed = 0;
double maxSpeed = double.MinValue;
double minSpeed = double.MaxValue;
2024-05-01 04:39:38 +01:00
cmdBuf = [];
2022-03-06 13:29:38 +00:00
// Initialize reader
UpdateStatus?.Invoke(Localization.Core.Initializing_reader);
2022-03-06 13:29:38 +00:00
var ataReader = new Reader(_dev, timeout, ataIdentify, _errorLog);
// Fill reader blocks
ulong blocks = ataReader.GetDeviceBlocks();
// Check block sizes
if(ataReader.GetBlockSize())
{
AaruLogging.Error(Localization.Core.ERROR_Cannot_get_block_size_0, ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
ErrorMessage(ataReader.ErrorMessage);
return;
}
2022-03-06 13:29:38 +00:00
uint blockSize = ataReader.LogicalBlockSize;
uint physicalSectorSize = ataReader.PhysicalBlockSize;
2022-03-06 13:29:38 +00:00
if(ataReader.FindReadCommand())
{
AaruLogging.Error(Localization.Core.ERROR_Cannot_find_correct_read_command_0,
ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
ErrorMessage(ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
// Check how many blocks to read, if error show and return
if(ataReader.GetBlocksToRead(_maximumReadable))
{
AaruLogging.Error(Localization.Core.ERROR_Cannot_get_blocks_to_read_0, ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
ErrorMessage(ataReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
uint blocksToRead = ataReader.BlocksToRead;
ushort cylinders = ataReader.Cylinders;
byte heads = ataReader.Heads;
byte sectors = ataReader.Sectors;
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Device_reports_0_blocks_1_bytes,
blocks,
blocks * blockSize));
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core
.Device_reports_0_cylinders_1_heads_2_sectors_per_track,
cylinders,
heads,
sectors));
UpdateStatus?.Invoke(string.Format(Localization.Core.Device_can_read_0_blocks_at_a_time, blocksToRead));
UpdateStatus?.Invoke(string.Format(Localization.Core.Device_reports_0_bytes_per_logical_block,
blockSize));
UpdateStatus?.Invoke(string.Format(Localization.Core.Device_reports_0_bytes_per_physical_block,
physicalSectorSize));
2022-03-06 13:29:38 +00:00
bool removable = !_dev.IsCompactFlash &&
ataId.GeneralConfiguration.HasFlag(Identify.GeneralConfigurationBit.Removable);
DumpHardware currentTry = null;
ExtentsULong extents = null;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
ResumeSupport.Process(ataReader.IsLba,
removable,
blocks,
_dev.Manufacturer,
_dev.Model,
_dev.Serial,
_dev.PlatformId,
ref _resume,
ref currentTry,
ref extents,
_dev.FirmwareRevision,
_private,
_force);
2022-03-06 13:29:38 +00:00
if(currentTry == null || extents == null)
2022-03-06 13:29:38 +00:00
{
StoppingErrorMessage?.Invoke(Localization.Core.Could_not_process_resume_file_not_continuing);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
MhddLog mhddLog;
IbgLog ibgLog;
var ret = true;
2022-03-06 13:29:38 +00:00
if(_dev.IsUsb &&
_dev.UsbDescriptors != null &&
!outputFormat.SupportedMediaTags.Contains(MediaTagType.USB_Descriptors))
{
ret = false;
ErrorMessage(Localization.Core.Output_format_does_not_support_USB_descriptors);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(_dev.IsPcmcia &&
_dev.Cis != null &&
!outputFormat.SupportedMediaTags.Contains(MediaTagType.PCMCIA_CIS))
{
ret = false;
ErrorMessage(Localization.Core.Output_format_does_not_support_PCMCIA_CIS_descriptors);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(!outputFormat.SupportedMediaTags.Contains(MediaTagType.ATA_IDENTIFY))
{
ret = false;
ErrorMessage(Localization.Core.Dump_Ata_Output_format_does_not_support_ATA_IDENTIFY_);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(!ret)
{
if(_force)
ErrorMessage(Localization.Core.Several_media_tags_not_supported_continuing);
2022-03-06 13:29:38 +00:00
else
2019-04-20 14:02:25 +01:00
{
StoppingErrorMessage?.Invoke(Localization.Core.Several_media_tags_not_supported_not_continuing);
2019-04-20 14:02:25 +01:00
return;
}
2022-03-06 13:29:38 +00:00
}
2024-05-01 04:05:22 +01:00
mediaType = MediaTypeFromDevice.GetFromAta(_dev.Manufacturer,
_dev.Model,
_dev.IsRemovable,
_dev.IsCompactFlash,
_dev.IsPcmcia,
blocks);
// TODO: HPA
ret = outputFormat.Create(_outputPath, mediaType, _formatOptions, blocks, 0, 0, blockSize);
2022-03-06 13:29:38 +00:00
// Cannot create image
if(!ret)
{
StoppingErrorMessage?.Invoke(Localization.Core.Error_creating_output_image_not_continuing +
Environment.NewLine +
outputFormat.ErrorMessage);
2022-03-06 13:29:38 +00:00
return;
}
// Setting geometry
outputFormat.SetGeometry(cylinders, heads, sectors);
2022-11-13 19:16:14 +00:00
bool recoveredError;
2022-03-06 13:29:38 +00:00
if(ataReader.IsLba)
{
UpdateStatus?.Invoke(string.Format(Localization.Core.Reading_0_sectors_at_a_time, blocksToRead));
2024-05-01 04:05:22 +01:00
if(_skip < blocksToRead) _skip = blocksToRead;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
mhddLog = new MhddLog(_outputPrefix + ".mhddlog.bin",
_dev,
blocks,
blockSize,
blocksToRead,
_private,
_dimensions);
2022-03-06 13:29:38 +00:00
ibgLog = new IbgLog(_outputPrefix + ".ibg", ataProfile);
if(_resume.NextBlock > 0)
UpdateStatus?.Invoke(string.Format(Localization.Core.Resuming_from_block_0, _resume.NextBlock));
2022-12-05 21:29:04 +00:00
if(_createGraph)
{
Spiral.DiscParameters discSpiralParameters = Spiral.DiscParametersFromMediaType(mediaType);
if(discSpiralParameters is not null)
_mediaGraph = new Spiral((int)_dimensions, (int)_dimensions, discSpiralParameters, blocks);
else
_mediaGraph = new BlockMap((int)_dimensions, (int)_dimensions, blocks);
if(_mediaGraph is not null)
2023-10-03 22:57:50 +01:00
{
2022-12-05 21:29:04 +00:00
foreach(Tuple<ulong, ulong> e in extents.ToArray())
_mediaGraph?.PaintSectorsGood(e.Item1, (uint)(e.Item2 - e.Item1 + 2));
2023-10-03 22:57:50 +01:00
}
2022-12-05 21:29:04 +00:00
_mediaGraph?.PaintSectorsBad(_resume.BadBlocks);
}
var newTrim = false;
2022-03-06 13:29:38 +00:00
_dumpStopwatch.Restart();
2024-04-26 03:16:36 +01:00
_speedStopwatch.Reset();
2023-10-03 22:57:50 +01:00
ulong sectorSpeedStart = 0;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
double elapsed = 0;
2022-03-06 13:29:38 +00:00
for(ulong i = _resume.NextBlock; i < blocks; i += blocksToRead)
{
2022-03-06 13:29:38 +00:00
if(_aborted)
{
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(blocks - i < blocksToRead) blocksToRead = (byte)(blocks - i);
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(currentSpeed > maxSpeed && currentSpeed > 0) maxSpeed = currentSpeed;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(currentSpeed < minSpeed && currentSpeed > 0) minSpeed = currentSpeed;
2022-03-06 13:29:38 +00:00
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
_speedStopwatch.Restart();
bool error = ataReader.ReadBlocks(out cmdBuf, i, blocksToRead, out _, out _, out _);
2024-04-26 03:16:36 +01:00
_speedStopwatch.Stop();
elapsed += _speedStopwatch.Elapsed.TotalMilliseconds;
2022-03-06 13:29:38 +00:00
_writeStopwatch.Restart();
2024-05-01 04:05:22 +01:00
2022-03-06 13:29:38 +00:00
if(!error)
{
mhddLog.Write(i, _speedStopwatch.Elapsed.TotalMilliseconds, blocksToRead);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, currentSpeed * 1024);
outputFormat.WriteSectors(cmdBuf,
i,
false,
blocksToRead,
Enumerable.Repeat(SectorStatus.Dumped, (int)blocksToRead)
.ToArray());
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
extents.Add(i, blocksToRead, true);
2022-12-05 21:29:04 +00:00
_mediaGraph?.PaintSectorsGood(i, blocksToRead);
2022-03-06 13:29:38 +00:00
}
2019-04-19 17:35:01 +01:00
else
{
2024-05-01 04:05:22 +01:00
if(i + _skip > blocks) _skip = (uint)(blocks - i);
2024-05-01 04:05:22 +01:00
for(ulong b = i; b < i + _skip; b++) _resume.BadBlocks.Add(b);
mhddLog.Write(i,
_speedStopwatch.Elapsed.TotalMilliseconds < 500
? 65535
: _speedStopwatch.Elapsed.TotalMilliseconds,
_skip);
2020-02-01 00:11:38 +00:00
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, 0);
outputFormat.WriteSectors(new byte[blockSize * _skip],
i,
false,
_skip,
Enumerable.Repeat(SectorStatus.NotDumped, (int)blocksToRead)
.ToArray());
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
UpdateProgress?.Invoke(Localization.Core.Skipping_0_blocks_from_errored_block_1,
_skip,
(long)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;
if(elapsed < 100) continue;
currentSpeed = sectorSpeedStart * blockSize / (1048576 * elapsed / 1000);
ibgLog.Write(i, currentSpeed * 1024);
2022-03-06 13:29:38 +00:00
sectorSpeedStart = 0;
elapsed = 0;
2024-04-26 03:16:36 +01:00
_speedStopwatch.Reset();
}
_speedStopwatch.Stop();
2022-03-06 13:29:38 +00:00
_resume.BadBlocks = _resume.BadBlocks.Distinct().ToList();
_dumpStopwatch.Stop();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
mhddLog.Close();
currentSpeed = sectorSpeedStart * blockSize / (1048576 * elapsed / 1000);
2024-05-01 04:05:22 +01:00
ibgLog.Close(_dev,
blocks,
blockSize,
_dumpStopwatch.Elapsed.TotalSeconds,
currentSpeed * 1024,
blockSize * (double)(blocks + 1) / 1024 / (totalDuration / 1000),
_devicePath);
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Dump_finished_in_0,
_dumpStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Average_dump_speed_0,
2024-05-01 04:05:22 +01:00
ByteSize.FromBytes(blockSize * (blocks + 1))
.Per(totalDuration.Milliseconds())
.Humanize()));
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Average_write_speed_0,
2024-05-01 04:05:22 +01:00
ByteSize.FromBytes(blockSize * (blocks + 1))
.Per(imageWriteDuration.Seconds())
.Humanize()));
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
#region Trimming
2023-10-03 22:57:50 +01:00
if(_resume.BadBlocks.Count > 0 && !_aborted && _trim && newTrim)
2022-03-06 13:29:38 +00:00
{
_trimStopwatch.Restart();
UpdateStatus?.Invoke(Localization.Core.Trimming_skipped_sectors);
2022-03-06 13:29:38 +00:00
ulong[] tmpArray = _resume.BadBlocks.ToArray();
InitProgress?.Invoke();
2022-03-06 13:29:38 +00:00
foreach(ulong badSector in tmpArray)
{
2019-12-25 18:07:05 +00:00
if(_aborted)
2017-06-20 05:48:09 +01:00
{
2017-12-21 14:30:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
break;
2017-06-20 05:48:09 +01:00
}
PulseProgress?.Invoke(string.Format(Localization.Core.Trimming_sector_0, badSector));
bool error = ataReader.ReadBlock(out cmdBuf,
badSector,
out double duration,
out recoveredError,
out _);
2022-03-06 13:29:38 +00:00
totalDuration += duration;
2024-05-01 04:05:22 +01:00
if(error && !recoveredError) continue;
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
outputFormat.WriteSector(cmdBuf, badSector, false, SectorStatus.Dumped);
2022-12-05 21:29:04 +00:00
_mediaGraph?.PaintSectorGood(badSector);
}
EndProgress?.Invoke();
_trimStopwatch.Stop();
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Trimming_finished_in_0,
_trimStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2022-03-06 13:29:38 +00:00
}
2024-05-01 04:05:22 +01:00
#endregion Trimming
2023-10-03 22:57:50 +01:00
2024-05-01 04:05:22 +01:00
#region Error handling
2023-10-03 22:57:50 +01:00
if(_resume.BadBlocks.Count > 0 && !_aborted && _retryPasses > 0)
2022-03-06 13:29:38 +00:00
{
var pass = 1;
var forward = true;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2023-10-03 22:57:50 +01:00
repeatRetryLba:
2022-03-06 13:29:38 +00:00
ulong[] tmpArray = _resume.BadBlocks.ToArray();
2022-03-06 13:29:38 +00:00
foreach(ulong badSector in tmpArray)
{
2022-03-06 13:29:38 +00:00
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;
}
if(forward)
2023-10-03 22:57:50 +01:00
{
PulseProgress?.Invoke(_persistent
2024-05-01 04:05:22 +01:00
? string.Format(Localization.Core
.Retrying_sector_0_pass_1_recovering_partial_data_forward,
badSector,
pass)
: string.Format(Localization.Core
.Retrying_sector_0_pass_1_forward,
badSector,
pass));
2023-10-03 22:57:50 +01:00
}
else
2023-10-03 22:57:50 +01:00
{
PulseProgress?.Invoke(_persistent
2024-05-01 04:05:22 +01:00
? string.Format(Localization.Core
.Retrying_sector_0_pass_1_recovering_partial_data_reverse,
badSector,
pass)
: string.Format(Localization.Core
.Retrying_sector_0_pass_1_reverse,
badSector,
pass));
2023-10-03 22:57:50 +01:00
}
bool error = ataReader.ReadBlock(out cmdBuf,
badSector,
out double duration,
out recoveredError,
out _);
2022-03-06 13:29:38 +00:00
totalDuration += duration;
2022-03-06 13:29:38 +00:00
if(!error || recoveredError)
{
2019-12-25 18:07:05 +00:00
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
outputFormat.WriteSector(cmdBuf, badSector, false, SectorStatus.Dumped);
2022-12-05 21:29:04 +00:00
_mediaGraph?.PaintSectorGood(badSector);
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core
.Correctly_retried_block_0_in_pass_1,
badSector,
pass));
}
else if(_persistent)
outputFormat.WriteSector(cmdBuf, badSector, false, SectorStatus.Errored);
}
if(pass < _retryPasses && !_aborted && _resume.BadBlocks.Count > 0)
{
2022-03-06 13:29:38 +00:00
pass++;
forward = !forward;
_resume.BadBlocks.Sort();
2024-05-01 04:05:22 +01:00
if(!forward) _resume.BadBlocks.Reverse();
2022-03-06 13:29:38 +00:00
goto repeatRetryLba;
}
EndProgress?.Invoke();
}
2023-10-03 22:57:50 +01:00
2024-05-01 04:05:22 +01:00
#endregion Error handling LBA
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
}
else
{
2024-05-01 04:05:22 +01:00
mhddLog = new MhddLog(_outputPrefix + ".mhddlog.bin",
_dev,
blocks,
blockSize,
blocksToRead,
_private,
_dimensions);
2022-03-06 13:29:38 +00:00
ibgLog = new IbgLog(_outputPrefix + ".ibg", ataProfile);
2022-12-05 21:29:04 +00:00
if(_createGraph)
{
Spiral.DiscParameters discSpiralParameters = Spiral.DiscParametersFromMediaType(mediaType);
if(discSpiralParameters is not null)
_mediaGraph = new Spiral((int)_dimensions, (int)_dimensions, discSpiralParameters, blocks);
else
_mediaGraph = new BlockMap((int)_dimensions, (int)_dimensions, blocks);
if(_mediaGraph is not null)
2023-10-03 22:57:50 +01:00
{
2022-12-05 21:29:04 +00:00
foreach(Tuple<ulong, ulong> e in extents.ToArray())
_mediaGraph?.PaintSectorsGood(e.Item1, (uint)(e.Item2 - e.Item1 + 2));
2023-10-03 22:57:50 +01:00
}
2022-12-05 21:29:04 +00:00
_mediaGraph?.PaintSectorsBad(_resume.BadBlocks);
}
2022-03-06 13:29:38 +00:00
ulong currentBlock = 0;
blocks = (ulong)(cylinders * heads * sectors);
_dumpStopwatch.Restart();
2024-04-26 03:16:36 +01:00
_speedStopwatch.Reset();
2023-10-03 22:57:50 +01:00
ulong sectorSpeedStart = 0;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
double elapsed = 0;
2022-03-06 13:29:38 +00:00
for(ushort cy = 0; cy < cylinders; cy++)
{
for(byte hd = 0; hd < heads; hd++)
{
for(byte sc = 1; sc < sectors; sc++)
{
2019-12-25 18:07:05 +00:00
if(_aborted)
2017-06-20 05:48:09 +01:00
{
2017-12-21 14:30:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
break;
2017-06-20 05:48:09 +01:00
}
2024-05-01 04:05:22 +01:00
if(currentSpeed > maxSpeed && currentSpeed > 0) maxSpeed = currentSpeed;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(currentSpeed < minSpeed && currentSpeed > 0) minSpeed = currentSpeed;
PulseProgress?.Invoke(string.Format(Localization.Core
.Reading_cylinder_0_head_1_sector_2_3,
cy,
hd,
sc,
ByteSize.FromMegabytes(currentSpeed)
.Per(_oneSecond)
.Humanize()));
2022-03-06 13:29:38 +00:00
_speedStopwatch.Restart();
2024-05-01 04:05:22 +01:00
bool error = ataReader.ReadChs(out cmdBuf, cy, hd, sc, out _, out recoveredError);
2024-05-01 04:05:22 +01:00
2024-04-26 03:16:36 +01:00
_speedStopwatch.Stop();
totalDuration += _speedStopwatch.Elapsed.TotalMilliseconds;
_writeStopwatch.Restart();
if(!error || recoveredError)
{
mhddLog.Write(currentBlock, _speedStopwatch.Elapsed.TotalMilliseconds);
outputFormat.WriteSector(cmdBuf,
(ulong)((cy * heads + hd) * sectors + (sc - 1)),
false,
SectorStatus.Dumped);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
extents.Add(currentBlock);
2023-10-03 22:57:50 +01:00
_mediaGraph?.PaintSectorGood((ulong)((cy * heads + hd) * sectors + (sc - 1)));
2022-03-06 13:29:38 +00:00
}
else
{
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Add(currentBlock);
mhddLog.Write(currentBlock,
_speedStopwatch.Elapsed.TotalMilliseconds < 500
? 65535
: _speedStopwatch.Elapsed.TotalMilliseconds);
2022-03-06 13:29:38 +00:00
outputFormat.WriteSector(new byte[blockSize],
(ulong)((cy * heads + hd) * sectors + (sc - 1)),
false,
SectorStatus.Errored);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
}
_writeStopwatch.Stop();
2022-03-06 13:29:38 +00:00
sectorSpeedStart++;
currentBlock++;
if(elapsed < 100) continue;
currentSpeed = sectorSpeedStart * blockSize / (1048576 * elapsed / 1000);
ibgLog.Write(currentBlock, currentSpeed * 1024);
2022-03-06 13:29:38 +00:00
sectorSpeedStart = 0;
elapsed = 0;
2024-04-26 03:16:36 +01:00
_speedStopwatch.Reset();
2022-03-06 13:29:38 +00:00
}
}
}
_speedStopwatch.Stop();
2022-03-06 13:29:38 +00:00
_resume.BadBlocks = _resume.BadBlocks.Distinct().ToList();
_dumpStopwatch.Stop();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
mhddLog.Close();
2024-05-01 04:05:22 +01:00
ibgLog.Close(_dev,
blocks,
blockSize,
_dumpStopwatch.Elapsed.TotalSeconds,
currentSpeed * 1024,
blockSize * (double)(blocks + 1) / 1024 / (totalDuration / 1000),
_devicePath);
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Dump_finished_in_0,
_dumpStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Average_dump_speed_0,
2024-05-01 04:05:22 +01:00
ByteSize.FromBytes(blockSize * (blocks + 1))
.Per(totalDuration.Milliseconds())
.Humanize()));
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Average_write_speed_0,
2024-05-01 04:05:22 +01:00
ByteSize.FromBytes(blockSize * (blocks + 1))
.Per(imageWriteDuration.Seconds())
.Humanize()));
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
foreach(ulong bad in _resume.BadBlocks)
AaruLogging.Information(Localization.Core.Sector_0_could_not_be_read, bad);
2022-03-06 13:29:38 +00:00
outputFormat.SetDumpHardware(_resume.Tries);
2022-03-06 13:29:38 +00:00
// TODO: Non-removable
var metadata = new CommonTypes.Structs.ImageInfo
2022-03-06 13:29:38 +00:00
{
Application = "Aaru",
ApplicationVersion = Version.GetInformationalVersion()
2022-03-06 13:29:38 +00:00
};
if(!outputFormat.SetImageInfo(metadata))
2023-10-03 22:57:50 +01:00
{
ErrorMessage?.Invoke(Localization.Core.Error_0_setting_metadata +
Environment.NewLine +
2022-03-06 13:29:38 +00:00
outputFormat.ErrorMessage);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(_preSidecar != null) outputFormat.SetMetadata(_preSidecar);
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke(Localization.Core.Closing_output_file);
_imageCloseStopwatch.Restart();
2022-03-06 13:29:38 +00:00
outputFormat.Close();
_imageCloseStopwatch.Stop();
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Closed_in_0,
_imageCloseStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2022-03-06 13:29:38 +00:00
if(_aborted)
{
UpdateStatus?.Invoke(Localization.Core.Aborted);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
double totalChkDuration = 0;
2022-03-06 13:29:38 +00:00
outputFormat.WriteMediaTag(ataIdentify, MediaTagType.ATA_IDENTIFY);
2017-12-19 20:33:03 +00:00
if(_dev.IsUsb && _dev.UsbDescriptors != null)
2022-03-06 13:29:38 +00:00
outputFormat.WriteMediaTag(_dev.UsbDescriptors, MediaTagType.USB_Descriptors);
2024-05-01 04:05:22 +01:00
if(_dev.IsPcmcia && _dev.Cis != null) outputFormat.WriteMediaTag(_dev.Cis, MediaTagType.PCMCIA_CIS);
2022-03-06 13:29:38 +00:00
if(_metadata)
{
UpdateStatus?.Invoke(Localization.Core.Creating_sidecar);
IFilter filter = PluginRegister.Singleton.GetFilter(_outputPath);
2022-03-07 07:36:44 +00:00
var inputPlugin = ImageFormat.Detect(filter) as IMediaImage;
2022-03-06 13:29:38 +00:00
ErrorNumber opened = inputPlugin.Open(filter);
if(opened != ErrorNumber.NoError)
{
StoppingErrorMessage?.Invoke(string.Format(Localization.Core.Error_0_opening_created_image,
opened));
return;
}
_sidecarStopwatch.Restart();
2022-03-06 13:29:38 +00:00
_sidecarClass = new Sidecar(inputPlugin, _outputPath, filter.Id, _encoding);
2022-03-06 13:29:38 +00:00
_sidecarClass.InitProgressEvent += InitProgress;
_sidecarClass.UpdateProgressEvent += UpdateProgress;
_sidecarClass.EndProgressEvent += EndProgress;
_sidecarClass.InitProgressEvent2 += InitProgress2;
_sidecarClass.UpdateProgressEvent2 += UpdateProgress2;
_sidecarClass.EndProgressEvent2 += EndProgress2;
_sidecarClass.UpdateStatusEvent += UpdateStatus;
Metadata sidecar = _sidecarClass.Create();
2022-03-06 13:29:38 +00:00
if(!_aborted)
{
2022-03-06 13:29:38 +00:00
if(_preSidecar != null)
2019-04-20 14:02:25 +01:00
{
_preSidecar.BlockMedias = sidecar.BlockMedias;
sidecar = _preSidecar;
2019-04-20 14:02:25 +01:00
}
if(_dev.IsUsb && _dev.UsbDescriptors != null)
{
UpdateStatus?.Invoke(Localization.Core.Reading_USB_descriptors);
sidecar.BlockMedias[0].Usb = new Usb
{
2022-03-06 13:29:38 +00:00
ProductID = _dev.UsbProductId,
VendorID = _dev.UsbVendorId,
Descriptors = new CommonTypes.AaruMetadata.Dump
{
2022-03-06 13:29:38 +00:00
Image = _outputPath,
Size = (ulong)_dev.UsbDescriptors.Length,
Checksums = Checksum.GetChecksums(_dev.UsbDescriptors)
2022-03-06 13:29:38 +00:00
}
};
}
if(_dev.IsPcmcia && _dev.Cis != null)
2022-03-06 13:29:38 +00:00
{
UpdateStatus?.Invoke(Localization.Core.Reading_PCMCIA_CIS);
sidecar.BlockMedias[0].Pcmcia = new Pcmcia
{
Cis = new CommonTypes.AaruMetadata.Dump
{
Image = _outputPath,
2022-03-06 13:29:38 +00:00
Size = (ulong)_dev.Cis.Length,
Checksums = Checksum.GetChecksums(_dev.Cis)
}
};
UpdateStatus?.Invoke(Localization.Core.Decoding_PCMCIA_CIS);
2022-03-06 13:29:38 +00:00
Tuple[] tuples = CIS.GetTuples(_dev.Cis);
2022-03-06 13:29:38 +00:00
if(tuples != null)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
foreach(Tuple tuple in tuples)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
switch(tuple.Code)
{
case TupleCodes.CISTPL_MANFID:
ManufacturerIdentificationTuple manufacturerId =
CIS.DecodeManufacturerIdentificationTuple(tuple);
2022-03-06 13:29:38 +00:00
if(manufacturerId != null)
{
sidecar.BlockMedias[0].Pcmcia.ManufacturerCode =
2022-03-06 13:29:38 +00:00
manufacturerId.ManufacturerID;
sidecar.BlockMedias[0].Pcmcia.CardCode = manufacturerId.CardID;
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
break;
case TupleCodes.CISTPL_VERS_1:
Level1VersionTuple version = CIS.DecodeLevel1VersionTuple(tuple);
2022-03-06 13:29:38 +00:00
if(version != null)
{
sidecar.BlockMedias[0].Pcmcia.Manufacturer = version.Manufacturer;
sidecar.BlockMedias[0].Pcmcia.ProductName = version.Product;
sidecar.BlockMedias[0].Pcmcia.Compliance =
2022-03-06 13:29:38 +00:00
$"{version.MajorVersion}.{version.MinorVersion}";
sidecar.BlockMedias[0].Pcmcia.AdditionalInformation =
2024-05-01 04:39:38 +01:00
[
..version.AdditionalInformation
];
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
break;
}
2023-10-03 22:57:50 +01:00
}
}
2022-03-06 13:29:38 +00:00
}
2024-05-01 04:05:22 +01:00
if(_private) DeviceReport.ClearIdentify(ataIdentify);
2022-03-06 13:29:38 +00:00
sidecar.BlockMedias[0].ATA = new ATA
2022-03-06 13:29:38 +00:00
{
Identify = new CommonTypes.AaruMetadata.Dump
2022-03-06 13:29:38 +00:00
{
Image = _outputPath,
Size = (ulong)cmdBuf.Length,
Checksums = Checksum.GetChecksums(cmdBuf)
2022-03-06 13:29:38 +00:00
}
};
_sidecarStopwatch.Stop();
2022-03-06 13:29:38 +00:00
totalChkDuration = _sidecarStopwatch.Elapsed.TotalMilliseconds;
2022-03-06 13:29:38 +00:00
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Sidecar_created_in_0,
2024-05-01 04:05:22 +01:00
_sidecarStopwatch.Elapsed
.Humanize(minUnit: TimeUnit.Second)));
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Average_checksum_speed_0,
2024-05-01 04:05:22 +01:00
ByteSize.FromBytes(blockSize * (blocks + 1))
.Per(totalChkDuration.Milliseconds())
.Humanize()));
2024-05-01 04:39:38 +01:00
List<(ulong start, string type)> filesystems = [];
if(sidecar.BlockMedias[0].FileSystemInformation != null)
2023-10-03 22:57:50 +01:00
{
filesystems.AddRange(from partition in sidecar.BlockMedias[0].FileSystemInformation
2022-03-06 13:29:38 +00:00
where partition.FileSystems != null
from fileSystem in partition.FileSystems
select (partition.StartSector, fileSystem.Type));
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
if(filesystems.Count > 0)
2023-10-03 22:57:50 +01:00
{
foreach(var filesystem in filesystems.Select(static o => new
{
o.start,
o.type
2024-05-01 04:05:22 +01:00
})
.Distinct())
2019-04-19 17:23:54 +01:00
{
UpdateStatus?.Invoke(string.Format(Localization.Core.Found_filesystem_0_at_sector_1,
2024-05-01 04:05:22 +01:00
filesystem.type,
filesystem.start));
2019-04-19 17:23:54 +01:00
}
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
(string type, string subType) = CommonTypes.Metadata.MediaType.MediaTypeToString(mediaType);
sidecar.BlockMedias[0].MediaType = type;
sidecar.BlockMedias[0].MediaSubType = subType;
sidecar.BlockMedias[0].Interface = "ATA";
sidecar.BlockMedias[0].LogicalBlocks = blocks;
sidecar.BlockMedias[0].PhysicalBlockSize = physicalSectorSize;
sidecar.BlockMedias[0].LogicalBlockSize = blockSize;
sidecar.BlockMedias[0].Manufacturer = _dev.Manufacturer;
sidecar.BlockMedias[0].Model = _dev.Model;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(!_private) sidecar.BlockMedias[0].Serial = _dev.Serial;
sidecar.BlockMedias[0].Size = blocks * blockSize;
if(cylinders > 0 && heads > 0 && sectors > 0)
2022-03-06 13:29:38 +00:00
{
sidecar.BlockMedias[0].Cylinders = cylinders;
sidecar.BlockMedias[0].Heads = heads;
sidecar.BlockMedias[0].SectorsPerTrack = sectors;
}
UpdateStatus?.Invoke(Localization.Core.Writing_metadata_sidecar);
var jsonFs = new FileStream(_outputPrefix + ".metadata.json", FileMode.Create);
2024-05-01 04:05:22 +01:00
JsonSerializer.Serialize(jsonFs,
new MetadataJson
{
AaruMetadata = sidecar
},
typeof(MetadataJson),
MetadataJsonContext.Default);
jsonFs.Close();
2022-03-06 13:29:38 +00:00
}
}
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("");
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core
.Took_a_total_of_0_1_processing_commands_2_checksumming_3_writing_4_closing,
_dumpStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second),
totalDuration.Milliseconds().Humanize(minUnit: TimeUnit.Second),
totalChkDuration.Milliseconds().Humanize(minUnit: TimeUnit.Second),
imageWriteDuration.Seconds().Humanize(minUnit: TimeUnit.Second),
_imageCloseStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Average_speed_0,
2024-05-01 04:05:22 +01:00
ByteSize.FromBytes(blockSize * (blocks + 1))
.Per(totalDuration.Milliseconds())
.Humanize()));
2022-03-06 13:29:38 +00:00
if(maxSpeed > 0)
2023-10-03 22:57:50 +01:00
{
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Fastest_speed_burst_0,
ByteSize.FromMegabytes(maxSpeed).Per(_oneSecond).Humanize()));
2023-10-03 22:57:50 +01:00
}
if(minSpeed is > 0 and < double.MaxValue)
2023-10-03 22:57:50 +01:00
{
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Slowest_speed_burst_0,
ByteSize.FromMegabytes(minSpeed).Per(_oneSecond).Humanize()));
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke(string.Format(Localization.Core._0_sectors_could_not_be_read,
_resume.BadBlocks.Count));
2024-05-01 04:05:22 +01:00
if(_resume.BadBlocks.Count > 0) _resume.BadBlocks.Sort();
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("");
}
2022-03-06 13:29:38 +00:00
Statistics.AddMedia(mediaType, true);
}
2022-03-06 13:29:38 +00:00
else
StoppingErrorMessage?.Invoke(Localization.Core.Unable_to_communicate_with_ATA_device);
}
2017-12-19 20:33:03 +00:00
}