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

868 lines
35 KiB
C#
Raw Normal View History

2020-05-14 03:43:21 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : MiniDisc.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Dumps MiniDisc 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
2020-05-14 03:43:21 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable JoinDeclarationAndInitializer
2020-05-14 03:43:21 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
2020-05-14 03:43:21 +01:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.AaruMetadata;
2020-05-14 03:43:21 +01:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Extents;
using Aaru.CommonTypes.Interfaces;
2022-12-05 21:29:04 +00:00
using Aaru.Core.Graphics;
2020-05-14 03:43:21 +01:00
using Aaru.Core.Logging;
using Aaru.Decoders.SCSI;
using Aaru.Devices;
using Aaru.Logging;
2023-09-26 02:40:11 +01:00
using Humanizer;
2023-09-26 01:29:40 +01:00
using Humanizer.Bytes;
2023-09-26 03:39:10 +01:00
using Humanizer.Localisation;
2020-05-14 03:43:21 +01:00
using Version = Aaru.CommonTypes.Interop.Version;
namespace Aaru.Core.Devices.Dumping;
2022-03-06 13:29:38 +00:00
/// <summary>Implements dumping MiniDisc Data devices</summary>
partial class Dump
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
/// <summary>Dumps a MiniDisc Data device</summary>
void MiniDisc()
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
bool sense;
byte scsiMediumType = 0;
const ushort sbcProfile = 0x0001;
double totalDuration = 0;
double currentSpeed = 0;
double maxSpeed = double.MinValue;
double minSpeed = double.MaxValue;
2022-03-06 13:29:38 +00:00
byte[] readBuffer;
Modes.DecodedMode? decMode = null;
Dictionary<MediaTagType, byte[]> mediaTags = new();
bool ret;
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
AaruLogging.WriteLine(Localization.Core.Initializing_reader);
2022-03-06 13:29:38 +00:00
var scsiReader = new Reader(_dev, _dev.Timeout, null, _errorLog);
ulong blocks = scsiReader.GetDeviceBlocks();
uint blockSize = scsiReader.LogicalBlockSize;
UpdateStatus?.Invoke(Localization.Core.Requesting_MODE_SENSE_6);
2022-03-06 13:29:38 +00:00
2022-11-14 01:10:11 +00:00
sense = _dev.ModeSense6(out byte[] cmdBuf, out _, true, ScsiModeSensePageControl.Current, 0x3F, 5, out _);
2022-03-06 13:29:38 +00:00
if(!sense && !_dev.Error && Modes.DecodeMode6(cmdBuf, _dev.ScsiType).HasValue)
2022-03-06 13:29:38 +00:00
decMode = Modes.DecodeMode6(cmdBuf, _dev.ScsiType);
2024-05-01 04:05:22 +01:00
if(decMode.HasValue) scsiMediumType = (byte)(decMode?.Header.MediumType ?? default(MediumTypes));
2022-03-06 13:29:38 +00:00
if(blockSize != 2048)
2020-05-14 03:43:21 +01:00
{
2024-05-01 04:05:22 +01:00
StoppingErrorMessage?.Invoke(Localization.Core
.MiniDisc_albums_NetMD_discs_or_user_written_audio_MiniDisc_cannot_be_dumped);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
const MediaType dskType = MediaType.MDData;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(scsiReader.FindReadCommand())
{
AaruLogging.Error(Localization.Core.ERROR_Cannot_find_correct_read_command_0, scsiReader.ErrorMessage);
StoppingErrorMessage?.Invoke(Localization.Core.Unable_to_read_medium);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
2022-03-17 23:54:41 +00:00
if(blocks != 0)
2022-03-06 13:29:38 +00:00
{
blocks++;
2023-09-26 01:29:40 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Media_has_0_blocks_of_1_bytes_each_for_a_total_of_2,
2024-05-01 04:05:22 +01:00
blocks,
blockSize,
2023-09-26 01:29:40 +01:00
ByteSize.FromBytes(blocks * blockSize).ToString("0.000")));
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
// Check how many blocks to read, if error show and return
// 64 works, gets maximum speed (150KiB/s), slow I know...
if(scsiReader.GetBlocksToRead())
{
AaruLogging.Error(Localization.Core.ERROR_Cannot_get_blocks_to_read_0, scsiReader.ErrorMessage);
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke(scsiReader.ErrorMessage);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
uint blocksToRead = scsiReader.BlocksToRead;
uint physicalBlockSize = scsiReader.PhysicalBlockSize;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(blocks == 0)
{
AaruLogging.Error(Localization.Core.ERROR_Unable_to_read_medium_or_empty_medium_present);
StoppingErrorMessage?.Invoke(Localization.Core.Unable_to_read_medium_or_empty_medium_present);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Device_reports_0_blocks_1_bytes,
blocks,
blocks * blockSize));
2023-10-03 22:57:50 +01:00
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,
scsiReader.LongBlockSize));
2023-10-03 22:57:50 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.SCSI_device_type_0, _dev.ScsiType));
UpdateStatus?.Invoke(string.Format(Localization.Core.SCSI_medium_type_0, scsiMediumType));
UpdateStatus?.Invoke(string.Format(Localization.Core.Media_identified_as_0, dskType));
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
sense = _dev.MiniDiscGetType(out cmdBuf, out _, _dev.Timeout, out _);
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(!sense && !_dev.Error) mediaTags.Add(MediaTagType.MiniDiscType, cmdBuf);
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
sense = _dev.MiniDiscD5(out cmdBuf, out _, _dev.Timeout, out _);
2020-06-05 01:54:31 +01:00
2024-05-01 04:05:22 +01:00
if(!sense && !_dev.Error) mediaTags.Add(MediaTagType.MiniDiscD5, cmdBuf);
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
sense = _dev.MiniDiscReadDataTOC(out cmdBuf, out _, _dev.Timeout, out _);
2020-06-05 01:54:31 +01:00
2024-05-01 04:05:22 +01:00
if(!sense && !_dev.Error) mediaTags.Add(MediaTagType.MiniDiscDTOC, cmdBuf);
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
var utocMs = new MemoryStream();
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
for(uint i = 0; i < 3; i++)
{
sense = _dev.MiniDiscReadUserTOC(out cmdBuf, out _, i, _dev.Timeout, out _);
2020-06-05 01:54:31 +01:00
2024-05-01 04:05:22 +01:00
if(sense || _dev.Error) break;
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
utocMs.Write(cmdBuf, 0, 2336);
}
2020-06-05 01:54:31 +01:00
2024-05-01 04:05:22 +01:00
if(utocMs.Length > 0) mediaTags.Add(MediaTagType.MiniDiscUTOC, utocMs.ToArray());
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
ret = true;
2020-06-05 01:54:31 +01:00
2022-03-06 13:29:38 +00:00
foreach(MediaTagType tag in mediaTags.Keys.Where(tag => !outputFormat.SupportedMediaTags.Contains(tag)))
{
ret = false;
ErrorMessage?.Invoke(string.Format(Localization.Core.Output_format_does_not_support_0, tag));
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(!ret)
{
if(_force)
ErrorMessage?.Invoke(Localization.Core.Several_media_tags_not_supported_continuing);
2022-03-06 13:29:38 +00:00
else
2020-05-14 03:43:21 +01:00
{
StoppingErrorMessage?.Invoke(Localization.Core.Several_media_tags_not_supported_not_continuing);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
2020-05-14 03:43:21 +01:00
}
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Reading_0_sectors_at_a_time, blocksToRead));
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
var mhddLog = new MhddLog(_outputPrefix + ".mhddlog.bin",
_dev,
blocks,
blockSize,
blocksToRead,
_private,
_dimensions);
var ibgLog = new IbgLog(_outputPrefix + ".ibg", sbcProfile);
2022-03-06 13:29:38 +00:00
ret = outputFormat.Create(_outputPath, dskType, _formatOptions, blocks, blockSize);
2020-05-14 03:43:21 +01:00
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);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
_dumpStopwatch.Restart();
2022-03-06 13:29:38 +00:00
double imageWriteDuration = 0;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(decMode?.Pages != null)
{
bool setGeometry = false;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
foreach(Modes.ModePage page in decMode.Value.Pages)
2023-10-03 22:57:50 +01:00
{
2022-11-13 19:38:03 +00:00
switch(page.Page)
2022-03-06 13:29:38 +00:00
{
2022-11-13 19:38:03 +00:00
case 0x04 when page.Subpage == 0x00:
{
Modes.ModePage_04? rigidPage = Modes.DecodeModePage_04(page.PageResponse);
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(!rigidPage.HasValue || setGeometry) continue;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core
.Setting_geometry_to_0_cylinders_1_heads_2_sectors_per_track,
rigidPage.Value.Cylinders,
rigidPage.Value.Heads,
(uint)(blocks /
(rigidPage.Value.Cylinders *
rigidPage.Value.Heads))));
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
outputFormat.SetGeometry(rigidPage.Value.Cylinders,
rigidPage.Value.Heads,
2022-11-13 19:38:03 +00:00
(uint)(blocks / (rigidPage.Value.Cylinders * rigidPage.Value.Heads)));
2020-05-14 03:43:21 +01:00
2022-11-13 19:38:03 +00:00
setGeometry = true;
2020-05-14 03:43:21 +01:00
2022-11-13 19:38:03 +00:00
break;
}
case 0x05 when page.Subpage == 0x00:
{
Modes.ModePage_05? flexiblePage = Modes.DecodeModePage_05(page.PageResponse);
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(!flexiblePage.HasValue) continue;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core
.Setting_geometry_to_0_cylinders_1_heads_2_sectors_per_track,
flexiblePage.Value.Cylinders,
flexiblePage.Value.Heads,
flexiblePage.Value.SectorsPerTrack));
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
outputFormat.SetGeometry(flexiblePage.Value.Cylinders,
flexiblePage.Value.Heads,
2022-11-13 19:38:03 +00:00
flexiblePage.Value.SectorsPerTrack);
setGeometry = true;
break;
}
2022-03-06 13:29:38 +00:00
}
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
DumpHardware currentTry = null;
ExtentsULong extents = null;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
ResumeSupport.Process(true,
_dev.IsRemovable,
blocks,
_dev.Manufacturer,
_dev.Model,
_dev.Serial,
_dev.PlatformId,
ref _resume,
ref currentTry,
ref extents,
_dev.FirmwareRevision,
_private,
_force);
2020-05-14 03:43:21 +01: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);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(_resume.NextBlock > 0)
UpdateStatus?.Invoke(string.Format(Localization.Core.Resuming_from_block_0, _resume.NextBlock));
2020-05-14 03:43:21 +01:00
2022-12-05 21:29:04 +00:00
if(_createGraph)
{
Spiral.DiscParameters discSpiralParameters = Spiral.DiscParametersFromMediaType(dskType);
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);
}
bool newTrim = false;
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();
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
for(ulong i = _resume.NextBlock; i < blocks; i += blocksToRead)
{
if(_aborted)
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
break;
}
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(blocks - i < blocksToRead) blocksToRead = (uint)(blocks - i);
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(currentSpeed > maxSpeed && currentSpeed > 0) maxSpeed = currentSpeed;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(currentSpeed < minSpeed && currentSpeed > 0) minSpeed = currentSpeed;
2020-05-14 03:43:21 +01: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);
2020-05-14 03:43:21 +01:00
2024-04-26 03:16:36 +01:00
_speedStopwatch.Start();
2024-05-01 04:05:22 +01:00
sense = _dev.Read6(out readBuffer,
out _,
(uint)i,
blockSize,
(byte)blocksToRead,
_dev.Timeout,
2022-03-06 13:29:38 +00:00
out double cmdDuration);
2024-05-01 04:05:22 +01:00
2024-04-26 03:16:36 +01:00
_speedStopwatch.Stop();
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
totalDuration += cmdDuration;
2020-05-14 03:43:21 +01:00
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(readBuffer, i, blocksToRead);
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
}
else
{
// TODO: Reset device after X errors
2024-05-01 04:05:22 +01:00
if(_stopOnError) return; // TODO: Return more cleanly
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(i + _skip > blocks) _skip = (uint)(blocks - i);
2020-05-14 03:43:21 +01:00
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;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
for(ulong b = i; b < i + _skip; b++) _resume.BadBlocks.Add(b);
2020-05-14 03:43:21 +01:00
2022-12-05 22:03:12 +00:00
mhddLog.Write(i, cmdDuration < 500 ? 65535 : cmdDuration, _skip);
2020-05-14 03:43:21 +01:00
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;
}
2020-05-14 03:43:21 +01:00
_writeStopwatch.Stop();
2022-03-06 13:29:38 +00:00
sectorSpeedStart += blocksToRead;
_resume.NextBlock = i + blocksToRead;
2020-05-14 03:43:21 +01:00
double elapsed = _speedStopwatch.Elapsed.TotalSeconds;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(elapsed <= 0 || sectorSpeedStart * blockSize < 524288) continue;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
currentSpeed = sectorSpeedStart * blockSize / (1048576 * elapsed);
sectorSpeedStart = 0;
2024-04-26 03:16:36 +01:00
_speedStopwatch.Reset();
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01: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();
2020-05-14 03:43:21 +01:00
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);
2020-05-14 03:43:21 +01:00
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Dump_finished_in_0,
_dumpStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2020-05-14 03:43:21 +01:00
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()));
2020-05-14 03:43:21 +01:00
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()));
2020-05-14 03:43:21 +01: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);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
ulong[] tmpArray = _resume.BadBlocks.ToArray();
InitProgress?.Invoke();
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
foreach(ulong badSector in tmpArray)
{
if(_aborted)
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
break;
}
2020-05-14 03:43:21 +01:00
PulseProgress?.Invoke(string.Format(Localization.Core.Trimming_sector_0, badSector));
2020-05-14 03:43:21 +01:00
2022-03-07 07:36:44 +00:00
sense = _dev.Read6(out readBuffer, out _, (uint)badSector, blockSize, 1, _dev.Timeout, out double _);
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(sense || _dev.Error) continue;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
outputFormat.WriteSector(readBuffer, badSector);
2022-12-05 21:29:04 +00:00
_mediaGraph?.PaintSectorGood(badSector);
2020-05-14 03:43:21 +01:00
}
2022-03-06 13:29:38 +00:00
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
}
2020-05-14 03:43:21 +01: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
{
int pass = 1;
bool forward = true;
bool runningPersistent = false;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
Modes.ModePage? currentModePage = null;
byte[] md6;
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(_persistent)
{
Modes.ModePage_01 pg;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
sense = _dev.ModeSense6(out readBuffer,
out _,
false,
ScsiModeSensePageControl.Current,
0x01,
_dev.Timeout,
out _);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(!sense)
{
Modes.DecodedMode? dcMode6 = Modes.DecodeMode6(readBuffer, _dev.ScsiType);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(dcMode6?.Pages != null)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
foreach(Modes.ModePage modePage in dcMode6.Value.Pages.Where(modePage =>
2024-05-01 04:05:22 +01:00
modePage is { Page: 0x01, Subpage: 0x00 }))
2022-03-06 13:29:38 +00:00
currentModePage = modePage;
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(currentModePage == null)
{
2020-05-14 03:43:21 +01:00
pg = new Modes.ModePage_01
{
2020-07-20 04:34:16 +01:00
PS = false,
2022-03-06 13:29:38 +00:00
AWRE = true,
ARRE = true,
TB = false,
2020-07-20 04:34:16 +01:00
RC = false,
EER = true,
PER = false,
2022-03-06 13:29:38 +00:00
DTE = true,
2020-07-20 04:34:16 +01:00
DCR = false,
2022-03-06 13:29:38 +00:00
ReadRetryCount = 32
2020-05-14 03:43:21 +01:00
};
2022-03-06 13:29:38 +00:00
currentModePage = new Modes.ModePage
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
Page = 0x01,
Subpage = 0x00,
PageResponse = Modes.EncodeModePage_01(pg)
};
}
pg = new Modes.ModePage_01
{
PS = false,
AWRE = false,
ARRE = false,
TB = true,
RC = false,
EER = true,
PER = false,
DTE = false,
DCR = false,
ReadRetryCount = 255
};
var md = new Modes.DecodedMode
{
Header = new Modes.ModeHeader(),
2024-05-01 04:39:38 +01:00
Pages =
[
2022-03-06 13:29:38 +00:00
new Modes.ModePage
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
Page = 0x01,
Subpage = 0x00,
PageResponse = Modes.EncodeModePage_01(pg)
2020-05-14 03:43:21 +01:00
}
2024-05-01 04:39:38 +01:00
]
2022-03-06 13:29:38 +00:00
};
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
md6 = Modes.EncodeMode6(md, _dev.ScsiType);
2020-05-14 03:43:21 +01:00
UpdateStatus?.Invoke(Localization.Core.Sending_MODE_SELECT_to_drive_return_damaged_blocks);
2025-08-22 19:57:09 +01:00
sense = _dev.ModeSelect(md6, out ReadOnlySpan<byte> senseBuf, true, false, _dev.Timeout, out _);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(sense)
{
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(Localization.Core
.Drive_did_not_accept_MODE_SELECT_command_for_persistent_error_reading);
2020-05-14 03:43:21 +01:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(Localization.Core.Error_0, Sense.PrettifySense(senseBuf));
2020-05-14 03:43:21 +01:00
}
2022-03-06 13:29:38 +00:00
else
runningPersistent = true;
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2023-10-03 22:57:50 +01:00
repeatRetry:
2022-03-06 13:29:38 +00:00
ulong[] tmpArray = _resume.BadBlocks.ToArray();
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
foreach(ulong badSector in tmpArray)
{
if(_aborted)
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
break;
}
2020-05-14 03:43:21 +01:00
if(forward)
2023-10-03 22:57:50 +01:00
{
PulseProgress?.Invoke(runningPersistent
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,
2024-05-01 04:05:22 +01:00
badSector,
pass));
2023-10-03 22:57:50 +01:00
}
else
2023-10-03 22:57:50 +01:00
{
PulseProgress?.Invoke(runningPersistent
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,
2024-05-01 04:05:22 +01:00
badSector,
pass));
2023-10-03 22:57:50 +01:00
}
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
sense = _dev.Read6(out readBuffer,
out _,
(uint)badSector,
blockSize,
1,
_dev.Timeout,
2022-03-06 13:29:38 +00:00
out double cmdDuration);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
totalDuration += cmdDuration;
2020-05-14 03:43:21 +01:00
if(!sense && !_dev.Error)
2022-03-06 13:29:38 +00:00
{
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
outputFormat.WriteSector(readBuffer, badSector);
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));
2020-05-14 03:43:21 +01:00
}
2024-05-01 04:05:22 +01:00
else if(runningPersistent) outputFormat.WriteSector(readBuffer, badSector);
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
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();
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
goto repeatRetry;
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(runningPersistent && currentModePage.HasValue)
{
var md = new Modes.DecodedMode
2020-05-14 03:43:21 +01:00
{
2022-03-06 13:29:38 +00:00
Header = new Modes.ModeHeader(),
2024-05-01 04:39:38 +01:00
Pages = [currentModePage.Value]
2022-03-06 13:29:38 +00:00
};
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
md6 = Modes.EncodeMode6(md, _dev.ScsiType);
2020-05-14 03:43:21 +01:00
UpdateStatus?.Invoke(Localization.Core.Sending_MODE_SELECT_to_drive_return_device_to_previous_status);
2022-03-06 13:29:38 +00:00
_dev.ModeSelect(md6, out _, true, false, _dev.Timeout, out _);
2020-05-14 03:43:21 +01:00
}
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
}
2023-10-03 22:57:50 +01:00
2024-05-01 04:05:22 +01:00
#endregion Error handling
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Sort();
2020-05-14 03:43:21 +01:00
foreach(ulong bad in _resume.BadBlocks)
AaruLogging.Information(Localization.Core.Sector_0_could_not_be_read, bad);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
outputFormat.SetDumpHardware(_resume.Tries);
2020-05-14 03:43:21 +01:00
var metadata = new CommonTypes.Structs.ImageInfo
2022-03-06 13:29:38 +00:00
{
Application = "Aaru",
ApplicationVersion = Version.GetVersion()
};
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);
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
double totalChkDuration = 0;
2020-05-14 03:43:21 +01:00
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)
2020-05-14 03:43:21 +01:00
{
StoppingErrorMessage?.Invoke(string.Format(Localization.Core.Error_0_opening_created_image, opened));
2020-05-14 03:43:21 +01:00
return;
}
_sidecarStopwatch.Restart();
2022-03-06 13:29:38 +00:00
_sidecarClass = new Sidecar(inputPlugin, _outputPath, filter.Id, _encoding);
_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();
_sidecarStopwatch.Stop();
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(!_aborted)
2020-05-14 03:43:21 +01:00
{
totalChkDuration = _sidecarStopwatch.Elapsed.TotalMilliseconds;
2020-05-14 03:43:21 +01:00
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Sidecar_created_in_0,
_sidecarStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
2020-05-14 03:43:21 +01:00
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()));
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(_preSidecar != null)
{
_preSidecar.BlockMedias = sidecar.BlockMedias;
sidecar = _preSidecar;
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
2024-05-01 04:39:38 +01:00
List<(ulong start, string type)> filesystems = [];
2020-05-14 03:43:21 +01:00
if(sidecar.BlockMedias[0].FileSystemInformation != null)
2023-10-03 22:57:50 +01:00
{
filesystems.AddRange(from partition in sidecar.BlockMedias[0].FileSystemInformation
2023-10-03 22:57:50 +01:00
where partition.FileSystems != null
from fileSystem in partition.FileSystems
2022-03-06 13:29:38 +00:00
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
{
2022-03-06 13:29:38 +00:00
foreach(var filesystem in filesystems.Select(o => new
{
o.start,
o.type
2024-05-01 04:05:22 +01:00
})
.Distinct())
{
UpdateStatus?.Invoke(string.Format(Localization.Core.Found_filesystem_0_at_sector_1,
2024-05-01 04:05:22 +01:00
filesystem.type,
filesystem.start));
}
2023-10-03 22:57:50 +01:00
}
2020-05-14 03:43:21 +01:00
2022-12-17 20:59:12 +00:00
sidecar.BlockMedias[0].Dimensions = Dimensions.FromMediaType(dskType);
2022-03-06 13:29:38 +00:00
(string type, string subType) xmlType = CommonTypes.Metadata.MediaType.MediaTypeToString(dskType);
sidecar.BlockMedias[0].MediaType = xmlType.type;
sidecar.BlockMedias[0].MediaSubType = xmlType.subType;
2022-03-06 13:29:38 +00:00
if(!_dev.IsRemovable || _dev.IsUsb)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
if(_dev.Type == DeviceType.ATAPI)
sidecar.BlockMedias[0].Interface = "ATAPI";
2022-03-06 13:29:38 +00:00
else if(_dev.IsUsb)
sidecar.BlockMedias[0].Interface = "USB";
2022-03-06 13:29:38 +00:00
else if(_dev.IsFireWire)
sidecar.BlockMedias[0].Interface = "FireWire";
2022-03-06 13:29:38 +00:00
else
sidecar.BlockMedias[0].Interface = "SCSI";
2023-10-03 22:57:50 +01:00
}
2020-05-14 03:43:21 +01:00
sidecar.BlockMedias[0].LogicalBlocks = blocks;
sidecar.BlockMedias[0].PhysicalBlockSize = physicalBlockSize;
sidecar.BlockMedias[0].LogicalBlockSize = blockSize;
sidecar.BlockMedias[0].Manufacturer = _dev.Manufacturer;
sidecar.BlockMedias[0].Model = _dev.Model;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(!_private) sidecar.BlockMedias[0].Serial = _dev.Serial;
2020-05-14 03:43:21 +01:00
sidecar.BlockMedias[0].Size = blocks * blockSize;
2020-05-14 03:43:21 +01:00
2024-05-01 04:05:22 +01:00
if(_dev.IsRemovable) sidecar.BlockMedias[0].DumpHardware = _resume.Tries;
2020-05-14 03:43:21 +01:00
UpdateStatus?.Invoke(Localization.Core.Writing_metadata_sidecar);
2020-05-14 03:43:21 +01:00
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);
2020-05-14 03:43:21 +01:00
jsonFs.Close();
2020-05-14 03:43:21 +01:00
}
2022-03-06 13:29:38 +00:00
}
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("");
2020-05-14 03:43:21 +01:00
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)));
2020-05-14 03:43:21 +01:00
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()));
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
if(maxSpeed > 0)
2023-10-03 22:57:50 +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 03:39:10 +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
}
2023-09-26 02:40:11 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core._0_sectors_could_not_be_read, _resume.BadBlocks.Count));
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("");
2020-05-14 03:43:21 +01:00
2022-03-06 13:29:38 +00:00
Statistics.AddMedia(dskType, true);
2020-05-14 03:43:21 +01:00
}
}