Rename Lite-On commands to ReadBuffer3C

This commit is contained in:
Rebecca Wallander
2026-01-11 10:23:21 +01:00
parent 6fb6535b6a
commit 8ebfd8c810
14 changed files with 3389 additions and 412 deletions

View File

@@ -779,8 +779,8 @@ public class TestedMedia
[DisplayName("Can read scrambled DVD sectors using HL-DT-ST cache trick")]
public bool? SupportsHLDTSTReadRawDVD { get; set; }
[DisplayName("Can read scrambled DVD sectors using Lite-On cache trick")]
public bool? SupportsLiteOnReadRawDVD { get; set; }
[DisplayName("Can read scrambled DVD sectors using SCSI ReadBuffer 3C")]
public bool? SupportsReadBuffer3CRawDVD { get; set; }
[DisplayName("Supports NEC READ CD-DA command")]
public bool? SupportsNECReadCDDA { get; set; }
@@ -1158,11 +1158,11 @@ public class TestedMedia
[DisplayName("Data from HL-DT-ST's scrambled DVD reading trick")]
public byte[] HLDTSTReadRawDVDData { get; set; }
[DisplayName("Data from Lite-On's scrambled DVD reading trick")]
public byte[] LiteOnReadRawDVDData { get; set; }
[DisplayName("Data from ReadBuffer 3C scrambled DVD reading")]
public byte[] ReadBuffer3CRawDVDData { get; set; }
[DisplayName("Lite-On ReadBuffer fallback data")]
public virtual List<CompressedBufferRead> LiteOnReadBufferData { get; set; }
[DisplayName("ReadBuffer 3C fallback data")]
public virtual List<CompressedBufferRead> ReadBuffer3CReadBufferData { get; set; }
#endregion
}

View File

@@ -804,7 +804,7 @@ partial class Dump
{
mediaTags.TryGetValue(MediaTagType.DVD_DiscKey_Decrypted, out byte[] discKey);
if(scsiReader.HldtstReadRaw || scsiReader.LiteOnReadRaw)
if(scsiReader.HldtstReadRaw || scsiReader.ReadBuffer3CReadRaw)
{
ReadCacheData(blocks,
blocksToRead,
@@ -904,7 +904,7 @@ partial class Dump
_titleKeys &&
// Unnecessary since keys are already in raw data
!scsiReader.LiteOnReadRaw &&
!scsiReader.ReadBuffer3CReadRaw &&
!scsiReader.HldtstReadRaw &&
mediaTag is not null)
RetryTitleKeys(dvdDecrypt, mediaTag, ref totalDuration);

View File

@@ -306,7 +306,7 @@ partial class Dump
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
if(scsiReader.LiteOnReadRaw || scsiReader.HldtstReadRaw)
if(scsiReader.ReadBuffer3CReadRaw || scsiReader.HldtstReadRaw)
{
var cmi = new byte[1];

View File

@@ -99,7 +99,7 @@ partial class Dump
_resume.BadBlocks.Remove(badSector);
extents.Add(badSector);
if(scsiReader.LiteOnReadRaw || scsiReader.HldtstReadRaw)
if(scsiReader.ReadBuffer3CReadRaw || scsiReader.HldtstReadRaw)
{
var cmi = new byte[1];

View File

@@ -55,7 +55,7 @@ sealed partial class Reader
// TODO: Raw reading
public bool HldtstReadRaw;
public uint layerbreak;
public bool LiteOnReadRaw;
public bool ReadBuffer3CReadRaw;
public bool otp;
ulong ScsiGetBlocks() => ScsiGetBlockSize() ? 0 : Blocks;
@@ -580,16 +580,14 @@ sealed partial class Reader
case "PLEXTOR":
_plextorReadRaw = !_dev.PlextorReadRawDvd(out _, out senseBuf, 0, 1, _timeout, out _);
break;
case "LITE-ON":
case "TSSTcorp":
LiteOnReadRaw =
!_dev.LiteOnReadRawDvd(out _, out senseBuf, 0, 1, _timeout, out _, layerbreak, otp);
break;
}
if(HldtstReadRaw || _plextorReadRaw || LiteOnReadRaw)
// Try ReadBuffer 3C on all devices
ReadBuffer3CReadRaw =
!_dev.ReadBuffer3CRawDvd(out _, out senseBuf, 0, 1, _timeout, out _, layerbreak, otp);
if(HldtstReadRaw || _plextorReadRaw || ReadBuffer3CReadRaw)
{
CanReadRaw = true;
LongBlockSize = 2064;
@@ -624,8 +622,8 @@ sealed partial class Reader
AaruLogging.WriteLine($"[slateblue1]{Localization.Core.Using_HL_DT_ST_raw_DVD_reading}[/]");
else if(_plextorReadRaw)
AaruLogging.WriteLine($"[slateblue1]{Localization.Core.Using_Plextor_raw_DVD_reading}[/]");
else if(LiteOnReadRaw)
AaruLogging.WriteLine($"[slateblue1]{Localization.Core.Using_Lite_On_raw_DVD_reading}[/]");
else if(ReadBuffer3CReadRaw)
AaruLogging.WriteLine($"[slateblue1]{Localization.Core.Using_ReadBuffer_3C_raw_DVD_reading}[/]");
}
else if(_read6)
AaruLogging.WriteLine($"[slateblue1]{Localization.Core.Using_SCSI_READ_6_command}[/]");
@@ -687,7 +685,7 @@ sealed partial class Reader
while(true)
{
if(HldtstReadRaw || LiteOnReadRaw)
if(HldtstReadRaw || ReadBuffer3CReadRaw)
BlocksToRead = 1;
else if(_read6)
{
@@ -835,16 +833,16 @@ sealed partial class Reader
_timeout,
out duration);
}
else if(LiteOnReadRaw)
else if(ReadBuffer3CReadRaw)
{
sense = _dev.LiteOnReadRawDvd(out buffer,
out senseBuf,
(uint)block,
count,
_timeout,
out duration,
layerbreak,
otp);
sense = _dev.ReadBuffer3CRawDvd(out buffer,
out senseBuf,
(uint)block,
count,
_timeout,
out duration,
layerbreak,
otp);
}
else
return true;

View File

@@ -2760,23 +2760,23 @@ public sealed partial class DeviceReport
{
ctx.AddTask(Localization.Core.Trying_ReadBuffer_3C_trick_to_raw_read_DVDs).IsIndeterminate();
mediaTest.SupportsLiteOnReadRawDVD =
!_dev.LiteOnReadRawDvd(out buffer, out _, 16, 1, _dev.Timeout, out _, 0xffff, false);
mediaTest.SupportsReadBuffer3CRawDVD =
!_dev.ReadBuffer3CRawDvd(out buffer, out _, 16, 1, _dev.Timeout, out _, 0xffff, false);
});
AaruLogging.Debug(SCSI_MODULE_NAME, Localization.Core.Sense_equals_0, !mediaTest.SupportsLiteOnReadRawDVD);
AaruLogging.Debug(SCSI_MODULE_NAME, Localization.Core.Sense_equals_0, !mediaTest.SupportsReadBuffer3CRawDVD);
if(mediaTest.SupportsLiteOnReadRawDVD == true)
mediaTest.SupportsLiteOnReadRawDVD = !ArrayHelpers.ArrayIsNullOrEmpty(buffer);
if(mediaTest.SupportsReadBuffer3CRawDVD == true)
mediaTest.SupportsReadBuffer3CRawDVD = !ArrayHelpers.ArrayIsNullOrEmpty(buffer);
if(mediaTest.SupportsLiteOnReadRawDVD == true)
if(mediaTest.SupportsReadBuffer3CRawDVD == true)
{
mediaTest.LiteOnReadRawDVDData = buffer;
mediaTest.ReadBuffer3CRawDVDData = buffer;
}
else
{
// Fallback: try multiple ReadBuffer 3C variants
mediaTest.LiteOnReadBufferData = new List<CompressedBufferRead>();
mediaTest.ReadBuffer3CReadBufferData = new List<CompressedBufferRead>();
// Try variant 3c 00 00
// First fill buffer with Read12
@@ -2792,7 +2792,7 @@ public sealed partial class DeviceReport
if(success && !ArrayHelpers.ArrayIsNullOrEmpty(buffer))
{
mediaTest.LiteOnReadBufferData.Add(new CompressedBufferRead
mediaTest.ReadBuffer3CReadBufferData.Add(new CompressedBufferRead
{
CommandVariant = "3c0000",
CompressedData = CompressBuffer(buffer),
@@ -2811,7 +2811,7 @@ public sealed partial class DeviceReport
if(success && !ArrayHelpers.ArrayIsNullOrEmpty(buffer))
{
mediaTest.LiteOnReadBufferData.Add(new CompressedBufferRead
mediaTest.ReadBuffer3CReadBufferData.Add(new CompressedBufferRead
{
CommandVariant = "3c0100",
CompressedData = CompressBuffer(buffer),
@@ -2830,7 +2830,7 @@ public sealed partial class DeviceReport
if(success && !ArrayHelpers.ArrayIsNullOrEmpty(buffer))
{
mediaTest.LiteOnReadBufferData.Add(new CompressedBufferRead
mediaTest.ReadBuffer3CReadBufferData.Add(new CompressedBufferRead
{
CommandVariant = "3c0101",
CompressedData = CompressBuffer(buffer),
@@ -2849,7 +2849,7 @@ public sealed partial class DeviceReport
if(success && !ArrayHelpers.ArrayIsNullOrEmpty(buffer))
{
mediaTest.LiteOnReadBufferData.Add(new CompressedBufferRead
mediaTest.ReadBuffer3CReadBufferData.Add(new CompressedBufferRead
{
CommandVariant = "3c0102",
CompressedData = CompressBuffer(buffer),
@@ -2868,7 +2868,7 @@ public sealed partial class DeviceReport
if(success && !ArrayHelpers.ArrayIsNullOrEmpty(buffer))
{
mediaTest.LiteOnReadBufferData.Add(new CompressedBufferRead
mediaTest.ReadBuffer3CReadBufferData.Add(new CompressedBufferRead
{
CommandVariant = "3c0200",
CompressedData = CompressBuffer(buffer),

View File

@@ -245,7 +245,7 @@ public sealed class AaruContext : DbContext
modelBuilder.Entity("Aaru.CommonTypes.Metadata.CompressedBufferRead",
static b => b.HasOne("Aaru.CommonTypes.Metadata.TestedMedia", null)
.WithMany("LiteOnReadBufferData")
.WithMany("ReadBuffer3CReadBufferData")
.HasForeignKey("TestedMediaId")
.OnDelete(DeleteBehavior.Cascade));

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Aaru.Database.Migrations
{
/// <inheritdoc />
public partial class RenameLiteOnToReadBuffer3C : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "SupportsLiteOnReadRawDVD",
table: "TestedMedia",
newName: "SupportsReadBuffer3CRawDVD");
migrationBuilder.RenameColumn(
name: "LiteOnReadRawDVDData",
table: "TestedMedia",
newName: "ReadBuffer3CRawDVDData");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "SupportsReadBuffer3CRawDVD",
table: "TestedMedia",
newName: "SupportsLiteOnReadRawDVD");
migrationBuilder.RenameColumn(
name: "ReadBuffer3CRawDVDData",
table: "TestedMedia",
newName: "LiteOnReadRawDVDData");
}
}
}

View File

@@ -1672,9 +1672,6 @@ namespace Aaru.Database.Migrations
b.Property<byte[]>("LeadOutData")
.HasColumnType("BLOB");
b.Property<byte[]>("LiteOnReadRawDVDData")
.HasColumnType("BLOB");
b.Property<ushort?>("LogicalAlignment")
.HasColumnType("INTEGER");
@@ -1759,6 +1756,9 @@ namespace Aaru.Database.Migrations
b.Property<byte[]>("Read6Data")
.HasColumnType("BLOB");
b.Property<byte[]>("ReadBuffer3CRawDVDData")
.HasColumnType("BLOB");
b.Property<byte[]>("ReadCdData")
.HasColumnType("BLOB");
@@ -1837,9 +1837,6 @@ namespace Aaru.Database.Migrations
b.Property<bool?>("SupportsHLDTSTReadRawDVD")
.HasColumnType("INTEGER");
b.Property<bool?>("SupportsLiteOnReadRawDVD")
.HasColumnType("INTEGER");
b.Property<bool?>("SupportsNECReadCDDA")
.HasColumnType("INTEGER");
@@ -1867,6 +1864,9 @@ namespace Aaru.Database.Migrations
b.Property<bool?>("SupportsRead6")
.HasColumnType("INTEGER");
b.Property<bool?>("SupportsReadBuffer3CRawDVD")
.HasColumnType("INTEGER");
b.Property<bool?>("SupportsReadCapacity")
.HasColumnType("INTEGER");
@@ -2638,7 +2638,7 @@ namespace Aaru.Database.Migrations
modelBuilder.Entity("Aaru.CommonTypes.Metadata.CompressedBufferRead", b =>
{
b.HasOne("Aaru.CommonTypes.Metadata.TestedMedia", null)
.WithMany("LiteOnReadBufferData")
.WithMany("ReadBuffer3CReadBufferData")
.HasForeignKey("TestedMediaId")
.OnDelete(DeleteBehavior.Cascade);
});
@@ -2952,7 +2952,7 @@ namespace Aaru.Database.Migrations
modelBuilder.Entity("Aaru.CommonTypes.Metadata.TestedMedia", b =>
{
b.Navigation("LiteOnReadBufferData");
b.Navigation("ReadBuffer3CReadBufferData");
});
modelBuilder.Entity("Aaru.CommonTypes.Metadata.TestedSequentialMedia", b =>

View File

@@ -1,360 +0,0 @@
// /***************************************************************************
// 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-2026 Rebecca Wallander
// ****************************************************************************/
using System;
using Aaru.CommonTypes.Enums;
using Aaru.Logging;
namespace Aaru.Devices;
public partial class Device
{
private enum LiteOnBufferFormat
{
Unknown = 0,
FullEccInterleaved,
PoOnly,
SectorDataOnly,
FullEccWithPadding
}
private uint _bufferOffset;
private uint _bufferCapacityInSectors;
private LiteOnBufferFormat _bufferFormat;
private uint _totalSectorsRead; // Tracks cumulative sectors read successfully since last buffer reset
/// <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="lba">Start block address.</param>
/// <param name="transferLength">How many blocks to read.</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="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 ReadOnlySpan<byte> senseBuffer, uint lba, uint transferLength,
uint timeout, out double duration, uint layerbreak, bool otp)
{
// Detect ReadBuffer 3C variant and stride on first call
if(!_readBuffer3CDetected)
{
bool detected = DetectReadBuffer3C(lba, timeout, out double detectDuration);
if(!detected || _detectedBufferStride == 0 || _detectedBufferStride < 2064 || _detectedBufferStride > 10000)
{
// Detection failed - raw reading is not supported on this drive
AaruLogging.Debug(SCSI_MODULE_NAME,
"ReadBuffer 3C detection failed - raw reading is not supported on this drive");
buffer = Array.Empty<byte>();
senseBuffer = SenseBuffer;
duration = detectDuration;
Error = true;
_readBuffer3CDetected = true;
return true; // Return failure - raw reading not supported
}
// Detect format based on stride (Lite-On specific)
_bufferFormat = _detectedBufferStride switch
{
2064 => LiteOnBufferFormat.SectorDataOnly,
2236 => LiteOnBufferFormat.PoOnly,
2384 => LiteOnBufferFormat.FullEccInterleaved,
> 2384 => LiteOnBufferFormat.FullEccWithPadding,
_ => LiteOnBufferFormat.FullEccInterleaved // Default for backward compatibility
};
AaruLogging.Debug(SCSI_MODULE_NAME,
"LiteOn buffer format detected based on stride: {0}, format: {1}",
_detectedBufferStride, _bufferFormat);
// Initialize buffer capacity with default value (will be refined dynamically when offset is lost)
// Buffer size is approximately 1,700,576 bytes but need to test on other drives to get the correct value
// It might also be the case that the buffer overflow works differently on different drives, so we need to test that as well.
// const uint BUFFER_SIZE = 1700576;
// _bufferCapacityInSectors = BUFFER_SIZE / _detectedBufferStride;
// if(_bufferCapacityInSectors == 0) _bufferCapacityInSectors = 714; // Fallback to known value
_bufferCapacityInSectors = 714;
_totalSectorsRead = 0; // Initialize tracking for dynamic capacity detection
_readBuffer3CDetected = true;
}
_bufferOffset %= _bufferCapacityInSectors;
bool sense;
if(layerbreak > 0 && transferLength > 1 && lba + 0x30000 > layerbreak - 256 && lba + 0x30000 < layerbreak + 256)
{
buffer = new byte[transferLength * 2064];
duration = 0;
senseBuffer = SenseBuffer;
return true;
}
if(_bufferCapacityInSectors - _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;
AaruLogging.Debug(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">How many blocks to read.</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 ReadOnlySpan<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);
// Use generic ReadBuffer method with detected variant
return ScsiReadBuffer(out buffer, out senseBuffer, bufferOffset, transferLength, timeout, out duration,
_detectedReadBufferMode, _detectedReadBufferId);
}
/// <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 ReadOnlySpan<byte> senseBuffer, uint lba,
uint transferLength, uint timeout, out double duration, uint layerbreak,
bool otp)
{
bool sense = LiteOnReadBuffer(out buffer,
out senseBuffer,
_bufferOffset * _detectedBufferStride,
transferLength * _detectedBufferStride,
timeout,
out duration,
lba);
byte[] deinterleaved = DeinterleaveEccBlock(buffer, transferLength, _detectedBufferStride, _bufferFormat);
if(!CheckSectorNumber(deinterleaved, lba, transferLength, layerbreak, true))
{
// Buffer offset lost - this means we've wrapped around
// Use the number of sectors read to detect buffer capacity
if(_totalSectorsRead > 0 && _totalSectorsRead >= 16 && _totalSectorsRead <= 2000)
{
uint detectedCapacity = _totalSectorsRead;
uint oldCapacity = _bufferCapacityInSectors;
// If we already have a capacity, verify new detection is consistent
if(_bufferCapacityInSectors == 714 || // Update if using default
(detectedCapacity >= _bufferCapacityInSectors * 9 / 10 &&
detectedCapacity <= _bufferCapacityInSectors * 11 / 10)) // Or within 10%
{
_bufferCapacityInSectors = detectedCapacity;
AaruLogging.Debug(SCSI_MODULE_NAME,
"Buffer capacity dynamically detected: {0} sectors (was {1})",
detectedCapacity, oldCapacity);
}
}
// Reset tracking for next cycle
_totalSectorsRead = 0;
// 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 * _detectedBufferStride,
transferLength * _detectedBufferStride,
timeout,
out duration,
lba);
deinterleaved = DeinterleaveEccBlock(buffer, transferLength, _detectedBufferStride, _bufferFormat);
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;
_totalSectorsRead += transferLength; // Track successful read for capacity detection
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 ReadOnlySpan<byte> senseBuffer, uint lba,
uint transferLength, uint timeout, out double duration,
uint layerbreak, bool otp)
{
uint newTransferLength1 = _bufferCapacityInSectors - _bufferOffset;
uint newTransferLength2 = transferLength - newTransferLength1;
bool sense1 = LiteOnReadBuffer(out byte[] buffer1,
out _,
_bufferOffset * _detectedBufferStride,
newTransferLength1 * _detectedBufferStride,
timeout,
out double duration1,
lba);
bool sense2 = LiteOnReadBuffer(out byte[] buffer2,
out _,
0,
newTransferLength2 * _detectedBufferStride,
timeout,
out double duration2,
lba);
senseBuffer = SenseBuffer; // TODO
buffer = new byte[_detectedBufferStride * transferLength];
Array.Copy(buffer1, buffer, buffer1.Length);
Array.Copy(buffer2, 0, buffer, buffer1.Length, buffer2.Length);
duration = duration1 + duration2;
byte[] deinterleaved = DeinterleaveEccBlock(buffer, transferLength, _detectedBufferStride, _bufferFormat);
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;
_totalSectorsRead += transferLength; // Track successful read for capacity detection
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 < _bufferCapacityInSectors; i++)
{
LiteOnReadBuffer(out byte[] buffer, out _, i * _detectedBufferStride, _detectedBufferStride, timeout,
out double _, lba);
byte[] deinterleaved = DeinterleaveEccBlock(buffer, 1, _detectedBufferStride, _bufferFormat);
if(CheckSectorNumber(deinterleaved, lba, 1, layerbreak, otp)) return (int)i;
}
return -1;
}
/// <summary>
/// Deinterleave the ECC block based on detected format
/// </summary>
/// <param name="buffer">Data buffer</param>
/// <param name="transferLength">How many blocks in buffer</param>
/// <param name="stride">Bytes per sector in buffer</param>
/// <param name="format">Buffer format type</param>
/// <returns>The deinterleaved sectors</returns>
private byte[] DeinterleaveEccBlock(byte[] buffer, uint transferLength, uint stride, LiteOnBufferFormat format)
{
return format switch
{
LiteOnBufferFormat.FullEccInterleaved => DeinterleaveFullEccInterleaved(buffer, transferLength, stride),
LiteOnBufferFormat.PoOnly => DeinterleavePoOnly(buffer, transferLength, stride),
LiteOnBufferFormat.SectorDataOnly => buffer, // No deinterleaving needed for sector-data-only format
LiteOnBufferFormat.FullEccWithPadding => DeinterleaveFullEccWithPadding(buffer, transferLength, stride),
_ => DeinterleaveFullEccInterleaved(buffer, transferLength, stride) // Default fallback
};
}
}

View File

@@ -32,6 +32,7 @@
using System;
using System.Collections.Generic;
using Aaru.CommonTypes.Enums;
using Aaru.Helpers;
using Aaru.Logging;
@@ -44,6 +45,21 @@ public partial class Device
private byte _detectedReadBufferId; // Detected buffer ID (0x00, 0x01, 0x02)
private uint _detectedBufferStride; // Detected stride in bytes per sector
private bool _readBuffer3CDetected; // Flag to track if detection has been performed
// Buffer format and management fields
private enum BufferFormat
{
Unknown = 0,
FullEccInterleaved,
PoOnly,
SectorDataOnly,
FullEccWithPadding
}
private uint _bufferOffset;
private uint _bufferCapacityInSectors;
private BufferFormat _bufferFormat;
private uint _totalSectorsRead; // Tracks cumulative sectors read successfully since last buffer reset
/// <summary>Reads from device buffer using SCSI READ BUFFER command with specified variant</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 response will be stored</param>
@@ -409,5 +425,306 @@ public partial class Device
return true;
}
/// <summary>Reads a "raw" sector from DVD using ReadBuffer 3C command.</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="lba">Start block address.</param>
/// <param name="transferLength">How many blocks to read.</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="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 ReadBuffer3CRawDvd(out byte[] buffer, out ReadOnlySpan<byte> senseBuffer, uint lba, uint transferLength,
uint timeout, out double duration, uint layerbreak, bool otp)
{
// Detect ReadBuffer 3C variant and stride on first call
if(!_readBuffer3CDetected)
{
bool detected = DetectReadBuffer3C(lba, timeout, out double detectDuration);
if(!detected || _detectedBufferStride == 0 || _detectedBufferStride < 2064 || _detectedBufferStride > 10000)
{
// Detection failed - raw reading is not supported on this drive
AaruLogging.Debug(SCSI_MODULE_NAME,
"ReadBuffer 3C detection failed - raw reading is not supported on this drive");
buffer = Array.Empty<byte>();
senseBuffer = SenseBuffer;
duration = detectDuration;
Error = true;
_readBuffer3CDetected = true;
return true; // Return failure - raw reading not supported
}
// Detect format based on stride
_bufferFormat = _detectedBufferStride switch
{
2064 => BufferFormat.SectorDataOnly,
2236 => BufferFormat.PoOnly,
2384 => BufferFormat.FullEccInterleaved,
> 2384 => BufferFormat.FullEccWithPadding,
_ => BufferFormat.FullEccInterleaved // Default for backward compatibility
};
AaruLogging.Debug(SCSI_MODULE_NAME,
"ReadBuffer 3C buffer format detected based on stride: {0}, format: {1}",
_detectedBufferStride, _bufferFormat);
// Initialize buffer capacity with default value (will be refined dynamically when offset is lost)
_bufferCapacityInSectors = 714;
_totalSectorsRead = 0; // Initialize tracking for dynamic capacity detection
_readBuffer3CDetected = true;
}
_bufferOffset %= _bufferCapacityInSectors;
bool sense;
if(layerbreak > 0 && transferLength > 1 && lba + 0x30000 > layerbreak - 256 && lba + 0x30000 < layerbreak + 256)
{
buffer = new byte[transferLength * 2064];
duration = 0;
senseBuffer = SenseBuffer;
return true;
}
if(_bufferCapacityInSectors - _bufferOffset < transferLength)
{
sense = ReadSectorsAcrossBufferBorder(out buffer,
out senseBuffer,
lba,
transferLength,
timeout,
out duration,
layerbreak,
otp);
}
else
{
sense = ReadSectorsFromBuffer(out buffer,
out senseBuffer,
lba,
transferLength,
timeout,
out duration,
layerbreak,
otp);
}
Error = LastError != 0;
AaruLogging.Debug(SCSI_MODULE_NAME, "ReadBuffer 3C READ DVD RAW took {0} ms", duration);
return sense;
}
/// <summary>
/// Reads the 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">How many blocks to read.</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 ReadBuffer3CInternal(out byte[] buffer, out ReadOnlySpan<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);
// Use generic ReadBuffer method with detected variant
return ScsiReadBuffer(out buffer, out senseBuffer, bufferOffset, transferLength, timeout, out duration,
_detectedReadBufferMode, _detectedReadBufferId);
}
/// <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 ReadSectorsFromBuffer(out byte[] buffer, out ReadOnlySpan<byte> senseBuffer, uint lba,
uint transferLength, uint timeout, out double duration, uint layerbreak,
bool otp)
{
bool sense = ReadBuffer3CInternal(out buffer,
out senseBuffer,
_bufferOffset * _detectedBufferStride,
transferLength * _detectedBufferStride,
timeout,
out duration,
lba);
byte[] deinterleaved = DeinterleaveEccBlock(buffer, transferLength, _detectedBufferStride, _bufferFormat);
if(!CheckSectorNumber(deinterleaved, lba, transferLength, layerbreak, true))
{
// Buffer offset lost - this means we've wrapped around
// Use the number of sectors read to detect buffer capacity
if(_totalSectorsRead > 0 && _totalSectorsRead >= 16 && _totalSectorsRead <= 2000)
{
uint detectedCapacity = _totalSectorsRead;
uint oldCapacity = _bufferCapacityInSectors;
// If we already have a capacity, verify new detection is consistent
if(_bufferCapacityInSectors == 714 || // Update if using default
(detectedCapacity >= _bufferCapacityInSectors * 9 / 10 &&
detectedCapacity <= _bufferCapacityInSectors * 11 / 10)) // Or within 10%
{
_bufferCapacityInSectors = detectedCapacity;
AaruLogging.Debug(SCSI_MODULE_NAME,
"Buffer capacity dynamically detected: {0} sectors (was {1})",
detectedCapacity, oldCapacity);
}
}
// Reset tracking for next cycle
_totalSectorsRead = 0;
// Buffer offset lost, try to find it again
int offset = FindBufferOffset(lba, timeout, layerbreak, otp);
if(offset == -1) return true;
_bufferOffset = (uint)offset;
sense = ReadBuffer3CInternal(out buffer,
out senseBuffer,
_bufferOffset * _detectedBufferStride,
transferLength * _detectedBufferStride,
timeout,
out duration,
lba);
deinterleaved = DeinterleaveEccBlock(buffer, transferLength, _detectedBufferStride, _bufferFormat);
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;
_totalSectorsRead += transferLength; // Track successful read for capacity detection
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 ReadSectorsAcrossBufferBorder(out byte[] buffer, out ReadOnlySpan<byte> senseBuffer, uint lba,
uint transferLength, uint timeout, out double duration,
uint layerbreak, bool otp)
{
uint newTransferLength1 = _bufferCapacityInSectors - _bufferOffset;
uint newTransferLength2 = transferLength - newTransferLength1;
bool sense1 = ReadBuffer3CInternal(out byte[] buffer1,
out _,
_bufferOffset * _detectedBufferStride,
newTransferLength1 * _detectedBufferStride,
timeout,
out double duration1,
lba);
bool sense2 = ReadBuffer3CInternal(out byte[] buffer2,
out _,
0,
newTransferLength2 * _detectedBufferStride,
timeout,
out double duration2,
lba);
senseBuffer = SenseBuffer; // TODO
buffer = new byte[_detectedBufferStride * transferLength];
Array.Copy(buffer1, buffer, buffer1.Length);
Array.Copy(buffer2, 0, buffer, buffer1.Length, buffer2.Length);
duration = duration1 + duration2;
byte[] deinterleaved = DeinterleaveEccBlock(buffer, transferLength, _detectedBufferStride, _bufferFormat);
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;
_totalSectorsRead += transferLength; // Track successful read for capacity detection
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 < _bufferCapacityInSectors; i++)
{
ReadBuffer3CInternal(out byte[] buffer, out _, i * _detectedBufferStride, _detectedBufferStride, timeout,
out double _, lba);
byte[] deinterleaved = DeinterleaveEccBlock(buffer, 1, _detectedBufferStride, _bufferFormat);
if(CheckSectorNumber(deinterleaved, lba, 1, layerbreak, otp)) return (int)i;
}
return -1;
}
/// <summary>
/// Deinterleave the ECC block based on detected format
/// </summary>
/// <param name="buffer">Data buffer</param>
/// <param name="transferLength">How many blocks in buffer</param>
/// <param name="stride">Bytes per sector in buffer</param>
/// <param name="format">Buffer format type</param>
/// <returns>The deinterleaved sectors</returns>
private byte[] DeinterleaveEccBlock(byte[] buffer, uint transferLength, uint stride, BufferFormat format)
{
return format switch
{
BufferFormat.FullEccInterleaved => DeinterleaveFullEccInterleaved(buffer, transferLength, stride),
BufferFormat.PoOnly => DeinterleavePoOnly(buffer, transferLength, stride),
BufferFormat.SectorDataOnly => buffer, // No deinterleaving needed for sector-data-only format
BufferFormat.FullEccWithPadding => DeinterleaveFullEccWithPadding(buffer, transferLength, stride),
_ => DeinterleaveFullEccInterleaved(buffer, transferLength, stride) // Default fallback
};
}
}

View File

@@ -6839,6 +6839,12 @@ namespace Aaru.Localization {
}
}
public static string Using_ReadBuffer_3C_raw_DVD_reading {
get {
return ResourceManager.GetString("Using_ReadBuffer_3C_raw_DVD_reading", resourceCulture);
}
}
public static string Filesystems_Identify_Error {
get {
return ResourceManager.GetString("Filesystems_Identify_Error", resourceCulture);

View File

@@ -3490,6 +3490,9 @@ It has no sense to do it, and it will put too much strain on the tape.</value>
<data name="Using_Lite-On_raw_DVD_reading" xml:space="preserve">
<value>[slateblue1]Using [fuchsia]Lite-On raw DVD[/] reading[/]</value>
</data>
<data name="Using_ReadBuffer_3C_raw_DVD_reading" xml:space="preserve">
<value>[slateblue1]Using [fuchsia]ReadBuffer 3C raw DVD[/] reading[/]</value>
</data>
<data name="Filesystems_Identify_Error" xml:space="preserve">
<value>Error identifying filesystem {0}. Please open a report with the following line in a Github issue.</value>
</data>