2017-05-27 20:02:57 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : SCSI.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Dumps media from SCSI devices.
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-05-27 20:02:57 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2017-05-27 20:02:57 +01:00
|
|
|
|
using System;
|
2017-12-19 01:38:11 +00:00
|
|
|
|
using System.Text;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using System.Threading;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
using DiscImageChef.Console;
|
2017-11-20 05:07:16 +00:00
|
|
|
|
using DiscImageChef.Core.Logging;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using DiscImageChef.Decoders.SCSI;
|
|
|
|
|
|
using DiscImageChef.Devices;
|
|
|
|
|
|
using DiscImageChef.Metadata;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
using Schemas;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using MediaType = DiscImageChef.CommonTypes.MediaType;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Core.Devices.Dumping
|
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
public class Scsi
|
2017-05-27 20:02:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
// TODO: Get cartridge serial number from Certance vendor EVPD
|
2017-12-19 20:33:03 +00:00
|
|
|
|
public static void Dump(Device dev, string devicePath, string outputPrefix, ushort retryPasses, bool force,
|
|
|
|
|
|
bool dumpRaw, bool persistent, bool stopOnError, bool separateSubchannel,
|
2017-12-21 14:30:38 +00:00
|
|
|
|
ref Resume resume, ref DumpLog dumpLog, bool dumpLeadIn, Encoding encoding)
|
2017-05-27 20:02:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
MediaType dskType = MediaType.Unknown;
|
2017-11-26 22:20:31 +00:00
|
|
|
|
int resets = 0;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
|
|
|
|
|
|
if(dev.IsRemovable)
|
|
|
|
|
|
{
|
2017-11-26 22:20:31 +00:00
|
|
|
|
deviceGotReset:
|
2017-12-21 23:00:30 +00:00
|
|
|
|
bool sense = dev.ScsiTestUnitReady(out byte[] senseBuf, dev.Timeout, out _);
|
2017-05-27 20:02:57 +01:00
|
|
|
|
if(sense)
|
|
|
|
|
|
{
|
2017-12-21 14:30:38 +00:00
|
|
|
|
FixedSense? decSense = Sense.DecodeFixed(senseBuf);
|
2017-05-27 20:02:57 +01:00
|
|
|
|
if(decSense.HasValue)
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h",
|
|
|
|
|
|
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
|
2017-11-26 22:20:31 +00:00
|
|
|
|
|
|
|
|
|
|
// Just retry, for 5 times
|
|
|
|
|
|
if(decSense.Value.ASC == 0x29)
|
|
|
|
|
|
{
|
|
|
|
|
|
resets++;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(resets < 5) goto deviceGotReset;
|
2017-11-26 22:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-27 20:02:57 +01:00
|
|
|
|
if(decSense.Value.ASC == 0x3A)
|
|
|
|
|
|
{
|
|
|
|
|
|
int leftRetries = 5;
|
|
|
|
|
|
while(leftRetries > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.WriteLine("\rWaiting for drive to become ready");
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Thread.Sleep(2000);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
sense = dev.ScsiTestUnitReady(out senseBuf, dev.Timeout, out _);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(!sense) break;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
|
decSense = Sense.DecodeFixed(senseBuf);
|
2017-11-20 05:07:16 +00:00
|
|
|
|
if(decSense.HasValue)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h",
|
|
|
|
|
|
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
|
2017-05-27 20:02:57 +01:00
|
|
|
|
leftRetries--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(sense)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("Please insert media in drive");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(decSense.Value.ASC == 0x04 && decSense.Value.ASCQ == 0x01)
|
|
|
|
|
|
{
|
|
|
|
|
|
int leftRetries = 10;
|
|
|
|
|
|
while(leftRetries > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.WriteLine("\rWaiting for drive to become ready");
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Thread.Sleep(2000);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
sense = dev.ScsiTestUnitReady(out senseBuf, dev.Timeout, out _);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(!sense) break;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
|
decSense = Sense.DecodeFixed(senseBuf);
|
2017-11-20 05:07:16 +00:00
|
|
|
|
if(decSense.HasValue)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h",
|
|
|
|
|
|
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
|
2017-05-27 20:02:57 +01:00
|
|
|
|
leftRetries--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(sense)
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.ErrorWriteLine("Error testing unit was ready:\n{0}",
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Sense.PrettifySense(senseBuf));
|
2017-05-27 20:02:57 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/*else if (decSense.Value.ASC == 0x29 && decSense.Value.ASCQ == 0x00)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!deviceReset)
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceReset = true;
|
|
|
|
|
|
DicConsole.ErrorWriteLine("Device did reset, retrying...");
|
|
|
|
|
|
goto retryTestReady;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.ErrorWriteLine("Error testing unit was ready:\n{0}", Decoders.SCSI.Sense.PrettifySense(senseBuf));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}*/
|
2017-09-11 18:34:01 +01:00
|
|
|
|
// These should be trapped by the OS but seems in some cases they're not
|
|
|
|
|
|
else if(decSense.Value.ASC == 0x28)
|
|
|
|
|
|
{
|
|
|
|
|
|
int leftRetries = 10;
|
|
|
|
|
|
while(leftRetries > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.WriteLine("\rWaiting for drive to become ready");
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Thread.Sleep(2000);
|
2017-12-21 23:00:30 +00:00
|
|
|
|
sense = dev.ScsiTestUnitReady(out senseBuf, dev.Timeout, out _);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(!sense) break;
|
2017-09-11 18:34:01 +01:00
|
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
|
decSense = Sense.DecodeFixed(senseBuf);
|
2017-11-20 05:07:16 +00:00
|
|
|
|
if(decSense.HasValue)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dumpLog.WriteLine("Device not ready. Sense {0}h ASC {1:X2}h ASCQ {2:X2}h",
|
|
|
|
|
|
decSense.Value.SenseKey, decSense.Value.ASC, decSense.Value.ASCQ);
|
2017-09-11 18:34:01 +01:00
|
|
|
|
leftRetries--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(sense)
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.ErrorWriteLine("Error testing unit was ready:\n{0}",
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Sense.PrettifySense(senseBuf));
|
2017-09-11 18:34:01 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-05-27 20:02:57 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.ErrorWriteLine("Error testing unit was ready:\n{0}",
|
2017-12-21 14:30:38 +00:00
|
|
|
|
Sense.PrettifySense(senseBuf));
|
2017-05-27 20:02:57 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("Unknown testing unit was ready.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CICMMetadataType sidecar = new CICMMetadataType();
|
|
|
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
|
switch(dev.ScsiType) {
|
2017-12-21 14:30:38 +00:00
|
|
|
|
case PeripheralDeviceTypes.SequentialAccess:
|
2017-12-21 04:43:29 +00:00
|
|
|
|
if(dumpRaw) throw new ArgumentException("Tapes cannot be dumped raw.");
|
|
|
|
|
|
|
|
|
|
|
|
Ssc.Dump(dev, outputPrefix, devicePath, ref sidecar, ref resume, ref dumpLog);
|
|
|
|
|
|
return;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
case PeripheralDeviceTypes.MultiMediaDevice:
|
2017-12-21 04:43:29 +00:00
|
|
|
|
Mmc.Dump(dev, devicePath, outputPrefix, retryPasses, force, dumpRaw, persistent, stopOnError,
|
|
|
|
|
|
ref sidecar, ref dskType, separateSubchannel, ref resume, ref dumpLog, dumpLeadIn, encoding);
|
|
|
|
|
|
return;
|
|
|
|
|
|
default:
|
|
|
|
|
|
Sbc.Dump(dev, devicePath, outputPrefix, retryPasses, force, dumpRaw, persistent, stopOnError, ref sidecar,
|
|
|
|
|
|
ref dskType, false, ref resume, ref dumpLog, encoding);
|
|
|
|
|
|
break;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|