Files
Aaru/Aaru.Core/Devices/Report/SSC.cs

290 lines
11 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : SSC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Creates reports from SCSI Streaming devices.
//
// --[ 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
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Core.Devices.Report;
2021-09-13 18:08:44 +01:00
using System;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Metadata;
using Aaru.Decoders.SCSI;
using Aaru.Decoders.SCSI.SSC;
using Aaru.Devices;
2022-03-07 07:36:44 +00:00
using global::Spectre.Console;
2022-03-06 13:29:38 +00:00
public sealed partial class DeviceReport
{
2022-03-06 13:29:38 +00:00
/// <summary>Creates a report from a SCSI Sequential Commands device</summary>
/// <returns>SSC report</returns>
public Ssc ReportScsiSsc()
{
2022-03-06 13:29:38 +00:00
var report = new Ssc();
2022-03-07 07:36:44 +00:00
var sense = true;
2022-03-06 13:29:38 +00:00
byte[] buffer = Array.Empty<byte>();
Spectre.ProgressSingleSpinner(ctx =>
{
2022-03-06 13:29:38 +00:00
ctx.AddTask("Querying SCSI READ BLOCK LIMITS...").IsIndeterminate();
sense = _dev.ReadBlockLimits(out buffer, out _, _dev.Timeout, out _);
});
2021-09-13 18:08:44 +01:00
2022-03-06 13:29:38 +00:00
if(!sense)
{
BlockLimits.BlockLimitsData? decBl = BlockLimits.Decode(buffer);
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(decBl?.granularity > 0)
report.BlockSizeGranularity = decBl.Value.granularity;
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(decBl?.maxBlockLen > 0)
report.MaxBlockLength = decBl.Value.maxBlockLen;
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
if(decBl?.minBlockLen > 0)
report.MinBlockLength = decBl.Value.minBlockLen;
}
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Querying SCSI REPORT DENSITY SUPPORT...").IsIndeterminate();
sense = _dev.ReportDensitySupport(out buffer, out _, false, false, _dev.Timeout, out _);
});
2022-03-06 13:29:38 +00:00
if(!sense)
{
DensitySupport.DensitySupportHeader? dsh = DensitySupport.DecodeDensity(buffer);
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(dsh.HasValue)
{
2022-03-07 07:36:44 +00:00
var array = new SupportedDensity[dsh.Value.descriptors.Length];
2019-12-27 18:00:03 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < dsh.Value.descriptors.Length; i++)
2022-03-06 13:29:38 +00:00
array[i] = new SupportedDensity
{
BitsPerMm = dsh.Value.descriptors[i].bpmm,
Capacity = dsh.Value.descriptors[i].capacity,
DefaultDensity = dsh.Value.descriptors[i].defaultDensity,
Description = dsh.Value.descriptors[i].description,
Duplicate = dsh.Value.descriptors[i].duplicate,
Name = dsh.Value.descriptors[i].name,
Organization = dsh.Value.descriptors[i].organization,
PrimaryCode = dsh.Value.descriptors[i].primaryCode,
SecondaryCode = dsh.Value.descriptors[i].secondaryCode,
Tracks = dsh.Value.descriptors[i].tracks,
Width = dsh.Value.descriptors[i].width,
Writable = dsh.Value.descriptors[i].writable
};
2022-03-06 13:29:38 +00:00
report.SupportedDensities = array.ToList();
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Querying SCSI REPORT DENSITY SUPPORT for medium types...").IsIndeterminate();
sense = _dev.ReportDensitySupport(out buffer, out _, true, false, _dev.Timeout, out _);
});
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(sense)
return report;
2022-03-06 13:29:38 +00:00
DensitySupport.MediaTypeSupportHeader? mtsh = DensitySupport.DecodeMediumType(buffer);
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(!mtsh.HasValue)
return report;
2022-03-07 07:36:44 +00:00
var array2 = new SscSupportedMedia[mtsh.Value.descriptors.Length];
2019-12-27 18:00:03 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < mtsh.Value.descriptors.Length; i++)
2022-03-06 13:29:38 +00:00
{
array2[i] = new SscSupportedMedia
{
2022-03-06 13:29:38 +00:00
Description = mtsh.Value.descriptors[i].description,
Length = mtsh.Value.descriptors[i].length,
MediumType = mtsh.Value.descriptors[i].mediumType,
Name = mtsh.Value.descriptors[i].name,
Organization = mtsh.Value.descriptors[i].organization,
Width = mtsh.Value.descriptors[i].width
};
if(mtsh.Value.descriptors[i].densityCodes == null)
continue;
2022-03-07 07:36:44 +00:00
var array3 = new DensityCode[mtsh.Value.descriptors[i].densityCodes.Length];
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
for(var j = 0; j < mtsh.Value.descriptors[i].densityCodes.Length; j++)
2022-03-06 13:29:38 +00:00
array3[j] = new DensityCode
{
2022-03-06 13:29:38 +00:00
Code = mtsh.Value.descriptors[i].densityCodes[j]
};
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
array2[i].DensityCodes = array3.Distinct().ToList();
}
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
report.SupportedMediaTypes = array2.ToList();
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
return report;
}
2022-03-06 13:29:38 +00:00
/// <summary>Creates a report for media inserted into an SSC device</summary>
/// <returns>Media report</returns>
public TestedSequentialMedia ReportSscMedia()
{
var seqTest = new TestedSequentialMedia();
2022-03-07 07:36:44 +00:00
var sense = true;
2022-03-06 13:29:38 +00:00
byte[] buffer = Array.Empty<byte>();
2022-03-06 13:29:38 +00:00
Modes.DecodedMode? decMode = null;
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
2022-03-06 13:29:38 +00:00
ctx.AddTask("Querying SCSI MODE SENSE (10)...").IsIndeterminate();
2022-03-06 13:29:38 +00:00
sense = _dev.ModeSense10(out buffer, out _, false, true, ScsiModeSensePageControl.Current, 0x3F, 0x00,
_dev.Timeout, out _);
});
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(!sense &&
!_dev.Error)
{
decMode = Modes.DecodeMode10(buffer, _dev.ScsiType);
seqTest.ModeSense10Data = buffer;
}
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Querying SCSI MODE SENSE...").IsIndeterminate();
sense = _dev.ModeSense(out buffer, out _, _dev.Timeout, out _);
});
2022-03-06 13:29:38 +00:00
if(!sense &&
!_dev.Error)
{
decMode ??= Modes.DecodeMode6(buffer, _dev.ScsiType);
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
seqTest.ModeSense6Data = buffer;
}
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(decMode.HasValue)
{
seqTest.MediumType = (byte)decMode.Value.Header.MediumType;
2022-03-06 13:29:38 +00:00
if(decMode.Value.Header.BlockDescriptors?.Length > 0)
seqTest.Density = (byte)decMode.Value.Header.BlockDescriptors?[0].Density;
}
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Querying SCSI REPORT DENSITY SUPPORT for current media...").IsIndeterminate();
sense = _dev.ReportDensitySupport(out buffer, out _, false, true, _dev.Timeout, out _);
});
2022-03-06 13:29:38 +00:00
if(!sense)
{
DensitySupport.DensitySupportHeader? dsh = DensitySupport.DecodeDensity(buffer);
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(dsh.HasValue)
{
2022-03-07 07:36:44 +00:00
var array = new SupportedDensity[dsh.Value.descriptors.Length];
2019-12-27 18:00:03 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < dsh.Value.descriptors.Length; i++)
2022-03-06 13:29:38 +00:00
array[i] = new SupportedDensity
{
BitsPerMm = dsh.Value.descriptors[i].bpmm,
Capacity = dsh.Value.descriptors[i].capacity,
DefaultDensity = dsh.Value.descriptors[i].defaultDensity,
Description = dsh.Value.descriptors[i].description,
Duplicate = dsh.Value.descriptors[i].duplicate,
Name = dsh.Value.descriptors[i].name,
Organization = dsh.Value.descriptors[i].organization,
PrimaryCode = dsh.Value.descriptors[i].primaryCode,
SecondaryCode = dsh.Value.descriptors[i].secondaryCode,
Tracks = dsh.Value.descriptors[i].tracks,
Width = dsh.Value.descriptors[i].width,
Writable = dsh.Value.descriptors[i].writable
};
2022-03-06 13:29:38 +00:00
seqTest.SupportedDensities = array.ToList();
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
2022-03-07 07:36:44 +00:00
ctx.AddTask("Querying SCSI REPORT DENSITY SUPPORT for medium types for current media...").IsIndeterminate();
2021-09-13 18:08:44 +01:00
2022-03-06 13:29:38 +00:00
sense = _dev.ReportDensitySupport(out buffer, out _, true, true, _dev.Timeout, out _);
});
2019-12-27 18:00:03 +00:00
2022-03-06 13:29:38 +00:00
if(!sense)
{
DensitySupport.MediaTypeSupportHeader? mtsh = DensitySupport.DecodeMediumType(buffer);
if(mtsh.HasValue)
{
2022-03-07 07:36:44 +00:00
var array = new SscSupportedMedia[mtsh.Value.descriptors.Length];
2019-12-27 18:00:03 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < mtsh.Value.descriptors.Length; i++)
{
2022-03-06 13:29:38 +00:00
array[i] = new SscSupportedMedia
{
2022-03-06 13:29:38 +00:00
Description = mtsh.Value.descriptors[i].description,
Length = mtsh.Value.descriptors[i].length,
MediumType = mtsh.Value.descriptors[i].mediumType,
Name = mtsh.Value.descriptors[i].name,
Organization = mtsh.Value.descriptors[i].organization,
Width = mtsh.Value.descriptors[i].width
};
2022-03-06 13:29:38 +00:00
if(mtsh.Value.descriptors[i].densityCodes == null)
continue;
2019-12-27 18:00:03 +00:00
2022-03-07 07:36:44 +00:00
var array2 = new DensityCode[mtsh.Value.descriptors[i].densityCodes.Length];
2019-12-27 18:00:03 +00:00
2022-03-07 07:36:44 +00:00
for(var j = 0; j < mtsh.Value.descriptors[i].densityCodes.Length; j++)
2022-03-06 13:29:38 +00:00
array2[j] = new DensityCode
{
Code = mtsh.Value.descriptors[i].densityCodes[j]
};
2022-03-06 13:29:38 +00:00
array[i].DensityCodes = array2.Distinct().ToList();
}
2022-03-06 13:29:38 +00:00
seqTest.SupportedMediaTypes = array.ToList();
}
2022-03-06 13:29:38 +00:00
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask("Trying SCSI READ MEDIA SERIAL NUMBER...").IsIndeterminate();
seqTest.CanReadMediaSerial = !_dev.ReadMediaSerialNumber(out buffer, out _, _dev.Timeout, out _);
});
2022-03-06 13:29:38 +00:00
return seqTest;
}
2017-12-19 20:33:03 +00:00
}