Files
Aaru/SCSI/Modes/Mode10.cs

360 lines
14 KiB
C#
Raw Normal View History

2017-12-21 02:03:21 +00:00
// /***************************************************************************
2020-02-27 12:31:23 +00:00
// Aaru Data Preservation Suite
2017-12-21 02:03:21 +00:00
// ----------------------------------------------------------------------------
//
// Filename : Mode10.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Device structures decoders.
//
// --[ Description ] ----------------------------------------------------------
//
// Decodes and encodines SCSI modes in MODE SENSE/SELECT (10) format.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:39 +00:00
// Copyright © 2011-2022 Natalia Portillo
2017-12-21 02:03:21 +00:00
// ****************************************************************************/
2022-03-07 07:36:42 +00:00
namespace Aaru.Decoders.SCSI;
2017-12-21 02:03:21 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
2020-02-27 00:33:24 +00:00
using Aaru.CommonTypes.Structs.Devices.SCSI;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
[SuppressMessage("ReSharper", "InconsistentNaming"), SuppressMessage("ReSharper", "MemberCanBeInternal"),
SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public static partial class Modes
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
public static ModeHeader? DecodeModeHeader10(byte[] modeResponse, PeripheralDeviceTypes deviceType)
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
if(modeResponse == null ||
modeResponse.Length < 8)
return null;
2017-12-21 02:03:21 +00:00
2022-03-07 07:36:42 +00:00
var modeLength = (ushort)((modeResponse[0] << 8) + modeResponse[1]);
var blockDescLength = (ushort)((modeResponse[6] << 8) + modeResponse[7]);
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(modeResponse.Length < modeLength)
return null;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
var header = new ModeHeader
{
MediumType = (MediumTypes)modeResponse[2]
};
bool longLBA = (modeResponse[4] & 0x01) == 0x01;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(blockDescLength > 0)
if(longLBA)
{
header.BlockDescriptors = new BlockDescriptor[blockDescLength / 16];
2017-12-21 02:03:21 +00:00
2022-03-07 07:36:42 +00:00
for(var i = 0; i < header.BlockDescriptors.Length; i++)
2017-12-21 02:03:21 +00:00
{
2022-03-07 07:36:42 +00:00
if(12 + i * 16 + 8 >= modeResponse.Length)
2022-03-06 13:29:37 +00:00
break;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
header.BlockDescriptors[i] = new BlockDescriptor
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
Density = DensityType.Default
};
2022-03-07 07:36:42 +00:00
var temp = new byte[8];
temp[0] = modeResponse[7 + i * 16 + 8];
temp[1] = modeResponse[6 + i * 16 + 8];
temp[2] = modeResponse[5 + i * 16 + 8];
temp[3] = modeResponse[4 + i * 16 + 8];
temp[4] = modeResponse[3 + i * 16 + 8];
temp[5] = modeResponse[2 + i * 16 + 8];
temp[6] = modeResponse[1 + i * 16 + 8];
temp[7] = modeResponse[0 + i * 16 + 8];
2022-03-06 13:29:37 +00:00
header.BlockDescriptors[i].Blocks = BitConverter.ToUInt64(temp, 0);
2022-03-07 07:36:42 +00:00
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[15 + i * 16 + 8] << 24);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[14 + i * 16 + 8] << 16);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[13 + i * 16 + 8] << 8);
header.BlockDescriptors[i].BlockLength += modeResponse[12 + i * 16 + 8];
2017-12-21 02:03:21 +00:00
}
2022-03-06 13:29:37 +00:00
}
else
{
header.BlockDescriptors = new BlockDescriptor[blockDescLength / 8];
2022-03-07 07:36:42 +00:00
for(var i = 0; i < header.BlockDescriptors.Length; i++)
2017-12-21 02:03:21 +00:00
{
2022-03-07 07:36:42 +00:00
if(7 + i * 8 + 8 >= modeResponse.Length)
2022-03-06 13:29:37 +00:00
break;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
header.BlockDescriptors[i] = new BlockDescriptor();
if(deviceType != PeripheralDeviceTypes.DirectAccess)
2022-03-07 07:36:42 +00:00
header.BlockDescriptors[i].Density = (DensityType)modeResponse[0 + i * 8 + 8];
2022-03-06 13:29:37 +00:00
else
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
header.BlockDescriptors[i].Density = DensityType.Default;
2022-03-07 07:36:42 +00:00
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[0 + i * 8 + 8] << 24);
2017-12-21 02:03:21 +00:00
}
2022-03-07 07:36:42 +00:00
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[1 + i * 8 + 8] << 16);
header.BlockDescriptors[i].Blocks += (ulong)(modeResponse[2 + i * 8 + 8] << 8);
header.BlockDescriptors[i].Blocks += modeResponse[3 + i * 8 + 8];
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[5 + i * 8 + 8] << 16);
header.BlockDescriptors[i].BlockLength += (uint)(modeResponse[6 + i * 8 + 8] << 8);
header.BlockDescriptors[i].BlockLength += modeResponse[7 + i * 8 + 8];
2022-03-06 13:29:37 +00:00
}
2017-12-21 02:03:21 +00:00
}
2022-03-06 13:29:37 +00:00
switch(deviceType)
{
case PeripheralDeviceTypes.DirectAccess:
case PeripheralDeviceTypes.MultiMediaDevice:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.DPOFUA = (modeResponse[3] & 0x10) == 0x10;
break;
case PeripheralDeviceTypes.SequentialAccess:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.Speed = (byte)(modeResponse[3] & 0x0F);
header.BufferedMode = (byte)((modeResponse[3] & 0x70) >> 4);
break;
case PeripheralDeviceTypes.PrinterDevice:
header.BufferedMode = (byte)((modeResponse[3] & 0x70) >> 4);
break;
case PeripheralDeviceTypes.OpticalDevice:
header.WriteProtected = (modeResponse[3] & 0x80) == 0x80;
header.EBC = (modeResponse[3] & 0x01) == 0x01;
header.DPOFUA = (modeResponse[3] & 0x10) == 0x10;
break;
2017-12-21 02:03:21 +00:00
}
2022-03-06 13:29:37 +00:00
return header;
}
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
public static string PrettifyModeHeader10(byte[] modeResponse, PeripheralDeviceTypes deviceType) =>
PrettifyModeHeader(DecodeModeHeader10(modeResponse, deviceType), deviceType);
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
public static DecodedMode? DecodeMode10(byte[] modeResponse, PeripheralDeviceTypes deviceType)
{
ModeHeader? hdr = DecodeModeHeader10(modeResponse, deviceType);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(!hdr.HasValue)
return null;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
var decoded = new DecodedMode
{
Header = hdr.Value
};
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
bool longlba = (modeResponse[4] & 0x01) == 0x01;
int offset;
2022-03-07 07:36:42 +00:00
var blkDrLength = 0;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(decoded.Header.BlockDescriptors != null)
blkDrLength = decoded.Header.BlockDescriptors.Length;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(longlba)
2022-03-07 07:36:42 +00:00
offset = 8 + blkDrLength * 16;
2022-03-06 13:29:37 +00:00
else
2022-03-07 07:36:42 +00:00
offset = 8 + blkDrLength * 8;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
int length = modeResponse[0] << 8;
length += modeResponse[1];
length += 2;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(length != modeResponse.Length)
return decoded;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
List<ModePage> listpages = new();
while(offset < modeResponse.Length)
{
bool isSubpage = (modeResponse[offset] & 0x40) == 0x40;
var pg = new ModePage();
2022-03-07 07:36:42 +00:00
var pageNo = (byte)(modeResponse[offset] & 0x3F);
2022-03-06 13:29:37 +00:00
if(pageNo == 0)
{
pg.PageResponse = new byte[modeResponse.Length - offset];
Array.Copy(modeResponse, offset, pg.PageResponse, 0, pg.PageResponse.Length);
pg.Page = 0;
pg.Subpage = 0;
offset += pg.PageResponse.Length;
}
else
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
if(isSubpage && offset + 3 < modeResponse.Length)
{
pg.PageResponse = new byte[(modeResponse[offset + 2] << 8) + modeResponse[offset + 3] + 4];
int copyLen = pg.PageResponse.Length;
if(pg.PageResponse.Length + offset > modeResponse.Length)
copyLen = modeResponse.Length - offset;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
Array.Copy(modeResponse, offset, pg.PageResponse, 0, copyLen);
pg.Page = (byte)(modeResponse[offset] & 0x3F);
pg.Subpage = modeResponse[offset + 1];
offset += pg.PageResponse.Length;
}
else if(isSubpage && offset + 1 < modeResponse.Length)
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
pg.PageResponse = new byte[modeResponse[offset + 1] + 2];
int copyLen = pg.PageResponse.Length;
if(pg.PageResponse.Length + offset > modeResponse.Length)
copyLen = modeResponse.Length - offset;
Array.Copy(modeResponse, offset, pg.PageResponse, 0, copyLen);
pg.Page = (byte)(modeResponse[offset] & 0x3F);
2018-06-22 08:08:38 +01:00
pg.Subpage = 0;
offset += pg.PageResponse.Length;
2017-12-21 02:03:21 +00:00
}
else
2022-03-06 13:29:37 +00:00
offset = modeResponse.Length;
}
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
listpages.Add(pg);
}
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
decoded.Pages = listpages.ToArray();
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
return decoded;
}
2017-12-21 02:03:21 +00:00
2022-03-07 07:36:42 +00:00
public static byte[] EncodeModeHeader10(ModeHeader header, PeripheralDeviceTypes deviceType, bool longLBA = false)
2022-03-06 13:29:37 +00:00
{
byte[] hdr;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(header.BlockDescriptors != null)
2022-03-07 07:36:42 +00:00
hdr = longLBA ? new byte[8 + header.BlockDescriptors.Length * 16]
: new byte[8 + header.BlockDescriptors.Length * 8];
2022-03-06 13:29:37 +00:00
else
hdr = new byte[8];
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
hdr[2] = (byte)header.MediumType;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
switch(deviceType)
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
case PeripheralDeviceTypes.DirectAccess:
case PeripheralDeviceTypes.MultiMediaDevice:
if(header.WriteProtected)
hdr[3] += 0x80;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(header.DPOFUA)
hdr[3] += 0x10;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
break;
case PeripheralDeviceTypes.SequentialAccess:
if(header.WriteProtected)
hdr[3] += 0x80;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
hdr[3] += (byte)(header.Speed & 0x0F);
hdr[3] += (byte)((header.BufferedMode << 4) & 0x70);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case PeripheralDeviceTypes.PrinterDevice:
hdr[3] += (byte)((header.BufferedMode << 4) & 0x70);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case PeripheralDeviceTypes.OpticalDevice:
if(header.WriteProtected)
hdr[3] += 0x80;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(header.EBC)
hdr[3] += 0x01;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(header.DPOFUA)
hdr[3] += 0x10;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
}
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(longLBA)
hdr[4] += 0x01;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(header.BlockDescriptors == null)
return hdr;
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
if(longLBA)
2022-03-07 07:36:42 +00:00
for(var i = 0; i < header.BlockDescriptors.Length; i++)
2022-03-06 13:29:37 +00:00
{
byte[] temp = BitConverter.GetBytes(header.BlockDescriptors[i].Blocks);
2022-03-07 07:36:42 +00:00
hdr[7 + i * 16 + 8] = temp[0];
hdr[6 + i * 16 + 8] = temp[1];
hdr[5 + i * 16 + 8] = temp[2];
hdr[4 + i * 16 + 8] = temp[3];
hdr[3 + i * 16 + 8] = temp[4];
hdr[2 + i * 16 + 8] = temp[5];
hdr[1 + i * 16 + 8] = temp[6];
hdr[0 + i * 16 + 8] = temp[7];
hdr[12 + i * 16 + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF000000) >> 24);
hdr[13 + i * 16 + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF0000) >> 16);
hdr[14 + i * 16 + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF00) >> 8);
hdr[15 + i * 16 + 8] = (byte)(header.BlockDescriptors[i].BlockLength & 0xFF);
2022-03-06 13:29:37 +00:00
}
else
2022-03-07 07:36:42 +00:00
for(var i = 0; i < header.BlockDescriptors.Length; i++)
2022-03-06 13:29:37 +00:00
{
if(deviceType != PeripheralDeviceTypes.DirectAccess)
2022-03-07 07:36:42 +00:00
hdr[0 + i * 8 + 8] = (byte)header.BlockDescriptors[i].Density;
2022-03-06 13:29:37 +00:00
else
2022-03-07 07:36:42 +00:00
hdr[0 + i * 8 + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF000000) >> 24);
hdr[1 + i * 8 + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF0000) >> 16);
hdr[2 + i * 8 + 8] = (byte)((header.BlockDescriptors[i].Blocks & 0xFF00) >> 8);
hdr[3 + i * 8 + 8] = (byte)(header.BlockDescriptors[i].Blocks & 0xFF);
hdr[5 + i * 8 + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF0000) >> 16);
hdr[6 + i * 8 + 8] = (byte)((header.BlockDescriptors[i].BlockLength & 0xFF00) >> 8);
hdr[7 + i * 8 + 8] = (byte)(header.BlockDescriptors[i].BlockLength & 0xFF);
2017-12-21 02:03:21 +00:00
}
2022-03-06 13:29:37 +00:00
return hdr;
}
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
public static byte[] EncodeMode10(DecodedMode mode, PeripheralDeviceTypes deviceType)
{
2022-03-07 07:36:42 +00:00
var modeSize = 0;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(mode.Pages != null)
modeSize += mode.Pages.Sum(page => page.PageResponse.Length);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
byte[] hdr = EncodeModeHeader10(mode.Header, deviceType);
modeSize += hdr.Length;
2022-03-07 07:36:42 +00:00
var md = new byte[modeSize];
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
Array.Copy(hdr, 0, md, 0, hdr.Length);
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
if(mode.Pages == null)
return md;
2017-12-21 02:03:21 +00:00
2022-03-06 13:29:37 +00:00
{
int offset = hdr.Length;
2022-03-06 13:29:37 +00:00
foreach(ModePage page in mode.Pages)
2017-12-21 02:03:21 +00:00
{
2022-03-06 13:29:37 +00:00
Array.Copy(page.PageResponse, 0, md, offset, page.PageResponse.Length);
offset += page.PageResponse.Length;
2017-12-21 02:03:21 +00:00
}
}
2022-03-06 13:29:37 +00:00
return md;
2017-12-21 02:03:21 +00:00
}
}