2019-12-31 21:02:22 +00:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2019-12-31 21:02:22 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
2020-03-11 21:56:55 +00:00
|
|
|
// Filename : Tracks.cs
|
2019-12-31 21:02:22 +00:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2020-03-11 21:56:55 +00:00
|
|
|
// Component : CompactDisc dumping.
|
2019-12-31 21:02:22 +00:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2020-03-11 21:56:55 +00:00
|
|
|
// Calculates CompactDisc tracks.
|
2019-12-31 21:02:22 +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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:53 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2019-12-31 21:02:22 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
// ReSharper disable JoinDeclarationAndInitializer
|
|
|
|
|
// ReSharper disable InlineOutVariableDeclaration
|
|
|
|
|
// ReSharper disable TooWideLocalVariableScope
|
|
|
|
|
|
|
|
|
|
namespace Aaru.Core.Devices.Dumping;
|
|
|
|
|
|
2019-12-31 21:02:22 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-12-31 19:28:47 +00:00
|
|
|
using Aaru.CommonTypes;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
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.CD;
|
|
|
|
|
using Aaru.Devices;
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
partial class Dump
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>Reads the TOC, processes it, returns the track list and last sector</summary>
|
|
|
|
|
/// <param name="dev">Device</param>
|
|
|
|
|
/// <param name="dumpLog">Dump log</param>
|
|
|
|
|
/// <param name="force">Force dump enabled</param>
|
|
|
|
|
/// <param name="lastSector">Last sector number</param>
|
|
|
|
|
/// <param name="leadOutStarts">Lead-out starts</param>
|
|
|
|
|
/// <param name="mediaTags">Media tags</param>
|
|
|
|
|
/// <param name="stoppingErrorMessage">Stopping error message handler</param>
|
|
|
|
|
/// <param name="toc">Full CD TOC</param>
|
|
|
|
|
/// <param name="trackFlags">Track flags</param>
|
|
|
|
|
/// <param name="updateStatus">Update status handler</param>
|
|
|
|
|
/// <returns>List of tracks</returns>
|
|
|
|
|
public static Track[] GetCdTracks(Device dev, DumpLog dumpLog, bool force, out long lastSector,
|
2022-03-07 07:36:44 +00:00
|
|
|
Dictionary<int, long> leadOutStarts, Dictionary<MediaTagType, byte[]> mediaTags,
|
2022-03-06 13:29:38 +00:00
|
|
|
ErrorMessageHandler stoppingErrorMessage, out FullTOC.CDFullTOC? toc,
|
|
|
|
|
Dictionary<byte, byte> trackFlags, UpdateStatusHandler updateStatus)
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
byte[] cmdBuf; // Data buffer
|
|
|
|
|
const uint sectorSize = 2352; // Full sector size
|
|
|
|
|
bool sense; // Sense indicator
|
|
|
|
|
var trackList = new List<Track>(); // Tracks in disc
|
|
|
|
|
byte[] tmpBuf; // Temporary buffer
|
2022-03-06 13:29:38 +00:00
|
|
|
toc = null;
|
|
|
|
|
lastSector = 0;
|
|
|
|
|
TrackType leadoutTrackType = TrackType.Audio;
|
|
|
|
|
|
|
|
|
|
// We discarded all discs that falsify a TOC before requesting a real TOC
|
|
|
|
|
// No TOC, no CD (or an empty one)
|
|
|
|
|
dumpLog?.WriteLine("Reading full TOC");
|
|
|
|
|
updateStatus?.Invoke("Reading full TOC");
|
|
|
|
|
sense = dev.ReadRawToc(out cmdBuf, out _, 0, dev.Timeout, out _);
|
|
|
|
|
|
|
|
|
|
if(!sense)
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
toc = FullTOC.Decode(cmdBuf);
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(toc.HasValue)
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
tmpBuf = new byte[cmdBuf.Length - 2];
|
|
|
|
|
Array.Copy(cmdBuf, 2, tmpBuf, 0, cmdBuf.Length - 2);
|
|
|
|
|
mediaTags?.Add(MediaTagType.CD_FullTOC, tmpBuf);
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
updateStatus?.Invoke("Building track map...");
|
|
|
|
|
dumpLog?.WriteLine("Building track map...");
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(toc.HasValue)
|
|
|
|
|
{
|
|
|
|
|
FullTOC.TrackDataDescriptor[] sortedTracks =
|
|
|
|
|
toc.Value.TrackDescriptors.OrderBy(track => track.POINT).ToArray();
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-16 11:47:00 +00:00
|
|
|
foreach(FullTOC.TrackDataDescriptor trk in sortedTracks.Where(trk => trk.ADR is 1 or 4))
|
2022-11-13 19:38:03 +00:00
|
|
|
switch(trk.POINT)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
case >= 0x01 and <= 0x63:
|
|
|
|
|
trackList.Add(new Track
|
|
|
|
|
{
|
|
|
|
|
Sequence = trk.POINT,
|
|
|
|
|
Session = trk.SessionNumber,
|
|
|
|
|
Type = (TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrack ||
|
|
|
|
|
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental ? TrackType.Data
|
|
|
|
|
: TrackType.Audio,
|
|
|
|
|
StartSector =
|
|
|
|
|
(ulong)(trk.PHOUR * 3600 * 75 + trk.PMIN * 60 * 75 + trk.PSEC * 75 + trk.PFRAME - 150),
|
|
|
|
|
BytesPerSector = (int)sectorSize,
|
|
|
|
|
RawBytesPerSector = (int)sectorSize
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
trackFlags?.Add(trk.POINT, trk.CONTROL);
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
|
|
|
|
case 0xA2:
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
int phour, pmin, psec, pframe;
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
if(trk.PFRAME == 0)
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
pframe = 74;
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
if(trk.PSEC == 0)
|
2019-12-31 21:02:22 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
psec = 59;
|
|
|
|
|
|
|
|
|
|
if(trk.PMIN == 0)
|
|
|
|
|
{
|
|
|
|
|
pmin = 59;
|
|
|
|
|
phour = trk.PHOUR - 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pmin = trk.PMIN - 1;
|
|
|
|
|
phour = trk.PHOUR;
|
|
|
|
|
}
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
psec = trk.PSEC - 1;
|
|
|
|
|
pmin = trk.PMIN;
|
2019-12-31 21:02:22 +00:00
|
|
|
phour = trk.PHOUR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
pframe = trk.PFRAME - 1;
|
|
|
|
|
psec = trk.PSEC;
|
|
|
|
|
pmin = trk.PMIN;
|
|
|
|
|
phour = trk.PHOUR;
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
2022-11-13 19:38:03 +00:00
|
|
|
|
|
|
|
|
lastSector = phour * 3600 * 75 + pmin * 60 * 75 + psec * 75 + pframe - 150;
|
|
|
|
|
leadOutStarts?.Add(trk.SessionNumber, lastSector + 1);
|
|
|
|
|
|
|
|
|
|
break;
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
2022-11-13 19:38:03 +00:00
|
|
|
case 0xA0 when trk.ADR == 1:
|
|
|
|
|
leadoutTrackType =
|
|
|
|
|
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrack ||
|
|
|
|
|
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental ? TrackType.Data
|
|
|
|
|
: TrackType.Audio;
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-11-13 19:38:03 +00:00
|
|
|
break;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
updateStatus?.Invoke("Cannot read RAW TOC, requesting processed one...");
|
|
|
|
|
dumpLog?.WriteLine("Cannot read RAW TOC, requesting processed one...");
|
|
|
|
|
sense = dev.ReadToc(out cmdBuf, out _, false, 0, dev.Timeout, out _);
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
TOC.CDTOC? oldToc = TOC.Decode(cmdBuf);
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if((sense || !oldToc.HasValue) &&
|
|
|
|
|
!force)
|
|
|
|
|
{
|
|
|
|
|
dumpLog?.WriteLine("Could not read TOC, if you want to continue, use force, and will try from LBA 0 to 360000...");
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
stoppingErrorMessage?.
|
|
|
|
|
Invoke("Could not read TOC, if you want to continue, use force, and will try from LBA 0 to 360000...");
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return null;
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(oldToc.HasValue)
|
2022-03-07 07:36:44 +00:00
|
|
|
foreach(TOC.CDTOCTrackDataDescriptor trk in oldToc.Value.TrackDescriptors.OrderBy(t => t.TrackNumber).
|
2022-03-16 11:47:00 +00:00
|
|
|
Where(trk => trk.ADR is 1 or 4))
|
2022-11-13 19:38:03 +00:00
|
|
|
switch(trk.TrackNumber)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-11-13 19:38:03 +00:00
|
|
|
case >= 0x01 and <= 0x63:
|
|
|
|
|
trackList.Add(new Track
|
|
|
|
|
{
|
|
|
|
|
Sequence = trk.TrackNumber,
|
|
|
|
|
Session = 1,
|
|
|
|
|
Type = (TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrack ||
|
|
|
|
|
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental
|
|
|
|
|
? TrackType.Data : TrackType.Audio,
|
|
|
|
|
StartSector = trk.TrackStartAddress,
|
|
|
|
|
BytesPerSector = (int)sectorSize,
|
|
|
|
|
RawBytesPerSector = (int)sectorSize
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
trackFlags?.Add(trk.TrackNumber, trk.CONTROL);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 0xAA:
|
|
|
|
|
leadoutTrackType =
|
|
|
|
|
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrack ||
|
|
|
|
|
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental ? TrackType.Data
|
|
|
|
|
: TrackType.Audio;
|
|
|
|
|
|
|
|
|
|
lastSector = trk.TrackStartAddress - 1;
|
|
|
|
|
|
|
|
|
|
break;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(trackList.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
updateStatus?.Invoke("No tracks found, adding a single track from 0 to Lead-Out");
|
|
|
|
|
dumpLog?.WriteLine("No tracks found, adding a single track from 0 to Lead-Out");
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
trackList.Add(new Track
|
2020-11-11 04:19:18 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
Sequence = 1,
|
|
|
|
|
Session = 1,
|
|
|
|
|
Type = leadoutTrackType,
|
|
|
|
|
StartSector = 0,
|
|
|
|
|
BytesPerSector = (int)sectorSize,
|
|
|
|
|
RawBytesPerSector = (int)sectorSize
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
trackFlags?.Add(1, (byte)(leadoutTrackType == TrackType.Audio ? 0 : 4));
|
|
|
|
|
}
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(lastSector != 0)
|
|
|
|
|
return trackList.ToArray();
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
sense = dev.ReadCapacity16(out cmdBuf, out _, dev.Timeout, out _);
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!sense)
|
|
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
var temp = new byte[8];
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Array.Copy(cmdBuf, 0, temp, 0, 8);
|
|
|
|
|
Array.Reverse(temp);
|
|
|
|
|
lastSector = (long)BitConverter.ToUInt64(temp, 0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sense = dev.ReadCapacity(out cmdBuf, out _, dev.Timeout, out _);
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!sense)
|
|
|
|
|
lastSector = ((cmdBuf[0] << 24) + (cmdBuf[1] << 16) + (cmdBuf[2] << 8) + cmdBuf[3]) & 0xFFFFFFFF;
|
|
|
|
|
}
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(lastSector > 0)
|
|
|
|
|
return trackList.ToArray();
|
2019-12-31 21:02:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!force)
|
|
|
|
|
{
|
|
|
|
|
stoppingErrorMessage?.
|
|
|
|
|
Invoke("Could not find Lead-Out, if you want to continue use force option and will continue until 360000 sectors...");
|
2020-11-11 04:19:18 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
dumpLog?.WriteLine("Could not find Lead-Out, if you want to continue use force option and will continue until 360000 sectors...");
|
2020-11-11 04:19:18 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return null;
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
updateStatus?.
|
|
|
|
|
Invoke("WARNING: Could not find Lead-Out start, will try to read up to 360000 sectors, probably will fail before...");
|
|
|
|
|
|
|
|
|
|
dumpLog?.WriteLine("WARNING: Could not find Lead-Out start, will try to read up to 360000 sectors, probably will fail before...");
|
|
|
|
|
lastSector = 360000;
|
|
|
|
|
|
|
|
|
|
return trackList.ToArray();
|
2019-12-31 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
}
|