Files
Aaru/Aaru.Core/Devices/Dumping/CompactDisc/Trim.cs

481 lines
22 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
2020-03-11 21:56:55 +00:00
// Filename : Trim.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
2020-03-11 21:56:55 +00:00
// Component : CompactDisc dumping.
//
// --[ Description ] ----------------------------------------------------------
//
2020-03-11 21:56:55 +00:00
// Trims skipped sectors when dumping CompactDiscs.
//
// --[ 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
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable JoinDeclarationAndInitializer
// ReSharper disable InlineOutVariableDeclaration
// ReSharper disable TooWideLocalVariableScope
using System;
using System.Collections.Generic;
using System.Linq;
using Aaru.Checksums;
using Aaru.CommonTypes.AaruMetadata;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Extents;
using Aaru.CommonTypes.Interfaces;
using Aaru.Core.Logging;
using Aaru.Decoders.CD;
using Aaru.Decoders.SCSI;
2020-02-27 00:33:26 +00:00
using Aaru.Devices;
2023-09-26 03:39:10 +01:00
using Humanizer;
using Humanizer.Localisation;
using Track = Aaru.CommonTypes.Structs.Track;
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>Trims errored sectors in a CompactDisc</summary>
/// <param name="audioExtents">Extents with audio sectors</param>
/// <param name="blockSize">Size of the read sector in bytes</param>
/// <param name="currentTry">Current dump hardware try</param>
/// <param name="extents">Extents</param>
/// <param name="newTrim">Is trim a new one?</param>
/// <param name="offsetBytes">Read offset</param>
/// <param name="read6">Device supports READ(6)</param>
/// <param name="read10">Device supports READ(10)</param>
/// <param name="read12">Device supports READ(12)</param>
/// <param name="read16">Device supports READ(16)</param>
/// <param name="readcd">Device supports READ CD</param>
/// <param name="sectorsForOffset">Sectors needed to fix offset</param>
/// <param name="subSize">Subchannel size in bytes</param>
/// <param name="supportedSubchannel">Drive's maximum supported subchannel</param>
/// <param name="supportsLongSectors">Supports reading EDC and ECC</param>
/// <param name="totalDuration">Total commands duration</param>
/// <param name="tracks">Disc tracks</param>
/// <param name="subLog">Subchannel log</param>
/// <param name="desiredSubchannel">Subchannel desired to save</param>
/// <param name="isrcs">List of disc ISRCs</param>
/// <param name="mcn">Disc media catalogue number</param>
/// <param name="subchannelExtents">List of subchannels not yet dumped correctly</param>
/// <param name="smallestPregapLbaPerTrack">List of smallest pregap relative address per track</param>
void TrimCdUserData(ExtentsULong audioExtents, uint blockSize, DumpHardware currentTry, ExtentsULong extents,
2022-03-07 07:36:44 +00:00
bool newTrim, int offsetBytes, bool read6, bool read10, bool read12, bool read16, bool readcd,
int sectorsForOffset, uint subSize, MmcSubchannel supportedSubchannel, bool supportsLongSectors,
ref double totalDuration, SubchannelLog subLog, MmcSubchannel desiredSubchannel, Track[] tracks,
2022-03-06 13:29:38 +00:00
Dictionary<byte, string> isrcs, ref string mcn, HashSet<int> subchannelExtents,
Dictionary<byte, int> smallestPregapLbaPerTrack)
{
bool sense = true; // Sense indicator
2022-03-06 13:29:38 +00:00
byte[] cmdBuf = null; // Data buffer
double cmdDuration = 0; // Command execution time
const uint sectorSize = 2352; // Full sector size
PlextorSubchannel supportedPlextorSubchannel;
byte[] senseBuf = null;
var outputOptical = _outputPlugin as IWritableOpticalImage;
2022-11-13 19:59:24 +00:00
supportedPlextorSubchannel = supportedSubchannel switch
2023-10-03 22:57:50 +01:00
{
MmcSubchannel.None => PlextorSubchannel.None,
MmcSubchannel.Raw => PlextorSubchannel.Pack,
MmcSubchannel.Q16 => PlextorSubchannel.Q16,
_ => PlextorSubchannel.None
};
2024-05-01 04:05:22 +01:00
if(_resume.BadBlocks.Count <= 0 || _aborted || !_trim || !newTrim) return;
UpdateStatus?.Invoke(Localization.Core.Trimming_skipped_sectors);
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
_trimStopwatch.Restart();
2023-10-03 22:57:50 +01:00
trimStart:
2022-03-06 13:29:38 +00:00
ulong[] tmpArray = _resume.BadBlocks.ToArray();
for(int b = 0; b < tmpArray.Length; b++)
2022-03-06 13:29:38 +00:00
{
ulong badSector = tmpArray[b];
2022-03-06 13:29:38 +00:00
if(_aborted)
{
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
UpdateStatus?.Invoke(Localization.Core.Aborted);
2022-03-06 13:29:38 +00:00
break;
}
PulseProgress?.Invoke(string.Format(Localization.Core.Trimming_sector_0, badSector));
2022-03-07 07:36:44 +00:00
Track track = tracks.OrderBy(t => t.StartSector).LastOrDefault(t => badSector >= t.StartSector);
2022-03-06 13:29:38 +00:00
byte sectorsToTrim = 1;
uint badSectorToRead = (uint)badSector;
if(_fixOffset && audioExtents.Contains(badSector) && offsetBytes != 0)
2022-03-06 13:29:38 +00:00
{
if(offsetBytes < 0)
{
if(badSectorToRead == 0)
badSectorToRead = uint.MaxValue - (uint)(sectorsForOffset - 1); // -1
else
badSectorToRead -= (uint)sectorsForOffset;
}
sectorsToTrim += (byte)sectorsForOffset;
2022-03-06 13:29:38 +00:00
}
if(_supportsPlextorD8 && audioExtents.Contains(badSector))
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
sense = ReadPlextorWithSubchannel(out cmdBuf,
out senseBuf,
badSectorToRead,
blockSize,
sectorsToTrim,
supportedPlextorSubchannel,
out cmdDuration);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
else if(readcd)
{
if(audioExtents.Contains(badSector))
{
2024-05-01 04:05:22 +01:00
sense = _dev.ReadCd(out cmdBuf,
out senseBuf,
badSectorToRead,
blockSize,
sectorsToTrim,
MmcSectorTypes.Cdda,
false,
false,
false,
MmcHeaderCodes.None,
true,
false,
MmcErrorField.None,
supportedSubchannel,
_dev.Timeout,
out cmdDuration);
2022-03-06 13:29:38 +00:00
if(sense)
{
2022-03-06 13:29:38 +00:00
DecodedSense? decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
// Try to workaround firmware
if(decSense is { ASC: 0x11, ASCQ: 0x05 } || decSense?.ASC == 0x64)
{
2024-05-01 04:05:22 +01:00
sense = _dev.ReadCd(out cmdBuf,
out _,
badSectorToRead,
blockSize,
sectorsToTrim,
MmcSectorTypes.AllTypes,
false,
false,
true,
MmcHeaderCodes.AllHeaders,
true,
true,
MmcErrorField.None,
supportedSubchannel,
_dev.Timeout,
2022-03-07 07:36:44 +00:00
out double cmdDuration2);
2022-03-06 13:29:38 +00:00
cmdDuration += cmdDuration2;
}
}
2022-03-06 13:29:38 +00:00
}
else
{
2024-05-01 04:05:22 +01:00
sense = _dev.ReadCd(out cmdBuf,
out senseBuf,
badSectorToRead,
blockSize,
sectorsToTrim,
MmcSectorTypes.AllTypes,
false,
false,
true,
MmcHeaderCodes.AllHeaders,
true,
true,
MmcErrorField.None,
supportedSubchannel,
_dev.Timeout,
out cmdDuration);
2022-03-06 13:29:38 +00:00
if(sense)
{
2022-03-06 13:29:38 +00:00
DecodedSense? decSense = Sense.Decode(senseBuf);
2022-03-06 13:29:38 +00:00
// Try to workaround firmware
if(decSense is { ASC: 0x11, ASCQ: 0x05 } || decSense?.ASC == 0x64)
{
byte scrambledSectorsToTrim = sectorsToTrim;
uint scrambledBadSectorToRead = badSectorToRead;
// Contrary to normal read, this must always be offset fixed, because it's data not audio
if(offsetBytes != 0)
{
if(offsetBytes < 0)
{
if(scrambledBadSectorToRead == 0)
scrambledBadSectorToRead = uint.MaxValue - (uint)(sectorsForOffset - 1); // -1
else
scrambledBadSectorToRead -= (uint)sectorsForOffset;
}
scrambledSectorsToTrim += (byte)sectorsForOffset;
}
2024-05-01 04:05:22 +01:00
sense = _dev.ReadCd(out cmdBuf,
out _,
scrambledBadSectorToRead,
blockSize,
scrambledSectorsToTrim,
MmcSectorTypes.Cdda,
false,
false,
false,
MmcHeaderCodes.None,
true,
false,
MmcErrorField.None,
supportedSubchannel,
_dev.Timeout,
out double cmdDuration2);
2022-03-06 13:29:38 +00:00
cmdDuration += cmdDuration2;
if(!sense)
{
uint scrambledBlocksToRead = scrambledSectorsToTrim;
2024-05-01 04:05:22 +01:00
FixOffsetData(offsetBytes,
sectorSize,
sectorsForOffset,
supportedSubchannel,
ref scrambledBlocksToRead,
subSize,
ref cmdBuf,
blockSize,
false);
// Descramble
cmdBuf = Sector.Scramble(cmdBuf);
// Check valid sector
2024-05-01 04:05:22 +01:00
CdChecksums.CheckCdSector(cmdBuf,
out bool? correctEccP,
out bool? correctEccQ,
out bool? correctEdc);
// Check mode, set sense if EDC/ECC validity is not correct
switch(cmdBuf[15] & 0x03)
{
case 0:
for(int c = 16; c < 2352; c++)
{
2024-05-01 04:05:22 +01:00
if(cmdBuf[c] == 0x00) continue;
sense = true;
break;
}
break;
case 1:
sense = correctEdc != true || correctEccP != true || correctEccQ != true;
break;
case 2:
if((cmdBuf[18] & 0x20) != 0x20)
{
2024-05-01 04:05:22 +01:00
if(correctEccP != true) sense = true;
2024-05-01 04:05:22 +01:00
if(correctEccQ != true) sense = true;
}
2024-05-01 04:05:22 +01:00
if(correctEdc != true) sense = true;
break;
}
}
}
}
}
totalDuration += cmdDuration;
2022-03-06 13:29:38 +00:00
}
else if(read16)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
sense = _dev.Read16(out cmdBuf,
out senseBuf,
0,
false,
true,
false,
badSectorToRead,
blockSize,
0,
sectorsToTrim,
false,
_dev.Timeout,
out cmdDuration);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
else if(read12)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
sense = _dev.Read12(out cmdBuf,
out senseBuf,
0,
false,
true,
false,
false,
badSectorToRead,
blockSize,
0,
sectorsToTrim,
false,
_dev.Timeout,
out cmdDuration);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
else if(read10)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
sense = _dev.Read10(out cmdBuf,
out senseBuf,
0,
false,
true,
false,
false,
badSectorToRead,
blockSize,
0,
sectorsToTrim,
_dev.Timeout,
out cmdDuration);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
else if(read6)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
sense = _dev.Read6(out cmdBuf,
out senseBuf,
badSectorToRead,
blockSize,
sectorsToTrim,
_dev.Timeout,
2022-03-07 07:36:44 +00:00
out cmdDuration);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
totalDuration += cmdDuration;
if(sense || _dev.Error)
{
_errorLog?.WriteLine(badSectorToRead, _dev.Error, _dev.LastError, senseBuf);
2022-03-06 13:29:38 +00:00
continue;
}
if(!sense && !_dev.Error)
2022-03-06 13:29:38 +00:00
{
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
2022-12-05 20:11:59 +00:00
_mediaGraph?.PaintSectorGood(badSector);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
// Because one block has been partially used to fix the offset
if(_fixOffset && audioExtents.Contains(badSector) && offsetBytes != 0)
2022-03-06 13:29:38 +00:00
{
uint blocksToRead = sectorsToTrim;
2024-05-01 04:05:22 +01:00
FixOffsetData(offsetBytes,
sectorSize,
sectorsForOffset,
supportedSubchannel,
ref blocksToRead,
subSize,
ref cmdBuf,
blockSize,
false);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(supportedSubchannel != MmcSubchannel.None)
{
byte[] data = new byte[sectorSize];
byte[] sub = new byte[subSize];
2023-10-03 22:57:50 +01:00
Array.Copy(cmdBuf, 0, data, 0, sectorSize);
Array.Copy(cmdBuf, sectorSize, sub, 0, subSize);
2022-03-06 13:29:38 +00:00
if(supportsLongSectors)
outputOptical.WriteSectorLong(data, badSector);
else
outputOptical.WriteSector(Sector.GetUserData(data), badSector);
2022-03-06 13:29:38 +00:00
ulong trkStartBefore = track.StartSector;
2020-05-05 15:46:32 +01:00
2024-05-01 04:05:22 +01:00
bool indexesChanged = Media.CompactDisc.WriteSubchannelToImage(supportedSubchannel,
desiredSubchannel,
sub,
badSector,
1,
subLog,
isrcs,
(byte)track.Sequence,
ref mcn,
tracks,
2022-03-07 07:36:44 +00:00
subchannelExtents,
2024-05-01 04:05:22 +01:00
_fixSubchannelPosition,
outputOptical,
_fixSubchannel,
_fixSubchannelCrc,
UpdateStatus,
smallestPregapLbaPerTrack,
true,
out _);
2022-03-06 13:29:38 +00:00
// Set tracks and go back
2024-05-01 04:05:22 +01:00
if(!indexesChanged) continue;
2022-03-07 07:36:44 +00:00
outputOptical.SetTracks(tracks.ToList());
if(track.StartSector != trkStartBefore && !_resume.BadBlocks.Contains(track.StartSector))
2022-03-06 13:29:38 +00:00
{
_resume.BadBlocks.Add(track.StartSector);
2022-03-06 13:29:38 +00:00
goto trimStart;
}
2022-03-06 13:29:38 +00:00
b--;
continue;
}
2022-03-06 13:29:38 +00:00
if(supportsLongSectors)
outputOptical.WriteSectorLong(cmdBuf, badSector);
else
outputOptical.WriteSector(Sector.GetUserData(cmdBuf), badSector);
}
2022-03-06 13:29:38 +00:00
_trimStopwatch.Stop();
2022-03-06 13:29:38 +00:00
EndProgress?.Invoke();
2023-09-26 03:39:10 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core.Trimming_finished_in_0,
_trimStopwatch.Elapsed.Humanize(minUnit: TimeUnit.Second)));
}
}