Read Lite-On raw DVD buffer

This commit is contained in:
Rebecca Wallander
2025-03-15 21:53:45 +01:00
parent 56a81aaf10
commit 1ab023bbc6
17 changed files with 7629 additions and 11046 deletions

View File

@@ -50,9 +50,15 @@ public partial class Device
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Start block address.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="layerbreak">The address in which the layerbreak occur</param>
/// <param name="otp">Set to <c>true</c> if disk is Opposite Track Path (OTP)</param>
public bool HlDtStReadRawDvd(out byte[] buffer, out byte[] senseBuffer, uint lba, uint transferLength, uint timeout,
out double duration)
out double duration, uint layerbreak, bool otp)
{
// We need to fill the buffer before reading it with the HL-DT-ST command. We don't care about sense,
// because the data can be wrong anyway, so we check the buffer data later instead.
Read12(out _, out _, 0, false, false, false, false, lba, 2048, 0, 16, false, timeout, out duration);
senseBuffer = new byte[64];
var cdb = new byte[12];
buffer = new byte[2064 * transferLength];
@@ -83,7 +89,7 @@ public partial class Device
AaruConsole.DebugWriteLine(SCSI_MODULE_NAME, Localization.HL_DT_ST_READ_DVD_RAW_took_0_ms, duration);
if(!CheckSectorNumber(buffer, lba, transferLength)) return true;
if(!CheckSectorNumber(buffer, lba, transferLength, layerbreak, otp)) return true;
if(_decoding.Scramble(buffer, transferLength, out byte[] scrambledBuffer) != ErrorNumber.NoError) return true;
@@ -97,19 +103,67 @@ public partial class Device
/// </summary>
/// <param name="buffer">Data buffer</param>
/// <param name="firstLba">First consecutive LBA of the buffer</param>
/// <param name="transferLength">How many blocks to in buffer</param>
/// <param name="transferLength">How many blocks in buffer</param>
/// <param name="layerbreak">The address in which the layerbreak occur</param>
/// <param name="otp">Set to <c>true</c> if disk is Opposite Track Path (OTP)</param>
/// <returns><c>false</c> if any sector is not matching expected value, else <c>true</c></returns>
static bool CheckSectorNumber(IReadOnlyList<byte> buffer, uint firstLba, uint transferLength)
static bool CheckSectorNumber(IReadOnlyList<byte> buffer, uint firstLba, uint transferLength, uint layerbreak,
bool otp)
{
for(var i = 0; i < transferLength; i++)
{
byte layer = (byte)(buffer[0 + 2064 * i] & 0x1);
byte[] sectorBuffer = [0x0, buffer[1 + 2064 * i], buffer[2 + 2064 * i], buffer[3 + 2064 * i]];
var sectorNumber = BigEndianBitConverter.ToUInt32(sectorBuffer, 0);
if(sectorNumber != firstLba + i + 0x30000) return false;
if(otp)
{
if(!IsCorrectDlOtpPsn(sectorNumber, (ulong)(firstLba + i), layer, layerbreak)) return false;
}
else
{
if(!IsCorrectSlPsn(sectorNumber, (ulong)(firstLba + i))) return false;
}
}
return true;
}
/// <summary>
/// Checks if the PSN for a raw sector matches the expected LBA for a single layer DVD
/// </summary>
/// <param name="sectorNumber">The Sector Number from Identification Data (ID) </param>
/// <param name="lba">The expected LBA</param>
/// <returns><c>false</c> if the sector is not matching expected value, else <c>true</c></returns>
private static bool IsCorrectSlPsn(uint sectorNumber, ulong lba) => sectorNumber == lba + 0x30000;
/// <summary>
/// Checks if the PSN for a raw sector matches the expected LBA for a dual layer DVD with parallel track path
/// </summary>
/// <param name="sectorNumber">The Sector Number from Identification Data (ID) </param>
/// <param name="lba">The expected LBA</param>
/// <returns><c>false</c> if the sector is not matching expected value, else <c>true</c></returns>
private static bool IsCorrectDlPtpPsn(uint sectorNumber, ulong lba, byte layer, uint layerbreak)
{
if(layer != 1) return IsCorrectSlPsn(sectorNumber, lba);
return sectorNumber == (lba - layerbreak) + 0x30000;
}
/// <summary>
/// Checks if the PSN for a raw sector matches the expected LBA for a dual layer DVD with opposite track path
/// </summary>
/// <param name="sectorNumber">The Sector Number from Identification Data (ID) </param>
/// <param name="lba">The expected LBA</param>
/// <returns><c>false</c> if the sector is not matching expected value, else <c>true</c></returns>
private static bool IsCorrectDlOtpPsn(uint sectorNumber, ulong lba, byte layer, uint layerbreak)
{
if(layer != 1) return IsCorrectSlPsn(sectorNumber, lba);
ulong n = ~(layerbreak + 1 + (layerbreak - (lba + 0x30000))) & 0x00ffffff;
return sectorNumber == n;
}
}

View File

@@ -0,0 +1,303 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : LiteOn.cs
// Author(s) : Rebecca Wallander <sakcheen@gmail.com>
//
// Component : LiteOn vendor commands.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains vendor commands for Lite-On SCSI devices.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2025 Rebecca Wallander
// ****************************************************************************/
using System;
using Aaru.CommonTypes.Enums;
using Aaru.Console;
namespace Aaru.Devices;
public partial class Device
{
private uint _bufferOffset = 0;
/// <summary>Reads a "raw" sector from DVD on Lite-On drives.</summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer" /> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the ReadBuffer (RAW) response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Start block address.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="layerbreak">The address in which the layerbreak occur</param>
/// <param name="otp">Set to <c>true</c> if disk is Opposite Track Path (OTP)</param>
public bool LiteOnReadRawDvd(out byte[] buffer, out byte[] senseBuffer, uint lba, uint transferLength, uint timeout,
out double duration, uint layerbreak, bool otp)
{
_bufferOffset %= 714;
bool sense;
if(layerbreak > 0 && transferLength > 1 && lba + 0x30000 > layerbreak - 256 && lba + 0x30000 < layerbreak + 256)
{
buffer = new byte[transferLength * 2064];
duration = 0;
senseBuffer = new byte[64];
return true;
}
if(714 - _bufferOffset < transferLength)
{
sense = LiteOnReadSectorsAcrossBufferBorder(out buffer,
out senseBuffer,
lba,
transferLength,
timeout,
out duration,
layerbreak,
otp);
}
else
{
sense = LiteOnReadSectorsFromBuffer(out buffer,
out senseBuffer,
lba,
transferLength,
timeout,
out duration,
layerbreak,
otp);
}
Error = LastError != 0;
AaruConsole.DebugWriteLine(SCSI_MODULE_NAME, Localization.LiteOn_READ_DVD_RAW_took_0_ms, duration);
return sense;
}
/// <summary>
/// Reads the Lite-On device's memory buffer and returns raw sector data
/// </summary>
/// <param name="buffer">Buffer where the ReadBuffer (RAW) response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="bufferOffset">The offset to read the buffer at</param>
/// <param name="transferLength"></param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Start block address.</param>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer" /> contains the sense buffer.</returns>
private bool LiteOnReadBuffer(out byte[] buffer, out byte[] senseBuffer, uint bufferOffset, uint transferLength,
uint timeout, out double duration, uint lba)
{
// We need to fill the buffer before reading it with the ReadBuffer command. We don't care about sense,
// because the data can be wrong anyway, so we check the buffer data later instead.
Read12(out _, out _, 0, false, false, false, false, lba, 2048, 0, 16, false, timeout, out duration);
senseBuffer = new byte[64];
var cdb = new byte[10];
buffer = new byte[transferLength];
cdb[0] = (byte)ScsiCommands.ReadBuffer;
cdb[1] = 0x01;
cdb[2] = 0x01;
cdb[3] = (byte)((bufferOffset & 0xFF0000) >> 16);
cdb[4] = (byte)((bufferOffset & 0xFF00) >> 8);
cdb[5] = (byte)(bufferOffset & 0xFF);
cdb[6] = (byte)((buffer.Length & 0xFF0000) >> 16);
cdb[7] = (byte)((buffer.Length & 0xFF00) >> 8);
cdb[8] = (byte)(buffer.Length & 0xFF);
LastError = SendScsiCommand(cdb,
ref buffer,
out senseBuffer,
timeout,
ScsiDirection.In,
out duration,
out bool sense);
return sense;
}
/// <summary>
/// Reads raw sectors from the device's memory
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer" /> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the ReadBuffer (RAW) response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Start block address.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="layerbreak">The address in which the layerbreak occur</param>
/// <param name="otp">Set to <c>true</c> if disk is Opposite Track Path (OTP)</param>
private bool LiteOnReadSectorsFromBuffer(out byte[] buffer, out byte[] senseBuffer, uint lba, uint transferLength,
uint timeout, out double duration, uint layerbreak, bool otp)
{
bool sense = LiteOnReadBuffer(out buffer,
out senseBuffer,
_bufferOffset * 2384,
transferLength * 2384,
timeout,
out duration,
lba);
byte[] deinterleaved = DeinterleaveEccBlock(buffer, transferLength);
if(!CheckSectorNumber(deinterleaved, lba, transferLength, layerbreak, true))
{
// Buffer offset lost, try to find it again
int offset = FindBufferOffset(lba, timeout, layerbreak, otp);
if(offset == -1) return true;
_bufferOffset = (uint)offset;
sense = LiteOnReadBuffer(out buffer,
out senseBuffer,
_bufferOffset * 2384,
transferLength * 2384,
timeout,
out duration,
lba);
deinterleaved = DeinterleaveEccBlock(buffer, transferLength);
if(!CheckSectorNumber(deinterleaved, lba, transferLength, layerbreak, otp)) return true;
}
if(_decoding.Scramble(deinterleaved, transferLength, out byte[] scrambledBuffer) != ErrorNumber.NoError)
return true;
buffer = scrambledBuffer;
_bufferOffset += transferLength;
return sense;
}
/// <summary>
/// Reads raw sectors when they cross the device's memory border
/// </summary>
/// <returns><c>true</c> if the command failed and <paramref name="senseBuffer" /> contains the sense buffer.</returns>
/// <param name="buffer">Buffer where the ReadBuffer (RAW) response will be stored</param>
/// <param name="senseBuffer">Sense buffer.</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="duration">Duration in milliseconds it took for the device to execute the command.</param>
/// <param name="lba">Start block address.</param>
/// <param name="transferLength">How many blocks to read.</param>
/// <param name="layerbreak">The address in which the layerbreak occur</param>
/// <param name="otp">Set to <c>true</c> if disk is Opposite Track Path (OTP)</param>
private bool LiteOnReadSectorsAcrossBufferBorder(out byte[] buffer, out byte[] senseBuffer, uint lba,
uint transferLength, uint timeout, out double duration,
uint layerbreak, bool otp)
{
uint newTransferLength1 = 714 - _bufferOffset;
uint newTransferLength2 = transferLength - newTransferLength1;
bool sense1 = LiteOnReadBuffer(out byte[] buffer1,
out byte[] _,
_bufferOffset * 2384,
newTransferLength1 * 2384,
timeout,
out double duration1,
lba);
bool sense2 = LiteOnReadBuffer(out byte[] buffer2,
out byte[] _,
0,
newTransferLength2 * 2384,
timeout,
out double duration2,
lba);
senseBuffer = new byte[64]; // TODO
buffer = new byte[2384 * transferLength];
Array.Copy(buffer1, buffer, buffer1.Length);
Array.Copy(buffer2, 0, buffer, buffer1.Length, buffer2.Length);
duration = duration1 + duration2;
byte[] deinterleaved = DeinterleaveEccBlock(buffer, transferLength);
if(!CheckSectorNumber(deinterleaved, lba, transferLength, layerbreak, otp)) return true;
if(_decoding.Scramble(deinterleaved, transferLength, out byte[] scrambledBuffer) != ErrorNumber.NoError)
return true;
buffer = scrambledBuffer;
_bufferOffset = newTransferLength2;
return sense1 && sense2;
}
/// <summary>
/// Sometimes the offset on the drive memory can get lost. This tries to find it again.
/// </summary>
/// <param name="lba">The expected LBA</param>
/// <param name="timeout">Timeout in seconds.</param>
/// <param name="layerbreak">The address in which the layerbreak occur</param>
/// <param name="otp">Set to <c>true</c> if disk is Opposite Track Path (OTP)</param>
/// <returns>The offset on the device memory, or -1 if not found</returns>
private int FindBufferOffset(uint lba, uint timeout, uint layerbreak, bool otp)
{
for(uint i = 0; i < 714; i++)
{
LiteOnReadBuffer(out byte[] buffer, out byte[] _, i * 2384, 2384, timeout, out double _, lba);
if(CheckSectorNumber(buffer, lba, 1, layerbreak, otp))
{
return (int)i;
}
}
return -1;
}
/// <summary>
/// Deinterleave the ECC block stored within a 2384 byte raw sector
/// </summary>
/// <param name="buffer">Data buffer</param>
/// <param name="transferLength">How many blocks in buffer</param>
/// <param name="deinterleaved"></param>
/// <returns>The deinterleaved sectors</returns>
private static byte[] DeinterleaveEccBlock(byte[] buffer, uint transferLength)
{
// TODO: Save ECC instead of just throwing it away
var deinterleaved = new byte[2064 * transferLength];
for(var j = 0; j < transferLength; j++)
{
for(var i = 0; i < 12; i++)
{
Array.Copy(buffer, (j * 2384) + (i * 182), deinterleaved, (j * 2064) + (i * 172), 172);
}
}
return deinterleaved;
}
}

View File

@@ -110,9 +110,9 @@ public partial class Device
cdb[3] = (byte)((lba & 0xFF0000) >> 16);
cdb[4] = (byte)((lba & 0xFF00) >> 8);
cdb[5] = (byte)(lba & 0xFF);
cdb[3] = (byte)((buffer.Length & 0xFF0000) >> 16);
cdb[4] = (byte)((buffer.Length & 0xFF00) >> 8);
cdb[5] = (byte)(buffer.Length & 0xFF);
cdb[6] = (byte)((buffer.Length & 0xFF0000) >> 16);
cdb[7] = (byte)((buffer.Length & 0xFF00) >> 8);
cdb[8] = (byte)(buffer.Length & 0xFF);
LastError = SendScsiCommand(cdb,
ref buffer,

File diff suppressed because it is too large Load Diff

View File

@@ -508,4 +508,7 @@
<data name="Remote_error_0_in_OS_Read" xml:space="preserve">
<value>Remote error {0} in OS Read...</value>
</data>
<data name="LiteOn_READ_DVD_RAW_took_0_ms" xml:space="preserve">
<value>Lite-On READ DVD (RAW) took {0} ms.</value>
</data>
</root>