2020-01-01 18:44:27 +00:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2020-01-01 18:44:27 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
2020-03-11 21:56:55 +00:00
|
|
|
// Filename : Trim.cs
|
2020-01-01 18:44:27 +00:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2020-03-11 21:56:55 +00:00
|
|
|
// Component : CompactDisc dumping.
|
2020-01-01 18:44:27 +00:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2020-03-11 21:56:55 +00:00
|
|
|
// Trims skipped sectors when dumping CompactDiscs.
|
2020-01-01 18:44:27 +00:00
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2020-01-01 18:44:27 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-05-05 20:26:18 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Extents;
|
2020-05-05 23:25:15 +01:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2020-05-05 20:26:18 +01:00
|
|
|
using Aaru.CommonTypes.Structs;
|
2020-04-27 02:29:49 +01:00
|
|
|
using Aaru.Core.Logging;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Devices;
|
2020-01-01 18:44:27 +00:00
|
|
|
using Schemas;
|
|
|
|
|
|
|
|
|
|
// ReSharper disable JoinDeclarationAndInitializer
|
|
|
|
|
// ReSharper disable InlineOutVariableDeclaration
|
|
|
|
|
// ReSharper disable TooWideLocalVariableScope
|
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
namespace Aaru.Core.Devices.Dumping
|
2020-01-01 18:44:27 +00:00
|
|
|
{
|
|
|
|
|
partial class Dump
|
|
|
|
|
{
|
|
|
|
|
void TrimCdUserData(ExtentsULong audioExtents, uint blockSize, DumpHardwareType currentTry,
|
|
|
|
|
ExtentsULong extents, bool newTrim, int offsetBytes, bool read6, bool read10, bool read12,
|
|
|
|
|
bool read16, bool readcd, int sectorsForOffset, uint subSize,
|
2020-04-27 02:29:49 +01:00
|
|
|
MmcSubchannel supportedSubchannel, bool supportsLongSectors, ref double totalDuration,
|
2020-05-05 20:26:18 +01:00
|
|
|
SubchannelLog subLog, MmcSubchannel desiredSubchannel, Track[] tracks,
|
2020-07-18 20:43:37 +01:00
|
|
|
Dictionary<byte, string> isrcs, ref string mcn, HashSet<int> subchannelExtents,
|
|
|
|
|
Dictionary<byte, int> smallestPregapLbaPerTrack)
|
2020-01-01 18:44:27 +00:00
|
|
|
{
|
2020-03-02 23:58:20 +00:00
|
|
|
DateTime start;
|
|
|
|
|
DateTime end;
|
|
|
|
|
bool sense = true; // Sense indicator
|
|
|
|
|
byte[] cmdBuf = null; // Data buffer
|
|
|
|
|
double cmdDuration = 0; // Command execution time
|
|
|
|
|
const uint sectorSize = 2352; // Full sector size
|
|
|
|
|
PlextorSubchannel supportedPlextorSubchannel;
|
2020-07-13 18:54:41 +01:00
|
|
|
byte[] senseBuf = null;
|
2020-03-02 23:58:20 +00:00
|
|
|
|
|
|
|
|
switch(supportedSubchannel)
|
|
|
|
|
{
|
|
|
|
|
case MmcSubchannel.None:
|
|
|
|
|
supportedPlextorSubchannel = PlextorSubchannel.None;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case MmcSubchannel.Raw:
|
|
|
|
|
supportedPlextorSubchannel = PlextorSubchannel.All;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case MmcSubchannel.Q16:
|
|
|
|
|
supportedPlextorSubchannel = PlextorSubchannel.Q16;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case MmcSubchannel.Rw:
|
|
|
|
|
supportedPlextorSubchannel = PlextorSubchannel.Pack;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
supportedPlextorSubchannel = PlextorSubchannel.None;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-01 18:44:27 +00:00
|
|
|
|
|
|
|
|
if(_resume.BadBlocks.Count <= 0 ||
|
|
|
|
|
_aborted ||
|
2020-01-04 02:28:17 +00:00
|
|
|
!_trim ||
|
2020-01-01 18:44:27 +00:00
|
|
|
!newTrim)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
start = DateTime.UtcNow;
|
2020-03-11 16:01:33 +00:00
|
|
|
UpdateStatus?.Invoke("Trimming skipped sectors");
|
|
|
|
|
_dumpLog.WriteLine("Trimming skipped sectors");
|
2020-01-01 18:44:27 +00:00
|
|
|
|
|
|
|
|
ulong[] tmpArray = _resume.BadBlocks.ToArray();
|
|
|
|
|
InitProgress?.Invoke();
|
|
|
|
|
|
2020-05-05 23:25:15 +01:00
|
|
|
for(int b = 0; b < tmpArray.Length; b++)
|
2020-01-01 18:44:27 +00:00
|
|
|
{
|
2020-05-05 23:25:15 +01:00
|
|
|
ulong badSector = tmpArray[b];
|
|
|
|
|
|
2020-01-01 18:44:27 +00:00
|
|
|
if(_aborted)
|
|
|
|
|
{
|
|
|
|
|
currentTry.Extents = ExtentsConverter.ToMetadata(extents);
|
|
|
|
|
UpdateStatus?.Invoke("Aborted!");
|
|
|
|
|
_dumpLog.WriteLine("Aborted!");
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PulseProgress?.Invoke($"Trimming sector {badSector}");
|
|
|
|
|
|
2020-05-05 20:26:18 +01:00
|
|
|
Track track = tracks.OrderBy(t => t.TrackStartSector).
|
|
|
|
|
LastOrDefault(t => badSector >= t.TrackStartSector);
|
|
|
|
|
|
2020-01-01 18:44:27 +00:00
|
|
|
byte sectorsToTrim = 1;
|
|
|
|
|
uint badSectorToRead = (uint)badSector;
|
|
|
|
|
|
|
|
|
|
if(_fixOffset &&
|
|
|
|
|
audioExtents.Contains(badSector) &&
|
|
|
|
|
offsetBytes != 0)
|
|
|
|
|
{
|
|
|
|
|
if(offsetBytes > 0)
|
|
|
|
|
{
|
|
|
|
|
badSectorToRead -= (uint)sectorsForOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sectorsToTrim = (byte)(sectorsForOffset + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:58:20 +00:00
|
|
|
if(_supportsPlextorD8 && audioExtents.Contains(badSector))
|
2020-07-13 18:54:41 +01:00
|
|
|
sense = ReadPlextorWithSubchannel(out cmdBuf, out senseBuf, badSectorToRead, blockSize,
|
|
|
|
|
sectorsToTrim, supportedPlextorSubchannel, out cmdDuration);
|
2020-07-14 15:04:20 +01:00
|
|
|
else if(readcd && audioExtents.Contains(badSector))
|
|
|
|
|
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);
|
2020-03-02 23:58:20 +00:00
|
|
|
else if(readcd)
|
2020-07-13 18:54:41 +01:00
|
|
|
sense = _dev.ReadCd(out cmdBuf, out senseBuf, badSectorToRead, blockSize, sectorsToTrim,
|
2020-01-01 18:44:27 +00:00
|
|
|
MmcSectorTypes.AllTypes, false, false, true, MmcHeaderCodes.AllHeaders, true,
|
|
|
|
|
true, MmcErrorField.None, supportedSubchannel, _dev.Timeout, out cmdDuration);
|
|
|
|
|
else if(read16)
|
2020-07-13 18:54:41 +01:00
|
|
|
sense = _dev.Read16(out cmdBuf, out senseBuf, 0, false, true, false, badSectorToRead, blockSize, 0,
|
2020-01-01 18:44:27 +00:00
|
|
|
sectorsToTrim, false, _dev.Timeout, out cmdDuration);
|
|
|
|
|
else if(read12)
|
2020-07-13 18:54:41 +01:00
|
|
|
sense = _dev.Read12(out cmdBuf, out senseBuf, 0, false, true, false, false, badSectorToRead,
|
|
|
|
|
blockSize, 0, sectorsToTrim, false, _dev.Timeout, out cmdDuration);
|
2020-01-01 18:44:27 +00:00
|
|
|
else if(read10)
|
2020-07-13 18:54:41 +01:00
|
|
|
sense = _dev.Read10(out cmdBuf, out senseBuf, 0, false, true, false, false, badSectorToRead,
|
|
|
|
|
blockSize, 0, sectorsToTrim, _dev.Timeout, out cmdDuration);
|
2020-01-01 18:44:27 +00:00
|
|
|
else if(read6)
|
2020-07-13 18:54:41 +01:00
|
|
|
sense = _dev.Read6(out cmdBuf, out senseBuf, badSectorToRead, blockSize, sectorsToTrim,
|
|
|
|
|
_dev.Timeout, out cmdDuration);
|
2020-01-01 18:44:27 +00:00
|
|
|
|
|
|
|
|
totalDuration += cmdDuration;
|
|
|
|
|
|
|
|
|
|
if(sense || _dev.Error)
|
2020-07-13 18:54:41 +01:00
|
|
|
{
|
|
|
|
|
_errorLog?.WriteLine(badSectorToRead, _dev.Error, _dev.LastError, senseBuf);
|
|
|
|
|
|
2020-01-01 18:44:27 +00:00
|
|
|
continue;
|
2020-07-13 18:54:41 +01:00
|
|
|
}
|
2020-01-01 18:44:27 +00:00
|
|
|
|
|
|
|
|
if(!sense &&
|
|
|
|
|
!_dev.Error)
|
|
|
|
|
{
|
|
|
|
|
_resume.BadBlocks.Remove(badSector);
|
|
|
|
|
extents.Add(badSector);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Because one block has been partially used to fix the offset
|
|
|
|
|
if(_fixOffset &&
|
|
|
|
|
audioExtents.Contains(badSector) &&
|
|
|
|
|
offsetBytes != 0)
|
|
|
|
|
{
|
2020-03-06 18:49:45 +00:00
|
|
|
uint blocksToRead = sectorsToTrim;
|
2020-01-01 18:44:27 +00:00
|
|
|
|
2020-03-06 18:49:45 +00:00
|
|
|
FixOffsetData(offsetBytes, sectorSize, sectorsForOffset, supportedSubchannel, ref blocksToRead,
|
|
|
|
|
subSize, ref cmdBuf, blockSize, false);
|
2020-01-01 18:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(supportedSubchannel != MmcSubchannel.None)
|
|
|
|
|
{
|
|
|
|
|
byte[] data = new byte[sectorSize];
|
|
|
|
|
byte[] sub = new byte[subSize];
|
|
|
|
|
Array.Copy(cmdBuf, 0, data, 0, sectorSize);
|
|
|
|
|
Array.Copy(cmdBuf, sectorSize, sub, 0, subSize);
|
|
|
|
|
_outputPlugin.WriteSectorLong(data, badSector);
|
2020-05-05 13:28:34 +01:00
|
|
|
|
2020-07-14 01:06:23 +01:00
|
|
|
bool indexesChanged = Media.CompactDisc.WriteSubchannelToImage(supportedSubchannel,
|
2020-07-14 15:04:20 +01:00
|
|
|
desiredSubchannel, sub, badSector, 1,
|
|
|
|
|
subLog, isrcs,
|
|
|
|
|
(byte)track.TrackSequence, ref mcn,
|
|
|
|
|
tracks, subchannelExtents,
|
|
|
|
|
_fixSubchannelPosition,
|
|
|
|
|
_outputPlugin, _fixSubchannel,
|
|
|
|
|
_fixSubchannelCrc, _dumpLog,
|
2020-07-18 20:43:37 +01:00
|
|
|
UpdateStatus,
|
|
|
|
|
smallestPregapLbaPerTrack);
|
2020-05-05 20:26:18 +01:00
|
|
|
|
2020-05-05 23:25:15 +01:00
|
|
|
// Set tracks and go back
|
|
|
|
|
if(!indexesChanged)
|
|
|
|
|
continue;
|
2020-05-05 15:46:32 +01:00
|
|
|
|
2020-05-05 23:25:15 +01:00
|
|
|
(_outputPlugin as IWritableOpticalImage).SetTracks(tracks.ToList());
|
|
|
|
|
b--;
|
2020-05-05 13:28:34 +01:00
|
|
|
|
2020-05-05 23:25:15 +01:00
|
|
|
continue;
|
2020-01-01 18:44:27 +00:00
|
|
|
}
|
2020-05-05 23:25:15 +01:00
|
|
|
|
|
|
|
|
if(supportsLongSectors)
|
|
|
|
|
_outputPlugin.WriteSectorLong(cmdBuf, badSector);
|
2020-01-01 18:44:27 +00:00
|
|
|
else
|
|
|
|
|
{
|
2020-05-05 23:25:15 +01:00
|
|
|
if(cmdBuf.Length % sectorSize == 0)
|
2020-01-01 18:44:27 +00:00
|
|
|
{
|
2020-05-05 23:25:15 +01:00
|
|
|
byte[] data = new byte[2048];
|
|
|
|
|
Array.Copy(cmdBuf, 16, data, 0, 2048);
|
|
|
|
|
|
|
|
|
|
_outputPlugin.WriteSector(data, badSector);
|
2020-01-01 18:44:27 +00:00
|
|
|
}
|
2020-05-05 23:25:15 +01:00
|
|
|
else
|
|
|
|
|
_outputPlugin.WriteSectorLong(cmdBuf, badSector);
|
2020-01-01 18:44:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EndProgress?.Invoke();
|
|
|
|
|
end = DateTime.UtcNow;
|
|
|
|
|
UpdateStatus?.Invoke($"Trimming finished in {(end - start).TotalSeconds} seconds.");
|
|
|
|
|
_dumpLog.WriteLine("Trimming finished in {0} seconds.", (end - start).TotalSeconds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|