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

496 lines
20 KiB
C#
Raw Normal View History

// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Data.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CompactDisc dumping.
//
// --[ Description ] ----------------------------------------------------------
//
// Dumps user data part.
//
// --[ 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.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Extents;
using Aaru.CommonTypes.Interfaces;
using Aaru.Core.Logging;
using Aaru.Decoders.CD;
using Aaru.Devices;
2023-09-26 02:40:11 +01:00
using Humanizer;
using Humanizer.Bytes;
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>Detects if a sector contains data</summary>
/// <param name="sector">Sector contents</param>
/// <returns><c>true</c> if it contains Yellow Book data, <c>false</c> otherwise</returns>
static bool IsData(byte[] sector)
{
2024-05-01 04:05:22 +01:00
if(sector?.Length != 2352) return false;
2022-03-06 13:29:38 +00:00
2024-05-01 04:39:38 +01:00
byte[] syncMark = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00];
byte[] testMark = new byte[12];
2022-03-06 13:29:38 +00:00
Array.Copy(sector, 0, testMark, 0, 12);
2022-03-06 13:29:38 +00:00
return syncMark.SequenceEqual(testMark) && (sector[0xF] == 0 || sector[0xF] == 1 || sector[0xF] == 2);
}
2022-03-06 13:29:38 +00:00
/// <summary>Detects if a sector contains scrambled data</summary>
/// <param name="sector">Sector contents</param>
/// <param name="wantedLba">What LBA we intended to read</param>
/// <param name="offset">Offset in bytes, if found</param>
/// <returns><c>true</c> if it contains Yellow Book data, <c>false</c> otherwise</returns>
static bool IsScrambledData(byte[] sector, int wantedLba, out int? offset)
{
offset = 0;
2024-05-01 04:05:22 +01:00
if(sector?.Length != 2352) return false;
2022-03-06 13:29:38 +00:00
2024-05-01 04:39:38 +01:00
byte[] syncMark = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00];
byte[] testMark = new byte[12];
for(int i = 0; i <= 2336; i++)
2022-03-06 13:29:38 +00:00
{
Array.Copy(sector, i, testMark, 0, 12);
2022-03-06 13:29:38 +00:00
if(!syncMark.SequenceEqual(testMark) ||
2023-10-03 22:57:50 +01:00
sector[i + 0xF] != 0x60 && sector[i + 0xF] != 0x61 && sector[i + 0xF] != 0x62)
2022-03-06 13:29:38 +00:00
continue;
2022-03-06 13:29:38 +00:00
// De-scramble M and S
int minute = sector[i + 12] ^ 0x01;
int second = sector[i + 13] ^ 0x80;
int frame = sector[i + 14];
2022-03-06 13:29:38 +00:00
// Convert to binary
2023-10-03 22:57:50 +01:00
minute = minute / 16 * 10 + (minute & 0x0F);
second = second / 16 * 10 + (second & 0x0F);
frame = frame / 16 * 10 + (frame & 0x0F);
2022-03-06 13:29:38 +00:00
// Calculate the first found LBA
2023-10-03 22:57:50 +01:00
int lba = minute * 60 * 75 + second * 75 + frame - 150;
2022-03-06 13:29:38 +00:00
// Calculate the difference between the found LBA and the requested one
int diff = wantedLba - lba;
2023-10-03 22:57:50 +01:00
offset = i + 2352 * diff;
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
// TODO: Set pregap for Track 1
// TODO: Detect errors in sectors
/// <summary>Reads all the hidden track in CD-i Ready discs</summary>
/// <param name="blocks">Total number of positive sectors</param>
/// <param name="blockSize">Size of the read sector in bytes</param>
/// <param name="currentSpeed">Current read speed</param>
/// <param name="currentTry">Current dump hardware try</param>
/// <param name="extents">Extents</param>
/// <param name="ibgLog">IMGBurn log</param>
/// <param name="imageWriteDuration">Duration of image write</param>
/// <param name="leadOutExtents">Lead-out extents</param>
/// <param name="maxSpeed">Maximum speed</param>
/// <param name="mhddLog">MHDD log</param>
/// <param name="minSpeed">Minimum speed</param>
/// <param name="offsetBytes">Read offset</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="totalDuration">Total commands duration</param>
/// <param name="cdiReadyReadAsAudio">Is the drive returning CD-i Ready hidden track as audio?</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 ReadCdiReady(uint blockSize, ref double currentSpeed, DumpHardware currentTry, ExtentsULong extents,
2022-03-07 07:36:44 +00:00
IbgLog ibgLog, ref double imageWriteDuration, ExtentsULong leadOutExtents, ref double maxSpeed,
MhddLog mhddLog, ref double minSpeed, uint subSize, MmcSubchannel supportedSubchannel,
ref double totalDuration, Track[] tracks, SubchannelLog subLog, MmcSubchannel desiredSubchannel,
Dictionary<byte, string> isrcs, ref string mcn, HashSet<int> subchannelExtents, ulong blocks,
bool cdiReadyReadAsAudio, int offsetBytes, int sectorsForOffset,
Dictionary<byte, int> smallestPregapLbaPerTrack)
2022-03-06 13:29:38 +00:00
{
2025-08-22 19:57:09 +01:00
ulong sectorSpeedStart = 0; // Used to calculate correct speed
bool sense; // Sense indicator
byte[] cmdBuf; // Data buffer
ReadOnlySpan<byte> senseBuf; // Sense buffer
double cmdDuration; // Command execution time
const uint sectorSize = 2352; // Full sector size
Track firstTrack = tracks.FirstOrDefault();
uint blocksToRead; // How many sectors to read at once
var outputOptical = _outputPlugin as IWritableOpticalImage;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(firstTrack is null) return;
2022-03-06 13:29:38 +00:00
if(cdiReadyReadAsAudio)
{
UpdateStatus?.Invoke(Localization.Core.Setting_speed_to_8x_for_CD_i_Ready_reading_as_audio);
2022-03-06 13:29:38 +00:00
_dev.SetCdSpeed(out _, RotationalControl.ClvAndImpureCav, 1416, 0, _dev.Timeout, out _);
}
2022-03-06 13:29:38 +00:00
InitProgress?.Invoke();
for(ulong i = _resume.NextBlock; i <= firstTrack.EndSector; i += blocksToRead)
2022-03-06 13:29:38 +00:00
{
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;
}
uint firstSectorToRead = (uint)i;
2022-03-06 13:29:38 +00:00
blocksToRead = _maximumReadable;
2024-05-01 04:05:22 +01:00
if(blocksToRead == 1 && cdiReadyReadAsAudio) blocksToRead += (uint)sectorsForOffset;
2022-03-06 13:29:38 +00:00
if(cdiReadyReadAsAudio)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
if(offsetBytes < 0)
{
2022-03-06 13:29:38 +00:00
if(i == 0)
firstSectorToRead = uint.MaxValue - (uint)(sectorsForOffset - 1); // -1
else
firstSectorToRead -= (uint)sectorsForOffset;
}
2023-10-03 22:57:50 +01:00
}
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;
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();
2024-05-01 04:05:22 +01:00
sense = _dev.ReadCd(out cmdBuf,
out senseBuf,
firstSectorToRead,
blockSize,
blocksToRead,
MmcSectorTypes.AllTypes,
false,
false,
true,
MmcHeaderCodes.AllHeaders,
true,
true,
MmcErrorField.None,
supportedSubchannel,
_dev.Timeout,
out cmdDuration);
2022-03-06 13:29:38 +00:00
totalDuration += cmdDuration;
2024-04-26 03:16:36 +01:00
_speedStopwatch.Stop();
2022-03-06 13:29:38 +00:00
double elapsed;
2022-03-06 13:29:38 +00:00
// Overcome the track mode change drive error
if(sense)
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
for(uint r = 0; r < _maximumReadable; r++)
{
2024-05-01 04:05:22 +01:00
UpdateProgress?.Invoke(string.Format(Localization.Core.Reading_sector_0_of_1_2,
i + r,
blocks,
ByteSize.FromMegabytes(currentSpeed)
.Per(_oneSecond)
.Humanize()),
(long)i + r,
(long)blocks);
2024-04-26 03:16:36 +01:00
_speedStopwatch.Start();
2024-05-01 04:05:22 +01:00
sense = _dev.ReadCd(out cmdBuf,
out senseBuf,
(uint)(i + r),
blockSize,
(uint)sectorsForOffset + 1,
MmcSectorTypes.AllTypes,
false,
false,
true,
MmcHeaderCodes.AllHeaders,
true,
true,
MmcErrorField.None,
supportedSubchannel,
_dev.Timeout,
out cmdDuration);
2022-03-06 13:29:38 +00:00
totalDuration += cmdDuration;
2024-04-26 03:16:36 +01:00
_speedStopwatch.Stop();
2024-05-01 04:05:22 +01:00
if(!sense && !_dev.Error)
2022-03-06 13:29:38 +00:00
{
mhddLog.Write(i + r, cmdDuration);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i + r, currentSpeed * 1024);
extents.Add(i + r, 1, true);
_writeStopwatch.Restart();
2022-03-06 13:29:38 +00:00
if(cdiReadyReadAsAudio)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
FixOffsetData(offsetBytes,
sectorSize,
sectorsForOffset,
supportedSubchannel,
ref blocksToRead,
subSize,
ref cmdBuf,
blockSize,
false);
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
if(supportedSubchannel != MmcSubchannel.None)
{
byte[] data = new byte[sectorSize];
byte[] sub = new byte[subSize];
2022-03-06 13:29:38 +00:00
Array.Copy(cmdBuf, 0, data, 0, sectorSize);
2022-03-06 13:29:38 +00:00
Array.Copy(cmdBuf, sectorSize, sub, 0, subSize);
2024-05-01 04:05:22 +01:00
if(cdiReadyReadAsAudio) data = Sector.Scramble(data);
2022-03-06 13:29:38 +00:00
outputOptical.WriteSectorsLong(data, i + r, 1);
2022-03-06 13:29:38 +00:00
bool indexesChanged = Media.CompactDisc.WriteSubchannelToImage(supportedSubchannel,
2024-05-01 04:05:22 +01:00
desiredSubchannel,
sub,
i + r,
1,
subLog,
isrcs,
1,
ref mcn,
tracks,
subchannelExtents,
_fixSubchannelPosition,
outputOptical,
_fixSubchannel,
_fixSubchannelCrc,
UpdateStatus,
smallestPregapLbaPerTrack,
true,
2022-11-15 01:35:06 +00:00
out List<ulong> _);
2022-03-06 13:29:38 +00:00
// Set tracks and go back
if(indexesChanged)
{
2022-03-07 07:36:44 +00:00
outputOptical.SetTracks(tracks.ToList());
2022-03-06 13:29:38 +00:00
i -= _maximumReadable;
2022-03-06 13:29:38 +00:00
continue;
}
}
else
2022-03-06 13:29:38 +00:00
outputOptical.WriteSectorsLong(cmdBuf, i + r, 1);
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-12-05 20:11:59 +00:00
_mediaGraph?.PaintSectorGood(i + r);
2022-03-06 13:29:38 +00:00
}
else
{
2025-08-22 19:57:09 +01:00
_errorLog?.WriteLine(i + r, _dev.Error, _dev.LastError, senseBuf.ToArray());
leadOutExtents.Add(i + r, firstTrack.EndSector);
2024-05-01 04:05:22 +01:00
UpdateStatus?.Invoke(string.Format(Localization.Core
.Adding_CD_i_Ready_hole_from_LBA_0_to_1_inclusive,
i + r,
firstTrack.EndSector));
2022-03-06 13:29:38 +00:00
break;
}
_writeStopwatch.Stop();
2022-03-06 13:29:38 +00:00
sectorSpeedStart += r;
2022-03-06 13:29:38 +00:00
_resume.NextBlock = i + r;
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
}
2023-10-03 22:57:50 +01:00
}
if(!sense && !_dev.Error)
2022-03-06 13:29:38 +00:00
{
if(cdiReadyReadAsAudio)
2023-10-03 22:57:50 +01:00
{
2024-05-01 04:05:22 +01:00
FixOffsetData(offsetBytes,
sectorSize,
sectorsForOffset,
supportedSubchannel,
ref blocksToRead,
subSize,
ref cmdBuf,
blockSize,
false);
2023-10-03 22:57:50 +01:00
}
mhddLog.Write(i, cmdDuration);
2022-03-06 13:29:38 +00:00
ibgLog.Write(i, currentSpeed * 1024);
extents.Add(i, blocksToRead, true);
_writeStopwatch.Restart();
2022-03-06 13:29:38 +00:00
if(supportedSubchannel != MmcSubchannel.None)
{
byte[] data = new byte[sectorSize * blocksToRead];
byte[] sub = new byte[subSize * blocksToRead];
byte[] tmpData = new byte[sectorSize];
for(int b = 0; b < blocksToRead; b++)
{
if(cdiReadyReadAsAudio)
{
2023-10-03 22:57:50 +01:00
Array.Copy(cmdBuf, (int)(0 + b * blockSize), tmpData, 0, sectorSize);
2022-03-06 13:29:38 +00:00
tmpData = Sector.Scramble(tmpData);
Array.Copy(tmpData, 0, data, sectorSize * b, sectorSize);
}
else
2023-10-03 22:57:50 +01:00
Array.Copy(cmdBuf, (int)(0 + b * blockSize), data, sectorSize * b, sectorSize);
2022-03-06 13:29:38 +00:00
2023-10-03 22:57:50 +01:00
Array.Copy(cmdBuf, (int)(sectorSize + b * blockSize), sub, subSize * b, subSize);
}
2022-03-06 13:29:38 +00:00
outputOptical.WriteSectorsLong(data, i, blocksToRead);
bool indexesChanged = Media.CompactDisc.WriteSubchannelToImage(supportedSubchannel,
2024-05-01 04:05:22 +01:00
desiredSubchannel,
sub,
i,
blocksToRead,
subLog,
isrcs,
1,
ref mcn,
tracks,
subchannelExtents,
_fixSubchannelPosition,
outputOptical,
_fixSubchannel,
_fixSubchannelCrc,
UpdateStatus,
smallestPregapLbaPerTrack,
true,
2023-10-03 22:57:50 +01:00
out List<ulong> newPregapSectors);
2022-03-06 13:29:38 +00:00
// Set tracks and go back
if(indexesChanged)
{
2022-03-07 07:36:44 +00:00
outputOptical.SetTracks(tracks.ToList());
2024-05-01 04:05:22 +01:00
foreach(ulong newPregapSector in newPregapSectors) _resume.BadBlocks.Add(newPregapSector);
if(i >= blocksToRead)
i -= blocksToRead;
else
i = 0;
2024-05-01 04:05:22 +01:00
if(i > 0) i--;
2022-03-06 13:29:38 +00:00
continue;
}
}
else
{
2022-03-06 13:29:38 +00:00
if(cdiReadyReadAsAudio)
{
byte[] tmpData = new byte[sectorSize];
byte[] data = new byte[sectorSize * blocksToRead];
2020-07-13 18:54:41 +01:00
for(int b = 0; b < blocksToRead; b++)
2022-03-06 13:29:38 +00:00
{
Array.Copy(cmdBuf, (int)(b * sectorSize), tmpData, 0, sectorSize);
tmpData = Sector.Scramble(tmpData);
Array.Copy(tmpData, 0, data, sectorSize * b, sectorSize);
}
2022-03-06 13:29:38 +00:00
outputOptical.WriteSectorsLong(data, i, blocksToRead);
}
else
outputOptical.WriteSectorsLong(cmdBuf, i, blocksToRead);
}
imageWriteDuration += _writeStopwatch.Elapsed.TotalSeconds;
2022-12-05 20:11:59 +00:00
_mediaGraph?.PaintSectorsGood(i, blocksToRead);
2022-03-06 13:29:38 +00:00
}
else
{
2025-08-22 19:57:09 +01:00
_errorLog?.WriteLine(i, _dev.Error, _dev.LastError, senseBuf.ToArray());
_resume.NextBlock = firstTrack.EndSector + 1;
2022-03-06 13:29:38 +00:00
break;
}
_writeStopwatch.Stop();
2022-03-06 13:29:38 +00:00
sectorSpeedStart += blocksToRead;
2022-03-06 13:29:38 +00:00
_resume.NextBlock = i + blocksToRead;
elapsed = _speedStopwatch.Elapsed.TotalSeconds;
2022-03-06 13:29:38 +00:00
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
EndProgress?.Invoke();
}
}