Files
Aaru/Aaru.Tests.Devices/SCSI/SMC.cs

313 lines
12 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-07-25 02:01:36 +01:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : SMC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
2020-07-25 02:01:36 +01:00
// Component : Aaru device testing.
//
// --[ 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.Tests.Devices.SCSI;
2017-12-21 14:30:38 +00:00
using System;
2020-02-27 00:33:26 +00:00
using Aaru.Console;
using Aaru.Decoders.SCSI;
using Aaru.Devices;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2022-03-07 07:36:44 +00:00
static class Smc
{
2022-03-06 13:29:38 +00:00
internal static void Menu(string devPath, Device dev)
{
2022-03-06 13:29:38 +00:00
while(true)
{
2022-03-07 07:36:44 +00:00
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
AaruConsole.WriteLine("Send a SCSI Media Changer command to the device:");
AaruConsole.WriteLine("1.- Send READ ATTRIBUTE command.");
AaruConsole.WriteLine("0.- Return to SCSI commands menu.");
AaruConsole.Write("Choose: ");
2020-02-29 18:03:35 +00:00
2022-03-07 07:36:44 +00:00
string strDev = Console.ReadLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!int.TryParse(strDev, out int item))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
switch(item)
{
2022-03-06 13:29:38 +00:00
case 0:
AaruConsole.WriteLine("Returning to SCSI commands menu...");
return;
case 1:
ReadAttribute(devPath, dev);
continue;
default:
AaruConsole.WriteLine("Incorrect option. Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2020-02-29 18:03:35 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
}
}
2022-03-06 13:29:38 +00:00
static void ReadAttribute(string devPath, Device dev)
{
ushort element = 0;
byte elementType = 0;
byte volume = 0;
byte partition = 0;
ushort firstAttribute = 0;
2022-03-07 07:36:44 +00:00
var cache = false;
2022-03-06 13:29:38 +00:00
ScsiAttributeAction action = ScsiAttributeAction.Values;
string strDev;
int item;
2022-03-07 07:36:44 +00:00
parameters:
2022-03-06 13:29:38 +00:00
while(true)
{
2022-03-07 07:36:44 +00:00
Console.Clear();
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Parameters for READ ATTRIBUTE command:");
AaruConsole.WriteLine("Action: {0}", action);
AaruConsole.WriteLine("Element: {0}", element);
AaruConsole.WriteLine("Element type: {0}", elementType);
AaruConsole.WriteLine("Volume: {0}", volume);
AaruConsole.WriteLine("Partition: {0}", partition);
AaruConsole.WriteLine("First attribute: {0}", firstAttribute);
AaruConsole.WriteLine("Use cache?: {0}", cache);
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine();
AaruConsole.WriteLine("Choose what to do:");
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("1.- Change parameters.");
AaruConsole.WriteLine("2.- Send command with these parameters.");
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("0.- Return to SCSI Media Changer commands menu.");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2020-02-29 18:03:35 +00:00
if(!int.TryParse(strDev, out item))
{
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Not a number. Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
}
switch(item)
{
case 0:
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Returning to SCSI Media Changer commands menu...");
return;
case 1:
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Attribute action");
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Available values: {0} {1} {2} {3} {4}", ScsiAttributeAction.Values,
ScsiAttributeAction.List, ScsiAttributeAction.VolumeList,
ScsiAttributeAction.PartitionList, ScsiAttributeAction.ElementList,
ScsiAttributeAction.Supported);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
AaruConsole.Write("Choose?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!Enum.TryParse(strDev, true, out action))
{
AaruConsole.WriteLine("Not a valid attribute action. Press any key to continue...");
action = ScsiAttributeAction.Values;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
AaruConsole.Write("Element?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!ushort.TryParse(strDev, out element))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
element = 0;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
continue;
}
AaruConsole.Write("Element type?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!byte.TryParse(strDev, out elementType))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
elementType = 0;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2022-03-06 13:29:38 +00:00
continue;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
AaruConsole.Write("Volume?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!byte.TryParse(strDev, out volume))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
volume = 0;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2022-03-06 13:29:38 +00:00
continue;
}
AaruConsole.Write("Partition?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!byte.TryParse(strDev, out partition))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
partition = 0;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2022-03-06 13:29:38 +00:00
continue;
}
AaruConsole.Write("First attribute?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!ushort.TryParse(strDev, out firstAttribute))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
firstAttribute = 0;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2022-03-06 13:29:38 +00:00
continue;
}
AaruConsole.Write("Use cache?: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!bool.TryParse(strDev, out cache))
{
AaruConsole.WriteLine("Not a boolean. Press any key to continue...");
cache = false;
2022-03-07 07:36:44 +00:00
Console.ReadKey();
2022-03-06 13:29:38 +00:00
}
break;
case 2: goto start;
}
}
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
start:
Console.Clear();
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
bool sense = dev.ReadAttribute(out byte[] buffer, out byte[] senseBuffer, action, element, elementType, volume,
partition, firstAttribute, cache, dev.Timeout, out double duration);
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
menu:
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
AaruConsole.WriteLine("Sending READ ATTRIBUTE to the device:");
AaruConsole.WriteLine("Command took {0} ms.", duration);
AaruConsole.WriteLine("Sense is {0}.", sense);
AaruConsole.WriteLine("Buffer is {0} bytes.", buffer?.Length.ToString() ?? "null");
AaruConsole.WriteLine("Buffer is null or empty? {0}", ArrayHelpers.ArrayIsNullOrEmpty(buffer));
AaruConsole.WriteLine("Sense buffer is {0} bytes.", senseBuffer?.Length.ToString() ?? "null");
AaruConsole.WriteLine("Sense buffer is null or empty? {0}", ArrayHelpers.ArrayIsNullOrEmpty(senseBuffer));
AaruConsole.WriteLine();
AaruConsole.WriteLine("Choose what to do:");
AaruConsole.WriteLine("1.- Print buffer.");
AaruConsole.WriteLine("2.- Print sense buffer.");
AaruConsole.WriteLine("3.- Decode sense buffer.");
AaruConsole.WriteLine("4.- Send command again.");
AaruConsole.WriteLine("5.- Change parameters.");
AaruConsole.WriteLine("0.- Return to SCSI Media Changer commands menu.");
AaruConsole.Write("Choose: ");
2022-03-07 07:36:44 +00:00
strDev = Console.ReadLine();
2022-03-06 13:29:38 +00:00
if(!int.TryParse(strDev, out item))
{
AaruConsole.WriteLine("Not a number. Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
Console.Clear();
2022-03-06 13:29:38 +00:00
goto menu;
}
switch(item)
{
case 0:
AaruConsole.WriteLine("Returning to SCSI Media Changer commands menu...");
return;
case 1:
2022-03-07 07:36:44 +00:00
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
AaruConsole.WriteLine("READ ATTRIBUTE response:");
if(buffer != null)
PrintHex.PrintHexArray(buffer, 64);
AaruConsole.WriteLine("Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
goto menu;
case 2:
2022-03-07 07:36:44 +00:00
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
AaruConsole.WriteLine("READ ATTRIBUTE sense:");
if(senseBuffer != null)
PrintHex.PrintHexArray(senseBuffer, 64);
AaruConsole.WriteLine("Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
goto menu;
case 3:
2022-03-07 07:36:44 +00:00
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
AaruConsole.WriteLine("READ ATTRIBUTE decoded sense:");
AaruConsole.Write("{0}", Sense.PrettifySense(senseBuffer));
AaruConsole.WriteLine("Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
Console.Clear();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Device: {0}", devPath);
goto menu;
case 4: goto start;
case 5: goto parameters;
default:
AaruConsole.WriteLine("Incorrect option. Press any key to continue...");
2022-03-07 07:36:44 +00:00
Console.ReadKey();
Console.Clear();
2022-03-06 13:29:38 +00:00
goto menu;
}
}
2017-12-19 20:33:03 +00:00
}