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

284 lines
11 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 : Tracks.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
// Calculates CompactDisc tracks.
//
// --[ 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;
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;
using Aaru.Decoders.CD;
using Aaru.Devices;
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>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, bool force, out long lastSector, Dictionary<int, long> leadOutStarts,
Dictionary<MediaTagType, byte[]> mediaTags,
ErrorMessageHandler stoppingErrorMessage, out FullTOC.CDFullTOC? toc,
2022-03-06 13:29:38 +00:00
Dictionary<byte, byte> trackFlags, UpdateStatusHandler updateStatus)
{
byte[] cmdBuf; // Data buffer
const uint sectorSize = 2352; // Full sector size
bool sense; // Sense indicator
2024-05-01 04:39:38 +01:00
List<Track> trackList = []; // 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)
updateStatus?.Invoke(Localization.Core.Reading_full_TOC);
2022-03-06 13:29:38 +00:00
sense = dev.ReadRawToc(out cmdBuf, out _, 0, dev.Timeout, out _);
if(!sense)
{
2022-03-06 13:29:38 +00:00
toc = FullTOC.Decode(cmdBuf);
2022-03-06 13:29:38 +00:00
if(toc.HasValue)
{
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);
}
2022-03-06 13:29:38 +00:00
}
updateStatus?.Invoke(Localization.Core.Building_track_map);
2022-03-06 13:29:38 +00:00
if(toc.HasValue)
{
FullTOC.TrackDataDescriptor[] sortedTracks =
toc.Value.TrackDescriptors.OrderBy(static track => track.POINT).ToArray();
foreach(FullTOC.TrackDataDescriptor trk in sortedTracks.Where(static trk => trk.ADR is 1 or 4))
2023-10-03 22:57:50 +01:00
{
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 ||
2023-10-03 22:57:50 +01:00
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental
? TrackType.Data
2022-11-13 19:38:03 +00:00
: TrackType.Audio,
StartSector =
(ulong)(trk.PHOUR * 3600 * 75 + trk.PMIN * 60 * 75 + trk.PSEC * 75 + trk.PFRAME - 150),
2022-11-13 19:38:03 +00:00
BytesPerSector = (int)sectorSize,
RawBytesPerSector = (int)sectorSize
});
trackFlags?.Add(trk.POINT, trk.CONTROL);
2022-11-13 19:38:03 +00:00
break;
case 0xA2:
{
2022-11-13 19:38:03 +00:00
int phour, pmin, psec, pframe;
2022-11-13 19:38:03 +00:00
if(trk.PFRAME == 0)
{
2022-11-13 19:38:03 +00:00
pframe = 74;
2022-11-13 19:38:03 +00:00
if(trk.PSEC == 0)
{
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;
}
}
else
{
2022-11-13 19:38:03 +00:00
psec = trk.PSEC - 1;
pmin = trk.PMIN;
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;
}
2022-11-13 19:38:03 +00:00
2023-10-03 22:57:50 +01:00
lastSector = phour * 3600 * 75 + pmin * 60 * 75 + psec * 75 + pframe - 150;
2022-11-13 19:38:03 +00:00
leadOutStarts?.Add(trk.SessionNumber, lastSector + 1);
break;
}
2022-11-13 19:38:03 +00:00
case 0xA0 when trk.ADR == 1:
leadoutTrackType =
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrack ||
2023-10-03 22:57:50 +01:00
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental
? TrackType.Data
2022-11-13 19:38:03 +00:00
: TrackType.Audio;
2022-11-13 19:38:03 +00:00
break;
2022-03-06 13:29:38 +00:00
}
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
}
else
{
updateStatus?.Invoke(Localization.Core.Cannot_read_RAW_TOC_requesting_processed_one);
2022-03-06 13:29:38 +00:00
sense = dev.ReadToc(out cmdBuf, out _, false, 0, dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
TOC.CDTOC? oldToc = TOC.Decode(cmdBuf);
if((sense || !oldToc.HasValue) && !force)
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:05:22 +01:00
stoppingErrorMessage?.Invoke(Localization.Core
.Could_not_read_TOC_if_you_want_to_continue_use_force_and_will_try_from_LBA_0_to_360000);
2022-03-06 13:29:38 +00:00
return null;
}
2022-03-06 13:29:38 +00:00
if(oldToc.HasValue)
2023-10-03 22:57:50 +01:00
{
foreach(TOC.CDTOCTrackDataDescriptor trk in oldToc.Value.TrackDescriptors
2025-11-24 11:45:16 +00:00
.Where(static trk => trk.ADR is 1 or 4)
.OrderBy(static t => t.TrackNumber))
2023-10-03 22:57:50 +01:00
{
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
2023-10-03 22:57:50 +01:00
? TrackType.Data
: TrackType.Audio,
2022-11-13 19:38:03 +00:00
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 ||
2023-10-03 22:57:50 +01:00
(TocControl)(trk.CONTROL & 0x0D) == TocControl.DataTrackIncremental
? TrackType.Data
2022-11-13 19:38:03 +00:00
: TrackType.Audio;
lastSector = trk.TrackStartAddress - 1;
break;
2022-03-06 13:29:38 +00:00
}
2023-10-03 22:57:50 +01:00
}
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(trackList.Count == 0)
{
updateStatus?.Invoke(Localization.Core.No_tracks_found_adding_a_single_track_from_zero_to_Lead_Out);
2022-03-06 13:29:38 +00:00
trackList.Add(new Track
{
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));
}
2024-05-01 04:05:22 +01:00
if(lastSector != 0) return trackList.ToArray();
2022-03-06 13:29:38 +00:00
sense = dev.ReadCapacity16(out cmdBuf, out _, dev.Timeout, out _);
2022-03-06 13:29:38 +00:00
if(!sense)
{
var temp = new byte[8];
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 _);
2024-05-01 04:05:22 +01:00
if(!sense) lastSector = (cmdBuf[0] << 24) + (cmdBuf[1] << 16) + (cmdBuf[2] << 8) + cmdBuf[3] & 0xFFFFFFFF;
2022-03-06 13:29:38 +00:00
}
2024-05-01 04:05:22 +01:00
if(lastSector > 0) return trackList.ToArray();
2022-03-06 13:29:38 +00:00
if(!force)
{
2024-05-01 04:05:22 +01:00
stoppingErrorMessage?.Invoke(Localization.Core
.Could_not_find_Lead_Out_if_you_want_to_continue_use_force_option);
2022-03-06 13:29:38 +00:00
return null;
}
2022-03-06 13:29:38 +00:00
updateStatus?.Invoke(Localization.Core.WARNING_Could_not_find_Lead_Out_start_will_try_to_read_up_to);
2022-03-06 13:29:38 +00:00
lastSector = 360000;
return trackList.ToArray();
}
}