Files
Aaru/DiscImageChef.Devices/Device/Constructor.cs
Natalia Portillo d777061105 * commandline:
* DiscImageChef.Settings/Settings.cs:
	* DiscImageChef.Settings/docs/README.txt:
	* DiscImageChef.Settings/packages.config:
	* DiscImageChef.Settings/docs/LICENSE.txt:
	* DiscImageChef.Settings/docs/ChangeLog.txt:
	* DiscImageChef.Settings/docs/mono/index.xml:
	* DiscImageChef.Settings/docs/html/index.html:
	* DiscImageChef.Settings/Properties/AssemblyInfo.cs:
	* DiscImageChef.Settings/DiscImageChef.Settings.csproj:
	* DiscImageChef.Settings/docs/mono/ns-Claunia.PropertyList.xml:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/UID.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/UID.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSSet.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/index.html:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSSet.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSDate.xml:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSData.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSDate.html:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSData.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSArray.xml:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSNumber.xml:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSString.xml:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSObject.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSArray.html:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSNumber.html:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSString.html:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSObject.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/NSDictionary.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/NSDictionary.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/PropertyListParser.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/PropertyListParser.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/XmlPropertyListParser.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/XmlPropertyListParser.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/ASCIIPropertyListParser.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/ASCIIPropertyListParser.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/BinaryPropertyListParser.xml:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/BinaryPropertyListWriter.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/BinaryPropertyListWriter.html:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/BinaryPropertyListParser.html:
	* DiscImageChef.Settings/docs/mono/Claunia.PropertyList/PropertyListFormatException.xml:
	* DiscImageChef.Settings/docs/html/Claunia.PropertyList/PropertyListFormatException.html:
	  Added supports for settings

	* DiscImageChef/Commands/Configure.cs:
	  Added support for settings.

	* DiscImageChef/Core/Statistics.cs:
	* DiscImageChef/Commands/Verify.cs:
	* DiscImageChef/Commands/Entropy.cs:
	* DiscImageChef/Commands/Formats.cs:
	* DiscImageChef/Commands/PrintHex.cs:
	* DiscImageChef/Commands/MediaInfo.cs:
	* DiscImageChef/Commands/Statistics.cs:
	  Added statistics.

	* DiscImageChef.Decoders/SCSI/Inquiry.cs:
	  Corrected bug on inquiry decoding.

	* DiscImageChef.Decoders/SCSI/Modes.cs:
	  Corrected bug on decoding mode page 2Ah without write
	  performance descriptors.
	Corrected bug when there is a vendor page 0 in mode sense
	  decoding.

	* DiscImageChef.Devices/Device/Constructor.cs:
	  Corrected detecting USB or FireWire attached CD/DVD/BD and
	  tape drives.
	Try ATA identify on USB or FireWire that don't have SCSI
	  INQUIRY.

	* DiscImageChef.DiscImages/CDRWin.cs:
	  Corrected CD-ROM XA vs CD-ROM detection.

	* DiscImageChef.Partitions/AppleMap.cs:
	  Corrected big endian working.
	Added debug output.

	* DiscImageChef.sln:
	  Added supports for settings.

	* DiscImageChef/Commands/Decode.cs:
	* DiscImageChef/Commands/Analyze.cs:
	* DiscImageChef/Commands/Compare.cs:
	* DiscImageChef/Commands/Checksum.cs:
	* DiscImageChef/Commands/Benchmark.cs:
	* DiscImageChef/Commands/DeviceInfo.cs:
	* DiscImageChef/Commands/CreateSidecar.cs:
	  Added statistics.

	* DiscImageChef/Commands/DeviceReport.cs:
	  Added statistics.
	Correct handling empty inquiry string fields.
	Suppose it is not removable, til proved wrong.
	Corrected MODE SENSE (6/10) detection and calling order.
	If device is MMC type but reports neither mode page 2Ah
	  neither GET CONFIGURATION, try all CDs (old drives work like
	  that).
	Try reading Lead-In and Lead-Out in Audio CD using Audio READ
	  CD commands.
	Corrected READ LONG information handling, some drives return
	  2s-complement in 32 bit. Upper 16 bits are ignored.
	Added support for DVD raw block (37856 bytes).
	Check READ LONG up to 36 times the cooked block size. That
	  should be enough to detect huge blocked media (like DVD and
	  BD) without taking ages.
	If READ LONG size had to be bruteforced, and debug is
	  activated, save the result.

	* DiscImageChef/Commands/DumpMedia.cs:
	  Added statistics.
	Corrected READ LONG information handling, some drives return
	  2s-complement in 32 bit. Upper 16 bits are ignored.
	Start trying with 64 blocks at a time. Some drives report to
	  be able to read 255 at a time, but they really don't, they
	  take a lot longer to read.

	* DiscImageChef/Commands/MediaScan.cs:
	  Added statistics.
	Start trying with 64 blocks at a time. Some drives report to
	  be able to read 255 at a time, but they really don't, they
	  take a lot longer to read.

	* DiscImageChef/DiscImageChef.csproj:
	  Added support for settings.
	Added statistics.

	* DiscImageChef/Main.cs:
	* DiscImageChef/Options.cs:
	  Added support for settings.
	Added statistics.
2016-02-03 18:58:11 +00:00

366 lines
16 KiB
C#

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Device.cs
// Version : 1.0
// Author(s) : Natalia Portillo
//
// Component : Component
//
// Revision : $Revision$
// Last change by : $Author$
// Date : $Date$
//
// --[ Description ] ----------------------------------------------------------
//
// Description
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright (C) 2011-2015 Claunia.com
// ****************************************************************************/
// //$Id$
using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using DiscImageChef.Decoders.ATA;
namespace DiscImageChef.Devices
{
public partial class Device
{
/// <summary>
/// Opens the device for sending direct commands
/// </summary>
/// <param name="devicePath">Device path</param>
public Device(string devicePath)
{
platformID = Interop.DetectOS.GetRealPlatformID();
Timeout = 15;
error = false;
removable = false;
switch (platformID)
{
case Interop.PlatformID.Win32NT:
{
fd = Windows.Extern.CreateFile(devicePath,
Windows.FileAccess.GenericRead | Windows.FileAccess.GenericWrite,
Windows.FileShare.Read | Windows.FileShare.Write,
IntPtr.Zero, Windows.FileMode.OpenExisting,
Windows.FileAttributes.Normal, IntPtr.Zero);
if (((SafeFileHandle)fd).IsInvalid)
{
error = true;
lastError = Marshal.GetLastWin32Error();
}
//throw new NotImplementedException();
break;
}
case Interop.PlatformID.Linux:
{
fd = Linux.Extern.open(devicePath, Linux.FileFlags.Readonly | Linux.FileFlags.NonBlocking);
if ((int)fd < 0)
{
error = true;
lastError = Marshal.GetLastWin32Error();
}
//throw new NotImplementedException();
break;
}
default:
throw new InvalidOperationException(String.Format("Platform {0} not yet supported.", platformID));
}
if (error)
throw new SystemException(String.Format("Error {0} opening device.", lastError));
type = DeviceType.Unknown;
scsiType = Decoders.SCSI.PeripheralDeviceTypes.UnknownDevice;
AtaErrorRegistersCHS errorRegisters;
byte[] ataBuf;
byte[] senseBuf;
byte[] inqBuf;
bool scsiSense = ScsiInquiry(out inqBuf, out senseBuf);
if (error)
throw new SystemException(String.Format("Error {0} trying device.", lastError));
#region USB
if(platformID == DiscImageChef.Interop.PlatformID.Linux)
{
if(devicePath.StartsWith("/dev/sd") || devicePath.StartsWith("/dev/sr") || devicePath.StartsWith("/dev/st"))
{
string devPath = devicePath.Substring(5);
if(System.IO.Directory.Exists("/sys/block/" + devPath))
{
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
resolvedLink = "/sys" + resolvedLink.Substring(2);
if(!string.IsNullOrEmpty(resolvedLink))
{
while(resolvedLink.Contains("usb"))
{
resolvedLink = System.IO.Path.GetDirectoryName(resolvedLink);
if(System.IO.File.Exists(resolvedLink + "/descriptors") &&
System.IO.File.Exists(resolvedLink + "/idProduct") &&
System.IO.File.Exists(resolvedLink + "/idVendor"))
{
System.IO.FileStream usbFs;
System.IO.StreamReader usbSr;
string usbTemp;
usbFs = new System.IO.FileStream(resolvedLink + "/descriptors", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] usbBuf = new byte[65536];
int usbCount = usbFs.Read(usbBuf, 0, 65536);
usbDescriptors = new byte[usbCount];
Array.Copy(usbBuf, 0, usbDescriptors, 0, usbCount);
usbFs.Close();
usbSr = new System.IO.StreamReader(resolvedLink + "/idProduct");
usbTemp = usbSr.ReadToEnd();
ushort.TryParse(usbTemp, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out usbProduct);
usbSr.Close();
usbSr = new System.IO.StreamReader(resolvedLink + "/idVendor");
usbTemp = usbSr.ReadToEnd();
ushort.TryParse(usbTemp, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out usbVendor);
usbSr.Close();
if(System.IO.File.Exists(resolvedLink + "/manufacturer"))
{
usbSr = new System.IO.StreamReader(resolvedLink + "/manufacturer");
usbManufacturerString = usbSr.ReadToEnd().Trim();
usbSr.Close();
}
if(System.IO.File.Exists(resolvedLink + "/product"))
{
usbSr = new System.IO.StreamReader(resolvedLink + "/product");
usbProductString = usbSr.ReadToEnd().Trim();
usbSr.Close();
}
if(System.IO.File.Exists(resolvedLink + "/serial"))
{
usbSr = new System.IO.StreamReader(resolvedLink + "/serial");
usbSerialString = usbSr.ReadToEnd().Trim();
usbSr.Close();
}
usb = true;
break;
}
}
}
}
}
}
// TODO: Implement for other operating systems
else
usb = false;
#endregion USB
#region FireWire
if(platformID == DiscImageChef.Interop.PlatformID.Linux)
{
if(devicePath.StartsWith("/dev/sd") || devicePath.StartsWith("/dev/sr") || devicePath.StartsWith("/dev/st"))
{
string devPath = devicePath.Substring(5);
if(System.IO.Directory.Exists("/sys/block/" + devPath))
{
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
resolvedLink = "/sys" + resolvedLink.Substring(2);
if(!string.IsNullOrEmpty(resolvedLink))
{
while(resolvedLink.Contains("firewire"))
{
resolvedLink = System.IO.Path.GetDirectoryName(resolvedLink);
if(System.IO.File.Exists(resolvedLink + "/model") &&
System.IO.File.Exists(resolvedLink + "/vendor") &&
System.IO.File.Exists(resolvedLink + "/guid"))
{
System.IO.StreamReader fwSr;
string fwTemp;
fwSr = new System.IO.StreamReader(resolvedLink + "/model");
fwTemp = fwSr.ReadToEnd();
uint.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out firewireModel);
fwSr.Close();
fwSr = new System.IO.StreamReader(resolvedLink + "/vendor");
fwTemp = fwSr.ReadToEnd();
uint.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out firewireVendor);
fwSr.Close();
fwSr = new System.IO.StreamReader(resolvedLink + "/guid");
fwTemp = fwSr.ReadToEnd();
ulong.TryParse(fwTemp, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out firewireGuid);
fwSr.Close();
if(System.IO.File.Exists(resolvedLink + "/model_name"))
{
fwSr = new System.IO.StreamReader(resolvedLink + "/model_name");
firewireModelName = fwSr.ReadToEnd().Trim();
fwSr.Close();
}
if(System.IO.File.Exists(resolvedLink + "/vendor_name"))
{
fwSr = new System.IO.StreamReader(resolvedLink + "/vendor_name");
firewireVendorName = fwSr.ReadToEnd().Trim();
fwSr.Close();
}
firewire = true;
break;
}
}
}
}
}
}
// TODO: Implement for other operating systems
else
firewire = false;
#endregion FireWire
if (!scsiSense)
{
Decoders.SCSI.Inquiry.SCSIInquiry? Inquiry = Decoders.SCSI.Inquiry.Decode(inqBuf);
type = DeviceType.SCSI;
bool serialSense = ScsiInquiry(out inqBuf, out senseBuf, 0x80);
if (!serialSense)
serial = Decoders.SCSI.EVPD.DecodePage80(inqBuf);
if (Inquiry.HasValue)
{
string tmp = StringHandlers.CToString(Inquiry.Value.ProductRevisionLevel);
if(tmp != null)
revision = tmp.Trim();
tmp = StringHandlers.CToString(Inquiry.Value.ProductIdentification);
if(tmp != null)
model = tmp.Trim();
tmp = StringHandlers.CToString(Inquiry.Value.VendorIdentification);
if(tmp != null)
manufacturer = tmp.Trim();
removable = Inquiry.Value.RMB;
scsiType = (Decoders.SCSI.PeripheralDeviceTypes)Inquiry.Value.PeripheralDeviceType;
}
bool atapiSense = AtapiIdentify(out ataBuf, out errorRegisters);
if (!atapiSense)
{
type = DeviceType.ATAPI;
Identify.IdentifyDevice? ATAID = Identify.Decode(ataBuf);
if (ATAID.HasValue)
serial = ATAID.Value.SerialNumber;
}
}
if ((scsiSense && (usb || firewire)) || manufacturer == "ATA")
{
bool ataSense = AtaIdentify(out ataBuf, out errorRegisters);
if (!ataSense)
{
type = DeviceType.ATA;
Identify.IdentifyDevice? ATAID = Identify.Decode(ataBuf);
if (ATAID.HasValue)
{
string[] separated = ATAID.Value.Model.Split(' ');
if (separated.Length == 1)
model = separated[0];
else
{
manufacturer = separated[0];
model = separated[separated.Length - 1];
}
revision = ATAID.Value.FirmwareRevision;
serial = ATAID.Value.SerialNumber;
scsiType = Decoders.SCSI.PeripheralDeviceTypes.DirectAccess;
if ((ushort)ATAID.Value.GeneralConfiguration != 0x848A)
{
removable |= (ATAID.Value.GeneralConfiguration & Identify.GeneralConfigurationBit.Removable) == Identify.GeneralConfigurationBit.Removable;
}
}
}
}
if (type == DeviceType.Unknown)
{
manufacturer = null;
model = null;
revision = null;
serial = null;
}
if (usb)
{
if (string.IsNullOrEmpty(manufacturer))
manufacturer = usbManufacturerString;
if (string.IsNullOrEmpty(model))
model = usbProductString;
if (string.IsNullOrEmpty(serial))
serial = usbSerialString;
else
{
foreach (char c in serial)
{
if(Char.IsControl(c))
serial = usbSerialString;
}
}
}
if (firewire)
{
if (string.IsNullOrEmpty(manufacturer))
manufacturer = firewireVendorName;
if (string.IsNullOrEmpty(model))
model = firewireModelName;
if (string.IsNullOrEmpty(serial))
serial = String.Format("{0:X16}", firewireGuid);
else
{
foreach (char c in serial)
{
if(Char.IsControl(c))
serial = String.Format("{0:X16}", firewireGuid);
}
}
}
}
}
}