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

1422 lines
57 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : SSC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Dumps media from SCSI Streaming 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable JoinDeclarationAndInitializer
namespace Aaru.Core.Devices.Dumping;
using System;
2019-05-03 00:24:30 +01:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2017-12-21 14:30:38 +00:00
using System.Threading;
2019-05-03 00:24:30 +01:00
using System.Xml.Serialization;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Extents;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Metadata;
using Aaru.CommonTypes.Structs;
2020-02-29 18:03:35 +00:00
using Aaru.Core.Logging;
2020-02-27 00:33:26 +00:00
using Aaru.Decoders.SCSI;
using Aaru.Decoders.SCSI.SSC;
using Aaru.Devices;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using Schemas;
2020-02-27 00:33:26 +00:00
using MediaType = Aaru.CommonTypes.MediaType;
using Version = Aaru.CommonTypes.Interop.Version;
2022-03-06 13:29:38 +00:00
partial class Dump
{
2022-03-06 13:29:38 +00:00
/// <summary>Dumps the tape from a SCSI Streaming device</summary>
void Ssc()
{
2022-03-06 13:29:38 +00:00
DecodedSense? decSense;
bool sense;
uint blockSize;
2022-03-17 23:54:41 +00:00
ulong blocks = 0;
MediaType dskType;
2022-03-06 13:29:38 +00:00
DateTime start;
DateTime end;
double totalDuration = 0;
double currentSpeed = 0;
double maxSpeed = double.MinValue;
double minSpeed = double.MaxValue;
var outputTape = _outputPlugin as IWritableTapeImage;
_dev.RequestSense(out byte[] senseBuf, _dev.Timeout, out double duration);
decSense = Sense.Decode(senseBuf);
InitProgress?.Invoke();
if(decSense.HasValue &&
decSense.Value.SenseKey != SenseKeys.NoSense)
{
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive has status error, please correct. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
return;
}
// Not in BOM/P
if(decSense is { ASC: 0x00 } &&
decSense.Value.ASCQ != 0x00 &&
decSense.Value.ASCQ != 0x04 &&
decSense.Value.SenseKey != SenseKeys.IllegalRequest)
{
_dumpLog.WriteLine("Rewinding, please wait...");
PulseProgress?.Invoke("Rewinding, please wait...");
// Rewind, let timeout apply
_dev.Rewind(out senseBuf, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
// Still rewinding?
// TODO: Pause?
do
{
PulseProgress?.Invoke("Rewinding, please wait...");
_dev.RequestSense(out senseBuf, _dev.Timeout, out duration);
decSense = Sense.Decode(senseBuf);
2022-03-17 23:54:41 +00:00
} while(decSense is { ASC: 0x00, ASCQ: 0x1A or not (0x04 and 0x00) });
2022-03-06 13:29:38 +00:00
_dev.RequestSense(out senseBuf, _dev.Timeout, out duration);
decSense = Sense.Decode(senseBuf);
// And yet, did not rewind!
if(decSense.HasValue &&
2022-03-07 07:36:44 +00:00
(decSense.Value.ASC == 0x00 && decSense.Value.ASCQ != 0x04 && decSense.Value.ASCQ != 0x00 ||
2022-03-06 13:29:38 +00:00
decSense.Value.ASC != 0x00))
{
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive could not rewind, please correct. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
_dumpLog.WriteLine("Drive could not rewind, please correct. Sense follows...");
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
return;
}
2022-03-06 13:29:38 +00:00
}
// Check position
2022-03-07 07:36:44 +00:00
sense = _dev.ReadPosition(out byte[] cmdBuf, out senseBuf, SscPositionForms.Short, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
if(sense)
{
// READ POSITION is mandatory starting SCSI-2, so do not cry if the drive does not recognize the command (SCSI-1 or earlier)
// Anyway, <=SCSI-1 tapes do not support partitions
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
if(decSense.HasValue &&
2022-03-07 07:36:44 +00:00
(decSense.Value.ASC == 0x20 && decSense.Value.ASCQ != 0x00 || decSense.Value.ASC != 0x20 &&
decSense.Value.SenseKey != SenseKeys.IllegalRequest))
{
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Could not get position. Sense follows..." + Environment.NewLine +
decSense.Value.Description);
_dumpLog.WriteLine("Could not get position. Sense follows...");
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
return;
}
}
else
{
// Not in partition 0
if(cmdBuf[1] != 0)
{
UpdateStatus?.Invoke("Drive not in partition 0. Rewinding, please wait...");
_dumpLog.WriteLine("Drive not in partition 0. Rewinding, please wait...");
// Rewind, let timeout apply
2022-03-06 13:29:38 +00:00
sense = _dev.Locate(out senseBuf, false, 0, 0, _dev.Timeout, out duration);
if(sense)
{
StoppingErrorMessage?.Invoke("Drive could not rewind, please correct. Sense follows..." +
Environment.NewLine + decSense?.Description);
_dumpLog.WriteLine("Drive could not rewind, please correct. Sense follows...");
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense?.SenseKey,
decSense?.ASC, decSense?.ASCQ);
return;
}
// Still rewinding?
// TODO: Pause?
do
{
2022-03-06 13:29:38 +00:00
Thread.Sleep(1000);
PulseProgress?.Invoke("Rewinding, please wait...");
2019-12-25 18:07:05 +00:00
_dev.RequestSense(out senseBuf, _dev.Timeout, out duration);
decSense = Sense.Decode(senseBuf);
} while(decSense is { ASC: 0x00 } &&
2022-03-16 11:47:00 +00:00
decSense.Value.ASCQ is 0x1A or 0x19);
// And yet, did not rewind!
if(decSense.HasValue &&
2022-03-07 07:36:44 +00:00
(decSense.Value.ASC == 0x00 && decSense.Value.ASCQ != 0x04 && decSense.Value.ASCQ != 0x00 ||
decSense.Value.ASC != 0x00))
{
StoppingErrorMessage?.Invoke("Drive could not rewind, please correct. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
2019-12-25 18:07:05 +00:00
_dumpLog.WriteLine("Drive could not rewind, please correct. Sense follows...");
2022-03-07 07:36:44 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
return;
}
2022-03-07 07:36:44 +00:00
sense = _dev.ReadPosition(out cmdBuf, out senseBuf, SscPositionForms.Short, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
if(sense)
{
2022-03-06 13:29:38 +00:00
decSense = Sense.Decode(senseBuf);
StoppingErrorMessage?.Invoke("Drive could not rewind, please correct. Sense follows..." +
Environment.NewLine + decSense?.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not rewind, please correct. Sense follows...");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense?.SenseKey,
decSense?.ASC, decSense?.ASCQ);
return;
}
2022-03-06 13:29:38 +00:00
// Still not in partition 0!!!?
if(cmdBuf[1] != 0)
{
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive could not rewind to partition 0 but no error occurred...");
_dumpLog.WriteLine("Drive could not rewind to partition 0 but no error occurred...");
2022-03-06 13:29:38 +00:00
return;
}
}
}
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
2022-03-06 13:29:38 +00:00
byte scsiMediumTypeTape = 0;
byte scsiDensityCodeTape = 0;
byte[] mode6Data = null;
byte[] mode10Data = null;
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("Requesting MODE SENSE (10).");
2022-03-07 07:36:44 +00:00
sense = _dev.ModeSense10(out cmdBuf, out senseBuf, false, true, ScsiModeSensePageControl.Current, 0x3F, 0xFF, 5,
out duration);
2022-03-06 13:29:38 +00:00
if(!sense ||
_dev.Error)
sense = _dev.ModeSense10(out cmdBuf, out senseBuf, false, true, ScsiModeSensePageControl.Current, 0x3F,
0x00, 5, out duration);
2022-03-06 13:29:38 +00:00
Modes.DecodedMode? decMode = null;
2022-03-06 13:29:38 +00:00
if(!sense &&
!_dev.Error)
if(Modes.DecodeMode10(cmdBuf, _dev.ScsiType).HasValue)
decMode = Modes.DecodeMode10(cmdBuf, _dev.ScsiType);
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("Requesting MODE SENSE (6).");
2022-03-06 13:29:38 +00:00
sense = _dev.ModeSense6(out cmdBuf, out senseBuf, false, ScsiModeSensePageControl.Current, 0x3F, 0x00, 5,
out duration);
2022-03-06 13:29:38 +00:00
if(sense || _dev.Error)
2022-03-07 07:36:44 +00:00
sense = _dev.ModeSense6(out cmdBuf, out senseBuf, false, ScsiModeSensePageControl.Current, 0x3F, 0x00, 5,
out duration);
2022-03-06 13:29:38 +00:00
if(sense || _dev.Error)
sense = _dev.ModeSense(out cmdBuf, out senseBuf, 5, out duration);
2022-03-06 13:29:38 +00:00
if(!sense &&
!_dev.Error)
if(Modes.DecodeMode6(cmdBuf, _dev.ScsiType).HasValue)
decMode = Modes.DecodeMode6(cmdBuf, _dev.ScsiType);
2022-03-06 13:29:38 +00:00
// TODO: Check partitions page
if(decMode.HasValue)
{
scsiMediumTypeTape = (byte)decMode.Value.Header.MediumType;
2022-03-06 13:29:38 +00:00
if(decMode.Value.Header.BlockDescriptors?.Length > 0)
scsiDensityCodeTape = (byte)decMode.Value.Header.BlockDescriptors[0].Density;
2022-03-06 13:29:38 +00:00
blockSize = decMode.Value.Header.BlockDescriptors?[0].BlockLength ?? 0;
2022-03-17 23:54:41 +00:00
UpdateStatus?.Invoke($"Device reports {blocks} blocks.");
2022-03-06 13:29:38 +00:00
}
else
blockSize = 1;
2022-03-06 13:29:38 +00:00
if(!_dev.ReadBlockLimits(out cmdBuf, out senseBuf, _dev.Timeout, out _))
{
BlockLimits.BlockLimitsData? blockLimits = BlockLimits.Decode(cmdBuf);
2022-03-06 13:29:38 +00:00
if(blockLimits?.minBlockLen > blockSize)
blockSize = blockLimits?.minBlockLen ?? 0;
}
2022-03-06 13:29:38 +00:00
if(blockSize == 0)
blockSize = 1;
2022-03-17 23:54:41 +00:00
dskType = MediaTypeFromDevice.GetFromScsi((byte)_dev.ScsiType, _dev.Manufacturer, _dev.Model,
scsiMediumTypeTape, scsiDensityCodeTape, blocks, blockSize,
_dev.IsUsb, false);
2022-03-06 13:29:38 +00:00
if(dskType == MediaType.Unknown)
dskType = MediaType.UnknownTape;
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"SCSI device type: {_dev.ScsiType}.");
UpdateStatus?.Invoke($"SCSI medium type: {scsiMediumTypeTape}.");
UpdateStatus?.Invoke($"SCSI density type: {scsiDensityCodeTape}.");
UpdateStatus?.Invoke($"Media identified as {dskType}.");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("SCSI device type: {0}.", _dev.ScsiType);
_dumpLog.WriteLine("SCSI medium type: {0}.", scsiMediumTypeTape);
_dumpLog.WriteLine("SCSI density type: {0}.", scsiDensityCodeTape);
_dumpLog.WriteLine("Media identified as {0}.", dskType);
2022-03-07 07:36:44 +00:00
var endOfMedia = false;
2022-03-06 13:29:38 +00:00
ulong currentBlock = 0;
uint currentFile = 0;
byte currentPartition = 0;
byte totalPartitions = 1; // TODO: Handle partitions.
2022-03-07 07:36:44 +00:00
var fixedLen = false;
2022-03-06 13:29:38 +00:00
uint transferLen = blockSize;
2022-03-07 07:36:44 +00:00
firstRead:
2022-03-06 13:29:38 +00:00
sense = _dev.Read6(out cmdBuf, out senseBuf, false, fixedLen, transferLen, blockSize, _dev.Timeout,
out duration);
2022-03-06 13:29:38 +00:00
if(sense)
{
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
if(decSense.HasValue)
if(decSense.Value.SenseKey == SenseKeys.IllegalRequest)
{
sense = _dev.Space(out senseBuf, SscSpaceCodes.LogicalBlock, -1, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
if(sense)
{
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
bool eom = decSense?.Fixed?.EOM == true;
2022-03-06 13:29:38 +00:00
if(decSense?.Descriptor != null &&
decSense.Value.Descriptor.Value.Descriptors.TryGetValue(4, out byte[] sscDescriptor))
Sense.DecodeDescriptor04(sscDescriptor, out _, out eom, out _);
2022-03-06 13:29:38 +00:00
if(!eom)
{
StoppingErrorMessage?.Invoke("Drive could not return back. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not return back. Sense follows...");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0} ASC {1:X2}h ASCQ {2:X2}h",
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
return;
}
}
2022-03-06 13:29:38 +00:00
fixedLen = true;
transferLen = 1;
2022-03-07 07:36:44 +00:00
sense = _dev.Read6(out cmdBuf, out senseBuf, false, fixedLen, transferLen, blockSize, _dev.Timeout,
out duration);
2022-03-06 13:29:38 +00:00
if(sense)
{
decSense = Sense.Decode(senseBuf);
2017-11-20 05:07:16 +00:00
2022-03-07 07:36:44 +00:00
StoppingErrorMessage?.Invoke("Drive could not read. Sense follows..." + Environment.NewLine +
decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not read. Sense follows...");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0} ASC {1:X2}h ASCQ {2:X2}h",
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
return;
}
}
else if(decSense.Value.ASC == 0x00 &&
decSense.Value.ASCQ == 0x00)
{
bool ili = decSense.Value.Fixed?.ILI == true;
bool valid = decSense.Value.Fixed?.InformationValid == true;
uint information = decSense.Value.Fixed?.Information ?? 0;
2022-03-06 13:29:38 +00:00
if(decSense.Value.Descriptor.HasValue)
{
2022-03-06 13:29:38 +00:00
valid = decSense.Value.Descriptor.Value.Descriptors.TryGetValue(0, out byte[] desc00);
2022-03-06 13:29:38 +00:00
if(valid)
information = (uint)Sense.DecodeDescriptor00(desc00);
2022-03-06 13:29:38 +00:00
if(decSense.Value.Descriptor.Value.Descriptors.TryGetValue(4, out byte[] desc04))
Sense.DecodeDescriptor04(desc04, out _, out _, out ili);
}
2022-03-06 13:29:38 +00:00
if(ili && valid)
{
blockSize = (uint)((int)blockSize -
BitConverter.ToInt32(BitConverter.GetBytes(information), 0));
2022-03-06 13:29:38 +00:00
transferLen = blockSize;
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"Blocksize changed to {blockSize} bytes at block {currentBlock}");
_dumpLog.WriteLine("Blocksize changed to {0} bytes at block {1}", blockSize, currentBlock);
2022-03-07 07:36:44 +00:00
sense = _dev.Space(out senseBuf, SscSpaceCodes.LogicalBlock, -1, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
totalDuration += duration;
if(sense)
{
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive could not go back one block. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not go back one block. Sense follows...");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h",
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
return;
}
2022-03-06 13:29:38 +00:00
goto firstRead;
}
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive could not read. Sense follows..." + Environment.NewLine +
decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not read. Sense follows...");
2022-03-07 07:36:44 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0} ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
return;
}
else
{
StoppingErrorMessage?.Invoke("Drive could not read. Sense follows..." + Environment.NewLine +
decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not read. Sense follows...");
2022-03-07 07:36:44 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0} ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
return;
}
else
{
StoppingErrorMessage?.Invoke("Cannot read device, don't know why, exiting...");
_dumpLog.WriteLine("Cannot read device, don't know why, exiting...");
2022-03-06 13:29:38 +00:00
return;
}
}
2022-03-06 13:29:38 +00:00
sense = _dev.Space(out senseBuf, SscSpaceCodes.LogicalBlock, -1, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
if(sense)
{
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
bool eom = decSense?.Fixed?.EOM == true;
2022-03-06 13:29:38 +00:00
if(decSense.Value.Descriptor.HasValue &&
decSense.Value.Descriptor.Value.Descriptors.TryGetValue(4, out byte[] desc04))
Sense.DecodeDescriptor04(desc04, out _, out eom, out _);
2022-03-06 13:29:38 +00:00
if(!eom)
{
StoppingErrorMessage?.Invoke("Drive could not return back. Sense follows..." + Environment.NewLine +
decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not return back. Sense follows...");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0} ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
DumpHardwareType currentTry = null;
ExtentsULong extents = null;
2022-03-06 13:29:38 +00: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, true);
2022-03-06 13:29:38 +00:00
if(currentTry == null ||
extents == null)
{
StoppingErrorMessage?.Invoke("Could not process resume file, not continuing...");
2022-03-06 13:29:38 +00:00
return;
}
2022-03-07 07:36:44 +00:00
var canLocateLong = false;
var canLocate = false;
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("Positioning tape to block 1.");
_dumpLog.WriteLine("Positioning tape to block 1");
2022-03-06 13:29:38 +00:00
sense = _dev.Locate16(out senseBuf, 1, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(!sense)
{
sense = _dev.ReadPositionLong(out cmdBuf, out senseBuf, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(!sense)
2019-05-01 18:56:19 +01:00
{
2022-03-06 13:29:38 +00:00
ulong position = Swapping.Swap(BitConverter.ToUInt64(cmdBuf, 8));
2022-03-06 13:29:38 +00:00
if(position == 1)
{
canLocateLong = true;
UpdateStatus?.Invoke("LOCATE LONG works.");
_dumpLog.WriteLine("LOCATE LONG works.");
}
2019-05-01 18:56:19 +01:00
}
2022-03-06 13:29:38 +00:00
}
2019-05-01 01:19:37 +01:00
2022-03-06 13:29:38 +00:00
sense = _dev.Locate(out senseBuf, 1, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(!sense)
{
sense = _dev.ReadPosition(out cmdBuf, out senseBuf, _dev.Timeout, out _);
if(!sense)
{
2022-03-06 13:29:38 +00:00
ulong position = Swapping.Swap(BitConverter.ToUInt32(cmdBuf, 4));
2022-03-06 13:29:38 +00:00
if(position == 1)
{
2022-03-06 13:29:38 +00:00
canLocate = true;
UpdateStatus?.Invoke("LOCATE works.");
_dumpLog.WriteLine("LOCATE works.");
}
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(_resume.NextBlock > 0)
{
UpdateStatus?.Invoke($"Positioning tape to block {_resume.NextBlock}.");
_dumpLog.WriteLine("Positioning tape to block {0}.", _resume.NextBlock);
2022-03-06 13:29:38 +00:00
if(canLocateLong)
{
2022-03-06 13:29:38 +00:00
sense = _dev.Locate16(out senseBuf, _resume.NextBlock, _dev.Timeout, out _);
if(!sense)
{
2022-03-06 13:29:38 +00:00
sense = _dev.ReadPositionLong(out cmdBuf, out senseBuf, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(sense)
{
2022-03-06 13:29:38 +00:00
if(!_force)
{
_dumpLog.
WriteLine("Could not check current position, unable to resume. If you want to continue use force.");
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.
Invoke("Could not check current position, unable to resume. If you want to continue use force.");
2022-03-06 13:29:38 +00:00
return;
}
2019-05-01 01:19:37 +01:00
2022-03-06 13:29:38 +00:00
_dumpLog.
WriteLine("Could not check current position, unable to resume. Dumping from the start.");
ErrorMessage?.
Invoke("Could not check current position, unable to resume. Dumping from the start.");
canLocateLong = false;
}
else
2019-05-01 01:19:37 +01:00
{
2022-03-06 13:29:38 +00:00
ulong position = Swapping.Swap(BitConverter.ToUInt64(cmdBuf, 8));
2019-05-01 01:19:37 +01:00
2022-03-06 13:29:38 +00:00
if(position != _resume.NextBlock)
2019-05-01 01:19:37 +01:00
{
2019-12-25 18:07:05 +00:00
if(!_force)
2019-05-01 01:19:37 +01:00
{
2019-12-25 18:07:05 +00:00
_dumpLog.
2022-03-06 13:29:38 +00:00
WriteLine("Current position is not as expected, unable to resume. If you want to continue use force.");
StoppingErrorMessage?.
2022-03-06 13:29:38 +00:00
Invoke("Current position is not as expected, unable to resume. If you want to continue use force.");
return;
2019-05-01 01:19:37 +01:00
}
2019-12-25 18:07:05 +00:00
_dumpLog.
2022-03-06 13:29:38 +00:00
WriteLine("Current position is not as expected, unable to resume. Dumping from the start.");
ErrorMessage?.
2022-03-06 13:29:38 +00:00
Invoke("Current position is not as expected, unable to resume. Dumping from the start.");
canLocateLong = false;
2019-05-01 01:19:37 +01:00
}
2022-03-06 13:29:38 +00:00
}
}
else
{
if(!_force)
{
_dumpLog.
WriteLine("Cannot reposition tape, unable to resume. If you want to continue use force.");
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.
Invoke("Cannot reposition tape, unable to resume. If you want to continue use force.");
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Cannot reposition tape, unable to resume. Dumping from the start.");
ErrorMessage?.Invoke("Cannot reposition tape, unable to resume. Dumping from the start.");
canLocateLong = false;
}
}
else if(canLocate)
{
sense = _dev.Locate(out senseBuf, (uint)_resume.NextBlock, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(!sense)
{
sense = _dev.ReadPosition(out cmdBuf, out senseBuf, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(sense)
{
2019-12-25 18:07:05 +00:00
if(!_force)
{
2019-12-25 18:07:05 +00:00
_dumpLog.
2022-03-06 13:29:38 +00:00
WriteLine("Could not check current position, unable to resume. If you want to continue use force.");
StoppingErrorMessage?.
2022-03-06 13:29:38 +00:00
Invoke("Could not check current position, unable to resume. If you want to continue use force.");
return;
}
2022-03-06 13:29:38 +00:00
_dumpLog.
WriteLine("Could not check current position, unable to resume. Dumping from the start.");
2022-03-06 13:29:38 +00:00
ErrorMessage?.
Invoke("Could not check current position, unable to resume. Dumping from the start.");
canLocate = false;
}
else
{
2022-03-06 13:29:38 +00:00
ulong position = Swapping.Swap(BitConverter.ToUInt32(cmdBuf, 4));
2022-03-06 13:29:38 +00:00
if(position != _resume.NextBlock)
{
2019-12-25 18:07:05 +00:00
if(!_force)
{
2019-12-25 18:07:05 +00:00
_dumpLog.
2022-03-06 13:29:38 +00:00
WriteLine("Current position is not as expected, unable to resume. If you want to continue use force.");
StoppingErrorMessage?.
2022-03-06 13:29:38 +00:00
Invoke("Current position is not as expected, unable to resume. If you want to continue use force.");
return;
}
2019-12-25 18:07:05 +00:00
_dumpLog.
2022-03-06 13:29:38 +00:00
WriteLine("Current position is not as expected, unable to resume. Dumping from the start.");
ErrorMessage?.
2022-03-06 13:29:38 +00:00
Invoke("Current position is not as expected, unable to resume. Dumping from the start.");
canLocate = false;
}
}
}
else
{
2019-12-25 18:07:05 +00:00
if(!_force)
{
2019-12-25 18:07:05 +00:00
_dumpLog.
WriteLine("Cannot reposition tape, unable to resume. If you want to continue use force.");
StoppingErrorMessage?.
Invoke("Cannot reposition tape, unable to resume. If you want to continue use force.");
return;
2019-05-01 01:19:37 +01:00
}
2019-12-25 18:07:05 +00:00
_dumpLog.WriteLine("Cannot reposition tape, unable to resume. Dumping from the start.");
ErrorMessage?.Invoke("Cannot reposition tape, unable to resume. Dumping from the start.");
canLocate = false;
2019-05-01 01:19:37 +01:00
}
}
2019-05-01 23:21:16 +01:00
else
2019-05-01 01:19:37 +01:00
{
2022-03-06 13:29:38 +00:00
if(!_force)
2019-05-01 23:21:16 +01:00
{
2022-03-07 07:36:44 +00:00
_dumpLog.WriteLine("Cannot reposition tape, unable to resume. If you want to continue use force.");
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.
Invoke("Cannot reposition tape, unable to resume. If you want to continue use force.");
2019-05-01 23:21:16 +01:00
return;
}
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Cannot reposition tape, unable to resume. Dumping from the start.");
ErrorMessage?.Invoke("Cannot reposition tape, unable to resume. Dumping from the start.");
canLocate = false;
2019-05-01 01:19:37 +01:00
}
2022-03-06 13:29:38 +00:00
}
else
{
_ = canLocateLong ? _dev.Locate16(out senseBuf, false, 0, 0, _dev.Timeout, out duration)
: _dev.Locate(out senseBuf, false, 0, 0, _dev.Timeout, out duration);
2019-05-01 01:19:37 +01:00
2022-03-06 13:29:38 +00:00
do
{
Thread.Sleep(1000);
PulseProgress?.Invoke("Rewinding, please wait...");
_dev.RequestSense(out senseBuf, _dev.Timeout, out duration);
decSense = Sense.Decode(senseBuf);
} while(decSense is { ASC: 0x00 } &&
2022-03-16 11:47:00 +00:00
decSense.Value.ASCQ is 0x1A or 0x19);
2022-03-06 13:29:38 +00:00
// And yet, did not rewind!
if(decSense.HasValue &&
2022-03-07 07:36:44 +00:00
(decSense.Value.ASC == 0x00 && decSense.Value.ASCQ != 0x00 && decSense.Value.ASCQ != 0x04 ||
2022-03-06 13:29:38 +00:00
decSense.Value.ASC != 0x00))
{
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive could not rewind, please correct. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Drive could not rewind, please correct. Sense follows...");
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
return;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
bool ret = outputTape.SetTape();
2019-04-30 23:33:33 +01:00
2022-03-06 13:29:38 +00:00
// Cannot set image to tape mode
if(!ret)
{
_dumpLog.WriteLine("Error setting output image in tape mode, not continuing.");
_dumpLog.WriteLine(outputTape.ErrorMessage);
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Error setting output image in tape mode, not continuing." +
Environment.NewLine + outputTape.ErrorMessage);
2022-03-06 13:29:38 +00:00
return;
}
2019-04-30 23:33:33 +01:00
2022-03-06 13:29:38 +00:00
ret = outputTape.Create(_outputPath, dskType, _formatOptions, 0, 0);
2022-03-06 13:29:38 +00:00
// Cannot create image
if(!ret)
{
_dumpLog.WriteLine("Error creating output image, not continuing.");
_dumpLog.WriteLine(outputTape.ErrorMessage);
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Error creating output image, not continuing." + Environment.NewLine +
outputTape.ErrorMessage);
2022-03-06 13:29:38 +00:00
return;
}
start = DateTime.UtcNow;
var mhddLog = new MhddLog(_outputPrefix + ".mhddlog.bin", _dev, blocks, blockSize, 1, _private);
var ibgLog = new IbgLog(_outputPrefix + ".ibg", 0x0008);
var currentTapeFile = new TapeFile
{
File = currentFile,
FirstBlock = currentBlock,
Partition = currentPartition
};
2022-03-06 13:29:38 +00:00
var currentTapePartition = new TapePartition
{
Number = currentPartition,
FirstBlock = currentBlock
};
if((canLocate || canLocateLong) &&
_resume.NextBlock > 0)
{
currentBlock = _resume.NextBlock;
currentTapeFile =
2022-03-07 07:36:44 +00:00
outputTape.Files.FirstOrDefault(f => f.LastBlock == outputTape?.Files.Max(g => g.LastBlock));
2022-03-06 13:29:38 +00:00
currentTapePartition =
outputTape.TapePartitions.FirstOrDefault(p => p.LastBlock ==
outputTape?.TapePartitions.Max(g => g.LastBlock));
}
2022-03-06 13:29:38 +00:00
if(mode6Data != null)
outputTape.WriteMediaTag(mode6Data, MediaTagType.SCSI_MODESENSE_6);
2022-03-06 13:29:38 +00:00
if(mode10Data != null)
outputTape.WriteMediaTag(mode10Data, MediaTagType.SCSI_MODESENSE_10);
2022-03-06 13:29:38 +00:00
DateTime timeSpeedStart = DateTime.UtcNow;
ulong currentSpeedSize = 0;
double imageWriteDuration = 0;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2022-03-06 13:29:38 +00:00
while(currentPartition < totalPartitions)
{
if(_aborted)
{
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke("Aborted!");
_dumpLog.WriteLine("Aborted!");
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
if(endOfMedia)
{
UpdateStatus?.Invoke($"Finished partition {currentPartition}");
_dumpLog.WriteLine("Finished partition {0}", currentPartition);
2022-03-06 13:29:38 +00:00
currentTapeFile.LastBlock = currentBlock - 1;
2022-03-06 13:29:38 +00:00
if(currentTapeFile.LastBlock > currentTapeFile.FirstBlock)
outputTape.AddFile(currentTapeFile);
2022-03-06 13:29:38 +00:00
currentTapePartition.LastBlock = currentBlock - 1;
outputTape.AddPartition(currentTapePartition);
2022-03-06 13:29:38 +00:00
currentPartition++;
2022-03-06 13:29:38 +00:00
if(currentPartition < totalPartitions)
{
currentFile++;
2022-03-06 13:29:38 +00:00
currentTapeFile = new TapeFile
{
File = currentFile,
FirstBlock = currentBlock,
Partition = currentPartition
};
2022-03-06 13:29:38 +00:00
currentTapePartition = new TapePartition
{
Number = currentPartition,
FirstBlock = currentBlock
};
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"Seeking to partition {currentPartition}");
_dev.Locate(out senseBuf, false, currentPartition, 0, _dev.Timeout, out duration);
totalDuration += duration;
}
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
if(currentSpeed > maxSpeed &&
currentSpeed > 0)
maxSpeed = currentSpeed;
2022-03-06 13:29:38 +00:00
if(currentSpeed < minSpeed &&
currentSpeed > 0)
minSpeed = currentSpeed;
2022-03-06 13:29:38 +00:00
PulseProgress?.Invoke($"Reading block {currentBlock} ({currentSpeed:F3} MiB/sec.)");
2022-03-06 13:29:38 +00:00
sense = _dev.Read6(out cmdBuf, out senseBuf, false, fixedLen, transferLen, blockSize, _dev.Timeout,
out duration);
2022-03-06 13:29:38 +00:00
totalDuration += duration;
2022-03-06 13:29:38 +00:00
if(sense &&
senseBuf?.Length != 0 &&
!ArrayHelpers.ArrayIsNullOrEmpty(senseBuf))
{
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
bool ili = decSense?.Fixed?.ILI == true;
bool valid = decSense?.Fixed?.InformationValid == true;
uint information = decSense?.Fixed?.Information ?? 0;
bool eom = decSense?.Fixed?.EOM == true;
bool filemark = decSense?.Fixed?.Filemark == true;
2022-03-06 13:29:38 +00:00
if(decSense?.Descriptor.HasValue == true)
{
if(decSense.Value.Descriptor.Value.Descriptors.TryGetValue(0, out byte[] desc00))
{
valid = true;
information = (uint)Sense.DecodeDescriptor00(desc00);
}
2019-05-16 23:29:54 +01:00
2022-03-06 13:29:38 +00:00
if(decSense.Value.Descriptor.Value.Descriptors.TryGetValue(4, out byte[] desc04))
Sense.DecodeDescriptor04(desc04, out filemark, out eom, out ili);
}
2022-03-06 13:29:38 +00:00
if(decSense.Value.ASC == 0x00 &&
decSense.Value.ASCQ == 0x00 &&
ili &&
valid)
{
2022-03-07 07:36:44 +00:00
blockSize = (uint)((int)blockSize - BitConverter.ToInt32(BitConverter.GetBytes(information), 0));
2022-03-06 13:29:38 +00:00
if(!fixedLen)
transferLen = blockSize;
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"Blocksize changed to {blockSize} bytes at block {currentBlock}");
_dumpLog.WriteLine("Blocksize changed to {0} bytes at block {1}", blockSize, currentBlock);
2022-03-06 13:29:38 +00:00
sense = _dev.Space(out senseBuf, SscSpaceCodes.LogicalBlock, -1, _dev.Timeout, out duration);
2022-03-06 13:29:38 +00:00
totalDuration += duration;
2022-03-06 13:29:38 +00:00
if(sense)
{
decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke("Drive could not go back one block. Sense follows..." +
Environment.NewLine + decSense.Value.Description);
2022-03-06 13:29:38 +00:00
outputTape.Close();
_dumpLog.WriteLine("Drive could not go back one block. Sense follows...");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h",
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
switch(decSense.Value.SenseKey)
{
case SenseKeys.BlankCheck when currentBlock == 0:
StoppingErrorMessage?.Invoke("Cannot dump a blank tape...");
outputTape.Close();
_dumpLog.WriteLine("Cannot dump a blank tape...");
2022-03-06 13:29:38 +00:00
return;
2022-03-06 13:29:38 +00:00
// For sure this is an end-of-tape/partition
case SenseKeys.BlankCheck when decSense.Value.ASC == 0x00 &&
2022-03-16 11:47:00 +00:00
(decSense.Value.ASCQ is 0x02 or 0x05 || eom):
// TODO: Detect end of partition
endOfMedia = true;
UpdateStatus?.Invoke("Found end-of-tape/partition...");
2019-12-25 18:07:05 +00:00
_dumpLog.WriteLine("Found end-of-tape/partition...");
continue;
2022-03-06 13:29:38 +00:00
case SenseKeys.BlankCheck:
StoppingErrorMessage?.Invoke("Blank block found, end of tape?...");
endOfMedia = true;
_dumpLog.WriteLine("Blank block found, end of tape?...");
2022-03-06 13:29:38 +00:00
continue;
}
2022-11-13 19:38:03 +00:00
switch(decSense.Value.SenseKey)
2022-03-06 13:29:38 +00:00
{
2022-11-13 19:38:03 +00:00
case SenseKeys.NoSense or SenseKeys.RecoveredError when decSense.Value.ASCQ is 0x02 or 0x05 || eom:
// TODO: Detect end of partition
endOfMedia = true;
UpdateStatus?.Invoke("Found end-of-tape/partition...");
_dumpLog.WriteLine("Found end-of-tape/partition...");
2022-11-13 19:38:03 +00:00
continue;
case SenseKeys.NoSense or SenseKeys.RecoveredError when decSense.Value.ASCQ == 0x01 || filemark:
currentTapeFile.LastBlock = currentBlock - 1;
outputTape.AddFile(currentTapeFile);
2022-11-13 19:38:03 +00:00
currentFile++;
2022-11-13 19:38:03 +00:00
currentTapeFile = new TapeFile
{
File = currentFile,
FirstBlock = currentBlock,
Partition = currentPartition
};
2022-11-13 19:38:03 +00:00
UpdateStatus?.Invoke($"Changed to file {currentFile} at block {currentBlock}");
_dumpLog.WriteLine("Changed to file {0} at block {1}", currentFile, currentBlock);
2022-11-13 19:38:03 +00:00
continue;
2022-03-06 13:29:38 +00:00
}
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(decSense is null)
{
2022-11-13 19:38:03 +00:00
StoppingErrorMessage?.Invoke($"Drive could not read block ${currentBlock
}. Sense cannot be decoded, look at log for dump...");
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine($"Drive could not read block ${currentBlock}. Sense bytes follow...");
_dumpLog.WriteLine(PrintHex.ByteArrayToHexArrayString(senseBuf, 32));
2019-05-01 18:56:19 +01:00
}
else
{
2022-11-13 19:38:03 +00:00
StoppingErrorMessage?.Invoke($"Drive could not read block ${currentBlock}. Sense follows...\n{
decSense.Value.SenseKey} {decSense.Value.Description}");
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine($"Drive could not read block ${currentBlock}. Sense follows...");
2022-03-07 07:36:44 +00:00
_dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h", decSense.Value.SenseKey,
decSense.Value.ASC, decSense.Value.ASCQ);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
// TODO: Reset device after X errors
if(_stopOnError)
return; // TODO: Return more cleanly
2022-03-06 13:29:38 +00:00
// Write empty data
DateTime writeStart = DateTime.Now;
outputTape.WriteSector(new byte[blockSize], currentBlock);
imageWriteDuration += (DateTime.Now - writeStart).TotalSeconds;
mhddLog.Write(currentBlock, duration < 500 ? 65535 : duration);
ibgLog.Write(currentBlock, 0);
_resume.BadBlocks.Add(currentBlock);
}
else
{
mhddLog.Write(currentBlock, duration);
ibgLog.Write(currentBlock, currentSpeed * 1024);
DateTime writeStart = DateTime.Now;
outputTape.WriteSector(cmdBuf, currentBlock);
imageWriteDuration += (DateTime.Now - writeStart).TotalSeconds;
extents.Add(currentBlock, 1, true);
}
2022-03-06 13:29:38 +00:00
currentBlock++;
_resume.NextBlock++;
currentSpeedSize += blockSize;
2022-03-06 13:29:38 +00:00
double elapsed = (DateTime.UtcNow - timeSpeedStart).TotalSeconds;
2022-03-06 13:29:38 +00:00
if(elapsed <= 0)
continue;
2022-03-06 13:29:38 +00:00
currentSpeed = currentSpeedSize / (1048576 * elapsed);
currentSpeedSize = 0;
timeSpeedStart = DateTime.UtcNow;
}
2022-03-06 13:29:38 +00:00
_resume.BadBlocks = _resume.BadBlocks.Distinct().ToList();
blocks = currentBlock + 1;
end = DateTime.UtcNow;
2022-03-06 13:29:38 +00:00
// If not aborted this is added at the end of medium
if(_aborted)
{
currentTapeFile.LastBlock = currentBlock - 1;
outputTape.AddFile(currentTapeFile);
2022-03-06 13:29:38 +00:00
currentTapePartition.LastBlock = currentBlock - 1;
outputTape.AddPartition(currentTapePartition);
}
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
mhddLog.Close();
2022-03-06 13:29:38 +00:00
ibgLog.Close(_dev, blocks, blockSize, (end - start).TotalSeconds, currentSpeed * 1024,
blockSize * (double)(blocks + 1) / 1024 / (totalDuration / 1000), _devicePath);
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"Dump finished in {(end - start).TotalSeconds} seconds.");
2022-11-13 19:38:03 +00:00
UpdateStatus?.Invoke($"Average dump speed {blockSize * (double)(blocks + 1) / 1024 / (totalDuration / 1000)
:F3} KiB/sec.");
2019-05-01 18:56:19 +01:00
2022-11-13 19:38:03 +00:00
UpdateStatus?.Invoke($"Average write speed {blockSize * (double)(blocks + 1) / 1024 / imageWriteDuration
:F3} KiB/sec.");
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Dump finished in {0} seconds.", (end - start).TotalSeconds);
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Average dump speed {0:F3} KiB/sec.",
blockSize * (double)(blocks + 1) / 1024 / (totalDuration / 1000));
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Average write speed {0:F3} KiB/sec.",
blockSize * (double)(blocks + 1) / 1024 / imageWriteDuration);
2022-03-06 13:29:38 +00:00
#region Error handling
if(_resume.BadBlocks.Count > 0 &&
!_aborted &&
_retryPasses > 0 &&
(canLocate || canLocateLong))
{
var pass = 1;
var forward = false;
const bool runningPersistent = false;
2022-03-06 13:29:38 +00:00
Modes.ModePage? currentModePage = null;
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(_persistent)
{
// TODO: Implement persistent
}
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2022-03-07 07:36:44 +00:00
repeatRetry:
2022-03-06 13:29:38 +00:00
ulong[] tmpArray = _resume.BadBlocks.ToArray();
2022-03-06 13:29:38 +00:00
foreach(ulong badBlock in tmpArray)
{
if(_aborted)
{
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke("Aborted!");
_dumpLog.WriteLine("Aborted!");
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
break;
}
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
PulseProgress?.Invoke(string.Format("Retrying block {0}, pass {1}, {3}{2}", badBlock, pass,
forward ? "forward" : "reverse",
runningPersistent ? "recovering partial data, " : ""));
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"Positioning tape to block {badBlock}.");
_dumpLog.WriteLine($"Positioning tape to block {badBlock}.");
if(canLocateLong)
{
sense = _dev.Locate16(out senseBuf, _resume.NextBlock, _dev.Timeout, out _);
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(!sense)
{
sense = _dev.ReadPositionLong(out cmdBuf, out senseBuf, _dev.Timeout, out _);
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(sense)
{
_dumpLog.WriteLine("Could not check current position, continuing.");
StoppingErrorMessage?.Invoke("Could not check current position, continuing.");
2022-03-06 13:29:38 +00:00
continue;
2019-05-01 18:56:19 +01:00
}
2022-03-06 13:29:38 +00:00
ulong position = Swapping.Swap(BitConverter.ToUInt64(cmdBuf, 8));
if(position != _resume.NextBlock)
2019-05-01 18:56:19 +01:00
{
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Current position is not as expected, continuing.");
StoppingErrorMessage?.Invoke("Current position is not as expected, continuing.");
2019-05-01 18:56:19 +01:00
continue;
}
}
else
{
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine($"Cannot position tape to block {badBlock}.");
ErrorMessage?.Invoke($"Cannot position tape to block {badBlock}.");
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
continue;
}
}
else
{
sense = _dev.Locate(out senseBuf, (uint)_resume.NextBlock, _dev.Timeout, out _);
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(!sense)
{
sense = _dev.ReadPosition(out cmdBuf, out senseBuf, _dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(sense)
{
_dumpLog.WriteLine("Could not check current position, continuing.");
StoppingErrorMessage?.Invoke("Could not check current position, continuing.");
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
continue;
}
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
ulong position = Swapping.Swap(BitConverter.ToUInt32(cmdBuf, 4));
2022-03-06 13:29:38 +00:00
if(position != _resume.NextBlock)
2019-05-01 18:56:19 +01:00
{
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Current position is not as expected, continuing.");
StoppingErrorMessage?.Invoke("Current position is not as expected, continuing.");
2019-05-01 18:56:19 +01:00
continue;
}
}
2022-03-06 13:29:38 +00:00
else
2019-05-01 18:56:19 +01:00
{
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine($"Cannot position tape to block {badBlock}.");
ErrorMessage?.Invoke($"Cannot position tape to block {badBlock}.");
continue;
2019-05-01 18:56:19 +01:00
}
}
2022-03-06 13:29:38 +00:00
sense = _dev.Read6(out cmdBuf, out senseBuf, false, fixedLen, transferLen, blockSize, _dev.Timeout,
out duration);
2022-03-06 13:29:38 +00:00
totalDuration += duration;
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(!sense &&
!_dev.Error)
2019-05-01 18:56:19 +01:00
{
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Remove(badBlock);
extents.Add(badBlock);
outputTape.WriteSector(cmdBuf, badBlock);
UpdateStatus?.Invoke($"Correctly retried block {badBlock} in pass {pass}.");
_dumpLog.WriteLine("Correctly retried block {0} in pass {1}.", badBlock, pass);
2019-05-01 18:56:19 +01:00
}
2022-03-06 13:29:38 +00:00
else if(runningPersistent)
outputTape.WriteSector(cmdBuf, badBlock);
}
if(pass < _retryPasses &&
!_aborted &&
_resume.BadBlocks.Count > 0)
{
pass++;
forward = !forward;
_resume.BadBlocks.Sort();
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
if(!forward)
_resume.BadBlocks.Reverse();
goto repeatRetry;
2019-05-01 18:56:19 +01:00
}
2022-03-06 13:29:38 +00:00
if(runningPersistent && currentModePage.HasValue)
{
// TODO: Persistent mode
}
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
}
#endregion Error handling
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Sort();
2019-05-01 18:56:19 +01:00
2022-03-06 13:29:38 +00:00
foreach(ulong bad in _resume.BadBlocks)
_dumpLog.WriteLine("Block {0} could not be read.", bad);
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
2020-01-09 18:01:43 +00:00
2022-03-06 13:29:38 +00:00
outputTape.SetDumpHardware(_resume.Tries);
// TODO: Media Serial Number
2022-03-07 07:36:44 +00:00
var metadata = new ImageInfo
2022-03-06 13:29:38 +00:00
{
Application = "Aaru",
ApplicationVersion = Version.GetVersion()
};
if(!outputTape.SetMetadata(metadata))
ErrorMessage?.Invoke("Error {0} setting metadata, continuing..." + Environment.NewLine +
outputTape.ErrorMessage);
if(_preSidecar != null)
outputTape.SetCicmMetadata(_preSidecar);
_dumpLog.WriteLine("Closing output file.");
UpdateStatus?.Invoke("Closing output file.");
DateTime closeStart = DateTime.Now;
outputTape.Close();
DateTime closeEnd = DateTime.Now;
UpdateStatus?.Invoke($"Closed in {(closeEnd - closeStart).TotalSeconds} seconds.");
_dumpLog.WriteLine("Closed in {0} seconds.", (closeEnd - closeStart).TotalSeconds);
if(_aborted)
{
UpdateStatus?.Invoke("Aborted!");
_dumpLog.WriteLine("Aborted!");
2020-01-09 18:01:43 +00:00
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
if(_aborted)
{
UpdateStatus?.Invoke("Aborted!");
_dumpLog.WriteLine("Aborted!");
2019-04-30 23:33:33 +01:00
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
if(_metadata)
{
UpdateStatus?.Invoke("Creating sidecar.");
_dumpLog.WriteLine("Creating sidecar.");
var filters = new FiltersList();
IFilter filter = filters.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)
2019-05-03 00:24:30 +01:00
{
2022-03-06 13:29:38 +00:00
StoppingErrorMessage?.Invoke($"Error {opened} opening created image.");
2019-05-03 00:24:30 +01:00
return;
}
2022-03-06 13:29:38 +00:00
DateTime chkStart = DateTime.UtcNow;
_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;
CICMMetadataType sidecar = _sidecarClass.Create();
end = DateTime.UtcNow;
if(!_aborted)
2019-05-03 00:24:30 +01:00
{
2022-03-06 13:29:38 +00:00
totalChkDuration = (end - chkStart).TotalMilliseconds;
UpdateStatus?.Invoke($"Sidecar created in {(end - chkStart).TotalSeconds} seconds.");
2022-11-13 19:38:03 +00:00
UpdateStatus?.Invoke($"Average checksum speed {
blockSize * (double)(blocks + 1) / 1024 / (totalChkDuration / 1000):F3} KiB/sec.");
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
_dumpLog.WriteLine("Sidecar created in {0} seconds.", (end - chkStart).TotalSeconds);
_dumpLog.WriteLine("Average checksum speed {0:F3} KiB/sec.",
blockSize * (double)(blocks + 1) / 1024 / (totalChkDuration / 1000));
2022-03-06 13:29:38 +00:00
if(_preSidecar != null)
{
_preSidecar.BlockMedia = sidecar.BlockMedia;
sidecar = _preSidecar;
}
2022-03-06 13:29:38 +00:00
List<(ulong start, string type)> filesystems = new();
2022-03-06 13:29:38 +00:00
if(sidecar.BlockMedia[0].FileSystemInformation != null)
filesystems.AddRange(from partition in sidecar.BlockMedia[0].FileSystemInformation
2022-03-07 07:36:44 +00:00
where partition.FileSystems != null from fileSystem in partition.FileSystems
2022-03-06 13:29:38 +00:00
select (partition.StartSector, fileSystem.Type));
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
if(filesystems.Count > 0)
foreach(var filesystem in filesystems.Select(o => new
{
o.start,
o.type
}).Distinct())
{
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"Found filesystem {filesystem.type} at sector {filesystem.start}");
_dumpLog.WriteLine("Found filesystem {0} at sector {1}", filesystem.type, filesystem.start);
}
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
sidecar.BlockMedia[0].Dimensions = Dimensions.DimensionsFromMediaType(dskType);
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
(string type, string subType) xmlType = CommonTypes.Metadata.MediaType.MediaTypeToString(dskType);
2022-03-06 13:29:38 +00:00
sidecar.BlockMedia[0].DiskType = xmlType.type;
sidecar.BlockMedia[0].DiskSubType = xmlType.subType;
2022-03-06 13:29:38 +00:00
// TODO: Implement device firmware revision
if(!_dev.IsRemovable ||
_dev.IsUsb)
if(_dev.Type == DeviceType.ATAPI)
sidecar.BlockMedia[0].Interface = "ATAPI";
else if(_dev.IsUsb)
sidecar.BlockMedia[0].Interface = "USB";
else if(_dev.IsFireWire)
sidecar.BlockMedia[0].Interface = "FireWire";
else
sidecar.BlockMedia[0].Interface = "SCSI";
2022-03-06 13:29:38 +00:00
sidecar.BlockMedia[0].LogicalBlocks = blocks;
sidecar.BlockMedia[0].Manufacturer = _dev.Manufacturer;
sidecar.BlockMedia[0].Model = _dev.Model;
2022-03-06 13:29:38 +00:00
if(!_private)
sidecar.BlockMedia[0].Serial = _dev.Serial;
2022-03-06 13:29:38 +00:00
sidecar.BlockMedia[0].Size = blocks * blockSize;
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
if(_dev.IsRemovable)
sidecar.BlockMedia[0].DumpHardwareArray = _resume.Tries.ToArray();
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("Writing metadata sidecar");
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
var xmlFs = new FileStream(_outputPrefix + ".cicm.xml", FileMode.Create);
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
var xmlSer = new XmlSerializer(typeof(CICMMetadataType));
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.Close();
2019-05-03 00:24:30 +01:00
}
2022-03-06 13:29:38 +00:00
}
2019-05-03 00:24:30 +01:00
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke("");
2022-11-13 19:38:03 +00:00
UpdateStatus?.Invoke($"Took a total of {(end - start).TotalSeconds:F3} seconds ({totalDuration / 1000
:F3} processing commands, {totalChkDuration / 1000:F3} checksumming, {imageWriteDuration:F3} writing, {
(closeEnd - closeStart).TotalSeconds:F3} closing).");
2022-11-13 19:38:03 +00:00
UpdateStatus?.Invoke($"Average speed: {blockSize * (double)(blocks + 1) / 1048576 / (totalDuration / 1000)
:F3} MiB/sec.");
2022-03-06 13:29:38 +00:00
if(maxSpeed > 0)
UpdateStatus?.Invoke($"Fastest speed burst: {maxSpeed:F3} MiB/sec.");
2022-03-06 13:29:38 +00:00
if(minSpeed > 0 &&
minSpeed < double.MaxValue)
UpdateStatus?.Invoke($"Slowest speed burst: {minSpeed:F3} MiB/sec.");
2022-03-06 13:29:38 +00:00
UpdateStatus?.Invoke($"{_resume.BadBlocks.Count} sectors could not be read.");
UpdateStatus?.Invoke("");
2022-03-06 13:29:38 +00:00
Statistics.AddMedia(dskType, true);
}
2017-12-19 20:33:03 +00:00
}