2015-10-12 19:55:00 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// 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;
|
2015-10-13 01:45:07 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2015-11-23 04:26:53 +00:00
|
|
|
|
using DiscImageChef.Decoders.ATA;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Devices
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class Device
|
|
|
|
|
|
{
|
2015-10-12 20:08:56 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Opens the device for sending direct commands
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="devicePath">Device path</param>
|
2015-10-12 19:55:00 +01:00
|
|
|
|
public Device(string devicePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
platformID = Interop.DetectOS.GetRealPlatformID();
|
|
|
|
|
|
Timeout = 15;
|
2015-10-13 01:45:07 +01:00
|
|
|
|
error = false;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
removable = false;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2015-10-13 01:45:07 +01:00
|
|
|
|
if (((SafeFileHandle)fd).IsInvalid)
|
|
|
|
|
|
{
|
|
|
|
|
|
error = true;
|
|
|
|
|
|
lastError = Marshal.GetLastWin32Error();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//throw new NotImplementedException();
|
|
|
|
|
|
break;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
case Interop.PlatformID.Linux:
|
|
|
|
|
|
{
|
2015-10-13 01:45:07 +01:00
|
|
|
|
fd = Linux.Extern.open(devicePath, Linux.FileFlags.Readonly | Linux.FileFlags.NonBlocking);
|
|
|
|
|
|
|
|
|
|
|
|
if ((int)fd < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
error = true;
|
|
|
|
|
|
lastError = Marshal.GetLastWin32Error();
|
|
|
|
|
|
}
|
2015-10-12 19:55:00 +01:00
|
|
|
|
|
2015-10-13 01:45:07 +01:00
|
|
|
|
//throw new NotImplementedException();
|
|
|
|
|
|
break;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new InvalidOperationException(String.Format("Platform {0} not yet supported.", platformID));
|
|
|
|
|
|
}
|
2015-10-19 04:46:09 +01:00
|
|
|
|
|
|
|
|
|
|
type = DeviceType.Unknown;
|
2015-10-19 05:20:42 +01:00
|
|
|
|
scsiType = Decoders.SCSI.PeripheralDeviceTypes.UnknownDevice;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
|
|
|
|
|
AtaErrorRegistersCHS errorRegisters;
|
|
|
|
|
|
|
|
|
|
|
|
byte[] ataBuf;
|
2015-11-05 06:50:02 +00:00
|
|
|
|
byte[] senseBuf;
|
|
|
|
|
|
byte[] inqBuf;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
bool scsiSense = ScsiInquiry(out inqBuf, out senseBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-12-31 16:12:22 +00:00
|
|
|
|
#region USB
|
|
|
|
|
|
if(platformID == DiscImageChef.Interop.PlatformID.Linux)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(devicePath.StartsWith("/dev/sd"))
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
|
|
|
2015-12-31 16:33:20 +00:00
|
|
|
|
#region FireWire
|
|
|
|
|
|
if(platformID == DiscImageChef.Interop.PlatformID.Linux)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(devicePath.StartsWith("/dev/sd"))
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
if (!scsiSense)
|
|
|
|
|
|
{
|
|
|
|
|
|
Decoders.SCSI.Inquiry.SCSIInquiry? Inquiry = Decoders.SCSI.Inquiry.Decode(inqBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
type = DeviceType.SCSI;
|
|
|
|
|
|
bool sense = ScsiInquiry(out inqBuf, out senseBuf, 0x80);
|
|
|
|
|
|
if (!sense)
|
|
|
|
|
|
serial = Decoders.SCSI.EVPD.DecodePage80(inqBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
if (Inquiry.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
revision = StringHandlers.SpacePaddedToString(Inquiry.Value.ProductRevisionLevel);
|
|
|
|
|
|
model = StringHandlers.SpacePaddedToString(Inquiry.Value.ProductIdentification);
|
|
|
|
|
|
manufacturer = StringHandlers.SpacePaddedToString(Inquiry.Value.VendorIdentification);
|
2015-12-30 11:45:27 +00:00
|
|
|
|
removable = Inquiry.Value.RMB;
|
2015-10-19 05:20:42 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
scsiType = (Decoders.SCSI.PeripheralDeviceTypes)Inquiry.Value.PeripheralDeviceType;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
}
|
2015-11-05 06:50:02 +00:00
|
|
|
|
|
2015-10-19 05:11:28 +01:00
|
|
|
|
sense = AtapiIdentify(out ataBuf, out errorRegisters);
|
|
|
|
|
|
|
|
|
|
|
|
if (!sense)
|
|
|
|
|
|
{
|
|
|
|
|
|
type = DeviceType.ATAPI;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
Identify.IdentifyDevice? ATAID = Identify.Decode(ataBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
|
|
|
|
|
if (ATAID.HasValue)
|
|
|
|
|
|
serial = ATAID.Value.SerialNumber;
|
|
|
|
|
|
}
|
2015-11-05 06:50:02 +00:00
|
|
|
|
}
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
if (scsiSense || manufacturer == "ATA")
|
|
|
|
|
|
{
|
|
|
|
|
|
bool sense = AtaIdentify(out ataBuf, out errorRegisters);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
if (!sense)
|
|
|
|
|
|
{
|
2015-11-05 06:50:02 +00:00
|
|
|
|
type = DeviceType.ATA;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
Identify.IdentifyDevice? ATAID = Identify.Decode(ataBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
if (ATAID.HasValue)
|
2015-10-19 05:11:28 +01:00
|
|
|
|
{
|
2015-11-05 06:50:02 +00:00
|
|
|
|
string[] separated = ATAID.Value.Model.Split(' ');
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
if (separated.Length == 1)
|
|
|
|
|
|
model = separated[0];
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
manufacturer = separated[0];
|
|
|
|
|
|
model = separated[separated.Length - 1];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
revision = ATAID.Value.FirmwareRevision;
|
|
|
|
|
|
serial = ATAID.Value.SerialNumber;
|
2015-10-19 05:20:42 +01:00
|
|
|
|
|
2015-11-05 06:50:02 +00:00
|
|
|
|
scsiType = Decoders.SCSI.PeripheralDeviceTypes.DirectAccess;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
|
|
|
|
|
|
if ((ushort)ATAID.Value.GeneralConfiguration != 0x848A)
|
|
|
|
|
|
{
|
|
|
|
|
|
removable |= (ATAID.Value.GeneralConfiguration & Identify.GeneralConfigurationBit.Removable) == Identify.GeneralConfigurationBit.Removable;
|
|
|
|
|
|
}
|
2015-10-19 05:11:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (type == DeviceType.Unknown)
|
|
|
|
|
|
{
|
|
|
|
|
|
manufacturer = null;
|
|
|
|
|
|
model = null;
|
|
|
|
|
|
revision = null;
|
|
|
|
|
|
serial = null;
|
|
|
|
|
|
}
|
2015-12-31 16:12:22 +00:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-12-31 16:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-10-12 19:55:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|