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

251 lines
11 KiB
C#
Raw Normal View History

// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Data.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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
// Copyright © 2020-2025 Rebecca Wallander
// ****************************************************************************/
using System;
using System.Linq;
using Aaru.CommonTypes.AaruMetadata;
2021-01-14 00:03:37 +01:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Extents;
2021-12-28 00:14:33 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.Core.Logging;
2021-01-14 00:03:37 +01:00
using Aaru.Decoders.DVD;
using Aaru.Decryption;
using Aaru.Decryption.DVD;
using Aaru.Logging;
2023-09-26 02:40:11 +01:00
using Humanizer;
using Humanizer.Bytes;
using DVDDecryption = Aaru.Decryption.DVD.Dump;
// ReSharper disable JoinDeclarationAndInitializer
// ReSharper disable InlineOutVariableDeclaration
// ReSharper disable TooWideLocalVariableScope
namespace Aaru.Core.Devices.Dumping;
2022-03-06 13:29:38 +00:00
partial class Dump
{
2022-03-06 13:29:38 +00:00
/// <summary>Dumps data when dumping from a SCSI Block Commands compliant device</summary>
/// <param name="blocks">Media blocks</param>
/// <param name="maxBlocksToRead">Maximum number of blocks to read in a single command</param>
/// <param name="blockSize">Block size in bytes</param>
/// <param name="currentTry">Resume information</param>
/// <param name="extents">Correctly dump extents</param>
/// <param name="currentSpeed">Current speed</param>
/// <param name="minSpeed">Minimum speed</param>
/// <param name="maxSpeed">Maximum speed</param>
/// <param name="totalDuration">Total time spent in commands</param>
/// <param name="scsiReader">SCSI reader</param>
/// <param name="mhddLog">MHDD log</param>
/// <param name="ibgLog">ImgBurn log</param>
/// <param name="imageWriteDuration">Total time spent writing to image</param>
/// <param name="newTrim">Set if we need to start a trim</param>
/// <param name="dvdDecrypt">DVD CSS decryption module</param>
/// <param name="discKey">The DVD disc key</param>
2023-10-03 22:57:50 +01:00
void ReadSbcData(in ulong blocks, in uint maxBlocksToRead, in uint blockSize, DumpHardware currentTry,
2022-03-06 13:29:38 +00:00
ExtentsULong extents, ref double currentSpeed, ref double minSpeed, ref double maxSpeed,
2023-10-03 22:57:50 +01:00
ref double totalDuration, Reader scsiReader, MhddLog mhddLog, IbgLog ibgLog,
ref double imageWriteDuration, ref bool newTrim, ref DVDDecryption dvdDecrypt, byte[] discKey)
{
2023-10-03 22:57:50 +01:00
ulong sectorSpeedStart = 0;
bool sense;
byte[] buffer;
uint blocksToRead = maxBlocksToRead;
var outputFormat = _outputPlugin as IWritableImage;
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
2024-04-26 03:16:36 +01:00
_speedStopwatch.Reset();
2022-03-06 13:29:38 +00:00
for(ulong i = _resume.NextBlock; i < blocks; i += blocksToRead)
{
if(_aborted)
{
2022-03-06 13:29:38 +00:00
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
2022-03-06 13:29:38 +00:00
break;
}
2024-05-01 04:05:22 +01:00
if(blocks - i < blocksToRead) blocksToRead = (uint)(blocks - i);
2024-05-01 04:05:22 +01:00
if(currentSpeed > maxSpeed && currentSpeed > 0) maxSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
if(currentSpeed < minSpeed && currentSpeed > 0) minSpeed = currentSpeed;
2024-05-01 04:05:22 +01:00
UpdateProgress?.Invoke(string.Format(Localization.Core.Reading_sector_0_of_1_2,
i,
blocks,
ByteSize.FromMegabytes(currentSpeed).Per(_oneSecond).Humanize()),
(long)i,
(long)blocks);
2024-04-26 03:16:36 +01:00
_speedStopwatch.Start();
2022-03-06 13:29:38 +00:00
sense = scsiReader.ReadBlocks(out buffer, i, blocksToRead, out double cmdDuration, out _, out _);
totalDuration += cmdDuration;
2024-04-26 03:16:36 +01:00
_speedStopwatch.Stop();
if(!sense && !_dev.Error)
2022-03-06 13:29:38 +00:00
{
if(Settings.Settings.Current.EnableDecryption && discKey != null && _titleKeys)
{
2022-03-06 13:29:38 +00:00
for(ulong j = 0; j < blocksToRead; j++)
2021-01-14 00:03:37 +01:00
{
2024-05-01 04:05:22 +01:00
if(_aborted) break;
2021-01-14 00:03:37 +01:00
2022-03-06 13:29:38 +00:00
if(!_resume.MissingTitleKeys.Contains(i + j))
2022-03-06 13:29:38 +00:00
// Key is already dumped.
continue;
2021-01-14 00:03:37 +01:00
2022-03-06 13:29:38 +00:00
byte[] tmpBuf;
2021-01-14 00:03:37 +01:00
2024-05-01 04:05:22 +01:00
bool tmpSense = dvdDecrypt.ReadTitleKey(out tmpBuf,
out _,
DvdCssKeyClass.DvdCssCppmOrCprm,
i + j,
_dev.Timeout,
out _);
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(tmpSense) continue;
CSS_CPRM.TitleKey? titleKey = CSS.DecodeTitleKey(tmpBuf, dvdDecrypt.BusKey);
if(titleKey.HasValue)
2024-05-01 04:39:38 +01:00
outputFormat.WriteSectorTag([titleKey.Value.CMI], i + j, SectorTagType.DvdSectorCmi);
else
continue;
// According to libdvdcss, if the key is all zeroes, the sector is actually
// not encrypted even if the CMI says it is.
if(titleKey.Value.Key.All(static k => k == 0))
{
2024-05-01 04:39:38 +01:00
outputFormat.WriteSectorTag([0, 0, 0, 0, 0], i + j, SectorTagType.DvdSectorTitleKey);
outputFormat.WriteSectorTag([0, 0, 0, 0, 0], i + j, SectorTagType.DvdTitleKeyDecrypted);
2021-01-14 00:03:37 +01:00
_resume.MissingTitleKeys.Remove(i + j);
continue;
}
outputFormat.WriteSectorTag(titleKey.Value.Key, i + j, SectorTagType.DvdSectorTitleKey);
_resume.MissingTitleKeys.Remove(i + j);
2023-07-29 23:50:37 +02:00
CSS.DecryptTitleKey(discKey, titleKey.Value.Key, out tmpBuf);
outputFormat.WriteSectorTag(tmpBuf, i + j, SectorTagType.DvdTitleKeyDecrypted);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(!_storeEncrypted)
2022-03-06 13:29:38 +00:00
// Todo: Flag in the outputFormat that a sector has been decrypted
{
ErrorNumber errno =
outputFormat.ReadSectorsTag(i, blocksToRead, SectorTagType.DvdSectorCmi, out byte[] cmi);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
ErrorMessage?.Invoke(string.Format(Localization.Core.Error_retrieving_CMI_for_sector_0, i));
2022-03-06 13:29:38 +00:00
else
{
2024-05-01 04:05:22 +01:00
errno = outputFormat.ReadSectorsTag(i,
blocksToRead,
SectorTagType.DvdTitleKeyDecrypted,
2022-03-06 13:29:38 +00:00
out byte[] titleKey);
if(errno != ErrorNumber.NoError)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
ErrorMessage?.Invoke(string.Format(Localization.Core
.Error_retrieving_title_key_for_sector_0,
i));
2023-10-03 22:57:50 +01:00
}
else
2023-07-29 23:50:37 +02:00
buffer = CSS.DecryptSector(buffer, titleKey, cmi, blocksToRead, blockSize);
}
2021-01-14 00:03:37 +01:00
}
}
2022-03-06 13:29:38 +00:00
2022-12-05 22:03:12 +00:00
mhddLog.Write(i, cmdDuration, blocksToRead);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, currentSpeed * 1024);
_writeStopwatch.Restart();
2022-03-06 13:29:38 +00:00
outputFormat.WriteSectors(buffer, i, blocksToRead);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
extents.Add(i, blocksToRead, true);
2022-12-05 20:11:59 +00:00
_mediaGraph?.PaintSectorsGood(i, blocksToRead);
2022-03-06 13:29:38 +00:00
}
else
{
if(_dev.Manufacturer.Equals("insite", StringComparison.InvariantCultureIgnoreCase))
{
2022-03-06 13:29:38 +00:00
_resume.BadBlocks.Add(i);
_resume.BadBlocks = _resume.BadBlocks.Distinct().ToList();
_resume.NextBlock++;
_aborted = true;
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(Localization.Core
.INSITE_floptical_drives_get_crazy_on_the_SCSI_bus_when_an_error_is_found);
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
// TODO: Reset device after X errors
2024-05-01 04:05:22 +01:00
if(_stopOnError) return; // TODO: Return more cleanly
2024-05-01 04:05:22 +01:00
if(i + _skip > blocks) _skip = (uint)(blocks - i);
2022-03-06 13:29:38 +00:00
// Write empty data
_writeStopwatch.Restart();
2022-03-06 13:29:38 +00:00
outputFormat.WriteSectors(new byte[blockSize * _skip], i, _skip);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2024-05-01 04:05:22 +01:00
for(ulong b = i; b < i + _skip; b++) _resume.BadBlocks.Add(b);
2022-12-05 22:03:12 +00:00
mhddLog.Write(i, cmdDuration < 500 ? 65535 : cmdDuration, _skip);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, 0);
AaruLogging.WriteLine(Localization.Core.Skipping_0_blocks_from_errored_block_1, _skip, i);
2022-03-06 13:29:38 +00:00
i += _skip - blocksToRead;
newTrim = true;
}
_writeStopwatch.Stop();
2022-03-06 13:29:38 +00:00
sectorSpeedStart += blocksToRead;
_resume.NextBlock = i + blocksToRead;
double elapsed = _speedStopwatch.Elapsed.TotalSeconds;
2024-05-01 04:05:22 +01:00
if(elapsed <= 0 || sectorSpeedStart * blockSize < 524288) continue;
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
}
_speedStopwatch.Stop();
2022-03-06 13:29:38 +00:00
_resume.BadBlocks = _resume.BadBlocks.Distinct().ToList();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
}
}